From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.1 required=5.0 tests=AWL autolearn=disabled version=3.1.3 Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 43FF8BC6C for ; Wed, 1 Nov 2006 06:39:37 +0100 (CET) Received: from mail.rsise.anu.edu.au (mail.rsise.anu.edu.au [150.203.208.4]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id kA15dX9q009490 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Wed, 1 Nov 2006 06:39:36 +0100 Received: from localhost (localhost [127.0.0.1]) by mail.rsise.anu.edu.au (Postfix) with ESMTP id 88F3853EA6 for ; Wed, 1 Nov 2006 16:39:23 +1100 (EST) Received: from mail.rsise.anu.edu.au ([150.203.208.4]) by localhost (mail [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 15904-01 for ; Wed, 1 Nov 2006 16:39:23 +1100 (EST) Received: from pulp.rsise.anu.edu.au (pulp.rsise.anu.edu.au [150.203.208.49]) by mail.rsise.anu.edu.au (Postfix) with ESMTP id 6F9F653D4B for ; Wed, 1 Nov 2006 16:39:14 +1100 (EST) Received: by pulp.rsise.anu.edu.au (Postfix, from userid 1560) id 587C0A0BB5B; Wed, 1 Nov 2006 16:40:39 +1100 (EST) Date: Wed, 1 Nov 2006 16:40:39 +1100 From: Pietro Abate To: ocaml ml Subject: design problem Message-ID: <20061101054039.GA25334@pulp.rsise.anu.edu.au> Mail-Followup-To: Pietro Abate , ocaml ml MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Operating-System: GNU/Linux X-Organization: Research School of Information Science and Engineering (Australian National University) User-Agent: Mutt/1.5.13 (2006-08-11) X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at mail.rsise.anu.edu.au X-Miltered: at concorde with ID 45483315.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; re-use:01 variants:01 variants:01 camlp:01 failwith:01 failwith:01 blog:98 blog:98 polymorphic:01 polymorphic:01 rec:01 rec:01 extensible:01 functions:01 functions:01 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