有没有“类型级组合器”?他们会在未来存在吗? [英] Are there "type-level combinators"? Will they exist in some future?

查看:105
本文介绍了有没有“类型级组合器”?他们会在未来存在吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我看来,使用Haskell非常好用的大部分是组合器,如(。) flip $ <> 等。感觉就像我可以在需要时创建新语法一样。

前段时间我做了一些事情,如果我可以翻转一个类型构造函数,这将非常方便。假设我有一些类型构造函数:

  m :: *  - > *  - > * 

并且我有一个类 MyClass 它需要一个带类型构造函数的类型 * - > * 。当然,我会选择以这样的方式编码类型:

  instance MyClass(ma)

但是,假设我无法更改该代码,并且假设 MyClass 类似于

 类型wb = mba 

实例MyClass w其中
...

然后我必须激活 XTypeSynonymInstances 。有什么办法可以创建一个类型级组合器 Flip ,这样我就可以做到:

  instance MyClass(Flip ma)其中
...

??或者我们在haskell中使用的常见运算符的其他类型级别的泛化?这是甚至有用还是我只是漫不经心?



编辑:

我可以这样做:

  newtype Flip mab = Flip(mba)

newtype Dot mwa = Dot m(wa)

...

但是我不得不使用数据构造函数 Flip Dot ,... ...模式匹配等等。是否值得?

解决方案

你的问题是有道理的,但答案是:不,目前不可能。



问题在于(在GHC Haskell的类型系统)你不能在类型级别使用lambda表达式。对于任何你可能尝试的东西,看起来它可以模拟或实现类型级lambda的效果,你会发现它不起作用。 (我知道,因为我做过。)

你可以做的是声明你的Flip newtypes,然后编写你想要的类的实例,痛苦地包装和解包(顺便说一句:使用记录语法),然后类的客户端可以在类型签名中使用newtypes,而不必担心细节。



我不是类型理论家,我不知道为什么我们不能拥有类型级lambda的细节。我认为这与类型推断成为不可能的事情有关,但是我再也不知道了。


Much of what makes haskell really nice to use in my opinion are combinators such as (.), flip, $ <*> and etc. It feels almost like I can create new syntax when I need to.

Some time ago I was doing something where it would be tremendously convenient if I could "flip" a type constructor. Suppose I have some type constructor:

m  :: * -> * -> * 

and that I have a class MyClass that needs a type with a type constructor with kind * -> *. Naturally I would choose to code the type in such a way that I can do:

instance MyClass (m a) 

But suppose I can't change that code, and suppose that what really fits into MyClass is something like

type w b = m b a 

instance MyClass w where 
    ...

and then I'd have to activate XTypeSynonymInstances. Is there some way to create a "type level combinator" Flip such that I can just do:

instance MyClass (Flip m a)  where 
   ...

?? Or other type level generalisations of common operators we use in haskell? Is this even useful or am I just rambling?

Edit:

I could do something like:

newtype Flip m a b = Flip (m b a)

newtype Dot m w a  = Dot m (w a)

...

But then I'd have to use the data constructors Flip, Dot, ... around for pattern matching and etc. Is it worth it?

解决方案

Your question makes sense, but the answer is: no, it's not currently possible.

The problem is that (in GHC Haskell's type system) you can't have lambdas at the type level. For anything you might try that looks like it could emulate or achieve the effect of a type level lambda, you will discover that it doesn't work. (I know, because I did.)

What you can do is declare your Flip newtypes, and then write instances of the classes you want for them, painfully with the wrapping and the unwrapping (by the way: use record syntax), and then clients of the classes can use the newtypes in type signatures and not have to worry about the details.

I'm not a type theorist and I don't know the details of why exactly we can't have type level lambdas. I think it was something to do with type inference becoming impossible, but again, I don't really know.

这篇关于有没有“类型级组合器”?他们会在未来存在吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