From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by sympa.inria.fr (Postfix) with ESMTPS id BB07E82355 for ; Fri, 26 Jan 2018 11:57:21 +0100 (CET) Authentication-Results: mail2-smtp-roc.national.inria.fr; spf=None smtp.pra=oleg@okmij.org; spf=Pass smtp.mailfrom=oleg@okmij.org; spf=None smtp.helo=postmaster@mail1.g3.pair.com Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of oleg@okmij.org) identity=pra; client-ip=66.39.3.114; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="oleg@okmij.org"; x-sender="oleg@okmij.org"; x-conformance=sidf_compatible Received-SPF: Pass (mail2-smtp-roc.national.inria.fr: domain of oleg@okmij.org designates 66.39.3.114 as permitted sender) identity=mailfrom; client-ip=66.39.3.114; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="oleg@okmij.org"; x-sender="oleg@okmij.org"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of postmaster@mail1.g3.pair.com) identity=helo; client-ip=66.39.3.114; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="oleg@okmij.org"; x-sender="postmaster@mail1.g3.pair.com"; x-conformance=sidf_compatible IronPort-PHdr: =?us-ascii?q?9a23=3Aa+GBHRPzZ2fJXKnUG0Al6mtUPXoX/o7sNwtQ0KIM?= =?us-ascii?q?zox0LPn7rarrMEGX3/hxlliBBdydt6odzbKO+4nbGkU4qa6bt34DdJEeHzQksu?= =?us-ascii?q?4x2zIaPcieFEfgJ+TrZSFpVO5LVVti4m3peRMNQJW2aFLduGC94iAPERvjKwV1?= =?us-ascii?q?Ov71GonPhMiryuy+4ZLebxlViDanfb9+MAi9oBnMuMURnYZsMLs6xAHTontPde?= =?us-ascii?q?RWxGdoKkyWkh3h+Mq+/4Nt/jpJtf45+MFOTav1f6IjTbxFFzsmKHw65NfqtRbY?= =?us-ascii?q?UwSC4GYXX3gMnRpJBwjF6wz6Xov0vyDnuOdxxDWWMMvrRr0yRD+s7bpkSAXwhS?= =?us-ascii?q?kJNzA37nzZhM9/g61HvR2uqQdyw5LIbIyPKPZyYrnQcc0cSGFcXshRTStBAoak?= =?us-ascii?q?YoUWC+oOI+lYpJT6qlsJqRuxGwasCfjoyj9Lg3/23K863/4mEQHc0wwtBtQDu2?= =?us-ascii?q?nXotXtLKofUOG4wLPGwDjHdf9b1y3x5JXVfh0uuf2AQLx9fdbLxUQhGA7JlEic?= =?us-ascii?q?pIj7Mz+P2OkAvG6W5Pd6W+21kW4osQRxryCvxsgyjonJgZoYylXe+iV4xIY5OM?= =?us-ascii?q?e0SEF1bNK+CJtfrSCaN5FsTsw+RGFovT42yrwAuZGlYCgH0JUqyh/FZ/GJd4WE?= =?us-ascii?q?+BzuWPyMLTtli39pYLe/iAyz8UikxO38TM600FNSoypCltnDqH8N2gfU6siET/?= =?us-ascii?q?t9/0Wh1SyI1wDJ5eFIOVo0mrbDK5493r4wkYIevljZES/znEX6lauWdkQ/+ui0?= =?us-ascii?q?8OnnebXnppqEN4BukA3+M6Iumta+AesiKAQOUXKbqqyA0+jo9Ej9BbFLleEek6?= =?us-ascii?q?/DsZmcK95IiLS+Blpy1osso0K4Czqp+NMbmHgFalVfd0TU3MDSJ1jSLaWgXr+E?= =?us-ascii?q?iFO2nWIznqGUDvjaGpzIa0P7vvLkdLd55VRbzVNqn9dc4pNWTLYbL6CqAxOjhJ?= =?us-ascii?q?njFhY8djeM7aP/Etwkj9EZWmeOAOmeKqyA6QbVtNJqGPGFYcougBi4K/Uh4Ke/?= =?us-ascii?q?33owmFtEO6bym4MeaWr+FfNjcR2U?= X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: =?us-ascii?q?A0AAAgCGCGtah3IDJ0JdHAEBAQQBAQoBA?= =?us-ascii?q?YUcjxyNd5lGFYICCodYGQcEMRcBAQEBAQEBAQEBARIBAQEIDQkIKC+COCKCSQE?= =?us-ascii?q?BBCgTRC4JNAWLCQiwYoMUOosJhFKCFYFWhGWFFwmBAIJ8gjQFiwGHO4EUkEOCB?= =?us-ascii?q?oEtkiYNghuBfog5h1aXX4E8IQGCBk0wCD2CK4IEYIICjm4BAQE?= X-IPAS-Result: =?us-ascii?q?A0AAAgCGCGtah3IDJ0JdHAEBAQQBAQoBAYUcjxyNd5lGFYI?= =?us-ascii?q?CCodYGQcEMRcBAQEBAQEBAQEBARIBAQEIDQkIKC+COCKCSQEBBCgTRC4JNAWLC?= =?us-ascii?q?QiwYoMUOosJhFKCFYFWhGWFFwmBAIJ8gjQFiwGHO4EUkEOCBoEtkiYNghuBfog?= =?us-ascii?q?5h1aXX4E8IQGCBk0wCD2CK4IEYIICjm4BAQE?= X-IronPort-AV: E=Sophos;i="5.46,415,1511823600"; d="scan'208";a="310780107" Received: from mail1.g3.pair.com ([66.39.3.114]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 26 Jan 2018 11:57:20 +0100 Received: from mail1.g3.pair.com (localhost [127.0.0.1]) by mail1.g3.pair.com (Postfix) with ESMTP id 45BD33FB06E; Fri, 26 Jan 2018 05:57:18 -0500 (EST) Received: from Magus.localnet (g.sf.ecei.tohoku.ac.jp [130.34.192.34]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail1.g3.pair.com (Postfix) with ESMTPSA id 98ABD582C71; Fri, 26 Jan 2018 05:57:17 -0500 (EST) Date: Fri, 26 Jan 2018 20:03:25 +0900 From: Oleg To: caml-list@inria.fr Message-ID: <20180126110325.GA3461@Magus.localnet> Mail-Followup-To: Oleg , caml-list@inria.fr MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.6.1 (2016-04-27) Subject: [Caml-list] Type annotations [Was: Are record types generative?] # Perhaps the subthread on type annotations should get its own # subject... > On Tue, Jan 23, 2018 at 6:14 PM, Hendrik Boom wrote: > > I'm starting to think that the ability to write OCaml programs without > mentioning the types is a drawback in the language, because it makes > programs hard to understand. I strongly recommend students to type-annotate all functions they write. In fact, they should start writing code by writing out its type. If they don't have a good idea what interface the function should have, it's very likely that they haven't really thought the problem through. In that case, they should think more about what it is they want to do, rather than jumping into writing code they have only a vaguest idea of. I follow the same advice myself. For example, suppose I want to write an accumulating map, which maps elements of a list and accumulates something along the way (this function proved very handy, at least in MetaOCaml implementation). The first this is to decide on the interface: let rec map_accum : ('accum -> 'a -> 'b * 'accum) -> 'accum -> 'a list -> 'b list * 'accum = after which the code is easy to write fun f acc -> function | [] -> ([],acc) | h::t -> let (h,acc) = f acc h in let (t,acc) = map_accum f acc t in (h::t, acc) One clear benefit of the explicit annotation is that the type errors become much more meaningful and precise (which is especially important for students). Granted, sometimes the type of some complicated callback could be too complicated to spell out (which is often the indication that one should give it a name). In that case, the underscore is handy: let rec map_accum : _ -> 'accum -> 'a list -> 'b list * 'accum = ... In short, if we know that a function is to take three arguments and return an int, why not to write it down. It also helps reading the code later on (without clicking on various identifiers: I can read much faster than I can click).