From: quercia@u-bourgogne.fr
To: Hubert.Fauque@enst-bretagne.fr, caml-list@margaux.inria.fr
Subject: Re: entiers et reels
Date: Fri, 5 Jan 1996 21:13:51 +0100 [thread overview]
Message-ID: <9601052013.AA74233@satie.u-bourgogne.fr> (raw)
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
next reply other threads:[~1996-01-07 23:50 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
1996-01-05 20:13 quercia [this message]
-- strict thread matches above, loose matches on Subject: below --
1996-01-05 9:05 Fauque UPS
1996-01-05 18:27 ` Pierre Weis
1996-01-05 21:14 ` Thorsten Ohl
1996-01-08 9:40 ` Xavier Leroy
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=9601052013.AA74233@satie.u-bourgogne.fr \
--to=quercia@u-bourgogne.fr \
--cc=Hubert.Fauque@enst-bretagne.fr \
--cc=caml-list@margaux.inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox