Alb*_*ani 3 haskell functor category-abstractions
下面定义的函数mapRightR仅更改地图的设置内容,而不更改键,并产生有效的Relation类型。
使用此高级函数定义Functor Relation实例真的是不可能的,还是我的实现错误。
{-# LANGUAGE GADTs #-}
import Data.Map as M
import Data.Set as S
data Relation a b where
R :: (Ord a, Ord b) => Map a (Set b) -> Relation a b
instance Functor Relation where
fmap f r = mapRightR f r
mapRightR :: Ord b1 => (b2 -> b1) -> Relation a b2 -> Relation a b1
mapRightR f (R r) = R $ M.map (S.map f) r
Run Code Online (Sandbox Code Playgroud)
谢谢,切普纳。
我尝试了关系的另一个定义,使用List而不是Set来工作!
data Relation a b where
R :: (Ord a) => Map a [b] -> Relation a b
instance Functor (Relation a) where
fmap f r = mapRightR f r
mapRightR :: (b2 -> b1) -> Relation a b2 -> Relation a b1
mapRightR f (R r) = R $ M.map (L.map f) r
Run Code Online (Sandbox Code Playgroud)
che*_*ner 11
mapRightR受到约束,它不会工作的任何类型b的fmap要求:
-- Specialized for f ~ Relation c
fmap :: (a -> b) -> Relation c a -> Relation c b
Run Code Online (Sandbox Code Playgroud)
但
mapRightR :: Ord b => (a -> b) -> Relation c a -> Relation c b
Run Code Online (Sandbox Code Playgroud)
在更明确的条款,Relation c是不是一个映射endofunctor Hask到Hask(这是什么Functor类型类代表),而是映射的一个子类仿函数Hask只包含类型的Ord实例Hask。(我认为我的描述正确无误;欢迎进行更正。)
| 归档时间: |
|
| 查看次数: |
95 次 |
| 最近记录: |