Skip to content

Instantly share code, notes, and snippets.

@awentzonline
Last active June 15, 2019 23:45
Show Gist options
  • Select an option

  • Save awentzonline/f42eecdbb28fd21a60fb76381c301579 to your computer and use it in GitHub Desktop.

Select an option

Save awentzonline/f42eecdbb28fd21a60fb76381c301579 to your computer and use it in GitHub Desktop.
Find a family tree which contains an Uncle-Brother relation
male(donnie).
male(jimmy).
male(jethro).
male(cletus).
female(krystal).
female(ruthie).
female(blanche).
female(ruby).
person(A) :-
male(A); female(A).
parent_of(Parent, Child, ParentOf) :-
person(Parent), person(Child),
Parent \== Child,
member(Parent-Child, ParentOf).
has_parents(A, ParentOf) :-
male(Dad), female(Mom),
A \== Mom, A \== Dad,
member(Mom-A, ParentOf),
member(Dad-A, ParentOf).
same_mom(A, B, ParentOf) :-
female(Mom),
parent_of(Mom, A, ParentOf),
parent_of(Mom, B, ParentOf).
same_dad(A, B, ParentOf) :-
male(Dad),
parent_of(Dad, A, ParentOf),
parent_of(Dad, B, ParentOf).
brothers(A, B, ParentOf) :-
male(A), male(B),
A \== B,
has_parents(A, ParentOf),
has_parents(B, ParentOf),
(same_dad(A, B, ParentOf); % They share either a mom or dad
same_mom(A, B, ParentOf)). % No solutions for same mom and dad
uncle_of(Uncle, Nephew, ParentOf) :-
male(Uncle), person(Nephew),
Uncle \== Nephew,
brothers(Uncle, Brother, ParentOf),
parent_of(Brother, Nephew, ParentOf).
uncle_brother_of(UncleBrother, Pivot, ParentOf) :-
uncle_of(UncleBrother, Pivot, ParentOf),
brothers(UncleBrother, Pivot, ParentOf).
uncle_brother(UB, Pivot, ParentOf) :-
length(ParentOf, _),
uncle_brother_of(UB, Pivot, ParentOf).
:- set_prolog_flag(verbose, silent).
:- initialization(main).
main :-
UncleBrother = jethro,
uncle_brother(UncleBrother, Pivot, FamilyTree),
writef('Uncle brother of %w is %w\n', [Pivot, UncleBrother]),
writef('Family tree: %w\n', [FamilyTree]),
halt.
main :-
writef('No solution\n'),
halt.
@awentzonline

awentzonline commented Jun 15, 2019

Copy link
Copy Markdown
Author
Uncle brother of donnie is jethro
Family tree: [krystal-jethro,donnie-jethro,krystal-jimmy,donnie-jimmy,jimmy-donnie,krystal-donnie]

@awentzonline

Copy link
Copy Markdown
Author

I haven't written prolog since college so true 'loggers might hate this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment