SlideShare uma empresa Scribd logo
1 de 20
Baixar para ler offline
Yet another object
                          system for R
                          Hadley Wickham
                           Rice University
Friday, 31 July 2009
Does R need another
                     object system?


Friday, 31 July 2009
Are S3 and S4
                          enough?


Friday, 31 July 2009
“Because it’s there”
                                                             —George Mallory




CC BY: http://www.flickr.com/photos/mckaysavage/497617014

Friday, 31 July 2009
1. Paradigms of programming
                 2. Existing systems in R
                 3. Prototype based programming
                 4. Examples
                 5. Scoping
                 6. Uses


Friday, 31 July 2009
Descriptive
                                              declarative
                                             programming
                                                     XML,
Data structures only                                 S!expression
 Turing complete                                     + procedure
                                                                                                                                                             + cell (state)
                                              First!order                                                                                                                           Imperative
  Observable                                   functional                                                                                                                          programming
nondeterminism? Yes No                       programming                                                                                      Guarded
                                                                                                                                             command      + nondet. choice                 Pascal, C
                                                     + closure                                                                             programming              Imperative
                                              Functional                                                                                 Dijkstra’s GCL               search
                                                                                                                                                                   programming     + search
                                             programming
            + unification                              Scheme, ML                               + name                                                    SNOBOL, Icon, Prolog
              (equality)                                                                          (unforgeable constant)
    Deterministic                                                  + continuation                                                                                              + cell
                                                                                             ADT              + cell         ADT                          + port
 logic programming                                           Continuation                  functional                     imperative                                          (state)      + closure
                                                             programming                                                                                   (channel)
                                                                                         programming                     programming                                               Sequential
            + search                                     Scheme, ML                                                                                 Event!loop                   object!oriented
                                                                                 Haskell, ML, E              CLU, OCaml, Oz                        programming
  Relational & logic        + by!need                           + thread                                                                                                          programming
    programming              synchron.                          + single assign.          + nondeterministic        + port                                E in one vat               Stateful
            Prolog, SQL              Lazy                  Monotonic                        choice                   (channel)                            + thread                  functional
            embeddings             functional                  dataflow                 Nonmonotonic                     Multi!agent                                              programming
                                                                                                                                                   Multi!agent
            + solver             programming                 programming                  dataflow                         dataflow                programming                            Java, OCam
  Constraint (logic)                     Haskell              Declarative               programming                      programming                                                      + thread
                                                                                                                                                 Message!passing
   programming                                                concurrent               Concurrent logic                Oz, Alice, AKL              concurrent                      Concurrent
            CLP, ILOG Solver                                 programming                programming                                               programming                    object!oriented
            + thread                                               Pipes, MapReduce             Oz, Alice, Curry, Excel,                                  Erlang, AKL             programming
                                       + thread       + by!need                                 AKL, FGHC, FCP                                                                    Shared!state
     Concurrent             + single assignment        synchronization                                                                                    + local cell
      constraint                                                                                + synch. on partial termination                                                    concurrent
    programming                                  Lazy                                 Functional reactive                                          Active object                  programming
                                               dataflow                               programming (FRP)                                            programming                            Smalltalk,
            LIFE, AKL                        programming
                                                                                    Continuous synchronous                                       Object!capability                        Java, Alice
            + by!need synchronization            Lazy                                                                                             programming
                                                                                         programming                                                                                      + log
   Lazy concurrent                            declarative
      constraint                              concurrent                                        FrTime, Yampa                                     CSP, Occam,                       Software
    programming                              programming                                        + clocked computation                             E, Oz, Alice,                   transactional
                                                                                                                                                publish/subscribe,               memory (STM)
 Oz, Alice, Curry                         Oz, Alice, Curry                           Discrete synchronous                                      tuple space (Linda)
                                                                                        programming                                                                           SQL embeddings
     Logic and                                                                      Esterel, Lustre, Signal             Dataflow and
     constraints                                Functional                                                             message passing            Message passing                  Shared state

                                                                                       Nondet. state
            Unnamed state (seq. or conc.)                                                                                                         Named state
Less                                                                                                                                                                                  More
Friday, 31 July 2009                                                                                                                                                 Expressiveness of state
Programming Paradigms for Dummies:
                       What Every Programmer Should Know.
                       Peter Van Roy.
                       http://www.info.ucl.ac.be/~pvr/VanRoyChapter.pdf

                       P. van Roy and S. Haridi. Concepts,
                       Techniques and Models of Computer
                       Programming. The MIT Press, 2004.




