[Web page edits (tclass and bindings). nordland@csee.ltu.se**20081112105544] { hunk ./web/bindings.html 100 -
+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. +
+
Examples | Comments |
---|---|
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 : y | Legal forward reference |
y = 2 : x | Ok, 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 -
-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 -