Wednesday, October 14, 2009

Demo Perm.pro Lab4 part8

?- ['perm.pro'].
% perm.pro compiled 0.00 sec, 136 bytes

Yes
?- perm([1,2,3],I).

I = [1, 2, 3] ;

I = [2, 1, 3] ;

I = [2, 3, 1] ;

I = [1, 3, 2] ;

I = [3, 1, 2] ;

I = [3, 2, 1] ;

No
?- perm([G,t,a,o],Wrd).

Wrd = [G, t, a, o] ;

Wrd = [t, G, a, o] ;

Wrd = [t, a, G, o] ;

Wrd = [t, a, o, G] ;

Wrd = [G, a, t, o] ;

Wrd = [a, G, t, o] ;

Wrd = [a, t, G, o] ;

Wrd = [a, t, o, G] ;

Wrd = [G, a, o, t] ;

Wrd = [a, G, o, t] ;

Wrd = [a, o, G, t] ;

Wrd = [a, o, t, G] ;

Wrd = [G, t, o, a] ;

Wrd = [t, G, o, a] ;

Wrd = [t, o, G, a] ;

Wrd = [t, o, a, G] ;

Wrd = [G, o, t, a] ;

Wrd = [o, G, t, a] ;

Wrd = [o, t, G, a] ;

Wrd = [o, t, a, G] ;

Wrd = [G, o, a, t]

Yes
?- halt.

Perm.pro Lab4 part8

% prints all possible permutations of a given list.

del(A,[A|T],T).
del(A,[H|T],[H|B]):-
del(A,T,B).

perm([],[]).
perm([H|T],R):-
perm(T,A),
del(H,R,A).

Demo Concat.pro Lab4 part7

?- ['concat.pro'].
% concat.pro compiled 0.00 sec, 0 bytes

Yes
?- con([1,2,3],[a,b,c],Result).

Result = [1, 2, 3, a, b, c]

Yes
?- con([X,Y,Z],[$,*,&],Result).

Result = [X, Y, Z, $, *, &]

Yes
?- con([$],[41.13],Result).

Result = [$, 41.13]

Yes
?- halt.

Concat.pro Lab4 part7

% concatenates one given list to another given list

con([],A,A).
con([H|T_1],B,[H|T_2]) :-
con(T_1,B,T_2).

Demo Member.pro Lab4 part6

?- ['member.pro'].
% member.pro compiled 0.00 sec, 136 bytes

Yes
?- mem(s,[t,d,r,f,d]).

No
?- mem(s,[t,d,r,f,s]).

Yes
?- mem(Z,[t,d,r,f,s]).

Z = t ;

Z = d ;

Z = r ;

Z = f ;

Z = s ;

No
?- mem(z,List).

List = [z|_G246] ;

List = [_G245, z|_G249] ;

List = [_G245, _G248, z|_G252] ;

List = [_G245, _G248, _G251, z|_G255] ;

List = [_G245, _G248, _G251, _G254, z|_G258] ;

List = [_G245, _G248, _G251, _G254, _G257, z|_G261] ;

List = [_G245, _G248, _G251, _G254, _G257, _G260, z|_G264] ;

List = [_G245, _G248, _G251, _G254, _G257, _G260, _G263, z|_G267]

Yes
?-halt.

Member.pro Lab4 part6

% checks to see if a given element is in a given list, returns yes or no.

mem(A, [A|_]). % Given element is a member if it matches the head.
mem(A, [_|Tail]) :-
mem(A, Tail). % Givem element is a member if it matches the tail.

Tuesday, October 13, 2009

Demo List.pro double Lab4 part5

?- consult('list.pro').
% list.pro compiled 0.00 sec, 3,768 bytes

Yes
?- rnlist(8,List), double_count(List,Num).

List = [1, 4, 4, 2, 9, 9, 6, 8]
Num = 2

Yes
?- rnlist(9,List), double_count(List,Num).

List = [4, 9, 7, 8, 6, 2, 3, 0, 7]
Num = 1

Yes
?- rnlist(7,List), double_count(List,Num).

List = [8, 0, 1, 4, 2, 0, 2]
Num = 2

Yes
?- rnlist(6,List), double_count(List,Num).

List = [6, 8, 7, 5, 7, 8]
Num = 2

Yes
?- halt.

List.pro double Lab4 part5

rnprint(0).
rnprint(A) :-
random(0,10,R),
write(R),
nl,
B is A-1,
rnprint(B).

rnlist(0,[]).
rnlist(N,X) :-
R is N-1,
rnlist(R,A),
random(0,10,B),
append(A,[B],X).

%counts occurrences of the given number in the generated list


count(_,[],0).
count(A,[H|T],B) :-
A = H,
count(A,T,K),
B is K + 1.
count(A,[H|T],B) :-
count(A,T,B).

%counts number of digits with single occurrences

single_count([],0).
single_count(A,K) :-
single_2(A,10,C),
K is C.

single_2(_,0,0).
single_2(A,B,K) :-
D is B - 1,
count(D,A,T),
T = 1,
single_2(A,D,C),
K is C + 1.
single_2(A,B,K) :-
D is B - 1,
single_2(A,D,K).

%counts number of digits within list that occur twice

double_count([],0).
double_count(A,B) :-
double_2(A,10,K),
B is K.

double_2(_,0,0).
double_2(A,C,B) :-
Z is C - 1,
count(Z,A,N),
N = 2,
double_2(A,Z,K),
B is K + 1.
double_2(A,C,B) :-
Z is C - 1,
double_2(A,Z,B).

Demo List.pro singleton Lab4 part4

?- consult('list.pro').
% list.pro compiled 0.00 sec, 0 bytes

Yes
?- rnlist(7,List),single_count(List,Num).

List = [1, 0, 3, 7, 3, 9, 8]
Num = 5

Yes
?- rnlist(7,List),single_count(List,Num).

List = [6, 0, 2, 4, 8, 9, 5]
Num = 7

Yes
?- rnlist(7,List),single_count(List,Num).

List = [2, 6, 1, 8, 9, 7, 4]
Num = 7

Yes
?- rnlist(9,List),single_count(List,Num).

List = [4, 5, 9, 2, 4, 9, 8, 5, 8]
Num = 1

Yes
?-halt.

List.pro singleton Lab4 part4

rnprint(0).
rnprint(A) :-
random(0,10,R),
write(R),nl,
B is A-1,
rnprint(B).

rnlist(0,[]).
rnlist(N,X) :-
R is N-1,
rnlist(R,A),
random(0,10,B),
append(A,[B],X).


%counts occurrences of the given number in the generated list


count(_,[],0).
count(A,[H|T],B) :-
A = H,
count(A,T,K),
B is K + 1.
count(A,[H|T],B) :-
count(A,T,B).

%counts number of digits with single occurrences

single_count([],0).
single_count(A,K) :-
single_2(A,10,C),
K is C.

single_2(_,0,0).
single_2(A,B,K) :-
D is B - 1,
count(D,A,T), T = 1,
single_2(A,D,C),
K is C + 1.
single_2(A,B,K) :-
D is B - 1,
single_2(A,D,K).

Demo List.pro Count Lab4 part3

?- consult('list.pro').
% list.pro compiled 0.00 sec, 0 bytes

Yes
?- rnlist(9,Out), count(2,List,Num).

List = [2, 3, 7, 2, 8, 5, 7, 0, 6]
Num = 2

Yes
?- rnlist(20,List), count(4,List,Num).

List = [8, 5, 0, 7, 8, 5, 0, 3, 5|...]
Num = 0 [write]

List = [8, 5, 0, 7, 8, 5, 0, 3, 5, 5, 2, 3, 7, 3, 2, 7, 0, 9, 7, 7]
Num = 0

Yes
?- rnlist(20,List), count(4,List,Num).

List = [7, 3, 6, 3, 2, 3, 6, 2, 7, 1, 9, 4, 7, 9, 2, 7, 8, 9, 2, 1]
Num = 1

Yes
?- rnlist(20,List), count(4,List,Num).

List = [8, 5, 5, 1, 4, 2, 4, 8, 7, 7, 6, 6, 6, 9, 3, 7, 5, 6, 7, 4]
Num = 3

Yes
?- halt.

List.pro Count Lab4 part3

rnprint(0).
rnprint(A) :-
random(0,10,R),
write(R),nl,
B is A-1,
rnprint(B).

rnlist(0,[]).
rnlist(N,X) :-
R is N-1,
rnlist(R,A),
random(0,10,B),
append(A,[B],X).


%counts given number in the list.

count(_,[],0).
count(A,[H|T],B) :-
A = H,
count(A,T,K),
B is K + 1.
count(A,[H|T],B) :-
count(A,T,B).

Demo List.pro Lab4 part1

?- consult('list.pro').
% list.pro compiled 0.01 sec, 0 bytes

Yes
?- rnprint(10).
3
8
5
5
1
1
7
8
2
2

Yes
?- rnprint(4).
7
3
0
0

Yes
?- rnprint(6).
5
2
9
4
2
5

Yes
?- halt.

List.pro Lab4 part1

%-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
% Given a number A of elements print
% out that number of random integers
% between 0 and 10.
%-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

rnprint(0).
rnprint(A) :-
random(0,10,R),
write(R),nl,
B is A-1,
rnprint(B).

Wednesday, October 7, 2009

Demo List.pro lab4 part2

?- consult('list.pro').
% list.pro compiled 0.00 sec, 0 bytes

Yes
?- rnlist(5,Expr).

Expr = [1, 7, 1, 0, 1]

Yes
?- rnlist(8,Expr).

Expr = [8, 8, 7, 1, 3, 5, 1, 6]


Yes
?- rnlist(12,Expr), write(Expr).
[5, 5, 8, 5, 7, 2, 3, 0, 8, 9, 4, 7]

Expr = [5, 5, 8, 5, 7, 2, 3, 0, 8|...]

Yes
?-halt.

List.pro Lab4 part2

rnprint(0).
rnprint(N) :-
random(0,10,R),
write(R),nl,
N is R-1,
rnprint(N).

rnlist(0,[]).
rnlist(N,X) :-
R is N-1,
rnlist(R,A),
random(0,10,B),
append(A,[B],X).

Tuesday, October 6, 2009

Demo Crypto.pro Lab3 part4

