First order logic resolution algorithm . Ask with a negative request - first-order-logic

I try to ask a knowledge base with a negative request. So before the resolution the complement (positif here) is add to the set of clauses uses by the algorithm.
With these facts:
Spouse(Sarah,Andrew)
Spouse(Sophie,Edward)
And theses axioms :
(∀ [a , b , c ] ( ( ( Spouse(a,c) ∧ Spouse(b,c) ) ⇒ Egal(a,b) ) ))
this axiom mean onely two people can be spouse
(∀ [a , b ] ( ( Spouse(a,b) ⇔ Spouse(b,a) ) ))
(∀ [x ] ( Egal(x,x) ))
(∀ [x , y ] ( ( Egal(x,y) ⇒ Egal(y,x) ) ))
reverse
(∀ [x , y , z ] ( ( ( Egal(x,y) ∧ Egal(y,z) ) ⇒ Egal(x,z) ) ))
transitive
(∀ [x ] ( ( Egal(x,x) ⇒ Egal(x,x) ) ))
clauses turn into CNF
( ( ¬ Spouse(a0,c0) ∨ ¬ Spouse(b0,c0) ) ∨ Egal(a0,b0) )
( ¬ Spouse(a1,b1) ∨ Spouse(b1,a1) )
( ¬ Spouse(b2,a2) ∨ Spouse(a2,b2) )
Egal(x0,x0)
( ¬ Egal(x1,y0) ∨ Egal(y0,x1) )
( ( ¬ Egal(x2,y1) ∨ ¬ Egal(y1,z0) ) ∨ Egal(x2,z0) )
( ¬ Egal(x3,x3) ∨ Egal(x3,x3) )
I can request
Spouse(Sarah,Andrew) or Spouse(Sophie,Edward)
this request ¬Spouse(Sarah,Edward) : is Sarah not spouse with Edward, doesn't work.
Of course I can ask if Spouse(Sarah,Edward) : is Sarah spouse with Edward, get no answer et consider that it's false.
What's wrong with this kind of request, because I don't know if there is a bug into my implementation or if it is not possible to do that (it's suppose to ...).
Thanks for your help !

Related

Candidate elimination algorithm lecture example

I am looking through some lecture slides and cannot understand why the bold hypothesis at last G are just discarded, I can come to the same answer but don't understand why they're just discarded.
sky temperature humidity
| | | | | |
Sunny Rainy Warm Coo Normal Low
and the set of positive and negative training examples:
1. ( S W N )+)
2. ( R C L )-)
3 . ( S C N )+)
4. ( S W L )-)
Training with the first example: ( S W N ) +) generalizing…
G = [( ? ? ? )]
S = [( S W N )]
Training with the second example: ( R C L ) -) specializing…
G = [( S ? ? ) ( ? W ? ) ( ? ? N )
S = [( S W N )]
Training with the third example: ( S C N ) +) generalizing…
G = [( S ? ? )( ? ? N )] (the other is discarded )
S = [( S ? N )]
Training with the fourth example: ( S W L ) -) specializing…
G = [( S C ? )( S ? N )( R ? N )(? C N)] (bold are discarded )
S = [( S ? N )]
Convergence, the learned concept must be: [( S ? N )]
G = [( S C ? )( S ? N )( R ? N )(? C N)] (bold are discarded )
It can be simply using the candidate elimination algorithm. According to that the reasons can be summarized as follows.
Inconsistent hypothesis: According to the algorithm we have to first remove the hypotheses which are not consistent with target data(D)
In this case ( R ? N ) is removed it's inconsistent with ( S ? N )
Specific boundary being more general than the general boundary.
If the specific boundy become more specific that the general one. There can be a boundary overlapping.
if we compare derived ( S C ? ) with ( S ? N ) , we can compare middle c with ? of (S ? N). The derived one having a constant makes it more specific compared to the specific boundary. So it should be removed. Same goes with (? C N).
I see the question is bit older but I hope someone would find this useful.

Calculation of graduation grade in Prolog

