caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* entiers et reels
@ 1996-01-05  9:05 Fauque UPS
  1996-01-05 18:27 ` Pierre Weis
  0 siblings, 1 reply; 5+ messages in thread
From: Fauque UPS @ 1996-01-05  9:05 UTC (permalink / raw)
  To: caml-list



*** english summary follows ***

une question sur caml-light:

pourquoi les relations < > <= etc.. sont-elles polymorphes et les operations
+ * etc.. ne le sont-elles pas?
caml accepte 5 > 6 et 5.0 > 6 mais refuse 3. + 2.; je comprend l'eventuelle
necessite de separer les types entier et reel mais pourquoi pas aussi pour
< >..


Hubert Fauque

in english:
I don't understand why caml accepts 5<6 and 5. < 6. but not 2+3 and 2.+3.






^ permalink raw reply	[flat|nested] 5+ messages in thread
* Re: entiers et reels
@ 1996-01-05 20:13 quercia
  0 siblings, 0 replies; 5+ messages in thread
From: quercia @ 1996-01-05 20:13 UTC (permalink / raw)
  To: Hubert.Fauque, caml-list


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




^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~1996-01-08 10:56 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-01-05  9:05 entiers et reels Fauque UPS
1996-01-05 18:27 ` Pierre Weis
1996-01-05 21:14   ` Thorsten Ohl
1996-01-08  9:40     ` Xavier Leroy
1996-01-05 20:13 quercia

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).