1 Nov 2006 06:40
design problem
Pietro Abate <Pietro.Abate <at> anu.edu.au>
2006-11-01 05:40:39 GMT
2006-11-01 05:40:39 GMT
Hi all,
I've the following code to write types and functions in an extensible
way, so to re-use my code a bit. Everything seems working fine.
I've two questions:
- Is there a better way of achieving a similar result ? Here I'm using
polymorphic variants, but I'm wondering if somebody already cooked
something together by using variants and camlp4 extensions...
- At the moment type errors are pretty horrible. How can I coerce these
functions to give prettier errors ? I've tried to coerce the function
f in the *_aux functions below, but of course this is not possible as
it needs to be polymorphic by design ...
thanks :)
p
(* my basic data type *)
type 'a termpc =
[`And of 'a * 'a
|`Or of 'a * 'a
|`Not of 'a
|`Atom of string
]
;;
(* a simple normal form function *)
let nnfpc_aux f = function
|`Not ( `Not x ) -> f x
|`Not ( `And(x,y) ) -> `Or (f (`Not x), f (`Not y) )
|`Not ( `Or (x,y) ) -> `And (f (`Not x), f (`Not y) )
(Continue reading)
If you like modules, you might want to try combining with private row
types and recursive modules (see the paper in publications), but
whether it helps or not depends on the size of your code.
> - At the moment type errors are pretty horrible. How can I coerce these
> functions to give prettier errors ? I've tried to coerce the function
> f in the *_aux functions below, but of course this is not possible as
> it needs to be polymorphic by design ...
You can perfectly write polymorphic type annotations.
Here is your code with some annotations, which gives readable types
after inference.
Note that I removed the catch-all cases in your pattern-matchings, are
they greatly reduce the interest of using polymorphic
variants. However I had to leave one for `Not, as otherwise the
parameter would not be extensible.
I also enclose a version using private row types, which separates
positive and negative cases, so as to remove all catch-all cases.
RSS Feed