From: Tim Vieira Date: Tue, 9 Jul 2013 20:55:04 +0000 (-0400) Subject: new infix operator for alist `->`/2 X-Git-Url: https://hydra-www.ietfng.org/gitweb/?a=commitdiff_plain;h=4cace908c790ea983d8721423fdafd2139c7b0a2;p=dyna2 new infix operator for alist `->`/2 tweaks to list tests. --- diff --git a/src/Dyna/Backend/Python/term.py b/src/Dyna/Backend/Python/term.py index 47c0df2..a9f032b 100644 --- a/src/Dyna/Backend/Python/term.py +++ b/src/Dyna/Backend/Python/term.py @@ -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)) diff --git a/src/Dyna/Term/SurfaceSyntax.hs b/src/Dyna/Term/SurfaceSyntax.hs index 38753cc..621a538 100644 --- a/src/Dyna/Term/SurfaceSyntax.hs +++ b/src/Dyna/Term/SurfaceSyntax.hs @@ -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 index 0000000..832a246 --- /dev/null +++ b/test/repl/alist.dynadoc @@ -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 diff --git a/test/repl/list.dynadoc b/test/repl/list.dynadoc index fe8b061..ebdcc22 100644 --- a/test/repl/list.dynadoc +++ b/test/repl/list.dynadoc @@ -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"]. -