mirror of
https://github.com/anoma/juvix.git
synced 2024-12-01 00:04:58 +03:00
e9284b3ef6
* Closes #1992 A function identifier `fun` can be declared as an iterator with ``` syntax iterator fun; ``` For example: ```haskell syntax iterator for; for : {A B : Type} -> (A -> B -> A) -> A -> List B -> List A; for f acc nil := acc; for f acc (x :: xs) := for (f acc x) xs; ``` Iterator application syntax allows for a finite number of initializers `acc := a` followed by a finite number of ranges `x in xs`. For example: ``` for (acc := 0) (x in lst) acc + x ``` The number of initializers plus the number of ranges must be non-zero. An iterator application ``` fun (acc1 := a1; ..; accn := an) (x1 in b1; ..; xk in bk) body ``` gets desugared to ``` fun \{acc1 .. accn x1 .. xk := body} a1 .. an b1 .. bk ``` The `acc1`, ..., `accn`, `x1`, ..., `xk` can be patterns. The desugaring works on a purely syntactic level. Without further restrictions, it is not checked if the number of initializers/ranges matches the type of the identifier. The restrictions on the number of initializers/ranges can be specified in iterator declaration: ``` syntax iterator fun {init: n, range: k}; syntax iterator for {init: 1, range: 1}; syntax iterator map {init: 0, range: 1}; ``` The attributes (`init`, `range`) in between braces are parsed as YAML to avoid inventing and parsing a new attribute language. Both attributes are optional.
15 lines
249 B
Plaintext
15 lines
249 B
Plaintext
module Iterators4;
|
|
|
|
syntax iterator map;
|
|
syntax iterator map {init: 0};
|
|
map : {A B : Type} → (A → B) → A → B;
|
|
map f x := f x;
|
|
|
|
builtin bool
|
|
type Bool :=
|
|
| true : Bool
|
|
| false : Bool;
|
|
|
|
main : Bool;
|
|
main := map (y := false) (x in true) x;
|