Skip to content

Commit 34ebbf0

Browse files
GulinSSspcfox
andcommitted
[ refactor ] ScopedSnocList: Preparing for the long jump (Phase 1)
Core idea is to split the process of transition from List-based Context on SnocList-based Context on 2 phases: 1. Apply a refactor which does not make any transition (even no observable changes for Idris users) actually but prepare the code for future update. 2. Roll out the transition with limited code changes invasion because it was partially done at the step before. Co-authored-by: Viktor Yudov <[email protected]>
1 parent d530f45 commit 34ebbf0

File tree

103 files changed

+1267
-824
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+1267
-824
lines changed

idris2api.ipkg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ modules =
7373
Core.LinearCheck,
7474
Core.Metadata,
7575
Core.Name,
76+
Core.Name.CompatibleVars,
7677
Core.Name.Namespace,
7778
Core.Name.Scoped,
7879
Core.Normalise,
@@ -191,6 +192,7 @@ modules =
191192
Libraries.Data.NameMap.Traversable,
192193
Libraries.Data.Ordering.Extra,
193194
Libraries.Data.PosMap,
195+
Libraries.Data.SnocList.Extra,
194196
Libraries.Data.SnocList.HasLength,
195197
Libraries.Data.SnocList.LengthMatch,
196198
Libraries.Data.SnocList.SizeOf,

libs/base/Data/SnocList.idr

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,3 +471,13 @@ tailRecAppendIsAppend : (sx, sy : SnocList a) -> tailRecAppend sx sy = sx ++ sy
471471
tailRecAppendIsAppend sx Lin = Refl
472472
tailRecAppendIsAppend sx (sy :< y) =
473473
trans (snocTailRecAppend y sx sy) (cong (:< y) $ tailRecAppendIsAppend sx sy)
474+
475+
||| `reverseOnto` reverses the snoc list and prepends it to the "onto" argument
476+
export
477+
revOnto : (xs, vs : SnocList a) -> reverseOnto xs vs = xs ++ reverse vs
478+
revOnto _ [<] = Refl
479+
revOnto xs (vs :< v) =
480+
do rewrite revOnto (xs :< v) vs
481+
rewrite sym $ appendAssociative xs [<v] (reverse vs)
482+
rewrite revOnto [<v] vs
483+
Refl

libs/base/Data/SnocList/HasLength.idr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ map f Z = Z
2929
map f (S hl) = S (map f hl)
3030

3131
export
32-
sucL : HasLength n sx -> HasLength (S n) ([<x] ++ sx)
33-
sucL Z = S Z
34-
sucL (S n) = S (sucL n)
32+
sucR : HasLength n sx -> HasLength (S n) ([<x] ++ sx)
33+
sucR Z = S Z
34+
sucR (S n) = S (sucR n)
3535

3636
export
3737
hlAppend : HasLength m sx -> HasLength n sy -> HasLength (n + m) (sx ++ sy)

libs/base/Data/SnocList/Operations.idr

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,17 @@ lengthHomomorphism sx (sy :< x) = Calc $
9696
~~ 1 + (length sx + length sy) ...(cong (1+) $ lengthHomomorphism _ _)
9797
~~ length sx + (1 + length sy) ...(plusSuccRightSucc _ _)
9898

99+
export
100+
lengthDistributesOverFish : (sx : SnocList a) -> (ys : List a) ->
101+
length (sx <>< ys) === length sx + length ys
102+
lengthDistributesOverFish sx [] = sym $ plusZeroRightNeutral _
103+
lengthDistributesOverFish sx (y :: ys) = Calc $
104+
|~ length ((sx :< y) <>< ys)
105+
~~ length (sx :< y) + length ys ...( lengthDistributesOverFish (sx :< y) ys)
106+
~~ S (length sx) + length ys ...( Refl )
107+
~~ length sx + S (length ys) ...( plusSuccRightSucc _ _ )
108+
~~ length sx + length (y :: ys) ...( Refl )
109+
99110
-- cons-list operations on snoc-lists
100111

101112
||| Take `n` first elements from `sx`, returning the whole list if

src/Compiler/ANF.idr

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import Core.Core
88
import Core.TT
99

