Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Xuanyi Chew: Exploring The Philosophy of Progra...

Xuanyi Chew: Exploring The Philosophy of Programming

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Xuanyi Chew:
Exploring The Philosophy of Programming
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
@ Kiwi PyCon 2014 - Saturday, 13 Sep 2014 - Track 1
http://kiwi.pycon.org/

**Audience level**

Intermediate

**Description**

One man's journey to internalizing the phrase "computer programs are just bits". It's merely a phrase, but one that can be deeply appreciated if time is taken to appreciate it. Talk will roughly be about bytes, types, computation theory, language design, and some life stories.

**Abstract**

It all started with a bunch of Python 3 code written in another language. What followed were explorations as to what it really means to be programming. Why are some languages easier to learn (Python)? Why is it easier to make mistakes in some languages (JavaScript)? And why are some languages totally alien (APL and its family)?

What role does familiarity play in being productive in a language? Why are there different paradigms in programming? What about the correctness of programs? Is one paradigm more correct than others? This talk explores the history of computer science, programming and the potential for the human race.

Ultimately, this talk tries to answer the question: is programming for humans or for machines? And what role Python plays in it. The question on the future is left to the audience to decide.

**YouTube**

https://www.youtube.com/watch?v=hUP27HAtSOc

New Zealand Python User Group

September 13, 2014
Tweet

More Decks by New Zealand Python User Group

Other Decks in Programming

