Difference between revisions of "GHC/Type system"
< GHC
Jump to navigation
Jump to search
(fix link to OverlappingInstances) |
|||
(16 intermediate revisions by 7 users not shown) | |||
Line 2: | Line 2: | ||
<span style='font-size: x-large; font-weight: bold'>Type system extensions in GHC</span> |
<span style='font-size: x-large; font-weight: bold'>Type system extensions in GHC</span> |
||
− | GHC comes with a rather large collection of type-system extensions (beyond Haskell 98). |
+ | GHC comes with a rather large collection of type-system extensions (beyond Haskell 98). {{GHCUsersGuide|exts||a section on Language Extensions}} But this page is a place to record user-oriented observations, notes, and suggestions on how to use them. |
+ | * '''[[GHC/Indexed_types|Indexed data types]]''' |
||
⚫ | |||
+ | * '''[[GHC/TypeHoles|Agda-like "holes" in GHC]]''' |
||
− | |||
⚫ | |||
− | It's quite common for people to write a function definition without a type signature, load it into GHCi, use <tt>:t</tt> to see what type it has, and then cut-and-paste that type into the source code as a type signature. Usually this works fine, but alas not always. Perhaps this is a deficiency in GHC, but here's one way it can happen: |
||
⚫ | |||
− | <haskell> |
||
+ | * '''Overlapping instances'''. GHC supports overlapping instances, with carefully specified rules. Make sure you read the [http://downloads.haskell.org/~ghc/latest/docs/html/users_guide/exts/instances.html#overlapping-instances relevant sections of the user manual]. Here is an application note about [[GHC/AdvancedOverlap|advanced use of overlapping instances]], combined with functional dependencies. |
||
− | class C a b where |
||
+ | * '''[[GHC/SuperClass|Super-Class Constraints: Type Families, Type Equalities, FunDep, Overlaps]]''' |
||
− | foo :: a -> b |
||
+ | * '''[[GHC/Coercible|Safe coercions using Coercible]]''' |
||
− | |||
− | konst :: a -> Bool |
||
− | konst x = True |
||
− | |||
− | f :: (C a b) => a -> Bool |
||
− | f x = konst (foo x) |
||
− | </haskell> |
||
− | If you comment out the type signature <hask>f :: (C a b) => a -> Bool</hask>, the module will load fine into GHCi, and <tt>:t</tt> will report exactly this type for <tt>f</tt>. But if you leave the type signature in, you'll get this error: |
||
− | <pre> |
||
− | Foo1.hs:12:13: |
||
− | Could not deduce (C a b1) from the context (C a b) |
||
− | arising from use of `foo' at Foo1.hs:12:13-17 |
||
− | Possible fix: add (C a b1) to the type signature(s) for `f' |
||
− | In the first argument of `konst', namely `(foo x)' |
||
− | In the expression: konst (foo x) |
||
− | In the definition of `f': f x = konst (foo x) |
||
− | </pre> |
||
− | What's going on? Without the type signature, GHC picks a type for <tt>x</tt>, say <tt>x::a</tt>. Then applying <tt>foo</tt> means GHC must pick a return type for <tt>foo</tt>, say <tt>b</tt>, and generates the type constraint <tt>(C a b)</tt>. The function <tt>konst</tt> just discards its argument, so nothing further is known abouut <tt>b</tt>. So GHC ends up saying that <hask>f :: (C a b) => a -> Bool</hask>. |
||
− | |||
− | This is probably a very stupid type. Suppose you called <tt>f</tt> thus: <tt>(f 'a')</tt>. Then you'd get a constraint <tt>(C Char b)</tt> where nothing is known about <tt>b</tt>. That would be OK if there was an instance like: |
||
− | <haskell> |
||
− | instance C Char b where ... |
||
− | </haskell> |
||
− | |||
− | == Overlapping instances == |
||
− | |||
− | Here an [http://www.haskell.org//pipermail/glasgow-haskell-bugs/2006-July/006808.html interesting message] about the interaction of existential types and overlapping instances. |
||
− | |||
− | == Indexed data types and indexed newtypes == |
||
− | |||
− | [[GHC/Indexed_types|Indexed data types]] (including associated data types) are a very recent addition to GHC's type system extensions that is not yet included in the user manual. To use the extension, you need to obtain a version of GHC from [http://hackage.haskell.org/trac/ghc/wiki/Building/GettingTheSources its source repository]. |
||
− | |||
− | == Stand-alone deriving clauses == |
||
− | |||
⚫ | |||
− | ----------------------- |
Latest revision as of 20:32, 23 July 2021
Type system extensions in GHC
GHC comes with a rather large collection of type-system extensions (beyond Haskell 98). The GHC Users Guide has a section on Language Extensions. But this page is a place to record user-oriented observations, notes, and suggestions on how to use them.
- Indexed data types
- Agda-like "holes" in GHC
- "Stand-alone deriving" declarations
- Type signatures and ambiguity
- Overlapping instances. GHC supports overlapping instances, with carefully specified rules. Make sure you read the relevant sections of the user manual. Here is an application note about advanced use of overlapping instances, combined with functional dependencies.
- Super-Class Constraints: Type Families, Type Equalities, FunDep, Overlaps
- Safe coercions using Coercible