我目前正在为我谨慎的数学课做个人项目,并试图在Haskell中形式化集理论。我们类中定义的集合是特定Universe的元素的任意嵌套。我选择将其表示为事实上的标准嵌套列表:
data Set a where
Empty :: Set a
Elem :: a -> Set a -> Set a
Set :: Set a -> Set a -> Set a
作为一个懒惰的Haskell程序员,我想为所有标准类型类编写实例。
Functor
实例很简单:
instance Functor Set where
fmap _ Empty = Empty
fmap f (Elem x set) = Elem (f x) set
fmap f (Set s set) = Set (fmap f s) $ fmap f set
Foldable
和Traversable
也相对容易实施。
不是我被Applicative
困住了。 pure
也很简单:
instance Applicative Set where
pure x = Elem x Empty
但是,我坚持为嵌套列表定义ap
。
-- set has a monoid instance
(<*>) :: Set (a -> b) -> Set a -> Set b
Elem fx fxs <*> x = fmap fx x `mappend` (fxs <*> x)
Set fxs fxss <*> x = Set ???
对于普通的非嵌套列表,applicative实例将每个函数的笛卡尔积用于每个元素并应用它:
fx <*> xs = [f x | f <- fx, x <- xs]
不知何故,嵌套列表必须保留它的底层结构。 什么是正确的实例?
答案 0 :(得分:2)
您的实例几乎是正确的,还有一些建议:
instance Applicative Set where
pure x = Elem x Empty
-- the cartesian product of the empty set and x is empty
Empty <*> x = Empty
-- the cartesian product of x and the empty set is empty
x <*> Empty = Empty
-- if you encounter a function, apply it to the entire list
-- and append the result of the recursive call to the rest.
Elem fx fxs <*> x = fmap fx x `mappend` (fxs <*> x)
-- If you reach a level of nesting, go into the nested list
-- and prepend that to the rest.
Set fxs fxss <*> x = Set (fxs <*> x) (fxss <*> x)
此实例符合所有适用法律:
pure id <*> x = x
pure f <*> pure x = pure $ f x
pure (.) <*> pure u <*> pure v <*> pure w = u <*> (v <*> w)
u <*> pure y = pure ($ y) <*> u