Skip to content

Commit 8230ceb

Browse files
spcfoxGulinSS
authored andcommitted
ScopedSnocList: Cleanup and refactor
1 parent 0a3ca35 commit 8230ceb

File tree

17 files changed

+112
-111
lines changed

17 files changed

+112
-111
lines changed

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/CompileExpr.idr

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,6 @@ import Libraries.Data.SnocList.Extra
2323

2424
%default covering
2525

26-
-- For ease of type level reasoning!
27-
public export
28-
rev : SnocList a -> SnocList a
29-
rev [<] = [<]
30-
rev (xs :< x) = [<x] ++ rev xs
31-
3226
data Args
3327
= NewTypeBy Nat Nat
3428
| EraseArgs Nat (List Nat)

src/Compiler/Inline.idr

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,11 @@ import Data.Maybe
1818
import Data.List
1919
import Data.SnocList
2020
import Data.Vect
21-
import Libraries.Data.List.LengthMatch
21+
2222
import Libraries.Data.NameMap
2323
import Libraries.Data.WithDefault
24-
2524
import Libraries.Data.List.SizeOf
26-
import Libraries.Data.SnocList.LengthMatch
2725
import Libraries.Data.SnocList.SizeOf
28-
import Libraries.Data.SnocList.HasLength
2926
import Libraries.Data.SnocList.Extra
3027

3128
%default covering

src/Compiler/LambdaLift.idr

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import Core.TT
1818

1919
import Data.List
2020
import Data.SnocList
21+
import Data.SnocList.Operations
2122
import Data.Vect
2223

2324
import Libraries.Data.SnocList.Extra
@@ -360,31 +361,19 @@ record Used (vars : SnocList Name) where
360361
initUsed : {vars : _} -> Used vars
361362
initUsed {vars} = MkUsed (replicate (length vars) False)
362363

363-
lengthDistributesOverAppend
364-
: (xs, ys : SnocList a)
365-
-> length (ys ++ xs) = length xs + length ys
366-
lengthDistributesOverAppend [<] ys = Refl
367-
lengthDistributesOverAppend (xs :< x) ys =
368-
cong S $ lengthDistributesOverAppend xs ys
369-
370364
weakenUsed : {outer : _} -> Used vars -> Used (vars ++ outer)
371365
weakenUsed {outer} (MkUsed xs) =
372-
MkUsed (rewrite lengthDistributesOverAppend outer vars in
373-
(replicate (length outer) False ++ xs))
374-
375-
-- TODO
376-
-- lengthDistributesOverAppendFish
377-
-- : (xs : List a)
378-
-- -> (ys : SnocList a)
379-
-- -> length (ys <>< xs) = length xs + length ys
366+
MkUsed (rewrite lengthHomomorphism vars outer in
367+
rewrite plusCommutative (length vars) (length outer) in
368+
replicate (length outer) False ++ xs)
380369

381370
weakenUsedFish : {outer : _} -> Used vars -> Used (vars <>< outer)
382371
weakenUsedFish {outer} (MkUsed xs) =
383372
do rewrite fishAsSnocAppend vars outer
384-
MkUsed $ do
385-
rewrite lengthDistributesOverAppend (cast outer) vars
386-
-- See lengthDistributesOverAppendFish
387-
(believe_me $ replicate (length outer) False ++ xs)
373+
MkUsed $ do rewrite lengthHomomorphism vars (cast outer)
374+
rewrite Extra.lengthDistributesOverFish [<] outer
375+
rewrite plusCommutative (length vars) (length outer)
376+
replicate (length outer) False ++ xs
388377

389378
contractUsed : (Used (vars :< x)) -> Used vars
390379
contractUsed (MkUsed xs) = MkUsed (tail xs)

src/Core/Case/CaseBuilder.idr

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,9 @@ covering
222222
where
223223
showAll : {vs, ts : _} -> NamedPats vs ts -> String
224224
showAll [] = ""
225-
showAll {ts = _ :< t } [x]
225+
showAll {ts = _ :< t} [x]
226226
= show t ++ " " ++ show (pat x) ++ " [" ++ show (argType x) ++ "]"
227-
showAll {ts = _ :< t } (x :: xs)
227+
showAll {ts = _ :< t} (x :: xs)
228228
= show t ++ " " ++ show (pat x) ++ " [" ++ show (argType x) ++ "]"
229229
++ ", " ++ showAll xs
230230

