Changeset 4477 for trunk/examples

Show
Ignore:
Timestamp:
10/02/08 08:42:54 (3 months ago)
Author:
pmoura
Message:

Improved "metainterpreters" example.

Location:
trunk/examples/metainterpreters
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/examples/metainterpreters/metainterpreters.lgt

    r2800 r4477  
    33 
    44    :- info([ 
    5         version is 1.0, 
    6         date is 2004/5/2, 
     5        version is 1.1, 
     6        date is 2008/10/2, 
    77        author is 'Paulo Moura', 
    88        comment is 'Simple meta-interpreter for pure Prolog with only conjunctions as clause bodies.']). 
     
    2121        solve(B). 
    2222    solve(A) :- 
    23         ::clause(A, B), 
     23        clause(A, B),   % retrieves clauses in "this", i.e. in the database of the object importing the category 
    2424        solve(B). 
    2525 
     
    3030 
    3131    :- info([ 
    32         version is 1.0, 
    33         date is 2004/5/2, 
     32        version is 1.1, 
     33        date is 2008/10/2, 
    3434        author is 'Paulo Moura', 
    3535        comment is 'Meta-interpreter for pure Prolog with only conjunctions as clause bodies.']). 
     
    4848        proof_tree(B, PB). 
    4949    proof_tree(A, (A :- PB)) :- 
    50         ::clause(A, B), 
     50        clause(A, B),   % retrieves clauses in "this", i.e. in the database of the object importing the category 
    5151        proof_tree(B, PB). 
    5252 
     
    5757 
    5858    :- info([ 
    59         version is 1.0, 
    60         date is 2004/5/5, 
     59        version is 1.1, 
     60        date is 2008/10/2, 
    6161        author is 'Paulo Moura', 
    6262        comment is 'A simple tracer meta-interpreter for pure Prolog with only conjunctions as clause bodies.']). 
     
    7979    trace(A, Depth) :- 
    8080        write_trace(call, A, Depth), 
    81         clause(A, B), 
     81        clause(A, B),   % retrieves clauses in "this", i.e. in the database of the object importing the category 
    8282        Depth2 is Depth + 1, 
    8383        trace(B, Depth2), 
    8484        (   write_trace(exit, A, Depth) 
    85             ; 
    86             write_trace(redo, A, Depth), 
    87             fail). 
     85        ;   write_trace(redo, A, Depth), 
     86            fail 
     87        ). 
    8888    trace(A, Depth) :- 
    8989        write_trace(fail, A, Depth), 
  • trunk/examples/metainterpreters/NOTES.txt

    r4466 r4477  
    2828To use a meta-interpreter with an object, simply import the corresponding  
    2929category. 
     30 
     31Defining meta-interpreters as categories allows the use of the built-in  
     32predicate clause/2 to access the clauses of object predicates without  
     33forcing these predicates to be declared public or protected. Within a  
     34category, calls to the built-in predicate clause/2 retrieve clauses in  
     35"this", i.e. in the database of the object importing the category.  
  • trunk/examples/metainterpreters/rules.lgt

    r3201 r4477  
    88 
    99    :- op(200,  fx, if). 
    10     :- op(100, xfx, then). 
     10    :- op(150, xfx, then). 
     11    :- op(100, xfy, &). 
    1112 
    1213    prove(true) :- 
    1314        !. 
    14  
    15     prove([]) :- 
    16         !. 
    17     prove([Cond| Conds]) :- 
     15    prove(Cond & Conds) :- 
    1816        !, 
    1917        prove(Cond), 
    2018        prove(Conds). 
    21  
    2219    prove(Fact) :- 
    23         ::clause(Fact, true). 
     20        clause(Fact, true). 
    2421    prove(Conclusion) :- 
    25         ::clause(if Conds then Conclusion, true), 
     22        clause(if Conds then Conclusion, true), 
    2623        prove(Conds). 
    2724 
     
    3835 
    3936    :- op(200,  fx, if). 
    40     :- op(100, xfx, then). 
     37    :- op(150, xfx, then). 
     38    :- op(100, xfy, &). 
    4139 
    42     if [weather(sunny), weekday(weekend), time(day)] then goto(beach). 
    43     if [weather(raining), weekday(weekend), time(night)] then goto(cinema). 
    44     if [weekday(workday), time(day)] then goto(work). 
     40    if weather(sunny) & weekday(weekend) & time(day) then goto(beach). 
     41    if weather(raining) & weekday(weekend) & time(night) then goto(cinema). 
     42    if weekday(workday) & time(day) then goto(work). 
    4543 
    4644    weather(raining).