2 | import Core.Case.CaseTree
3 | import Core.CompileExpr
4 | import public Core.Context.Context
5 | import public Core.Core
8 | import public Core.Name
10 | import public Core.Options.Log
11 | import public Core.TT
13 | import Libraries.Utils.Binary
14 | import Libraries.Utils.Path
15 | import Libraries.Utils.Scheme
16 | import Libraries.Text.PrettyPrint.Prettyprinter
18 | import Idris.Syntax.Pragmas
24 | import Libraries.Data.IntMap
25 | import Libraries.Data.NameMap
26 | import Libraries.Data.NatSet
27 | import Libraries.Data.StringMap
28 | import Libraries.Data.UserNameMap
29 | import Libraries.Data.WithDefault
30 | import Libraries.Text.Distance.Levenshtein
33 | import System.Directory
38 | getContent : Context -> Ref Arr (IOArray ContextEntry)
39 | getContent = content
42 | namesResolvedAs : Context -> NameMap Name
43 | namesResolvedAs ctxt = map Resolved ctxt.resolvedAs
48 | decode : Context -> Int -> (update : Bool) -> ContextEntry -> Core GlobalDef
57 | initCtxtS : Int -> Core Context
59 | = do arr <- coreLift $
newArray s
60 | aref <- newRef Arr arr
64 | , resolvedAs = empty
69 | , visibleNS = [partialEvalNS]
71 | , inlineOnly = False
77 | initCtxt : Core Context
78 | initCtxt = initCtxtS initSize
80 | addPossible : Name -> Int ->
81 | UserNameMap (List PossibleName) -> UserNameMap (List PossibleName)
83 | = case userNameRoot n of
86 | case lookup nr ps of
87 | Nothing => insert nr [Direct n i] ps
88 | Just nis => insert nr (Direct n i :: nis) ps
90 | addAlias : Name -> Name -> Int ->
91 | UserNameMap (List PossibleName) -> UserNameMap (List PossibleName)
92 | addAlias alias full i ps
93 | = case userNameRoot alias of
96 | case lookup nr ps of
97 | Nothing => insert nr [Alias alias full i] ps
98 | Just nis => insert nr (Alias alias full i :: nis) ps
101 | newEntry : Name -> Context -> Core (Int, Context)
103 | = do let idx = nextEntry ctxt
104 | let a = content ctxt
106 | when (idx >= max arr) $
107 | do arr' <- coreLift $
newArrayCopy (max arr + Grow) arr
109 | pure (idx, { nextEntry := idx + 1,
110 | resolvedAs $= insert n idx,
111 | possibles $= addPossible n idx
118 | getPosition : Name -> Context -> Core (Int, Context)
119 | getPosition (Resolved idx) ctxt = pure (idx, ctxt)
121 | = case lookup n (resolvedAs ctxt) of
123 | do pure (idx, ctxt)
124 | Nothing => newEntry n ctxt
126 | newAlias : Name -> Name -> Context -> Core Context
127 | newAlias alias full ctxt
128 | = do (idx, ctxt) <- getPosition full ctxt
129 | pure $
{ possibles $= addAlias alias full idx } ctxt
132 | getNameID : Name -> Context -> Maybe Int
133 | getNameID (Resolved idx) ctxt = Just idx
134 | getNameID n ctxt = lookup n (resolvedAs ctxt)
140 | addCtxt : Name -> GlobalDef -> Context -> Core (Int, Context)
141 | addCtxt n val ctxt_in
142 | = if branchDepth ctxt_in == 0
143 | then do (idx, ctxt) <- getPosition n ctxt_in
144 | let a = content ctxt
146 | coreLift_ $
writeArray arr idx (Decoded val)
148 | else do (idx, ctxt) <- getPosition n ctxt_in
149 | pure (idx, { staging $= insert idx (Decoded val) } ctxt)
152 | addEntry : Name -> ContextEntry -> Context -> Core (Int, Context)
153 | addEntry n entry ctxt_in
154 | = if branchDepth ctxt_in == 0
155 | then do (idx, ctxt) <- getPosition n ctxt_in
156 | let a = content ctxt
158 | coreLift_ $
writeArray arr idx entry
160 | else do (idx, ctxt) <- getPosition n ctxt_in
161 | pure (idx, { staging $= insert idx entry } ctxt)
163 | returnDef : Bool -> Int -> GlobalDef -> Maybe (Int, GlobalDef)
164 | returnDef False idx def = Just (idx, def)
165 | returnDef True idx def
166 | = case definition def of
167 | PMDef pi _ _ _ _ =>
169 | then Just (idx, def)
174 | lookupCtxtExactI : Name -> Context -> Core (Maybe (Int, GlobalDef))
175 | lookupCtxtExactI (Resolved idx) ctxt
176 | = case lookup idx (staging ctxt) of
178 | pure $
returnDef (inlineOnly ctxt) idx !(decode ctxt idx True val)
180 | do arr <- get Arr @{content ctxt}
181 | Just def <- coreLift (readArray arr idx)
182 | | Nothing => pure Nothing
183 | pure $
returnDef (inlineOnly ctxt) idx !(decode ctxt idx True def)
184 | lookupCtxtExactI n ctxt
185 | = do let Just idx = lookup n (resolvedAs ctxt)
186 | | Nothing => pure Nothing
187 | lookupCtxtExactI (Resolved idx) ctxt
190 | lookupCtxtExact : Name -> Context -> Core (Maybe GlobalDef)
191 | lookupCtxtExact (Resolved idx) ctxt
192 | = case lookup idx (staging ctxt) of
194 | do def <- decode ctxt idx True res
195 | pure $
map (\(_, def) => def) $
196 | returnDef (inlineOnly ctxt) idx def
198 | do arr <- get Arr @{content ctxt}
199 | Just res <- coreLift (readArray arr idx)
200 | | Nothing => pure Nothing
201 | def <- decode ctxt idx True res
202 | pure $
map (\(_, def) => def) $
203 | returnDef (inlineOnly ctxt) idx def
204 | lookupCtxtExact n ctxt
205 | = do Just (i, def) <- lookupCtxtExactI n ctxt
206 | | Nothing => pure Nothing
210 | lookupContextEntry : Name -> Context -> Core (Maybe (Int, ContextEntry))
211 | lookupContextEntry (Resolved idx) ctxt
212 | = case lookup idx (staging ctxt) of
213 | Just res => pure (Just (idx, res))
215 | do let a = content ctxt
217 | Just res <- coreLift (readArray arr idx)
218 | | Nothing => pure Nothing
219 | pure (Just (idx, res))
220 | lookupContextEntry n ctxt
221 | = do let Just idx = lookup n (resolvedAs ctxt)
222 | | Nothing => pure Nothing
223 | lookupContextEntry (Resolved idx) ctxt
227 | isHidden : Name -> Context -> Bool
228 | isHidden fulln ctxt = isJust $
lookup fulln (hidden ctxt)
233 | lookupCtxtName' : Bool -> Name -> Context -> Core (List (Name, Int, GlobalDef))
234 | lookupCtxtName' allowHidden n ctxt
235 | = case userNameRoot n of
236 | Nothing => do Just (i, res) <- lookupCtxtExactI n ctxt
237 | | Nothing => pure []
240 | do let Just ps = lookup r (possibles ctxt)
241 | | Nothing => pure []
242 | lookupPossibles [] ps
245 | resn : (Name, Int, GlobalDef) -> Int
248 | hlookup : Name -> NameMap () -> Maybe ()
249 | hlookup fulln hiddens = if allowHidden
251 | else lookup fulln hiddens
253 | lookupPossibles : List (Name, Int, GlobalDef) ->
254 | List PossibleName ->
255 | Core (List (Name, Int, GlobalDef))
256 | lookupPossibles acc [] = pure (reverse acc)
257 | lookupPossibles acc (Direct fulln i :: ps)
258 | = case (hlookup fulln (hidden ctxt)) of
260 | do Just res <- lookupCtxtExact (Resolved i) ctxt
261 | | Nothing => lookupPossibles acc ps
262 | if matches n fulln && not (i `elem` map resn acc)
263 | then lookupPossibles ((fulln, i, res) :: acc) ps
264 | else lookupPossibles acc ps
265 | _ => lookupPossibles acc ps
266 | lookupPossibles acc (Alias asn fulln i :: ps)
267 | = case (hlookup fulln (hidden ctxt)) of
269 | do Just res <- lookupCtxtExact (Resolved i) ctxt
270 | | Nothing => lookupPossibles acc ps
271 | if (matches n asn) && not (i `elem` map resn acc)
272 | then lookupPossibles ((fulln, i, res) :: acc) ps
273 | else lookupPossibles acc ps
274 | _ => lookupPossibles acc ps
278 | lookupCtxtName : Name -> Context -> Core (List (Name, Int, GlobalDef))
279 | lookupCtxtName = lookupCtxtName' False
283 | lookupHiddenCtxtName : Name -> Context -> Core (List (Name, Int, GlobalDef))
284 | lookupHiddenCtxtName = lookupCtxtName' True
286 | hideName : Name -> Context -> Context
287 | hideName n ctxt = { hidden $= insert n () } ctxt
289 | unhideName : Name -> Context -> Context
290 | unhideName n ctxt = { hidden $= delete n } ctxt
292 | branchCtxt : Context -> Core Context
293 | branchCtxt ctxt = pure ({ branchDepth $= S } ctxt)
295 | commitCtxt : Context -> Core Context
297 | = case branchDepth ctxt of
300 | do let a = content ctxt
302 | coreLift $
commitStaged (toList (staging ctxt)) arr
303 | pure ({ staging := empty,
304 | branchDepth := Z } ctxt)
305 | S k => pure ({ branchDepth := k } ctxt)
309 | commitStaged : List (Int, ContextEntry) -> IOArray ContextEntry -> IO ()
310 | commitStaged [] arr = pure ()
311 | commitStaged ((idx, val) :: rest) arr
312 | = do ignore $
writeArray arr idx val
313 | commitStaged rest arr
324 | newDef : (fc : FC) -> (n : Name) -> (rig : RigCount) -> (vars : Scope) ->
325 | (ty : ClosedTerm) -> (vis : WithDefault Visibility Private) -> (def : Def) -> GlobalDef
326 | newDef fc n rig vars ty vis def
331 | , eraseArgs = NatSet.empty
332 | , safeErase = NatSet.empty
333 | , specArgs = NatSet.empty
334 | , inferrable = NatSet.empty
335 | , multiplicity = rig
338 | , totality = unchecked
339 | , isEscapeHatch = False
341 | , refersToM = Nothing
342 | , refersToRuntimeM = Nothing
343 | , invertible = False
345 | , linearChecked = False
347 | , compexpr = Nothing
348 | , namedcompexpr = Nothing
350 | , schemeExpr = Nothing
360 | data Transform : Type where
361 | MkTransform : {vars : _} ->
363 | Env Term vars -> Term vars -> Term vars -> Transform
368 | data BuiltinType : Type where
369 | BuiltinNatural : BuiltinType
370 | NaturalToInteger : BuiltinType
371 | IntegerToNatural : BuiltinType
374 | Show BuiltinType where
375 | show BuiltinNatural = "Natural"
376 | show NaturalToInteger = "NaturalToInteger"
377 | show IntegerToNatural = "IntegerToNatural"
380 | getFnName : Transform -> Maybe Name
381 | getFnName (MkTransform _ _ app _)
382 | = case getFn app of
383 | Ref _ _ fn => Just fn
391 | interface HasNames a where
392 | full : Context -> a -> Core a
393 | resolved : Context -> a -> Core a
396 | HasNames Name where
397 | full gam (Resolved i)
398 | = do Just gdef <- lookupCtxtExact (Resolved i) gam
401 | | Nothing => pure (Resolved i)
402 | pure (fullname gdef)
403 | full gam n = pure n
405 | resolved gam (Resolved i)
406 | = pure (Resolved i)
408 | = do let Just i = getNameID n gam
409 | | Nothing => pure n
413 | HasNames UConstraint where
415 | = do x' <- full gam x;
y' <- full gam y
418 | = do x' <- full gam x;
y' <- full gam y
421 | resolved gam (ULT x y)
422 | = do x' <- resolved gam x;
y' <- resolved gam y
424 | resolved gam (ULE x y)
425 | = do x' <- resolved gam x;
y' <- resolved gam y
429 | HasNames (Term vars) where
430 | full gam (Ref fc x (Resolved i))
431 | = do Just gdef <- lookupCtxtExact (Resolved i) gam
432 | | Nothing => pure (Ref fc x (Resolved i))
433 | pure (Ref fc x (fullname gdef))
434 | full gam (Meta fc x i xs)
435 | = do xs <- traverse (full gam) xs
436 | pure $
case !(lookupCtxtExact (Resolved i) gam) of
437 | Nothing => Meta fc x i xs
438 | Just gdef => Meta fc (fullname gdef) i xs
439 | full gam (Bind fc x b scope)
440 | = pure (Bind fc x !(traverse (full gam) b) !(full gam scope))
441 | full gam (App fc fn arg)
442 | = pure (App fc !(full gam fn) !(full gam arg))
443 | full gam (As fc s p tm)
444 | = pure (As fc s !(full gam p) !(full gam tm))
445 | full gam (TDelayed fc x y)
446 | = pure (TDelayed fc x !(full gam y))
447 | full gam (TDelay fc x t y)
448 | = pure (TDelay fc x !(full gam t) !(full gam y))
449 | full gam (TForce fc r y)
450 | = pure (TForce fc r !(full gam y))
451 | full gam (TType fc (Resolved i))
452 | = do Just gdef <- lookupCtxtExact (Resolved i) gam
453 | | Nothing => pure (TType fc (Resolved i))
454 | pure (TType fc (fullname gdef))
455 | full gam (Erased fc (Dotted t))
456 | = pure (Erased fc (Dotted !(full gam t)))
457 | full gam tm = pure tm
459 | resolved gam (Ref fc x n)
460 | = do let Just i = getNameID n gam
461 | | Nothing => pure (Ref fc x n)
462 | pure (Ref fc x (Resolved i))
463 | resolved gam (Meta fc x y xs)
464 | = do xs' <- traverse (resolved gam) xs
465 | let Just i = getNameID x gam
466 | | Nothing => pure (Meta fc x y xs')
467 | pure (Meta fc x i xs')
468 | resolved gam (Bind fc x b scope)
469 | = pure (Bind fc x !(traverse (resolved gam) b) !(resolved gam scope))
470 | resolved gam (App fc fn arg)
471 | = pure (App fc !(resolved gam fn) !(resolved gam arg))
472 | resolved gam (As fc s p tm)
473 | = pure (As fc s !(resolved gam p) !(resolved gam tm))
474 | resolved gam (TDelayed fc x y)
475 | = pure (TDelayed fc x !(resolved gam y))
476 | resolved gam (TDelay fc x t y)
477 | = pure (TDelay fc x !(resolved gam t) !(resolved gam y))
478 | resolved gam (TForce fc r y)
479 | = pure (TForce fc r !(resolved gam y))
480 | resolved gam (TType fc n)
481 | = do let Just i = getNameID n gam
482 | | Nothing => pure (TType fc n)
483 | pure (TType fc (Resolved i))
484 | resolved gam (Erased fc (Dotted t))
485 | = pure (Erased fc (Dotted !(resolved gam t)))
486 | resolved gam tm = pure tm
490 | full gam (PAs fc n p)
491 | = [| PAs (pure fc) (full gam n) (full gam p) |]
492 | full gam (PCon fc n i ar ps)
493 | = [| PCon (pure fc) (full gam n) (pure i) (pure ar) (traverse (full gam) ps) |]
494 | full gam (PTyCon fc n ar ps)
495 | = [| PTyCon (pure fc) (full gam n) (pure ar) (traverse (full gam) ps) |]
496 | full gam p@(PConst {}) = pure p
497 | full gam (PArrow fc x p q)
498 | = [| PArrow (pure fc) (full gam x) (full gam p) (full gam q) |]
499 | full gam (PDelay fc laz p q)
500 | = [| PDelay (pure fc) (pure laz) (full gam p) (full gam q) |]
501 | full gam (PLoc fc n) = PLoc fc <$> full gam n
502 | full gam (PUnmatchable fc t) = PUnmatchable fc <$> full gam t
504 | resolved gam (PAs fc n p)
505 | = [| PAs (pure fc) (resolved gam n) (resolved gam p) |]
506 | resolved gam (PCon fc n i ar ps)
507 | = [| PCon (pure fc) (resolved gam n) (pure i) (pure ar) (traverse (resolved gam) ps) |]
508 | resolved gam (PTyCon fc n ar ps)
509 | = [| PTyCon (pure fc) (resolved gam n) (pure ar) (traverse (resolved gam) ps) |]
510 | resolved gam p@(PConst {}) = pure p
511 | resolved gam (PArrow fc x p q)
512 | = [| PArrow (pure fc) (resolved gam x) (resolved gam p) (resolved gam q) |]
513 | resolved gam (PDelay fc laz p q)
514 | = [| PDelay (pure fc) (pure laz) (resolved gam p) (resolved gam q) |]
515 | resolved gam (PLoc fc n) = PLoc fc <$> resolved gam n
516 | resolved gam (PUnmatchable fc t) = PUnmatchable fc <$> resolved gam t
520 | HasNames (CaseTree vars) where
521 | full gam (Case i v ty alts)
522 | = pure $
Case i v !(full gam ty) !(traverse (full gam) alts)
523 | full gam (STerm i tm)
524 | = pure $
STerm i !(full gam tm)
525 | full gam t = pure t
527 | resolved gam (Case i v ty alts)
528 | = pure $
Case i v !(resolved gam ty) !(traverse (resolved gam) alts)
529 | resolved gam (STerm i tm)
530 | = pure $
STerm i !(resolved gam tm)
531 | resolved gam t = pure t
534 | HasNames (CaseAlt vars) where
535 | full gam (ConCase n t args sc)
536 | = do sc' <- full gam sc
537 | Just gdef <- lookupCtxtExact n gam
538 | | Nothing => pure (ConCase n t args sc')
539 | pure $
ConCase (fullname gdef) t args sc'
540 | full gam (DelayCase ty arg sc)
541 | = pure $
DelayCase ty arg !(full gam sc)
542 | full gam (ConstCase c sc)
543 | = pure $
ConstCase c !(full gam sc)
544 | full gam (DefaultCase sc)
545 | = pure $
DefaultCase !(full gam sc)
547 | resolved gam (ConCase n t args sc)
548 | = do sc' <- resolved gam sc
549 | let Just i = getNameID n gam
550 | | Nothing => pure (ConCase n t args sc')
551 | pure $
ConCase (Resolved i) t args sc'
552 | resolved gam (DelayCase ty arg sc)
553 | = pure $
DelayCase ty arg !(resolved gam sc)
554 | resolved gam (ConstCase c sc)
555 | = pure $
ConstCase c !(resolved gam sc)
556 | resolved gam (DefaultCase sc)
557 | = pure $
DefaultCase !(resolved gam sc)
560 | HasNames (Env Term vars) where
561 | full gam [] = pure Env.empty
563 | = pure $
!(traverse (full gam) b) :: !(full gam bs)
565 | resolved gam [] = pure Env.empty
566 | resolved gam (b :: bs)
567 | = pure $
!(traverse (resolved gam) b) :: !(resolved gam bs)
570 | HasNames Clause where
571 | full gam (MkClause env lhs rhs)
572 | = pure $
MkClause !(full gam env) !(full gam lhs) !(full gam rhs)
574 | resolved gam (MkClause env lhs rhs)
575 | = [| MkClause (resolved gam env) (resolved gam lhs) (resolved gam rhs) |]
580 | full gam (PMDef r args ct rt pats)
581 | = pure $
PMDef r args !(full gam ct) !(full gam rt)
582 | !(traverse fullNamesPat pats)
584 | fullNamesPat : (
vs ** (Env Term vs, Term vs, Term vs))
->
585 | Core (
vs ** (Env Term vs, Term vs, Term vs))
586 | fullNamesPat (
_ ** (env, lhs, rhs))
587 | = pure $ (
_ ** (!(full gam env),
588 | !(full gam lhs), !(full gam rhs)))
589 | full gam (TCon a ps ds u ms mcs det)
590 | = pure $
TCon a ps ds u !(traverse (full gam) ms)
591 | !(traverseOpt (traverse (full gam)) mcs) det
592 | full gam (BySearch c d def)
593 | = pure $
BySearch c d !(full gam def)
594 | full gam (Guess tm b cs)
595 | = pure $
Guess !(full gam tm) b cs
596 | full gam t = pure t
598 | resolved gam (PMDef r args ct rt pats)
599 | = pure $
PMDef r args !(resolved gam ct) !(resolved gam rt)
600 | !(traverse resolvedNamesPat pats)
602 | resolvedNamesPat : (
vs ** (Env Term vs, Term vs, Term vs))
->
603 | Core (
vs ** (Env Term vs, Term vs, Term vs))
604 | resolvedNamesPat (
_ ** (env, lhs, rhs))
605 | = pure $ (
_ ** (!(resolved gam env),
606 | !(resolved gam lhs), !(resolved gam rhs)))
607 | resolved gam (TCon a ps ds u ms mcs det)
608 | = pure $
TCon a ps ds u !(traverse (resolved gam) ms)
609 | !(traverseOpt (traverse (full gam)) mcs) det
610 | resolved gam (BySearch c d def)
611 | = pure $
BySearch c d !(resolved gam def)
612 | resolved gam (Guess tm b cs)
613 | = pure $
Guess !(resolved gam tm) b cs
614 | resolved gam t = pure t
617 | StripNamespace Def where
618 | trimNS ns (PMDef i args ct rt pats)
619 | = PMDef i args (trimNS ns ct) rt (map trimNSpat pats)
621 | trimNSpat : (
vs ** (Env Term vs, Term vs, Term vs))
->
622 | (
vs ** (Env Term vs, Term vs, Term vs))
623 | trimNSpat (
vs ** (env, lhs, rhs))
624 | = (
vs ** (env, trimNS ns lhs, trimNS ns rhs))
627 | restoreNS ns (PMDef i args ct rt pats)
628 | = PMDef i args (restoreNS ns ct) rt (map restoreNSpat pats)
630 | restoreNSpat : (
vs ** (Env Term vs, Term vs, Term vs))
->
631 | (
vs ** (Env Term vs, Term vs, Term vs))
632 | restoreNSpat (
vs ** (env, lhs, rhs))
633 | = (
vs ** (env, restoreNS ns lhs, restoreNS ns rhs))
637 | StripNamespace GlobalDef where
638 | trimNS ns def = { definition $= trimNS ns } def
639 | restoreNS ns def = { definition $= restoreNS ns } def
641 | HasNames (NameMap a) where
643 | = insertAll empty (toList nmap)
645 | insertAll : NameMap a -> List (Name, a) -> Core (NameMap a)
646 | insertAll ms [] = pure ms
647 | insertAll ms ((k, v) :: ns)
648 | = insertAll (insert !(full gam k) v ms) ns
651 | = insertAll empty (toList nmap)
653 | insertAll : NameMap a -> List (Name, a) -> Core (NameMap a)
654 | insertAll ms [] = pure ms
655 | insertAll ms ((k, v) :: ns)
656 | = insertAll (insert !(resolved gam k) v ms) ns
659 | HasNames PartialReason where
660 | full gam NotStrictlyPositive = pure NotStrictlyPositive
661 | full gam (BadCall ns) = pure $
BadCall !(traverse (full gam) ns)
662 | full gam (BadPath init n) = pure $
BadPath !(traverse (traversePair (full gam)) init) !(full gam n)
663 | full gam (RecPath loop) = pure $
RecPath !(traverse (traversePair (full gam)) loop)
665 | resolved gam NotStrictlyPositive = pure NotStrictlyPositive
666 | resolved gam (BadCall ns) = pure $
BadCall !(traverse (resolved gam) ns)
667 | resolved gam (BadPath init n) = pure $
BadPath !(traverse (traversePair (resolved gam)) init) !(resolved gam n)
668 | resolved gam (RecPath loop) = pure $
RecPath !(traverse (traversePair (resolved gam)) loop)
671 | HasNames Terminating where
672 | full gam (NotTerminating p) = pure $
NotTerminating !(full gam p)
673 | full gam t = pure t
675 | resolved gam (NotTerminating p) = pure $
NotTerminating !(resolved gam p)
676 | resolved gam t = pure t
679 | HasNames Covering where
680 | full gam IsCovering = pure IsCovering
681 | full gam (MissingCases ts)
682 | = pure $
MissingCases !(traverse (full gam) ts)
683 | full gam (NonCoveringCall ns)
684 | = pure $
NonCoveringCall !(traverse (full gam) ns)
686 | resolved gam IsCovering = pure IsCovering
687 | resolved gam (MissingCases ts)
688 | = pure $
MissingCases !(traverse (resolved gam) ts)
689 | resolved gam (NonCoveringCall ns)
690 | = pure $
NonCoveringCall !(traverse (resolved gam) ns)
693 | HasNames CaseError where
694 | full gam DifferingArgNumbers = pure DifferingArgNumbers
695 | full gam DifferingTypes = pure DifferingTypes
696 | full gam (MatchErased (
vs ** (rho, t))
) = do
697 | rho <- full gam rho
699 | pure (MatchErased (
vs ** (rho, t))
)
700 | full gam (NotFullyApplied n) = NotFullyApplied <$> full gam n
701 | full gam UnknownType = pure UnknownType
703 | resolved gam DifferingArgNumbers = pure DifferingArgNumbers
704 | resolved gam DifferingTypes = pure DifferingTypes
705 | resolved gam (MatchErased (
vs ** (rho, t))
) = do
706 | rho <- resolved gam rho
707 | t <- resolved gam t
708 | pure (MatchErased (
vs ** (rho, t))
)
709 | resolved gam (NotFullyApplied n) = NotFullyApplied <$> resolved gam n
710 | resolved gam UnknownType = pure UnknownType
714 | HasNames Warning where
715 | full gam (ParserWarning fc x) = pure (ParserWarning fc x)
716 | full gam (UnreachableClause fc rho s) = UnreachableClause fc <$> full gam rho <*> full gam s
717 | full gam (ShadowingGlobalDefs fc xs)
718 | = ShadowingGlobalDefs fc <$> traverseList1 (traversePair (traverseList1 (full gam))) xs
719 | full gam (IncompatibleVisibility fc x y n) = IncompatibleVisibility fc x y <$> full gam n
720 | full gam w@(ShadowingLocalBindings {}) = pure w
721 | full gam (Deprecated fc x y) = Deprecated fc x <$> traverseOpt (traversePair (full gam)) y
722 | full gam (GenericWarn fc x) = pure (GenericWarn fc x)
724 | resolved gam (ParserWarning fc x) = pure (ParserWarning fc x)
725 | resolved gam (UnreachableClause fc rho s) = UnreachableClause fc <$> resolved gam rho <*> resolved gam s
726 | resolved gam (ShadowingGlobalDefs fc xs)
727 | = ShadowingGlobalDefs fc <$> traverseList1 (traversePair (traverseList1 (resolved gam))) xs
728 | resolved gam (IncompatibleVisibility fc x y n) = IncompatibleVisibility fc x y <$> resolved gam n
729 | resolved gam w@(ShadowingLocalBindings {}) = pure w
730 | resolved gam (Deprecated fc x y) = Deprecated fc x <$> traverseOpt (traversePair (resolved gam)) y
731 | resolved gam (GenericWarn fc x) = pure (GenericWarn fc x)
734 | HasNames Error where
735 | full gam (Fatal err) = Fatal <$> full gam err
736 | full _ (CantConvert fc gam rho s t)
737 | = CantConvert fc gam <$> full gam rho <*> full gam s <*> full gam t
738 | full _ (CantSolveEq fc gam rho s t)
739 | = CantSolveEq fc gam <$> full gam rho <*> full gam s <*> full gam t
740 | full gam (PatternVariableUnifies fc fct rho n s)
741 | = PatternVariableUnifies fc fct <$> full gam rho <*> full gam n <*> full gam s
742 | full gam (CyclicMeta fc rho n s)
743 | = CyclicMeta fc <$> full gam rho <*> full gam n <*> full gam s
744 | full _ (WhenUnifying fc gam rho s t err)
745 | = WhenUnifying fc gam <$> full gam rho <*> full gam s <*> full gam t <*> full gam err
746 | full gam (ValidCase fc rho x)
747 | = ValidCase fc <$> full gam rho <*> either (map Left . full gam) (map Right . full gam) x
748 | full gam (UndefinedName fc n) = UndefinedName fc <$> full gam n
749 | full gam (InvisibleName fc n mns) = InvisibleName fc <$> full gam n <*> pure mns
750 | full gam (BadTypeConType fc n) = BadTypeConType fc <$> full gam n
751 | full gam (BadDataConType fc n n') = BadDataConType fc <$> full gam n <*> full gam n'
752 | full gam (NotCovering fc n cov) = NotCovering fc <$> full gam n <*> full gam cov
753 | full gam (NotTotal fc n pr) = NotTotal fc <$> full gam n <*> full gam pr
754 | full gam ImpossibleCase = pure ImpossibleCase
755 | full gam (LinearUsed fc k n) = LinearUsed fc k <$> full gam n
756 | full gam (LinearMisuse fc n x y) = LinearMisuse fc <$> full gam n <*> pure x <*> pure y
757 | full gam (BorrowPartial fc rho s t) = BorrowPartial fc <$> full gam rho <*> full gam s <*> full gam t
758 | full gam (BorrowPartialType fc rho s) = BorrowPartialType fc <$> full gam rho <*> full gam s
759 | full gam (AmbiguousName fc xs) = AmbiguousName fc <$> traverse (full gam) xs
760 | full gam (AmbiguousElab fc rho xs)
761 | = AmbiguousElab fc <$> full gam rho <*> traverse (\ (gam, t) => (gam,) <$> full gam t) xs
762 | full gam (AmbiguousSearch fc rho s xs)
763 | = AmbiguousSearch fc <$> full gam rho <*> full gam s <*> traverse (full gam) xs
764 | full gam (AmbiguityTooDeep fc n xs) = AmbiguityTooDeep fc <$> full gam n <*> traverse (full gam) xs
765 | full gam (AllFailed xs)
766 | = map AllFailed $
for xs $
\ (mn, err) =>
767 | (,) <$> traverseOpt (full gam) mn <*> full gam err
768 | full gam (RecordTypeNeeded fc rho) = RecordTypeNeeded fc <$> full gam rho
769 | full gam (DuplicatedRecordUpdatePath fc xs) = pure (DuplicatedRecordUpdatePath fc xs)
770 | full gam (NotRecordField fc x mn) = NotRecordField fc x <$> traverseOpt (full gam) mn
771 | full gam (NotRecordType fc n) = NotRecordType fc <$> full gam n
772 | full gam (IncompatibleFieldUpdate fc xs) = pure (IncompatibleFieldUpdate fc xs)
773 | full gam (InvalidArgs fc rho xs s) = InvalidArgs fc <$> full gam rho <*> traverse (full gam) xs <*> full gam s
774 | full gam (TryWithImplicits fc rho xs)
775 | = TryWithImplicits fc <$> full gam rho
776 | <*> for xs (\ (n, t) => (,) <$> full gam n <*> full gam t)
777 | full gam (BadUnboundImplicit fc rho n s) = BadUnboundImplicit fc <$> full gam rho <*> full gam n <*> full gam s
778 | full _ (CantSolveGoal fc gam rho s merr)
779 | = CantSolveGoal fc gam <$> full gam rho <*> full gam s <*> traverseOpt (full gam) merr
780 | full gam (DeterminingArg fc n x rho s)
781 | = DeterminingArg fc <$> full gam n <*> pure x <*> full gam rho <*> full gam s
782 | full gam (UnsolvedHoles xs) = UnsolvedHoles <$> traverse (traversePair (full gam)) xs
783 | full gam (CantInferArgType fc rho n n1 s)
784 | = CantInferArgType fc <$> full gam rho <*> full gam n <*> full gam n1 <*> full gam s
785 | full gam (SolvedNamedHole fc rho n s) = SolvedNamedHole fc <$> full gam rho <*> full gam n <*> full gam s
786 | full gam (VisibilityError fc x n y n1) = VisibilityError fc x <$> full gam n <*> pure y <*> full gam n1
787 | full gam (NonLinearPattern fc n) = NonLinearPattern fc <$> full gam n
788 | full gam (BadPattern fc n) = BadPattern fc <$> full gam n
789 | full gam (NoDeclaration fc n) = NoDeclaration fc <$> full gam n
790 | full gam (AlreadyDefined fc n) = AlreadyDefined fc <$> full gam n
791 | full gam (NotFunctionType fc rho s) = NotFunctionType fc <$> full gam rho <*> full gam s
792 | full gam (RewriteNoChange fc rho s t) = RewriteNoChange fc <$> full gam rho <*> full gam s <*> full gam t
793 | full gam (NotRewriteRule fc rho s) = NotRewriteRule fc <$> full gam rho <*> full gam s
794 | full gam (CaseCompile fc n x) = CaseCompile fc <$> full gam n <*> full gam x
795 | full gam (MatchTooSpecific fc rho s) = MatchTooSpecific fc <$> full gam rho <*> full gam s
796 | full gam (BadDotPattern fc rho x s t)
797 | = BadDotPattern fc <$> full gam rho <*> pure x <*> full gam s <*> full gam t
798 | full gam (BadImplicit fc x) = pure (BadImplicit fc x)
799 | full gam (BadRunElab fc rho s desc) = BadRunElab fc <$> full gam rho <*> full gam s <*> pure desc
800 | full gam (RunElabFail e) = RunElabFail <$> full gam e
801 | full gam (GenericMsg fc x) = pure (GenericMsg fc x)
802 | full gam (GenericMsgSol fc x y z) = pure (GenericMsgSol fc x y z)
803 | full gam (TTCError x) = pure (TTCError x)
804 | full gam (FileErr x y) = pure (FileErr x y)
805 | full gam (CantFindPackage x) = pure (CantFindPackage x)
806 | full gam (LazyImplicitFunction fc) = pure (LazyImplicitFunction fc)
807 | full gam (LazyPatternVar fc) = pure (LazyPatternVar fc)
808 | full gam (LitFail fc) = pure (LitFail fc)
809 | full gam (LexFail fc x) = pure (LexFail fc x)
810 | full gam (ParseFail xs) = pure (ParseFail xs)
811 | full gam (ModuleNotFound fc x) = pure (ModuleNotFound fc x)
812 | full gam (CyclicImports xs) = pure (CyclicImports xs)
813 | full gam ForceNeeded = pure ForceNeeded
814 | full gam (InternalError x) = pure (InternalError x)
815 | full gam (UserError x) = pure (UserError x)
816 | full gam (NoForeignCC fc xs) = pure (NoForeignCC fc xs)
817 | full gam (BadMultiline fc x) = pure (BadMultiline fc x)
818 | full gam (Timeout x) = pure (Timeout x)
819 | full gam (FailingDidNotFail fc) = pure (FailingDidNotFail fc)
820 | full gam (FailingWrongError fc x err) = FailingWrongError fc x <$> traverseList1 (full gam) err
821 | full gam (InType fc n err) = InType fc <$> full gam n <*> full gam err
822 | full gam (InCon n err) = InCon <$> traverse (full gam) n <*> full gam err
823 | full gam (InLHS fc n err) = InLHS fc <$> full gam n <*> full gam err
824 | full gam (InRHS fc n err) = InRHS fc <$> full gam n <*> full gam err
825 | full gam (MaybeMisspelling err xs) = MaybeMisspelling <$> full gam err <*> pure xs
826 | full gam (WarningAsError wrn) = WarningAsError <$> full gam wrn
827 | full gam (OperatorBindingMismatch {print} fc expected actual (Left opName) rhs candidates)
828 | = OperatorBindingMismatch {print} fc expected actual
829 | <$> (Left <$> full gam opName) <*> pure rhs <*> pure candidates
830 | full gam (OperatorBindingMismatch {print} fc expected actual (Right opName) rhs candidates)
831 | = OperatorBindingMismatch {print} fc expected actual
832 | <$> (Right <$> full gam opName) <*> pure rhs <*> pure candidates
834 | resolved gam (Fatal err) = Fatal <$> resolved gam err
835 | resolved _ (CantConvert fc gam rho s t)
836 | = CantConvert fc gam <$> resolved gam rho <*> resolved gam s <*> resolved gam t
837 | resolved _ (CantSolveEq fc gam rho s t)
838 | = CantSolveEq fc gam <$> resolved gam rho <*> resolved gam s <*> resolved gam t
839 | resolved gam (PatternVariableUnifies fc fct rho n s)
840 | = PatternVariableUnifies fc fct <$> resolved gam rho <*> resolved gam n <*> resolved gam s
841 | resolved gam (CyclicMeta fc rho n s)
842 | = CyclicMeta fc <$> resolved gam rho <*> resolved gam n <*> resolved gam s
843 | resolved _ (WhenUnifying fc gam rho s t err)
844 | = WhenUnifying fc gam <$> resolved gam rho <*> resolved gam s <*> resolved gam t <*> resolved gam err
845 | resolved gam (ValidCase fc rho x)
846 | = ValidCase fc <$> resolved gam rho <*> either (map Left . resolved gam) (map Right . resolved gam) x
847 | resolved gam (UndefinedName fc n) = UndefinedName fc <$> resolved gam n
848 | resolved gam (InvisibleName fc n mns) = InvisibleName fc <$> resolved gam n <*> pure mns
849 | resolved gam (BadTypeConType fc n) = BadTypeConType fc <$> resolved gam n
850 | resolved gam (BadDataConType fc n n') = BadDataConType fc <$> resolved gam n <*> resolved gam n'
851 | resolved gam (NotCovering fc n cov) = NotCovering fc <$> resolved gam n <*> resolved gam cov
852 | resolved gam (NotTotal fc n pr) = NotTotal fc <$> resolved gam n <*> resolved gam pr
853 | resolved gam ImpossibleCase = pure ImpossibleCase
854 | resolved gam (LinearUsed fc k n) = LinearUsed fc k <$> resolved gam n
855 | resolved gam (LinearMisuse fc n x y) = LinearMisuse fc <$> resolved gam n <*> pure x <*> pure y
856 | resolved gam (BorrowPartial fc rho s t) = BorrowPartial fc <$> resolved gam rho <*> resolved gam s <*> resolved gam t
857 | resolved gam (BorrowPartialType fc rho s) = BorrowPartialType fc <$> resolved gam rho <*> resolved gam s
858 | resolved gam (AmbiguousName fc xs) = AmbiguousName fc <$> traverse (resolved gam) xs
859 | resolved gam (AmbiguousElab fc rho xs)
860 | = AmbiguousElab fc <$> resolved gam rho <*> traverse (\ (gam, t) => (gam,) <$> resolved gam t) xs
861 | resolved gam (AmbiguousSearch fc rho s xs)
862 | = AmbiguousSearch fc <$> resolved gam rho <*> resolved gam s <*> traverse (resolved gam) xs
863 | resolved gam (AmbiguityTooDeep fc n xs) = AmbiguityTooDeep fc <$> resolved gam n <*> traverse (resolved gam) xs
864 | resolved gam (AllFailed xs)
865 | = map AllFailed $
for xs $
\ (mn, err) =>
866 | (,) <$> traverseOpt (resolved gam) mn <*> resolved gam err
867 | resolved gam (RecordTypeNeeded fc rho) = RecordTypeNeeded fc <$> resolved gam rho
868 | resolved gam (DuplicatedRecordUpdatePath fc xs) = pure (DuplicatedRecordUpdatePath fc xs)
869 | resolved gam (NotRecordField fc x mn) = NotRecordField fc x <$> traverseOpt (resolved gam) mn
870 | resolved gam (NotRecordType fc n) = NotRecordType fc <$> resolved gam n
871 | resolved gam (IncompatibleFieldUpdate fc xs) = pure (IncompatibleFieldUpdate fc xs)
872 | resolved gam (InvalidArgs fc rho xs s) = InvalidArgs fc <$> resolved gam rho <*> traverse (resolved gam) xs <*> resolved gam s
873 | resolved gam (TryWithImplicits fc rho xs)
874 | = TryWithImplicits fc <$> resolved gam rho
875 | <*> for xs (\ (n, t) => (,) <$> resolved gam n <*> resolved gam t)
876 | resolved gam (BadUnboundImplicit fc rho n s) = BadUnboundImplicit fc <$> resolved gam rho <*> resolved gam n <*> resolved gam s
877 | resolved _ (CantSolveGoal fc gam rho s merr)
878 | = CantSolveGoal fc gam <$> resolved gam rho <*> resolved gam s <*> traverseOpt (resolved gam) merr
879 | resolved gam (DeterminingArg fc n x rho s)
880 | = DeterminingArg fc <$> resolved gam n <*> pure x <*> resolved gam rho <*> resolved gam s
881 | resolved gam (UnsolvedHoles xs) = UnsolvedHoles <$> traverse (traversePair (resolved gam)) xs
882 | resolved gam (CantInferArgType fc rho n n1 s)
883 | = CantInferArgType fc <$> resolved gam rho <*> resolved gam n <*> resolved gam n1 <*> resolved gam s
884 | resolved gam (SolvedNamedHole fc rho n s) = SolvedNamedHole fc <$> resolved gam rho <*> resolved gam n <*> resolved gam s
885 | resolved gam (VisibilityError fc x n y n1) = VisibilityError fc x <$> resolved gam n <*> pure y <*> resolved gam n1
886 | resolved gam (NonLinearPattern fc n) = NonLinearPattern fc <$> resolved gam n
887 | resolved gam (BadPattern fc n) = BadPattern fc <$> resolved gam n
888 | resolved gam (NoDeclaration fc n) = NoDeclaration fc <$> resolved gam n
889 | resolved gam (AlreadyDefined fc n) = AlreadyDefined fc <$> resolved gam n
890 | resolved gam (NotFunctionType fc rho s) = NotFunctionType fc <$> resolved gam rho <*> resolved gam s
891 | resolved gam (RewriteNoChange fc rho s t) = RewriteNoChange fc <$> resolved gam rho <*> resolved gam s <*> resolved gam t
892 | resolved gam (NotRewriteRule fc rho s) = NotRewriteRule fc <$> resolved gam rho <*> resolved gam s
893 | resolved gam (CaseCompile fc n x) = CaseCompile fc <$> resolved gam n <*> resolved gam x
894 | resolved gam (MatchTooSpecific fc rho s) = MatchTooSpecific fc <$> resolved gam rho <*> resolved gam s
895 | resolved gam (BadDotPattern fc rho x s t)
896 | = BadDotPattern fc <$> resolved gam rho <*> pure x <*> resolved gam s <*> resolved gam t
897 | resolved gam (BadImplicit fc x) = pure (BadImplicit fc x)
898 | resolved gam (BadRunElab fc rho s desc) = BadRunElab fc <$> resolved gam rho <*> resolved gam s <*> pure desc
899 | resolved gam (RunElabFail e) = RunElabFail <$> resolved gam e
900 | resolved gam (GenericMsg fc x) = pure (GenericMsg fc x)
901 | resolved gam (GenericMsgSol fc x y z) = pure (GenericMsgSol fc x y z)
902 | resolved gam (TTCError x) = pure (TTCError x)
903 | resolved gam (FileErr x y) = pure (FileErr x y)
904 | resolved gam (CantFindPackage x) = pure (CantFindPackage x)
905 | resolved gam (LazyImplicitFunction fc) = pure (LazyImplicitFunction fc)
906 | resolved gam (LazyPatternVar fc) = pure (LazyPatternVar fc)
907 | resolved gam (LitFail fc) = pure (LitFail fc)
908 | resolved gam (LexFail fc x) = pure (LexFail fc x)
909 | resolved gam (ParseFail xs) = pure (ParseFail xs)
910 | resolved gam (ModuleNotFound fc x) = pure (ModuleNotFound fc x)
911 | resolved gam (CyclicImports xs) = pure (CyclicImports xs)
912 | resolved gam ForceNeeded = pure ForceNeeded
913 | resolved gam (InternalError x) = pure (InternalError x)
914 | resolved gam (UserError x) = pure (UserError x)
915 | resolved gam (NoForeignCC fc xs) = pure (NoForeignCC fc xs)
916 | resolved gam (BadMultiline fc x) = pure (BadMultiline fc x)
917 | resolved gam (Timeout x) = pure (Timeout x)
918 | resolved gam (FailingDidNotFail fc) = pure (FailingDidNotFail fc)
919 | resolved gam (FailingWrongError fc x err) = FailingWrongError fc x <$> traverseList1 (resolved gam) err
920 | resolved gam (InType fc n err) = InType fc <$> resolved gam n <*> resolved gam err
921 | resolved gam (InCon n err) = InCon <$> traverse (resolved gam) n <*> resolved gam err
922 | resolved gam (InLHS fc n err) = InLHS fc <$> resolved gam n <*> resolved gam err
923 | resolved gam (InRHS fc n err) = InRHS fc <$> resolved gam n <*> resolved gam err
924 | resolved gam (MaybeMisspelling err xs) = MaybeMisspelling <$> resolved gam err <*> pure xs
925 | resolved gam (WarningAsError wrn) = WarningAsError <$> resolved gam wrn
926 | resolved gam (OperatorBindingMismatch {print} fc expected actual (Left opName) rhs candidates)
927 | = OperatorBindingMismatch {print} fc expected actual
928 | <$> (Left <$> resolved gam opName) <*> pure rhs <*> pure candidates
929 | resolved gam (OperatorBindingMismatch {print} fc expected actual (Right opName) rhs candidates)
930 | = OperatorBindingMismatch {print} fc expected actual
931 | <$> (Right <$> resolved gam opName) <*> pure rhs <*> pure candidates
934 | HasNames Totality where
935 | full gam (MkTotality t c) = pure $
MkTotality !(full gam t) !(full gam c)
936 | resolved gam (MkTotality t c) = pure $
MkTotality !(resolved gam t) !(resolved gam c)
939 | HasNames SCCall where
940 | full gam sc = pure $
{ fnCall := !(full gam (fnCall sc)) } sc
941 | resolved gam sc = pure $
{ fnCall := !(resolved gam (fnCall sc)) } sc
944 | HasNames a => HasNames (Maybe a) where
945 | full gam Nothing = pure Nothing
946 | full gam (Just x) = pure $
Just !(full gam x)
947 | resolved gam Nothing = pure Nothing
948 | resolved gam (Just x) = pure $
Just !(resolved gam x)
951 | HasNames GlobalDef where
958 | pure $
{ type := !(full gam (type def)),
959 | definition := !(full gam (definition def)),
960 | totality := !(full gam (totality def)),
961 | refersToM := !(full gam (refersToM def)),
962 | refersToRuntimeM := !(full gam (refersToRuntimeM def)),
963 | sizeChange := !(traverse (full gam) (sizeChange def))
966 | = pure $
{ type := !(resolved gam (type def)),
967 | definition := !(resolved gam (definition def)),
968 | totality := !(resolved gam (totality def)),
969 | refersToM := !(resolved gam (refersToM def)),
970 | refersToRuntimeM := !(resolved gam (refersToRuntimeM def)),
971 | sizeChange := !(traverse (resolved gam) (sizeChange def))
975 | HasNames Transform where
976 | full gam (MkTransform n env lhs rhs)
977 | = pure $
MkTransform !(full gam n) !(full gam env)
978 | !(full gam lhs) !(full gam rhs)
980 | resolved gam (MkTransform n env lhs rhs)
981 | = pure $
MkTransform !(resolved gam n) !(resolved gam env)
982 | !(resolved gam lhs) !(resolved gam rhs)
986 | allNames : Context -> Core (List Name)
987 | allNames ctxt = traverse (full ctxt) $
map Resolved [1..nextEntry ctxt - 1]
993 | mutData : List Name
994 | currentNS : Namespace
995 | nestedNS : List Namespace
997 | toSave : NameMap ()
998 | typeHints : NameMap (List (Name, Bool))
1004 | autoHints : NameMap Bool
1012 | localHints : NameMap ()
1014 | saveTypeHints : List (Name, Name, Bool)
1017 | saveAutoHints : List (Name, Bool)
1018 | transforms : NameMap (List Transform)
1021 | saveTransforms : List (Name, Transform)
1022 | namedirectives : NameMap (List String)
1024 | importHashes : List (Namespace, Int)
1026 | imported : List (ModuleIdent, Bool, Namespace)
1028 | allImported : List (String, (ModuleIdent, Bool, Namespace))
1032 | cgdirectives : List (CG, String)
1035 | toCompileCase : List Name
1037 | incData : List (CG, String, List String)
1040 | allIncData : List (CG, List String, List String)
1046 | userHoles : NameMap Bool
1050 | peFailures : NameMap ()
1053 | timings : StringMap (Bool, Integer)
1055 | timer : Maybe (Integer, String)
1058 | warnings : List Warning
1060 | schemeEvalLoaded : Bool
1061 | foreignExports : NameMap (List (String, String))
1064 | defsStack : SnocList Name
1072 | clearDefs : Defs -> Core Defs
1074 | = pure ({ gamma->inlineOnly := True } defs)
1096 | , namedirectives = empty
1111 | , schemeEvalLoaded = False
1112 | , foreignExports = empty
1118 | clearCtxt : {auto c : Ref Ctxt Defs} ->
1122 | put Ctxt ({ options := resetElab (options defs),
1123 | timings := timings defs } !initDefs)
1125 | resetElab : Options -> Options
1127 | let tot = totalReq (session opts) in
1128 | { elabDirectives := { totality := tot } defaultElab } opts
1131 | getFieldNames : Context -> Namespace -> List Name
1132 | getFieldNames ctxt recNS
1133 | = let nms = resolvedAs ctxt in
1134 | keys $
flip filterBy nms $
\ n =>
1137 | Just (ns, field) => ns == recNS
1141 | getSimilarNames : {auto c : Ref Ctxt Defs} ->
1144 | {default Nothing keepPredicate : Maybe ((Name, GlobalDef) -> Core Bool)} ->
1145 | Name -> Core (Maybe (String, List (Name, Visibility, Nat)))
1146 | getSimilarNames nm = case show <$> userNameRoot nm of
1147 | Nothing => pure Nothing
1148 | Just str => if length str <= 1 then pure (Just (str, [])) else
1150 | let threshold : Nat := max 1 (assert_total (divNat (length str) 3))
1151 | let test : Name -> Core (Maybe (Visibility, Nat)) := \ nm => do
1152 | let (Just str') = show <$> userNameRoot nm
1154 | dist <- coreLift $
Levenshtein.compute str str'
1155 | let True = dist <= threshold
1156 | | False => pure Nothing
1157 | Just def <- lookupCtxtExact nm (gamma defs)
1158 | | Nothing => pure Nothing
1159 | let predicate = fromMaybe (\_ => pure True) keepPredicate
1160 | True <- predicate (nm, def)
1161 | | False => pure Nothing
1162 | pure (Just (collapseDefault $
visibility def, dist))
1163 | kept <- NameMap.mapMaybeM @{CORE} test (resolvedAs (gamma defs))
1164 | pure $
Just (str, toList kept)
1167 | showSimilarNames : Namespace -> Name -> String ->
1168 | List (Name, Visibility, Nat) -> List String
1169 | showSimilarNames ns nm str kept
1170 | = let (loc, priv) := partitionEithers $
kept <&> \ (nm', vis, n) =>
1171 | let False = fst (splitNS nm') `isParentOf` ns
1176 | sorted := sortBy (compare `on` snd)
1177 | roots1 := mapMaybe (showNames nm str False . fst) (sorted loc)
1178 | roots2 := mapMaybe (showNames nm str True . fst) (sorted priv)
1179 | in nub roots1 ++ nub roots2
1183 | showNames : Name -> String -> Bool -> Name -> Maybe String
1184 | showNames target str priv nm = do
1185 | let adj = if priv then " (not exported)" else ""
1188 | | _ => pure (root ++ adj)
1189 | let full = show (pretty nm)
1190 | let True = (str == full || show target == full) && not priv
1191 | | _ => pure (full ++ adj)
1195 | getVisibility : {auto c : Ref Ctxt Defs} ->
1196 | FC -> Name -> Core (WithDefault Visibility Private)
1199 | Just def <- lookupCtxtExact n (gamma defs)
1200 | | Nothing => throw (UndefinedName fc n)
1203 | maybeMisspelling : {auto c : Ref Ctxt Defs} ->
1204 | Error -> Name -> Core a
1205 | maybeMisspelling err nm = do
1206 | ns <- currentNS <$> get Ctxt
1207 | Just (str, kept) <- getSimilarNames nm
1209 | let candidates = showSimilarNames ns nm str kept
1212 | (x::xs) => throw (MaybeMisspelling err (x ::: xs))
1216 | undefinedName : {auto c : Ref Ctxt Defs} ->
1218 | undefinedName loc nm = maybeMisspelling (UndefinedName loc nm) nm
1222 | noDeclaration : {auto c : Ref Ctxt Defs} ->
1224 | noDeclaration loc nm = maybeMisspelling (NoDeclaration loc nm) nm
1227 | ambiguousName : {auto c : Ref Ctxt Defs} -> FC
1230 | ambiguousName fc n ns = do
1231 | ns <- filterM (\x => pure $
!(collapseDefault <$> getVisibility fc x) /= Private) ns
1233 | [] => undefinedName fc n
1234 | ns => throw $
AmbiguousName fc ns
1239 | canonicalName : {auto c : Ref Ctxt Defs} ->
1240 | FC -> Name -> Core Name
1243 | case !(lookupCtxtName n (gamma defs)) of
1245 | ns => ambiguousName fc n (map fst ns)
1249 | aliasName : {auto c : Ref Ctxt Defs} ->
1253 | let Just r = userNameRoot fulln
1254 | | Nothing => pure fulln
1255 | let Just ps = lookup r (possibles (gamma defs))
1256 | | Nothing => pure fulln
1259 | findAlias : List PossibleName -> Core Name
1260 | findAlias [] = pure fulln
1261 | findAlias (Alias as full i :: ps)
1265 | findAlias (_ :: ps) = findAlias ps
1271 | addHash : {auto c : Ref Ctxt Defs} -> Hashable a => a -> Core ()
1272 | addHash x = update Ctxt { ifaceHash $= flip hashWithSalt x }
1275 | initHash : {auto c : Ref Ctxt Defs} -> Core ()
1276 | initHash = update Ctxt { ifaceHash := 5381 }
1279 | addUserHole : {auto c : Ref Ctxt Defs} ->
1283 | addUserHole ext n = update Ctxt { userHoles $= insert n ext }
1286 | clearUserHole : {auto c : Ref Ctxt Defs} -> Name -> Core ()
1287 | clearUserHole n = update Ctxt { userHoles $= delete n }
1290 | getUserHoles : {auto c : Ref Ctxt Defs} ->
1294 | let hs = sort (keys (userHoles defs))
1295 | filterM (isHole defs) hs
1301 | isHole : Defs -> Name -> Core Bool
1303 | = do Just def <- lookupCtxtExact n (gamma defs)
1305 | pure $
case definition def of
1311 | addDef : {auto c : Ref Ctxt Defs} ->
1312 | Name -> GlobalDef -> Core Int
1315 | (idx, gam') <- addCtxt n def (gamma defs)
1316 | put Ctxt ({ gamma := gam' } defs)
1320 | _ => clearUserHole (fullname def)
1324 | addContextEntry : {auto c : Ref Ctxt Defs} ->
1325 | Namespace -> Name -> Binary -> Core Int
1326 | addContextEntry ns n def
1328 | (idx, gam') <- addEntry n (Coded ns def) (gamma defs)
1329 | put Ctxt ({ gamma := gam' } defs)
1333 | addContextAlias : {auto c : Ref Ctxt Defs} ->
1334 | Name -> Name -> Core ()
1335 | addContextAlias alias full
1337 | Nothing <- lookupCtxtExact alias (gamma defs)
1339 | gam' <- newAlias alias full (gamma defs)
1340 | put Ctxt ({ gamma := gam' } defs)
1343 | addBuiltin : {arity : _} ->
1344 | {auto x : Ref Ctxt Defs} ->
1345 | Name -> ClosedTerm -> Totality ->
1346 | PrimFn arity -> Core ()
1353 | , eraseArgs = NatSet.empty
1354 | , safeErase = NatSet.empty
1355 | , specArgs = NatSet.empty
1356 | , inferrable = NatSet.empty
1358 | , localVars = Scope.empty
1359 | , visibility = specified Public
1361 | , isEscapeHatch = False
1364 | , refersToRuntimeM = Nothing
1368 | , definition = Builtin op
1370 | , namedcompexpr = Nothing
1376 | updateDef : {auto c : Ref Ctxt Defs} ->
1377 | Name -> (Def -> Maybe Def) -> Core ()
1380 | Just gdef <- lookupCtxtExact n (gamma defs)
1382 | case fdef (definition gdef) of
1384 | Just def' => ignore $
addDef n ({ definition := def',
1385 | schemeExpr := Nothing } gdef)
1388 | updateTy : {auto c : Ref Ctxt Defs} ->
1389 | Int -> ClosedTerm -> Core ()
1392 | Just gdef <- lookupCtxtExact (Resolved i) (gamma defs)
1394 | ignore $
addDef (Resolved i) ({ type := ty } gdef)
1397 | setCompiled : {auto c : Ref Ctxt Defs} ->
1398 | Name -> CDef -> Core ()
1401 | Just gdef <- lookupCtxtExact n (gamma defs)
1403 | ignore $
addDef n ({ compexpr := Just cexp } gdef)
1408 | setLinearCheck : {auto c : Ref Ctxt Defs} ->
1412 | Just gdef <- lookupCtxtExact (Resolved i) (gamma defs)
1414 | ignore $
addDef (Resolved i) ({ linearChecked := chk } gdef)
1417 | setCtxt : {auto c : Ref Ctxt Defs} -> Context -> Core ()
1418 | setCtxt gam = update Ctxt { gamma := gam }
1421 | resolveName : {auto c : Ref Ctxt Defs} ->
1423 | resolveName (Resolved idx) = pure idx
1426 | (i, gam') <- getPosition n (gamma defs)
1431 | addName : {auto c : Ref Ctxt Defs} ->
1433 | addName (Resolved idx) = pure idx
1436 | (i, gam') <- newEntry n (gamma defs)
1445 | branch : {auto c : Ref Ctxt Defs} ->
1449 | gam' <- branchCtxt (gamma ctxt)
1457 | commit : {auto c : Ref Ctxt Defs} ->
1461 | gam' <- commitCtxt (gamma defs)
1465 | depth : {auto c : Ref Ctxt Defs} ->
1469 | pure (branchDepth (gamma defs))
1472 | dumpStaging : {auto c : Ref Ctxt Defs} ->
1476 | coreLift $
putStrLn $
"Staging area: " ++ show (keys (staging (gamma defs)))
1480 | addToSave : {auto c : Ref Ctxt Defs} ->
1484 | n <- full (gamma defs) n_in
1485 | put Ctxt ({ toSave $= insert n (),
1491 | lookupExactBy : (GlobalDef -> a) -> Name -> Context ->
1494 | = do Just gdef <- lookupCtxtExact n gam
1495 | | Nothing => pure Nothing
1499 | lookupNameBy : (GlobalDef -> a) -> Name -> Context ->
1500 | Core (List (Name, Int, a))
1502 | = do gdef <- lookupCtxtName n gam
1503 | pure (map (\ (n, i, gd) => (n, i, fn gd)) gdef)
1506 | lookupDefExact : Name -> Context -> Core (Maybe Def)
1507 | lookupDefExact = lookupExactBy definition
1510 | lookupDefName : Name -> Context -> Core (List (Name, Int, Def))
1511 | lookupDefName = lookupNameBy definition
1514 | lookupTyExact : Name -> Context -> Core (Maybe ClosedTerm)
1515 | lookupTyExact = lookupExactBy type
1518 | lookupTyName : Name -> Context -> Core (List (Name, Int, ClosedTerm))
1519 | lookupTyName = lookupNameBy type
1522 | lookupDefTyExact : Name -> Context -> Core (Maybe (Def, ClosedTerm))
1523 | lookupDefTyExact = lookupExactBy (\g => (definition g, type g))
1529 | visibleIn : Namespace -> Name -> Visibility -> Bool
1530 | visibleIn nspace (NS ns n) Private = isParentOf ns nspace
1532 | visibleIn nspace n _ = True
1535 | visibleInAny : List Namespace -> Name -> Visibility -> Bool
1536 | visibleInAny nss n vis = any (\ns => visibleIn ns n vis) nss
1538 | reducibleIn : Namespace -> Name -> Visibility -> Bool
1539 | reducibleIn nspace (NS ns (UN n)) Export = isParentOf ns nspace
1540 | reducibleIn nspace (NS ns (UN n)) Private = isParentOf ns nspace
1541 | reducibleIn nspace n _ = True
1544 | reducibleInAny : List Namespace -> Name -> Visibility -> Bool
1545 | reducibleInAny nss n vis = any (\ns => reducibleIn ns n vis) nss
1548 | toFullNames : {auto c : Ref Ctxt Defs} ->
1549 | HasNames a => a -> Core a
1555 | toResolvedNames : {auto c : Ref Ctxt Defs} ->
1556 | HasNames a => a -> Core a
1559 | resolved (gamma defs) t
1563 | prettyName : {auto c : Ref Ctxt Defs} ->
1565 | prettyName (Nested (i, _) n)
1566 | = do i' <- toFullNames (Resolved i)
1567 | pure (!(prettyName i') ++ "," ++
1569 | prettyName (CaseBlock outer idx)
1570 | = pure ("case block in " ++ outer)
1571 | prettyName (WithBlock outer idx)
1572 | = pure ("with block in " ++ outer)
1573 | prettyName (NS ns n) = prettyName n
1574 | prettyName n = pure (show n)
1581 | addHashWithNames : {auto c : Ref Ctxt Defs} ->
1582 | Hashable a => HasNames a => a -> Core ()
1583 | addHashWithNames x = toFullNames x >>= addHash
1586 | setIsEscapeHatch : {auto c : Ref Ctxt Defs} ->
1590 | Just def <- lookupCtxtExact n (gamma defs)
1591 | | Nothing => undefinedName fc n
1592 | ignore $
addDef n ({ isEscapeHatch := True } def)
1595 | setFlag : {auto c : Ref Ctxt Defs} ->
1596 | FC -> Name -> DefFlag -> Core ()
1599 | Just def <- lookupCtxtExact n (gamma defs)
1600 | | Nothing => undefinedName fc n
1601 | let flags' = fl :: filter (/= fl) (flags def)
1602 | ignore $
addDef n ({ flags := flags' } def)
1605 | setNameFlag : {auto c : Ref Ctxt Defs} ->
1606 | FC -> Name -> DefFlag -> Core ()
1609 | [(n', i, def)] <- lookupCtxtName n (gamma defs)
1610 | | res => ambiguousName fc n (map fst res)
1611 | let flags' = fl :: filter (/= fl) (flags def)
1612 | ignore $
addDef (Resolved i) ({ flags := flags' } def)
1615 | unsetFlag : {auto c : Ref Ctxt Defs} ->
1616 | FC -> Name -> DefFlag -> Core ()
1619 | Just def <- lookupCtxtExact n (gamma defs)
1620 | | Nothing => undefinedName fc n
1621 | let flags' = filter (/= fl) (flags def)
1622 | ignore $
addDef n ({ flags := flags' } def)
1625 | hasFlag : {auto c : Ref Ctxt Defs} ->
1626 | FC -> Name -> DefFlag -> Core Bool
1629 | Just def <- lookupCtxtExact n (gamma defs)
1630 | | Nothing => undefinedName fc n
1631 | pure (fl `elem` flags def)
1634 | hasSetTotal : {auto c : Ref Ctxt Defs} -> FC -> Name -> Core Bool
1637 | Just def <- lookupCtxtExact n (gamma defs)
1638 | | Nothing => undefinedName fc n
1639 | pure $
isJust $
findSetTotal def.flags
1642 | setSizeChange : {auto c : Ref Ctxt Defs} ->
1643 | FC -> Name -> List SCCall -> Core ()
1646 | Just def <- lookupCtxtExact n (gamma defs)
1647 | | Nothing => undefinedName loc n
1648 | ignore $
addDef n ({ sizeChange := sc } def)
1651 | setTotality : {auto c : Ref Ctxt Defs} ->
1652 | FC -> Name -> Totality -> Core ()
1655 | Just def <- lookupCtxtExact n (gamma defs)
1656 | | Nothing => undefinedName loc n
1657 | ignore $
addDef n ({ totality := tot } def)
1660 | setCovering : {auto c : Ref Ctxt Defs} ->
1661 | FC -> Name -> Covering -> Core ()
1664 | Just def <- lookupCtxtExact n (gamma defs)
1665 | | Nothing => undefinedName loc n
1666 | ignore $
addDef n ({ totality->isCovering := tot } def)
1669 | setTerminating : {auto c : Ref Ctxt Defs} ->
1670 | FC -> Name -> Terminating -> Core ()
1671 | setTerminating loc n tot
1673 | Just def <- lookupCtxtExact n (gamma defs)
1674 | | Nothing => undefinedName loc n
1675 | ignore $
addDef n ({ totality->isTerminating := tot } def)
1678 | getTotality : {auto c : Ref Ctxt Defs} ->
1679 | FC -> Name -> Core Totality
1682 | Just def <- lookupCtxtExact n (gamma defs)
1683 | | Nothing => undefinedName loc n
1687 | getSizeChange : {auto c : Ref Ctxt Defs} ->
1688 | FC -> Name -> Core (List SCCall)
1691 | Just def <- lookupCtxtExact n (gamma defs)
1692 | | Nothing => undefinedName loc n
1696 | setVisibility : {auto c : Ref Ctxt Defs} ->
1697 | FC -> Name -> Visibility -> Core ()
1700 | Just def <- lookupCtxtExact n (gamma defs)
1701 | | Nothing => undefinedName fc n
1702 | ignore $
addDef n ({ visibility := specified vis } def)
1705 | withDefStacked : {auto c : Ref Ctxt Defs} ->
1706 | Name -> Core a -> Core a
1709 | let ds = defs.defsStack
1710 | put Ctxt $
{defsStack $= (:< n)} defs
1711 | act <* update Ctxt {defsStack := ds}
1714 | record SearchData where
1715 | constructor MkSearchData
1731 | hintGroups : List (Bool, List Name)
1735 | getSearchData : {auto c : Ref Ctxt Defs} ->
1736 | FC -> (defaults : Bool) -> Name ->
1738 | getSearchData fc defaults target
1740 | Just (TCon _ _ dets u _ _ _) <- lookupDefExact target (gamma defs)
1741 | | _ => undefinedName fc target
1742 | hs <- case lookup !(toFullNames target) (typeHints defs) of
1743 | Just hs => filterM (\x => notHidden x (gamma defs)) hs
1746 | then let defns = map fst !(filterM (\x => pure $
isDefault x
1747 | && !(notHidden x (gamma defs)))
1748 | (toList (autoHints defs))) in
1749 | pure (MkSearchData NatSet.empty [(False, defns)])
1750 | else let opens = map fst !(filterM (\x => notHidden x (gamma defs))
1751 | (toList (openHints defs)))
1752 | autos = map fst !(filterM (\x => pure $
not (isDefault x)
1753 | && !(notHidden x (gamma defs)))
1754 | (toList (autoHints defs)))
1755 | tyhs = map fst (filter direct hs)
1756 | chasers = map fst (filter (not . direct) hs) in
1757 | pure (MkSearchData dets (filter (isCons . snd)
1760 | (not (uniqueAuto u), tyhs),
1765 | notHidden : forall a. (Name, a) -> Context -> Core Bool
1766 | notHidden (n, _) ctxt = do
1768 | pure $
not (isHidden fulln ctxt)
1770 | isDefault : (Name, Bool) -> Bool
1773 | direct : (Name, Bool) -> Bool
1777 | setMutWith : {auto c : Ref Ctxt Defs} ->
1778 | FC -> Name -> List Name -> Core ()
1781 | Just g <- lookupCtxtExact tn (gamma defs)
1782 | | _ => undefinedName fc tn
1783 | let TCon a ps dets u _ cons det = definition g
1784 | | _ => throw (GenericMsg fc (show (fullname g) ++ " is not a type constructor [setMutWith]"))
1785 | updateDef tn (const (Just (TCon a ps dets u tns cons det)))
1788 | addMutData : {auto c : Ref Ctxt Defs} ->
1790 | addMutData n = update Ctxt { mutData $= (n ::) }
1793 | dropMutData : {auto c : Ref Ctxt Defs} ->
1795 | dropMutData n = update Ctxt { mutData $= filter (/= n) }
1798 | setDetermining : {auto c : Ref Ctxt Defs} ->
1799 | FC -> Name -> List1 Name -> Core ()
1800 | setDetermining fc tyn args
1802 | Just g <- lookupCtxtExact tyn (gamma defs)
1803 | | _ => undefinedName fc tyn
1804 | let TCon a ps _ u cons ms det = definition g
1805 | | _ => throw (GenericMsg fc (show (fullname g) ++ " is not a type constructor [setDetermining]"))
1806 | apos <- getPos 0 (forget args) (type g)
1807 | updateDef tyn (const (Just (TCon a ps apos u cons ms det)))
1811 | getPos : Nat -> List Name -> Term vs -> Core NatSet
1812 | getPos i ns (Bind _ x (Pi {}) sc)
1814 | then do rest <- getPos (1 + i) (filter (/=x) ns) sc
1816 | else getPos (1 + i) ns sc
1817 | getPos _ [] _ = pure NatSet.empty
1818 | getPos _ ns ty = throw (GenericMsg fc ("Unknown determining arguments: "
1819 | ++ showSep ", " (map show ns)))
1822 | setDetags : {auto c : Ref Ctxt Defs} ->
1823 | FC -> Name -> Maybe NatSet -> Core ()
1826 | Just g <- lookupCtxtExact tyn (gamma defs)
1827 | | _ => undefinedName fc tyn
1828 | let TCon a ps det u cons ms _ = definition g
1829 | | _ => throw (GenericMsg fc (show (fullname g) ++ " is not a type constructor [setDetermining]"))
1830 | updateDef tyn (const (Just (TCon a ps det u cons ms args)))
1833 | setUniqueSearch : {auto c : Ref Ctxt Defs} ->
1834 | FC -> Name -> Bool -> Core ()
1835 | setUniqueSearch fc tyn u
1837 | Just g <- lookupCtxtExact tyn (gamma defs)
1838 | | _ => undefinedName fc tyn
1839 | let TCon a ps ds fl cons ms det = definition g
1840 | | _ => throw (GenericMsg fc (show (fullname g) ++ " is not a type constructor [setDetermining]"))
1841 | let fl' = { uniqueAuto := u } fl
1842 | updateDef tyn (const (Just (TCon a ps ds fl' cons ms det)))
1845 | setExternal : {auto c : Ref Ctxt Defs} ->
1846 | FC -> Name -> Bool -> Core ()
1849 | Just g <- lookupCtxtExact tyn (gamma defs)
1850 | | _ => undefinedName fc tyn
1851 | let TCon a ps ds fl cons ms det = definition g
1852 | | _ => throw (GenericMsg fc (show (fullname g) ++ " is not a type constructor [setDetermining]"))
1853 | let fl' = { external := u } fl
1854 | updateDef tyn (const (Just (TCon a ps ds fl' cons ms det)))
1857 | addHintFor : {auto c : Ref Ctxt Defs} ->
1858 | FC -> Name -> Name -> Bool -> Bool -> Core ()
1859 | addHintFor fc tyn_in hintn_in direct loading
1861 | tyn <- toFullNames tyn_in
1865 | hintn <- toResolvedNames hintn_in
1867 | let hs = case lookup tyn (typeHints defs) of
1872 | ({ typeHints $= insert tyn ((hintn, direct) :: hs)
1875 | ({ typeHints $= insert tyn ((hintn, direct) :: hs),
1876 | saveTypeHints $= ((tyn, hintn, direct) :: )
1880 | addGlobalHint : {auto c : Ref Ctxt Defs} ->
1881 | Name -> Bool -> Core ()
1882 | addGlobalHint hintn_in isdef
1883 | = do hintn <- toResolvedNames hintn_in
1884 | update Ctxt { autoHints $= insert hintn isdef,
1885 | saveAutoHints $= ((hintn, isdef) ::) }
1888 | addLocalHint : {auto c : Ref Ctxt Defs} ->
1891 | = do hintn <- toResolvedNames hintn_in
1892 | update Ctxt { localHints $= insert hintn () }
1895 | addOpenHint : {auto c : Ref Ctxt Defs} -> Name -> Core ()
1897 | = do hintn <- toResolvedNames hintn_in
1898 | update Ctxt { openHints $= insert hintn () }
1901 | dropOpenHint : {auto c : Ref Ctxt Defs} -> Name -> Core ()
1903 | = do hintn <- toResolvedNames hintn_in
1904 | update Ctxt { openHints $= delete hintn }
1907 | setOpenHints : {auto c : Ref Ctxt Defs} -> NameMap () -> Core ()
1908 | setOpenHints hs = update Ctxt { openHints := hs }
1911 | addTransform : {auto c : Ref Ctxt Defs} ->
1912 | FC -> Transform -> Core ()
1915 | let Just fn_in = getFnName t_in
1917 | throw (GenericMsg fc "LHS of a transformation must be a function application")
1918 | fn <- toResolvedNames fn_in
1919 | t <- toResolvedNames t_in
1920 | fn_full <- toFullNames fn_in
1921 | t_full <- toFullNames t_in
1922 | case lookup fn (transforms defs) of
1924 | put Ctxt ({ transforms $= insert fn [t],
1925 | saveTransforms $= ((fn_full, t_full) ::) } defs)
1927 | put Ctxt ({ transforms $= insert fn (t :: ts),
1928 | saveTransforms $= ((fn_full, t_full) ::) } defs)
1931 | clearSavedHints : {auto c : Ref Ctxt Defs} -> Core ()
1932 | clearSavedHints = update Ctxt { saveTypeHints := [], saveAutoHints := [] }
1936 | setNS : {auto c : Ref Ctxt Defs} -> Namespace -> Core ()
1937 | setNS ns = update Ctxt { currentNS := ns }
1941 | setNestedNS : {auto c : Ref Ctxt Defs} ->
1942 | List Namespace -> Core ()
1943 | setNestedNS ns = update Ctxt { nestedNS := ns }
1947 | getNS : {auto c : Ref Ctxt Defs} ->
1955 | getNestedNS : {auto c : Ref Ctxt Defs} ->
1966 | addImported : {auto c : Ref Ctxt Defs} ->
1967 | (ModuleIdent, Bool, Namespace) -> Core ()
1968 | addImported mod = update Ctxt { imported $= (mod ::) }
1971 | getImported : {auto c : Ref Ctxt Defs} ->
1972 | Core (List (ModuleIdent, Bool, Namespace))
1978 | addDirective : {auto c : Ref Ctxt Defs} ->
1979 | String -> String -> Core ()
1982 | case getCG (options defs) c of
1985 | coreLift $
putStrLn $
"Unknown code generator " ++ c
1986 | Just cg => put Ctxt ({ cgdirectives $= ((cg, str) ::) } defs)
1989 | getDirectives : {auto c : Ref Ctxt Defs} ->
1990 | CG -> Core (List String)
1993 | pure $
defs.options.session.directives ++
1994 | mapMaybe getDir (cgdirectives defs)
1996 | getDir : (CG, String) -> Maybe String
1997 | getDir (x', str) = if cg == x' then Just str else Nothing
2004 | extendNS : {auto c : Ref Ctxt Defs} -> Namespace -> Core ()
2005 | extendNS ns = update Ctxt { currentNS $= (<.> ns) }
2008 | withExtendedNS : {auto c : Ref Ctxt Defs} ->
2009 | Namespace -> Core a -> Core a
2012 | let cns = currentNS defs
2013 | put Ctxt ({ currentNS := cns <.> ns } defs)
2014 | ma <- catch (Right <$> act) (pure . Left)
2016 | put Ctxt ({ currentNS := cns } defs)
2025 | inCurrentNS : {auto c : Ref Ctxt Defs} ->
2029 | pure (NS (currentNS defs) (UN n))
2030 | inCurrentNS n@(CaseBlock {})
2032 | pure (NS (currentNS defs) n)
2033 | inCurrentNS n@(WithBlock {})
2035 | pure (NS (currentNS defs) n)
2036 | inCurrentNS n@(Nested {})
2038 | pure (NS (currentNS defs) n)
2041 | pure (NS (currentNS defs) n)
2044 | pure (NS (currentNS defs) n)
2048 | setVisible : {auto c : Ref Ctxt Defs} ->
2050 | setVisible nspace = update Ctxt { gamma->visibleNS $= (nspace ::) }
2053 | getVisible : {auto c : Ref Ctxt Defs} ->
2057 | pure (visibleNS (gamma defs))
2063 | setAllPublic : {auto c : Ref Ctxt Defs} ->
2064 | (pub : Bool) -> Core ()
2065 | setAllPublic pub = update Ctxt { gamma->allPublic := pub }
2068 | isAllPublic : {auto c : Ref Ctxt Defs} ->
2072 | pure (allPublic (gamma defs))
2077 | isVisible : {auto c : Ref Ctxt Defs} ->
2081 | pure (any visible (allParents (currentNS defs) ++
2083 | visibleNS (gamma defs)))
2088 | visible : Namespace -> Bool
2089 | visible visns = isParentOf visns nspace
2094 | getNextEntry : {auto c : Ref Ctxt Defs} ->
2098 | pure (nextEntry (gamma defs))
2101 | setNextEntry : {auto c : Ref Ctxt Defs} ->
2103 | setNextEntry i = update Ctxt { gamma->nextEntry := i }
2108 | resetFirstEntry : {auto c : Ref Ctxt Defs} ->
2112 | put Ctxt ({ gamma->firstEntry := nextEntry (gamma defs) } defs)
2115 | getFullName : {auto c : Ref Ctxt Defs} ->
2117 | getFullName (Resolved i)
2119 | Just gdef <- lookupCtxtExact (Resolved i) (gamma defs)
2120 | | Nothing => pure (Resolved i)
2127 | getPPrint : {auto c : Ref Ctxt Defs} ->
2131 | pure (printing (options defs))
2134 | setPPrint : {auto c : Ref Ctxt Defs} -> PPrinter -> Core ()
2135 | setPPrint ppopts = update Ctxt { options->printing := ppopts }
2138 | updatePPrint : {auto c : Ref Ctxt Defs} -> (PPrinter -> PPrinter) -> Core ()
2139 | updatePPrint f = update Ctxt { options->printing $= f }
2142 | setCG : {auto c : Ref Ctxt Defs} -> CG -> Core ()
2143 | setCG cg = update Ctxt { options->session->codegen := cg }
2146 | getDirs : {auto c : Ref Ctxt Defs} -> Core Dirs
2149 | pure (dirs (options defs))
2152 | addExtraDir : {auto c : Ref Ctxt Defs} -> String -> Core ()
2153 | addExtraDir dir = update Ctxt { options->dirs->extra_dirs $= ((::) dir) . filter (/= dir) }
2156 | addPackageDir: {auto c : Ref Ctxt Defs} -> String -> Core ()
2157 | addPackageDir dir = update Ctxt { options->dirs->package_dirs $= ((::) dir) . filter (/= dir) }
2160 | addPackageSearchPath: {auto c : Ref Ctxt Defs} -> String -> Core ()
2161 | addPackageSearchPath dir =
2162 | let newPath = parse dir
2163 | in update Ctxt { options->dirs->package_search_paths $= ((::) newPath) . filter (/= newPath) }
2166 | addDataDir : {auto c : Ref Ctxt Defs} -> String -> Core ()
2167 | addDataDir dir = update Ctxt { options->dirs->data_dirs $= (++ [dir]) }
2170 | addLibDir : {auto c : Ref Ctxt Defs} -> String -> Core ()
2171 | addLibDir dir = update Ctxt { options->dirs->lib_dirs $= (++ [dir]) }
2174 | setBuildDir : {auto c : Ref Ctxt Defs} -> String -> Core ()
2175 | setBuildDir dir = update Ctxt { options->dirs->build_dir := dir }
2178 | setDependsDir : {auto c : Ref Ctxt Defs} -> String -> Core ()
2179 | setDependsDir dir = update Ctxt { options->dirs->depends_dir := dir }
2182 | setOutputDir : {auto c : Ref Ctxt Defs} -> Maybe String -> Core ()
2183 | setOutputDir dir = update Ctxt { options->dirs->output_dir := dir }
2186 | setSourceDir : {auto c : Ref Ctxt Defs} -> Maybe String -> Core ()
2187 | setSourceDir mdir = update Ctxt { options->dirs->source_dir := mdir }
2190 | setWorkingDir : {auto c : Ref Ctxt Defs} -> String -> Core ()
2192 | = do coreLift_ $
changeDir dir
2193 | Just cdir <- coreLift $
currentDir
2194 | | Nothing => throw (InternalError "Can't get current directory")
2195 | update Ctxt { options->dirs->working_dir := cdir }
2198 | getWorkingDir : Core String
2200 | = do Just d <- coreLift $
currentDir
2201 | | Nothing => throw (InternalError "Can't get current directory")
2205 | setExtraDirs : {auto c : Ref Ctxt Defs} -> List String -> Core ()
2206 | setExtraDirs dirs = update Ctxt { options->dirs->extra_dirs := dirs }
2209 | setPackageDirs : {auto c : Ref Ctxt Defs} -> List String -> Core ()
2210 | setPackageDirs dirs = update Ctxt { options->dirs->package_dirs := dirs }
2213 | withCtxt : {auto c : Ref Ctxt Defs} -> Core a -> Core a
2214 | withCtxt = wrapRef Ctxt resetCtxt
2216 | resetCtxt : Defs -> Core ()
2217 | resetCtxt defs = do let dir = defs.options.dirs.working_dir
2218 | coreLift_ $
changeDir dir
2221 | setPrefix : {auto c : Ref Ctxt Defs} -> String -> Core ()
2222 | setPrefix dir = update Ctxt { options->dirs->prefix_dir := dir }
2225 | setExtension : {auto c : Ref Ctxt Defs} -> LangExt -> Core ()
2226 | setExtension e = update Ctxt { options $= setExtension e }
2229 | isExtension : LangExt -> Defs -> Bool
2230 | isExtension e defs = isExtension e (options defs)
2233 | checkUnambig : {auto c : Ref Ctxt Defs} ->
2234 | FC -> Name -> Core Name
2237 | case !(lookupDefName n (gamma defs)) of
2238 | [(fulln, i, _)] => pure (Resolved i)
2239 | ns => ambiguousName fc n (map fst ns)
2242 | lazyActive : {auto c : Ref Ctxt Defs} -> Bool -> Core ()
2243 | lazyActive a = update Ctxt { options->elabDirectives->lazyActive := a }
2246 | setUnboundImplicits : {auto c : Ref Ctxt Defs} -> Bool -> Core ()
2247 | setUnboundImplicits a = update Ctxt { options->elabDirectives->unboundImplicits := a }
2250 | setPrefixRecordProjections : {auto c : Ref Ctxt Defs} -> Bool -> Core ()
2251 | setPrefixRecordProjections b = update Ctxt { options->elabDirectives->prefixRecordProjections := b }
2254 | setDefaultTotalityOption : {auto c : Ref Ctxt Defs} ->
2256 | setDefaultTotalityOption tot = update Ctxt { options->elabDirectives->totality := tot }
2259 | setAmbigLimit : {auto c : Ref Ctxt Defs} ->
2261 | setAmbigLimit max = update Ctxt { options->elabDirectives->ambigLimit := max }
2264 | setTotalLimit : {auto c : Ref Ctxt Defs} ->
2266 | setTotalLimit max = update Ctxt { options->elabDirectives->totalLimit := max }
2269 | setAutoImplicitLimit : {auto c : Ref Ctxt Defs} ->
2271 | setAutoImplicitLimit max = update Ctxt { options->elabDirectives->autoImplicitLimit := max }
2274 | setNFThreshold : {auto c : Ref Ctxt Defs} ->
2276 | setNFThreshold max = update Ctxt { options->elabDirectives->nfThreshold := max }
2279 | setSearchTimeout : {auto c : Ref Ctxt Defs} ->
2281 | setSearchTimeout t = update Ctxt { options->session->searchTimeout := t }
2284 | isLazyActive : {auto c : Ref Ctxt Defs} ->
2288 | pure (lazyActive (elabDirectives (options defs)))
2291 | isUnboundImplicits : {auto c : Ref Ctxt Defs} ->
2295 | pure (unboundImplicits (elabDirectives (options defs)))
2298 | isPrefixRecordProjections : {auto c : Ref Ctxt Defs} -> Core Bool
2299 | isPrefixRecordProjections =
2300 | prefixRecordProjections . elabDirectives . options <$> get Ctxt
2303 | getDefaultTotalityOption : {auto c : Ref Ctxt Defs} ->
2305 | getDefaultTotalityOption
2307 | pure (totality (elabDirectives (options defs)))
2310 | getAmbigLimit : {auto c : Ref Ctxt Defs} ->
2314 | pure (ambigLimit (elabDirectives (options defs)))
2317 | getAutoImplicitLimit : {auto c : Ref Ctxt Defs} ->
2321 | pure (autoImplicitLimit (elabDirectives (options defs)))
2324 | addForeignImpl : {auto c : Ref Ctxt Defs} ->
2325 | FC -> (fullName : Name) -> (def : String) -> Core ()
2326 | addForeignImpl fc name def
2327 | = do name <- toFullNames name
2328 | update Ctxt { options $= addForeignImpl name def }
2331 | setPair : {auto c : Ref Ctxt Defs} ->
2332 | FC -> (pairType : Name) -> (fstn : Name) -> (sndn : Name) ->
2335 | = do ty' <- checkUnambig fc ty
2336 | f' <- checkUnambig fc f
2337 | s' <- checkUnambig fc s
2338 | update Ctxt { options $= setPair ty' f' s' }
2341 | setRewrite : {auto c : Ref Ctxt Defs} ->
2342 | FC -> (eq : Name) -> (rwlemma : Name) -> Core ()
2344 | = do rw' <- checkUnambig fc rw
2345 | eq' <- checkUnambig fc eq
2346 | update Ctxt { options $= setRewrite eq' rw' }
2350 | setFromInteger : {auto c : Ref Ctxt Defs} ->
2352 | setFromInteger n = update Ctxt { options $= setFromInteger n }
2355 | setFromString : {auto c : Ref Ctxt Defs} ->
2357 | setFromString n = update Ctxt { options $= setFromString n }
2360 | setFromChar : {auto c : Ref Ctxt Defs} ->
2362 | setFromChar n = update Ctxt { options $= setFromChar n }
2365 | setFromDouble : {auto c : Ref Ctxt Defs} ->
2367 | setFromDouble n = update Ctxt { options $= setFromDouble n }
2370 | setFromTTImp : {auto c : Ref Ctxt Defs} ->
2372 | setFromTTImp n = update Ctxt { options $= setFromTTImp n }
2375 | setFromName : {auto c : Ref Ctxt Defs} ->
2377 | setFromName n = update Ctxt { options $= setFromName n }
2380 | setFromDecls : {auto c : Ref Ctxt Defs} ->
2382 | setFromDecls n = update Ctxt { options $= setFromDecls n }
2385 | addNameDirective : {auto c : Ref Ctxt Defs} ->
2386 | FC -> Name -> List String -> Core ()
2387 | addNameDirective fc n ns
2388 | = do n' <- checkUnambig fc n
2389 | update Ctxt { namedirectives $= insert n' ns }
2394 | isPairType : {auto c : Ref Ctxt Defs} ->
2398 | case pairnames (options defs) of
2400 | Just l => pure $
!(getFullName n) == !(getFullName (pairType l))
2403 | fstName : {auto c : Ref Ctxt Defs} ->
2407 | pure $
maybe Nothing (Just . fstName) (pairnames (options defs))
2410 | sndName : {auto c : Ref Ctxt Defs} ->
2414 | pure $
maybe Nothing (Just . sndName) (pairnames (options defs))
2417 | getRewrite :{auto c : Ref Ctxt Defs} ->
2421 | pure $
maybe Nothing (Just . rewriteName) (rewritenames (options defs))
2424 | isEqualTy : {auto c : Ref Ctxt Defs} ->
2428 | case rewritenames (options defs) of
2430 | Just r => pure $
!(getFullName n) == !(getFullName (equalType r))
2433 | fromIntegerName : {auto c : Ref Ctxt Defs} ->
2437 | pure $
fromIntegerName (primnames (options defs))
2440 | fromStringName : {auto c : Ref Ctxt Defs} ->
2444 | pure $
fromStringName (primnames (options defs))
2447 | fromCharName : {auto c : Ref Ctxt Defs} ->
2451 | pure $
fromCharName (primnames (options defs))
2454 | fromDoubleName : {auto c : Ref Ctxt Defs} ->
2458 | pure $
fromDoubleName (primnames (options defs))
2461 | fromTTImpName : {auto c : Ref Ctxt Defs} ->
2465 | pure $
fromTTImpName (primnames (options defs))
2468 | fromNameName : {auto c : Ref Ctxt Defs} ->
2472 | pure $
fromNameName (primnames (options defs))
2475 | fromDeclsName : {auto c : Ref Ctxt Defs} ->
2479 | pure $
fromDeclsName (primnames (options defs))
2482 | getPrimNames : {auto c : Ref Ctxt Defs} -> Core PrimNames
2483 | getPrimNames = [| MkPrimNs fromIntegerName
2492 | getPrimitiveNames : {auto c : Ref Ctxt Defs} -> Core (List Name)
2493 | getPrimitiveNames = primNamesToList <$> getPrimNames
2496 | isPrimName : List Name -> Name -> Bool
2497 | isPrimName prims given = let (ns, nm) = splitNS given in go ns nm prims where
2499 | go : Namespace -> Name -> List Name -> Bool
2502 | = let (ns', nm') = splitNS p in
2503 | (nm' == nm && (ns' `isApproximationOf` ns))
2507 | addLogLevel : {auto c : Ref Ctxt Defs} ->
2508 | Maybe LogLevel -> Core ()
2509 | addLogLevel Nothing = update Ctxt { options->session->logEnabled := False, options->session->logLevel := defaultLogLevel }
2510 | addLogLevel (Just l) = update Ctxt { options->session->logEnabled := True, options->session->logLevel $= insertLogLevel l }
2513 | setLogLevel : {auto c : Ref Ctxt Defs} ->
2515 | setLogLevel = addLogLevel . Just
2518 | stopLogging : {auto c : Ref Ctxt Defs} -> Core ()
2519 | stopLogging = addLogLevel Nothing
2522 | withLogLevel : {auto c : Ref Ctxt Defs} ->
2523 | LogLevel -> Core a -> Core a
2524 | withLogLevel l comp = do
2526 | let logs = logLevel (session (options defs))
2527 | put Ctxt ({ options->session->logLevel := insertLogLevel l logs } defs)
2530 | put Ctxt ({ options->session->logLevel := logs } defs)
2534 | setLogTimings : {auto c : Ref Ctxt Defs} -> Nat -> Core ()
2535 | setLogTimings n = update Ctxt { options->session->logTimings := Just n }
2538 | setDebugElabCheck : {auto c : Ref Ctxt Defs} -> Bool -> Core ()
2539 | setDebugElabCheck b = update Ctxt { options->session->debugElabCheck := b }
2542 | getSession : {auto c : Ref Ctxt Defs} ->
2546 | pure (session (options defs))
2549 | setSession : {auto c : Ref Ctxt Defs} -> Session -> Core ()
2550 | setSession sopts = update Ctxt { options->session := sopts }
2554 | updateSession : {auto c : Ref Ctxt Defs} ->
2555 | (Session -> Session) -> Core ()
2556 | updateSession f = setSession (f !getSession)
2559 | recordWarning : {auto c : Ref Ctxt Defs} -> Warning -> Core ()
2560 | recordWarning w = update Ctxt { warnings $= (w ::) }
2565 | = do clock <- coreLift (clockTime Monotonic)
2566 | pure (seconds clock * nano + nanoseconds clock)
2580 | startTimer : {auto c : Ref Ctxt Defs} ->
2581 | Integer -> String -> Core ()
2584 | update Ctxt { timer := Just (t + tmax * 1000000, action) }
2588 | clearTimer : {auto c : Ref Ctxt Defs} -> Core ()
2589 | clearTimer = update Ctxt { timer := Nothing }
2593 | checkTimer : {auto c : Ref Ctxt Defs} ->
2597 | let Just (max, action) = timer defs
2601 | then throw (Timeout action)
2607 | addImportedInc : {auto c : Ref Ctxt Defs} ->
2608 | ModuleIdent -> List (CG, String, List String) -> Core ()
2609 | addImportedInc modNS inc
2613 | when (cg `elem` s.incrementalCGs) $
2618 | do recordWarning (GenericWarn emptyFC ("No incremental compile data for " ++ show modNS))
2620 | put Ctxt ({ allIncData $= drop cg } defs)
2622 | when (show modNS /= "") $
2623 | updateSession { incrementalCGs $= (delete cg) }
2625 | put Ctxt ({ allIncData $= addMod cg (mods, extra) }
2628 | addMod : CG -> (String, List String) ->
2629 | List (CG, (List String, List String)) ->
2630 | List (CG, (List String, List String))
2631 | addMod cg (mod, all) [] = [(cg, ([mod], all))]
2632 | addMod cg (mod, all) ((cg', (mods, libs)) :: xs)
2634 | then ((cg, (mod :: mods, libs ++ all)) :: xs)
2635 | else ((cg', (mods, libs)) :: addMod cg (mod, all) xs)
2637 | drop : CG -> List (CG, a) -> List (CG, a)
2642 | else ((x, v) :: drop cg xs)
2645 | setIncData : {auto c : Ref Ctxt Defs} ->
2646 | CG -> (String, List String) -> Core ()
2647 | setIncData cg res = update Ctxt { incData $= ((cg, res) :: )}
2652 | hide : {auto c : Ref Ctxt Defs} ->
2656 | [(nsn, _)] <- lookupCtxtName n (gamma defs)
2657 | | res => ambiguousName fc n (map fst res)
2658 | put Ctxt ({ gamma $= hideName nsn } defs)
2663 | unhide : {auto c : Ref Ctxt Defs} ->
2667 | [(nsn, _)] <- lookupHiddenCtxtName n (gamma defs)
2668 | | res => ambiguousName fc n (map fst res)
2669 | put Ctxt ({ gamma $= unhideName nsn } defs)
2670 | unless (isHidden nsn (gamma defs)) $
do
2671 | recordWarning $
GenericWarn fc $
2672 | "Trying to %unhide `" ++ show nsn ++ "`, which was not hidden in the first place"