简体   繁体   中英

Confused about Haskell polymorphic types

I have defined a function :

gen :: a -> b

So just trying to provide a simple implementation :

gen 2 = "test"

But throws error :

gen.hs:51:9:
    Couldn't match expected type ‘b’ with actual type ‘[Char]’
      ‘b’ is a rigid type variable bound by
          the type signature for gen :: a -> b at gen.hs:50:8
    Relevant bindings include gen :: a -> b (bound at gen.hs:51:1)
    In the expression: "test"
    In an equation for ‘gen’: gen 2 = "test"
Failed, modules loaded: none.

So my function is not correct. Why is a not typed as Int and b not typed as String ?

This is a very common misunderstanding.

The key thing to understand is that if you have a variable in your type signature, then the caller gets to decide what type that is, not you!

So you cannot say "this function returns type x " and then just return a String ; your function actually has to be able to return any possible type that the caller may ask for. If I ask your function to return an Int , it has to return an Int . If I ask it to return a Bool , it has to return a Bool .

Your function claims to be able to return any possible type, but actually it only ever returns String . So it doesn't do what the type signature claims it does. Hence, a compile-time error.

A lot of people apparently misunderstand this. In (say) Java, you can say "this function returns Object ", and then your function can return anything it wants. So the function decides what type it returns. In Haskell, the caller gets to decide what type is returned, not the function.

Edit: Note that the type you're written, a -> b , is impossible. No function can ever have this type. There's no way a function can construct a value of type b out of thin air. The only way this can work is if some of the inputs also involve type b , or if b belongs to some kind of typeclass which allows value construction.

For example:

head :: [x] -> x

The return type here is x ("any possible type"), but the input type also mentions x , so this function is possible; you just have to return one of the values that was in the original list.

Similarly, gen :: a -> a is a perfectly valid function. But the only thing it can do is return it's input unchanged (ie, what the id function does).

This property of type signatures telling you what a function does is a very useful and powerful property of Haskell.

gen :: a -> b does not mean "for some type a and some type b , foo must be of type a -> b ", it means "for any type a and any type b , foo must be of type a -> b ".

to motivate this: If the type checker sees something like let x :: Int = gen "hello" , it sees that gen is used as String -> Int here and then looks at gen 's type to see whether it can be used that way. The type is a -> b , which can be specialized to String -> Int , so the type checker decides that this is fine and allows this call. That is since the function is declared to have type a -> b , the type checker allows you to call the function with any type you want and allows you to use the result as any type you want.

However that clearly does not match the definition you gave the function. The function knows how to handle numbers as arguments - nothing else. And likewise it knows how to produce strings as its result - nothing else. So clearly it should not be possible to call the function with a string as its argument or to use the function's result as an Int. So since the type a -> b would allow that, it's clearly the wrong type for that function.

Your type signature gen :: a -> b is stating, that your function can work for any type a (and provide any type b the caller of the function demands).

Besides the fact that such a function is hard to come by, the line gen 2 = "test" tries to return a String which very well may not be what the caller demands.

Excellent answers. Given your profile, however, you seem to know Java, so I think it's valuable to connect this to Java as well.

Java offers two kinds of polymorphism:

  1. Subtype polymorphism: eg, every type is a subtype of java.lang.Object
  2. Generic polymorphism: eg, in the List<T> interface.

Haskell's type variables are a version of (2). Haskell doesn't really have a version of (1).

One way to think of generic polymorphism is in terms of templates (which is what C++ people call them): a type that has a type variable parameter is a template that can be specialized into a variety of monomorphic types. So for example, the interface List<T> is a template for constructing monomorphic interfaces like List<String> , List<List<String>> and so on, all of which have the same structure but differ only because the type variable T gets replaced uniformly throughout the signatures with the instantiation type.

The concept that "the caller chooses" that several responders have mentioned here is basically a friendly way of referring to instantiation. In Java, for example, the most common point where the type variable gets "chosen" is when an object is instantiated:

List<String> myList = new ArrayList<String>();

Second common point is that a subtype of a generic type may instantiate the supertype's variables:

class MyFunction implements Function<Integer, String> {
    public String apply(Integer i) { ... }
}

Third one is methods that allow the caller to instantiate a variable that's not a parameter of its enclosing type:

/**
 * Visitor-pattern style interface for a simple arithmetical language
 * abstract syntax tree.
 */
interface Expression {
    // The caller of `accept` implicitly chooses which type `R` is,
    // by supplying a `Visitor<R>` with `R` instantiated to something
    // of its choice.
    <R> accept(Expression.Visitor<R> visitor);

    static interface Visitor<R> {
        R constant(int i);
        R add(Expression a, Expression b);
        R multiply(Expression a, Expression b);
    }
}

In Haskell, instantiation is carried out implicitly by the type inference algorithm. In any expression where you use gen :: a -> b , type inference will infer what types need to be instantiated for a and b , given the context in which gen is used. So basically, "caller chooses" means that any code that uses gen controls the types to which a and b will be instantiated; if I write gen [()] , then I'm implicitly instantiating a to [()] . The error here means that your type declaration says that gen [()] is allowed, but your equation gen 2 = "test" implies that it's not.

In Haskell, type variables are implicitly quantified, but we can make this explicit:

{-# LANGUAGE ScopedTypeVariables #-}

gen :: forall a b . a -> b
gen x = ????

The "forall" is really just a type level version of a lambda, often written Λ. So gen is a function taking three arguments: a type, bound to the name a , another type, bound to the name b , and a value of type a , bound to the name x . When your function is called, it is called with those three arguments. Consider a saner case:

fst :: (a,b) -> a
fst (x1,x2) = x1

This gets translated to

fst :: forall (a::*) (b::*) . (a,b) -> a
fst = /\ (a::*) -> /\ (b::*) -> \ (x::(a,b)) ->
   case x of
      (x1, x2) -> x1

where * is the type (often called a kind ) of normal concrete types. If I call fst (3::Int, 'x') , that gets translated into

fst Int Char (3Int, 'x')

where I use 3Int to represent specifically the Int version of 3 . We could then calculate it as follows:

fst Int Char (3Int, 'x')
=
(/\ (a::*) -> /\ (b::*) -> \(x::(a,b)) -> case x of (x1,x2) -> x1) Int Char (3Int, 'x')
=
(/\ (b::*) -> \(x::(Int,b)) -> case x of (x1,x2) -> x1) Char (3Int, 'x')
=
(\(x::(Int,Char)) -> case x of (x1,x2) -> x1) (3Int, x)
=
case (3Int,x) of (x1,x2) -> x1
=
3Int

Whatever types I pass in, as long as the value I pass in matches, the fst function will be able to produce something of the required type. If you try to do this for a->b , you will get stuck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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