Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lambda for type expressions in Haskell?

Tags:

lambda

haskell

Does Haskell, or a specific compiler, have anything like type-level lambdas (if that's even a term)?

To elaborate, say I have a parametrized type Foo a b and want Foo _ b to be an instance of, say, Functor. Is there any mechanism that would let me do something akin to

instance Functor (\a -> Foo a b) where ... 

?

like image 946
gspr Avatar asked Nov 01 '10 14:11

gspr


People also ask

Can lambda expression contains data type?

The lambda expressions have a very simple, precise syntax and provide flexibility to specify the datatypes for the function parameters.

What is lambda expression in Haskell?

λ-expressions (λ is the small Greek letter lambda) are a convenient way to easily create anonymous functions — functions that are not named and can therefore not be called out of context — that can be passed as parameters to higher order functions like map, zip etc.


2 Answers

While sclv answered your direct question, I'll add as an aside that there's more than one possible meaning for "type-level lambda". Haskell has a variety of type operators but none really behave as proper lambdas:

  • Type constructors: Abstract type operators that introduce new types. Given a type A and a type constructor F, the function application F A is also a type but carries no further (type level) information than "this is F applied to A".
  • Polymorphic types: A type like a -> b -> a implicitly means forall a b. a -> b -> a. The forall binds the type variables within its scope, thus behaving somewhat like a lambda. If memory serves me this is roughly the "capital lambda" in System F.
  • Type synonyms: A limited form of type operators that must be fully applied, and can produce only base types and type constructors.
  • Type classes: Essentially functions from types/type constructors to values, with the ability to inspect the type argument (i.e., by pattern matching on type constructors in roughly the same way that regular functions pattern match on data constructors) and serving to define a membership predicate on types. These behave more like a regular function in some ways, but are very limited: type classes aren't first-class entities that can be manipulated, and they operate on types only as input (not output) and values only as output (definitely not input).
  • Functional dependencies: Along with some other extensions, these allow type classes to implicitly produce types as results as well, which can then be used as the parameters to other type classes. Still very limited, e.g. by being unable to take other type classes as arguments.
  • Type families: An alternate approach to what functional dependencies do; they allow functions on types to be defined in a manner that looks much closer to regular value-level functions. The usual restrictions still apply, however.

Other extensions relax some of the restrictions mentioned, or provide partial workarounds (see also: Oleg's type hackery). However, pretty much the one thing you can't do anywhere in any way is exactly what you were asking about, namely introduce new a binding scope with an anonymous function abstraction.

like image 105
C. A. McCann Avatar answered Sep 23 '22 22:09

C. A. McCann


From TypeCompose:

newtype Flip (~>) b a = Flip { unFlip :: a ~> b } 

http://hackage.haskell.org/packages/archive/TypeCompose/0.6.3/doc/html/Control-Compose.html#t:Flip

Also, if something is a Functor in two arguments, you can make it a bifunctor:

http://hackage.haskell.org/packages/archive/category-extras/0.44.4/doc/html/Control-Bifunctor.html

(or, in a later category-extras, a more general version: http://hackage.haskell.org/packages/archive/category-extras/0.53.5/doc/html/Control-Functor.html#t:Bifunctor)

like image 40
sclv Avatar answered Sep 21 '22 22:09

sclv