?- consult('crypto.pro').
% gv.pro compiled 0.00 sec, 3,112 bytes
% combosets.pro compiled 0.01 sec, 7,496 bytes
% crypto.pro compiled 0.01 sec, 22,344 bytes

Yes
?- demo.
Crypto Problem: numbers = {8, 12, 9, 12, 0} Goal = 8
Possible Solution: ( ( 9 * 0 ) - ( 12 - ( 8 + 12 ) ) )

Yes
?- demo5.
Crypto Problem: numbers = {10, 12, 6, 5, 15} Goal = 12
Possible Solution: ( 6 * ( ( 10 + 12 ) - ( 5 + 15 ) ) )

Crypto Problem: numbers = {1, 12, 12, 3, 9} Goal = 13
Possible Solution: ( ( 9 + ( 1 + 12 ) ) - ( 12 - 3 ) )

Crypto Problem: numbers = {2, 8, 4, 0, 2} Goal = 0
Possible Solution: ( ( 4 * 0 ) * ( 2 + ( 2 + 8 ) ) )

Crypto Problem: numbers = {5, 14, 14, 8, 8} Goal = 0
Possible Solution: ( ( 14 + ( 5 + 14 ) ) * ( 8 - 8 ) )

Crypto Problem: numbers = {6, 9, 3, 3, 15} Goal = 8
Possible Solution: ( ( 3 * 3 ) - ( ( 6 + 9 ) / 15 ) )

Yes
?- demo5.
Crypto Problem: numbers = {12, 2, 5, 7, 7} Goal = 15
Possible Solution: ( 5 * ( ( 7 + ( 12 + 2 ) ) / 7 ) )

Crypto Problem: numbers = {1, 5, 12, 14, 9} Goal = 3
Possible Solution: ( ( ( 1 + 5 ) * ( 14 - 12 ) ) - 9 )

Crypto Problem: numbers = {13, 2, 1, 0, 2} Goal = 12
Possible Solution: ( ( ( 13 + 2 ) - 2 ) - ( 1 + 0 ) )

Crypto Problem: numbers = {5, 2, 9, 3, 8} Goal = 4
Possible Solution: ( ( 9 / 3 ) + ( 8 - ( 5 + 2 ) ) )

Crypto Problem: numbers = {14, 8, 9, 3, 1} Goal = 2
Possible Solution: ( ( 14 + 8 ) / ( ( 9 + 3 ) - 1 ) )

Yes
?- halt.

Crypto.pro Lab3 part4

%-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-%
%
% FILE: crypto.pro in directory xsolve3.
% TYPE: Prolog source code.
% LINE: Crypto.
% DATE: 09/29/2009.
%
%-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-%

% Load of combosets.pro

:- consult('gv.pro').
:- consult('combosets.pro').

establishCryptoProblemParameters :-
declare(lo,0),
declare(hi,15).

generateRandomCryptoNumber(R) :-
valueOf(lo, Lo),
valueOf(hi, Hi),
Hip is Hi + 1,
random(Lo,Hip,R).

generateRandomCryptoProblem :-
generateRandomCryptoNumber(N1),
generateRandomCryptoNumber(N2),
generateRandomCryptoNumber(N3),
generateRandomCryptoNumber(N4),
generateRandomCryptoNumber(N5),
generateRandomCryptoNumber(G),
addCryptoProblemToKnowledgeBase(N1,N2,N3,N4,N5,G).

addCryptoProblemToKnowledgeBase(N1,N2,N3,N4,N5,G) :-
retract(problem(_,_)),
assert(problem(numbers(N1,N2,N3,N4,N5) ,goal(G))).
addCryptoProblemToKnowledgeBase(N1,N2,N3,N4,N5,G) :-
assert(problem(numbers(N1,N2,N3,N4,N5) ,goal(G))).

%-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-

% Display the problem after it has been internalized.

displayProblem :-
problem(numbers(N1,N2,N3,N4,N5) ,goal(G)),
write('Crypto Problem: numbers = {'),
write(N1), write(', '),
write(N2), write(', '),
write(N3), write(', '),
write(N4), write(', '),
write(N5), write('} Goal = '),
write(G), nl.

%-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-

% Internalize the problem.

internalizeProblem :-
problem(numbers(N1,N2,N3,N4,N5) ,goal(G)),
eraseProblemBindings,
eraseProblem,
eraseSolution,
assert( problem( numbers(N1,N2,N3,N4,N5), goal(G) ) ),
assert( binding(n1, N1 ) ),
assert( binding(n2, N2 ) ),
assert( binding(n3, N3 ) ),
assert( binding(n4, N4 ) ),
assert( binding(n5, N5 ) ),
assert( binding(g, G ) ).

eraseProblem :-
retract( problem( _,_ ) ),
fail.
eraseProblem.

eraseSolution :-
retract( solution( _ ) ),
fail.
eraseSolution.

eraseProblemBindings :-
retract( binding( n1, _) ),
retract( binding( n2, _) ),
retract( binding( n3, _) ),
retract( binding( n4, _) ),
retract( binding( n5, _) ),
retract( binding( g, _) ),
fail.
eraseProblemBindings.

%-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-

% Solve the problem decompositionally after internalization.

