caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Dario Teixeira <darioteixeira@yahoo.com>
To: caml-list@yquem.inria.fr
Subject: Sources, sinks, and unbound parameter types
Date: Mon, 24 Mar 2008 21:56:09 +0000 (GMT)	[thread overview]
Message-ID: <535050.42465.qm@web54603.mail.re2.yahoo.com> (raw)

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/


             reply	other threads:[~2008-03-24 21:56 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-03-24 21:56 Dario Teixeira [this message]
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

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=535050.42465.qm@web54603.mail.re2.yahoo.com \
    --to=darioteixeira@yahoo.com \
    --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;
as well as URLs for NNTP newsgroup(s).