From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id NAA12128; Wed, 25 Apr 2001 13:04:38 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id NAA12010 for ; Wed, 25 Apr 2001 13:04:37 +0200 (MET DST) Received: from p-mail2.cnet.fr (p-mail2.rd.francetelecom.fr [193.49.124.32]) by concorde.inria.fr (8.11.1/8.10.0) with SMTP id f3PB4aX13154 for ; Wed, 25 Apr 2001 13:04:36 +0200 (MET DST) Received: by p-voyageur.rd.francetelecom.fr with Internet Mail Service (5.5.2653.19) id <22FVSXLS>; Wed, 25 Apr 2001 13:04:21 +0200 Received: from lat4153 (lat4153.rd.francetelecom.fr [161.104.14.80]) by l-mhs1.lannion.cnet.fr with SMTP (Microsoft Exchange Internet Mail Service Version 5.5.2653.13) id GQ90PDHF; Wed, 25 Apr 2001 13:04:08 +0200 From: Jean-Francois Monin MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-ID: <15078.44847.842364.483958@gargle.gargle.HOWL> Date: Wed, 25 Apr 2001 13:04:15 +0200 (CEST) To: Mark Wotton hons2001 Cc: caml-list@inria.fr Subject: Re: [Caml-list] Subtypes in Ocaml In-Reply-To: References: <4.3.2.7.2.20010425002928.00d588b0@shell16.ba.best.com> X-Mailer: VM 6.72 under 21.1 (patch 10) "Capitol Reef" XEmacs Lucid Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk > 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