{-# LANGUAGE DataKinds         #-}
{-# LANGUAGE GADTs             #-}
{-# LANGUAGE LambdaCase        #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes        #-}
-- | Type errors, warnings and holes of the new checker, and how they are
-- shown.
--
-- An error is captured at its own scope, with the context it was raised
-- in, so that it can be shown with the names of that context. A warning
-- and a hole are recorded already rendered, so they are independent of the
-- scope they came from.
module Rzk.Check.Error
  ( TypeError (..)
  , typeErrorTag
  , ErrorInContext (..)
  , errorTag
  , errorLocation
  , ppTypeError
  , ppErrorInContext
  , ppAction
  , ppTope
  , ppPoint
    -- * Warnings
  , Warning (..)
  , warningTag
  , warningLocation
  , ppWarning
    -- * Hints
  , Hint (..)
  , hintTag
  , hintLocation
  , ppHint
    -- * Holes
  , HoleInfo (..)
  , ppHoleInfo
  ) where

import           Control.Monad.Foil       (Distinct)
import qualified Control.Monad.Foil       as Foil
import           Data.List                (intercalate)

import           Language.Rzk.Foil.Names  (TModality (..), VarIdent,
                                           fromVarIdent, ppVarIdentWithLocation)
import qualified Language.Rzk.Syntax      as Rzk
import           Rzk.Check.Context
import           Rzk.Check.Display
import           Rzk.Check.Syntax
import           Rzk.Check.Topes

data TypeError n
  = TypeErrorOther String
    -- ^ a diagnostic that does not yet have a dedicated constructor
  | TypeErrorUnsupported String
    -- ^ a construct the new checker does not cover yet
  | TypeErrorUnify (Core n) (Value n) (Value n)
    -- ^ the term, the expected type and its actual type
  | TypeErrorUnifyTerms (Value n) (Value n)
    -- ^ the expected and actual values that are not convertible
  | TypeErrorNotPair (Core n) (Value n)
    -- ^ a projected term and its non-pair type
  | TypeErrorNotFunction (Core n) (Value n)
    -- ^ an applied term and its non-function type
  | TypeErrorUnexpectedLambda (Raw n) (Value n)
    -- ^ a λ checked against a non-function type
  | TypeErrorUnexpectedPair (Raw n) (Value n)
    -- ^ a pair checked against a non-pair type
  | TypeErrorUnexpectedRefl (Raw n) (Value n)
    -- ^ reflexivity checked against a non-identity type
  | TypeErrorCannotInferBareLambda (Raw n)
    -- ^ a λ without a parameter annotation in inference position
  | TypeErrorCannotInferBareRefl (Raw n)
    -- ^ reflexivity without an element annotation in inference position
  | TypeErrorCannotInferHole (Raw n)
    -- ^ a user hole whose type cannot be inferred
  | TypeErrorUnsolvedHole (Maybe VarIdent) (Value n)
    -- ^ a user hole and the goal it leaves unsolved in strict mode
  | TypeErrorUndefined VarIdent
    -- ^ an unresolved surface identifier
  | TypeErrorTopeNotSatisfied [Tope n] (Tope n)
    -- ^ context topes and the goal they do not entail
  | TypeErrorTopeContextDisjoint (Tope n) [Tope n]
    -- ^ a tope disjoint from the listed context
  | TypeErrorInvalidArgumentType (Raw n) (Value n)
    -- ^ a function parameter and its invalid kind
  | TypeErrorDuplicateTopLevel [VarIdent] VarIdent
    -- ^ previous definitions and the duplicate name
  | TypeErrorUnusedVariable (Foil.Name n) (Value n)
    -- ^ an unused section assumption and its type
  | TypeErrorUnusedUsedVariables [Foil.Name n] (Foil.Name n)
    -- ^ assumptions listed as used but not needed by the declaration
  | TypeErrorImplicitAssumption (Foil.Name n, Value n) (Foil.Name n)
    -- ^ an assumption used implicitly, its type, and the declaration using it
  | TypeErrorNotIntervalCube String (Value n) (Value n)
    -- ^ an operation and the two operand types, which are not interval cubes
  | TypeErrorRepeatedBinder VarIdent [VarIdent]
    -- ^ a repeated binder and its previous occurrences

-- | The constructor name: the stable code of a diagnostic, and the tag the
-- fixtures pin.
typeErrorTag :: TypeError n -> String
typeErrorTag :: forall (n :: S). TypeError n -> String
typeErrorTag = \case
  TypeErrorOther{}                -> String
"TypeErrorOther"
  TypeErrorUnsupported{}          -> String
"TypeErrorUnsupported"
  TypeErrorUnify{}                -> String
"TypeErrorUnify"
  TypeErrorUnifyTerms{}           -> String
"TypeErrorUnifyTerms"
  TypeErrorNotPair{}              -> String
"TypeErrorNotPair"
  TypeErrorNotFunction{}          -> String
"TypeErrorNotFunction"
  TypeErrorUnexpectedLambda{}     -> String
"TypeErrorUnexpectedLambda"
  TypeErrorUnexpectedPair{}       -> String
"TypeErrorUnexpectedPair"
  TypeErrorUnexpectedRefl{}       -> String
"TypeErrorUnexpectedRefl"
  TypeErrorCannotInferBareLambda{} -> String
"TypeErrorCannotInferBareLambda"
  TypeErrorCannotInferBareRefl{}  -> String
"TypeErrorCannotInferBareRefl"
  TypeErrorCannotInferHole{}      -> String
"TypeErrorCannotInferHole"
  TypeErrorUnsolvedHole{}         -> String
"TypeErrorUnsolvedHole"
  TypeErrorUndefined{}            -> String
"TypeErrorUndefined"
  TypeErrorTopeNotSatisfied{}     -> String
"TypeErrorTopeNotSatisfied"
  TypeErrorTopeContextDisjoint{}  -> String
"TypeErrorTopeContextDisjoint"
  TypeErrorInvalidArgumentType{}  -> String
"TypeErrorInvalidArgumentType"
  TypeErrorDuplicateTopLevel{}    -> String
"TypeErrorDuplicateTopLevel"
  TypeErrorUnusedVariable{}       -> String
"TypeErrorUnusedVariable"
  TypeErrorUnusedUsedVariables{}  -> String
"TypeErrorUnusedUsedVariables"
  TypeErrorImplicitAssumption{}   -> String
"TypeErrorImplicitAssumption"
  TypeErrorNotIntervalCube{}      -> String
"TypeErrorNotIntervalCube"
  TypeErrorRepeatedBinder{}       -> String
"TypeErrorRepeatedBinder"

-- | An error with the context it was raised in. The scope is existential:
-- the context says what the names are called.
data ErrorInContext where
  ErrorInContext :: Distinct n => Context n -> TypeError n -> ErrorInContext

errorTag :: ErrorInContext -> String
errorTag :: ErrorInContext -> String
errorTag (ErrorInContext Context n
_ TypeError n
err) = TypeError n -> String
forall (n :: S). TypeError n -> String
typeErrorTag TypeError n
err

errorLocation :: ErrorInContext -> Maybe LocationInfo
errorLocation :: ErrorInContext -> Maybe LocationInfo
errorLocation (ErrorInContext Context n
ctx TypeError n
_) = Context n -> Maybe LocationInfo
forall (n :: S). Context n -> Maybe LocationInfo
ctxLocation Context n
ctx

ppTypeError :: Distinct n => Context n -> TypeError n -> String
ppTypeError :: forall (n :: S). Distinct n => Context n -> TypeError n -> String
ppTypeError Context n
ctx = \case
  TypeErrorOther String
msg -> String
msg
  TypeErrorUnsupported String
what -> String
"not supported by the new checker yet: " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
what
  TypeErrorUnify Core n
term Value n
expected Value n
actual -> [String] -> String
block
    [ String
"cannot unify expected type"
    , String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Value n -> String
ppV Value n
expected
    , String
"with actual type"
    , String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Value n -> String
ppV Value n
actual
    , String
"for term"
    , String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Core n -> String
ppC Core n
term ]
  TypeErrorUnifyTerms Value n
expected Value n
actual -> [String] -> String
block
    [ String
"cannot unify term"
    , String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Value n -> String
ppV Value n
expected
    , String
"with term"
    , String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Value n -> String
ppV Value n
actual ]
  TypeErrorNotPair Core n
term Value n
ty -> [String] -> String
block
    [ String
"expected a cube product or dependent pair"
    , String
"but got type"
    , String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Value n -> String
ppV Value n
ty
    , String
"for term"
    , String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Core n -> String
ppC Core n
term ]
  TypeErrorNotFunction Core n
term Value n
ty -> [String] -> String
block
    [ String
"expected a function or extension type"
    , String
"but got type"
    , String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Value n -> String
ppV Value n
ty
    , String
"for term"
    , String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Core n -> String
ppC Core n
term ]
  TypeErrorUnexpectedLambda Raw n
term Value n
ty -> [String] -> String
block
    [ String
"unexpected lambda abstraction"
    , String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Raw n -> String
ppR Raw n
term
    , String
"when typechecking against a non-function type"
    , String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Value n -> String
ppV Value n
ty ]
  TypeErrorUnexpectedPair Raw n
term Value n
ty -> [String] -> String
block
    [ String
"unexpected pair"
    , String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Raw n -> String
ppR Raw n
term
    , String
"when typechecking against a type that is not a product or a dependent sum"
    , String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Value n -> String
ppV Value n
ty ]
  TypeErrorUnexpectedRefl Raw n
term Value n
ty -> [String] -> String
block
    [ String
"unexpected refl"
    , String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Raw n -> String
ppR Raw n
term
    , String
"when typechecking against a type that is not an identity type"
    , String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Value n -> String
ppV Value n
ty ]
  TypeErrorCannotInferBareLambda Raw n
term -> [String] -> String
block
    [ String
"cannot infer the type of the argument"
    , String
"in lambda abstraction"
    , String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Raw n -> String
ppR Raw n
term ]
  TypeErrorCannotInferBareRefl Raw n
term -> [String] -> String
block
    [ String
"cannot infer the type of term"
    , String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Raw n -> String
ppR Raw n
term ]
  TypeErrorCannotInferHole Raw n
term -> [String] -> String
block
    [ String
"cannot infer the type of a hole"
    , String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Raw n -> String
ppR Raw n
term
    , String
"a hole is only allowed where its type is already known (checking position)" ]
  TypeErrorUnsolvedHole Maybe VarIdent
mname Value n
goal -> [String] -> String
block
    [ String
"found an unsolved hole" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String -> (VarIdent -> String) -> Maybe VarIdent -> String
forall b a. b -> (a -> b) -> Maybe a -> b
maybe String
"" (\VarIdent
name -> String
" ?" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> VarIdent -> String
forall a. Show a => a -> String
show VarIdent
name) Maybe VarIdent
mname
    , String
"expected type (goal):"
    , String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Value n -> String
ppV Value n
goal ]
  TypeErrorUndefined VarIdent
var -> String
"undefined variable: " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> VarIdent -> String
forall a. Show a => a -> String
show VarIdent
var
  TypeErrorTopeNotSatisfied [Tope n]
topes Tope n
tope -> [String] -> String
block
    [ String
"local context is not included in (does not entail) the tope"
    , String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Naming n -> Tope n -> String
forall (n :: S). Naming n -> Tope n -> String
ppTope Naming n
naming Tope n
tope
    , String
"in local context (normalised)"
    , String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
"\n" ((Tope n -> String) -> [Tope n] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map ((String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<>) (String -> String) -> (Tope n -> String) -> Tope n -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Naming n -> Tope n -> String
forall (n :: S). Naming n -> Tope n -> String
ppTope Naming n
naming) [Tope n]
topes) ]
  TypeErrorTopeContextDisjoint Tope n
tope [Tope n]
topes -> [String] -> String
block
    [ String
"the tope"
    , String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Naming n -> Tope n -> String
forall (n :: S). Naming n -> Tope n -> String
ppTope Naming n
naming Tope n
tope
    , String
"is disjoint from the local tope context (their conjunction is the empty tope ⊥),"
    , String
"so this restriction face or recOR branch is vacuous everywhere"
    , String
"in local context (normalised)"
    , String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
"\n" ((Tope n -> String) -> [Tope n] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map ((String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<>) (String -> String) -> (Tope n -> String) -> Tope n -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Naming n -> Tope n -> String
forall (n :: S). Naming n -> Tope n -> String
ppTope Naming n
naming) [Tope n]
topes) ]
  TypeErrorInvalidArgumentType Raw n
argType Value n
argKind -> [String] -> String
block
    [ String
"invalid function parameter type"
    , String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Raw n -> String
ppR Raw n
argType
    , String
"function parameter can be a cube, a shape, or a type"
    , String
"but given parameter type has type"
    , String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Value n -> String
ppV Value n
argKind ]
  TypeErrorDuplicateTopLevel [VarIdent]
previous VarIdent
lastName -> [String] -> String
block
    [ String
"duplicate top-level definition"
    , String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> VarIdent -> String
ppVarIdentWithLocation VarIdent
lastName
    , String
"previous top-level definitions found at"
    , String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
"\n" [ String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> VarIdent -> String
ppVarIdentWithLocation VarIdent
name | VarIdent
name <- [VarIdent]
previous ] ]
  TypeErrorUnusedVariable Name n
name Value n
ty -> [String] -> String
block
    [ String
"unused variable"
    , String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Naming n -> Name n -> String
forall (n :: S). Naming n -> Name n -> String
ppName Naming n
naming Name n
name String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" : " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Value n -> String
ppV Value n
ty ]
  TypeErrorUnusedUsedVariables [Name n]
vars Name n
name -> [String] -> String
block
    [ String
"unused variables"
    , String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> [String] -> String
unwords ((Name n -> String) -> [Name n] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map (Naming n -> Name n -> String
forall (n :: S). Naming n -> Name n -> String
ppName Naming n
naming) [Name n]
vars)
    , String
"declared as used in definition of"
    , String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Naming n -> Name n -> String
forall (n :: S). Naming n -> Name n -> String
ppName Naming n
naming Name n
name ]
  TypeErrorImplicitAssumption (Name n
a, Value n
aType) Name n
name -> [String] -> String
block
    [ String
"implicit assumption"
    , String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Naming n -> Name n -> String
forall (n :: S). Naming n -> Name n -> String
ppName Naming n
naming Name n
a String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" : " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Value n -> String
ppV Value n
aType
    , String
"used in definition of"
    , String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Naming n -> Name n -> String
forall (n :: S). Naming n -> Name n -> String
ppName Naming n
naming Name n
name ]
  TypeErrorNotIntervalCube String
op Value n
lType Value n
rType -> [String] -> String
block
    [ String
op String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" expects both points in the same interval cube (2 or 𝕀)"
    , String
"but got a point of type"
    , String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Value n -> String
ppV Value n
lType
    , String
"and a point of type"
    , String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Value n -> String
ppV Value n
rType ]
  TypeErrorRepeatedBinder VarIdent
name [VarIdent]
previous -> [String] -> String
block
    [ VarIdent -> String
forall a. Show a => a -> String
show VarIdent
name String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" is bound multiple times in one binder group"
    , String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> VarIdent -> String
ppVarIdentWithLocation VarIdent
name
    , String
"already bound at"
    , String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
"\n" [ String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> VarIdent -> String
ppVarIdentWithLocation VarIdent
prev | VarIdent
prev <- [VarIdent]
previous ] ]
  where
    naming :: Naming n
naming = Context n -> Naming n
forall (n :: S). Context n -> Naming n
namingOfContext Context n
ctx
    ppV :: Value n -> String
ppV = Context n -> Value n -> String
forall (n :: S). Distinct n => Context n -> Value n -> String
ppValueIn Context n
ctx
    ppC :: Core n -> String
ppC = Naming n -> Core n -> String
forall (n :: S). Distinct n => Naming n -> Core n -> String
ppCore Naming n
naming
    ppR :: Raw n -> String
ppR = Naming n -> Core n -> String
forall (n :: S). Distinct n => Naming n -> Core n -> String
ppCore Naming n
naming (Core n -> String) -> (Raw n -> Core n) -> Raw n -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Raw n -> Core n
forall (n :: S). Raw n -> Core n
stripPositions
    block :: [String] -> String
block = String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
"\n"

-- | An error, with the context it was raised in.
ppErrorInContext :: ErrorInContext -> String
ppErrorInContext :: ErrorInContext -> String
ppErrorInContext (ErrorInContext Context n
ctx TypeError n
err) = String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
"\n"
  [ Context n -> TypeError n -> String
forall (n :: S). Distinct n => Context n -> TypeError n -> String
ppTypeError Context n
ctx TypeError n
err
  , String
""
  , Context n -> String
forall (n :: S). Distinct n => Context n -> String
ppContext Context n
ctx ]

-- | The context an error was raised in: where, the tope context, the stack
-- of judgements, and the hypotheses.
ppContext :: Distinct n => Context n -> String
ppContext :: forall (n :: S). Distinct n => Context n -> String
ppContext Context n
ctx = String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
"\n" ([String] -> String) -> [String] -> String
forall a b. (a -> b) -> a -> b
$ (String -> Bool) -> [String] -> [String]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> (String -> Bool) -> String -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null)
  [ case Context n -> Maybe LocationInfo
forall (n :: S). Context n -> Maybe LocationInfo
ctxLocation Context n
ctx of
      Just (LocationInfo (Just String
path) (Just Int
lineNo) (Just Int
col)) ->
        String
path String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" (line " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Int -> String
forall a. Show a => a -> String
show Int
lineNo String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
", column " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Int -> String
forall a. Show a => a -> String
show Int
col String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"):"
      Just (LocationInfo (Just String
path) (Just Int
lineNo) Maybe Int
Nothing) ->
        String
path String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" (line " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Int -> String
forall a. Show a => a -> String
show Int
lineNo String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"):"
      Just (LocationInfo (Just String
path) Maybe Int
_ Maybe Int
_) -> String
path String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
":"
      Maybe LocationInfo
_ -> String
""
  , case (ModalTope n -> Bool) -> [ModalTope n] -> [ModalTope n]
forall a. (a -> Bool) -> [a] -> [a]
filter ((Tope n -> Tope n -> Bool
forall a. Eq a => a -> a -> Bool
/= Tope n
forall (n :: S). Tope n
TTop) (Tope n -> Bool) -> (ModalTope n -> Tope n) -> ModalTope n -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ModalTope n -> Tope n
forall (n :: S). ModalTope n -> Tope n
tTope) (TopeContext n -> [ModalTope n]
forall (n :: S). TopeContext n -> [ModalTope n]
contextTopes (Context n -> TopeContext n
forall (n :: S). Context n -> TopeContext n
ctxTopes Context n
ctx)) of
      [] -> String
"Local tope context is unrestricted (⊤)."
      [ModalTope n]
topes -> String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
"\n" ([String] -> String) -> [String] -> String
forall a b. (a -> b) -> a -> b
$ String
"Local tope context:" String -> [String] -> [String]
forall a. a -> [a] -> [a]
:
        [ String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> (if ModalTope n -> Bool
forall (n :: S). ModalTope n -> Bool
isAccessible ModalTope n
mt then String
"" else String
"× ") String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Naming n -> Tope n -> String
forall (n :: S). Naming n -> Tope n -> String
ppTope Naming n
naming (ModalTope n -> Tope n
forall (n :: S). ModalTope n -> Tope n
tTope ModalTope n
mt)
        | ModalTope n
mt <- [ModalTope n]
topes ]
  , String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
"\n"
      [ String
"when " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Context n -> Action n -> String
forall (n :: S). Distinct n => Context n -> Action n -> String
ppAction Context n
ctx Action n
action
      | Action n
action <- [Action n] -> [Action n]
forall a. [a] -> [a]
reverse (Diag n -> [Action n]
forall (n :: S). Diag n -> [Action n]
diagActions (Context n -> Diag n
forall (n :: S). Context n -> Diag n
ctxDiag Context n
ctx)) ]
  , String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
"\n" ([String] -> String) -> [String] -> String
forall a b. (a -> b) -> a -> b
$ String
"Definitions in context:" String -> [String] -> [String]
forall a. a -> [a] -> [a]
:
      [ String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Naming n -> Name n -> String
forall (n :: S). Naming n -> Name n -> String
ppName Naming n
naming Name n
name String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" : " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Context n -> Value n -> String
forall (n :: S). Distinct n => Context n -> Value n -> String
ppValueIn Context n
ctx (Entry n -> Value n
forall (n :: S). Entry n -> Value n
entryType Entry n
entry)
      | Name n
name <- Context n -> [Name n]
forall (n :: S). Context n -> [Name n]
ctxBound Context n
ctx, let entry :: Entry n
entry = Name n -> Context n -> Entry n
forall (n :: S). Name n -> Context n -> Entry n
lookupEntry Name n
name Context n
ctx ]
  ]
  where
    naming :: Naming n
naming = Context n -> Naming n
forall (n :: S). Context n -> Naming n
namingOfContext Context n
ctx

ppAction :: Distinct n => Context n -> Action n -> String
ppAction :: forall (n :: S). Distinct n => Context n -> Action n -> String
ppAction Context n
ctx = \case
  ActionCheck Raw n
term Value n
ty -> [String] -> String
block
    [ String
"typechecking"
    , String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Naming n -> Core n -> String
forall (n :: S). Distinct n => Naming n -> Core n -> String
ppCore Naming n
naming (Raw n -> Core n
forall (n :: S). Raw n -> Core n
stripPositions Raw n
term)
    , String
"against type"
    , String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Context n -> Value n -> String
forall (n :: S). Distinct n => Context n -> Value n -> String
ppValueIn Context n
ctx Value n
ty ]
  ActionInfer Raw n
term -> [String] -> String
block
    [ String
"inferring type for term"
    , String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Naming n -> Core n -> String
forall (n :: S). Distinct n => Naming n -> Core n -> String
ppCore Naming n
naming (Raw n -> Core n
forall (n :: S). Raw n -> Core n
stripPositions Raw n
term) ]
  ActionConvert Value n
expected Value n
actual -> [String] -> String
block
    [ String
"comparing expected"
    , String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Context n -> Value n -> String
forall (n :: S). Distinct n => Context n -> Value n -> String
ppValueIn Context n
ctx Value n
expected
    , String
"with actual"
    , String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Context n -> Value n -> String
forall (n :: S). Distinct n => Context n -> Value n -> String
ppValueIn Context n
ctx Value n
actual ]
  ActionEntails [Tope n]
topes Tope n
tope -> [String] -> String
block
    [ String
"checking if local context"
    , String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
"\n" ((Tope n -> String) -> [Tope n] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map ((String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<>) (String -> String) -> (Tope n -> String) -> Tope n -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Naming n -> Tope n -> String
forall (n :: S). Naming n -> Tope n -> String
ppTope Naming n
naming) [Tope n]
topes)
    , String
"is included in (entails) the tope"
    , String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Naming n -> Tope n -> String
forall (n :: S). Naming n -> Tope n -> String
ppTope Naming n
naming Tope n
tope ]
  ActionEntailsUnion [Tope n]
topes [Tope n]
union -> [String] -> String
block
    [ String
"checking if local context"
    , String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
"\n" ((Tope n -> String) -> [Tope n] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map ((String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<>) (String -> String) -> (Tope n -> String) -> Tope n -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Naming n -> Tope n -> String
forall (n :: S). Naming n -> Tope n -> String
ppTope Naming n
naming) [Tope n]
topes)
    , String
"is included in (entails) the union of the topes"
    , String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
"\n" ((Tope n -> String) -> [Tope n] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map ((String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<>) (String -> String) -> (Tope n -> String) -> Tope n -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Naming n -> Tope n -> String
forall (n :: S). Naming n -> Tope n -> String
ppTope Naming n
naming) [Tope n]
union) ]
  ActionCheckLetValue Maybe VarIdent
mname ->
    String
"checking the local definition " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String -> (VarIdent -> String) -> Maybe VarIdent -> String
forall b a. b -> (a -> b) -> Maybe a -> b
maybe String
"_" (VarIdent -> String
forall a. Print a => a -> String
Rzk.printTree (VarIdent -> String)
-> (VarIdent -> VarIdent) -> VarIdent -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. VarIdent -> VarIdent
fromVarIdent) Maybe VarIdent
mname
  ActionCloseSection Maybe String
Nothing -> String
"closing the file and collecting assumptions (variables)"
  ActionCloseSection (Just String
name) -> String
"closing #section " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
name String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" and collecting assumptions (variables)"
  where
    naming :: Naming n
naming = Context n -> Naming n
forall (n :: S). Context n -> Naming n
namingOfContext Context n
ctx
    block :: [String] -> String
block = String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
"\n"

-- | A tope of the solver, shown with the context's names.
ppTope :: Naming n -> Tope n -> String
ppTope :: forall (n :: S). Naming n -> Tope n -> String
ppTope Naming n
naming = Tope n -> String
go
  where
    go :: Tope n -> String
go = \case
      Tope n
TTop -> String
"⊤"
      Tope n
TBot -> String
"⊥"
      TEq Point n
x Point n
y -> Naming n -> Point n -> String
forall (n :: S). Naming n -> Point n -> String
ppPoint Naming n
naming Point n
x String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" ≡ " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Naming n -> Point n -> String
forall (n :: S). Naming n -> Point n -> String
ppPoint Naming n
naming Point n
y
      TLeq Point n
x Point n
y -> Naming n -> Point n -> String
forall (n :: S). Naming n -> Point n -> String
ppPoint Naming n
naming Point n
x String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" ≤ " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Naming n -> Point n -> String
forall (n :: S). Naming n -> Point n -> String
ppPoint Naming n
naming Point n
y
      TAnd Tope n
l Tope n
r -> String -> String
forall {t :: * -> *}.
(Foldable t, Semigroup (t Char), IsString (t Char)) =>
t Char -> t Char
paren (Tope n -> String
go Tope n
l) String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" ∧ " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String -> String
forall {t :: * -> *}.
(Foldable t, Semigroup (t Char), IsString (t Char)) =>
t Char -> t Char
paren (Tope n -> String
go Tope n
r)
      TOr Tope n
l Tope n
r -> String -> String
forall {t :: * -> *}.
(Foldable t, Semigroup (t Char), IsString (t Char)) =>
t Char -> t Char
paren (Tope n -> String
go Tope n
l) String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" ∨ " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String -> String
forall {t :: * -> *}.
(Foldable t, Semigroup (t Char), IsString (t Char)) =>
t Char -> t Char
paren (Tope n -> String
go Tope n
r)
      TInv Tope n
t -> String
"inv " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String -> String
forall {t :: * -> *}.
(Foldable t, Semigroup (t Char), IsString (t Char)) =>
t Char -> t Char
paren (Tope n -> String
go Tope n
t)
      TUninv Tope n
t -> String
"uninv " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String -> String
forall {t :: * -> *}.
(Foldable t, Semigroup (t Char), IsString (t Char)) =>
t Char -> t Char
paren (Tope n -> String
go Tope n
t)
      TModal TModality
md Tope n
t -> TModality -> String
ppModality TModality
md String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String -> String
forall {t :: * -> *}.
(Foldable t, Semigroup (t Char), IsString (t Char)) =>
t Char -> t Char
paren (Tope n -> String
go Tope n
t)
      TModApp TModality
md Tope n
t -> String
"mod " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> TModality -> String
ppModality TModality
md String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String -> String
forall {t :: * -> *}.
(Foldable t, Semigroup (t Char), IsString (t Char)) =>
t Char -> t Char
paren (Tope n -> String
go Tope n
t)
      TNeutral Point n
p -> Naming n -> Point n -> String
forall (n :: S). Naming n -> Point n -> String
ppPoint Naming n
naming Point n
p
      THole Maybe VarIdent
m -> String -> (VarIdent -> String) -> Maybe VarIdent -> String
forall b a. b -> (a -> b) -> Maybe a -> b
maybe String
"?" ((String
"?" String -> String -> String
forall a. Semigroup a => a -> a -> a
<>) (String -> String) -> (VarIdent -> String) -> VarIdent -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. VarIdent -> String
forall a. Show a => a -> String
show) Maybe VarIdent
m
    paren :: t Char -> t Char
paren t Char
s = if Char
' ' Char -> t Char -> Bool
forall a. Eq a => a -> t a -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` t Char
s then t Char
"(" t Char -> t Char -> t Char
forall a. Semigroup a => a -> a -> a
<> t Char
s t Char -> t Char -> t Char
forall a. Semigroup a => a -> a -> a
<> t Char
")" else t Char
s

ppPoint :: Naming n -> Point n -> String
ppPoint :: forall (n :: S). Naming n -> Point n -> String
ppPoint Naming n
naming = Point n -> String
go
  where
    go :: Point n -> String
go = \case
      PVar Name n
x Cube
_ -> Naming n -> Name n -> String
forall (n :: S). Naming n -> Name n -> String
ppName Naming n
naming Name n
x
      Point n
PStar -> String
"*₁"
      Point n
P2_0 -> String
"0₂"
      Point n
P2_1 -> String
"1₂"
      Point n
PI_0 -> String
"0ᵢ"
      Point n
PI_1 -> String
"1ᵢ"
      PPair Point n
a Point n
b -> String
"(" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Point n -> String
go Point n
a String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
", " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Point n -> String
go Point n
b String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
")"
      PFst Point n
p -> String
"π₁ " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String -> String
forall {t :: * -> *}.
(Foldable t, Semigroup (t Char), IsString (t Char)) =>
t Char -> t Char
paren (Point n -> String
go Point n
p)
      PSnd Point n
p -> String
"π₂ " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String -> String
forall {t :: * -> *}.
(Foldable t, Semigroup (t Char), IsString (t Char)) =>
t Char -> t Char
paren (Point n -> String
go Point n
p)
      PSup Point n
a Point n
b -> String
"sup " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String -> String
forall {t :: * -> *}.
(Foldable t, Semigroup (t Char), IsString (t Char)) =>
t Char -> t Char
paren (Point n -> String
go Point n
a) String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String -> String
forall {t :: * -> *}.
(Foldable t, Semigroup (t Char), IsString (t Char)) =>
t Char -> t Char
paren (Point n -> String
go Point n
b)
      PInf Point n
a Point n
b -> String
"inf " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String -> String
forall {t :: * -> *}.
(Foldable t, Semigroup (t Char), IsString (t Char)) =>
t Char -> t Char
paren (Point n -> String
go Point n
a) String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String -> String
forall {t :: * -> *}.
(Foldable t, Semigroup (t Char), IsString (t Char)) =>
t Char -> t Char
paren (Point n -> String
go Point n
b)
      PFlip Point n
p -> String
"flip " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String -> String
forall {t :: * -> *}.
(Foldable t, Semigroup (t Char), IsString (t Char)) =>
t Char -> t Char
paren (Point n -> String
go Point n
p)
      PUnflip Point n
p -> String
"unflip " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String -> String
forall {t :: * -> *}.
(Foldable t, Semigroup (t Char), IsString (t Char)) =>
t Char -> t Char
paren (Point n -> String
go Point n
p)
      PModApp TModality
md Point n
p -> String
"mod " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> TModality -> String
ppModality TModality
md String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String -> String
forall {t :: * -> *}.
(Foldable t, Semigroup (t Char), IsString (t Char)) =>
t Char -> t Char
paren (Point n -> String
go Point n
p)
      PModExtract TModality
app TModality
inn Point n
p -> String
"$extract$ " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> TModality -> String
ppModality TModality
app String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"/" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> TModality -> String
ppModality TModality
inn String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String -> String
forall {t :: * -> *}.
(Foldable t, Semigroup (t Char), IsString (t Char)) =>
t Char -> t Char
paren (Point n -> String
go Point n
p)
      PApp Cube
_ Point n
f Point n
a -> Point n -> String
go Point n
f String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String -> String
forall {t :: * -> *}.
(Foldable t, Semigroup (t Char), IsString (t Char)) =>
t Char -> t Char
paren (Point n -> String
go Point n
a)
      PHole Maybe VarIdent
m -> String -> (VarIdent -> String) -> Maybe VarIdent -> String
forall b a. b -> (a -> b) -> Maybe a -> b
maybe String
"?" ((String
"?" String -> String -> String
forall a. Semigroup a => a -> a -> a
<>) (String -> String) -> (VarIdent -> String) -> VarIdent -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. VarIdent -> String
forall a. Show a => a -> String
show) Maybe VarIdent
m
      POpaque String
_ -> String
"…"
    paren :: t Char -> t Char
paren t Char
s = if Char
' ' Char -> t Char -> Bool
forall a. Eq a => a -> t a -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` t Char
s then t Char
"(" t Char -> t Char -> t Char
forall a. Semigroup a => a -> a -> a
<> t Char
s t Char -> t Char -> t Char
forall a. Semigroup a => a -> a -> a
<> t Char
")" else t Char
s

ppModality :: TModality -> String
ppModality :: TModality -> String
ppModality = \case
  TModality
Flat  -> String
"♭"
  TModality
Sharp -> String
"♯"
  TModality
Op    -> String
"ᵒᵖ"
  TModality
Id    -> String
"_id"

-- * Warnings

-- | A warning, rendered.
data Warning
  = OverhangWarning String String [String] (Maybe LocationInfo)
    -- ^ a restriction face or a recOR branch guard, the tope, the local
    -- tope context
  | TopeFamilyDomainWarning String String (Maybe LocationInfo)
    -- ^ the family, its declared domain
  | ShadowingWarning String [String] (Maybe LocationInfo)
    -- ^ a binder, and the binding it shadows, each with where it was written
  | UnsupportedOptionWarning String (Maybe LocationInfo)
    -- ^ an option that the new checker accepts but does not implement
  deriving (Warning -> Warning -> Bool
(Warning -> Warning -> Bool)
-> (Warning -> Warning -> Bool) -> Eq Warning
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Warning -> Warning -> Bool
== :: Warning -> Warning -> Bool
$c/= :: Warning -> Warning -> Bool
/= :: Warning -> Warning -> Bool
Eq, Int -> Warning -> String -> String
[Warning] -> String -> String
Warning -> String
(Int -> Warning -> String -> String)
-> (Warning -> String)
-> ([Warning] -> String -> String)
-> Show Warning
forall a.
(Int -> a -> String -> String)
-> (a -> String) -> ([a] -> String -> String) -> Show a
$cshowsPrec :: Int -> Warning -> String -> String
showsPrec :: Int -> Warning -> String -> String
$cshow :: Warning -> String
show :: Warning -> String
$cshowList :: [Warning] -> String -> String
showList :: [Warning] -> String -> String
Show)

-- | The stable code of a warning.
warningTag :: Warning -> String
warningTag :: Warning -> String
warningTag = \case
  OverhangWarning{}         -> String
"OverhangWarning"
  TopeFamilyDomainWarning{} -> String
"TopeFamilyDomainWarning"
  ShadowingWarning{}        -> String
"ShadowingWarning"
  UnsupportedOptionWarning{} -> String
"UnsupportedOptionWarning"

warningLocation :: Warning -> Maybe LocationInfo
warningLocation :: Warning -> Maybe LocationInfo
warningLocation = \case
  OverhangWarning String
_ String
_ [String]
_ Maybe LocationInfo
loc     -> Maybe LocationInfo
loc
  TopeFamilyDomainWarning String
_ String
_ Maybe LocationInfo
loc -> Maybe LocationInfo
loc
  ShadowingWarning String
_ [String]
_ Maybe LocationInfo
loc        -> Maybe LocationInfo
loc
  UnsupportedOptionWarning String
_ Maybe LocationInfo
loc   -> Maybe LocationInfo
loc

ppWarning :: Warning -> String
ppWarning :: Warning -> String
ppWarning = \case
  OverhangWarning String
what String
tope [String]
topes Maybe LocationInfo
_ -> String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
"\n" ([String] -> String) -> [String] -> String
forall a b. (a -> b) -> a -> b
$
    [ String
"Warning: " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
what String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" overhangs the local tope context"
    , String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
tope
    , String
"local tope context:" ] [String] -> [String] -> [String]
forall a. Semigroup a => a -> a -> a
<> (String -> String) -> [String] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map (String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<>) [String]
topes
  TopeFamilyDomainWarning String
family String
domain Maybe LocationInfo
_ -> String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
"\n"
    [ String
"Warning: the tope family"
    , String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
family
    , String
"is not included in its declared domain"
    , String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
domain
    , String
"and is read as their intersection" ]
  ShadowingWarning String
binder [String]
previous Maybe LocationInfo
_ -> String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
"\n" ([String] -> String) -> [String] -> String
forall a b. (a -> b) -> a -> b
$
    [ String
"Warning: a binder shadows a name in scope"
    , String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
binder
    , String
"previously bound at" ] [String] -> [String] -> [String]
forall a. Semigroup a => a -> a -> a
<> (String -> String) -> [String] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map (String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<>) [String]
previous
  UnsupportedOptionWarning String
name Maybe LocationInfo
_ ->
    String
"Warning: option " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String -> String
forall a. Show a => a -> String
show String
name String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" is not supported by the new checker yet and has no effect"

-- * Hints

-- | A hint, rendered and independent of the scope where it was found.
data Hint
  = SimplificationHint String String (Maybe LocationInfo)
    -- ^ the original and suggested terms, rendered, and the source location
  deriving (Hint -> Hint -> Bool
(Hint -> Hint -> Bool) -> (Hint -> Hint -> Bool) -> Eq Hint
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Hint -> Hint -> Bool
== :: Hint -> Hint -> Bool
$c/= :: Hint -> Hint -> Bool
/= :: Hint -> Hint -> Bool
Eq, Int -> Hint -> String -> String
[Hint] -> String -> String
Hint -> String
(Int -> Hint -> String -> String)
-> (Hint -> String) -> ([Hint] -> String -> String) -> Show Hint
forall a.
(Int -> a -> String -> String)
-> (a -> String) -> ([a] -> String -> String) -> Show a
$cshowsPrec :: Int -> Hint -> String -> String
showsPrec :: Int -> Hint -> String -> String
$cshow :: Hint -> String
show :: Hint -> String
$cshowList :: [Hint] -> String -> String
showList :: [Hint] -> String -> String
Show)

-- | The stable code of a hint.
hintTag :: Hint -> String
hintTag :: Hint -> String
hintTag = \case
  SimplificationHint{} -> String
"SimplificationHint"

hintLocation :: Hint -> Maybe LocationInfo
hintLocation :: Hint -> Maybe LocationInfo
hintLocation = \case
  SimplificationHint String
_ String
_ Maybe LocationInfo
loc -> Maybe LocationInfo
loc

ppHint :: Hint -> String
ppHint :: Hint -> String
ppHint = \case
  SimplificationHint String
original String
suggestion Maybe LocationInfo
_ -> String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
"\n"
    [ String
"Hint: consider replacing"
    , String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
original
    , String
"with the following"
    , String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
suggestion ]

-- * Holes

-- | The goal and the context at a hole, rendered.
data HoleInfo = HoleInfo
  { HoleInfo -> Maybe VarIdent
holeName     :: Maybe VarIdent
    -- ^ the optional user-supplied name of the hole
  , HoleInfo -> String
holeGoal     :: String
    -- ^ the rendered type expected at the hole
  , HoleInfo -> [(String, String)]
holeTermVars :: [(String, String)]
    -- ^ rendered names and types of local term variables
  , HoleInfo -> [(String, String)]
holeCubeVars :: [(String, String)]
    -- ^ rendered names and types of local cube variables
  , HoleInfo -> [String]
holeTopes    :: [String]
    -- ^ the rendered local tope context
  , HoleInfo -> Maybe LocationInfo
holeLocation :: Maybe LocationInfo
    -- ^ where the hole occurs
  } deriving (HoleInfo -> HoleInfo -> Bool
(HoleInfo -> HoleInfo -> Bool)
-> (HoleInfo -> HoleInfo -> Bool) -> Eq HoleInfo
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: HoleInfo -> HoleInfo -> Bool
== :: HoleInfo -> HoleInfo -> Bool
$c/= :: HoleInfo -> HoleInfo -> Bool
/= :: HoleInfo -> HoleInfo -> Bool
Eq, Int -> HoleInfo -> String -> String
[HoleInfo] -> String -> String
HoleInfo -> String
(Int -> HoleInfo -> String -> String)
-> (HoleInfo -> String)
-> ([HoleInfo] -> String -> String)
-> Show HoleInfo
forall a.
(Int -> a -> String -> String)
-> (a -> String) -> ([a] -> String -> String) -> Show a
$cshowsPrec :: Int -> HoleInfo -> String -> String
showsPrec :: Int -> HoleInfo -> String -> String
$cshow :: HoleInfo -> String
show :: HoleInfo -> String
$cshowList :: [HoleInfo] -> String -> String
showList :: [HoleInfo] -> String -> String
Show)

ppHoleInfo :: HoleInfo -> String
ppHoleInfo :: HoleInfo -> String
ppHoleInfo HoleInfo
info = [String] -> String
unlines ([String] -> String) -> [String] -> String
forall a b. (a -> b) -> a -> b
$
  [ String
"Hole" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String -> (VarIdent -> String) -> Maybe VarIdent -> String
forall b a. b -> (a -> b) -> Maybe a -> b
maybe String
"" ((String
" ?" String -> String -> String
forall a. Semigroup a => a -> a -> a
<>) (String -> String) -> (VarIdent -> String) -> VarIdent -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. VarIdent -> String
forall a. Show a => a -> String
show) (HoleInfo -> Maybe VarIdent
holeName HoleInfo
info) String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String -> (LocationInfo -> String) -> Maybe LocationInfo -> String
forall b a. b -> (a -> b) -> Maybe a -> b
maybe String
"" LocationInfo -> String
ppLoc (HoleInfo -> Maybe LocationInfo
holeLocation HoleInfo
info)
  , String
"  Goal: " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> HoleInfo -> String
holeGoal HoleInfo
info ]
  [String] -> [String] -> [String]
forall a. Semigroup a => a -> a -> a
<> [ String
"  " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
x String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" : " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
ty | (String
x, String
ty) <- HoleInfo -> [(String, String)]
holeCubeVars HoleInfo
info [(String, String)] -> [(String, String)] -> [(String, String)]
forall a. Semigroup a => a -> a -> a
<> HoleInfo -> [(String, String)]
holeTermVars HoleInfo
info ]
  [String] -> [String] -> [String]
forall a. Semigroup a => a -> a -> a
<> [ String
"  tope: " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
t | String
t <- HoleInfo -> [String]
holeTopes HoleInfo
info ]
  where
    ppLoc :: LocationInfo -> String
ppLoc (LocationInfo Maybe String
path Maybe Int
line Maybe Int
col) =
      String
" at " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String -> (String -> String) -> Maybe String -> String
forall b a. b -> (a -> b) -> Maybe a -> b
maybe String
"" String -> String
forall a. a -> a
id Maybe String
path String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String -> (Int -> String) -> Maybe Int -> String
forall b a. b -> (a -> b) -> Maybe a -> b
maybe String
"" ((String
":" String -> String -> String
forall a. Semigroup a => a -> a -> a
<>) (String -> String) -> (Int -> String) -> Int -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> String
forall a. Show a => a -> String
show) Maybe Int
line String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String -> (Int -> String) -> Maybe Int -> String
forall b a. b -> (a -> b) -> Maybe a -> b
maybe String
"" ((String
":" String -> String -> String
forall a. Semigroup a => a -> a -> a
<>) (String -> String) -> (Int -> String) -> Int -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> String
forall a. Show a => a -> String
show) Maybe Int
col