```    1:- module(langsem,[]).    2:- use_module(util(math)).    3:- use_module(pac(op)).    4:- use_module(pac(basic)).    5
6% ?- val(:append([a,b],[c,d]), X).
7
8val(X, Y):- pac:eval(X, Y).
9
10% ?- listing(powerset).
11% ?- montague_basic(j, X).
12% ?- montague_basic(powerset([a,b,c]), X).
13% ?- montague_basic(filter([a,b,c], [c]), X).
14% ?- montague_basic(currify([(a,b)]), X).
15% ?- montague_basic(a, A), length(A, L).
16% ?- montague_basic(inverse([(1, b), (3, a), (2, b)], [b]), A).
17% ?- montague_basic(value(a, [(a,b)]), A).
18% ?- montague_basic(value(a, [(a,b), (a,c)]), A).
19% ?- montague_basic(member(2, [1,2]), R).
20%
21individuals([j,m,u]).
22%
23montague_basic(Ind, Ind):-individuals(D), memq(Ind, D), !.
24montague_basic(unicorn, [u]):-!.
25montague_basic(find, [(j, u)]):-!.
26montague_basic(powerset(X), P) :- !, math:powerset(X, P).
27montague_basic(filter(A, B), F):- !, math:filter(B, A, F).
28montague_basic(currify(A), C):- !, currify(A, C).
29montague_basic(a, A):-!, individuals(D), a(D, A).
30montague_basic(value(R, X), V):- !, value(R, X, V).
31montague_basic(inverse(F, Y), X):- !, inverse(F, Y, X).
32montague_basic(member(X, Y), R):- !, member(X, Y, R).
33
34% ?- 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)]`]).
35
36% ?- test.
37test :- repeat,
38	(sample(X), writeln(X), write('=> '),
39	 (montague(X, Y)-> writeln(Y); writeln( '** fail **')),
40	 fail
41	; true ), !.
42
43% ?- montague(value(currify(a([j,u])), unicorn), R).
44% ?- montague(value @ currify(a([j,u])) @ unicorn, R).
45% ?- montague(value(currify(a([j,u])), [j]), R).
46
47sample(filter([j], [j,u])).
48sample(currify([(a,b)])).
49sample(a([j,u])).
50sample(unicorn).
51sample(find).
52sample(currify(a([j,u]))).
53sample(value(currify(a([j,u])), unicorn)).
54sample(currify(find)).
55sample(value(currify(a([j,u])), unicorn)).
56sample(value @ currify(a([j,u])) @ unicorn).
57sample(inverse(currify(find), (value @ currify(a([j,u])) @ unicorn))).
58sample(member(inverse(currify(find), (value @currify(a([j,u])) @ unicorn)), filter([j], [j, u]))).
59sample(member(inverse(currify(find), value(currify(a([j,u])), unicorn)), filter([j], [j, u]))).
60
61% ?- test.
62test(Query, YesNo):- D = [j,u],
63	J = filter([j],D),
64	A = a(D),
65	AC = currify(A),
66	U = unicorn,
67	AUnicorn = (@(AC,U)),
68	Love = love,
69	LoveC = currify(Love),
70	Inv = inverse(LoveC,AUnicorn),
71	Query = member(Inv,J),
72	val(Query, YesNo).
73
74%
75member(X, Y, true):-member(X,Y),!.
76member(_, _, false).
77
78% ?- field([(a,b),(b,c)], X).
79% ?- currify([(a,b), (b,c)], X).
80% ?- field([(a,b), (a,a), (c,d), (c,c), (b,c)], F).
81% ?- field([], F).
82
83field(R, F):- findall(X,
84					  (		member((A,B), R),
85							(	X = A
86							;	X = B
87							)
88					  ),
89					  F0),
90			  sort(F0, F).
91%
92value(F, A, V):- member((A,V), F).
93
94% ?- values(x, [(x,a), (y,b), (x,c)], Vs).
95% ?- values(z, [(x,a), (y,b), (x,c)], Vs).
96values(X, R, Vs):- findall(V, member((X, V), R), Vs0),
97				   sort(Vs0, Vs).
98
99% ?- currify([(x,a), (y,b), (x,c)], C).
100currify(R, C):-	field(R, F),
101	findall((X, Ys),
102			(	member(X, F),
103				values(X, R, Ys)
104			),
105			C0),
106	sort(C0, C).
107%
108compose(X, Y, V):-  join(X, Y, V).
109
110% ?- join([(a,b), (c,d)],[(b,c),(d,e)], V).
111join(X, Y, V):-
112	findall((A,C),
113			( member((A,B), X),
114			  member((B,C), Y)
115			),
116			V0),
117	sort(V0, V).
118
119% ?- inverse([(1, a),(2, b), (3, a)], [a], X).
120inverse(F, R, S):-
121	findall(X,
122			(	member(Y, R),
123				member((X,Y), F)
124			),
125			S0),
126	sort(S0, S).
127
128%
129meet(X,Y):- member(A,X), member(A,Y).
130%
131have(X, Y, true):- member(Y, X), !.
132have(_, _, false).
133
134% ?- a([1,2], R), maplist(writeln, R).
135a(D, R):- powerset(D, PowD),
136		findall((X,Y),
137				(	member(X,PowD),
138					member(Y,PowD),
139					meet(X,Y)
140				),
141				R0),
142		sort(R0, R).
143	%
144love([(j,u)]).
145%
146find([(j,u)]).
147%
148unicorn([u]).
149
150% ?- montague(1, X).
151% ?- montague(j, X).
152% ?- montague(k, X).	% error.
153% ?- montague(a, X).	% default for a
154% ?- montague(a([1,2]), X).
155% ?- montague(a@[1,2], X).
156% ?- montague([1,2], X).
157% ?- montague(A, X).
158% ?- montague(powerset@powerset([1]), X).
159% ?- montague_main(powerset@powerset([1,2,3,4]), X).
160montague(X, Y):- montague_main(X, Z), term_string(Z, Y).
161%
162montague_main(X, X):- (var(X); integer(X); is_list(X)), !.
163montague_main(X, Y):- elim_atmark(X, X0),
164	montague_args(X0, U),
165	(	montague_basic(U, Y) -> true
166	;	call(U, Y)
167	).
168%
169montague_args([F|As], V):-
170	montague_args(F, F0),
171	maplist(montague_main, As, Bs),
172	reduce:complete_args(F0, Bs, V).
173
174%
175montague_args(F, G):- nonvar(F),
176	F=..[H|As],
177	maplist(montague_main, As, Bs),
178	G=..[H|Bs].
179
180% ?- elim_atmark(a@(b@c), X).
181% ?- elim_atmark(a@b@c, X).
182elim_atmark(X, [X]):- var(X), !.
183elim_atmark(X@Y, Z):- !, elim_atmark(X, U),
184	append(U, [Y], Z).
185elim_atmark(X, [X])```