···11+def main : IO Unit :=
22+ IO.println "Hello, world!"
33+44+
+155
Learn-lean/DepType.lean
···11+namespace Intro
22+33+/-
44+ Lean natural numbers are arbitrary precision unsigned integers.
55+ Type is an abbrev. for Type 0 (universe of types)
66+-/
77+88+constant a : Nat
99+constant b : Nat
1010+1111+constant f : Nat → Nat
1212+constant t: Nat × Nat
1313+constant T: Prod Nat Nat
1414+constant F: (Nat -> Nat) -> Nat
1515+1616+#check a
1717+#check a + 4
1818+#check f
1919+#check t.1
2020+#check T.fst
2121+#check F f
2222+2323+#check Nat
2424+#check Nat -> Bool
2525+2626+constant α : Type
2727+constant 𝔽 : Type -> Type
2828+2929+#check 𝔽 α
3030+#check 𝔽 Bool
3131+#check Prod α Nat
3232+3333+#check Type
3434+#check List
3535+#check Prod
3636+3737+constant C : Type _
3838+universe U
3939+constant D : Type U
4040+#check C
4141+#check D
4242+4343+-- fn :: a -> a
4444+-- fn a = a
4545+def fn (α : Type _)(a : α) := a
4646+#check fn
4747+end Intro
4848+4949+namespace Fun_Abstraction
5050+5151+#check fun (x: Nat) => x + 5
5252+#check λ (α : Type _) (a: α) => a
5353+5454+constant f : Nat -> Bool -> Bool
5555+constant h : Nat -> Bool
5656+#check fun x y => f x (h y)
5757+5858+/-alpha equivalence-/
5959+constant b: Bool
6060+#check (fun (α β : Type) (b: β) (x: α) => b) Nat Bool
6161+#reduce (fun (α β : Type) (b: β) (x: α) => b) Nat Bool
6262+end Fun_Abstraction
6363+6464+/-definitions-/
6565+constant Bar : Nat -> Nat -> Nat
6666+constant x : Nat
6767+constant y : Nat
6868+def bar (x : Nat) (y : Nat) : Nat := x + y
6969+7070+/-this approach of defining functions is better-/
7171+def barr : Nat -> Nat -> Nat :=
7272+ fun x y => x + y
7373+7474+#check Bar
7575+#check Bar x y
7676+#reduce Bar x y
7777+#check bar
7878+#print bar
7979+#eval bar 1 2
8080+#reduce bar
8181+8282+#check barr
8383+#reduce barr
8484+#eval barr 2 3
8585+#print barr
8686+8787+def f (α : Type u) (β : α -> Type v) (a : α) (b: β a) : (a : α) × β a := ⟨ a, b ⟩
8888+8989+#reduce f
9090+#reduce (f Type (fun α => α) Nat 10).2
9191+9292+section
9393+variable (α β γ : Type)
9494+variable (g: β -> γ) (f: α -> β) (h: α -> α)
9595+variable (x: α)
9696+9797+def compose := g (f x)
9898+def doTwice := h (h x)
9999+def doThrice := h (h (h x))
100100+101101+#print compose
102102+#print doTwice
103103+#print doThrice
104104+end
105105+106106+namespace typeclasses
107107+108108+class Add(α : Type) where
109109+ add : α → α → α
110110+111111+#check @Add.add
112112+113113+constant el : Type u -> Type u
114114+namespace el
115115+universe u
116116+117117+inductive nat: Type u where
118118+| z : nat
119119+| succ : nat -> nat
120120+121121+#check nat.succ nat.z
122122+123123+def add (a: nat) (b: nat): nat :=
124124+ match a with
125125+ | nat.z => b
126126+ | nat.succ x => nat.succ (add x b)
127127+128128+#reduce add (nat.succ nat.z) (nat.succ nat.z)
129129+130130+constant F : { a : Type u } -> List a -> Type u
131131+132132+constant a : Type v
133133+def b := List (List (Type u))
134134+#check b
135135+#check F
136136+#check F List.nil
137137+138138+universe v
139139+inductive elof (α : Type v): Type v where
140140+| mk (a : α) : elof α
141141+142142+def elof.mk1 : {α : Type u} -> { n: List α } -> (b : List (List α)) -> elof (List α) :=
143143+ fun {α} {n} xs => match xs with
144144+ | List.nil => elof.mk n
145145+ | List.cons _ => elof.mk n
146146+ | List.cons (List.cons a List.nil) _ => elof.mk n
147147+148148+-- #check @elof.mk a
149149+-- #check List.cons
150150+-- #reduce elof.mk1 (List.cons a List.nil : List (Type v))
151151+-- #reduce elof.mk1 (List.nil : List (Type v))
152152+153153+end el
154154+155155+end typeclasses