简体   繁体   中英

Check whether formula is correct in haskell

---- update 2 ----

At last, he told me that is Exists

thank you all.

---- update ----

Okay, we call it Forsome

ex3: forsome x0::[False,True]. forsome x1::[0,1,2]. (x0 || (0 < x1))

(whom told me "what is forall" added):

the constructor says "forall x in blah" but it really means "for some x in blah". 

the formula is satisfied for some assignment of variables so it is satisfiable.

How can I do it? Thanks

---- original ----

Suppose we have a formula ex3

ex3: forall x0::[False,True]. forall x1::[0,1,2]. (x0 || (0 < x1)) ex3: forall x0::[False,True]. forall x1::[0,1,2]. (x0 || (0 < x1)) .

At first I think ex3 is False , cause when x0 = False and x1 = 0 the formula is (False || (0 < 0)) so ex3 is absolutely false. But I be told that ex3 is True ,

"satisfiable ex3 is true because there is at least one combination from sets x0 and x1 which returns true. So as long as there is 1 valid solution in Forall, it is true."

Assume that is correct…

I think it need to check groups of combination with same level but I am not figure out how to do it. To determine 'Are them same group` seems difficult.

Here is my codes:

File: Formula.hs

{-# LANGUAGE GADTs #-}

module Formula where

-- Datatype of formulas
-- --------------------

data Formula ts where
  Body   :: Term Bool                     -> Formula ()
  Forall :: Show a 
         => [a] -> (Term a -> Formula as) -> Formula (a, as)

data Term t where
  Con     :: t         -> Term t
  And     :: Term Bool -> Term Bool -> Term Bool
  Or      :: Term Bool -> Term Bool -> Term Bool
  Smaller :: Term Int  -> Term Int  -> Term Bool
  Plus    :: Term Int  -> Term Int  -> Term Int
  Name    :: String    -> Term t    -- to facilitate pretty printing

-- Pretty printing formulas
-- ------------------------

instance Show t => Show (Term t) where
  show (Con v)       = show v
  show (And p q)     = "(" ++ show p ++ " && " ++ show q ++ ")"
  show (Or p q)      = "(" ++ show p ++ " || " ++ show q ++ ")"
  show (Smaller n m) = "(" ++ show n ++ " < "  ++ show m ++ ")"
  show (Plus n m)    = "(" ++ show n ++ " + "  ++ show m ++ ")"
  show (Name name)   = name

instance Show (Formula ts) where
  show = show' ['x' : show i | i <- [0..]]
    where
      show' :: [String] -> Formula ts' -> String
      show' ns     (Body body)   = show body
      show' (n:ns) (Forall vs p) = "forall " ++ n ++ "::" ++ show vs ++ ". " ++ show' ns (p (Name n))


-- Example formulas
-- ----------------

ex1 :: Formula ()
ex1 = Body (Con True)

ex2 :: Formula (Int, ())
ex2 = Forall [1..10] $ \n ->
        Body $ n `Smaller` (n `Plus` Con 1)

ex3 :: Formula (Bool, (Int, ()))
ex3 = Forall [False, True] $ \p -> 
      Forall [0..2] $ \n -> 
        Body $ p `Or` (Con 0 `Smaller` n)

wrongFormula :: Formula (Int, ())
wrongFormula = Forall [0..4] $ \n ->
                 Body $ n `Smaller` (Con 0)

File: Solver.hs

{-# LANGUAGE GADTs #-}

module Solver where

import Formula


-- Evaluating terms
-- ----------------

eval :: Term t -> t
eval (Con     v)   = v
eval (And     p q) = eval p && eval q
eval (Or      p q) = eval p || eval q
eval (Smaller n m) = eval n <  eval m
eval (Plus    n m) = eval n +  eval m
eval (Name    _)   = error "eval: Name"


-- Checking formulas
-- -----------------

satisfiable :: Formula ts -> Bool
satisfiable (Body body)    = eval body
-- FIXME wrong implement
--satisfiable (Forall xs f) = helper f xs
--  where helper :: (Term a -> Formula t) -> [a] -> Bool
--        helper fn (a:as) = (satisfiable $ (fn . Con) a) && (helper fn as)
--        helper _ []     = True

Any suggestion will be appreciated.

I agree with Daniel that this describes Exists , not Forall , but if you want to interpret it that way, you just have to change && to || and True to False .

Or, even better, using the Prelude functions

all :: (a -> Bool) -> [a] -> Bool  -- is predicate true for all elements?
any :: (a -> Bool) -> [a] -> Bool  -- is predicate true for any element?

you can write your existing implementation as

satisfiable (Forall xs f) = all (satisfiable . f . Con) xs

so to change it, you just change the all to any .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM