forked from aclements/commuter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymtypes.py
167 lines (133 loc) · 5.24 KB
/
symtypes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import z3
from simsym import *
class SListBase(Symbolic):
@classmethod
def var(cls, *args, **kwargs):
return super(SListBase, cls).var(*args, _start=0, **kwargs)
def _declare_assumptions(self, assume):
super(SListBase, self)._declare_assumptions(assume)
assume(self._len >= 0)
def __check_idx(self, idx):
if idx < 0:
raise IndexError("SList index out of range: %r < %r" % (idx, 0))
if idx >= self.len():
raise IndexError("SList index out of range: %r >= %r" %
(idx, self._len))
return idx
def __getitem__(self, idx):
return self._vals[self.__check_idx(idx) + self._start]
def __setitem__(self, idx, val):
self._vals[self.__check_idx(idx) + self._start] = val
def __eq__(self, o):
if type(o) != type(self):
return NotImplemented
i = SInt.var()
return symand([self._len == o._len,
forall(i, implies(symand([i >= 0, i < self._len]),
self._vals[i + self._start] ==
o._vals[i + o._start]))])
def len(self):
# Overriding __len__ isn't useful because the len() builtin
# will try hard to coerce it to an integer.
return self._len
def append(self, val):
l = self.len()
self._len += 1
self[l] = val
def shift(self, by=1):
if by == 0:
return
self.__check_idx(by - 1)
self._len -= by
self._start += by
def tlist(valueType, lenType=SInt):
"""Return a new list type whose values are valueType.
lenType, if specified, will be the type used for indexes and
length. It must be an ordered type and support '+' (which
effectively means it has to be SInt or a synonym type).
"""
name = "SList_" + valueType.__name__
base = tstruct(_vals = tmap(lenType, valueType), _len = lenType,
_start = lenType)
return type(name, (SListBase, base), {})
class SDictBase(Symbolic):
def __getitem__(self, key):
if self._valid[key]:
return self._map[key]
raise KeyError(key)
def __setitem__(self, key, val):
self._valid[key] = True
self._map[key] = val
def __delitem__(self, key):
self._valid[key] = False
def __eq__(self, o):
if type(self) != type(o):
return NotImplemented
key = self._map._indexType.var()
return symand([self._valid == o._valid,
forall(key, implies(self._valid[key],
self._map[key] == o._map[key]))])
@classmethod
def empty(cls, name=None):
return cls.var(name, _valid=cls._valid_type.constVal(False))
def contains(self, key):
return self._valid[key]
def __contains__(self, key):
# The "in" operator will force our result to a boolean, which
# we don't want. However, if we don't provide a __contains__,
# "in" will attempt to iterate over this object by calling
# __getitem__ for successive integers, which is *really* not
# what we want. So complain.
raise Exception("Use SDictBase.contains instead of the 'in' operator")
def create(self, key):
"""Return the value at key, creating it if necessary.
Note that there are no guarantees about the returned value,
even if it is freshly created (e.g., if a value at this key
was previously deleted, this will revive that deleted value).
The caller should be sure to override anything that matters in
the returned value.
"""
self._valid[key] = True
return self._map[key]
def tdict(keyType, valueType):
name = "SDict_" + keyType.__name__ + "_" + valueType.__name__
valid_type = tmap(keyType, SBool)
base = tstruct(_map = tmap(keyType, valueType),
_valid = valid_type)
return type(name, (SDictBase, base), {'_valid_type':valid_type})
class SSetBase(Symbolic):
@classmethod
def empty(cls):
return cls.var(_bmap = cls._mapType.constVal(False))
@classmethod
def all(cls):
return cls.var(_bmap = cls._mapType.constVal(True))
def add(self, val):
self._bmap = self._bmap.store(val, True)
def clear(self, val):
self._bmap = self._mapType.constVal(False)
def discard(self, val):
self._bmap = self._bmap.store(val, False)
def contains(self, val):
return self._bmap[val]
def tset(valueType):
"""Return a set type with the given value type."""
name = "SSet_" + valueType.__name__
mapType = tmap(valueType, SBool)
base = tstruct(_bmap = mapType)
return type(name, (base, SSetBase), {"_mapType": mapType})
class SBagBase(Symbolic):
def add(self, val):
assume(self._imap[val] >= 0)
self._imap[val] = self._imap[val] + 1
def take(self):
v = self._valueType.var()
add_internal(v)
assume(self._imap[v] > 0)
self._imap[v] = self._imap[v] - 1
return v
def tbag(valueType):
name = "SBag_" + valueType.__name__
mapType = tmap(valueType, SInt)
base = tstruct(_imap = mapType)
return type(name, (base, SBagBase), {"_valueType": valueType})