itec 380 organization of programming languages lecture 10 – prolog

17
ITEC 380 Organization of programming languages Lecture 10 – Prolog

Upload: marcus-obrien

Post on 26-Dec-2015

226 views

Category:

Documents


7 download

TRANSCRIPT

Page 1: ITEC 380 Organization of programming languages Lecture 10 – Prolog

ITEC 380

Organization of programming languages

Lecture 10 – Prolog

Page 2: 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

Page 3: ITEC 380 Organization of programming languages Lecture 10 – Prolog

Prolog

Objectives

• Review Prolog• Look at GUIs with Prolog

Page 4: ITEC 380 Organization of programming languages Lecture 10 – 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?

Page 5: ITEC 380 Organization of programming languages Lecture 10 – Prolog

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?

Page 6: ITEC 380 Organization of programming languages Lecture 10 – Prolog

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).

Page 7: ITEC 380 Organization of programming languages Lecture 10 – Prolog

Prolog

XPCE

• A system for creating GUIs using prolog

• Demo of capabilities– Type manpce in your prolog interpreter

Page 8: ITEC 380 Organization of programming languages Lecture 10 – Prolog

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).

Page 9: ITEC 380 Organization of programming languages Lecture 10 – Prolog

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

Page 10: ITEC 380 Organization of programming languages Lecture 10 – Prolog

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))) ]),

Page 11: ITEC 380 Organization of programming languages Lecture 10 – Prolog

Prolog

Continuing on

send_list(S, append, [male, female]), send_list(D, append,

[research, development, marketing]), send(Dialog, default_button, enter), send(Dialog, open).

Page 12: ITEC 380 Organization of programming languages Lecture 10 – Prolog

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.

Page 13: ITEC 380 Organization of programming languages Lecture 10 – Prolog

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)))).

Page 14: ITEC 380 Organization of programming languages Lecture 10 – Prolog

Prolog

Display

• Can get the information from the GUI– get(@demo, display, D). %Display var– get(@display, size, Size)– get(Size, width, W)

Page 15: ITEC 380 Organization of programming languages Lecture 10 – Prolog

Prolog

Process

• Find basic component idea• Find out arguments• Figure out when to call new, send,

get • Build it piece by piece

Page 16: ITEC 380 Organization of programming languages Lecture 10 – Prolog

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)?

Page 17: ITEC 380 Organization of programming languages Lecture 10 – Prolog

Prolog

Next week

• C#