solveProblemDecompositionally :-
problem(numbers(N1,N2,N3,N4,N5) ,goal(G)),
crypto(N1,N2,N3,N4,N5,G, Expression),
assert(solution(Expression)).

crypto(N1,N2,Goal,ex(N1,+,N2)) :-Goal is (N1 + N2).
crypto(N1,N2,Goal,ex(N1,*,N2)) :-Goal is (N1 * N2).
crypto(N1,N2,Goal,ex(N1,-,N2)) :-Goal is (N1 - N2).
crypto(N1,N2,Goal,ex(N2,-,N1)) :-Goal is (N2 - N1).
crypto(N1,N2,Goal,ex(N1,/,N2)) :-N2 > 0, Goal is (N1 / N2).
crypto(N1,N2,Goal,ex(N2,/,N1)) :-N1 > 0, Goal is (N2 / N1).

crypto( N1, N2, N3, G, Expr ) :-
combos( set(N1,N2,N3), combo(A,B), extra(C) ),
crypto( A, B, SG, SGE ),
crypto( C, SG, G, UGE ),
substitute( SGE, SG, UGE, Expr ).

crypto( N1, N2, N3, N4, G, Expr ) :-
combos( set(N1,N2,N3,N4), combo(A,B), extras(C,D) ),
crypto( A, B, SG, SGE ),
crypto( C, D, SG, G, UGE ),
substitute( SGE, SG, UGE, Expr).

crypto( N1, N2, N3, N4, N5, G, Expr ) :-
combos( set(N1,N2,N3,N4,N5), combo(A,B), extras(C,D,E) ),
crypto( A, B, SG, SGE ),
crypto( C, D, E, SG, G, UGE ),
substitute( SGE, SG, UGE, Expr).

%-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

% Key substitution code.

substitute( New, Old, ex( Old, O, Z ), ex( New, O, Z ) ).
substitute( New, Old, ex( X, O, Old ), ex( X, O, New ) ).
substitute( New, Old, ex( X, O, Z ), ex( Q, O, Z) ) :-
substitute( New, Old, X, Q ).
substitute( New, Old, ex( X, O, Z), ex(X, O, Q ) ) :-
substitute( New, Old, Z, Q ).

%-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

% Display the solution after being solved.

displaySolution :-
write( 'Possible Solution: '),
solution( S ),
displayResult( S ),
nl.
displaySolution.

%-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

% Display the result after being solved.

displayResult(ex(A,O,B)) :-
number(A) ,number(B),
write('( '),write(A),write(' '),write(O),write(' '),write(B),write(' )').
displayResult(ex(A,O,B)) :-
number(A), B = ex(A1,O1,B1),
write('( '),write(A),write(' '),write(O),write(' '),
displayResult(ex(A1,O1,B1)),write(' )').
displayResult(ex(A,O,B)) :-
number(B), A = ex(A1,O1,B1),
write('( '),displayResult(ex(A1,O1,B1)),write(' '),write(O),write(' '),
write(B),write(' )').
displayResult(ex(A,O,B)) :-
A = ex(A1,O1,B1),B = ex(A2,O2,B2),
write('( '),displayResult(ex(A1,O1,B1)),write(' '),write(O),write(' '),
displayResult(ex(A2,O2,B2)),write(' )').

%-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

% Demo program.

demo :-
generateRandomCryptoProblem,
internalizeProblem,
displayProblem,
solveProblemDecompositionally,
displaySolution.

%-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

% Demo five times.

demo5 :-
demo,nl, demo,nl, demo,nl, demo,nl, demo.

%-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

% Establish a particular crypto problem.

establishCrptoProblem(numbers(N1,N2,N3,N4,N5) ,goal(G)) :-
addCryptoProblemToKnowledgeBase(N1,N2,N3,N4,N5,G).

%-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

% Crypto Problem Solver.

solve(numbers(N1,N2,N3,N4,N5) ,goal(G)) :-
establishCryptoProblem(numbers(N1,N2,N3,N4,N5) ,goal(G)),
internalizeProblem,
displayProblem,
solveProblemDecompositionally,
displaySolution.

%-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

% Initialize problem.

:- establishCryptoProblemParameters.

%-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Demo Crypto.pro Lab3 part3

DEMO

?- consult('crypto.pro').
% combosets.pro compiled 0.00 sec, 7,584 bytes
% crypto.pro compiled 0.00 sec, 11,152 bytes

Yes
?- crypto(1,2,3,4,5,15,Expr).

Expr = ex(ex(ex(1, +, 2), +, 4), +, ex(5, +, 3)) ;

Expr = ex(ex(3, +, 4), +, ex(5, +, ex(1, +, 2))) ;

Expr = ex(5, +, ex(ex(1, +, 2), +, ex(3, +, 4))) ;

Expr = ex(5, +, ex(3, +, ex(ex(1, +, 2), +, 4))) ;

Expr = ex(ex(1, +, 2), +, ex(5, +, ex(3, +, 4))) ;

Expr = ex(3, +, ex(5, +, ex(ex(1, +, 2), +, 4))) ;

Expr = ex(ex(4, -, ex(1, +, 2)), *, ex(5, *, 3)) ;

