summaryrefslogtreecommitdiff
path: root/prolog
diff options
context:
space:
mode:
authorTimotej Lazar <timotej.lazar@araneo.org>2015-02-10 00:10:12 +0100
committerAleš Smodiš <aless@guru.si>2015-08-11 14:26:02 +0200
commit4204f59b524a447b49dc067d142600e347cc7d88 (patch)
tree9eb8385a6fdc1ab88731b80160f374ba83784bfa /prolog
parent12c1e264dbb8ec979c17da362ff120a3fa87039c (diff)
Move normalize to prolog.util
Diffstat (limited to 'prolog')
-rw-r--r--prolog/util.py11
1 files changed, 11 insertions, 0 deletions
diff --git a/prolog/util.py b/prolog/util.py
index e5a93e2..48e3345 100644
--- a/prolog/util.py
+++ b/prolog/util.py
@@ -156,6 +156,17 @@ def rename_vars(tokens, names=None):
tokens[i] = Token('VARIABLE', names[cur_name])
return tokens
+# Helper function to remove trailing punctuation from lines and rename
+# variables to A1,A2,A3,… (potentially using [var_names]). Return a tuple.
+def normalized(line, var_names=None):
+ # Remove trailing punctuation.
+ i = len(line)
+ while i > 0:
+ if line[i-1].type not in ('COMMA', 'PERIOD', 'SEMI'):
+ break
+ i -= 1
+ return tuple(rename_vars(line[:i], var_names))
+
# transformation = before → after; applied on line which is part of rule
# return mapping from formal vars in before+after to actual vars in rule
# line and rule should of course not be normalized