Haskell 编译错误 - 保护类型不匹配

Haskell Compilation Error - Guard Type mismatching

我一直停留在这个特定阶段,一直在解决我遇到的问题,我对 haskell 的体验仍处于初学者水平。

在尝试创建一个函数来创建一个节点(由一个字符串 "Key" 和一个值组成)插入到二叉搜索树中时,我尝试了一种使用保护括号的方法。我似乎误解了如何在这里做某事,我收到的错误对我来说没有意义。


module Ex03 where                                                                                                                                                                                                  
import Data.List ((\))                                                                                                                                                                                            

-- Datatypes -------------------------------------------------------------------                                                                                                                                   

-- do not change anything in this section !                                                                                                                                                                        

-- Binary Tree                                                                                                                                                                                                     
data BT a b                                                                                                                                                                                                        
  = Leaf                                                                                                                                                                                                           
  | Branch (BT a b) a b (BT a b)                                                                                                                                                                                   
  deriving (Eq, Show)                                                                                                                                                                                              

-- association list                                                                                                                                                                                                
type Assoc a b = [(a,b)]                                                                                                                                                                                           

-- lookup binary (search) tree                                                                                                                                                                                     
lkpBST :: Ord a1 => BT a1 a -> a1 -> Maybe a                                                                                                                                                                       
lkpBST Leaf _  =  Nothing                                                                                                                                                                                          
lkpBST (Branch left k d right) k'                                                                                                                                                                                  
 | k < k'     =  lkpBST left k'                                                                                                                                                                                    
 | k > k'     =  lkpBST right k'                                                                                                                                                                                   
 | otherwise  =  Just d                                                                                                                                                                                            

-- Coding Part 1 (13 Marks)                                                                                                                                                                                        

-- insert into binary (search) tree                                                                                                                                                                                
insBST :: Ord a => a -> b -> BT a b -> BT a b                                                                                                                                                                      
insBST labelX valueX Leaf = Branch Leaf labelX valueX Leaf                                                                                                                                                         
{-                                                                                                                                                                                                                 
  insBST a 1 Branch Leaf b 1 Leaf                                                                                                                                                                                  
@?= Branch Branch Leaf a 1 Leaf  b 2 Leaf                                                                                                                                                                          
-}                                                                                                                                                                                                                 
insBST labelX valueX (Branch left labelRoot valueRoot right)                                                                                                                                                       
    | valueX < valueRoot = (Branch (insBST labelX valueX left) labelRoot valueRoot right)                                                                                                                          
    | valueX > valueRoot = (Branch left labelRoot valueRoot (insBST labelX valueX right))                                                                                                                          
    | otherwise = Branch left labelX valueX right                                                                                                                                                                  

-- Coding Part 2 (6 Marks)                                                                                                                                                                                         

-- convert an association list to a binary search tree                                                                                                                                                             
assoc2bst :: Ord a => Assoc a b -> BT a b                                                                                                                                                                          
assoc2bst _ = error "assoc2bst not yet implemented"                                                                                                                                                                

-- Coding Part 3 (6 Marks)                                                                                                                                                                                         

-- convert a binary search tree into an (ordered) association list                                                                                                                                                 
bst2assoc :: Ord c =>  BT c e -> Assoc c e                                                                                                                                                                         
bst2assoc _ = error "bst2assoc not yet   



我收到的错误如下

Hours of hacking await!
If I break, you can:
  1. Restart:           M-x haskell-process-restart
  2. Configure logging: C-h v haskell-process-log (useful for debugging)
  3. General config:    M-x customize-mode
  4. Hide these tips:   C-h v haskell-process-show-debug-tips
src/Ex03.hs:35:14: Could not deduce (Ord b) arising from a use of ‘<’ …
    from the context (Ord a)
      bound by the type signature for
                 insBST :: Ord a => a -> b -> BT a b -> BT a b
      at /Users/eoindowling/college/3year2/michaelmas/CS3016-_Introduction_to_Functional_Programming/CSU34016-1920/Exercise03/src/Ex03.hs:28:11-45
    Possible fix:
      add (Ord b) to the context of
        the type signature for
          insBST :: Ord a => a -> b -> BT a b -> BT a b
    In the expression: valueX < valueRoot
    In a stmt of a pattern guard for
                   an equation for ‘insBST’:
      valueX < valueRoot
    In an equation for ‘insBST’:
        insBST labelX valueX (Branch left labelRoot valueRoot right)
          | valueX < valueRoot
          = (Branch (insBST labelX valueX left) labelRoot valueRoot right)
          | valueX > valueRoot
          = (Branch left labelRoot valueRoot (insBST labelX valueX right))
          | otherwise = Branch left labelX valueX right
Compilation failed.

我这里的 guard 语句有什么问题?我见过类似的方法,但我不明白我做错了什么。

您正在比较类型 bvalueXvalueRoot。错误消息说这样做需要一个约束 Ord b,但你只有一个约束 Ord a

也许您是想比较 labelXlabelRoot(它们的类型为 a)?