1
1
mirror of https://github.com/anoma/juvix.git synced 2024-12-15 01:52:11 +03:00
juvix/tests/positive/Syntax.juvix
Jan Mas Rovira 8daca2ba0a
New fixity/iterator syntax (#2332)
- Closes #2330
- Closes #2329 

This pr implements the syntax changes described in #2330. It drops
support for the old yaml-based syntax.
Some valid examples:
```
syntax iterator for {init := 1; range := 1};

syntax fixity cons := binary {assoc := right};
syntax fixity cmp := binary;
syntax fixity cmp := binary {}; -- debatable whether we want to accept empty {} or not. I think we should
```
# Future work
This pr creates an asymmetry between iterators and operators
definitions. Iterators definition do not require a constructor. We could
add it to make it homogeneous, but it looks a bit redundant:
```
syntax iterator for := mkIterator {init := 1; range := 1};
```

We could consider merging iterator and fixity declarations with this
alternative syntax.
```
syntax XXX for := iterator {init := 1; range := 1};

syntax XXX cons := binary {assoc := right};
```
where `XXX` is a common keyword. Suggestion by @lukaszcz XXX = declare

---------

Co-authored-by: Łukasz Czajka <62751+lukaszcz@users.noreply.github.com>
Co-authored-by: Lukasz Czajka <lukasz@heliax.dev>
2023-09-14 10:57:38 +02:00

63 lines
1.2 KiB
Plaintext

module Syntax;
compose {A B C : Type} (f : B -> C) (g : A -> B) (x : A)
: C := f (g x);
compose' {A B C : Type} (f : B -> C) (g : A -> B) : A -> C
| x := f (g x);
type Bool :=
| false : Bool
| true : Bool;
type Nat :=
| zero : Nat
| suc : Nat -> Nat;
not : Bool -> Bool
| false := true
| true := false;
even : Nat -> Bool
| zero := true
| (suc n) := odd n;
odd : Nat -> Bool
| zero := false
| (suc n) := even n;
syntax fixity cmp := binary {};
syntax operator ==1 cmp;
==1 : Nat -> Nat -> Bool
| zero zero := true
| (suc a) (suc b) := a ==2 b
| _ _ := false;
-- note that ==2 is used before its infix definition
syntax operator ==2 cmp;
==2 : Nat -> Nat -> Bool
| zero zero := true
| (suc a) (suc b) := a ==1 b
| _ _ := false;
module MutualTypes;
-- we use Tree and isEmpty before their definition
isNotEmpty {a : Type} (t : Tree a) : Bool :=
not (isEmpty t);
isEmpty {a : Type} : (t : Tree a) -> Bool
| empty := true
| (node _ _) := false;
type Tree (a : Type) :=
| empty : Tree a
| node : a -> Forest a -> Tree a;
type Forest (a : Type) :=
| nil : Forest a
| cons : Tree a -> Forest a;
end;