{-# LANGUAGE LambdaCase #-}
-- | The tope solver, on first-order topes.
--
-- A tope is read off a value into 'Tope', a first-order syntax with no
-- binders and no scope index, whose equality is structural and whose
-- ordering keys a set. The solver is the saturation solver of
-- "Rzk.TypeCheck.Eval" (Riehl and Shulman's tope logic, the lattice
-- operations on the intervals, and the cover of a disjunction by
-- alternatives), made pure and without inversion under @ᵒᵖ@ (see
-- 'saturateInv'). The context caches its saturation lazily, and a query
-- reads it.
--
-- The modal layer (topes assumed under a modality, locks, and ex falso
-- across modalities) follows the modal topes of LIshy2 in the old checker
-- (#230). Inversion under @ᵒᵖ@ is not implemented yet.
module Rzk.Check.Topes
  ( -- * Topes
    Cube (..)
  , Point (..)
  , Tope (..)
  , cubeOf
  , pointFirst
  , pointSecond
  , topeAnd
  , topeOr
  , ModalTope (..)
  , plainTope
  , isAccessible
    -- * The tope context
  , TopeContext
  , emptyTopeContext
  , contextTopes
  , accessibleTopes
  , assume
  , assumeDiscrete
  , applyModality
    -- * Queries
  , entails
  , entailsBottom
  , entailsUnion
  , entailedBy
  , disjoint
  , equivalent
  , topesEntail
  , alternatives
  , inAlternatives
  ) where

import           Control.Monad.Foil      (Name)
import           Data.List               (nub, tails)

import qualified Data.Set                as Set

import           Language.Rzk.Foil.Names (VarIdent)
import           Rzk.Check.Modality

-- * Topes

-- | A cube, as far as the solver tells cubes apart.
data Cube
  = KUnit
  | K2
  | KI
  | KProd Cube Cube
  | KOther
    -- ^ a cube the solver has no rules for: a neutral one, or a modal one
  deriving (Cube -> Cube -> Bool
(Cube -> Cube -> Bool) -> (Cube -> Cube -> Bool) -> Eq Cube
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Cube -> Cube -> Bool
== :: Cube -> Cube -> Bool
$c/= :: Cube -> Cube -> Bool
/= :: Cube -> Cube -> Bool
Eq, Eq Cube
Eq Cube =>
(Cube -> Cube -> Ordering)
-> (Cube -> Cube -> Bool)
-> (Cube -> Cube -> Bool)
-> (Cube -> Cube -> Bool)
-> (Cube -> Cube -> Bool)
-> (Cube -> Cube -> Cube)
-> (Cube -> Cube -> Cube)
-> Ord Cube
Cube -> Cube -> Bool
Cube -> Cube -> Ordering
Cube -> Cube -> Cube
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 :: Cube -> Cube -> Ordering
compare :: Cube -> Cube -> Ordering
$c< :: Cube -> Cube -> Bool
< :: Cube -> Cube -> Bool
$c<= :: Cube -> Cube -> Bool
<= :: Cube -> Cube -> Bool
$c> :: Cube -> Cube -> Bool
> :: Cube -> Cube -> Bool
$c>= :: Cube -> Cube -> Bool
>= :: Cube -> Cube -> Bool
$cmax :: Cube -> Cube -> Cube
max :: Cube -> Cube -> Cube
$cmin :: Cube -> Cube -> Cube
min :: Cube -> Cube -> Cube
Ord, Int -> Cube -> ShowS
[Cube] -> ShowS
Cube -> String
(Int -> Cube -> ShowS)
-> (Cube -> String) -> ([Cube] -> ShowS) -> Show Cube
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Cube -> ShowS
showsPrec :: Int -> Cube -> ShowS
$cshow :: Cube -> String
show :: Cube -> String
$cshowList :: [Cube] -> ShowS
showList :: [Cube] -> ShowS
Show)

-- | A point of a cube. Evaluation has normalised it: a projection applies
-- only to a neutral, the lattice operations only to points that are not
-- endpoints, and a flip only to a neutral.
data Point n
  = PVar !(Name n) !Cube
    -- ^ a variable, with its cube
  | PStar
  | P2_0
  | P2_1
  | PI_0
  | PI_1
  | PPair (Point n) (Point n)
  | PFst (Point n)
  | PSnd (Point n)
  | PSup (Point n) (Point n)
  | PInf (Point n) (Point n)
  | PFlip (Point n)
  | PUnflip (Point n)
  | PModApp TModality (Point n)
  | PModExtract TModality TModality (Point n)
  | PApp !Cube (Point n) (Point n)
    -- ^ a neutral application, with its cube
  | PHole (Maybe VarIdent)
  | POpaque String
    -- ^ a point the solver cannot read (an elimination it has no rule for),
    -- keyed by a fingerprint of its term, so that equal values read back equal
  deriving (Point n -> Point n -> Bool
(Point n -> Point n -> Bool)
-> (Point n -> Point n -> Bool) -> Eq (Point n)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall (n :: S). Point n -> Point n -> Bool
$c== :: forall (n :: S). Point n -> Point n -> Bool
== :: Point n -> Point n -> Bool
$c/= :: forall (n :: S). Point n -> Point n -> Bool
/= :: Point n -> Point n -> Bool
Eq, Eq (Point n)
Eq (Point n) =>
(Point n -> Point n -> Ordering)
-> (Point n -> Point n -> Bool)
-> (Point n -> Point n -> Bool)
-> (Point n -> Point n -> Bool)
-> (Point n -> Point n -> Bool)
-> (Point n -> Point n -> Point n)
-> (Point n -> Point n -> Point n)
-> Ord (Point n)
Point n -> Point n -> Bool
Point n -> Point n -> Ordering
Point n -> Point n -> Point n
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
forall (n :: S). Eq (Point n)
forall (n :: S). Point n -> Point n -> Bool
forall (n :: S). Point n -> Point n -> Ordering
forall (n :: S). Point n -> Point n -> Point n
$ccompare :: forall (n :: S). Point n -> Point n -> Ordering
compare :: Point n -> Point n -> Ordering
$c< :: forall (n :: S). Point n -> Point n -> Bool
< :: Point n -> Point n -> Bool
$c<= :: forall (n :: S). Point n -> Point n -> Bool
<= :: Point n -> Point n -> Bool
$c> :: forall (n :: S). Point n -> Point n -> Bool
> :: Point n -> Point n -> Bool
$c>= :: forall (n :: S). Point n -> Point n -> Bool
>= :: Point n -> Point n -> Bool
$cmax :: forall (n :: S). Point n -> Point n -> Point n
max :: Point n -> Point n -> Point n
$cmin :: forall (n :: S). Point n -> Point n -> Point n
min :: Point n -> Point n -> Point n
Ord, Int -> Point n -> ShowS
[Point n] -> ShowS
Point n -> String
(Int -> Point n -> ShowS)
-> (Point n -> String) -> ([Point n] -> ShowS) -> Show (Point n)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall (n :: S). Int -> Point n -> ShowS
forall (n :: S). [Point n] -> ShowS
forall (n :: S). Point n -> String
$cshowsPrec :: forall (n :: S). Int -> Point n -> ShowS
showsPrec :: Int -> Point n -> ShowS
$cshow :: forall (n :: S). Point n -> String
show :: Point n -> String
$cshowList :: forall (n :: S). [Point n] -> ShowS
showList :: [Point n] -> ShowS
Show)

-- | A tope.
data Tope n
  = TTop
  | TBot
  | TEq (Point n) (Point n)
  | TLeq (Point n) (Point n)
  | TAnd (Tope n) (Tope n)
  | TOr (Tope n) (Tope n)
  | TInv (Tope n)
  | TUninv (Tope n)
  | TModal TModality (Tope n)
  | TModApp TModality (Tope n)
  | TNeutral (Point n)
    -- ^ a tope family applied, or a tope variable: a neutral point of the
    -- tope universe
  | THole (Maybe VarIdent)
  deriving (Tope n -> Tope n -> Bool
(Tope n -> Tope n -> Bool)
-> (Tope n -> Tope n -> Bool) -> Eq (Tope n)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall (n :: S). Tope n -> Tope n -> Bool
$c== :: forall (n :: S). Tope n -> Tope n -> Bool
== :: Tope n -> Tope n -> Bool
$c/= :: forall (n :: S). Tope n -> Tope n -> Bool
/= :: Tope n -> Tope n -> Bool
Eq, Eq (Tope n)
Eq (Tope n) =>
(Tope n -> Tope n -> Ordering)
-> (Tope n -> Tope n -> Bool)
-> (Tope n -> Tope n -> Bool)
-> (Tope n -> Tope n -> Bool)
-> (Tope n -> Tope n -> Bool)
-> (Tope n -> Tope n -> Tope n)
-> (Tope n -> Tope n -> Tope n)
-> Ord (Tope n)
Tope n -> Tope n -> Bool
Tope n -> Tope n -> Ordering
Tope n -> Tope n -> Tope n
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
forall (n :: S). Eq (Tope n)
forall (n :: S). Tope n -> Tope n -> Bool
forall (n :: S). Tope n -> Tope n -> Ordering
forall (n :: S). Tope n -> Tope n -> Tope n
$ccompare :: forall (n :: S). Tope n -> Tope n -> Ordering
compare :: Tope n -> Tope n -> Ordering
$c< :: forall (n :: S). Tope n -> Tope n -> Bool
< :: Tope n -> Tope n -> Bool
$c<= :: forall (n :: S). Tope n -> Tope n -> Bool
<= :: Tope n -> Tope n -> Bool
$c> :: forall (n :: S). Tope n -> Tope n -> Bool
> :: Tope n -> Tope n -> Bool
$c>= :: forall (n :: S). Tope n -> Tope n -> Bool
>= :: Tope n -> Tope n -> Bool
$cmax :: forall (n :: S). Tope n -> Tope n -> Tope n
max :: Tope n -> Tope n -> Tope n
$cmin :: forall (n :: S). Tope n -> Tope n -> Tope n
min :: Tope n -> Tope n -> Tope n
Ord, Int -> Tope n -> ShowS
[Tope n] -> ShowS
Tope n -> String
(Int -> Tope n -> ShowS)
-> (Tope n -> String) -> ([Tope n] -> ShowS) -> Show (Tope n)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall (n :: S). Int -> Tope n -> ShowS
forall (n :: S). [Tope n] -> ShowS
forall (n :: S). Tope n -> String
$cshowsPrec :: forall (n :: S). Int -> Tope n -> ShowS
showsPrec :: Int -> Tope n -> ShowS
$cshow :: forall (n :: S). Tope n -> String
show :: Tope n -> String
$cshowList :: forall (n :: S). [Tope n] -> ShowS
showList :: [Tope n] -> ShowS
Show)

-- | The cube of a point.
cubeOf :: Point n -> Cube
cubeOf :: forall (n :: S). Point n -> Cube
cubeOf = \case
  PVar Name n
_ Cube
c       -> Cube
c
  Point n
PStar          -> Cube
KUnit
  Point n
P2_0           -> Cube
K2
  Point n
P2_1           -> Cube
K2
  Point n
PI_0           -> Cube
KI
  Point n
PI_1           -> Cube
KI
  PPair Point n
l Point n
r      -> Cube -> Cube -> Cube
KProd (Point n -> Cube
forall (n :: S). Point n -> Cube
cubeOf Point n
l) (Point n -> Cube
forall (n :: S). Point n -> Cube
cubeOf Point n
r)
  PFst Point n
p         -> case Point n -> Cube
forall (n :: S). Point n -> Cube
cubeOf Point n
p of { KProd Cube
a Cube
_ -> Cube
a; Cube
_ -> Cube
KOther }
  PSnd Point n
p         -> case Point n -> Cube
forall (n :: S). Point n -> Cube
cubeOf Point n
p of { KProd Cube
_ Cube
b -> Cube
b; Cube
_ -> Cube
KOther }
  PSup Point n
l Point n
r       -> Cube -> Cube -> Cube
interval (Point n -> Cube
forall (n :: S). Point n -> Cube
cubeOf Point n
l) (Point n -> Cube
forall (n :: S). Point n -> Cube
cubeOf Point n
r)
  PInf Point n
l Point n
r       -> Cube -> Cube -> Cube
interval (Point n -> Cube
forall (n :: S). Point n -> Cube
cubeOf Point n
l) (Point n -> Cube
forall (n :: S). Point n -> Cube
cubeOf Point n
r)
  PFlip Point n
_        -> Cube
KOther
  PUnflip Point n
_      -> Cube
KOther
  PModApp{}      -> Cube
KOther
  PModExtract{}  -> Cube
KOther
  PApp Cube
c Point n
_ Point n
_     -> Cube
c
  PHole Maybe VarIdent
_        -> Cube
KOther
  POpaque String
_      -> Cube
KOther
  where
    interval :: Cube -> Cube -> Cube
interval Cube
K2 Cube
K2 = Cube
K2
    interval Cube
KI Cube
KI = Cube
KI
    interval Cube
KI Cube
K2 = Cube
KI
    interval Cube
K2 Cube
KI = Cube
KI
    interval Cube
_ Cube
_         = Cube
KOther

-- | The first component of a point of a product cube.
pointFirst :: Point n -> Point n
pointFirst :: forall (n :: S). Point n -> Point n
pointFirst (PPair Point n
a Point n
_) = Point n
a
pointFirst Point n
p           = Point n -> Point n
forall (n :: S). Point n -> Point n
PFst Point n
p

pointSecond :: Point n -> Point n
pointSecond :: forall (n :: S). Point n -> Point n
pointSecond (PPair Point n
_ Point n
b) = Point n
b
pointSecond Point n
p           = Point n -> Point n
forall (n :: S). Point n -> Point n
PSnd Point n
p

-- | Conjunction, with ⊥ absorbing and ⊤ dropped.
topeAnd :: Tope n -> Tope n -> Tope n
topeAnd :: forall (n :: S). Tope n -> Tope n -> Tope n
topeAnd Tope n
TBot Tope n
_ = Tope n
forall (n :: S). Tope n
TBot
topeAnd Tope n
_ Tope n
TBot = Tope n
forall (n :: S). Tope n
TBot
topeAnd Tope n
TTop Tope n
r = Tope n
r
topeAnd Tope n
l Tope n
TTop = Tope n
l
topeAnd Tope n
l Tope n
r    = Tope n -> Tope n -> Tope n
forall (n :: S). Tope n -> Tope n -> Tope n
TAnd Tope n
l Tope n
r

-- | Disjunction, with ⊥ dropped.
topeOr :: Tope n -> Tope n -> Tope n
topeOr :: forall (n :: S). Tope n -> Tope n -> Tope n
topeOr Tope n
TBot Tope n
r = Tope n
r
topeOr Tope n
l Tope n
TBot = Tope n
l
topeOr Tope n
l Tope n
r    = Tope n -> Tope n -> Tope n
forall (n :: S). Tope n -> Tope n -> Tope n
TOr Tope n
l Tope n
r

isLatticePoint :: Point n -> Bool
isLatticePoint :: forall (n :: S). Point n -> Bool
isLatticePoint PSup{} = Bool
True
isLatticePoint PInf{} = Bool
True
isLatticePoint Point n
_      = Bool
False

-- | A tope, together with the modalities under which it is available: the
-- lock accumulated since it was assumed, and the modality it was assumed
-- at.
data ModalTope n = ModalTope
  { forall (n :: S). ModalTope n -> TModality
tModAccum :: TModality
  , forall (n :: S). ModalTope n -> TModality
tModVar   :: TModality
  , forall (n :: S). ModalTope n -> Tope n
tTope     :: Tope n
  } deriving (ModalTope n -> ModalTope n -> Bool
(ModalTope n -> ModalTope n -> Bool)
-> (ModalTope n -> ModalTope n -> Bool) -> Eq (ModalTope n)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall (n :: S). ModalTope n -> ModalTope n -> Bool
$c== :: forall (n :: S). ModalTope n -> ModalTope n -> Bool
== :: ModalTope n -> ModalTope n -> Bool
$c/= :: forall (n :: S). ModalTope n -> ModalTope n -> Bool
/= :: ModalTope n -> ModalTope n -> Bool
Eq, Eq (ModalTope n)
Eq (ModalTope n) =>
(ModalTope n -> ModalTope n -> Ordering)
-> (ModalTope n -> ModalTope n -> Bool)
-> (ModalTope n -> ModalTope n -> Bool)
-> (ModalTope n -> ModalTope n -> Bool)
-> (ModalTope n -> ModalTope n -> Bool)
-> (ModalTope n -> ModalTope n -> ModalTope n)
-> (ModalTope n -> ModalTope n -> ModalTope n)
-> Ord (ModalTope n)
ModalTope n -> ModalTope n -> Bool
ModalTope n -> ModalTope n -> Ordering
ModalTope n -> ModalTope n -> ModalTope n
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
forall (n :: S). Eq (ModalTope n)
forall (n :: S). ModalTope n -> ModalTope n -> Bool
forall (n :: S). ModalTope n -> ModalTope n -> Ordering
forall (n :: S). ModalTope n -> ModalTope n -> ModalTope n
$ccompare :: forall (n :: S). ModalTope n -> ModalTope n -> Ordering
compare :: ModalTope n -> ModalTope n -> Ordering
$c< :: forall (n :: S). ModalTope n -> ModalTope n -> Bool
< :: ModalTope n -> ModalTope n -> Bool
$c<= :: forall (n :: S). ModalTope n -> ModalTope n -> Bool
<= :: ModalTope n -> ModalTope n -> Bool
$c> :: forall (n :: S). ModalTope n -> ModalTope n -> Bool
> :: ModalTope n -> ModalTope n -> Bool
$c>= :: forall (n :: S). ModalTope n -> ModalTope n -> Bool
>= :: ModalTope n -> ModalTope n -> Bool
$cmax :: forall (n :: S). ModalTope n -> ModalTope n -> ModalTope n
max :: ModalTope n -> ModalTope n -> ModalTope n
$cmin :: forall (n :: S). ModalTope n -> ModalTope n -> ModalTope n
min :: ModalTope n -> ModalTope n -> ModalTope n
Ord, Int -> ModalTope n -> ShowS
[ModalTope n] -> ShowS
ModalTope n -> String
(Int -> ModalTope n -> ShowS)
-> (ModalTope n -> String)
-> ([ModalTope n] -> ShowS)
-> Show (ModalTope n)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall (n :: S). Int -> ModalTope n -> ShowS
forall (n :: S). [ModalTope n] -> ShowS
forall (n :: S). ModalTope n -> String
$cshowsPrec :: forall (n :: S). Int -> ModalTope n -> ShowS
showsPrec :: Int -> ModalTope n -> ShowS
$cshow :: forall (n :: S). ModalTope n -> String
show :: ModalTope n -> String
$cshowList :: forall (n :: S). [ModalTope n] -> ShowS
showList :: [ModalTope n] -> ShowS
Show)

plainTope :: Tope n -> ModalTope n
plainTope :: forall (n :: S). Tope n -> ModalTope n
plainTope = TModality -> TModality -> Tope n -> ModalTope n
forall (n :: S). TModality -> TModality -> Tope n -> ModalTope n
ModalTope TModality
Id TModality
Id

isAccessible :: ModalTope n -> Bool
isAccessible :: forall (n :: S). ModalTope n -> Bool
isAccessible ModalTope n
mt = TModality -> TModality -> Bool
coe (ModalTope n -> TModality
forall (n :: S). ModalTope n -> TModality
tModVar ModalTope n
mt) (ModalTope n -> TModality
forall (n :: S). ModalTope n -> TModality
tModAccum ModalTope n
mt)

filterAccessible :: [ModalTope n] -> [ModalTope n]
filterAccessible :: forall (n :: S). [ModalTope n] -> [ModalTope n]
filterAccessible = (ModalTope n -> Bool) -> [ModalTope n] -> [ModalTope n]
forall a. (a -> Bool) -> [a] -> [a]
filter ModalTope n -> Bool
forall (n :: S). ModalTope n -> Bool
isAccessible

partitionAccessible :: [ModalTope n] -> ([ModalTope n], [ModalTope n])
partitionAccessible :: forall (n :: S). [ModalTope n] -> ([ModalTope n], [ModalTope n])
partitionAccessible [ModalTope n]
topes = ((ModalTope n -> Bool) -> [ModalTope n] -> [ModalTope n]
forall a. (a -> Bool) -> [a] -> [a]
filter ModalTope n -> Bool
forall (n :: S). ModalTope n -> Bool
isAccessible [ModalTope n]
topes, (ModalTope n -> Bool) -> [ModalTope n] -> [ModalTope n]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> (ModalTope n -> Bool) -> ModalTope n -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ModalTope n -> Bool
forall (n :: S). ModalTope n -> Bool
isAccessible) [ModalTope n]
topes)

topesOf :: [ModalTope n] -> [Tope n]
topesOf :: forall (n :: S). [ModalTope n] -> [Tope n]
topesOf = (ModalTope n -> Tope n) -> [ModalTope n] -> [Tope n]
forall a b. (a -> b) -> [a] -> [b]
map ModalTope n -> Tope n
forall (n :: S). ModalTope n -> Tope n
tTope ([ModalTope n] -> [Tope n])
-> ([ModalTope n] -> [ModalTope n]) -> [ModalTope n] -> [Tope n]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [ModalTope n] -> [ModalTope n]
forall (n :: S). [ModalTope n] -> [ModalTope n]
filterAccessible

-- * The tope context

-- | The assumed topes and what the solver derives from them. The derived
-- parts are lazy: the alternatives of a disjunctive context, whether the
-- context is contradictory, and its saturation are computed by the first
-- query that needs them.
data TopeContext n = TopeContext
  { forall (n :: S). TopeContext n -> [ModalTope n]
tcTopes         :: [ModalTope n]
    -- ^ the assumed topes, newest first
  , forall (n :: S). TopeContext n -> [ModalTope n]
tcDiscrete      :: [ModalTope n]
    -- ^ the discreteness axioms of the flat cube variables in scope
  , forall (n :: S). TopeContext n -> [[ModalTope n]]
tcAlternatives  :: [[ModalTope n]]
    -- ^ the context split at its disjunctions
  , forall (n :: S). TopeContext n -> Bool
tcEntailsBottom :: Bool
  , forall (n :: S). TopeContext n -> [[ModalTope n]]
tcSaturated     :: [[ModalTope n]]
    -- ^ each alternative saturated
  }

-- | The empty context: ⊤ holds under every modality.
emptyTopeContext :: TopeContext n
emptyTopeContext :: forall (n :: S). TopeContext n
emptyTopeContext = TopeContext n -> TopeContext n
forall (n :: S). TopeContext n -> TopeContext n
refresh TopeContext
  { tcTopes :: [ModalTope n]
tcTopes =
      [ TModality -> TModality -> Tope n -> ModalTope n
forall (n :: S). TModality -> TModality -> Tope n -> ModalTope n
ModalTope TModality
Id TModality
Id    Tope n
forall (n :: S). Tope n
TTop
      , TModality -> TModality -> Tope n -> ModalTope n
forall (n :: S). TModality -> TModality -> Tope n -> ModalTope n
ModalTope TModality
Id TModality
Flat  Tope n
forall (n :: S). Tope n
TTop
      , TModality -> TModality -> Tope n -> ModalTope n
forall (n :: S). TModality -> TModality -> Tope n -> ModalTope n
ModalTope TModality
Id TModality
Op    Tope n
forall (n :: S). Tope n
TTop
      , TModality -> TModality -> Tope n -> ModalTope n
forall (n :: S). TModality -> TModality -> Tope n -> ModalTope n
ModalTope TModality
Id TModality
Sharp Tope n
forall (n :: S). Tope n
TTop ]
  , tcDiscrete :: [ModalTope n]
tcDiscrete = []
  , tcAlternatives :: [[ModalTope n]]
tcAlternatives = []
  , tcEntailsBottom :: Bool
tcEntailsBottom = Bool
False
  , tcSaturated :: [[ModalTope n]]
tcSaturated = []
  }

-- | Recompute the derived parts, lazily.
refresh :: TopeContext n -> TopeContext n
refresh :: forall (n :: S). TopeContext n -> TopeContext n
refresh TopeContext n
tc = TopeContext n
tc
  { tcAlternatives = alternativesOf (tcTopes tc)
  , tcEntailsBottom = entailsFrom tc (tcTopes tc) TBot
  , tcSaturated = saturateForEntailment (tcDiscrete tc) (tcTopes tc)
  }
  where
    alternativesOf :: [ModalTope n] -> [[ModalTope n]]
alternativesOf [ModalTope n]
topes = ([ModalTope n] -> [ModalTope n])
-> [[ModalTope n]] -> [[ModalTope n]]
forall a b. (a -> b) -> [a] -> [b]
map [ModalTope n] -> [ModalTope n]
forall a. Eq a => [a] -> [a]
nub ([ModalTope n] -> [[ModalTope n]]
forall {n :: S}. [ModalTope n] -> [[ModalTope n]]
simplifyLHSwithDisjunctions [ModalTope n]
topes)

-- | The assumed topes, newest first, with their modalities.
contextTopes :: TopeContext n -> [ModalTope n]
contextTopes :: forall (n :: S). TopeContext n -> [ModalTope n]
contextTopes = TopeContext n -> [ModalTope n]
forall (n :: S). TopeContext n -> [ModalTope n]
tcTopes

-- | The assumed topes that are available at the current lock.
accessibleTopes :: TopeContext n -> [Tope n]
accessibleTopes :: forall (n :: S). TopeContext n -> [Tope n]
accessibleTopes = [ModalTope n] -> [Tope n]
forall (n :: S). [ModalTope n] -> [Tope n]
topesOf ([ModalTope n] -> [Tope n])
-> (TopeContext n -> [ModalTope n]) -> TopeContext n -> [Tope n]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TopeContext n -> [ModalTope n]
forall (n :: S). TopeContext n -> [ModalTope n]
tcTopes

-- | Assume a tope. A tope that says nothing new (a reflexive equation, or
-- one already assumed) leaves the context as it is, so that the derived
-- parts stay shared.
assume :: Tope n -> TopeContext n -> TopeContext n
assume :: forall (n :: S). Tope n -> TopeContext n -> TopeContext n
assume Tope n
tope TopeContext n
tc
  | Bool
noNewInformation = TopeContext n
tc
  | Bool
otherwise = TopeContext n -> TopeContext n
forall (n :: S). TopeContext n -> TopeContext n
refresh TopeContext n
tc { tcTopes = mt : tcTopes tc }
  where
    mt :: ModalTope n
mt = Tope n -> ModalTope n
forall (n :: S). Tope n -> ModalTope n
plainTope Tope n
tope
    noNewInformation :: Bool
noNewInformation = case Tope n
tope of
      Tope n
TTop           -> Bool
True
      TEq Point n
x Point n
y | Point n
x Point n -> Point n -> Bool
forall a. Eq a => a -> a -> Bool
== Point n
y -> Bool
True
      Tope n
_              -> ModalTope n
mt ModalTope n -> [ModalTope n] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` TopeContext n -> [ModalTope n]
forall (n :: S). TopeContext n -> [ModalTope n]
tcTopes TopeContext n
tc

-- | Add a discreteness axiom (a flat point of an interval is an endpoint).
assumeDiscrete :: Tope n -> TopeContext n -> TopeContext n
assumeDiscrete :: forall (n :: S). Tope n -> TopeContext n -> TopeContext n
assumeDiscrete Tope n
tope TopeContext n
tc = TopeContext n -> TopeContext n
forall (n :: S). TopeContext n -> TopeContext n
refresh TopeContext n
tc { tcDiscrete = plainTope tope : tcDiscrete tc }

-- | Accumulate a lock over the assumed topes.
applyModality :: TModality -> TopeContext n -> TopeContext n
applyModality :: forall (n :: S). TModality -> TopeContext n -> TopeContext n
applyModality TModality
Id TopeContext n
tc = TopeContext n
tc
applyModality TModality
md TopeContext n
tc = TopeContext n -> TopeContext n
forall (n :: S). TopeContext n -> TopeContext n
refresh TopeContext n
tc
  { tcTopes = map lock (tcTopes tc)
  , tcDiscrete = map lock (tcDiscrete tc)
  }
  where
    lock :: ModalTope n -> ModalTope n
lock ModalTope n
mt = ModalTope n
mt { tModAccum = comp (tModAccum mt) md }

-- * Queries

-- | Does the context entail the tope?
entails :: TopeContext n -> Tope n -> Bool
entails :: forall (n :: S). TopeContext n -> Tope n -> Bool
entails TopeContext n
tc Tope n
goal = ([ModalTope n] -> Bool) -> [[ModalTope n]] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all ([ModalTope n] -> Tope n -> Bool
forall (n :: S). [ModalTope n] -> Tope n -> Bool
`solveRHS` Tope n
goal) (TopeContext n -> [[ModalTope n]]
forall (n :: S). TopeContext n -> [[ModalTope n]]
tcSaturated TopeContext n
tc)

-- | Is the context contradictory?
entailsBottom :: TopeContext n -> Bool
entailsBottom :: forall (n :: S). TopeContext n -> Bool
entailsBottom = TopeContext n -> Bool
forall (n :: S). TopeContext n -> Bool
tcEntailsBottom

-- | Does the context entail the disjunction of the topes: is every point
-- of the context covered by one of them?
entailsUnion :: TopeContext n -> [Tope n] -> Bool
entailsUnion :: forall (n :: S). TopeContext n -> [Tope n] -> Bool
entailsUnion TopeContext n
tc [Tope n]
topes = TopeContext n -> Tope n -> Bool
forall (n :: S). TopeContext n -> Tope n -> Bool
entails TopeContext n
tc ((Tope n -> Tope n -> Tope n) -> Tope n -> [Tope n] -> Tope n
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Tope n -> Tope n -> Tope n
forall (n :: S). Tope n -> Tope n -> Tope n
topeOr Tope n
forall (n :: S). Tope n
TBot [Tope n]
topes)

-- | Does the tope entail the conjunction of the context's accessible topes?
entailedBy :: TopeContext n -> Tope n -> Bool
entailedBy :: forall (n :: S). TopeContext n -> Tope n -> Bool
entailedBy TopeContext n
tc Tope n
tope = TopeContext n -> [ModalTope n] -> Tope n -> Bool
forall (n :: S). TopeContext n -> [ModalTope n] -> Tope n -> Bool
entailsFrom TopeContext n
tc [Tope n -> ModalTope n
forall (n :: S). Tope n -> ModalTope n
plainTope Tope n
tope] ((Tope n -> Tope n -> Tope n) -> Tope n -> [Tope n] -> Tope n
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Tope n -> Tope n -> Tope n
forall (n :: S). Tope n -> Tope n -> Tope n
topeAnd Tope n
forall (n :: S). Tope n
TTop (TopeContext n -> [Tope n]
forall (n :: S). TopeContext n -> [Tope n]
accessibleTopes TopeContext n
tc))

-- | Is the tope disjoint from the context: does assuming it make the
-- context contradictory?
disjoint :: TopeContext n -> Tope n -> Bool
disjoint :: forall (n :: S). TopeContext n -> Tope n -> Bool
disjoint TopeContext n
tc Tope n
tope = TopeContext n -> [ModalTope n] -> Tope n -> Bool
forall (n :: S). TopeContext n -> [ModalTope n] -> Tope n -> Bool
entailsFrom TopeContext n
tc (Tope n -> ModalTope n
forall (n :: S). Tope n -> ModalTope n
plainTope Tope n
tope ModalTope n -> [ModalTope n] -> [ModalTope n]
forall a. a -> [a] -> [a]
: TopeContext n -> [ModalTope n]
forall (n :: S). TopeContext n -> [ModalTope n]
tcTopes TopeContext n
tc) Tope n
forall (n :: S). Tope n
TBot

-- | Are two topes equivalent on their own, under the context's
-- discreteness axioms?
equivalent :: TopeContext n -> Tope n -> Tope n -> Bool
equivalent :: forall (n :: S). TopeContext n -> Tope n -> Tope n -> Bool
equivalent TopeContext n
tc Tope n
l Tope n
r = Tope n
l Tope n -> Tope n -> Bool
forall a. Eq a => a -> a -> Bool
== Tope n
r Bool -> Bool -> Bool
|| (TopeContext n -> [Tope n] -> Tope n -> Bool
forall (n :: S). TopeContext n -> [Tope n] -> Tope n -> Bool
topesEntail TopeContext n
tc [Tope n
l] Tope n
r Bool -> Bool -> Bool
&& TopeContext n -> [Tope n] -> Tope n -> Bool
forall (n :: S). TopeContext n -> [Tope n] -> Tope n -> Bool
topesEntail TopeContext n
tc [Tope n
r] Tope n
l)

-- | Do the topes alone (with the discreteness axioms) entail the goal?
topesEntail :: TopeContext n -> [Tope n] -> Tope n -> Bool
topesEntail :: forall (n :: S). TopeContext n -> [Tope n] -> Tope n -> Bool
topesEntail TopeContext n
tc [Tope n]
topes = TopeContext n -> [ModalTope n] -> Tope n -> Bool
forall (n :: S). TopeContext n -> [ModalTope n] -> Tope n -> Bool
entailsFrom TopeContext n
tc ((Tope n -> ModalTope n) -> [Tope n] -> [ModalTope n]
forall a b. (a -> b) -> [a] -> [b]
map Tope n -> ModalTope n
forall (n :: S). Tope n -> ModalTope n
plainTope [Tope n]
topes)

-- | The alternatives of a disjunctive context, each as the context with
-- that alternative assumed instead; a context with one alternative has
-- none to split.
alternatives :: TopeContext n -> [TopeContext n]
alternatives :: forall (n :: S). TopeContext n -> [TopeContext n]
alternatives TopeContext n
tc = case TopeContext n -> [[ModalTope n]]
forall (n :: S). TopeContext n -> [[ModalTope n]]
tcAlternatives TopeContext n
tc of
  [[ModalTope n]
_] -> []
  [[ModalTope n]]
alts -> [ TopeContext n -> TopeContext n
forall (n :: S). TopeContext n -> TopeContext n
refresh TopeContext n
tc { tcTopes = alt } | [ModalTope n]
alt <- [[ModalTope n]]
alts ]

-- | Does a check hold in every alternative of the context? A context with
-- one alternative runs the check once, on itself.
inAlternatives :: TopeContext n -> (TopeContext n -> Bool) -> Bool
inAlternatives :: forall (n :: S). TopeContext n -> (TopeContext n -> Bool) -> Bool
inAlternatives TopeContext n
tc TopeContext n -> Bool
check = case TopeContext n -> [TopeContext n]
forall (n :: S). TopeContext n -> [TopeContext n]
alternatives TopeContext n
tc of
  []   -> TopeContext n -> Bool
check TopeContext n
tc
  [TopeContext n]
alts -> (TopeContext n -> Bool) -> [TopeContext n] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all TopeContext n -> Bool
check [TopeContext n]
alts

-- | Entailment from the given topes, with the context's discreteness
-- axioms.
entailsFrom :: TopeContext n -> [ModalTope n] -> Tope n -> Bool
entailsFrom :: forall (n :: S). TopeContext n -> [ModalTope n] -> Tope n -> Bool
entailsFrom TopeContext n
tc [ModalTope n]
modalTopes Tope n
goal =
  ([ModalTope n] -> Bool) -> [[ModalTope n]] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all ([ModalTope n] -> Tope n -> Bool
forall (n :: S). [ModalTope n] -> Tope n -> Bool
`solveRHS` Tope n
goal) ([ModalTope n] -> [ModalTope n] -> [[ModalTope n]]
forall (n :: S). [ModalTope n] -> [ModalTope n] -> [[ModalTope n]]
saturateForEntailment (TopeContext n -> [ModalTope n]
forall (n :: S). TopeContext n -> [ModalTope n]
tcDiscrete TopeContext n
tc) [ModalTope n]
modalTopes)

-- * Saturation

-- | The preprocessing before a search: dedup, split off the context's
-- disjunctions, and saturate each alternative.
saturateForEntailment :: [ModalTope n] -> [ModalTope n] -> [[ModalTope n]]
saturateForEntailment :: forall (n :: S). [ModalTope n] -> [ModalTope n] -> [[ModalTope n]]
saturateForEntailment [ModalTope n]
discrete [ModalTope n]
modalTopes =
  ([ModalTope n] -> [ModalTope n])
-> [[ModalTope n]] -> [[ModalTope n]]
forall a b. (a -> b) -> [a] -> [b]
map ([ModalTope n] -> [ModalTope n]
forall (n :: S). [ModalTope n] -> [ModalTope n]
saturateTopes ([ModalTope n] -> [ModalTope n])
-> ([ModalTope n] -> [ModalTope n])
-> [ModalTope n]
-> [ModalTope n]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [ModalTope n] -> [ModalTope n]
forall (n :: S). [ModalTope n] -> [ModalTope n]
saturateBottom ([ModalTope n] -> [ModalTope n])
-> ([ModalTope n] -> [ModalTope n])
-> [ModalTope n]
-> [ModalTope n]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [ModalTope n] -> [ModalTope n]
forall (n :: S). [ModalTope n] -> [ModalTope n]
saturateInv) [[ModalTope n]]
alts
  where
    alts :: [[ModalTope n]]
alts = [ModalTope n] -> [[ModalTope n]]
forall {n :: S}. [ModalTope n] -> [[ModalTope n]]
simplifyLHSwithDisjunctions ([ModalTope n] -> [ModalTope n]
forall a. Eq a => [a] -> [a]
nub ([ModalTope n]
modalTopes [ModalTope n] -> [ModalTope n] -> [ModalTope n]
forall a. Semigroup a => a -> a -> a
<> [ModalTope n]
discrete))

saturateTopes :: [ModalTope n] -> [ModalTope n]
saturateTopes :: forall (n :: S). [ModalTope n] -> [ModalTope n]
saturateTopes [ModalTope n]
topes = [ModalTope n]
saturated [ModalTope n] -> [ModalTope n] -> [ModalTope n]
forall a. Semigroup a => a -> a -> a
<> [ModalTope n]
inaccessible
  where
    ([ModalTope n]
accessible, [ModalTope n]
inaccessible) = [ModalTope n] -> ([ModalTope n], [ModalTope n])
forall (n :: S). [ModalTope n] -> ([ModalTope n], [ModalTope n])
partitionAccessible [ModalTope n]
topes
    saturated :: [ModalTope n]
saturated = ([ModalTope n] -> [ModalTope n] -> [ModalTope n])
-> [ModalTope n] -> [ModalTope n]
forall a. Ord a => ([a] -> [a] -> [a]) -> [a] -> [a]
saturateWith
      (\[ModalTope n]
new [ModalTope n]
old -> (Tope n -> ModalTope n) -> [Tope n] -> [ModalTope n]
forall a b. (a -> b) -> [a] -> [b]
map Tope n -> ModalTope n
forall (n :: S). Tope n -> ModalTope n
plainTope ([Tope n] -> [Tope n] -> [Tope n]
forall (n :: S). [Tope n] -> [Tope n] -> [Tope n]
generateTopes ((ModalTope n -> Tope n) -> [ModalTope n] -> [Tope n]
forall a b. (a -> b) -> [a] -> [b]
map ModalTope n -> Tope n
forall (n :: S). ModalTope n -> Tope n
tTope [ModalTope n]
new) ((ModalTope n -> Tope n) -> [ModalTope n] -> [Tope n]
forall a b. (a -> b) -> [a] -> [b]
map ModalTope n -> Tope n
forall (n :: S). ModalTope n -> Tope n
tTope [ModalTope n]
old)))
      [ModalTope n]
accessible

-- | Inversion under @ᵒᵖ@, not implemented yet, so the identity. This loses
-- nothing in the non-modal fragment, where every tope sits at the identity
-- modality. Under @ᵒᵖ@ the solver misses the entailments inversion gives.
saturateInv :: [ModalTope n] -> [ModalTope n]
saturateInv :: forall (n :: S). [ModalTope n] -> [ModalTope n]
saturateInv = [ModalTope n] -> [ModalTope n]
forall a. a -> a
id

-- | Ex falso for ⊥, lifted across modalities: a contradiction among the
-- topes available at the identity modality entails @_μ ⊥@ at every lock
-- where an available tope was hidden.
saturateBottom :: [ModalTope n] -> [ModalTope n]
saturateBottom :: forall (n :: S). [ModalTope n] -> [ModalTope n]
saturateBottom [ModalTope n]
modalTopes
  | [TModality] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [TModality]
droppedAccums = [ModalTope n]
modalTopes
  | Bool
botDerivable       = [ModalTope n]
modalTopes [ModalTope n] -> [ModalTope n] -> [ModalTope n]
forall a. Semigroup a => a -> a -> a
<> [ModalTope n]
forall {n :: S}. [ModalTope n]
fresh
  | Bool
otherwise          = [ModalTope n]
modalTopes
  where
    idAccessible :: [ModalTope n]
idAccessible  = (ModalTope n -> Bool) -> [ModalTope n] -> [ModalTope n]
forall a. (a -> Bool) -> [a] -> [a]
filter (\ModalTope n
mt -> TModality -> TModality -> Bool
coe (ModalTope n -> TModality
forall (n :: S). ModalTope n -> TModality
tModVar ModalTope n
mt) TModality
Id) [ModalTope n]
modalTopes
    droppedAccums :: [TModality]
droppedAccums = [TModality] -> [TModality]
forall a. Eq a => [a] -> [a]
nub [ ModalTope n -> TModality
forall (n :: S). ModalTope n -> TModality
tModAccum ModalTope n
mt | ModalTope n
mt <- [ModalTope n]
idAccessible, Bool -> Bool
not (ModalTope n -> Bool
forall (n :: S). ModalTope n -> Bool
isAccessible ModalTope n
mt) ]
    saturatedId :: [Tope n]
saturatedId   = ([Tope n] -> [Tope n] -> [Tope n]) -> [Tope n] -> [Tope n]
forall a. Ord a => ([a] -> [a] -> [a]) -> [a] -> [a]
saturateWith [Tope n] -> [Tope n] -> [Tope n]
forall (n :: S). [Tope n] -> [Tope n] -> [Tope n]
generateTopes ((ModalTope n -> Tope n) -> [ModalTope n] -> [Tope n]
forall a b. (a -> b) -> [a] -> [b]
map ModalTope n -> Tope n
forall (n :: S). ModalTope n -> Tope n
tTope [ModalTope n]
idAccessible)
    botDerivable :: Bool
botDerivable  = Tope n
forall (n :: S). Tope n
TBot Tope n -> [Tope n] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Tope n]
saturatedId
    fresh :: [ModalTope n]
fresh = [ ModalTope n
forall {n :: S}. ModalTope n
mt
            | TModality
acc <- [TModality]
droppedAccums
            , let mt :: ModalTope n
mt = TModality -> TModality -> Tope n -> ModalTope n
forall (n :: S). TModality -> TModality -> Tope n -> ModalTope n
ModalTope TModality
acc TModality
acc Tope n
forall (n :: S). Tope n
TBot
            , ModalTope n
forall {n :: S}. ModalTope n
mt ModalTope n -> [ModalTope n] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [ModalTope n]
modalTopes ]

-- | Saturate a set under a step that derives new elements from the new
-- and the old ones, until nothing new comes.
saturateWith :: Ord a => ([a] -> [a] -> [a]) -> [a] -> [a]
saturateWith :: forall a. Ord a => ([a] -> [a] -> [a]) -> [a] -> [a]
saturateWith [a] -> [a] -> [a]
step [a]
zs = [a] -> [a] -> Set a -> [a]
go ([a] -> [a]
nubOrd [a]
zs) [] ([a] -> Set a
forall a. Ord a => [a] -> Set a
Set.fromList [a]
zs)
  where
    go :: [a] -> [a] -> Set a -> [a]
go [a]
lastNew [a]
xs Set a
seen
      | [a] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [a]
new  = [a]
lastNew
      | Bool
otherwise = [a]
lastNew [a] -> [a] -> [a]
forall a. Semigroup a => a -> a -> a
<> [a] -> [a] -> Set a -> [a]
go [a]
new [a]
xs' Set a
seen'
      where
        xs' :: [a]
xs' = [a]
lastNew [a] -> [a] -> [a]
forall a. Semigroup a => a -> a -> a
<> [a]
xs
        new :: [a]
new = (a -> Bool) -> [a] -> [a]
forall a. (a -> Bool) -> [a] -> [a]
filter (a -> Set a -> Bool
forall a. Ord a => a -> Set a -> Bool
`Set.notMember` Set a
seen) ([a] -> [a]
nubOrd ([a] -> [a] -> [a]
step [a]
lastNew [a]
xs))
        seen' :: Set a
seen' = (a -> Set a -> Set a) -> Set a -> [a] -> Set a
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr a -> Set a -> Set a
forall a. Ord a => a -> Set a -> Set a
Set.insert Set a
seen [a]
new
    nubOrd :: [a] -> [a]
nubOrd = Set a -> [a]
forall a. Set a -> [a]
Set.toList (Set a -> [a]) -> ([a] -> Set a) -> [a] -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> Set a
forall a. Ord a => [a] -> Set a
Set.fromList

generateTopes :: [Tope n] -> [Tope n] -> [Tope n]
generateTopes :: forall (n :: S). [Tope n] -> [Tope n] -> [Tope n]
generateTopes [Tope n]
newTopes [Tope n]
oldTopes
  | Tope n
forall (n :: S). Tope n
TBot Tope n -> [Tope n] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Tope n]
newTopes = []
  | Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TEq Point n
forall (n :: S). Point n
P2_0 Point n
forall (n :: S). Point n
P2_1 Tope n -> [Tope n] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Tope n]
newTopes = [Tope n
forall (n :: S). Tope n
TBot]
  | Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TEq Point n
forall (n :: S). Point n
PI_0 Point n
forall (n :: S). Point n
PI_1 Tope n -> [Tope n] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Tope n]
newTopes = [Tope n
forall (n :: S). Tope n
TBot]
  | [Tope n] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Tope n]
oldTopes Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
100 = []    -- FIXME
  | Bool
otherwise = [[Tope n]] -> [Tope n]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
      [  -- symmetry EQ
        [ Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TEq Point n
y Point n
x | TEq Point n
x Point n
y <- [Tope n]
newTopes ]
        -- transitivity EQ (1)
      , [ Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TEq Point n
x Point n
z
        | TEq Point n
x Point n
y : [Tope n]
newTopes' <- [Tope n] -> [[Tope n]]
forall a. [a] -> [[a]]
tails [Tope n]
newTopes
        , TEq Point n
y' Point n
z <- [Tope n]
newTopes' [Tope n] -> [Tope n] -> [Tope n]
forall a. Semigroup a => a -> a -> a
<> [Tope n]
oldTopes
        , Point n
y Point n -> Point n -> Bool
forall a. Eq a => a -> a -> Bool
== Point n
y' ]
        -- transitivity EQ (2)
      , [ Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TEq Point n
x Point n
z
        | TEq Point n
y Point n
z : [Tope n]
newTopes' <- [Tope n] -> [[Tope n]]
forall a. [a] -> [[a]]
tails [Tope n]
newTopes
        , TEq Point n
x Point n
y' <- [Tope n]
newTopes' [Tope n] -> [Tope n] -> [Tope n]
forall a. Semigroup a => a -> a -> a
<> [Tope n]
oldTopes
        , Point n
y Point n -> Point n -> Bool
forall a. Eq a => a -> a -> Bool
== Point n
y' ]

        -- transitivity LEQ (1)
      , [ Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TLeq Point n
x Point n
z
        | TLeq Point n
x Point n
y : [Tope n]
newTopes' <- [Tope n] -> [[Tope n]]
forall a. [a] -> [[a]]
tails [Tope n]
newTopes
        , TLeq Point n
y' Point n
z <- [Tope n]
newTopes' [Tope n] -> [Tope n] -> [Tope n]
forall a. Semigroup a => a -> a -> a
<> [Tope n]
oldTopes
        , Point n
y Point n -> Point n -> Bool
forall a. Eq a => a -> a -> Bool
== Point n
y' ]
        -- transitivity LEQ (2)
      , [ Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TLeq Point n
x Point n
z
        | TLeq Point n
y Point n
z : [Tope n]
newTopes' <- [Tope n] -> [[Tope n]]
forall a. [a] -> [[a]]
tails [Tope n]
newTopes
        , TLeq Point n
x Point n
y' <- [Tope n]
newTopes' [Tope n] -> [Tope n] -> [Tope n]
forall a. Semigroup a => a -> a -> a
<> [Tope n]
oldTopes
        , Point n
y Point n -> Point n -> Bool
forall a. Eq a => a -> a -> Bool
== Point n
y' ]

        -- antisymmetry LEQ
      , [ Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TEq Point n
x Point n
y
        | TLeq Point n
x Point n
y : [Tope n]
newTopes' <- [Tope n] -> [[Tope n]]
forall a. [a] -> [[a]]
tails [Tope n]
newTopes
        , TLeq Point n
y' Point n
x' <- [Tope n]
newTopes' [Tope n] -> [Tope n] -> [Tope n]
forall a. Semigroup a => a -> a -> a
<> [Tope n]
oldTopes
        , Point n
y Point n -> Point n -> Bool
forall a. Eq a => a -> a -> Bool
== Point n
y'
        , Point n
x Point n -> Point n -> Bool
forall a. Eq a => a -> a -> Bool
== Point n
x' ]

        -- transitivity EQ-LEQ (1)
      , [ Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TLeq Point n
x Point n
z
        | TEq Point n
y Point n
z : [Tope n]
newTopes' <- [Tope n] -> [[Tope n]]
forall a. [a] -> [[a]]
tails [Tope n]
newTopes
        , TLeq Point n
x Point n
y' <- [Tope n]
newTopes' [Tope n] -> [Tope n] -> [Tope n]
forall a. Semigroup a => a -> a -> a
<> [Tope n]
oldTopes
        , Point n
y Point n -> Point n -> Bool
forall a. Eq a => a -> a -> Bool
== Point n
y' ]
        -- transitivity EQ-LEQ (2)
      , [ Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TLeq Point n
x Point n
z
        | TEq Point n
x Point n
y : [Tope n]
newTopes' <- [Tope n] -> [[Tope n]]
forall a. [a] -> [[a]]
tails [Tope n]
newTopes
        , TLeq Point n
y' Point n
z <- [Tope n]
newTopes' [Tope n] -> [Tope n] -> [Tope n]
forall a. Semigroup a => a -> a -> a
<> [Tope n]
oldTopes
        , Point n
y Point n -> Point n -> Bool
forall a. Eq a => a -> a -> Bool
== Point n
y' ]
        -- transitivity EQ-LEQ (3)
      , [ Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TLeq Point n
x Point n
z
        | TLeq Point n
y Point n
z : [Tope n]
newTopes' <- [Tope n] -> [[Tope n]]
forall a. [a] -> [[a]]
tails [Tope n]
newTopes
        , TEq Point n
x Point n
y' <- [Tope n]
newTopes' [Tope n] -> [Tope n] -> [Tope n]
forall a. Semigroup a => a -> a -> a
<> [Tope n]
oldTopes
        , Point n
y Point n -> Point n -> Bool
forall a. Eq a => a -> a -> Bool
== Point n
y' ]
        -- transitivity EQ-LEQ (4)
      , [ Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TLeq Point n
x Point n
z
        | TLeq Point n
x Point n
y : [Tope n]
newTopes' <- [Tope n] -> [[Tope n]]
forall a. [a] -> [[a]]
tails [Tope n]
newTopes
        , TEq Point n
y' Point n
z <- [Tope n]
newTopes' [Tope n] -> [Tope n] -> [Tope n]
forall a. Semigroup a => a -> a -> a
<> [Tope n]
oldTopes
        , Point n
y Point n -> Point n -> Bool
forall a. Eq a => a -> a -> Bool
== Point n
y' ]

        -- a point below 0 or above 1 is that endpoint
      , [ Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TEq Point n
x Point n
y | TLeq Point n
x y :: Point n
y@Point n
P2_0 <- [Tope n]
newTopes ]
      , [ Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TEq Point n
x Point n
y | TLeq x :: Point n
x@Point n
P2_1 Point n
y <- [Tope n]
newTopes ]
      , [ Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TEq Point n
x Point n
y | TLeq Point n
x y :: Point n
y@Point n
PI_0 <- [Tope n]
newTopes ]
      , [ Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TEq Point n
x Point n
y | TLeq x :: Point n
x@Point n
PI_1 Point n
y <- [Tope n]
newTopes ]

        -- subtyping 2 <: 𝕀: endpoints and order of 2 lift to 𝕀
      , [ Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TEq Point n
x Point n
forall (n :: S). Point n
PI_0 | TEq Point n
x Point n
P2_0 <- [Tope n]
newTopes ]
      , [ Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TEq Point n
forall (n :: S). Point n
PI_0 Point n
x | TEq Point n
P2_0 Point n
x <- [Tope n]
newTopes ]
      , [ Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TEq Point n
x Point n
forall (n :: S). Point n
PI_1 | TEq Point n
x Point n
P2_1 <- [Tope n]
newTopes ]
      , [ Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TEq Point n
forall (n :: S). Point n
PI_1 Point n
x | TEq Point n
P2_1 Point n
x <- [Tope n]
newTopes ]
      , [ Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TLeq Point n
x Point n
forall (n :: S). Point n
PI_0 | TLeq Point n
x Point n
P2_0 <- [Tope n]
newTopes ]
      , [ Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TLeq Point n
forall (n :: S). Point n
PI_0 Point n
x | TLeq Point n
P2_0 Point n
x <- [Tope n]
newTopes ]
      , [ Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TLeq Point n
x Point n
forall (n :: S). Point n
PI_1 | TLeq Point n
x Point n
P2_1 <- [Tope n]
newTopes ]
      , [ Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TLeq Point n
forall (n :: S). Point n
PI_1 Point n
x | TLeq Point n
P2_1 Point n
x <- [Tope n]
newTopes ]

      , [ Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TLeq Point n
a Point n
c | TLeq (PSup Point n
a Point n
_) Point n
c <- [Tope n]
newTopes ]
      , [ Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TLeq Point n
b Point n
c | TLeq (PSup Point n
_ Point n
b) Point n
c <- [Tope n]
newTopes ]
      , [ Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TLeq Point n
c Point n
a | TLeq Point n
c (PInf Point n
a Point n
_) <- [Tope n]
newTopes ]
      , [ Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TLeq Point n
c Point n
b | TLeq Point n
c (PInf Point n
_ Point n
b) <- [Tope n]
newTopes ]

        -- an equation between lattice points gives the two inequalities,
        -- which feed the sup and inf rules above
      , [ Tope n
leq
        | TEq Point n
x Point n
y <- [Tope n]
newTopes
        , Point n -> Bool
forall (n :: S). Point n -> Bool
isLatticePoint Point n
x Bool -> Bool -> Bool
|| Point n -> Bool
forall (n :: S). Point n -> Bool
isLatticePoint Point n
y
        , Tope n
leq <- [Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TLeq Point n
x Point n
y, Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TLeq Point n
y Point n
x] ]
      ]

-- | The lemmas about points that a disjunctive goal may case on: a point of
-- the unit cube is its point, and two points of @2@ are comparable.
generateTopesForPoints :: [Point n] -> [Tope n]
generateTopesForPoints :: forall (n :: S). [Point n] -> [Tope n]
generateTopesForPoints [Point n]
points = [[Tope n]] -> [Tope n]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[Tope n]]
topes [[Tope n]] -> [[Tope n]] -> [[Tope n]]
forall a. Semigroup a => a -> a -> a
<> [[Tope n]]
stars)
  where
    endpoints :: [Point n]
endpoints = [Point n
forall (n :: S). Point n
P2_0, Point n
forall (n :: S). Point n
P2_1, Point n
forall (n :: S). Point n
PI_0, Point n
forall (n :: S). Point n
PI_1]
    pairs :: [(Point n, Point n)]
pairs = [(Point n, Point n)] -> [(Point n, Point n)]
forall a. Eq a => [a] -> [a]
nub
      [ (Point n
x, Point n
y)
      | Point n
x : [Point n]
points' <- [Point n] -> [[Point n]]
forall a. [a] -> [[a]]
tails ((Point n -> Bool) -> [Point n] -> [Point n]
forall a. (a -> Bool) -> [a] -> [a]
filter (Point n -> [Point n] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [Point n]
forall {n :: S}. [Point n]
endpoints) [Point n]
points)
      , Point n
y <- [Point n]
points'
      , Point n
x Point n -> Point n -> Bool
forall a. Eq a => a -> a -> Bool
/= Point n
y ]
    stars :: [[Tope n]]
stars = [ [Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TEq Point n
x Point n
forall (n :: S). Point n
PStar] | Point n
x <- [Point n]
points, Point n -> Cube
forall (n :: S). Point n -> Cube
cubeOf Point n
x Cube -> Cube -> Bool
forall a. Eq a => a -> a -> Bool
== Cube
KUnit ]
    topes :: [[Tope n]]
topes =
      [ [Tope n -> Tope n -> Tope n
forall (n :: S). Tope n -> Tope n -> Tope n
TOr (Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TLeq Point n
x Point n
y) (Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TLeq Point n
y Point n
x)]
      | (Point n
x, Point n
y) <- [(Point n, Point n)]
pairs
      , Point n -> Cube
forall (n :: S). Point n -> Cube
cubeOf Point n
x Cube -> Cube -> Bool
forall a. Eq a => a -> a -> Bool
== Cube
K2, Point n -> Cube
forall (n :: S). Point n -> Cube
cubeOf Point n
y Cube -> Cube -> Bool
forall a. Eq a => a -> a -> Bool
== Cube
K2 ]

allTopePoints :: Tope n -> [Point n]
allTopePoints :: forall (n :: S). Tope n -> [Point n]
allTopePoints = [Point n] -> [Point n]
forall a. Eq a => [a] -> [a]
nub ([Point n] -> [Point n])
-> (Tope n -> [Point n]) -> Tope n -> [Point n]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Point n -> [Point n]) -> [Point n] -> [Point n]
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap Point n -> [Point n]
forall (n :: S). Point n -> [Point n]
subPoints ([Point n] -> [Point n])
-> (Tope n -> [Point n]) -> Tope n -> [Point n]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Point n] -> [Point n]
forall a. Eq a => [a] -> [a]
nub ([Point n] -> [Point n])
-> (Tope n -> [Point n]) -> Tope n -> [Point n]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Tope n -> [Point n]
forall (n :: S). Tope n -> [Point n]
topePoints

topePoints :: Tope n -> [Point n]
topePoints :: forall (n :: S). Tope n -> [Point n]
topePoints = \case
  TAnd Tope n
l Tope n
r -> Tope n -> [Point n]
forall (n :: S). Tope n -> [Point n]
topePoints Tope n
l [Point n] -> [Point n] -> [Point n]
forall a. Semigroup a => a -> a -> a
<> Tope n -> [Point n]
forall (n :: S). Tope n -> [Point n]
topePoints Tope n
r
  TOr Tope n
l Tope n
r  -> Tope n -> [Point n]
forall (n :: S). Tope n -> [Point n]
topePoints Tope n
l [Point n] -> [Point n] -> [Point n]
forall a. Semigroup a => a -> a -> a
<> Tope n -> [Point n]
forall (n :: S). Tope n -> [Point n]
topePoints Tope n
r
  TEq Point n
x Point n
y  -> [Point n
x, Point n
y]
  TLeq Point n
x Point n
y -> [Point n
x, Point n
y]
  Tope n
_        -> []

subPoints :: Point n -> [Point n]
subPoints :: forall (n :: S). Point n -> [Point n]
subPoints = \case
  p :: Point n
p@(PPair Point n
x Point n
y) -> Point n
p Point n -> [Point n] -> [Point n]
forall a. a -> [a] -> [a]
: (Point n -> [Point n]) -> [Point n] -> [Point n]
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap Point n -> [Point n]
forall (n :: S). Point n -> [Point n]
subPoints [Point n
x, Point n
y]
  p :: Point n
p@PVar{}      -> [Point n
p]
  Point n
p -> case Point n -> Cube
forall (n :: S). Point n -> Cube
cubeOf Point n
p of
    Cube
KUnit -> [Point n
p]
    Cube
K2    -> [Point n
p]
    Cube
_        -> []

-- * Simplifying the left-hand side

-- | Simplify the context, splitting disjunctions into alternatives.
simplifyLHSwithDisjunctions :: [ModalTope n] -> [[ModalTope n]]
simplifyLHSwithDisjunctions :: forall {n :: S}. [ModalTope n] -> [[ModalTope n]]
simplifyLHSwithDisjunctions [ModalTope n]
topes = ([ModalTope n] -> [ModalTope n])
-> [[ModalTope n]] -> [[ModalTope n]]
forall a b. (a -> b) -> [a] -> [b]
map [ModalTope n] -> [ModalTope n]
forall a. Eq a => [a] -> [a]
nub ([[ModalTope n]] -> [[ModalTope n]])
-> [[ModalTope n]] -> [[ModalTope n]]
forall a b. (a -> b) -> a -> b
$ case [ModalTope n]
topes of
  [] -> [[]]
  ModalTope TModality
_ TModality
_ Tope n
TTop : [ModalTope n]
topes' -> [ModalTope n] -> [[ModalTope n]]
forall {n :: S}. [ModalTope n] -> [[ModalTope n]]
simplifyLHSwithDisjunctions [ModalTope n]
topes'
  ModalTope TModality
mAcc TModality
mVar Tope n
TBot : [ModalTope n]
_ -> [[TModality -> TModality -> Tope n -> ModalTope n
forall (n :: S). TModality -> TModality -> Tope n -> ModalTope n
ModalTope TModality
mAcc TModality
mVar Tope n
forall (n :: S). Tope n
TBot]]
  ModalTope TModality
mAcc TModality
mVar (TAnd Tope n
l Tope n
r) : [ModalTope n]
topes' ->
    [ModalTope n] -> [[ModalTope n]]
forall {n :: S}. [ModalTope n] -> [[ModalTope n]]
simplifyLHSwithDisjunctions (TModality -> TModality -> Tope n -> ModalTope n
forall (n :: S). TModality -> TModality -> Tope n -> ModalTope n
ModalTope TModality
mAcc TModality
mVar Tope n
l ModalTope n -> [ModalTope n] -> [ModalTope n]
forall a. a -> [a] -> [a]
: TModality -> TModality -> Tope n -> ModalTope n
forall (n :: S). TModality -> TModality -> Tope n -> ModalTope n
ModalTope TModality
mAcc TModality
mVar Tope n
r ModalTope n -> [ModalTope n] -> [ModalTope n]
forall a. a -> [a] -> [a]
: [ModalTope n]
topes')
  ModalTope TModality
mAcc TModality
mVar (TOr Tope n
l Tope n
r) : [ModalTope n]
topes' ->
    [ModalTope n] -> [[ModalTope n]]
forall {n :: S}. [ModalTope n] -> [[ModalTope n]]
simplifyLHSwithDisjunctions (TModality -> TModality -> Tope n -> ModalTope n
forall (n :: S). TModality -> TModality -> Tope n -> ModalTope n
ModalTope TModality
mAcc TModality
mVar Tope n
l ModalTope n -> [ModalTope n] -> [ModalTope n]
forall a. a -> [a] -> [a]
: [ModalTope n]
topes')
      [[ModalTope n]] -> [[ModalTope n]] -> [[ModalTope n]]
forall a. Semigroup a => a -> a -> a
<> [ModalTope n] -> [[ModalTope n]]
forall {n :: S}. [ModalTope n] -> [[ModalTope n]]
simplifyLHSwithDisjunctions (TModality -> TModality -> Tope n -> ModalTope n
forall (n :: S). TModality -> TModality -> Tope n -> ModalTope n
ModalTope TModality
mAcc TModality
mVar Tope n
r ModalTope n -> [ModalTope n] -> [ModalTope n]
forall a. a -> [a] -> [a]
: [ModalTope n]
topes')
  ModalTope TModality
mAcc TModality
mVar (TEq (PPair Point n
x Point n
y) (PPair Point n
x' Point n
y')) : [ModalTope n]
topes' ->
    [ModalTope n] -> [[ModalTope n]]
forall {n :: S}. [ModalTope n] -> [[ModalTope n]]
simplifyLHSwithDisjunctions
      (TModality -> TModality -> Tope n -> ModalTope n
forall (n :: S). TModality -> TModality -> Tope n -> ModalTope n
ModalTope TModality
mAcc TModality
mVar (Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TEq Point n
x Point n
x') ModalTope n -> [ModalTope n] -> [ModalTope n]
forall a. a -> [a] -> [a]
: TModality -> TModality -> Tope n -> ModalTope n
forall (n :: S). TModality -> TModality -> Tope n -> ModalTope n
ModalTope TModality
mAcc TModality
mVar (Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TEq Point n
y Point n
y') ModalTope n -> [ModalTope n] -> [ModalTope n]
forall a. a -> [a] -> [a]
: [ModalTope n]
topes')
  ModalTope TModality
mAcc TModality
mVar (TModal TModality
md Tope n
inTope) : [ModalTope n]
topes' ->
    [ModalTope n] -> [[ModalTope n]]
forall {n :: S}. [ModalTope n] -> [[ModalTope n]]
simplifyLHSwithDisjunctions (TModality -> TModality -> Tope n -> ModalTope n
forall (n :: S). TModality -> TModality -> Tope n -> ModalTope n
ModalTope TModality
mAcc (TModality -> TModality -> TModality
comp TModality
mVar TModality
md) Tope n
inTope ModalTope n -> [ModalTope n] -> [ModalTope n]
forall a. a -> [a] -> [a]
: [ModalTope n]
topes')
  ModalTope n
t : [ModalTope n]
topes' -> ([ModalTope n] -> [ModalTope n])
-> [[ModalTope n]] -> [[ModalTope n]]
forall a b. (a -> b) -> [a] -> [b]
map (ModalTope n
t ModalTope n -> [ModalTope n] -> [ModalTope n]
forall a. a -> [a] -> [a]
:) ([ModalTope n] -> [[ModalTope n]]
forall {n :: S}. [ModalTope n] -> [[ModalTope n]]
simplifyLHSwithDisjunctions [ModalTope n]
topes')

-- * Solving the right-hand side

-- | Search a saturated context for the goal.
solveRHS :: [ModalTope n] -> Tope n -> Bool
solveRHS :: forall (n :: S). [ModalTope n] -> Tope n -> Bool
solveRHS [ModalTope n]
modalTopes Tope n
goal =
  let topes :: [Tope n]
topes = [ModalTope n] -> [Tope n]
forall (n :: S). [ModalTope n] -> [Tope n]
topesOf [ModalTope n]
modalTopes
  in case Tope n
goal of
    Tope n
_ | Tope n
forall (n :: S). Tope n
TBot Tope n -> [Tope n] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Tope n]
topes -> Bool
True
    Tope n
TTop -> Bool
True
    TModal TModality
md Tope n
inTope ->
      let shifted :: [ModalTope n]
shifted = (ModalTope n -> ModalTope n) -> [ModalTope n] -> [ModalTope n]
forall a b. (a -> b) -> [a] -> [b]
map (\ModalTope n
mt -> ModalTope n
mt { tModAccum = comp (tModAccum mt) md }) [ModalTope n]
modalTopes
      in [ModalTope n] -> Tope n -> Bool
forall (n :: S). [ModalTope n] -> Tope n -> Bool
solveRHS ([ModalTope n] -> [ModalTope n]
forall (n :: S). [ModalTope n] -> [ModalTope n]
saturateInv ([ModalTope n] -> [ModalTope n]
forall (n :: S). [ModalTope n] -> [ModalTope n]
saturateTopes [ModalTope n]
shifted)) Tope n
inTope
    TEq (PPair Point n
x Point n
y) (PPair Point n
x' Point n
y') ->
      [ModalTope n] -> Tope n -> Bool
forall (n :: S). [ModalTope n] -> Tope n -> Bool
solveRHS [ModalTope n]
modalTopes (Tope n -> Tope n -> Tope n
forall (n :: S). Tope n -> Tope n -> Tope n
TAnd (Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TEq Point n
x Point n
x') (Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TEq Point n
y Point n
y'))
    TEq l :: Point n
l@(PPair Point n
x Point n
y) Point n
r | KProd Cube
_ Cube
_ <- Point n -> Cube
forall (n :: S). Point n -> Cube
cubeOf Point n
l ->
      [ModalTope n] -> Tope n -> Bool
forall (n :: S). [ModalTope n] -> Tope n -> Bool
solveRHS [ModalTope n]
modalTopes (Tope n -> Tope n -> Tope n
forall (n :: S). Tope n -> Tope n -> Tope n
TAnd (Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TEq Point n
x (Point n -> Point n
forall (n :: S). Point n -> Point n
pointFirst Point n
r)) (Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TEq Point n
y (Point n -> Point n
forall (n :: S). Point n -> Point n
pointSecond Point n
r)))
    TEq Point n
l r :: Point n
r@(PPair Point n
x Point n
y) | KProd Cube
_ Cube
_ <- Point n -> Cube
forall (n :: S). Point n -> Cube
cubeOf Point n
r ->
      [ModalTope n] -> Tope n -> Bool
forall (n :: S). [ModalTope n] -> Tope n -> Bool
solveRHS [ModalTope n]
modalTopes (Tope n -> Tope n -> Tope n
forall (n :: S). Tope n -> Tope n -> Tope n
TAnd (Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TEq (Point n -> Point n
forall (n :: S). Point n -> Point n
pointFirst Point n
l) Point n
x) (Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TEq (Point n -> Point n
forall (n :: S). Point n -> Point n
pointSecond Point n
l) Point n
y))
    TEq Point n
P2_0 Point n
PI_0 -> Bool
True
    TEq Point n
PI_0 Point n
P2_0 -> Bool
True
    TEq Point n
P2_1 Point n
PI_1 -> Bool
True
    TEq Point n
PI_1 Point n
P2_1 -> Bool
True
    TEq Point n
l Point n
r
      | Point n
l Point n -> Point n -> Bool
forall a. Eq a => a -> a -> Bool
== Point n
r -> Bool
True
      | Tope n
goal Tope n -> [Tope n] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Tope n]
topes -> Bool
True
      | Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TEq Point n
r Point n
l Tope n -> [Tope n] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Tope n]
topes -> Bool
True
      | Bool
otherwise -> case (Point n -> Cube
forall (n :: S). Point n -> Cube
cubeOf Point n
l, Point n -> Cube
forall (n :: S). Point n -> Cube
cubeOf Point n
r) of
          (Cube
KUnit, Cube
KUnit) -> Bool
True
          -- antisymmetry proves an equation between lattice points
          (Cube, Cube)
_ | Point n -> Bool
forall (n :: S). Point n -> Bool
isLatticePoint Point n
l Bool -> Bool -> Bool
|| Point n -> Bool
forall (n :: S). Point n -> Bool
isLatticePoint Point n
r ->
                [ModalTope n] -> Tope n -> Bool
forall (n :: S). [ModalTope n] -> Tope n -> Bool
solveRHS [ModalTope n]
modalTopes (Tope n -> Tope n -> Tope n
forall (n :: S). Tope n -> Tope n -> Tope n
TAnd (Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TLeq Point n
l Point n
r) (Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TLeq Point n
r Point n
l))
          (Cube, Cube)
_ -> Bool
False
    TLeq Point n
l Point n
r
      | Point n
l Point n -> Point n -> Bool
forall a. Eq a => a -> a -> Bool
== Point n
r -> Bool
True
      | Tope n
goal Tope n -> [Tope n] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Tope n]
topes -> Bool
True
      | [Tope n] -> Tope n -> Bool
forall (n :: S). [Tope n] -> Tope n -> Bool
solveTopes [Tope n]
topes (Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TEq Point n
l Point n
r) -> Bool
True
      | [Tope n] -> Tope n -> Bool
forall (n :: S). [Tope n] -> Tope n -> Bool
solveTopes [Tope n]
topes (Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TEq Point n
l Point n
forall (n :: S). Point n
P2_0) -> Bool
True
      | [Tope n] -> Tope n -> Bool
forall (n :: S). [Tope n] -> Tope n -> Bool
solveTopes [Tope n]
topes (Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TEq Point n
l Point n
forall (n :: S). Point n
PI_0) -> Bool
True
      | [Tope n] -> Tope n -> Bool
forall (n :: S). [Tope n] -> Tope n -> Bool
solveTopes [Tope n]
topes (Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TEq Point n
r Point n
forall (n :: S). Point n
P2_1) -> Bool
True
      | [Tope n] -> Tope n -> Bool
forall (n :: S). [Tope n] -> Tope n -> Bool
solveTopes [Tope n]
topes (Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TEq Point n
r Point n
forall (n :: S). Point n
PI_1) -> Bool
True
    TLeq (PSup Point n
a Point n
b) Point n
r ->
      [ModalTope n] -> Tope n -> Bool
forall (n :: S). [ModalTope n] -> Tope n -> Bool
solveRHS [ModalTope n]
modalTopes (Tope n -> Tope n -> Tope n
forall (n :: S). Tope n -> Tope n -> Tope n
TAnd (Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TLeq Point n
a Point n
r) (Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TLeq Point n
b Point n
r))
    TLeq Point n
l (PInf Point n
a Point n
b) ->
      [ModalTope n] -> Tope n -> Bool
forall (n :: S). [ModalTope n] -> Tope n -> Bool
solveRHS [ModalTope n]
modalTopes (Tope n -> Tope n -> Tope n
forall (n :: S). Tope n -> Tope n -> Tope n
TAnd (Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TLeq Point n
l Point n
a) (Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TLeq Point n
l Point n
b))
    TLeq Point n
l (PSup Point n
a Point n
b) ->
      [ModalTope n] -> Tope n -> Bool
forall (n :: S). [ModalTope n] -> Tope n -> Bool
solveRHS [ModalTope n]
modalTopes (Tope n -> Tope n -> Tope n
forall (n :: S). Tope n -> Tope n -> Tope n
TOr (Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TLeq Point n
l Point n
a) (Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TLeq Point n
l Point n
b))
    TLeq (PInf Point n
a Point n
b) Point n
r ->
      [ModalTope n] -> Tope n -> Bool
forall (n :: S). [ModalTope n] -> Tope n -> Bool
solveRHS [ModalTope n]
modalTopes (Tope n -> Tope n -> Tope n
forall (n :: S). Tope n -> Tope n -> Tope n
TOr (Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TLeq Point n
a Point n
r) (Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TLeq Point n
b Point n
r))
    TAnd Tope n
l Tope n
r -> [ModalTope n] -> Tope n -> Bool
forall (n :: S). [ModalTope n] -> Tope n -> Bool
solveRHS [ModalTope n]
modalTopes Tope n
l Bool -> Bool -> Bool
&& [ModalTope n] -> Tope n -> Bool
forall (n :: S). [ModalTope n] -> Tope n -> Bool
solveRHS [ModalTope n]
modalTopes Tope n
r
    Tope n
_ | Tope n
goal Tope n -> [Tope n] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Tope n]
topes -> Bool
True
    TInv{} -> Bool
False
    TUninv{} -> Bool
False
    TOr Tope n
l Tope n
r
      | [ModalTope n] -> Tope n -> Bool
forall (n :: S). [ModalTope n] -> Tope n -> Bool
solveRHS [ModalTope n]
modalTopes Tope n
l Bool -> Bool -> Bool
|| [ModalTope n] -> Tope n -> Bool
forall (n :: S). [ModalTope n] -> Tope n -> Bool
solveRHS [ModalTope n]
modalTopes Tope n
r -> Bool
True
      | Bool
otherwise ->
          -- case on a lemma about the goal's points whose sides are not
          -- yet in the context
          let lems :: [Tope n]
lems = [ Tope n
lem | lem :: Tope n
lem@(TOr Tope n
t1 Tope n
t2) <- [Point n] -> [Tope n]
forall (n :: S). [Point n] -> [Tope n]
generateTopesForPoints (Tope n -> [Point n]
forall (n :: S). Tope n -> [Point n]
allTopePoints Tope n
goal)
                           , (Tope n -> Bool) -> [Tope n] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Tope n -> [Tope n] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [Tope n]
topes) [Tope n
t1, Tope n
t2] ]
              ([ModalTope n]
accessible, [ModalTope n]
hidden) = [ModalTope n] -> ([ModalTope n], [ModalTope n])
forall (n :: S). [ModalTope n] -> ([ModalTope n], [ModalTope n])
partitionAccessible [ModalTope n]
modalTopes
              withTope :: Tope n -> [ModalTope n]
withTope Tope n
t = [ModalTope n]
hidden [ModalTope n] -> [ModalTope n] -> [ModalTope n]
forall a. [a] -> [a] -> [a]
++ [ModalTope n] -> [ModalTope n]
forall (n :: S). [ModalTope n] -> [ModalTope n]
saturateTopes (Tope n -> ModalTope n
forall (n :: S). Tope n -> ModalTope n
plainTope Tope n
t ModalTope n -> [ModalTope n] -> [ModalTope n]
forall a. a -> [a] -> [a]
: [ModalTope n]
accessible)
          in case [Tope n]
lems of
               TOr Tope n
t1 Tope n
t2 : [Tope n]
_ -> [ModalTope n] -> Tope n -> Bool
forall (n :: S). [ModalTope n] -> Tope n -> Bool
solveRHS (Tope n -> [ModalTope n]
withTope Tope n
t1) Tope n
goal Bool -> Bool -> Bool
&& [ModalTope n] -> Tope n -> Bool
forall (n :: S). [ModalTope n] -> Tope n -> Bool
solveRHS (Tope n -> [ModalTope n]
withTope Tope n
t2) Tope n
goal
               [Tope n]
_ -> Bool
False
    Tope n
_ -> Bool
False

-- | The syntactic part of the search, on the accessible topes alone.
solveTopes :: [Tope n] -> Tope n -> Bool
solveTopes :: forall (n :: S). [Tope n] -> Tope n -> Bool
solveTopes [Tope n]
topes Tope n
tope = case Tope n
tope of
  Tope n
_ | Tope n
forall (n :: S). Tope n
TBot Tope n -> [Tope n] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Tope n]
topes -> Bool
True
  Tope n
TTop -> Bool
True
  TEq (PPair Point n
x Point n
y) (PPair Point n
x' Point n
y')
    | [Tope n] -> Tope n -> Bool
forall (n :: S). [Tope n] -> Tope n -> Bool
solveTopes [Tope n]
topes (Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TEq Point n
x Point n
x') Bool -> Bool -> Bool
&& [Tope n] -> Tope n -> Bool
forall (n :: S). [Tope n] -> Tope n -> Bool
solveTopes [Tope n]
topes (Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TEq Point n
y Point n
y') -> Bool
True
  TEq l :: Point n
l@(PPair Point n
x Point n
y) Point n
r
    | KProd Cube
_ Cube
_ <- Point n -> Cube
forall (n :: S). Point n -> Cube
cubeOf Point n
l
    , [Tope n] -> Tope n -> Bool
forall (n :: S). [Tope n] -> Tope n -> Bool
solveTopes [Tope n]
topes (Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TEq Point n
x (Point n -> Point n
forall (n :: S). Point n -> Point n
pointFirst Point n
r))
    , [Tope n] -> Tope n -> Bool
forall (n :: S). [Tope n] -> Tope n -> Bool
solveTopes [Tope n]
topes (Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TEq Point n
y (Point n -> Point n
forall (n :: S). Point n -> Point n
pointSecond Point n
r)) -> Bool
True
  TEq Point n
l r :: Point n
r@(PPair Point n
x Point n
y)
    | KProd Cube
_ Cube
_ <- Point n -> Cube
forall (n :: S). Point n -> Cube
cubeOf Point n
r
    , [Tope n] -> Tope n -> Bool
forall (n :: S). [Tope n] -> Tope n -> Bool
solveTopes [Tope n]
topes (Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TEq (Point n -> Point n
forall (n :: S). Point n -> Point n
pointFirst Point n
l) Point n
x)
    , [Tope n] -> Tope n -> Bool
forall (n :: S). [Tope n] -> Tope n -> Bool
solveTopes [Tope n]
topes (Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TEq (Point n -> Point n
forall (n :: S). Point n -> Point n
pointSecond Point n
l) Point n
y) -> Bool
True
  TEq Point n
P2_0 Point n
PI_0 -> Bool
True
  TEq Point n
PI_0 Point n
P2_0 -> Bool
True
  TEq Point n
P2_1 Point n
PI_1 -> Bool
True
  TEq Point n
PI_1 Point n
P2_1 -> Bool
True
  TEq Point n
l Point n
r -> [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
or
    [ Point n
l Point n -> Point n -> Bool
forall a. Eq a => a -> a -> Bool
== Point n
r
    , Tope n
tope Tope n -> [Tope n] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Tope n]
topes
    , Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TEq Point n
r Point n
l Tope n -> [Tope n] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Tope n]
topes ]
  TLeq Point n
l Point n
r
    | Point n
l Point n -> Point n -> Bool
forall a. Eq a => a -> a -> Bool
== Point n
r -> Bool
True
    | [Tope n] -> Tope n -> Bool
forall (n :: S). [Tope n] -> Tope n -> Bool
solveTopes [Tope n]
topes (Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TEq Point n
l Point n
r) -> Bool
True
    | [Tope n] -> Tope n -> Bool
forall (n :: S). [Tope n] -> Tope n -> Bool
solveTopes [Tope n]
topes (Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TEq Point n
l Point n
forall (n :: S). Point n
P2_0) -> Bool
True
    | [Tope n] -> Tope n -> Bool
forall (n :: S). [Tope n] -> Tope n -> Bool
solveTopes [Tope n]
topes (Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TEq Point n
l Point n
forall (n :: S). Point n
PI_0) -> Bool
True
    | [Tope n] -> Tope n -> Bool
forall (n :: S). [Tope n] -> Tope n -> Bool
solveTopes [Tope n]
topes (Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TEq Point n
r Point n
forall (n :: S). Point n
P2_1) -> Bool
True
    | [Tope n] -> Tope n -> Bool
forall (n :: S). [Tope n] -> Tope n -> Bool
solveTopes [Tope n]
topes (Point n -> Point n -> Tope n
forall (n :: S). Point n -> Point n -> Tope n
TEq Point n
r Point n
forall (n :: S). Point n
PI_1) -> Bool
True
  TAnd Tope n
l Tope n
r -> [Tope n] -> Tope n -> Bool
forall (n :: S). [Tope n] -> Tope n -> Bool
solveTopes [Tope n]
topes Tope n
l Bool -> Bool -> Bool
&& [Tope n] -> Tope n -> Bool
forall (n :: S). [Tope n] -> Tope n -> Bool
solveTopes [Tope n]
topes Tope n
r
  TOr Tope n
l Tope n
r -> [Tope n] -> Tope n -> Bool
forall (n :: S). [Tope n] -> Tope n -> Bool
solveTopes [Tope n]
topes Tope n
l Bool -> Bool -> Bool
|| [Tope n] -> Tope n -> Bool
forall (n :: S). [Tope n] -> Tope n -> Bool
solveTopes [Tope n]
topes Tope n
r
  Tope n
_ -> Tope n
tope Tope n -> [Tope n] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Tope n]
topes