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)))
37 | thinNestedNames : Thinnable NestedNames
38 | thinNestedNames (MkNested ns) sub
39 | = MkNested $
ns <&> \(n, nm, vars, rep) =>
40 | (n, nm, flip thin sub <$> vars, flip thin sub .: rep)
44 | mapNestedName : NestedNames vars -> Name -> Name
45 | mapNestedName nest n = case lookup n (names nest) of
46 | (Just (Just n', _)) => n'
55 | data BindMode = PI RigCount | PATTERN | COVERAGE | NONE
63 | RawImp = RawImp' Name
67 | IRawImp = RawImp' KindedName
70 | data RawImp' : Type -> Type where
71 | IVar : FC -> nm -> RawImp' nm
72 | IPi : FC -> RigCount -> PiInfo (RawImp' nm) -> Maybe Name ->
73 | (argTy : RawImp' nm) -> (retTy : RawImp' nm) -> RawImp' nm
74 | ILam : FC -> RigCount -> PiInfo (RawImp' nm) -> Maybe Name ->
75 | (argTy : RawImp' nm) -> (lamTy : RawImp' nm) -> RawImp' nm
76 | ILet : FC -> (lhsFC : FC) -> RigCount -> Name ->
77 | (nTy : RawImp' nm) -> (nVal : RawImp' nm) ->
78 | (scope : RawImp' nm) -> RawImp' nm
79 | ICase : FC -> List (FnOpt' nm) -> RawImp' nm -> (ty : RawImp' nm) ->
80 | List (ImpClause' nm) -> RawImp' nm
81 | ILocal : FC -> List (ImpDecl' nm) -> RawImp' nm -> RawImp' nm
86 | ICaseLocal : FC -> (uname : Name) ->
87 | (internalName : Name) ->
88 | (args : List Name) -> RawImp' nm -> RawImp' nm
90 | IUpdate : FC -> List (IFieldUpdate' nm) -> RawImp' nm -> RawImp' nm
92 | IApp : FC -> RawImp' nm -> RawImp' nm -> RawImp' nm
93 | IAutoApp : FC -> RawImp' nm -> RawImp' nm -> RawImp' nm
94 | INamedApp : FC -> RawImp' nm -> Name -> RawImp' nm -> RawImp' nm
95 | IWithApp : FC -> RawImp' nm -> RawImp' nm -> RawImp' nm
97 | ISearch : FC -> (depth : Nat) -> RawImp' nm
98 | IAlternative : FC -> AltType' nm -> List (RawImp' nm) -> RawImp' nm
99 | IRewrite : FC -> RawImp' nm -> RawImp' nm -> RawImp' nm
100 | ICoerced : FC -> RawImp' nm -> RawImp' nm
104 | IBindHere : FC -> BindMode -> RawImp' nm -> RawImp' nm
106 | IBindVar : FC -> Name -> RawImp' nm
108 | IAs : FC -> (nameFC : FC) -> UseSide -> Name -> RawImp' nm -> RawImp' nm
111 | IMustUnify : FC -> DotReason -> RawImp' nm -> RawImp' nm
114 | IDelayed : FC -> LazyReason -> RawImp' nm -> RawImp' nm
115 | IDelay : FC -> RawImp' nm -> RawImp' nm
116 | IForce : FC -> RawImp' nm -> RawImp' nm
119 | IQuote : FC -> RawImp' nm -> RawImp' nm
120 | IQuoteName : FC -> Name -> RawImp' nm
121 | IQuoteDecl : FC -> List (ImpDecl' nm) -> RawImp' nm
122 | IUnquote : FC -> RawImp' nm -> RawImp' nm
123 | IRunElab : FC -> (requireExtension : Bool) -> RawImp' nm -> RawImp' nm
125 | IPrimVal : FC -> (c : Constant) -> RawImp' nm
126 | IType : FC -> RawImp' nm
127 | IHole : FC -> String -> RawImp' nm
129 | IUnifyLog : FC -> LogLevel -> RawImp' nm -> RawImp' nm
133 | Implicit : FC -> (bindIfUnsolved : Bool) -> RawImp' nm
136 | IWithUnambigNames : FC -> List (FC, Name) -> RawImp' nm -> RawImp' nm
141 | IFieldUpdate : Type
142 | IFieldUpdate = IFieldUpdate' Name
145 | data IFieldUpdate' : Type -> Type where
146 | ISetField : (path : List String) -> RawImp' nm -> IFieldUpdate' nm
147 | ISetFieldApp : (path : List String) -> RawImp' nm -> IFieldUpdate' nm
148 | %name IFieldUpdate'
upd
152 | AltType = AltType' Name
155 | data AltType' : Type -> Type where
156 | FirstSuccess : AltType' nm
157 | Unique : AltType' nm
158 | UniqueDefault : RawImp' nm -> AltType' nm
163 | Show nm => Show (RawImp' nm) where
164 | show (IVar fc n) = show n
165 | show (IPi fc c p n arg ret)
166 | = "(%pi " ++ show c ++ " " ++ show p ++ " " ++
167 | showPrec App n ++ " " ++ show arg ++ " " ++ show ret ++ ")"
168 | show (ILam fc c p n arg sc)
169 | = "(%lam " ++ show c ++ " " ++ show p ++ " " ++
170 | showPrec App n ++ " " ++ show arg ++ " " ++ show sc ++ ")"
171 | show (ILet fc lhsFC c n ty val sc)
172 | = "(%let " ++ show c ++ " " ++ " " ++ show n ++ " " ++ show ty ++
173 | " " ++ show val ++ " " ++ show sc ++ ")"
174 | show (ICase _ _ scr scrty alts)
175 | = "(%case (" ++ show scr ++ " : " ++ show scrty ++ ") " ++ show alts ++ ")"
176 | show (ILocal _ def scope)
177 | = "(%local (" ++ show def ++ ") " ++ show scope ++ ")"
178 | show (ICaseLocal _ uname iname args sc)
179 | = "(%caselocal (" ++ show uname ++ " " ++ show iname
180 | ++ " " ++ show args ++ ") " ++ show sc ++ ")"
181 | show (IUpdate _ flds rec)
182 | = "(%record " ++ showSep ", " (map show flds) ++ " " ++ show rec ++ ")"
184 | = "(" ++ show f ++ " " ++ show a ++ ")"
185 | show (INamedApp fc f n a)
186 | = "(" ++ show f ++ " [" ++ show n ++ " = " ++ show a ++ "])"
187 | show (IAutoApp fc f a)
188 | = "(" ++ show f ++ " [" ++ show a ++ "])"
189 | show (IWithApp fc f a)
190 | = "(" ++ show f ++ " | " ++ show a ++ ")"
191 | show (ISearch fc d)
193 | show (IAlternative fc ty alts)
194 | = "(|" ++ showSep "," (map show alts) ++ "|)"
195 | show (IRewrite _ rule tm)
196 | = "(%rewrite (" ++ show rule ++ ") (" ++ show tm ++ "))"
197 | show (ICoerced _ tm) = "(%coerced " ++ show tm ++ ")"
199 | show (IBindHere fc b sc)
200 | = "(%bindhere " ++ show sc ++ ")"
201 | show (IBindVar fc n) = "$" ++ show n
202 | show (IAs fc _ _ n tm) = show n ++ "@(" ++ show tm ++ ")"
203 | show (IMustUnify fc r tm) = ".(" ++ show tm ++ ")"
204 | show (IDelayed fc r tm) = "(%delayed " ++ show tm ++ ")"
205 | show (IDelay fc tm) = "(%delay " ++ show tm ++ ")"
206 | show (IForce fc tm) = "(%force " ++ show tm ++ ")"
207 | show (IQuote fc tm) = "(%quote " ++ show tm ++ ")"
208 | show (IQuoteName fc tm) = "(%quotename " ++ show tm ++ ")"
209 | show (IQuoteDecl fc tm) = "(%quotedecl " ++ show tm ++ ")"
210 | show (IUnquote fc tm) = "(%unquote " ++ show tm ++ ")"
211 | show (IRunElab fc _ tm) = "(%runelab " ++ show tm ++ ")"
212 | show (IPrimVal fc c) = show c
213 | show (IHole _ x) = "?" ++ x
214 | show (IUnifyLog _ lvl x) = "(%logging " ++ show lvl ++ " " ++ show x ++ ")"
215 | show (IType fc) = "%type"
216 | show (Implicit fc True) = "_"
217 | show (Implicit fc False) = "?"
218 | show (IWithUnambigNames fc ns rhs) = "(%with " ++ show ns ++ " " ++ show rhs ++ ")"
222 | Show nm => Show (IFieldUpdate' nm) where
223 | show (ISetField p val) = showSep "->" p ++ " = " ++ show val
224 | show (ISetFieldApp p val) = showSep "->" p ++ " $= " ++ show val
228 | FnOpt = FnOpt' Name
231 | data FnOpt' : Type -> Type where
234 | NoInline : FnOpt' nm
236 | Deprecate : FnOpt' nm
237 | TCInline : FnOpt' nm
240 | Hint : Bool -> FnOpt' nm
242 | GlobalHint : Bool -> FnOpt' nm
243 | ExternFn : FnOpt' nm
245 | ForeignFn : List (RawImp' nm) -> FnOpt' nm
247 | ForeignExport : List (RawImp' nm) -> FnOpt' nm
249 | Invertible : FnOpt' nm
250 | Totality : TotalReq -> FnOpt' nm
252 | SpecArgs : List Name -> FnOpt' nm
256 | isTotalityReq : FnOpt' nm -> Bool
257 | isTotalityReq (Totality _) = True
258 | isTotalityReq _ = False
261 | extractTotality : FnOpt' nm -> Maybe TotalReq
262 | extractTotality (Totality t) = Just t
263 | extractTotality _ = Nothing
266 | findTotality : List (FnOpt' nm) -> Maybe TotalReq
267 | findTotality = foldr (\elem, acc => extractTotality elem <|> acc) empty
271 | Show nm => Show (FnOpt' nm) where
272 | show Unsafe = "%unsafe"
273 | show Inline = "%inline"
274 | show NoInline = "%noinline"
275 | show Deprecate = "%deprecate"
276 | show TCInline = "%tcinline"
277 | show (Hint t) = "%hint " ++ show t
278 | show (GlobalHint t) = "%globalhint " ++ show t
279 | show ExternFn = "%extern"
280 | show (ForeignFn cs) = "%foreign " ++ showSep " " (map show cs)
281 | show (ForeignExport cs) = "%export " ++ showSep " " (map show cs)
282 | show Invertible = "%invertible"
283 | show (Totality Total) = "total"
284 | show (Totality CoveringOnly) = "covering"
285 | show (Totality PartialOK) = "partial"
286 | show Macro = "%macro"
287 | show (SpecArgs ns) = "%spec " ++ showSep " " (map show ns)
291 | Inline == Inline = True
292 | NoInline == NoInline = True
293 | Deprecate == Deprecate = True
294 | TCInline == TCInline = True
295 | (Hint x) == (Hint y) = x == y
296 | (GlobalHint x) == (GlobalHint y) = x == y
297 | ExternFn == ExternFn = True
298 | (ForeignFn xs) == (ForeignFn ys) = True
299 | (ForeignExport xs) == (ForeignExport ys) = True
300 | Invertible == Invertible = True
301 | (Totality tot_lhs) == (Totality tot_rhs) = tot_lhs == tot_rhs
302 | Macro == Macro = True
303 | (SpecArgs ns) == (SpecArgs ns') = ns == ns'
308 | ImpTy = ImpTy' Name
311 | ImpTy' : Type -> Type
312 | ImpTy' = AddMetadata FC' . AddMetadata TyName' . RawImp'
316 | Show nm => Show (ImpTy' nm) where
317 | show ty = "(%claim " ++ show ty.tyName.val ++ " " ++ show ty.val ++ ")"
321 | ImpData = ImpData' Name
324 | data ImpData' : Type -> Type where
325 | MkImpData : FC -> (n : Name) ->
328 | (tycon : Maybe (RawImp' nm)) ->
329 | (opts : List DataOpt) ->
330 | (datacons : List (ImpTy' nm)) -> ImpData' nm
331 | MkImpLater : FC -> (n : Name) -> (tycon : RawImp' nm) -> ImpData' nm
337 | Show nm => Show (ImpData' nm) where
338 | show (MkImpData fc n (Just tycon) _ cons)
339 | = "(%data " ++ show n ++ " " ++ show tycon ++ " " ++ show cons ++ ")"
340 | show (MkImpData fc n Nothing _ cons)
341 | = "(%data " ++ show n ++ " " ++ show cons ++ ")"
342 | show (MkImpLater fc n tycon)
343 | = "(%datadecl " ++ show n ++ " " ++ show tycon ++ ")"
347 | IField = IField' Name
350 | IField' : Type -> Type
351 | IField' nm = AddFC $
ImpParameter' (RawImp' nm)
354 | ImpParameter : Type
355 | ImpParameter = ImpParameter' (RawImp' Name)
358 | ImpParameter' : Type -> Type
359 | ImpParameter' nm = WithRig $
WithName $
PiBindData nm
363 | OldParameters' : Type -> Type
364 | OldParameters' nm = (Name, RigCount, PiInfo (RawImp' nm), RawImp' nm)
367 | toOldParams : ImpParameter' (RawImp' nm) -> OldParameters' nm
368 | toOldParams bind = (bind.name.val, bind.rig, bind.val.info, bind.val.boundType)
371 | fromOldParams : OldParameters' nm -> ImpParameter' (RawImp' nm)
372 | fromOldParams (nm, rig, info,type) = Mk [rig, NoFC nm] (MkPiBindData info type)
375 | Show nm => Show (ImpParameter' nm) where
376 | show x = "\{show x.rig}\{show x.name.val} \{show x.val.boundType}"
380 | ImpRecord = AddFC $
ImpRecordData Name
383 | DataHeader : Type -> Type
384 | DataHeader nm = WithName $
List (ImpParameter' (RawImp' nm))
387 | RecordBody : Type -> Type
388 | RecordBody nm = WithName $
WithOpts $
List (IField' nm)
393 | record ImpRecordData (nm : Type) where
394 | constructor MkImpRecord
395 | header : DataHeader nm
396 | body : RecordBody nm
400 | Show nm => Show (IField' nm) where
401 | show f@(MkWithData _ (MkPiBindData Explicit ty)) = show f.name.val ++ " : " ++ show ty
402 | show f@(MkWithData _ ty) = "{" ++ show f.name.val ++ " : " ++ show ty.boundType ++ "}"
406 | Show nm => Show (ImpRecordData nm) where
407 | show (MkImpRecord header body)
408 | = "record " ++ show header.name.val ++ " " ++ show header.val ++
409 | " " ++ show body.name.val ++ "\n\t" ++
410 | showSep "\n\t" (map show body.val) ++ "\n"
418 | Syntactic == Syntactic = True
422 | ImpClause = ImpClause' Name
426 | IImpClause = ImpClause' KindedName
429 | data ImpClause' : Type -> Type where
430 | PatClause : FC -> (lhs : RawImp' nm) -> (rhs : RawImp' nm) -> ImpClause' nm
431 | WithClause : FC -> (lhs : RawImp' nm) ->
432 | (rig : RigCount) -> (wval : RawImp' nm) ->
433 | (prf : Maybe (RigCount, Name)) ->
434 | (flags : List WithFlag) ->
435 | List (ImpClause' nm) -> ImpClause' nm
436 | ImpossibleClause : FC -> (lhs : RawImp' nm) -> ImpClause' nm
438 | %name ImpClause'
cl
442 | Show nm => Show (ImpClause' nm) where
443 | show (PatClause fc lhs rhs)
444 | = show lhs ++ " = " ++ show rhs
445 | show (WithClause fc lhs rig wval prf flags block)
447 | ++ " with " ++ showCount rig ++ "(" ++ show wval ++ ")"
449 | ++ maybe "" (the (_ -> _) $
\(rg, nm) => " proof " ++ showCount rg ++ show nm) prf
450 | ++ "\n\t" ++ show block
451 | show (ImpossibleClause fc lhs)
452 | = show lhs ++ " impossible"
456 | ImpDecl = ImpDecl' Name
459 | record IClaimData (nm : Type) where
460 | constructor MkIClaimData
463 | opts : List (FnOpt' nm)
467 | data ImpDecl' : Type -> Type where
468 | IClaim : WithFC (IClaimData nm) -> ImpDecl' nm
469 | IData : FC -> WithDefault Visibility Private ->
470 | Maybe TotalReq -> ImpData' nm -> ImpDecl' nm
471 | IDef : FC -> Name -> List (ImpClause' nm) -> ImpDecl' nm
472 | IParameters : FC ->
473 | List1 (ImpParameter' (RawImp' nm)) ->
474 | List (ImpDecl' nm) -> ImpDecl' nm
477 | WithDefault Visibility Private ->
479 | AddFC (ImpRecordData nm) -> ImpDecl' nm
480 | IFail : FC -> Maybe String -> List (ImpDecl' nm) -> ImpDecl' nm
481 | INamespace : FC -> Namespace -> List (ImpDecl' nm) -> ImpDecl' nm
482 | ITransform : FC -> Name -> RawImp' nm -> RawImp' nm -> ImpDecl' nm
483 | IRunElabDecl : FC -> RawImp' nm -> ImpDecl' nm
484 | IPragma : FC -> List Name ->
488 | NestedNames vars -> Env Term vars -> Core ()) ->
490 | ILog : Maybe (List String, Nat) -> ImpDecl' nm
491 | IBuiltin : FC -> BuiltinType -> Name -> ImpDecl' nm
493 | %name ImpDecl'
decl
497 | Show nm => Show (ImpDecl' nm) where
498 | show (IClaim (MkWithData _ $
MkIClaimData c _ opts ty))
499 | = show opts ++ " " ++ show c ++ " " ++ show ty
500 | show (IData _ _ _ d) = show d
501 | show (IDef _ n cs) = "(%def " ++ show n ++ " " ++ show cs ++ ")"
502 | show (IParameters _ ps ds)
503 | = "parameters " ++ show ps ++ "\n\t" ++
504 | showSep "\n\t" (assert_total $
map show ds)
505 | show (IRecord _ _ _ _ d) = show d.val
506 | show (IFail _ msg decls)
507 | = "fail" ++ maybe "" ((" " ++) . show) msg ++ "\n" ++
508 | showSep "\n" (assert_total $
map ((" " ++) . show) decls)
509 | show (INamespace _ ns decls)
510 | = "namespace " ++ show ns ++
511 | showSep "\n" (assert_total $
map show decls)
512 | show (ITransform _ n lhs rhs)
513 | = "%transform " ++ show n ++ " " ++ show lhs ++ " ==> " ++ show rhs
514 | show (IRunElabDecl _ tm)
515 | = "%runElab " ++ show tm
516 | show (IPragma {}) = "[externally defined pragma]"
517 | show (ILog Nothing) = "%logging off"
518 | show (ILog (Just (topic, lvl))) = "%logging " ++ case topic of
520 | _ => concat (intersperse "." topic) ++ " " ++ show lvl
521 | show (IBuiltin _ type name) = "%builtin " ++ show type ++ " " ++ show name
525 | mkWithClause : FC -> RawImp' nm -> List1 (RigCount, RawImp' nm, Maybe (RigCount, Name)) ->
526 | List WithFlag -> List (ImpClause' nm) -> ImpClause' nm
527 | mkWithClause fc lhs ((rig, wval, prf) ::: []) flags cls
528 | = WithClause fc lhs rig wval prf flags cls
529 | mkWithClause fc lhs ((rig, wval, prf) ::: wp :: wps) flags cls
530 | = let vfc = virtualiseFC fc
531 | arg = UN $
Basic "arg"
532 | in WithClause fc lhs rig wval prf flags
533 | [mkWithClause fc (IApp vfc lhs $
IBindVar vfc arg) (wp ::: wps) flags cls]
537 | getFieldUpdateTerm : IFieldUpdate' nm -> RawImp' nm
538 | getFieldUpdateTerm (ISetField _ term) = term
539 | getFieldUpdateTerm (ISetFieldApp _ term) = term
543 | getFieldUpdatePath : IFieldUpdate' nm -> List String
544 | getFieldUpdatePath (ISetField path _) = path
545 | getFieldUpdatePath (ISetFieldApp path _) = path
549 | mapFieldUpdateTerm : (RawImp' nm -> RawImp' nm) -> IFieldUpdate' nm -> IFieldUpdate' nm
550 | mapFieldUpdateTerm f (ISetField x term) = ISetField x (f term)
551 | mapFieldUpdateTerm f (ISetFieldApp x term) = ISetFieldApp x (f term)
555 | isIPrimVal : RawImp' nm -> Maybe Constant
556 | isIPrimVal (IPrimVal _ c) = Just c
557 | isIPrimVal _ = Nothing
561 | data ImpREPL : Type where
562 | Eval : RawImp -> ImpREPL
563 | Check : RawImp -> ImpREPL
564 | ProofSearch : Name -> ImpREPL
565 | ExprSearch : Name -> ImpREPL
566 | GenerateDef : Int -> Name -> ImpREPL
567 | Missing : Name -> ImpREPL
568 | CheckTotal : Name -> ImpREPL
569 | DebugInfo : Name -> ImpREPL
573 | mapAltType : (RawImp' nm -> RawImp' nm) -> AltType' nm -> AltType' nm
574 | mapAltType f (UniqueDefault x) = UniqueDefault (f x)
578 | lhsInCurrentNS : {auto c : Ref Ctxt Defs} ->
579 | NestedNames vars -> RawImp -> Core RawImp
580 | lhsInCurrentNS nest (IApp loc f a)
581 | = do f' <- lhsInCurrentNS nest f
582 | pure (IApp loc f' a)
583 | lhsInCurrentNS nest (IAutoApp loc f a)
584 | = do f' <- lhsInCurrentNS nest f
585 | pure (IAutoApp loc f' a)
586 | lhsInCurrentNS nest (INamedApp loc f n a)
587 | = do f' <- lhsInCurrentNS nest f
588 | pure (INamedApp loc f' n a)
589 | lhsInCurrentNS nest (IWithApp loc f a)
590 | = do f' <- lhsInCurrentNS nest f
591 | pure (IWithApp loc f' a)
592 | lhsInCurrentNS nest tm@(IVar loc (NS {})) = pure tm
593 | lhsInCurrentNS nest (IVar loc n)
594 | = case lookup n (names nest) of
596 | do n' <- inCurrentNS n
601 | Just _ => pure (IVar loc n)
602 | lhsInCurrentNS nest tm = pure tm
605 | findIBinds : RawImp' nm -> List String
606 | findIBinds (IPi fc rig p mn aty retty)
607 | = findIBinds aty ++ findIBinds retty
608 | findIBinds (ILam fc rig p n aty sc)
609 | = findIBinds aty ++ findIBinds sc
610 | findIBinds (IApp fc fn av)
611 | = findIBinds fn ++ findIBinds av
612 | findIBinds (IAutoApp fc fn av)
613 | = findIBinds fn ++ findIBinds av
614 | findIBinds (INamedApp _ fn _ av)
615 | = findIBinds fn ++ findIBinds av
616 | findIBinds (IWithApp fc fn av)
617 | = findIBinds fn ++ findIBinds av
618 | findIBinds (IAs fc _ _ (UN (Basic n)) pat)
619 | = n :: findIBinds pat
620 | findIBinds (IAs fc _ _ n pat)
622 | findIBinds (IMustUnify fc r pat)
624 | findIBinds (IAlternative fc u alts)
625 | = concatMap findIBinds alts
626 | findIBinds (IDelayed fc _ ty) = findIBinds ty
627 | findIBinds (IDelay fc tm) = findIBinds tm
628 | findIBinds (IForce fc tm) = findIBinds tm
629 | findIBinds (IQuote fc tm) = findIBinds tm
630 | findIBinds (IUnquote fc tm) = findIBinds tm
631 | findIBinds (IRunElab fc _ tm) = findIBinds tm
632 | findIBinds (IBindHere _ _ tm) = findIBinds tm
633 | findIBinds (IBindVar _ (UN (Basic n))) = [n]
634 | findIBinds (IUpdate fc updates tm)
635 | = findIBinds tm ++ concatMap (findIBinds . getFieldUpdateTerm) updates
641 | findImplicits : RawImp' nm -> List String
642 | findImplicits (IPi fc rig p (Just (UN (Basic mn))) aty retty)
643 | = mn :: findImplicits aty ++ findImplicits retty
644 | findImplicits (IPi fc rig p mn aty retty)
645 | = findImplicits aty ++ findImplicits retty
646 | findImplicits (ILam fc rig p n aty sc)
647 | = findImplicits aty ++ findImplicits sc
648 | findImplicits (IApp fc fn av)
649 | = findImplicits fn ++ findImplicits av
650 | findImplicits (IAutoApp _ fn av)
651 | = findImplicits fn ++ findImplicits av
652 | findImplicits (INamedApp _ fn _ av)
653 | = findImplicits fn ++ findImplicits av
654 | findImplicits (IWithApp fc fn av)
655 | = findImplicits fn ++ findImplicits av
656 | findImplicits (IAs fc _ _ n pat)
657 | = findImplicits pat
658 | findImplicits (IMustUnify fc r pat)
659 | = findImplicits pat
660 | findImplicits (IAlternative fc u alts)
661 | = concatMap findImplicits alts
662 | findImplicits (IDelayed fc _ ty) = findImplicits ty
663 | findImplicits (IDelay fc tm) = findImplicits tm
664 | findImplicits (IForce fc tm) = findImplicits tm
665 | findImplicits (IQuote fc tm) = findImplicits tm
666 | findImplicits (IUnquote fc tm) = findImplicits tm
667 | findImplicits (IRunElab fc _ tm) = findImplicits tm
668 | findImplicits (IBindVar _ (UN (Basic n))) = [n]
669 | findImplicits (IUpdate fc updates tm)
670 | = findImplicits tm ++ concatMap (findImplicits . getFieldUpdateTerm) updates
671 | findImplicits tm = []
678 | implicitsAs : {auto c : Ref Ctxt Defs} ->
680 | (vars : List Name) ->
681 | RawImp -> Core RawImp
682 | implicitsAs n defs ns tm
683 | = do let implicits = findIBinds tm
684 | log "declare.def.lhs.implicits" 30 $
"Found implicits: " ++ show implicits
685 | setAs (map Just (ns ++ map (UN . Basic) implicits)) [] tm
692 | setAs : List (Maybe Name) -> List (Maybe Name) -> RawImp -> Core RawImp
693 | setAs is es (IApp loc f a)
694 | = do f' <- setAs is (Nothing :: es) f
695 | pure $
IApp loc f' a
696 | setAs is es (IAutoApp loc f a)
697 | = do f' <- setAs (Nothing :: is) es f
698 | pure $
IAutoApp loc f' a
699 | setAs is es (INamedApp loc f n a)
700 | = do f' <- setAs (Just n :: is) (Just n :: es) f
701 | pure $
INamedApp loc f' n a
702 | setAs is es (IWithApp loc f a)
703 | = do f' <- setAs is es f
704 | pure $
IWithApp loc f' a
705 | setAs is es (IVar loc nm)
707 | = case !(lookupTyExact (Resolved n) (gamma defs)) of
709 | do log "declare.def.lhs.implicits" 30 $
710 | "Could not find variable " ++ show n
713 | do ty' <- nf defs Env.empty ty
714 | implicits <- findImps is es ns ty'
715 | log "declare.def.lhs.implicits" 30 $
716 | "\n In the type of " ++ show n ++ ": " ++ show ty ++
717 | "\n Using locals: " ++ show ns ++
718 | "\n Found implicits: " ++ show implicits
719 | pure $
impAs (virtualiseFC loc) implicits (IVar loc nm)
724 | updateNs : Name -> List (Maybe Name) -> Maybe (List (Maybe Name))
725 | updateNs n (Nothing :: ns) = Just ns
726 | updateNs n (x :: ns)
729 | else do ns' <- updateNs n ns
731 | updateNs n [] = Nothing
741 | findImps : List (Maybe Name) -> List (Maybe Name) ->
742 | List Name -> ClosedNF ->
743 | Core (List (Name, PiInfo RawImp))
749 | findImps ns es (_ :: locals) (NBind fc x (Pi {}) sc)
750 | = do body <- sc defs (toClosure defaultOpts Env.empty (Erased fc Placeholder))
751 | findImps ns es locals body
754 | findImps ns es [] (NBind fc x (Pi _ _ Explicit _) sc)
755 | = do body <- sc defs (toClosure defaultOpts Env.empty (Erased fc Placeholder))
758 | Just (UN Underscore) :: _ => findImps ns es [] body
760 | _ => case updateNs x es of
762 | Just es' => findImps ns es' [] body
764 | findImps ns es [] (NBind fc x (Pi _ _ AutoImplicit _) sc)
765 | = do body <- sc defs (toClosure defaultOpts Env.empty (Erased fc Placeholder))
766 | case updateNs x ns of
768 | pure $
(x, AutoImplicit) :: !(findImps ns es [] body)
769 | Just ns' => findImps ns' es [] body
770 | findImps ns es [] (NBind fc x (Pi _ _ p _) sc)
771 | = do body <- sc defs (toClosure defaultOpts Env.empty (Erased fc Placeholder))
772 | if Just x `elem` ns
773 | then findImps ns es [] body
774 | else pure $
(x, forgetDef p) :: !(findImps ns es [] body)
775 | findImps _ _ locals _
776 | = do log "declare.def.lhs.implicits" 50 $
777 | "Giving up with the following locals left: " ++ show locals
780 | impAs : FC -> List (Name, PiInfo RawImp) -> RawImp -> RawImp
781 | impAs loc' [] tm = tm
782 | impAs loc' ((nm@(UN (Basic _)), AutoImplicit) :: ns) tm
784 | INamedApp loc' tm nm (IBindVar loc' nm)
786 | impAs loc' ((n, Implicit) :: ns) tm
788 | INamedApp loc' tm n
789 | (IAs loc' EmptyFC UseLeft n (Implicit loc' True))
791 | impAs loc' ((n, DefImplicit t) :: ns) tm
793 | INamedApp loc' tm n
794 | (IAs loc' EmptyFC UseLeft n (Implicit loc' True))
796 | impAs loc' (_ :: ns) tm = impAs loc' ns tm
797 | setAs is es tm = pure tm
802 | definedInBlock : Namespace ->
803 | List ImpDecl -> List Name
804 | definedInBlock ns decls =
805 | Prelude.toList $
foldl (defName ns) empty decls
807 | getName : ImpTy -> Name
808 | getName = (.tyName.val)
810 | getFieldName : IField -> Name
811 | getFieldName f = f.name.val
813 | expandNS : Namespace -> Name -> Name
815 | = if ns == emptyNS then n else case n of
821 | defName : Namespace -> SortedSet Name -> ImpDecl -> SortedSet Name
822 | defName ns acc (IClaim c) = insert (expandNS ns (getName c.val.type)) acc
823 | defName ns acc (IDef _ nm _) = insert (expandNS ns nm) acc
824 | defName ns acc (IData _ _ _ (MkImpData _ n _ _ cons))
825 | = foldl (flip insert) acc $
expandNS ns n :: map (expandNS ns . getName) cons
826 | defName ns acc (IData _ _ _ (MkImpLater _ n _)) = insert (expandNS ns n) acc
827 | defName ns acc (IParameters _ _ pds) = foldl (defName ns) acc pds
828 | defName ns acc (IFail _ _ nds) = foldl (defName ns) acc nds
829 | defName ns acc (INamespace _ n nds) = foldl (defName (ns <.> n)) acc nds
830 | defName ns acc (IRecord _ fldns _ _ rec)
831 | = foldl (flip insert) acc $
expandNS ns rec.val.body.name.val :: all
834 | fldns' = maybe ns (\ f => ns <.> mkNamespace f) fldns
836 | toRF : Name -> Name
837 | toRF (UN (Basic n)) = UN (Field n)
841 | fnsUN = map getFieldName rec.val.body.val
844 | fnsRF = map toRF fnsUN
854 | all = expandNS ns rec.val.header.name.val :: map (expandNS fldns') (fnsRF ++ fnsUN)
856 | defName ns acc (IPragma _ pns _) = foldl (flip insert) acc $
map (expandNS ns) pns
857 | defName _ acc _ = acc
860 | isIVar : RawImp' nm -> Maybe (FC, nm)
861 | isIVar (IVar fc v) = Just (fc, v)
865 | isIBindVar : RawImp' nm -> Maybe (FC, Name)
866 | isIBindVar (IBindVar fc v) = Just (fc, v)
867 | isIBindVar _ = Nothing
870 | getFC : RawImp' nm -> FC
871 | getFC (IVar x _) = x
872 | getFC (IPi x _ _ _ _ _) = x
873 | getFC (ILam x _ _ _ _ _) = x
874 | getFC (ILet x _ _ _ _ _ _) = x
875 | getFC (ICase x _ _ _ _) = x
876 | getFC (ILocal x _ _) = x
877 | getFC (ICaseLocal x _ _ _ _) = x
878 | getFC (IUpdate x _ _) = x
879 | getFC (IApp x _ _) = x
880 | getFC (INamedApp x _ _ _) = x
881 | getFC (IAutoApp x _ _) = x
882 | getFC (IWithApp x _ _) = x
883 | getFC (ISearch x _) = x
884 | getFC (IAlternative x _ _) = x
885 | getFC (IRewrite x _ _) = x
886 | getFC (ICoerced x _) = x
887 | getFC (IPrimVal x _) = x
888 | getFC (IHole x _) = x
889 | getFC (IUnifyLog x _ _) = x
890 | getFC (IType x) = x
891 | getFC (IBindVar x _) = x
892 | getFC (IBindHere x _ _) = x
893 | getFC (IMustUnify x _ _) = x
894 | getFC (IDelayed x _ _) = x
895 | getFC (IDelay x _) = x
896 | getFC (IForce x _) = x
897 | getFC (IQuote x _) = x
898 | getFC (IQuoteName x _) = x
899 | getFC (IQuoteDecl x _) = x
900 | getFC (IUnquote x _) = x
901 | getFC (IRunElab x _ _) = x
902 | getFC (IAs x _ _ _ _) = x
903 | getFC (Implicit x _) = x
904 | getFC (IWithUnambigNames x _ _) = x
909 | getFC : ImpDecl' nm -> FC
910 | getFC (IClaim c) = c.fc
911 | getFC (IData fc _ _ _) = fc
912 | getFC (IDef fc _ _) = fc
913 | getFC (IParameters fc _ _) = fc
914 | getFC (IRecord fc _ _ _ _) = fc
915 | getFC (IFail fc _ _) = fc
916 | getFC (INamespace fc _ _) = fc
917 | getFC (ITransform fc _ _ _) = fc
918 | getFC (IRunElabDecl fc _) = fc
919 | getFC (IPragma fc _ _) = fc
920 | getFC (ILog _) = EmptyFC
921 | getFC (IBuiltin fc _ _) = fc
925 | = Explicit FC (RawImp' nm)
926 | | Auto FC (RawImp' nm)
927 | | Named FC Name (RawImp' nm)
936 | IArg = Arg' KindedName
939 | isExplicit : Arg' nm -> Maybe (FC, RawImp' nm)
940 | isExplicit (Explicit fc t) = Just (fc, t)
941 | isExplicit _ = Nothing
944 | unIArg : Arg' nm -> RawImp' nm
945 | unIArg (Explicit _ t) = t
946 | unIArg (Auto _ t) = t
947 | unIArg (Named _ _ t) = t
951 | Show nm => Show (Arg' nm) where
952 | show (Explicit fc t) = show t
953 | show (Auto fc t) = "@{" ++ show t ++ "}"
954 | show (Named fc n t) = "{" ++ show n ++ " = " ++ show t ++ "}"
957 | getFnArgs : RawImp' nm -> List (Arg' nm) -> (RawImp' nm, List (Arg' nm))
958 | getFnArgs (IApp fc f arg) args = getFnArgs f (Explicit fc arg :: args)
959 | getFnArgs (INamedApp fc f n arg) args = getFnArgs f (Named fc n arg :: args)
960 | getFnArgs (IAutoApp fc f arg) args = getFnArgs f (Auto fc arg :: args)
961 | getFnArgs tm args = (tm, args)
966 | apply : RawImp' nm -> List (Arg' nm) -> RawImp' nm
967 | apply f (Explicit fc a :: args) = apply (IApp fc f a) args
968 | apply f (Auto fc a :: args) = apply (IAutoApp fc f a) args
969 | apply f (Named fc n a :: args) = apply (INamedApp fc f n a) args
973 | apply : RawImp' nm -> List (RawImp' nm) -> RawImp' nm
975 | apply f (x :: xs) =
976 | let fFC = getFC f in
977 | apply (IApp (fromMaybe fFC (mergeFC fFC (getFC x))) f x) xs
980 | gapply : RawImp' nm -> List (Maybe Name, RawImp' nm) -> RawImp' nm
982 | gapply f (x :: xs) = gapply (uncurry (app f) x) xs where
984 | app : RawImp' nm -> Maybe Name -> RawImp' nm -> RawImp' nm
985 | app f Nothing x = IApp (getFC f) f x
986 | app f (Just nm) x = INamedApp (getFC f) f nm x
990 | getFn : RawImp' nm -> RawImp' nm
991 | getFn (IApp _ f _) = getFn f
992 | getFn (IWithApp _ f _) = getFn f
993 | getFn (INamedApp _ f _ _) = getFn f
994 | getFn (IAutoApp _ f _) = getFn f
995 | getFn (IAs _ _ _ _ f) = getFn f
996 | getFn (IMustUnify _ _ f) = getFn f
1001 | logRaw : {auto c : Ref Ctxt Defs} ->
1002 | LogTopic -> Nat -> Lazy String -> RawImp -> Core ()
1003 | logRaw s n msg tm = log s n $
msg ++ ": " ++ show tm