OSDN Git Service

Getting back to parity after jumble.
authorSimon Forman <sforman@hushmail.com>
Thu, 19 Jul 2018 22:18:42 +0000 (15:18 -0700)
committerSimon Forman <sforman@hushmail.com>
Thu, 19 Jul 2018 22:18:42 +0000 (15:18 -0700)
Polytypes module folded into types module, with all the actual type
information done when you load the library module.  Some definitions can
be inferred from their body expression, others need to be
CombinatorJoyType wrapped.  Still to do: loop.

joy/library.py
test/test_type_inference.py

index f1a863d..4798e99 100644 (file)
@@ -1519,25 +1519,32 @@ add_aliases(FUNCTIONS, ALIASES)
 DefinitionWrapper.add_definitions(definitions, _dictionary)
 
 
-for name in ('''
-of quoted enstacken ? dinfrirst
-'''.split()):
-  of_ = _dictionary[name]
-  secs = infer_expression(of_.body)
-  assert len(secs) == 1, repr(secs)
-  _log.info(
-    'Setting stack effect for definition %s := %s',
-    name,
-    doc_from_stack_effect(*secs[0]),
-    )
-  FUNCTIONS[name] = SymbolJoyType(name, infer_expression(of_.body), _SYM_NUMS())
+EXPECTATIONS = dict(
+  nullary=(s7, s6),
+)
+
+
+for name in '''
+  dinfrirst
+  nullary
+  '''.split():
+  C = _dictionary[name]
+  expect = EXPECTATIONS.get(name)
+  if expect:
+    sec = doc_from_stack_effect(expect)
+    _log.info('Setting stack EXPECT for combinator %s := %s', C.name, sec)
+  else:
+    _log.info('combinator %s', C.name)
+  FUNCTIONS[name] = CombinatorJoyType(name, [C], _COMB_NUMS(), expect)
 
 
 for name in ('''
-of quoted enstacken ? dinfrirst
+of quoted enstacken ?
+unary binary ternary
+sqr codireco unquoted
 '''.split()):
   of_ = _dictionary[name]
-  
+  secs = infer_expression(of_.body)
   assert len(secs) == 1, repr(secs)
   _log.info(
     'Setting stack effect for definition %s := %s',
@@ -1556,14 +1563,8 @@ of quoted enstacken ? dinfrirst
 ##  enstacken == stack [clear] dip
 ##  ? == dup truthy
 ##  disenstacken == ? [uncons ?] loop pop
-##  dinfrirst == dip infra first
-##  nullary == [stack] dinfrirst
-##  unary == nullary popd
-##  binary == nullary [popop] dip
-##  ternary == unary [popop] dip
 ##  pam == [i] map
 ##  run == [] swap infra
-##  sqr == dup mul
 ##  size == 0 swap [pop ++] step
 ##  fork == [i] app2
 ##  cleave == fork [popd] dip
index 9d1782d..3175f25 100644 (file)
@@ -1,12 +1,26 @@
 #!/usr/bin/env python
-import unittest
+import logging, sys, unittest
 
-from joy.utils.polytypes import *
+from joy.utils.types import *
 from joy.utils.stack import list_to_stack as __
-
+from joy.library import (
+  a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, As,
+  b0, b1, b2, b3, b4, b5, b6, b7, b8, b9,
+  n0, n1, n2, n3, n4, n5, n6, n7, n8, n9, Ns,
+  s0, s1, s2, s3, s4, s5, s6, s7, s8, s9,
+  f0, f1, f2, f3, f4, f5, f6, f7, f8, f9,
+  i0, i1, i2, i3, i4, i5, i6, i7, i8, i9,
+  t0, t1, t2, t3, t4, t5, t6, t7, t8, t9,
+  )
 
 globals().update(FUNCTIONS)
 
+logging.basicConfig(
+  format='%(message)s',
+  stream=sys.stdout,
+  level=logging.INFO,
+  )
+
 
 class TestMixin(object):
 
@@ -52,7 +66,7 @@ class TestCombinators(TestMixin, unittest.TestCase):
     self.assertEqualTypeStructure(infer(*expression), f)
 
   def test_concat(self):
-    expression = (swons, s3), (a4, s1), concat_
+    expression = (swons, s3), (a4, s1), concat
     f = (s1, ((swons, (a1, s2)), s1))  # (-- [swons a1 ...2])
     self.assertEqualTypeStructure(infer(*expression), [f])
 
@@ -90,18 +104,14 @@ class TestCombinators(TestMixin, unittest.TestCase):
       infra
       ]
     f = [
-      (s1, ((f1, (n1, s2)), s3)),  # (-- [f1 n1 ...2])
-      (s1, ((i1, (n1, s2)), s3)),  # (-- [i1 n1 ...2])
+      (s1, ((n1, (n2, s2)), s1)),  # (-- [n1 n2 ...2])
       ]
     self.assertEqualTypeStructure(infer(*expression), f)
 
   def test_nullary(self):
     expression = n1, n2, (mul, s2), (stack, s3), dip, infra, first
     f = [
-      (s1, (f1, (f2, (f3, s1)))),  # (-- f3 f2 f1)
-      (s1, (f1, (f2, (i1, s1)))),  # (-- i1 f2 f1)
-      (s1, (f1, (i1, (f2, s1)))),  # (-- f2 i1 f1)
-      (s1, (i1, (i2, (i3, s1)))),  # (-- i3 i2 i1)
+      (s1, (n1, (n2, (n3, s1)))),  # (-- n3 n2 n1)
       ]
     self.assertEqualTypeStructure(infer(*expression), f)