Expr = ex(ex(4, -, 3), *, ex(5, *, ex(1, +, 2))) ;

Expr = ex(ex(5, *, ex(1, +, 2)), /, ex(4, -, 3)) ;

Expr = ex(ex(5, *, 3), /, ex(4, -, ex(1, +, 2)))

Yes
?- crypto(1,2,2,4,7,15,Expr).

Expr = ex(ex(7, *, ex(1, +, 2)), -, ex(2, +, 4)) ;

Expr = ex(ex(1, +, 2), *, ex(7, +, ex(2, -, 4))) ;

Expr = ex(ex(1, +, 2), *, ex(7, -, ex(4, -, 2))) ;

Expr = ex(ex(1, +, 2), *, ex(7, -, ex(4, /, 2))) ;

Expr = ex(ex(1, +, 2), *, ex(ex(2, +, 7), -, 4)) ;

Expr = ex(ex(2, *, 7), +, ex(4, -, ex(1, +, 2))) ;

Expr = ex(ex(2, *, 7), -, ex(ex(1, +, 2), -, 4)) ;

Expr = ex(4, -, ex(ex(1, +, 2), -, ex(2, *, 7))) ;

Expr = ex(4, +, ex(ex(2, *, 7), -, ex(1, +, 2))) ;

Expr = ex(ex(4, +, ex(2, *, 7)), -, ex(1, +, 2)) ;

Expr = ex(ex(2, +, ex(1, +, 2)), *, ex(7, -, 4))

Yes
?-halt.

Crypto.pro Lab3 part3

%-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-%
% %
% FILE: crypto.pro in directory xsolve2. %
% TYPE: Prolog source code. %
% LINE: Crypto. %
% DATE: 09/29/2009. %
% %
%-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-%

% Load of combosets.pro

:- consult('combosets.pro').

crypto(N1,N2,Goal,ex(N1,+,N2)) :-Goal is (N1 + N2).
crypto(N1,N2,Goal,ex(N1,*,N2)) :-Goal is (N1 * N2).
crypto(N1,N2,Goal,ex(N1,-,N2)) :-Goal is (N1 - N2).
crypto(N1,N2,Goal,ex(N2,-,N1)) :-Goal is (N2 - N1).
crypto(N1,N2,Goal,ex(N1,/,N2)) :-N2 > 0, Goal is (N1 / N2).
crypto(N1,N2,Goal,ex(N2,/,N1)) :-N1 > 0, Goal is (N2 / N1).

crypto( N1, N2, N3, G, Expr ) :-
combos( set(N1,N2,N3), combo(A,B), extra(C) ),
crypto( A, B, SG, SGE ),
crypto( C, SG, G, UGE ),
substitute( SGE, SG, UGE, Expr ).

crypto( N1, N2, N3, N4, G, Expr ) :-
combos( set(N1,N2,N3,N4), combo(A,B), extras(C,D) ),
crypto( A, B, SG, SGE ),
crypto( C, D, SG, G, UGE ),
substitute( SGE, SG, UGE, Expr).

crypto( N1, N2, N3, N4, N5, G, Expr ) :-
combos( set(N1,N2,N3,N4,N5), combo(A,B), extras(C,D,E) ),
crypto( A, B, SG, SGE ),
crypto( C, D, E, SG, G, UGE ),
substitute( SGE, SG, UGE, Expr).

%-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
% Key substitution code.

substitute( New, Old, ex( Old, O, Z ), ex( New, O, Z ) ).
substitute( New, Old, ex( X, O, Old ), ex( X, O, New ) ).
substitute( New, Old, ex( X, O, Z ), ex( Q, O, Z) ) :-
substitute( New, Old, X, Q ).
substitute( New, Old, ex( X, O, Z), ex(X, O, Q ) ) :-
substitute( New, Old, Z, Q ).

Tuesday, September 29, 2009

Demo Crypto.pro Lab3 part 2

DEMO

?- consult('crypto.pro').
% combosets.pro compiled 0.00 sec, 7,584 bytes
% crypto.pro compiled 0.00 sec, 10,688 bytes

Yes
?- crypto(1,2,3,A).

A = ex(1, +, 2) ;

No
?- crypto(1,2,3,6,A).

A = ex(ex(1, +, 2), +, 3) ;

A = ex(3, +, ex(1, +, 2)) ;

A = ex(3, *, ex(1, *, 2)) ;

A = ex(3, *, ex(2, /, 1)) ;

A = ex(1, +, ex(2, +, 3)) ;

A = ex(1, *, ex(2, *, 3)) ;

A = ex(ex(2, *, 3), /, 1) ;

A = ex(2, +, ex(1, +, 3)) ;

A = ex(2, *, ex(1, *, 3)) ;

A = ex(2, *, ex(3, /, 1)) ;

No
?- crypto(1,2,3,6,12,A).

A = ex(ex(1, +, 2), +, ex(3, +, 6)) ;

A = ex(3, +, ex(ex(1, +, 2), +, 6)) ;

A = ex(ex(1, +, 2), +, ex(6, +, 3)) ;

A = ex(3, +, ex(6, +, ex(1, +, 2))) ;

A = ex(ex(ex(1, +, 2), +, 3), +, 6) ;

