From: "Daniel Bünzli" <daniel.buenzli@epfl.ch>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] composing functions...
Date: Thu, 1 Dec 2005 09:46:20 +0100 [thread overview]
Message-ID: <D6623413-34BD-453F-816E-93AF3DCC3AA3@epfl.ch> (raw)
In-Reply-To: <1133400888.20197.40.camel@rosella>
Le 1 déc. 05 à 02:34, skaller a écrit :
> let h = compose f g
>
> You can of course fold 'compose' over a list if all the
> functions have the same type, if not, then you can't
> make the list in the first place :)
Unless you define your own type for lists of composable functions
(see below). This was an example I wrote in this discussion [1] about
encoding existential types in ocaml.
Best,
Daniel
[1] <http://sardes.inrialpes.fr/~aschmitt/cwn/2004.01.13.html#1>
module Funlist : sig
(* The funlist datatype *)
type ('a, 'b) t
(* Constructors *)
val nil : ('a, 'a) t
val cons : ('a -> 'b) -> ('b, 'c) t -> ('a, 'c) t
(* Applying a value to a composition *)
val apply : ('a, 'b) t -> 'a -> 'b
end = struct
(* List of composable functions.
The intended type expressed by the four types below is :
type ('a, 'b) t = Nil of ('a -> 'b)
| Cons of exists 'c. ('a -> 'c) * ('c, 'b) t
*)
type ('a, 'b) t = Nil of ('a -> 'b) | Cons of ('a, 'b) packed_list
and ('a, 'b, 'z) list_scope = { bind_list : 'c. ('a -> 'c) * ('c, 'b) t
-> 'z}
and ('a, 'b) packed_list = { open_list : 'z. ('a, 'b, 'z) list_scope ->
'z }
(* Packing and unpacking lists *)
let pack_list h t = { open_list = fun scope -> scope.bind_list (h,t) }
let with_packed_list p e = p.open_list e
(* Constructors *)
let nil = Nil (fun x -> x)
let cons h t = Cons (pack_list h t)
(* Type to handle the polymorphic recursion of the apply function *)
type poly_rec = { apply : 'a 'b. poly_rec -> ('a, 'b) t -> 'a -> 'b }
let apply' r l x = match l with
| Nil id -> id x
| Cons l ->
with_packed_list l { bind_list = function h,t -> r.apply r t (h
x) }
let poly_rec = { apply = apply' }
let apply l x = apply' poly_rec l x
end
(* Example of use *)
let twice x = 2*x
let double x = (x, x)
let list = Funlist.cons twice (Funlist.cons (( = ) 4) (Funlist.cons
double Funlist.nil))
let a, b = Funlist.apply list 2
next prev parent reply other threads:[~2005-12-01 8:45 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-12-01 0:55 Jonathan Roewen
2005-12-01 1:34 ` skaller
2005-12-01 2:00 ` Jonathan Roewen
2005-12-01 8:46 ` Daniel Bünzli [this message]
2005-12-01 1:38 ` Pietro Abate
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=D6623413-34BD-453F-816E-93AF3DCC3AA3@epfl.ch \
--to=daniel.buenzli@epfl.ch \
--cc=caml-list@yquem.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