简体   繁体   English

了解功能签名

[英]Understanding function signature

I was reading a paper and one of the most fundamental parts of it is the following function, written in Haskell: 我正在阅读一篇论文 ,其中一个最基本的部分是以下函数,用Haskell编写:

fixP :: Eq a => (Parser a -> Parser a) -> Parser a
fixP h x = fixS f 
           where f s = h p x 
                       where p y = if x == y then s
                                             else fixP h y

My Haskell is rusty. 我的Haskell生锈了。 As I understand it fixP takes 1 argument which is a function Parser a -> Parser a , where a is constrained to have equality defined. 据我所知, fixP需要1个参数,这是一个函数Parser a -> Parser a ,其中a被约束为定义了相等。 However the pattern matches 2 arguments, h and x . 但是,模式匹配2个参数, hx What is x referring to? x指的是什么?

Additional type signatures involved: 涉及的其他类型签名:

type Parser a = State -> Set (a,State)

type State = String

type Set a = [a]

fixS :: Eq a => (Set a -> Set a) -> Set a

After reading and understanding the answer and for anyone interested; 阅读并理解答案后,感兴趣的人; here's the same function written in javascript: 这是用javascript编写的相同函数:

function fixP(h) {
    return function(x) {
        var f = function(s) {
            var p = function(y) {
                if(x == y) {
                    return s;
                } else {
                    return fixP(h)(y);
                }
            };
            return h(p)(x);
        };
        return fixS(f);
    };
}

Note that fixP h has type Parser a . 请注意, fixP h类型为Parser a Since Parser a is a synonym for State -> Set (a, State) , we see that fixP h is in fact a function: 由于Parser aState -> Set (a, State)的同义词,我们看到fixP h实际上是一个函数:

(fixP h) :: State -> Set (a, State)

We can therefore apply this function to some argument x of type State . 因此,我们可以将此函数应用于State类型的某个参数x That looks like (fixP h) x . 这看起来像(fixP h) x Since function application is left associative , (fixP h) x is the same as fixP hx . 由于函数应用程序是左关联的(fixP h) xfixP hx相同。

In words: To define what fixP is, we define what it does to arguments, ie we define what fixP h is. 用语言来定义fixP是什么,我们定义它对参数的作用,即我们定义fixP h是什么。 Since fixP h is itself a function, we need to define it. 由于fixP h本身就是一个函数,我们需要定义它。 We define it by specifying what it does to arguments, ie we define what (fixP h) x is. 我们通过指定它对参数的作用来定义它,即我们定义什么(fixP h) x是什么。 Left associativity of function application means the latter can be written fixP hx . 函数应用的左关联性意味着后者可以写成fixP hx

As to the question "what's x?": Its type is State , so it smells like some sort of parser state, which according to the type synonyms you gave is a string. 关于“什么是x?”的问题:它的类型是State ,所以它闻起来像某种解析器状态,根据你给出的类型同义词是一个字符串。 Exactly what that string's role is, though, is not clear just from the types :) 究竟该字符串的作用是什么,仅从类型:)不清楚

Simple explanation: Parser a is a type like this: 简单解释: Parser a是这样的type

type Parser a = (String -> a)

This code then 这个代码然后

module Main where 模块主要在哪里

type NT a = (Int -> a)

f :: (NT a -> NT a) -> NT a
f h x = undefined

g :: NT Double
g 0 = 0.0
g _ = 1.0

main = undefined

typechecks well. typechecks很好。

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

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