A = ex(ex(3, +, ex(1, +, 2)), +, 6) ;

A = ex(6, +, ex(ex(1, +, 2), +, 3)) ;

A = ex(6, +, ex(3, +, ex(1, +, 2))) ;

A = ex(3, *, ex(6, -, ex(1, *, 2))) ;

A = ex(ex(3, *, ex(1, *, 2)), +, 6) ;

A = ex(6, +, ex(3, *, ex(1, *, 2))) ;

A = ex(6, *, ex(3, +, ex(1, -, 2))) ;

A = ex(6, *, ex(3, -, ex(2, -, 1))) ;

A = ex(3, *, ex(6, -, ex(2, /, 1))) ;

A = ex(ex(3, *, ex(2, /, 1)), +, 6) ;

A = ex(6, +, ex(3, *, ex(2, /, 1))) ;

A = ex(ex(1, +, 3), +, ex(2, +, 6)) ;

A = ex(ex(1, +, 3), *, ex(6, /, 2)) ;

A = ex(2, +, ex(6, +, ex(1, +, 3))) ;

A = ex(ex(6, *, ex(1, +, 3)), /, 2) ;

A = ex(ex(2, +, ex(1, +, 3)), +, 6) ;

A = ex(6, +, ex(2, +, ex(1, +, 3))) ;

A = ex(6, *, ex(ex(1, +, 3), -, 2)) ;

A = ex(6, *, ex(ex(1, +, 3), /, 2)) ;

A = ex(ex(1, *, 3), *, ex(6, -, 2)) ;

A = ex(ex(2, *, ex(1, *, 3)), +, 6) ;

A = ex(6, +, ex(2, *, ex(1, *, 3))) ;

A = ex(ex(3, /, 1), *, ex(6, -, 2)) ;

A = ex(ex(2, *, ex(3, /, 1)), +, 6) ;

A = ex(6, +, ex(2, *, ex(3, /, 1))) ;

A = ex(ex(1, +, 6), +, ex(2, +, 3)) ;

A = ex(2, +, ex(3, +, ex(1, +, 6))) ;

A = ex(3, +, ex(2, +, ex(1, +, 6))) ;

A = ex(ex(2, *, 3), +, ex(1, *, 6)) ;

A = ex(ex(1, *, 6), +, ex(2, *, 3)) ;

A = ex(3, *, ex(ex(1, *, 6), -, 2)) ;

A = ex(ex(2, *, 3), +, ex(6, /, 1)) ;

A = ex(ex(6, /, 1), +, ex(2, *, 3)) ;

A = ex(3, *, ex(ex(6, /, 1), -, 2)) ;

A = ex(ex(2, +, 3), +, ex(1, +, 6)) ;

A = ex(1, +, ex(6, +, ex(2, +, 3))) ;

A = ex(ex(1, +, ex(2, +, 3)), +, 6) ;

A = ex(6, +, ex(1, +, ex(2, +, 3))) ;

A = ex(ex(1, *, 6), +, ex(2, *, 3)) ;

A = ex(ex(1, *, ex(2, *, 3)), +, 6) ;

A = ex(ex(2, *, 3), +, ex(1, *, 6)) ;

A = ex(6, +, ex(1, *, ex(2, *, 3))) ;

A = ex(ex(6, /, 1), +, ex(2, *, 3)) ;

A = ex(ex(ex(2, *, 3), /, 1), +, 6) ;

A = ex(ex(2, *, 3), +, ex(6, /, 1)) ;

A = ex(6, +, ex(ex(2, *, 3), /, 1)) ;

A = ex(1, *, ex(ex(2, *, 3), +, 6)) ;

A = ex(1, *, ex(6, +, ex(2, *, 3))) ;

A = ex(ex(ex(2, *, 3), +, 6), /, 1) ;

A = ex(ex(6, +, ex(2, *, 3)), /, 1) ;

A = ex(ex(1, *, 6), +, ex(2, *, 3)) ;

A = ex(ex(1, *, ex(2, *, 3)), +, 6) ;

A = ex(ex(2, *, 3), +, ex(1, *, 6)) ;

A = ex(6, +, ex(1, *, ex(2, *, 3))) ;

A = ex(ex(6, /, 1), +, ex(2, *, 3)) ;

A = ex(ex(ex(2, *, 3), /, 1), +, 6) ;

A = ex(ex(2, *, 3), +, ex(6, /, 1)) ;

A = ex(6, +, ex(ex(2, *, 3), /, 1)) ;

A = ex(6, *, ex(1, -, ex(2, -, 3))) ;

A = ex(6, *, ex(ex(3, -, 2), +, 1)) ;

A = ex(6, *, ex(1, +, ex(3, -, 2))) ;

A = ex(ex(2, +, 6), +, ex(1, +, 3)) ;

A = ex(1, +, ex(3, +, ex(2, +, 6))) ;

A = ex(3, +, ex(1, +, ex(2, +, 6))) ;

A = ex(ex(6, -, 2), *, ex(1, *, 3)) ;

A = ex(ex(6, -, 2), *, ex(3, /, 1)) ;

A = ex(1, *, ex(3, *, ex(6, -, 2))) ;

A = ex(ex(3, *, ex(6, -, 2)), /, 1)

