1:- module(pha_mi, [mi/3]).
6:- use_module(library(typedef)). 7
8:- type goal == term.
9:- type rv(_) == term.
10:- type prob == float.
11
12:- type pair(A,B) ---> A-B.
13:- type weighted(A) ---> prob:A.
14:- type assumption ---> rv(A) := A.
15:- type cont ---> ans(term) ; [goal|cont].
16:- type susreq ---> susreq(assumption, cont, prob). % suspend on probabilistic choice
17:- type request ---> sus(list(susreq)) % result of meta-interpretation is suspension
18 ; ret(goal). 19
20:- multifile pha_user:rule/3, pha_user:rv/2.
28mi(Cont, AS, R) :- mi(Cont, [], AS, [], R).
31mi(ans(G),_,_,_,ret(G)).
32mi([H|G1],T1,AS,TS,R) :- mi1(H,G1,T1,AS,TS,R) -> true; mi2(T1,AS,TS,R).
36mi1(N:=V, G1,T1,AS,TS1,R) :- !,
37 ( member(N:=V1,AS) 38 -> V=V1, 39 mi(G1,T1,AS,TS1,R) 40 ; rv(N,Dist), 41 findall(susreq(N:=V,G1,P), member(P:V,Dist),TS2,TS1), 42 mi2(T1,AS,TS2,R) 43 ).
44mi1(G,G1,T1,AS,TS,R) :-
45 46 findall(G2,rule(G,G2,G1),T2,T1), 47 mi2(T2,AS,TS,R).
50mi2([],_,Threads,sus(Threads)). 51mi2([T|T1],AS,TS,R) :- mi(T,T1,AS,TS,R). 52
53rule(X=X,GS,GS).
54rule(X\=Y,GS,GS) :- X\=Y.
55rule(X is Y,GS,GS) :- X is Y.
56rule(or(GA,GB,G2),G1,G2) :- G1=GA; G1=GB.
57rule(H,G1,G2) :- pha_user:rule(H,G1,G2).
58rv(N,V) :- pha_user:rv(N,V)
PHA meta-interpreter
*/