This site is updated infrequently. For up-to-date information, please visit the new OCaml website at ocaml.org.

[Caml-list] Is arrow programming impossible in ocaml?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: 2003-10-15 (01:48) From: Nick Name Subject: Re: [Caml-list] Is arrow programming impossible in ocaml?
```Alle 02:22, mercoledì 15 ottobre 2003, Jacques Garrigue ha scritto:
> I'm afraid you will have to state in more detail what you want to do.
> Or to show some Haskell code.

Here it is:

module type ARROW =
sig
type ('a,'b) t

val arr : ('a -> 'b) -> ('a,'b) t
val (>>>) : ('a,'b) t -> ('b,'c) t -> ('a,'c) t
val (&&&) : ('a,'b) t -> ('a,'c) t -> ('a,('b * 'c)) t
end

module Time : sig

include ARROW

val time : ('a,float) t
val run : (unit,'a) t -> float -> 'a option
val const : 'a -> ('b,'a) t
end =
struct
type state = (float * float)

(* A signal transformer is a function wich takes the current time (and
dt) and maybe returns a value, or stops with None *)

type ('a,'b) t = T of ((state * 'a) -> (('b * ('a,'b) t) option))

let rec arr f = T (fun (_,x) -> Some (f x,arr f))

let rec (>>>) (T a1) (T a2) =
T (fun (s,x) ->
match a1 (s,x) with
None -> None
| Some (r1,n1) ->
match a2 (s,r1) with
None -> None
| Some (r2,n2) ->
Some (r2,n1 >>> n2))

let rec (&&&) (T a1) (T a2) =
T (fun arg ->
match (a1 arg,a2 arg) with
((None,_)|(_,None)) -> None
| (Some (r1,n1),Some (r2,n2)) -> Some ((r1,r2),n1 &&& n2))

let rec time = T (fun ((t,_),_) -> Some (t,time))

let run (T f) t =
match f ((t,0.0),()) with
None -> None
| Some (r,_) -> Some r

let const x = arr (fun _ -> x)
end

(* now in the toplevel

# let t = time >>> time;;
val t : ('_a, float) Var.Time.t = <abstr> *)

Well, that '_a is exactly what I wish to avoid; I had this suggestion
privately (with simplified type declarations):

# type ('a,'b) sf = Arr of ('a * float -> 'b);;
type ('a, 'b) sf = Arr of ('a * float -> 'b)
# let (>>>) f1 f2  = match f1(),f2() with (Arr f1),(Arr f2) -> Arr (fun
(a,s) -> f2 (f1(a,s),s));;
val ( >>> ) : (unit -> ('a, 'b) sf) -> (unit -> ('b, 'c) sf) -> ('a, 'c)
sf =
<fun>
# let time () = Arr (fun (_,t) -> t);;
val time : unit -> ('a, float) sf = <fun>
# let t () = time >>> time;;
val t : unit -> ('a, float) sf = <fun>

and yet I don't know if it's satisfactory, however I realize that this
is a general problem with caml view of polymorphism, probably driven by
type-inference decidability issues; in fact I can't write a polymorphic
compose operator in general, because I will get exactly the same
problems.

Vincenzo

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners

```