@@ -33,11 +33,6 @@ structure Param where
3333def Param.toExpr (p : Param) : Expr :=
3434 .fvar p.fvarId
3535
36- inductive AltCore (Code : Type ) where
37- | alt (ctorName : Name) (params : Array Param) (code : Code)
38- | default (code : Code)
39- deriving Inhabited
40-
4136inductive LitValue where
4237 | natVal (val : Nat)
4338 | strVal (val : String)
@@ -136,42 +131,48 @@ structure LetDecl where
136131 value : LetValue
137132 deriving Inhabited, BEq
138133
139- structure FunDeclCore (Code : Type ) where
134+ mutual
135+
136+ inductive Alt where
137+ | alt (ctorName : Name) (params : Array Param) (code : Code)
138+ | default (code : Code)
139+
140+ structure FunDecl where
140141 fvarId : FVarId
141142 binderName : Name
142143 params : Array Param
143144 type : Expr
144145 value : Code
145- deriving Inhabited
146-
147- def FunDeclCore.getArity (decl : FunDeclCore Code) : Nat :=
148- decl.params.size
149146
150- structure CasesCore (Code : Type ) where
147+ structure Cases where
151148 typeName : Name
152149 resultType : Expr
153150 discr : FVarId
154- alts : Array (AltCore Code)
151+ alts : Array Alt
155152 deriving Inhabited
156153
157154inductive Code where
158155 | let (decl : LetDecl) (k : Code)
159- | fun (decl : FunDeclCore Code ) (k : Code)
160- | jp (decl : FunDeclCore Code ) (k : Code)
156+ | fun (decl : FunDecl ) (k : Code)
157+ | jp (decl : FunDecl ) (k : Code)
161158 | jmp (fvarId : FVarId) (args : Array Arg)
162- | cases (cases : CasesCore Code )
159+ | cases (cases : Cases )
163160 | return (fvarId : FVarId)
164161 | unreach (type : Expr)
165162 deriving Inhabited
166163
167- abbrev Alt := AltCore Code
168- abbrev FunDecl := FunDeclCore Code
169- abbrev Cases := CasesCore Code
164+ end
165+
166+ deriving instance Inhabited for Alt
167+ deriving instance Inhabited for FunDecl
168+
169+ def FunDecl.getArity (decl : FunDecl) : Nat :=
170+ decl.params.size
170171
171172/--
172173Return the constructor names that have an explicit (non-default) alternative.
173174-/
174- def CasesCore .getCtorNames (c : Cases) : NameSet :=
175+ def Cases .getCtorNames (c : Cases) : NameSet :=
175176 c.alts.foldl (init := {}) fun ctorNames alt =>
176177 match alt with
177178 | .default _ => ctorNames
@@ -241,15 +242,15 @@ instance : BEq Code where
241242instance : BEq FunDecl where
242243 beq := FunDecl.beq
243244
244- def AltCore .getCode : Alt → Code
245+ def Alt .getCode : Alt → Code
245246 | .default k => k
246247 | .alt _ _ k => k
247248
248- def AltCore .getParams : Alt → Array Param
249+ def Alt .getParams : Alt → Array Param
249250 | .default _ => #[]
250251 | .alt _ ps _ => ps
251252
252- def AltCore .forCodeM [Monad m] (alt : Alt) (f : Code → m Unit) : m Unit := do
253+ def Alt .forCodeM [Monad m] (alt : Alt) (f : Code → m Unit) : m Unit := do
253254 match alt with
254255 | .default k => f k
255256 | .alt _ _ k => f k
@@ -259,14 +260,14 @@ private unsafe def updateAltCodeImp (alt : Alt) (k' : Code) : Alt :=
259260 | .default k => if ptrEq k k' then alt else .default k'
260261 | .alt ctorName ps k => if ptrEq k k' then alt else .alt ctorName ps k'
261262
262- @[implemented_by updateAltCodeImp] opaque AltCore .updateCode (alt : Alt) (c : Code) : Alt
263+ @[implemented_by updateAltCodeImp] opaque Alt .updateCode (alt : Alt) (c : Code) : Alt
263264
264265private unsafe def updateAltImp (alt : Alt) (ps' : Array Param) (k' : Code) : Alt :=
265266 match alt with
266267 | .alt ctorName ps k => if ptrEq k k' && ptrEq ps ps' then alt else .alt ctorName ps' k'
267268 | _ => unreachable!
268269
269- @[implemented_by updateAltImp] opaque AltCore .updateAlt! (alt : Alt) (ps' : Array Param) (k' : Code) : Alt
270+ @[implemented_by updateAltImp] opaque Alt .updateAlt! (alt : Alt) (ps' : Array Param) (k' : Code) : Alt
270271
271272@[inline] private unsafe def updateAltsImp (c : Code) (alts : Array Alt) : Code :=
272273 match c with
@@ -364,9 +365,9 @@ Low-level update `FunDecl` function. It does not update the local context.
364365Consider using `FunDecl.update : LetDecl → Expr → Array Param → Code → CompilerM FunDecl` if you want the local context
365366to be updated.
366367-/
367- @[implemented_by updateFunDeclCoreImp] opaque FunDeclCore .updateCore (decl: FunDecl) (type : Expr) (params : Array Param) (value : Code) : FunDecl
368+ @[implemented_by updateFunDeclCoreImp] opaque FunDecl .updateCore (decl : FunDecl) (type : Expr) (params : Array Param) (value : Code) : FunDecl
368369
369- def CasesCore .extractAlt! (cases : Cases) (ctorName : Name) : Alt × Cases :=
370+ def Cases .extractAlt! (cases : Cases) (ctorName : Name) : Alt × Cases :=
370371 let found i := (cases.alts[i], { cases with alts := cases.alts.eraseIdx i })
371372 if let some i := cases.alts.findFinIdx? fun | .alt ctorName' .. => ctorName == ctorName' | _ => false then
372373 found i
@@ -375,7 +376,7 @@ def CasesCore.extractAlt! (cases : Cases) (ctorName : Name) : Alt × Cases :=
375376 else
376377 unreachable!
377378
378- def AltCore .mapCodeM [Monad m] (alt : Alt) (f : Code → m Code) : m Alt := do
379+ def Alt .mapCodeM [Monad m] (alt : Alt) (f : Code → m Code) : m Alt := do
379380 return alt.updateCode (← f alt.getCode)
380381
381382def Code.isDecl : Code → Bool
@@ -678,7 +679,7 @@ private partial def collectParams (ps : Array Param) (s : FVarIdSet) : FVarIdSet
678679 ps.foldl (init := s) fun s p => collectType p.type s
679680
680681mutual
681- partial def FunDeclCore .collectUsed (decl : FunDecl) (s : FVarIdSet := {}) : FVarIdSet :=
682+ partial def FunDecl .collectUsed (decl : FunDecl) (s : FVarIdSet := {}) : FVarIdSet :=
682683 decl.value.collectUsed <| collectParams decl.params <| collectType decl.type s
683684
684685partial def Code.collectUsed (code : Code) (s : FVarIdSet := {}) : FVarIdSet :=
0 commit comments