1
2
3
4
5
6
7
8 from api import *
9
11 """
12 Abstract class for representing a binary combinator.
13 Merely defines functions for checking if the function and argument
14 are able to be combined, and what the resulting category is.
15
16 Note that as no assumptions are made as to direction, the unrestricted
17 combinators can perform all backward, forward and crossed variations
18 of the combinators; these restrictions must be added in the rule
19 class.
20 """
22 raise AssertionError, 'UndirectedBinaryCombinator is an abstract interface'
23
24 - def combine (self,function,argument):
25 raise AssertionError, 'UndirectedBinaryCombinator is an abstract interface'
26
28 """
29 Wrapper for the undirected binary combinator.
30 It takes left and right categories, and decides which is to be
31 the function, and which the argument.
32 It then decides whether or not they can be combined.
33 """
35 raise AssertionError, 'DirectedBinaryCombinator is an abstract interface'
36
38 raise AssertionError, 'DirectedBinaryCombinator is an abstract interface'
39
41 '''
42 Class representing combinators where the primary functor is on the left.
43
44 Takes an undirected combinator, and a predicate which adds constraints
45 restricting the cases in which it may apply.
46 '''
47 - def __init__(self, combinator, predicate, suffix=''):
48 self._combinator = combinator
49 self._predicate = predicate
50 self._suffix = suffix
51
53 return (self._combinator.can_combine(left,right) and
54 self._predicate(left,right))
55
57 for cat in self._combinator.combine(left,right):
58 yield cat
59
61 return '>' + str(self._combinator) + self._suffix
62
64 '''
65 The backward equivalent of the ForwardCombinator class.
66 '''
67 - def __init__(self, combinator, predicate, suffix=''):
68 self._combinator = combinator
69 self._predicate = predicate
70 self._suffix = suffix
71
73 return (self._combinator.can_combine(right, left) and
74 self._predicate(left,right))
76 for cat in self._combinator.combine(right, left):
77 yield cat
78
80 return '<' + str(self._combinator) + self._suffix
81
83 """
84 Class representing function application.
85 Implements rules of the form:
86 X/Y Y -> X (>)
87 And the corresponding backwards application rule
88 """
89
95
96 - def combine(self,function,argument):
105
108
109
110
111
112
115
116
119
120
121 ForwardApplication = ForwardCombinator(UndirectedFunctionApplication(),
122 forwardOnly)
123 BackwardApplication = BackwardCombinator(UndirectedFunctionApplication(),
124 backwardOnly)
125
126
128 """
129 Functional composition (harmonic) combinator.
130 Implements rules of the form
131 X/Y Y/Z -> X/Z (B>)
132 And the corresponding backwards and crossed variations.
133 """
142
143 - def combine(self, function, argument):
151
154
155
158
161
162
163
166
176
177
178 ForwardComposition = ForwardCombinator(UndirectedComposition(),
179 forwardOnly)
180 BackwardComposition = BackwardCombinator(UndirectedComposition(),
181 backwardOnly)
182
183
184 BackwardBx = BackwardCombinator(UndirectedComposition(),backwardBxConstraint,
185 suffix='x')
186
187
189 """
190 Substitution (permutation) combinator.
191 Implements rules of the form
192 Y/Z (X\Y)/Z -> X/Z (<Sx)
193 And other variations.
194 """
209
210 - def combine(self,function,argument):
213
216
217
222
223
230
231
232 ForwardSubstitution = ForwardCombinator(UndirectedSubstitution(),
233 forwardSConstraint)
234 BackwardSx = BackwardCombinator(UndirectedSubstitution(),
235 backwardSxConstraint,'x')
236
237
238
239
244
246 '''
247 Undirected combinator for type raising.
248 '''
266
281
284
285
286
287
288
289
293
297
298
299 ForwardT = ForwardCombinator(UndirectedTypeRaise(), forwardTConstraint)
300 BackwardT = BackwardCombinator(UndirectedTypeRaise(), backwardTConstraint)
301