go ("&",2) = Just $ PDBS $ infixOp "&"
go ("%",2) = Just $ PDBS $ infixOp "%"
go ("+",2) = Just $ PDBS $ infixOp "+"
-
- go ("mod",2) = Just $ PDBS $ infixOp "%"
- go ("abs",1) = Just $ PDBS $ call "abs"
- go ("log",1) = Just $ PDBS $ call "log"
- go ("exp",1) = Just $ PDBS $ call "exp"
-
-
-
- go ("pycall", _) = Just $ PDBS $ call "pycall"
- go ("getattr", 2) = Just $ PDBS $ call "getattr"
-
-
+ go ("mod",2) = Just $ PDBS $ infixOp "%"
+ go ("abs",1) = Just $ PDBS $ call "abs"
+
+ go ("log",_) = Just $ PDBS $ call "log"
+ go ("exp",1) = Just $ PDBS $ call "exp"
+ go ("sqrt",1) = Just $ PDBS $ call "sqrt"
+ go ("split", _) = Just $ PDBS $ call "split"
+ go ("float", _) = Just $ PDBS $ call "float"
+ go ("int", _) = Just $ PDBS $ call "int"
+ go ("pycall", _) = Just $ PDBS $ call "pycall"
+ go ("getattr", _) = Just $ PDBS $ call "getattr"
go ("uniform", _) = Just $ PDBS $ call "uniform"
go ("<=",2) = Just $ PDBS $ infixOp "<="
driver am um is bc qp pr fh = do
hPutStrLn fh "from __future__ import division"
+ hPutStrLn fh "from stdlib import *"
-- Parser resume state
hPutStrLn fh "parser_state = \"\"\""
+import re
from term import Term, Cons, Nil
try:
def uniform(a=0, b=1):
return _random() * (b - a) + a
-import re
def split(s, delim='\s+'):
- return re.split(delim, s)
+ return _todynalist(re.split(delim, s))
-# used as a work around to bring arbitrary python functions into dyna
def pycall(name, *args):
+ """
+ Temporary foreign function interface - call Python functions from dyna!
+ """
x = eval(name)(*args)
if isinstance(x, list):
- return todynalist(x)
+ return _todynalist(x)
return x
-def todynalist(x):
+def _todynalist(x):
if not x:
return Nil
- return Cons(x[0], todynalist(x[1:]))
+ return Cons(x[0], _todynalist(x[1:]))
% binary rules
rewrite(X, Y, Z) :=
rules_tsv(Linenum, Cost, X, R),
- [Y, Z] is pycall("split", R, "\\s+"),
- pycall("float", Cost).
+ [Y, Z] is split(R, "\\s+"),
+ float(Cost).
% load unary rules
rewrite(X, Y) :=
rules_tsv(Linenum, Cost, X, R),
- [Y] is pycall("split", R, "\\s+"),
- pycall("float", Cost).
+ [Y] is split(R, "\\s+"),
+ float(Cost).
phrase(S,X,I,K) += phrase(S,Y,I,K) + rewrite(X,Y).
phrase(S,X,I,K) += phrase(S,Y,I,J) + phrase(S,Z,J,K) + rewrite(X,Y,Z).