Why can't you (totally) apply a type synonym that has arguments using an other type synonym?

后端 未结 1 1855
谎友^
谎友^ 2021-01-19 06:55

We can define type synonyms with arguments and this works fine when used with actual types:

type MyType t = t String String

data Test a b = Test a b

f :: M         


        
相关标签:
1条回答
  • 2021-01-19 07:33

    According to the Haskell report type synonyms cannot be partially applied:

    Type constructor symbols T introduced by type synonym declarations cannot be partially applied; it is a static error to use T without the full number of arguments.

    In particular this is checked before expanding the type synonyms, this means that in the expression:

    MyType Test'
    

    The check is performed before expanding MyType and hence Test' results partially applied.

    However, it is possible to achieve that behaviour using GHC LiberalTypeSynonyms extension. This extension treats type synonyms as macros, which are expanded without any check and afterwards the type checker will see whether the type synonym was partially applied.

    Kind inference is still done before type synonym expansion.

    Note that type synonyms cannot be partially applied even in this case, i.e. something such as:

    Test' Int
    

    is still an error. However you now can use type synonyms to totally apply other type synonyms without receiving errors.

    Allowing partial application of type synonyms would make type inference undecidible, so it's not possible to easily extend the type system in that direction.

    0 讨论(0)
提交回复
热议问题