Skip to content

Commit b94f7e9

Browse files
committed
Strip current namespace when writing out defs
This saves writing the same strings over and over again in the body of a definition. At the cost of a traversal on reading and writing the ttcs, there is a good bit less to write out.
1 parent d51fe89 commit b94f7e9

File tree

6 files changed

+125
-23
lines changed

6 files changed

+125
-23
lines changed

src/Compiler/Common.idr

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ execute {c} cg tm
110110
-- If an entry isn't already decoded, get the minimal entry we need for
111111
-- compilation, and record the Binary so that we can put it back when we're
112112
-- done (so that we don't obliterate the definition)
113-
getMinimalDef : ContextEntry -> Core (GlobalDef, Maybe Binary)
113+
getMinimalDef : ContextEntry -> Core (GlobalDef, Maybe (Namespace, Binary))
114114
getMinimalDef (Decoded def) = pure (def, Nothing)
115-
getMinimalDef (Coded bin)
115+
getMinimalDef (Coded ns bin)
116116
= do b <- newRef Bin bin
117117
cdef <- fromBuf b
118118
refsRList <- fromBuf b
@@ -125,12 +125,12 @@ getMinimalDef (Coded bin)
125125
[] Public (MkTotality Unchecked IsCovering)
126126
[] Nothing refsR False False True
127127
None cdef Nothing []
128-
pure (def, Just bin)
128+
pure (def, Just (ns, bin))
129129

130130
-- ||| Recursively get all calls in a function definition
131131
getAllDesc : {auto c : Ref Ctxt Defs} ->
132132
List Name -> -- calls to check
133-
IOArray (Int, Maybe Binary) ->
133+
IOArray (Int, Maybe (Namespace, Binary)) ->
134134
-- which nodes have been visited. If the entry is
135135
-- present, it's visited. Keep the binary entry, if
136136
-- we partially decoded it, so that we can put back
@@ -174,10 +174,10 @@ getNamedDef n
174174
pure (Just (n, location def, d))
175175

176176
replaceEntry : {auto c : Ref Ctxt Defs} ->
177-
(Int, Maybe Binary) -> Core ()
177+
(Int, Maybe (Namespace, Binary)) -> Core ()
178178
replaceEntry (i, Nothing) = pure ()
179-
replaceEntry (i, Just b)
180-
= ignore $ addContextEntry (Resolved i) b
179+
replaceEntry (i, Just (ns, b))
180+
= ignore $ addContextEntry ns (Resolved i) b
181181

182182
natHackNames : List Name
183183
natHackNames

src/Core/Binary.idr

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,13 @@ getSaveDefs modns (n :: ns) acc defs
251251
case definition gdef of
252252
Builtin _ => getSaveDefs modns ns acc defs
253253
_ => do bin <- initBinaryS 16384
254-
toBuf bin !(full (gamma defs) gdef)
254+
toBuf bin (trimNS modns !(full (gamma defs) gdef))
255255
b <- get Bin
256-
getSaveDefs modns ns ((trimNS (fullname gdef), b) :: acc) defs
256+
getSaveDefs modns ns ((trimName (fullname gdef), b) :: acc) defs
257257
where
258-
trimNS : Name -> Name
259-
trimNS n@(NS defns d) = if defns == modns then d else n
260-
trimNS n = n
258+
trimName : Name -> Name
259+
trimName n@(NS defns d) = if defns == modns then d else n
260+
trimName n = n
261261

262262
-- Write out the things in the context which have been defined in the
263263
-- current source file
@@ -295,9 +295,10 @@ writeToTTC extradata fname
295295
pure ()
296296

297297
addGlobalDef : {auto c : Ref Ctxt Defs} ->
298-
(modns : ModuleIdent) -> (importAs : Maybe Namespace) ->
298+
(modns : ModuleIdent) -> Namespace ->
299+
(importAs : Maybe Namespace) ->
299300
(Name, Binary) -> Core ()
300-
addGlobalDef modns asm (n, def)
301+
addGlobalDef modns filens asm (n, def)
301302
= do defs <- get Ctxt
302303
codedentry <- lookupContextEntry n (gamma defs)
303304
-- Don't update the coded entry because some names might not be
@@ -307,7 +308,7 @@ addGlobalDef modns asm (n, def)
307308
pure (Just x))
308309
codedentry
309310
unless (completeDef entry) $
310-
ignore $ addContextEntry n def
311+
ignore $ addContextEntry filens n def
311312

312313
whenJust asm $ \ as => addContextAlias (asName modns as n) n
313314

@@ -450,7 +451,7 @@ readFromTTC nestedns loc reexp fname modNS importAs
450451
else do
451452
ttc <- readTTCFile True fname as bin
452453
let ex = extraData ttc
453-
traverse_ (addGlobalDef modNS as) (context ttc)
454+
traverse_ (addGlobalDef modNS (currentNS ttc) as) (context ttc)
454455
traverse_ addUserHole (userHoles ttc)
455456
setNS (currentNS ttc)
456457
when nestedns $ setNestedNS (nestedNS ttc)

src/Core/CaseTree.idr

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,32 @@ isDefault : CaseAlt vars -> Bool
5050
isDefault (DefaultCase _) = True
5151
isDefault _ = False
5252

53+
mutual
54+
export
55+
StripNamespace (CaseTree vars) where
56+
trimNS ns (Case idx p scTy xs)
57+
= Case idx p (trimNS ns scTy) (map (trimNS ns) xs)
58+
trimNS ns (STerm x t) = STerm x (trimNS ns t)
59+
trimNS ns c = c
60+
61+
restoreNS ns (Case idx p scTy xs)
62+
= Case idx p (restoreNS ns scTy) (map (restoreNS ns) xs)
63+
restoreNS ns (STerm x t) = STerm x (restoreNS ns t)
64+
restoreNS ns c = c
65+
66+
export
67+
StripNamespace (CaseAlt vars) where
68+
trimNS ns (ConCase x tag args t) = ConCase x tag args (trimNS ns t)
69+
trimNS ns (DelayCase ty arg t) = DelayCase ty arg (trimNS ns t)
70+
trimNS ns (ConstCase x t) = ConstCase x (trimNS ns t)
71+
trimNS ns (DefaultCase t) = DefaultCase (trimNS ns t)
72+
73+
restoreNS ns (ConCase x tag args t) = ConCase x tag args (restoreNS ns t)
74+
restoreNS ns (DelayCase ty arg t) = DelayCase ty arg (restoreNS ns t)
75+
restoreNS ns (ConstCase x t) = ConstCase x (restoreNS ns t)
76+
restoreNS ns (DefaultCase t) = DefaultCase (restoreNS ns t)
77+
78+
5379
public export
5480
data Pat : Type where
5581
PAs : FC -> Name -> Pat -> Pat

src/Core/Context.idr

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,8 @@ data Arr : Type where
311311
-- binary blob yet, so decode it first time
312312
public export
313313
data ContextEntry : Type where
314-
Coded : Binary -> ContextEntry
314+
Coded : Namespace -> -- namespace for decoding into, with restoreNS
315+
Binary -> ContextEntry
315316
Decoded : GlobalDef -> ContextEntry
316317

317318
data PossibleName : Type where
@@ -791,7 +792,6 @@ HasNames (Term vars) where
791792

792793
export
793794
HasNames Pat where
794-
795795
full gam (PAs fc n p)
796796
= [| PAs (pure fc) (full gam n) (full gam p) |]
797797
full gam (PCon fc n i ar ps)
@@ -918,6 +918,31 @@ HasNames Def where
918918
= pure $ Guess !(resolved gam tm) b cs
919919
resolved gam t = pure t
920920

921+
export
922+
StripNamespace Def where
923+
trimNS ns (PMDef i args ct rt pats)
924+
= PMDef i args (trimNS ns ct) rt (map trimNSpat pats)
925+
where
926+
trimNSpat : (vs ** (Env Term vs, Term vs, Term vs)) ->
927+
(vs ** (Env Term vs, Term vs, Term vs))
928+
trimNSpat (vs ** (env, lhs, rhs))
929+
= (vs ** (env, trimNS ns lhs, trimNS ns rhs))
930+
trimNS ns d = d
931+
932+
restoreNS ns (PMDef i args ct rt pats)
933+
= PMDef i args (restoreNS ns ct) rt (map restoreNSpat pats)
934+
where
935+
restoreNSpat : (vs ** (Env Term vs, Term vs, Term vs)) ->
936+
(vs ** (Env Term vs, Term vs, Term vs))
937+
restoreNSpat (vs ** (env, lhs, rhs))
938+
= (vs ** (env, restoreNS ns lhs, restoreNS ns rhs))
939+
restoreNS ns d = d
940+
941+
export
942+
StripNamespace GlobalDef where
943+
trimNS ns def = record { definition $= trimNS ns } def
944+
restoreNS ns def = record { definition $= restoreNS ns } def
945+
921946
HasNames (NameMap a) where
922947
full gam nmap
923948
= insertAll empty (toList nmap)
@@ -1294,10 +1319,10 @@ addDef n def
12941319

