How to relate to type from outer context

后端 未结 2 1533
太阳男子
太阳男子 2020-12-29 02:21

Let us consider the following code snippet:

blah :: a -> b -> a
blah x y = ble x where
    ble :: b -> b
    ble x = x

This compil

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-29 03:12

    This is possible with the ScopedTypeVariables extension. You need to use explicit forall's to bring the type variables into scope.

    blah :: forall a b. a -> b -> a
    blah x y = ble x where
        ble :: b -> b
        ble x = x
    

    Trying to load this definition with ScopedTypeVariables enabled gives:

    foo.hs:2:16:
        Couldn't match type `a' with `b'
          `a' is a rigid type variable bound by
              the type signature for blah :: a -> b -> a at foo.hs:2:1
          `b' is a rigid type variable bound by
              the type signature for blah :: a -> b -> a at foo.hs:2:1
        In the first argument of `ble', namely `x'
        In the expression: ble x
        In an equation for `blah':
            blah x y
              = ble x
              where
                  ble :: b -> b
                  ble x = x
    

    You can tell that GHC interprets the two bs as the same type because the error says that a and b are bound on the same line.

提交回复
热议问题