]> hydra-www.ietfng.org Git - dyna2/commitdiff
new infix operator for alist `->`/2
authorTim Vieira <tim.f.vieira@gmail.com>
Tue, 9 Jul 2013 20:55:04 +0000 (16:55 -0400)
committerTim Vieira <tim.f.vieira@gmail.com>
Tue, 9 Jul 2013 20:55:04 +0000 (16:55 -0400)
tweaks to list tests.

src/Dyna/Backend/Python/term.py
src/Dyna/Term/SurfaceSyntax.hs
test/repl/alist.dynadoc [new file with mode: 0644]
test/repl/list.dynadoc

index 47c0df20073efb74062fa8a0b00805286d3a6870..a9f032b3c8fee67b7deaab9253939667d57d001f 100644 (file)
@@ -30,6 +30,10 @@ class Term(object):
 
     def __repr__(self):
         "Pretty print a term. Will retrieve the complete (ground) term."
+
+        if self.fn == '->/2':
+            return '%s -> %s' % self.args
+
         fn = '/'.join(self.fn.split('/')[:-1])  # drop arity from name.
         if not self.args:
             return fn
@@ -94,10 +98,11 @@ class Cons(Term):
 #        try:
 #            return cmp(self.aslist, other.aslist)
 #        except AttributeError:
-#            return 
+#            return
 
 
 class _Nil(Term):
+
     def __init__(self):
         Term.__init__(self, 'nil/0', ())
         self.aggregator = NoAggregator
@@ -129,19 +134,3 @@ class _Nil(Term):
 
 
 Nil = _Nil()
-
-
-#class AList(Cons):
-#
-#    def __init__(self, head, tail):
-#
-#        if not (isinstance(tail, AList) or tail is Nil):
-#            raise TypeError('Malformed alist: tail is not an alist')
-#
-#        if not (isinstance(head, Cons) and len(head.aslist) == 2):
-#            raise TypeError('Malformed alist: head is not a pair.')
-#
-#        Cons.__init__(self, head, tail)
-#
-#    def __repr__(self):
-#        return '[%s]' % (', '.join('%s -> %s' % (_repr(k), _repr(v)) for k,v in self.aslist))
index 38753cc63ec2a5980afdf95c20b5f498d3783df4..621a538bf05d4df52f029acc11cd05eee276a573 100644 (file)
@@ -81,6 +81,8 @@ defOperSpec = foldr (\(k,v) -> mapInOrCons k v) def more
 
     , ("with_key" ,[(4,PFIn AssocNone )      ])
 
+    , ("->" ,[(5,PFIn AssocNone )      ])
+
     , ("<=" ,[(4,PFIn AssocNone )            ])
     , ("<"  ,[(4,PFIn AssocNone )            ])
     , ("="  ,[(4,PFIn AssocNone )            ])
@@ -179,6 +181,7 @@ disposTab_prologish t = DisposTab s a
        -- lists
        , (("nil",  0),(SDQuote,[]))
        , (("cons", 2),(SDQuote,[ADEval,ADEval]))
+       , (("->",2),(SDQuote,[ADQuote, ADQuote]))
        ]
 
 -- | Make the default surface syntax more functional.  Here, all functors
@@ -209,6 +212,7 @@ disposTab_dyna t = DisposTab s a
        -- key
        , (("$key" ,1),(SDEval,[ADQuote]))
        , (("with_key",2),(SDQuote,[ADEval, ADQuote]))
+       , (("->",2),(SDQuote,[ADQuote, ADQuote]))
        ]
 
 ------------------------------------------------------------------------}}}
diff --git a/test/repl/alist.dynadoc b/test/repl/alist.dynadoc
new file mode 100644 (file)
index 0000000..832a246
--- /dev/null
@@ -0,0 +1,40 @@
+% fun with associative lists
+
+> a = [1 -> "a", 2 -> "b", 3 -> "c", 3 -> "d"].
+|
+| :- backchain findall/2.
+| findall(K,[_|Xs]) := findall(K,Xs).
+| findall(K,[(K -> V)|Xs]) := [V|findall(K,Xs)].
+| findall(_,[]) := [].
+|
+| :- backchain get/2.
+| get(K,[_|Xs]) := get(K,Xs).
+| get(K,[(K -> V)|Xs]) := V.
+| get(_,[]) := null.
+|
+| :- backchain set/3.
+| set(K,V,A) = [K -> V|A].
+
+Changes
+=======
+a = [1 -> a, 2 -> b, 3 -> c, 3 -> d].
+
+> query findall(3, a)
+
+findall(3, a) = ["c", "d"].
+
+> query findall(4, a)
+
+findall(4, a) = [].
+
+> query get(3, a)
+
+get(3, a) = "c".
+
+> query get("three", a)
+
+No results.
+
+> query findall(3, set(3, "three", a))
+
+findall(3, set(3, "three", a)) = ["three", "c", "d"].
\ No newline at end of file
index fe8b061b8d90238b2aeaed52ed297c8dfa55fbfb..ebdcc22afc17c85a9081211a1caeac53c9811a04 100644 (file)
@@ -84,18 +84,23 @@ d(2) = true.
 d(4) = true.
 
 % quote is important! or else we enumerate everything!
-> foo(A) := true for &f(A) in [1,2,&f("a"),3].
+> foo(A) := true for &bar(A) in [1,2,&bar("a"),3].
 |
-| % this one checks if the value of f(A) is in the list, (note: 1 == True, in python).
-| goo(A) := true for f(A) in [1,2,&f("a"),3].
-
+| bar("int") = 1.
+| bar("bool") = true.
+| bar("spaz") = 2.
+|
+| goo(A) := true for bar(A) in [true,2,&bar("a"),3].
 
 Changes
 =======
+bar("bool") = true.
+bar("int") = 1.
+bar("spaz") = 2.
 foo("a") = true.
-goo(1) = true.
-goo([1, 2, 3, [2, 1], f(1)]) = true.
-goo([]) = true.
+goo("bool") = true.
+goo("int") = true.                 % TODO: this should't appear in the results (XREF:bool)
+goo("spaz") = true.
 
 
 % unfortunately 1 == true and 0 == false in python so the following is true
@@ -103,7 +108,7 @@ goo([]) = true.
 
 Changes
 =======
-testbool = true.
+testbool = true.                  % TODO: XREF:bool
 
 
 % fun with set= at bag=
@@ -111,14 +116,15 @@ testbool = true.
 | thingsbag bag= 1.
 | thingsbag bag= 1.
 | thingsbag bag= 2.
+| thingsbag bag= true.            % XREF:bool
 |
 | thingset set= "three".
 | thingset set= 1.
 | thingset set= 1.
+| thingset set= true.            % XREF:bool should appear separately in set,
 | thingset set= 2.
 
 Changes
 =======
-thingsbag = [1, 1, 2, "three"].
+thingsbag = [1, 1, 1, 2, "three"].
 thingset = [1, 2, "three"].
-