I have an error in my prolog program I try to calculate the CGPA in WinProlog and the code as follows:
grade(X).
start:-
( write('please enter ur cgpa'), read(X), X > 0 , X < 2,
write('ur cgpa is poor'), write(X)
; X >= 2, X < 3,
write('ur cgpa is good')
; X >= 3,
write('ur cgpa is excellent')
).
// comments
// %cgpa(X,good):-grade(X) , X>=2 , X<3.
// %cgpa(X,good):-grade(X) , X>=3 , write("your grade is excelent").
// is there anything missing, please help me
As #lurker noted, logical AND (,) and logical OR (;) are operators with different precedences. The expression doesn't necessarily bind the way you think it does. How does the expression
a , b ; c , d
bind? Is it...
AND having higher precendence than OR: (a,b) ; (c,d)
OR having higher precedence than AND: a , (b;c) , d
Equal precedence, left-associative: ( ( (a,b) ; c ) , d )
Equal precedence, right-associative: ( a , ( b ; (c,d)))
If you're not sure, make the binding clear with parentheses (but don't overdo it).
Further, one thing with Prolog is that alternatives are often better specified as individual clauses of a predicate rather than using the logical OR (;) operator. Something like this
evaluate_grade( X , poor ) :- X >= 0.0 , X < 2.0 .
evaluate_grade( X , good ) :- X >= 2.0 , X < 3.0 .
evaluate_grade( X , excellent ) :- X >= 3.0 .
is easier to debug, extend, modify and understand than your
( X>0 , X<2 ,
write('ur cgpa is poor') , write(X) ;
X>=2 , X<3,
write('ur cgpa is good') ;
X>=3 ,
write('ur cgpa is excellent')
)
Taking this approach also helps lead you to the Single Responsibility Principle — a fancy way of saying that things should do just one simple thing.
So, you might try decomposing the problem into smaller pieces that each do a single thing. Something like this:
start:-
read_grade(X) ,
evaluate_grade(X,R),
write('Your CGPA is ') ,
write(R) ,
nl
.
read_grade(X) :-
write('please enter your CGPA: ') ,
read(X),
number(X)
.
evaluate_grade( X , poor ) :- X >= 0.0 , X < 2.0 .
evaluate_grade( X , good ) :- X >= 2.0 , X < 3.0 .
evaluate_grade( X , excellent ) :- X >= 3.0 .

Exclusion and inclusion in Z3

