* Re: [Caml-list] Polymorphic variant difference... [not found] <20070514100005.459F6BC70@yquem.inria.fr> @ 2007-05-14 10:52 ` David Allsopp 2007-05-15 4:40 ` Jacques Garrigue 2007-05-14 10:52 ` [Caml-list] Compiling a library with findlib David Allsopp 1 sibling, 1 reply; 7+ messages in thread From: David Allsopp @ 2007-05-14 10:52 UTC (permalink / raw) To: caml-list > Given a polymorphic variant type t with a label `B how does one build > the type corresponding to t without the label `B. So that you could write something like: type t = [ `A | `B | `C ] let f x = match x with `B -> (* x has type t *) | #(t minus [ `B ]) as x -> (* x has type [ `A | `C ] *) I end up having to write lots of tedious extra types to achieve that normally... I'd find a subtraction syntax very handy too. On a similar subject, am I the only person who finds I often need to take a fixed polymorphic variant value and coerce it so that it can just accept more constructors e.g. (seriously contrived example)... type t = [ `A | `B | `C ] let f (x : t) = match x with `A -> `D | _ -> (x : t :> [> t ]) Something more concise than (x : t :> [> t ]) would be nice: e.g. (x ::> t) But perhaps that really is a job for camlp4! David ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [Caml-list] Polymorphic variant difference... 2007-05-14 10:52 ` [Caml-list] Polymorphic variant difference David Allsopp @ 2007-05-15 4:40 ` Jacques Garrigue 2007-05-15 9:45 ` David Allsopp 0 siblings, 1 reply; 7+ messages in thread From: Jacques Garrigue @ 2007-05-15 4:40 UTC (permalink / raw) To: dra-news; +Cc: caml-list From: "David Allsopp" <dra-news@metastack.com> > > Given a polymorphic variant type t with a label `B how does one build > > the type corresponding to t without the label `B. > > So that you could write something like: > > type t = [ `A | `B | `C ] > > let f x = > match x with > `B -> (* x has type t *) > | #(t minus [ `B ]) as x -> (* x has type [ `A | `C ] *) > > I end up having to write lots of tedious extra types to achieve that > normally... I'd find a subtraction syntax very handy too. There is no theoretical difficulty in adding this (when t is defined), but this would mean yet more syntax... I would be curious to see code where this becomes such a pain. In my view, having to name this type should help clarity. > On a similar subject, am I the only person who finds I often need to take a > fixed polymorphic variant value and coerce it so that it can just accept > more constructors e.g. (seriously contrived example)... > > type t = [ `A | `B | `C ] > > let f (x : t) = > match x with > `A -> `D > | _ -> (x : t :> [> t ]) > > Something more concise than (x : t :> [> t ]) would be nice: e.g. (x ::> t) > But perhaps that really is a job for camlp4! Indeed, this one is just about syntactic sugar, so camlp4 can do it. An alternative without new syntax would be to let (x :> [> t]) mean (x : [< t] :> [> t]), but this would break the invariant that coercions always work when the coerced value is already an instance of the target type (not that anybody uses this invariant...) Jacques Garrigue ^ permalink raw reply [flat|nested] 7+ messages in thread
* RE: [Caml-list] Polymorphic variant difference... 2007-05-15 4:40 ` Jacques Garrigue @ 2007-05-15 9:45 ` David Allsopp 2007-05-15 10:21 ` Frédéric van der Plancke 0 siblings, 1 reply; 7+ messages in thread From: David Allsopp @ 2007-05-15 9:45 UTC (permalink / raw) To: 'Jacques Garrigue', caml-list A SQL converter I'm writing ends up with these two types: type t' = [ `CreateDB of ... | `CreateType of ... | `CreateTable of ... | `CreateFunction of ... | `CreateProcedure of ... | `CreateRole of ... | `Eof ] type t = [ `Comment of ... | `Block of ... | `Blank | t' ] Solely so that further down the code I can say: match elt with `Block block -> ... | `Comment comment -> ... | `Blank -> ... | #t' as elt -> (* specific code only relevant when elt <> `Block, `Comment or `Blank *) match elt with (* Remaining match cases not including the three checked already *) type t' is only defined for use in # matches (the actual code will translate a variant-SQL script to T-SQL and is compacting custom blocks of unparsed SQL code, comments and blank lines into a single `Block before parsing actual clauses that it understands how to translate). If I don't use #t' then, as far as I can see, I must either add redundant matches to the second match block to prevent match warnings (and so have an "Unexpected" exception or failwith "programmer is an idiot" call for these 'unreachable' match clauses) or duplicate a lot of code between the match clauses. I equally get it with certain error handling matches: e.g. (contrived again, but I'd have to send a lot of code for a real version... this comes up with error handling between read/write functions in a socket stream! ) type t' = [ `B | `C ] type t = [ `A | t' ] val f : int -> [ `Success of int | `Error of t ] exception SpecialExceptionForA exception GeneralException of int (* NB matter of personal taste: I don't consider x in this function but I know that's not everyone's view!! *) let g x = match x with `B -> 0 | `C -> 1 match f 0 with `Success int -> true | `Error `A -> raise SpecialExceptionForErrorA | `Error (#t' as x) -> raise (GeneralException(g x)) It would be, I think, clearer (especially when considering ocamldoc comments**) for the type to be: type t = [ `A | `B | `C ] ... match f 0 with `Success int -> true | `Error `A -> ... | `Error (#t minus [ `A ] as x) -> ... So, in summary, my irritation lies with the need to create extra types just for the # matches. But I am very fussy :o) Of course, this is related to a previous thread: http://tinyurl.com/yp7rvy ... for polymorphic variants, it would be inferring the subtraction of `A from the type of x automatically because of the previous match clause. David ** Furthermore, I can't even hide these redundant types in the implementation: Foo.mli type t = [ `A | `B ] Foo.ml type t' = [ `A ] type t = [ `A | t' ] Is not valid even though t is /effectively/ the same p-variant in both the .mli and .ml -----Original Message----- From: Jacques Garrigue [mailto:garrigue@math.nagoya-u.ac.jp] Sent: 15 May 2007 05:41 To: dra-news@metastack.com Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Polymorphic variant difference... From: "David Allsopp" <dra-news@metastack.com> > > Given a polymorphic variant type t with a label `B how does one build > > the type corresponding to t without the label `B. > > So that you could write something like: > > type t = [ `A | `B | `C ] > > let f x = > match x with > `B -> (* x has type t *) > | #(t minus [ `B ]) as x -> (* x has type [ `A | `C ] *) > > I end up having to write lots of tedious extra types to achieve that > normally... I'd find a subtraction syntax very handy too. There is no theoretical difficulty in adding this (when t is defined), but this would mean yet more syntax... I would be curious to see code where this becomes such a pain. In my view, having to name this type should help clarity. > On a similar subject, am I the only person who finds I often need to take a > fixed polymorphic variant value and coerce it so that it can just accept > more constructors e.g. (seriously contrived example)... > > type t = [ `A | `B | `C ] > > let f (x : t) = > match x with > `A -> `D > | _ -> (x : t :> [> t ]) > > Something more concise than (x : t :> [> t ]) would be nice: e.g. (x ::> t) > But perhaps that really is a job for camlp4! Indeed, this one is just about syntactic sugar, so camlp4 can do it. An alternative without new syntax would be to let (x :> [> t]) mean (x : [< t] :> [> t]), but this would break the invariant that coercions always work when the coerced value is already an instance of the target type (not that anybody uses this invariant...) Jacques Garrigue ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [Caml-list] Polymorphic variant difference... 2007-05-15 9:45 ` David Allsopp @ 2007-05-15 10:21 ` Frédéric van der Plancke 0 siblings, 0 replies; 7+ messages in thread From: Frédéric van der Plancke @ 2007-05-15 10:21 UTC (permalink / raw) To: caml-list David Allsopp wrote: > ** Furthermore, I can't even hide these redundant types in the > implementation: > > Foo.mli > > type t = [ `A | `B ] > > Foo.ml > > type t' = [ `A ] > type t = [ `A | t' ] > > Is not valid even though t is /effectively/ the same p-variant in both the > .mli and .ml > That's maybe just because of a typo (you wrote `A for `B below) ? This seems to works with 3.09: # module M : sig type t = [ `A | `B ] end = struct type t' = [`A] type t = [`B | t' ] end;; module M : sig type t = [ `A | `B ] end Frédéric ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [Caml-list] Compiling a library with findlib [not found] <20070514100005.459F6BC70@yquem.inria.fr> 2007-05-14 10:52 ` [Caml-list] Polymorphic variant difference David Allsopp @ 2007-05-14 10:52 ` David Allsopp 2007-05-14 13:46 ` Sébastien Hinderer 1 sibling, 1 reply; 7+ messages in thread From: David Allsopp @ 2007-05-14 10:52 UTC (permalink / raw) To: caml-list > 1. Is it necessary to compile/install the lib;a and lib.so files ? Yes they are both needed: lib.so is the bytecode stub library --- so needed for any .cmo file that uses the C module lib.a is the native stub library --- linked in when you compile with ocamlopt for the same purpose > 2. How can these files be produced (in addition to the .cma and .cmxa > files) on a system where ocamlfindlib is installed ? I'm a Windows native port user so detest Makefiles that use ocamlmklib because the Windows port doesn't include it! The automation provided by ocamlmklib is IMHO pretty simplistic (see #18.10 in the manual). ocamlmklib -verbose will tell you the commands it uses for your project - all the ocamlc/ocamlopt commands can then be prefixed with ocamlfind to import any packages you may need. When doing ocamlfind install, you just need to include lib.so and lib.a with the files (r17.html#OCAMLFIND.INSTALL of the findlib docs explains how stub libraries are dealt with). HTH, David ---------- Here's a snippet from the Makefile I use for doing this under Windows for a library with a lot of stubs (with some omitted variables for clarity). NB I prefer to call gcc directly - but ocamlc can of course call gcc for you. libMSLStdLib.a: $(O_FILES) ar rsc $@ $(O_FILES) dllMSLStdLib.dll: $(O_FILES) gcc -mno-cygwin -mms-bitfields -shared -I $(OCAMLLIB) -L $(OCAMLLIB) -o $@ $(O_FILES) -locamlrun MSLStdLib.cma: $(CMI_FILES) $(CMO_FILES) dllMSLStdLib.dll ocamlfind ocamlc $(PACKAGES) -a -o $@ $(CMO_FILES) -dllib -lMSLStdLib MSLStdLib.cmxa: $(CMI_FILES) $(CMX_FILES) libMSLStdLib.a ocamlfind ocamlopt $(PACKAGES) -a -o $@ $(CMX_FILES) -cclib -lMSLStdLib install: MSLStdLib.cma MSLStdLib.cmxa $(CMI_FILES) $(CMX_FILES) META dllMSLStdLib.dll libMSLStdLib.a ocamlfind install MSLStdLib META MSLStdLib.cma MSLStdLib.cmxa MSLStdLib.a libMSLStdLib.a $(CMI_FILES) $(CMX_FILES) dllMSLStdLib.dll %.o: %.c gcc -O -Wall -Wno-unused -mms-bitfields -mno-cygwin -I $(OCAMLLIB) -c $*.c ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Compiling a library with findlib 2007-05-14 10:52 ` [Caml-list] Compiling a library with findlib David Allsopp @ 2007-05-14 13:46 ` Sébastien Hinderer 0 siblings, 0 replies; 7+ messages in thread From: Sébastien Hinderer @ 2007-05-14 13:46 UTC (permalink / raw) To: caml-list Dear David, dear all, > > 1. Is it necessary to compile/install the lib;a and lib.so files ? > Yes they are both needed: > lib.so is the bytecode stub library --- so needed for any .cmo file that > uses the C module > lib.a is the native stub library --- linked in when you compile with > ocamlopt for the same purpose Okay, thanks a lot for this clarification. > > 2. How can these files be produced (in addition to the .cma and .cmxa > > files) on a system where ocamlfindlib is installed ? > I'm a Windows native port user so detest Makefiles that use ocamlmklib > because the Windows port doesn't include it! Too bad :) > The automation provided by ocamlmklib is IMHO pretty simplistic (see #18.10 > in the manual). ocamlmklib -verbose will tell you the commands it uses for > your project - all the ocamlc/ocamlopt commands can then be prefixed with > ocamlfind to import any packages you may need. To me, the work done by ocamlmklib is not _that_ simplistic. Indeed, the use of -verbose reveals that is calls commands like ar and ranlib, which I'd prefer not tocall directly in the Makefile. Also, I'd rather avoid depending on OCamlMakefile, to try to minimize the dependencies and keep the compilationprocess assimple as possible. > When doing ocamlfind install, you just need to include lib.so and lib.a with > the files (r17.html#OCAMLFIND.INSTALL of the findlib docs explains how stub > libraries are dealt with). I will have a look to this section, thanks ! Cheers, Sébastien. ^ permalink raw reply [flat|nested] 7+ messages in thread
[parent not found: <20070515154127.94D62BC96@yquem.inria.fr>]
* Re: [Caml-list] Polymorphic variant difference... [not found] <20070515154127.94D62BC96@yquem.inria.fr> @ 2007-05-15 17:02 ` David Allsopp 0 siblings, 0 replies; 7+ messages in thread From: David Allsopp @ 2007-05-15 17:02 UTC (permalink / raw) To: caml-list > That's maybe just because of a typo (you wrote `A for `B below) ? > > This seems to works with 3.09: > > # module M : sig type t = [ `A | `B ] end = struct type t' = [`A] type t > = [`B | t' ] end;; > module M : sig type t = [ `A | `B ] end > <blush> my bad! It was indeed a typo... David ^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2007-05-15 17:02 UTC | newest] Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- [not found] <20070514100005.459F6BC70@yquem.inria.fr> 2007-05-14 10:52 ` [Caml-list] Polymorphic variant difference David Allsopp 2007-05-15 4:40 ` Jacques Garrigue 2007-05-15 9:45 ` David Allsopp 2007-05-15 10:21 ` Frédéric van der Plancke 2007-05-14 10:52 ` [Caml-list] Compiling a library with findlib David Allsopp 2007-05-14 13:46 ` Sébastien Hinderer [not found] <20070515154127.94D62BC96@yquem.inria.fr> 2007-05-15 17:02 ` [Caml-list] Polymorphic variant difference David Allsopp
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox