Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Verify the type of a lambda expression

I need to verify the type for the lambda expression: lambdaexp

My method gives me: result

Im trying to define it in Haskell (on Hugs) like this:

h= \f x -> f (f x)

When i call the :type comamnd it gives me:

(a -> a) -> a -> a

Is mi function correctly defined in Haskell?, or my method gives me a wrong result?

like image 820
Wyvern666 Avatar asked Mar 19 '14 03:03

Wyvern666


People also ask

Which is a valid type for this lambda function in Java?

Note that lambda expressions can only be used to implement functional interfaces. In the above example also, the lambda expression implements Consumer Functional Interface. A Java program to demonstrate working of lambda expression with two arguments. // data type for x and y is optional.

What is the type of a lambda expression?

The lambda expressions have a very simple, precise syntax and provide flexibility to specify the datatypes for the function parameters. Its return type is a parameter -> expression body to understand the syntax, we can divide it into three parts.

Which is the valid type of this lambda function?

A lambda expression is a function or subroutine without a name that can be used wherever a delegate is valid. Lambda expressions can be functions or subroutines and can be single-line or multi-line.


1 Answers

Note that f gets called with both x and f x as its argument—this immediately means that the type of x and the type of f x must be the same[1]. Carrying this argument onward, we see that since x is the input to f and f x is the output of f, the input and output of f must be the same[2].

Finally, we examine the lambda term

\f x -> f (f x)

it has two inputs, f (a function) and x, and it returns whatever the return type of f is[3]. Putting all of this information together we have

(a -> b) -> c -> d

where:

  b ~ c        by [1]
  a ~ b        by [2]
  d ~ b        by [3]

thus the type which Haskell inferred is correct

h :: (a -> a) -> a -> a
h f x = f (f x)
like image 74
J. Abrahamson Avatar answered Sep 18 '22 23:09

J. Abrahamson