/* The False Statement in Picat. From Prolog code http://www.anselm.edu/internet/compsci/faculty_staff/mmalita/HOMEPAGE/logic/ifthen.txt """ Author: If Then (sol MM) Title: The false Statements 1. Only one of these statements is false 2. Only two of these statements is false 3. Only three of these statements is false 4. Only four of these statements is false 5. All five of these statements is false Which if any is true? ?- start. I = [false,false,false,true,false] ; """ Cf my CP model http://www.hakank.org/picat/five_statements.pi This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ % import util. % import cp. main => go. go ?=> find(I), writeln(I), fail, nl. go => true. /* mem/2: tests/generates if all the elements in the first list are members in the second list. | ?- mem([X,Y,Z],[a,b]). X = Y = Z = a ; X = Y = a ,Z = b ; X = Z = a ,Y = b ; X = a ,Y = Z = b ; X = b ,Y = Z = a ; X = Z = b ,Y = a ; X = Y = b ,Z = a ; X = Y = Z = b ; There are 2^3 = 8 solutions. */ mem([],_L) => true. mem([H|T],L) => member(H,L),mem(T,L). find(A) => A = [A1,A2,A3,A4,A5], mem([A1,A2,A3,A4,A5],[true,false]), Sol=[A1,A2,A3,A4,A5], % count(false,Sol,N), N = count2(false,Sol), %% This yield "*** Undefined procedure: -> /2" in Picat % ( A1->N=1 ; (not(A1) -> not(N=1))), % ( A2->N=2 ; (not(A2) -> not(N=2))), % ( A3->N=3 ; (not(A3) -> not(N=3))), % ( A4->N=4 ; (not(A4) -> not(N=4))), % ( A5->N=5 ; (not(A5) -> not(N=5))). %% This works % ( A1->N=1 ; not(N=1)), % ( A2->N=2 ; not(N=2)), % ( A3->N=3 ; not(N=3)), % ( A4->N=4 ; not(N=4)), % ( A5->N=5 ; not(N=5)). %% This works as well % if A1 then N=1 else N!=1 end, % if A2 then N=2 else N!=2 end, % if A3 then N=3 else N!=3 end, % if A4 then N=4 else N!=4 end, % if A5 then N=5 else N!=5 end. % General approach: foreach(I in 1..A.length) if A[I] == true then N = I else N != I end end. /* counts how many times an element (A) occurs in a list. (only on first level) ?- count(b,[a,b,c,b],N). N=2 */ % count(_A,[],N) => N = 0. % count(A,AT,N) ?=> AT = [A|T], count(A,T,N1),N = N1+1. % count(A,BT,N) => BT = [B|T], B!=A, count(A,T,N). % Picat version: count2(E,L) = [A : A in L, A = E].length.