Commits

Carl Friedrich Bolz  committed 36eae21

introduce an InStorageAtomShape that doesn't reduce memory but stores the type

  • Participants
  • Parent commits 53b55b1
  • Branches type-tag-terms

Comments (0)

Files changed (2)

File prolog/interpreter/shape.py

         return "InStorageIntShape()"
 InStorageIntShape._singleton = InStorageIntShape()
 
+class InStorageAtomShape(InStorageShape):
+    @staticmethod
+    def build():
+        return InStorageAtomShape._singleton
+
+    def resolve(self, shaped_callable, index):
+        result = unerase(shaped_callable.get_raw_storage(index))
+        assert isinstance(result, term.Atom)
+        return result
+
+    def write(self, shaped_callable, i, val):
+        assert val is None or isinstance(val, term.Atom)
+        shaped_callable.set_raw_storage(i, erase(val))
+
+    def str(self):
+        return "InStorageAtomShape()"
+InStorageAtomShape._singleton = InStorageAtomShape()
+
 def can_be_tagged(obj):
     val = obj.num
     # bit sucky
             self.set_shape(new_shape)
             # new shape will take care to store unwrapped
             self.set_storage(index, obj)
+        elif isinstance(obj, term.Atom):
+            new_shape = shape.replace(index, InStorageAtomShape.build())
+            self.set_shape(new_shape)
+            self.set_storage(index, obj)
         return None
 
     @staticmethod

File prolog/interpreter/test/test_shape.py

     s = shape.InStorageIntShape()
     assert s.resolve(FakeShapedCallable([shape.rerased.erase_int(1)]), 0).num == 1
 
+def test_instorage_atom_resolve():
+    s = shape.InStorageAtomShape()
+    a = term.Atom.build("a")
+    assert s.resolve(erased_storage([a]), 0) is a
+
 def test_sharing_resolve():
     sig = signature.Signature.getsignature("f", 2)
     s = shape.SharingShape(sig, [
     assert res is None
     assert shape.unerase(c1.get_raw_storage(1)) is None
 
+def test_shaped_callable_replace_child_atom():
+    import sys
+    sig = signature.Signature.getsignature(".", 2)
+    build = shape.SharingShape
+    X = shape.InStorageShape.build()
+    s1 = build(sig, [X, X])
+    a = term.Callable.build("a")
+    b = term.Callable.build("b")
+    nil = term.Callable.build("[]")
+    c1 = shape.ShapedCallable(s1, [a, None])
+    res = c1.replace_child(1, nil)
+    assert res is None
+    assert isinstance(c1.shape.children[1], shape.InStorageAtomShape)
+    assert c1.get_full_storage() == [a, nil]
+
 def test_depth():
     sig = signature.Signature.getsignature(".", 2)
     b = shape.SharingShape.build
     s1.get_transition(1, s1)
     s2 = s1.get_transition(1, s1)
 
-    a = term.Callable.build("a")
-    b = term.Callable.build("b")
+    a = term.Float(0)
+    b = term.Float(1)
     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])