2014-10-27 32 views
0

我有這個功能測試羯羊元素是二叉樹或不:搜索元素在樹中伊莎貝爾

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 

這裏有什麼問題?

+0

'root','left'和'right'的類型是什麼? – 2014-10-27 14:54:33

+0

順便說一句,函數總是返回'false',因爲第一個模式總是匹配。 – 2014-10-27 14:56:51

+1

你應該展示一個THY的完整工作示例,除其他外,它將顯示'imports'。你在那裏使用4個函數,'root','lookup','left'和'right'。如果他們是你自己的,那麼人們需要知道類型。如果他們來自src/HOL,那麼'Complex_Main'中有很多人不使用,或者在圖書館中THYs中人們不會導入,使用甚至不知道。 – 2014-10-27 15:14:08

回答

1

它看起來像你的數據類型聲明沒有提及葉的任何關聯值。因此,它可能看起來像

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))" 
0

簡短的回答是,你濫用root。這是NthRoot.root,從src/HOL/NthRoot.thy,並root該類型是在錯誤信息中顯示:

root :: nat => real => real 

我CNTL-點擊root,它帶我去功能NthRoot.thy

從這裏,我基本上抱怨說,你讓我比我想要的更努力,回答你的問題,甚至現在,我假設我讓我變成了你正在談論的重複內容。

你在評論中給了我這句話:「進口主樹」。但是,在THY僅使用Main之後,THY顯然不是您正在做的,因爲我沒有收到錯誤消息Type unification failed: Clash of types "bst" and "nat"

theory Scratch 
imports Main "~~/src/HOL/Library/Tree" 
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))"    
end 

此外,在THY中,root沒有定義,它是一個變量。我以兩種方式看到了這一點。我試圖讓CNTL點擊它,但什麼都沒發生。然後我注意到它是藍色的,這意味着它是一個局部變量。

因此,我導入了Complex_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 

無論如何,人們不想看到太多的問題來源,他們不想看到太少。如果你提供魔術數量,而他們所要做的只是剪切和粘貼,那麼他們不需要努力回答你的問題。

從你上一個問題,Predefined functions for Binary trees in Isabelle,我知道從Andreas說什麼Tree

安德烈亞斯是像我和你這樣的人的答案。如果你想增加像他這樣的人回答問題的機會,那麼你希望他必須儘可能少地工作以找出你的問題。

一個最簡單的工作示例可以幫助確保每個人都在同一頁面上,甚至在提出問題之前在您的結尾處發現一些錯誤。

+0

我會牢記這一點。 @Alexander:這是我的代碼,使用你的函數:'理論Ex02 進口主樹 開始 datatype_new bst = Leaf int |節點int bst bst fun lookup ::「int⇒bst⇒bool」其中 「lookup x(Leaf y)=(x = y)」|如果x = y,則返回true else(如果x≤y,則查找x leftbst else lookup x rightbst))「end' 我得到的錯誤消息是」Variable「true 「僅在右側發生」。 – user2057890 2014-10-27 18:16:30

+0

@ user2057890:我拼錯了它 - 它應該是'True'。 – 2014-10-27 18:29:54

+0

@ user2057890另外,在這種情況下,既沒有類型也沒有函數使用_Tree_理論,所以你可以從理論中排除它(除非你想從那裏使用樹,而不是你的理論)。 – 2014-10-27 18:36:57