From: Zheng Li <li@pps.jussieu.fr>
To: caml-list@inria.fr
Subject: Re: "Ref" and copy of functions
Date: Fri, 14 Dec 2007 11:49:09 +0100 [thread overview]
Message-ID: <87d4t9k0uy.fsf@pps.jussieu.fr> (raw)
In-Reply-To: <6cb897b30712130927m7df88e3axec56eacba4d47fdf@mail.gmail.com>
Hi, "Pierre-Evariste Dagand" <pedagand@gmail.com> 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
next prev parent reply other threads:[~2007-12-14 11:15 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-12-13 17:27 Pierre-Evariste Dagand
2007-12-14 10:49 ` Zheng Li [this message]
2007-12-14 14:51 ` [Caml-list] " David Teller
2007-12-14 16:19 ` Zheng Li
2007-12-14 14:54 ` [Caml-list] " Pierre-Evariste Dagand
2007-12-14 16:12 ` Zheng Li
2007-12-14 16:55 ` [Caml-list] " Pierre-Evariste Dagand
2007-12-14 16:30 ` Loup Vaillant
[not found] ` <6cb897b30712140848j52e5628avbf0e3dadcb771f71@mail.gmail.com>
2007-12-14 16:57 ` Pierre-Evariste Dagand
2007-12-16 5:17 ` [Caml-list] " Jacques Garrigue
2007-12-16 16:39 ` Pierre-Evariste Dagand
2007-12-16 18:27 ` Pierre-Evariste Dagand
2007-12-15 12:08 oleg
2007-12-15 15:19 ` Pierre-Evariste Dagand
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=87d4t9k0uy.fsf@pps.jussieu.fr \
--to=li@pps.jussieu.fr \
--cc=caml-list@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