Binary tree 在Isabelle中的树中搜索元素

Binary tree 在Isabelle中的树中搜索元素,binary-tree,isabelle,Binary Tree,Isabelle,我有一个测试元素是否在二叉树中的函数: fun lookup::"int⇒bst⇒bool" where "lookup x _ = false" | "lookup x bst = ( if x = root(bst) then true else if x≤root(bst) then lookup x left(bst) else lookup x right(bst))" 我收到了错误信息 类型统一失败: “bst”和“int”类型冲突 数据类型bst定义为 dataty

我有一个测试元素是否在二叉树中的函数:

fun lookup::"int⇒bst⇒bool" where
"lookup x _ = false" |
"lookup x bst = ( if x = root(bst) then true else if x≤root(bst) then lookup x left(bst)  else    lookup x right(bst))" 
我收到了错误信息 类型统一失败: “bst”和“int”类型冲突

数据类型bst定义为

  datatype bst  = Leaf | Node int bst bst

这里有什么问题?

看起来您的数据类型声明没有提到叶的任何关联值。所以,它可能看起来像

datatype_new bst  = Leaf int | Node int bst bst
然后该函数只检查当前节点的所有构造函数:

fun lookup :: "int ⇒ bst ⇒ bool" where
    "lookup x (Leaf y) = x = y" |
    "lookup x (Node y leftbst rightbst) =
        (if x = y then True
         else (if x ≤ y then lookup x leftbst else lookup x rightbst))"
简单的回答是您误用了
root
。它是、来自,并且
根目录的类型显示在错误消息中:

root :: nat => real => real
我点击了
root
,它带我去了
NthRoot.thy
中的函数

从这里开始,我基本上是在抱怨你让我比我想要的更努力地工作,来回答你的问题,即使现在,我也认为我是在重复你所说的

你在评论中给了我这句话:“导入主树”。但是,在仅使用
Main
创建THY之后,THY显然不是您正在做的事情,因为我没有收到错误消息
类型统一失败:类型“bst”和“nat”的冲突

另外,由于没有定义根,
root
,因此它是一个变量。我从两个方面看到了这一点。我试着点击它,但什么也没发生。然后我注意到它是蓝色的,这意味着它是一个局部变量

因此,我导入了
Complex\u Main
,如下所示,我得到了您所说的错误消息。我对二叉树知之甚少,但错误消息中显示的
root
类型可以很快告诉您
root
很可能不是您想要的,因为它使用的是
real

theory Scratch2
imports Complex_Main
begin
datatype bst  = Leaf | Node int bst bst

fun lookup :: "int => bst => bool" where
  "lookup x _ = false" |
  "lookup x bst = (if x = root(bst) then true else if x ≤ root(bst) 
                   then lookup x left(bst)  else    lookup x right(bst))"
(*Type unification failed: Clash of types "bst" and "nat"

  Type error in application: incompatible operand type

  Operator:  root :: nat => real => real*)
end
无论如何,人们不希望看到太多的问题来源,也不希望看到太少。如果你提供了神奇的数量,他们所要做的就是剪切和粘贴,那么他们就不必那么努力地回答你的问题

从你的最后一个问题中,我知道从安德烈亚斯的话中可以找到

安德烈亚斯是像我和你这样的人的答案。如果你想增加像他这样的人回答问题的机会,那么你希望他必须尽可能少地工作来找出你的问题


一个简单的工作示例有助于确保每个人都在同一页上,甚至在你提问之前发现你这边的一些错误。

的类型是什么?顺便说一句,函数总是返回
,因为第一个模式总是匹配的。您应该显示THY的完整工作示例,以便除其他外,它将显示导入的
。您在那里使用了4个函数,
查找
,和
。如果它们是你自己的,那么人们需要知道它们的类型。如果它们来自
src/HOL
,那么在
Complex_Main
中有很多是人们不使用的,或者在库中有很多是人们不导入、使用甚至不知道的⇒英国理工学院⇒bool“where”lookup x Leaf=(如果x=Leaf,则为true,否则为false)“|”lookup x(a leftbst rightbst)=(如果x=a,则为true,否则为x≤a then lookup x leftbst else lookup x rightbst)“
但我通常不理解“bst”和“int”类型冲突的问题,或者我应该写什么来代替被认为是int的x,或者对于被认为是bst的bst,我会记住这一点@Alexander:这是我使用函数的代码:
theory Ex02导入主树开始数据类型_new bst=Leaf int | Node int bst bst有趣的查找::“int⇒ 英国理工学院⇒ bool“where”lookup x(叶y)=(x=y)”|“lookup x(节点y leftbst rightbst)=(如果x=y,则为真,否则为真(如果x≤ y然后lookup x leftbst else lookup x rightbst)“end
我得到的错误消息是“Variable”true“仅出现在右手边”。@user2057890:我拼写错误-它也应该是
true
@user2057890,在这种情况下,类型和函数都不使用理论树,因此可以将其从理论中排除(除非你想使用那里的树,而不是你的)。
theory Scratch2
imports Complex_Main
begin
datatype bst  = Leaf | Node int bst bst

fun lookup :: "int => bst => bool" where
  "lookup x _ = false" |
  "lookup x bst = (if x = root(bst) then true else if x ≤ root(bst) 
                   then lookup x left(bst)  else    lookup x right(bst))"
(*Type unification failed: Clash of types "bst" and "nat"

  Type error in application: incompatible operand type

  Operator:  root :: nat => real => real*)
end