Re: [Caml-list] when OO is wrong

2009-03-31 Thread Oliver Bandel

Quoting Jon Harrop j...@ffconsultancy.com:


On Monday 30 March 2009 23:38:45 Ed Keith wrote:

I do wish I had better tools for visualizing FP designs. I use
Nassi-Schneiderman diagram for procedural designs and UML of OO designs.


That's an interesting idea. Someone must have worked on this?

[...]

In the book Haskell: The Craft of Functional Programming
at some pages are graphical notations, at least explanatory,
for some things.


A paper which you also might find interesting:

To Dissect a Mockingbird:
A Graphical Notation for the Lambda Calculus with Animated Reduction

  http://users.bigpond.net.au/d.keenan/Lambda/index.htm


Ciao,
   Oliver

___
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs


Re: [Caml-list] integer regular expressions

2009-03-31 Thread Florent Ouchet

Martin Jambon a écrit :

You want something called views or active patterns.
You can do that in OCaml with mikmatch, which includes such syntax extension.

See http://martin.jambon.free.fr/mikmatch-manual.html#htoc10

Here is your example:

let view Mod30 = fun x - x mod 30 = 0
(*
   but not:
 let view Mod m = fun x - x mod m = 0

   but it could be implemented without difficulty (if really needed).
*)


let test x =
  match x with
 %Mod30 - ...
   | ... -


Of course the whole point is to use %Mod30 within arbitrary patterns,
otherwise it wouldn't be useful.

I you want to match regular expressions over anything else than bytes, there's
nothing out-of-the-box. You can define views on lists that would consume any
number of elements, but it is pretty limited.
  


Hello Martin,

That's an elegant way for writing custom pattern matches in OCaml code 
but that is not exactly what I am looking for. Your proposal fits well 
for fixed pattern matches while I would like to make the regular 
expressions customizable via the application command line (ie at 
run-time). A module dedicated to integer matches (like RegExp is to 
string matches) would fit better.


Regards,

Florent
VSYML - VHDL Symbolic Simulator in OCaml: 
http://users-tima.imag.fr/vds/ouchet/vsyml.html


begin:vcard
fn:Florent Ouchet
n:Ouchet;Florent
org:;CIS/VDS
adr;quoted-printable:46, avenue F=C3=A9lix Viallet,;;TIMA Laboratory - T131;GRENOBLE Cedex;;38031;France
email;internet:florent.ouc...@imag.fr
title:PhD student
tel;work:0476574872
x-mozilla-html:FALSE
url:http://tima.imag.fr
version:2.1
end:vcard

___
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs


Re: [Caml-list] when OO is wrong

2009-03-31 Thread Ed Keith

--- On Tue, 3/31/09, Oliver Bandel oli...@first.in-berlin.de wrote:
 Quoting Jon Harrop j...@ffconsultancy.com:

  On Monday 30 March 2009 23:38:45 Ed Keith wrote:
  I do wish I had better tools for visualizing FP designs. I use
  Nassi-Schneiderman diagram for procedural designs and UML of OO designs.
  
  That's an interesting idea. Someone must have worked on this?
[...]


 A paper which you also might find interesting:

 To Dissect a Mockingbird:
 A Graphical Notation for the Lambda Calculus with Animated Reduction
 
  http://users.bigpond.net.au/d.keenan/Lambda/index.htm

The notation is very useful for teaching, but I can not imagine using it to 
design a complex system.

   -EdK

Ed Keith
e_...@yahoo.com

Blog: edkeith.blogspot.com






___
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs


Re: [Caml-list] questions

2009-03-31 Thread Kuba Ober


the following are the most visible ocaml tutorials, but are low  
quality, blog like, full of misleading characterizations,  
irrevelancies, misleading comparisons. The type that you'd spend  
hours on and got more confused, regardless whether you are a expert  
logician or expert industrial programer. These tutorial's quality  
and nature are similar to the ones you'd find of the freely bundled  
official tutorials from perl, java, or even haskell. Typically  
written as a revised diary of learning experiences by student  
programers, or by academicians who are llliterate in technical  
writing.


