{-# OPTIONS_GHC -Wall #-}
module ToySolver.Arith.OmegaTest
(
Model
, solve
, solveQFLIRAConj
, Options (..)
, checkRealNoCheck
, checkRealByFM
, checkRealByCAD
, checkRealByVS
, checkRealBySimplex
) where
import Control.Monad
import Control.Monad.ST
import qualified Data.IntMap as IM
import qualified Data.IntSet as IS
import Data.Maybe
import qualified Data.Set as Set
import qualified ToySolver.Data.LA as LA
import qualified ToySolver.Data.Polynomial as P
import ToySolver.Data.IntVar
import qualified ToySolver.Arith.CAD as CAD
import qualified ToySolver.Arith.Simplex as Simplex
import qualified ToySolver.Arith.VirtualSubstitution as VS
import ToySolver.Arith.OmegaTest.Base
checkRealByCAD :: VarSet -> [LA.Atom Rational] -> Bool
checkRealByCAD :: VarSet -> [Atom Rational] -> Bool
checkRealByCAD VarSet
vs [Atom Rational]
as = forall a. Maybe a -> Bool
isJust forall a b. (a -> b) -> a -> b
$ forall v.
(Ord v, Show v, PrettyVar v) =>
Set v -> [OrdRel (Polynomial Rational v)] -> Maybe (Model v)
CAD.solve Set Int
vs2 (forall a b. (a -> b) -> [a] -> [b]
map (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Expr Rational -> Polynomial Rational Int
f) [Atom Rational]
as)
where
vs2 :: Set Int
vs2 = forall a. Eq a => [a] -> Set a
Set.fromAscList forall a b. (a -> b) -> a -> b
$ VarSet -> [Int]
IS.toAscList VarSet
vs
f :: LA.Expr Rational -> P.Polynomial Rational Int
f :: Expr Rational -> Polynomial Rational Int
f Expr Rational
t = forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [ if Int
x forall a. Eq a => a -> a -> Bool
== Int
LA.unitVar
then forall k v. (Eq k, Num k) => k -> Polynomial k v
P.constant Rational
c
else forall k v. (Eq k, Num k) => k -> Polynomial k v
P.constant Rational
c forall a. Num a => a -> a -> a
* forall a v. Var a v => v -> a
P.var Int
x
| (Rational
c,Int
x) <- forall r. Expr r -> [(r, Int)]
LA.terms Expr Rational
t ]
checkRealByVS :: VarSet -> [LA.Atom Rational] -> Bool
checkRealByVS :: VarSet -> [Atom Rational] -> Bool
checkRealByVS VarSet
vs [Atom Rational]
as = forall a. Maybe a -> Bool
isJust forall a b. (a -> b) -> a -> b
$ VarSet -> [Atom Rational] -> Maybe (Model Rational)
VS.solve VarSet
vs [Atom Rational]
as
checkRealBySimplex :: VarSet -> [LA.Atom Rational] -> Bool
checkRealBySimplex :: VarSet -> [Atom Rational] -> Bool
checkRealBySimplex VarSet
vs [Atom Rational]
as = forall a. (forall s. ST s a) -> a
runST forall a b. (a -> b) -> a -> b
$ do
GenericSolverM (ST s) (Delta Rational)
solver <- forall (m :: * -> *) v.
(PrimMonad m, SolverValue v) =>
m (GenericSolverM m v)
Simplex.newSolver
IntMap (Expr Rational)
s <- forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM forall a. [(Int, a)] -> IntMap a
IM.fromAscList forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM (VarSet -> [Int]
IS.toAscList VarSet
vs) forall a b. (a -> b) -> a -> b
$ \Int
v -> do
Int
v2 <- forall (m :: * -> *) v.
(PrimMonad m, SolverValue v) =>
GenericSolverM m v -> m Int
Simplex.newVar GenericSolverM (ST s) (Delta Rational)
solver
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
v, forall r. Num r => Int -> Expr r
LA.var Int
v2)
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Atom Rational]
as forall a b. (a -> b) -> a -> b
$ \Atom Rational
a -> do
forall (m :: * -> *).
PrimMonad m =>
GenericSolverM m (Delta Rational) -> Atom Rational -> m ()
Simplex.assertAtomEx GenericSolverM (ST s) (Delta Rational)
solver (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall r. (Num r, Eq r) => VarMap (Expr r) -> Expr r -> Expr r
LA.applySubst IntMap (Expr Rational)
s) Atom Rational
a)
forall (m :: * -> *) v.
(PrimMonad m, SolverValue v) =>
GenericSolverM m v -> m Bool
Simplex.check GenericSolverM (ST s) (Delta Rational)
solver