如何定义一个可以添加“功能”而不会遇到重叠实例的环境?
假设我们有以下数据类型和类型类:
type Name = String
data Fruit = Orange | Pear | Apple
data Vegetable = Cucumber | Carrot | Spinach
data Legume = Lentils | Chickpeas | BlackEyedPeas
class HasFruit e where
getFruit :: e -> Name -> Maybe Fruit
class HasVegetable e where
getVegetable :: e -> Name -> Maybe Vegetable
class HasLegume e where
getLegume :: e -> Name -> Maybe Legume
现在我们想要定义几个需要环境中某些成分的函数:
data Smootie
mkSmoothie :: (HasFruit e, HasVegetable e) => e -> Smootie
mkSmoothie = undefined
data Salad
mkSalad :: (HasVegetable e, HasLegume e) => e -> Salad
mkSalad = undefined
我们为 Has*
定义一些实例:
instance HasFruit [Fruit] where
getFruit = undefined
instance HasVegetable [Vegetable] where
getVegetable = undefined
instance HasLegume [Legume] where
getLegume = undefined
最后,我们想定义一个准备冰沙和沙拉的函数:
cook :: (Smootie, Salad)
cook = let ingredients = undefined in
(mkSmoothie ingredients, mkSalad ingredients)
现在第一个问题是,要传递什么作为成分才能使用上面定义的实例?我的第一个解决方案是使用元组:
instance HasFruit e0 => HasFruit (e0, e1, e2) where
getFruit (e0, _, _) = getFruit e0
instance HasVegetable e1 => HasVegetable (e0, e1, e2) where
getVegetable (_, e1, _) = getVegetable e1
instance HasLegume e2 => HasLegume (e0, e1, e2) where
getLegume (_, _, e2) = getLegume e2
cook :: (Smootie, Salad)
cook = let ingredients = ([Orange], [Cucumber], [BlackEyedPeas]) in
(mkSmoothie ingredients, mkSalad ingredients)
尽管这很麻烦,但还是有效的。但现在假设我们
决定添加一个 mkStew
,它需要一些 HasMeat
实例。
然后我们必须更改上面的所有实例。此外,
如果我们想单独使用 mkSmothie
,我们不能仅仅
传递 ([Orange], [Cucumber])
因为没有定义实例
为了它。
我可以定义:
data Sum a b = Sum a b
以及类似的实例:
instance HasFruit e0 => HasFruit (Sum e0 e1) where
getFruit (Sum e0 _) = getFruit e0
instance HasVegetable e1 => HasVegetable (Sum e0 e1) where
getVegetable (Sum _ e1) = getVegetable e1
instance HasLegume e1 => HasLegume (Sum e0 e1) where
getLegume (Sum _ e1) = getLegume e1
但以下内容不起作用(没有 HasVegetable [Legume]
实例):
cook1 :: (Smootie, Salad)
cook1 = let ingredients = Sum [Orange] (Sum [Cucumber] [BlackEyedPeas]) in
(mkSmoothie ingredients, mkSalad ingredients)
这个实例将会重叠!
instance HasVegetable e0 => HasVegetable (Sum e0 e1) where
getVegetable (Sum e0 e1) = getVegetable e0
有没有办法优雅地解决这个问题?
最佳答案
当前 Sum
实例的问题是我们不知道我们正在寻找的对象是在左边还是在右边。
计划如下:环境的每个组件都应声明它提供的功能,以便我们可以搜索它。
Gist这个答案。
声明功能
随着环境的组成,我们将需要一个(类型级)数据结构来承载不同部分的功能。我们将使用二叉树,这样我们就可以保留组件的结构。
-- Tree of capabilities (ingredient categories)
data Tree a = Leaf a | Node (Tree a) (Tree a)
与环境相关的功能是通过此类型系列声明的。
type family Contents basket :: Tree *
type instance Contents [Fruit] = 'Leaf Fruit
type instance Contents [Vegetable] = 'Leaf Vegetable
type instance Contents [Legume] = 'Leaf Legume
-- Pair of environments
data a :& b = a :& b -- "Sum" was confusing
-- The capabilities of a pair are the pair of their capabilities.
type instance Contents (a :& b) = 'Node (Contents a) (Contents b)
-- e.g., Contents ([Fruit] :& [Vegetable]) = 'Node ('Leaf Fruit) ('Leaf Vegetable)
查找功能
正如一开始提到的,当遇到一对 :&
时,我们需要判断是在左组件还是右组件中查找能力。因此,我们从一个函数(在类型级别)开始,如果可以在树中找到该功能,该函数将返回 True
。
type family In (x :: *) (ys :: Tree *) :: Bool where
In x (Leaf y) = x == y
In x (Node l r) = In x l || In x r
type family x == y :: Bool where
x == x = 'True
x == y = 'False
有
类
这个类现在有一个父类(super class)约束:我们正在寻找的功能确实可用。
class (In item (Contents basket) ~ 'True)
=> Has item basket where
get :: basket -> Name -> Maybe item
这似乎是多余的,因为如果找不到该功能,实例解析无论如何都会失败,但精确的父类(super class)约束有好处:
防止错误:如果缺少某些内容,编译器会提前发出警告;
一种文档形式,通知我们实例何时可能存在。
叶实例
instance Has Fruit [Fruit] where
get = (...)
instance Has Vegetable [Vegetable] where
get = (...)
instance Has Legume [Legume] where
get = (...)
我们不需要编写诸如Has Fruit [Vegetable]
之类的可疑实例;我们实际上不能:它们会与父类(super class)约束相矛盾。
(:&)
的实例
我们需要遵循一个新类,PairHas
,它将区分两侧 In
谓词的结果,以确定要放大环境的哪一部分.
instance PairHas item a b (In item (Contents a)) (In item (Contents b))
=> Has item (a :& b) where
get = getPair
我们再次使父类(super class)约束对于 PairHas
的意图更加精确。 inA
和 inB
只能分别用 In item (Contents a)
和 In item (Contents b)
实例化,并且它们的析取应该为 True
,这意味着 item
可以在其中至少一个中找到。
class ( In item (Contents a) ~ inA
, In item (Contents b) ~ inB
, (inA || inB) ~ 'True)
=> PairHas item a b inA inB where
getPair :: (a :& b) -> Name -> Maybe item
当然,我们有两个实例分别向左和向右移动,使用递归 Has
约束(请注意,Has
通过其自己的父类(super class)约束提供了一个相等性)。
instance ( Has item a
, In item (Contents b) ~ 'False)
=> PairHas item a b 'True 'False where
getPair (a :& _) = get a
instance ( In item (Contents a) ~ 'False
, Has item b)
=> PairHas item a b 'False 'True where
getPair (_ :& b) = get b
如果双方能力相同怎么办?我们将认为这是一个错误,并要求用户通过其他机制显式隐藏重复功能之一。我们可以使用 TypeError 在编译时打印自定义错误消息。我们也可以默认选择任一侧。
instance (TypeError (Text "Duplicate contents") -- can be more descriptive
, In item (Contents a) ~ 'True
, In item (Contents b) ~ 'True)
=> PairHas item a b 'True 'True where
getPair = undefined
我们还可以为双方都为假的情况编写自定义错误消息。这有点令人惊讶,因为这与父类(super class)约束 (inA || inB) ~ 'True
相矛盾,但消息确实被打印出来,所以我们不会提示。
instance ( TypeError (Text "Not found") -- can be more descriptive
, In item (Contents a) ~ 'False
, In item (Contents b) ~ 'False
, 'False ~ 'True)
=> PairHas item a b 'False 'False where
getPair = undefined
我们做饭吧
现在我们可以安全地编写cook
:
cook :: (Smootie, Salad)
cook = let ingredients = [Orange] :& [Cucumber] :& [BlackEyedPeas] in
(mkSmootie ingredients, mkSalad ingredients)
您还可以查看如果重复或忘记某些成分会发生什么
cook :: (Smootie, Salad)
cook = let ingredients = [Orange] :& [Cucumber] :& [BlackEyedPeas] :& [Pear] in
(mkSmootie ingredients, mkSalad ingredients)
-- error: Duplicate contents
cook :: (Smootie, Salad)
cook = let ingredients = [Orange] :& [Cucumber] in
(mkSmootie ingredients, mkSalad ingredients)
-- error: Not found
关于haskell - 定义增量环境而不会遇到重叠实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49545045/