• intro to ocaml, from official site
http://caml.inria.fr/pub/docs/manual-ocaml/manual003.html

• “Objective CAML Tutorial”, most cited tutorial on the web
http://www.ocaml-tutorial.org/

There are 3 or so more ocaml tutorials i've looked on the web, from  
the first page of google search with word “ocaml tutorial”. I don't  
think they are not worth your time.


-

I'd be good if the ocaml managers perhaps thru some arrangement, to  
borrow Jon Harrop's chapter 1, or other quality sources, in  
replacement of the tutorial on the official site. Because, a quailty  
tutorial bundled with the official release has great impact. The  
official tutorial makes the first impression of the lang for most  
people.


  Xah


There must be some reason why the manual and other materials on the  
official site are of such poor quality. I've thought a bit about it,  
and the only reason I see is that the authors do not have a feel for  
what it takes to learn/understand/use that language. They obviously  
know it all through, but that's still far removed from being able to  
explain it to someone else. I don't know, of course, how it is that  
one understands something well yet is not able to explain it to  
somebody else. To me, that's very fragile knowledge. I always thought  
that deep understanding implies an ability to extract what's  
important, and to lead the other person from some basics (whatever  
they may be) to the conclusion. Some experience in imperative  
languages can be perhaps expected of the OCaml beginners. But the  
manual, the official tutorial, and even ocaml-tutorial, fall short of  
being really useful - for me. Personally, I found them next to  
useless, but that perhaps has to do with my own shortcomings.


Books that lag behind the current release's features are not all that  
great either -- you find a book that's a good match to your needs, and  
then, after a while, find that you miss on a lot of good stuff that's  
not mentioned in the book. I have two examples of such books: Jon's  
book, and Marcelo DiPierro's web2py book. Both are very good books  
because the authors have a feel for what it takes to understand what  
they talk about. Yet both miss out on some newer features of OCaml and  
web2py, respectively -- features that would be best explained by the  
very same authors!


Cheers, Kuba
___
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs


Re: [Caml-list] questions

2009-03-31 Thread Martin Jambon
Kuba Ober wrote:
 There must be some reason why the manual and other materials on the
 official site are of such poor quality. I've thought a bit about it, and
 the only reason I see is that the authors do not have a feel for what it
 takes to learn/understand/use that language. They obviously know it all
 through, but that's still far removed from being able to explain it to
 someone else. I don't know, of course, how it is that one understands
 something well yet is not able to explain it to somebody else. To me,
 that's very fragile knowledge. I always thought that deep understanding
 implies an ability to extract what's important, and to lead the other
 person from some basics (whatever they may be) to the conclusion.

I can see one reason: like many other French OCaml programmers, I learned
OCaml at school (it was in 1998). French teachers don't rely heavily on a
book. There is however one book that covers the essentials, Le Langage Caml
by Weis and Leroy, which despite using the Caml Light dialect is the most
enlightening programming book I've ever got to read. For the rest, there is
the reference manual of OCaml and plenty of source code all around the web.

I think that's why there is not much more incentive to write a complete
replace-the-teacher text book on OCaml written by the core OCaml developers,
who are mostly a French team. Besides, it's a lot of work and doesn't make 
money.

Of course there are now a few great books and tutorials on OCaml in English,
none of them having an official status.


Martin

-- 
http://mjambon.com/

___
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs


[Caml-list] Dynamic loading of native code : what about librairies and packs ?

2009-03-31 Thread Pierre-Loïc Garoche
I am discovering this feature of loading dynamically native code. The
Frama-C framework (www.frama-c.cea.fr) provides now a plugin
architecture that allows you to develop your own plugin and loads it
directly in the framework.

I targeted to adapt a simple tool I developped as a Frama-C plugin and
faced some difficulties. I did not found enough information about the
compilation of cmxs files.

** First: what about external libraries ?
Could you detail the behavior of the linking process of cmxs file with
respect to dependancies.

If I understand well any library used to compile cmx files should be
- either know by the software dynamically loading the plugin,
- or its cmxa should be passed as argument of ocamlopt -shared command.

But what is the difference between:
   ocamlopt.opt -shared  mylib.cmxa xxx.cmx yyy.cmx -o target.cmxs
