Changes between Version 2 and Version 3 of TypeApplication


Ignore:
Timestamp:
Sep 14, 2013 6:27:05 AM (7 months ago)
Author:
Hamidhasan
Comment:

Added design decisions, and cleaned up formatting

Legend:

Unmodified
Added
Removed
Modified
  • TypeApplication

    v2 v3  
     1 
     2== Type Application == 
     3 
     4 
    15[Explicit] Type Application is a feature requested for Haskell that lets a programmer explicitly declare what types should be instantiated for the arguments to a function application, in which the function is polymorphic (containing type variables and possibly constraints) . Doing so essentially “short-circuits” much of the type variable unification process, which is what GHC normally attempts when dealing with polymorphic function application. 
    2     In general, Haskell’s unification algorithm is robust enough that it can do this without the programmer needing to specify explicit types. There are a few edge cases, however: see below for examples. Though some of these cases can be solved with type annotations, such annotations can be cumbersome, as the programmer needs to provide the entire signature, which can be cumbersome in complicated expressions.  
     6 
     7In general, Haskell’s unification algorithm is robust enough that it can do this without the programmer needing to specify explicit types. There are a few edge cases, however: see below for examples. Though some of these cases can be solved with type annotations, such annotations can be cumbersome, as the programmer needs to provide the entire signature, which can be cumbersome in complicated expressions.  
     8 
     9 
    310It is worth noting that GHC’s intermediate language, “Core”, is fully typed and has no ambiguity - every polymorphic function application is explicitly typed. Thus, one way to think of this addition is “exposing” this feature of Core to the programmer. Indeed, once the evidence (generated by instantiating the types) is propagated to Core, it is able to be handled completely by that part of the compiler - implementing this feature did not require any changes to Core, nor to the lower-level compiler pipeline (optimizations, assembly generation, etc). 
    411See directly below for usage examples. More detailed design decisions follow the examples. 
    512 
    613 
    7 '''Usage:'''  
     14---- 
     15 
     16 
     17== '''Usage:''' == 
     18  
    819 
    920This extension can be enabled with “ExplicitTypeApplication” within the LANGUAGE pragma, or use the flag “-XExplicitTypeApplication”. The usage of this flag does not turn on any other flag implicitly. Using this flag will make a whitespace-sensitive distinction preceding each ‘@’ character: No whitespace before ‘@’ will result in an as-pattern, while having whitespace before the ‘@’ will parse as a type-application. (When this flag is turned off, the behavior is the same as it is now - no whitespace sensitivity). See '''Design''' with more information on the syntax. 
     
    6475twoBad = too @Maybe (T :: Two Either) 
    6576}}} 
     77 
     78---- 
     79 
     80== '''Design''' == 
     81 
     82There are several small design questions that can be asked of Explicit Type Application. Below are the questions and the decisions that were made: 
     83 
     84- ''Is a type annotation and/or signature required for a function in order to use type applications when applying it?'' 
     85 
     86No. Haskell generalizes certain functions, with a simple, straightforward signature; all the type variables are at the top, and it is a fairly simple signature to instantiate and work with.  
     87 
     88- ''Should we require a forall in the signature?''  
     89 
     90No, for similar reasons as above. Additionally, we did not want to create a dependency on the "ExplicitForAll" flag, and wanted type applications to be a small, surgical feature that has as few side effects as possible. 
     91 
     92- ''What order is used to instantiate the type variables?''  
     93 
     94Left-to-right order of the type variables appearing in the foralls. This is the most logical order that occurs when the instantiation is done at the type-variable level. Nested foralls work slightly differently, but at a single forall location with multiple variables, left-to-right order takes place. (See below for nested foralls). 
     95 
     96- ''How will it work with partial function application? Will we allow: leaving out arguments in function application, but allow type application to de-generalize the expression?'' 
     97 
     98Yes. This will allow the programmer to use the partially applied the function later, but only to arguments with specific types. This could be useful for a library designer, to use a generalized function internally, and only expose a specialized version of that function in the interface.   
     99 
     100- '' Wildcard Application'' 
     101Allows the programmer to not instantiate every type variable if they do not want to. See the examples section (above) for a use case. 
     102 
     103- ''Should non-prenex-form functions be allowed to use type applications? If so, how should we allow it? '' 
     104 
     105Yes. We allow this by requiring that type application appear where the forall is located in the type. See the following example: 
     106{{{ 
     107many :: forall a b. a -> b -> forall c. [c] -> forall d . Num d => d -> (a, b, [c], d) 
     108many a b c d = (a, b, c, d) 
     109foo =  many @Int @Bool 5 True @Char "hello" @Float 17 
     110}}} 
     111 
     112- ''Concrete Syntax'': 
     113 
     114We choose to use the ‘@’ symbol, as this is the symbol that is used in Core, GHC’s intermediate language. Turning on this extension will make the ‘@’ symbol whitespace-sensitive in the front: whitespace before an ‘@’ will parse as a type-application, while no whitespace in front of the ‘@’ will parse as an as-pattern. This is similar to the way ‘.’ behaves differently with whitespace (function composition vs. module naming), but note that the only whitespace sensitivity occurs ‘’’before’’’ the ‘@’ and not after. Additionally, when the extension is off, there is no change in current behavior and no whitespace sensitivity. 
     115