0 | module TTImp.ProcessDef.Dot
  1 |
  2 | import Core.Context
  3 | import Core.Env
  4 | import Core.Normalise
  5 | import Core.Value
  6 |
  7 | import TTImp.TTImp
  8 | import TTImp.Elab.App
  9 |
 10 | import Data.DPair
 11 | import Data.SnocList
 12 |
 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
 20 | isPattern _ = False
 21 |
 22 | isImplicit : RawImp -> Bool
 23 | isImplicit (Implicit {}) = True
 24 | isImplicit (IAs _ _ _ _ tm) = isImplicit tm
 25 | isImplicit _ = False
 26 |
 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
 31 |                else tm
 32 |
 33 | parameters {auto c : Ref Ctxt Defs} {vars : Scope}
 34 |            (defs : Defs) (nest : NestedNames vars) (env : Env Term vars)
 35 |   export
 36 |   dotInferred : (topLevel : Bool) ->
 37 |                 RawImp ->
 38 |                 Term vars ->
 39 |                 Core (Term vars)
 40 |
 41 |   dotIfInferred : FC -> RawImp -> Term vars ->
 42 |                   Core (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
 49 |
 50 |   addDots : (acc : SnocList (FC, Term vars)) ->
 51 |             (ty : NF 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')
 80 |                    args exps [] named'
 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."
 99 |   addDots {}
100 |       = throw $ InternalError $ "Impossible happened: arguments mismatch."
101 |
102 |   skipArgs : Nat ->
103 |              (acc : SnocList (FC, Term vars)) ->
104 |              (args : List (FC, Term vars)) ->
105 |              (ty : NF 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)
110 |   skipArgs {}
111 |       = throw $ InternalError "Impossible happened: expected nested argument."
112 |
113 |   dotInferred topLevel (IAlternative fc ty alts) tm
114 |       = do let (Ref _ _ nm) = getFn tm
115 |              | _ => pure tm
116 |            let Just alt = pickAlt !(toFullNames nm) alts
117 |              | _ => pure tm
118 |            dotInferred topLevel alt tm
119 |     where
120 |       pickAlt : Name -> List RawImp -> Maybe RawImp
121 |       pickAlt nm [] = Nothing
122 |       pickAlt nm (x :: xs)
123 |           = do let (IVar _ nm') = getFn x
124 |                  | _ => Nothing
125 |                if nm == nm'
126 |                   then Just x
127 |                   else pickAlt nm xs
128 |   dotInferred topLevel raw tm = go raw [] [] []
129 |     where
130 |       -- Don't dot primitives functions in argument position
131 |       needsDot : Name -> Core Bool
132 |       needsDot nm
133 |         = if topLevel
134 |                then pure True
135 |                else do prims <- getPrimitiveNames
136 |                        pure $ not $ isPrimName prims nm
137 |
138 |       go : RawImp ->
139 |            (expargs : List RawImp) ->
140 |            (autoargs : List RawImp) ->
141 |            (namedargs : List (Name, RawImp)) ->
142 |            Core (Term vars)
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
153 |                  | _ => pure tm
154 |                Just def <- lookupCtxtExact nm (gamma defs)
155 |                  | Nothing => undefinedName fc nm
156 |                True <- needsDot rawName
157 |                  | _ => pure tm
158 |                -- TODO: remove `the` after fix idris-lang/Idris2#3418
159 |                let skip = maybe 0 (the (_ -> _) $ \(_, n, _) => length n) $
160 |                                   lookup nm (names nest) <|> lookup rawName (names nest)
161 |                                -- ^ Local block put raw name in `NestedNames`
162 |                                --   while parameters put `Resolved`.
163 |                                --   So, we need to check both
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 _) [] [] []
169 |          = case tm of
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}"
175 |       go {} = pure tm
176 |