{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Data.SBV.Examples.Misc.Enumerate where
import Data.SBV
data E = A | B | C
mkSymbolicEnumeration ''E
-- | Give a name to the symbolic variants of 'E', for convenience
type SE = SBV E
-- | Have the SMT solver enumerate the elements of the domain. We have:
--
-- >>> elts
-- Solution #1:
-- s0 = B :: E
-- Solution #2:
-- s0 = A :: E
-- Solution #3:
-- s0 = C :: E
-- Found 3 different solutions.
elts :: IO AllSatResult
elts = allSat $ \(x::SE) -> x .== x
-- | Shows that if we require 4 distinct elements of the type 'E', we shall fail; as
-- the domain only has three elements. We have:
--
-- >>> four
-- Unsatisfiable
four :: IO SatResult
four = sat $ \a b c (d::SE) -> distinct [a, b, c, d]
-- | Enumerations are automatically ordered, so we can ask for the maximum
-- element. Note the use of quantification. We have:
--
-- >>> maxE
-- Satisfiable. Model:
-- maxE = C :: E
maxE :: IO SatResult
maxE = sat $ do mx <- exists "maxE"
e <- forall "e"
return $ mx .>= (e::SE)
-- | Similarly, we get the minumum element. We have:
--
-- >>> minE
-- Satisfiable. Model:
-- minE = A :: E
minE :: IO SatResult
minE = sat $ do mx <- exists "minE"
e <- forall "e"
return $ mx .<= (e::SE)