我试图证明一些关于奇数和偶数自然数的公理。我在我的证明中使用了三种定义的数据类型。
data Nat = Z | S Nat
data Even (a :: Nat) :: * where
ZeroEven :: Even Z
NextEven :: Even n -> Even (S (S n))
data Odd (a :: Nat) :: * where
OneOdd :: Odd (S Z)
NextOdd :: Odd n -> Odd (S (S n))
我还为添加和乘法定义了以下类型系列。
type family Add (n :: Nat) (m :: Nat) :: Nat
type instance Add Z m = m
type instance Add (S n) m = S (Add n m)
type family Mult (n :: Nat) (m :: Nat) :: Nat
type instance Mult Z m = Z
type instance Mult (S n) m = Add (Mult n m) n
我有定义的功能,证明两个平均值的总和是均匀的,两个平均值的乘积是偶数。
evenPlusEven :: Even n -> Even m -> Even (Add n m)
evenPlusEven ZeroEven m = m
evenPlusEven (NextEven n) m = NextEven (evenPlusEven n m)
evenTimesEven :: Even n -> Even m -> Even (Mult n m)
evenTimesEven ZeroEven m = ZeroEven
evenTimesEven (NextEven n) m = evenPlusEven (EvenTimesEven n m) n
我使用的是GADTs
,DataKinds
,TypeFamilies
和UndecidableInstances
语言扩展程序以及GHC版本7.10.3。运行evenPlusEven
给出了我期望的结果,但是当我包含evenTimesEven
时,我收到了编译错误。错误是:
Could not deduce (Add (Add (Mult n1 m) n1) ('S n1)
~ Add (Mult n1 m) n1)
from the context (n ~ 'S ('S n1))
bound by a pattern with constructor
NextEven :: forall (n :: Nat). Even n -> Even ('S ('S n)),
in an equation for `evenTimesEven'
at OddsAndEvens.hs:71:16-25
NB: `Add' is a type function, and may not be injective
Expected type: Even (Mult n m)
Actual type: Even (Add (Mult n1 m) n1)
Relevant bindings include
m :: Even m
(bound at OddsAndEvens.hs:71:28)
n :: Even n1
(bound at OddsAndEvens.hs:71:25)
evenTimesEven :: Even n -> Even m -> Even (Mult n m)
(bound at OddsAndEvens.hs:70:1)
In the expression: evenPlusEven (evenTimesEven n m) n
In an equation for `evenTimesEven':
evenTimesEven (NextEven n) m = evenPlusEven (evenTimesEven n m) n
Mult
的类型族实例编译正常,如果我用错误抛出替换evenTimesEven
的最后一行,我可以编译代码,并且函数运行良好,输入为{{1} }这让我觉得我的ZeroEven
实例是正确的,我Mult
的实现是问题所在,但我不确定原因。
不应该evenTimesEven
和Even (Mult n m)
有同类吗?
答案 0 :(得分:6)
下面,我会滥用常用的数学符号。
j
由此,我们得到from the context (n ~ 'S ('S n1))
。
n = 2+n1
我们需要证明Expected type: Even (Mult n m)
偶数,即n*m
偶数。
(2+n1)*m
我们已证明Actual type: Even (Add (Mult n1 m) n1)
。这不一样。附加条款应为(n1*m)+n1
,而非m
,并且应添加两次。