1
1
mirror of https://github.com/anoma/juvix.git synced 2024-11-30 05:42:26 +03:00
juvix/tests/Rust/Compilation/positive/test060.juvix
Łukasz Czajka 3030196fdd Non-recursive definitions (#3138)
* Closes #2968 
* Implements detection of function-like definitions, which either:
  - have some arguments on the left of `:`, or
  - have at least one clause.
* Only function-like definitions are recursive.
* Non-recursive definitions are not mutually recursive either, and can
be used only after their definition. This necessitates rearranging some
definitions in existing Juvix code.
* Changes the scoping of identifiers in record updates. Now field names
on the right side don't refer to the old values of the record fields but
to identifiers in scope defined outside the record update. To refer to
old values, one needs to explicitly use record projections, e.g.
```
r@Rec{x := Rec.x r}
```
2024-11-04 18:18:39 +01:00

49 lines
1009 B
Plaintext

-- record updates and record patterns
module test060;
import Stdlib.Prelude open hiding {fst};
type Triple (A B C : Type) :=
mkTriple {
fst : A;
snd : B;
thd : C
};
type Pair' (A B : Type) :=
mkPair {
fst : A;
snd : B
};
sum : Triple Nat Nat Nat → Nat
| mkTriple@{fst; snd; thd} := fst + snd + thd;
mf : Pair' (Pair' Bool (List Nat)) (List Nat) → Bool
| mkPair@{fst := mkPair@{fst; snd := nil};
snd := zero :: _} := fst
| x := case x of {_ := false};
main : Nat :=
let
p : Triple Nat Nat Nat := mkTriple 2 2 2;
p' : Triple Nat Nat Nat :=
p@Triple{
fst := Triple.fst p + 1;
snd := Triple.snd p * 3 + Triple.thd p + Triple.fst p
};
f : Triple Nat Nat Nat -> Triple Nat Nat Nat :=
\{p := p@Triple{fst := Triple.fst p * 10}};
in sum <| ite
(mf
mkPair@{
fst := mkPair true nil;
snd := 0 :: nil
})
(f p')
mkTriple@{
fst := 0;
thd := 0;
snd := 0
};