### 问题

This paper establishes that type inference (called "typability" in the paper) in System F is undecidable. What I've never heard mentioned elsewhere is the second result of the paper, namely that "type checking" in F is also undecidable. Here the "type checking" question means: given a term `t`

, type `T`

and typing environment `A`

, is the judgment `A ⊢ t : T`

derivable? That this question is undecidable (and that it's equivalent to the question of typability) is surprising to me, because it seems intuitively like it should be an easier question to answer.

But in any case, given that Haskell is based on System F (or F-omega, even), the result about type checking would seem to suggest that there is a Haskell term `t`

and type `T`

such that the compiler would be unable to decide whether `t :: T`

is valid. If that's the case, I'm curious what such a term and type are... if it's not the case, what am I misunderstanding?

Presumably comprehending the paper would lead to a constructive answer, but I'm a little out of my depth :)

### 回答1:

Type checking can be made decidable by enriching the syntax appropriately. For example, in the paper, we have lambdas written as `\x -> e`

; to type-check this, you must *guess* the type of `x`

. However, with a suitably enriched syntax, this can be written as `\x :: t -> e`

instead, which takes the guess-work out of the process. Similarly, in the paper, they allow type-level lambdas to be implicit; that is, if `e :: t`

, then also `e :: forall a. t`

. To do typechecking, you have to guess when and how many `forall`

's to add, and when to eliminate them. As before, you can make this more deterministic by adding syntax: we add two new expression forms `/\a. e`

and `e [t]`

and two new typing rule that says if `e :: t`

, then `/\a. e :: forall a. t`

, and if `e :: forall a. t`

, then `e [t'] :: t [t' / a]`

(where the brackets in `t [t' / a]`

are substitution brackets). Then the syntax tells us when and how many foralls to add, and when to eliminate them as well.

So the question is: can we go from Haskell to sufficiently-annotated System F terms? And the answer is yes, thanks to a few critical restrictions placed by the Haskell type system. The most critical is that all types are rank one*. Without going into too much detail, "rank" is related to how many times you have to go to the left of an `->`

constructor to find a `forall`

.

```
Int -> Bool -- rank 0?
forall a. (a -> a) -- rank 1
(forall a. a -> a) -> (forall a. a -> a) -- rank 2
```

In particular, this restricts polymorphism a bit. We can't type something like this with rank one types:

```
foo :: (forall a. a -> a) -> (String, Bool) -- a rank-2 type
foo polymorphicId = (polymorphicId "hey", polymorphicId True)
```

The next most critical restriction is that type variables can only be replaced by monomorphic types. (This includes other type variables, like `a`

, but not polymorphic types like `forall a. a`

.) This ensures in part that type substitution preserves rank-one-ness.

It turns out that if you make these two restrictions, then not only is type-inference decidable, but you also get *minimal* types.

If we turn from Haskell to GHC, then we can talk not only about what is typable, but how the inference algorithm looks. In particular, in GHC, there are extensions that relax the above two restrictions; how does GHC do inference in that setting? Well, the answer is that it simply doesn't even try. If you want to write terms using those features, then you must add the typing annotations we talked about all the way back in paragraph one: you must explicitly annotate where `forall`

s get introduced and eliminated. So, can we write a term that GHC's type-checker rejects? Yes, it's easy: simply use un-annotated rank-two (or higher) types or impredicativity. For example, the following doesn't type-check, even though it has an explicit type annotation and is typable with rank-two types:

```
{-# LANGUAGE Rank2Types #-}
foo :: (String, Bool)
foo = (\f -> (f "hey", f True)) id
```

* Actually, restricting to rank two is enough to make it decidable, but the algorithm for rank one types can be more efficient. Rank three types already give the programmer enough rope to make the inference problem undecidable. I'm not sure whether these facts were known at the time that the committee chose to restrict Haskell to rank-one types.

### 回答2:

Here is an example for a type level implementation of the SKI calculus in Scala: http://michid.wordpress.com/2010/01/29/scala-type-level-encoding-of-the-ski-calculus/

The last example shows an unbounded iteration. If you do the same in Haskell (and I'm pretty sure you can), you have an example for an "untypeable expression".

来源：`https://stackoverflow.com/questions/7644658/are-there-type-signatures-which-haskell-cant-verify`