2 | import Core.Context.Log
4 | import Core.Normalise
7 | import public Data.List1
8 | import Data.SortedSet
10 | import Libraries.Data.List.SizeOf
11 | import Libraries.Data.WithDefault
17 | record NestedNames (vars : Scope) where
18 | constructor MkNested
23 | names : List (Name, (Maybe Name,
25 | FC -> NameType -> Term vars))
28 | Weaken NestedNames where
29 | weakenNs {ns = wkns} s (MkNested ns) = MkNested (map wknName ns)
31 | wknName : (Name, (Maybe Name, List (Var vars), FC -> NameType -> Term vars)) ->
32 | (Name, (Maybe Name, List (Var (wkns ++ vars)), FC -> NameType -> Term (wkns ++ vars)))
33 | wknName (n, (mn, vars, rep))
34 | = (n, (mn, map (weakenNs s) vars, \fc, nt => weakenNs s (rep fc nt)))
38 | mapNestedName : NestedNames vars -> Name -> Name
39 | mapNestedName nest n = case lookup n (names nest) of
40 | (Just (Just n', _)) => n'
49 | data BindMode = PI RigCount | PATTERN | COVERAGE | NONE
57 | RawImp = RawImp' Name
61 | IRawImp = RawImp' KindedName
64 | data RawImp' : Type -> Type where
65 | IVar : FC -> nm -> RawImp' nm
66 | IPi : FC -> RigCount -> PiInfo (RawImp' nm) -> Maybe Name ->
67 | (argTy : RawImp' nm) -> (retTy : RawImp' nm) -> RawImp' nm
68 | ILam : FC -> RigCount -> PiInfo (RawImp' nm) -> Maybe Name ->
69 | (argTy : RawImp' nm) -> (lamTy : RawImp' nm) -> RawImp' nm
70 | ILet : FC -> (lhsFC : FC) -> RigCount -> Name ->
71 | (nTy : RawImp' nm) -> (nVal : RawImp' nm) ->
72 | (scope : RawImp' nm) -> RawImp' nm
73 | ICase : FC -> List (FnOpt' nm) -> RawImp' nm -> (ty : RawImp' nm) ->
74 | List (ImpClause' nm) -> RawImp' nm
75 | ILocal : FC -> List (ImpDecl' nm) -> RawImp' nm -> RawImp' nm
80 | ICaseLocal : FC -> (uname : Name) ->
81 | (internalName : Name) ->
82 | (args : List Name) -> RawImp' nm -> RawImp' nm
84 | IUpdate : FC -> List (IFieldUpdate' nm) -> RawImp' nm -> RawImp' nm
86 | IApp : FC -> RawImp' nm -> RawImp' nm -> RawImp' nm
87 | IAutoApp : FC -> RawImp' nm -> RawImp' nm -> RawImp' nm
88 | INamedApp : FC -> RawImp' nm -> Name -> RawImp' nm -> RawImp' nm
89 | IWithApp : FC -> RawImp' nm -> RawImp' nm -> RawImp' nm
91 | ISearch : FC -> (depth : Nat) -> RawImp' nm
92 | IAlternative : FC -> AltType' nm -> List (RawImp' nm) -> RawImp' nm
93 | IRewrite : FC -> RawImp' nm -> RawImp' nm -> RawImp' nm
94 | ICoerced : FC -> RawImp' nm -> RawImp' nm
98 | IBindHere : FC -> BindMode -> RawImp' nm -> RawImp' nm
100 | IBindVar : FC -> Name -> RawImp' nm
102 | IAs : FC -> (nameFC : FC) -> UseSide -> Name -> RawImp' nm -> RawImp' nm
105 | IMustUnify : FC -> DotReason -> RawImp' nm -> RawImp' nm
108 | IDelayed : FC -> LazyReason -> RawImp' nm -> RawImp' nm
109 | IDelay : FC -> RawImp' nm -> RawImp' nm
110 | IForce : FC -> RawImp' nm -> RawImp' nm
113 | IQuote : FC -> RawImp' nm -> RawImp' nm
114 | IQuoteName : FC -> Name -> RawImp' nm
115 | IQuoteDecl : FC -> List (ImpDecl' nm) -> RawImp' nm
116 | IUnquote : FC -> RawImp' nm -> RawImp' nm
117 | IRunElab : FC -> (requireExtension : Bool) -> RawImp' nm -> RawImp' nm
119 | IPrimVal : FC -> (c : Constant) -> RawImp' nm
120 | IType : FC -> RawImp' nm
121 | IHole : FC -> String -> RawImp' nm
123 | IUnifyLog : FC -> LogLevel -> RawImp' nm -> RawImp' nm
127 | Implicit : FC -> (bindIfUnsolved : Bool) -> RawImp' nm
130 | IWithUnambigNames : FC -> List (FC, Name) -> RawImp' nm -> RawImp' nm
135 | IFieldUpdate : Type
136 | IFieldUpdate = IFieldUpdate' Name
139 | data IFieldUpdate' : Type -> Type where
140 | ISetField : (path : List String) -> RawImp' nm -> IFieldUpdate' nm
141 | ISetFieldApp : (path : List String) -> RawImp' nm -> IFieldUpdate' nm
142 | %name IFieldUpdate'
upd
146 | AltType = AltType' Name
149 | data AltType' : Type -> Type where
150 | FirstSuccess : AltType' nm
151 | Unique : AltType' nm
152 | UniqueDefault : RawImp' nm -> AltType' nm
157 | Show nm => Show (RawImp' nm) where
158 | show (IVar fc n) = show n
159 | show (IPi fc c p n arg ret)
160 | = "(%pi " ++ show c ++ " " ++ show p ++ " " ++
161 | showPrec App n ++ " " ++ show arg ++ " " ++ show ret ++ ")"
162 | show (ILam fc c p n arg sc)
163 | = "(%lam " ++ show c ++ " " ++ show p ++ " " ++
164 | showPrec App n ++ " " ++ show arg ++ " " ++ show sc ++ ")"
165 | show (ILet fc lhsFC c n ty val sc)
166 | = "(%let " ++ show c ++ " " ++ " " ++ show n ++ " " ++ show ty ++
167 | " " ++ show val ++ " " ++ show sc ++ ")"
168 | show (ICase _ _ scr scrty alts)
169 | = "(%case (" ++ show scr ++ " : " ++ show scrty ++ ") " ++ show alts ++ ")"
170 | show (ILocal _ def scope)
171 | = "(%local (" ++ show def ++ ") " ++ show scope ++ ")"
172 | show (ICaseLocal _ uname iname args sc)
173 | = "(%caselocal (" ++ show uname ++ " " ++ show iname
174 | ++ " " ++ show args ++ ") " ++ show sc ++ ")"
175 | show (IUpdate _ flds rec)
176 | = "(%record " ++ showSep ", " (map show flds) ++ " " ++ show rec ++ ")"
178 | = "(" ++ show f ++ " " ++ show a ++ ")"
179 | show (INamedApp fc f n a)
180 | = "(" ++ show f ++ " [" ++ show n ++ " = " ++ show a ++ "])"
181 | show (IAutoApp fc f a)
182 | = "(" ++ show f ++ " [" ++ show a ++ "])"
183 | show (IWithApp fc f a)
184 | = "(" ++ show f ++ " | " ++ show a ++ ")"
185 | show (ISearch fc d)
187 | show (IAlternative fc ty alts)
188 | = "(|" ++ showSep "," (map show alts) ++ "|)"
189 | show (IRewrite _ rule tm)
190 | = "(%rewrite (" ++ show rule ++ ") (" ++ show tm ++ "))"
191 | show (ICoerced _ tm) = "(%coerced " ++ show tm ++ ")"
193 | show (IBindHere fc b sc)
194 | = "(%bindhere " ++ show sc ++ ")"
195 | show (IBindVar fc n) = "$" ++ show n
196 | show (IAs fc _ _ n tm) = show n ++ "@(" ++ show tm ++ ")"
197 | show (IMustUnify fc r tm) = ".(" ++ show tm ++ ")"
198 | show (IDelayed fc r tm) = "(%delayed " ++ show tm ++ ")"
199 | show (IDelay fc tm) = "(%delay " ++ show tm ++ ")"
200 | show (IForce fc tm) = "(%force " ++ show tm ++ ")"
201 | show (IQuote fc tm) = "(%quote " ++ show tm ++ ")"
202 | show (IQuoteName fc tm) = "(%quotename " ++ show tm ++ ")"
203 | show (IQuoteDecl fc tm) = "(%quotedecl " ++ show tm ++ ")"
204 | show (IUnquote fc tm) = "(%unquote " ++ show tm ++ ")"
205 | show (IRunElab fc _ tm) = "(%runelab " ++ show tm ++ ")"
206 | show (IPrimVal fc c) = show c
207 | show (IHole _ x) = "?" ++ x
208 | show (IUnifyLog _ lvl x) = "(%logging " ++ show lvl ++ " " ++ show x ++ ")"
209 | show (IType fc) = "%type"
210 | show (Implicit fc True) = "_"
211 | show (Implicit fc False) = "?"
212 | show (IWithUnambigNames fc ns rhs) = "(%with " ++ show ns ++ " " ++ show rhs ++ ")"
216 | Show nm => Show (IFieldUpdate' nm) where
217 | show (ISetField p val) = showSep "->" p ++ " = " ++ show val
218 | show (ISetFieldApp p val) = showSep "->" p ++ " $= " ++ show val
222 | FnOpt = FnOpt' Name
225 | data FnOpt' : Type -> Type where
228 | NoInline : FnOpt' nm
230 | Deprecate : FnOpt' nm
231 | TCInline : FnOpt' nm
234 | Hint : Bool -> FnOpt' nm
236 | GlobalHint : Bool -> FnOpt' nm
237 | ExternFn : FnOpt' nm
239 | ForeignFn : List (RawImp' nm) -> FnOpt' nm
241 | ForeignExport : List (RawImp' nm) -> FnOpt' nm
243 | Invertible : FnOpt' nm
244 | Totality : TotalReq -> FnOpt' nm
246 | SpecArgs : List Name -> FnOpt' nm
250 | isTotalityReq : FnOpt' nm -> Bool
251 | isTotalityReq (Totality _) = True
252 | isTotalityReq _ = False
255 | extractTotality : FnOpt' nm -> Maybe TotalReq
256 | extractTotality (Totality t) = Just t
257 | extractTotality _ = Nothing
260 | findTotality : List (FnOpt' nm) -> Maybe TotalReq
261 | findTotality = foldr (\elem, acc => extractTotality elem <|> acc) empty
265 | Show nm => Show (FnOpt' nm) where
266 | show Unsafe = "%unsafe"
267 | show Inline = "%inline"
268 | show NoInline = "%noinline"
269 | show Deprecate = "%deprecate"
270 | show TCInline = "%tcinline"
271 | show (Hint t) = "%hint " ++ show t
272 | show (GlobalHint t) = "%globalhint " ++ show t
273 | show ExternFn = "%extern"
274 | show (ForeignFn cs) = "%foreign " ++ showSep " " (map show cs)
275 | show (ForeignExport cs) = "%export " ++ showSep " " (map show cs)
276 | show Invertible = "%invertible"
277 | show (Totality Total) = "total"
278 | show (Totality CoveringOnly) = "covering"
279 | show (Totality PartialOK) = "partial"
280 | show Macro = "%macro"
281 | show (SpecArgs ns) = "%spec " ++ showSep " " (map show ns)
285 | Inline == Inline = True
286 | NoInline == NoInline = True
287 | Deprecate == Deprecate = True
288 | TCInline == TCInline = True
289 | (Hint x) == (Hint y) = x == y
290 | (GlobalHint x) == (GlobalHint y) = x == y
291 | ExternFn == ExternFn = True
292 | (ForeignFn xs) == (ForeignFn ys) = True
293 | (ForeignExport xs) == (ForeignExport ys) = True
294 | Invertible == Invertible = True
295 | (Totality tot_lhs) == (Totality tot_rhs) = tot_lhs == tot_rhs
296 | Macro == Macro = True
297 | (SpecArgs ns) == (SpecArgs ns') = ns == ns'
302 | ImpTy = ImpTy' Name
305 | ImpTy' : Type -> Type
306 | ImpTy' = AddMetadata FC' . AddMetadata TyName' . RawImp'
310 | Show nm => Show (ImpTy' nm) where
311 | show ty = "(%claim " ++ show ty.tyName.val ++ " " ++ show ty.val ++ ")"
315 | ImpData = ImpData' Name
318 | data ImpData' : Type -> Type where
319 | MkImpData : FC -> (n : Name) ->
322 | (tycon : Maybe (RawImp' nm)) ->
323 | (opts : List DataOpt) ->
324 | (datacons : List (ImpTy' nm)) -> ImpData' nm
325 | MkImpLater : FC -> (n : Name) -> (tycon : RawImp' nm) -> ImpData' nm
331 | Show nm => Show (ImpData' nm) where
332 | show (MkImpData fc n (Just tycon) _ cons)
333 | = "(%data " ++ show n ++ " " ++ show tycon ++ " " ++ show cons ++ ")"
334 | show (MkImpData fc n Nothing _ cons)
335 | = "(%data " ++ show n ++ " " ++ show cons ++ ")"
336 | show (MkImpLater fc n tycon)
337 | = "(%datadecl " ++ show n ++ " " ++ show tycon ++ ")"
341 | IField = IField' Name
344 | IField' : Type -> Type
345 | IField' nm = AddFC $
ImpParameter' (RawImp' nm)
348 | ImpParameter : Type
349 | ImpParameter = ImpParameter' (RawImp' Name)
352 | ImpParameter' : Type -> Type
353 | ImpParameter' nm = WithRig $
WithName $
PiBindData nm
357 | OldParameters' : Type -> Type
358 | OldParameters' nm = (Name, RigCount, PiInfo (RawImp' nm), RawImp' nm)
361 | toOldParams : ImpParameter' (RawImp' nm) -> OldParameters' nm
362 | toOldParams bind = (bind.name.val, bind.rig, bind.val.info, bind.val.boundType)
365 | fromOldParams : OldParameters' nm -> ImpParameter' (RawImp' nm)
366 | fromOldParams (nm, rig, info,type) = Mk [rig, NoFC nm] (MkPiBindData info type)
369 | Show nm => Show (ImpParameter' nm) where
370 | show x = "\{show x.rig}\{show x.name.val} \{show x.val.boundType}"
374 | ImpRecord = AddFC $
ImpRecordData Name
377 | DataHeader : Type -> Type
378 | DataHeader nm = WithName $
List (ImpParameter' (RawImp' nm))
381 | RecordBody : Type -> Type
382 | RecordBody nm = WithName $
WithOpts $
List (IField' nm)
387 | record ImpRecordData (nm : Type) where
388 | constructor MkImpRecord
389 | header : DataHeader nm
390 | body : RecordBody nm
394 | Show nm => Show (IField' nm) where
395 | show f@(MkWithData _ (MkPiBindData Explicit ty)) = show f.name.val ++ " : " ++ show ty
396 | show f@(MkWithData _ ty) = "{" ++ show f.name.val ++ " : " ++ show ty.boundType ++ "}"
400 | Show nm => Show (ImpRecordData nm) where
401 | show (MkImpRecord header body)
402 | = "record " ++ show header.name.val ++ " " ++ show header.val ++
403 | " " ++ show body.name.val ++ "\n\t" ++
404 | showSep "\n\t" (map show body.val) ++ "\n"
412 | Syntactic == Syntactic = True
416 | ImpClause = ImpClause' Name
420 | IImpClause = ImpClause' KindedName
423 | data ImpClause' : Type -> Type where
424 | PatClause : FC -> (lhs : RawImp' nm) -> (rhs : RawImp' nm) -> ImpClause' nm
425 | WithClause : FC -> (lhs : RawImp' nm) ->
426 | (rig : RigCount) -> (wval : RawImp' nm) ->
427 | (prf : Maybe (RigCount, Name)) ->
428 | (flags : List WithFlag) ->
429 | List (ImpClause' nm) -> ImpClause' nm
430 | ImpossibleClause : FC -> (lhs : RawImp' nm) -> ImpClause' nm
432 | %name ImpClause'
cl
436 | Show nm => Show (ImpClause' nm) where
437 | show (PatClause fc lhs rhs)
438 | = show lhs ++ " = " ++ show rhs
439 | show (WithClause fc lhs rig wval prf flags block)
441 | ++ " with " ++ showCount rig ++ "(" ++ show wval ++ ")"
443 | ++ maybe "" (the (_ -> _) $
\(rg, nm) => " proof " ++ showCount rg ++ show nm) prf
444 | ++ "\n\t" ++ show block
445 | show (ImpossibleClause fc lhs)
446 | = show lhs ++ " impossible"
450 | ImpDecl = ImpDecl' Name
453 | record IClaimData (nm : Type) where
454 | constructor MkIClaimData
457 | opts : List (FnOpt' nm)
461 | data ImpDecl' : Type -> Type where
462 | IClaim : WithFC (IClaimData nm) -> ImpDecl' nm
463 | IData : FC -> WithDefault Visibility Private ->
464 | Maybe TotalReq -> ImpData' nm -> ImpDecl' nm
465 | IDef : FC -> Name -> List (ImpClause' nm) -> ImpDecl' nm
466 | IParameters : FC ->
467 | List1 (ImpParameter' (RawImp' nm)) ->
468 | List (ImpDecl' nm) -> ImpDecl' nm
471 | WithDefault Visibility Private ->
473 | AddFC (ImpRecordData nm) -> ImpDecl' nm
474 | IFail : FC -> Maybe String -> List (ImpDecl' nm) -> ImpDecl' nm
475 | INamespace : FC -> Namespace -> List (ImpDecl' nm) -> ImpDecl' nm
476 | ITransform : FC -> Name -> RawImp' nm -> RawImp' nm -> ImpDecl' nm
477 | IRunElabDecl : FC -> RawImp' nm -> ImpDecl' nm
478 | IPragma : FC -> List Name ->
482 | NestedNames vars -> Env Term vars -> Core ()) ->
484 | ILog : Maybe (List String, Nat) -> ImpDecl' nm
485 | IBuiltin : FC -> BuiltinType -> Name -> ImpDecl' nm
487 | %name ImpDecl'
decl
491 | Show nm => Show (ImpDecl' nm) where
492 | show (IClaim (MkWithData _ $
MkIClaimData c _ opts ty))
493 | = show opts ++ " " ++ show c ++ " " ++ show ty
494 | show (IData _ _ _ d) = show d
495 | show (IDef _ n cs) = "(%def " ++ show n ++ " " ++ show cs ++ ")"
496 | show (IParameters _ ps ds)
497 | = "parameters " ++ show ps ++ "\n\t" ++
498 | showSep "\n\t" (assert_total $
map show ds)
499 | show (IRecord _ _ _ _ d) = show d.val
500 | show (IFail _ msg decls)
501 | = "fail" ++ maybe "" ((" " ++) . show) msg ++ "\n" ++
502 | showSep "\n" (assert_total $
map ((" " ++) . show) decls)
503 | show (INamespace _ ns decls)
504 | = "namespace " ++ show ns ++
505 | showSep "\n" (assert_total $
map show decls)
506 | show (ITransform _ n lhs rhs)
507 | = "%transform " ++ show n ++ " " ++ show lhs ++ " ==> " ++ show rhs
508 | show (IRunElabDecl _ tm)
509 | = "%runElab " ++ show tm
510 | show (IPragma {}) = "[externally defined pragma]"
511 | show (ILog Nothing) = "%logging off"
512 | show (ILog (Just (topic, lvl))) = "%logging " ++ case topic of
514 | _ => concat (intersperse "." topic) ++ " " ++ show lvl
515 | show (IBuiltin _ type name) = "%builtin " ++ show type ++ " " ++ show name
519 | mkWithClause : FC -> RawImp' nm -> List1 (RigCount, RawImp' nm, Maybe (RigCount, Name)) ->
520 | List WithFlag -> List (ImpClause' nm) -> ImpClause' nm
521 | mkWithClause fc lhs ((rig, wval, prf) ::: []) flags cls
522 | = WithClause fc lhs rig wval prf flags cls
523 | mkWithClause fc lhs ((rig, wval, prf) ::: wp :: wps) flags cls
524 | = let vfc = virtualiseFC fc
525 | arg = UN $
Basic "arg"
526 | in WithClause fc lhs rig wval prf flags
527 | [mkWithClause fc (IApp vfc lhs $
IBindVar vfc arg) (wp ::: wps) flags cls]
531 | getFieldUpdateTerm : IFieldUpdate' nm -> RawImp' nm
532 | getFieldUpdateTerm (ISetField _ term) = term
533 | getFieldUpdateTerm (ISetFieldApp _ term) = term
537 | getFieldUpdatePath : IFieldUpdate' nm -> List String
538 | getFieldUpdatePath (ISetField path _) = path
539 | getFieldUpdatePath (ISetFieldApp path _) = path
543 | mapFieldUpdateTerm : (RawImp' nm -> RawImp' nm) -> IFieldUpdate' nm -> IFieldUpdate' nm
544 | mapFieldUpdateTerm f (ISetField x term) = ISetField x (f term)
545 | mapFieldUpdateTerm f (ISetFieldApp x term) = ISetFieldApp x (f term)
549 | isIPrimVal : RawImp' nm -> Maybe Constant
550 | isIPrimVal (IPrimVal _ c) = Just c
551 | isIPrimVal _ = Nothing
555 | data ImpREPL : Type where
556 | Eval : RawImp -> ImpREPL
557 | Check : RawImp -> ImpREPL
558 | ProofSearch : Name -> ImpREPL
559 | ExprSearch : Name -> ImpREPL
560 | GenerateDef : Int -> Name -> ImpREPL
561 | Missing : Name -> ImpREPL
562 | CheckTotal : Name -> ImpREPL
563 | DebugInfo : Name -> ImpREPL
567 | mapAltType : (RawImp' nm -> RawImp' nm) -> AltType' nm -> AltType' nm
568 | mapAltType f (UniqueDefault x) = UniqueDefault (f x)
572 | lhsInCurrentNS : {auto c : Ref Ctxt Defs} ->
573 | NestedNames vars -> RawImp -> Core RawImp
574 | lhsInCurrentNS nest (IApp loc f a)
575 | = do f' <- lhsInCurrentNS nest f
576 | pure (IApp loc f' a)
577 | lhsInCurrentNS nest (IAutoApp loc f a)
578 | = do f' <- lhsInCurrentNS nest f
579 | pure (IAutoApp loc f' a)
580 | lhsInCurrentNS nest (INamedApp loc f n a)
581 | = do f' <- lhsInCurrentNS nest f
582 | pure (INamedApp loc f' n a)
583 | lhsInCurrentNS nest (IWithApp loc f a)
584 | = do f' <- lhsInCurrentNS nest f
585 | pure (IWithApp loc f' a)
586 | lhsInCurrentNS nest tm@(IVar loc (NS {})) = pure tm
587 | lhsInCurrentNS nest (IVar loc n)
588 | = case lookup n (names nest) of
590 | do n' <- inCurrentNS n
595 | Just _ => pure (IVar loc n)
596 | lhsInCurrentNS nest tm = pure tm
599 | findIBinds : RawImp' nm -> List String
600 | findIBinds (IPi fc rig p mn aty retty)
601 | = findIBinds aty ++ findIBinds retty
602 | findIBinds (ILam fc rig p n aty sc)
603 | = findIBinds aty ++ findIBinds sc
604 | findIBinds (IApp fc fn av)
605 | = findIBinds fn ++ findIBinds av
606 | findIBinds (IAutoApp fc fn av)
607 | = findIBinds fn ++ findIBinds av
608 | findIBinds (INamedApp _ fn _ av)
609 | = findIBinds fn ++ findIBinds av
610 | findIBinds (IWithApp fc fn av)
611 | = findIBinds fn ++ findIBinds av
612 | findIBinds (IAs fc _ _ (UN (Basic n)) pat)
613 | = n :: findIBinds pat
614 | findIBinds (IAs fc _ _ n pat)
616 | findIBinds (IMustUnify fc r pat)
618 | findIBinds (IAlternative fc u alts)
619 | = concatMap findIBinds alts
620 | findIBinds (IDelayed fc _ ty) = findIBinds ty
621 | findIBinds (IDelay fc tm) = findIBinds tm
622 | findIBinds (IForce fc tm) = findIBinds tm
623 | findIBinds (IQuote fc tm) = findIBinds tm
624 | findIBinds (IUnquote fc tm) = findIBinds tm
625 | findIBinds (IRunElab fc _ tm) = findIBinds tm
626 | findIBinds (IBindHere _ _ tm) = findIBinds tm
627 | findIBinds (IBindVar _ (UN (Basic n))) = [n]
628 | findIBinds (IUpdate fc updates tm)
629 | = findIBinds tm ++ concatMap (findIBinds . getFieldUpdateTerm) updates
635 | findImplicits : RawImp' nm -> List String
636 | findImplicits (IPi fc rig p (Just (UN (Basic mn))) aty retty)
637 | = mn :: findImplicits aty ++ findImplicits retty
638 | findImplicits (IPi fc rig p mn aty retty)
639 | = findImplicits aty ++ findImplicits retty
640 | findImplicits (ILam fc rig p n aty sc)
641 | = findImplicits aty ++ findImplicits sc
642 | findImplicits (IApp fc fn av)
643 | = findImplicits fn ++ findImplicits av
644 | findImplicits (IAutoApp _ fn av)
645 | = findImplicits fn ++ findImplicits av
646 | findImplicits (INamedApp _ fn _ av)
647 | = findImplicits fn ++ findImplicits av
648 | findImplicits (IWithApp fc fn av)
649 | = findImplicits fn ++ findImplicits av
650 | findImplicits (IAs fc _ _ n pat)
651 | = findImplicits pat
652 | findImplicits (IMustUnify fc r pat)
653 | = findImplicits pat
654 | findImplicits (IAlternative fc u alts)
655 | = concatMap findImplicits alts
656 | findImplicits (IDelayed fc _ ty) = findImplicits ty
657 | findImplicits (IDelay fc tm) = findImplicits tm
658 | findImplicits (IForce fc tm) = findImplicits tm
659 | findImplicits (IQuote fc tm) = findImplicits tm
660 | findImplicits (IUnquote fc tm) = findImplicits tm
661 | findImplicits (IRunElab fc _ tm) = findImplicits tm
662 | findImplicits (IBindVar _ (UN (Basic n))) = [n]
663 | findImplicits (IUpdate fc updates tm)
664 | = findImplicits tm ++ concatMap (findImplicits . getFieldUpdateTerm) updates
665 | findImplicits tm = []
672 | implicitsAs : {auto c : Ref Ctxt Defs} ->
674 | (vars : List Name) ->
675 | RawImp -> Core RawImp
676 | implicitsAs n defs ns tm
677 | = do let implicits = findIBinds tm
678 | log "declare.def.lhs.implicits" 30 $
"Found implicits: " ++ show implicits
679 | setAs (map Just (ns ++ map (UN . Basic) implicits)) [] tm
686 | setAs : List (Maybe Name) -> List (Maybe Name) -> RawImp -> Core RawImp
687 | setAs is es (IApp loc f a)
688 | = do f' <- setAs is (Nothing :: es) f
689 | pure $
IApp loc f' a
690 | setAs is es (IAutoApp loc f a)
691 | = do f' <- setAs (Nothing :: is) es f
692 | pure $
IAutoApp loc f' a
693 | setAs is es (INamedApp loc f n a)
694 | = do f' <- setAs (Just n :: is) (Just n :: es) f
695 | pure $
INamedApp loc f' n a
696 | setAs is es (IWithApp loc f a)
697 | = do f' <- setAs is es f
698 | pure $
IWithApp loc f' a
699 | setAs is es (IVar loc nm)
701 | = case !(lookupTyExact (Resolved n) (gamma defs)) of
703 | do log "declare.def.lhs.implicits" 30 $
704 | "Could not find variable " ++ show n
707 | do ty' <- nf defs Env.empty ty
708 | implicits <- findImps is es ns ty'
709 | log "declare.def.lhs.implicits" 30 $
710 | "\n In the type of " ++ show n ++ ": " ++ show ty ++
711 | "\n Using locals: " ++ show ns ++
712 | "\n Found implicits: " ++ show implicits
713 | pure $
impAs (virtualiseFC loc) implicits (IVar loc nm)
718 | updateNs : Name -> List (Maybe Name) -> Maybe (List (Maybe Name))
719 | updateNs n (Nothing :: ns) = Just ns
720 | updateNs n (x :: ns)
723 | else do ns' <- updateNs n ns
725 | updateNs n [] = Nothing
735 | findImps : List (Maybe Name) -> List (Maybe Name) ->
736 | List Name -> ClosedNF ->
737 | Core (List (Name, PiInfo RawImp))
743 | findImps ns es (_ :: locals) (NBind fc x (Pi {}) sc)
744 | = do body <- sc defs (toClosure defaultOpts Env.empty (Erased fc Placeholder))
745 | findImps ns es locals body
748 | findImps ns es [] (NBind fc x (Pi _ _ Explicit _) sc)
749 | = do body <- sc defs (toClosure defaultOpts Env.empty (Erased fc Placeholder))
752 | Just (UN Underscore) :: _ => findImps ns es [] body
754 | _ => case updateNs x es of
756 | Just es' => findImps ns es' [] body
758 | findImps ns es [] (NBind fc x (Pi _ _ AutoImplicit _) sc)
759 | = do body <- sc defs (toClosure defaultOpts Env.empty (Erased fc Placeholder))
760 | case updateNs x ns of
762 | pure $
(x, AutoImplicit) :: !(findImps ns es [] body)
763 | Just ns' => findImps ns' es [] body
764 | findImps ns es [] (NBind fc x (Pi _ _ p _) sc)
765 | = do body <- sc defs (toClosure defaultOpts Env.empty (Erased fc Placeholder))
766 | if Just x `elem` ns
767 | then findImps ns es [] body
768 | else pure $
(x, forgetDef p) :: !(findImps ns es [] body)
769 | findImps _ _ locals _
770 | = do log "declare.def.lhs.implicits" 50 $
771 | "Giving up with the following locals left: " ++ show locals
774 | impAs : FC -> List (Name, PiInfo RawImp) -> RawImp -> RawImp
775 | impAs loc' [] tm = tm
776 | impAs loc' ((nm@(UN (Basic _)), AutoImplicit) :: ns) tm
778 | INamedApp loc' tm nm (IBindVar loc' nm)
780 | impAs loc' ((n, Implicit) :: ns) tm
782 | INamedApp loc' tm n
783 | (IAs loc' EmptyFC UseLeft n (Implicit loc' True))
785 | impAs loc' ((n, DefImplicit t) :: ns) tm
787 | INamedApp loc' tm n
788 | (IAs loc' EmptyFC UseLeft n (Implicit loc' True))
790 | impAs loc' (_ :: ns) tm = impAs loc' ns tm
791 | setAs is es tm = pure tm
796 | definedInBlock : Namespace ->
797 | List ImpDecl -> List Name
798 | definedInBlock ns decls =
799 | Prelude.toList $
foldl (defName ns) empty decls
801 | getName : ImpTy -> Name
802 | getName = (.tyName.val)
804 | getFieldName : IField -> Name
805 | getFieldName f = f.name.val
807 | expandNS : Namespace -> Name -> Name
809 | = if ns == emptyNS then n else case n of
815 | defName : Namespace -> SortedSet Name -> ImpDecl -> SortedSet Name
816 | defName ns acc (IClaim c) = insert (expandNS ns (getName c.val.type)) acc
817 | defName ns acc (IDef _ nm _) = insert (expandNS ns nm) acc
818 | defName ns acc (IData _ _ _ (MkImpData _ n _ _ cons))
819 | = foldl (flip insert) acc $
expandNS ns n :: map (expandNS ns . getName) cons
820 | defName ns acc (IData _ _ _ (MkImpLater _ n _)) = insert (expandNS ns n) acc
821 | defName ns acc (IParameters _ _ pds) = foldl (defName ns) acc pds
822 | defName ns acc (IFail _ _ nds) = foldl (defName ns) acc nds
823 | defName ns acc (INamespace _ n nds) = foldl (defName (ns <.> n)) acc nds
824 | defName ns acc (IRecord _ fldns _ _ rec)
825 | = foldl (flip insert) acc $
expandNS ns rec.val.body.name.val :: all
828 | fldns' = maybe ns (\ f => ns <.> mkNamespace f) fldns
830 | toRF : Name -> Name
831 | toRF (UN (Basic n)) = UN (Field n)
835 | fnsUN = map getFieldName rec.val.body.val
838 | fnsRF = map toRF fnsUN
848 | all = expandNS ns rec.val.header.name.val :: map (expandNS fldns') (fnsRF ++ fnsUN)
850 | defName ns acc (IPragma _ pns _) = foldl (flip insert) acc $
map (expandNS ns) pns
851 | defName _ acc _ = acc
854 | isIVar : RawImp' nm -> Maybe (FC, nm)
855 | isIVar (IVar fc v) = Just (fc, v)
859 | isIBindVar : RawImp' nm -> Maybe (FC, Name)
860 | isIBindVar (IBindVar fc v) = Just (fc, v)
861 | isIBindVar _ = Nothing
864 | getFC : RawImp' nm -> FC
865 | getFC (IVar x _) = x
866 | getFC (IPi x _ _ _ _ _) = x
867 | getFC (ILam x _ _ _ _ _) = x
868 | getFC (ILet x _ _ _ _ _ _) = x
869 | getFC (ICase x _ _ _ _) = x
870 | getFC (ILocal x _ _) = x
871 | getFC (ICaseLocal x _ _ _ _) = x
872 | getFC (IUpdate x _ _) = x
873 | getFC (IApp x _ _) = x
874 | getFC (INamedApp x _ _ _) = x
875 | getFC (IAutoApp x _ _) = x
876 | getFC (IWithApp x _ _) = x
877 | getFC (ISearch x _) = x
878 | getFC (IAlternative x _ _) = x
879 | getFC (IRewrite x _ _) = x
880 | getFC (ICoerced x _) = x
881 | getFC (IPrimVal x _) = x
882 | getFC (IHole x _) = x
883 | getFC (IUnifyLog x _ _) = x
884 | getFC (IType x) = x
885 | getFC (IBindVar x _) = x
886 | getFC (IBindHere x _ _) = x
887 | getFC (IMustUnify x _ _) = x
888 | getFC (IDelayed x _ _) = x
889 | getFC (IDelay x _) = x
890 | getFC (IForce x _) = x
891 | getFC (IQuote x _) = x
892 | getFC (IQuoteName x _) = x
893 | getFC (IQuoteDecl x _) = x
894 | getFC (IUnquote x _) = x
895 | getFC (IRunElab x _ _) = x
896 | getFC (IAs x _ _ _ _) = x
897 | getFC (Implicit x _) = x
898 | getFC (IWithUnambigNames x _ _) = x
903 | getFC : ImpDecl' nm -> FC
904 | getFC (IClaim c) = c.fc
905 | getFC (IData fc _ _ _) = fc
906 | getFC (IDef fc _ _) = fc
907 | getFC (IParameters fc _ _) = fc
908 | getFC (IRecord fc _ _ _ _) = fc
909 | getFC (IFail fc _ _) = fc
910 | getFC (INamespace fc _ _) = fc
911 | getFC (ITransform fc _ _ _) = fc
912 | getFC (IRunElabDecl fc _) = fc
913 | getFC (IPragma fc _ _) = fc
914 | getFC (ILog _) = EmptyFC
915 | getFC (IBuiltin fc _ _) = fc
919 | = Explicit FC (RawImp' nm)
920 | | Auto FC (RawImp' nm)
921 | | Named FC Name (RawImp' nm)
930 | IArg = Arg' KindedName
933 | isExplicit : Arg' nm -> Maybe (FC, RawImp' nm)
934 | isExplicit (Explicit fc t) = Just (fc, t)
935 | isExplicit _ = Nothing
938 | unIArg : Arg' nm -> RawImp' nm
939 | unIArg (Explicit _ t) = t
940 | unIArg (Auto _ t) = t
941 | unIArg (Named _ _ t) = t
945 | Show nm => Show (Arg' nm) where
946 | show (Explicit fc t) = show t
947 | show (Auto fc t) = "@{" ++ show t ++ "}"
948 | show (Named fc n t) = "{" ++ show n ++ " = " ++ show t ++ "}"
951 | getFnArgs : RawImp' nm -> List (Arg' nm) -> (RawImp' nm, List (Arg' nm))
952 | getFnArgs (IApp fc f arg) args = getFnArgs f (Explicit fc arg :: args)
953 | getFnArgs (INamedApp fc f n arg) args = getFnArgs f (Named fc n arg :: args)
954 | getFnArgs (IAutoApp fc f arg) args = getFnArgs f (Auto fc arg :: args)
955 | getFnArgs tm args = (tm, args)
960 | apply : RawImp' nm -> List (Arg' nm) -> RawImp' nm
961 | apply f (Explicit fc a :: args) = apply (IApp fc f a) args
962 | apply f (Auto fc a :: args) = apply (IAutoApp fc f a) args
963 | apply f (Named fc n a :: args) = apply (INamedApp fc f n a) args
967 | apply : RawImp' nm -> List (RawImp' nm) -> RawImp' nm
969 | apply f (x :: xs) =
970 | let fFC = getFC f in
971 | apply (IApp (fromMaybe fFC (mergeFC fFC (getFC x))) f x) xs
974 | gapply : RawImp' nm -> List (Maybe Name, RawImp' nm) -> RawImp' nm
976 | gapply f (x :: xs) = gapply (uncurry (app f) x) xs where
978 | app : RawImp' nm -> Maybe Name -> RawImp' nm -> RawImp' nm
979 | app f Nothing x = IApp (getFC f) f x
980 | app f (Just nm) x = INamedApp (getFC f) f nm x
984 | getFn : RawImp' nm -> RawImp' nm
985 | getFn (IApp _ f _) = getFn f
986 | getFn (IWithApp _ f _) = getFn f
987 | getFn (INamedApp _ f _ _) = getFn f
988 | getFn (IAutoApp _ f _) = getFn f
989 | getFn (IAs _ _ _ _ f) = getFn f
990 | getFn (IMustUnify _ _ f) = getFn f
995 | logRaw : {auto c : Ref Ctxt Defs} ->
996 | LogTopic -> Nat -> Lazy String -> RawImp -> Core ()
997 | logRaw s n msg tm = log s n $
msg ++ ": " ++ show tm