级联函数列表的类型是什么? [英] What would the type of a list of cascading functions be?

查看:157
本文介绍了级联函数列表的类型是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Haskell语法中,我们可以有一个(抽象)类型,如 [a - > b] ,这是a到b的函数列表。具体的类型是 [Int - > Int] ,例如 map(*)[1..10] 。是否可以在类似 [a - > b,b - > c,c - > d,...] ?列表中的各个元素都是不同的(我认为),所以我不认为这是可能的。但是依赖类型可能吗?它的类型签名是什么(最好是使用伪Haskell语法)?

解决方案

,但你可以像下面这样构造自己的类列表类型:

  { - #LANGUAGE GADTs# - } 

data CascadingList io其中
Id :: CascadingList ii
Cascade ::(b - > o) - > CascadingList i b - > CascadingList io

然后你可以使这些 CascadingList 如下所示:

p pre $ add pre $ add $ p $ add $ p $ add $ p $ add $ p $ add $ p $ add $ p $ add $ 1)$ Id

您可以'折叠'列表:

  collapse :: CascadingList ab  - > a  - > b 
collapse id = id
collapse(Cascade f c)= f。折叠c

然后您将拥有

  collapse addOnePositive 0 == True 

请注意,考虑到中间函数的类型,所以它可能不是你正在寻找的。




我刚才意识到这更接近于[c - > d,b - > c,a - > b]。让它更接近你的意图是一个容易的改变;我可以编辑它,但我认为你明白了。


In Haskell syntax, we can have a (abstract) type like [a -> b], which is a list of functions a to b. A concrete type of this would be [Int -> Int], such as map (*) [1..10]. Is it possible to have a list of cascading functions in a type like [a -> b, b -> c, c -> d, ...]? The individual elements of the list are all different (I think) so I don't think it's possible. But is it possible with dependent types? What would its type signature be (preferably in pseudo-Haskell syntax)?

解决方案

You can't do that with a plain list, but you could construct your own list-like type as follows:

{-# LANGUAGE GADTs #-}

data CascadingList i o where
    Id :: CascadingList i i
    Cascade :: (b -> o) -> CascadingList i b -> CascadingList i o

Then you could make these CascadingLists as follows:

addOnePositive :: CascadingList Int Bool
addOnePositive = Cascade (>0) $ Cascade (+1) $ Id

You could 'collapse' the lists:

collapse :: CascadingList a b -> a -> b
collapse Id = id
collapse (Cascade f c) = f . collapse c

Then you would have

collapse addOnePositive 0 == True

Note that this does not take into account the types of the intermediate functions, so it may not be what you are looking for.


I've just realised that this is closer to something like [c -> d, b -> c, a -> b]. It's an easy change to make it closer to your intentions; I could edit it but I think you get the idea.

这篇关于级联函数列表的类型是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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