RE: variant filtering

From: Jacques Garrigue (garrigue@kurims.kyoto-u.ac.jp)
Date: Mon Feb 21 2000 - 03:44:55 MET

  • Next message: Coscoy, Yann: "Stream standard library"

    From: Manuel Fahndrich <maf@microsoft.com>

    > yes, I see that one has to be able to capture the row variable and it would
    > be part of two different types. The work-around you suggest is fine when the
    > number of variant cases is small and known. However, one nice thing about
    > polymorphic variants is their openness. Thus, enumerating all the other
    > cases is not really an option in many cases.

    There are two different problems here. One of expressiveness (all
    cases have to be known, clearly variants in ocaml are not as
    expressive as in Wallace), and another of conciseness (the need to
    explicitely write all cases).

    I think that expressiveness is not the main problem. The added
    expressive power is in my view too complex to be used in most
    programs. And in general open pattern matching on variants is not a
    good idea, beacuse it weakens the type checking (typos do not cause
    type errors).

    There are solutions for the conciseness problem, like allowing one to
    write a name of variant type instead of the complete case list:

    type failures = [`FailureA | `FailureB | `FailureC]

    let g () =
      match f () with
        `Success s -> `Success 1
      | #failures as other -> other

    Would it be enough?

    Remark also that most examples that would use the extra expressiveness
    of explicit row variables can also be rewritten in this formalism:

    # let f0 = function
          `SuccessA x -> x
        | `SuccessB x -> x*2;;
    val f0 : [<`SuccessA int|`SuccessB int] -> int

    With row variables:
    # let protect f x =
        match x with
          `FailureA -> -1
        | `FailureB -> -2
        | `FailureC -> -3
        | other -> f other;;
    val protect : (['r] -> int) -> [<`FailureA|`FailureB|`FailureC|'r] -> int
    # let g = protect f0;;
    g : [<`FailureA|`FailureB|`FailureC|`SuccessA int|`SuccessB int] -> int

    Without row variables:
    # let handler x =
        match x with
          `FailureA -> -1
        | `FailureB -> -2
        | `FailureC -> -3;;
    val handler : [<`FailureA|`FailureB|`FailureC] -> int
    # let g = function
        | #success as x -> f0 x
        | #failures as x -> handler x;;
    g : [<`FailureA|`FailureB|`FailureC|`SuccessA int|`SuccessB int] -> int

    As you can see, the main difference is that you have to add some glue
    by hand. This is less abstract than only applying a function, but the
    advantage is that you stay at a simpler level of reasonning. In
    practice I believe you do not get more verbose.

    Jacques
    ---------------------------------------------------------------------------
    Jacques Garrigue Kyoto University garrigue at kurims.kyoto-u.ac.jp
                    <A HREF=http://wwwfun.kurims.kyoto-u.ac.jp/~garrigue/>JG</A>



    This archive was generated by hypermail 2b29 : Mon Feb 21 2000 - 18:26:56 MET