{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Rzk.Check.Context
(
Value
, Variance (..)
, opposite
, Verbosity (..)
, LocationInfo (..)
, atPosition
, HoleMode (..)
, Options (..)
, defaultOptions
, Approximation (..)
, Unfolding (..)
, EntryKind (..)
, Entry (..)
, localEntry
, Section (..)
, Action (..)
, Diag (..)
, emptyDiag
, Context (..)
, emptyContext
, lookupEntry
, lookupNamed
, entriesInScope
, enterPattern
, sinkContextUnchecked
, insertEntry
, withVariance
) where
import Control.Monad.Foil (DExt, Distinct, NameMap, Scope)
import qualified Control.Monad.Foil as Foil
import Data.Map (Map)
import qualified Data.Map as Map
import Unsafe.Coerce (unsafeCoerce)
import qualified FreeFoil.NbE as NbE
import FreeFoil.NbE.Definitions (Unfolding (..))
import Language.Rzk.Foil.Names (RzkPosition (..), TModality (..),
VarIdent)
import Rzk.Check.Foil.Extras (replaceName)
import Rzk.Check.Syntax
import Rzk.Check.Topes (Tope, TopeContext,
emptyTopeContext)
type Value = NbE.Value Pattern Sig
data Variance = Covariant | Contravariant | Invariant
deriving (Variance -> Variance -> Bool
(Variance -> Variance -> Bool)
-> (Variance -> Variance -> Bool) -> Eq Variance
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Variance -> Variance -> Bool
== :: Variance -> Variance -> Bool
$c/= :: Variance -> Variance -> Bool
/= :: Variance -> Variance -> Bool
Eq, Int -> Variance -> ShowS
[Variance] -> ShowS
Variance -> String
(Int -> Variance -> ShowS)
-> (Variance -> String) -> ([Variance] -> ShowS) -> Show Variance
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Variance -> ShowS
showsPrec :: Int -> Variance -> ShowS
$cshow :: Variance -> String
show :: Variance -> String
$cshowList :: [Variance] -> ShowS
showList :: [Variance] -> ShowS
Show)
opposite :: Variance -> Variance
opposite :: Variance -> Variance
opposite = \case
Variance
Covariant -> Variance
Contravariant
Variance
Contravariant -> Variance
Covariant
Variance
Invariant -> Variance
Invariant
data Verbosity = Debug | Normal | Silent
deriving (Verbosity -> Verbosity -> Bool
(Verbosity -> Verbosity -> Bool)
-> (Verbosity -> Verbosity -> Bool) -> Eq Verbosity
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Verbosity -> Verbosity -> Bool
== :: Verbosity -> Verbosity -> Bool
$c/= :: Verbosity -> Verbosity -> Bool
/= :: Verbosity -> Verbosity -> Bool
Eq, Eq Verbosity
Eq Verbosity =>
(Verbosity -> Verbosity -> Ordering)
-> (Verbosity -> Verbosity -> Bool)
-> (Verbosity -> Verbosity -> Bool)
-> (Verbosity -> Verbosity -> Bool)
-> (Verbosity -> Verbosity -> Bool)
-> (Verbosity -> Verbosity -> Verbosity)
-> (Verbosity -> Verbosity -> Verbosity)
-> Ord Verbosity
Verbosity -> Verbosity -> Bool
Verbosity -> Verbosity -> Ordering
Verbosity -> Verbosity -> Verbosity
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Verbosity -> Verbosity -> Ordering
compare :: Verbosity -> Verbosity -> Ordering
$c< :: Verbosity -> Verbosity -> Bool
< :: Verbosity -> Verbosity -> Bool
$c<= :: Verbosity -> Verbosity -> Bool
<= :: Verbosity -> Verbosity -> Bool
$c> :: Verbosity -> Verbosity -> Bool
> :: Verbosity -> Verbosity -> Bool
$c>= :: Verbosity -> Verbosity -> Bool
>= :: Verbosity -> Verbosity -> Bool
$cmax :: Verbosity -> Verbosity -> Verbosity
max :: Verbosity -> Verbosity -> Verbosity
$cmin :: Verbosity -> Verbosity -> Verbosity
min :: Verbosity -> Verbosity -> Verbosity
Ord, Int -> Verbosity -> ShowS
[Verbosity] -> ShowS
Verbosity -> String
(Int -> Verbosity -> ShowS)
-> (Verbosity -> String)
-> ([Verbosity] -> ShowS)
-> Show Verbosity
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Verbosity -> ShowS
showsPrec :: Int -> Verbosity -> ShowS
$cshow :: Verbosity -> String
show :: Verbosity -> String
$cshowList :: [Verbosity] -> ShowS
showList :: [Verbosity] -> ShowS
Show)
data LocationInfo = LocationInfo
{ LocationInfo -> Maybe String
locationFilePath :: Maybe FilePath
, LocationInfo -> Maybe Int
locationLine :: Maybe Int
, LocationInfo -> Maybe Int
locationColumn :: Maybe Int
} deriving (LocationInfo -> LocationInfo -> Bool
(LocationInfo -> LocationInfo -> Bool)
-> (LocationInfo -> LocationInfo -> Bool) -> Eq LocationInfo
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: LocationInfo -> LocationInfo -> Bool
== :: LocationInfo -> LocationInfo -> Bool
$c/= :: LocationInfo -> LocationInfo -> Bool
/= :: LocationInfo -> LocationInfo -> Bool
Eq, Int -> LocationInfo -> ShowS
[LocationInfo] -> ShowS
LocationInfo -> String
(Int -> LocationInfo -> ShowS)
-> (LocationInfo -> String)
-> ([LocationInfo] -> ShowS)
-> Show LocationInfo
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> LocationInfo -> ShowS
showsPrec :: Int -> LocationInfo -> ShowS
$cshow :: LocationInfo -> String
show :: LocationInfo -> String
$cshowList :: [LocationInfo] -> ShowS
showList :: [LocationInfo] -> ShowS
Show)
atPosition :: RzkPosition -> LocationInfo -> LocationInfo
atPosition :: RzkPosition -> LocationInfo -> LocationInfo
atPosition RzkPosition
pos LocationInfo
loc = case RzkPosition -> BNFC'Position
rzkLineCol RzkPosition
pos of
BNFC'Position
Nothing -> LocationInfo
loc
Just (Int
line, Int
col) -> LocationInfo
loc { locationLine = Just line, locationColumn = Just col }
data HoleMode
= HolesAreErrors
| HolesDeferred
| HolesStructural
deriving (HoleMode -> HoleMode -> Bool
(HoleMode -> HoleMode -> Bool)
-> (HoleMode -> HoleMode -> Bool) -> Eq HoleMode
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: HoleMode -> HoleMode -> Bool
== :: HoleMode -> HoleMode -> Bool
$c/= :: HoleMode -> HoleMode -> Bool
/= :: HoleMode -> HoleMode -> Bool
Eq, Int -> HoleMode -> ShowS
[HoleMode] -> ShowS
HoleMode -> String
(Int -> HoleMode -> ShowS)
-> (HoleMode -> String) -> ([HoleMode] -> ShowS) -> Show HoleMode
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> HoleMode -> ShowS
showsPrec :: Int -> HoleMode -> ShowS
$cshow :: HoleMode -> String
show :: HoleMode -> String
$cshowList :: [HoleMode] -> ShowS
showList :: [HoleMode] -> ShowS
Show)
data Options = Options
{ Options -> Verbosity
optVerbosity :: Verbosity
, Options -> HoleMode
optHoles :: HoleMode
, Options -> [VarIdent]
optHintLemmas :: [VarIdent]
, Options -> Bool
optWarnOverhang :: Bool
, Options -> Bool
optWarnTopeFamilyDomain :: Bool
, Options -> Maybe String
optRender :: Maybe String
} deriving (Options -> Options -> Bool
(Options -> Options -> Bool)
-> (Options -> Options -> Bool) -> Eq Options
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Options -> Options -> Bool
== :: Options -> Options -> Bool
$c/= :: Options -> Options -> Bool
/= :: Options -> Options -> Bool
Eq, Int -> Options -> ShowS
[Options] -> ShowS
Options -> String
(Int -> Options -> ShowS)
-> (Options -> String) -> ([Options] -> ShowS) -> Show Options
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Options -> ShowS
showsPrec :: Int -> Options -> ShowS
$cshow :: Options -> String
show :: Options -> String
$cshowList :: [Options] -> ShowS
showList :: [Options] -> ShowS
Show)
defaultOptions :: Options
defaultOptions :: Options
defaultOptions = Options
{ optVerbosity :: Verbosity
optVerbosity = Verbosity
Normal
, optHoles :: HoleMode
optHoles = HoleMode
HolesAreErrors
, optHintLemmas :: [VarIdent]
optHintLemmas = []
, optWarnOverhang :: Bool
optWarnOverhang = Bool
False
, optWarnTopeFamilyDomain :: Bool
optWarnTopeFamilyDomain = Bool
True
, optRender :: Maybe String
optRender = Maybe String
forall a. Maybe a
Nothing
}
data Approximation
= ApproxRigid
| ApproxFlex
| ApproxFull
deriving (Approximation -> Approximation -> Bool
(Approximation -> Approximation -> Bool)
-> (Approximation -> Approximation -> Bool) -> Eq Approximation
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Approximation -> Approximation -> Bool
== :: Approximation -> Approximation -> Bool
$c/= :: Approximation -> Approximation -> Bool
/= :: Approximation -> Approximation -> Bool
Eq, Int -> Approximation -> ShowS
[Approximation] -> ShowS
Approximation -> String
(Int -> Approximation -> ShowS)
-> (Approximation -> String)
-> ([Approximation] -> ShowS)
-> Show Approximation
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Approximation -> ShowS
showsPrec :: Int -> Approximation -> ShowS
$cshow :: Approximation -> String
show :: Approximation -> String
$cshowList :: [Approximation] -> ShowS
showList :: [Approximation] -> ShowS
Show)
data EntryKind
= EntryLocal
| EntryAssumption
| EntryTopLevel
deriving (EntryKind -> EntryKind -> Bool
(EntryKind -> EntryKind -> Bool)
-> (EntryKind -> EntryKind -> Bool) -> Eq EntryKind
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: EntryKind -> EntryKind -> Bool
== :: EntryKind -> EntryKind -> Bool
$c/= :: EntryKind -> EntryKind -> Bool
/= :: EntryKind -> EntryKind -> Bool
Eq, Int -> EntryKind -> ShowS
[EntryKind] -> ShowS
EntryKind -> String
(Int -> EntryKind -> ShowS)
-> (EntryKind -> String)
-> ([EntryKind] -> ShowS)
-> Show EntryKind
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> EntryKind -> ShowS
showsPrec :: Int -> EntryKind -> ShowS
$cshow :: EntryKind -> String
show :: EntryKind -> String
$cshowList :: [EntryKind] -> ShowS
showList :: [EntryKind] -> ShowS
Show)
data Entry n = Entry
{ forall (n :: S). Entry n -> Value n
entryType :: Value n
, forall (n :: S). Entry n -> Maybe (Value n)
entryValue :: Maybe (Value n)
, forall (n :: S). Entry n -> TModality
entryModality :: TModality
, forall (n :: S). Entry n -> TModality
entryLocks :: TModality
, forall (n :: S). Entry n -> EntryKind
entryKind :: EntryKind
, forall (n :: S). Entry n -> Maybe VarIdent
entryName :: Maybe VarIdent
, forall (n :: S). Entry n -> Maybe LocationInfo
entryLocation :: Maybe LocationInfo
, forall (n :: S). Entry n -> [Name n]
entryUses :: [Foil.Name n]
, forall (n :: S). Entry n -> Maybe (Core n)
entryTypeCore :: Maybe (Core n)
, forall (n :: S). Entry n -> Maybe (Core n)
entryValueCore :: Maybe (Core n)
}
instance Foil.Sinkable Entry where
sinkabilityProof :: forall (n :: S) (l :: S). (Name n -> Name l) -> Entry n -> Entry l
sinkabilityProof Name n -> Name l
rename Entry n
entry = Entry n
entry
{ entryType = Foil.sinkabilityProof rename (entryType entry)
, entryValue = Foil.sinkabilityProof rename <$> entryValue entry
, entryUses = map rename (entryUses entry)
, entryTypeCore = Foil.sinkabilityProof rename <$> entryTypeCore entry
, entryValueCore = Foil.sinkabilityProof rename <$> entryValueCore entry
}
localEntry :: TModality -> Maybe VarIdent -> Maybe LocationInfo -> Value n -> Entry n
localEntry :: forall (n :: S).
TModality
-> Maybe VarIdent -> Maybe LocationInfo -> Value n -> Entry n
localEntry TModality
md Maybe VarIdent
mname Maybe LocationInfo
loc Value n
ty = Entry
{ entryType :: Value n
entryType = Value n
ty
, entryValue :: Maybe (Value n)
entryValue = Maybe (Value n)
forall a. Maybe a
Nothing
, entryModality :: TModality
entryModality = TModality
md
, entryLocks :: TModality
entryLocks = TModality
Id
, entryKind :: EntryKind
entryKind = EntryKind
EntryLocal
, entryName :: Maybe VarIdent
entryName = Maybe VarIdent
mname
, entryLocation :: Maybe LocationInfo
entryLocation = Maybe LocationInfo
loc
, entryUses :: [Name n]
entryUses = []
, entryTypeCore :: Maybe (Core n)
entryTypeCore = Maybe (Core n)
forall a. Maybe a
Nothing
, entryValueCore :: Maybe (Core n)
entryValueCore = Maybe (Core n)
forall a. Maybe a
Nothing
}
data Section n = Section
{ forall (n :: S). Section n -> Maybe String
sectionName :: Maybe String
, forall (n :: S). Section n -> [Name n]
sectionEntries :: [Foil.Name n]
}
sinkEntries :: DExt n l => NameMap n (Entry n) -> NameMap n (Entry l)
sinkEntries :: forall (n :: S) (l :: S).
DExt n l =>
NameMap n (Entry n) -> NameMap n (Entry l)
sinkEntries = NameMap n (Entry n) -> NameMap n (Entry l)
forall (f :: * -> *) (e :: S -> *) (n :: S) (l :: S).
(Functor f, Sinkable e, DExt n l) =>
f (e n) -> f (e l)
Foil.sink1
data Action n
= ActionCheck (Raw n) (Value n)
| ActionInfer (Raw n)
| ActionConvert (Value n) (Value n)
| ActionEntails [Tope n] (Tope n)
| ActionEntailsUnion [Tope n] [Tope n]
| ActionCheckLetValue (Maybe VarIdent)
| ActionCloseSection (Maybe String)
data Diag n = Diag
{ forall (n :: S). Diag n -> [Action n]
diagActions :: [Action n]
, forall (n :: S). Diag n -> Int
diagDepth :: !Int
}
emptyDiag :: Diag n
emptyDiag :: forall (n :: S). Diag n
emptyDiag = [Action n] -> Int -> Diag n
forall (n :: S). [Action n] -> Int -> Diag n
Diag [] Int
0
data Context n = Context
{ forall (n :: S). Context n -> Scope n
ctxScope :: Scope n
, forall (n :: S). Context n -> NameMap n (Entry n)
ctxEntries :: NameMap n (Entry n)
, forall (n :: S). Context n -> Map VarIdent (Name n)
ctxNamed :: Map VarIdent (Foil.Name n)
, forall (n :: S). Context n -> [Name n]
ctxBound :: [Foil.Name n]
, forall (n :: S). Context n -> TopeContext n
ctxTopes :: TopeContext n
, forall (n :: S). Context n -> Variance
ctxVariance :: Variance
, forall (n :: S). Context n -> Unfolding
ctxUnfolding :: Unfolding
, forall (n :: S). Context n -> Approximation
ctxApproximation :: Approximation
, forall (n :: S). Context n -> Options
ctxOptions :: Options
, forall (n :: S). Context n -> Maybe LocationInfo
ctxLocation :: Maybe LocationInfo
, forall (n :: S). Context n -> [Section n]
ctxSections :: [Section n]
, forall (n :: S). Context n -> Diag n
ctxDiag :: Diag n
}
emptyContext :: Context Foil.VoidS
emptyContext :: Context 'VoidS
emptyContext = Context
{ ctxScope :: Scope 'VoidS
ctxScope = Scope 'VoidS
Foil.emptyScope
, ctxEntries :: NameMap 'VoidS (Entry 'VoidS)
ctxEntries = NameMap 'VoidS (Entry 'VoidS)
forall a. NameMap 'VoidS a
Foil.emptyNameMap
, ctxNamed :: Map VarIdent (Name 'VoidS)
ctxNamed = Map VarIdent (Name 'VoidS)
forall k a. Map k a
Map.empty
, ctxBound :: [Name 'VoidS]
ctxBound = []
, ctxTopes :: TopeContext 'VoidS
ctxTopes = TopeContext 'VoidS
forall (n :: S). TopeContext n
emptyTopeContext
, ctxVariance :: Variance
ctxVariance = Variance
Covariant
, ctxUnfolding :: Unfolding
ctxUnfolding = Unfolding
Unfolded
, ctxApproximation :: Approximation
ctxApproximation = Approximation
ApproxRigid
, ctxOptions :: Options
ctxOptions = Options
defaultOptions
, ctxLocation :: Maybe LocationInfo
ctxLocation = Maybe LocationInfo
forall a. Maybe a
Nothing
, ctxSections :: [Section 'VoidS]
ctxSections = []
, ctxDiag :: Diag 'VoidS
ctxDiag = Diag 'VoidS
forall (n :: S). Diag n
emptyDiag
}
lookupEntry :: Foil.Name n -> Context n -> Entry n
lookupEntry :: forall (n :: S). Name n -> Context n -> Entry n
lookupEntry Name n
name Context n
ctx = Name n -> NameMap n (Entry n) -> Entry n
forall (n :: S) a. Name n -> NameMap n a -> a
Foil.lookupName Name n
name (Context n -> NameMap n (Entry n)
forall (n :: S). Context n -> NameMap n (Entry n)
ctxEntries Context n
ctx)
lookupNamed :: VarIdent -> Context n -> Maybe (Foil.Name n)
lookupNamed :: forall (n :: S). VarIdent -> Context n -> Maybe (Name n)
lookupNamed VarIdent
name Context n
ctx = VarIdent -> Map VarIdent (Name n) -> Maybe (Name n)
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup VarIdent
name (Context n -> Map VarIdent (Name n)
forall (n :: S). Context n -> Map VarIdent (Name n)
ctxNamed Context n
ctx)
entriesInScope :: Context n -> [(Foil.Name n, Entry n)]
entriesInScope :: forall (n :: S). Context n -> [(Name n, Entry n)]
entriesInScope Context n
ctx = [ (Name n
name, Name n -> Context n -> Entry n
forall (n :: S). Name n -> Context n -> Entry n
lookupEntry Name n
name Context n
ctx) | Name n
name <- [Name n] -> [Name n]
forall a. [a] -> [a]
reverse (Context n -> [Name n]
forall (n :: S). Context n -> [Name n]
ctxBound Context n
ctx) ]
sinkContextUnchecked :: DExt n l => Context n -> Context l
sinkContextUnchecked :: forall (n :: S) (l :: S). DExt n l => Context n -> Context l
sinkContextUnchecked = Context n -> Context l
forall a b. a -> b
unsafeCoerce
enterPattern
:: (Distinct n, DExt n l)
=> Pattern n l -> [Entry l] -> Context n -> Context l
enterPattern :: forall (n :: S) (l :: S).
(Distinct n, DExt n l) =>
Pattern n l -> [Entry l] -> Context n -> Context l
enterPattern Pattern n l
pat [Entry l]
entries Context n
ctx = (Context n -> Context l
forall (n :: S) (l :: S). DExt n l => Context n -> Context l
sinkContextUnchecked Context n
ctx)
{ ctxScope = Foil.extendScopePattern pat (ctxScope ctx)
, ctxEntries = Foil.addNameBinders pat entries (sinkEntries (ctxEntries ctx))
, ctxBound = reverse (map snd names) <> sinkNames (ctxBound ctx)
, ctxNamed = foldr named (sinkNamed (ctxNamed ctx)) names
}
where
names :: [(Maybe VarIdent, Name l)]
names = Pattern n l -> [(Maybe VarIdent, Name l)]
forall (n :: S) (l :: S).
(Distinct n, Distinct l) =>
Pattern n l -> [(Maybe VarIdent, Name l)]
patternNames Pattern n l
pat
named :: (Maybe k, a) -> Map k a -> Map k a
named (Just k
x, a
name) = k -> a -> Map k a -> Map k a
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert k
x a
name
named (Maybe k
Nothing, a
_) = Map k a -> Map k a
forall a. a -> a
id
sinkNames :: DExt n l => [Foil.Name n] -> [Foil.Name l]
sinkNames :: forall (n :: S) (l :: S). DExt n l => [Name n] -> [Name l]
sinkNames = [Name n] -> [Name l]
forall (f :: * -> *) (e :: S -> *) (n :: S) (l :: S).
(Functor f, Sinkable e, DExt n l) =>
f (e n) -> f (e l)
Foil.sink1
sinkNamed :: DExt n l => Map VarIdent (Foil.Name n) -> Map VarIdent (Foil.Name l)
sinkNamed :: forall (n :: S) (l :: S).
DExt n l =>
Map VarIdent (Name n) -> Map VarIdent (Name l)
sinkNamed = Map VarIdent (Name n) -> Map VarIdent (Name l)
forall (f :: * -> *) (e :: S -> *) (n :: S) (l :: S).
(Functor f, Sinkable e, DExt n l) =>
f (e n) -> f (e l)
Foil.sink1
insertEntry :: Foil.Name n -> Entry n -> Context n -> Context n
insertEntry :: forall (n :: S). Name n -> Entry n -> Context n -> Context n
insertEntry Name n
name Entry n
entry Context n
ctx = Context n
ctx { ctxEntries = replaceName name entry (ctxEntries ctx) }
withVariance :: Variance -> Context n -> Context n
withVariance :: forall (n :: S). Variance -> Context n -> Context n
withVariance Variance
v Context n
ctx = Context n
ctx { ctxVariance = v }