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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With