Path: csiph.com!usenet.pasdenom.info!news.franciliens.net!fdn.fr!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'programmer': 0.03; 'explicitly': 0.05; 'subsequent': 0.05; '*not*': 0.07; 'assign': 0.07; 'compiler': 0.07; 'explicit': 0.07; 'odd': 0.07; 'okay': 0.09; 'restriction': 0.09; 'stating': 0.09; 'things,': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; '"an': 0.16; "(it's": 0.16; 'declarations': 0.16; 'freedom.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'governed': 0.16; 'list"': 0.16; 'lower-case': 0.16; 'personally,': 0.16; 'picks': 0.16; 'programmer,': 0.16; 'str,': 0.16; 'subject:programming': 0.16; 'variable.': 0.16; 'wrote:': 0.18; 'variable': 0.18; "python's": 0.19; 'rules': 0.22; 'cc:addr:python.org': 0.22; 'specify': 0.24; 'fine': 0.24; 'cc:2**0': 0.24; "i've": 0.25; 'handling': 0.26; 'code:': 0.26; 'header:In-Reply-To:1': 0.27; 'correct': 0.29; 'chris': 0.29; 'am,': 0.29; 'array': 0.29; '(like': 0.30; 'message-id:@mail.gmail.com': 0.30; 'apparently': 0.31; "d'aprano": 0.31; 'int,': 0.31; 'pascal': 0.31; 'steven': 0.31; 'types.': 0.31; 'lists': 0.32; 'probably': 0.32; 'languages': 0.32; 'guess': 0.33; 'third': 0.33; "i'd": 0.34; 'problem': 0.35; 'created': 0.35; 'except': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'otherwise.': 0.36; 'tight': 0.36; 'possible': 0.36; 'list': 0.37; 'actions': 0.38; 'needed': 0.38; 'whatever': 0.38; 'rather': 0.38; 'bad': 0.39; 'easy': 0.60; 'is.': 0.60; 'letters': 0.60; 'most': 0.60; 'hope': 0.61; 'course': 0.61; 'first': 0.61; 'kind': 0.63; 'more': 0.64; 'side': 0.67; 'close': 0.67; 'mar': 0.68; 'anything.': 0.68; 'picture.': 0.84; 'pike': 0.84; 'tricky': 0.84; 'union,': 0.84; 'str.': 0.91; 'that),': 0.91; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=3/efbj4Ml/5S0sGr0fCFQEpXQdnCpUbMHPNo7prnbFY=; b=hcMqhyqYY/smMf/lxtXcWe/RwpSWtnem1NysHe+KNKF2Y81vyboEs6kpW44Ps4xrls A4sZsSXbeeAbiC95yqie6CsdfStHs2DYHJu0NmULHQJMCtudTx+4TbMo6jOzcvs7xcit 3RDCr6KdJt1Z5RVwTYvTULTYlah9Qcx3epidriLHz/IJXfLA8hNS3quxHG1E0RdItqFs S+w2dE9Z03rheZtNShzIBVw1HlA8SohvWKB6Kjua6DqrK/QSUjtD1LIRFFKostWm2AY+ NJGh8BSMcYDuyVOcnS3OF6xcsDwpfxFNufUfsKopxywz+HaM24fRJhJYkscLV7gP7kmV EOcA== MIME-Version: 1.0 X-Received: by 10.68.197.36 with SMTP id ir4mr21259375pbc.46.1393871847554; Mon, 03 Mar 2014 10:37:27 -0800 (PST) In-Reply-To: <5314bb96$0$29985$c3e8da3$5496439d@news.astraweb.com> References: <4c7dbc57-eef9-4582-aecd-aac13a39b45f@googlegroups.com> <3b54a279-03a1-4a81-a428-ecad6eb16036@googlegroups.com> <216bb5f4-32c4-4f86-a9f4-1b0dd37a2a81@googlegroups.com> <0129a5b9-b85f-4ad5-b5e2-bfb2a48041d5@googlegroups.com> <5314bb96$0$29985$c3e8da3$5496439d@news.astraweb.com> Date: Tue, 4 Mar 2014 05:37:27 +1100 Subject: Re: Functional programming From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 62 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1393871851 news.xs4all.nl 2900 [2001:888:2000:d::a6]:36893 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:67570 On Tue, Mar 4, 2014 at 4:27 AM, Steven D'Aprano wrote: > On Tue, 04 Mar 2014 02:01:47 +1100, Chris Angelico wrote: > >> This is why it's tricky to put rules in based on type inference. The >> programmer's intent isn't in the picture. > > Of course it is. If I assign 23 to variable x, that signals my intent to > assign an int to x. By Occam's razor, it is reasonable to extrapolate > that intent to mean "x is an int", rather than "an int, or a list" or "an > odd int larger than 7 but smaller than 25", or "any int except 13". Type > inference picks the type which involves the fewest additional > assumptions. The programmer can always over-ride the type inference by > explicitly stating the type. Yes, and that's fine for most purposes. The problem isn't the inference, the problem is when rules are created based on that kind of guess - when the programmer's subsequent actions are governed by a guess the compiler takes. x = 23 # Compiler goes: Okay, x takes ints. x += 5 # Compiler: No prob, int += int --> int x = str(x) # Compiler: NO WAY! str(int) --> str, not allowed! It's fine and correct to infer that x is an int, x is an int, x is a str. It's *not* okay to make the third line a SyntaxError because you just put a str into an int variable. >> If Python ever acquires that >> kind of restriction ("here's a list that can contain only this type / >> these types of object"), I would hope that it's left up to the >> programmer, not the compiler, to stipulate. > > That's not type inference. That's ancient and annoying obligatory type > declarations as used by ancient languages with primitive type systems, > like Pascal and C. And that's exactly what Haskell apparently has, with homogeneous lists and no easy way to say that it can take more types. Python's handling is: A list can hold anything. Pike's handling is: An array can hold anything, unless you specify otherwise. You can specify whatever you can code: array(int(1000..2000) | string('a'..'z') | float) foo = ({1234, "abcd", 1.2}); Haskell's handling apparently is: A list/array can hold one thing and one thing only. That 'thing' can be a union, but then you need to be REALLY explicit about which side is which. It's not possible to sub-specify a type (like the "string('a'..'x')" type in Pike that will take only strings with nothing but the first 24 lower-case letters - not that I've ever needed that), but the compiler can work out everything else. The way I see it, Python's form is fully dynamic and open, Pike's is fully dynamic and the programmer's allowed to explicitly close things, and Haskell's is rigidly tight. That's not to say that tight is a bad thing (it's probably good for learning under), but personally, I'd rather have the freedom. ChrisA