PDA

View Full Version : I AM A GOLDEN GOD!


Sir Penguin
26-11-2004, 08:01:23
Submitted either 2 minutes before it's due, or 72 hours and 2 minutes before it's due, depending on whether or not the prof decided to give us an extension (either way, he apparently forgot to tell us).

Part 2 uses 2 more rules than Dr. Horspool said he needed, but Part 3 uses 1 less. :coolgrin: I haven't tested it extensively though, so I'm not convinced that it's completely correct.

% ass5.pl - Source file for C SC 330 Assignment 5.
% Author: Neil MacMillan, #0229199

% Part 1 - check if an expression is in Disjunctive Normal Form

% match a literal
isLiteral(X) :- atom(X).
isLiteral(not(X)) :- atom(X).

% match a conjunction
isConjunct(X) :- isLiteral(X).
isConjunct(and(X,Y)) :- isConjunct(X),isConjunct(Y).

% match a disjunction
dnf(X) :- isConjunct(X).
dnf(or(X,Y)) :- dnf(X),dnf(Y).


% Part 2 - cancel double-negations in a boolean expression
cancel_negations(not(not(X)),X1) :- cancel_negations(X,X1).
cancel_negations(or(X,Y),or(X1,Y1)) :- cancel_negations(X,X1),cancel_negations(Y,Y1).
cancel_negations(not(or(X,Y)),not(or(X1,Y1))) :- cancel_negations(X,X1),cancel_negations(Y,Y1).
cancel_negations(and(X,Y),and(X1,Y1)) :- cancel_negations(X,X1),cancel_negations(Y,Y1).
cancel_negations(not(and(X,Y)),not(and(X1,Y1))) :- cancel_negations(X,X1),cancel_negations(Y,Y1).
cancel_negations(X,X) :- isLiteral(X).


% Part 3 - apply DeMorgan's law to a boolean expression
deMorgan(not(and(X,Y)), or(X1,Y1)) :-
cancel_negations(not(X),X2),
deMorgan(X2,X1),
cancel_negations(not(Y),Y2),
deMorgan(Y2,Y1).
deMorgan(not(or(X,Y)), and(X1,Y1)) :-
cancel_negations(not(X),X2),
deMorgan(X2,X1),
cancel_negations(not(Y),Y2),
deMorgan(Y2,Y1).
deMorgan(not(not(X)), X1) :-
cancel_negations(X,X2),
deMorgan(X2,X1).
deMorgan(and(X,Y), and(X1,Y1)) :-
cancel_negations(X,X2),
deMorgan(X2,X1),
cancel_negations(Y,Y2),
deMorgan(Y2,Y1).
deMorgan(or(X,Y), or(X1,Y1)) :-
cancel_negations(X,X2),
deMorgan(X2,X1),
cancel_negations(Y,Y2),
deMorgan(Y2,Y1).
deMorgan(X,X) :- isLiteral(X).


SP

Asher
26-11-2004, 13:04:35
Excellent work, what does this mean for my pr0n collection?

Sir Penguin
26-11-2004, 19:41:48
Is it boolean?

SP

Drekkus
26-11-2004, 19:55:52
You're not the negotiating type, are you?

miester gandertak
28-11-2004, 11:39:11
Originally posted by Drekkus
You're not the negotiating type, are you? :lol:

LoD
06-12-2004, 13:46:28
PROLOG? NOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO OOOOOOOOOOOOOOOOO!

Nav
07-12-2004, 11:24:06
out of interest what does this mean ':-'?

Sir Penguin
07-12-2004, 18:28:11
It means "if", or "implies". The statement on the left is true if the statement on the right is true. Or, if the statement on the right is true, then that implies that the statement on the left is true. Same difference.

SP

Sir Penguin
10-12-2004, 21:56:51
This is an automatically generated message which explains the grading of your
assignment 5.

Each predicate is worth 10 points, for a total of 30. dnf has 8 simple tests
worth 1 point each as the only possible outputs are yes or no. cancel_negations
and deMorgan have 4 tests each, using unification to find correct solutions that
are worth 2 points each, if your code generated multiple solutions where some
are incorrect there was no penalty so long as the correct output was there as
well. This results in 8 points for each question, with an additional 2 each for
Code Inspection.

The details below show the test input to the prolog interpreter followed by a -
and the expected output.
In your case your mark breaks down as follows:

dnf:
1 / 1 1.1: dnf( not(a) ). - yes
1 / 1 1.2: dnf( and( and(not(a), not(b)), c) ). - yes
1 / 1 1.3: dnf( or(a, or(and(b, not(a)), not(c)) ) ). - yes
1 / 1 1.4: dnf(or( or( and(and(not(a),not(b)),c), and(and(a,not(b)),not(c))),
or( and(and(not(a),b),not(c)), and(and(a,b),c) ) )). - yes
1 / 1 1.5: dnf( not(not(a)) ). - no
1 / 1 1.6: dnf( not( and(a,b) ) ). - no
1 / 1 1.7: dnf( and( or(a,b), c ) ). - no
1 / 1 1.8: dnf( or(a, or(not(and(b, a)), not(c)) ) ). - no
2 / 2 1.9: dnf code inspection

cancel_negations:
2 / 2 2.1: cancel_negations( not(not(a)), W). - W = a
2 / 2 2.2: cancel_negations( or( not( not(and(a,b)) ), c), X). - X =
or(and(a,b),c)
2 / 2 2.3: cancel_negations( not(and( or(not(a),not(b)), or(not(c),not(d)) )),
Y). - Y = not(and(or(not(a),not(b)),or(not(c),not(d))))
2 / 2 2.4: cancel_negations( not(and( not(not(or(not(a),not(not(b))))),
or(not(not(c)),not(d)) )), Z). - Z = not(and(or(not(a),b),or(c,not(d))))
2 / 2 2.5: cancel_negations code inspection

deMorgan:
2 / 2 3.1: deMorgan( not(and(a,not(b))), W). - W = or(not(a),b)
2 / 2 3.2: deMorgan( or(a, not(or(and(a,b),c))), X). - X =
or(a,and(or(not(a),not(b)),not(c)))
2 / 2 3.3: deMorgan( not(or(not(and(a,c)),b)), Y). - Y = and(and(a,c),not(b))
2 / 2 3.4: deMorgan(
not(and(or(not(and(a,b)),not(c)),or(not(and(d,e)), not(f)))), Z). - Z =
or(and(and(a,b),c),and(and(d,e),f))
2 / 2 3.5: deMorgan code inspection

Your total score was 30 marks (out of 30).

:bounce:

The final for this class was dead simple, too. I'm pretty sure I would have got an A+ if I had heard that the third assignment (worth 10% of the final mark) was posted, before it was due.

SP