@@ -233,7 +233,7 @@ covering
233233
where
234234
prettyAll : {vs, ts : _} -> NamedPats vs ts -> List (Doc IdrisSyntax)
235235
prettyAll [] = []
236-
prettyAll {ts = _ :< t } (x :: xs)
236+
prettyAll {ts = _ :< t} (x :: xs)
237237
= parens (pretty0 t <++> equals <++> pretty (pat x))
238238
:: prettyAll xs
239239

@@ -597,10 +597,6 @@ updatePatNames _ [] = []
597597
updatePatNames ns (pi :: ps)
598598
= { pat $= update } pi :: updatePatNames ns ps
599599
where
600-
lookup : Name -> SnocList (Name, Name) -> Maybe Name
601-
lookup n [<] = Nothing
602-
lookup n (ns :< (x, n')) = if x == n then Just n' else lookup n ns
603-
604600
update : Pat -> Pat
605601
update (PAs fc n p)
606602
= case lookup n ns of
@@ -1226,7 +1222,7 @@ mkPat args orig (Ref fc (DataCon t a) n) = pure $ PCon fc n t a (rev args)
12261222
mkPat args orig (Ref fc (TyCon t a) n) = pure $ PTyCon fc n a (rev args)
12271223
mkPat args orig (Ref fc Func n)
12281224
= do prims <- getPrimitiveNames
1229-
mtm <- normalisePrims (const True) isPConst True prims n args orig [<]
1225+
mtm <- normalisePrims (const True) isPConst True prims n (cast args) orig [<]
12301226
case mtm of
12311227
Just tm => if tm /= orig -- check we made progress; if there's an
12321228
-- unresolved interface, we might be stuck

src/Core/Context/Context.idr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ export
195195
covering
196196
Show Clause where
197197
show (MkClause {vars} env lhs rhs)
198-
= show (toList $ reverse vars) ++ ": " ++ show lhs ++ " = " ++ show rhs
198+
= show (asList vars) ++ ": " ++ show lhs ++ " = " ++ show rhs
199199

200200
public export
201201
data DefFlag

src/Core/Env.idr

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,6 @@ bindEnv loc (env :< b) tm
7878
Explicit
7979
(binderType b)) tm)
8080

81-
public export
82-
revNs : (vs, ns : SnocList a) -> reverse vs ++ reverse ns = reverse (ns ++ vs)
83-
revNs [<] ns = rewrite appendLinLeftNeutral (reverse ns) in Refl
84-
revNs (vs :< v) ns
85-
= rewrite Extra.revOnto [<v] vs in
86-
rewrite Extra.revOnto [<v] (ns ++ vs) in
87-
rewrite sym $ revNs vs ns in
88-
rewrite appendAssociative [<v] (reverse vs) (reverse ns) in Refl
89-
9081
-- Weaken by all the names at once at the end, to save multiple traversals
9182
-- in big environments
9283
-- Also reversing the names at the end saves significant time over concatenating

src/Core/Normalise.idr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ normalisePrims : {auto c : Ref Ctxt Defs} -> {vs : _} ->
362362
List Name ->
363363
-- view of the potential redex
364364
(n : Name) -> -- function name
365-
(args : SnocList arg) -> -- arguments from inside out (arg1, ..., argk)
365+
(args : List arg) -> -- arguments from inside out (arg1, ..., argk)
366366
-- actual term to evaluate if needed
367367
(tm : Term vs) -> -- original term (n arg1 ... argk)
368368
Env Term vs -> -- evaluation environment
@@ -371,7 +371,7 @@ normalisePrims : {auto c : Ref Ctxt Defs} -> {vs : _} ->
371371
normalisePrims boundSafe viewConstant all prims n args tm env
372372
= do let True = isPrimName prims !(getFullName n) -- is a primitive
373373
| _ => pure Nothing
374-
let (_ :< mc) = reverse args -- with at least one argument
374+
let (mc :: _) = args -- with at least one argument
375375
| _ => pure Nothing
376376
let (Just c) = viewConstant mc -- that is a constant
377377
| _ => pure Nothing

src/Core/Normalise/Convert.idr

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import Data.List
1414
import Data.SnocList
1515

1616
import Libraries.Data.List.SizeOf
17+
import Libraries.Data.SnocList.HasLength
18+
import Libraries.Data.SnocList.SizeOf
1719

