Skip to content

Commit 0c882af

Browse files
committed
[ refactor ] change constructor argument representation from Scope to List Name
This refactor changes the representation of constructor arguments throughout the compiler from `Scope` to `List Name`. This simplifies the API and reduces complexity in handling variable indices. Key changes: - Updated `MkConAlt` and related types to use `List Name` instead of `Scope` for constructor arguments - Modified variable binding and weakening operations to work with list-based arguments - Adjusted pattern matching and case analysis code throughout the compiler - Added new helper functions for list-based variable operations The change affects multiple compiler modules including ANF, LambdaLift, CaseOpts, and optimization passes, ensuring consistent handling of constructor arguments across the compilation pipeline.
1 parent caf5425 commit 0c882af

File tree

16 files changed

+122
-76
lines changed

16 files changed

+122
-76
lines changed

src/Compiler/ANF.idr

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,12 @@ mlet fc val sc
194194

195195
bindAsFresh :
196196
{auto v : Ref Next Int} ->
197-
(args : Scope) -> AVars vars' ->
198-
Core (List Int, AVars (vars' ++ args))
199-
bindAsFresh [<] vs = pure ([], vs)
200-
bindAsFresh (ns :< n) vs
197+
(args : List Name) -> AVars vars' ->
198+
Core (List Int, AVars (Scope.ext vars' args))
199+
bindAsFresh [] vs = pure ([], vs)
200+
bindAsFresh (n :: ns) vs
201201
= do i <- nextVar
202-
bimap (i ::) (:< i) <$> bindAsFresh ns vs
202+
mapFst (i ::) <$> bindAsFresh ns (vs :< i)
203203

204204
mutual
205205
anfArgs : {auto v : Ref Next Int} ->
@@ -265,11 +265,11 @@ toANF : LiftedDef -> Core ANFDef
265265
toANF (MkLFun args scope sc)
266266
= do v <- newRef Next (the Int 0)
267267
(iargs, vsNil) <- bindAsFresh args AVars.empty
268-
let vs : AVars args
269-
:= rewrite sym $ appendLinLeftNeutral args in
270-
vsNil
271-
(iargs', vs) <- bindAsFresh scope vs
272-
sc' <- anf vs sc
268+
(iargs', vs) <- bindAsFresh (toList scope) vsNil
269+
sc' <- anf vs $
270+
do rewrite fishAsSnocAppend (cast args) (toList scope)
271+
rewrite castToList scope
272+
sc
273273
pure $ MkAFun (iargs ++ iargs') sc'
274274
toANF (MkLCon t a ns) = pure $ MkACon t a ns
275275
toANF (MkLForeign ccs fargs t) = pure $ MkAForeign ccs fargs t

src/Compiler/CaseOpts.idr

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,26 +42,26 @@ shiftUnder : {args : _} ->
4242
shiftUnder First = weakenNVar (mkSizeOf args) (MkNVar First)
4343
shiftUnder (Later p) = insertNVar (mkSizeOf args) (MkNVar p)
4444

45-
shiftVar : {inner : Scope} -> {args : Scope} ->
46-
NVar n ((vars ++ args :< x) ++ inner) ->
47-
NVar n ((vars :< x ++ args) ++ inner)
45+
shiftVar : {inner : Scope} -> {args : List Name} ->
46+
NVar n ((vars <>< args :< x) ++ inner) ->
47+
NVar n ((vars :< x <>< args) ++ inner)
4848
shiftVar nvar
4949
= let inn = mkSizeOf inner in
5050
case locateNVar inn nvar of
51-
Left (MkNVar p) => weakenNs inn (shiftUnderNs (mkSizeOf args) p)
51+
Left (MkNVar p) => weakenNs inn (shiftUndersN (mkSizeOf _) p)
5252
Right nvar => embed nvar
5353

5454
mutual
5555
shiftBinder : {inner, args : _} ->
5656
(new : Name) ->
57-
CExp (((vars ++ args) :< old) ++ inner) ->
58-
CExp ((vars :< new ++ args) ++ inner)
57+
CExp (((vars <>< args) :< old) ++ inner) ->
58+
CExp ((vars :< new <>< args) ++ inner)
5959
shiftBinder new (CLocal fc p)
6060
= case shiftVar (MkNVar p) of
6161
MkNVar p' => CLocal fc (renameVar p')
6262
where
63-
renameVar : IsVar x i ((vars :< old ++ args) ++ local) ->
64-
IsVar x i ((vars :< new ++ args) ++ local)
63+
renameVar : IsVar x i ((vars :< old <>< args) ++ local) ->
64+
IsVar x i ((vars :< new <>< args) ++ local)
6565
renameVar = believe_me -- it's the same index, so just the identity at run time
6666
shiftBinder new (CRef fc n) = CRef fc n
6767
shiftBinder {inner} new (CLam fc n sc)
@@ -92,27 +92,27 @@ mutual
9292

9393
shiftBinderConAlt : {inner, args : _} ->
9494
(new : Name) ->
95-
CConAlt (((vars ++ args) :< old) ++ inner) ->
96-
CConAlt ((vars :< new ++ args) ++ inner)
95+
CConAlt (((vars <>< args) :< old) ++ inner) ->
96+
CConAlt ((vars :< new <>< args) ++ inner)
9797
shiftBinderConAlt new (MkConAlt n ci t args' sc)
98-
= let sc' : CExp (((vars ++ args) :< old) ++ (inner ++ args'))
99-
= (rewrite appendAssociative ((vars ++ args) :< old) inner args' in sc) in
98+
= let sc' : CExp (((vars <>< args) :< old) ++ (inner <>< args'))
99+
= rewrite sym $ snocAppendFishAssociative (vars <>< args :< old) inner args' in sc in
100100
MkConAlt n ci t args' $
101-
rewrite sym $ appendAssociative (vars :< new ++ args) inner args' in
101+
rewrite snocAppendFishAssociative (vars :< new <>< args) inner args' in
102102
shiftBinder new sc'
103103

104104
shiftBinderConstAlt : {inner, args : _} ->
105105
(new : Name) ->
106-
CConstAlt (((vars ++ args) :< old) ++ inner) ->
107-
CConstAlt ((vars :< new ++ args) ++ inner)
106+
CConstAlt (((vars <>< args) :< old) ++ inner) ->
107+
CConstAlt ((vars :< new <>< args) ++ inner)
108108
shiftBinderConstAlt new (MkConstAlt c sc) = MkConstAlt c $ shiftBinder new sc
109109

110110
-- If there's a lambda inside a case, move the variable so that it's bound
111111
-- outside the case block so that we can bind it just once outside the block
112112
liftOutLambda : {args : _} ->
113113
(new : Name) ->
114-
CExp (Scope.bind (Scope.addInner vars args) old) ->
115-
CExp (Scope.addInner (Scope.bind vars new) args)
114+
CExp (Scope.bind (Scope.ext vars args) old) ->
115+
CExp (Scope.ext (Scope.bind vars new) args)
116116
liftOutLambda = shiftBinder {inner = Scope.empty}
117117

118118
-- If all the alternatives start with a lambda, we can have a single lambda
@@ -133,7 +133,7 @@ tryLiftOutConst : (new : Name) ->
133133
tryLiftOutConst new [] = Just []
134134
tryLiftOutConst new (MkConstAlt c (CLam fc x sc) :: as)
135135
= do as' <- tryLiftOutConst new as
136-
let sc' = liftOutLambda {args = [<]} new sc
136+
let sc' = liftOutLambda {args = []} new sc
137137
pure (MkConstAlt c sc' :: as')
138138
tryLiftOutConst _ _ = Nothing
139139

@@ -142,7 +142,7 @@ tryLiftDef : (new : Name) ->
142142
Maybe (Maybe (CExp (Scope.bind vars new)))
143143
tryLiftDef new Nothing = Just Nothing
144144
tryLiftDef new (Just (CLam fc x sc))
145-
= let sc' = liftOutLambda {args = [<]} new sc in
145+
= let sc' = liftOutLambda {args = []} new sc in
146146
pure (Just sc')
147147
tryLiftDef _ _ = Nothing
148148

@@ -318,8 +318,8 @@ doCaseOfCase fc x xalts xdef alts def
318318
updateAlt (MkConAlt n ci t args sc)
319319
= MkConAlt n ci t args $
320320
CConCase fc sc
321-
(map (weakenNs (mkSizeOf args)) alts)
322-
(map (weakenNs (mkSizeOf args)) def)
321+
(map (weakensN (mkSizeOf args)) alts)
322+
(map (weakensN (mkSizeOf args)) def)
323323

324324
updateDef : CExp vars -> CExp vars
325325
updateDef sc = CConCase fc sc alts def

src/Compiler/Common.idr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ getCompileDataWith exports doLazyAnnots phase_in tm_in
356356
traverse (lambdaLift doLazyAnnots) cseDefs
357357
else pure []
358358

359-
let lifted = (mainname, MkLFun Scope.empty Scope.empty liftedtm) ::
359+
let lifted = (mainname, MkLFun [] Scope.empty liftedtm) ::
360360
(ldefs ++ concat lifted_in)
361361

362362
anf <- if phase >= ANF

src/Compiler/CompileExpr.idr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ mutual
229229
Just gdef <- lookupCtxtExact x (gamma defs)
230230
| Nothing => -- primitive type match
231231
do xn <- getFullName x
232-
pure $ MkConAlt xn TYCON Nothing (cast args) !(toCExpTree n (rewrite sym $ fishAsSnocAppend vars args in sc))
232+
pure $ MkConAlt xn TYCON Nothing (cast args) !(toCExpTree n sc)
233233
:: !(conCases n ns)
234234
case (definition gdef) of
235235
DCon _ arity (Just pos) => conCases n ns -- skip it
@@ -240,8 +240,8 @@ mutual
240240
sc' <- toCExpTree n sc
241241
ns' <- conCases n ns
242242
if dcon (definition gdef)
243-
then pure $ MkConAlt xn !(dconFlag xn) (Just tag) args' (shrinkCExp subList sc') :: ns'
244-
else pure $ MkConAlt xn !(dconFlag xn) Nothing args' (shrinkCExp subList sc') :: ns'
243+
then pure $ MkConAlt xn !(dconFlag xn) (Just tag) (cast args') (rewrite sym $ snocAppendAsFish vars args' in shrinkCExp subList sc') :: ns'
244+
else pure $ MkConAlt xn !(dconFlag xn) Nothing (cast args') (rewrite sym $ snocAppendAsFish vars args' in shrinkCExp subList sc') :: ns'
245245
where
246246
dcon : Def -> Bool
247247
dcon (DCon {}) = True

src/Compiler/Inline.idr

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ mutual
130130
usedCon : {free : _} ->
131131
{idx : Nat} -> (0 p : IsVar n idx free) -> CConAlt free -> Int
132132
usedCon n (MkConAlt _ _ _ args sc)
133-
= let MkVar n' = weakenNs (mkSizeOf args) (MkVar n) in
133+
= let MkVar n' = weakensN (mkSizeOf args) (MkVar n) in
134134
used n' sc
135135

136136
usedConst : {free : _} ->
@@ -306,9 +306,13 @@ mutual
306306
FC -> List Name -> EEnv free vars -> Stack free -> CConAlt (Scope.addInner free vars) ->
307307
Core (CConAlt free)
308308
evalAlt {free} {vars} fc rec env stk (MkConAlt n ci t args sc)
309-
= do (bs, env') <- extendLoc fc env args
310-
scEval <- eval rec env' stk (rewrite appendAssociative free vars args in sc)
311-
pure $ MkConAlt n ci t args (refsToLocals bs scEval)
309+
= do (bs, env') <- extendLoc fc env (cast args)
310+
scEval <- eval rec env' stk $
311+
do rewrite appendAssociative free vars (cast args)
312+
rewrite sym $ fishAsSnocAppend (free ++ vars) (args)
313+
sc
314+
let sc'' = rewrite snocAppendFishAssociative free Scope.empty args in refsToLocals bs scEval
315+
pure $ MkConAlt n ci t args sc''
312316
313317
evalConstAlt : {vars, free : _} ->
314318
{auto c : Ref Ctxt Defs} ->
@@ -335,10 +339,7 @@ mutual
335339
Just m =>
336340
do let env' = extend env (toList args') args m
337341
pure $ Just !(eval rec env' stk
338-
(do rewrite sym $ snocAppendFishAssociative free vars (toList args')
339-
rewrite sym $ snocAppendAsFish (free ++ vars) args'
340-
sc
341-
))
342+
(rewrite sym $ snocAppendFishAssociative free vars args' in sc))
342343
else pickAlt rec env stk con alts def
343344
where
344345
matches : Name -> Maybe Int -> Name -> Maybe Int -> Bool

src/Compiler/LambdaLift.idr

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,7 @@ mutual
194194
||| @ body is the expression that is evaluated as the consequence of
195195
||| this branch matching.
196196
MkLConAlt : (n : Name) -> (info : ConInfo) -> (tag : Maybe Int) ->
197-
-- TODO should args be a List?
198-
(args : Scope) -> (body : Lifted (Scope.addInner vars args)) ->
197+
(args : List Name) -> (body : Lifted (Scope.ext vars args)) ->
199198
LiftedConAlt vars
200199

201200
||| A branch of an "LConst" (constant expression) case statement.
@@ -232,8 +231,8 @@ data LiftedDef : Type where
232231
-- (Sorry for the awkward API - it's to do with how the indices are
233232
-- arranged for the variables, and it could be expensive to reshuffle them!
234233
-- See Compiler.ANF for an example of how they get resolved to names)
235-
MkLFun : (args : Scope) -> (scope : Scope) ->
236-
(body : Lifted (Scope.addInner args scope)) -> LiftedDef
234+
MkLFun : (args : List Name) -> (scope : Scope) ->
235+
(body : Lifted (Scope.addInner (cast args) scope)) -> LiftedDef
237236

238237
||| Constructs a definition of a constructor for a compound data type.
239238
|||
@@ -454,7 +453,7 @@ usedVars used (LConCase fc sc alts def) =
454453
usedConAlt : {default Nothing lazy : Maybe LazyReason} ->
455454
Used vars -> LiftedConAlt vars -> Used vars
456455
usedConAlt used (MkLConAlt n ci tag args sc) =
457-
contractUsedMany {remove=args} (usedVars (weakenUsed used) sc)
456+
contractUsedManyFish {remove=args} (usedVars (weakenUsedFish used) sc)
458457

459458
usedVars used (LConstCase fc sc alts def) =
460459
let defUsed = maybe used (usedVars used {vars}) def
@@ -553,7 +552,7 @@ dropUnused inn unused (LConstCase fc sc alts def) =
553552
LConstCase fc (dropUnused inn unused sc) alts' (map (dropUnused inn unused) def)
554553

555554
dropConCase inn unused (MkLConAlt n ci t args sc) =
556-
MkLConAlt n ci t args (underBinders Lifted (\inn => dropUnused inn unused) inn (mkSizeOf args) sc)
555+
MkLConAlt n ci t args (underBinderz Lifted (\inn => dropUnused inn unused) inn (mkSizeOf args) sc)
557556

558557
dropConstCase inn unused (MkLConstAlt c val) = MkLConstAlt c (dropUnused inn unused val)
559558

@@ -575,8 +574,10 @@ mutual
575574
unused = getUnused unusedContracted
576575
scl' = dropUnused (mkSizeOf bound) unused scl
577576
n <- genName
578-
update Lifts { defs $= ((n, MkLFun (dropped vars unused) bound scl') ::) }
579-
pure $ LUnderApp fc n (length bound) (allVars fc vars unused)
577+
let scl'' : Lifted ((cast (toList $ dropped vars unused)) ++ bound)
578+
:= rewrite castToList (dropped vars unused) in scl'
579+
update Lifts { defs $= ((n, MkLFun (toList $ dropped vars unused) bound scl'') ::) }
580+
pure $ LUnderApp fc n (length bound) (reverse $ allVars fc vars unused)
580581
where
581582
582583
allPrfs : (vs : Scope) -> SizeOf inner -> (unused : Vect (length vs) Bool) -> List (Var (vs <>< inner))
@@ -650,7 +651,7 @@ export
650651
lambdaLiftDef : (doLazyAnnots : Bool) -> Name -> CDef -> Core (List (Name, LiftedDef))
651652
lambdaLiftDef doLazyAnnots n (MkFun args exp)
652653
= do (expl, defs) <- liftBody {doLazyAnnots} n exp
653-
pure ((n, MkLFun args Scope.empty expl) :: defs)
654+
pure ((n, MkLFun (toList args) Scope.empty (rewrite castToList args in expl)) :: defs)
654655
lambdaLiftDef _ n (MkCon t a nt) = pure [(n, MkLCon t a nt)]
655656
lambdaLiftDef _ n (MkForeign ccs fargs ty) = pure [(n, MkLForeign ccs fargs ty)]
656657
lambdaLiftDef doLazyAnnots n (MkError exp)

src/Compiler/Opts/CSE.idr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,9 @@ mutual
174174
dropConAlt : Drop CConAlt
175175
dropConAlt inn (MkConAlt x y tag args z) =
176176
MkConAlt x y tag args <$>
177-
dropCExp
178-
(inn + mkSizeOf args)
179-
(replace {p = CExp} (sym $ appendAssociative outer inner args) z)
177+
dropCExp {outer=outer}
178+
(rewrite fishAsSnocAppend inner args in inn + mkSizeOf (cast args))
179+
(rewrite sym $ snocAppendFishAssociative outer inner args in z)
180180

181181
dropConstAlt : Drop CConstAlt
182182
dropConstAlt inn (MkConstAlt x y) = MkConstAlt x <$> dropCExp inn y

src/Compiler/Opts/ConstantFold.idr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ wk sout (Wk {ws, ds, vars} rho sws)
5757
Wk rho (sws + sout)
5858
wk ws rho = Wk rho ws
5959

60+
wksN : Subst ds vars -> SizeOf out -> Subst (Scope.ext ds out) (Scope.ext vars out)
61+
wksN s s'
62+
= rewrite fishAsSnocAppend ds out in
63+
rewrite fishAsSnocAppend vars out in
64+
wk (zero <>< s') s
65+
6066
record WkCExp (vars : Scope) where
6167
constructor MkWkCExp
6268
{0 outer, supp : Scope}
@@ -172,7 +178,7 @@ constFold rho (CConCase fc sc xs x)
172178
where
173179
foldAlt : CConAlt vars -> CConAlt vars'
174180
foldAlt (MkConAlt n ci t xs e)
175-
= MkConAlt n ci t xs $ constFold (wk (mkSizeOf xs) rho) e
181+
= MkConAlt n ci t xs $ constFold (wksN rho (mkSizeOf xs)) e
176182

177183
constFold rho (CConstCase fc sc xs x) =
178184
let sc' = constFold rho sc

src/Compiler/Opts/Constructor.idr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,12 @@ natBranch (MkConAlt n SUCC _ _ _) = True
105105
natBranch _ = False
106106

107107
trySBranch : CExp vars -> CConAlt vars -> Maybe (CExp vars)
108-
trySBranch n (MkConAlt nm SUCC _ [<arg] sc)
108+
trySBranch n (MkConAlt nm SUCC _ [arg] sc)
109109
= Just (CLet (getFC n) arg YesInline (magic__natUnsuc (getFC n) (getFC n) [n]) sc)
110110
trySBranch _ _ = Nothing
111111

112112
tryZBranch : CConAlt vars -> Maybe (CExp vars)
113-
tryZBranch (MkConAlt n ZERO _ [<] sc) = Just sc
113+
tryZBranch (MkConAlt n ZERO _ [] sc) = Just sc
114114
tryZBranch _ = Nothing
115115

116116
getSBranch : CExp vars -> List (CConAlt vars) -> Maybe (CExp vars)
@@ -159,7 +159,7 @@ enum (CConCase fc sc alts def) = do
159159
Just $ CConstCase fc sc alts' def
160160
where
161161
toEnum : CConAlt vars -> Maybe (CConstAlt vars)
162-
toEnum (MkConAlt nm (ENUM n) (Just tag) [<] sc)
162+
toEnum (MkConAlt nm (ENUM n) (Just tag) [] sc)
163163
= pure $ MkConstAlt (enumTag n tag) sc
164164
toEnum _ = Nothing
165165
enum t = Nothing
@@ -172,7 +172,7 @@ enum t = Nothing
172172

173173
unitTree : Ref NextMN Int => CExp vars -> Core (Maybe (CExp vars))
174174
unitTree exp@(CConCase fc sc alts def) =
175-
let [MkConAlt _ UNIT _ [<] e] = alts
175+
let [MkConAlt _ UNIT _ [] e] = alts
176176
| _ => pure Nothing
177177
in case sc of -- TODO: Check scrutinee has no effect, and skip let binding
178178
CLocal {} => pure $ Just e

src/Compiler/Opts/Identity.idr

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,19 @@ import Data.Vect
1010
import Libraries.Data.List.SizeOf
1111
import Libraries.Data.SnocList.SizeOf
1212

13+
-- TODO reduce quadratic weakening
1314
makeArgs : (args : Scope) -> List (Var (Scope.addInner vars args))
1415
makeArgs args = makeArgs' args id
1516
where
1617
makeArgs' : (args : Scope) -> (Var (Scope.addInner vars args) -> a) -> List a
1718
makeArgs' [<] f = []
1819
makeArgs' (xs :< x) f = f first :: makeArgs' xs (f . weaken)
1920

21+
makeArgz : (args : List Name) -> List (Var (Scope.ext vars args))
22+
makeArgz args
23+
= embedFishily @{ListFreelyEmbeddable}
24+
$ reverse $ allVars ([<] <>< args)
25+
2026
parameters (fn1 : Name) (idIdx : Nat)
2127
mutual
2228
-- special case for matching on 'Nat'-shaped things
@@ -89,8 +95,8 @@ parameters (fn1 : Name) (idIdx : Nat)
8995
altEq : CConAlt vars -> Bool
9096
altEq (MkConAlt y _ _ args exp) =
9197
cexpIdentity
92-
(weakenNs (mkSizeOf args) var)
93-
(Just (y, makeArgs args))
98+
(weakensN (mkSizeOf args) var)
99+
(Just (y, makeArgz args))
94100
const
95101
exp
96102
cexpIdentity var con const (CConstCase fc sc xs x) =

0 commit comments

Comments
 (0)