Yes
?- halt.

Crypto.pro Lab3 part 2

%-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-%
% %
% FILE: crypto.pro in directory xsolve1. %
% TYPE: Prolog source code. %
% LINE: Crypto. %
% DATE: 09/29/2009. %
% %
%-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-%

% Load of combosets.pro

:- consult('combosets.pro').

crypto(N1,N2,Goal,ex(N1,+,N2)) :-Goal is (N1 + N2).
crypto(N1,N2,Goal,ex(N1,*,N2)) :-Goal is (N1 * N2).
crypto(N1,N2,Goal,ex(N1,-,N2)) :-Goal is (N1 - N2).
crypto(N1,N2,Goal,ex(N2,-,N1)) :-Goal is (N2 - N1).
crypto(N1,N2,Goal,ex(N1,/,N2)) :-N2 > 0, Goal is (N1 / N2).
crypto(N1,N2,Goal,ex(N2,/,N1)) :-N1 > 0, Goal is (N2 / N1).

crypto( N1, N2, N3, G, Expr ) :-
combos( set(N1,N2,N3), combo(A,B), extra(C) ),
crypto( A, B, SG, SGE ),
crypto( C, SG, G, UGE ),
substitute( SGE, SG, UGE, Expr ).

crypto( N1, N2, N3, N4, G, Expr ) :-
combos( set(N1,N2,N3,N4), combo(A,B), extra(C,D) ),
crypto( A, B, SG, SGE ),
crypto( C, D, SG, G, UGE ),
substitute( SGE, SG, UGE, Expr ).

%-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
% Key substitution code.

substitute( New, Old, ex( Old, O, Z ), ex( New, O, Z ) ).
substitute( New, Old, ex( X, O, Old ), ex( X, O, New ) ).
substitute( New, Old, ex( X, O, Z ), ex( Q, O, Z) ) :-
substitute( New, Old, X, Q ).
substitute( New, Old, ex( X, O, Z), ex(X, O, Q ) ) :-
substitute( New, Old, Z, Q ).

Demo combosets.pro

DEMO

?- consult('combosets.pro').
% combosets.pro compiled 0.00 sec, 7,432 bytes

Yes

?- combos(set(a,b,c),C,X).

C = combo(a, b)
X = extra(c) ;

C = combo(b, c)
X = extra(a) ;

C = combo(a, c)
X = extra(b) ;

No

?- combos(set(1,2,3,7),C,X).

C = combo(1, 2)
X = extras(3, 7) ;

C = combo(1, 3)
X = extras(2, 7) ;

C = combo(1, 7)
X = extras(2, 3) ;

C = combo(2, 3)
X = extras(1, 7) ;

C = combo(2, 7)
X = extras(1, 3) ;

C = combo(3, 7)
X = extras(1, 2) ;

No

?- combos(set(12,2,4,6),C,X).

C = combo(12, 2)
X = extras(4, 6) ;

C = combo(12, 4)
X = extras(2, 6) ;

C = combo(12, 6)
X = extras(2, 4) ;

C = combo(2, 4)
X = extras(12, 6) ;

C = combo(2, 6)
X = extras(12, 4) ;

C = combo(4, 6)
X = extras(12, 2) ;

No

?-halt.

Combosets.pro

% FILE: combosets.pro
% TYPE: Prolog source
% LINE: a bit of combinatorial set code
% DATE: November, 1995

%-----------------------------------------------------------------
% combination procedures

combos( set(N1,N2,N3), combo(N1,N2), extra(N3) ).
combos( set(N1,N2,N3), combo(N2,N3), extra(N1) ).
combos( set(N1,N2,N3), combo(N1,N3), extra(N2) ).

combos( set(N1,N2,N3,N4), combo(N1,N2), extras(N3,N4) ).
combos( set(N1,N2,N3,N4), combo(N1,N3), extras(N2,N4) ).
combos( set(N1,N2,N3,N4), combo(N1,N4), extras(N2,N3) ).
combos( set(N1,N2,N3,N4), combo(N2,N3), extras(N1,N4) ).
combos( set(N1,N2,N3,N4), combo(N2,N4), extras(N1,N3) ).
combos( set(N1,N2,N3,N4), combo(N3,N4), extras(N1,N2) ).

combos( set(N1,N2,N3,N4,N5), combo(N1,N2), extras(N3,N4,N5) ).
combos( set(N1,N2,N3,N4,N5), combo(N1,N3), extras(N2,N4,N5) ).
combos( set(N1,N2,N3,N4,N5), combo(N1,N4), extras(N2,N3,N5) ).
combos( set(N1,N2,N3,N4,N5), combo(N1,N5), extras(N2,N3,N4) ).
combos( set(N1,N2,N3,N4,N5), combo(N2,N3), extras(N1,N4,N5) ).
combos( set(N1,N2,N3,N4,N5), combo(N2,N4), extras(N1,N3,N5) ).
combos( set(N1,N2,N3,N4,N5), combo(N2,N5), extras(N1,N3,N4) ).
combos( set(N1,N2,N3,N4,N5), combo(N3,N4), extras(N1,N2,N5) ).
combos( set(N1,N2,N3,N4,N5), combo(N3,N5), extras(N1,N2,N4) ).
combos( set(N1,N2,N3,N4,N5), combo(N4,N5), extras(N1,N2,N3) ).


