Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to resolve type synonyms in haskell?

Tags:

types

haskell

When you create a type synonym with type, ghc/ghci will use it instead of the original type whenever it is used explicitly, but will never attempt to work backwards from an inferred type to a matching synonym. Getting the most "abstract" synonym for a type would be pretty handy to learn complicated applications and libraries, which define synonyms for monad stacks and possibly synonyms of synonyms.

Has anybody ever written such a piece of code? I imagine it would be backtracking and it would also generate some spurious candidates (e.g. if two types are aliases of String, then they will both be candidates whenever a String must be resolved), but it could be useful in certain situations.

like image 522
BruceBerry Avatar asked Aug 05 '13 19:08

BruceBerry


1 Answers

Not an answer, but a question. Type synonyms are often used to nicely name types in "high level code", but as soon as you pass those types into lower level/helper code (that are defined in terms of more concrete types), how should the system keep track of which synonym applies? consider the following:

type Title = String
type Name  = String

capitalise :: String -> String

my_title = "Mayor" :: Title

shouted_title = capitalise my_title :: ???

How does the typechecker know that the String going into heleper function capitalise is conceptually the same type as the String coming out of capitalise? In the presence of multiple type aliases, how should the type checker choose which to use?

like image 188
Thomas Avatar answered Sep 19 '22 11:09

Thomas