caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Romain Bardou <Romain.Bardou@lri.fr>
To: Dawid Toton <d0@wp.pl>
Cc: caml-list <caml-list@inria.fr>
Subject: Re: [Caml-list] Attach an invariant to a type
Date: Thu, 31 Jan 2008 14:50:10 +0100	[thread overview]
Message-ID: <47A1D212.3010205@lri.fr> (raw)
In-Reply-To: <47a1cd167921b@wp.pl>

Well, there is no such thing as invariants with run-time checks in 
OCaml, but there are some solutions:

1) use a camlp4 syntax extension

I would like to highlight the fact that there would be a lot of problems 
to give your extension a good semantics. Your example only tackles the 
case where your objects appears directly in some function argument. What 
about, for instance, if you have a structure with a field of type 
"subindex" as an argument of a function? There are solutions but it's 
not easy.

2) (much better imo) use a module with an abstract type, such as:

module Subindex: sig
   type t
   val of_int: int -> t
   val to_int: t -> int
end = struct
   type t = int
   let of_int n =
     assert (n >= 10 && x <= 100);
     n
   let to_int n = n
end

Typing ensures that the only way one can build a value of type 
Subindex.t is by using the function Subindex.of_int, thus ensuring the 
invariant for every value of type Subindex.t thanks to the assert.

(You could use some user-defined exception such as 
Invariant_not_verified, or simply Invalid_argument, to make it clearer 
instead of using assert)

	Romain Bardou

Dawid Toton a écrit :
> What should I do if I have need for the following? Does already exist 
> any equivalent solution?
> 
> I'd like to write:
> 
> type subindex = int invariant x -> (x>=10)&&(x<=100)
> 
> let doit (a:subindex) (b:subindex) =
>   let result = some_operation a b in
>   (result:subindex)
> 
> And it should be translated to:
> 
> type subindex = int
> let subindex_invariant x = (x>=10)&&(x<=100)
> 
> let doit (a:subindex) (b:subindex) =
>   assert (subindex_invariant a);
>   assert (subindex_invariant b);
>   let result = some_operation a b in
>   assert (subindex_invariant result);
>   (result:subindex)
> 
> Am I going right direction at all?
> 
> ----------------------------------------------------
> Promocyjne oferty biletów lotniczych!
> Praga, Rzym, Paryż, Mediolan już od 499zł - Kliknij:
> http://klik.wp.pl/?adr=http%3A%2F%2Fsamoloty.wp.pl%2Fpromocje%2F&sid=202
> 
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs


  reply	other threads:[~2008-01-31 13:48 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-01-31 13:28 Dawid Toton
2008-01-31 13:50 ` Romain Bardou [this message]
2008-01-31 17:58   ` [Caml-list] " David Teller
2008-01-31 18:13     ` Romain Bardou
2008-01-31 19:13       ` Hezekiah M. Carty
2008-01-31 19:29         ` Stéphane Lescuyer
2008-01-31 19:51           ` Dawid Toton
2008-01-31 20:26             ` Edgar Friendly
2008-02-01 10:00               ` Keiko Nakata
2008-01-31 20:13           ` Romain Bardou
2008-02-10 18:00           ` Stéphane Glondu

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=47A1D212.3010205@lri.fr \
    --to=romain.bardou@lri.fr \
    --cc=caml-list@inria.fr \
    --cc=d0@wp.pl \
    /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).