2323
2424class RenamingSubstitution (DeferrableSubstitution [V ]):
2525 """
26- Visitor which replaces a free Variable with another term
27- Renames bound variables if a free variable gets bound
26+ ABC for Substitutions which rename
27+ bound variables if a free variable gets bound.
2828 """
2929
3030 variable : V
@@ -38,39 +38,67 @@ class RenamingSubstitution(DeferrableSubstitution[V]):
3838
3939 @abstractmethod
4040 def prevent_collision (self , abstraction : terms .Abstraction [V ]) -> terms .Abstraction [V ]:
41- """prevent collisions by renaming bound variables"""
41+ """
42+ Prevent collisions by renaming bound variables.
43+
44+ :param abstraction: abstraction term which could bind free variables
45+ :return: abstraction term which does not bind free variables
46+ """
4247 raise NotImplementedError ()
4348
4449 @final
4550 def trace (self ) -> TracingDecorator [V ]:
46- """return a new visitor which yields when an alpha conversion occurs"""
51+ """
52+ Create a new visitor which yields when an alpha conversion occurs.
53+
54+ :return: new visitor wrapping this instance
55+ """
4756 return TracingDecorator (self )
4857
4958 @final
5059 def visit_variable (self , variable : terms .Variable [V ]) -> terms .Term [V ]:
51- """visit a Variable term"""
60+ """
61+ Visit a Variable term.
62+
63+ :param variable: variable term to visit
64+ :return: variable term or value which should be substituted
65+ """
5266 if variable .name != self .variable :
5367 return variable
5468 return self .value
5569
5670 @final
5771 def defer_abstraction (self , abstraction : terms .Abstraction [V ]) -> tuple [terms .Abstraction [V ], RenamingSubstitution [V ] | None ]:
58- """visit an Abstraction term and return the visitor used to visit its body"""
72+ """
73+ Visit an Abstraction term.
74+
75+ :param abstraction: abstraction term to visit
76+ :return: tuple containing an abstraction term not binding free variables and
77+ this visitor to be used for visiting its body if variable is not bound
78+ """
5979 if abstraction .bound == self .variable :
6080 return abstraction , None
6181 return self .prevent_collision (abstraction ), self
6282
6383 @final
6484 def defer_application (self , application : terms .Application [V ]) -> tuple [terms .Application [V ], RenamingSubstitution [V ], RenamingSubstitution [V ]]:
65- """visit an Application term and return the visitors used to visit its abstraction and argument"""
85+ """
86+ Visit an Application term.
87+
88+ :param application: application term to visit
89+ :return: tuple containing the application term and this visitor
90+ to be used for visiting its abstraction and argument
91+ """
6692 return application , self , self
6793
6894
6995@final
7096class TracingDecorator (Visitor [Generator ["terms.Term[V]" , None , "terms.Term[V]" ], V ]):
7197 """
72- Visitor which transforms a RenamingSubstitution into an Generator
73- which yields after performing an alpha conversion
98+ Visitor which transforms a :class:`RenamingSubstitution` into an Generator
99+ which yields after performing an alpha conversion and returns the term with substitutions.
100+
101+ :param substitution: instance to wrap
74102 """
75103
76104 substitution : RenamingSubstitution [V ]
@@ -81,13 +109,25 @@ def __init__(self, substitution: RenamingSubstitution[V]) -> None:
81109 self .substitution = substitution
82110
83111 def visit_variable (self , variable : terms .Variable [V ]) -> Generator [terms .Variable [V ], None , terms .Term [V ]]:
84- """visit a Variable term"""
112+ """
113+ Visit a Variable term.
114+
115+ :param variable: variable term to visit
116+ :return: empty Generator returning the result
117+ of :meth:`RenamingSubstitution.visit_variable`
118+ """
85119 return self .substitution .visit_variable (variable )
86120 # to create a generator
87121 yield variable # type: ignore[unreachable]
88122
89123 def visit_abstraction (self , abstraction : terms .Abstraction [V ]) -> Generator [terms .Abstraction [V ], None , terms .Abstraction [V ]]:
90- """visit an Abstraction term"""
124+ """
125+ Visit an Abstraction term
126+
127+ :param abstraction: abstraction term to visit
128+ :return: Generator yielding alpha conversions and
129+ returning the term with substitutions
130+ """
91131 substituted , visitor = self .substitution .defer_abstraction (abstraction )
92132 if visitor is None :
93133 return substituted
@@ -98,7 +138,13 @@ def visit_abstraction(self, abstraction: terms.Abstraction[V]) -> Generator[term
98138 return terms .Abstraction (substituted .bound , body )
99139
100140 def visit_application (self , application : terms .Application [V ]) -> Generator [terms .Application [V ], None , terms .Application [V ]]:
101- """visit an Application term"""
141+ """
142+ Visit an Application term
143+
144+ :param appliation: application term to visit
145+ :return: Generator yielding alpha conversions and
146+ returning the term with substitutions
147+ """
102148 # distinguish between last alpha conversion (step) and
103149 # substituted abstraction (abstraction)
104150 abstraction = step = application .abstraction
@@ -119,8 +165,12 @@ def visit_application(self, application: terms.Application[V]) -> Generator[term
119165@final
120166class CountingSubstitution (RenamingSubstitution [str ]):
121167 """
122- Visitor which replaces a free Variable with another term
123- Renames bound variables if a free variable gets bound by appending a number
168+ Substitution which renames bound variables
169+ if a free variable gets bound by appending a number.
170+
171+ :param variable: variable to substitute
172+ :param value: value which should be substituted
173+ :param free_variables: free variables which should not be bound
124174 """
125175
126176 free_variables : Set [str ]
@@ -134,11 +184,22 @@ def __init__(self, variable: str, value: terms.Term[str], free_variables: Set[st
134184
135185 @classmethod
136186 def from_substitution (cls , variable : str , value : terms .Term [str ]) -> CountingSubstitution :
137- """create an instance from the substitution it should perform"""
187+ """
188+ Create an instance from the substitution it should perform
189+
190+ :param variable: variable to substitute
191+ :param value: value which should be substituted
192+ :return: new instance with free_variables set to the free variables of value
193+ """
138194 return cls (variable , value , value .free_variables ())
139195
140196 def prevent_collision (self , abstraction : terms .Abstraction [str ]) -> terms .Abstraction [str ]:
141- """prevent collisions by renaming bound variables"""
197+ """
198+ Prevent collisions by appending a number.
199+
200+ :param abstraction: abstraction term which could bind free variables
201+ :return: abstraction term which does not bind free variables
202+ """
142203 if abstraction .bound in self .free_variables :
143204 used_variables = abstraction .body .bound_variables () \
144205 | abstraction .free_variables () \
0 commit comments