* [Caml-list] composing functions... @ 2005-12-01 0:55 Jonathan Roewen 2005-12-01 1:34 ` skaller 2005-12-01 1:38 ` Pietro Abate 0 siblings, 2 replies; 5+ messages in thread From: Jonathan Roewen @ 2005-12-01 0:55 UTC (permalink / raw) To: caml-list Hi, I'm getting a bit stuck, and am wondering if there's anyway to compose a bunch of functions together easily without having to pre-maturely apply any of them. My current idea is trying to use objects, like: class virtual ['a] composable = object (self) method compose a b = self#apply a @ b method virtual apply : 'a -> int list end;; class c1 = object inherit ['a] composable method apply (a,b,c) -> [a;b;int_of_char c] end;; class c2 = object inherit ['a] composable method apply a -> [a] end;; let o1 = new c1 and o2 = new c2;; I can do something like let f a1 a2 = o1#compose a1 (o2#compose a2 []);; and get a list back... But what I'm wondering is if it's possible to make a generic compose function that takes say a list of either classes or object instances, and return a new function that I can apply the bunch of tuples to. Theory: let f = compose [o1;o2];; f a1 a2;; maybe it's not worth the hassle (if it's even possible). Jonathan ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] composing functions... 2005-12-01 0:55 [Caml-list] composing functions Jonathan Roewen @ 2005-12-01 1:34 ` skaller 2005-12-01 2:00 ` Jonathan Roewen 2005-12-01 8:46 ` Daniel Bünzli 2005-12-01 1:38 ` Pietro Abate 1 sibling, 2 replies; 5+ messages in thread From: skaller @ 2005-12-01 1:34 UTC (permalink / raw) To: Jonathan Roewen; +Cc: caml-list On Thu, 2005-12-01 at 13:55 +1300, Jonathan Roewen wrote: > Hi, > > I'm getting a bit stuck, and am wondering if there's anyway to compose > a bunch of functions together easily without having to pre-maturely > apply any of them. let compose f g x = f (g x) 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 :) -- John Skaller <skaller at users dot sf dot net> Felix, successor to C++: http://felix.sf.net ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] composing functions... 2005-12-01 1:34 ` skaller @ 2005-12-01 2:00 ` Jonathan Roewen 2005-12-01 8:46 ` Daniel Bünzli 1 sibling, 0 replies; 5+ messages in thread From: Jonathan Roewen @ 2005-12-01 2:00 UTC (permalink / raw) To: skaller; +Cc: caml-list Okay, how about something different. How could I do some sort of composing where I pass objects and arguments to be applied. Something like let f arg1 arg2 = obj1 arg1 --> obj2 arg2, where the function/operator would call the correct method on the object, passing the argument to it. Jonathan ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] composing functions... 2005-12-01 1:34 ` skaller 2005-12-01 2:00 ` Jonathan Roewen @ 2005-12-01 8:46 ` Daniel Bünzli 1 sibling, 0 replies; 5+ messages in thread From: Daniel Bünzli @ 2005-12-01 8:46 UTC (permalink / raw) To: caml-list 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 ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] composing functions... 2005-12-01 0:55 [Caml-list] composing functions Jonathan Roewen 2005-12-01 1:34 ` skaller @ 2005-12-01 1:38 ` Pietro Abate 1 sibling, 0 replies; 5+ messages in thread From: Pietro Abate @ 2005-12-01 1:38 UTC (permalink / raw) To: caml-list Let's see if I'm able to apply these 4 days of reading about monads... module ListMonadMake(T:sig type t end) = struct type mt = T.t list let return x = [x] let join mm = List.flatten mm let map f m = List.map f m let bind m f = join (map f m) let mzero = [] let mplus = List.append end module LM = ListMonadMake(struct type t = int end);; open LM ;; let apply1 (a,b,c) = [a;b;int_of_char c] ;; let apply2 a = [a] ;; let compose f1 f2 m = bind (bind (return m) f2) f1;; let f v = compose apply1 apply2 v ;; f (1,2,'a');; - : int list = [1; 2; 97] does this help ? I'm still learning, but I see a lot of potential here... How do people encode monads in ocaml ? Can you use objects to build monads similarly to Haskell ? :) pp On Thu, Dec 01, 2005 at 01:55:38PM +1300, Jonathan Roewen wrote: > Hi, > > I'm getting a bit stuck, and am wondering if there's anyway to compose > a bunch of functions together easily without having to pre-maturely > apply any of them. > > My current idea is trying to use objects, like: > > class virtual ['a] composable = object (self) > method compose a b = self#apply a @ b > method virtual apply : 'a -> int list > end;; > > class c1 = object > inherit ['a] composable > method apply (a,b,c) -> [a;b;int_of_char c] > end;; > > class c2 = object > inherit ['a] composable > method apply a -> [a] > end;; > > let o1 = new c1 and o2 = new c2;; > > I can do something like let f a1 a2 = o1#compose a1 (o2#compose a2 > []);; and get a list back... > > But what I'm wondering is if it's possible to make a generic compose > function that takes say a list of either classes or object instances, > and return a new function that I can apply the bunch of tuples to. > > Theory: let f = compose [o1;o2];; f a1 a2;; maybe it's not worth the > hassle (if it's even possible). > > Jonathan > > _______________________________________________ > Caml-list mailing list. Subscription management: > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list > Archives: http://caml.inria.fr > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs -- ++ Blog: http://blog.rsise.anu.edu.au/?q=pietro ++ ++ "All great truths begin as blasphemies." -George Bernard Shaw ++ Please avoid sending me Word or PowerPoint attachments. See http://www.fsf.org/philosophy/no-word-attachments.html ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2005-12-01 8:45 UTC | newest] Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2005-12-01 0:55 [Caml-list] composing functions Jonathan Roewen 2005-12-01 1:34 ` skaller 2005-12-01 2:00 ` Jonathan Roewen 2005-12-01 8:46 ` Daniel Bünzli 2005-12-01 1:38 ` Pietro Abate
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox