Browse thread
Re: entiers et reels
- quercia@u...
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: | 1996-01-07 (23:50) |
From: | quercia@u... |
Subject: | Re: entiers et reels |
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 = <poly> #(* 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 = [<poly>; <poly>] #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 = [<poly>; <poly>] #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 = <fun> #(f 1 :int);; - : int = 2 #f 1 2;; Segmentation fault bash$ ----------------------------------------------------------------------------- Michel Quercia Lycee Carnot 16 bd Thiers 21000 Dijon France