caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Sources, sinks, and unbound parameter types
@ 2008-03-24 21:56 Dario Teixeira
  2008-03-24 22:17 ` [Caml-list] " Daniel Bünzli
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Dario Teixeira @ 2008-03-24 21:56 UTC (permalink / raw)
  To: caml-list

Hi,

I'm looking for a way to express the composition of functional components
in a tree-like data structure.  Each node in the tree is either:

  a) a Source, producing values "out of nowhere": unit -> 'a
  b) a Sink, end point of the tree: 'b -> unit
  c) a Processor, transforming values from one type to another: 'c -> 'd


The Ocaml type that represents a node is as follows: (pretty
straightforward, though I wonder if there's a way to explicitly
say that "Source is of 'a -> 'b where 'a must be of type unit")

type ('a, 'b) node_t =
        | Source of (unit -> 'b)
        | Sink of ('a -> unit)
        | Processor of ('a -> 'b)


In addition, there are two types of connectors linking these nodes in the tree:

  a) a Pipe, connecting one node that outputs a value of type 'a into
     another that inputs an 'a.
  b) a Splitter, essentially like a Pipe, but able to feed the same
     value into multiple inputs.


If you'll pardon the ASCII art, here's a diagram of one such simple tree:


|                        ===========
|                        | source1 | 
|                        ===========
|                             |
|                             |
|                             O Pipe
|                             |
|                             |
|                        ============
|                        | process1 |
|                        ============
|                             |
|                             |
|             ----------------O----------------
|             |           Splitter            |
|             |                               |
|        ===========                     ===========
|        |  sink1  |                     |  sink2  |
|        ===========                     ===========


Where each component node can, for example, be defined as follows:

let source1 () = 10
let process1 n = 2.0 *. (float_of_int n)
let sink1 x = Printf.printf "Sink1: %f\n" x
let sink2 x = Printf.printf "Sink2: %f\n" x


To define the tree type, I would like to express something like the code
below.  Note that I am trying to get to the compiler to statically enforce
that outputs and inputs are correctly matched type-wise.

type ('a, 'b) tree_t =
        | Node of ('a, 'b) node_t
        | Pipe of ('a, 'c) tree_t * ('c, 'b) tree_t
        | Splitter of ('a, 'c) tree_t * ('c, 'b) tree_t list


The code above obviously won't work because of the unbound type parameter 'c.
(Also, please ignore for now the fact it allows splitters with an empty
output list -- that can easily be circumvented).

So, my question is if there is any way to express what I want?  I guess there
is a solution involving the creation of a syntax extension, but I'm looking
for a pure Ocaml way.

Thanks in advance for your time!
Best regards,
Dario Teixeira





      ___________________________________________________________ 
Rise to the challenge for Sport Relief with Yahoo! For Good  

http://uk.promotions.yahoo.com/forgood/


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2008-03-24 23:06 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-03-24 21:56 Sources, sinks, and unbound parameter types Dario Teixeira
2008-03-24 22:17 ` [Caml-list] " Daniel Bünzli
2008-03-24 22:19 ` Christopher L Conway
2008-03-24 23:03 ` Jeremy Yallop

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).