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 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id D0C22BC69 for ; Wed, 14 Nov 2007 12:01:20 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAADtkOkfU4363nmdsb2JhbACBW40lAQEHAggp X-IronPort-AV: E=Sophos;i="4.21,415,1188770400"; d="scan'208";a="19272695" Received: from moutng.kundenserver.de ([212.227.126.183]) by mail4-smtp-sop.national.inria.fr with ESMTP; 14 Nov 2007 12:01:20 +0100 Received: from gate.lan.gerd-stolpmann.de (dslb-084-059-105-237.pools.arcor-ip.net [84.59.105.237]) by mrelayeu.kundenserver.de (node=mrelayeu8) with ESMTP (Nemesis) id 0ML31I-1IsFzg0mF3-0003aF; Wed, 14 Nov 2007 12:01:19 +0100 Received: from flakew.lan.gerd-stolpmann.de (fw.lan.gerd-stolpmann.de [192.168.1.1]) by gate.lan.gerd-stolpmann.de (Postfix) with ESMTP id 81034C097; Wed, 14 Nov 2007 12:01:12 +0100 (CET) Subject: Re: [Caml-list] Compiler feature - useful or not? From: Gerd Stolpmann To: Edgar Friendly Cc: caml-list In-Reply-To: <473A363F.2080301@gmail.com> References: <473A363F.2080301@gmail.com> Content-Type: text/plain Date: Wed, 14 Nov 2007 12:01:11 +0100 Message-Id: <1195038071.29197.85.camel@localhost.localdomain> Mime-Version: 1.0 X-Mailer: Evolution 2.6.1 Content-Transfer-Encoding: 7bit X-Provags-ID: V01U2FsdGVkX19D2WM9mYbkGr6lbP42oYNO3gSTdiuFfWMwH2i GdqVs3OrD/vnD/GPw9LyQnyrqIYw5QK9LZz0N3AS0s+1zkM6hj fC0vf3ulb+Jx5JwUC3nnq0GQFJPklp+ X-Spam: no; 0.00; compiler:01 gerd:01 stolpmann:01 ocaml:01 variants:01 runtime:01 gerd:01 functors:01 sig:01 val:01 val:01 functor:01 struct:01 abstraction:01 functor:01 Am Dienstag, den 13.11.2007, 17:41 -0600 schrieb Edgar Friendly: > When one writes > > type row = int > type col = int > > This allows one to use the type names "row" and "col" as synonyms of > int. But it doesn't prevent one from using a value of type row in the > place of a value of type col. OCaml allows us to enforce row as > distinct from int two ways: > > 1) Variants: > type row = Row of int > type col = Col of int > > Downside: unnecessary boxing and tagging > conversion from row -> int: (fun r -> match r with Row i -> i) Note that you can write much shorter: (fun Row i -> i), or let some_function (Row i) = ... If the small runtime penalty is not essential, this is probably the best way to do it. Gerd > conversion from int -> row: (fun i -> Row i) > > 2) Functors: > module type RowCol = > sig > type row > val int_of_row : row -> int > val row_of_int : int -> row > type col > val int_of_col : col -> int > val col_of_int : int -> col > end > > module Main = functor (RC: RowCol) -> struct > (* REST OF PROGRAM HERE *) > end > > Any code using rows and cols could be written to take a module as a > parameter, and because of the abstraction granted when doing so, type > safety is ensured. > > Downside: functor overhead, misuse of functors, need to write > boilerplate conversion functions > conversion from row -> int, int -> row: provided by RowCol boilerplate > > IS THE FOLLOWING POSSIBLE: > Modify the type system such that one can declare > > type row = new int > type col = new int > > Row and col would thus become distinct from int, and require explicit > casting/coercion (2 :> row). There would be no runtime overhead for use > of these types, only bookkeeping overhead at compilation. > > Downside: compiler changes (hopefully not too extensive) > conversion from row -> int: (fun r -> (r :> int)) (* might need (r : row > :> int) if it's not already inferred *) > conversion from int -> row: (fun i -> (i :> row)) > > Thoughts? Do any of you use Variants or Functors to do this now? Do > you find this style of typing useful? > > E. > > _______________________________________________ > 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 > -- ------------------------------------------------------------ Gerd Stolpmann * Viktoriastr. 45 * 64293 Darmstadt * Germany gerd@gerd-stolpmann.de http://www.gerd-stolpmann.de Phone: +49-6151-153855 Fax: +49-6151-997714 ------------------------------------------------------------