Skip to content

Commit

Permalink
Initial source checkin
Browse files Browse the repository at this point in the history
  • Loading branch information
Anniepoo committed Oct 18, 2012
1 parent 081f64c commit 778776c
Show file tree
Hide file tree
Showing 20 changed files with 1,754 additions and 1 deletion.
72 changes: 71 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,74 @@
prolog-examples
===============

Some simple examples for new Prolog programmers
Some simple examples for new Prolog programmers.
Varying degrees of polish. Have fun.

## finished examples

### detective problem

These are two approaches to solving a detective's problem.
She's interrogated three witnesses to a murder, all of whom are also suspects.
The problem is to determine which of the suspect's testimony is
inconsistent with the others.

* detectivepuzzle.pl
* adriandetective.pl

### talespin2.pl

Implementation of a classic story generator

### tictactoe.pl

Implementation of tic tac toe


### birds.pl

The birds example from the 'expert systems in Prolog' tutorial
by Amzi (on the web)

### cannibals2nocomments.pl

A well commented example of the cannibals/missionaries problem

This was used as an example for a talk whose notes are in may15version.txt

### emoticons.pl

A joke. I'm known for my happy array of 'smiley' emoticons. Sometimes others
have trouble understanding my smileys. This program purports to help.

### familytree.pl

The classic familytree example. This one has two separate families in it.
(left as exercise for the reader to find the two disconnected groups).

### socketdemo.pl

Demonstration of reading from a socket

## Constraint examples

Examples of CLPFD

### addlists.pl

Constrains two lists to pairwise add to a third.

### children.pl

A fully worked problem involving seating children in a classroom

### constraintolist.pl

A fully worked problem, constraining a variable to be a member of a list.

### sudoku.pl

Solves a Sudoku puzzle by CLPFD



9 changes: 9 additions & 0 deletions addlists.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
:- use_module(library(clpfd)).

addlists([], [], []).

addlists([HA|TA],[HB|TB],[HC|TC]):-
HC #= HA+HB,
addlists(TA,TB,TC).


23 changes: 23 additions & 0 deletions adriandetective.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

murderer(X) :- bagof(Y,contradictsPerson(X,Y),Ys), length(Ys,N), N > 1.

contradictsPerson(A,B) :-
claims(A,AClaims), claims(B,BClaims), contradictsClaims(AClaims,BClaims).

contradictsClaims(Claims,[H|T]) :-
contradictsClaims1(Claims,H); contradictsClaims(Claims,T).
contradictsClaims1([H|T],C) :-
contradictsClaim(H,C); contradictsClaims1(T,C).
contradictsClaim([Claim1,Who],[Claim2,Who]) :-
contradictory(Claim1,Claim2).

claims(art,[[innocent,art],[knewVic,burt],[knewVic,carl]]).
claims(burt,[[outOfTown,burt],[didNotKnowVic,burt]]).
claims(carl,[[innocent,carl],[inTown,burt],[inTown,carl]]).

contradictory(innocent,murderer).
contradictory(murderer,innocent).
contradictory(outOfTown,inTown).
contradictory(inTown,outOfTown).
contradictory(knewVic,didNotKnowVic).
contradictory(didNotKnowVic,knewVic).
156 changes: 156 additions & 0 deletions birds.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
:- module(birds, [solve/0]).

:- dynamic
known/3,
voice/1,
season/1,
cheek/1,
head/1,
flight/1,
bill/1,
live/1,
nostrils/1.

:- discontiguous bird/1, wings/1.
:- set_prolog_flag(unknown, error).

bird(laysan_albatross):-
family(albatross),
color(white).

bird(black_footed_albatross):-
family(albatross),
color(dark).

bird(whistling_swan) :-
family(swan),
voice(muffled_musical_whistle).

bird(trumpeter_swan) :-
family(swan),
voice(loud_trumpeting).


order(tubenose) :-
nostrils(external_tubular),
live(at_sea),
bill(hooked).

order(waterfowl) :-
feet(webbed),
bill(flat).

family(albatross) :-
order(tubenose),
size(large),
wings(long_narrow).

family(swan) :-
order(waterfowl),
neck(long),
color(white),
flight(ponderous).

bird(canada_goose):-
family(goose),
season(winter),
country(united_states),
head(black),
cheek(white).

bird(canada_goose):-
family(goose),
season(summer),
country(canada),
head(black),
cheek(white).

country(united_states):- region(mid_west).

country(united_states):- region(south_west).

country(united_states):- region(north_west).

country(united_states):- region(mid_atlantic).

country(canada):- province(ontario).

country(canada):- province(quebec).

region(new_england):-
state(X),
member(X, [massachusetts, vermont]).

region(south_east):-
state(X),
member(X, [florida, mississippi]).

state(X) :- member(X, [florida, mississippi, massachusetts, vermont]).

province(X) :- member(X, [ontario, quebec]).

ask(A, V):-
known(yes, A, V), % succeed if true
!. % stop looking

ask(A, V):-
known(_, A, V), % fail if false
!, fail.

% known is barfing
ask(A, V):-
write(A:V), % ask user
write('? : '),
read(Y), % get the answer
asserta(known(Y, A, V)), % remember it
Y == yes. % succeed or fail
ask(A, V):-
\+ multivalued(A),
known(yes, A, V2),
V \== V2,
!, fail.

eats(X):- ask(eats, X).

feet(X):- ask(feet, X).

wings(X):- ask(wings, X).

neck(X):- ask(neck, X).

color(X):- ask(color, X).

multivalued(voice).
multivalued(feed).

size(X):- menuask(size, X, [large, plump, medium, small]).

flight(X):- menuask(flight, X, [ponderous, agile, flap_glide]).



menuask(A, V, MenuList) :-
write('What is the value for'), write(A), write('?'), nl,
write(MenuList), nl,
read(X),
check_val(X, A, V, MenuList),
asserta( known(yes, A, X) ),
X == V.

check_val(X, _A, _V, MenuList) :-
member(X, MenuList), !.

check_val(X, A, V, MenuList) :-
write(X), write(' is not a legal value, try again.'), nl,
menuask(A, V, MenuList).

top_goal(X) :- bird(X).

solve :-
retractall(known/3),
top_goal(X),
write('The answer is '), write(X), nl.

solve :-
write('No answer found.'), nl.

Loading

2 comments on commit 778776c

@kailash95bisht
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how to run this code on prolog??

@Anniepoo
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For most of the programs there's a starter predicate.
For example, the tictactoe game can be run by doing this at the bash prompt, assuming you're in the directory with the code.

swipl -s tictactoe.pl
... bunch of stuff printed....
?- tictactoe. <-- type tictactoe and a period at the ?- prompt

Prolog works by proving a query - in this case 'is tictactoe true?'
Everything else that happens is a side effect.

It stops for input - your moves. You type 1 thru 9, selecting an empty space

Please sign in to comment.