Isabelle:通用数据类型和等价

时间:2016-01-07 13:48:32

标签: generics types isabelle unification

我刚刚开始在Isabelle中创建我的类型泛型现在,一旦我开始使用括号,我会收到有趣的错误消息。

theory Scratch
imports Main
begin
no_notation plus (infixl "+" 65)

datatype typeA = aa
datatype typeB = bb
datatype ('a, 'b) genericType = cc | 
                                plus 'a 'b (infixr "+" 35)

lemma test1 : "x + y ≡ x + y"
by auto
lemma test2 : "x + y + z ≡ x + y + z"
by auto
lemma test3 : "x + (y + z) ≡ x + y + z"
by auto
lemma test4 : "(x + y) + z ≡ x + y + z"
lemma test5 : "(aa + aa) + aa ≡ aa + aa + aa"
lemma test6 : "(cc + cc) + cc ≡ cc + cc + cc"
lemma test7 : "(cc + aa) + cc ≡ cc + aa + cc"
lemma test8 : "(aa + cc) + cc ≡ cc + aa + cc"

test1-3一切正常,但测试4和5导致此错误:

Type unification failed: Occurs check!

Type error in application: incompatible operand type

Operator:  op ≡ ((x + y) + z) :: ((??'a, ??'b) genericType, ??'c) genericType ⇒ prop
Operand:   x + y + z :: (??'a, (??'b, ??'c) genericType) genericType

并且分别是:

Type unification failed: Clash of types "typeA" and "(_, _) genericType"

Type error in application: incompatible operand type

Operator:  op ≡ ((aa + aa) + aa) :: ((typeA, typeA) genericType, typeA) genericType ⇒ prop
Operand:   aa + aa + aa :: (typeA, (typeA, typeA) genericType) genericType

test6和奇怪的测试7/8再次没问题。更换" cc"与" aa"但是会导致同样的问题。

PS:我可以以某种方式指定类型' a和' b?

1 个答案:

答案 0 :(得分:1)

Isabelle中的两种类型只有在语法上相等时才相等。左侧和右侧具有不同类型的等式不是很好的类型,将导致错误消息。

您将+定义为右关联,因此x + y + z只是x + (y + z)的缩写。因此,test1test3的引理是正确的,因为等式的两个边在句法上是相等的。

test4test5中,左侧有((_,_) genericType, _) genericType类型。另一方面,右侧有(_, (_,_) genericType) genericType类型。

为了更好地说明一下,我会在中缀中为+genericType:左侧有类型(_ + _) + _;右侧有_ + (_ + _)。这两种类型在语法上是相同的,因此是不同的类型。它们显然是同构的,但仍然不同。

test6test8类型正确的事实与多态性有关:cc与原型类型变量'a'b具有多态性可以被实例化为任何东西,并且重要的是,如果cc在一个术语中多次出现,则类型变量可以在每次出现时被实例化为不同的类型。使用前面示例中的xyz等变量,

但请注意,虽然test6test8可以进行类型检查,但很明显它们都是假的,正如QuickCheck会告诉你的那样。

至于specifying类型变量:您可以使用类型注释术语,例如x + y :: (typeA, typeB) genericType(x :: typeA) + (y :: typeB)