12951320
export
12961321
addContextEntry : {auto c : Ref Ctxt Defs} ->
1297-
Name -> Binary -> Core Int
1298-
addContextEntry n def
1322+
Namespace -> Name -> Binary -> Core Int
1323+
addContextEntry ns n def
12991324
= do defs <- get Ctxt
1300-
(idx, gam') <- addEntry n (Coded def) (gamma defs)
1325+
(idx, gam') <- addEntry n (Coded ns def) (gamma defs)
13011326
put Ctxt (record { gamma = gam' } defs)
13021327
pure idx
13031328

src/Core/TT.idr

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,56 @@ data Term : List Name -> Type where
743743
Term vars
744744
TType : FC -> Term vars
745745

746+
-- Remove/restore the given namespace from all Refs. This is to allow
747+
-- writing terms and case trees to disk without repeating the same namespace
748+
-- all over the place.
749+
public export
750+
interface StripNamespace a where
751+
trimNS : Namespace -> a -> a
752+
restoreNS : Namespace -> a -> a
753+
754+
export
755+
StripNamespace (Term vars) where
756+
trimNS ns tm@(Ref fc x (NS tns n))
757+
= if ns == tns then Ref fc x (NS (unsafeFoldNamespace []) n) else tm
758+
-- ^ A blank namespace, rather than a UN, so we don't catch primitive
759+
-- names which are represented as UN.
760+
trimNS ns (Meta fc x y xs)
761+
= Meta fc x y (map (trimNS ns) xs)
762+
trimNS ns (Bind fc x b scope)
763+
= Bind fc x (map (trimNS ns) b) (trimNS ns scope)
764+
trimNS ns (App fc fn arg)
765+
= App fc (trimNS ns fn) (trimNS ns arg)
766+
trimNS ns (As fc s p tm)
767+
= As fc s (trimNS ns p) (trimNS ns tm)
768+
trimNS ns (TDelayed fc x y)
769+
= TDelayed fc x (trimNS ns y)
770+
trimNS ns (TDelay fc x t y)
771+
= TDelay fc x (trimNS ns t) (trimNS ns y)
772+
trimNS ns (TForce fc r y)
773+
= TForce fc r (trimNS ns y)
774+
trimNS ns tm = tm
775+
776+
restoreNS ns tm@(Ref fc x (NS tmns n))
777+
= if isNil (unsafeUnfoldNamespace tmns)
778+
then Ref fc x (NS ns n)
779+
else tm
780+
restoreNS ns (Meta fc x y xs)
781+
= Meta fc x y (map (restoreNS ns) xs)
782+
restoreNS ns (Bind fc x b scope)
783+
= Bind fc x (map (restoreNS ns) b) (restoreNS ns scope)
784+
restoreNS ns (App fc fn arg)
785+
= App fc (restoreNS ns fn) (restoreNS ns arg)
786+
restoreNS ns (As fc s p tm)
787+
= As fc s (restoreNS ns p) (restoreNS ns tm)
788+
restoreNS ns (TDelayed fc x y)
789+
= TDelayed fc x (restoreNS ns y)
790+
restoreNS ns (TDelay fc x t y)
791+
= TDelay fc x (restoreNS ns t) (restoreNS ns y)
792+
restoreNS ns (TForce fc r y)
793+
= TForce fc r (restoreNS ns y)
794+
restoreNS ns tm = tm
795+
746796
export
747797
isErased : Term vars -> Bool
748798
isErased (Erased _ _) = True

src/Core/TTC.idr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,12 +1079,12 @@ TTC Transform where
10791079
pure (MkTransform {vars} n env lhs rhs)
10801080

10811081
-- decode : Context -> Int -> (update : Bool) -> ContextEntry -> Core GlobalDef
1082-
Core.Context.decode gam idx update (Coded bin)
1082+
Core.Context.decode gam idx update (Coded ns bin)
10831083
= do b <- newRef Bin bin
10841084
def <- fromBuf b
10851085
let a = getContent gam
10861086
arr <- get Arr
1087-
def' <- resolved gam def
1087+
def' <- resolved gam (restoreNS ns def)
10881088
when update $ coreLift $ writeArray arr idx (Decoded def')
10891089
pure def'
10901090
Core.Context.decode gam idx update (Decoded def) = pure def

0 commit comments

Comments
 (0)