Commits

Carl Friedrich Bolz committed 76ccfa1

get rid of the standardizer functionality, because copy_standardize_apart does
things better (creates VarInTerms, when appropriate) and isn't really slower.

Comments (0)

Files changed (3)

prolog/interpreter/function.py

         body = self.body
         if body is None:
             return None
-        return self.body_standardizer.make_shaped_callable(env, heap)
+        return self.body_standardizer.make_shaped_callable(heap, env)
 
     def __repr__(self):
         if self.body is None:

prolog/interpreter/shape.py

 def make_standardizer(w_obj):
     memo = []
     shape = term_with_numbered_vars_to_shape(w_obj, memo)
-    return Standardizer(shape, memo[:])
+    if isinstance(shape, SharingShape):
+        w_obj = ShapedCallable(shape, memo[:])
+    elif isinstance(shape, WrapShape):
+        w_obj = shape.w_obj
+    else:
+        assert 0, "should be unreachable"
+    return Standardizer(w_obj)
 
 def term_with_numbered_vars_to_shape(w_obj, memo):
     from prolog.interpreter import term
     if isinstance(w_obj, term.NumberedVar):
-        memo.append(w_obj.num)
+        memo.append(w_obj)
         return InStorageShape.build()
     elif isinstance(w_obj, term.Callable):
         argshapes = [term_with_numbered_vars_to_shape(w_arg, memo)
     return WrapShape(w_obj)
 
 class Standardizer(object):
-    _immutable_fields_ = ["shape", "memo[*]"]
-    def __init__(self, shape, memo):
-        self.shape = shape
-        self.memo = memo
+    _immutable_fields_ = ["w_obj"]
+    def __init__(self, w_obj):
+        self.w_obj = w_obj
 
-    @jit.unroll_safe
-    def make_shaped_callable(self, env, heap):
-        storage = [None] * len(self.memo)
-        for i in range(len(self.memo)):
-            index = self.memo[i]
-            if index < 0:
-                # XXX introduce an UnsharedVar class?
-                obj = heap.newvar()
-            else:
-                obj = env[index]
-                if obj is None:
-                    # XXX use newvar_in_term?
-                    obj = env[index] = heap.newvar()
-            storage[i] = obj
-        return ShapedCallable.build(self.shape, storage)
+    def make_shaped_callable(self, heap, env):
+        return self.w_obj.copy_standardize_apart(heap, env)
 
 # _____________________________________________________________________
 

prolog/interpreter/test/test_shape.py

                                       term.NumberedVar(0),
                                       term.NumberedVar(1)])
     std = shape.make_standardizer(w_obj)
-    s = std.shape
+    s = std.w_obj.shape
     assert isinstance(s, shape.SharingShape)
     assert s.children[0].w_obj.signature().name == "a"
     assert s.children[1].w_obj.num == 12
     assert isinstance(s.children[2], shape.InStorageShape)
     assert isinstance(s.children[3], shape.InStorageShape)
-    w_obj = std.make_shaped_callable([4, 5], None)
+    w_obj = std.make_shaped_callable(None, [4, 5])
     assert w_obj.get_full_storage() == [4, 5]
 
 
     w_obj = term.Callable.build("f", [term.Callable.build("a"),
                                       term.Number(12)])
     std = shape.make_standardizer(w_obj)
-    s = std.shape
-    assert isinstance(s, shape.WrapShape)
-    assert s.w_obj.signature().name == "f"
-    w_obj = std.make_shaped_callable([], None)
-    assert w_obj is s.w_obj
+    w_obj = std.make_shaped_callable(None, [])
+    assert w_obj is std.w_obj
 
     w_obj = term.Callable.build("f", [term.NumberedVar(0),
                                       term.NumberedVar(0),
                                       term.NumberedVar(1),
                                       term.NumberedVar(-1)])
     std = shape.make_standardizer(w_obj)
-    s = std.shape
+    s = std.w_obj.shape
     assert s.signature.name == "f"
     assert isinstance(s.children[0], shape.InStorageShape)
     assert isinstance(s.children[1], shape.InStorageShape)
     assert isinstance(s.children[2], shape.InStorageShape)
     assert isinstance(s.children[3], shape.InStorageShape)
-    assert std.memo == [0, 0, 1, -1]
     class FakeHeap(object):
         def newvar(self):
             return 7
-    w_obj = std.make_shaped_callable([4, 5], FakeHeap())
+        def newvar_in_term(self, parent, index):
+            return 7
+    w_obj = std.make_shaped_callable(FakeHeap(), [4, 5])
     assert w_obj.get_full_storage() == [4, 4, 5, 7]
-    w_obj = std.make_shaped_callable([4, None], FakeHeap())
+    w_obj = std.make_shaped_callable(FakeHeap(), [4, None])
     assert w_obj.get_full_storage() == [4, 4, 7, 7]
 
     class FakeHeap(object):
         def newvar(self):
             return object()
-    w_obj = std.make_shaped_callable([None, None], FakeHeap())
+        def newvar_in_term(self, parent, index):
+            return 7
+    w_obj = std.make_shaped_callable(FakeHeap(), [None, None])
     assert w_obj.get_storage(0) is w_obj.get_storage(1)
 
 def test_replace():
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.