跨类型构造函数编写泛型仿函数实例?

编程入门 行业动态 更新时间:2024-10-25 14:22:49
本文介绍了跨类型构造函数编写泛型仿函数实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在学习基本类型类,并为我的类型Test a写了自己的functor实现(与Maybe一样):

I'm learning basic type classes and have written my own implementation of functor for my type Test a (behaves just like Maybe):

data Test a = Test a | Emp class FC c a where t :: (a -> b) -> c a -> c b instance FC Test a where t f (Test a) = Test (f a) t f (Emp) = Emp instance FC Maybe a where t f (Just a) = Just (f a) t f (Nothing) = Nothing

是否可以实现类似的内容:

Is it possible to implement something like:

instance FC c where t f (c v) = c (f v)

错误:

Parse error in pattern: c

换句话说,将类型构造函数抽象出来,替换为c和v,从而创建一个通用实例,该实例可以应用于具有上下文的任何值吗?

In other words, abstract away the type constructor, replace with c and v, therefore creating a general instance that can be applied to any value with a context?

推荐答案

如您所知,c a并不是语法上有效的模式.但是,请阅读您的问题作为功能建议:这将如何工作?并非每个Functor都有一个单元素构造函数,可以根据您的模式进行映射.一些例子:

As you've learned, c a is not a syntactically valid pattern. But reading your question instead as a feature proposal: How would that work? Not every Functor has a single-element constructor which can be mapped over according to your pattern. Some examples:

data Pair a = Pair a a -- more than one element instance Functor Pair where fmap f (Pair x y) = Pair (f x) (f y) data Proxy a = Proxy -- no elements instance Functor Proxy where fmap f Proxy = Proxy newtype Cont r a = Cont { runCont :: (a -> r) -> r } -- element appears in a double-negative position instance Functor (Cont r) where fmap f (Cont g) = Cont (g . (. f))

无论如何,我认为通用实例"的想法真的没有道理.该实例是您放置特定于类型的代码的地方. (它必须去某个地方!)

In any case, I don't think the idea of a "generic instance" really makes sense. The instance is where you put your type-specific code. (It has to go somewhere!)

如果您想花更少的精力编写Functor实例,则可以使用GHC的DeriveFunctor扩展名.

If you want to exert less effort in writing Functor instances you can use GHC's DeriveFunctor extension.

{-# LANGUAGE DeriveFunctor #-} data Pair a = Pair a a deriving Functor data Proxy a = Proxy deriving Functor newtype Cont r a = Cont { runCont :: (a -> r) -> r } deriving Functor

更多推荐

跨类型构造函数编写泛型仿函数实例?

本文发布于:2023-11-02 15:06:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552718.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   实例   类型   泛型仿

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!