简体   繁体   English

Haskell:类系统扩展提议

[英]Haskell: class system extension proposal

I'm solving some ploblem on generating ancestor instances in Haskell. 我正在解决在Haskell中生成祖先实例的一些问题。 Recently I found this on Haskell wiki: Class system extension proposal . 最近我在Haskell wiki上发现了这个: 类系统扩展提议 So, I would like to know, are there any solutions for this proposal already? 所以,我想知道,这个提案有没有解决方案?

Here are the examples from the Proposal: 以下是提案中的示例:

The current class system in Haskell is based on the idea that you can often provide default implementations for class methods at the same time as defining the class, by using other methods of the class or its ancestors. Haskell中的当前类系统基于这样的思想,即通过使用类或其祖先的其他方法,您可以在定义类的同时为类方法提供默认实现。 However consider the following hierarchy, adapted from Functor hierarchy proposal and The Other Prelude: 但是请考虑以下层次结构,该层次结构根据Functor层次结构提议和其他前奏改编:

 class Functor m where fmap :: (a -> b) -> ma -> mb class Functor m => Applicative m where return :: a -> ma apply :: m (a -> b) -> ma -> mb (>>) :: ma -> mb -> mb ma >> mb = (fmap (const id) ma) `apply` mb class Applicative m => Monad m where (>>=) :: ma -> (a -> mb) -> mb 

For all concrete instances of Monad we can define fmap, apply, and (>>)in terms of return and (>>=) as follows: 对于Monad的所有具体实例,我们可以在return和(>> =)方面定义fmap,apply和(>>),如下所示:

 fmap f ma = ma >>= (\\a -> return (fa)) apply mf ma = mf >>= \\f -> ma >>= \\a -> return (fa) ma >> mb = ma >>= \\_ -> mb 

In other words, we'd like to be able to write: 换句话说,我们希望能够写:

 class Applicative m => Monad m where (>>=) :: ma -> (a -> mb) -> mb fmap f ma = ma >>= (\\a -> return (fa)) apply mf ma = mf >>= \\f -> ma >>= \\a -> return (fa) ma >> mb = ma >>= \\_ -> mb 

and be able to define new instances of Monad just by supplying definitions for return and (>>=) by writing: 并且能够通过写入来提供返回和(>> =)的定义来定义Monad的新实例:

 instance Monad T where ma >>= a_mb = ... -- some definition return a = ... -- some definition 

Explicit import/export of instances 显式导入/导出实例

This is needed so that large programs can be built without fear of colliding instance declarations between different packages. 这是必需的,以便可以构建大型程序而不必担心不同包之间的实例声明冲突。 A possible syntax could be: 可能的语法可能是:

 module M -- exported instances ( instance Monad T , instance Functor (F a) hiding (Functor (F Int), Functor (F Char)) , F(..) ) where import Foo (instance Monad a hiding Monad Maybe) data T a data F ab 

where the context is elided because this isn't used in instance selection (at the moment). 上下文被省略的地方因为在实例选择中没有使用它(此刻)。 The import directive tells the compiler to use all Monad instances exported by Foo except for the Monad Maybe instance (it doesn't matter whether or not Foo actually does export a Monad Maybe instance - all that matters here is that we don't want it if there is one). import指令告诉编译器使用Foo导出的所有Monad实例,除了Monad Maybe实例(Foo是否实际导出Monad Maybe实例无关紧要 - 这里重要的是我们不需要它如果有的话)。

Yes, the DefaultSignatures extension allows this. 是的, DefaultSignatures扩展允许这样做。 For example, for the Functor / Applicative example, one could write 例如,对于Functor / Applicative示例,可以编写

{-# LANGUAGE DefaultSignatures #-}
class Functor f where
    fmap :: (a -> b) -> f a -> f b
    default fmap :: Applicative f => (a -> b) -> f a -> f b
    fmap = liftA

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM