View source with raw comments or as raw
    1:- module(scasp_prolog,
    2          [ scasp_prolog/2              % :Goal, -Justification
    3          ]).    4:- use_module(library(apply)).    5:- use_module(library(prolog_code)).    6
    7:- meta_predicate
    8    scasp_prolog(0, -).
 scasp_prolog(:Goal, -Justification)
Proof Goal using a Prolog meta interpreter. Justification is a s(CASP) compatible justification tree.
   15scasp_prolog(M:Goal, Tree) :-
   16    prolog_current_choice(Ch),
   17    scasp_prolog(Goal, M, Tree, Ch).
   18
   19scasp_prolog(true, _, Tree, _) =>
   20    Tree = [].
   21scasp_prolog(false, _, _, _) =>
   22    fail.
   23scasp_prolog(fail, _, _, _) =>
   24    fail.
   25scasp_prolog(M:Goal, _, Tree, Ch) =>
   26    scasp_prolog(Goal, M, Tree, Ch).
   27scasp_prolog((A,B), M, Children, Ch) =>
   28    body_list((A,B), Conj),
   29    maplist(scasp_prolog_ch(Ch, M), Conj, Children).
   30scasp_prolog((If->Then;Else), M, Tree, Ch) =>
   31    (   prolog_current_choice(Ch2),
   32        scasp_prolog(If, M, TreeA, Ch2)
   33    ->  scasp_prolog(Then, M, TreeB, Ch),
   34        Tree = [TreeA,TreeB]
   35    ;   scasp_prolog(Else, M, TreeC, Ch),
   36        Tree = [not(If)-[], TreeC]
   37    ).
   38scasp_prolog((A;B), M, Tree, Ch) =>
   39    (   scasp_prolog(A, M, Tree, Ch)
   40    ;   scasp_prolog(B, M, Tree, Ch)
   41    ).
   42scasp_prolog(\+ A, M, Tree, _) =>
   43    prolog_current_choice(Ch),
   44    \+ scasp_prolog(A, M, _, Ch),
   45    Tree = not(A)-[].
   46scasp_prolog(!, _, Tree, Ch) =>
   47    Tree = !-[],
   48    prolog_cut_to(Ch).
   49scasp_prolog(Goal, M, Tree, _),
   50    opaque(M:Goal) =>
   51    (   predicate_property(system:Goal, foreign)
   52    ->  Tree = Goal-[]
   53    ;   Tree = (M:Goal)-[]
   54    ),
   55    call(M:Goal).
   56scasp_prolog(Goal, M, Tree, Ch),
   57    predicate_property(M:Goal, imported_from(M2)) =>
   58    scasp_prolog(Goal, M2, Tree, Ch).
   59scasp_prolog(Goal, M, Tree, _),
   60    predicate_property(M:Goal, ssu) =>
   61    most_general_goal(Goal, Head),
   62    prolog_current_choice(Ch),
   63    (   rule(M:Head, Rule, ClauseRef),
   64        split_rule(Rule, HeadGuard, Body),
   65        body_list(HeadGuard, [Head|GuardConj]),
   66        subsumes_term(Head, Goal)
   67    *-> Head = Goal,
   68        Tree = goal_origin(M:Goal, ClauseRef)-Children,
   69        body_list(Body, BodyConj),
   70        append([GuardConj, [!], BodyConj], Conj),
   71        maplist(scasp_prolog_ch(Ch, M), Conj, Children0),
   72        flatten(Children0, Children)
   73    ;   existence_error(matching_rule, M:Goal)
   74    ).
   75scasp_prolog(Goal, M, Tree, _) =>
   76    Tree = goal_origin(M:Goal, ClauseRef)-Children,
   77    prolog_current_choice(Ch),
   78    clause(M:Goal, Body, ClauseRef),
   79    body_list(Body, Conj),
   80    maplist(scasp_prolog_ch(Ch, M), Conj, Children0),
   81    flatten(Children0, Children).
   82
   83scasp_prolog_ch(Ch, M, Goal, Tree) :-
   84    scasp_prolog(Goal, M, Tree, Ch).
   85
   86split_rule(HeadGuard0 => Body0, HeadGuard, Body) =>
   87    HeadGuard = HeadGuard0,
   88    Body = Body0.
   89
   90body_list(Body, List) :-
   91    phrase(body_list(Body), List).
   92
   93body_list(A) -->
   94    { var(A) },
   95    !,
   96    [A].
   97body_list((A,B)) -->
   98    !,
   99    body_list(A),
  100    body_list(B).
  101body_list(X) -->
  102    [X].
  103
  104opaque(Goal) :-
  105    predicate_property(Goal, foreign),
  106    !.
  107opaque(Goal) :-
  108    predicate_property(Goal, meta_predicate(_)),
  109    !