From 5f04f17907ce37a8e8dd6cff9cb6dc026b30b8f6 Mon Sep 17 00:00:00 2001 From: Patrick Simianer Date: Fri, 25 Apr 2014 18:21:11 +0200 Subject: better readme, example and code --- data/geoquery/README | 3 +- data/geoquery/smt-semparse/decode_sentence.py | 36 --- data/geoquery/smt-semparse/functionalizer.py | 143 --------- data/geoquery/smt-semparse/moses.py | 173 ----------- data/geoquery/wasp-1.0/eval.pl | 374 ++++++++++++++++++++++++ data/geoquery/wasp-1.0/geoquery.pl | 403 ++++++++++++++++++++++++++ data/geoquery/wasp/eval.pl | 374 ------------------------ data/geoquery/wasp/geoquery.pl | 403 -------------------------- 8 files changed, 778 insertions(+), 1131 deletions(-) delete mode 100644 data/geoquery/smt-semparse/decode_sentence.py delete mode 100644 data/geoquery/smt-semparse/functionalizer.py delete mode 100644 data/geoquery/smt-semparse/moses.py create mode 100644 data/geoquery/wasp-1.0/eval.pl create mode 100644 data/geoquery/wasp-1.0/geoquery.pl delete mode 100644 data/geoquery/wasp/eval.pl delete mode 100644 data/geoquery/wasp/geoquery.pl (limited to 'data') diff --git a/data/geoquery/README b/data/geoquery/README index 8e147fa..05067ee 100644 --- a/data/geoquery/README +++ b/data/geoquery/README @@ -13,6 +13,5 @@ split880.train.ids : 880 train/test split train ids ../stopwords.en : English stopwords file ../weights.init : initial weights -smt-semparse/ : slightly adapted code for Andreas' smt-semparse -wasp/ : adapted stuff from wasp-1.0 +wasp-1.0/ : modified stuff from wasp-1.0 diff --git a/data/geoquery/smt-semparse/decode_sentence.py b/data/geoquery/smt-semparse/decode_sentence.py deleted file mode 100644 index 1914734..0000000 --- a/data/geoquery/smt-semparse/decode_sentence.py +++ /dev/null @@ -1,36 +0,0 @@ -import sys -import os -import tempfile, shutil -from src.extractor import Extractor -from src.smt_semparse_config import SMTSemparseConfig -from src.moses import Moses -from src.functionalizer import Functionalizer - -#input: English sentence -if __name__ == '__main__': - sentence = '' - if len(sys.argv) == 3: - experiment_dir = sys.argv[1] - sentence = sys.argv[2] - else: - assert False - - # load config - config = SMTSemparseConfig('/workspace/grounded/smt-semparse-cp/settings.yaml', '/workspace/grounded/smt-semparse-cp/dependencies.yaml') - - #stem - sentence = Extractor(config).preprocess_nl(sentence) - - # we need a temp dir! - temp_dir = tempfile.mkdtemp() - - #decode - moses = Moses(config) - moses.decode_sentence(experiment_dir, sentence, temp_dir) - - #convert to bracket structure - print Functionalizer(config).run_sentence(experiment_dir, temp_dir) - - #delete tmp files - shutil.rmtree(temp_dir) - diff --git a/data/geoquery/smt-semparse/functionalizer.py b/data/geoquery/smt-semparse/functionalizer.py deleted file mode 100644 index 782b4e5..0000000 --- a/data/geoquery/smt-semparse/functionalizer.py +++ /dev/null @@ -1,143 +0,0 @@ -import logging -import util -import sys - -class Functionalizer: - - def __init__(self, config): - self.config = config - - def run(self): - hyp_file = open('%s/hyp.mrl.nbest' % self.config.experiment_dir) - fun_file = open('%s/hyp.fun' % self.config.experiment_dir, 'w') - - hypsets = [] - hypset = [] - last_eid = 0 - for line in hyp_file: - parts = line.split('|||') - eid = int(parts[0]) - if eid != last_eid: - hypsets.append(hypset) - hypset = [] - last_eid = eid - score = parts[2] + ' ||| ' + parts[3].strip() - hyp = parts[1].strip() - hypset.append((hyp,score)) - hypsets.append(hypset) - - counter = 0 - for hypset in hypsets: - hypset = list(reversed(hypset)) - while hypset: - hyp, score = hypset.pop() - fun = self.functionalize(hyp) - if fun: - print >>fun_file, counter, '|||', fun, '|||', score - break - counter += 1 - - def run_sentence(self, experiment_dir, temp_dir): - hyp_file = open('%s/nbest.tmp' % temp_dir, 'r') - - hypsets = [] - hypset = [] - last_eid = 0 - for line in hyp_file: - parts = line.split('|||') - eid = int(parts[0]) - if eid != last_eid: - hypsets.append(hypset) - hypset = [] - last_eid = eid - score = parts[2] + ' ||| ' + parts[3].strip() - hyp = parts[1].strip() - hypset.append((hyp,score)) - hypsets.append(hypset) - hyp_file.close() - - counter = 0 - for hypset in hypsets: - hypset = list(reversed(hypset)) - while hypset: - hyp, score = hypset.pop() - fun = self.functionalize(hyp) - if fun: - return fun - break - counter += 1 - return "" - - #xc = 0 - def functionalize(self, mrl): - - #if '_@0' in mrl and 'cityid@2' in mrl: - # #print '===' - # #print mrl - # self.xc += 1 - # if self.xc > 5: - # exit() - - stack = [] - r = [] - tokens = list(reversed(mrl.split())) - - #print tokens - - while tokens: - it = tokens.pop() - #print it - if util.ARITY_SEP not in it: - token = it - arity = util.ARITY_STR - logging.warn('unrecognized token: %s', it) - else: - token, arity = it.rsplit(util.ARITY_SEP) - if arity == util.ARITY_STR: - arity = 0 - arity_str = True - elif not (arity == util.ARITY_ANY): - arity = int(arity) - arity_str = False - - if arity == util.ARITY_ANY or arity > 0: - r.append(token) - r.append('(') - stack.append(arity) - else: - assert arity == 0 - if arity_str: - r.append("'%s'" % token.replace('_', ' ')) - else: - r.append(token) - #print r - while stack: - top = stack.pop() - if top == util.ARITY_ANY and tokens: - r.append(',') - stack.append(util.ARITY_ANY) - break - elif top != util.ARITY_ANY and top > 1: - r.append(',') - stack.append(top - 1) - break - else: - r.append(')') - - if not stack and tokens: - return None - - if stack: - return None - - r = ''.join(r) - - # nasty hacks to fix misplaced _ - if '(_' in r: - return None - if ',_' in r and not ('cityid' in r): - return None - if '_),_)' in r: - return None - - return r diff --git a/data/geoquery/smt-semparse/moses.py b/data/geoquery/smt-semparse/moses.py deleted file mode 100644 index 9a159c3..0000000 --- a/data/geoquery/smt-semparse/moses.py +++ /dev/null @@ -1,173 +0,0 @@ -import logging -import os -import subprocess -import gzip - -from subprocess import Popen, PIPE, STDOUT - -class Moses: - - def __init__(self, config): - self.config = config - - def run_train(self): - args = [self.config.moses_train, - '--root-dir', self.config.experiment_dir, - '--corpus', '%s/%s' % (self.config.experiment_dir, - self.config.train_name), - '--f', self.config.src, - '--e', self.config.tgt, - '--lm', '0:3:%s/%s.arpa' % (self.config.experiment_dir, self.config.tgt), - #'-score-options', "'--OnlyDirect --NoPhraseCount'" - '--alignment', self.config.symm, - '-external-bin-dir', self.config.giza] - if self.config.model == 'hier': - args += ['-hierarchical', '-glue-grammar'] - - logging.info(' '.join(args)) - - log = open('%s/train.log' % self.config.experiment_dir, 'w') - p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=log) - p.wait() - log.close() - - def run_retrain(self): - old_train_nl = '%s/%s.nl' % (self.config.experiment_dir, - self.config.train_name) - old_train_mrl = '%s/%s.mrl' % (self.config.experiment_dir, - self.config.train_name) - moved_train_nl = '%s.notune' % old_train_nl - moved_train_mrl = '%s.notune' % old_train_mrl - tune_nl = '%s/tune.nl' % self.config.experiment_dir - tune_mrl = '%s/tune.mrl' % self.config.experiment_dir - os.rename(old_train_nl, moved_train_nl) - os.rename(old_train_mrl, moved_train_mrl) - with open(old_train_nl, 'w') as rt_train_nl: - subprocess.call(['cat', moved_train_nl, tune_nl], stdout=rt_train_nl) - with open(old_train_mrl, 'w') as rt_train_mrl: - subprocess.call(['cat', moved_train_mrl, tune_mrl], stdout=rt_train_mrl) - - os.remove('%s/model/extract.inv.gz' % self.config.experiment_dir) - os.remove('%s/model/extract.gz' % self.config.experiment_dir) - if self.config.model == 'hier': - os.remove('%s/model/rule-table.gz' % self.config.experiment_dir) - else: - os.remove('%s/model/phrase-table.gz' % self.config.experiment_dir) - - self.run_train() - - def parens_ok(self, line): - mrl_part = line.split(' ||| ')[1] - tokens = [t[-1] for t in mrl_part.split() if t[-2] == '@'] - tokens.reverse() - stack = [] - while tokens: - t = tokens.pop() - assert t != '*' - if t == 's': - t = 0 - t = int(t) - if t > 0: - stack.append(t) - else: - while stack: - top = stack.pop() - if top > 1: - stack.append(top - 1) - break - if tokens and not stack: - return False - return True - - def filter_phrase_table(self): - table_name = 'phrase' if self.config.model == 'phrase' else 'rule' - oldname = '%s/model/%s-table.gz' % (self.config.experiment_dir, table_name) - newname = '%s/model/%s-table.old.gz' % (self.config.experiment_dir, table_name) - os.rename(oldname, newname) - - with gzip.open(oldname, 'w') as filtered_table_f: - with gzip.open(newname, 'r') as old_table_f: - for line in old_table_f: - if self.parens_ok(line): - print >>filtered_table_f, line, - - def run_tune(self): - wd = os.getcwd() - os.chdir(self.config.experiment_dir) - args = [self.config.moses_tune, - '%s/tune.%s' % (self.config.experiment_dir, self.config.src), - '%s/tune.%s' % (self.config.experiment_dir, self.config.tgt)] - if self.config.model == 'hier': - args += [self.config.moses_decode_hier] - else: - args += [self.config.moses_decode_phrase] - args += ['%s/model/moses.ini' % self.config.experiment_dir, - '--mertdir', '%s/bin' % self.config.moses] - if self.config.model == 'hier': - args += ['--filtercmd', - '%s/scripts/training/filter-model-given-input.pl --Hierarchical'\ - % self.config.moses] - - log = open('%s/tune.log' % self.config.experiment_dir, 'w') - p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=log) - p.wait() - log.close() - os.chdir(wd) - - def run_decode(self): - if self.config.model == 'phrase': - args = [self.config.moses_decode_phrase] - elif self.config.model == 'hier': - args = [self.config.moses_decode_hier] - else: - assert False - - if self.config.run == 'test' or self.config.run == 'all': - args += ['-f', '%s/mert-work/moses.ini' % self.config.experiment_dir] - else: - args += ['-f', '%s/model/moses.ini' % self.config.experiment_dir] - #args += ['-f', '%s/model/moses.ini' % self.config.experiment_dir] - - args += ['-drop-unknown', - '-n-best-list', '%s/hyp.%s.nbest' % (self.config.experiment_dir, self.config.tgt), - str(self.config.nbest), 'distinct', - '-threads', '3'] - - #nullfile = open(os.devnull, 'w') - infile = open('%s/test.%s' % (self.config.experiment_dir, self.config.src)) - outfile = open('%s/hyp.%s' % (self.config.experiment_dir, self.config.tgt), 'w') - log = open('%s/decode.log' % self.config.experiment_dir, 'w') - p = subprocess.Popen(args, stdin=infile, stdout=outfile, stderr=log) - p.wait() - infile.close() - log.close() - outfile.close() - - def decode_sentence(self, experiment_dir, sentence, temp_dir): - if self.config.model == 'phrase': - args = [self.config.moses_decode_phrase] - elif self.config.model == 'hier': - args = [self.config.moses_decode_hier] - else: - assert False - - if self.config.run == 'test' or self.config.run == 'all': - args += ['-f', '%s/mert-work/moses.ini' % experiment_dir] - else: - args += ['-f', '%s/model/moses.ini' % experiment_dir] - - args += ['-drop-unknown', - '-n-best-list', '%s/nbest.tmp' % temp_dir, - str(self.config.nbest), 'distinct', - '-threads', '1'] - - infile = open('%s/sent.tmp' % temp_dir, 'w') - print >>infile, sentence - infile.close - infile = open('%s/sent.tmp' % temp_dir, 'r') - nullfile = open(os.devnull, 'w') - p = subprocess.Popen(args, stdin=infile, stdout=nullfile, stderr=nullfile) - p.wait() - infile.close() - return - diff --git a/data/geoquery/wasp-1.0/eval.pl b/data/geoquery/wasp-1.0/eval.pl new file mode 100644 index 0000000..e00a067 --- /dev/null +++ b/data/geoquery/wasp-1.0/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-1.0/geoquery.pl b/data/geoquery/wasp-1.0/geoquery.pl new file mode 100644 index 0000000..5d5d9bc --- /dev/null +++ b/data/geoquery/wasp-1.0/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). + + diff --git a/data/geoquery/wasp/eval.pl b/data/geoquery/wasp/eval.pl deleted file mode 100644 index e00a067..0000000 --- a/data/geoquery/wasp/eval.pl +++ /dev/null @@ -1,374 +0,0 @@ -:- 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 deleted file mode 100644 index 5d5d9bc..0000000 --- a/data/geoquery/wasp/geoquery.pl +++ /dev/null @@ -1,403 +0,0 @@ -:- 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). - - -- cgit v1.2.3