Replies: 4 comments
|
This would also enable popping from arrays while maintaining static memory allocation |
0 replies
|
In many cases where one would use type level arithmetic you can just use python wrappers instead: clunky but workable from guppylang import guppy
from guppylang.std.builtins import array, py, result
from guppylang.defs import GuppyFunctionDefinition
T = guppy.type_var("T", copyable=True)
def n_double(n: int) -> GuppyFunctionDefinition:
@guppy
def foo(ar: array[T, py(n)]) -> array[T, py(2*n)]:
out = array(ar[i%py(n)] for i in range(py(2*n)))
return out
return foo
double3 = n_double(3)
@guppy
def bar() -> None:
a = array(0 for _ in range(3))
a = double3(a)
result("a", a)
bar.check() |
0 replies
|
In guppy-algorithms, I have experimented with a templating decorator to carry out monomorphisation in python to allow:
I decided not to merge or use it, so I wanted to write a record of it here. @guppy_template(replacements={"n": range(2, 50)})and this decorator is identical to |
0 replies
|
Many "template" like use cases outlined here should be addressed by #1299 |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Users have asked for generic types like
array[qubit, n + 1]. This are some thoughts on how we could achieve that.Proposal
Allow arbitrary arithmetic expressions made up of
+, -, *, //, **as type args. Arithmetic expressions only unify if they are exactly the same (in the future, we could make this smarter by implementing some kind of normalisation strategy).However, users may convince the type checker by adding explicit asserts:
These asserts are checked during monomorphisation:
The second call to
badwould fail during monomorphisation.Pros
Cons
if, but this would still fail during monomorphisation since theifguard is not taken into account. To support this, we would need a length cast that is executed at run-time rather than compile-time. To explain this to users, we should probably frame this feature as "Zig/C++ templates" rather than "dependent types".Questions
Should we monomorphise in Guppy or Hugr? Doing it in Hugr would require generalising array ops (e.g.
pop_left: arrar<N, T> -> T, array<M, T>) and adding casts. Doing it in Guppy would slow down the compiler but allow for nicer error messages.Alternatives
We could restrict to a decidable fragment of arithmetic (e.g. Presbuger arithmetic or Brat's arithmetic language). However, my feeling is that users would not be super happy with that.
All reactions