Logtalk 2.17.1 files.
git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@1071 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
=================================================================
|
||||
Logtalk - Object oriented extension to Prolog
|
||||
Release 2.17.0
|
||||
Release 2.17.1
|
||||
|
||||
Copyright (c) 1998-2004 Paulo Moura. All Rights Reserved.
|
||||
=================================================================
|
||||
@@ -9,5 +9,33 @@ Copyright (c) 1998-2004 Paulo Moura. All Rights Reserved.
|
||||
To load all entities in this example consult the dcgs.loader
|
||||
utility file (note that this is a Prolog file).
|
||||
|
||||
This folder contains examples of using DCGs inside objects and
|
||||
categories.
|
||||
This folder contains the following examples of using DCGs inside
|
||||
objects and categories:
|
||||
|
||||
calculator
|
||||
canonical DCG example of parsing arithmetic expressions
|
||||
bom
|
||||
bill of materials DCG example (see below for original source)
|
||||
sentence
|
||||
simple parsing of natural language sentences
|
||||
parsetree
|
||||
same as above but building and returning the parse tree
|
||||
xml
|
||||
conversion between XML and Prolog terms
|
||||
url
|
||||
parsing of URLs, decomposing them in components
|
||||
shell
|
||||
parsing of coomand-line shell commands
|
||||
faa
|
||||
command language DCG example (see below for original source)
|
||||
walker
|
||||
parsing of walker movements and calculation of distance
|
||||
travelled
|
||||
|
||||
This folder contains two examples of DCGs (bom and faa) adopted with
|
||||
permission from the Amzi! Prolog documentation. The documentation is
|
||||
available on-line in HTML format at the URL:
|
||||
|
||||
http://www.amzi.com/
|
||||
|
||||
Please refer to the book for more information on the original examples.
|
||||
|
@@ -1,6 +1,6 @@
|
||||
=================================================================
|
||||
Logtalk - Object oriented extension to Prolog
|
||||
Release 2.17.0
|
||||
Release 2.17.1
|
||||
|
||||
Copyright (c) 1998-2004 Paulo Moura. All Rights Reserved.
|
||||
=================================================================
|
||||
@@ -14,7 +14,7 @@ Result = -9
|
||||
yes
|
||||
|
||||
|
||||
% recognizing gramatically correct sentences
|
||||
% recognizing gramatically correct sentences:
|
||||
|
||||
| ?- sentence::parse([the, girl, likes, the, boy], Result).
|
||||
|
||||
@@ -27,7 +27,7 @@ Result = false
|
||||
yes
|
||||
|
||||
|
||||
% generating parse trees for sentences
|
||||
% generating parse trees for sentences:
|
||||
|
||||
| ?- parsetree::parse([the, girl, likes, the, boy], Tree).
|
||||
|
||||
@@ -35,7 +35,38 @@ Tree = Tree = s(np(d(the), n(girl)), vp(v(likes), np(d(the), n(boy))))
|
||||
yes
|
||||
|
||||
|
||||
% conversion between compound terms and XML
|
||||
% bill of materials example:
|
||||
|
||||
| ?- bom::parts(bike, L).
|
||||
|
||||
L = [frame, crank, pedal, pedal, chain, spokes, rim, hub, spokes, rim, hub]
|
||||
yes
|
||||
|
||||
| ?- bom::parts(wheel, L).
|
||||
|
||||
L = [spokes, rim, hub]
|
||||
yes
|
||||
|
||||
|
||||
% parsing command-line shell input:
|
||||
|
||||
| ?- shell::parse("pwd; cd ..; ls -a", L).
|
||||
|
||||
L = [pwd,'cd ..','ls -a'] ?
|
||||
|
||||
yes
|
||||
|
||||
|
||||
% walker movements:
|
||||
|
||||
| ?- walker::walk([n(5), e(4), s(2), nw(8), s(5), se(1), n(4)], Ending).
|
||||
|
||||
Ending = -0.94974746830583223,6.9497474683058318 ?
|
||||
|
||||
yes
|
||||
|
||||
|
||||
% conversion between compound terms and XML:
|
||||
|
||||
| ?- xml::convert(word(child, children), word(singular, plural), XML).
|
||||
|
||||
@@ -49,7 +80,7 @@ Interpretation = word(singular, plural)
|
||||
yes
|
||||
|
||||
|
||||
% parsing URLs
|
||||
% parsing URLs:
|
||||
|
||||
| ?- url::parse("http://www.logtalk.org", Components).
|
||||
|
||||
@@ -75,3 +106,20 @@ yes
|
||||
|
||||
Components = [protocol(http), address([193, 136, 64, 5]), path([files, update]), file('')]
|
||||
yes
|
||||
|
||||
|
||||
% command language example:
|
||||
|
||||
?- faa::main.
|
||||
Fly Amzi! Air
|
||||
enter command> list flights
|
||||
aa101
|
||||
aa102
|
||||
aa103
|
||||
enter command> book elana aa102
|
||||
enter command> book tom aa102
|
||||
enter command> list passengers aa102
|
||||
elana
|
||||
tom
|
||||
enter command> exit
|
||||
yes
|
||||
|
33
Logtalk/examples/dcgs/bom.lgt
Normal file
33
Logtalk/examples/dcgs/bom.lgt
Normal file
@@ -0,0 +1,33 @@
|
||||
|
||||
:- object(bom).
|
||||
|
||||
:- info([
|
||||
version is 1.0,
|
||||
date is 2004/5/11,
|
||||
author is 'Paulo Moura',
|
||||
comment is 'Adaptation of the bill of materials DCG example from the Amzi! Prolog manual.']).
|
||||
|
||||
:- public(parts/2).
|
||||
:- mode(parts(+atom, -list), one).
|
||||
:- info(parts/2, [
|
||||
comment is 'Returns the list of parts for building an object.',
|
||||
argnames is ['Object', 'Parts']]).
|
||||
|
||||
parts(Object, Parts) :-
|
||||
phrase(Object, Parts).
|
||||
|
||||
bike --> frame, drivechain, wheel, wheel.
|
||||
|
||||
wheel --> spokes, rim, hub.
|
||||
|
||||
drivechain --> crank, pedal, pedal, chain.
|
||||
|
||||
spokes --> [spokes].
|
||||
crank --> [crank].
|
||||
pedal --> [pedal].
|
||||
chain --> [chain].
|
||||
rim --> [rim].
|
||||
hub --> [hub].
|
||||
frame --> [frame].
|
||||
|
||||
:- end_object.
|
@@ -9,4 +9,8 @@
|
||||
verbs,
|
||||
sentence,
|
||||
url,
|
||||
xml])).
|
||||
xml,
|
||||
shell,
|
||||
walker,
|
||||
bom,
|
||||
faa])).
|
||||
|
94
Logtalk/examples/dcgs/faa.lgt
Normal file
94
Logtalk/examples/dcgs/faa.lgt
Normal file
@@ -0,0 +1,94 @@
|
||||
|
||||
:- object(faa).
|
||||
|
||||
:- info([
|
||||
version is 1.0,
|
||||
date is 2004/5/10,
|
||||
author is 'Paulo Moura',
|
||||
comment is 'Adaptation of the command language DCG example from the Amzi! Prolog manual.']).
|
||||
|
||||
:- public(main/0).
|
||||
:- mode(main, one).
|
||||
:- info(main/0, [
|
||||
comment is 'Starts iteractive command language interpreter.']).
|
||||
|
||||
:- private(booked/2).
|
||||
:- dynamic(booked/2).
|
||||
:- mode(booked(?atom, ?atom), zero_or_more).
|
||||
:- info(booked/2, [
|
||||
comment is 'Booked places in flight.',
|
||||
argnames is ['Passenger', 'Flight']]).
|
||||
|
||||
main :-
|
||||
write('Fly Amzi! Air'), nl,
|
||||
repeat,
|
||||
do_command(Command),
|
||||
Command == exit.
|
||||
|
||||
do_command(Command) :-
|
||||
write('enter command> '),
|
||||
read_tokens(Tokens),
|
||||
phrase(command(List), Tokens),
|
||||
Command =.. List,
|
||||
call(Command),
|
||||
!.
|
||||
|
||||
read_tokens(Tokens) :-
|
||||
read_codes(Codes),
|
||||
codes_to_tokens(Codes, Tokens).
|
||||
|
||||
read_codes(Codes) :-
|
||||
get_code(Code),
|
||||
read_codes(Code, Codes).
|
||||
|
||||
read_codes(10, [[]]) :-
|
||||
!.
|
||||
read_codes(13, [[]]) :-
|
||||
!.
|
||||
read_codes(32, [[]| Rest]) :-
|
||||
!, read_codes(Rest).
|
||||
read_codes(Code, [[Code| Codes]| Rest]) :-
|
||||
read_codes([Codes| Rest]).
|
||||
|
||||
codes_to_tokens([], []).
|
||||
codes_to_tokens([List| Lists], [Token| Tokens]) :-
|
||||
atom_codes(Token, List),
|
||||
codes_to_tokens(Lists, Tokens).
|
||||
|
||||
command([Op| Args]) --> operation(Op), arguments(Args).
|
||||
|
||||
arguments([Arg| Args]) --> argument(Arg), arguments(Args).
|
||||
arguments([]) --> [].
|
||||
|
||||
operation(report) --> [list].
|
||||
operation(book) --> [book].
|
||||
operation(exit) --> ([exit]; [quit]; [bye]).
|
||||
|
||||
argument(passengers) --> [passengers].
|
||||
argument(flights) --> [flights].
|
||||
|
||||
argument(Flight) --> [Flight], {flight(Flight)}.
|
||||
argument(Passenger) --> [Passenger].
|
||||
|
||||
flight(aa101).
|
||||
flight(aa102).
|
||||
flight(aa103).
|
||||
|
||||
report(flights) :-
|
||||
flight(Flight),
|
||||
write(Flight), nl,
|
||||
fail.
|
||||
report(_).
|
||||
|
||||
report(passengers, Flight) :-
|
||||
booked(Passenger, Flight),
|
||||
write(Passenger), nl,
|
||||
fail.
|
||||
report(_, _).
|
||||
|
||||
book(Passenger, Flight) :-
|
||||
assertz(booked(Passenger, Flight)).
|
||||
|
||||
exit.
|
||||
|
||||
:- end_object.
|
54
Logtalk/examples/dcgs/shell.lgt
Normal file
54
Logtalk/examples/dcgs/shell.lgt
Normal file
@@ -0,0 +1,54 @@
|
||||
|
||||
:- object(shell).
|
||||
|
||||
|
||||
:- info([
|
||||
version is 1.0,
|
||||
date is 2004/4/29,
|
||||
author is 'Paulo Moura',
|
||||
comment is 'Simple example of command-line shell parsing.']).
|
||||
|
||||
|
||||
:- public(parse/2).
|
||||
|
||||
:- mode(parse(@list, -list), zero_or_one).
|
||||
|
||||
:- info(parse/2, [
|
||||
comment is 'Parses a sequence of commands.',
|
||||
argnames is ['Sequence', 'Commands']]).
|
||||
|
||||
|
||||
parse(Sequence, Commands) :-
|
||||
phrase(commands(Commands), Sequence).
|
||||
|
||||
|
||||
commands([C| Cs]) -->
|
||||
command(C), separator, commands(Cs).
|
||||
commands([C]) -->
|
||||
command(C).
|
||||
|
||||
separator --> ";".
|
||||
|
||||
whitespace --> " ", whitespace.
|
||||
whitespace --> [].
|
||||
|
||||
command(Cd) -->
|
||||
whitespace, "cd", whitespace, cdargs(Args), whitespace,
|
||||
{atom_concat(cd, Args, Cd)}.
|
||||
command(Ls) -->
|
||||
whitespace, "ls", whitespace, lsargs(Args), whitespace,
|
||||
{atom_concat(ls, Args, Ls)}.
|
||||
command(pwd) -->
|
||||
whitespace, "pwd", whitespace.
|
||||
|
||||
cdargs(' ~') --> "~".
|
||||
cdargs(' ..') --> "..".
|
||||
cdargs(' .') --> ".".
|
||||
cdargs('') --> [].
|
||||
|
||||
lsargs(' -l') --> "-l".
|
||||
lsargs(' -a') --> "-a".
|
||||
lsargs('') --> [].
|
||||
|
||||
|
||||
:- end_object.
|
43
Logtalk/examples/dcgs/walker.lgt
Normal file
43
Logtalk/examples/dcgs/walker.lgt
Normal file
@@ -0,0 +1,43 @@
|
||||
|
||||
:- object(walker).
|
||||
|
||||
|
||||
:- info([
|
||||
version is 1.0,
|
||||
date is 2004/4/29,
|
||||
author is 'Paulo Moura',
|
||||
comment is 'Walker movements.']).
|
||||
|
||||
|
||||
:- public(walk/2).
|
||||
|
||||
:- mode(walk(@list, -position), one).
|
||||
|
||||
:- info(walk/2, [
|
||||
comment is 'Parses a sequence of walker moves, returning ending position.',
|
||||
argnames is ['Moves', 'Ending']]).
|
||||
|
||||
|
||||
walk(Moves, Ending) :-
|
||||
phrase(walk(Ending), Moves).
|
||||
|
||||
|
||||
walk(Ending) -->
|
||||
moves((0, 0), Ending).
|
||||
|
||||
moves(Start, Ending) -->
|
||||
move(Start, Temp), moves(Temp, Ending).
|
||||
moves(Ending, Ending) -->
|
||||
[].
|
||||
|
||||
move((X0, Y0), (X, Y)) --> [ n(S)], {X is X0, Y is Y0 + S}.
|
||||
move((X0, Y0), (X, Y)) --> [ne(S)], {X is X0 + S / sqrt(2), Y is Y0 + S / sqrt(2)}.
|
||||
move((X0, Y0), (X, Y)) --> [ e(S)], {X is X0 + S, Y = Y0}.
|
||||
move((X0, Y0), (X, Y)) --> [se(S)], {X is X0 + S / sqrt(2), Y is Y0 - S / sqrt(2)}.
|
||||
move((X0, Y0), (X, Y)) --> [ s(S)], {X is X0, Y is Y0 - S}.
|
||||
move((X0, Y0), (X, Y)) --> [sw(S)], {X is X0 - S / sqrt(2), Y is Y0 - S / sqrt(2)}.
|
||||
move((X0, Y0), (X, Y)) --> [ w(S)], {X is X0 - S, Y = Y0}.
|
||||
move((X0, Y0), (X, Y)) --> [nw(S)], {X is X0 - S / sqrt(2), Y is Y0 + S / sqrt(2)}.
|
||||
|
||||
|
||||
:- end_object.
|
Reference in New Issue
Block a user