Friday, 31 July 2009
S3 / S4             Alternative


                          Immutable              Mutable
                         (pass-by-value)     (pass-by-reference)




                       Generic functions     Message passing
                       (function based OO)    (class based OO)




Friday, 31 July 2009
Mutable         Immutable
       Required by many of         Hard to derive
      most efficient algorithms computational complexity

         Simplifies dependence
                                    Must “thread” state
         between components


                 Concurrency hard    Concurrency easy


                                    Compilers can make
           Hard to reason about
                                       very efficient

Friday, 31 July 2009
Aside: memory efficiency
                       “A persistent data structure is a data structure
                       which always preserves the previous version of
                       itself when it is modified.”
                       “While persistence can be achieved by simple
                       copying, this is inefficient in time and space,
                       because most operations make only small changes
                       to a data structure. A better method is to exploit
                       the similarity between the new and old versions to
                       share structure between them, such as using the
                       same subtree in a number of tree structures.”

                       http://en.wikipedia.org/wiki/Persistent_data_structure
Friday, 31 July 2009
Existing systems
                       S3/S4: immutable + generic functions
                       R.oo : mutable + generic functions
                       OOP: mutable + message passing
                       Proto: mutable + message passing
                       YAROS: mutable + message passing



Friday, 31 July 2009
Prototype based
                             programming
                       Generalisation of class-based oo (like
                       Java) that removes the distinction
                       between classes and instances.
                       Single dispatch, but often implement
                       multiple (& dynamic) inheritance
                       Notable languages: Javascript, Io



Friday, 31 July 2009
Counter <- Object$clone()$do({
         init <- function() self$counter <- 0
         count <- function() {
           self$counter <- self$counter + 1
           self$counter
         }
      })

      counter_a <- Counter$clone()
      counter_b <- Counter$clone()

      counter_a$count()
      counter_a$count()
      counter_b$count()

Friday, 31 July 2009
Account <- Object$clone()$do({
         balance <- 0.0
         deposit <- function(v) self$balance <- self$balance + v
         withdraw <- function(v) self$balance <- self$balance - v
         show <- function() cat("Account balance: $", self$balance, "n")
         init <- function() self$balance <- 0
      })

      Account$show()
      cat("Depositing $10n")
      Account$deposit(10.0)
      Account$show()

      Savings <- Account$clone()$do({
         interest <- 0.05
         withdraw <- NULL
      })
      Savings$show()

Friday, 31 July 2009
Observable <- Object$clone()$do({
        listeners <- list()
        add_listener <- function(f) {
          self$listeners <- c(self$listeners, f)
        }

            signal <- function(...) {
              for(l in self$listeners) l(...)
            }

            init <- function() {
              self$listeners <- list()
            }
      })

      counter_a$append_proto(Observable$clone())

Friday, 31 July 2009
Advantages
                       Clean separation of object methods and
                       base functions.
                       No extra function parameters.
                       Rich behaviour, including introspection,
                       based on io.
                       Mutable, multiple inheritance
                       (depth-first search of inheritance graph).


Friday, 31 July 2009
Scoping

                       self$method() needs to have object
                       scoping, not lexical scoping.
                       That is, rather than looking up based on
                       environment at time function was defined,
                       look it up based on current object.




Friday, 31 July 2009
"$.io" <- function(x, i, ...) {
        res <- core(x)$get_local_slot(i)
        object_scope(res, x)
      }

      object_scope <- function(res, self) {
        # Add environment to top of stack that contains
        # the self object
        if (is.function(res)) {
          env <- new.env()
          env$self <- self
          parent.env(env) <- environment(res)
          environment(res) <- env
        }
        res
      }


Friday, 31 July 2009
Uses
                       Can simplify code which requires
                       coordinating data from multiple locations:
                       scale code in ggplot2
                       Complex simulations: e.g. card counting
                       in black jack
                       When there really is one true underlying
                       object: GUIs and interactive graphics


Friday, 31 July 2009
Conclusions
                       S3/S4 don’t fulfil every need, and it’s fun to
                       experiment with alternative paradigms.
                       Prototype-based OO is an interesting idea
                       that the distinction between inheritance
                       and instantiation.
                       We can actually implement it in R, with
                       different scoping rules. Has been a great
                       learning experience.


Friday, 31 July 2009

Mais conteúdo relacionado

Semelhante a Yet another object system for R

MOST (Newsfromthefront 2010)
MOST (Newsfromthefront 2010)MOST (Newsfromthefront 2010)
MOST (Newsfromthefront 2010)STI International
 
Dimitry Solovyov - The imminent threat of functional programming
Dimitry Solovyov - The imminent threat of functional programmingDimitry Solovyov - The imminent threat of functional programming
Dimitry Solovyov - The imminent threat of functional programmingDmitry Buzdin
 
GTC 2012: NVIDIA OpenGL in 2012
GTC 2012: NVIDIA OpenGL in 2012GTC 2012: NVIDIA OpenGL in 2012
GTC 2012: NVIDIA OpenGL in 2012Mark Kilgard
 
Hadoop World 2011: Proven Tools to Manage Hadoop Environments - Joey Jablonsk...
Hadoop World 2011: Proven Tools to Manage Hadoop Environments - Joey Jablonsk...Hadoop World 2011: Proven Tools to Manage Hadoop Environments - Joey Jablonsk...
Hadoop World 2011: Proven Tools to Manage Hadoop Environments - Joey Jablonsk...Cloudera, Inc.
 
Distributed ML with Dask and Kubernetes
Distributed ML with Dask and KubernetesDistributed ML with Dask and Kubernetes
Distributed ML with Dask and KubernetesRay Hilton
 
Generating Assertion Code from OCL: A Transformational Approach Based on Simi...
Generating Assertion Code from OCL: A Transformational Approach Based on Simi...Generating Assertion Code from OCL: A Transformational Approach Based on Simi...
Generating Assertion Code from OCL: A Transformational Approach Based on Simi...Shinpei Hayashi
 
SIGGRAPH Asia 2012 Exhibitor Talk: OpenGL 4.3 and Beyond
SIGGRAPH Asia 2012 Exhibitor Talk: OpenGL 4.3 and BeyondSIGGRAPH Asia 2012 Exhibitor Talk: OpenGL 4.3 and Beyond
SIGGRAPH Asia 2012 Exhibitor Talk: OpenGL 4.3 and BeyondMark Kilgard
 
Functional Domain Modeling
Functional Domain ModelingFunctional Domain Modeling
Functional Domain ModelingMichal Bigos
 
2010 10 provxg_datagovuk
2010 10 provxg_datagovuk2010 10 provxg_datagovuk
2010 10 provxg_datagovukJun Zhao
 
Introduction to ggplot2
Introduction to ggplot2Introduction to ggplot2
Introduction to ggplot2maikroeder
 
SIGGRAPH 2012: NVIDIA OpenGL for 2012
SIGGRAPH 2012: NVIDIA OpenGL for 2012SIGGRAPH 2012: NVIDIA OpenGL for 2012
SIGGRAPH 2012: NVIDIA OpenGL for 2012Mark Kilgard
 

Semelhante a Yet another object system for R (15)

MOST (Newsfromthefront 2010)
MOST (Newsfromthefront 2010)MOST (Newsfromthefront 2010)
MOST (Newsfromthefront 2010)
 
Dimitry Solovyov - The imminent threat of functional programming
Dimitry Solovyov - The imminent threat of functional programmingDimitry Solovyov - The imminent threat of functional programming
Dimitry Solovyov - The imminent threat of functional programming
 
GTC 2012: NVIDIA OpenGL in 2012
GTC 2012: NVIDIA OpenGL in 2012GTC 2012: NVIDIA OpenGL in 2012
GTC 2012: NVIDIA OpenGL in 2012
 
Functional programming
Functional programmingFunctional programming
Functional programming
 
Hadoop World 2011: Proven Tools to Manage Hadoop Environments - Joey Jablonsk...
Hadoop World 2011: Proven Tools to Manage Hadoop Environments - Joey Jablonsk...Hadoop World 2011: Proven Tools to Manage Hadoop Environments - Joey Jablonsk...
Hadoop World 2011: Proven Tools to Manage Hadoop Environments - Joey Jablonsk...
 
Distributed ML with Dask and Kubernetes
Distributed ML with Dask and KubernetesDistributed ML with Dask and Kubernetes
Distributed ML with Dask and Kubernetes
 
IN4308 1
IN4308 1IN4308 1
IN4308 1
 
Generating Assertion Code from OCL: A Transformational Approach Based on Simi...
Generating Assertion Code from OCL: A Transformational Approach Based on Simi...Generating Assertion Code from OCL: A Transformational Approach Based on Simi...
Generating Assertion Code from OCL: A Transformational Approach Based on Simi...
 
SIGGRAPH Asia 2012 Exhibitor Talk: OpenGL 4.3 and Beyond
SIGGRAPH Asia 2012 Exhibitor Talk: OpenGL 4.3 and BeyondSIGGRAPH Asia 2012 Exhibitor Talk: OpenGL 4.3 and Beyond
SIGGRAPH Asia 2012 Exhibitor Talk: OpenGL 4.3 and Beyond
 
Functional Domain Modeling
Functional Domain ModelingFunctional Domain Modeling
Functional Domain Modeling
 
Realizing OpenGL
Realizing OpenGLRealizing OpenGL
Realizing OpenGL
 
2010 10 provxg_datagovuk
2010 10 provxg_datagovuk2010 10 provxg_datagovuk
2010 10 provxg_datagovuk
 
Introduction to ggplot2
Introduction to ggplot2Introduction to ggplot2
Introduction to ggplot2
 
SIGGRAPH 2012: NVIDIA OpenGL for 2012
SIGGRAPH 2012: NVIDIA OpenGL for 2012SIGGRAPH 2012: NVIDIA OpenGL for 2012
SIGGRAPH 2012: NVIDIA OpenGL for 2012
 
Mud flash
Mud flashMud flash
Mud flash
 

Mais de Hadley Wickham (20)

27 development
27 development27 development
27 development
 
27 development
27 development27 development
27 development
 
24 modelling
24 modelling24 modelling
24 modelling
 
23 data-structures
23 data-structures23 data-structures
23 data-structures
 
Graphical inference
Graphical inferenceGraphical inference
Graphical inference
 
R packages
R packagesR packages
R packages
 
22 spam
22 spam22 spam
22 spam
 
21 spam
21 spam21 spam
21 spam
 
20 date-times
20 date-times20 date-times
20 date-times
 
19 tables
19 tables19 tables
19 tables
 
18 cleaning
18 cleaning18 cleaning
18 cleaning
 
17 polishing
17 polishing17 polishing
17 polishing
 
16 critique
16 critique16 critique
16 critique
 
15 time-space
15 time-space15 time-space
15 time-space
 
14 case-study
14 case-study14 case-study
14 case-study
 
12 adv-manip
12 adv-manip12 adv-manip
12 adv-manip
 
11 adv-manip
11 adv-manip11 adv-manip
11 adv-manip
 
11 adv-manip
11 adv-manip11 adv-manip
11 adv-manip
 
10 simulation
10 simulation10 simulation
10 simulation
 
10 simulation
10 simulation10 simulation
10 simulation
 

Último

Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxNikitaBankoti2
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesShubhangi Sonawane
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 

Último (20)

Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 

