caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Christophe Raffalli <raffalli@univ-savoie.fr>
To: Lukasz Stafiniak <lukstafi@gmail.com>
Cc: "Michael D. Adams" <mdmkolbe@gmail.com>, caml-list@inria.fr
Subject: Re: [Caml-list] Efficency of varient types
Date: Mon, 28 Nov 2005 09:14:04 +0100	[thread overview]
Message-ID: <438ABC4C.5060103@univ-savoie.fr> (raw)
In-Reply-To: <4a708d20511270657j2518f0b6m@mail.gmail.com>

Lukasz Stafiniak a écrit :

>2005/11/26, Michael D. Adams <mdmkolbe@gmail.com>:
>  
>
>>I have recently learned about OCaml and have been impressed by how
>>fast it is in the benchmarks.  However I have discovered that variant
>>types can slow down a program quite a bit.
>>    
>>
>[...]
>  
>
>>I am working on a program that translates code from scheme into ocaml.
>> (Later I will try python into ocaml.)  Because it is a dynamicly
>>typed language, the most natural translation would make everything a
>>function of a large variant type like:
>>
>>type value = Int of int
>> | Char of char
>> | String of string
>> (* etc. *)
>>
>>    
>>

I would suggest that you run a typing algorithm on your scheme program 
with an extra type "top" for
polymorphic function where object are boxed as proposed above.

Then, when typing fails, you insert in your code the proper conversion 
which are defined by induction over types:


(from int to top) : fun x -> Int x
(from char to top) : fun x -> Char x
...

(from top to int) : function Int x -> x | _ -> failwith "bad scheme program"
...

(from a to a) : identity (needed for optimization bellow)

(from a -> b to a' -> b') : fun f x -> (from b to b') f ((from a' to a) x)

(from a list to b list) : fun l -> List.map (from a to b) l

The pb here is to try to avoid as much as possible the composed function 
(the two latest, especially the latest, the one for
function is not that bad, it does not allocate a lot of memory at 
runtime) ... a possible way to do this is the following:

in the typing algorithm, do not solve the unification constraint 
immediately, just collect them as triple

(a,b,p) where a and b are the type that should be equal and p is the 
position in the code where to insert the (from to ...) function.

Then, consider the smallest relation on type variable with a < b if 
something like (b, list a, p) or (list a, b) appear (idem for arrow and
type constructor ...

there may be cycle in this relation, but do your best to propagate 
unification constraint from the "maximum" type for this relation ...
the solution being not unique, this is not a trivial task.

Moreover, if you compile a library, you may get a very nice solution for 
the library ... that require a lot of translation when you
finally use the library. But then, the only solution is probably to ask 
a little help from the programmer ...

This is the hardness of this problem that makes statically typed 
language better than dynamically typed language.

For lisp, there is one more specific pb, you use "cons" both for pairs 
and lists so for each "cons" there is a choice to translate it as (,) or 
(::)
... exploring all the possibility will probably increase a lot the 
complexity of the algorithm... but feaseable solution may be possible by 
trying
to delay the problem again chosing the order in which you solve 
unification constraint (you try to make "pertinent" information 
propagate before
doing compromising choices) ...

But I am sure this is an active field of reasearch in the lisp compiler 
community ...

I hope this helps.

Christophe


  parent reply	other threads:[~2005-11-28  8:13 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-11-25 23:53 Michael D. Adams
2005-11-26  0:31 ` [Caml-list] " Nicolas Cannasse
2005-11-26  1:22   ` Brian Hurt
2005-11-26  9:39     ` Nicolas Cannasse
2005-11-28  0:17     ` Obj or not Obj Christophe Raffalli
2005-11-28  8:41       ` [Caml-list] " Nicolas Cannasse
2005-11-28  9:27         ` Christophe Raffalli
2005-11-28  9:33         ` skaller
2005-11-28  8:43       ` Alain Frisch
2005-11-26  2:54   ` [Caml-list] Efficency of varient types skaller
2005-11-27  5:06   ` Michael D. Adams
2005-11-27  5:45     ` Brian Hurt
2005-11-27 10:02       ` Nicolas Cannasse
2005-11-27 15:35       ` Michael D. Adams
2005-11-27 18:08         ` Brian Hurt
2005-12-02 15:07           ` Michael D. Adams
2005-11-26  1:18 ` Jon Harrop
2005-11-27 14:57 ` Lukasz Stafiniak
2005-11-27 15:47   ` Lukasz Stafiniak
2005-11-28  8:14   ` Christophe Raffalli [this message]
2005-11-28  7:24 ` David Baelde
2005-11-28  7:49   ` Jacques Garrigue
2005-11-28 10:01     ` Jon Harrop
2005-11-28 10:26       ` Luc Maranget
2005-11-28  7:53   ` Ville-Pertti Keinonen
2005-12-01 17:05 ` Stefan Monnier
2005-12-02 15:07   ` [Caml-list] " Michael D. Adams

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=438ABC4C.5060103@univ-savoie.fr \
    --to=raffalli@univ-savoie.fr \
    --cc=caml-list@inria.fr \
    --cc=lukstafi@gmail.com \
    --cc=mdmkolbe@gmail.com \
    /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;
as well as URLs for NNTP newsgroup(s).