<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE message PUBLIC
  "-//MLarc//DTD MLarc output files//EN"
  "../../mlarc.dtd"[
  <!ATTLIST message
    listname CDATA #REQUIRED
    title CDATA #REQUIRED
  >
]>

  <?xml-stylesheet href="../../mlarc.xsl" type="text/xsl"?>


<message 
  url="2002/07/1b29cfdfd19552b03e846f44303aa30e"
  from="Nicolas FRANCOIS &lt;nicolas.francois@f...&gt;"
  author="Nicolas FRANCOIS"
  date="2002-07-15T00:35:01"
  subject="[Caml-list] About modules again"
  prev="2002/07/9d191dbdc8b05bc3b233f36a11ac6504"
  next="2002/07/6c4a835c1b222da5d3b140bc24cc7dbf"
  next-in-thread="2002/07/47f6ab2d7a669a9060d5725cfc33d0cb"
  prev-thread="2002/07/9d191dbdc8b05bc3b233f36a11ac6504"
  next-thread="2002/07/9c344668384b0077ed1732712963cb19"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="[Caml-list] About modules again">
<msg 
  url="2002/07/1b29cfdfd19552b03e846f44303aa30e"
  from="Nicolas FRANCOIS &lt;nicolas.francois@f...&gt;"
  author="Nicolas FRANCOIS"
  date="2002-07-15T00:35:01"
  subject="[Caml-list] About modules again">
<msg 
  url="2002/07/47f6ab2d7a669a9060d5725cfc33d0cb"
  from="Markus Mottl &lt;markus@o...&gt;"
  author="Markus Mottl"
  date="2002-07-15T08:44:45"
  subject="Re: [Caml-list] About modules again">
<msg 
  url="2002/07/9318d960b2578fae39e524c42bcf2882"
  from="Nicolas FRANCOIS &lt;nicolas.francois@f...&gt;"
  author="Nicolas FRANCOIS"
  date="2002-07-16T09:51:19"
  subject="Re: [Caml-list] About modules again">
</msg>
</msg>
</msg>
</thread>

<contents>
I'd like to be able to define some polymorphic functions in module
signatures. I explain it right now : I have some abstract modules (this is
not my code, it seems to be a pre-project from Foc) :

module type Set =
sig
  type elem
    
  val (==) : elem -&gt; elem -&gt; bool     
      (* equality *)

  val normalize : elem -&gt; elem      
      (* there may be more than one
	 representation of each element in the set,
	 this function may perform some simplification
      *)

  val print : elem -&gt; unit          
      (* printing (using the Format library) *)
  val write : formatter -&gt; elem -&gt; unit    
      (* writing in file (using the Format library) *)
  val parse : char Stream.t -&gt; elem
  val read : in_channel -&gt; elem
      (* reading from file, compatible with write *)
  val write_bin : out_channel -&gt; elem -&gt; unit  
      (* writing as binary value *)
  val read_bin : in_channel -&gt; elem         
      (* reading as binary value *)
end

module type Group =
sig
  include Set

  val zero : elem
    (* zero ! *)
  val (++) : elem -&gt; elem -&gt; elem         
      (* sum *)
  val (--) : elem -&gt; elem -&gt; elem         
      (* subtraction *)
  val opp : elem -&gt; elem
      (* opposite (0 -- x) *)   
end

module type Ring =
sig
  include Group                         
    
  val one : elem                    
    (* one ! *)
  val t_of_int : int -&gt; elem        
      (* the standard mapping from integer *)
  val ( ** ) : elem -&gt; elem -&gt; elem
      (* product *)
  val conjugate : elem -&gt; elem
      (* conjugate value *)
end

So you see module type Ring implement the "methods" of Group. My problem
is : the exponentiation (x -&gt; x^i for a positive integer) can be
accomplished with just the product, without knowledge of what the objects
are.

Next we define some Rings, filling the holes in the definitions :

module Ring_Z = 
  struct
    type elem = int
    type stathm = int
    let zero = 0
    let one = 1
    let t_of_int n = n
    let (++) = (+)
    let (--) = (-)
    let ( ** ) = ( * )
    let ( == ) = ( = )
    let opp x = - x
    let print = print_int
    let write ch = pp_print_int ch 
    let parse = parse_int
    let read ch = parse (Stream.of_channel ch)  
    let write_bin = output_value
    let read_bin = input_value
    let stathm = abs
    let less = (&lt;)
    let (//) = (/)
    let (mod) = (mod)
    let div_mod x y = x / y, x mod y
    let normalize x = x
    let conjugate x = x
  end

(it's an Euclidian Ring, but that's not the point). So you see my problem
: I have to define a concrete method in an abstract module, just not to
have to fill the definition for every new Ring I define.

Is there a way to do this ? Or is this specific to objects ?

\bye

-- 

                   Nicolas FRANCOIS
            http://nicolas.francois.free.fr
 A TRUE Klingon programmer does NOT comment his code
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners

</contents>

</message>

