caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jean-Francois Monin <jeanfrancois.monin@rd.francetelecom.fr>
To: Mark Wotton hons2001 <mrak@cs.usyd.edu.au>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Subtypes in Ocaml
Date: Wed, 25 Apr 2001 13:04:15 +0200 (CEST)	[thread overview]
Message-ID: <15078.44847.842364.483958@gargle.gargle.HOWL> (raw)
In-Reply-To: <Pine.LNX.4.21.0104251737080.14021-100000@hons.cs.usyd.edu.au>

> I might wait until i'm a hardcore ocaml hacker, then.
> Might be a while.

Unless you have a special reason to think with subtypes, my advice
would be to change a little your way of seeing things. Basic notions of
Ocaml like sum (now called variant) types are clean, simple and very
good for describing syntactic trees and parsers. See sec 1.4 of the
manual (Records and variants).

A typical example is given in sec 1.7 (Symbolic processing of
expressions). I lost your original message, may be you want something
like:

type expression =
  | Lit of litteral
  | Sum of expression * expression
  | Diff of expression * expression
  | Prod of expression * expression
  | Quot of expression * expression

type litteral =
  | Const of float
  | Var of string

You can convert a litteral l into an expression using Lit(l). 
Conversely, if you have an expression e which shoud come
from a litteral, use pattern matching, e.g. 

let lit_of_exp e = 
  match e with
  | Lit(l) -> l
  | _ -> failwith "expression should come from a litteral"
  end


In some contexts you may also consider:

type expression =
  | Sum of expression * term
  | Diff of expression * term
  | Term of term

and term =
  | Lit of litteral
  | Prod of term * litteral
  | Quot of term * litteral
  | Parentexp of expression

and litteral =
  | Const of float
  | Var of string

  Jean-Francois Monin

-----------------------------------------------------------------------
Mark Wotton hons2001 wrote:
 > On Wed, 25 Apr 2001, Chris Hecker wrote:
 > 
 > > 
 > > >Patternmatching seems to do it. if this isn't the right way to do it,
 > > >please, someone let me know before i do something stupid.
 > > 
 > > I'm not 100% sure what you're saying.  My first interpretation of your
 > > question leads me to say "this is what variants and pattern matching
 > > are all about".  Since a variant is a union type, it can be a Literal
 > > and just an expression and pattern matching will let you choose which
 > > to interpret it as.
 > 
 > Yes, that was it.
 > 
 > I had a suspicion it was a dumb question... i'm just not used to using
 > pattern-matching like that. It's working nicely now.
 > 
 > > My second interpretation is that you want to have a function typed to
 > > only take a subset of the variant type.  In this case, you can either
 > > use exceptions (the default pattern match one, or one raised with a |
 > > _ -> failwith "bad type" if you don't want the compiler warnings), 
 > 
 > Yes, I want this too.
 > Pattern matching seems to do it nicely. I was doing without it and
 > wondering why everything was so hard...
 > 
 > > or I think you can do something with polymorphic variants, but I must
 > > admit to being kind of confused by them.
 > 
 > I might wait until i'm a hardcore ocaml hacker, then.
 > Might be a while.
 > 
 > mrak
 > 
 > 
 > -------------------
 > To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr
-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr


  reply	other threads:[~2001-04-25 11:04 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-04-23 10:05 [Caml-list] Cygwin ocaml crashes wester
2001-04-23 13:49 ` Alan Schmitt
2001-04-23 14:16   ` wester
2001-04-23 15:39     ` Alan Schmitt
2001-04-23 15:59     ` Mattias Waldau
2001-04-24  7:00       ` wester
2001-04-24  7:44         ` Alan Schmitt
2001-04-24 20:52           ` Mattias Waldau
2001-04-25  3:17             ` [Caml-list] Subtypes in Ocaml Mark Wotton hons2001
2001-04-25  5:45               ` Mark Wotton hons2001
2001-04-25  7:35                 ` Chris Hecker
2001-04-25  7:42                   ` Mark Wotton hons2001
2001-04-25 11:04                     ` Jean-Francois Monin [this message]
2001-04-25 10:02           ` [Caml-list] Cygwin ocaml crashes Christophe Macabiau

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=15078.44847.842364.483958@gargle.gargle.HOWL \
    --to=jeanfrancois.monin@rd.francetelecom.fr \
    --cc=caml-list@inria.fr \
    --cc=mrak@cs.usyd.edu.au \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).