On Feb 25, 9:26 am, Konrad Hinsen <konrad.hin...@laposte.net> wrote:
> I have just added a new library to clojure.contrib. It provides a  
> proof-of-concept implementation of algebraic data types. An example  
> for what you can do with it:
>
> (deftype tree
>    empty-tree
>    (leaf value)
>    (node left-tree right-tree))
>
> (def a-tree (node (leaf :a)
>                   (node (leaf :b)
>                         (leaf :c))))
>
> (defn depth
>    [#^tree t]
>    (match t
>      empty-tree  0
>      (leaf n)    1
>      (node l r)  (inc (max (depth l) (depth r)))))
>
> (depth empty-tree)
> (depth a-tree)
>
> For more examples, see clojure.contrib.types.examples.
>
> The reason why I call this a proof-of-concept implementation is that  
> it has a major drawback that seriously limits its applicability:  
> equality tests on objects created from these types are done by  
> identity, not by value equality, which makes them practically  
> useless. This is due to the fact that the objects are actually  
> implemented as closures. Another drawback, following as well from the  
> implementation as closures, is that the objects cannot have any meta-
> data attached.

I've been working on two small subsystems that might be of interest.

Model is a subsystem that works similarly to defstruct and struct, but
with extensions that permit you to create map objects from prototypes,
restrict the values allowed in the fields when maps are created, and
test whether a given map is an instance of a model. Because instances
of models are just maps, value equality works with them.

GF is a subsystem that implements CLOS-style generic functions for
Clojure, with a couple of extensions. In addition to the expected
class-based dispatch and EQL specializers, GF also dispatches on a
user-supplied predicate. In addition, you can supply an optional
argument to make-generic-function (or define-generic) to provide an
alternate set of tests to the dispatching algorithm, permitting you to
easily customize dispatching.

It seems like, taken together, model and gf might be of some help in
the implementation of algebraic datatypes; I would like to see a nice
algebraic datatypes packge for Clojure.

Model and GF are brand new code, lightly tested, and largely
unoptimized. I would expect you to find bugs, performance problems,
and mistakes in the design.

I've uploaded current versions of model.clj and gf.clj in case you or
others want to try them out. They're self-contained, but be aware that
they're part of a larger project I'm working on, and they presently
live in the xg namespace; you might want to change the namespace to be
something more convenient for use with your code.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to