MetaProgramming t k prasadwright edu http www knoesis

  • Slides: 23
Download presentation
Meta-Programming t. k. prasad@wright. edu http: //www. knoesis. org/tkprasad/ cs 774 (Prasad) L 11

Meta-Programming t. k. prasad@wright. edu http: //www. knoesis. org/tkprasad/ cs 774 (Prasad) L 11 Meta. Pgm 1

Meta-Programs : Program manipulating Programs • Extend Prolog – introduce new search strategy or

Meta-Programs : Program manipulating Programs • Extend Prolog – introduce new search strategy or modify existing search strategy – add expressive logical connective – extend/modify syntax/semantics • Enable Debugger • Support Theorem Provers and Rule-based Systems cs 774 (Prasad) L 11 Meta. Pgm 2

Basic Meta-Interpreter prove( true ) : - !. prove( (G 1, G 2) )

Basic Meta-Interpreter prove( true ) : - !. prove( (G 1, G 2) ) : - !, prove(G 1), prove(G 2). prove( G ) : system(G), G. prove( G ) : clause(G, B), prove(B). • System-predicate holds of built-ins. cs 774 (Prasad) L 11 Meta. Pgm 3

Upgrading the Meta-Interpreter • Adding an or-operator prove( (G 1 ; G 2) )

Upgrading the Meta-Interpreter • Adding an or-operator prove( (G 1 ; G 2) ) : prove(G 1). prove( (G 1 ; G 2) ) : prove(G 2). • Changing the order of evaluation from right to left prove( (G 1, G 2) ) : - !, prove(G 2), prove(G 1). cs 774 (Prasad) L 11 Meta. Pgm 4

Tracing Basic Meta-Interpreter tracep( Goal ) : tracep(Goal, 0). tracep( true, Dpth ) :

Tracing Basic Meta-Interpreter tracep( Goal ) : tracep(Goal, 0). tracep( true, Dpth ) : - !. tracep((G 1, G 2), Dpth ): -!, tracep(G 1, Dpth), tracep(G 2, Dpth). cs 774 (Prasad) L 11 Meta. Pgm 5

(cont’d) tracep( G, D ) : display(’Call: ’, G, D), clause(G, B), D 1

(cont’d) tracep( G, D ) : display(’Call: ’, G, D), clause(G, B), D 1 is D+1, tracep(B, D 1), display(’Exit: ’, G, D), display_redo(G, D). tracep( G, D ) : display(’Fail: ’, G, D), fail. cs 774 (Prasad) L 11 Meta. Pgm 6

(cont’d) display( Msg, G, D ) : tab(D), write(Msg), write(G), nl. display_redo( G, D

(cont’d) display( Msg, G, D ) : tab(D), write(Msg), write(G), nl. display_redo( G, D ) : true ; display(’Redo: ’, G, D), cs 774 (Prasad) L 11 Meta. Pgm fail. 7

(Trace Output) 4 ? - tracep(member(5, [1, 5, 3])). Call: member(5, [1, 5, 3])

(Trace Output) 4 ? - tracep(member(5, [1, 5, 3])). Call: member(5, [1, 5, 3]) Call: member(5, [5, 3]) Exit: member(5, [1, 5, 3]) true ; Redo: member(5, [1, 5, 3]) Redo: member(5, [5, 3]) Call: member(5, []) Fail: member(5, [3]) Fail: member(5, [5, 3]) Fail: member(5, [1, 5, 3]) false. cs 774 (Prasad) L 11 Meta. Pgm 8

Other AI Applications • Forward chaining or Bottom-up computation • From facts to all

Other AI Applications • Forward chaining or Bottom-up computation • From facts to all conclusions • Heuristic search (incl. breadth-first search) • Diagnosis; Explanation generation • Determine the set of facts, which when assumed, proves the goal • Abductive reasoning cs 774 (Prasad) L 11 Meta. Pgm 9

Meta-level Definition not(A) : - call(A), !, fail. not(A). • Meta-variables range over terms

Meta-level Definition not(A) : - call(A), !, fail. not(A). • Meta-variables range over terms encoding a goal. ? -setof(X, G, L). • X and L are object variables, while G is meta-variable. cs 774 (Prasad) L 11 Meta. Pgm 10

Program Manipulation Example • Write a meta-program to collect all variables in a formula

Program Manipulation Example • Write a meta-program to collect all variables in a formula A. constant(A) : atom(A); integer(A) ; float(A). collect_var(A, Q-Q) : constant(A). collect_var(A, [A|Q]-Q) : var(A). cs 774 (Prasad) L 11 Meta. Pgm 11

(cont’d) collect_var(A, Q) : A =. . [P|As], collect_vars(As, Q). collect_vars([], Q-Q) : -

(cont’d) collect_var(A, Q) : A =. . [P|As], collect_vars(As, Q). collect_vars([], Q-Q) : - !. collect_vars([A|As], Q-Qt) : collect_var(A, Q-Qs), collect_vars(As, Qs-Qt). ? -collect_var(p(X, Y), L). L= [X, Y| _1]-_1 • Difference lists for efficient append cs 774 (Prasad) L 11 Meta. Pgm 12

Theorem Proving Applications and others • Proving equivalence of boolean expressions, arithmetic expressions, etc

Theorem Proving Applications and others • Proving equivalence of boolean expressions, arithmetic expressions, etc • Normalization of expressions • Refutation theorem proving • Goal reordering based on binding information for efficient execution of query • Pretty Printing cs 774 (Prasad) L 11 Meta. Pgm 13

Equational Reasoning: Example • Prove two terms are equivalent given that the operators satisfy

Equational Reasoning: Example • Prove two terms are equivalent given that the operators satisfy certain equality constraints. • Associativity: (a + b) + c = a + (b + c) (a * b) * c = a * (b * c) • Commutativity: (a + b) =(b + a) (a * b) =(b * a) cs 774 (Prasad) L 11 Meta. Pgm 14

(cont’d) • Identity: (a + 0) = a = (0 + a) (a *

(cont’d) • Identity: (a + 0) = a = (0 + a) (a * 1) = a = (1 * a) • Zero: (a * 0) = 0 = (0 * a) • Distributive Law: (a + b) * c =(a * c + b * c) cs 774 (Prasad) L 11 Meta. Pgm 15

Digression : Normal Form • Equality constraints induce an equivalence relation on terms (e.

Digression : Normal Form • Equality constraints induce an equivalence relation on terms (e. g. , arithmetic expressions) • An equivalence relation partitions the set of terms into equivalence classes • A normal form of a term is the representative of the equivalence class to which the term belongs. cs 774 (Prasad) L 11 Meta. Pgm 16

Normalization Procedure • Associative operation : Ignore nestings – collapse term trees to lists

Normalization Procedure • Associative operation : Ignore nestings – collapse term trees to lists • Commutative operation : Ignore order – sort • Zeros and Identities – simplify + delete elements • Distributivity – sum of products form or product of sums form cs 774 (Prasad) L 11 Meta. Pgm 17

Application • Evaluation of a variable-free/constant arithmetic expression is normalization. 2 + 3 =>

Application • Evaluation of a variable-free/constant arithmetic expression is normalization. 2 + 3 => 5 (1 + 2) + 1) => 5 • Equality constraints given by axioms are used as rewrite rules for normalization. – Definition of primitive operations – Relationship among operations cs 774 (Prasad) L 11 Meta. Pgm 18

An Interpreter for Object-Oriented Programs • Class Definition object(Object, Methods) E. g. , object(rectangle(Len,

An Interpreter for Object-Oriented Programs • Class Definition object(Object, Methods) E. g. , object(rectangle(Len, Wid), [(area(A) : - A is Len * Wid)] ). class-name + instance variables cs 774 (Prasad) message L 11 Meta. Pgm method 19

(cont’d) • object/instance rectangle(5, 10) • passing a message send(Object-instance, Message) E. g. ,

(cont’d) • object/instance rectangle(5, 10) • passing a message send(Object-instance, Message) E. g. , send(rectangle(5, 10), area(A)). • specifying class-subclass hierarchy E. g. , isa(square(sd), rectangle(sd, sd)). cs 774 (Prasad) L 11 Meta. Pgm 20

Interpreter Code send( Object, Message) : get_methods( Object, Methods), process( Message, Methods). % Find

Interpreter Code send( Object, Message) : get_methods( Object, Methods), process( Message, Methods). % Find Object's methods % Execute corresponding method get_methods( Object, Methods) : object( Object, Methods). % Private methods get_methods( Object, Methods) : isa( Object, Super), get_methods( Super, Methods). % Inherited methods cs 774 (Prasad) L 11 Meta. Pgm 21

(cont’d) process( Msg, [Msg | _]). % Use a fact process( Msg, [ (Msg

(cont’d) process( Msg, [Msg | _]). % Use a fact process( Msg, [ (Msg : - Body) | _]) : call( Body). % Use a rule process( Msg, [_ | Methods]) : process( Msg, Methods). cs 774 (Prasad) L 11 Meta. Pgm 22

? - trace, send(square(4), area(A)). Call: (7) send(square(4), area(_G 414)) ? creep Call: (8)

? - trace, send(square(4), area(A)). Call: (7) send(square(4), area(_G 414)) ? creep Call: (8) get_methods(square(4), _G 551) ? creep Call: (9) object(square(4), _G 551) ? creep Fail: (9) object(square(4), _G 551) ? creep Redo: (8) get_methods(square(4), _G 551) ? creep Call: (9) isa(square(4), _G 551) ? creep Exit: (9) isa(square(4), rectangle(4, 4)) ? creep Call: (9) get_methods(rectangle(4, 4), _G 554) ? creep Call: (10) object(rectangle(4, 4), _G 554) ? creep Exit: (10) object(rectangle(4, 4), [ (area(_G 555): -_G 555 is 4*4)]) ? creep Exit: (9) get_methods(rectangle(4, 4), [ (area(_G 555): -_G 555 is 4*4)]) ? creep Exit: (8) get_methods(square(4), [ (area(_G 555): -_G 555 is 4*4)]) ? creep Call: (8) process(area(_G 414), [ (area(_G 555): -_G 555 is 4*4)]) ? creep Call: (9) _G 414 is 4*4 ? creep Exit: (9) 16 is 4*4 ? creep Exit: (8) process(area(16), [ (area(16): -16 is 4*4)]) ? creep Exit: (7) send(square(4), area(16)) ? creep A = 16. cs 774 (Prasad) L 11 Meta. Pgm 23