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.0 required=5.0 tests=AWL,SPF_FAIL autolearn=disabled version=3.1.3 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id 5EE62BC6B for ; Fri, 14 Dec 2007 12:15:35 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAEb0YUfAXQInh2dsb2JhbACQAAEBAQgKKZhz X-IronPort-AV: E=Sophos;i="4.24,166,1196636400"; d="scan'208";a="5643415" Received: from concorde.inria.fr ([192.93.2.39]) by mail1-smtp-roc.national.inria.fr with ESMTP; 14 Dec 2007 12:15:35 +0100 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id lBEBFYhF026734 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=OK) for ; Fri, 14 Dec 2007 12:15:35 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAEb0YUdQW+UCh2dsb2JhbACQAAEBAQgKKZhz X-IronPort-AV: E=Sophos;i="4.24,166,1196636400"; d="scan'208";a="5643414" Received: from main.gmane.org (HELO ciao.gmane.org) ([80.91.229.2]) by mail1-smtp-roc.national.inria.fr with ESMTP; 14 Dec 2007 12:15:34 +0100 Received: from list by ciao.gmane.org with local (Exim 4.43) id 1J38KM-0000MS-2J for caml-list@inria.fr; Fri, 14 Dec 2007 11:03:34 +0000 Received: from ivr94-8-88-162-26-239.fbx.proxad.net ([88.162.26.239]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 14 Dec 2007 11:03:34 +0000 Received: from li by ivr94-8-88-162-26-239.fbx.proxad.net with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 14 Dec 2007 11:03:34 +0000 X-Injected-Via-Gmane: http://gmane.org/ To: caml-list@inria.fr From: Zheng Li Subject: Re: "Ref" and copy of functions Date: Fri, 14 Dec 2007 11:49:09 +0100 Message-ID: <87d4t9k0uy.fsf@pps.jussieu.fr> References: <6cb897b30712130927m7df88e3axec56eacba4d47fdf@mail.gmail.com> Mime-Version: 1.0 Content-Type: text/plain; format=flowed X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: ivr94-8-88-162-26-239.fbx.proxad.net User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.0.50 (gnu/linux) Cancel-Lock: sha1:+e2fTc93sjkG6xgYS3utTkGRelU= Sender: news X-Miltered: at concorde with ID 476265D6.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; advices:01 combinator:01 val:01 mutable:01 combinators:01 syntax:01 recursion:01 camlp:01 recursive:01 rec:01 rec:01 pps:01 pps:01 functions:01 jussieu:01 Hi, "Pierre-Evariste Dagand" writes: > 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 : I don't understand what "arrow" is and the rational behind it, so don't take my suggestion seriously. > type ('a,'b) arrow = Arrow of ( 'a -> 'b ) > let arr f = Arrow ( f ) > val arr : ('a -> 'b) -> ('a, 'b) arrow > let (>>>) (Arrow f) (Arrow g) =Arrow ( fun c -> g ( f c ) ) > 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 > ) > 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 > let run (Arrow f) input = > f input At least on this particular example, you can use the following encoding (I deliberately use rectypes here, but you can always use extra variant instead) ---definition--- (* Only in mind: type ('a,'b) arrow = 'a -> 'b * ('a, 'b) arrow;; *) let rec arr f x = f x, arr f;; let rec (>>>) f g x = let rf,nf = f x in let rg,ng = g rf in rg, nf >>> ng;; let rec loop init f x = let ((init', r), n) = f (init,x) in r, loop init' n;; let rec run f = function | [] -> [] | h::t -> let r,n = f h in r :: run n t;; ---test--- let arr_counter = loop 0 (arr (fun (c,x) -> c+1,c+x));; let arr_double = arr (( * ) 2);; let arr_my_small_arrow = arr_counter >>> arr_double;; run arr_my_small_arrow [6;5;4;3];; - : int list = [12; 12; 12; 12] Since it doesn't use mutable variable, it's free to make copy. > P.S.: For those who are just curious, these combinators are used in a > functional-reactive library for Peer-to-Peer overlays programming. It looks like "arrow" is for some kind of flow-like programming, then you may also take a look at SDFlow [1]. Your example can be encoded in a point-free style as ---flow--- let arr_counter = comb |- map (fun (x,c) -> x+c,c+1) |- split |> curry |- feedr[<'0>];; let arr_double = map (( * ) 2);; let arr_my_small_arrow = arr_counter |- arr_double;; arr_my_small_arrow [<'6;'5;'4;'3>] |> to_list;; - : int list = [12; 12; 12; 12] Note however, SDFlow is just a proof of concept. The syntax on recursion can be made much more straightforward with some camlp4 script to allow recursive value with preconditions. I just don't have time to do that. HTH. -- Zheng Li http://www.pps.jussieu.fr/~li