and
   ocamlopt.opt -shared -linkall mylib.cmxa xxx.cmx yyy.cmx -o target.cmxs

Both seems to work similarly on my example (no differences between
resulting files).


** Second: how about packs ?
Does the option -shared -o target.cmxs is similar to -pack -o
target.cmx, without the dynamic loading thing ? And what if the plugin
components (cmx files) are or are not compiled with the -for-pack option ?


Any information would be greatly appreciated.

Best regards,

pl
-- 
Pierre-Loïc Garoche
pierre-loic.garo...@onera.fr
http://www.onera.fr/staff/pierre-loic-garoche/



signature.asc
Description: OpenPGP digital signature
___
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs


Re: [Caml-list] questions

2009-03-31 Thread Xavier Leroy
 There must be some reason why the manual and other materials on the
 official site are of such poor quality. I've thought a bit about it, and
 the only reason I see is that the authors do not have a feel for what it
 takes to learn/understand/use that language. They obviously know it all
 through, but that's still far removed from being able to explain it to
 someone else. I don't know, of course, how it is that one understands
 something well yet is not able to explain it to somebody else. To me,
 that's very fragile knowledge.

Because we are autistic morons who lack your rock-solid knowledge, if
I properly catch your (rather insulting) drift?

At the very least, you're confusing to be able with to intend to.
The tutorial part of the OCaml reference manual was a quick job
targeted at readers who already know functional programming and just
want a quick overview of what's standard and what's different in
OCaml.  Maybe that shouldn't be titled tutorial at all.

Teaching functional programming in OCaml to beginners is a rather
different job, for which they are plenty of good books already.  Most
of them happen to be in French for various reasons: O'Reilly's refusal
to publish the English translation of the Chailloux-Manoury-Pagano
book; the Hickey-Rentsch controversy, etc.  But, yes, some talented
teachers invested huge amounts of time in writing good intro to Caml
programming books.  Don't brush their efforts aside.

One last word to you, that Xah Lee troll, and anyone else on this
list: if you're not happy with the existing material, write something
better.  Everyone will thank you and you'll get to better appreciate
the difficulty of the task.

- Xavier Leroy

___
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs


Re: [Caml-list] Dynamic loading of native code : what about librairies and packs ?

2009-03-31 Thread Alain Frisch

Pierre-Loïc Garoche wrote:

** First: what about external libraries ?
Could you detail the behavior of the linking process of cmxs file with
respect to dependancies.

If I understand well any library used to compile cmx files should be
- either know by the software dynamically loading the plugin,
- or its cmxa should be passed as argument of ocamlopt -shared command.

But what is the difference between:
   ocamlopt.opt -shared  mylib.cmxa xxx.cmx yyy.cmx -o target.cmxs
and
   ocamlopt.opt -shared -linkall mylib.cmxa xxx.cmx yyy.cmx -o target.cmxs

Both seems to work similarly on my example (no differences between
resulting files).


The -linkall forces all the modules in the included libraries (here, 
mylib.cmxa) to be included in the .cmxs file, even if they are not 
explicitly used by other linked modules. This option is generally 
useless when linking cmxs files, unless the .cmxs file is loaded in 
non-private mode, and you want subsequently loaded .cmxs files to be 
able to access any module in the libraries. (Typically, if you turn 
mylib.cmxa into a mylib.cmxs, without any other module, you want -linkall.)




** Second: how about packs ?


As far as I can tell, packing and creating cmxs files are orthogonal 
features: they don't overlap and they don't have bad interactions. It is 
ok to put in cmxs files a module produced by -pack. I think it is also 
ok to put modules compiled with -for-pack, but I don't see why you would 
do that.




Regards,


Alain

___
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs


[Caml-list] ANNOUNCE: ocaml-autoconf macros 1.0 released

2009-03-31 Thread Richard Jones
We've just released the first version of the 'official' OCaml autoconf
macros.  Now there is a central place to collect autoconf macros
related to detecting OCaml, OCaml tools and OCaml libraries.
Everything has been relicensed under a simple 3-clause BSD license (or
rewritten).

Home: https://forge.ocamlcore.org/projects/ocaml-autoconf/
Download: https://forge.ocamlcore.org/frs/?group_id=69
Git repo: 
http://git.ocamlcore.org/cgi-bin/gitweb.cgi?p=ocaml-autoconf/ocaml-autoconf.git;a=summary

This version was made possible by ...

* Olivier Andrieu
* Jean-Christophe Fillia^tre
* Richard W.M. Jones
* Georges Mariano
* Jim Meyering
* Stefano Zacchiroli
* OCamlForge, Debian  Red Hat

Richard Jones  Stefano Zacchiroli

-- 
Richard Jones
Red Hat

___
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs


Re: [Caml-list] Dynamic loading of native code : what about librairies and packs ?

2009-03-31 Thread Alain Frisch

Pierre-Loïc Garoche wrote:

Do you suggest to create a first cmx pack and then make it dynamically
loadable ?


Yes, absolutely.

-- Alain

___
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs


[Caml-list] OCaml-based logic and theorem proving book available

2009-03-31 Thread Harrison, John R
I'm pleased to announce the availability of my textbook on logic and
automated theorem proving, in which all the major techniques that are
described are also implemented as concrete OCaml code:

 Handbook of Practical Logic and Automated Reasoning

John Harrison

   Cambridge University Press 2009

 ISBN: 9780521899574

 Publisher's Web page: http://www.cambridge.org/9780521899574
 Code and resources:   http://www.cl.cam.ac.uk/~jrh13/atp/

You can already buy the book in Europe, and it should be available
elsewhere very soon, if it isn't already. Here's a table of
contents:

1 Introduction
1.1  What is logical reasoning?
1.2  Calculemus!
1.3  Symbolism
1.4  Boole's algebra of logic
1.5  Syntax and semantics
1.6  Symbolic computation and OCaml
1.7  Parsing
1.8  Prettyprinting

2 Propositional Logic
2.1  The syntax of propositional logic
2.2  The semantics of propositional logic
2.3  Validity, satisfiability and tautology
2.4  The De Morgan laws, adequacy and duality
2.5  Simplification and negation normal form
2.6  Disjunctive and conjunctive normal forms
2.7  Applications of propositional logic
2.8  Definitional CNF
2.9  The Davis-Putnam procedure
2.10 Staalmarck's method
2.11 Binary Decision Diagrams
2.12 Compactness

3 First-order logic
3.1  First-order logic and its implementation
3.2  Parsing and printing
3.3  The semantics of first-order logic
3.4  Syntax operations
3.5  Prenex normal form
3.6  Skolemization
3.7  Canonical models
3.8  Mechanizing Herbrand's theorem
3.9  Unification
3.10 Tableaux
3.11 Resolution
3.12 Subsumption and replacement
3.13 Refinements of resolution
3.14 Horn clauses and Prolog
3.15 Model elimination
3.16 More first-order metatheorems

4 Equality
4.1  Equality axioms
4.2  Categoricity and elementary equivalence
4.3  Equational logic and completeness theorems
4.4  Congruence closure
4.5  Rewriting
4.6  Termination orderings
4.7  Knuth-Bendix completion
4.8  Equality elimination
4.9  Paramodulation

5 Decidable problems
5.1  The decision problem
5.2  The AE fragment
5.3  Miniscoping and the monadic fragment
5.4  Syllogisms
5.5  The finite model property
5.6  Quantifier elimination
5.7  Presburger arithmetic
5.8  The complex numbers
5.9  The real numbers
5.10 Rings, ideals and word problems
5.11 Groebner bases
5.12 Geometric theorem proving
5.13 Combining decision procedures

6 Interactive theorem proving
6.1  Human-oriented methods
6.2  Interactive provers and proof checkers
6.3  Proof systems for first-order logic
6.4  LCF implementation of first-order logic
6.5  Propositional derived rules
6.6  Proving tautologies by inference
6.7  First-order derived rules
6.8  First-order proof by inference
6.9  Interactive proof styles