Yet another object system for R

  • 1. Yet another object system for R Hadley Wickham Rice University Friday, 31 July 2009
  • 2. Does R need another object system? Friday, 31 July 2009
  • 3. Are S3 and S4 enough? Friday, 31 July 2009
  • 4. “Because it’s there” —George Mallory CC BY: http://www.flickr.com/photos/mckaysavage/497617014 Friday, 31 July 2009
  • 5. 1. Paradigms of programming 2. Existing systems in R 3. Prototype based programming 4. Examples 5. Scoping 6. Uses Friday, 31 July 2009
  • 6. Descriptive declarative programming XML, Data structures only S!expression Turing complete + procedure + cell (state) First!order Imperative Observable functional programming nondeterminism? Yes No programming Guarded command + nondet. choice Pascal, C + closure programming Imperative Functional Dijkstra’s GCL search programming + search programming + unification Scheme, ML + name SNOBOL, Icon, Prolog (equality) (unforgeable constant) Deterministic + continuation + cell ADT + cell ADT + port logic programming Continuation functional imperative (state) + closure programming (channel) programming programming Sequential + search Scheme, ML Event!loop object!oriented Haskell, ML, E CLU, OCaml, Oz programming Relational & logic + by!need + thread programming programming synchron. + single assign. + nondeterministic + port E in one vat Stateful Prolog, SQL Lazy Monotonic choice (channel) + thread functional embeddings functional dataflow Nonmonotonic Multi!agent programming Multi!agent + solver programming programming dataflow dataflow programming Java, OCam Constraint (logic) Haskell Declarative programming programming + thread Message!passing programming concurrent Concurrent logic Oz, Alice, AKL concurrent Concurrent CLP, ILOG Solver programming programming programming object!oriented + thread Pipes, MapReduce Oz, Alice, Curry, Excel, Erlang, AKL programming + thread + by!need AKL, FGHC, FCP Shared!state Concurrent + single assignment synchronization + local cell constraint + synch. on partial termination concurrent programming Lazy Functional reactive Active object programming dataflow programming (FRP) programming Smalltalk, LIFE, AKL programming Continuous synchronous Object!capability Java, Alice + by!need synchronization Lazy programming programming + log Lazy concurrent declarative constraint concurrent FrTime, Yampa CSP, Occam, Software programming programming + clocked computation E, Oz, Alice, transactional publish/subscribe, memory (STM) Oz, Alice, Curry Oz, Alice, Curry Discrete synchronous tuple space (Linda) programming SQL embeddings Logic and Esterel, Lustre, Signal Dataflow and constraints Functional message passing Message passing Shared state Nondet. state Unnamed state (seq. or conc.) Named state Less More Friday, 31 July 2009 Expressiveness of state
  • 7. Programming Paradigms for Dummies: What Every Programmer Should Know. Peter Van Roy. http://www.info.ucl.ac.be/~pvr/VanRoyChapter.pdf P. van Roy and S. Haridi. Concepts, Techniques and Models of Computer Programming. The MIT Press, 2004. Friday, 31 July 2009
  • 8. S3 / S4 Alternative Immutable Mutable (pass-by-value) (pass-by-reference) Generic functions Message passing (function based OO) (class based OO) Friday, 31 July 2009
  • 9. Mutable Immutable Required by many of Hard to derive most efficient algorithms computational complexity Simplifies dependence Must “thread” state between components Concurrency hard Concurrency easy Compilers can make Hard to reason about very efficient Friday, 31 July 2009
  • 10. Aside: memory efficiency “A persistent data structure is a data structure which always preserves the previous version of itself when it is modified.” “While persistence can be achieved by simple copying, this is inefficient in time and space, because most operations make only small changes to a data structure. A better method is to exploit the similarity between the new and old versions to share structure between them, such as using the same subtree in a number of tree structures.” http://en.wikipedia.org/wiki/Persistent_data_structure Friday, 31 July 2009
  • 11. Existing systems S3/S4: immutable + generic functions R.oo : mutable + generic functions OOP: mutable + message passing Proto: mutable + message passing YAROS: mutable + message passing Friday, 31 July 2009
  • 12. Prototype based programming Generalisation of class-based oo (like Java) that removes the distinction between classes and instances. Single dispatch, but often implement multiple (& dynamic) inheritance Notable languages: Javascript, Io Friday, 31 July 2009
  • 13. Counter <- Object$clone()$do({ init <- function() self$counter <- 0 count <- function() { self$counter <- self$counter + 1 self$counter } }) counter_a <- Counter$clone() counter_b <- Counter$clone() counter_a$count() counter_a$count() counter_b$count() Friday, 31 July 2009
  • 14. Account <- Object$clone()$do({ balance <- 0.0 deposit <- function(v) self$balance <- self$balance + v withdraw <- function(v) self$balance <- self$balance - v show <- function() cat("Account balance: $", self$balance, "n") init <- function() self$balance <- 0 }) Account$show() cat("Depositing $10n") Account$deposit(10.0) Account$show() Savings <- Account$clone()$do({ interest <- 0.05 withdraw <- NULL }) Savings$show() Friday, 31 July 2009
  • 15. Observable <- Object$clone()$do({ listeners <- list() add_listener <- function(f) { self$listeners <- c(self$listeners, f) } signal <- function(...) { for(l in self$listeners) l(...) } init <- function() { self$listeners <- list() } }) counter_a$append_proto(Observable$clone()) Friday, 31 July 2009
  • 16. Advantages Clean separation of object methods and base functions. No extra function parameters. Rich behaviour, including introspection, based on io. Mutable, multiple inheritance (depth-first search of inheritance graph). Friday, 31 July 2009
  • 17. Scoping self$method() needs to have object scoping, not lexical scoping. That is, rather than looking up based on environment at time function was defined, look it up based on current object. Friday, 31 July 2009
  • 18. "$.io" <- function(x, i, ...) { res <- core(x)$get_local_slot(i) object_scope(res, x) } object_scope <- function(res, self) { # Add environment to top of stack that contains # the self object if (is.function(res)) { env <- new.env() env$self <- self parent.env(env) <- environment(res) environment(res) <- env } res } Friday, 31 July 2009
  • 19. Uses Can simplify code which requires coordinating data from multiple locations: scale code in ggplot2 Complex simulations: e.g. card counting in black jack When there really is one true underlying object: GUIs and interactive graphics Friday, 31 July 2009
  • 20. Conclusions S3/S4 don’t fulfil every need, and it’s fun to experiment with alternative paradigms. Prototype-based OO is an interesting idea that the distinction between inheritance and instantiation. We can actually implement it in R, with different scoping rules. Has been a great learning experience. Friday, 31 July 2009