1
1
mirror of https://github.com/anoma/juvix.git synced 2024-11-30 14:13:27 +03:00
juvix/tests/Compilation/positive/test061.juvix
Łukasz Czajka 5f803ec6c2
Named arguments for record projections (#3173)
* Closes #3054
* Adds named argument signatures for generated record projections.
2024-11-15 18:25:49 +01:00

108 lines
2.5 KiB
Plaintext

--- Traits
module test061;
import Stdlib.Prelude open hiding {Show; mkShow; module Show};
trait
type Show A := mkShow {show : A → String};
Show' : Type -> Type := Show;
Bool' : Type := Bool;
instance
showStringI : Show String :=
mkShow@?{
show := id
};
instance
showBoolI : Show' Bool' :=
mkShow@{
show (x : Bool) : String := ite x "true" "false"
};
instance
showNatI : Show Nat :=
mkShow@?{
show := natToString
};
g : {A : Type} → {{Show A}} → Nat := 5;
instance
showListI {A} {{Show' A}} : Show (List A) :=
mkShow@{
show : List A → String
| nil := "nil"
| (h :: t) := Show.show h ++str " :: " ++str show t
};
instance
showMaybeI {A} {{Show A}} : Show (Maybe A) :=
mkShow@{
show : Maybe A → String
| (just x) := "just (" ++str Show.show x ++str ")"
| nothing := "nothing"
};
f {A} {{Show A}} : A → String
| x := Show.show x;
f' {A} : {{Show A}} → A → String
| {{mkShow s}} x := s x;
f'' {A} : {{Show A}} → A → String
| {{M}} x := Show.show {{M}} x;
f'3 {A} {{M : Show A}} : A → String := Show.show {{M}};
f'4 {A} {{_ : Show A}} : A → String := Show.show;
f5 {A} {{Show' A}} (n : String) (a : A) : String := n ++str Show.show a;
instance
showBoolFunI : Show (Bool → Bool) :=
mkShow@{
show (f : Bool → Bool) : String :=
let
b1 : Bool := f true;
b2 : Bool := f false;
in "\\{ true := " ++str Show.show b1 ++str " | false := " ++str Show.show b2 ++str " }"
};
instance
showPairI {A B} {{Show A}} {{Show' B}} : Show' (Pair A B) :=
mkShow λ {(x, y) := Show.show x ++str ", " ++str Show.show y};
trait
type T A := mkT {a : A};
instance
tNatI : T Nat := mkT 0;
instance
tFunI {A} {{T A}} : T (A -> A) := mkT \ {a := a};
main : IO :=
printStringLn (Show.show true)
>>> printStringLn (f false)
>>> printStringLn (Show.show 3)
>>> printStringLn (Show.show (g {Nat}))
>>> printStringLn (Show.show [true; false])
>>> printStringLn (Show.show [1; 2; 3])
>>> printStringLn (f' [1; 2])
>>> printStringLn (f'' [true; false])
>>> printStringLn (f'3 [just true; nothing; just false])
>>> printStringLn (f'4 [just [1]; nothing; just [2; 3]])
>>> printStringLn (f'3 "abba")
>>> printStringLn
(f'3@?{
M := mkShow λ {x := x ++str "!"}
}
"abba")
>>> printStringLn (f5 "abba" 3)
>>> printStringLn (Show.show {{_}} ["a"; "b"; "c"; "d"])
>>> printStringLn (Show.show λ {x := not x})
>>> printStringLn (Show.show (3, [1; 2 + T.a 0]));