3
3
Algebraic Data Types (ADT) is used to represent two kinds of things:
4
4
5
5
1. A discrimintated union of types, called sum
6
- 2. A combination of some types, called product.
6
+ 2. A combination of ADT types, called product.
7
7
8
8
# Sum types
9
9
10
- Sum types represents a concept of generalizing. For example,
10
+ Sum types represent a concept of generalizing. For example,
11
11
on ARM R0 and R1 are all general purpose registers (GPR). Also on ARM
12
12
we have Condition Code registers (CCR) :
13
13
@@ -48,10 +48,10 @@ class ADT(object):
48
48
""" Algebraic Data Type.
49
49
50
50
This is a base class for all ADTs. ADT represented by a tuple of arguments,
51
- stored in a val field. Arguments should be instances of ADT class, or numbers,
51
+ stored in a `arg` field. Arguments should be instances of ADT class, or numbers,
52
52
or strings. Empty set of arguments is permitted.
53
53
A one-tuple is automatically untupled, i.e., `Int(12)` has value `12`, not `(12,)`.
54
- For convenience, a name of the constructor is provided in `name ` field.
54
+ A name of the constructor is stored in the `constr ` field
55
55
56
56
A structural comparison is provided.
57
57
@@ -115,7 +115,7 @@ def run(self, adt):
115
115
116
116
Otherwise, for an ADT of type C the method `visit_C` is looked up in the
117
117
visitors methods dictionary. If it doesn't exist, then `visit_B` is
118
- looked up, where `D ` is the base class of `C`. The process continues,
118
+ looked up, where `B ` is the base class of `C`. The process continues,
119
119
until the method is found. This is guaranteed to terminate,
120
120
since visit_ADT method is defined.
121
121
@@ -124,8 +124,8 @@ def run(self, adt):
124
124
Once the method is found it is called. It is the method's responsiblity
125
125
to recurse into sub-elements, e.g., call run method.
126
126
127
- For example, suppose that we want to count negative values in a given
128
- BIL expression:
127
+ For example, suppose that we want to count negative values in
128
+ some BIL expression:
129
129
130
130
class CountNegatives(Visitor):
131
131
def __init__(self):
@@ -148,7 +148,7 @@ def visit_NEG(self, op):
148
148
visit_Int for Int constructor and visit_NEG for counting unary minuses.
149
149
(Actually we should count for bitwise NOT operation also, since it will
150
150
change the sign bit also, but lets forget about it for the matter of the
151
- excercise (and it can be easily fixed just by matching visit_UnOp)).
151
+ exercise (and it can be easily fixed just by matching visit_UnOp)).
152
152
153
153
When we hit visit_NEG we toggle current sign, storing its previous value
154
154
and recurse into the operand. After we return from the recursion, we restore
0 commit comments