0 | module TTImp.TTImp
   1 |
   2 | import Core.Context.Log
   3 | import Core.Env
   4 | import Core.Normalise
   5 | import Core.Value
   6 |
   7 | import public Data.List1
   8 | import Data.SortedSet
   9 |
  10 | import Libraries.Data.List.SizeOf
  11 | import Libraries.Data.WithDefault
  12 |
  13 | %default covering
  14 |
  15 | -- Information about names in nested blocks
  16 | public export
  17 | record NestedNames (vars : Scope) where
  18 |   constructor MkNested
  19 |   -- A map from names to the decorated version of the name, and the new name
  20 |   -- applied to its enclosing environment
  21 |   -- Takes the location and name type, because we don't know them until we
  22 |   -- elaborate the name at the point of use
  23 |   names : List (Name, (Maybe Name,  -- new name if there is one
  24 |                        List (Var vars), -- names used from the environment
  25 |                        FC -> NameType -> Term vars))
  26 |
  27 | export
  28 | Weaken NestedNames where
  29 |   weakenNs {ns = wkns} s (MkNested ns) = MkNested (map wknName ns)
  30 |     where
  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)))
  35 |
  36 | export
  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)
  41 |
  42 | -- replace nested name with full name
  43 | export
  44 | mapNestedName : NestedNames vars -> Name -> Name
  45 | mapNestedName nest n = case lookup n (names nest) of
  46 |                                (Just (Just n', _)) => n'
  47 |                                _ => n
  48 |
  49 | -- Unchecked terms, with implicit arguments
  50 | -- This is the raw, elaboratable form.
  51 | -- Higher level expressions (e.g. case, pattern matching let, where blocks,
  52 | -- do notation, etc, should elaborate via this, perhaps in some local
  53 | -- context).
  54 | public export
  55 | data BindMode = PI RigCount | PATTERN | COVERAGE | NONE
  56 |
  57 | %name BindMode bm
  58 |
  59 | mutual
  60 |
  61 |   public export
  62 |   RawImp : Type
  63 |   RawImp = RawImp' Name
  64 |
  65 |   public export
  66 |   IRawImp : Type
  67 |   IRawImp = RawImp' KindedName
  68 |
  69 |   public export
  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
  82 |        -- Local definitions made elsewhere, but that we're pushing
  83 |        -- into a case branch as nested names.
  84 |        -- An appearance of 'uname' maps to an application of
  85 |        -- 'internalName' to 'args'.
  86 |        ICaseLocal : FC -> (uname : Name) ->
  87 |                     (internalName : Name) ->
  88 |                     (args : List Name) -> RawImp' nm -> RawImp' nm
  89 |
  90 |        IUpdate : FC -> List (IFieldUpdate' nm) -> RawImp' nm -> RawImp' nm
  91 |
  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
  96 |
  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
 101 |
 102 |        -- Any implicit bindings in the scope should be bound here, using
 103 |        -- the given binder
 104 |        IBindHere : FC -> BindMode -> RawImp' nm -> RawImp' nm
 105 |        -- A name which should be implicitly bound
 106 |        IBindVar : FC -> Name -> RawImp' nm
 107 |        -- An 'as' pattern, valid on the LHS of a clause only
 108 |        IAs : FC -> (nameFC : FC) -> UseSide -> Name -> RawImp' nm -> RawImp' nm
 109 |        -- A 'dot' pattern, i.e. one which must also have the given value
 110 |        -- by unification
 111 |        IMustUnify : FC -> DotReason -> RawImp' nm -> RawImp' nm
 112 |
 113 |        -- Laziness annotations
 114 |        IDelayed : FC -> LazyReason -> RawImp' nm -> RawImp' nm -- the type
 115 |        IDelay : FC -> RawImp' nm -> RawImp' nm -- delay constructor
 116 |        IForce : FC -> RawImp' nm -> RawImp' nm
 117 |
 118 |        -- Quasiquoting
 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
 124 |
 125 |        IPrimVal : FC -> (c : Constant) -> RawImp' nm
 126 |        IType : FC -> RawImp' nm
 127 |        IHole : FC -> String -> RawImp' nm
 128 |
 129 |        IUnifyLog : FC -> LogLevel -> RawImp' nm -> RawImp' nm
 130 |        -- An implicit value, solved by unification, but which will also be
 131 |        -- bound (either as a pattern variable or a type variable) if unsolved
 132 |        -- at the end of elaborator
 133 |        Implicit : FC -> (bindIfUnsolved : Bool) -> RawImp' nm
 134 |
 135 |        -- with-disambiguation
 136 |        IWithUnambigNames : FC -> List (FC, Name) -> RawImp' nm -> RawImp' nm
 137 |
 138 |   %name RawImp' t, u
 139 |
 140 |   public export
 141 |   IFieldUpdate : Type
 142 |   IFieldUpdate = IFieldUpdate' Name
 143 |
 144 |   public export
 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
 149 |
 150 |   public export
 151 |   AltType : Type
 152 |   AltType = AltType' Name
 153 |
 154 |   public export
 155 |   data AltType' : Type -> Type where
 156 |        FirstSuccess : AltType' nm
 157 |        Unique : AltType' nm
 158 |        UniqueDefault : RawImp' nm -> AltType' nm
 159 |   %name AltType' alt
 160 |
 161 |   export
 162 |   covering
 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 ++ ")"
 183 |       show (IApp fc f a)
 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)
 192 |          = "%search"
 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 ++ ")"
 198 |
 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 ++ ")"
 219 |
 220 |   export
 221 |   covering
 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
 225 |
 226 |   public export
 227 |   FnOpt : Type
 228 |   FnOpt = FnOpt' Name
 229 |
 230 |   public export
 231 |   data FnOpt' : Type -> Type where
 232 |        Unsafe : FnOpt' nm
 233 |        Inline : FnOpt' nm
 234 |        NoInline : FnOpt' nm
 235 |        ||| Mark a function as deprecated.
 236 |        Deprecate : FnOpt' nm
 237 |        TCInline : FnOpt' nm
 238 |        -- Flag means the hint is a direct hint, not a function which might
 239 |        -- find the result (e.g. chasing parent interface dictionaries)
 240 |        Hint : Bool -> FnOpt' nm
 241 |        -- Flag means to use as a default if all else fails
 242 |        GlobalHint : Bool -> FnOpt' nm
 243 |        ExternFn : FnOpt' nm
 244 |        -- Defined externally, list calling conventions
 245 |        ForeignFn : List (RawImp' nm) -> FnOpt' nm
 246 |        -- Mark for export to a foreign language, list calling conventions
 247 |        ForeignExport : List (RawImp' nm) -> FnOpt' nm
 248 |        -- assume safe to cancel arguments in unification
 249 |        Invertible : FnOpt' nm
 250 |        Totality : TotalReq -> FnOpt' nm
 251 |        Macro : FnOpt' nm
 252 |        SpecArgs : List Name -> FnOpt' nm
 253 |   %name FnOpt' fopt
 254 |
 255 |   public export
 256 |   isTotalityReq : FnOpt' nm -> Bool
 257 |   isTotalityReq (Totality _) = True
 258 |   isTotalityReq _ = False
 259 |
 260 |   export
 261 |   extractTotality : FnOpt' nm -> Maybe TotalReq
 262 |   extractTotality (Totality t) = Just t
 263 |   extractTotality _ = Nothing
 264 |
 265 |   export
 266 |   findTotality : List (FnOpt' nm) -> Maybe TotalReq
 267 |   findTotality = foldr (\elem, acc => extractTotality elem <|> acc) empty
 268 |
 269 |   export
 270 |   covering
 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)
 288 |
 289 |   export
 290 |   Eq FnOpt where
 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 -- xs == ys
 299 |     (ForeignExport xs) == (ForeignExport ys) = True -- xs == ys
 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'
 304 |     _ == _ = False
 305 |
 306 |   public export
 307 |   ImpTy : Type
 308 |   ImpTy = ImpTy' Name
 309 |
 310 |   public export
 311 |   ImpTy' : Type -> Type
 312 |   ImpTy' = AddMetadata FC' . AddMetadata TyName' . RawImp'
 313 |
 314 |   export
 315 |   covering
 316 |   Show nm => Show (ImpTy' nm) where
 317 |     show ty = "(%claim " ++ show ty.tyName.val ++ " " ++ show ty.val ++ ")"
 318 |
 319 |   public export
 320 |   ImpData : Type
 321 |   ImpData = ImpData' Name
 322 |
 323 |   public export
 324 |   data ImpData' : Type -> Type where
 325 |        MkImpData : FC -> (n : Name) ->
 326 |                    -- if we have already declared the type using `MkImpLater`,
 327 |                    -- we are allowed to leave the telescope out here.
 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
 332 |
 333 |   %name ImpData' dat
 334 |
 335 |   export
 336 |   covering
 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 ++ ")"
 344 |
 345 |   public export
 346 |   IField : Type
 347 |   IField = IField' Name
 348 |
 349 |   public export
 350 |   IField' : Type -> Type
 351 |   IField' nm = AddFC $ ImpParameter' (RawImp' nm)
 352 |
 353 |   public export
 354 |   ImpParameter : Type
 355 |   ImpParameter = ImpParameter' (RawImp' Name)
 356 |
 357 |   public export
 358 |   ImpParameter' : Type -> Type
 359 |   ImpParameter' nm = WithRig $ WithName $ PiBindData nm
 360 |
 361 |   -- old datatype for ImpParameter, used for elabreflection compatibility
 362 |   public export
 363 |   OldParameters' : Type -> Type
 364 |   OldParameters' nm = (Name, RigCount, PiInfo (RawImp' nm), RawImp' nm)
 365 |
 366 |   public export
 367 |   toOldParams : ImpParameter' (RawImp' nm) -> OldParameters' nm
 368 |   toOldParams bind = (bind.name.val, bind.rig, bind.val.info, bind.val.boundType)
 369 |
 370 |   public export
 371 |   fromOldParams : OldParameters' nm -> ImpParameter' (RawImp' nm)
 372 |   fromOldParams (nm, rig, info,type) = Mk [rig, NoFC nm] (MkPiBindData info type)
 373 |
 374 |   export
 375 |   Show nm => Show (ImpParameter' nm) where
 376 |     show x = "\{show x.rig}\{show x.name.val} \{show x.val.boundType}"
 377 |
 378 |   public export 0
 379 |   ImpRecord : Type
 380 |   ImpRecord = AddFC $ ImpRecordData Name
 381 |
 382 |   public export 0
 383 |   DataHeader : Type -> Type -- the name is the type constructor's name
 384 |   DataHeader nm = WithName $ List (ImpParameter' (RawImp' nm))
 385 |
 386 |   public export 0
 387 |   RecordBody : Type -> Type -- The name is the data constructor's name
 388 |   RecordBody nm = WithName $ WithOpts $ List (IField' nm)
 389 |
 390 |   ||| A record is defined by its header containing the name and parameters, and its body
 391 |   ||| containing the constructor name, options, and a list of fields
 392 |   public export
 393 |   record ImpRecordData (nm : Type) where
 394 |     constructor MkImpRecord
 395 |     header : DataHeader nm
 396 |     body : RecordBody nm
 397 |
 398 |   export
 399 |   covering
 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 ++ "}"
 403 |
 404 |   export
 405 |   covering
 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"
 411 |
 412 |   public export
 413 |   data WithFlag
 414 |          = Syntactic -- abstract syntactically, rather than by value
 415 |
 416 |   export
 417 |   Eq WithFlag where
 418 |       Syntactic == Syntactic = True
 419 |
 420 |   public export
 421 |   ImpClause : Type
 422 |   ImpClause = ImpClause' Name
 423 |
 424 |   public export
 425 |   IImpClause : Type
 426 |   IImpClause = ImpClause' KindedName
 427 |
 428 |   public export
 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) -> -- with'd expression (& quantity)
 433 |                     (prf : Maybe (RigCount, Name)) -> -- optional name for the proof
 434 |                     (flags : List WithFlag) ->
 435 |                     List (ImpClause' nm) -> ImpClause' nm
 436 |        ImpossibleClause : FC -> (lhs : RawImp' nm) -> ImpClause' nm
 437 |
 438 |   %name ImpClause' cl
 439 |
 440 |   export
 441 |   covering
 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)
 446 |        = show lhs
 447 |        ++ " with " ++ showCount rig ++ "(" ++ show wval ++ ")"
 448 |           -- TODO: remove `the` after fix idris-lang/Idris2#3418
 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"
 453 |
 454 |   public export
 455 |   ImpDecl : Type
 456 |   ImpDecl = ImpDecl' Name
 457 |
 458 |   public export
 459 |   record IClaimData (nm : Type) where
 460 |     constructor MkIClaimData
 461 |     rig : RigCount
 462 |     vis : Visibility
 463 |     opts : List (FnOpt' nm)
 464 |     type : ImpTy' nm
 465 |
 466 |   public export
 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
 475 |        IRecord : FC ->
 476 |                  Maybe String -> -- nested namespace
 477 |                  WithDefault Visibility Private ->
 478 |                  Maybe TotalReq ->
 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 -> -- pragmas might define names that wouldn't
 485 |                                     -- otherwise be spotted in 'definedInBlock' so they
 486 |                                     -- can be flagged here.
 487 |                  ({vars : _} ->
 488 |                   NestedNames vars -> Env Term vars -> Core ()) ->
 489 |                  ImpDecl' nm
 490 |        ILog : Maybe (List String, Nat) -> ImpDecl' nm
 491 |        IBuiltin : FC -> BuiltinType -> Name -> ImpDecl' nm
 492 |
 493 |   %name ImpDecl' decl
 494 |
 495 |   export
 496 |   covering
 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
 519 |       [] => show lvl
 520 |       _  => concat (intersperse "." topic) ++ " " ++ show lvl
 521 |     show (IBuiltin _ type name) = "%builtin " ++ show type ++ " " ++ show name
 522 |
 523 |
 524 | export
 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]
 534 |
 535 | -- Extract the RawImp term from a FieldUpdate.
 536 | export
 537 | getFieldUpdateTerm : IFieldUpdate' nm -> RawImp' nm
 538 | getFieldUpdateTerm (ISetField    _ term) = term
 539 | getFieldUpdateTerm (ISetFieldApp _ term) = term
 540 |
 541 |
 542 | export
 543 | getFieldUpdatePath : IFieldUpdate' nm -> List String
 544 | getFieldUpdatePath (ISetField    path _) = path
 545 | getFieldUpdatePath (ISetFieldApp path _) = path
 546 |
 547 |
 548 | export
 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)
 552 |
 553 |
 554 | export
 555 | isIPrimVal : RawImp' nm -> Maybe Constant
 556 | isIPrimVal (IPrimVal _ c) = Just c
 557 | isIPrimVal _ = Nothing
 558 |
 559 | -- REPL commands for TTImp interaction
 560 | public export
 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
 570 |      Quit : ImpREPL
 571 |
 572 | export
 573 | mapAltType : (RawImp' nm -> RawImp' nm) -> AltType' nm -> AltType' nm
 574 | mapAltType f (UniqueDefault x) = UniqueDefault (f x)
 575 | mapAltType _ u = u
 576 |
 577 | export
 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 -- leave explicit NS alone
 593 | lhsInCurrentNS nest (IVar loc n)
 594 |     = case lookup n (names nest) of
 595 |            Nothing =>
 596 |               do n' <- inCurrentNS n
 597 |                  pure (IVar loc n')
 598 |            -- If it's one of the names in the current nested block, we'll
 599 |            -- be rewriting it during elaboration to be in the scope of the
 600 |            -- parent name.
 601 |            Just _ => pure (IVar loc n)
 602 | lhsInCurrentNS nest tm = pure tm
 603 |
 604 | export
 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)
 621 |     = findIBinds pat
 622 | findIBinds (IMustUnify fc r pat)
 623 |     = findIBinds 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
 636 | -- We've skipped lambda, case, let and local - rather than guess where the
 637 | -- name should be bound, leave it to the programmer
 638 | findIBinds tm = []
 639 |
 640 | export
 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 = []
 672 |
 673 | -- Update the lhs of a clause so that any implicits named in the type are
 674 | -- bound as @-patterns (unless they're already explicitly bound or appear as
 675 | -- IBindVar anywhere else in the pattern) so that they will be available on the
 676 | -- rhs
 677 | export
 678 | implicitsAs : {auto c : Ref Ctxt Defs} ->
 679 |               Int -> 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
 686 |   where
 687 |     -- Takes the function application expression which is the lhs of a clause
 688 |     -- and decomposes it into the underlying function symbol and the variables
 689 |     -- bound by appearing as arguments, and then passes this onto `findImps`.
 690 |     -- More precisely, implicit and explicit arguments are recorded separately,
 691 |     -- into `is` and `es` respectively.
 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)
 706 |         -- #834 Use the (already) resolved name rather than the local one
 707 |         = case !(lookupTyExact (Resolved n) (gamma defs)) of
 708 |             Nothing =>
 709 |                do log "declare.def.lhs.implicits" 30 $
 710 |                     "Could not find variable " ++ show n
 711 |                   pure $ IVar loc nm
 712 |             Just ty =>
 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)
 720 |       where
 721 |         -- If there's an @{c} in the list of given implicits, that's the next
 722 |         -- autoimplicit, so don't rewrite the LHS and update the list of given
 723 |         -- implicits
 724 |         updateNs : Name -> List (Maybe Name) -> Maybe (List (Maybe Name))
 725 |         updateNs n (Nothing :: ns) = Just ns
 726 |         updateNs n (x :: ns)
 727 |             = if Just n == x
 728 |                  then Just ns -- found it
 729 |                  else do ns' <- updateNs n ns
 730 |                          pure (x :: ns')
 731 |         updateNs n [] = Nothing
 732 |
 733 |         -- Finds the missing implicits which should be added to the lhs of the
 734 |         -- original pattern clause.
 735 |         --
 736 |         -- The first argument, `ns`, specifies which implicit variables alredy
 737 |         -- appear in the lhs, and therefore need not be added.
 738 |         -- The second argument, `es`, specifies which *explicit* variables appear
 739 |         -- in the lhs: this is used to determine when to stop searching for further
 740 |         -- implicits to add.
 741 |         findImps : List (Maybe Name) -> List (Maybe Name) ->
 742 |                    List Name -> ClosedNF ->
 743 |                    Core (List (Name, PiInfo RawImp))
 744 |         -- #834 When we are in a local definition, we have an explicit telescope
 745 |         -- corresponding to the variables bound in the parent function.
 746 |         -- Parameter blocks also introduce additional telescope of implicit, auto,
 747 |         -- and explicit variables. So we first peel off all of the quantifiers
 748 |         -- corresponding to these variables.
 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
 752 |                -- ^ TODO? check that name of the pi matches name of local?
 753 |         -- don't add implicits coming after explicits that aren't given
 754 |         findImps ns es [] (NBind fc x (Pi _ _ Explicit _) sc)
 755 |             = do body <- sc defs (toClosure defaultOpts Env.empty (Erased fc Placeholder))
 756 |                  case es of
 757 |                    -- Explicits were skipped, therefore all explicits are given anyway
 758 |                    Just (UN Underscore) :: _ => findImps ns es [] body
 759 |                    -- Explicits weren't skipped, so we need to check
 760 |                    _ => case updateNs x es of
 761 |                           Nothing => pure [] -- explicit wasn't given
 762 |                           Just es' => findImps ns es' [] body
 763 |         -- if the implicit was given, skip it
 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
 767 |                    Nothing => -- didn't find explicit call
 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
 778 |                pure []
 779 |
 780 |         impAs : FC -> List (Name, PiInfo RawImp) -> RawImp -> RawImp
 781 |         impAs loc' [] tm = tm
 782 |         impAs loc' ((nm@(UN (Basic _)), AutoImplicit) :: ns) tm
 783 |             = impAs loc' ns $
 784 |                  INamedApp loc' tm nm (IBindVar loc' nm)
 785 |
 786 |         impAs loc' ((n, Implicit) :: ns) tm
 787 |             = impAs loc' ns $
 788 |                  INamedApp loc' tm n
 789 |                      (IAs loc' EmptyFC UseLeft n (Implicit loc' True))
 790 |
 791 |         impAs loc' ((n, DefImplicit t) :: ns) tm
 792 |             = impAs loc' ns $
 793 |                  INamedApp loc' tm n
 794 |                      (IAs loc' EmptyFC UseLeft n (Implicit loc' True))
 795 |
 796 |         impAs loc' (_ :: ns) tm = impAs loc' ns tm
 797 |     setAs is es tm = pure tm
 798 |
 799 | ||| `definedInBlock` is used to figure out which definitions should
 800 | ||| receive the additional arguments introduced by a Parameters directive
 801 | export
 802 | definedInBlock : Namespace -> -- namespace to resolve names
 803 |                  List ImpDecl -> List Name
 804 | definedInBlock ns decls =
 805 |     Prelude.toList $ foldl (defName ns) empty decls
 806 |   where
 807 |     getName : ImpTy -> Name
 808 |     getName = (.tyName.val)
 809 |
 810 |     getFieldName : IField -> Name
 811 |     getFieldName f = f.name.val
 812 |
 813 |     expandNS : Namespace -> Name -> Name
 814 |     expandNS ns n
 815 |        = if ns == emptyNS then n else case n of
 816 |            UN {} => NS ns n
 817 |            MN {} => NS ns n
 818 |            DN {} => NS ns n
 819 |            _ => n
 820 |
 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
 832 |       where
 833 |         fldns' : Namespace
 834 |         fldns' = maybe ns (\ f => ns <.> mkNamespace f) fldns
 835 |
 836 |         toRF : Name -> Name
 837 |         toRF (UN (Basic n)) = UN (Field n)
 838 |         toRF n = n
 839 |
 840 |         fnsUN : List Name
 841 |         fnsUN = map getFieldName rec.val.body.val
 842 |
 843 |         fnsRF : List Name
 844 |         fnsRF = map toRF fnsUN
 845 |
 846 |         -- Depending on %prefix_record_projections,
 847 |         -- the record may or may not produce prefix projections (fnsUN).
 848 |         --
 849 |         -- However, since definedInBlock is pure, we can't check that flag
 850 |         -- (and it would also be wrong if %prefix_record_projections appears
 851 |         -- inside the parameter block)
 852 |         -- so let's just declare all of them and some may go unused.
 853 |         all : List Name
 854 |         all = expandNS ns rec.val.header.name.val :: map (expandNS fldns') (fnsRF ++ fnsUN)
 855 |
 856 |     defName ns acc (IPragma _ pns _) = foldl (flip insert) acc $ map (expandNS ns) pns
 857 |     defName _ acc _ = acc
 858 |
 859 | export
 860 | isIVar : RawImp' nm -> Maybe (FC, nm)
 861 | isIVar (IVar fc v) = Just (fc, v)
 862 | isIVar _ = Nothing
 863 |
 864 | export
 865 | isIBindVar : RawImp' nm -> Maybe (FC, Name)
 866 | isIBindVar (IBindVar fc v) = Just (fc, v)
 867 | isIBindVar _ = Nothing
 868 |
 869 | export
 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
 905 |
 906 | namespace ImpDecl
 907 |
 908 |   public export
 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
 922 |
 923 | public export
 924 | data Arg' nm
 925 |    = Explicit FC (RawImp' nm)
 926 |    | Auto     FC (RawImp' nm)
 927 |    | Named    FC Name (RawImp' nm)
 928 | %name Arg' arg
 929 |
 930 | public export
 931 | Arg : Type
 932 | Arg = Arg' Name
 933 |
 934 | public export
 935 | IArg : Type
 936 | IArg = Arg' KindedName
 937 |
 938 | export
 939 | isExplicit : Arg' nm -> Maybe (FC, RawImp' nm)
 940 | isExplicit (Explicit fc t) = Just (fc, t)
 941 | isExplicit _ = Nothing
 942 |
 943 | export
 944 | unIArg : Arg' nm -> RawImp' nm
 945 | unIArg (Explicit _ t) = t
 946 | unIArg (Auto _ t) = t
 947 | unIArg (Named _ _ t) = t
 948 |
 949 | export
 950 | covering
 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 ++ "}"
 955 |
 956 | export
 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)
 962 |
 963 | -- TODO: merge these definitions
 964 | namespace Arg
 965 |   export
 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
 970 |   apply f [] = f
 971 |
 972 | export
 973 | apply : RawImp' nm -> List (RawImp' nm) -> RawImp' nm
 974 | apply f [] = f
 975 | apply f (x :: xs) =
 976 |   let fFC = getFC f in
 977 |   apply (IApp (fromMaybe fFC (mergeFC fFC (getFC x))) f x) xs
 978 |
 979 | export
 980 | gapply : RawImp' nm -> List (Maybe Name, RawImp' nm) -> RawImp' nm
 981 | gapply f [] = f
 982 | gapply f (x :: xs) = gapply (uncurry (app f) x) xs where
 983 |
 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
 987 |
 988 |
 989 | export
 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
 997 | getFn f = f
 998 |
 999 | -- Log message with a RawImp
1000 | export
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
1004 |