Skip to content

Commit 06b2d1d

Browse files
committed
generic tuple
1 parent 56ed515 commit 06b2d1d

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/kdrag/__init__.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,40 @@ def Some(x: smt.ExprRef) -> smt.DatatypeRef:
224224
return OptionSort(x.sort()).Some(x)
225225

226226

227+
@functools.cache
228+
def TupleSort(*elts: smt.SortRef) -> smt.DatatypeSortRef:
229+
"""
230+
Define a Tuple type for given element types
231+
>>> T = TupleSort(smt.IntSort(), smt.BoolSort())
232+
>>> t = T(42, True)
233+
>>> t
234+
Tuple_Int_Bool(42, True)
235+
>>> t._0
236+
_0(Tuple_Int_Bool(42, True))
237+
>>> t._1
238+
_1(Tuple_Int_Bool(42, True))
239+
"""
240+
name = "Tuple_" + "_".join(e.name() for e in elts)
241+
Tuple = Inductive(name)
242+
Tuple.declare(name, *[(f"_{i}", elt) for i, elt in enumerate(elts)])
243+
return Tuple.create()
244+
245+
246+
def tuple_(*args: smt.ExprRef) -> smt.DatatypeRef:
247+
"""
248+
Helper to create Tuple values
249+
>>> t = tuple_(42, True)
250+
>>> t
251+
Tuple_Int_Bool(42, True)
252+
>>> t.sort()
253+
Tuple_Int_Bool
254+
"""
255+
# debatably this should take in a iterator like built in python `tuple`
256+
args1 = [smt._py2expr(a) for a in args]
257+
T = TupleSort(*(a.sort() for a in args1))
258+
return T(*args1)
259+
260+
227261
Complex = datatype.Struct("C", ("re", smt.RealSort()), ("im", smt.RealSort()))
228262

229263
z, w, u, z1, z2 = smt.Consts("z w u z1 z2", Complex)

0 commit comments

Comments
 (0)