James - Algebraic Refactoring Flashcards
Meanwhile, when you are choosing the best implementation of a design, you are picking from a space of possible programs that all do the same thing. Where do these possibilities come from?
They come from simple rules for turning one program into an equivalent one. And the best part is you learned most of them in high school.
When you are choosing the best implementation of a design, you are picking from a space of possible programs that all do the same thing. Where do these possibilities come from?
They come from simple rules for turning one program into an equivalent one.
We can roughly split software design into three levels, what are these?
Design -> At a high level, what should it do -> A spec saying that I need a positive integer
Behavior -> At a low level -> what does it do -> A low-level spec saying it returns the number five
Implementation -> How does it do it? -> Choosing from possible implementations how the low-level spec should be met.
What is a reduction?
Is a single step of computation
1 + 5 -> 6
69 -> 54
(1+5)(72) -> 6(72)
(1+5)(72) -> (1+5)9
For refactoring, we allow reduction in different orders and that gives us a graph of all the ways we can reduce an expression.
(1+5)*(7*2) 6*(7*2) (1+5)*9 6*9 54
Since we can reduce in any order, we can also go backward and unreduce expressions.
(1+5)(72) -> (1+10/2)(72)
Reducing and unreducing give us an infinite space of possible expressions that all give us the same answer.
What is equivalence?
If A can be rewritten to B by a series of reductions and backwards reductions, then A and B are equivalent.
What is a substitution?
In an expression with variables, we can substitute all instances of the same variable for a value.
1/z
x^2 + xy + 1
What is equality?
You can say that a + b = b + a, because no matter what concrete values I choose for a and b they reduce to the same result. In a sense, we can derive equality from equivalence.
What ability equality give us?
We can use substitution and unsubstitution along with reduction and unreduction to explore the infinite space of all possible expressions that give us the same answer.
unsubstitution is the beginning of an add parameter refactoring and also a case of the extract method refactoring.
1+sqrt((x1-x2)(x1-x2)+(y1-y2)(y1-y2)))
1+[x1-x2/x, y1-y2/y]sqrt(xx+yy)
1+(fn (x, y) -> sqrt(xx+yy) (x1-x2, y1-y2))
1+(fn (x, y) -> sqrt(xx+yy*))/mag
mag = (x, y) -> sqrt(xx+yy*)
1+mag (x1-x2, y1-y2)
What are the three fundamental constructs for data structures?
Products, Sums and Exponentials
What is a product?
|A x B| = |A| * |B|
Product is an and.
C structs, python tuples, python dictionaries are products. They only differ in how their elements are indexed. Classes are also like structs.
What are the laws associated with products?
We have commutativity (you can reorder):
A x B = B x A
We have associativity:
(A x B) x C = A x (B x C)
((1, 2), 3) = (1, (2, 3))
class Employee int id class Engineer extends Employee String name int deskno
is equivalent to
class Employee int id String name class Engineer extends Employee int deskno
What is a Sum?
Sum is an OR.
Sum = Left | Right
case sum of
Left -> do something
Right -> do otherstuf
In C we have unions, in Java we can use subclasses, in Python everything is a sum because of dynamic typing.
Does Int + Int = Int?
No, because in a Sum we have knowledge of which value is each, it can be either the first one or the second one.
Int + Int = Int * 2 = Int * Bool
What is the unit type?
Its the type that only contains one thing.
In C/Java is called void. If we have a void function, is not that it never returns, it just doesn’t return anything interesting, like returning the same thing everytime.
Or enum Unit { UNIT } is also a unit in C.
What is a boolean?
bool = 1 + 1 = 2
It’s either the left of nothing interesting (unit type) or it’s right of nothing interesting.
boolean is also called two, it’s the type with two values. And is equivalent to any other type with two values.