Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] syntax of private constructors in CVS version
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2003-06-30 (21:36)
From: brogoff@s...
Subject: Re: [Caml-list] syntax of private constructors in CVS version
On Mon, 30 Jun 2003, Chris Hecker wrote:
> [Excellent rationale for private types snipped.]

Indeed. Nice feature. 

> >In contrast with abstract data types, the private types still maintain
> >the elegant pattern matching facility of ML outside the module that
> >defines the type.
> So, when do we get views to solve this problem for abstract types?  If 
> pattern matching is so great (which it is) it seems a crime to not allow it 
> on abstract types (another great feature).  To make an american candy joke, 
> peanut butter and chocolate taste great together!

I guess we all know what you're going for when you hit that vending machine!

I was going to reply to a message in last week's thread about polytypic 
programming in which the use of open recursion/fixed points on functions was
demonstrated, that there is a "dual" trick at the level of types, the 
so-called two level types. One place this trick finds some use is in the 
encoding of views into ML like languages. You can find the trick demonstrated 
for SML here

but I'm not sure how much using this style costs in efficiency in OCaml. 
If it costs too much, we'll have to bug Stephen Weeks to implement OCamlton. 
BTW, it's nice to have views (and all kinds of things) supported directly in 
the language but these tricks seem workable to me. It seems like OCaml 
could be a bit nicer than SML here (what a surprise! :), since we could use 
polymorphic variants for the external interface, as there is no type 
abstraction there. For example, something like the following for the earliest 
examples in that paper 

module type NAT =
    type t
    type 'a s = [`Zero | `Succ of 'a]
    val inj : t s -> t
    val prj : t -> t s

module SimpleNat : NAT =
    type 'a s = [`Zero | `Succ of 'a]

    (* Look ma, no intermediate constructor for recursive types! *)
    type t = t s

    let inj x = x
    let prj x = x

module FastNat : NAT =
    type 'a s = [`Zero | `Succ of 'a]
    type t = int

    let inj = function
        `Zero -> 0
      | `Succ n -> n + 1

    let prj = function
        0 -> `Zero
      | n -> `Succ (n - 1)

As a digression, the two level type trick is also used (in OCaml) here
(This is a very appealing paper to me, as the embedded language technique the 
author describes looks applicable to a few things I'm doing) 

and here

and of course in a ton of Haskell/Gofer/Squiggol oriented papers. 

As another digression, the trick of using open recursive functions and "fixing" 
them manually with special fps is explained nicely here:

though the post last week was the first time I saw it used in quite that way. 
That was quite neat. I wonder how that technique dovetails with two level 

-- Brian

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: