From 40837bf9d7bb44787048e476cd66b58e847ab4e8 Mon Sep 17 00:00:00 2001 From: Alex Huddleston Date: Wed, 15 Nov 2017 23:56:06 +0000 Subject: [PATCH] Hurry omg. --- hw3/README.txt | 35 +++++++++++++ hw3/hw3pr2.pl | 70 +++++++++++++++++++------- hw3/hw3pr3.pl | 133 +++++++++++++++++++++++++++++++++++++++++++++++++ hw3/testing.pl | 22 ++++++++ 4 files changed, 243 insertions(+), 17 deletions(-) create mode 100644 hw3/README.txt create mode 100644 hw3/hw3pr3.pl create mode 100644 hw3/testing.pl diff --git a/hw3/README.txt b/hw3/README.txt new file mode 100644 index 0000000..1f64589 --- /dev/null +++ b/hw3/README.txt @@ -0,0 +1,35 @@ +//Alexander Huddleston +//CSCE 420 +//Due: November 15, 2017 +//README.txt + +run gprolog +[hw3prX]. + +problem 1 expects arguments: + +my_sort(Unsorted, Sorted). + +problem 2 expects arguments: + +my_topo_sort(Partial_order, Total_order). + +problem 3 can tell you these relations: + +is_spouse(X, Y). NOTE: Only expected to be used in the rest of the definitions. +is_child(X, Y). NOTE: Only expected to be used in the rest of the definitions. + +grandchild(X, Y). +greatgrandparent(X, Y). +ancestor(X, Y). +brother(X, Y). +sister(X, Y). +daughter(X, Y). +son(X, Y). +first_cousin(X, Y). +brother_in_law(X, Y). +sister_in_law(X, Y). +aunt(X, Y). +uncle(X, Y). + +cousin(X, Y). NOTE: This just called first_cousin(X, Y), since a cousin is a synonym for first cousin. diff --git a/hw3/hw3pr2.pl b/hw3/hw3pr2.pl index c3fce50..3082f2a 100644 --- a/hw3/hw3pr2.pl +++ b/hw3/hw3pr2.pl @@ -1,25 +1,61 @@ -my_sort([], Sorted2, Sorted2). +:- dynamic(greater/2). -my_sort(Unsorted, Sorted, Sorted2) :- - get_max(Unsorted, Max, LessList), - append(Sorted, [Max], NewSorted), - my_sort(LessList, NewSorted, Sorted2). +make_rules([]). -my_sort(Unsorted, Sorted) :- - my_sort(Unsorted, [], ReverseSorted), - reverse(ReverseSorted, Sorted), - write(Sorted). +make_rules([X]). + +make_rules([X,Y|XS]) :- + \+ is_greater(X, Y), + assertz(greater(Y, X)), + append([Y], XS, XS2), + make_rules(XS2). + +all_rules([]). + +all_rules(Partial_order) :- + select(X, Partial_order, Partial_order2), + make_rules(X), + all_rules(Partial_order2). + +is_greater(Y, X) :- + greater(Y, X); + greater(Z, X), + is_greater(Y, Z). + +remove_duplicates([], Unsorted_flat_concise, Unsorted_flat_concise). + +remove_duplicates(Unsorted_flat, Unsorted_flat2, Unsorted_flat_concise) :- + select(X, Unsorted_flat, Unsorted_flat_rest), + (member(X, Unsorted_flat2), + remove_duplicates(Unsorted_flat_rest, Unsorted_flat2, Unsorted_flat_concise); + append([X], Unsorted_flat2, Unsorted_flat3), + remove_duplicates(Unsorted_flat_rest, Unsorted_flat3, Unsorted_flat_concise)). + +my_topo_sort([], Sorted2, Sorted2). + +my_topo_sort(Unsorted, Sorted, Sorted2) :- + get_max(Unsorted, Max, LessList), + append(Sorted, [Max], NewSorted), + my_topo_sort(LessList, NewSorted, Sorted2). + +my_topo_sort(Unsorted, Sorted) :- + all_rules(Unsorted), + flatten(Unsorted, Unsorted_flat), + remove_duplicates(Unsorted_flat, [], Unsorted_flat_concise), + my_topo_sort(Unsorted_flat_concise, [], ReverseSorted), + reverse(ReverseSorted, Sorted), + write(Sorted). get_max([], Max, LessList, Max, LessList). get_max(Unsorted, X, Unsorted2, Max, LessList) :- - select(Y, Unsorted, Rest), - (X > Y, - append(Unsorted2, [Y], NewList), - get_max(Rest, X, NewList, Max, LessList); - append(Unsorted2, [X], NewList), - get_max(Rest, Y, NewList, Max, LessList)). + select(Y, Unsorted, Rest), + (is_greater(X, Y), + append(Unsorted2, [Y], NewList), + get_max(Rest, X, NewList, Max, LessList); + append(Unsorted2, [X], NewList), + get_max(Rest, Y, NewList, Max, LessList)). get_max(Unsorted, Max, LessList) :- - select(X, Unsorted, Unsorted2), - get_max(Unsorted2, X, [], Max, LessList). + select(X, Unsorted, Unsorted2), + get_max(Unsorted2, X, [], Max, LessList). diff --git a/hw3/hw3pr3.pl b/hw3/hw3pr3.pl new file mode 100644 index 0000000..baeaf4f --- /dev/null +++ b/hw3/hw3pr3.pl @@ -0,0 +1,133 @@ +male(george). +male(phillip). +male(spencer). +male(charles). +male(mark). +male(andrew). +male(edward). +male(william). +male(harry). +male(peter). +male(mike). +male(james). +male(george2). + +spouse(george, mum). +spouse(elizabeth, philip). +spouse(spencer, kydd). +spouse(diana, charles). +spouse(anne, mark). +spouse(andrew, sarah). +spouse(edward, sophie). +spouse(william, catherine). +spouse(peter, autumn). +spouse(zara, mike). + +child(elizabeth, mum). +child(margaret, mum). +child(charles, elizabeth). +child(anne, elizabeth). +child(andrew, elizabeth). +child(edward, elizabeth). +child(diana, kydd). +child(william, diana). +child(harry, diana). +child(peter, anne). +child(zara, anne). +child(beatrice, sarah). +child(eugenie, sarah). +child(louise, sophie). +child(james, sophie). +child(george2, catherine). +child(charlotte, catherine). +child(savannah, autumn). +child(isla, autumn). +child(mia, zara). + +is_spouse(X, Y) :- + spouse(X,Y); + spouse(Y,X). + +is_child(X, Y) :- + child(X, Y); + (is_spouse(Z, Y), + child(X, Z)). + +grandchild(X, Y) :- + is_child(Z, Y), + is_child(X, Z). + +greatgrandparent(X, Y) :- + (is_child(Z, X), + is_child(A, Z), + is_child(Y, A)); + (is_spouse(X,B), + is_child(Z, B), + is_child(A, Z), + is_child(Y, A)). + +ancestor(X, Y) :- + is_child(Y, X); + (is_child(Z, X), + ancestor(Z, Y)). + +brother(X, Y) :- + child(X, A), + child(Y, A), + male(X), + \==(X, Y). + +sister(X, Y) :- + child(X, A), + child(Y, A), + \+ male(X), + \==(X, Y). + +daughter(X, Y) :- + is_child(X, Y), + \+ male(X). + +son(X, Y) :- + is_child(X, Y), + male(X). + +first_cousin(X, Y) :- + (uncle(A, X), + is_child(Y, A)). + +brother_in_law(X, Y) :- + (spouse(B, X), + child(B, A), + child(Z, A), + spouse(Y, Z), + \+ is_spouse(Y, X), + male(X)); + (child(X, A), + child(Z, A), + spouse(Y, Z), + \+ is_spouse(Y, X), + male(X)). + +sister_in_law(X, Y) :- + (spouse(B, X), + child(B, A), + child(Z, A), + spouse(Y, Z), + \+ is_spouse(Y, X), + \+ male(X)); + (child(X, A), + child(Z, A), + spouse(Y, Z), + \+ is_spouse(Y, X), + \+ male(X)). + +aunt(X, Y) :- + is_child(Y, Z), + sister(X, Z). + +uncle(X, Y) :- + is_child(Y, Z), + brother(X, Z). + +cousin(X, Y) :- + first_cousin(X, Y). diff --git a/hw3/testing.pl b/hw3/testing.pl new file mode 100644 index 0000000..9aab5b6 --- /dev/null +++ b/hw3/testing.pl @@ -0,0 +1,22 @@ +:- dynamic(greater/2). + +make_rules([]). + +make_rules([X]). + +make_rules([X,Y|XS]) :- + assertz(greater(Y, X)), + append([Y], XS, XS2), + make_rules(XS2). + +my_topo_sort([], Total_order). + +my_topo_sort(Partial_order, Total_order) :- + select(X, Partial_order, Partial_order2), + make_rules(X), + my_topo_sort(Partial_order2, Total_order). + +is_greater(Y, X) :- + greater(Y, X); + greater(Z, X), + is_greater(Y, Z).