1820
%default covering
1921

@@ -369,10 +371,12 @@ mutual
369371

370372
convGen q inf defs env (NApp fc val args) (NApp _ val' args')
371373
= if !(chkConvHead q inf defs env val val')
372-
then do i <- getInfPos val
373-
allConv q inf defs env
374-
(cast {from = List (FC, Closure vars)} $ dropInf 0 i $ cast args) -- TODO: UGH!
375-
(cast {from = List (FC, Closure vars)} $ dropInf 0 i $ cast args')
374+
then case !(getInfPos val) of
375+
[] => allConv q inf defs env args args'
376+
i => allConv q inf defs env
377+
(dropInf i args $ mkSizeOf args)
378+
(dropInf i args' $ mkSizeOf args')
379+
376380
else chkConvCaseBlock fc q inf defs env val args1 val' args2
377381
where
378382
getInfPos : NHead vars -> Core (List Nat)
@@ -384,13 +388,12 @@ mutual
384388
else pure []
385389
getInfPos _ = pure []
386390

387-
dropInf : Nat -> List Nat -> List a -> List a
388-
dropInf _ [] xs = xs
389-
dropInf _ _ [] = []
390-
dropInf i ds (x :: xs)
391+
dropInf : List Nat -> (sx : SnocList a) -> SizeOf sx -> SnocList a
392+
dropInf _ [<] _ = [<]
393+
dropInf ds (sx :< x) (MkSizeOf (S i) (S p))
391394
= if i `elem` ds
392-
then dropInf (S i) ds xs
393-
else x :: dropInf (S i) ds xs
395+
then dropInf ds sx (MkSizeOf i p)
396+
else dropInf ds sx (MkSizeOf i p) :< x
394397

395398
-- Discard file context information irrelevant for conversion checking
396399
args1 : SnocList (Closure vars)

src/Core/SchemeEval/Compile.idr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ reverseOnto acc [<] = acc
9797
reverseOnto acc (sx :< x) = reverseOnto (acc :< x) sx
9898

9999
reverse : SchVars vars -> SchVars (reverse vars)
100-
reverse sx = reverseOnto [<] sx
100+
reverse = reverseOnto [<]
101101

102102
getSchVar : {idx : _} -> (0 _ : IsVar n idx vars) -> SchVars vars -> String
103103
getSchVar First (xs :< Bound x) = x

src/Core/Unify.idr

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,10 @@ unifyArgs : (Unify tm, Quote tm) =>
273273
{auto c : Ref Ctxt Defs} ->
274274
{auto u : Ref UST UState} ->
275275
UnifyInfo -> FC -> Env Term vars ->
276-
SnocList (tm vars) -> SnocList (tm vars) ->
276+
List (tm vars) -> List (tm vars) ->
277277
Core UnifyResult
278-
unifyArgs mode loc env [<] [<] = pure success
279-
unifyArgs mode loc env (cxs :< cx) (cys :< cy)
278+
unifyArgs mode loc env [] [] = pure success
279+
unifyArgs mode loc env (cx :: cxs) (cy :: cys)
280280
= do -- Do later arguments first, since they may depend on earlier
281281
-- arguments and use their solutions.
282282
cs <- unifyArgs mode loc env cxs cys
@@ -286,6 +286,24 @@ unifyArgs mode loc env (cxs :< cx) (cys :< cy)
286286
pure (union res cs)
287287
unifyArgs mode loc env _ _ = ufail loc ""
288288

289+
unifySpine : (Unify tm, Quote tm) =>
290+
{vars : _} ->
291+
{auto c : Ref Ctxt Defs} ->
292+
{auto u : Ref UST UState} ->
293+
UnifyInfo -> FC -> Env Term vars ->
294+
SnocList (tm vars) -> SnocList (tm vars) ->
295+
Core UnifyResult
296+
unifySpine mode loc env [<] [<] = pure success
297+
unifySpine mode loc env (cxs :< cx) (cys :< cy)
298+
= do -- Do later arguments first, since they may depend on earlier
299+
-- arguments and use their solutions.
300+
res <- unify (lower mode) loc env cx cy
301+
logC "unify" 20 $ pure $ "unify done: " ++ show res
302+
cs <- unifySpine mode loc env cxs cys
303+
logC "unify" 20 $ pure $ "unifySpine done: " ++ show cs
304+
pure (union cs res)
305+
unifySpine mode loc env _ _ = ufail loc ""
306+
289307
-- Get the variables in an application argument list; fail if any arguments
290308
-- are not variables, fail if there's any repetition of variables
291309
-- We use this to check that the pattern unification rule is applicable
@@ -460,7 +478,7 @@ tryInstantiate : {auto c : Ref Ctxt Defs} ->
460478
Term newvars -> -- shrunk environment
461479
Core Bool -- postpone if the type is yet unknown
462480
tryInstantiate {newvars} loc mode env mname mref num mdef locs otm tm
463-
= do logTerm "unify.instantiate" 5 ("Instantiating in " ++ show !(traverse toFullNames (reverse $ toList newvars))) !(toFullNames tm)
481+
= do logTerm "unify.instantiate" 5 ("Instantiating in " ++ show !(traverse toFullNames (asList newvars))) !(toFullNames tm)
464482
-- let Hole _ _ = definition mdef
465483
-- | def => ufail {a=()} loc (show mname ++ " already resolved as " ++ show def)
466484
case fullname mdef of
@@ -473,7 +491,7 @@ tryInstantiate {newvars} loc mode env mname mref num mdef locs otm tm
473491
logTerm "unify.instantiate" 5 ("Type: " ++ show !(toFullNames mname)) (type mdef)
474492
logTerm "unify.instantiate" 5 ("Type: " ++ show mname) ty
475493
log "unify.instantiate" 5 ("With locs: " ++ show locs)
476-
log "unify.instantiate" 5 ("From vars: " ++ show (reverse $ toList newvars))
494+
log "unify.instantiate" 5 ("From vars: " ++ show (asList newvars))
477495

478496
defs <- get Ctxt
479497
-- Try to instantiate the hole
@@ -1001,7 +1019,7 @@ mutual
10011019
-- for *all* possible values, we can safely unify the arguments.
10021020
unifyBothApps mode@(MkUnifyInfo p InTerm) loc env xfc (NLocal xr x xp) xargs yfc (NLocal yr y yp) yargs
10031021
= if x == y
1004-
then unifyArgs mode loc env (reverse $ map snd xargs) (reverse $ map snd yargs)
1022+
then unifySpine mode loc env (map snd xargs) (map snd yargs)
10051023
else postpone loc mode "Postponing local app"
10061024
env (NApp xfc (NLocal xr x xp) xargs)
10071025
(NApp yfc (NLocal yr y yp) yargs)
@@ -1015,8 +1033,8 @@ mutual
10151033
if xi == yi && (invx || umode mode == InSearch)
10161034
-- Invertible, (from auto implicit search)
10171035
-- so we can also unify the arguments.
1018-
then unifyArgs mode loc env (reverse $ map snd $ xargs' ++ xargs)
1019-
(reverse $ map snd $ yargs' ++ yargs)
1036+
then unifySpine mode loc env (map snd $ xargs' ++ xargs)
1037+
(map snd $ yargs' ++ yargs)
10201038
else do xlocs <- localsIn xargs
10211039
ylocs <- localsIn yargs
10221040
-- Solve the one with the bigger context, and if they're
@@ -1054,7 +1072,7 @@ mutual
10541072
(NApp yfc (NMeta yn yi yargs) yargs')
10551073
unifyBothApps mode@(MkUnifyInfo p InSearch) loc env xfc fx@(NRef xt hdx) xargs yfc fy@(NRef yt hdy) yargs
10561074
= if hdx == hdy
1057-
then unifyArgs mode loc env (reverse $ map snd xargs) (reverse $ map snd yargs)
1075+
then unifySpine mode loc env (map snd xargs) (map snd yargs)
10581076
else unifyApp False mode loc env xfc fx xargs (NApp yfc fy yargs)
10591077
unifyBothApps mode@(MkUnifyInfo p InMatch) loc env xfc fx@(NRef xt hdx) xargs yfc fy@(NRef yt hdy) yargs
10601078
= if hdx == hdy
@@ -1063,7 +1081,7 @@ mutual
10631081
xs <- traverse (quote defs env) (map snd xargs)
10641082
ys <- traverse (quote defs env) (map snd yargs)
10651083
pure ("Matching args " ++ show xs ++ " " ++ show ys))
1066-
unifyArgs mode loc env (reverse $ map snd xargs) (reverse $ map snd yargs)
1084+
unifySpine mode loc env (map snd xargs) (map snd yargs)
10671085
else unifyApp False mode loc env xfc fx xargs (NApp yfc fy yargs)
10681086
unifyBothApps mode loc env xfc fx ax yfc fy ay
10691087
= unifyApp False mode loc env xfc fx ax (NApp yfc fy ay)
@@ -1183,7 +1201,7 @@ mutual
11831201
log "unify" 20 "WITH:"
11841202
traverse_ (dumpArg env) ys
11851203
-}
1186-
unifyArgs mode loc env (reverse $ map snd xs) (reverse $ map snd ys)
1204+
unifySpine mode loc env (map snd xs) (map snd ys)
11871205
else convertError loc env
11881206
(NDCon xfc x tagx ax xs)
11891207
(NDCon yfc y tagy ay ys)
@@ -1194,14 +1212,11 @@ mutual
11941212
pure $ "Comparing type constructors " ++ show x ++ " and " ++ show y
11951213
if x == y
11961214
then do -- [Note] Restore logging sequence
1197-
let xs = reverse $ map snd xs
1198-
let ys = reverse $ map snd ys
1199-
12001215
logC "unify" 20 $
12011216
pure $ "Constructor " ++ show x
1202-
logC "unify" 20 $ map (const "xs ↑") $ traverse_ (dumpArg env) $ xs
1203-
logC "unify" 20 $ map (const "ys ↑") $ traverse_ (dumpArg env) $ ys
1204-
unifyArgs mode loc env xs ys
1217+
logC "unify" 20 $ map (const "xs ↑") $ traverse_ (dumpArg env) $ reverse $ map snd xs
1218+
logC "unify" 20 $ map (const "ys ↑") $ traverse_ (dumpArg env) $ reverse $ map snd ys
1219+
unifySpine mode loc env (map snd xs) (map snd ys)
12051220
-- TODO: Type constructors are not necessarily injective.
12061221
-- If we don't know it's injective, need to postpone the
12071222
-- constraint. But before then, we need some way to decide
@@ -1215,10 +1230,10 @@ mutual
12151230
unifyNoEta mode loc env (NDelayed xfc _ x) (NDelayed yfc _ y)
12161231
= unify (lower mode) loc env x y
12171232
unifyNoEta mode loc env (NDelay xfc _ xty x) (NDelay yfc _ yty y)
1218-
= unifyArgs mode loc env [<xty, x] [<yty, y]
1233+
= unifyArgs mode loc env [xty, x] [yty, y]
12191234
unifyNoEta mode loc env (NForce xfc _ x axs) (NForce yfc _ y ays)
12201235
= do cs <- unify (lower mode) loc env x y
1221-
cs' <- unifyArgs mode loc env (reverse $ map snd axs) (reverse $ map snd ays)
1236+
cs' <- unifySpine mode loc env (map snd axs) (map snd ays)
12221237
pure (union cs cs')
12231238
unifyNoEta mode loc env x@(NApp xfc fx@(NMeta _ _ _) axs)
12241239
y@(NApp yfc fy@(NMeta _ _ _) ays)

src/Core/Value.idr

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,6 @@ export
129129
(++) sx Lin = sx
130130
(++) sx (sy :< y) = (sx ++ sy) :< y
131131

132-
export
133-
reverseOnto : LocalEnv free varsl -> LocalEnv free varsr -> LocalEnv free (varsl ++ reverse varsr)
134-
reverseOnto acc Lin = acc
135-
reverseOnto {varsr=[<] :< r} acc ([<] :< x) = reverseOnto {varsl=varsl :< r} (acc :< x) [<]
136-
reverseOnto {varsr=[<] :< vars :< r} acc (sx :< x) = reverseOnto {varsl=varsl :< r} (acc :< x) sx
137-
138-
export
139-
reverse : LocalEnv free vars -> LocalEnv free (reverse vars)
140-
reverse {vars} = rewrite sym $ appendLinLeftNeutral (reverse vars) in reverseOnto Lin
141-
142132
export
143133
cons : LocalEnv free vars -> Closure free -> LocalEnv free (v `cons` vars)
144134
cons [<] p = Lin :< p

0 commit comments

Comments
 (0)