miranda

Abusing the algebra of algebraic data types - why does this work?

爱⌒轻易说出口 提交于 2019-11-26 08:38:36
问题 The \'algebraic\' expression for algebraic data types looks very suggestive to someone with a background in mathematics. Let me try to explain what I mean. Having defined the basic types Product • Union + Singleton X Unit 1 and using the shorthand X² for X•X and 2X for X+X et cetera, we can then define algebraic expressions for e.g. linked lists data List a = Nil | Cons a (List a) ↔ L = 1 + X • L and binary trees: data Tree a = Nil | Branch a (Tree a) (Tree a) ↔ T = 1 + X • T² Now, my first