1:- module(langsem,[eval_ground_term/2]).    2:- use_module(misc(math)).    3:- include(pac(op)).    4
    5val(X, Y):- mpac:expand_exp_run([], X, Y).
    6
    7% ?- trace, run_cgi(`peek(currify@find), langsem:montague`, []).
    8% montague(X, Y) :- distribute_context(langsem, X, X0), val(X0, Y).
    9montague(X, Y) :-  eval_ground_term(langsem:X, Y).
   10
   11% ad hoc.
   12distribute_context(_, X, X):- var(X), !.
   13distribute_context(M, P@Q, P0@Q0):-!, distribute_context(M, P, P0),
   14	distribute_context(M, Q, Q0).
   15distribute_context(M, :(P), Q):- !, distribute_context(M, P, Q).
   16distribute_context(_, M:P, Q):- !, distribute_context(M, P, Q).
   17distribute_context([], P, :P):- !.
   18distribute_context(M, P, M:P).
   19
   20% ?- trace, run_cgi(`(peek([@phrase2, @words2,@frame2]), maplist(herbrand), apply(models:semantics))`, [words2 = `[a, man]`, phrase2 =`np`, frame2 =`[man(j),man(b),woman(m),unicorn(u), kick(u,j), find(m,u), walk(j),walk(b),walk(m)]`]).
   21
   22%
   23test :- repeat,
   24	(sample(X), writeln(X), write('=> '),
   25	 (montague(X, Y)-> writeln(Y); writeln( '** fail **')),
   26	 fail
   27	; true ), !.
   28
   29sample(filter([j], [j,u])).
   30sample(currify([(a,b)])).
   31sample(a([j,u])).
   32sample(unicorn).
   33sample(find).
   34sample(currify(a([j,u]))).
   35sample(value(currify(a([j,u])), unicorn)).
   36sample(currify(find)).
   37sample(value(currify(a([j,u])), unicorn)).
   38sample(inverse(currify(find), (value @ currify(a([j,u]))@unicorn))).
   39sample(value @ currify(a([j,u])) @ unicorn ).
   40sample(inverse(currify(find), (value @currify(a([j,u])) @ unicorn))).
   41sample(member(inverse(currify(find), (value @currify(a([j,u])) @ unicorn)), filter([j], [j, u]))).
   42sample(member(inverse(currify(find), value(currify(a([j,u])), unicorn)), filter([j], [j, u]))).
   43
   44test(Query, YesNo):- D=[j,u],
   45	J=filter([j],D),
   46	A=a(D),
   47	AC=currify(A),
   48	U=unicorn,
   49	AUnicorn= (@(AC,U)),
   50	Love=love,
   51	LoveC=currify(Love),
   52	Inv=inverse(LoveC,AUnicorn),
   53	Query= member(Inv,J),
   54	% eval(Query, YesNo).
   55	val(Query, YesNo).
   56
   57%
   58member(X, Y, true):-member(X,Y),!.
   59member(_, _, false).
   60
   61%  ?- langsem:field([(a,b),(b,c)], X).
   62%@ X = [a, b, c].
   63%  ?- langsem:values(1, [(1,2), (1,3)], X).
   64% X = [2, 3].
   65% ?- langsem:currify([(a,b), (b,c)], X).
   66%@ X = [ (a, [b]), (b, [c]), (c, [])].
   67
   68field(R, F):- setof(A, B^(member((A,B), R); member((B, A), R)), F), !.
   69field(_, []).
   70
   71values(X, R, Vs):- setof(V, member((X, V), R), Vs), !.
   72values(_, _, []).
   73
   74currify(R, C):-	field(R, S),
   75          setof((X, Ys), (member(X, S), values(X, R, Ys)), C),
   76	  !.
   77currify(_, []).
   78
   79compose(X, Y, V):-  join(X, Y, V).
   80
   81join(X, Y, V):-
   82	setof((A, B),
   83	      C^(member((A,C),X), member((C,B),Y)), V),
   84	!.
   85join(_, _, []).
   86
   87filter(X, D, F):- powerset(D, PowD),
   88	setof(M, (member(M, PowD), subset(X, M)), F), !.
   89filter(_, _, []).
   90
   91
   92% ?- inverse([(1, a),(2, b), (3, a)], [a], X).
   93
   94% X = [1, 3].
   95
   96inverse(F, R, S):-setof(X, Y^(member(Y, R),member((X, Y), F)),S), !.
   97inverse(_, _, []).
   98
   99% ?- langsem:value([(a,b)], a, X).
  100% X = b
  101value(F, A, V):- memberchk((A,V), F).
  102
  103meet(X,Y):- member(A,X), member(A,Y).
  104
  105have(X, Y, true):- member(Y, X), !.
  106have(_, _, false).
  107
  108%%
  109
  110a(D, R):-( powerset(D, PowD),
  111	   setof((X,Y),
  112		(member(X,PowD),member(Y,PowD), meet(X,Y)),
  113		 R)
  114	-> true
  115	;  R=[]
  116	).
  117
  118love([(j,u)]).
  119
  120find([(j,u)]).
  121
  122unicorn([u]).
  123
  124% ?- langsem:term_to_exp(f(a), R).
  125% ?- langsem:term_to_exp(f(a,b), R).
  126% ?- langsem:term_to_exp(f(a,g(b), c), R).
  127%@ R = @(@(@(f, a), @(g, b)), c) .
  128
  129% ?- langsem:eval_term(langsemterm_to_exp(f(a,b), R).
  130% ?- langsem:sample(E), langsem:eval_term(langsem:E, V).
  131% ?- langsem:eval_term(langsem:(value @ currify(a([j,u])) @ unicorn), V).
  132
  133% ?- langsem:eval_term(langsem:inverse(currify(find), (value @currify(a([j,u])) @ unicorn)), V).
  134
  135% ?- langsem:sample(E), langsem:eval_ground_term(langsem:E, V).
  136
  137eval_ground_term(T, V):- once(term_to_exp(T, E)),
  138	once(val(E, V)).
  139
  140%
  141term_to_exp(X, X) :- is_list(X); atomic(X).
  142term_to_exp(X@Y, X0@Y0)	:- term_to_exp(X, X0),
  143	term_to_exp(Y, Y0).
  144term_to_exp(:X, :Y):- term_to_exp(X, Y).
  145term_to_exp(M:X, M:Y):- term_to_exp(X, Y).
  146term_to_exp(X, Y):- X =.. [F|As],
  147	maplist(term_to_exp, As, Bs),
  148	term_to_exp(F, Bs, Y).
  149%
  150term_to_exp(X,  [], X).
  151term_to_exp(X,  [A|As], Y):- term_to_exp((X