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=1.0 required=5.0 tests=AWL,SPF_NEUTRAL 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 72673BC69 for ; Wed, 14 Nov 2007 00:41:58 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAANvEOUfRVYT7i2dsb2JhbACOewEBAQgCBhMRBQ X-IronPort-AV: E=Sophos;i="4.21,412,1188770400"; d="scan'208";a="19250512" Received: from an-out-0708.google.com ([209.85.132.251]) by mail4-smtp-sop.national.inria.fr with ESMTP; 14 Nov 2007 00:41:57 +0100 Received: by an-out-0708.google.com with SMTP id c24so365057ana for ; Tue, 13 Nov 2007 15:41:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:user-agent:mime-version:to:subject:content-type:content-transfer-encoding; bh=rAQ/6aclUCpRhhr0o0UM6kPLmfoB3tWsNPkjJD97XT4=; b=JM8bwECBsb8a56aOp4cJPjlCs8V05q5oORCLn5XzbSgxzkDl6syQh93ugQ5+RUv5gA9vmIvM8nUEqORcG28TMh+yKRoHvGj3zy+kzBS10VGRB/HiZPrkMFq/l65So7mowAXle6Zx8r6trFCM9ODdRxO6CQ3iipwsNPrZEqy7A0s= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:user-agent:mime-version:to:subject:content-type:content-transfer-encoding; b=kxcA3luA5ELBC0+cPRCMBvRlHabDFr0yB7bY290t+X+3st0YqFEcGMtVvIo47UNM6OayWMKotjn4BAVEr4ddwC5i7BuVB7UUy/qxUFXHXBzV767t3+2slLBIzNn1Xdei/avaJ1p/q+akMbyggxI+0VCqYS7JsovNve3oXhxoGGs= Received: by 10.100.206.11 with SMTP id d11mr10789461ang.1194997316738; Tue, 13 Nov 2007 15:41:56 -0800 (PST) Received: from ?192.168.0.7? ( [69.152.94.247]) by mx.google.com with ESMTPS id b29sm9682093ana.2007.11.13.15.41.54 (version=SSLv3 cipher=RC4-MD5); Tue, 13 Nov 2007 15:41:54 -0800 (PST) Message-ID: <473A363F.2080301@gmail.com> Date: Tue, 13 Nov 2007 17:41:51 -0600 From: Edgar Friendly User-Agent: Thunderbird 2.0.0.9 (X11/20071031) MIME-Version: 1.0 To: caml-list Subject: Compiler feature - useful or not? Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam: no; 0.00; compiler:01 ocaml:01 variants:01 functors:01 sig:01 val:01 val:01 functor:01 struct:01 abstraction:01 functor:01 functors:01 runtime:01 bookkeeping:01 compilation:01 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) 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.