# xpce en welgefundeerde piter/kr/  · piter dykstra 13 december 2009 onderwerpen •hexamy’s

Post on 15-Oct-2018

212 views

Category:

## Documents

Embed Size (px)

TRANSCRIPT

• Kennisrepresentatie & Redeneren

Piter Dykstra

Instituut voor Informatica en Cognitie

www.math.rug.nl/~piter

p.dykstra@rug.nl

13 december 2009

XPCE en WELGEFUNDEERDE SEMANTIEK

Kennisrepresentatie & Redeneren Week6: Grammaticas

• Piter Dykstra 13 december 2009

Onderwerpen

Hexamys met Constraints

XPCE

Terugkoppeling werkcollege(Met dank aan Wouter Lueks)

Welgefundeermde Semantiek

Contextvrije grammaticas

Van Wijngaarden-grammaticas

Isomorfie met Logische programmas

Logische grammaticas

Definiete Clausule grammaticas

Vertaling naar Prolog

Kennisrepresentatie & Redeneren Week6: Grammaticas 1

• Piter Dykstra 13 december 2009

Hexamy met Constraints

:- module(hexamy,[hexamy/1,col/1,on/2,cnt/1,collineair/3,notcol/3,concurrence/5]).

:- dynamic cnt/1.:- use_module(library(chr)).

:- chr_constraint col/1.:- chr_constraint on/2.

c1 @ on(X,L), on(Y,L) | col([X,Y,Z]) on(Z,L).c2 @ on(X,L), on(Z,L) | col([X,Y,Z]) on(Y,L).c3 @ on(Y,L), on(Z,L) | col([X,Y,Z]) on(X,L).c4 @ col(Points) Points = [X,Y,Z],

on(X,L), on(Y,L), on(Z,L),newline(L).

idempotence @ on(X,Y) | on(X,Y) true.

Kennisrepresentatie & Redeneren Week6: Grammaticas 2

• Piter Dykstra 13 december 2009

newline(l(1)) :- not(cnt(_)), assert(cnt(1)).newline(l(X)) :- retract(cnt(X0)), X is X0 + 1, assert(cnt(X)).

:- chr_constraint collineair/3.col @ on(A,L), on(B,L), on(C,L) | collineair(A,B,C) true.

:- chr_constraint notcol/3.notcol1 @ on(A,L), on(B,L), on(C,L), notcol(A,B,C) fail.notcol2 @ notcol(_,_,_) true.

:-chr_constraint concurrence/5.conc1 @ on(A,D),on(B,D),

on(C,Dp),on(E,Dp),on(P,D),on(P,Dp) | concurrence(A,B,C,E,P) true.

:-chr_constraint hexamy/1.hex @ hexamy([A,B,C,D,E,F]) concurrence(A,B,D,E,P),

(notcol(A,B,D) ; notcol(A,B,E)).

Kennisrepresentatie & Redeneren Week6: Grammaticas 3

• Piter Dykstra 13 december 2009

Hexamy met Constraints

% chrtest compiled into hexamy 0.13 sec, 1,936 bytestrue.

?- col([a,b,c]),col([d,e,c]),concurrence(a,b,d,e,X),(X=b;X=c;X=a).on(c, l(12))on(e, l(12))on(d, l(12))on(c, l(11))on(b, l(11))on(a, l(11))concurrence(a, b, d, e, b)X = b ;on(c, l(12))on(e, l(12))on(d, l(12))on(c, l(11))on(b, l(11))on(a, l(11))X = c

Kennisrepresentatie & Redeneren Week6: Grammaticas 4

• Piter Dykstra 13 december 2009

Prolog-XPCE interface

Kennisrepresentatie & Redeneren Week6: Grammaticas 5

• Piter Dykstra 13 december 2009

XPCE-klassen

pce begin class (persoon, object, "Zomaar een klasse") variable (naam, char array, both, "de naam dus") variable (leeftijd, int, both) initialise (S,N : char array, L : [ int ]) default (L, 18, Leeftijd),send (S, send super, initialise),send (S, slot, naam, N),send (S, slot, leeftijd, Leeftijd) pce end class

Kennisrepresentatie & Redeneren Week6: Grammaticas 6

• Piter Dykstra 13 december 2009

Dialoog

ask employeenew (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, (message (@prolog,

assert employee,

N1 ? selection,

N2 ? selection,

S ? selection,

A ? selection,

D ? selection),

Kennisrepresentatie & Redeneren Week6: Grammaticas 7

• Piter Dykstra 13 december 2009

message (@prolog,nl)))]),

send list (S, append, [male, female ]),send list (D, append, [research, development,marketing ]),send (Dialog, default button, enter),send (Dialog, open)

assert employee (FirstName, FamilyName, Sex,Age,Dept)format (Adding ~w ~w ~w, age ~w, working at ~w,

[Sex, FirstName, FamilyName,Age,Dept ])

Kennisrepresentatie & Redeneren Week6: Grammaticas 8

• Piter Dykstra 13 december 2009

RationalAgent

Kennisrepresentatie & Redeneren Week6: Grammaticas 9

• Piter Dykstra 13 december 2009

Sudokugui

:- pce_begin_class(sudokugui, frame, "Een sudokugui").

initialise(M) :->"Create a sudoku window gui"::send(M, send_super,initialise,Sudoku),% Create the two primary partsnew(S,sudokupicture),new(B,dialog),% Add some buttons to the dialogsend(B, append, new(N, button(New,message(S,new)))),send(B, append, new(C, button(Check,message(S,check)))),send(B, append, new(H, button(Hint,message(S,hint)))),send(B, append, new(U, button(Undo,message(S,undo)))),send(C, below, N),....send(B, right, S),send(M, open).

