2023-09-08 13:16:43 +03:00
|
|
|
--- Traits
|
|
|
|
module test061;
|
|
|
|
|
2023-09-13 15:48:25 +03:00
|
|
|
import Stdlib.Prelude open hiding {Show; mkShow; module Show};
|
2023-09-08 13:16:43 +03:00
|
|
|
|
|
|
|
trait
|
2023-09-28 18:07:38 +03:00
|
|
|
type Show A := mkShow {show : A → String};
|
2023-09-08 13:16:43 +03:00
|
|
|
|
2024-05-15 15:29:44 +03:00
|
|
|
Show' : Type -> Type := Show;
|
2024-07-12 19:31:09 +03:00
|
|
|
|
2024-05-15 15:29:44 +03:00
|
|
|
Bool' : Type := Bool;
|
|
|
|
|
2023-09-08 13:16:43 +03:00
|
|
|
instance
|
2024-07-12 19:31:09 +03:00
|
|
|
showStringI : Show String :=
|
|
|
|
mkShow@?{
|
|
|
|
show := id
|
|
|
|
};
|
2023-09-08 13:16:43 +03:00
|
|
|
|
|
|
|
instance
|
2024-05-15 15:29:44 +03:00
|
|
|
showBoolI : Show' Bool' :=
|
2023-09-28 18:07:38 +03:00
|
|
|
mkShow@{
|
2024-07-02 11:03:06 +03:00
|
|
|
show (x : Bool) : String := ite x "true" "false"
|
2023-09-28 18:07:38 +03:00
|
|
|
};
|
2023-09-08 13:16:43 +03:00
|
|
|
|
|
|
|
instance
|
2024-07-12 19:31:09 +03:00
|
|
|
showNatI : Show Nat :=
|
|
|
|
mkShow@?{
|
|
|
|
show := natToString
|
|
|
|
};
|
2023-09-08 13:16:43 +03:00
|
|
|
|
|
|
|
g : {A : Type} → {{Show A}} → Nat := 5;
|
|
|
|
|
|
|
|
instance
|
2024-05-15 15:29:44 +03:00
|
|
|
showListI {A} {{Show' A}} : Show (List A) :=
|
2023-09-28 18:07:38 +03:00
|
|
|
mkShow@{
|
2024-11-15 20:25:49 +03:00
|
|
|
show : List A → String
|
2023-09-28 18:07:38 +03:00
|
|
|
| nil := "nil"
|
|
|
|
| (h :: t) := Show.show h ++str " :: " ++str show t
|
|
|
|
};
|
2023-09-08 13:16:43 +03:00
|
|
|
|
|
|
|
instance
|
2023-09-28 18:07:38 +03:00
|
|
|
showMaybeI {A} {{Show A}} : Show (Maybe A) :=
|
|
|
|
mkShow@{
|
2024-11-15 20:25:49 +03:00
|
|
|
show : Maybe A → String
|
2023-09-28 18:07:38 +03:00
|
|
|
| (just x) := "just (" ++str Show.show x ++str ")"
|
|
|
|
| nothing := "nothing"
|
|
|
|
};
|
2023-09-08 13:16:43 +03:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2024-07-12 19:31:09 +03:00
|
|
|
f5 {A} {{Show' A}} (n : String) (a : A) : String := n ++str Show.show a;
|
2023-09-25 15:08:55 +03:00
|
|
|
|
|
|
|
instance
|
2024-07-12 19:31:09 +03:00
|
|
|
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 " }"
|
|
|
|
};
|
2023-09-25 15:08:55 +03:00
|
|
|
|
2023-09-29 14:08:05 +03:00
|
|
|
instance
|
2024-06-26 11:23:35 +03:00
|
|
|
showPairI {A B} {{Show A}} {{Show' B}} : Show' (Pair A B) :=
|
2023-09-29 14:08:05 +03:00
|
|
|
mkShow λ {(x, y) := Show.show x ++str ", " ++str Show.show y};
|
|
|
|
|
2023-10-04 13:00:48 +03:00
|
|
|
trait
|
2024-07-12 19:31:09 +03:00
|
|
|
type T A := mkT {a : A};
|
2023-10-04 13:00:48 +03:00
|
|
|
|
|
|
|
instance
|
|
|
|
tNatI : T Nat := mkT 0;
|
|
|
|
|
|
|
|
instance
|
2024-07-12 19:31:09 +03:00
|
|
|
tFunI {A} {{T A}} : T (A -> A) := mkT \ {a := a};
|
2023-10-04 13:00:48 +03:00
|
|
|
|
2023-09-08 13:16:43 +03:00
|
|
|
main : IO :=
|
2024-07-12 19:31:09 +03:00
|
|
|
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]));
|