From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: * X-Spam-Status: No, score=1.4 required=5.0 tests=SPF_NEUTRAL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id E41F9BC6B for ; Thu, 13 Dec 2007 18:27:52 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAKr6YEdA6ba9i2dsb2JhbACPeQEBAQgEBCQFmiw X-IronPort-AV: E=Sophos;i="4.24,162,1196636400"; d="scan'208";a="20289747" Received: from nf-out-0910.google.com ([64.233.182.189]) by mail4-smtp-sop.national.inria.fr with ESMTP; 13 Dec 2007 18:27:52 +0100 Received: by nf-out-0910.google.com with SMTP id e27so610513nfd.13 for ; Thu, 13 Dec 2007 09:27:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to:subject:mime-version:content-type:content-transfer-encoding:content-disposition; bh=j3WeUwpDdplFnxQ63Dwbcn5xzWL6XZMa45RGBY8LpYM=; b=K5IXq3ggnjsBoov7QQp56VGMdbw/7fsYCobz0GMQIEJaoRZ8ZaH6s5AcyOe19rgF5tQDk83W2M/hVYDKV8MByajfQBf8WICiOuw+rPEAFJGiKN4SqLQv+OD95EbkUFfN9GKhgekwIIr5JjE0HZQj06G5kdav8Sa1CYVt13cSaYQ= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:mime-version:content-type:content-transfer-encoding:content-disposition; b=k18FSKH8cGftJQ/58z4IfHfgdUOB07AIWu9aC0PeTxANwFzm5oaowd7tnHwr/oPrF7gHsGBpXqVVGqF0ZsWtKOyP4NWb7z4Ny58rGqyqMEevNF7zlvSGWvC6Xm/EOOlwKaPj0rq64ipIjRka4GMbhAt85b4HHSkWKgkr3BAsSdU= Received: by 10.82.186.5 with SMTP id j5mr5953936buf.16.1197566871076; Thu, 13 Dec 2007 09:27:51 -0800 (PST) Received: by 10.82.154.16 with HTTP; Thu, 13 Dec 2007 09:27:51 -0800 (PST) Message-ID: <6cb897b30712130927m7df88e3axec56eacba4d47fdf@mail.gmail.com> Date: Thu, 13 Dec 2007 18:27:51 +0100 From: "Pierre-Evariste Dagand" To: caml-list@yquem.inria.fr Subject: "Ref" and copy of functions MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline X-Spam: no; 0.00; advices:01 combinator:01 val:01 val:01 combinator:01 combinators:01 closures:01 wrote:01 caml-list:01 functions:01 marshal:01 marshal:01 ens-cachan:01 cps:02 variables:02 Hi Caml-list, I'm looking for advices about a "clean way" of doing something which, by design, isn't. So, let states the problem. I'm doing a combinator library based on Arrows for a Yampa-like system : type ('a,'b) arrow = Arrow of ( 'a -> 'b ) Thus, I can instantiate an arrow with : let arr f = Arrow ( f ) val arr : ('a -> 'b) -> ('a, 'b) arrow Then, I have, for example, the composition of arrows : let (>>>) (Arrow f) (Arrow g) =Arrow ( fun c -> g ( f c ) ) val ( >>> ) : ('a, 'b) arrow -> ('b, 'c) arrow -> ('a, 'c) arrow Right here, everything plays well (excepted some weak type variables issues but that's not a big deal). But (and that's here that the trouble is) I need a "loop" combinator, that I wrote this way : let loop init (Arrow f) = let state = ref init in Arrow ( fun c -> let new_state , output = f ( !state , c ) in state := new_state; output ) val loop : 'a -> ('a * 'b, 'a * 'c) arrow -> ('b, 'c) arrow Finally, I can write a small arrow : let arr_counter = loop 0 ( arr ( fun ( counter , x ) -> counter+1 , counter+x ) ) let arr_double = arr ( fun x -> 2*x ) let arr_my_small_arrow = arr_counter >>> arr_double And I execute it with : let run (Arrow f) input = f input val run : ('a, 'b) arrow -> 'a -> 'b And it works. Right. But now, I would like to be able to "copy" a built arrow and to be able to execute the copy without side-effecting on the first one. Obviously, I cannot do that in this implementation. My current solution is really *ugly* : let arrow_string = Marshal.to_string arrow [ Marshal.No_sharing ; Marshal.Closures ] Then I "Marshal.from_string arrow_string". I'm not even sure if it will not blow out of my hands with "strange" things in the Ref cell. I'm aware of implementations in CPS but I use to think that it has a performance cost that I'm not ready to pay (I'm fighting against a C++ code so...). So if someone knows an elegant solution to my problem (if someone has read this mail until here), please let me know :-) Best regards, P.S.: For those who are just curious, these combinators are used in a functional-reactive library for Peer-to-Peer overlays programming. -- Pierre-Evariste DAGAND http://perso.eleves.bretagne.ens-cachan.fr/~dagand/