1010
import Data.List
11+
import Data.SnocList
1112
import Data.Vect
1213
import Libraries.Data.SortedSet
14+
import Libraries.Data.SnocList.Extra
1315

1416
%default covering
1517

@@ -136,10 +138,13 @@ Show ANFDef where
136138
show args ++ " -> " ++ show ret
137139
show (MkAError exp) = "Error: " ++ show exp
138140

139-
data AVars : List Name -> Type where
140-
Nil : AVars []
141+
data AVars : Scoped where
142+
Nil : AVars ScopeEmpty
141143
(::) : Int -> AVars xs -> AVars (x :: xs)
142144

145+
ScopeEmpty : AVars ScopeEmpty
146+
ScopeEmpty = []
147+
143148
data Next : Type where
144149

145150
nextVar : {auto v : Ref Next Int} ->
@@ -280,7 +285,7 @@ toANF (MkLCon t a ns) = pure $ MkACon t a ns
280285
toANF (MkLForeign ccs fargs t) = pure $ MkAForeign ccs fargs t
281286
toANF (MkLError err)
282287
= do v <- newRef Next (the Int 0)
283-
pure $ MkAError !(anf [] err)
288+
pure $ MkAError !(anf ScopeEmpty err)
284289

285290
export
286291
freeVariables : ANF -> SortedSet AVar

src/Compiler/CaseOpts.idr

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,13 @@ import Core.FC
1010
import Core.TT
1111

1212
import Data.List
13+
import Data.SnocList
1314
import Data.Vect
1415

16+
import Libraries.Data.List.SizeOf
17+
import Libraries.Data.SnocList.SizeOf
18+
import Libraries.Data.SnocList.Extra
19+
1520
%default covering
1621

