Python from Wise Guy's Viewpoint

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joachim Durchholz

    #406
    Re: Python from Wise Guy's Viewpoint

    Espen Vestre wrote:[color=blue]
    > Now you're conflating two readings of "want declarations" (i.e. "want
    > them whenever they're convenient for optimizing" vs. "want them
    > everywhere and always")[/color]

    Type inference is about "as much static checking as possible with as
    little annotations as absolutely necessary".
    HM typing is extremely far on the "few declarations" side: a handful
    even in large systems.

    It sounds unbelievable, but it really works.

    Oh, there's one catch: Most functional programs have heaps of type
    definitions similar to this one:
    Tree a = Leaf a
    | Node (Tree a) (Tree a)
    | Empty
    However, these definitions don't only declare the type, they also
    introduce constructors, which also serve as inspectors for pattern matching.

    In other words, the above code is all that's needed to allow me to
    construct arbitrary values of the new type (gratuitious angle brackets
    inserts to make the code easier to recognize with a C++ background),
    like this:
    Leaf 5 -- Creates a Leaf <Integer> object that contains value 5
    Node Empty Empty -- Creates a node that doesn't have leaves
    -- Type is Tree <anything>, i.e. we can insert this object into
    -- a tree of any type, since there's no way that this can ever
    -- lead to type errors.
    Node (Leaf 5) (Leaf 6) -- Creates a node with two leaves

    It also allows me to use the constructor names as tags for pattern
    matching. Note that every one of the following three definitions
    consists of the name of the function being defined, a pattern that the
    arguments must follow to select this particular definition, and the
    actual function body (which is just an expression here). Calling a
    function with parameters that match neither pattern is considered an
    error (which is usually caught at compile time but not in all cases -
    not everything is static even in functional languages).
    mapTree f (Leaf foo) = Leaf f foo
    -- If mapTree if given a "something" called f,
    -- and some piece of data that was constructed using Leaf foo,
    -- then the result will be obtained by applying f as a function
    -- to that foo, and making the result a Leaf.
    -- The occurence of f in a function position on the right side
    -- makes type inference recognize it as a function.
    -- The Leaf pattern (and, incidentally, the use of the Leaf
    -- constructor on the right side) will make type inference
    -- recognize that the second parameter of mapTree must be
    -- of Tree <anything> type. Usage details will also make
    -- type inference conclude that f must be a function from
    -- one "anything" type to another, potentially different
    -- "anything" type.
    -- In other words, the type of mapTree is known to be
    -- (a -> b) -> Tree a -> Tree b
    -- without a single type declaration in mapTree's code!
    mapTree f (Node left right) = Node (maptree f left) (maptree f right)
    -- This code is structured in exact analogy to the Leaf case.
    -- The only difference is that it uses recursion to descend
    -- into the subtrees.
    -- Incidentally, this definition of mapTree
    mapTree f Empty = Empty
    -- The null value doesn't need to be mapped, it will look the
    -- same on output.
    -- Note that this definition of mapTree doesn't restrict the
    -- type of f in the least.
    -- In HM typing, you usually don't specify the types, every
    -- usage of some object adds further restrictions what that
    -- type can be. If the set of types that a name can have becomes
    -- empty, you have contradictory type usage and hence a type error.

    Hope this helps
    Jo

    Comment

    • Fergus Henderson

      #407
      Re: Python from Wise Guy's Viewpoint

      Pascal Costanza <costanza@web.d e> writes:[color=blue]
      >Fergus Henderson wrote:[color=green]
      >>Pascal Costanza <costanza@web.d e> writes:[color=darkred]
      >>>Fergus Henderson wrote:
      >>>>Pascal Costanza <costanza@web.d e> writes:
      >>>Furthermor e, if I remember correctly, dynamically compiled systems use
      >>>type inferencing at runtime to reduce the number of type checks.[/color]
      >>
      >> In cases such as the one described above, they may reduce the number of
      >> times that the type of the _collection_ is checked, but they won't be
      >> able to avoid checking the element type at every element access.[/color]
      >
      >Why? If the collection happens to contain only elements of a single type
      >(or this type at most), you only need to check write accesses if they
      >violate this condition. As long as they don't, you don't need to check
      >read accesses.[/color]

      So which, if any, implementations of dynamic languages actually perform such
      optimizations?

      --
      Fergus Henderson <fjh@cs.mu.oz.a u> | "I have always known that the pursuit
      The University of Melbourne | of excellence is a lethal habit"
      WWW: <http://www.cs.mu.oz.au/~fjh> | -- the last words of T. S. Garp.

      Comment

      • Pascal Bourguignon

        #408
        Re: Python from Wise Guy's Viewpoint

        Matthias Blume <find@my.addres s.elsewhere> writes:
        [color=blue]
        > Pascal Costanza <costanza@web.d e> writes:
        >[color=green]
        > > Computers are fast enough and have enough memory nowadays. You are
        > > talking about micro efficiency. That's not interesting anymore.[/color]
        >
        > I have worked on projects where people worried about *every cycle*.
        > (Most of the time I agree with you, though. Still, using infinite
        > precision by default is, IMO, a mistake.[/color]

        What are you writing about? Figments of your imagination or real
        concrete systems?


        [20]> (typep (fact 100) 'fixnum)
        NIL
        [21]> (typep (fact 100) 'bignum)
        T
        [22]> (typep (/ (fact 100) (fact 99)) 'fixnum)
        T
        [23]> (typep (/ (fact 100) (fact 99)) 'bignum)
        NIL
        [24]> (/ 1 3)
        1/3
        [25]> (/ 1.0 3)
        0.33333334

        Where do you see "infinite precision by default"?


        --
        __Pascal_Bourgu ignon__

        Comment

        • Joe Marshall

          #409
          Re: Python from Wise Guy's Viewpoint

          Joachim Durchholz <joachim.durchh olz@web.de> writes:
          [color=blue]
          > Espen Vestre wrote:[color=green]
          >> Now you're conflating two readings of "want declarations" (i.e. "want
          >> them whenever they're convenient for optimizing" vs. "want them
          >> everywhere and always")[/color]
          >
          > Type inference is about "as much static checking as possible with as
          > little annotations as absolutely necessary".
          > HM typing is extremely far on the "few declarations" side: a handful
          > even in large systems.[/color]

          I certainly don't mind as much static checking as possible. I get a
          little put off by *any* annotations that are *absolutely necessary*,
          though. My preference is that all `lexically correct' code be
          compilable, even if the object code ends up being the single
          instruction `jmp error-handler'. Of course I'd like to get a
          compilation warning in this case.
          [color=blue]
          >
          > It sounds unbelievable, but it really works.
          >[/color]

          I believe you. I have trouble swallowing claims like `It is never
          wrong, always completes, and the resulting code never has a run-time
          error.' or `You will never need to run the kind of code it doesn't allow.'

          Comment

          • Joe Marshall

            #410
            Re: Python from Wise Guy's Viewpoint

            Pascal Costanza <costanza@web.d e> writes:
            [color=blue]
            > No, for christ's sake! There are dynamically typed programs that you
            > cannot translate into statically typed ones![/color]

            You are really going to confuse the static typers here. Certainly
            there is no program expressable in a dynamically typed language such
            as Lisp that is not also expressible in a statically typed language
            such as SML.

            But it *is* the case that there are programs for which safe execution
            *must* depend upon checks (type checks or pattern matching) that are
            performed at run time. Static analysis will not remove the need for
            these.

            It is *also* the case that there are programs for which safe execution
            requires *no* runtime checking, yet static analysis cannot prove that
            this is the case.

            A static analyzer that neither inserts the necessary run-time checks,
            nor requires the user to do so will either fail to compile some correct
            programs, or fail to correctly compile some programs.

            I think the static typers will be agree (but probably not be happy
            with) this statement: There exist programs that may dynamically admit
            a correct solution for which static analyzers are unable to prove that
            a correct solution exists.

            Comment

            • Pascal Costanza

              #411
              Re: Python from Wise Guy's Viewpoint

              Joe Marshall wrote:[color=blue]
              > Pascal Costanza <costanza@web.d e> writes:
              >[color=green]
              >>No, for christ's sake! There are dynamically typed programs that you
              >>cannot translate into statically typed ones![/color]
              >
              > You are really going to confuse the static typers here. Certainly
              > there is no program expressable in a dynamically typed language such
              > as Lisp that is not also expressible in a statically typed language
              > such as SML.[/color]

              Yes, of course. Bad wording on my side.

              Thanks for clarification.

              I am not interested in Turing equivalence in the static vs. dynamic
              typing debate. It's taken for granted that for every program written in
              either kind of language you can write an equivalent program in the other
              kind of language. I seriously don't intend to suggest that dynamically
              typed languages beat Turing computability. ;-)

              I am not interested in _what_ programs can be implemented, but in _how_
              programs can be implemented.


              Pascal

              --
              Pascal Costanza University of Bonn
              mailto:costanza @web.de Institute of Computer Science III
              http://www.pascalcostanza.de Römerstr. 164, D-53117 Bonn (Germany)

              Comment

              • John Thingstad

                #412
                Re: Python from Wise Guy's Viewpoint

                On Wed, 29 Oct 2003 19:53:12 +0100, Pascal Costanza <costanza@web.d e>
                wrote:
                [color=blue]
                > Joe Marshall wrote:[color=green]
                >> Pascal Costanza <costanza@web.d e> writes:
                >>[/color]
                >
                > Pascal
                >[/color]

                Do you ever do any real work? Or do you spend all your time constructing
                replies here...

                --
                Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

                Comment

                • Garry Hodgson

                  #413
                  Re: Re: Python from Wise Guy's Viewpoint

                  raffael@mediaon e.net (Raffael Cavallaro) wrote:
                  [color=blue]
                  > With lisp, you only add as much type checking as you need, *when* you
                  > need it.[/color]

                  if you knew how much you needed and when, you wouldn't need it.

                  ----
                  Garry Hodgson, Technology Consultant, AT&T Labs

                  Be happy for this moment.
                  This moment is your life.

                  Comment

                  • Kees van Reeuwijk

                    #414
                    Re: Python from Wise Guy's Viewpoint

                    Raffael Cavallaro <raffael@mediao ne.net> wrote:
                    [color=blue]
                    > Matthias Blume <find@my.addres s.elsewhere> wrote in message
                    > news:<m11xsx2ai u.fsf@tti5.uchi cago.edu>...[color=green]
                    > > [This whole discussion is entirely due to a mismatch of our notions of
                    > > what constitutes expressive power.][/color]
                    >
                    > No, it is due to your desire to be type constrained inappropriately
                    > early in the development process. Lispers know that early on,[/color]

                    That's very arrogant. You presume to know what is appropriate for *him*.

                    And I could retort with ``Static typers know that even early in the
                    development process it is appropriate to have the additional safety that
                    static typing brings to a program'', but I won't, since I'm not that
                    arrogant :-).

                    Comment

                    • Henrik Motakef

                      #415
                      Re: Python from Wise Guy's Viewpoint

                      John Thingstad <john.thingstad @chello.no> writes:
                      [color=blue]
                      > Do you ever do any real work? Or do you spend all your time
                      > constructing replies here...[/color]

                      Proves the point about programmer efficiency, eh? Use Lisp, and you
                      too can spend most of the day posting to Usenet! ;-)

                      Comment

                      • Dirk Thierbach

                        #416
                        Re: Python from Wise Guy's Viewpoint

                        Joe Marshall <jrm@ccs.neu.ed u> wrote:[color=blue]
                        > I have trouble swallowing claims like `It is never wrong, always
                        > completes, and the resulting code never has a run-time error.'[/color]

                        I can understand this. Fortunately, you don't have to swallow it,
                        you can verify it for yourself.

                        A comprehensive book about the lambda calculus should contain the
                        Hindley-Mindler type inference algorithm. The HM-algorithm is fairly
                        old, so I don't know if there are any papers on the web that explain
                        it in detail. Unification of types and the HM-algorithm together are
                        maybe two pages.

                        * Termination is easy to verify; the algorithm works by induction on
                        the structure of the lambda term.

                        * For "it never has a runtime error" look at the typing rules of
                        the lambda calculus and convince yourself that they express the
                        invariant that any function (including "constants" , i.e. built-in
                        functions) will only be applied to types that match its own
                        type signature. Hence, no runtime errors.

                        A good book will also contain the proof (or a sketch of it) that
                        if the HM-algorithm succeeds, the term in question can indeed by
                        typed by the typing rules.

                        * For the other case (i.e., there is a mismatch during unification; I
                        guess that's what you mean by "it is never wrong"), try to assign to
                        every variable a value of the type under the current type
                        environment, and reduce along every possible reduction path of the
                        subterm. One of those reductions will fail with a type error (though
                        this reduction may never happen if execution never reaches this part
                        of the subterm on the path that the evaluation strategy of your
                        language chooses).

                        Maybe it's best to do this for a few examples.
                        [color=blue]
                        > or `You will never need to run the kind of code it doesn't allow.'[/color]

                        The last point should show that such a code at least is problematic,
                        unless you can somehow make sure that the part that contains the
                        type error is never executed. In that case, this part is useless,
                        so the code should be probably rewritten. At least I cannot think
                        of a good reason why you would "need" such kind of code.

                        - Dirk

                        Comment

                        • Dave Brueck

                          #417
                          Re: Python from Wise Guy's Viewpoint

                          > > Do you ever do any real work? Or do you spend all your time[color=blue][color=green]
                          > > constructing replies here...[/color]
                          >
                          > Proves the point about programmer efficiency, eh? Use Lisp, and you
                          > too can spend most of the day posting to Usenet! ;-)[/color]

                          Because you're unemployed? ;-)

                          Comment

                          • Joachim Durchholz

                            #418
                            Re: Python from Wise Guy's Viewpoint

                            Joe Marshall wrote:
                            [color=blue]
                            > Joachim Durchholz <joachim.durchh olz@web.de> writes:
                            >[color=green]
                            >>Type inference is about "as much static checking as possible with as
                            >>little annotations as absolutely necessary".
                            >>HM typing is extremely far on the "few declarations" side: a handful
                            >>even in large systems.[/color]
                            >
                            > I certainly don't mind as much static checking as possible. I get a
                            > little put off by *any* annotations that are *absolutely necessary*,
                            > though. My preference is that all `lexically correct' code be
                            > compilable, even if the object code ends up being the single
                            > instruction `jmp error-handler'. Of course I'd like to get a
                            > compilation warning in this case.[/color]

                            Then static typing is probably not for you.
                            Mainstream FPLs tend to require an occasional type declaration. And
                            you'll have to know about the type machinery to interpret the type
                            errors that the compiler is going to spit at you - never mind that these
                            errors will always indicate a bug (unless one of those rare explicit
                            type annotations is involved, in which case it could be a bug or a
                            defective type annotation).
                            [color=blue][color=green]
                            >>It sounds unbelievable, but it really works.[/color]
                            >
                            > I believe you. I have trouble swallowing claims like `It is never
                            > wrong, always completes, and the resulting code never has a run-time
                            > error.' or `You will never need to run the kind of code it doesn't allow.'[/color]

                            This kind of claim comes is usually just a misunderstandin g.
                            For example, the above claim indeed holds for HM typing - for the right
                            definitions of "never wrong" and "never has an error".

                            HM typing "is never wrong and never has a run-time error" in the
                            following sense: the algorithm will never allow an ill-typed program to
                            pass, and there will never be a type error at run-time. However, people
                            tend to overlook the "type" bit in the "type error" term, at which point
                            the discussion quickly degenerates into discourses of general correctness.
                            Adding to the confusion is the often-reported experience of functional
                            programmers, that annotating your code with static type declarations can
                            be a very efficient way to finding design errors soon.
                            The type correctness claims are backed by hard theory; the design
                            improvement claims are of a social nature and cannot be proved (they
                            could be verified by field studies at best).

                            Comment

                            • Stephen J. Bevan

                              #419
                              Re: Python from Wise Guy's Viewpoint

                              Pascal Costanza <costanza@web.d e> writes:[color=blue]
                              > These are both all or nothing solutions.
                              >
                              > + "all the tests for a particular feature in one place" - maybe that's
                              > not what I want (and you have ignored my arguments in this regard)
                              >
                              > and:
                              > + what if I want to run _some_ of the tests that my macro produces but
                              > not _all_ of them?
                              >
                              >
                              > Actually, that seems to be the typical reaction of static typing
                              > fans.[/color]

                              The solutions may be all or nothing but IMHO they are simple and I
                              like simple things. I can't really say the same for scenarios which
                              involve running only some tests generated by macros that may or may
                              not be in the same files as other tests generated from the same
                              macros. Perhaps it all comes down to different approaches to the
                              programming process rather than languages per se, e.g. I don't do
                              either of the above even when writing Common Lisp.

                              Comment

                              • Stephen J. Bevan

                                #420
                                Re: Python from Wise Guy's Viewpoint

                                Pascal Costanza <costanza@web.d e> writes:[color=blue]
                                > Are these algorithms reason enough to have machine word sized
                                > numerical data types as the default for a _general purpose_ language?[/color]

                                I've no idea, I don't care that much what the default is since I
                                prefer to specify what the type/size should be if the compiler fails
                                to infer the one I wanted :-)

                                Comment

                                Working...