我想定义以下类型类Mapping
:
{-# LANGUAGE MultiParamTypeClasses #-}
class Mapping k v m where
empty :: m v
insert :: k -> v -> m v -> m v
search :: k -> m v -> Maybe v
delete :: k -> m v -> m v
Run Code Online (Sandbox Code Playgroud)
一个例子Mapping
是Data.Map.Map
{-# LANGUAGE ..., FlexibleInstances #-}
instance Ord k => Mapping k v (Map.Map k) where
empty = Map.empty
search = Map.lookup
insert = Map.insert
delete = Map.delete
Run Code Online (Sandbox Code Playgroud)
现在我想创建一个类型Trie :: * -> * -> * -> *
,如
{-# LANGUAGE ..., UndecidableInstances #-}
data Trie m k v = Trie {
trValue :: Maybe v,
trChildren :: m (Trie m k v)
}
instance Mapping k (Trie m k v) m => Mapping [k] v (Trie m k) where
search [] tree = trValue tree
search (x:xs) tree =
search xs =<< search x (trChildren tree)
Run Code Online (Sandbox Code Playgroud)
到目前为止好,现在我也想定义Trie
的insert
和empty
,而这也正是我进入的问题.
我会讨论,empty
因为它更简单,insert
无论如何都需要它..如果我试试这个:
instance Mapping k (Trie m k v) m => Mapping [k] v (Trie m k) where
empty = Trie { trValue = Nothing, trChildren = empty }
...
Run Code Online (Sandbox Code Playgroud)
这让我得到以下错误:
Could not deduce (Mapping k (Trie m k1 v) (m k1))
from the context (Mapping [k1] v (Trie m k1),
Mapping k1 (Trie m k1 v) (m k1))
arising from a use of `empty' at test.hs:27:49-53
Possible fix:
add (Mapping k (Trie m k1 v) (m k1)) to the context of
the instance declaration
or add an instance declaration for (Mapping k (Trie m k1 v) (m k1))
In the `trChildren' field of a record
In the expression: Trie {trValue = Nothing, trChildren = empty}
In the definition of `empty':
empty = Trie {trValue = Nothing, trChildren = empty}
Run Code Online (Sandbox Code Playgroud)
我试过并试图解决它但失败了.
有谁知道如何使它工作?它甚至可能吗?
GS *_*ica 14
添加功能依赖:
{-# LANGUAGE ..., FunctionalDependencies #-}
class Mapping k v m | m -> k where
...
Run Code Online (Sandbox Code Playgroud)
之前得到的错误是因为程序对于在某些地方使用哪种键类型是模糊的,因此关于类型变量的错误k1
.函数依赖性允许从地图类型推导出密钥类型(通过声明只有一个可能的答案),它处理这个问题.
用于演示Ganesh答案的代码:
{-# LANGUAGE FlexibleInstances, FunctionalDependencies, MultiParamTypeClasses, StandaloneDeriving, UndecidableInstances #-}
import qualified Data.Map as Map
import Data.Maybe (fromMaybe)
class Mapping k m | m -> k where
empty :: m v
insert :: k -> v -> m v -> m v
search :: k -> m v -> Maybe v
delete :: k -> m v -> m v
instance Ord k => Mapping k (Map.Map k) where
empty = Map.empty
search = Map.lookup
insert = Map.insert
delete = Map.delete
data Trie m v = Trie {
trValue :: Maybe v,
trChildren :: m (Trie m v)
}
deriving instance (Show v, Show (m (Trie m v))) => Show (Trie m v)
trieMod :: Mapping k m => Maybe v -> [k] -> Trie m v -> Trie m v
trieMod val [] trie = trie { trValue = val }
trieMod val (x:xs) trie =
trie { trChildren = insert x newChild children }
where
children = trChildren trie
newChild = trieMod val xs prevChild
prevChild = fromMaybe empty . search x $ children
instance Mapping k m => Mapping [k] (Trie m) where
empty = Trie { trValue = Nothing, trChildren = empty }
search [] trie = trValue trie
search (x:xs) trie =
search xs =<< search x (trChildren trie)
insert key val = trieMod (Just val) key
delete = trieMod Nothing
type TernarySearchTree a = Trie (Map.Map a)
Run Code Online (Sandbox Code Playgroud)
顺便说一句:如果没有功能依赖,我们可能需要在烦人的界面上妥协并使用函数表而不是类型类.