7 Limitations
7.1  Hilbert's programme
7.2  Tarski's theorem on the undefinability of truth
7.3  Incompleteness of axiom systems
7.4  Goedel's incompleteness theorem
7.5  Definability and decidability
7.6  Church's theorem
7.7  Further limitative results
7.8  Retrospective: the nature of logic

John.

___
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs


[Caml-list] Bug? Constraints get ignored in methods

2009-03-31 Thread Goswin von Brederlow
Hi,

I want to keep a linked list of structures that have a common subset
of functionality. I thought this would be a good use of ocaml objects.
A base class with the common subset of functionality and methods to
link them. And then derived classes for the specific types. Most
simplified it looks like this:

# class type base_type = object val mutable next : base_type option method 
set_next : base_type option - unit end;;
class type base_type =
  object
val mutable next : base_type option
method set_next : base_type option - unit
  end

# class base : base_type = object val mutable next = None method set_next n = 
next - n end;;
class base : base_type

# class foo = object inherit base method foo = () end;;
class foo :
  object
val mutable next : base_type option
method foo : unit
method set_next : base_type option - unit
  end

# let a = new base in
let b = new foo in
  a#set_next (Some (b : base_type));;
- : unit = ()

# let a = new base in
let b = new foo in
  a#set_next (Some b);;
   ^
Error: This expression has type foo but is here used with type base_type
   The second object type has no method foo

This last error isn't nice. I don't want to have to cast the objects
all the time. So I thought there must be a better way using
polymorphic methods with a constraint. But here is where everything
breaks down. First lets look at just the set_next method:

# class type virtual vbase_type = object method virtual set_next : 'a. 'a 
option - unit constraint 'a = #vbase_type end;;
class type virtual vbase_type =
  object method virtual set_next : 'a option - unit end

# class virtual vbase : vbase_type = object method virtual set_next : 'a. 'a 
option - unit constraint 'a = #vbase_type end;;
class virtual vbase : vbase_type

# class base = object inherit vbase method set_next _ = () end;;
class base : object method set_next : 'a option - unit end

# let b = new base;;
val b : base = obj

# b#set_next (Some 1);;
- : unit = ()

Huh? That should not work. 1 is not a superset of #vbase_type. The
constraint gets completly ignored by ocaml. Adding back the next gives
further problems:

# class type virtual vbase_type = object val mutable next : #vbase_type option 
method virtual set_next : 'a. 'a option - unit constraint 'a = #vbase_type 
end;;
class type virtual vbase_type =
  object
val mutable next : #vbase_type option
method virtual set_next : 'a option - unit
  end

# class virtual vbase : vbase_type = object val mutable next = None method 
virtual set_next : 'a. 'a option - unit constraint 'a = #vbase_type end;;
class virtual vbase : vbase_type

# class base = object inherit vbase
method set_next n = next - (n : vbase_type option) end;;

Error: This method has type #vbase_type option - unit
   which is less general than 'a. 'a option - unit

Again I  blame ocaml for dropping the constraint. Given the constraint
the type would be correct.



So how do I have to specify the set_next method that any superset of
#base_type will be accepted as argument? Or is that a bug in ocaml and
my syntax is perfectly fine?

MfG
Goswin

___
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs


Re: [Caml-list] Bug? Constraints get ignored in methods

2009-03-31 Thread Martin Jambon
Goswin von Brederlow wrote:
 Hi,
 
 I want to keep a linked list of structures that have a common subset
 of functionality. I thought this would be a good use of ocaml objects.

It is not a good use of objects. You'll notice this pretty soon as you'll run
into a variety of problems:

- polymorphism
- initialization
- verbosity
- performance

All of these issues are inexistent if you use records instead of objects for
the list structure (or just a classic list).

You can still use objects as elements of the list, but the elements would have
to share the base type, as you know.


(continued below)

 A base class with the common subset of functionality and methods to
 link them. And then derived classes for the specific types. Most
 simplified it looks like this:
 
 # class type base_type = object val mutable next : base_type option method 
 set_next : base_type option - unit end;;
 class type base_type =
   object
 val mutable next : base_type option
 method set_next : base_type option - unit
   end
 
 # class base : base_type = object val mutable next = None method set_next n = 
 next - n end;;
 class base : base_type
 
 # class foo = object inherit base method foo = () end;;
 class foo :
   object
 val mutable next : base_type option
 method foo : unit
 method set_next : base_type option - unit
   end
 
 # let a = new base in
 let b = new foo in
   a#set_next (Some (b : base_type));;
 - : unit = ()
 
 # let a = new base in
 let b = new foo in
   a#set_next (Some b);;
^
 Error: This expression has type foo but is here used with type base_type
The second object type has no method foo
 
 This last error isn't nice. I don't want to have to cast the objects
 all the time. So I thought there must be a better way using
 polymorphic methods with a constraint. But here is where everything
 breaks down. First lets look at just the set_next method:
 
 # class type virtual vbase_type = object method virtual set_next : 'a. 'a 
 option - unit constraint 'a = #vbase_type end;;
 class type virtual vbase_type =
   object method virtual set_next : 'a option - unit end
 
 # class virtual vbase : vbase_type = object method virtual set_next : 'a. 'a 
 option - unit constraint 'a = #vbase_type end;;
 class virtual vbase : vbase_type
 
 # class base = object inherit vbase method set_next _ = () end;;
 class base : object method set_next : 'a option - unit end
 
 # let b = new base;;
 val b : base = obj
 
 # b#set_next (Some 1);;
 - : unit = ()
 
 Huh? That should not work. 1 is not a superset of #vbase_type. The
 constraint gets completly ignored by ocaml. Adding back the next gives
 further problems:
 
 # class type virtual vbase_type = object val mutable next : #vbase_type 
 option method virtual set_next : 'a. 'a option - unit constraint 'a = 
 #vbase_type end;;
 class type virtual vbase_type =
   object
 val mutable next : #vbase_type option
 method virtual set_next : 'a option - unit
   end
 
 # class virtual vbase : vbase_type = object val mutable next = None method 
 virtual set_next : 'a. 'a option - unit constraint 'a = #vbase_type end;;
 class virtual vbase : vbase_type
 
 # class base = object inherit vbase
 method set_next n = next - (n : vbase_type option) end;;
 
 Error: This method has type #vbase_type option - unit
which is less general than 'a. 'a option - unit
 
 Again I  blame ocaml for dropping the constraint. Given the constraint
 the type would be correct.
 
 
 
 So how do I have to specify the set_next method that any superset of
 #base_type will be accepted as argument? Or is that a bug in ocaml and
 my syntax is perfectly fine?


I have no idea. It looks way too complicated.
Use a classic list:


class base = ...
class derived = ... (* inherits base *)

type obj = Base of base | Derived of derived

let obj_list = [ Base (new base); Derived (new derived); ... ]

let iter_base f l =
  List.iter (function Base x - f x | Derived x - f (x : base)) l

let iter_derived f l =
  List.iter (function Derived x - f x | Base _ - ()) l

...




Martin

-- 
http://mjambon.com/

___
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs


Re: [Caml-list] Bug? Constraints get ignored in methods

2009-03-31 Thread Martin Jambon
Martin Jambon wrote:
 Use a classic list:
 
 
 class base = ...
 class derived = ... (* inherits base *)
 
 type obj = Base of base | Derived of derived
 
 let obj_list = [ Base (new base); Derived (new derived); ... ]
 
 let iter_base f l =
   List.iter (function Base x - f x | Derived x - f (x : base)) l
 
 let iter_derived f l =
   List.iter (function Derived x - f x | Base _ - ()) l
 
 ...

Or simply one list per type, each list containing all the elements compatible
with the type.

I would stay away from any solution that requires special methods in order to
solve the polymorphism/heterogeneity problem. Keep the modifications 
non-invasive.


Cheers,


Martin

-- 
http://mjambon.com/

___
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs


Re: [Caml-list] Bug? Constraints get ignored in methods

2009-03-31 Thread Peng Zang
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Hi,

