{-# OPTIONS_GHC -Wall #-}
-----------------------------------------------------------------------------
-- |
-- Module      :  ToySolver.Arith.OmegaTest
-- Copyright   :  (c) Masahiro Sakai 2011
-- License     :  BSD-style
--
-- Maintainer  :  masahiro.sakai@gmail.com
-- Stability   :  provisional
-- Portability :  portable
--
-- (incomplete) implementation of Omega Test
--
-- References:
--
-- * William Pugh. The Omega test: a fast and practical integer
--   programming algorithm for dependence analysis. In Proceedings of
--   the 1991 ACM/IEEE conference on Supercomputing (1991), pp. 4-13.
--
-- * <http://users.cecs.anu.edu.au/~michaeln/pubs/arithmetic-dps.pdf>
--
-- See also:
--
-- * <http://hackage.haskell.org/package/Omega>
--
-----------------------------------------------------------------------------
module ToySolver.Arith.OmegaTest
    (
    -- * Solving
      Model
    , solve
    , solveQFLIRAConj
    -- * Options for solving
    , 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 = Maybe (Model Key) -> Bool
forall a. Maybe a -> Bool
isJust (Maybe (Model Key) -> Bool) -> Maybe (Model Key) -> Bool
forall a b. (a -> b) -> a -> b
$ Set Key -> [OrdRel (Polynomial Rational Key)] -> Maybe (Model Key)
forall v.
(Ord v, Show v, PrettyVar v) =>
Set v -> [OrdRel (Polynomial Rational v)] -> Maybe (Model v)
CAD.solve Set Key
vs2 ((Atom Rational -> OrdRel (Polynomial Rational Key))
-> [Atom Rational] -> [OrdRel (Polynomial Rational Key)]
forall a b. (a -> b) -> [a] -> [b]
map ((Expr Rational -> Polynomial Rational Key)
-> Atom Rational -> OrdRel (Polynomial Rational Key)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Expr Rational -> Polynomial Rational Key
f) [Atom Rational]
as)
  where
    vs2 :: Set Key
vs2 = [Key] -> Set Key
forall a. Eq a => [a] -> Set a
Set.fromAscList ([Key] -> Set Key) -> [Key] -> Set Key
forall a b. (a -> b) -> a -> b
$ VarSet -> [Key]
IS.toAscList VarSet
vs

    f :: LA.Expr Rational -> P.Polynomial Rational Int
    f :: Expr Rational -> Polynomial Rational Key
f Expr Rational
t = [Polynomial Rational Key] -> Polynomial Rational Key
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [ if Key
x Key -> Key -> Bool
forall a. Eq a => a -> a -> Bool
== Key
LA.unitVar
                then Rational -> Polynomial Rational Key
forall k v. (Eq k, Num k) => k -> Polynomial k v
P.constant Rational
c
                else Rational -> Polynomial Rational Key
forall k v. (Eq k, Num k) => k -> Polynomial k v
P.constant Rational
c Polynomial Rational Key
-> Polynomial Rational Key -> Polynomial Rational Key
forall a. Num a => a -> a -> a
* Key -> Polynomial Rational Key
forall a v. Var a v => v -> a
P.var Key
x
              | (Rational
c,Key
x) <- Expr Rational -> [(Rational, Key)]
forall r. Expr r -> [(r, Key)]
LA.terms Expr Rational
t ]

checkRealByVS :: VarSet -> [LA.Atom Rational] -> Bool
checkRealByVS :: VarSet -> [Atom Rational] -> Bool
checkRealByVS VarSet
vs [Atom Rational]
as = Maybe (Model Rational) -> Bool
forall a. Maybe a -> Bool
isJust (Maybe (Model Rational) -> Bool) -> Maybe (Model Rational) -> Bool
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 s. ST s Bool) -> Bool
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s Bool) -> Bool) -> (forall s. ST s Bool) -> Bool
forall a b. (a -> b) -> a -> b
$ do
  GenericSolverM (ST s) (Delta Rational)
solver <- ST s (GenericSolverM (ST s) (Delta Rational))
forall (m :: * -> *) v.
(PrimMonad m, SolverValue v) =>
m (GenericSolverM m v)
Simplex.newSolver
  IntMap (Expr Rational)
s <- ([(Key, Expr Rational)] -> IntMap (Expr Rational))
-> ST s [(Key, Expr Rational)] -> ST s (IntMap (Expr Rational))
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM [(Key, Expr Rational)] -> IntMap (Expr Rational)
forall a. [(Key, a)] -> IntMap a
IM.fromAscList (ST s [(Key, Expr Rational)] -> ST s (IntMap (Expr Rational)))
-> ST s [(Key, Expr Rational)] -> ST s (IntMap (Expr Rational))
forall a b. (a -> b) -> a -> b
$ [Key]
-> (Key -> ST s (Key, Expr Rational))
-> ST s [(Key, Expr Rational)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM (VarSet -> [Key]
IS.toAscList VarSet
vs) ((Key -> ST s (Key, Expr Rational)) -> ST s [(Key, Expr Rational)])
-> (Key -> ST s (Key, Expr Rational))
-> ST s [(Key, Expr Rational)]
forall a b. (a -> b) -> a -> b
$ \Key
v -> do
    Key
v2 <- GenericSolverM (ST s) (Delta Rational) -> ST s Key
forall (m :: * -> *) v.
(PrimMonad m, SolverValue v) =>
GenericSolverM m v -> m Key
Simplex.newVar GenericSolverM (ST s) (Delta Rational)
solver
    (Key, Expr Rational) -> ST s (Key, Expr Rational)
forall (m :: * -> *) a. Monad m => a -> m a
return (Key
v, Key -> Expr Rational
forall r. Num r => Key -> Expr r
LA.var Key
v2)
  [Atom Rational] -> (Atom Rational -> ST s ()) -> ST s ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Atom Rational]
as ((Atom Rational -> ST s ()) -> ST s ())
-> (Atom Rational -> ST s ()) -> ST s ()
forall a b. (a -> b) -> a -> b
$ \Atom Rational
a -> do
    GenericSolverM (ST s) (Delta Rational) -> Atom Rational -> ST s ()
forall (m :: * -> *).
PrimMonad m =>
GenericSolverM m (Delta Rational) -> Atom Rational -> m ()
Simplex.assertAtomEx GenericSolverM (ST s) (Delta Rational)
solver ((Expr Rational -> Expr Rational) -> Atom Rational -> Atom Rational
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (IntMap (Expr Rational) -> Expr Rational -> Expr Rational
forall r. (Num r, Eq r) => VarMap (Expr r) -> Expr r -> Expr r
LA.applySubst IntMap (Expr Rational)
s) Atom Rational
a)
  GenericSolverM (ST s) (Delta Rational) -> ST s Bool
forall (m :: * -> *) v.
(PrimMonad m, SolverValue v) =>
GenericSolverM m v -> m Bool
Simplex.check GenericSolverM (ST s) (Delta Rational)
solver