|
99 | 99 |
|
100 | 100 | # Generalized Algebraic Data Type
|
101 | 101 |
|
102 |
| -As long as you trust inheritance |
| 102 | +As long as you trust inheritance and dynamic dispatch you would probably also trust: |
103 | 103 |
|
104 | 104 | <small>
|
105 | 105 | ```tut
|
|
116 | 116 | ```
|
117 | 117 | </small>
|
118 | 118 |
|
| 119 | +<small>Paul Chiusano (scalaz-stream) has documented his attempts [here](https://github.com/scalaz/scalaz-stream/blob/topic/0.8-redesign/implementation-notes.markdown#gadts) |
| 120 | +</small> |
119 | 121 | ---
|
120 | 122 |
|
121 | 123 | # Sum Types and Product Types
|
122 | 124 |
|
123 |
| -Structurally: |
| 125 | +Speaking structurally: |
124 | 126 |
|
125 | 127 | ```tut
|
126 | 128 | type Sum[A] = Lam[A] // Lift ∨ Tup ∨ Lambda ∨ App ∨ Fix
|
|
179 | 181 |
|
180 | 182 | ---
|
181 | 183 |
|
| 184 | +# Free Algebras |
| 185 | + |
| 186 | +The group \\((Z,+)\\) of integers is free; we can take \\(S = {1}\\). |
| 187 | + |
| 188 | +A monoid is free: |
| 189 | + |
| 190 | +```tut:invisible |
| 191 | +trait Equal[F] { def equal(a: F, b: F): Boolean } |
| 192 | +``` |
| 193 | + |
| 194 | +```tut:silent |
| 195 | +trait Monoid[F] { |
| 196 | + def zero: F |
| 197 | + def append(a: F, b: F): F |
| 198 | + trait MonoidLaw { |
| 199 | + def associative(f1: F, f2: F, f3: F)(implicit F: Equal[F]) = |
| 200 | + F.equal(append(f1, append(f2, f3)), append(append(f1, f2), f3)) |
| 201 | + def leftIdentity(a: F)(implicit F: Equal[F]) = F.equal(a, append(zero, a)) |
| 202 | + def rightIdentity(a: F)(implicit F: Equal[F]) = F.equal(a, append(a, zero)) |
| 203 | + } |
| 204 | +} |
| 205 | +``` |
| 206 | + |
| 207 | +`List[G]` is the canonical implementation of the Monoid Algebra. |
| 208 | + |
| 209 | +`List[Set[G]]` is the canonical implementation of Classical Linear Logic (CLL). |
| 210 | + |
| 211 | +--- |
| 212 | + |
182 | 213 | # References
|
183 | 214 |
|
184 |
| -* [Algebraic Data Types](http://tpolecat.github.io/presentations/algebraic_types.html) |
185 |
| -by Rob Norris (@tpolecat) |
186 |
| -* [Shapeless 2.1.0 Feature Overview](https://github.com/milessabin/shapeless/wiki/Feature-overview:-shapeless-2.1.0) |
187 |
| -* [I am not a monad, I am a free algebra](http://biosimilarity.blogspot.com/2011/08/i-am-not-monad-i-am-free-algebra-pt-1.html) |
188 |
| -* [Sweeping crap APIs under the rug](http://tpolecat.github.io/presentations/lambdaconf14.html) |
189 |
| -by Rob Norris (@tpolecat) |
| 215 | +* [Algebraic Data Types](http://tpolecat.github.io/presentations/algebraic_types.html) by Rob Norris (@tpolecat) |
| 216 | +* [Encoding of GADTs in Scala](https://github.com/scalaz/scalaz-stream/blob/topic/0.8-redesign/implementation-notes.markdown#gadts) by Paul Chiusano (@pchiusano) |
| 217 | +* [Shapeless 2.1.0 Feature Overview](https://github.com/milessabin/shapeless/wiki/Feature-overview:-shapeless-2.1.0) by Miles Sabin (@milessabin) |
| 218 | +* [I am not a monad, I am a free algebra](http://biosimilarity.blogspot.com/2011/08/i-am-not-monad-i-am-free-algebra-pt-1.html) by Greg Meredith |
| 219 | +* [Sweeping crap APIs under the rug](http://tpolecat.github.io/presentations/lambdaconf14.html) by Rob Norris (@tpolecat) |
190 | 220 |
|
191 | 221 | </textarea>
|
192 | 222 | <script src="http://gnab.github.io/remark/downloads/remark-latest.min.js" type="text/javascript"></script>
|
|
0 commit comments