%-----------------------------------------------------------------
% the permutation facts

perm(s(A,B),p(A,B)).
perm(s(A,B),p(B,A)).

perm(s(A,B,C),p(A,X,Y)) :- perm(s(B,C),p(X,Y)).
perm(s(A,B,C),p(B,X,Y)) :- perm(s(A,C),p(X,Y)).
perm(s(A,B,C),p(C,X,Y)) :- perm(s(A,B),p(X,Y)).

perm(s(A,B,C,D),p(A,X,Y,Z)) :- perm(s(B,C,D),p(X,Y,Z)).
perm(s(A,B,C,D),p(B,X,Y,Z)) :- perm(s(A,C,D),p(X,Y,Z)).
perm(s(A,B,C,D),p(C,X,Y,Z)) :- perm(s(A,B,D),p(X,Y,Z)).
perm(s(A,B,C,D),p(D,X,Y,Z)) :- perm(s(A,B,C),p(X,Y,Z)).

perm(s(A,B,C,D,E),p(A,X,Y,Z,W)) :- perm(s(B,C,D,E),p(X,Y,Z,W)).
perm(s(A,B,C,D,E),p(B,X,Y,Z,W)) :- perm(s(A,C,D,E),p(X,Y,Z,W)).
perm(s(A,B,C,D,E),p(C,X,Y,Z,W)) :- perm(s(B,A,D,E),p(X,Y,Z,W)).
perm(s(A,B,C,D,E),p(D,X,Y,Z,W)) :- perm(s(B,C,A,E),p(X,Y,Z,W)).
perm(s(A,B,C,D,E),p(E,X,Y,Z,W)) :- perm(s(B,C,D,A),p(X,Y,Z,W)).

Thursday, September 24, 2009

Demo crypto.pro Lab 2


DEMO.


Welcome to SWI-Prolog (Multi-threaded, Version 5.6.24)
Copyright (c) 1990-2006 University of Amsterdam.
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit http://www.swi-prolog.org for details.

For help, use ?- help(Topic). or ?- apropos(Word).

?- consult('crypto.pro').
% gv.pro compiled 0.00 sec, 2,544 bytes
% crypto.pro compiled 0.00 sec, 5,528 bytes

Yes
?- eCryptoP.

Yes
?- gRNumber(N).

N = 2 ;

No
?- gRProblem.

Yes
?- gRProblem.

Yes
?- gRProblem.

Yes
?- gRProblem.

Yes
?- gRProblem.

Yes
?- gRProblem.

Yes
?- gRProblem.

Yes
?- gRProblem.

Yes
?- gRProblem.

Yes
?- gRProblem.

Yes
?- demo.
Crypto Problem: Numbers = { 10, 5, 0, 14, 14} Goal = 8
Crypto Problem: Numbers = { 2, 0, 11, 1, 8} Goal = 13
Crypto Problem: Numbers = { 6, 0, 5, 1, 7} Goal = 0
Crypto Problem: Numbers = { 8, 8, 6, 4, 14} Goal = 3
Crypto Problem: Numbers = { 12, 2, 8, 10, 6} Goal = 4
Crypto Problem: Numbers = { 13, 11, 13, 11, 10} Goal = 12
Crypto Problem: Numbers = { 4, 9, 5, 11, 1} Goal = 7
Crypto Problem: Numbers = { 13, 8, 12, 15, 3} Goal = 13
Crypto Problem: Numbers = { 6, 5, 12, 0, 2} Goal = 3
Crypto Problem: Numbers = { 6, 9, 13, 0, 2} Goal = 14

No
?- halt.

Friday, September 18, 2009

crypto.pro

%----------------------------------------------------------------------------
% Jeremey Ferris Cog 366 Lab #2 Random Crypto Problem Generator
% Filename: crytpo.pro
% Load Files: gv.pro
% Date Created: 09/16/2009
%-----------------------------------------------------------------------------

:-consult('gv.pro').

eCryptoP:-
declare(a,0),
declare(b,15).

gRNumber(N):-
valueOf(a,Lo),
valueOf(b,Hi),
Hip is Hi+1,
random(Lo,Hip,N).

gRProblem:-
gRNumber(N1),
gRNumber(N2),
gRNumber(N3),
gRNumber(N4),
gRNumber(N5),
gRNumber(G),
addProbToKB(N1,N2,N3,N4,N5,G).

addProbToKB(N1,N2,N3,N4,N5,G):-
retract(problem(numbers(N1,N2,N3,N4,N5),goal(G))),
assert(problem(numbers(N1,N2,N3,N4,N5),goal(G))).
addProbToKB(N1,N2,N3,N4,N5,G):-
assert(problem(numbers(N1,N2,N3,N4,N5),goal(G))).

demo:-
problem(numbers(N1,N2,N3,N4,N5),goal(G)),
write('Crypto Problem: Numbers = { '),
write(N1), write(', '),
write(N2), write(', '),
write(N3), write(', '),
write(N4), write(', '),
write(N5), write('} Goal = '),
write(G),nl,fail.
eCryptoP.