0 | module TTImp.ProcessDef.Dot
4 | import Core.Normalise
8 | import TTImp.Elab.App
11 | import Data.SnocList
13 | isPattern : Term vars -> Bool
14 | isPattern (App {}) = True
15 | isPattern (Ref {}) = True
16 | isPattern (TDelay {}) = True
17 | isPattern (PrimVal {}) = True
18 | isPattern (TType {}) = True
19 | isPattern (Bind _ _ (Pi {}) _) = True
22 | isImplicit : RawImp -> Bool
23 | isImplicit (Implicit {}) = True
24 | isImplicit (IAs _ _ _ _ tm) = isImplicit tm
25 | isImplicit _ = False
27 | dot : FC -> Term vars -> Term vars
28 | dot _ tm@(Erased {}) = tm
29 | dot fc tm = if isPattern tm
30 | then Erased fc $
Dotted tm
33 | parameters {auto c : Ref Ctxt Defs} {vars : Scope}
34 | (defs : Defs) (nest : NestedNames vars) (env : Env Term vars)
36 | dotInferred : (topLevel : Bool) ->
41 | dotIfInferred : FC -> RawImp -> Term vars ->
43 | dotIfInferred fc (IAs _ _ _ _ rawArg) (As fc' s n arg)
44 | = As fc' s n <$> dotIfInferred fc rawArg arg
45 | dotIfInferred fc rawArg arg
46 | = if isImplicit rawArg
47 | then pure $
dot fc arg
48 | else dotInferred False rawArg arg
50 | addDots : (acc : SnocList (FC, Term vars)) ->
52 | (args : List (FC, Term vars)) ->
53 | (expargs : List RawImp) ->
54 | (autoargs : List RawImp) ->
55 | (namedargs : List (Name, RawImp)) ->
56 | Core (List (FC, Term vars))
57 | addDots acc (NBind _ n (Pi _ _ Explicit _) sc) ((fc, arg) :: args) (rawArg :: exps) autos named
58 | = addDots (acc :< (fc, !(dotIfInferred fc rawArg arg)))
59 | !(sc defs $
toClosure defaultOpts env arg)
60 | args exps autos named
61 | addDots acc (NBind _ n (Pi _ _ AutoImplicit _) sc) ((fc, arg) :: args) exps (rawArg :: autos) named
62 | = addDots (acc :< (fc, !(dotIfInferred fc rawArg arg)))
63 | !(sc defs $
toClosure defaultOpts env arg)
64 | args exps autos named
65 | addDots acc (NBind _ n (Pi _ _ Explicit _) sc) ((fc, arg) :: args) [] autos named
66 | = do (arg', named') <- case findNamed n named of
67 | Nothing => case findBindAllExpPattern named of
68 | Nothing => throw $
InternalError "Impossible happened: explicit argument not found."
69 | Just rawArg => pure (!(dotIfInferred fc rawArg arg), named)
70 | Just ((_, rawArg), named') => pure (!(dotIfInferred fc rawArg arg), named')
71 | addDots (acc :< (fc, arg'))
72 | !(sc defs $
toClosure defaultOpts env arg')
73 | args [] autos named'
74 | addDots acc (NBind _ n (Pi _ _ AutoImplicit _) sc) ((fc, arg) :: args) exps [] named
75 | = do (arg', named') <- case findNamed n named of
76 | Nothing => pure (dot fc arg, named)
77 | Just ((_, rawArg), named') => pure (!(dotIfInferred fc rawArg arg), named')
78 | addDots (acc :< (fc, arg'))
79 | !(sc defs $
toClosure defaultOpts env arg')
81 | addDots acc (NBind _ n (Pi _ _ Implicit _) sc) ((fc, arg) :: args) exps autos named
82 | = do (arg', named') <- case findNamed n named of
83 | Nothing => pure (dot fc arg, named)
84 | Just ((_, rawArg), named') => pure (!(dotIfInferred fc rawArg arg), named')
85 | addDots (acc :< (fc, arg'))
86 | !(sc defs $
toClosure defaultOpts env arg')
87 | args exps autos named'
88 | addDots acc (NBind _ n (Pi _ _ (DefImplicit defArg) _) sc) ((fc, arg) :: args) exps autos named
89 | = do (arg', named') <- case findNamed n named of
90 | Nothing => pure (dot fc arg, named)
91 | Just ((_, rawArg), named') => pure (!(dotIfInferred fc rawArg arg), named')
92 | addDots (acc :< (fc, arg'))
93 | !(sc defs $
toClosure defaultOpts env arg')
94 | args exps autos named'
95 | addDots acc ty [] [] [] named
96 | = if null $
filterBindAllExpPattern named
97 | then pure $
toList acc
98 | else throw $
InternalError "Impossible happened: arguments mismatch."
100 | = throw $
InternalError $
"Impossible happened: arguments mismatch."
103 | (acc : SnocList (FC, Term vars)) ->
104 | (args : List (FC, Term vars)) ->
106 | Core (SnocList (FC, Term vars), List (FC, Term vars), NF vars)
107 | skipArgs Z acc args ty = pure (acc, args, ty)
108 | skipArgs (S n) acc ((fc, arg) :: args) (NBind _ _ (Pi {}) sc)
109 | = skipArgs n (acc :< (fc, arg)) args !(sc defs $
toClosure defaultOpts env arg)
111 | = throw $
InternalError "Impossible happened: expected nested argument."
113 | dotInferred topLevel (IAlternative fc ty alts) tm
114 | = do let (Ref _ _ nm) = getFn tm
116 | let Just alt = pickAlt !(toFullNames nm) alts
118 | dotInferred topLevel alt tm
120 | pickAlt : Name -> List RawImp -> Maybe RawImp
121 | pickAlt nm [] = Nothing
122 | pickAlt nm (x :: xs)
123 | = do let (IVar _ nm') = getFn x
128 | dotInferred topLevel raw tm = go raw [] [] []
131 | needsDot : Name -> Core Bool
135 | else do prims <- getPrimitiveNames
136 | pure $
not $
isPrimName prims nm
139 | (expargs : List RawImp) ->
140 | (autoargs : List RawImp) ->
141 | (namedargs : List (Name, RawImp)) ->
143 | go (IApp fc fn arg) exps autos named
144 | = go fn (arg :: exps) autos named
145 | go (IWithApp fc fn arg) exps autos named
146 | = go fn (arg :: exps) autos named
147 | go (IAutoApp fc fn arg) exps autos named
148 | = go fn exps (arg :: autos) named
149 | go (INamedApp fc fn nm arg) exps autos named
150 | = go fn exps autos ((nm, arg) :: named)
151 | go (IVar _ rawName) exps autos named
152 | = do let (fn@(Ref fc t nm), args) = getFnArgsWithFC tm
154 | Just def <- lookupCtxtExact nm (gamma defs)
155 | | Nothing => undefinedName fc nm
156 | True <- needsDot rawName
159 | let skip = maybe 0 (the (_ -> _) $
\(_, n, _) => length n) $
160 | lookup nm (names nest) <|> lookup rawName (names nest)
164 | tynf <- nf defs env $
embed $
type def
165 | (skipped, rest, ty') <- skipArgs skip [<] args tynf
166 | args' <- addDots skipped ty' rest exps autos named
167 | pure $
applyStackWithFC fn args'
168 | go (IPi _ _ _ _ aty _) [] [] []
170 | Bind fc n (Pi bfc r p ty) sc
171 | => pure $
Bind fc n (Pi bfc r p !(dotIfInferred bfc aty ty)) sc
172 | _ => throw $
InternalError "Expected Pi type, got \{show tm}"
173 | go (IPi _ _ _ _ aty _) exps autos named
174 | = throw $
InternalError "Unexpected arguments Pi-type with arguments: \{show raw}"