Here's an example of how constraints are specified for polymorphic methods.  
In this example I define a list type which can compare to anything that is 
foldable.

  class type ['a] foldable = object
method foldl : 'z. ('z - 'a - 'z) - 'z - 'z
  end

  class type ['a] mylist = object
inherit ['a] foldable
method compare : 'z. ('a #foldable as 'z) - int
  end

Direct application to your example would not work:

  # class virtual base = object
  method virtual setnext : 'a. (#base as 'a) option - unit
end
  Error: This type scheme cannot quantify 'a : it escapes this scope.
  #

OCaml does not allow the recursive reference when the method is polymorphic.  
One option is to just deal with coercions or a function that does it for you:

  class virtual base = object
method virtual setnext : base option - unit
  end

  let callsetnext (obj:#base) (n:#base option) = 
obj#setnext (n : base option)

Another option is to factor out the basic operations you need like the in list 
example.  I didn't make the list compare method work with other lists, I made 
it more general to work with anything that is foldable.  This avoids the 
recursive reference because foldable is defined ahead of time.

Cheers,

Peng



On Tuesday 31 March 2009 06:05:00 pm Goswin von Brederlow wrote:
 Hi,

 I want to keep a linked list of structures that have a common subset
 of functionality. I thought this would be a good use of ocaml objects.
 A base class with the common subset of functionality and methods to
 link them. And then derived classes for the specific types. Most
 simplified it looks like this:

 # class type base_type = object val mutable next : base_type option method
 set_next : base_type option - unit end;; class type base_type =
   object
 val mutable next : base_type option
 method set_next : base_type option - unit
   end

 # class base : base_type = object val mutable next = None method set_next n
 = next - n end;; class base : base_type

 # class foo = object inherit base method foo = () end;;
 class foo :
   object
 val mutable next : base_type option
 method foo : unit
 method set_next : base_type option - unit
   end

 # let a = new base in
 let b = new foo in
   a#set_next (Some (b : base_type));;
 - : unit = ()

 # let a = new base in
 let b = new foo in
   a#set_next (Some b);;
^
 Error: This expression has type foo but is here used with type base_type
The second object type has no method foo

 This last error isn't nice. I don't want to have to cast the objects
 all the time. So I thought there must be a better way using
 polymorphic methods with a constraint. But here is where everything
 breaks down. First lets look at just the set_next method:

 # class type virtual vbase_type = object method virtual set_next : 'a. 'a
 option - unit constraint 'a = #vbase_type end;; class type virtual
 vbase_type =
   object method virtual set_next : 'a option - unit end

 # class virtual vbase : vbase_type = object method virtual set_next : 'a.
 'a option - unit constraint 'a = #vbase_type end;; class virtual vbase :
 vbase_type

 # class base = object inherit vbase method set_next _ = () end;;
 class base : object method set_next : 'a option - unit end

 # let b = new base;;
 val b : base = obj

 # b#set_next (Some 1);;
 - : unit = ()

 Huh? That should not work. 1 is not a superset of #vbase_type. The
 constraint gets completly ignored by ocaml. Adding back the next gives
 further problems:

 # class type virtual vbase_type = object val mutable next : #vbase_type
 option method virtual set_next : 'a. 'a option - unit constraint 'a =
 #vbase_type end;; class type virtual vbase_type =
   object
 val mutable next : #vbase_type option
 method virtual set_next : 'a option - unit
   end

 # class virtual vbase : vbase_type = object val mutable next = None method
 virtual set_next : 'a. 'a option - unit constraint 'a = #vbase_type end;;
 class virtual vbase : vbase_type

 # class base = object inherit vbase
 method set_next n = next - (n : vbase_type option) end;;
 
 Error: This method has type #vbase_type option - unit
which is less general than 'a. 'a option - unit

 Again I  blame ocaml for dropping the constraint. Given the constraint
 the type would be correct.



 So how do I have to specify the set_next method that any superset of
 #base_type will be accepted as argument? Or is that a bug in ocaml and
 my syntax is perfectly fine?

 MfG
 Goswin

 ___
 Caml-list mailing list. Subscription management:
 http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
 Archives: http://caml.inria.fr
 Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
 Bug reports: http://caml.inria.fr/bin/caml-bugs
-BEGIN PGP SIGNATURE-
Version: GnuPG v2.0.7