2014-03-25 01:32:24 +04:00
|
|
|
# General functions
|
|
|
|
|
2014-04-03 07:23:37 +04:00
|
|
|
def _equal_Q(a, b):
|
2014-03-25 01:32:24 +04:00
|
|
|
ota, otb = type(a), type(b)
|
2014-04-03 07:23:37 +04:00
|
|
|
if not (ota == otb or (_sequential_Q(a) and _sequential_Q(b))):
|
2014-03-25 01:32:24 +04:00
|
|
|
return False;
|
2014-04-03 07:23:37 +04:00
|
|
|
if _symbol_Q(a):
|
2014-03-25 01:32:24 +04:00
|
|
|
return a == b
|
2014-04-03 07:23:37 +04:00
|
|
|
elif _list_Q(a) or _vector_Q(a):
|
2014-03-25 01:32:24 +04:00
|
|
|
if len(a) != len(b): return False
|
|
|
|
for i in range(len(a)):
|
2014-04-03 07:23:37 +04:00
|
|
|
if not _equal_Q(a[i], b[i]): return False
|
2014-03-25 01:32:24 +04:00
|
|
|
return True
|
2014-04-03 07:23:37 +04:00
|
|
|
elif _hash_map_Q(a):
|
2014-03-25 01:32:24 +04:00
|
|
|
akeys = a.keys()
|
|
|
|
akeys.sort()
|
|
|
|
bkeys = b.keys()
|
|
|
|
bkeys.sort()
|
|
|
|
if len(akeys) != len(bkeys): return False
|
|
|
|
for i in range(len(akeys)):
|
|
|
|
if akeys[i] != bkeys[i]: return False
|
|
|
|
if not equal_Q(a[akeys[i]], b[bkeys[i]]): return False
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return a == b
|
|
|
|
|
2014-04-03 07:23:37 +04:00
|
|
|
def _sequential_Q(seq): return _list_Q(seq) or _vector_Q(seq)
|
2014-03-25 01:32:24 +04:00
|
|
|
|
2014-04-03 07:23:37 +04:00
|
|
|
# Scalars
|
|
|
|
def _nil_Q(exp): return exp is None
|
|
|
|
def _true_Q(exp): return exp is True
|
|
|
|
def _false_Q(exp): return exp is False
|
|
|
|
def _string_Q(exp): return type(exp) in [str, unicode]
|
2014-03-25 01:32:24 +04:00
|
|
|
|
2014-04-03 07:23:37 +04:00
|
|
|
# Symbols
|
2014-03-25 01:32:24 +04:00
|
|
|
class Symbol(str): pass
|
2014-04-03 07:23:37 +04:00
|
|
|
def _symbol(str): return Symbol(str)
|
|
|
|
def _symbol_Q(exp): return type(exp) == Symbol
|
2014-03-25 01:32:24 +04:00
|
|
|
|
2014-04-03 07:23:37 +04:00
|
|
|
# Functions
|
2014-04-10 08:58:27 +04:00
|
|
|
def _function(Eval, Env, ast, env, params):
|
|
|
|
def gen_fn():
|
|
|
|
def fn(*args):
|
|
|
|
return Eval(ast, Env(env, params, args))
|
|
|
|
fn.__meta__ = None
|
|
|
|
fn.__ast__ = ast
|
|
|
|
fn.__gen_env__ = lambda args: Env(env, params, args)
|
|
|
|
fn.__copy__ = gen_fn
|
|
|
|
return fn
|
|
|
|
return gen_fn()
|
2014-04-03 07:23:37 +04:00
|
|
|
def _function_Q(f): return type(f) == type(function_Q)
|
2014-03-25 01:32:24 +04:00
|
|
|
|
|
|
|
# lists
|
|
|
|
class List(list):
|
|
|
|
def __add__(self, rhs): return List(list.__add__(self, rhs))
|
|
|
|
def __getitem__(self, i):
|
|
|
|
if type(i) == slice: return List(list.__getitem__(self, i))
|
|
|
|
elif i >= len(self): return None
|
|
|
|
else: return list.__getitem__(self, i)
|
|
|
|
def __getslice__(self, *a): return List(list.__getslice__(self, *a))
|
2014-04-03 07:23:37 +04:00
|
|
|
def _list(*vals): return List(vals)
|
|
|
|
def _list_Q(exp): return type(exp) == List
|
2014-03-25 01:32:24 +04:00
|
|
|
|
|
|
|
|
|
|
|
# vectors
|
|
|
|
class Vector(list):
|
|
|
|
def __add__(self, rhs): return Vector(list.__add__(self, rhs))
|
|
|
|
def __getitem__(self, i):
|
|
|
|
if type(i) == slice: return Vector(list.__getitem__(self, i))
|
|
|
|
elif i >= len(self): return None
|
|
|
|
else: return list.__getitem__(self, i)
|
|
|
|
def __getslice__(self, *a): return Vector(list.__getslice__(self, *a))
|
2014-04-03 07:23:37 +04:00
|
|
|
def _vector(*vals): return Vector(vals)
|
|
|
|
def _vector_Q(exp): return type(exp) == Vector
|
2014-03-25 01:32:24 +04:00
|
|
|
|
2014-04-03 07:23:37 +04:00
|
|
|
# Hash maps
|
|
|
|
class Hash_Map(dict): pass
|
|
|
|
def _hash_map(*key_vals):
|
|
|
|
hm = Hash_Map()
|
|
|
|
for i in range(0,len(key_vals),2): hm[key_vals[i]] = key_vals[i+1]
|
|
|
|
return hm
|
|
|
|
def _hash_map_Q(exp): return type(exp) == Hash_Map
|
2014-03-25 01:32:24 +04:00
|
|
|
|
|
|
|
# atoms
|
|
|
|
class Atom(object):
|
|
|
|
def __init__(self, val):
|
|
|
|
self.val = val
|
2014-04-03 07:23:37 +04:00
|
|
|
def _atom(val): return Atom(val)
|
|
|
|
def _atom_Q(exp): return type(exp) == Atom
|