Search code examples
haskellsignaturecurryingpartial-application

How does Haskell evaluate this signature?


ggt_euklid :: Nat1 -> (Nat1 -> Nat1)

I am trying to learn partial application, I know that in this case, if the parentheses would be left out, I would get the same result, but I do not know how this signature should be evaluated.

As far as I have understood, parentheses signify that it is a function? Would that not imply that ggt_euklid takes a value Nat1 and returns a function?

Below is the complete function:

ggt_euklid x y
| x == y = x
|x>y =ggt_euklid(x-y) y 
|x<y =ggt_euklid x (y-x)

Solution

  • Would that not imply that ggt_euklid takes a value Nat1 and returns a function?

    No, it still imply that ggt_euklid takes one argument of type Nat1 and return a function of type Nat1->Nat1, even though, the parentheses be left out.

    The Arrow -> always be right-associativity (when no parentheses), i.e.:

    Nat1 -> Nat1 -> Nat1 
    

    is equivalent to

    Nat1 -> (Nat1 -> Nat1)
    

    which is corresponding to the function application always be left-associativity. (when no parentheses) for example:

    ggt_euklid 1 2
    

    is equivalent to

    (ggt_euklid 1) 2
    

    Here

    (ggt_euklid 1) ~ Nat1 -> Nat1
    

    and

    (ggt_euklid 1) 2 ~ Nat1
    

    So, no matter whether one or two arguments apply to ggt_euklid, it always return a function of type Nat1 -> Nat1 firstly, if second argument is provided, it applies second argument to the returned function.