计算代数数据类型

时间:2014-05-28 22:18:17

标签: haskell theory algebraic-data-types

我正在阅读/听取Chris Taylor关于代数数据类型的演讲。

http://chris-taylor.github.io/blog/2013/02/10/the-algebra-of-algebraic-data-types/

还有一个关于功能类型的部分。特别是示例

data Bool = True | False
data Trio = First | Second | Third

根据法律

a -> b == B^A

鉴于

Trio -> Bool     should equal     8

为什么8而不是6通过乘法?

如果我正确理解这一点,那么具体的组合应该是

First  -> True
First  -> False
Second -> True
Second -> False
Third  -> True
Third  -> False

这不仅仅是Trio -> Bool的6个具体实现吗?

我错过了什么?

1 个答案:

答案 0 :(得分:18)

那些不是完整的实现。对于完整实现,它类似于从二进制计数从0到7(总共8 = 2 3 数字),每个实现的每一行代表三个位中的一个。所有可能性都是这样的(如果我们调用我们的函数f):

1)

f First  = False
f Second = False
f Third  = False

2)

f First  = True
f Second = False
f Third  = False

3)

f First  = False
f Second = True
f Third  = False

4)

f First  = True
f Second = True
f Third  = False

5)

f First  = False
f Second = False
f Third  = True

6)

f First  = True
f Second = False
f Third  = True

7)

f First  = False
f Second = True
f Third  = True

8)

f First  = True
f Second = True
f Third  = True