5

I have two functions for controlling loops, continue and break:

type Control a = (a -> a) -> a -> a

continue :: Control a
continue = id

break :: Control a
break = const id

Then, I wanted to simplify the Control type synonym. Hence, I wrote:

type Endo a = a -> a

type Control a = Endo (Endo a)

continue :: Control a
continue = id

break :: Control a
break = const id

However, when I tried to further simplify it I got an error:

GHCi, version 7.10.2: http://www.haskell.org/ghc/  :? for help
Prelude> type Endo a = a -> a
Prelude> type Duplicate w a = w (w a)
Prelude> type Control a = Duplicate Endo a

<interactive>:4:1:
    Type synonym ‘Endo’ should have 1 argument, but has been given none
    In the type declaration for ‘Control’

I don't understand why I am getting this error. Perhaps you could enlighten me.

Aadit M Shah
  • 72,912
  • 30
  • 168
  • 299

2 Answers2

13

As said by Fraser, this kind of stuff can't generally work because type partially applied type synonyms make everything undecidable.

However, if you chuck in the -XLiberalTypeSynonyms extension, GHC will inline the synonyms until it can work out the inference:

Prelude> type Endo a = a -> a
Prelude> type Duplicate w a = w (w a)
Prelude> type Control a = Duplicate Endo a

<‌interactive>:4:1:
    Type synonym ‘Endo’ should have 1 argument, but has been given none
    In the type declaration for ‘Control’
Prelude> :set -XLiberalTypeSynonyms
Prelude> type Control a = Duplicate Endo a
Community
  • 1
  • 1
leftaroundabout
  • 117,950
  • 5
  • 174
  • 319
9

Type synonyms have to be fully applied al all times. You cannot partially apply them.

If you're intent on doing this, you will likely need to newtype it.

Fraser
  • 1,521
  • 2
  • 14
  • 23
  • 2
    This. `Duplicate Endo a` is valid if `Endo` is a `data` or `newtype`, but *not* if it's just a `type`. – MathematicalOrchid Jul 30 '15 at 15:36
  • 1
    see [here](https://ghc.haskell.org/trac/ghc/ticket/785) and [here](http://stackoverflow.com/questions/4922560/why-doesnt-typesynonyminstances-allow-partially-applied-type-synonyms-to-be-use) for more info. the gist is that inference on partially applied synonyms is undecidable – Fraser Jul 30 '15 at 15:42