Re: let ... in layout

From: Pierre Weis (weis@pauillac.inria.fr)
Date: Fri Mar 17 2000 - 10:42:15 MET

  • Next message: thierry BRAVIER: "Re: typing of a class"

    > Jean-Yves Moyen <Jean-Yves.Moyen@loria.fr> writes:
    >
    > > let f a b c=
    > > match a,b,c with
    > > ...
    > >
    > > which allows you to match several arguments at once.
    >
    > Sure, but
    >
    > let f a b c =
    > match a,b,c with
    > 0,1,2 -> 3
    >
    > is a lot more cluttered than
    >
    > f 0 1 2 = 3
    >
    > This wouldn't really be an issue except that pattern matching is so
    > common. Intuitive brevity (as opposed to the brevity represented by
    > languages such as perl) is what makes both haskell and *ml so
    > readable. While both functions seem equally readable because they are
    > both small, once you have a screen full of them there is a significant
    > difference in clarity.
    >
    > Cheers,
    > Julian.

    You're right but this is not only a problem of syntax. There are
    semantics issues to consider.

    As for the syntax, your example is a bit contrieved, since there are
    not that many functions with pattern matching on 3 arguments that has
    only one match case. In general, such a complex function will have
    many cases and then the (admittedly heavier) way of writing the code
    in Caml gives you an extra benefit of naming the arguments, not
    repeating the function name, and clearly exhibit the complete pattern
    matching in one place:

    let f a b c =
        match a, b, c with
        | 0, _, 0 -> a + b + c
        | ....

    Also, the semantics of partial applications of curried pattern
    matching is not clear: consider your f example

    let f 0 1 2 = 3;;

    What's the meaning of this definition ? Two interpretations have been
    used in Caml:

    let f1 a b c =
        match a, b, c with
        | 0, 1, 2 -> 3
        | _ -> raise Match_failure

    let f2 = function
      | 0 -> (function
              | 1 -> (function
                      | 2 -> 3
                      | _ -> raise Match_failure)
              | _ -> raise Match_Failure)
      | _ -> raise Match_Failure

    I guess f2 is more consistent with the usual semantics of ML, while f1
    seems to be more natural for users:

    with f2 partial applications will fail as early as necessary:
    (f 1 raises Match_failure as the definition of g in let g = List.map (f 1)),

    while with f1, application of f will fail only when 3 arguments are provided.

    I guess that this is this semantic problem, and the new syntax to add
    that prevents the addition of this feature to Caml.

    Best regards,

    Pierre Weis

    INRIA, Projet Cristal, http://pauillac.inria.fr/~weis



    This archive was generated by hypermail 2b29 : Fri Mar 17 2000 - 11:31:39 MET