Skip to content

Instantly share code, notes, and snippets.

@GaProgMan
Last active December 25, 2015 07:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GaProgMan/6941592 to your computer and use it in GitHub Desktop.
Save GaProgMan/6941592 to your computer and use it in GitHub Desktop.
A short Prolog script that works with a family dataset. There are basic commands to reason the relationships of the members of the family.
/*
* Project Name: Parents
* Solution Name: Parents
* Original creation date: 01/10/2006
* Edit date: 11/10/2013
* Programmer name: Jamie Taylor (aka "GaProgMan")
* File name: parents.pl
*
* Purpose of the project:
* This project was undertaken as coursework in class
* 08226 (Artificial Intelligence). The purpose of this
* coursework was to make students aware of how relationships
* are created between data objects in a Prolog code listing.
* We were given a list of people and their
* relationships with each other and were told to create
* a reasoning engine that could answer basic questions
* about the relatioships between the people.
* The user could then ask the Prolog engine
* questions based on the initial data set, and the Prolog
* engine would then be able to provide the correct answer
* The questions where "Who is the child of X?",
* "Who is the grandparent of X?", "Who is the sibling of X?",
* and "Who is the inlaw of X?"
*
* GNU Copyright information
* Copyright 2011 Jamie Taylor <jamie@taylorj.org.uk>
*
* This program is free software; you can redistribute
* it and/or modify it under the terms of the GNU General
* Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* You should have received a copy of the GNU General
* Public License along with this program; if not, write
* to the Free Software Foundation, Inc., 51 Franklin
* Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
parent(frank,george). %the parent of frank is george
parent(helen,frank). %the parent of helen is frank
parent(amy,frank). %the parent of amy is frank
parent(mick,frank). %the parent of mickis frank
parent(emma,helen). %the parent of emma is helen
parent(susan,helen). %the parent of susan is helen
parent(john,amy). %the parent of john is amy
parent(paul,amy). %the parent of paul is amy
parent(ringo,amy). %the parent of ringo is amy
parent(steven,mick). %the parent of steven is mick
parent(charlie,mick). %the parent of charlie is mick
parent(bob,mick). %the parent of bob is mick
parent(beth,mick). %the parent of beth is mick
parent(kylie,emma). %the parent of kylie is emma
partner(george,mildred). %george's partner is mildred
partner(frank,betty). %frank's partner is betty
partner(helen,geoffrey). %helen's partner is geoffrey
partner(amy,richard). %amy's partner is richard
partner(mick,sue). %mick's partner is sue
partner(emma, jason). %emma's partner is jason
/* The child of method takes two arguments: X and Y, and
* seeks to reason whether Y is the child of X. It will
* then return whether Y is the child of X
*/
child_of(Y, X):- % Y is a child of X if
parent(Y, X). % X is a parent of Y
/* The grand parent methos takes two arguments: X and Z,
* and seeks to reason whether Z is the grandchild of X.
* It does so by recursively calling the parent method
* to see whether X is the parent of Y, and whether Y is
* the parent of Z. If so, then Z is the grandchild of X
*/
grandparent_of(X,Z):- % X is a grandparent of Z if
parent(X,Y), % X is the parent of Y and
parent(Y,Z). % Y is the parent of Z
/* The sibling method takes two arguments: X and Y, and
* seeks to reason whether they are siblings.
* The simplest way of doing this is to check whether
* they share any parents.
*/
sibling_of(X,Y):- % X is a sibling of Y if
parent(X,Z), % Z is X's parent and
parent(Y,Z). % z is also Y's parent
/* The inlaw method takes two arguments: A and X, and seeks
* to reason whether they are inlaws by checking who the
* partner of A is and whether on of said partner's parents
* are X.
*/
inlaw(A, X):- % X is an inlaw of A if
partner(B, A), % A had a partner B and
parent(B, X), % B had a parent X.
write(X),
write(' is an inlaw of '),
write(A).
/* The inlaw method takes two arguments: A and X, and seeks
* to reason whether they are inlaws by checking who the
* partner of A is and whether on of said partner's siblings
* are X.
*/
inlaw(A, X):- % X is an inlaw of A if
partner(B, A), % A had a partner B and
sibling_of(B, X), % B had a sibling X.
write(X),
write(' is an inlaw of '),
write(A).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment