From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from weis@localhost) by pauillac.inria.fr (8.6.10/8.6.6) id AAA00392 for caml-redistribution; Mon, 8 Jan 1996 00:50:50 +0100 Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.6.10/8.6.6) with ESMTP id VAA28605 for ; Fri, 5 Jan 1996 21:13:39 +0100 From: quercia@u-bourgogne.fr Received: from margaux.inria.fr (margaux.inria.fr [128.93.8.2]) by concorde.inria.fr (8.7.1/8.7.1) with ESMTP id VAA15506 for ; Fri, 5 Jan 1996 21:13:39 +0100 (MET) Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by margaux.inria.fr (8.6.10/8.6.6) with ESMTP id VAA06386 for ; Fri, 5 Jan 1996 21:13:38 +0100 Received: from satie.u-bourgogne.fr (satie.u-bourgogne.fr [192.134.59.2]) by concorde.inria.fr (8.7.1/8.7.1) with SMTP id VAA15502 for ; Fri, 5 Jan 1996 21:13:37 +0100 (MET) Received: by satie.u-bourgogne.fr (AIX 3.2/UCB 5.64/4.03) id AA74233; Fri, 5 Jan 1996 21:13:51 +0100 Date: Fri, 5 Jan 1996 21:13:51 +0100 Message-Id: <9601052013.AA74233@satie.u-bourgogne.fr> To: Hubert.Fauque@enst-bretagne.fr, caml-list@margaux.inria.fr Subject: Re: entiers et reels Sender: weis En attendant la reponse de l'INRIA, voici une petite recreation sur une addition polymorphe de mon cru : Le principe est par un bidouillage en C de tester le type des arguments d'une somme puis d'effectuer l'operation adequate. While waiting for an answer from INRIA, here's a recreation on a ploymorphic addition : With some C hacking, one can test the type of the arguments of a sum and call the appropriate routine. --------------------- Fichier addition.c ------------------------------------ #include "misc.h" #include "mlvalues.h" #include "alloc.h" value addition(value a, value b) { #define cas(x) (Is_long(x) ? 0 : (Tag_val(x) == Double_tag ? 1 : 2)) switch (cas(a) + 3*cas(b)) { case 0 : return(a+b-1); case 1 : return(copy_double( Double_val(a) + (double) (b >> 1) )); case 3 : return(copy_double( (double) (a >> 1) + Double_val(b) )); case 4 : return(copy_double( Double_val(a) + Double_val(b) )); default: invalid_argument("+"); } } ----------------------- Fichier addition.mli --------------------------------- value prefix + : 'a -> 'b -> 'c = 2 "addition";; ----------------------- Fichier addition.ml --------------------------------- (* rien *) (* empty file *) ----------------------- Commandes de compilation ---------------------------- #!/bin/sh camlc -custom -c addition.mli addition.c addition.ml camlmktop -custom -o camladdition addition.o addition.zo --------------------- Essai au terminal ------------------------------------- ---------------------------- Demo ------------------------------------------- bash$ camllight camladdition > Caml Light version 0.7 ##open "addition";; #(* Sommes homogenes *) print_int(1 + 2);; print_float(1.2 + 3.4);; 3- : unit = () #4.6- : unit = () #(* Sommes heterogenes *) print_float(1 + 2.3);; print_float(1.2 + 3);; 3.3- : unit = () #4.2- : unit = () #(* Somme invalide *) print_float(1 + true);; Uncaught exception: Invalid_argument "+" #(* Pb : Caml n'est plus capable de typer le resultat d'une somme *) 2 + 3;; - : '_a = #(* mais on peut l'aider *) (2 + 3 : int);; (4.5 + 6.7 : float);; - : int = 5 #- : float = 11.2 #(* peut-on mentir ? *) (2 + 3 : float);; (4.5 + 6.7 : int);; Segmentation fault bash$ camllight camladdition > Caml Light version 0.7 ##open "addition";; #(* plus subtil *) let liste = [1 + 2; 3.14 + 4.57];; liste : '_a list = [; ] #print_int(hd liste);; 3- : unit = () #print_float(hd(tl liste));; Toplevel input: >print_float(hd(tl liste));; > ^^^^^^^^^^^^ This expression has type int, but is used with type float. #let liste = [1 + 2; 3.14 + 4.57];; liste : '_a list = [; ] #print_float(hd(tl liste));; 7.71- : unit = () #print_int(hd liste);; Toplevel input: >print_int(hd liste);; > ^^^^^^^^ This expression has type float, but is used with type int. #(* un essai idiot *) let f x = x+1;; f : 'a -> 'b = #(f 1 :int);; - : int = 2 #f 1 2;; Segmentation fault bash$ ----------------------------------------------------------------------------- Michel Quercia Lycee Carnot 16 bd Thiers 21000 Dijon France