quox/lib/Quox/Typing/Error.idr

408 lines
13 KiB
Idris
Raw Normal View History

2023-03-13 16:41:57 -04:00
module Quox.Typing.Error
2023-05-01 21:06:25 -04:00
import Quox.Loc
2023-03-13 16:41:57 -04:00
import Quox.Syntax
import Quox.Typing.Context
import Quox.Typing.EqMode
import Quox.Pretty
import Data.List
2023-03-31 13:23:30 -04:00
import Control.Eff
2023-09-17 13:09:54 -04:00
import Derive.Prelude
%language ElabReflection
%hide TT.Name
2023-03-13 16:41:57 -04:00
2023-04-15 09:13:01 -04:00
public export
record NameContexts d n where
constructor MkNameContexts
2023-05-01 21:06:25 -04:00
dnames : BContext d
tnames : BContext n
2023-09-17 13:09:54 -04:00
%runElab deriveIndexed "NameContexts" [Show]
2023-04-15 09:13:01 -04:00
namespace NameContexts
export
empty : NameContexts 0 0
empty = MkNameContexts [<] [<]
export
2023-05-01 21:06:25 -04:00
extendDimN : BContext s -> NameContexts d n -> NameContexts (s + d) n
2023-04-15 09:13:01 -04:00
extendDimN xs = {dnames $= (++ toSnocVect' xs)}
export
2023-05-01 21:06:25 -04:00
extendDim : BindName -> NameContexts d n -> NameContexts (S d) n
2023-04-15 09:13:01 -04:00
extendDim i = extendDimN [< i]
namespace TyContext
public export
(.names) : TyContext d n -> NameContexts d n
(MkTyContext {dnames, tnames, _}).names =
MkNameContexts {dnames, tnames}
namespace EqContext
public export
(.names) : (e : EqContext n) -> NameContexts e.dimLen n
(MkEqContext {dnames, tnames, _}).names =
MkNameContexts {dnames, tnames}
public export
(.names0) : EqContext n -> NameContexts 0 n
(MkEqContext {tnames, _}).names0 =
MkNameContexts {dnames = [<], tnames}
namespace WhnfContext
public export
(.names) : WhnfContext d n -> NameContexts d n
(MkWhnfContext {dnames, tnames, _}).names =
MkNameContexts {dnames, tnames}
2023-03-13 16:41:57 -04:00
public export
2023-04-01 13:16:43 -04:00
data Error
2023-05-01 21:06:25 -04:00
= ExpectedTYPE Loc (NameContexts d n) (Term d n)
| ExpectedPi Loc (NameContexts d n) (Term d n)
| ExpectedSig Loc (NameContexts d n) (Term d n)
| ExpectedEnum Loc (NameContexts d n) (Term d n)
| ExpectedEq Loc (NameContexts d n) (Term d n)
| ExpectedNat Loc (NameContexts d n) (Term d n)
| ExpectedBOX Loc (NameContexts d n) (Term d n)
| BadUniverse Loc Universe Universe
| TagNotIn Loc TagVal (SortedSet TagVal)
| BadCaseEnum Loc (SortedSet TagVal) (SortedSet TagVal)
| BadQtys Loc String (TyContext d n) (List (QOutput n, Term d n))
2023-03-13 16:41:57 -04:00
-- first term arg of ClashT is the type
2023-05-01 21:06:25 -04:00
| ClashT Loc (EqContext n) EqMode (Term 0 n) (Term 0 n) (Term 0 n)
| ClashTy Loc (EqContext n) EqMode (Term 0 n) (Term 0 n)
| ClashE Loc (EqContext n) EqMode (Elim 0 n) (Elim 0 n)
| ClashU Loc EqMode Universe Universe
| ClashQ Loc Qty Qty
| NotInScope Loc Name
2023-03-13 16:41:57 -04:00
2023-05-01 21:06:25 -04:00
| NotType Loc (TyContext d n) (Term d n)
| WrongType Loc (EqContext n) (Term 0 n) (Term 0 n)
2023-03-13 16:41:57 -04:00
2023-05-01 21:06:25 -04:00
| MissingEnumArm Loc TagVal (List TagVal)
2023-04-15 09:13:01 -04:00
2023-03-13 16:41:57 -04:00
-- extra context
| WhileChecking
(TyContext d n) SQty
2023-04-01 13:16:43 -04:00
(Term d n) -- term
(Term d n) -- type
Error
2023-03-13 16:41:57 -04:00
| WhileCheckingTy
2023-04-01 13:16:43 -04:00
(TyContext d n)
(Term d n)
2023-03-13 16:41:57 -04:00
(Maybe Universe)
2023-04-01 13:16:43 -04:00
Error
2023-03-13 16:41:57 -04:00
| WhileInferring
(TyContext d n) SQty
2023-04-01 13:16:43 -04:00
(Elim d n)
Error
2023-03-13 16:41:57 -04:00
| WhileComparingT
(EqContext n) EqMode SQty
2023-04-01 13:16:43 -04:00
(Term 0 n) -- type
(Term 0 n) (Term 0 n) -- lhs/rhs
Error
2023-03-13 16:41:57 -04:00
| WhileComparingE
(EqContext n) EqMode SQty
2023-04-01 13:16:43 -04:00
(Elim 0 n) (Elim 0 n)
Error
2023-03-13 16:41:57 -04:00
%name Error err
2023-09-17 13:09:54 -04:00
%runElab derive "Error" [Show]
2023-03-13 16:41:57 -04:00
public export
2023-04-01 13:16:43 -04:00
ErrorEff : Type -> Type
ErrorEff = Except Error
2023-03-13 16:41:57 -04:00
export
Located Error where
(ExpectedTYPE loc _ _).loc = loc
(ExpectedPi loc _ _).loc = loc
(ExpectedSig loc _ _).loc = loc
(ExpectedEnum loc _ _).loc = loc
(ExpectedEq loc _ _).loc = loc
(ExpectedNat loc _ _).loc = loc
(ExpectedBOX loc _ _).loc = loc
(BadUniverse loc _ _).loc = loc
(TagNotIn loc _ _).loc = loc
(BadCaseEnum loc _ _).loc = loc
(BadQtys loc _ _ _).loc = loc
(ClashT loc _ _ _ _ _).loc = loc
(ClashTy loc _ _ _ _).loc = loc
(ClashE loc _ _ _ _).loc = loc
(ClashU loc _ _ _).loc = loc
(ClashQ loc _ _).loc = loc
(NotInScope loc _).loc = loc
(NotType loc _ _).loc = loc
(WrongType loc _ _ _).loc = loc
(MissingEnumArm loc _ _).loc = loc
(WhileChecking _ _ _ _ err).loc = err.loc
(WhileCheckingTy _ _ _ err).loc = err.loc
(WhileInferring _ _ _ err).loc = err.loc
(WhileComparingT _ _ _ _ _ _ err).loc = err.loc
(WhileComparingE _ _ _ _ _ err).loc = err.loc
2023-03-13 16:41:57 -04:00
||| separates out all the error context layers
||| (e.g. "while checking s : A, …")
export
2023-04-01 13:16:43 -04:00
explodeContext : Error -> (List (Error -> Error), Error)
2023-05-14 13:58:46 -04:00
explodeContext (WhileChecking ctx x s t err) =
mapFst (WhileChecking ctx x s t ::) $ explodeContext err
explodeContext (WhileCheckingTy ctx s k err) =
mapFst (WhileCheckingTy ctx s k ::) $ explodeContext err
explodeContext (WhileInferring ctx x e err) =
mapFst (WhileInferring ctx x e ::) $ explodeContext err
explodeContext (WhileComparingT ctx x sg s t r err) =
mapFst (WhileComparingT ctx x sg s t r ::) $ explodeContext err
explodeContext (WhileComparingE ctx x sg e f err) =
mapFst (WhileComparingE ctx x sg e f ::) $ explodeContext err
2023-05-14 13:58:46 -04:00
explodeContext err = ([], err)
2023-03-13 16:41:57 -04:00
||| leaves the outermost context layer, and the innermost (up to) n, and removes
||| the rest if there are more than n+1 in total
export
2023-04-01 13:16:43 -04:00
trimContext : Nat -> Error -> Error
2023-03-13 16:41:57 -04:00
trimContext n err =
case explodeContext err of
([], err) => err
(f :: fs, err) => f $ foldr apply err $ takeEnd n fs
where
takeEnd : Nat -> List a -> List a
takeEnd n = reverse . take n . reverse
2023-03-31 13:23:30 -04:00
private
expect : Has (Except e) fs =>
(a -> a -> e) -> (a -> a -> Bool) -> a -> a -> Eff fs ()
expect err cmp x y = unless (x `cmp` y) $ throw $ err x y
2023-03-13 16:41:57 -04:00
2023-05-01 21:06:25 -04:00
parameters {auto _ : Has ErrorEff fs} (loc : Loc)
2023-03-13 16:41:57 -04:00
export %inline
2023-04-01 13:16:43 -04:00
expectEqualQ : Qty -> Qty -> Eff fs ()
2023-05-01 21:06:25 -04:00
expectEqualQ = expect (ClashQ loc) (==)
2023-03-13 16:41:57 -04:00
export %inline
2023-04-01 13:16:43 -04:00
expectCompatQ : Qty -> Qty -> Eff fs ()
2023-05-01 21:06:25 -04:00
expectCompatQ = expect (ClashQ loc) compat
2023-03-13 16:41:57 -04:00
export %inline
2023-03-31 13:23:30 -04:00
expectModeU : EqMode -> Universe -> Universe -> Eff fs ()
2023-05-01 21:06:25 -04:00
expectModeU mode = expect (ClashU loc mode) $ ucmp mode
2023-03-13 16:41:57 -04:00
2023-03-15 10:54:51 -04:00
private
2023-05-14 13:58:46 -04:00
prettyMode : EqMode -> String
2023-03-15 10:54:51 -04:00
prettyMode Equal = "equal to"
prettyMode Sub = "a subtype of"
prettyMode Super = "a supertype of"
private
2023-05-14 13:58:46 -04:00
prettyModeU : EqMode -> String
2023-03-15 10:54:51 -04:00
prettyModeU Equal = "equal to"
prettyModeU Sub = "less than or equal to"
prettyModeU Super = "greater than or equal to"
private
2023-05-14 13:58:46 -04:00
isTypeInUniverse : Maybe Universe -> String
2023-03-15 10:54:51 -04:00
isTypeInUniverse Nothing = "is a type"
2023-05-14 13:58:46 -04:00
isTypeInUniverse (Just k) = "is a type in universe \{show k}"
2023-03-15 10:54:51 -04:00
2023-05-14 13:58:46 -04:00
private
filterSameQtys : BContext n -> List (QOutput n, z) ->
Exists $ \n' => (BContext n', List (QOutput n', z))
filterSameQtys [<] qts = Evidence 0 ([<], qts)
filterSameQtys (ns :< n) qts =
let (qs, qts) = unzip $ map (\(qs :< q, t) => (q, qs, t)) qts
Evidence l (ns, qts) = filterSameQtys ns qts
in
if allSame qs
then Evidence l (ns, qts)
else Evidence (S l) (ns :< n, zipWith (\(qs, t), q => (qs :< q, t)) qts qs)
where
allSame : List Qty -> Bool
allSame [] = True
allSame (q :: qs) = all (== q) qs
private
printCaseQtys : {opts : _} -> TyContext d n ->
BContext n' -> List (QOutput n', Term d n) ->
Eff Pretty (List (Doc opts))
printCaseQtys ctx ns qts =
let Evidence _ (ns, qts) = filterSameQtys ns qts in
traverse (line ns) qts
where
line : BContext l -> (QOutput l, Term d n) -> Eff Pretty (Doc opts)
line ns (qs, t) = map (("-" <++>) . sep) $ sequence
[hangDSingle "the term"
!(prettyTerm ctx.dnames ctx.tnames t),
hangDSingle "uses variables" $
separateTight !commaD $ toSnocList' !(traverse prettyTBind ns),
hangDSingle "with quantities" $
separateTight !commaD $ toSnocList' !(traverse prettyQty qs)]
export
prettyErrorNoLoc : {opts : _} -> (showContext : Bool) -> Error ->
Eff Pretty (Doc opts)
prettyErrorNoLoc showContext = \case
ExpectedTYPE _ ctx s =>
hangDSingle "expected a type universe, but got"
!(prettyTerm ctx.dnames ctx.tnames s)
ExpectedPi _ ctx s =>
hangDSingle "expected a function type, but got"
!(prettyTerm ctx.dnames ctx.tnames s)
ExpectedSig _ ctx s =>
hangDSingle "expected a pair type, but got"
!(prettyTerm ctx.dnames ctx.tnames s)
ExpectedEnum _ ctx s =>
hangDSingle "expected an enumeration type, but got"
!(prettyTerm ctx.dnames ctx.tnames s)
ExpectedEq _ ctx s =>
hangDSingle "expected an enumeration type, but got"
!(prettyTerm ctx.dnames ctx.tnames s)
ExpectedNat _ ctx s =>
hangDSingle
("expected the type" <++>
!(prettyTerm [<] [<] $ Nat noLoc) <+> ", but got")
!(prettyTerm ctx.dnames ctx.tnames s)
ExpectedBOX _ ctx s =>
hangDSingle "expected a box type, but got"
!(prettyTerm ctx.dnames ctx.tnames s)
BadUniverse _ k l => pure $
sep ["the universe level" <++> !(prettyUniverse k),
"is not strictly less than" <++> !(prettyUniverse l)]
TagNotIn _ tag set =>
hangDSingle (hsep ["the tag", !(prettyTag tag), "is not contained in"])
!(prettyTerm [<] [<] $ Enum set noLoc)
BadCaseEnum _ head body => sep <$> sequence
[hangDSingle "case expression has head of type"
!(prettyTerm [<] [<] $ Enum head noLoc),
hangDSingle "but cases for"
!(prettyTerm [<] [<] $ Enum body noLoc)]
BadQtys _ what ctx arms =>
hangDSingle (text "inconsistent variable usage in \{what}") $
sep !(printCaseQtys ctx ctx.tnames arms)
ClashT _ ctx mode ty s t =>
inEContext ctx . sep =<< sequence
[hangDSingle "the term" !(prettyTerm [<] ctx.tnames s),
hangDSingle (text "is not \{prettyMode mode}")
!(prettyTerm [<] ctx.tnames t),
hangDSingle "at type" !(prettyTerm [<] ctx.tnames ty)]
ClashTy _ ctx mode a b =>
inEContext ctx . sep =<< sequence
[hangDSingle "the type" !(prettyTerm [<] ctx.tnames a),
hangDSingle (text "is not \{prettyMode mode}")
!(prettyTerm [<] ctx.tnames b)]
ClashE _ ctx mode e f =>
inEContext ctx . sep =<< sequence
[hangDSingle "the term" !(prettyElim [<] ctx.tnames e),
hangDSingle (text "is not \{prettyMode mode}")
!(prettyElim [<] ctx.tnames f)]
ClashU _ mode k l => pure $
sep ["the universe level" <++> !(prettyUniverse k),
text "is not \{prettyModeU mode}" <++> !(prettyUniverse l)]
ClashQ _ pi rh => pure $
sep ["the quantity" <++> !(prettyQty pi),
"is not equal to" <++> !(prettyQty rh)]
NotInScope _ x => pure $
hsep [!(prettyFree x), "is not in scope"]
NotType _ ctx s =>
inTContext ctx . sep =<< sequence
[hangDSingle "the term" !(prettyTerm ctx.dnames ctx.tnames s),
pure "is not a type"]
WrongType _ ctx ty s =>
inEContext ctx . sep =<< sequence
[hangDSingle "the term" !(prettyTerm [<] ctx.tnames s),
hangDSingle "cannot have type" !(prettyTerm [<] ctx.tnames ty)]
MissingEnumArm _ tag tags => pure $
sep [hsep ["the tag", !(prettyTag tag), "is not contained in"],
!(prettyTerm [<] [<] $ Enum (fromList tags) noLoc)]
WhileChecking ctx sg s a err =>
2023-05-14 13:58:46 -04:00
[|vappendBlank
(inTContext ctx . sep =<< sequence
[hangDSingle "while checking" !(prettyTerm ctx.dnames ctx.tnames s),
hangDSingle "has type" !(prettyTerm ctx.dnames ctx.tnames a),
hangDSingle "with quantity" !(prettyQty sg.qty)])
2023-05-14 13:58:46 -04:00
(prettyErrorNoLoc showContext err)|]
WhileCheckingTy ctx a k err =>
[|vappendBlank
(inTContext ctx . sep =<< sequence
[hangDSingle "while checking" !(prettyTerm ctx.dnames ctx.tnames a),
pure $ text $ isTypeInUniverse k])
(prettyErrorNoLoc showContext err)|]
WhileInferring ctx sg e err =>
2023-05-14 13:58:46 -04:00
[|vappendBlank
(inTContext ctx . sep =<< sequence
[hangDSingle "while inferring the type of"
!(prettyElim ctx.dnames ctx.tnames e),
hangDSingle "with quantity" !(prettyQty sg.qty)])
2023-05-14 13:58:46 -04:00
(prettyErrorNoLoc showContext err)|]
WhileComparingT ctx mode sg a s t err =>
2023-05-14 13:58:46 -04:00
[|vappendBlank
(inEContext ctx . sep =<< sequence
[hangDSingle "while checking that" !(prettyTerm [<] ctx.tnames s),
hangDSingle (text "is \{prettyMode mode}")
!(prettyTerm [<] ctx.tnames t),
hangDSingle "at type" !(prettyTerm [<] ctx.tnames a),
hangDSingle "with quantity" !(prettyQty sg.qty)])
2023-05-14 13:58:46 -04:00
(prettyErrorNoLoc showContext err)|]
WhileComparingE ctx mode sg e f err =>
2023-05-14 13:58:46 -04:00
[|vappendBlank
(inEContext ctx . sep =<< sequence
[hangDSingle "while checking that" !(prettyElim [<] ctx.tnames e),
hangDSingle (text "is \{prettyMode mode}")
!(prettyElim [<] ctx.tnames f),
hangDSingle "with quantity" !(prettyQty sg.qty)])
2023-05-14 13:58:46 -04:00
(prettyErrorNoLoc showContext err)|]
where
vappendBlank : Doc opts -> Doc opts -> Doc opts
vappendBlank a b = flush a `vappend` b
inTContext : TyContext d n -> Doc opts -> Eff Pretty (Doc opts)
inTContext ctx doc =
if showContext && not (null ctx) then
pure $ vappend doc (sep ["in context", !(prettyTyContext ctx)])
else pure doc
inEContext : EqContext n -> Doc opts -> Eff Pretty (Doc opts)
inEContext ctx doc =
if showContext && not (null ctx) then
pure $ vappend doc (sep ["in context", !(prettyEqContext ctx)])
else pure doc
export
prettyError : {opts : _} -> (showContext : Bool) ->
Error -> Eff Pretty (Doc opts)
prettyError showContext err = sep <$> sequence
[prettyLoc err.loc, indentD =<< prettyErrorNoLoc showContext err]