:- pce_end_class.

start :- new(S,sudokugui).

Kennisrepresentatie & Redeneren Week6: Grammaticas 10

• Piter Dykstra 13 december 2009

Sudokubox

:- pce_begin_class(sudokubox, device, "Een sudokuvierkantje").

variable(value,int:=0,both,"De waarde van het vakje op dit moment").variable(sudoku,sudokupicture,both,"De sudokupicture").variable(col,int,both,"De kolom van het vakje").variable(row,int,both,"De rij van het vakje").

Kennisrepresentatie & Redeneren Week6: Grammaticas 11

• Piter Dykstra 13 december 2009

Sudokubox

send(S,recogniser,Click),send(S,recogniser,ClickDel),send(S,recogniser,MouseHOn),send(S,recogniser,MouseHOff),

send(S,recogniser,new(K,key_binding(@nil, argument))),forall(snumber(X),

send(S, send super, initialise),send(S, slot, sudoku, P),send(S, slot, col, C),send(S, slot, row, R),send(S, display,box(L,L)),send(S, display,text(T,center)),send(S, recenter).

Kennisrepresentatie & Redeneren Week6: Grammaticas 12

• Piter Dykstra 13 december 2009

Sudokubox

recenter(S) :->get(S, member, text, Txt),get(S, member, box, Box),send(Txt, center, Box?center).

remove_prolog_fact(S) :->get(S, col, C),get(S, row, R),( value(cell(C,R), )-> send(@prolog,retract(value(cell(C,R), ))); true).

notify(S,Vnew:int) :->get(S, sudoku, Not),get(S,slot,value,Vold),send(Not,updated(S,Vold,Vnew)).

Kennisrepresentatie & Redeneren Week6: Grammaticas 13

• Piter Dykstra 13 december 2009

Sudokubox

clear(S) :->writeln(Clear!),get(S, member, text, Txt),get(S, sudoku, Not),send(S,remove_prolog fact),send(Not,clear_hints),send(S,slot,value,0),send(Txt, string, ).

value(S,V:int) :->get(S, col, C),get(S, row, R),format(Cel cell(~d,~d): ~d~n,[C,R,V]),get(S, member, text, Txt),send(S,remove_prolog_fact),send(@prolog,assert(value(cell(C,R),V))),send(S,notify(V)),send(S,slot,value,V),send(Txt, string, V).

value(S,V) :

• Piter Dykstra 13 december 2009

Sudokubox

mouseOn(S) :->get(S, member, box, B),get(B, fill_pattern,Colour),( Colour = colour(blue) % test if selected-> send(B,fill_patter,colour(purple)); send(B,fill_pattern,colour(orange))).

mouseOff(S) :->get(S, member, box, B),get(B, fill_pattern,Colour),( (Colour = colour(purple);Colour=colour(blue)) % test if selected-> send(B,fill_patter,colour(blue)); send(B,fill_pattern,colour(white))).

clicked(S) :->get(S, col, C),get(S, row, R),get(S, member, box, B),send(B,fill_pattern,colour(blue)).

:- pce_end_class.

Kennisrepresentatie & Redeneren Week6: Grammaticas 15

• Piter Dykstra 13 december 2009

Sudokupicture

:- pce_begin_class(sudokupicture, picture, "Een sudokupicture").variable(boxs,chain:=new(chain),both,"de boxjes in de sudoku").variable(selection,sudokubox,both,"de geselecteerde sudokubox").variable(hints,chain:=new(chain),both,"set van gegeven hints").variable(hintvalue,int,both,"De waarde van de gehinte cel hint").variable(updates,chain:=new(chain),both,"a list of subsequent

actions, the first action is the newest").

Kennisrepresentatie & Redeneren Week6: Grammaticas 16

• Piter Dykstra 13 december 2009

Sudokupicture

initialise(S,W:[int],H:[int]) :->"Sudokupicture"::send(S, send_super, initialise,Sudoku!),send(S, slot, hintvalue, 0),length(L_),L is L_*L_,Bdt is (L+1)*25+11,send(S, send_super, size, size(Bdt,Bdt+20)),default(W,L,Width),default(H,L,Height),get(S,boxs,BS),forall(between(1,Width,C),

( forall(between(1,Height,R),(Vert is R*25,Hth is C*25,send(S,display,new(B,sudokubox(S,C,R)),point(Hth,Vert)),send(BS,append,B)))

)),send(S,display,new(T,text),point(25,Bdt+10)),send(T,colour,colour(red)),send(S,drawThickLines).

Kennisrepresentatie & Redeneren Week6: Grammaticas 17

• Piter Dykstra 13 december 2009

Sudokupicture

drawThickLines(S) :->length(L),LL is L*L,forall(between(0,L,Pf),(

P is (Pf*(L*25)+25),new(Lv,line(P,25,P,(LL+1)*25)),new(Lh,line(25,P,(LL+1)*25,P)),send_list([Lv,Lh],pen,3),send_list(S,display,[Lv,Lh]))).

draw_inconsistent(S) :->....

draw_known_square(S,X,Y) :->....

Kennisrepresentatie & Redeneren Week6: Grammaticas 18

• Piter Dykstra 13 december 2009

Sudokupicture

hint(S) :->get(S,hintvalue,VHint),( (VHint > 0) ->

send(S,hint_value(VHint));

send(S,clear_hints),( ( send(@prolog,inconsistent),

send(S,

Recommended