atom-haskell-archive / haskell-ghc-mod

haskell-ghc-mod atom package
https://atom.io/packages/haskell-ghc-mod
MIT License
69 stars 20 forks source link

Insert Type should use type synonyms #72

Closed wiwa closed 8 years ago

wiwa commented 8 years ago

Given:

type ManyInt = [Int]

I would except a function with type-inferred signature [Int] to have a signature of ManyInt if I use "Insert Type" from the right-click menu. However, it continues to use [Int].

DanielG commented 8 years ago

This is a limitation in GHC as far as I know. It tries to only apply type synonyms in places where they are actually relevant which is going to not give what you want sometimes. Imagine if you imported a type synonym type Foo = String from some module. Would you want GHC to rewrite every occurrence of String with Foo? And what if some other package/module you're also importing defines type Bar = String, which one would it use?

wiwa commented 8 years ago

Interesting, that makes sense. What are these places in which it would apply type synonyms?

DanielG commented 8 years ago

I think it would be anything that doesn't try to unify Foo with anything else and ends up having to look inside the synonym, some examples:

> type Foo = String
> let strfoo :: String -> Foo; strfoo = id

These just unify Foo ~ a so GHC can keep the type synonym intact:

> :t strfoo ""
strfoo "" :: Foo

> :t id $ strfoo ""
id $ strfoo "" :: Foo

> :t repeat $ strfoo ""
repeat $ strfoo "" :: [Foo]

> :t (:[]) $ strfoo ""
(:[]) $ strfoo "" :: [Foo]

> :t repeat $ strfoo ""
repeat $ strfoo "" :: [Foo]

whereas these have to unify Foo ~ [a] so it can't:

> :t (++) "" $ strfoo ""
(++) "" $ strfoo "" :: [Char]

> :t cycle $ strfoo ""
cycle $ strfoo "" :: [Char]
lierdakil commented 8 years ago

Closing, as this is out of scope for this package.