caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Maxence Guesdon <maxence.guesdon@inria.fr>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Re: Create a constraint between variant type and data list
Date: Fri, 17 Sep 2010 09:29:29 +0200	[thread overview]
Message-ID: <20100917092929.43c39973@alcazar.inria.fr> (raw)
In-Reply-To: <slrni82q3o.skq.sylvain@gallu.homelinux.org>

On Fri, 3 Sep 2010 21:28:24 +0000 (UTC)
Sylvain Le Gall <sylvain@le-gall.net> wrote:

> On 03-09-2010, bluestorm <bluestorm.dylc@gmail.com> wrote:
> > Hi,
> >
> > Finally, I have a third solution based on code generation : given my
> > first solution (turning the association list into a function), what
> > you need is only a list of all the constructors (and you can build
> > your assoc list with List.map (fun x -> x, assoc_function x)). This
> > can easily be generated from the datatype declaration using direct
> > camlp4, or Markus Mottl's type-conv (
> > http://www.ocaml.info/home/ocaml_sources.html#toc11 ).
> >
> 
> Your answer and the one from Martin/Ashish, makes me think that I need
> to go back to camlp4/type-conv... I would have like to avoid this
> solution, but I think it is the best one.

Hello,

Here is another solution, based on oug[1]. The idea is to:
1. make oug analyse your source files and dump the result,
2. write a program loading the dump and performing the checks you want.

Here is an example, which performs 1. and 2., with only one source file "foo.ml".

=== myoug.ml
let (data, _) = Ouglib.Analyze.analyze ["foo.ml"];;

let number_variants =
  Ouglib.Lang.filter_elements data
    (Ouglib.Lang.Name
     {
       Ouglib.Lang.sel_name = "Foo.number.*" ;
       sel_kind = [ Ouglib.Data.Type_field ] ;
     }
    )
;;

let numbers_list_id =
  match
    Ouglib.Lang.filter_elements data
      (Ouglib.Lang.Name
       {
         Ouglib.Lang.sel_name = "Foo.numbers" ;
         sel_kind = [ Ouglib.Data.Value ] ;
       }
      )
  with
    [id] -> id
  | _ -> assert false
;;

let graph = data.Ouglib.Data.graph ;;
let created_by_numbers_list =
  let l = Ouglib.Graph.succ graph numbers_list_id in
  let f acc (id, k) =
    match k with
      Ouglib.Data.Create -> id :: acc
    | _ -> acc
  in
  List.fold_left f [] l
;;

let (_,missing) =
  List.partition (fun id -> List.mem id created_by_numbers_list) number_variants
;;

match missing with
  [] -> exit 0
| _ ->
    let b = Buffer.create 256 in
    Ouglib.Dump.print_element_list data b  missing;
    prerr_endline
      (Printf.sprintf "The following constructors are not present in Foo.numbers:\n%s"
       (Buffer.contents b)
      );
    exit 1
;;
=== /myoug.ml ==

=== foo.ml ===
type number = One | Two | Three | Four;;

let numbers = [ One, 1 ; Two, 2 ];;
=== /foo.ml ==

Compilation of myoug.ml to myoug.x:
# ocamlc -I +oug str.cma toplevellib.cma oug.cma -o myoug.x myoug.ml 

Launching myoug.x gives the following:
The following constructors are not present in Foo.numbers:
f Foo.number.Three
f Foo.number.Four

Of course, you can adapt the program to fit your needs: have a makefile
target to create the oug dump and use this dump in various checking
programs, or in one program performing various checks.

Hope this helps,

Maxence

[1] http://home.gna.org/oug/index.fr.html



  reply	other threads:[~2010-09-17  7:29 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-09-03 17:16 Sylvain Le Gall
2010-09-03 17:38 ` [Caml-list] " bluestorm
2010-09-03 21:28   ` Sylvain Le Gall
2010-09-17  7:29     ` Maxence Guesdon [this message]
2010-09-03 18:51 ` [Caml-list] " Martin Jambon
2010-09-03 19:39   ` Ashish Agarwal
2010-09-03 21:13 ` Maxence Guesdon
2010-09-03 21:25   ` Sylvain Le Gall
2010-09-04  6:35 ` [Caml-list] " Julien Signoles
2010-09-04  6:40   ` Julien Signoles
2010-09-04 16:25 ` Anil Madhavapeddy
2010-09-17  8:57 ` Sylvain Le Gall

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=20100917092929.43c39973@alcazar.inria.fr \
    --to=maxence.guesdon@inria.fr \
    --cc=caml-list@yquem.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;
as well as URLs for NNTP newsgroup(s).