我想在Isabelle中构建一种类型层次结构:
B is of type A ( B::A )
C and D are of type of B (C,D ::B)
E and F are of type of C (E,F ::C)
在Isabelle中对此进行编码的最佳方法是什么?有没有直接的方法来定义这个层次结构,或者我需要一个解决方法。我应该在哪里看?
PS:假设A..F都是抽象的,并且在每种类型上定义了一些函数)
由于
答案 0 :(得分:3)
(第一点注意:在我看来,正如你一直在使用它,“#34; abstract"”这个词没有明确的含义。可以使用用于定义类型的Isabelle关键词如我所见,定义抽象或具体类型,或可以同时考虑两者的类型,例如'a list
。)
(第二注:在这里,我考虑到你在面向对象编程中的比较。对于编程语言和逻辑语言,应该注意,我只是观察者。)
对于我自己来说,这一切都变成了一个大模糊,在我看来,我停止明确区分术语和类型,所以像Rene Thiemann这样的答案有助于重新考虑这一点。
不过,我不知道多少,我可以简单地考虑一下你的短语" type hierarchy"与"类型层次结构"同义,并进一步与"排序层次结构"同义。这使我能够愉快地为你的问题提供答案,尽管其他人的接受可能是不稳定的。
当然,在Isabelle词汇表中,类型和种类并不是同义词,但在Isabelle / HOL中,它们是不可分割的,因为每个HOL术语都有一个类别。 (一个危险的主张,因为类型prop
在HOL中使用,也许我仍然不明白空类型是什么。):
'a::type
之间的差异
通过区域设置Isabelle提供继承(以及其他方式?),其中类型类是(或包括),其中包括区域设置:
我创建了5个类型类,满足您的要求,至少是简单的。每个人都有自己的身份功能。 +
符号是神奇的继承符号。
declare [[show_sorts, show_consts]]
(*The 5 type classes.*)
class cA =
fixes idA :: "'a => 'a"
assumes idA_is_id: "idA x = x"
class cB = cA +
fixes idB :: "'a => 'a"
assumes idB_is_id: "idB x = x"
class cC = cB +
fixes idC :: "'a => 'a"
assumes idC_is_id: "idC x = x"
class cD = cB +
fixes idD :: "'a => 'a"
assumes idD_is_id: "idD x = x"
class cE = cC +
fixes idE :: "'a => 'a"
assumes idE_is_id: "idE x = x"
class cF = cC +
fixes idF :: "'a => 'a"
assumes idF_is_id: "idF x = x"
有效:
(*Any of type class cB, cC, cD, and cF can be used where cA can be used.*)
term "idA (x::'a::cA)"
term "idA (x::'a::cB)"
term "idA (x::'a::cC)"
term "idA (x::'a::cD)"
term "idA (x::'a::cE)"
term "idA (x::'a::cF)"
继承的更多例子:
(*Use of idC shows that cE inherited all of what's in cC.*)
term "idC :: ('a::cC => 'a::cC)"
term "idC :: ('a::cE => 'a::cE)"
term "idC (x::'a::cE)"
(*But here, there's a type sort clash, because cC didn't inherit from cE. *)
term "idE :: ('a::cC) => ('a::cC)"
现在,通过将nat
实例化为类型类cF
来添加一些具体内容:
(*It took me over an hour to see I was using 'idA_cA' instead of 'idA_nat'.
99% of the battle can be learning the syntax.*)
instantiation nat :: "{cF}"
begin
definition idA_nat :: "nat => nat" where "idA_nat == id"
definition idB_nat :: "nat => nat" where "idB_nat == id"
definition idC_nat :: "nat => nat" where "idC_nat == id"
definition idF_nat :: "nat => nat" where "idF_nat == id"
instance (*proof. Use 'proof' and see 4 subgoals: the need for 4 id functions.*)
by(default, auto simp add: idA_nat_def idB_nat_def idC_nat_def idF_nat_def)
end
(*When I instantiated nat as cF, I had to instantiate nat as cA, cB, and cC,
because I had not previously done so. Normally, you would be adding useful
'fixes' and 'assumes' with each type class, and proving useful theorems about
them.*)
value "idA (0::nat)"
value "idB (0::nat)"
value "idC (0::nat)"
value "idF (0::nat)"
(*You have to show that a type satisfies all of the 'fixes' and 'assumes'
of a type class. But additional proved theorems, you get those for free.
That's the great benefit of type classes. You may have 20 types that
you instantiate as a type class, but you only have to prove 'extraneous'
theorems once, that being for the type class, based on the 'fixes' and
'assumes' (and other things defined with other keywords).*)
我还会定期尝试学习如何正确使用Isabelle词汇。所以,我正在查看关键字subclass
:
subclass
,第97页isar-ref.pdf
我不妨证明cF
是cA
的子类,以表明观点:
context cF
begin
subclass cA ..
end
cA
是cF
的子类,反之亦然?好吧,如果我没有在isar-ref.pdf的子类定义中替换c
和d
,我就不会已经提交了。
暴露于语言的本质,无论是逻辑还是编程,你发现它们都没有给你任何东西,所以你最终想要所有的东西都分别给你。
您可以轻松地在C ++中定义对象,但尝试用C ++定义代数数据类型。
你可以:
但与Isabelle / HOL类似,这与Haskell相似并不容易,而且我尝试过的解决方案的模式匹配能力甚至不是很接近。
但是,嘿,我完全收集了我们想要的所有这些东西。
答案 1 :(得分:1)
至少在Isabelle / HOL中,这不是直接可能,因为术语和类型之间存在严格的分离,其中::
在左侧采用一个术语和一个类型在右手侧。
因此,在撰写B :: A
时,B
是一个术语,而A
是一种类型。然后,不可能写C :: B
。
我不确定,但也许你的设置可以直接在Isabelle / ZF中建模。
关于变通方法,您可以按集交换类型,并使用成员资格:
::
。然后你可以写
context
fixes A B C D E F
assumes
"B : A"
"C : B" "D : B"
"E : C" "F : C"
begin
...
end
但是您没有得到类型检查器的支持,而A
的类型为'a set set set
。
希望这有帮助。