Transcript

  1. @chewxy Let’s Learn Chinese class  塔类(list):        

     def  __init__(塔⾃自⼰己,  座标x,  塔⾊色=红):                  塔⾃自⼰己.座标x  =  座标x                  塔⾃自⼰己.⾊色=  塔⾊色    显⻳龟()                  笔⾊色(塔⾊色)                  笔⼤大⼩小(5)                  提笔();  前往(座标x,  -­‐150)                  下笔();  前往(座标x,  +150)                  提笔()                  藏⻳龟()     https://github.com/renyuanL/pythonTurtleInChinese/
  2. @chewxy What Are These Numbers? 85, 72, 137, 229, 137,

    125, 252, 137, 117, 248, 139, 69, 248, 139, 85, 252, 1, 208, 93, 195
  3. @chewxy As String (Array of Chars) 85, 72, 137, 229,

    137, 125, 252, 137, 117, 248, 139, 69, 248, 139, 85, 252, 1, 208, 93, 195     >>>  ''.join([chr(x)  for  x  in  listOfNumbers])   UH‰å‰}ü‰uø‹Eø‹  
  4. @chewxy As Byte Representation 85, 72, 137, 229, 137, 125,

    252, 137, 117, 248, 139, 69, 248, 139, 85, 252, 1, 208, 93, 195   >>>  '  '.join([hex(j)  for  j  in  x])   0x55  0x48  0x89  0xe5  0x89  0x7d  0xfc  0x89  0x75  0xf8   0x8b  0x45  0xf8  0x8b  0x55  0xfc  0x01  0xd0  0x5d  0xc3  
  5. @chewxy Rearranged 0x55              

                0x48  0x89  0xe5   0x89  0x7d  0xfc   0x89  0x75  0xf8   0x8b  0x45  0xf8   0x8b  0x55  0xfc   0x01  0xd0   0x5d   0xc3  
  6. @chewxy Rearranged, Labelled 0x55            

                    0x48  0x89  0xe5   0x89  0x7d  0xfc   0x89  0x75  0xf8   0x8b  0x45  0xf8     0x8b  0x55  0xfc     0x01  0xd0                 0x5d   0xc3   pushq      %rbp   movq        %rsp,  %rbp   movl        %edi,  -­‐4(%rbp)   movl        %esi,  -­‐8(%rbp)   movl        -­‐8(%rbp),  %eax   movl        -­‐4(%rbp),  %edx   addl        %edx,  %eax   popq        %rbp   ret
  7. @chewxy Duck Typing class  Duck():    def  Quack(self):    

     …    def  Dies(self):      …   class  HumanBean():    def  Quack(self):      …    def  Dies(self):      …  
  8. @chewxy Dick Cheney (Python) def  shoot(self,  duck):    try:  

       duck.Quack()      duck.Dies()    except  AttributeError:      print("Not  a  duck.  Shot  anyway")  
  9. @chewxy Java Interface public  class  Bird  implements  Duck  {  

     …    public  string  Quack()  {  …  }   }     public  class  Human  implements  Duck  {    …    public  string  Quack()  {  …  }   }  
  10. @chewxy Not (necessarily) Static Typing class  Vector  {    final

     num  x,  y;    Vector(this.x,  this.y);    Vector  add(Vector  anotherVector)  {      return  new  Vector(x  +  another.x,  y  +  another.y);    }   }   foo()  {    Vector  v1  =  new  Vector(0,  0)    Vector  v2  =  new  Vector(1,  1)    int  n  =  v1.add(v2)   }  
  11. @chewxy Not (necessarily) Static Typing class  Vector  {    final

     num  x,  y;    Vector(this.x,  this.y);    Vector  add(Vector  anotherVector)  {      return  new  Vector(x  +  another.x,  y  +  another.y);    }   }   foo()  {    Vector  v1  =  new  Vector(0,  0)    Vector  v2  =  new  Vector(1,  1)    int  n  =  v1.add(v2)   }   Different types!
  12. @chewxy Manifestly Typed v  Dart is optionally typed v  Manifestly

    typed v  Major confusion in the populace with static typing
  13. @chewxy Static vs Dynamic Typing v  Static typing = type

    of variables are known at compile time v  Dynamic typing = types are only known/discovered at runtime
  14. @chewxy Static vs Dynamic Typing v  Static typing = type

    of variables are known at compile time v  Dynamic typing = types are only known/discovered at runtime ✘
  15. @chewxy Static vs Dynamic Semantics v  Static semantics specifies the

    program, including the types v  Dynamic semantics is about what the program does when it is executed
  16. @chewxy The Halting Problem Can you tell just by reading,

    what inputs will cause the following to run forever? def  foo(x):    try:      return  1/x    except:        while  True:        pass  
  17. @chewxy What are x and y's type? def  add(x,  y):

       return  x  +  y     function  add(x,  y)  {    return  x  +  y;   }    
  18. @chewxy Introducing the Unitype def  add(x,  y):    return  x

     +  y     function  add(x,  y)  {    return  x  +  y;   }     v  x, y ∈ { all types in Python}* v  x, y ∈ { all types in JavaScript }
  19. @chewxy Strong Typing v  WTF is strong typing? v  Two

    general ideas about the "strength" of typing: v  How much of the type system can be subverted? v  How restrictive the type system is to implicit coercions?
  20. @chewxy Consider Implicit Coercions function  add(x,  y)  {    return

     x  +  y   }   >>>  add(1,  "hello  world")   "1helloworld"  
  21. @chewxy Consider Implicit Coercions def  add(x,  y):    return  x

     +  y     >>>  add(1,  "hello  world")   TypeError:  unsupported  operand  type(s)  for  +:  'int'   and  'str'  
  22. @chewxy So Python's strongly typed, right? >>>  x  =  1

      >>>  y  =  2.0   >>>  type(x)   <type  'int'>   >>>  type(y)   <type  'float'>   >>>  add(x,  y)   3.0  
  23. @chewxy So Python's kinda strongly typed >>>  x  =  1

      >>>  y  =  2.0   >>>  type(x)   <type  'int'>   >>>  type(y)   <type  'float'>   >>>  add(x,  y)   3.0   DIFFERENT TYPES! BUT WORKS!
  24. @chewxy Subverting the type system class  Human(object):    def  shoot(self,

     duck):      print("Shot  %s"  %  duck)   class  Duck(object):    def  quack(self):      print("quack")   >>>  donald,  howard  =  Duck(),  Duck()   >>>  donald.__class__  =  Human().__class__   >>>  donald.shoot(howard)   Shot  <__main__.Duck  object  at  0x1099c62d0>    
  25. @chewxy Strong Typing v  Not at all useful v  No

    consensus on what strong/weak typing is v  Also commonly known as type safety
  26. @chewxy Static vs Dynamic Semantics v  Static semantics specifies the

    program, including the types v  Dynamic semantics is about how to execute a program
  27. @chewxy Type Safety v  Type safe implies that the static

    meaning of the code is more or less the same as the dynamic meaning of the code v  Context sensitive – how you analyze your program matters "Well typed programs cannot go wrong" - Robin Milner (1978)
  28. @chewxy Type Systems Help Programmers "A type system is a

    tractable syntactic method for proving the absence of certain program behaviors by classifying phrases according to the kinds of values they compute." - Ben Pierce (Types and Programming Languages)
  29. @chewxy Types Form User Experiences v  Manifestly typed languages supposedly

    improves code understanding [citation needed] v  Types help us reason about the programs we're writing
  30. @chewxy The Chinese Python Snippet class  塔类(list):      

       def  __init__(塔⾃自⼰己,  座标x,  塔⾊色=红):                  塔⾃自⼰己.座标x  =  座标x                  塔⾃自⼰己.⾊色=  塔⾊色  显⻳龟()                  笔⾊色(塔⾊色)                  笔⼤大⼩小(5)                  提笔();  前往(座标x,  -­‐150)                  下笔();  前往(座标x,  +150)                  提笔()                  藏⻳龟()     https://github.com/renyuanL/pythonTurtleInChinese/
  31. @chewxy Google Translated class  塔类(list):          def

     __init__(塔⾃自⼰己,  座标x,  塔⾊色=  红):    塔⾃自⼰己.座标x  =  座标x    塔⾃自⼰己.⾊色=  塔⾊色    显⻳龟()    笔⾊色(塔⾊色)    笔⼤大⼩小(5)    提笔();  前往(座标x,  -­‐150)    下笔();  前往(座标x,  +150)    提笔()    藏⻳龟()   class  towerClass  (list):              def  __init  __  (tower,  coordinateX,  towerColor=red):                      tower.CoordinatesX  =  coordinateX                      tower.Color  =  towerColor                      displayTurtle()                      penColor(towerColor)                      penSize(5)                      liftPen();  go(coordinateX,  -­‐150)                      write();  go(coordinatesX,  +150)                      liftPen()                      hideTurtle()    
  32. @chewxy Symbolic Systems v  Programmers work in symbolic systems. v 

    Naming things is one of the fundamental things programmers do "There are only 2 hard problems in computer science: cache invalidation, naming things, and off-by-one errors" - Phil Karlton
  33. @chewxy Symbolic Systems v  Example of names: pushq  %rbp  

    movq    %rsp  %rbp     x  =  foo(1,2,3,4)   y  =  x  +  x   id(1)  
  34. @chewxy Symbolic Systems Spectrum: BANCStar: 8607,,,1   11547,15475,22002,22002   1316,1629,1,1649

      APL: life←{↑1  ⍵∨.∧3  4=+/,¯ˉ1  0  1∘.⊖¯ˉ1  0  1∘.⌽⊂⍵}    
  35. @chewxy Names Give Meaning To Programs Takes this: function  foo(y)

     {    var  z  =  2;    for  (x  =  0;  x  <  10;  x++)  {      y  +=  x      y  -­‐=  z    }    return  y   }  
  36. @chewxy User Experience v  It's the MAJOR reason why I

    use Python daily v  Python as a language has a fantastic UX
  37. @chewxy User Experience – Ease of Use v  Would you

    rather read/write code that looks like this?   8607,,,1   11547,15475,22002,22002   1316,1629,1,1649   3001,1316,3,30078   11528,22052,22002,22002   9301,0,1528,1528   31568,10001,800,107   8560,,,1568   8550,210,,   3001,,,  
  38. @chewxy v  FORTRAN has optionally explicit type declarations v  If

    your variable name starts with I,  J,  K,  L,  M, it is a Integer type. so compiler wow much clever hueristics plz User Experience – SURPRISE!
  39. @chewxy User Experience - Feedback v  Would you work with

    a programming language that doesn't tell you that your program has gone wrong? On  Error  Resume  Next   v  Wouldn't it be nice if the program could be checked for errors before it's run?
  40. @chewxy User Experience – Cognitive Load v  How much of

    the language is in your mind vs how much of the program is in your mind when coding?
  41. @chewxy User Experience – Development Cycle v  How quickly can

    you write a program? v  How quickly can you reason around a big program?
  42. @chewxy User Experience Considerations v  Ease of use v  Surprises

    v  Feedback v  Cognitive load v  Development cycle
  43. @chewxy What Python Does Right ü  Ease of use ü 

    Surprises ~  Feedback ü  Cognitive load ~  Development cycle
  44. @chewxy Programme == Program programme (/ˈprəʊgram/). n. 1. a planned

    series of future events or performances. Ÿ programme v. 1. arrange according to plan or schedule
  45. @chewxy Programmers Have Superpowers v  Names gives programs meaning v 

    Types help us reason about the programs we create v  We create our own meaning in life