1722
{-
@@ -37,7 +42,7 @@ shiftUnder : {args : _} ->
3742
shiftUnder First = weakenNVar (mkSizeOf args) (MkNVar First)
3843
shiftUnder (Later p) = insertNVar (mkSizeOf args) (MkNVar p)
3944

40-
shiftVar : {outer, args : Scope} ->
45+
shiftVar : {outer : Scope} -> {args : List Name} ->
4146
NVar n (outer ++ (x :: args ++ vars)) ->
4247
NVar n (outer ++ (args ++ x :: vars))
4348
shiftVar nvar
@@ -108,7 +113,7 @@ liftOutLambda : {args : _} ->
108113
(new : Name) ->
109114
CExp (old :: args ++ vars) ->
110115
CExp (args ++ new :: vars)
111-
liftOutLambda = shiftBinder {outer = []}
116+
liftOutLambda = shiftBinder {outer = ScopeEmpty}
112117

113118
-- If all the alternatives start with a lambda, we can have a single lambda
114119
-- binding outside

src/Compiler/Common.idr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ Ord UsePhase where
8181
public export
8282
record CompileData where
8383
constructor MkCompileData
84-
mainExpr : CExp [] -- main expression to execute. This also appears in
84+
mainExpr : ClosedCExp -- main expression to execute. This also appears in
8585
-- the definitions below as MN "__mainExpression" 0
8686
-- For incremental compilation and for compiling exported
8787
-- names only, this can be set to 'erased'.
@@ -153,7 +153,7 @@ getMinimalDef (Coded ns bin)
153153
name <- fromBuf b
154154
let def
155155
= MkGlobalDef fc name (Erased fc Placeholder) [] [] [] [] mul
156-
[] (specified Public) (MkTotality Unchecked IsCovering) False
156+
ScopeEmpty (specified Public) (MkTotality Unchecked IsCovering) False
157157
[] Nothing refsR False False True
158158
None cdef Nothing [] Nothing
159159
pure (def, Just (ns, bin))
@@ -355,8 +355,8 @@ getCompileDataWith exports doLazyAnnots phase_in tm_in
355355
traverse (lambdaLift doLazyAnnots) cseDefs
356356
else pure []
357357

358-
let lifted = (mainname, MkLFun [] [] liftedtm) ::
359-
ldefs ++ concat lifted_in
358+
let lifted = (mainname, MkLFun ScopeEmpty ScopeEmpty liftedtm) ::
359+
(ldefs ++ concat lifted_in)
360360

361361
anf <- if phase >= ANF
362362
then logTime 2 "Get ANF" $ traverse (\ (n, d) => pure (n, !(toANF d))) lifted
@@ -412,7 +412,7 @@ getCompileData = getCompileDataWith []
412412

413413
export
414414
compileTerm : {auto c : Ref Ctxt Defs} ->
415-
ClosedTerm -> Core (CExp [])
415+
ClosedTerm -> Core ClosedCExp
416416
compileTerm tm_in
417417
= do tm <- toFullNames tm_in
418418
fixArityExp !(compileExp tm)

src/Compiler/CompileExpr.idr

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,14 @@ import Core.TT
1313
import Core.Value
1414

1515
import Data.List
16+
import Data.SnocList
1617
import Data.Maybe
1718
import Data.Vect
1819

20+
import Libraries.Data.List.SizeOf
21+
import Libraries.Data.SnocList.SizeOf
22+
import Libraries.Data.SnocList.Extra
23+
1924
%default covering
2025

2126
data Args
@@ -40,7 +45,7 @@ numArgs defs (Ref _ _ n)
4045
_ => pure (Arity 0)
4146
numArgs _ tm = pure (Arity 0)
4247

43-
mkSub : Nat -> (ns : List Name) -> List Nat -> (ns' ** Thin ns' ns)
48+
mkSub : Nat -> (ns : Scope) -> List Nat -> (ns' ** Thin ns' ns)
4449
mkSub i _ [] = (_ ** Refl)
4550
mkSub i [] ns = (_ ** Refl)
4651
mkSub i (x :: xs) es
@@ -319,7 +324,7 @@ mutual
319324
where
320325
mkSubst : Nat -> CExp vs ->
321326
Nat -> (args : List Name) -> (SizeOf args, SubstCEnv args vs)
322-
mkSubst _ _ _ [] = (zero, [])
327+
mkSubst _ _ _ [] = (zero, ScopeEmpty)
323328
mkSubst i scr pos (a :: as)
324329
= let (s, env) = mkSubst (1 + i) scr pos as in
325330
if i == pos
@@ -386,8 +391,8 @@ mutual
386391

387392
-- Need this for ensuring that argument list matches up to operator arity for
388393
-- builtins
389-
data ArgList : Nat -> List Name -> Type where
390-
NoArgs : ArgList Z []
394+
data ArgList : Nat -> Scope -> Type where
395+
NoArgs : ArgList Z ScopeEmpty
391396
ConsArg : (a : Name) -> ArgList k as -> ArgList (S k) (a :: as)
392397

393398
mkArgList : Int -> (n : Nat) -> (ns ** ArgList n ns)
@@ -397,17 +402,17 @@ mkArgList i (S k)
397402
(_ ** ConsArg (MN "arg" i) rec)
398403

399404
data NArgs : Type where
400-
User : Name -> List (Closure []) -> NArgs
401-
Struct : String -> List (String, Closure []) -> NArgs
405+
User : Name -> List ClosedClosure -> NArgs
406+
Struct : String -> List (String, ClosedClosure) -> NArgs
402407
NUnit : NArgs
403408
NPtr : NArgs
404409
NGCPtr : NArgs
405410
NBuffer : NArgs
406411
NForeignObj : NArgs
407-
NIORes : Closure [] -> NArgs
412+
NIORes : ClosedClosure -> NArgs
408413

409414
getPArgs : {auto c : Ref Ctxt Defs} ->
410-
Defs -> Closure [] -> Core (String, Closure [])
415+
Defs -> ClosedClosure -> Core (String, ClosedClosure)
411416
getPArgs defs cl
412417
= do NDCon fc _ _ _ args <- evalClosure defs cl
413418
| nf => throw (GenericMsg (getLoc nf) "Badly formed struct type")
@@ -419,7 +424,7 @@ getPArgs defs cl
419424
_ => throw (GenericMsg fc "Badly formed struct type")
420425

421426
getFieldArgs : {auto c : Ref Ctxt Defs} ->
422-
Defs -> Closure [] -> Core (List (String, Closure []))
427+
Defs -> ClosedClosure -> Core (List (String, ClosedClosure))
423428
getFieldArgs defs cl
424429
= do NDCon fc _ _ _ args <- evalClosure defs cl
425430
| nf => throw (GenericMsg (getLoc nf) "Badly formed struct type")
@@ -433,7 +438,7 @@ getFieldArgs defs cl
433438
_ => pure []
434439

435440
getNArgs : {auto c : Ref Ctxt Defs} ->
436-
Defs -> Name -> List (Closure []) -> Core NArgs
441+
Defs -> Name -> List ClosedClosure -> Core NArgs
437442
getNArgs defs (NS _ (UN $ Basic "IORes")) [arg] = pure $ NIORes arg
438443
getNArgs defs (NS _ (UN $ Basic "Ptr")) [arg] = pure NPtr
439444
getNArgs defs (NS _ (UN $ Basic "AnyPtr")) [] = pure NPtr
@@ -449,7 +454,7 @@ getNArgs defs (NS _ (UN $ Basic "Struct")) [n, args]
449454
getNArgs defs n args = pure $ User n args
450455

451456
nfToCFType : {auto c : Ref Ctxt Defs} ->
452-
FC -> (inStruct : Bool) -> NF [] -> Core CFType
457+
FC -> (inStruct : Bool) -> ClosedNF -> Core CFType
453458
nfToCFType _ _ (NPrimVal _ $ PrT IntType) = pure CFInt
454459
nfToCFType _ _ (NPrimVal _ $ PrT IntegerType) = pure CFInteger
455460
nfToCFType _ _ (NPrimVal _ $ PrT Bits8Type) = pure CFUnsigned8
@@ -469,15 +474,15 @@ nfToCFType _ _ (NPrimVal _ $ PrT WorldType) = pure CFWorld
469474
nfToCFType _ False (NBind fc _ (Pi _ _ _ ty) sc)
470475
= do defs <- get Ctxt
471476
sty <- nfToCFType fc False !(evalClosure defs ty)
472-
sc' <- sc defs (toClosure defaultOpts [] (Erased fc Placeholder))
477+
sc' <- sc defs (toClosure defaultOpts ScopeEmpty (Erased fc Placeholder))
473478
tty <- nfToCFType fc False sc'
474479
pure (CFFun sty tty)
475480
nfToCFType _ True (NBind fc _ _ _)
476481
= throw (GenericMsg fc "Function types not allowed in a foreign struct")
477482
nfToCFType _ s (NTCon fc n_in _ _ args)
478483
= do defs <- get Ctxt
479484
n <- toFullNames n_in
480-
case !(getNArgs defs n $ map snd args) of
485+
case !(getNArgs defs n $ toList (map snd args)) of
481486
User un uargs =>
482487
do nargs <- traverse (evalClosure defs) uargs
483488
cargs <- traverse (nfToCFType fc s) nargs
@@ -504,24 +509,24 @@ nfToCFType _ s (NErased _ _)
504509
= pure (CFUser (UN (Basic "__")) [])
505510
nfToCFType fc s t
506511
= do defs <- get Ctxt
507-
ty <- quote defs [] t
512+
ty <- quote defs ScopeEmpty t
508513
throw (GenericMsg (getLoc t)
509514
("Can't marshal type for foreign call " ++
510515
show !(toFullNames ty)))
511516

512517
getCFTypes : {auto c : Ref Ctxt Defs} ->
513-
List CFType -> NF [] ->
518+
List CFType -> ClosedNF ->
514519
Core (List CFType, CFType)
515520
getCFTypes args (NBind fc _ (Pi _ _ _ ty) sc)
516521
= do defs <- get Ctxt
517522
aty <- nfToCFType fc False !(evalClosure defs ty)
518-
sc' <- sc defs (toClosure defaultOpts [] (Erased fc Placeholder))
523+
sc' <- sc defs (toClosure defaultOpts ScopeEmpty (Erased fc Placeholder))
519524
getCFTypes (aty :: args) sc'
520525
getCFTypes args t
521526
= pure (reverse args, !(nfToCFType (getLoc t) False t))
522527

523-
lamRHSenv : Int -> FC -> (ns : List Name) -> (SizeOf ns, SubstCEnv ns [])
524-
lamRHSenv i fc [] = (zero, [])
528+
lamRHSenv : Int -> FC -> (ns : Scope) -> (SizeOf ns, SubstCEnv ns ScopeEmpty)
529+
lamRHSenv i fc [] = (zero, ScopeEmpty)
525530
lamRHSenv i fc (n :: ns)
526531
= let (s, env) = lamRHSenv (i + 1) fc ns in
527532
(suc s, CRef fc (MN "x" i) :: env)
@@ -531,7 +536,7 @@ mkBounds [] = None
531536
mkBounds (x :: xs) = Add x x (mkBounds xs)
532537

533538
getNewArgs : {done : _} ->
534-
SubstCEnv done args -> List Name
539+
SubstCEnv done args -> Scope
535540
getNewArgs [] = []
536541
getNewArgs (CRef _ n :: xs) = n :: getNewArgs xs
537542
getNewArgs {done = x :: xs} (_ :: sub) = x :: getNewArgs sub
@@ -540,16 +545,16 @@ getNewArgs {done = x :: xs} (_ :: sub) = x :: getNewArgs sub
540545
-- we have to assume arity 0 for incremental compilation because
541546
-- we have no idea how it's defined, and when we made calls to the
542547
-- function, they had arity 0.
543-
lamRHS : (ns : List Name) -> CExp ns -> CExp []
548+
lamRHS : (ns : Scope) -> CExp ns -> ClosedCExp
544549
lamRHS ns tm
545550
= let (s, env) = lamRHSenv 0 (getFC tm) ns
546551
tmExp = substs s env (rewrite appendNilRightNeutral ns in tm)
547552
newArgs = reverse $ getNewArgs env
548553
bounds = mkBounds newArgs
549-
expLocs = mkLocals zero {vars = []} bounds tmExp in
554+
expLocs = mkLocals zero {vars = ScopeEmpty} bounds tmExp in
550555
lamBind (getFC tm) _ expLocs
551556
where
552-
lamBind : FC -> (ns : List Name) -> CExp ns -> CExp []
557+
lamBind : FC -> (ns : Scope) -> CExp ns -> ClosedCExp
553558
lamBind fc [] tm = tm
554559
lamBind fc (n :: ns) tm = lamBind fc ns (CLam fc n tm)
555560

@@ -566,7 +571,7 @@ toCDef n ty erased (PMDef pi args _ tree _)
566571
else MkFun args' (shrinkCExp p comptree)
567572
where
568573
toLam : Bool -> CDef -> CDef
569-
toLam True (MkFun args rhs) = MkFun [] (lamRHS args rhs)
574+
toLam True (MkFun args rhs) = MkFun ScopeEmpty (lamRHS args rhs)
570575
toLam _ d = d
571576
toCDef n ty _ (ExternDef arity)
572577
= let (ns ** args) = mkArgList 0 arity in
@@ -580,7 +585,7 @@ toCDef n ty _ (ExternDef arity)
580585
getVars (ConsArg a rest) = MkVar First :: map weakenVar (getVars rest)
581586
toCDef n ty _ (ForeignDef arity cs)
582587
= do defs <- get Ctxt
583-
(atys, retty) <- getCFTypes [] !(nf defs [] ty)
588+
(atys, retty) <- getCFTypes [] !(nf defs ScopeEmpty ty)
584589
pure $ MkForeign cs atys retty
585590
toCDef n ty _ (Builtin {arity} op)
586591
= let (ns ** args) = mkArgList 0 arity in
@@ -595,7 +600,7 @@ toCDef n ty _ (Builtin {arity} op)
595600
toCDef n _ _ (DCon tag arity pos)
596601
= do let nt = snd <$> pos
597602
defs <- get Ctxt
598-
args <- numArgs {vars = []} defs (Ref EmptyFC (DataCon tag arity) n)
603+
args <- numArgs {vars = ScopeEmpty} defs (Ref EmptyFC (DataCon tag arity) n)
599604
let arity' = case args of
600605
NewTypeBy ar _ => ar
601606
EraseArgs ar erased => ar `minus` length erased
@@ -620,7 +625,7 @@ toCDef n ty _ def
620625

621626
export
622627
compileExp : {auto c : Ref Ctxt Defs} ->
623-
ClosedTerm -> Core (CExp [])
628+
ClosedTerm -> Core ClosedCExp
624629
compileExp tm
625630
= do s <- newRef NextMN 0
626631
exp <- toCExp (UN $ Basic "main") tm

0 commit comments

Comments
 (0)