1+ """Lattice for constant analysis.
2+ """
3+
14from typing import Any , final
25from dataclasses import field , dataclass
36
@@ -22,6 +25,7 @@ class Result(
2225 BoundedLattice ["Result" ],
2326 _ElemVisitor ,
2427):
28+ """Base class for constant analysis results."""
2529
2630 @classmethod
2731 def top (cls ) -> "Result" :
@@ -35,6 +39,7 @@ def bottom(cls) -> "Result":
3539@final
3640@dataclass
3741class Unknown (Result , metaclass = SingletonMeta ):
42+ """Unknown constant value. This is the top element of the lattice."""
3843
3944 def is_subseteq (self , other : Result ) -> bool :
4045 return isinstance (other , Unknown )
@@ -43,6 +48,7 @@ def is_subseteq(self, other: Result) -> bool:
4348@final
4449@dataclass
4550class Bottom (Result , metaclass = SingletonMeta ):
51+ """Bottom element of the lattice."""
4652
4753 def is_subseteq (self , other : Result ) -> bool :
4854 return True
@@ -51,6 +57,8 @@ def is_subseteq(self, other: Result) -> bool:
5157@final
5258@dataclass
5359class Value (Result ):
60+ """Constant value. Wraps any Python value."""
61+
5462 data : Any
5563
5664 def is_subseteq_Value (self , other : "Value" ) -> bool :
@@ -64,11 +72,19 @@ def is_equal(self, other: Result) -> bool:
6472
6573@dataclass
6674class PartialConst (Result ):
75+ """Base class for partial constant values."""
76+
6777 pass
6878
6979
7080@final
7181class PartialTupleMeta (LatticeMeta ):
82+ """Metaclass for PartialTuple.
83+
84+ This metaclass canonicalizes PartialTuple instances with all Value elements
85+ into a single Value instance.
86+ """
87+
7288 def __call__ (cls , data : tuple [Result , ...]):
7389 if all (isinstance (x , Value ) for x in data ):
7490 return Value (tuple (x .data for x in data )) # type: ignore
@@ -78,6 +94,8 @@ def __call__(cls, data: tuple[Result, ...]):
7894@final
7995@dataclass
8096class PartialTuple (PartialConst , metaclass = PartialTupleMeta ):
97+ """Partial tuple constant value."""
98+
8199 data : tuple [Result , ...]
82100
83101 def join (self , other : Result ) -> Result :
@@ -125,6 +143,11 @@ def is_subseteq_Value(self, other: Value) -> bool:
125143@final
126144@dataclass
127145class PartialLambda (PartialConst ):
146+ """Partial lambda constant value.
147+
148+ This represents a closure with captured variables.
149+ """
150+
128151 argnames : list [str ]
129152 code : ir .Statement
130153 captured : tuple [Result , ...]
@@ -177,6 +200,7 @@ def meet(self, other: Result) -> Result:
177200class Purity (
178201 SimpleJoinMixin ["Purity" ], SimpleMeetMixin ["Purity" ], BoundedLattice ["Purity" ]
179202):
203+ """Base class for purity lattice."""
180204
181205 @classmethod
182206 def bottom (cls ) -> "Purity" :
@@ -189,29 +213,43 @@ def top(cls) -> "Purity":
189213
190214@dataclass (frozen = True )
191215class Pure (Purity , metaclass = SingletonMeta ):
216+ """The result is from a pure function."""
192217
193218 def is_subseteq (self , other : Purity ) -> bool :
194219 return isinstance (other , (NotPure , Pure ))
195220
196221
197222@dataclass (frozen = True )
198223class NotPure (Purity , metaclass = SingletonMeta ):
224+ """The result is from an impure function."""
199225
200226 def is_subseteq (self , other : Purity ) -> bool :
201227 return isinstance (other , NotPure )
202228
203229
204230@dataclass (frozen = True )
205231class PurityBottom (Purity , metaclass = SingletonMeta ):
232+ """The bottom element of the purity lattice."""
206233
207234 def is_subseteq (self , other : Purity ) -> bool :
208235 return True
209236
210237
211238@dataclass
212239class JointResult (BoundedLattice ["JointResult" ]):
240+ """Joint result of constant value and purity.
241+
242+ This lattice is used to join the constant value and purity of a function
243+ during constant propagation analysis. This allows the analysis to track
244+ both the constant value and the purity of the function, so that the analysis
245+ can propagate constant values through function calls even if the function
246+ is only partially pure.
247+ """
248+
213249 const : Result
250+ """The constant value of the result."""
214251 purity : Purity = field (default_factory = Purity .top )
252+ """The purity of statement that produces the result."""
215253
216254 @classmethod
217255 def from_const (cls , value : Any ) -> "JointResult" :
0 commit comments