summaryrefslogtreecommitdiff
path: root/data/geoquery/wasp
diff options
context:
space:
mode:
authorPatrick Simianer <simianer@cl.uni-heidelberg.de>2014-04-14 16:25:44 +0200
committerPatrick Simianer <simianer@cl.uni-heidelberg.de>2014-04-14 16:25:44 +0200
commit056969a3d9dda06248afd0eaaa6eb1da2f174ea7 (patch)
tree722cd71d802be877d3679152f6dffe6eba76c0ea /data/geoquery/wasp
parentb0bff4f48b2de88560199be09e5a29feecaa267c (diff)
wasp
Diffstat (limited to 'data/geoquery/wasp')
-rw-r--r--data/geoquery/wasp/eval.pl374
-rw-r--r--data/geoquery/wasp/geoquery.pl403
2 files changed, 777 insertions, 0 deletions
diff --git a/data/geoquery/wasp/eval.pl b/data/geoquery/wasp/eval.pl
new file mode 100644
index 0000000..e00a067
--- /dev/null
+++ b/data/geoquery/wasp/eval.pl
@@ -0,0 +1,374 @@
+:- ensure_loaded(geoquery).
+:- ensure_loaded(geobase).
+
+:- style_check(-singleton).
+:- style_check(-discontiguous).
+
+:- set_prolog_flag(toplevel_print_options, [quoted(true), portray(true)]).
+
+eval([]).
+eval([I,J,F1,F2|L]) :-
+ execute_funql_query(F1, A1),
+ execute_funql_query(F2, A2),
+ print(I), print(' '), print(J), (A1 == A2 -> print(' y') ; print(' n')), nl,
+ eval(L).
+
+execute_funql_query(null, null).
+execute_funql_query(Q, U) :- process(Q,P), sort(P, U).
+execute_funql_query(Q, []). % empty result
+
+process(answer(Q), P) :- process(Q, P).
+
+process(stateid(A), [stateid(A)]).
+process(cityid(A,B), [cityid(A,B)]).
+process(riverid(A), [riverid(A)]).
+process(countryid(A), [countryid(A)]).
+process(placeid(A), [placeid(A)]).
+
+process(city(all), A) :- findall(B, city(B), A).
+process(mountain(all), A) :- findall(B, place(B), A).
+process(place(all), A) :- findall(B, place(B), A).
+process(river(all), A) :- findall(B, river(B), A).
+process(lake(all), A) :- findall(B, lake(B), A).
+process(state(all), A) :- findall(B, state(B), A).
+process(capital(all), A) :- findall(B, capital(B), A).
+
+% filter the list by the predicate
+process(capital(A), P) :- process(A,L), process(capital(L), P).
+process(capital([]), []).
+process(capital([A|AA]), [A|PP]) :- capital(A), !, process(capital(AA), PP).
+process(capital([A|AA]), PP) :- process(capital(AA), PP).
+process2(capital(A), P) :- process2(A,L), process2(capital(L), P).
+process2(capital([]), []).
+process2(capital([A-S|AA]), [PA-S|PP]) :- process(capital(A),PA), process2(capital(AA), PP).
+
+process(city(A), P) :- process(A,L), process(city(L), P).
+process(city([]), []).
+process(city([A|AA]), [A|PP]) :- city(A), !, process(city(AA), PP).
+process(city([A|AA]), PP) :- process(city(AA), PP).
+process2(city(A), P) :- process2(A,L), process2(city(L), P).
+process2(city([]), []).
+process2(city([A-S|AA]), [PA-S|PP]) :- process(city(A),PA), process2(city(AA), PP).
+
+process(major(A), P) :- process(A,L), process(major(L), P).
+process(major([]), []).
+process(major([A|AA]), [A|PP]) :- major(A), !, process(major(AA), PP).
+process(major([A|AA]), PP) :- process(major(AA), PP).
+process2(major(A), P) :- process2(A,L), process2(major(L), P).
+process2(major([]), []).
+process2(major([A-S|AA]), [PA-S|PP]) :- process(major(A),PA), process2(major(AA), PP).
+
+process(place(A), P) :- process(A,L), process(place(L), P).
+process(place([]), []).
+process(place([A|AA]), [A|PP]) :- place(A), !, process(place(AA), PP).
+process(place([A|AA]), PP) :- process(place(AA), PP).
+process2(place(A), P) :- process2(A,L), process2(place(L), P).
+process2(place([]), []).
+process2(place([A-S|AA]), [PA-S|PP]) :- process(place(A),PA), process2(place(AA), PP).
+
+process(river(A), P) :- process(A,L), process(river(L), P).
+process(river([]), []).
+process(river([A|AA]), [A|PP]) :- river(A), !, process(river(AA), PP).
+process(river([A|AA]), PP) :- process(river(AA), PP).
+process2(river(A), P) :- process2(A,L), process2(river(L), P).
+process2(river([]), []).
+process2(river([A-S|AA]), [PA-S|PP]) :- process(river(A),PA), process2(river(AA), PP).
+
+process(lake(A), P) :- process(A,L), process(lake(L), P).
+process(lake([]), []).
+process(lake([A|AA]), [A|PP]) :- lake(A), !, process(lake(AA), PP).
+process(lake([A|AA]), PP) :- process(lake(AA), PP).
+process2(lake(A), P) :- process2(A,L), process2(lake(L), P).
+process2(lake([]), []).
+process2(lake([A-S|AA]), [PA-S|PP]) :- process(lake(A),PA), process2(lake(AA), PP).
+
+process(state(A), P) :- process(A,L), process(state(L), P).
+process(state([]), []).
+process(state([A|AA]), [A|PP]) :- state(A), !, process(state(AA), PP).
+process(state([A|AA]), PP) :- process(state(AA), PP).
+process2(state(A), P) :- process2(A,L), process2(state(L), P).
+process2(state([]), []).
+process2(state([A-S|AA]), [PA-S|PP]) :- process(state(A),PA), process2(state(AA), PP).
+
+process(mountain(A), P) :- process(A,L), process(mountain(L), P).
+process(mountain([]), []).
+process(mountain([A|AA]), [A|PP]) :- place(A), !, process(mountain(AA), PP).
+process(mountain([A|AA]), PP) :- process(mountain(AA), PP).
+process2(mountain(A), P) :- process2(A,L), process2(mountain(L), P).
+process2(mountain([]), []).
+process2(mountain([A-S|AA]), [PA-S|PP]) :- process(mountain(A),PA), process2(mountain(AA), PP).
+
+% find the required (one-to-one); process2 generates pairwise list
+process(len(A), P) :- process(A,L), process(len(L), P).
+process(len([]), []).
+process(len([A|AA]), [P|PP]) :- len(A, P), process(len(AA), PP).
+process(len([A|AA]), PP) :- process(len(AA), PP).
+process2(len(A), P) :- process(A,L), process2(len(L), P).
+process2(len([]), []).
+process2(len([A|AA]), [P-A|PP]) :- len(A, P), process2(len(AA), PP).
+process2(len([A|AA]), PP) :- process2(len(AA), PP).
+
+process(size(A), P) :- process(A,L), process(size(L), P).
+process(size([]), []).
+process(size([A|AA]), [P|PP]) :- size(A, P), process(size(AA), PP).
+process(size([A|AA]), PP) :- process(size(AA), PP).
+process2(size(A), P) :- process(A,L), process2(size(L), P).
+process2(size([]), []).
+process2(size([A|AA]), [P-A|PP]) :- size(A, P), process2(size(AA), PP).
+process2(size([A|AA]), PP) :- process2(size(AA), PP).
+
+process(area_1(A), P) :- process(A,L), process(area_1(L), P).
+process(area_1([]), []).
+process(area_1([A|AA]), [P|PP]) :- area(A, P), process(area_1(AA), PP).
+process(area_1([A|AA]), PP) :- process(area_1(AA), PP).
+process2(area_1(A), P) :- process(A,L), process2(area_1(L), P).
+process2(area_1([]), []).
+process2(area_1([A|AA]), [P-A|PP]) :- area(A, P), process2(area_1(AA), PP).
+process2(area_1([A|AA]), PP) :- process2(area_1(AA), PP).
+
+process(population_1(A), P) :- process(A,L), process(population_1(L), P).
+process(population_1([]), []).
+process(population_1([A|AA]), [P|PP]) :- population(A, P), process(population_1(AA), PP).
+process(population_1([A|AA]), PP) :- process(population_1(AA), PP). % if not found
+process2(population_1(A), P) :- process(A,L), process2(population_1(L), P).
+process2(population_1([]), []).
+process2(population_1([A|AA]), [P-A|PP]) :- population(A, P), process2(population_1(AA), PP).
+process2(population_1([A|AA]), PP) :- process2(population_1(AA), PP). % if not found
+
+process(density_1(A), P) :- process(A,L), process(density_1(L), P).
+process(density_1([]), []).
+process(density_1([A|AA]), [P|PP]) :- density(A, P), process(density_1(AA), PP).
+process(density_1([A|AA]), PP) :- process(density_1(AA), PP).
+process2(density_1(A), P) :- process(A,L), process2(density_1(L), P).
+process2(density_1([]), []).
+process2(density_1([A|AA]), [P-A|PP]) :- density(A, P), process2(density_1(AA), PP).
+process2(density_1([A|AA]), PP) :- process2(density_1(AA), PP).
+
+process(elevation_1(A), P) :- process(A,L), process(elevation_1(L), P).
+process(elevation_1([]), []).
+process(elevation_1([A|AA]), [P|PP]) :- elevation(A, P), process(elevation_1(AA), PP).
+process(elevation_1([A|AA]), PP) :- process(elevation_1(AA), PP).
+process2(elevation_1(A), P) :- process(A,L), process2(elevation_1(L), P).
+process2(elevation_1([]), []).
+process2(elevation_1([A|AA]), [P-A|PP]) :- elevation(A, P), process2(elevation_1(AA), PP).
+process2(elevation_1([A|AA]), PP) :- process2(elevation_1(AA), PP).
+
+%%%% no need for process2
+
+process(capital_1(A), P) :- process(A,L), process(capital_1(L), P).
+process(capital_1([]), []).
+process(capital_1([A|AA]), [P|PP]) :- capital(A, P), process(capital_1(AA), PP).
+process(capital_1([A|AA]), PP) :- process(capital_1(AA), PP).
+
+% find all the required (one-to-many)
+process(capital_2(A), P) :- process(A,L), process(capital_2(L), P).
+process(capital_2([]), []).
+process(capital_2([A|L]), P) :- findall(B, capital(B, A), AA),
+ process(capital_2(L),LL), append(AA,LL,P).
+
+process(elevation_2(A), P) :- process(A,L), process(elevation_2(L), P).
+process(elevation_2([]), []).
+process(elevation_2([A|L]), P) :- findall(B, elevation(B, A), AA),
+ process(elevation_2(L),LL), append(AA,LL,P).
+
+process(high_point_1(A), P) :- process(A,L), process(high_point_1(L), P).
+process(high_point_1([]), []).
+process(high_point_1([A|L]), P) :- findall(B, high_point(A, B), AA),
+ process(high_point_1(L),LL), append(AA,LL,P).
+process2(high_point_1(A), P) :- process(A,L), process2(high_point_1(L), P).
+process2(high_point_1([]), []).
+process2(high_point_1([A|L]), [AA-A|P]) :- findall(B, high_point(A, B), AA),
+ process2(high_point_1(L),P).
+
+process(higher_1(A), P) :- process(A,L), process(higher_1(L), P).
+process(higher_1([]), []).
+process(higher_1([A|L]), P) :- findall(B, higher(A, B), AA),
+ process(higher_1(L),LL), append(AA,LL,P).
+process2(higher_1(A), P) :- process(A,L), process2(higher_1(L), P).
+process2(higher_1([]), []).
+process2(higher_1([A|L]), [AA-A|P]) :- findall(B, higher(A, B), AA),
+ process2(higher_1(L),P).
+
+process(lower_1(A), P) :- process(A,L), process(lower_1(L), P).
+process(lower_1([]), []).
+process(lower_1([A|L]), P) :- findall(B, lower(A, B), AA),
+ process(lower_1(L),LL), append(AA,LL,P).
+process2(lower_1(A), P) :- process(A,L), process2(lower_1(L), P).
+process2(lower_1([]), []).
+process2(lower_1([A|L]), [AA-A|P]) :- findall(B, lower(A, B), AA),
+ process2(lower_1(L),P).
+
+process(loc_1(A), P) :- process(A,L), process(loc_1(L), P).
+process(loc_1([]), []).
+process(loc_1([A|L]), P) :- findall(B, loc(A, B), AA),
+ process(loc_1(L),LL), append(AA,LL,P).
+process2(loc_1(A), P) :- process(A,L), process2(loc_1(L), P).
+process2(loc_1([]), []).
+process2(loc_1([A|L]), [AA-A|P]) :- findall(B, loc(A, B), AA),
+ process2(loc_1(L),P).
+
+process(low_point_1(A), P) :- process(A,L), process(low_point_1(L), P).
+process(low_point_1([]), []).
+process(low_point_1([A|L]), P) :- findall(B, low_point(A, B), AA),
+ process(low_point_1(L),LL), append(AA,LL,P).
+process2(low_point_1(A), P) :- process(A,L), process2(low_point_1(L), P).
+process2(low_point_1([]), []).
+process2(low_point_1([A|L]), [AA-A|P]) :- findall(B, low_point(A, B), AA),
+ process2(low_point_1(L),P).
+
+process(next_to_1(A), P) :- process(A,L), process(next_to_1(L), P).
+process(next_to_1([]), []).
+process(next_to_1([A|L]), P) :- findall(B, next_to(A, B), AA),
+ process(next_to_1(L),LL), append(AA,LL,P).
+process2(next_to_1(A), P) :- process(A,L), process2(next_to_1(L), P).
+process2(next_to_1([]), []).
+process2(next_to_1([A|L]), [AA-A|P]) :- findall(B, next_to(A, B), AA),
+ process2(next_to_1(L),P).
+
+process(traverse_1(A), P) :- process(A,L), process(traverse_1(L), P).
+process(traverse_1([]), []).
+process(traverse_1([A|L]), P) :- findall(B, traverse(A, B), AA),
+ process(traverse_1(L),LL), append(AA,LL,P).
+process2(traverse_1(A), P) :- process(A,L), process2(traverse_1(L), P).
+process2(traverse_1([]), []).
+process2(traverse_1([A|L]), [AA-A|P]) :- findall(B, traverse(A, B), AA),
+ process2(traverse_1(L),P).
+
+process(high_point_2(A), P) :- process(A,L), process(high_point_2(L), P).
+process(high_point_2([]), []).
+process(high_point_2([A|L]), P) :- findall(B, high_point(B, A), AA),
+ process(high_point_2(L),LL), append(AA,LL,P).
+process2(high_point_2(A), P) :- process(A,L), process2(high_point_2(L), P).
+process2(high_point_2([]), []).
+process2(high_point_2([A|L]), [AA-A|P]) :- findall(B, high_point(B, A), AA),
+ process2(high_point_2(L),P).
+
+process(higher_2(A), P) :- process(A,L), process(higher_2(L), P).
+process(higher_2([]), []).
+process(higher_2([A|L]), P) :- findall(B, higher(B, A), AA),
+ process(higher_2(L),LL), append(AA,LL,P).
+process2(higher_2(A), P) :- process(A,L), process2(higher_2(L), P).
+process2(higher_2([]), []).
+process2(higher_2([A|L]), [AA-A|P]) :- findall(B, higher(B, A), AA),
+ process2(higher_2(L),P).
+
+process(lower_2(A), P) :- process(A,L), process(lower_2(L), P).
+process(lower_2([]), []).
+process(lower_2([A|L]), P) :- findall(B, lower(B, A), AA),
+ process(lower_2(L),LL), append(AA,LL,P).
+process2(lower_2(A), P) :- process(A,L), process2(lower_2(L), P).
+process2(lower_2([]), []).
+process2(lower_2([A|L]), [AA-A|P]) :- findall(B, lower(B, A), AA),
+ process2(lower_2(L),P).
+
+process(loc_2(A), P) :- process(A,L), process(loc_2(L), P).
+process(loc_2([]), []).
+process(loc_2([A|L]), P) :- findall(B, loc(B, A), AA),
+ process(loc_2(L),LL), append(AA,LL,P).
+process2(loc_2(A), P) :- process(A,L), process2(loc_2(L), P).
+process2(loc_2([]), []).
+process2(loc_2([A|L]), [AA-A|P]) :- findall(B, loc(B, A), AA),
+ process2(loc_2(L),P).
+
+process(low_point_2(A), P) :- process(A,L), process(low_point_2(L), P).
+process(low_point_2([]), []).
+process(low_point_2([A|L]), P) :- findall(B, low_point(B, A), AA),
+ process(low_point_2(L),LL), append(AA,LL,P).
+process2(low_point_2(A), P) :- process(A,L), process2(low_point_2(L), P).
+process2(low_point_2([]), []).
+process2(low_point_2([A|L]), [AA-A|P]) :- findall(B, low_point(B, A), AA),
+ process2(low_point_2(L),P).
+
+process(traverse_2(A), P) :- process(A,L), process(traverse_2(L), P).
+process(traverse_2([]), []).
+process(traverse_2([A|L]), P) :- findall(B, traverse(B, A), AA),
+ process(traverse_2(L),LL), append(AA,LL,P).
+process2(traverse_2(A), P) :- process(A,L), process2(traverse_2(L), P).
+process2(traverse_2([]), []).
+process2(traverse_2([A|L]), [AA-A|P]) :- findall(B, traverse(B, A), AA),
+ process2(traverse_2(L),P).
+
+process(next_to_2(A), P) :- process(A,L), process(next_to_2(L), P).
+process(next_to_2([]), []).
+process(next_to_2([A|L]), P) :- findall(B, next_to(B, A), AA),
+ process(next_to_2(L),LL), append(AA,LL,P).
+process2(next_to_2(A), P) :- process(A,L), process2(next_to_2(L), P).
+process2(next_to_2([]), []).
+process2(next_to_2([A|L]), [AA-A|P]) :- findall(B, next_to(B, A), AA),
+ process2(next_to_2(L),P).
+
+process(longer(A), P) :- process(A,L), process(longer(L), P).
+process(longer([]), []).
+process(longer([A|L]), P) :- findall(B, longer(B, A), AA),
+ process(longer(L),LL), append(AA,LL,P).
+process2(longer(A), P) :- process(A,L), process2(longer(L), P).
+process2(longer([]), []).
+process2(longer([A|L]), [AA-A|P]) :- findall(B, longer(B, A), AA),
+ process2(longer(L),P).
+% metas
+ % helpful for meta
+pair_size([A|AA], [(Size-A)|LL]) :- size(A,Size), pair_size(AA, LL).
+pair_size([A|AA], LL) :- pair_size(AA, LL).
+pair_size([], []).
+pair_elevation([A|AA], [(Elevation-A)|LL]) :- elevation(A,Elevation), pair_elevation(AA,LL).
+pair_elevation([A|AA], LL) :- pair_elevation(AA,LL).
+pair_elevation([], []).
+pair_len([A|AA], [(Len-A)|LL]) :- len(A,Len), pair_len(AA, LL).
+pair_len([A|AA], LL) :- pair_len(AA, LL).
+pair_len([], []).
+
+process(largest(A), PP) :- process(A,P), pair_size(P, PS),
+ (PS=[] -> PP=[]; (max_key(PS, M),PP=[M])).
+process(smallest(A), PP) :- process(A,P), pair_size(P, PS),
+ (PS=[] -> PP=[]; (min_key(PS, M),PP=[M])).
+
+process(highest(A), PP) :- process(A,P), pair_elevation(P, PS),
+ (PS=[] -> PP=[]; (max_key(PS, M),PP=[M])).
+process(lowest(A), PP) :- process(A,P), pair_elevation(P, PS),
+ (PS=[] -> PP=[]; (min_key(PS, M),PP=[M])).
+
+process(longest(A), PP) :- process(A,P), pair_len(P, PS),
+ (PS=[] -> PP=[]; (max_key(PS, M),PP=[M])).
+process(shortest(A), PP) :- process(A,P), pair_len(P, PS),
+ (PS=[] -> PP=[]; (min_key(PS, M),PP=[M])).
+
+% ones
+numerify([],[]).
+numerify([L-S|R], [N-S|NR]) :- sort(L,LL), length(LL,N), numerify(R,NR).
+
+process(largest_one(A), P) :- process2(A, S),
+ (S=[]-> P=[]; (max_key(S,M), P=[M])).
+process(highest_one(A), P) :- process2(A, S),
+ (S=[]-> P=[]; (max_key(S,M), P=[M])).
+process(longest_one(A), P) :- process2(A, S),
+ (S=[]-> P=[]; (max_key(S,M), P=[M])).
+process(most(A), P) :- process2(A, S),numerify(S,NS),
+ (S=[]-> P=[]; (max_key(NS,M), P=[M])).
+
+process(smallest_one(A), P) :- process2(A, S),
+ (S=[]-> P=[]; (min_key(S,M), P=[M])).
+process(lowest_one(A), P) :- process2(A, S),
+ (S=[]-> P=[]; (min_key(S,M), P=[M])).
+process(shortest_one(A), P) :- process2(A, S),
+ (S=[]-> P=[]; (min_key(S,M), P=[M])).
+process(fewest(A), P) :- process2(A, S),numerify(S,NS),
+ (S=[]-> P=[]; (min_key(NS,M), P=[M])).
+
+
+process(count(A), [P]) :- process(A, B), sort(B, BB), length(BB, P).
+process(sum(A), [P]) :- process(A, B), sumlist(B, 0, P).
+
+% what's the meaning of each really? -ywwong
+process(each(Q), P) :- process(Q, P).
+
+% exclude and intersection
+ % helpful: remove all occurrences of elements of the second list from the first list
+minus(L,[],L).
+minus(L, [A|AA], P) :- delete(L,A,L2), minus(L2, AA, P).
+ % helpful: intersection of two lists
+intersect([],L,[]).
+intersect([A|L1], L2, [A|L]) :- member(A,L2), intersect(L1, L2, L).
+intersect([B|L1], L2, L) :- intersect(L1, L2, L).
+
+process(exclude(A, B), P) :- process(A,P1), process(B,P2), minus(P1, P2, P).
+process(intersection(A, B), P) :- process(A,P1), process(B,P2), intersect(P1, P2, P).
diff --git a/data/geoquery/wasp/geoquery.pl b/data/geoquery/wasp/geoquery.pl
new file mode 100644
index 0000000..5d5d9bc
--- /dev/null
+++ b/data/geoquery/wasp/geoquery.pl
@@ -0,0 +1,403 @@
+:- ensure_loaded(library('lists')).
+:- ensure_loaded(library('ordsets')).
+:- ensure_loaded(geobase).
+
+country(countryid(usa)).
+
+state(stateid(State)) :- state(State,_,_,_,_,_,_,_,_,_).
+
+city(cityid(City,St)) :- city(_,St,City,_).
+
+river(riverid(R)) :- river(R,_,_).
+
+place(placeid(P)) :- highlow(_,_,P,_,_,_).
+place(placeid(P)) :- highlow(_,_,_,_,P,_).
+
+mountain(A) :- mountain(A,_,_,_). % Rohit
+lake(A) :- lake(A,_,_). % Rohit
+
+abbreviation(stateid(State), Ab) :-
+ state(State,Ab,_,_,_,_,_,_,_,_).
+abbreviation(Ab) :- abbreviation(_,Ab).
+
+capital(stateid(State), cityid(Cap,St)) :- state(State,St,Cap,_,_,_,_,_,_,_).
+capital(Cap) :- capital(_,Cap).
+
+print_name(stateid(X),X) :- !.
+print_name(cityid(X,_), X) :- !.
+print_name(riverid(X), X) :- !.
+print_name(placeid(X), X) :- !.
+print_name(Goal, Y) :- (Goal=_/_;Goal=_*_;Goal=_+_;Goal=_-_),!, Y is Goal.
+print_name(X,X).
+
+loc(X,countryid(usa)) :-
+ city(X) ; state(X) ; river(X) ; place(X).
+loc(cityid(City,St), stateid(State)) :-
+ city(State, St, City,_).
+loc(placeid(P), stateid(S)) :- highlow(S,_,P,_,_,_). % Rohit
+loc(placeid(P), stateid(S)) :- highlow(S,_,_,_,P,_). % Rohit
+%loc(placeid(P), stateid(S)) :-
+% ( highlow(S,_,P,_,_,_) ->
+% true
+% ; highlow(S,_,_,_,P,_)
+% ).
+loc(riverid(R), stateid(S)) :-
+ river(R,_,States),
+ member(S,States).
+
+traverse(riverid(R), stateid(S)) :-
+ river(R,_,States),
+ member(S,States).
+
+high_point(countryid(usa), placeid('mount mckinley')).
+high_point(stateid(S), placeid(P)) :-
+ highlow(S,_,P,_,_,_).
+
+low_point(countryid(usa), placeid('death valley')).
+low_point(stateid(S), placeid(P)) :-
+ highlow(S,_,_,_,P,_).
+
+area(stateid(X),Areal) :-
+ state(X,_,_,_,Area,_,_,_,_,_),
+ Areal is float(Area).
+
+major(cityid(C,S)) :-
+ X = cityid(C,S),
+ city(X),
+ population(X,P),
+ P > 150000.
+major(riverid(R)) :-
+ X = riverid(R),
+ river(X),
+ len(X,L),
+ L > 750.
+
+first(G) :- (G -> true).
+
+n_solutions(N,Goal) :-
+ findall(Goal, Goal, GList0),
+ length(Solutions, N),
+ append(Solutions,_,GList0),
+ member(Goal, Solutions).
+
+nth_solution(N,Goal) :-
+ findall(Goal, Goal, GList),
+ nth(N,GList,Goal).
+
+population(stateid(X),Pop) :-
+ state(X,_,_,Pop,_,_,_,_,_,_).
+population(cityid(X,St), Pop) :-
+ city(_,St,X,Pop).
+
+len(riverid(R), L) :-
+ river(R,L,_).
+
+elevation(placeid(P),E) :- highlow(_,_,_,_,P,E). % Rohit
+elevation(placeid(P),E) :- highlow(_,_,P,E,_,_). % Rohit
+%elevation(placeid(P),E) :-
+% ( highlow(_,_,P,E,_,_)
+% ; highlow(_,_,_,_,P,E)
+% ).
+
+size(stateid(X), S) :-
+ area(stateid(X), S).
+size(cityid(X,St), S) :-
+ population(cityid(X,St), S).
+size(riverid(X), S) :-
+ len(riverid(X),S).
+size(placeid(X), S) :-
+ elevation(placeid(X),S).
+size(X,X) :-
+ number(X).
+
+next_to(stateid(X),stateid(Y)) :-
+ border(X,_,Ys),
+ member(Y,Ys).
+
+density(S,D) :-
+ population(S,P),
+ area(S,A),
+ D is P / A.
+
+largest(Var, Goal) :-
+ findall(Size-Goal, (Goal,size(Var,Size)), Pairs0),
+ max_key(Pairs0, Goal).
+
+max_key([Key-Value|Rest],Result) :-
+ max_key(Rest, Key, Value, Result).
+
+max_key([], _, Value, Value).
+max_key([K-V|T], Key, Value, Result):-
+ ( K > Key ->
+ max_key(T, K, V, Result)
+ ; max_key(T, Key, Value, Result)
+ ).
+
+smallest(Var, Goal) :-
+ findall(Size-Goal, (Goal,size(Var,Size)), Pairs0),
+ min_key(Pairs0, Goal).
+
+min_key([Key-Value|Rest],Result) :-
+ min_key(Rest, Key, Value, Result).
+
+min_key([], _, Value, Value).
+min_key([K-V|T], Key, Value, Result):-
+ ( K < Key ->
+ min_key(T, K, V, Result)
+ ; min_key(T, Key, Value, Result)
+ ).
+
+count(V,Goal,N) :-
+ findall(V,Goal,Ts),
+ sort(Ts, Unique),
+ length(Unique, N).
+
+at_least(Min,V,Goal) :-
+ count(V,N,Goal),
+ Goal, % This is a hack to instantiate N, making this order independent.
+ N >= Min.
+
+at_most(Max,V,Goal) :-
+ count(V,Goal,N),
+ N =< Max.
+
+execute_query(Query, Unique):-
+ tq(Query, answer(Var,Goal)),
+ findall(Name, (Goal, print_name(Var,Name)), Answers),
+ sort(Answers, Unique).
+%---------------------------------------------------------------------------
+tq(G,G) :-
+ var(G), !.
+tq(largest(V,Goal), largest(Vars, DVars, DV, DGoal)) :-
+ !,
+ variables_in(Goal, Vars),
+ copy_term((Vars,V,Goal),(DVars,DV,Goal1)),
+ tq(Goal1,DGoal).
+tq(smallest(V,Goal), smallest(Vars, DVars, DV, DGoal)) :-
+ !,
+ variables_in(Goal, Vars),
+ copy_term((Vars,V,Goal),(DVars,DV,Goal1)),
+ tq(Goal1,DGoal).
+tq(highest(V,Goal), highest(Vars, DVars, DV, DGoal)) :-
+ !,
+ variables_in(Goal, Vars),
+ copy_term((Vars,V,Goal),(DVars,DV,Goal1)),
+ tq(Goal1,DGoal).
+tq(most(I,V,Goal), most(Vars, DVars, DI, DV, DGoal)) :-
+ !,
+ variables_in(Goal, Vars),
+ copy_term((Vars,I,V,Goal),(DVars,DI,DV,Goal1)),
+ tq(Goal1,DGoal).
+tq(fewest(I,V,Goal), fewest(Vars, DVars, DI, DV, DGoal)) :-
+ !,
+ variables_in(Goal, Vars),
+ copy_term((Vars,I,V,Goal),(DVars,DI,DV,Goal1)),
+ tq(Goal1,DGoal).
+tq(Goal,TGoal) :-
+ functor(Goal,F,N),
+ functor(TGoal,F,N),
+ tq_args(N,Goal,TGoal).
+
+tq_args(N,Goal,TGoal) :-
+ ( N =:= 0 ->
+ true
+ ; arg(N,Goal,GArg),
+ arg(N,TGoal,TArg),
+ tq(GArg,TArg),
+ N1 is N - 1,
+ tq_args(N1,Goal,TGoal)
+ ).
+
+variables_in(A, Vs) :- variables_in(A, [], Vs).
+
+variables_in(A, V0, V) :-
+ var(A), !, add_var(V0, A, V).
+variables_in(A, V0, V) :-
+ ground(A), !, V = V0.
+variables_in(Term, V0, V) :-
+ functor(Term, _, N),
+ variables_in_args(N, Term, V0, V).
+
+variables_in_args(N, Term, V0, V) :-
+ ( N =:= 0 ->
+ V = V0
+ ; arg(N, Term, Arg),
+ variables_in(Arg, V0, V1),
+ N1 is N-1,
+ variables_in_args(N1, Term, V1, V)
+ ).
+
+add_var(Vs0, V, Vs) :-
+ ( contains_var(V, Vs0) ->
+ Vs = Vs0
+ ; Vs = [V|Vs0]
+ ).
+
+
+contains_var(Variable, Term) :-
+ \+ free_of_var(Variable, Term).
+
+% free_of_var(+Variable, +Term)
+% is true when the given Term contains no sub-term identical to the
+% given Variable (which may actually be any term, not just a var).
+% For variables, this is precisely the "occurs check" which is
+% needed for sound unification.
+
+free_of_var(Variable, Term) :-
+ Term == Variable,
+ !,
+ fail.
+free_of_var(Variable, Term) :-
+ compound(Term),
+ !,
+ functor(Term, _, Arity),
+ free_of_var(Arity, Term, Variable).
+free_of_var(_, _).
+
+free_of_var(1, Term, Variable) :- !,
+ arg(1, Term, Argument),
+ free_of_var(Variable, Argument).
+free_of_var(N, Term, Variable) :-
+ arg(N, Term, Argument),
+ free_of_var(Variable, Argument),
+ M is N-1, !,
+ free_of_var(M, Term, Variable).
+
+%---------------------------------------------------------------------------
+/*
+execute_query(answer(Var, Goal), Unique) :-
+ findall(Name,(Goal,print_name(Var,Name)),Answers),
+ sort(Answers,Unique).
+*/
+answer(Var, Goal) :-
+ nl,nl,
+ findall(Name,(Goal,print_name(Var,Name)),Answers),
+ sort(Answers,Unique),
+ format('Answer = ~w~n',[Unique]).
+
+sum(V, Goal, X) :-
+ findall(V, Goal, Vs),
+ sumlist(Vs, 0, X).
+
+highest(Vars, DVars, DV, Goal) :-
+ highest(DV, Goal), !,
+ Vars = DVars.
+
+highest(X, Goal) :-
+ largest(Y, (Goal, elevation(X,Y))).
+/*CAT. bug
+lowest(X,Goal) :-
+ largest(Y, (Goal, elevation(X,Y))).
+*/
+lowest(X,Goal) :-
+ smallest(Y, (Goal, elevation(X,Y))).
+
+shortest(X,Goal) :-
+ smallest(Y, (Goal, len(X,Y))).
+
+longest(X,Goal) :-
+ largest(Y, (Goal, len(X,Y))).
+
+
+higher(X,Y) :-
+ elevation(X,EX),
+ elevation(Y,EY),
+ EX > EY.
+
+%---------------------------------
+%CAT added
+lower(X, Y) :-
+ elevation(X,EX),
+ elevation(Y,EY),
+ EX < EY.
+
+longer(X, Y) :-
+ len(X,LX),
+ len(Y, LY),
+ LX > LY.
+
+shorter(X, Y) :-
+ len(X,LX),
+ len(Y, LY),
+ LX < LY.
+
+more(X, Y) :-
+ X > Y.
+%---------------------------------
+
+divide(X,Y, X/Y).
+multiply(X,Y,X*Y).
+add(X,Y,X+Y).
+%subtract(X,Y,X-Y).
+
+sumlist([], Sum, Sum).
+sumlist([V|Vs], Sum0, Sum) :-
+ Sum1 is Sum0 + V,
+ sumlist(Vs, Sum1, Sum).
+
+const(V, V).
+
+largest(Vars, DVars, DV, DGoal) :-
+ largest(DV, DGoal),!,
+ Vars = DVars.
+
+smallest(Vars, DVars, DV, DGoal) :-
+ smallest(DV, DGoal),!,
+ Vars = DVars.
+
+most(Vars, DVars, DI, DV, DGoal) :-
+ most(DI, DV, DGoal),!,
+ Vars = DVars.
+
+fewest(Vars, DVars, DI, DV, DGoal) :-
+ fewest(DI, DV, DGoal),!,
+ Vars = DVars.
+
+most(Index,Var,Goal) :-
+ setof(Index-Var, Goal, Solutions),
+ keysort(Solutions, Collect),
+ maximum_run(Collect, Index).
+
+maximum_run(Solutions, Index) :-
+ maximum_run(Solutions, foo, 0, Index).
+
+maximum_run([], Index, _Count, Index) :- !.
+maximum_run([Index1-_|Rest], BestIndex0, Count0, BestIndex) :-
+ first_run(Rest, Index1, 1, Count1, Rest1),
+ ( Count1 > Count0 ->
+ BestIndex2 = Index1,
+ Count2 = Count1
+ ; BestIndex2 = BestIndex0,
+ Count2 = Count0
+ ),
+ maximum_run(Rest1, BestIndex2, Count2, BestIndex).
+
+first_run([], _Index, N, N, []).
+first_run([Index-G|Rest0], Target, N0, N, Rest) :-
+ ( Target = Index ->
+ N1 is N0 + 1,
+ first_run(Rest0, Target, N1, N, Rest)
+ ; N = N0,
+ Rest = [Index-G|Rest0]
+ ).
+
+fewest(Index,Var,Goal) :-
+ setof(Index-Var, Goal, Solutions),
+ keysort(Solutions, Collect),
+ minimum_run(Collect, Index).
+
+minimum_run(Solutions, Index) :-
+ minimum_run(Solutions, foo, 1000, Index).
+
+minimum_run([], Index, _Count, Index) :- !.
+minimum_run([Index1-_|Rest], BestIndex0, Count0, BestIndex) :-
+ first_run(Rest, Index1, 1, Count1, Rest1),
+ ( Count1 < Count0 ->
+ BestIndex2 = Index1,
+ Count2 = Count1
+ ; BestIndex2 = BestIndex0,
+ Count2 = Count0
+ ),
+ minimum_run(Rest1, BestIndex2, Count2, BestIndex).
+
+