Skip to content

Instantly share code, notes, and snippets.

@feltnerm
Created November 18, 2014 16:05
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 feltnerm/74e8642dc00c273c52fc to your computer and use it in GitHub Desktop.
Save feltnerm/74e8642dc00c273c52fc to your computer and use it in GitHub Desktop.
Prolog Exercises
% Authors:
% Mark Feltner
% Aaron Decker
% 1.
%% Succeeds if we have a square.
square(SideA, SideA).
% 2.
%% Succeeds if Result is the sum of the values from 1 to N and fails
%% otherwise.
sumTo(0,0).
sumTo(N, Result) :- N > 0, P is N - 1, sumTo(P, NewResult), Result is N + NewResult.
% 3.
%% Succeeds if the list Stack has alternating colors
isRed(c(r, _)).
isBlack(c(b, _)).
alternating([]).
alternating([_]).
alternating([R,B|Tail]) :- isRed(R), isBlack(B), alternating([B|Tail]).
alternating([B,R|Tail]) :- isRed(R), isBlack(B), alternating([R|Tail]).
% 4.
%% Succeeds if a list of words is a valid command
command(X) :- member(X, [take, drop]).
article(X) :- member(X, [a, an, the]).
adjective(X) :- member(X, [red, green, blue, clear, empty, occupied]).
noun(X) :- member(X, [block, floor, table]).
valid([X]) :- noun(X).
valid([Head | Tail]) :- adjective(Head), valid(Tail).
valid([First, Second | Rest]) :- command(First), article(Second), valid(Rest).
% parsing a phrase-structured grammar
%
% The grammar is based on Computational Intelligence: A Logical Approach,
% by D. Poole, A. Mackworth, and R. Goebel, Oxford University Press, 1998.
%
% Tests:
% parse([the, red, block, is, on, a, blue, block], T), show_tree(T).
% parse([a, is, on, the, floor, and, b, is, clear], T), show_tree(T).
% parse([the, floor, is, below, a, or, it, is, occupied], T), show_tree(T).
%
parse(Sentence, Parse) :-
append(NP, VP, Sentence),
noun_phrase(NP, NP_Parse),
verb_phrase(VP, VP_Parse),
Parse = clause(NP_Parse, VP_Parse).
parse(Sentence, conjunct(Word, Parse1, Parse2)) :-
append(Part1, [Word | Part2], Sentence),
conjunct(Word),
parse(Part1, Parse1),
parse(Part2, Parse2).
conjunct(and).
conjunct(or).
noun_phrase([Word], noun(Word)) :- proper_noun(Word).
noun_phrase([Word], noun(Word)) :- pronoun(Word).
noun_phrase([a | Words], np(det(a), NP)) :- descriptive_np(Words, NP).
noun_phrase([the | Words], np(det(the), NP)) :- descriptive_np(Words, NP).
descriptive_np([Word], noun(Word)) :- noun(Word).
descriptive_np([Word | Rest], np(adj(Word), SubParse)) :-
adjective(Word), descriptive_np(Rest, SubParse).
proper_noun(a).
proper_noun(b).
proper_noun(c).
noun(block).
noun(floor).
noun(table).
pronoun(it).
pronoun(this).
adjective(red).
adjective(green).
adjective(blue).
adjective(clear).
adjective(empty).
adjective(occupied).
verb_phrase([is, Word], vp(verb(is), adj(Word))) :-
adjective(Word).
verb_phrase([is | Rest], vp(verb(is), PP_Parse)) :-
prep_phrase(Rest, PP_Parse).
prep_phrase([Word | Rest], pp(prep(Word), NP_Parse)) :-
preposition(Word),
noun_phrase(Rest, NP_Parse).
preposition(on).
preposition(above).
preposition(under).
preposition(below).
preposition(beside).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% show_tree(+Tree)
% Displays a tree as an indented structure
% [Written by Philip Brooks, 05/08/03, distributed as part of
% "SCP: A Simple Chunk Parser"
show_tree(Tree) :-
show_tree_x(Tree,0).
show_tree_x(Atom,N) :-
atomic(Atom),
!,
write_spaces(N),
write(Atom),
nl.
show_tree_x(Tree,N) :-
NewN is N + 3,
functor(Tree,Functor,Args),
show_tree_x(Functor,N),
between(1,Args,X),
arg(X,Tree,Item),
show_tree_x(Item,NewN),
fail.
show_tree_x(_,_).
% write_spaces(+Num)
% Writes Num spaces. Called by show_tree/1.
write_spaces(X) :-
X >= 0, % Sanity check
write_spaces_x(X).
write_spaces_x(0) :- !.
write_spaces_x(N) :-
write(' '),
NewN is N - 1,
write_spaces_x(NewN).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% binds N to all values between Low and High where Low <= High
between(Low, High, N) :-
mklist(Low, High, Numbers), member(N, Numbers).
mklist(Max, Max, [Max]).
mklist(Min, Max, [Min | Rest]) :- Min < Max, N is Min + 1, mklist(N, Max, Rest).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment