@@ -45,8 +45,8 @@ class Sum(Value):
4545 """Sum-of-product value.
4646
4747 Example:
48- >>> Sum(0, tys.Sum([[tys.Bool], [tys.Unit]]), [TRUE])
49- Sum(tag=0, typ=Sum([[Bool], [Unit]]), vals=[TRUE])
48+ >>> Sum(0, tys.Sum([[tys.Bool], [tys.Unit], [tys.Bool] ]), [TRUE])
49+ Sum(tag=0, typ=Sum([[Bool], [Unit], [Bool] ]), vals=[TRUE])
5050 """
5151
5252 #: Tag identifying the variant.
@@ -70,6 +70,59 @@ def _to_serial(self) -> sops.SumValue:
7070 vs = ser_it (self .vals ),
7171 )
7272
73+ def __repr__ (self ) -> str :
74+ if self == TRUE :
75+ return "TRUE"
76+ elif self == FALSE :
77+ return "FALSE"
78+ elif self == Unit :
79+ return "Unit"
80+ elif all (len (row ) == 0 for row in self .typ .variant_rows ):
81+ return f"UnitSum({ self .tag } , { self .n_variants } )"
82+ elif len (self .typ .variant_rows ) == 1 :
83+ return f"Tuple({ comma_sep_repr (self .vals )} )"
84+ elif len (self .typ .variant_rows ) == 2 and len (self .typ .variant_rows [0 ]) == 0 :
85+ # Option
86+ if self .tag == 0 :
87+ return f"None({ comma_sep_str (self .typ .variant_rows [1 ])} )"
88+ else :
89+ return f"Some({ comma_sep_repr (self .vals )} )"
90+ elif len (self .typ .variant_rows ) == 2 :
91+ # Either
92+ left_typ , right_typ = self .typ .variant_rows
93+ if self .tag == 0 :
94+ return f"Left(vals={ self .vals } , right_typ={ list (right_typ )} )"
95+ else :
96+ return f"Right(left_typ={ list (left_typ )} , vals={ self .vals } )"
97+ else :
98+ return f"Sum(tag={ self .tag } , typ={ self .typ } , vals={ self .vals } )"
99+
100+ def __str__ (self ) -> str :
101+ if self == TRUE :
102+ return "TRUE"
103+ elif self == FALSE :
104+ return "FALSE"
105+ elif self == Unit :
106+ return "Unit"
107+ elif all (len (row ) == 0 for row in self .typ .variant_rows ):
108+ return f"UnitSum({ self .tag } , { self .n_variants } )"
109+ elif len (self .typ .variant_rows ) == 1 :
110+ return f"Tuple({ comma_sep_str (self .vals )} )"
111+ elif len (self .typ .variant_rows ) == 2 and len (self .typ .variant_rows [0 ]) == 0 :
112+ # Option
113+ if self .tag == 0 :
114+ return "None"
115+ else :
116+ return f"Some({ comma_sep_str (self .vals )} )"
117+ elif len (self .typ .variant_rows ) == 2 :
118+ # Either
119+ if self .tag == 0 :
120+ return f"Left({ comma_sep_str (self .vals )} )"
121+ else :
122+ return f"Right({ comma_sep_str (self .vals )} )"
123+ else :
124+ return f"Sum({ self .tag } , { self .typ } , { self .vals } )"
125+
73126 def __eq__ (self , other : object ) -> bool :
74127 return (
75128 isinstance (other , Sum )
@@ -100,6 +153,7 @@ def to_model(self) -> model.Term:
100153 )
101154
102155
156+ @dataclass (eq = False , repr = False )
103157class UnitSum (Sum ):
104158 """Simple :class:`Sum` with each variant being an empty row.
105159
@@ -119,15 +173,6 @@ def __init__(self, tag: int, size: int):
119173 vals = [],
120174 )
121175
122- def __repr__ (self ) -> str :
123- if self == TRUE :
124- return "TRUE"
125- if self == FALSE :
126- return "FALSE"
127- if self == Unit :
128- return "Unit"
129- return f"UnitSum({ self .tag } , { self .n_variants } )"
130-
131176
132177def bool_value (b : bool ) -> UnitSum :
133178 """Convert a python bool to a HUGR boolean value.
@@ -149,7 +194,7 @@ def bool_value(b: bool) -> UnitSum:
149194FALSE = bool_value (False )
150195
151196
152- @dataclass (eq = False )
197+ @dataclass (eq = False , repr = False )
153198class Tuple (Sum ):
154199 """Tuple or product value, defined by a list of values.
155200 Internally a :class:`Sum` with a single variant row.
@@ -177,10 +222,10 @@ def _to_serial(self) -> sops.TupleValue: # type: ignore[override]
177222 )
178223
179224 def __repr__ (self ) -> str :
180- return f"Tuple( { comma_sep_repr ( self . vals ) } )"
225+ return super (). __repr__ ()
181226
182227
183- @dataclass (eq = False )
228+ @dataclass (eq = False , repr = False )
184229class Some (Sum ):
185230 """Optional tuple of value, containing a list of values.
186231
@@ -199,11 +244,8 @@ def __init__(self, *vals: Value):
199244 tag = 1 , typ = tys .Option (* (v .type_ () for v in val_list )), vals = val_list
200245 )
201246
202- def __repr__ (self ) -> str :
203- return f"Some({ comma_sep_repr (self .vals )} )"
204-
205247
206- @dataclass (eq = False )
248+ @dataclass (eq = False , repr = False )
207249class None_ (Sum ):
208250 """Optional tuple of value, containing no values.
209251
@@ -219,14 +261,8 @@ class None_(Sum):
219261 def __init__ (self , * types : tys .Type ):
220262 super ().__init__ (tag = 0 , typ = tys .Option (* types ), vals = [])
221263
222- def __repr__ (self ) -> str :
223- return f"None({ comma_sep_str (self .typ .variant_rows [1 ])} )"
224-
225- def __str__ (self ) -> str :
226- return "None"
227-
228264
229- @dataclass (eq = False )
265+ @dataclass (eq = False , repr = False )
230266class Left (Sum ):
231267 """Left variant of a :class:`tys.Either` type, containing a list of values.
232268
@@ -248,15 +284,8 @@ def __init__(self, vals: Iterable[Value], right_typ: Iterable[tys.Type]):
248284 vals = val_list ,
249285 )
250286
251- def __repr__ (self ) -> str :
252- _ , right_typ = self .typ .variant_rows
253- return f"Left(vals={ self .vals } , right_typ={ list (right_typ )} )"
254-
255- def __str__ (self ) -> str :
256- return f"Left({ comma_sep_str (self .vals )} )"
257-
258287
259- @dataclass (eq = False )
288+ @dataclass (eq = False , repr = False )
260289class Right (Sum ):
261290 """Right variant of a :class:`tys.Either` type, containing a list of values.
262291
@@ -280,13 +309,6 @@ def __init__(self, left_typ: Iterable[tys.Type], vals: Iterable[Value]):
280309 vals = val_list ,
281310 )
282311
283- def __repr__ (self ) -> str :
284- left_typ , _ = self .typ .variant_rows
285- return f"Right(left_typ={ list (left_typ )} , vals={ self .vals } )"
286-
287- def __str__ (self ) -> str :
288- return f"Right({ comma_sep_str (self .vals )} )"
289-
290312
291313@dataclass
292314class Function (Value ):
0 commit comments