itec 380 organization of programming languages lecture 10 – prolog
TRANSCRIPT
ITEC 380
Organization of programming languages
Lecture 10 – Prolog
Prolog
Review
• Homework 2 due on F• Semester project will be out either F
or M• C# picked as OO Language• Lists• Recursion
Prolog
Objectives
• Review Prolog• Look at GUIs with Prolog
Prolog
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?
Prolog
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?
Prolog
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).
Prolog
XPCE
• A system for creating GUIs using prolog
• Demo of capabilities– Type manpce in your prolog interpreter
Prolog
Basics
• Four predicates for controlling GUIs– New, send, get free– Java GUI components comparison
• Example of creating a GUI with prolog
new(@demo, dialog(“Demo Window”)).send(@demo, open).
Prolog
Components
• To add to the window you use send– send(@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 data
Prolog
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))) ]),
Prolog
Continuing on
send_list(S, append, [male, female]), send_list(D, append,
[research, development, marketing]), send(Dialog, default_button, enter), send(Dialog, open).
Prolog
Example 2
• Get a nameask_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 buttonget(D, confirm, Answer), % This blocks!send(D, destroy),Answer \== @nil, % canceledName = Answer.
Prolog
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)))).
Prolog
Display
• Can get the information from the GUI– get(@demo, display, D). %Display var– get(@display, size, Size)– get(Size, width, W)
Prolog
Process
• Find basic component idea• Find out arguments• Figure out when to call new, send,
get • Build it piece by piece
Prolog
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)?
Prolog
Next week
• C#