prolog - input and output

5
Prolog - Input and output Prolog uses the write predicate to present output to the user. Prolog uses the read predicate to read Terms from standard input. Standard involve must be followed by a period. LPA Prolog has the ability to create and manipulate dialog boxes. test :- write('Enter an atom'), nl, read(Atom), atom(Atom), assertz(Atom), call(Atom), listing. NOTE: Some new built in predicates introduced here.

Upload: eagan-kaufman

Post on 31-Dec-2015

14 views

Category:

Documents


0 download

DESCRIPTION

Prolog uses the write predicate to present output to the user. Prolog uses the read predicate to read Terms from standard input. Standard involve must be followed by a period. LPA Prolog has the ability to create and manipulate dialog boxes. test :- write('Enter an atom'), nl, - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Prolog - Input and output

Prolog - Input and output

• Prolog uses the write predicate to present output to the user.

• Prolog uses the read predicate to read Terms from standard input. Standard involve must be followed by a period.

• LPA Prolog has the ability to create and manipulate dialog boxes.

test :-

write('Enter an atom'),

nl,

read(Atom),

atom(Atom),

assertz(Atom),

call(Atom),

listing.

NOTE: Some new built in predicates are introduced here.

Page 2: Prolog - Input and output

Listing the contents

• You can use the listing predicate to list all dynamic predicates loaded.

• Alternately you can use a combination of write and the fail predicate.

• See capitals.pl

capital_of(georgia,atlanta).

capital_of(california,sacramento).

capital_of(florida,tallahassee).

capital_of(maine,augusta).

print_capitals :-

capital_of(State,City),

write(City), write(‘ is the capital of),

write(State), nl,

fail.

Page 3: Prolog - Input and output

Loading Prolog terms

• To load clauses from the listing file into the Prolog interpreter use consult or reconsult

• ensure_loaded can be used to automatically load files when a buffer is being consulted.

• You can use the compiler directive :- in front of any of these commands and they will be carried out as a buffer is being re-consulted.

Page 4: Prolog - Input and output

File handling

• This see predicate is used to stream input from a file.

• After the see predicate all reads refer to the input stream from the file.

• seen is used to close input stream.

• That tell predicate is used to redirect output to a file.

• All subsequent write statements are redirected to the file named in tell predicate.

• told closes the output stream.

You can see these features in learner.pl used with kb.pl

Page 5: Prolog - Input and output

A sample Expert system

• See car.pl• Basic Components

– Inference Engine (Prolog’s built in backward chaining).

– Knowledge base defect_may_be/1.

– Knowledge acquisition mechanism ask_questions/1.

– Recording what the user answers stored_answer/2.

– An explanation facility explain/1.