2023-03-13 16:41:57 -04:00
|
|
|
module Quox.Typing.Error
|
|
|
|
|
|
|
|
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-03-13 16:41:57 -04:00
|
|
|
|
|
|
|
|
2023-04-15 09:13:01 -04:00
|
|
|
public export
|
|
|
|
record NameContexts d n where
|
|
|
|
constructor MkNameContexts
|
|
|
|
dnames : NContext d
|
|
|
|
tnames : NContext n
|
|
|
|
|
|
|
|
namespace NameContexts
|
|
|
|
export
|
|
|
|
empty : NameContexts 0 0
|
|
|
|
empty = MkNameContexts [<] [<]
|
|
|
|
|
|
|
|
export
|
|
|
|
extendDimN : NContext s -> NameContexts d n -> NameContexts (s + d) n
|
|
|
|
extendDimN xs = {dnames $= (++ toSnocVect' xs)}
|
|
|
|
|
|
|
|
export
|
|
|
|
extendDim : BaseName -> NameContexts d n -> NameContexts (S d) n
|
|
|
|
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-04-15 09:13:01 -04:00
|
|
|
= ExpectedTYPE (NameContexts d n) (Term d n)
|
|
|
|
| ExpectedPi (NameContexts d n) (Term d n)
|
|
|
|
| ExpectedSig (NameContexts d n) (Term d n)
|
|
|
|
| ExpectedEnum (NameContexts d n) (Term d n)
|
|
|
|
| ExpectedEq (NameContexts d n) (Term d n)
|
|
|
|
| ExpectedNat (NameContexts d n) (Term d n)
|
|
|
|
| ExpectedBOX (NameContexts d n) (Term d n)
|
2023-03-13 16:41:57 -04:00
|
|
|
| BadUniverse Universe Universe
|
|
|
|
| TagNotIn TagVal (SortedSet TagVal)
|
2023-03-26 08:41:17 -04:00
|
|
|
| BadCaseEnum (SortedSet TagVal) (SortedSet TagVal)
|
2023-04-15 09:13:01 -04:00
|
|
|
| BadQtys String (TyContext d n) (List (QOutput n, Term d n))
|
2023-03-13 16:41:57 -04:00
|
|
|
|
2023-03-13 22:22:26 -04:00
|
|
|
-- first term arg of ClashT is the type
|
2023-04-01 13:16:43 -04:00
|
|
|
| ClashT (EqContext n) EqMode (Term 0 n) (Term 0 n) (Term 0 n)
|
|
|
|
| ClashTy (EqContext n) EqMode (Term 0 n) (Term 0 n)
|
|
|
|
| ClashE (EqContext n) EqMode (Elim 0 n) (Elim 0 n)
|
2023-03-13 22:22:26 -04:00
|
|
|
| ClashU EqMode Universe Universe
|
2023-04-01 13:16:43 -04:00
|
|
|
| ClashQ Qty Qty
|
2023-03-13 22:22:26 -04:00
|
|
|
| NotInScope Name
|
2023-03-13 16:41:57 -04:00
|
|
|
|
2023-04-01 13:16:43 -04:00
|
|
|
| NotType (TyContext d n) (Term d n)
|
|
|
|
| WrongType (EqContext n) (Term 0 n) (Term 0 n)
|
2023-03-13 16:41:57 -04:00
|
|
|
|
2023-04-15 09:13:01 -04:00
|
|
|
| MissingEnumArm TagVal (List TagVal)
|
|
|
|
|
2023-03-13 16:41:57 -04:00
|
|
|
-- extra context
|
|
|
|
| WhileChecking
|
2023-04-01 13:16:43 -04:00
|
|
|
(TyContext d n) Qty
|
|
|
|
(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
|
2023-04-01 13:16:43 -04:00
|
|
|
(TyContext d n) Qty
|
|
|
|
(Elim d n)
|
|
|
|
Error
|
2023-03-13 16:41:57 -04:00
|
|
|
| WhileComparingT
|
2023-04-01 13:16:43 -04:00
|
|
|
(EqContext n) EqMode
|
|
|
|
(Term 0 n) -- type
|
|
|
|
(Term 0 n) (Term 0 n) -- lhs/rhs
|
|
|
|
Error
|
2023-03-13 16:41:57 -04:00
|
|
|
| WhileComparingE
|
2023-04-01 13:16:43 -04:00
|
|
|
(EqContext n) EqMode
|
|
|
|
(Elim 0 n) (Elim 0 n)
|
|
|
|
Error
|
2023-03-13 16:41:57 -04:00
|
|
|
%name Error err
|
|
|
|
|
|
|
|
public export
|
2023-04-01 13:16:43 -04:00
|
|
|
ErrorEff : Type -> Type
|
|
|
|
ErrorEff = Except Error
|
2023-03-13 16:41:57 -04:00
|
|
|
|
|
|
|
|
|
|
|
||| whether the error is surrounded in some context
|
|
|
|
||| (e.g. "while checking s : A, …")
|
|
|
|
public export
|
2023-04-01 13:16:43 -04:00
|
|
|
isErrorContext : Error -> Bool
|
2023-03-13 16:41:57 -04:00
|
|
|
isErrorContext (WhileChecking {}) = True
|
|
|
|
isErrorContext (WhileCheckingTy {}) = True
|
|
|
|
isErrorContext (WhileInferring {}) = True
|
|
|
|
isErrorContext (WhileComparingT {}) = True
|
|
|
|
isErrorContext (WhileComparingE {}) = True
|
|
|
|
isErrorContext _ = False
|
|
|
|
|
|
|
|
||| remove one layer of context
|
|
|
|
export
|
2023-04-01 13:16:43 -04:00
|
|
|
peelContext : (e : Error) -> (0 _ : So (isErrorContext e)) =>
|
|
|
|
(Error -> Error, Error)
|
2023-03-13 16:41:57 -04:00
|
|
|
peelContext (WhileChecking ctx x s t err) =
|
|
|
|
(WhileChecking ctx x s t, err)
|
|
|
|
peelContext (WhileCheckingTy ctx s k err) =
|
|
|
|
(WhileCheckingTy ctx s k, err)
|
|
|
|
peelContext (WhileInferring ctx x e err) =
|
|
|
|
(WhileInferring ctx x e, err)
|
|
|
|
peelContext (WhileComparingT ctx x s t r err) =
|
|
|
|
(WhileComparingT ctx x s t r, err)
|
|
|
|
peelContext (WhileComparingE ctx x e f err) =
|
|
|
|
(WhileComparingE ctx x e f, err)
|
|
|
|
|
|
|
|
||| 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-03-13 16:41:57 -04:00
|
|
|
explodeContext err =
|
|
|
|
case choose $ isErrorContext err of
|
|
|
|
Left y =>
|
|
|
|
let (f, inner) = peelContext err
|
|
|
|
(fs, root) = explodeContext $ assert_smaller err inner in
|
|
|
|
(f :: fs, root)
|
|
|
|
Right n => ([], err)
|
|
|
|
|
|
|
|
||| 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-04-01 13:16:43 -04:00
|
|
|
parameters {auto _ : Has ErrorEff fs}
|
2023-03-13 16:41:57 -04:00
|
|
|
export %inline
|
2023-04-01 13:16:43 -04:00
|
|
|
expectEqualQ : Qty -> Qty -> Eff fs ()
|
2023-03-13 16:41:57 -04:00
|
|
|
expectEqualQ = expect ClashQ (==)
|
|
|
|
|
|
|
|
export %inline
|
2023-04-01 13:16:43 -04:00
|
|
|
expectCompatQ : Qty -> Qty -> Eff fs ()
|
|
|
|
expectCompatQ = expect ClashQ 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-03-13 16:41:57 -04:00
|
|
|
expectModeU mode = expect (ClashU mode) $ ucmp mode
|
|
|
|
|
2023-03-15 10:54:51 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
prettyMode : EqMode -> Doc HL
|
|
|
|
prettyMode Equal = "equal to"
|
|
|
|
prettyMode Sub = "a subtype of"
|
|
|
|
prettyMode Super = "a supertype of"
|
|
|
|
|
|
|
|
private
|
|
|
|
prettyModeU : EqMode -> Doc HL
|
|
|
|
prettyModeU Equal = "equal to"
|
|
|
|
prettyModeU Sub = "less than or equal to"
|
|
|
|
prettyModeU Super = "greater than or equal to"
|
|
|
|
|
|
|
|
private
|
|
|
|
isTypeInUniverse : Maybe Universe -> Doc HL
|
|
|
|
isTypeInUniverse Nothing = "is a type"
|
|
|
|
isTypeInUniverse (Just k) = "is a type in universe" <++> prettyUniverse k
|
|
|
|
|
2023-04-01 13:16:43 -04:00
|
|
|
parameters (unicode : Bool)
|
2023-03-15 10:54:51 -04:00
|
|
|
private
|
2023-04-15 09:13:01 -04:00
|
|
|
termn : NameContexts d n -> Term d n -> Doc HL
|
|
|
|
termn ctx = hang 4 . prettyTerm unicode ctx.dnames ctx.tnames
|
2023-03-15 10:54:51 -04:00
|
|
|
|
|
|
|
private
|
2023-04-15 09:13:01 -04:00
|
|
|
dstermn : {s : Nat} -> NameContexts d n -> DScopeTermN s d n -> Doc HL
|
|
|
|
dstermn ctx (S i t) = termn (extendDimN i ctx) t.term
|
2023-03-15 10:54:51 -04:00
|
|
|
|
|
|
|
private
|
2023-04-15 09:13:01 -04:00
|
|
|
filterSameQtys : NContext n -> List (QOutput n, z) ->
|
|
|
|
Exists $ \n' => (NContext 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
|
2023-03-15 10:54:51 -04:00
|
|
|
in
|
2023-04-15 09:13:01 -04:00
|
|
|
if allSame qs
|
|
|
|
then Evidence l (ns, qts)
|
|
|
|
else Evidence (S l) (ns :< n, zipWith (\(qs, t), q => (qs :< q, t)) qts qs)
|
2023-03-15 10:54:51 -04:00
|
|
|
where
|
2023-04-01 13:16:43 -04:00
|
|
|
allSame : List Qty -> Bool
|
2023-03-15 10:54:51 -04:00
|
|
|
allSame [] = True
|
|
|
|
allSame (q :: qs) = all (== q) qs
|
|
|
|
|
2023-04-15 09:13:01 -04:00
|
|
|
private
|
|
|
|
printCaseQtys : TyContext d n ->
|
|
|
|
NContext n' -> List (QOutput n', Term d n) ->
|
|
|
|
List (Doc HL)
|
|
|
|
printCaseQtys ctx ns qts =
|
|
|
|
let Evidence l (ns, qts) = filterSameQtys ns qts in
|
|
|
|
map (line ns) qts
|
|
|
|
where
|
|
|
|
commaList : PrettyHL a => Context' a l -> Doc HL
|
|
|
|
commaList = hseparate comma . map (pretty0 unicode) . toList'
|
|
|
|
|
|
|
|
line : NContext l -> (QOutput l, Term d n) -> Doc HL
|
|
|
|
line ns (qs, t) =
|
|
|
|
"-" <++> asep ["the term", termn ctx.names t,
|
|
|
|
"uses variables", commaList $ TV <$> ns,
|
|
|
|
"with quantities", commaList qs]
|
2023-03-15 10:54:51 -04:00
|
|
|
|
|
|
|
-- [todo] only show some contexts, probably
|
|
|
|
export
|
2023-04-01 13:16:43 -04:00
|
|
|
prettyError : (showContext : Bool) -> Error -> Doc HL
|
2023-03-15 10:54:51 -04:00
|
|
|
prettyError showContext = \case
|
|
|
|
ExpectedTYPE ctx s =>
|
2023-04-15 09:13:01 -04:00
|
|
|
sep ["expected a type universe, but got", termn ctx s]
|
2023-03-15 10:54:51 -04:00
|
|
|
|
|
|
|
ExpectedPi ctx s =>
|
2023-04-15 09:13:01 -04:00
|
|
|
sep ["expected a function type, but got", termn ctx s]
|
2023-03-15 10:54:51 -04:00
|
|
|
|
|
|
|
ExpectedSig ctx s =>
|
2023-04-15 09:13:01 -04:00
|
|
|
sep ["expected a pair type, but got", termn ctx s]
|
2023-03-15 10:54:51 -04:00
|
|
|
|
|
|
|
ExpectedEnum ctx s =>
|
2023-04-15 09:13:01 -04:00
|
|
|
sep ["expected an enumeration type, but got", termn ctx s]
|
2023-03-15 10:54:51 -04:00
|
|
|
|
|
|
|
ExpectedEq ctx s =>
|
2023-04-15 09:13:01 -04:00
|
|
|
sep ["expected an equality type, but got", termn ctx s]
|
2023-03-15 10:54:51 -04:00
|
|
|
|
2023-03-31 13:11:35 -04:00
|
|
|
ExpectedNat ctx s {d, n} =>
|
2023-04-01 13:16:43 -04:00
|
|
|
sep ["expected the type", pretty0 unicode $ Nat {d, n},
|
2023-04-15 09:13:01 -04:00
|
|
|
"but got", termn ctx s]
|
2023-03-31 13:11:35 -04:00
|
|
|
|
|
|
|
ExpectedBOX ctx s =>
|
2023-04-15 09:13:01 -04:00
|
|
|
sep ["expected a box type, but got", termn ctx s]
|
2023-03-26 08:40:54 -04:00
|
|
|
|
2023-03-15 10:54:51 -04:00
|
|
|
BadUniverse k l =>
|
|
|
|
sep ["the universe level", prettyUniverse k,
|
|
|
|
"is not strictly less than", prettyUniverse l]
|
|
|
|
|
|
|
|
TagNotIn tag set =>
|
|
|
|
sep [sep ["tag", prettyTag tag, "is not contained in"],
|
2023-04-15 09:13:01 -04:00
|
|
|
termn empty (Enum set)]
|
2023-03-15 10:54:51 -04:00
|
|
|
|
2023-03-26 08:41:17 -04:00
|
|
|
BadCaseEnum type arms =>
|
2023-04-15 09:13:01 -04:00
|
|
|
sep ["case expression has head of type", termn empty (Enum type),
|
|
|
|
"but cases for", termn empty (Enum arms)]
|
2023-03-26 08:41:17 -04:00
|
|
|
|
2023-04-15 09:13:01 -04:00
|
|
|
BadQtys what ctx arms =>
|
2023-03-15 10:54:51 -04:00
|
|
|
hang 4 $ sep $
|
2023-04-15 09:13:01 -04:00
|
|
|
("inconsistent variable usage in" <++> fromString what) ::
|
|
|
|
printCaseQtys ctx ctx.tnames arms
|
2023-03-15 10:54:51 -04:00
|
|
|
|
|
|
|
ClashT ctx mode ty s t =>
|
|
|
|
inEContext ctx $
|
2023-04-15 09:13:01 -04:00
|
|
|
sep ["the term", termn ctx.names0 s,
|
|
|
|
hsep ["is not", prettyMode mode], termn ctx.names0 t,
|
|
|
|
"at type", termn ctx.names0 ty]
|
2023-03-15 10:54:51 -04:00
|
|
|
|
|
|
|
ClashTy ctx mode a b =>
|
|
|
|
inEContext ctx $
|
2023-04-15 09:13:01 -04:00
|
|
|
sep ["the type", termn ctx.names0 a,
|
|
|
|
hsep ["is not", prettyMode mode], termn ctx.names0 b]
|
2023-03-15 10:54:51 -04:00
|
|
|
|
|
|
|
ClashE ctx mode e f =>
|
|
|
|
inEContext ctx $
|
2023-04-15 09:13:01 -04:00
|
|
|
sep ["the term", termn ctx.names0 $ E e,
|
|
|
|
hsep ["is not", prettyMode mode], termn ctx.names0 $ E f]
|
2023-03-15 10:54:51 -04:00
|
|
|
|
|
|
|
ClashU mode k l =>
|
|
|
|
sep ["the universe level", prettyUniverse k,
|
|
|
|
hsep ["is not", prettyMode mode], prettyUniverse l]
|
|
|
|
|
|
|
|
ClashQ pi rh =>
|
|
|
|
sep ["the quantity", pretty0 unicode pi,
|
|
|
|
"is not equal to", pretty0 unicode rh]
|
|
|
|
|
|
|
|
NotInScope x =>
|
|
|
|
hsep [hl' Free $ pretty0 unicode x, "is not in scope"]
|
|
|
|
|
|
|
|
NotType ctx s =>
|
|
|
|
inTContext ctx $
|
2023-04-15 09:13:01 -04:00
|
|
|
sep ["the term", termn ctx.names s, "is not a type"]
|
2023-03-15 10:54:51 -04:00
|
|
|
|
|
|
|
WrongType ctx ty s =>
|
|
|
|
inEContext ctx $
|
2023-04-15 09:13:01 -04:00
|
|
|
sep ["the term", termn ctx.names0 s,
|
|
|
|
"cannot have type", termn ctx.names0 ty]
|
|
|
|
|
|
|
|
MissingEnumArm tag tags =>
|
|
|
|
sep [hsep ["the tag", hl Tag $ pretty tag, "is not contained in"],
|
|
|
|
termn empty $ Enum $ fromList tags]
|
2023-03-15 10:54:51 -04:00
|
|
|
|
|
|
|
WhileChecking ctx pi s a err =>
|
|
|
|
vsep [inTContext ctx $
|
2023-04-15 09:13:01 -04:00
|
|
|
sep ["while checking", termn ctx.names s,
|
|
|
|
"has type", termn ctx.names a,
|
2023-03-15 10:54:51 -04:00
|
|
|
hsep ["with quantity", pretty0 unicode pi]],
|
|
|
|
prettyError showContext err]
|
|
|
|
|
|
|
|
WhileCheckingTy ctx a k err =>
|
|
|
|
vsep [inTContext ctx $
|
2023-04-15 09:13:01 -04:00
|
|
|
sep ["while checking", termn ctx.names a,
|
2023-03-15 10:54:51 -04:00
|
|
|
isTypeInUniverse k],
|
|
|
|
prettyError showContext err]
|
|
|
|
|
|
|
|
WhileInferring ctx pi e err =>
|
|
|
|
vsep [inTContext ctx $
|
2023-04-15 09:13:01 -04:00
|
|
|
sep ["while inferring the type of", termn ctx.names $ E e,
|
2023-03-15 10:54:51 -04:00
|
|
|
hsep ["with quantity", pretty0 unicode pi]],
|
|
|
|
prettyError showContext err]
|
|
|
|
|
2023-03-26 10:15:30 -04:00
|
|
|
WhileComparingT ctx mode a s t err =>
|
2023-03-15 10:54:51 -04:00
|
|
|
vsep [inEContext ctx $
|
2023-04-15 09:13:01 -04:00
|
|
|
sep ["while checking that", termn ctx.names0 s,
|
|
|
|
hsep ["is", prettyMode mode], termn ctx.names0 t,
|
|
|
|
"at type", termn ctx.names0 a],
|
2023-03-15 10:54:51 -04:00
|
|
|
prettyError showContext err]
|
|
|
|
|
|
|
|
WhileComparingE ctx mode e f err =>
|
|
|
|
vsep [inEContext ctx $
|
2023-04-15 09:13:01 -04:00
|
|
|
sep ["while checking that", termn ctx.names0 $ E e,
|
|
|
|
hsep ["is", prettyMode mode], termn ctx.names0 $ E f],
|
2023-03-15 10:54:51 -04:00
|
|
|
prettyError showContext err]
|
|
|
|
where
|
2023-04-01 13:16:43 -04:00
|
|
|
inTContext : TyContext d n -> Doc HL -> Doc HL
|
2023-03-15 10:54:51 -04:00
|
|
|
inTContext ctx doc =
|
2023-03-26 10:10:39 -04:00
|
|
|
if showContext && not (null ctx) then
|
2023-03-15 10:54:51 -04:00
|
|
|
vsep [sep ["in context", prettyTyContext unicode ctx], doc]
|
|
|
|
else doc
|
|
|
|
|
2023-04-01 13:16:43 -04:00
|
|
|
inEContext : EqContext n -> Doc HL -> Doc HL
|
2023-03-15 10:54:51 -04:00
|
|
|
inEContext ctx doc =
|
2023-03-26 10:10:39 -04:00
|
|
|
if showContext && not (null ctx) then
|
2023-03-15 10:54:51 -04:00
|
|
|
vsep [sep ["in context", prettyEqContext unicode ctx], doc]
|
|
|
|
else doc
|