[Web page edits (tclass and bindings). nordland@csee.ltu.se**20081112105544] { hunk ./web/bindings.html 100 -  where f x = rJust n (show x) +  where f x = rJust n (show x) hunk ./web/bindings.html 139 -   where Just y = lookup x ps +   where Just y = lookup x ps hunk ./web/bindings.html 143 -
  • Instance bindings for implicit struct types. +
  • Instance bindings for struct types declared as type classes. hunk ./web/bindings.html 146 -implicit var :: type - funBind* +instance var :: type where + bind* hunk ./web/bindings.html 149 +This is just a notational short-hand for an instance declaration of some variable and the binding +of that variable to a struct expression. hunk ./web/bindings.html 152 -The type signature of an instance of an implicit type is prepended by the keyword implicit. For instances, -a type signature is compulsory; only the function binding defining the instance is not sufficient, +
    +instance var :: type
    +var = struct
    +  bind*
    +
    hunk ./web/bindings.html 159 -The order between bindings in a sequence of bindings is not significant, with the following exceptions: + +With the exception of struct expressions, +bindings in a sequence are mutually recursive. This amounts to bound variables of all types and is +independent of whether the corresponding right-hand sides need further evaluation or not. +

    +Evaluation +of bindings takes place in dependency order, but follows the declared order for bindings that are +truly mutually dependent. This declared order is significant for one specific reason: evaluation +of each binding must be able to complete without looking into the actual value of any of its +forward references. Failure to do so will result in a run-time error. +

    + + + + + + + + + + + + + +
    ExamplesComments
    f 0 = 1
    f n = n * f (n-1)Ordinary recursive function binding
    g h 0 = 1
    g h n = n * h (n-1) Non-recursive higher-order function binding
    f' = g f'Recursive binding of f' (equivalent to f)
    x = 1 : yLegal forward reference
    y = 2 : xOk, both x and y are cyclic lists
    a = 1 : b
    b = head a : []Legal backward reference (ok to look into the value of a)
    a' = head b' : []Illegal forward reference (must look into the value of b')
    b' = 2 : a'Not reached (run-time error on previous line)
    +

    +In addition, binding sequences are subject to the following syntactic restrictions: hunk ./web/bindings.html 187 -

  • All the equations that make up a function binding must be adjacent to each other with no other binding intervening. -
  • A type signature must precede the binding equations for the variables that are typed by the signature. +
  • All the equations that make up a function binding must be adjacent to each other with no + other binding intervening. +
  • A type signature must precede the binding equations for the variables that are typed by + the signature. hunk ./web/bindings.html 192 -

    -Function bindings are recursive. For pattern bindings certain limited forms of recursion are possible, in order -to build finite, cyclic data structures. To be described more... hunk ./web/expr.html 185 + hunk ./web/expr.html 268 + hunk ./web/tclass.html 34 -An object of type Num Int has three fields, defining addition, +A value of type Num Int has three fields, defining addition, hunk ./web/tclass.html 36 -of the prescribed type, but the intention is to supply the standard arithmetic operators.) Similarly, an object of type Num Float +of the prescribed type, but the intention is to supply the standard arithmetic operators.) Similarly, a value of type Num Float hunk ./web/tclass.html 40 -properly defined numInt :: Num Int. We can then define +properly defined struct value numInt :: Num Int. We can then define hunk ./web/tclass.html 47 -Unfortunately, the first argument in the recursive call now looks horrible. We cannot accept to write integer addition in this way. But there is -at least something positive; we can generalize the type by giving the Num object as an argument: +Of course, the first argument in the recursive call now looks horrible; it would be silly to write integer addition in this way. +But it has now become easy to generalize the code by abstracting out the Num object as an argument: hunk ./web/tclass.html 55 -We can now use add for lists of any type of objects for which we can define the arithmetic operators, at the expense of passing an extra -argument to the function. +This version of add can be used for lists of any type of objects for which we can define the arithmetic operators, at +the expense of passing an extra argument to the function. hunk ./web/tclass.html 64 -For such types, the selectors are used without the +For any such type, its selectors can be used without the hunk ./web/tclass.html 66 -body, the compiler -does the following: +body, the compiler does the following: hunk ./web/tclass.html 74 -Our running example now becomes +With Num defined as a type class, our running example becomes hunk ./web/tclass.html 87 -To use function add to sum a list of integers, we could write e.g. add 0 [1, 7, 4]. The compiler must now insert +To use function add to sum a list of integers, we would like to write e.g. add 0 [1, 7, 4]. The compiler must now insert hunk ./web/tclass.html 89 -However, since there might be several objects of type Num Int defined, we must indicate in instance declarations -the objects that are to be used at different types. The definition of the struct value and its instance declaration can be combined -into one declaration. As an example, here is an instance of Num for rational numbers: +However, since there might be several objects of type Num Int defined, we must indicate in instance declarations +the objects that are to be used at different types. +

    +instance numInt :: Num Int
    +
    +An instance declaration is essentially just a type signature flagged with the additional information that the corresponding value +is available for automatic argument insertion by the compiler. +For convenience, an instance declaration and its struct value definition can also be combined +into one declaration. As an example, here is an instance of Num for rational numbers: hunk ./web/toplevel.html 17 -
  • Definitions of instances of implicit struct types. +
  • Definitions of instances of implicit struct types. }