summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimotej Lazar <timotej.lazar@fri.uni-lj.si>2016-01-04 18:20:30 +0100
committerTimotej Lazar <timotej.lazar@fri.uni-lj.si>2016-01-04 18:20:30 +0100
commit1d6df08059f1b15292237a4fe6a80bca696adfd7 (patch)
tree443fb1e9f3b36b2253bf16718fdd1a544239c1ff
parentb76536f4d15f605576dbab139038880e8afa2762 (diff)
Add function to find predicates used by a Prolog program
-rw-r--r--prolog/util.py18
1 files changed, 18 insertions, 0 deletions
diff --git a/prolog/util.py b/prolog/util.py
index 11a6f94..e69d4d5 100644
--- a/prolog/util.py
+++ b/prolog/util.py
@@ -224,6 +224,24 @@ def map_vars(a, b, tokens, variables):
return [t if t.type != 'VARIABLE' else t.clone(val=mapping[t.val]) for t in b]
+# Return a set of predicate names (e.g. conc/3) used in [program].
+def used_predicates(program):
+ predicates = set()
+ def walk(tree):
+ if isinstance(tree, Tree):
+ if tree.label() == 'term' and len(tree) >= 3 and \
+ isinstance(tree[0], Tree) and tree[0].label() == 'functor':
+ if len(tree) == 3:
+ predicates.add('{}/0'.format(tree[0][0]))
+ else:
+ predicates.add('{}/{}'.format(tree[0][0], (len(tree[2])+1)//2))
+ for subtree in tree:
+ walk(subtree)
+ tree = parse(program)
+ if tree is not None:
+ walk(tree)
+ return predicates
+
# Basic sanity check.
if __name__ == '__main__':
var_names = {}