Commits

Carl Friedrich Bolz committed aa68405

also do compression when standardizing apart a term

  • Participants
  • Parent commits 95306a0
  • Branches compress-terms2

Comments (0)

Files changed (2)

File prolog/interpreter/shape.py

             result.set_storage(i, cloned)
         if newinstance:
             if not needmutable:
-                return result._make_immutable()
-            return result
+                result = result._make_immutable()
+            return result.compress()
         else:
             return self
 
         return None
 
     @staticmethod
-    @jit.unroll_safe
     def build(shape, storage):
         if isinstance(shape, WrapShape):
             assert not storage
             return shape.w_obj
         result = ShapedCallable(shape, storage)
+        return result.compress()
+
+    @jit.unroll_safe
+    def compress(self):
         i = 0
-        while i < result.size_storage():
-            child = result.get_storage(i)
-            newresult = result.replace_child(i, child)
+        while i < self.size_storage():
+            child = self.get_storage(i)
+            newresult = self.replace_child(i, child)
             if not newresult:
                 i += 1
             else:
-                result = newresult
-        assert result.get_shape().num_storage_vars() == result.size_storage()
-        return result
+                self = newresult
+        assert self.get_shape().num_storage_vars() == self.size_storage()
+        return self
 
     _dot = _dot
 

File prolog/interpreter/test/test_shape.py

 import py
-from prolog.interpreter import shape, term, signature
+from prolog.interpreter import shape, term, signature, heap
 from prolog.interpreter.continuation import view
 
 class FakeShapedCallable(object):
     assert c1.get_full_storage() == [b, nil, a]
 
 def test_replace_child_fixup_varinterm_at_end():
-    from prolog.interpreter.heap import Heap
-    h = Heap()
+    h = heap.Heap()
     sig = signature.Signature.getsignature(".", 3)
     build = shape.SharingShape
     X = shape.InStorageShape.build()
 
 
 def test_replace_child_fixup_varinterm_from_replacement():
-    from prolog.interpreter.heap import Heap
-    h = Heap()
+    h = heap.Heap()
     sig = signature.Signature.getsignature(".", 3)
     build = shape.SharingShape
     X = shape.InStorageShape.build()
     c4 = shape.ShapedCallable.build(s1, [4, c2])
     assert c4.get_full_storage() == [4, 3, 2]
 
+def test_copy_standardize_apart_compresses():
+    h = heap.Heap()
+    sig = signature.Signature.getsignature(".", 2)
+    b = shape.SharingShape
+    X = shape.InStorageShape.build()
+    s1 = b(sig, [X, X])
+    s1.get_transition(1, s1)
+    s2 = s1.get_transition(1, s1)
+
+    a = term.Callable.build("a")
+    b = term.Callable.build("b")
+    c_numbered = shape.ShapedCallable(s1, [term.NumberedVar(0), term.NumberedVar(1)])
+    c1 = shape.ShapedCallable(s1, [a, b])
+    c2 = c_numbered.copy_standardize_apart(h, [a, c1])
+    assert c2.get_shape() is s2
+
+
 def test_shaped_callable_unify():
-    from prolog.interpreter import heap
     a = term.Callable.build("a")
     b = term.Callable.build("b")
     c = term.Callable.build("c")