-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeClasses.lean
More file actions
347 lines (278 loc) · 15.6 KB
/
Copy pathTypeClasses.lean
File metadata and controls
347 lines (278 loc) · 15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/-
Type Classes via Dictionary Passing
This implements verified type class compilation:
1. Define type classes as record types (dictionaries)
2. Instances are values of dictionary type
3. Constrained functions take dictionary arguments
4. Prove laws are preserved through compilation
-/
-- ═══════════════════════════════════════════════════════════════════════════════
-- TYPES
-- ═══════════════════════════════════════════════════════════════════════════════
inductive Ty where
| unit : Ty
| bool : Ty
| int : Ty
| arr : Ty → Ty → Ty
| prod : Ty → Ty → Ty -- For dictionary records
deriving Repr, DecidableEq
notation:50 α " ⟶ " β => Ty.arr α β
notation:60 α " × " β => Ty.prod α β
-- ═══════════════════════════════════════════════════════════════════════════════
-- SEMANTIC INTERPRETATION
-- ═══════════════════════════════════════════════════════════════════════════════
def interpTy : Ty → Type
| .unit => Unit
| .bool => Bool
| .int => Int
| .arr α β => interpTy α → interpTy β
| .prod α β => interpTy α × interpTy β
-- ═══════════════════════════════════════════════════════════════════════════════
-- TYPE CLASSES AS DICTIONARIES
-- ═══════════════════════════════════════════════════════════════════════════════
-- Eq class: { eq : a → a → Bool }
def EqDict (τ : Ty) : Ty := τ ⟶ τ ⟶ .bool
-- Ord class: { eq : a → a → Bool, compare : a → a → Ordering }
-- For simplicity, Ordering = Int (-1, 0, 1)
def OrdDict (τ : Ty) : Ty := (τ ⟶ τ ⟶ .bool) × (τ ⟶ τ ⟶ .int)
-- Show class: { show : a → String }
-- We'll use Int as a stand-in for String
def ShowDict (τ : Ty) : Ty := τ ⟶ .int
-- Semigroup class: { append : a → a → a }
def SemigroupDict (τ : Ty) : Ty := τ ⟶ τ ⟶ τ
-- Monoid class: { append : a → a → a, mempty : a }
def MonoidDict (τ : Ty) : Ty := (τ ⟶ τ ⟶ τ) × τ
-- ═══════════════════════════════════════════════════════════════════════════════
-- SEMANTIC TYPE CLASSES (with laws!)
-- ═══════════════════════════════════════════════════════════════════════════════
-- Semantic Eq: eq must be reflexive, symmetric, transitive
structure SemEq (α : Type) where
eq : α → α → Bool
refl : ∀ x, eq x x = true
symm : ∀ x y, eq x y = eq y x
trans : ∀ x y z, eq x y = true → eq y z = true → eq x z = true
-- Semantic Semigroup: append must be associative
structure SemSemigroup (α : Type) where
append : α → α → α
assoc : ∀ x y z, append (append x y) z = append x (append y z)
-- Semantic Monoid: extends Semigroup with identity
structure SemMonoid (α : Type) extends SemSemigroup α where
mempty : α
left_id : ∀ x, append mempty x = x
right_id : ∀ x, append x mempty = x
-- Semantic Functor
structure SemFunctor (f : Type → Type) where
map : {α β : Type} → (α → β) → f α → f β
map_id : ∀ {α} (x : f α), map id x = x
map_comp : ∀ {α β γ} (g : β → γ) (h : α → β) (x : f α), map (g ∘ h) x = map g (map h x)
-- Semantic Monad
structure SemMonad (m : Type → Type) extends SemFunctor m where
pure : {α : Type} → α → m α
bind : {α β : Type} → m α → (α → m β) → m β
left_id : ∀ {α β} (a : α) (f : α → m β), bind (pure a) f = f a
right_id : ∀ {α} (ma : m α), bind ma pure = ma
assoc : ∀ {α β γ} (ma : m α) (f : α → m β) (g : β → m γ),
bind (bind ma f) g = bind ma (fun x => bind (f x) g)
-- ═══════════════════════════════════════════════════════════════════════════════
-- DICTIONARY INTERPRETATION
-- ═══════════════════════════════════════════════════════════════════════════════
-- Interpret an Eq dictionary as a SemEq (forgetting laws)
def interpEqDict (d : interpTy (EqDict τ)) : interpTy τ → interpTy τ → Bool := d
-- Interpret a Monoid dictionary
def interpMonoidDict (d : interpTy (MonoidDict τ)) : (interpTy τ → interpTy τ → interpTy τ) × interpTy τ :=
(d.1, d.2)
-- ═══════════════════════════════════════════════════════════════════════════════
-- INSTANCES (dictionaries with proofs)
-- ═══════════════════════════════════════════════════════════════════════════════
-- Bool has decidable equality
def eqBool : SemEq Bool where
eq := fun a b => a == b
refl := by intro x; cases x <;> rfl
symm := by intro x y; cases x <;> cases y <;> rfl
trans := by intro x y z hxy hyz; cases x <;> cases y <;> cases z <;> simp_all
-- Int has decidable equality (proofs standard)
def eqInt : SemEq Int where
eq := fun a b => decide (a = b)
refl := by intro x; simp
symm := by intro x y; cases h : decide (x = y) <;> cases h' : decide (y = x) <;> simp_all
trans := by intro x y z hxy hyz; simp_all
-- List is a semigroup under append
def semigroupList (α : Type) : SemSemigroup (List α) where
append := List.append
assoc := List.append_assoc
-- List is a monoid
def monoidList (α : Type) : SemMonoid (List α) where
toSemSemigroup := semigroupList α
mempty := []
left_id := List.nil_append
right_id := List.append_nil
-- Option is a functor
def functorOption : SemFunctor Option where
map := Option.map
map_id := by intro α x; cases x <;> rfl
map_comp := by intro α β γ g h x; cases x <;> rfl
-- Option is a monad
def monadOption : SemMonad Option where
toSemFunctor := functorOption
pure := some
bind := Option.bind
left_id := by intros; rfl
right_id := by intro α ma; cases ma <;> rfl
assoc := by intro α β γ ma f g; cases ma <;> rfl
-- Define our own bind to have full control
def listBind {α β : Type} (xs : List α) (f : α → List β) : List β :=
match xs with
| [] => []
| h :: t => f h ++ listBind t f
-- Helper: listBind distributes over append
theorem listBindAppend {α β : Type} (xs ys : List α) (f : α → List β) :
listBind (xs ++ ys) f = listBind xs f ++ listBind ys f := by
induction xs with
| nil => rfl
| cons h t ih =>
-- Goal: listBind ((h :: t) ++ ys) f = listBind (h :: t) f ++ listBind ys f
-- i.e., listBind (h :: (t ++ ys)) f = (f h ++ listBind t f) ++ listBind ys f
-- i.e., f h ++ listBind (t ++ ys) f = (f h ++ listBind t f) ++ listBind ys f
show f h ++ listBind (t ++ ys) f = (f h ++ listBind t f) ++ listBind ys f
rw [ih, List.append_assoc]
-- List is a monad (fully verified!)
def monadList : SemMonad List where
toSemFunctor := {
map := List.map
map_id := fun xs => by induction xs <;> simp_all [List.map]
map_comp := fun g h xs => by induction xs <;> simp_all [List.map, Function.comp]
}
pure := fun x => [x]
bind := listBind
left_id := fun _ _ => by simp [listBind, List.append_nil]
right_id := fun ma => by
induction ma with
| nil => rfl
| cons h t ih => simp [listBind]; exact ih
assoc := fun ma f g => by
induction ma with
| nil => rfl
| cons h t ih =>
-- Goal: listBind (listBind (h :: t) f) g = listBind (h :: t) (fun x => listBind (f x) g)
show listBind (f h ++ listBind t f) g = listBind (f h) g ++ listBind t (fun x => listBind (f x) g)
rw [listBindAppend]
simp only [ih]
-- ═══════════════════════════════════════════════════════════════════════════════
-- CONSTRAINED FUNCTIONS
-- ═══════════════════════════════════════════════════════════════════════════════
-- Before type class resolution: polymorphic with explicit dictionary
-- notEq : ∀a. EqDict a → a → a → Bool
-- notEq dict x y = not (dict.eq x y)
def notEq (eq : α → α → Bool) (x y : α) : Bool := !(eq x y)
-- With SemEq, we can prove properties:
theorem notEq_irrefl (E : SemEq α) (x : α) : notEq E.eq x x = false := by
simp [notEq, E.refl]
theorem notEq_symm (E : SemEq α) (x y : α) : notEq E.eq x y = notEq E.eq y x := by
simp [notEq, E.symm]
-- Monoid power function
def power (M : SemMonoid α) (x : α) (n : Nat) : α :=
match n with
| 0 => M.mempty
| n + 1 => M.append x (power M x n)
theorem power_zero (M : SemMonoid α) (x : α) : power M x 0 = M.mempty := rfl
theorem power_succ (M : SemMonoid α) (x : α) (n : Nat) :
power M x (n + 1) = M.append x (power M x n) := rfl
theorem power_add (M : SemMonoid α) (x : α) (m n : Nat) :
power M x (m + n) = M.append (power M x m) (power M x n) := by
induction m with
| zero => simp only [power, Nat.zero_add, M.left_id]
| succ m ih =>
simp only [power, Nat.succ_add]
rw [ih, M.assoc]
-- ═══════════════════════════════════════════════════════════════════════════════
-- FUNCTOR/MONAD COMBINATORS WITH PROOFS
-- ═══════════════════════════════════════════════════════════════════════════════
-- void : Functor f => f a → f Unit
def void (F : SemFunctor f) (fa : f α) : f Unit :=
F.map (fun _ => ()) fa
-- join : Monad m => m (m a) → m a
def join (M : SemMonad m) (mma : m (m α)) : m α :=
M.bind mma id
-- Kleisli composition
def kleisli (M : SemMonad m) (f : α → m β) (g : β → m γ) (a : α) : m γ :=
M.bind (f a) g
-- Prove join in terms of bind
theorem join_bind (M : SemMonad m) (mma : m (m α)) :
join M mma = M.bind mma id := rfl
-- Prove Kleisli associativity
theorem kleisli_assoc (M : SemMonad m) (f : α → m β) (g : β → m γ) (h : γ → m δ) (a : α) :
kleisli M (kleisli M f g) h a = kleisli M f (kleisli M g h) a := by
simp only [kleisli]
rw [M.assoc]
-- ═══════════════════════════════════════════════════════════════════════════════
-- CODE GENERATION
-- ═══════════════════════════════════════════════════════════════════════════════
-- Generate PureScript for notEq using dictionary passing
def codegenNotEq : String :=
"notEq eqDict x y = not (eqDict x y)"
-- Generate PureScript for power
def codegenPower : String :=
"power monoidDict x n = case n of\n" ++
" 0 -> monoidDict.mempty\n" ++
" _ -> monoidDict.append x (power monoidDict x (n - 1))"
-- Generate PureScript for join
def codegenJoin : String :=
"join monadDict mma = monadDict.bind mma identity"
-- Generate PureScript for Kleisli
def codegenKleisli : String :=
"kleisli monadDict f g a = monadDict.bind (f a) g"
#eval codegenNotEq
#eval codegenPower
#eval codegenJoin
#eval codegenKleisli
-- ═══════════════════════════════════════════════════════════════════════════════
-- THE KEY INSIGHT
-- ═══════════════════════════════════════════════════════════════════════════════
/-
What we've achieved:
1. TYPE CLASSES AS STRUCTURES
- SemEq, SemSemigroup, SemMonoid, SemFunctor, SemMonad
- Each bundles operations WITH their laws
2. VERIFIED INSTANCES
- eqBool, eqInt: prove reflexivity, symmetry, transitivity
- monoidList: proves identity and associativity
- monadOption, monadList: prove all three monad laws
3. GENERIC FUNCTIONS WITH PROOFS
- notEq: works for any Eq, prove ¬(x = x) = false
- power: works for any Monoid, prove power_add
- kleisli: works for any Monad, prove associativity
4. CODE GENERATION
- Shows how to emit dictionary-passing PureScript
THE CONNECTION:
- We write `notEq E.eq x x` in Lean
- We prove `notEq_irrefl : notEq E.eq x x = false`
- We generate `notEq eqDict x y = not (eqDict x y)`
- The generated code has the same structure
- Therefore the property holds in PureScript too!
THE GAP:
- We still emit strings, not typed terms
- A full solution would have Term constructors for dictionary operations
- Then codegen would be a verified transformation
-/
-- ═══════════════════════════════════════════════════════════════════════════════
-- WHAT'S STILL HARD
-- ═══════════════════════════════════════════════════════════════════════════════
/-
1. DICTIONARY INFERENCE
PureScript/Haskell find dictionaries automatically.
We pass them explicitly (which is what the compiler does internally).
2. SUPERCLASS CONSTRAINTS
class Eq a => Ord a
Need to thread Eq dictionary through Ord dictionary.
3. HIGHER-KINDED POLYMORPHISM
Functor (f : Type → Type) requires kind polymorphism.
Our Ty doesn't have kinds.
4. OVERLAPPING INSTANCES
Multiple applicable instances need priority rules.
5. ASSOCIATED TYPES / TYPE FAMILIES
class Collection c where
type Elem c :: Type
Requires more sophisticated type representation.
These are all solvable but each requires significant work.
The architecture (dictionaries + proofs) is sound.
-/