Generics Isabelle:泛型数据类型和等价性

Generics Isabelle:泛型数据类型和等价性,generics,types,isabelle,unification,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) l

我刚开始在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一切正常,但test4和test5会导致以下错误:

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和奇怪的test7/8又恢复正常了。不过,用“aa”替换“cc”会导致同样的问题


PS:我能不能具体说明“a”和“b”两种类型?

只有当它们在语法上真正相等时,伊莎贝尔中的两种类型才是相等的。左侧和右侧具有不同类型的方程式类型不正确,将导致错误消息

您将
+
定义为右关联,因此
x+y+z
只是
x+(y+z)
的缩写。因此,从
test1
test3
的引理是非常正确的,因为等式的两边在语法上是相等的

test4
test5
中,左侧有type
((u,u)genericType,u)genericType
。另一方面,右侧有type
(,(,,)genericType)genericType

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

事实上,
test6
test8
是类型正确的,这与多态性有关:
cc
是多态的,原理图类型变量
'a
'b
可以实例化为任何对象,重要的是,如果
cc
在一个术语中多次出现,类型变量可以在每次事件中实例化为不同的类型。对于前面示例中的
x
y
z
等变量,这是不可能的

然而,请注意,虽然可以对
test6
test8
进行类型检查,但正如QuickCheck所告诉您的,它们显然是错误的


至于
指定类型变量:你可以用类型来注释术语,例如
x+y::(typeA,typeB)genericType
(x::typeA)+(y::typeB)

我理解,它们是不相等的。我的问题不是,引理在某种程度上显然是不正确的。我的问题是,我甚至不能说出这个词。我明白,它们是不平等的。我的问题不是,引理在某种程度上显然是不正确的。我的问题是,我甚至不能说出这个词。至于规格,我的意思是更像是指定‘a’和‘b’。对不起,有几次输入错误好的,我现在明白了。谢谢你的回答。Guess==不是我想要的:/i我想用公理化为上面这样的术语(有用的术语)定义相等。你能给我指个地方吗?