I am trying to model inclusion and exclusion of elements in sets with Z3. In particular inclusion of elements with distinct values, and exclusion of elements not already in a target set. So basically I want to have a set U and have Z3 find a set U_d that only contains elements of U with distinct values.
My current approach uses quantifiers, but I'm having trouble understanding how to state that I want to always include elements in U_d if they appear in U.
( set-option :produce-models true)
;;; Two simple sorts.
;;; Sets and Zs.
( declare-sort Z 0 )
( declare-sort Set 0 )
;;; A set can contain a Z or not.
;;; Zs can have a value.
( declare-fun contains (Set Z) bool )
( declare-fun value (Z) Int )
;;; Two sets and two Z instances for use in the example.
( declare-const set Set )
( declare-const distinct_set Set )
( declare-const A Z )
( declare-const B Z )
;;; The elements and sets are distinct.
( assert ( distinct A B ) )
( assert ( distinct set distinct_set ) )
;;; Set 'set' contains A but not B
( assert ( = ( contains set A ) true ) )
( assert ( = ( contains set B ) false ) )
;;; Assert that all elements contained by distinct_set have different values unless they're the same variable.
( assert
( forall ( (x Z) (y Z) )
( =>
( and
( contains distinct_set x )
( contains distinct_set y )
( = ( value x ) ( value y ) ) )
( = x y ) )))
;;; distinct_set can contain only elements that appear in set.
;;; In other words, distinct_set is a proper set.
( assert
( forall ( ( x Z ) )
( =>
( contains distinct_set x )
( contains set x ))))
;;; Give elements some values.
( assert ( = (value A) 0 ) )
( assert ( = (value B) 1 ) )
( push )
( check-sat )
( get-value (( contains distinct_set A )))
( get-value (( contains distinct_set B )))
( pop )
The assignments it produces are:
sat
((( contains distinct_set A ) false))
((( contains distinct_set B ) false))
The assignments I would like are:
sat
((( contains distinct_set A ) true))
((( contains distinct_set B ) false))
I understand that an assignment of false to both A and B is a logically correct assignment, but I don't know how to state things in such a way as to rule those sorts of cases out.
Perhaps I'm not thinking about the problem correctly.
Any advice would be much appreciated. :)
What do you think of the following assertion?
(assert
(forall ((x Z))
(=> (contains set x)
(exists ((y Z))
(and (= (value x) (value y))
(contains set y)
(contains distinct_set y))))))
It says that for every element x of set (i.e., U), there is a y s.t.
value of y is equal to value of x
y is also an element of set
y is an element of distinct_set (i.e., U_d)
This assertion makes sure that if there are two elements in set with the same value, then one and only one of them is an element of distinct_set. Is that what you want?
Note that, if we just add this assertion, Z3 will still produce a model where
((( contains distinct_set A ) false))
((( contains distinct_set B ) false))
If we inspect the model produced by Z3 using (get-model), we will notice that set contains another element different from A. So, to force set to contain only the element A, we have to assert
(assert
(forall ((x Z))
(= (contains set x) (= x A))))
After you add this assertion, the following two assertions become redundant:
( assert ( = ( contains set A ) true ) )
( assert ( = ( contains set B ) false ) )
Now, let us consider the case where set contains two values: A and C, and they both have the same value. The following script also asks questions such as: is there a model where
distinct_set does not contain A
distinct_set does not contain A nor C
distinct_set contains A and C
Script:
( set-option :produce-models true)
;;; Two simple sorts.
;;; Sets and Zs.
( declare-sort Z 0 )
( declare-sort Set 0 )
;;; A set can contain a Z or not.
;;; Zs can have a value.
( declare-fun contains (Set Z) bool )
( declare-fun value (Z) Int )
;;; Two sets and two Z instances for use in the example.
( declare-const set Set )
( declare-const distinct_set Set )
( declare-const A Z )
( declare-const B Z )
( declare-const C Z )
;;; The elements and sets are distinct.
( assert ( distinct A B C) )
( assert ( distinct set distinct_set ) )
;;; set contains only A and C
(assert
(forall ((x Z))
(= (contains set x) (or (= x A) (= x C)))))
;;; Assert that all elements contained by distinct_set have different values unless they're the same variable.
( assert
( forall ( (x Z) (y Z) )
( =>
( and
( contains distinct_set x )
( contains distinct_set y )
( = ( value x ) ( value y ) ) )
( = x y ) )))
;;; distinct_set can contain only elements that appear in set.
;;; In other words, distinct_set is a proper set.
( assert
( forall ( ( x Z ) )
( =>
( contains distinct_set x )
( contains set x ))))
;;; Give elements some values.
( assert ( = (value A) 0 ) )
( assert ( = (value B) 1 ) )
( assert ( = (value C) 0 ) )
(assert
(forall ((x Z))
(=> (contains set x)
(exists ((y Z))
(and (= (value x) (value y))
(contains set y)
(contains distinct_set y))))))
( push )
( check-sat )
( get-model )
( get-value (( contains distinct_set A )))
( get-value (( contains distinct_set B )))
( get-value (( contains distinct_set C )))
( echo "Is there another model where A is not in distinct_set")
( assert (not ( contains distinct_set A )))
(check-sat)
( get-value (( contains distinct_set A )))
( get-value (( contains distinct_set B )))
( get-value (( contains distinct_set C )))
( echo "Is there another model where A and C are not in distinct_set")
( assert (not ( contains distinct_set C )))
(check-sat)
( pop ) ;; retracting the last two assertions
( push )
( echo "Is there a model where A and C are in distinct_set")
( assert ( contains distinct_set A ))
( assert ( contains distinct_set C ))
( check-sat )

SPOJ Problem Flibonakki time limit exceed

