caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* ANN: pretty-printing, type-safe marshalling, dynamic typing for free.
@ 2007-06-20  0:14 Jeremy Yallop
  2007-06-20  8:01 ` [Caml-list] " Nicolas Pouillard
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Jeremy Yallop @ 2007-06-20  0:14 UTC (permalink / raw)
  To: caml-list

I'm pleased to announce an initial release of `deriving', a system for
constructing functions automatically from type definitions.

Deriving extends OCaml with two new operations: a syntax for calling
an "overloaded" function at a particular type:

    C.function<t> v

and the `deriving' construct found in Haskell (and Clean) for
requesting that the implementation generate an instance of an
overloaded function from a type definition:

    type t = r
      deriving (C1, C2)

Deriving provides functions for pretty-printing, dynamic typing,
type-safe structure-sharing marshalling, SML-style equality,
mapping, and more.  Functions can be generated for most OCaml types,
including records, standard and polymorphic variants, tuples, mutually
recursive types, and types that use abstract type constructors defined
elsewhere.  A few more exotic constructs, such as private types and type
replication, are supported.  Derived functions are also extensible: the
user can choose to supply an implementation at a particular type if the
generated version is not to his taste.

You can find deriving (along with preliminary documentation and tests)
at

    http://code.google.com/p/deriving/

Note: you'll need OCaml 3.10.

Some examples of use follow.

Pretty-printing:

    Show.show<int> 3
    =>
    "3"

    let factors = [(10,[2;5]); (11, []);  12, [2;3;4;6]]
    Show.show<(int * int list) list> factors
    =>
    "[(10,[2; 5]); (11, []);  12, [2; 3; 4; 6]]"


    type 'a tree = Leaf of 'a | Branch of 'a tree * 'a * 'a tree
        deriving (Show, Typeable, Eq, Shelve)

    type point = { x : float; y : float }
        deriving (Show, Typeable, Eq, Shelve)

    let points = Branch (Leaf {x=0.0;
                               y=0.0;},
                         {x=2.0; y=2.0},
                         Branch (Leaf {x=1.0; y=1.0},
                                 {x=1.0; y=0.0},
                                 Leaf {x=0.0; y=1.0}))

    Show.show<point tree> points
    =>
    "Branch
       (Leaf {x =193.11; y =132.13}, {x =211.91; y =201.11},
        Branch
          (Leaf {x =113.12; y =1.}, {x =12.7; y =44.1}, Leaf {x =0.; y 
=13.41}))"

Dynamic typing:

    let items =
       [Typeable.mk<int> 3;
        Typeable.mk<float> 3.0;
        Typeable.mk<string tree> (Leaf "three")]
    =>
    [<abstr>; <abstr>; <abstr>]

    Typeable.cast<int> (List.hd items)
    =>
    Some 3

    Typeable.cast<float> (List.hd items)
    =>
    None


Dynamic typing again:

    type 'a seq = [`Nil | `Cons of 'a * 'a seq]
        deriving (Typeable)

    let l = `Cons (3, `Cons (2, `Cons (1, `Nil)))

    Typeable.cast<[`Cons of int * 'a|`Nil] as 'a>
      (Typeable.mk<int seq> l)
    =>
    Some (`Cons (3, `Cons (2, `Cons (1, `Nil))))

Serialisation (marshalling):

    Shelve.shelveS<point tree> points
    =>
 
"\007\003\t\128\128\128\128\128\128\128\248?\t\128\128\128\128\128\128\128\128@\001\000\005\000\001\008\000\001\n\001\003\004\t\003\000\001\012\001\003\006\011\005\005\002\002\000\002\000\002\002\000\000\002\001\001\002\002\002"

    Shelve.unshelveS<point tree> (Shelve.shelveS<point tree> points)
    =>
    Branch
      (Leaf {x =193.11; y =132.13}, {x =211.91; y =201.11},
       Branch
         (Leaf {x =113.12; y =1.}, {x =12.7; y =44.1}, Leaf {x =0.;
                                                             y =13.41}))


    Shelve.unshelveS<int> (Shelve.shelveS<point tree> points)
    =>
    *** Exception

For more see the documentation and test suite.

Comments, bug reports, and other feedback will be most welcome.

Jeremy.


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

end of thread, other threads:[~2007-07-20  7:44 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-06-20  0:14 ANN: pretty-printing, type-safe marshalling, dynamic typing for free Jeremy Yallop
2007-06-20  8:01 ` [Caml-list] " Nicolas Pouillard
     [not found] ` <AB56B84F-A45E-433B-B419-2B49F5D92043@gmail.com>
2007-06-20 10:27   ` Jeremy Yallop
2007-06-20 18:28     ` Stefan Monnier
2007-06-20 19:38       ` [Caml-list] " Jeremy Yallop
2007-06-20 21:06         ` Eric Cooper
2007-06-21  7:37           ` Nicolas Pouillard
2007-06-22  0:05             ` Jeremy Yallop
2007-07-15  4:03 ` [Caml-list] " Jon Harrop
2007-07-17  3:05   ` Jon Harrop
2007-07-19  0:20     ` Jeremy Yallop
2007-07-20  6:24       ` Jon Harrop

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).