From: Tim Vieira Date: Tue, 2 Jul 2013 22:13:36 +0000 (-0400) Subject: stable `set` and `bag` ordering. X-Git-Url: https://hydra-www.ietfng.org/gitweb/?a=commitdiff_plain;h=9b2b338b17f09aa992d1b5ecfcb61630e76d5f28;p=dyna2 stable `set` and `bag` ordering. --- diff --git a/examples/expected/lists.py.out b/examples/expected/lists.py.out index 98ad10a..24c96b5 100644 --- a/examples/expected/lists.py.out +++ b/examples/expected/lists.py.out @@ -6,6 +6,8 @@ b = true. c = true. d = false. things = [1, [2, 2], [3, 4]]. +thingsbag = [1, 1, 2, "three"]. +thingset = [1, 2, "three"]. d/1 === diff --git a/examples/lists.dyna b/examples/lists.dyna index 9080c63..5e4dd94 100644 --- a/examples/lists.dyna +++ b/examples/lists.dyna @@ -24,3 +24,13 @@ foo(A) := true for &f(A) in [1,2,&f("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]. + +thingsbag bag= "three". +thingsbag bag= 1. +thingsbag bag= 1. +thingsbag bag= 2. + +thingset set= "three". +thingset set= 1. +thingset set= 1. +thingset set= 2. \ No newline at end of file diff --git a/src/Dyna/Backend/Python/stdlib.py b/src/Dyna/Backend/Python/stdlib.py index 2f47eb2..9c0d2d1 100644 --- a/src/Dyna/Backend/Python/stdlib.py +++ b/src/Dyna/Backend/Python/stdlib.py @@ -1,5 +1,6 @@ import re from term import Term, Cons, Nil +from collections import Counter try: from numpy import log, exp, sqrt @@ -27,7 +28,7 @@ def pycall(name, *args): return todyna(x) def todyna(x): - if isinstance(x, (set, list, tuple)): + if isinstance(x, (list, tuple, set, Counter)): return todynalist(x) return x @@ -37,6 +38,10 @@ def topython(x): return x def todynalist(x): + if isinstance(x, (set, Counter)): + x = list(x) + x.sort() + return todynalist(x) return _todynalist(list(x)) def _todynalist(x): diff --git a/src/Dyna/Backend/Python/term.py b/src/Dyna/Backend/Python/term.py index 60f54e4..fa2e069 100644 --- a/src/Dyna/Backend/Python/term.py +++ b/src/Dyna/Backend/Python/term.py @@ -71,11 +71,11 @@ class Cons(Term): else: yield a, (None,), a - def __eq__(self, other): + def __cmp__(self, other): try: - return self.aslist == other.aslist + return cmp(self.aslist, other.aslist) except AttributeError: - return False + return 1 class _Nil(Term):