I am trying to solve this problem in Haskell but getting time limit exceed. I applied all my Haskell and mathematical skill to optimize this but all in vain. Could some one please suggest me how to optimize this code further. The sequence F_3 + F_7 + F_11 .... + F_(4n+3) = F_2n*F_(2n+1). I used O(log n) to method to calculate the Fibonacci numbers.
import Data.List
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as BS
matmul :: [Integer] -> [Integer] -> Integer -> [Integer]
matmul [a,b,c] [d,e,f] m = [x,y,z] where
y = (a*e + b*f) `mod` m
z = (b*e + c*f) `mod` m
x = y + z
powM ::[Integer] -> Integer -> Integer -> [Integer]
powM a n m | n == 1 = a
| n == 2 = matmul a a m
| even n = powM ( matmul a a m ) ( div n 2 ) m
| otherwise = matmul a ( powM ( matmul a a m ) ( div n 2 ) m ) m
readInt :: BS.ByteString -> Integer
readInt = fst.fromJust.BS.readInteger
solve::Integer -> BS.ByteString
solve n = BS.pack.show $ mod ( c*d ) 1000000007 where
[c,d,_] = powM [1,1,0] ( 2*n ) 1000000007
--([_,a,_]:_) = powM [[1,2,1],[0,5,3],[0,3,2]] n 1000000007
-- f_3+f_7+f_11+f_15 = f_2n*f_(2n+1)
main = BS.interact $ BS.unlines. map ( solve.readInt ) . tail . BS.lines
Your solving seems to be fast enough but it seems that your main function does not print the answer after each new line. In fact it requires an extra newline to get the last answer so this can be the cause of your timeout! Here is a version that prints each answer directly after the input.
import Data.List
import Data.Maybe
import Control.Monad
import qualified Data.ByteString.Lazy.Char8 as B
import qualified Data.ByteString.Char8 as BC
import qualified Text.Show.ByteString as BS
matmul :: [Integer] -> [Integer] -> Integer -> [Integer]
matmul [a,b,c] [d,e,f] m = [x,y,z] where
y = (a*e + b*f) `mod` m
z = (b*e + c*f) `mod` m
x = y + z
powM :: [Integer] -> Integer -> Integer -> [Integer]
powM a n m | n == 1 = a
| n == 2 = matmul a a m
| even n = powM ( matmul a a m ) ( div n 2 ) m
| otherwise = matmul a ( powM ( matmul a a m ) ( div n 2 ) m ) m
solve :: Integer -> Integer
solve n = mod ( c*d ) 1000000007
where
[c,d,_] = powM [1,1,0] ( 2*n ) 1000000007
readInteger :: B.ByteString -> Integer
readInteger = fst . fromJust . B.readInteger
readInt :: B.ByteString -> Int
readInt = fst . fromJust . B.readInt
get :: IO B.ByteString
get = liftM (B.fromChunks . (:[])) BC.getLine
main :: IO ()
main = do
n <- liftM readInt get
replicateM_ n ( liftM readInteger get >>= B.putStrLn . BS.show . solve )

How do I write ∀x ( P(x) and Q(x) ) in Coq?

I'm trying out Coq, but I'm not completely sure what I'm doing. Is:
Theorem new_theorem : forall x, P:Prop /\ Q:Prop
Equivalent to:
∀x ( P(x) and Q(x) )
Edit: I think they are.
Are you having problems with the syntax?
$ coqtop
Welcome to Coq 8.1pl3 (Dec. 2007)
Coq < Section Test.
Coq < Variable X:Set.
X is assumed
Coq < Variables P Q:X -> Prop.
P is assumed
Q is assumed
Coq < Theorem forall_test: forall x:X, P(x) /\ Q(x).
1 subgoal
X : Set
P : X -> Prop
Q : X -> Prop
============================
forall x : X, P x /\ Q x
forall_test <
Well, to answer your question:
Section test.
Variable A : Type. (* assume some universe A *)
Variable P Q : A -> Prop. (* and two predicates over A, P and Q *)
Goal forall x, P x /\ Q x. (* Ax, ( P(x) and Q(x) ) *)
End test.

Resources