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

Xuanyi Chew: Exploring The Philosophy of Programming

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

6b880a0b67fac54c42c77fe70d97334d?s=128

New Zealand Python User Group

September 13, 2014
Tweet

Transcript

  1. @chewxy Exploring The Philosophy of Programming Xuanyi Chew @chewxy

  2. @chewxy if  __name__  ==  '__main__':  

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

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

  5. @chewxy The Question Why is programming called programming?

  6. @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
  7. @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ø‹  
  8. @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  
  9. @chewxy Rearranged 0x55              

                0x48  0x89  0xe5   0x89  0x7d  0xfc   0x89  0x75  0xf8   0x8b  0x45  0xf8   0x8b  0x55  0xfc   0x01  0xd0   0x5d   0xc3  
  10. @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
  11. @chewxy

  12. @chewxy showSection('Types')  

  13. @chewxy Python is… v  Strongly typed v  Dynamically typed v 

    Duckly typed
  14. @chewxy

  15. @chewxy Duck Typing class  Duck():    def  Quack(self):    

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

       duck.Quack()      duck.Dies()    except  AttributeError:      print("Not  a  duck.  Shot  anyway")  
  17. @chewxy Java Interface interface  Duck  {    string  Quack();  

     void  Dies();   }  
  18. @chewxy Java Interface public  class  Bird  implements  Duck  {  

     …    public  string  Quack()  {  …  }   }     public  class  Human  implements  Duck  {    …    public  string  Quack()  {  …  }   }  
  19. @chewxy Dick Cheney (Java) public  void  Shoot(Duck  duck)  {  

     duck.Quack();    duck.Dies();   }  
  20. @chewxy Duck Typing v  Method lookup/resolution at runtime v  Interfaces

    are statically defined, and known at compile time
  21. @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)   }  
  22. @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!
  23. @chewxy Manifestly Typed v  Dart is optionally typed v  Manifestly

    typed v  Major confusion in the populace with static typing
  24. @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
  25. @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 ✘
  26. @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
  27. @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  
  28. @chewxy What are x and y's type? def  add(x,  y):

       return  x  +  y     function  add(x,  y)  {    return  x  +  y;   }    
  29. @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 }
  30. @chewxy Unitype v  PyObject represents any value in Python

  31. @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?
  32. @chewxy Consider Implicit Coercions function  add(x,  y)  {    return

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

     +  y     >>>  add(1,  "hello  world")   TypeError:  unsupported  operand  type(s)  for  +:  'int'   and  'str'  
  34. @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  
  35. @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!
  36. @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>    
  37. @chewxy Subverting the type system v  What about builtins? v 

    Use ctypes.POINTER, ctypes.cast  
  38. @chewxy Strong Typing v  Not at all useful v  No

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

    program, including the types v  Dynamic semantics is about how to execute a program
  40. @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)
  41. @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)
  42. @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
  43. @chewxy showSection('Names')  

  44. @chewxy The Chinese Python Snippet class  塔类(list):      

       def  __init__(塔⾃自⼰己,  座标x,  塔⾊色=红):                  塔⾃自⼰己.座标x  =  座标x                  塔⾃自⼰己.⾊色=  塔⾊色  显⻳龟()                  笔⾊色(塔⾊色)                  笔⼤大⼩小(5)                  提笔();  前往(座标x,  -­‐150)                  下笔();  前往(座标x,  +150)                  提笔()                  藏⻳龟()     https://github.com/renyuanL/pythonTurtleInChinese/
  45. @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()    
  46. @chewxy

  47. @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
  48. @chewxy Symbolic Systems v  Example of names: pushq  %rbp  

    movq    %rsp  %rbp     x  =  foo(1,2,3,4)   y  =  x  +  x   id(1)  
  49. @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∘.⌽⊂⍵}    
  50. @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   }  
  51. @chewxy Statically represented as:

  52. @chewxy Into this, a representation of the dynamic state of

    the program
  53. @chewxy showSection('UX')  

  54. @chewxy User Experience v  It's the MAJOR reason why I

    use Python daily v  Python as a language has a fantastic UX
  55. @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,,,  
  56. @chewxy User Experience – SURPRISE! v  FORTRAN has optionally explicit

    type declarations
  57. @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!
  58. @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?
  59. @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?
  60. @chewxy User Experience – Development Cycle v  How quickly can

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

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

    Surprises ~  Feedback ü  Cognitive load ~  Development cycle
  63. @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
  64. @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
  65. @chewxy print('Thank  You')   return   follow me on Twitter:

    @chewxy