Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Pietro Abate <Pietro.Abate@anu.edu.au>
To: ocaml ml <caml-list@inria.fr>
Subject: design problem
Date: Wed, 1 Nov 2006 16:40:39 +1100	[thread overview]
Message-ID: <20061101054039.GA25334@pulp.rsise.anu.edu.au> (raw)

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) )
  |`And (x,y)         -> `And (f x, f y)
  |`Or  (x,y)         -> `Or  (f x, f y)
  |`Not ( `Atom _) as x -> x
  |`Atom _ as x -> x
  |_ -> failwith "impossible pc"
;;

let rec nnfpc t = nnfpc_aux nnfpc t;;

(* extension of the basic data type *)
type 'a termk =
    [`Dia of 'a
    |`Box of 'a
    |'a termpc
    ]
;;

let nnfk_aux f = function
  |`Not (`Dia x ) -> `Box (f (`Not x))
  |`Not (`Box x ) -> `Dia (f (`Not x))
  |`Dia x         -> `Dia (f x)
  |`Box x         -> `Box (f x)
  |#termpc as x -> nnfpc_aux f x
  |_ -> failwith "impossible k"
;;

let rec nnfk t = nnfk_aux nnfk t;;

(* an other extension on top of termk *)
type 'a termlck =
    [`CDia of 'a
    |`CBox of 'a
    |'a termk
    ]
;;

let rec nnflck_aux f = function
  |`Not (`CDia x ) -> `CBox (f (`Not x))
  |`Not (`CBox x ) -> `CDia (f (`Not x))
  |`CDia x         -> `CDia (f x)
  |`CBox x         -> `CBox (f x)
  |#termk as x -> nnfk_aux f x
  |_ -> failwith "impossible lck"
;;

let rec nnflck t = nnflck_aux nnflck t;;

let a = `Not (`Not (`Not (`Dia (`CDia (`Atom "a")))));;
let b = nnflck a;;



-- 
++ Blog: http://blog.rsise.anu.edu.au/?q=pietro
++ 
++ "All great truths begin as blasphemies." -George Bernard Shaw
++ Please avoid sending me Word or PowerPoint attachments.
   See http://www.fsf.org/philosophy/no-word-attachments.html


             reply	other threads:[~2006-11-01  5:39 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-11-01  5:40 Pietro Abate [this message]
2006-11-01  6:53 ` [Caml-list] " Jacques Garrigue

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=20061101054039.GA25334@pulp.rsise.anu.edu.au \
    --to=pietro.abate@anu.edu.au \
    --cc=caml-list@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