- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个具有 Functor 实例的递归数据类型:
data Expr1 a
= Val1 a
| Add1 (Expr1 a) (Expr1 a)
deriving (Eq, Show, Functor)
newtype Fix f = Fix {unFix :: f (Fix f)}
data ExprF a r
= Val a
| Add r r
deriving (Eq, Show, Functor)
type Expr2 a = Fix (ExprF a)
cata :: Functor f => (f a -> a) -> Fix f -> a
cata f = f . fmap (cata f) . unFix
eval :: Expr2 Int -> Int
eval = cata $ \case
Val n -> n
Add x y -> x + y
main :: IO ()
main =
print $ eval
(Fix (Add (Fix (Val 1)) (Fix (Val 2))))
Expr2
与原始
Expr
相同的仿函数实例有。尝试定义仿函数实例时似乎存在一种不匹配:
instance Functor (Fix (ExprF a)) where
fmap = undefined
Kind mis-match
The first argument of `Functor' should have kind `* -> *',
but `Fix (ExprF a)' has kind `*'
In the instance declaration for `Functor (Fix (ExprF a))'
Expr2
编写 Functor 实例?
newtype Expr2 a = Expr2 (Fix (ExprF a))
将 Expr2 包装在一个新类型中。但是这个新类型需要解包才能传递给
cata
,我不太喜欢。我也不知道是否可以自动推导出
Expr2
仿函数实例,就像我对
Expr1
所做的那样.
最佳答案
这对我来说是一个老痛处。关键是你的ExprF
它的两个参数都是函数式的。所以如果我们有
class Bifunctor b where
bimap :: (x1 -> y1) -> (x2 -> y2) -> b x1 x2 -> b y1 y2
instance Bifunctor ExprF where
bimap k1 k2 (Val a) = Val (k1 a)
bimap k1 k2 (Add x y) = Add (k2 x) (k2 y)
newtype Fix2 b a = MkFix2 (b a (Fix2 b a))
map1cata2 :: Bifunctor b => (a -> a') -> (b a' t -> t) -> Fix2 b a -> t
map1cata2 e f (MkFix2 bar) = f (bimap e (map1cata2 e f) bar)
instance Bifunctor b => Functor (Fix2 b) where
fmap k = map1cata2 k MkFix2
Bifunctor
实例不会被魔法构建。你需要一个不同的定点运算符和一种全新的仿函数,这有点烦人。问题是你现在有两种子结构:“值”和“子表达式”。
DataKinds
)并
type s :-> t = forall x. s x -> t x
class FunctorIx (f :: (i -> *) -> (o -> *)) where
mapIx :: (s :-> t) -> f s :-> f t
i
。和“结构”在某种其他
o
上的索引.我们采取
i
-将元素上的功能保留到
o
保留结构上的功能。至关重要的是,
i
和
o
可以不同。
*
可以很容易地变成一种简单索引的 GADT
() -> *
.并且可以将两种类型卷在一起制成一种 GADT
Either () () -> *
.这意味着我们可以将两种子结构组合在一起。一般来说,我们有一种类型级别
either
.
data Case :: (a -> *) -> (b -> *) -> Either a b -> * where
CL :: f a -> Case f g (Left a)
CR :: g b -> Case f g (Right b)
mapCase :: (f :-> f') -> (g :-> g') -> Case f g :-> Case f' g'
mapCase ff gg (CL fx) = CL (ff fx)
mapCase ff gg (CR gx) = CR (gg gx)
Either
-索引
FunctorIx
实例。
f
其中有两个元素的位置
p
或子节点。这和我们上面的交易一样。
newtype FixIx (f :: (Either i o -> *) -> (o -> *))
(p :: i -> *)
(b :: o)
= MkFixIx (f (Case p (FixIx f p)) b)
mapCata :: forall f p q t. FunctorIx f =>
(p :-> q) -> (f (Case q t) :-> t) -> FixIx f p :-> t
mapCata e f (MkFixIx node) = f (mapIx (mapCase e (mapCata e f)) node)
FunctorIx
的事实。在
FixIx
下关闭.
instance FunctorIx f => FunctorIx (FixIx f) where
mapIx f = mapCata f MkFixIx
Functor
相比,它们享有更多方便的封闭特性。是的。我不认为他们会 catch 。
关于haskell - 如何将 Functor 实例赋予为一般递归方案构建的数据类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27028089/
我想在不创建仿函数类的情况下使用仿函数,但即使我将匹配的字符串存储在 foundVector 中,我的 foundVector 仍显示为空。 也告诉我有没有更好的方法来使用fucntors 我正在使用
在阅读类型类时,我发现 Functor、Applicative Functor 和 Monad 之间的关系是严格递增的权力。仿函数是可以映射的类型。 Applicative Functors 可以做同
我对在 Java 中使用仿函数(函数对象)很感兴趣。通过快速谷歌搜索,我找到了这 3 个包: Java 泛型算法:http://jga.sourceforge.net/ 公共(public)仿函数:h
我是 Haskell 的新手。我做了一个类型 Maybe3。 data Maybe3 a= Just3 a| Unknown3 | Missing3 deriving (Show, Eq, Ord)
在研究了基于 MacLane、Awodey 和 Spivak 书籍的类别理论之后,我试图理解 Haskell 中的自由/操作单子(monad)。 我们可以使用 Control.Monad.Free 从
我正在努力深入了解 Monad类的层次结构。当然,其中一部分是看到很多例子,但我对这些类是如何首次发现的历史及其动机特别感兴趣。 我了解 Monad s 最初是作为 Haskell 中 IO 问题的解
我有一个多项式 data Poly a = Poly [a] 我希望能够做类似 fmap (take 3) polynomial 的事情但我不能,因为 Poly不是真正的仿函数,因为 f我在 fmap
我想知道到什么程度Functor Haskell 中的实例由仿函数定律(唯一地)确定。 由于ghc可以推导出Functor对于至少“普通”数据类型的实例,似乎它们至少在各种情况下必须是唯一的。 为方便
我想知道到什么程度Functor Haskell 中的实例由仿函数定律(唯一)确定。 自 ghc可以得出Functor至少对于“普通”数据类型的实例,它们似乎至少在各种情况下必须是唯一的。 为方便起见
有这种类型: {-# LANGUAGE GADTs #-} data Rgb a = (Num a, Show a) => Rgb a a a 我完全能够实现 Show 类型类: instance S
背景。在我的一个类中,我们一直在探索Parser monad。 Parser monad 通常定义为 newtype Parser a = Parser (String -> [(a, String)
我正在尝试使用 length streaming-bytestring Data.ByteString.Streaming.Char8 库的函数。 我看到返回值的类型为Of,但我不清楚如何检查它。我尝
我正在尝试编写一个多态的 map (Functor) 但我遇到了这种类型的错误。 给定以下类型 type Result = | Success of 'TSuccess | Error
我一直在努力理解 Haskell 中的 Functor 是什么,为此我想要一个没有任何其他属性的 Functor 示例。 我想出的工作示例是 data MyFunctor a b = MyFuncto
我正在努力将 GHC/Arr.hs 移植到弗雷格。 数组定义: data Array i e = Array{u,l::i,n::Int,elems::(JArray e)} 有函数: amap ::
我不认为这是可能的,如果它是或为什么不是这样,我很好奇: class A { } 我想将 A 的实例视为函数,例如 A a = new A(); a(); or a(3); etc... 这是为了在特
我正在使用 C++ 模板传入 Strategy 仿函数以更改函数的行为。它工作正常。我传递的仿函数是一个没有存储的无状态类,它只是以经典的仿函数方式重载 () 运算符。 template int f
我有一个类,我试图指向其成员函数,问题是我不断收到此错误 reference to non-static member function must be called根据我的理解,需要指向一个成员函数
我一直潜伏在这里,试图弄清楚 Functor 是否可以做我需要它做的事情。 我想做的是包装对类方法的调用,并以某种方式捕获函数返回的值。鉴于我的 Functor 类,我需要做些什么才能将我的评论转化为
我想用 c++ 编写一个函数,它接受一个 int 类型的变量,它的作用是定义一个仿函数的重载运算符 () 并将该仿函数作为输出参数返回。例如: template Functor myFunc(doub
我是一名优秀的程序员,十分优秀!