Organization of programming languages Lecture 10 Prolog Review Homework 2 due on F Semester project will be out either F or M C picked as OO Language Lists Recursion Objectives Review Prolog ID: 165683
Download Presentation The PPT/PDF document "ITEC 380" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
ITEC 380
Organization of programming languages
Lecture
10
– PrologSlide2
Review
Homework 2 due on F
Semester project will be out either F or M
C# picked as OO Language
Lists
RecursionSlide3
Objectives
Review Prolog
Look at GUIs with PrologSlide4
Exercise
How do you create a set of facts/rules in Prolog that can read in five numbers and store them a stack of numbers?
How would you reverse the contents of the list?
How would you pop off the top 2 and add the result to the top of the stack?Slide5
Review
How would you represent that Bill has five dollars and Ted has two in Prolog?
How would your represent a purchase of an item that costs three dollars?
How would you add two numbers and print out if the result is greater than 5?
How would you change the previous example to allow user input?Slide6
Methods of reversing
Accumulator versus appending
How do we tell which one is better?
naiverev
([],[]).
naiverev
([H|T],R):-
naiverev
(
T,RevT
), append(
RevT
,[H],R)
.
Versus
accRev
([H|T],A,R):-
accRev
(T,[H|A],R).
accRev
([],A,A).
rev
(L,R):-
accRev
(L,[],R). Slide7
XPCE
A system for creating GUIs using prolog
Demo of capabilities
Type
manpce
in your prolog interpreterSlide8
Basics
Four predicates for controlling GUIs
New, send, get free
Java GUI components comparison
Example of creating a GUI with prolog
n
ew(@demo, dialog(“Demo Window”)).
send(@demo, open).Slide9
Components
To add to the window you use send
s
end(@demo, append(
text_item
(‘Hello’)).
Capabilities
button (name,
RuleToCall
).
int_item
%Integer with bump up/down
slider %Numerical value in a range
menu %Radio button, tick-box, combo-box
label %Images / Text
list_browser
%View a list of data
editor %Allow editing of dataSlide10
Example program
ask_employee
:-
new(Dialog, dialog('Define employee')),
send_list
(Dialog, append,
[
new(N1,
text_item
(
first_name
)),
new
(N2,
text_item
(
family_name
)),
new
(S, new(S, menu(sex))),
new
(A,
int_item
(age, low := 18, high := 65)),
new
(D, menu(department, cycle)),
button
(cancel, message(Dialog, destroy)),
button
(enter, and(message(@prolog,
assert_employee
,
N1?selection,
N2?selection,
S?selection
,
A?selection
,
D?selection
),
message(Dialog, destroy)))
]),
Slide11
Continuing on
send_list
(S, append, [male, female]),
send_list
(D, append,
[
research, development, marketing]),
send(Dialog,
default_button
, enter),
send(Dialog, open).Slide12
Example 2
Get a name
ask_name
(Name) :-
new(D, dialog('Prompting for name')),
send(D, append
, new
(TI,
text_item
(name, ’’))),
send(D, append
, button
(ok, message(D, return
,
TI
?selection
))),
send(D, append,
button(cancel, message(D, return, @nil))),
send(D,
default_button
, ok), % Ok: default button
get(D, confirm, Answer), % This blocks!
send(D, destroy),
Answer \== @nil, % canceled
Name = Answer.Slide13
Shapes
Can get creative
send(@p, display,
new(@
bo
, box(100,100))).
send
(@p, display
, new
(@ci, circle(50)), point(25,25)).
send
(@p, display
, new
(@
tx
, text(’Hello’)), point(120, 50)).
send
(@p,
display
,new
(@
bz
,
bezier_curve
(point(50,100),
point
(120,132),
point
(50, 160),
point
(120, 200)))).Slide14
Display
Can get the information from the GUI
get(@demo, display, D)
. %Display
var
get(@display, size, Size)
get(Size, width, W)Slide15
Process
Find basic component idea
Find out arguments
Figure out when to call new, send, get
Build it piece by pieceSlide16
GUIs
What is your opinion of Prolog’s GUI implementation?
What are it’s strengths?
What are it’s weaknesses?
If you wanted to do something other than this GUI with prolog, what would you do (hint, covered previously)?Slide17
Next week
C#