0

---- 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)).

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.

Pikaurd
  • 1,114
  • 1
  • 8
  • 22
  • I don't get what you're trying to compute... your formula seems to belong to some [satisfiability modulo theory](http://en.wikipedia.org/wiki/Satisfiability_Modulo_Theories) for integers and booleans, thus it should comply to first-order logic. [Satisfiability](http://en.wikipedia.org/wiki/Satisfiability), which is undecidable in general in first-order logic, is about the existence of an interpretation of the formula that makes it true, but it seems to me that you already fixed an interpretation for the formula by having a domain for both `x0` and `x1`. – Riccardo T. Apr 23 '12 at 08:19
  • 1
    "So as long as there is 1 valid solution in Forall, it is true." That sentence seems very wrong. That describes `Exists`, not `Forall`. – Daniel Fischer Apr 23 '12 at 08:37
  • Well, that is somebody told me. My question is "If he said is correct, how to do it?" – Pikaurd Apr 23 '12 at 08:48

1 Answers1

1

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.

hammar
  • 138,522
  • 17
  • 304
  • 385
  • I think any is not what I want. I think he(whom told me `what is forall`) means "[(False || 0 < 0), (True || 0 < 0)] there is at least one true, so that is true", but using `any` makes it "if we have `a` true, that is true". – Pikaurd Apr 23 '12 at 08:58
  • I mean "If we using any, some formula likes `forall x0::[0,1,2,3,4]. (x0 < 1)` is true". Obviously that is false. – Pikaurd Apr 23 '12 at 09:12
  • 1
    @Pikaurd: Yes, and that is why I think your original code is correct. But, if you're supposed to interpret it so that the body only has to be true for at least one combination of variables (in which case it should be called `Exists`, not `Forall`), then that is true, since `(0 < 1)` is true. – hammar Apr 23 '12 at 09:16
  • Indeed that is `Exists` not `Forall`. But now I need to interpret it `what somebody told me "how it works"`. That is my fault maybe we can call it `Forsome`… And thanks for answer and reply – Pikaurd Apr 23 '12 at 09:22