[[project @ 2005-05-12 12:55:32 by chak] chak**20050512125532 Added a new section that describes GHC's hybrid type representation `TypeRep.Type' and it's friends. This sufficiently subtle and GHC-specific that it warrants extra treatment outside of the section on type checking. ] { addfile ./ghc/docs/comm/the-beast/types.html hunk ./ghc/docs/comm/index.html 9 -

The Glasgow Haskell Compiler (GHC) Commentary [v0.16]

+

The Glasgow Haskell Compiler (GHC) Commentary [v0.17]

hunk ./ghc/docs/comm/index.html 68 +
  • Hybrid Types hunk ./ghc/docs/comm/index.html 117 -Last modified: Wed May 4 11:48:54 EST 2005 +Last modified: Thu May 12 19:03:42 EST 2005 hunk ./ghc/docs/comm/the-beast/typecheck.html 31 - Id. + Id. +

    +

    + It is also noteworthy, that the representations of types changes during + type checking from HsType to TypeRep.Type. + The latter is a hybrid type representation that + is used to type Core, but still contains sufficient information to + recover source types. In particular, the type checker maintains and + compares types in their Type form. hunk ./ghc/docs/comm/the-beast/typecheck.html 312 -Last modified: Mon May 9 11:02:20 EST 2005 +Last modified: Thu May 12 22:52:46 EST 2005 hunk ./ghc/docs/comm/the-beast/types.html 1 + + + + + The GHC Commentary - Hybrid Types + + + +

    The GHC Commentary - Hybrid Types

    +

    + GHC essentially supports two type systems: (1) the source type + system (which is a heavily extended version of the type system of + Haskell 98) and the Core type system, which is the type system + used by the by far most important intermediate language (see also Sugar Free: From Haskell To Core). +

    +

    + During parsing and renaming, type information is represented in a form + that is very close to Haskell's concrete syntax; it is defined by + HsTypes.HsType. In addition, type, class, and instance + declarations are maintained in their source form as defined in the + module HsDecl. The situation changes during type checking, + where types are translated into a second representation, which is + defined in the module types/TypeRep.lhs, as type + Type. This second representation is peculiar in that it is + a hybrid between the source representation of types and the Core + representation of types. Using functions, such as + Type.coreView and Type.deepCoreView, a value + of type Type exhibits its Core representation. On the + other hand, pretty printing a Type with + TypeRep.pprType yields the type's source representation. +

    +

    + In fact, the type checker maintains type + environments based on Type, but needs to perform type + checking on source-level types. As a result, we have functions + Type.tcEqType and Type.tcCmpType, which + compare types based on their source representation, as well as the + function coreEqType, which compares them based on their + core representation. The latter is needed during type checking of Core + (as performed by the functions in the module + coreSyn/CoreLint.lhs). +

    + +

    Type Synonyms

    +

    + Type synonyms in Haskell are essentially a form of macro definitions on + the type level. For example, when the type checker compares two type + terms, synonyms are always compared in their expanded form. However, to + produce good error messages, we like to avoid expanding type synonyms + during pretty printing. Hence, Type has a variant + NoteTy TyNote Type, where +

    +
    +
    +data TyNote
    +  = FTVNote TyVarSet	-- The free type variables of the noted expression
    +
    +  | SynNote Type	-- Used for type synonyms
    +			-- The Type is always a TyConApp, and is the un-expanded form.
    +			-- The type to which the note is attached is the expanded form.
    +
    +

    + In other words, a NoteTy represents the expanded form of a + type synonym together with a note stating its source form. +

    + +

    Newtypes

    +

    + Slightly more involved are data types declared via a + newtype declaration. These newtypes constitute new type + constructors---i.e., they are not just type macros, but introduce new + type names. However, provide that a newtype is not recursive, we still + want to implement it by its representation type. In other words, + tcEqType cannot see through a newtype, but + coreEqType can. +

    + +

    Predicates

    +

    + The dictionary translation of type classes, translates each predicate in + a type context of a type signature into an additional argument, which + carries a dictionary with the functions overloaded by the corresponding + class. The Type data type has a special variant + PredTy PredType for predicates, where +

    +
    +
    +data PredType 
    +  = ClassP Class [Type]		-- Class predicate
    +  | IParam (IPName Name) Type	-- Implicit parameter
    +
    +

    + These types need to be handled as source type during type checking, but + turn into their representations when inspected through + coreView. The representation is determined by + Type.predTypeRep. +

    + +

    Classes and Instances

    +

    + Class declarations turn into values of type Class.Class. + They represent methods as the Ids of the dictionary + selector functions. Similar selector functions are available for + superclass dictionaries. +

    +

    + Instance declarations turn into values of type + InstEnv.Instance, which in interface files are represented + as IfaceSyn.IfaceInst. Moreover, the type + InstEnv.InstEnv, which is a synonym for UniqFM + ClsInstEnv, provides a mapping of classes to their instances - + ClsInstEnv is essentially a list of instance declarations. +

    + +

    + +Last modified: Thu May 12 22:47:50 EST 2005 + +

    + + }