0 | module Core.Context.Context
2 | import Core.Case.CaseTree
3 | import Core.CompileExpr
5 | import public Core.Name
6 | import public Core.Options.Log
7 | import public Core.TT
8 | import public Core.WithData
10 | import public Algebra.SizeChange
17 | import Libraries.Data.IntMap
18 | import Libraries.Data.NameMap
19 | import Libraries.Data.NatSet
20 | import Libraries.Data.UserNameMap
21 | import Libraries.Data.WithDefault
22 | import Libraries.Data.SparseMatrix
23 | import Libraries.Utils.Binary
24 | import Libraries.Utils.Scheme
26 | %hide LabelledValue.label
30 | data Ref : (l : label) -> Type -> Type where
32 | MkRef : IORef a -> Ref x a
40 | record PMDefInfo where
41 | constructor MkPMDefInfo
47 | %name PMDefInfo
pminfo
50 | defaultPI : PMDefInfo
51 | defaultPI = MkPMDefInfo NotHole False False
54 | record TypeFlags where
55 | constructor MkTypeFlags
60 | defaultFlags : TypeFlags
61 | defaultFlags = MkTypeFlags False False
64 | record HoleFlags where
65 | constructor MkHoleFlags
70 | holeInit : Bool -> HoleFlags
71 | holeInit b = MkHoleFlags b False
74 | data Def : Type where
76 | PMDef : (pminfo : PMDefInfo) ->
78 | (treeCT : CaseTree args) ->
79 | (treeRT : CaseTree args) ->
80 | (pats : List (vs : Scope ** (Env Term vs, Term vs, Term vs))) ->
87 | ExternDef : (arity : Nat) -> Def
88 | ForeignDef : (arity : Nat) ->
92 | Builtin : {arity : Nat} -> PrimFn arity -> Def
93 | DCon : (tag : Int) -> (arity : Nat) ->
94 | (newtypeArg : Maybe (Bool, Nat)) ->
103 | TCon : (arity : Nat) ->
104 | (parampos : NatSet) ->
105 | (detpos : NatSet) ->
106 | (flags : TypeFlags) ->
107 | (mutwith : List Name) ->
108 | (datacons : Maybe (List Name)) ->
109 | (detagabbleBy : Maybe NatSet) ->
114 | Hole : (numlocs : Nat) ->
118 | BySearch : RigCount -> (maxdepth : Nat) -> (defining : Name) -> Def
121 | Guess : (guess : ClosedTerm) ->
124 | (constraints : List Int) -> Def
127 | UniverseLevel : Integer -> Def
133 | defNameType : Def -> Maybe NameType
134 | defNameType None = Nothing
135 | defNameType (PMDef {}) = Just Func
136 | defNameType (ExternDef {}) = Just Func
137 | defNameType (ForeignDef {}) = Just Func
138 | defNameType (Builtin {}) = Just Func
139 | defNameType (DCon tag ar _) = Just (DataCon tag ar)
140 | defNameType (TCon ar _ _ _ _ _ _) = Just (TyCon ar)
141 | defNameType (Hole {}) = Just Func
142 | defNameType (BySearch {}) = Nothing
143 | defNameType (Guess {}) = Nothing
144 | defNameType ImpBind = Just Bound
145 | defNameType (UniverseLevel {}) = Nothing
146 | defNameType Delayed = Nothing
151 | show None = "undefined"
152 | show (PMDef _ args ct rt pats)
153 | = unlines [ show args ++ ";"
154 | , "Compile time tree: " ++ show ct
155 | , "Run time tree: " ++ show rt
158 | = "DataCon " ++ show t ++ " " ++ show a
159 | ++ maybe "" (\n => " (newtype by " ++ show n ++ ")") nt
160 | show (TCon a ps ds u ms cons det)
161 | = "TyCon " ++ show a ++ " params: " ++ show ps ++
162 | " constructors: " ++ show cons ++
163 | " mutual with: " ++ show ms ++
164 | " detaggable by: " ++ show det
165 | show (ExternDef arity) = "<external def with arity " ++ show arity ++ ">"
166 | show (ForeignDef a cs) = "<foreign def with arity " ++ show a ++
167 | " " ++ show cs ++">"
168 | show (Builtin {arity} _) = "<builtin with arith " ++ show arity ++ ">"
169 | show (Hole _ p) = "Hole" ++ if implbind p then " [impl]" else ""
170 | show (BySearch c depth def) = "Search in " ++ show def
171 | show (Guess tm _ cs) = "Guess " ++ show tm ++ " when " ++ show cs
172 | show (UniverseLevel i) = "Universe level #" ++ show i
173 | show ImpBind = "Bound name"
174 | show Delayed = "Delayed"
177 | Constructor' : Type -> Type
178 | Constructor' = AddFC . WithName . WithArity
182 | Constructor = Constructor' ClosedTerm
185 | data DataDef : Type where
186 | MkData : (tycon : Constructor) -> (datacons : List Constructor) ->
190 | data Clause : Type where
191 | MkClause : {vars : _} ->
192 | (env : Env Term vars) ->
193 | (lhs : Term vars) -> (rhs : Term vars) -> Clause
199 | show (MkClause {vars} env lhs rhs)
200 | = show vars ++ ": " ++ show lhs ++ " = " ++ show rhs
218 | | SetTotal TotalReq
221 | | PartialEval (List (Name, Nat))
235 | %name DefFlag
dflag
239 | (==) Inline Inline = True
240 | (==) NoInline NoInline = True
241 | (==) Deprecate Deprecate = True
242 | (==) Invertible Invertible = True
243 | (==) Overloadable Overloadable = True
244 | (==) TCInline TCInline = True
245 | (==) (SetTotal x) (SetTotal y) = x == y
246 | (==) BlockedHint BlockedHint = True
247 | (==) Macro Macro = True
248 | (==) (PartialEval x) (PartialEval y) = x == y
249 | (==) AllGuarded AllGuarded = True
250 | (==) (ConType x) (ConType y) = x == y
251 | (==) (Identity x) (Identity y) = x == y
256 | show Inline = "inline"
257 | show NoInline = "noinline"
258 | show Deprecate = "deprecate"
259 | show Invertible = "invertible"
260 | show Overloadable = "overloadable"
261 | show TCInline = "tcinline"
262 | show (SetTotal x) = show x
263 | show BlockedHint = "blockedhint"
264 | show Macro = "macro"
265 | show (PartialEval _) = "partialeval"
266 | show AllGuarded = "allguarded"
267 | show (ConType ci) = "contype " ++ show ci
268 | show (Identity x) = "identity " ++ show x
271 | record SCCall where
272 | constructor MkSCCall
274 | fnArgs : Matrix SizeChange
283 | show c = show (fnCall c) ++ ": " ++ show (fnArgs c)
287 | x == y = fnCall x == fnCall y && fnArgs x == fnArgs y
295 | Eq SchemeMode where
296 | EvalAll == EvalAll = True
297 | BlockExport == BlockExport = True
301 | record GlobalDef where
302 | constructor MkGlobalDef
311 | inferrable : NatSet
312 | multiplicity : RigCount
314 | visibility : WithDefault Visibility Private
315 | totality : Totality
316 | isEscapeHatch : Bool
317 | flags : List DefFlag
318 | refersToM : Maybe (NameMap Bool)
319 | refersToRuntimeM : Maybe (NameMap Bool)
326 | linearChecked : Bool
328 | compexpr : Maybe CDef
329 | namedcompexpr : Maybe NamedDef
330 | sizeChange : List SCCall
331 | schemeExpr : Maybe (SchemeMode, SchemeObj Write)
334 | getDefNameType : GlobalDef -> NameType
335 | getDefNameType = fromMaybe Func . defNameType . definition
338 | gDefKindedName : GlobalDef -> KindedName
340 | = let nm = fullname def in
341 | MkKindedName (defNameType $
definition def) nm nm
344 | refersTo : GlobalDef -> NameMap Bool
345 | refersTo def = maybe empty id (refersToM def)
348 | refersToRuntime : GlobalDef -> NameMap Bool
349 | refersToRuntime def = maybe empty id (refersToRuntimeM def)
352 | findSetTotal : List DefFlag -> Maybe TotalReq
353 | findSetTotal [] = Nothing
354 | findSetTotal (SetTotal t :: _) = Just t
355 | findSetTotal (_ :: xs) = findSetTotal xs
359 | data Arr : Type where
364 | data ContextEntry : Type where
365 | Coded : Namespace ->
366 | Binary -> ContextEntry
367 | Decoded : GlobalDef -> ContextEntry
370 | data PossibleName : Type where
371 | Direct : Name -> Int -> PossibleName
377 | data UConstraint : Type where
378 | ULE : Name -> Name -> UConstraint
379 | ULT : Name -> Name -> UConstraint
382 | Eq UConstraint where
383 | ULE x y == ULE x' y' = x == x' && y == y'
384 | ULT x y == ULT x' y' = x == x' && y == y'
388 | Ord UConstraint where
389 | compare (ULE {}) (ULT {}) = LT
390 | compare (ULT {}) (ULE {}) = GT
391 | compare (ULE x y) (ULE x' y')
392 | = case compare x x' of
395 | compare (ULT x y) (ULT x' y')
396 | = case compare x x' of
404 | record Context where
405 | constructor MkContext
409 | resolvedAs : NameMap Int
411 | possibles : UserNameMap (List PossibleName)
413 | content : Ref Arr (IOArray ContextEntry)
419 | staging : IntMap ContextEntry
425 | visibleNS : List Namespace
432 | hidden : NameMap ()
433 | uconstraints : List UConstraint