What are skolems?

后端 未结 3 948
终归单人心
终归单人心 2020-12-08 00:13

Eeek! GHCi found Skolems in my code!

...
Couldn\'t match type `k0\' with `b\'
  because type variable `b\' would escape its scope
This (rigid, skolem) type          


        
3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-08 00:38

    As I understand it, a "Skolem variable" is a variable which does not match any other variable, including itself.

    This seems to pop up in Haskell when you use features like explicit foralls, GADTs, and other type system extensions.

    For example, consider the following type:

    data AnyWidget = forall x. Widget x => AnyWidget x
    

    What this says is that you can take any type that implements the Widget class, and wrap it into an AnyWidget type. Now, suppose you try to unwrap this:

    unwrap (AnyWidget w) = w
    

    Um, no, you can't do that. Because, at compile-time, we have no idea what type w has, so there's no way to write a correct type signature for this. Here the type of w has "escaped" from AnyWidget, which is not allowed.

    As I understand it, internally GHC gives w a type which is a Skolem variable, to represent the fact that it must not escape. (This is not the only such scenario; there's a couple of other places where a certain value cannot escape due to typing issues.)

提交回复
热议问题