SlideShare une entreprise Scribd logo
1  sur  53
Télécharger pour lire hors ligne
Functional Languages
      Interpretation
        in Smalltalk

Alexandre Bergel
Lero & DSG, Trinity College Dublin,
Ireland

Alexandre.Bergel@cs.tcd.ie
www.cs.tcd.ie/Alexandre.Bergel

November 2006
Goal

     Studying the implementation of Scheme-like languages in an
 •
     object-oriented environment.
     Dynamic versus static scoping
 •




Alexandre Bergel                2
Outline

 1.    Essence of Scheme
 2.    SmallScheme overview
 3.    Modeling environment
 4.    Reading and parsing Scheme
 5.    Primitive expression types
 6.    Dynamic and static scoping
 7.    Further notes...




Alexandre Bergel               3
Essence of Scheme

 1. Idea behind Scheme
 2. Examples
 3. Evaluation




Alexandre Bergel         4
Essence of Scheme

    “Scheme demonstrates that a very small number of rules
    for forming expression, with no restriction on how they are
    composed, suffice to form a practical and efficient
    programming language that is flexible enough to support
    most of the major programming paradigms in use today.”

    R5RS, page 2

    The first description of Scheme was written in 1975




Alexandre Bergel                5
Manipulate parenthesized expressions

    Example of Scheme expression:
         10                    => 10
         ‘aSymbol              => aSymbol
         (+ 1 2)               => 3
         (if cond else then)
         (let ((a 10)
                 (b 15)
             (+ a (* 2 b)))    => 40
         (define (fac n)
            (if (= n 0)
               1
               (* n (fac (- n 1)))))
         (fac 10)              => 3628800


Alexandre Bergel              6
Closure application

    In such expression:
         (expr expr1 expr2 ... exprN)



    1 - expr is first evaluated into f


    2 - expr1 ... exprN are evaluated (most of the time
    sequentially). Let’s assume results are v1 ... vN


    3 - v1 ... vN are applied to f

Alexandre Bergel                 7
Special form (1/3)

    Exceptions to the previous rule are special forms.

   Condition of a if statement needs to be evaluated before
   other arguments.

         (if (= x 0)
           ‘error
           (/ 1 x))

   (= x 0) is evaluated first, then, according to this value,
   ‘error or (/ 1 x) is evaluated.




Alexandre Bergel                8
Special form (2/3)

    The body of a function is evaluated only when the function
    is applied
         (define (foo x) (+ x (* 2 x)))

    It binds (lambda (x) (+ x (* 2 x))) to foo




Alexandre Bergel                9
Special form (3/3)

    The body of an anonymous function is evaluated only when
    applied
         ((lambda (x) (* x x)) 2)




Alexandre Bergel              10
Manipulate parenthesized expressions

    Sequence of evaluation
         (fac 2)




Alexandre Bergel             11
Manipulate parenthesized expressions

    Sequence of evaluation
         (fac 2)
         ((lambda (n) (if (= n 0)
                        1
                        (* n (fac (- n 1))))) 2)




Alexandre Bergel             12
Manipulate parenthesized expressions

    Sequence of evaluation
         (fac 2)
         ((lambda (n) (if (= n 0)
                        1
                        (* n (fac (- n 1))))) 2)
         (if (= 2 0)
             1
             (* 2 (fac (- 2 1))))




Alexandre Bergel             13
Manipulate parenthesized expressions

    Sequence of evaluation
         (fac 2)
         ((lambda (n) (if (= n 0)
                        1
                        (* n (fac (- n 1))))) 2)
         (if (= 2 0)
             1
             (* 2 (fac (- 2 1))))

         (* 2      (fac (- 2 1)))
         (* 2      (fac (- 2 1)))
         (* 2      (fac (- 2 1)))
         (* 2      ((lambda (n) (if (= n 0) ...)) (- 2 1)))
         ...

Alexandre Bergel                  14
SmallScheme overview




Alexandre Bergel        15
What is SmallScheme

     SmallScheme is an interpreter of a subset of Scheme in
 •
     Squeak.




Alexandre Bergel                16
How to use SmallScheme

    In a programmatic way, Scheme expression can be
    evaluated:

         Scheme evalString: ‘(+ 2 3)’
         => 5

         Scheme evalString:
            ‘(define (fac n)
               (if (= n 0)
                   1
                   (* n (fac (- n 1)))))
              (fac 10)’
         => 3628800


Alexandre Bergel              17
Writing Scheme in Smalltalk with SmallScheme

    Available on www.squeaksource.com

         MCHttpRepository
           location: 'http://www.squeaksource.com/Scheme'
           user: ''
           password: ''




Alexandre Bergel             18
Class Hierarchy

                              SchemeObject
                               eval: anEnv


Boolean             Number                    Pair              String      Symbol

                                 Primitive
                       shouldArgsBeEvaluated
                        apply: args withEnv: anEnv

    Add              Begin         Car               If           Closure

                       Cons        MakeClosure            ...
 Alexandre Bergel                        19
Modeling environment

 1. Set of bindings
 2. Hierarchy of environments
 3. Primitive definitions




Alexandre Bergel            20
Environment


                        (let ((x 5) (y 7))
                          (let ((a 1) (b 2))
                            ...))

                                                         + -> ...
current                                                  - -> ...
                      a -> 1               x -> 5
environment                                              if -> ...
                      b -> 2               y -> 7
                                                         ...



               An environment has a parent and a set of bindings


  Alexandre Bergel                    21
Modeling Environments (1/3)

    Each environment has a set of bindings and a parent:

         Object subclass: #SchemeEnvironment
           instanceVariableNames: 'dictionary parent'

         SchemeEnvironment>>get: aSymbol
           ^ dictionary
              at: aSymbol asSmalltalkObject
              ifAbsent: [parent get: aSymbol]

         SchemeEnvironment>>at: aSymbol put: aValue
           dictionary
              at: aSymbol asSmalltalkObject
              put: aValue

Alexandre Bergel                22
Modeling Environment (2/3)

    Creating a new environment:

         SchemeEnvironment class>>createFromEnv: anEnv
           ^ self new withEnvironment: anEnv.

         SchemeEnvironment>>withEnvironment: anEnv
           parent := anEnv.
           dictionary := Dictionary new.
           parent isNil
              ifTrue: [self initializePrimitives].

    The environment root should be aware of different primitives.



Alexandre Bergel               23
Modeling Environment (3/3)

    The root of the environment contains the primitives:
         SchemeEnvironment>>initializePrimitives
            {{'if' . SchemePrimitiveIf} .
            {'begin' . SchemePrimitiveBegin} .
            {'loadfile' . SchemePrimitiveLoadfile} .
            {'display' . SchemePrimitiveDisplay} .
            {'newline' . SchemePrimitiveNewline} .
            {'set!' . SchemePrimitiveSet} .
            {'let' . SchemePrimitiveLet} .
            {'>' . SchemePrimitiveGreater} .
            {'<' . SchemePrimitiveLesser} .
            {'+' . SchemePrimitiveAdd} .
            {'/' . SchemePrimitiveDiv} .
            {'*' . SchemePrimitiveMul} .
            {'-' . SchemePrimitiveSub} .
            {'=' . SchemePrimitiveEqual} .
            {'define' . SchemePrimitiveDefine} .
            {'quote' . SchemePrimitiveQuote} .
            {'lambda' . SchemePrimitiveMakeClosure} .
            {'null?' . SchemePrimitiveNull} .
            {'car' . SchemePrimitiveCar} .
            {'cdr' . SchemePrimitiveCdr} .
            {'cons' . SchemePrimitiveCons}}
               do: [:pair| self at: pair first asSymbol put: pair second new]
Alexandre Bergel                         24
Reading Scheme (R5RS, Section 1.2, 2, 7)

 1. Lexical analysis
 2. Parsing Scheme
 3. SmallScheme uses a minimal grammar




Alexandre Bergel            25
Lexical Analysis

    Defined by the class SchemeLexer

    Public methods are:
         SchemeLexer   class>>analyseString: aString
         SchemeLexer   >>next
         SchemeLexer   >>getToken
         SchemeLexer   >>getType

    The type of a token could either be a boolean, (, ), ‘(), an
    integer, a string, or a symbol.




Alexandre Bergel                  26
Parsing Scheme

    Defined by the class SchemeParser

    Public methods are:
         SchemeParser class>>lexer: aLexer
         SchemeParser >>next

    The next method returns a scheme object




Alexandre Bergel             27
Grammar of SmallScheme

    Very minimal grammar
         expr =
           <number>        |
           <symbol>        |
           <string>        |
           <nil>           |
           ( expr+ )       |
           ‘ expr

    Primitive types are not part of the grammar. This gives more
    flexibility regarding the definition of those types.

    This is a major difference between SmallScheme and R5RS.

Alexandre Bergel                28
Example: the quote case

                   ‘x = (quote x) =
                                      quote x
    SchemeParser>>next
      |l|
      l := lexer next.
      ...
      lexer getType == SchemeLexer quoteID
        ifTrue: [
           ^ SchemePair
             car: (‘quote’ asSchemeSymbol)
             cdr: (SchemePair
                     car: self next
                     cdr: nil)]


Alexandre Bergel                        29
Primitive expression types (R5RS, Section 4.1)

 1.    Variable reference
 2.    Literal expression (quote)
 3.    Procedure calls
 4.    Procedures (lambda)
 5.    Conditionals




Alexandre Bergel                    30
Variable Reference

    Class SchemeSymbol:

         SchemeObject subclass: #SchemeSymbol
           instanceVariableNames: 'value'

         SchemeSymbol>>eval: anEnvironment
           ^anEnvironment get: value

    Scheme evalString: ‘(define x 5) x’
    => 5




Alexandre Bergel                 31
Literal expression (quote)

    Class SchemePrimitiveQuote:

         SchemePrimitive subclass: #SchemePrimitiveQuote

         SchemePrimitiveQuote>>
           apply: listOfArguments withEnv: anEnvironment
           “arguments are not evaluated”
           ^listOfArguments car

         SchemePrimitiveQuote>>initialize
           self doNotEvaluateArguments

    Scheme evalString: '(quote (+ 1 2)'
    => (+ 1 2)
Alexandre Bergel                 32
Procedure calls

    Associated with the lambda keyword
    SchemePrimitive subclass:
                #SchemePrimitiveMakeClosure

    SchemePrimitiveMakeClosure>>
      apply: listOfArguments withEnv: anEnvironment

              |args body|
              args := listOfArguments car.
              body := listOfArguments cdr.

              ^ SchemePrimitiveClosure
                 createWithArgs: args
                 body: body
                 env: anEnvironment
                                33
Alexandre Bergel
Procedure calls

    SchemePrimitive subclass: #SchemePrimitiveClosure
      instanceVariableNames: 'arguments body env'

    When we execute the following:
    Scheme evalString: ‘((lambda (x y) (+ x y)) 2 3)’

    SchemePrimitiveClosure>>
      apply: listOfArguments withEnv: anEnvironment
      ...

   listOfArguments is a collection containing 2 and 3




Alexandre Bergel                 34
Procedure calls

    SchemePrimitiveClosure>>
       apply: listOfArguments withEnv: anEnvironment
       | env b valuesProvided argNames |
       valuesProvided := listOfArguments.
       argNames := arguments.
       env := SchemeEnvironment createFromEnvironment: environment.

         [valuesProvided isNil] whileFalse: [
            env at: argNames car put: valuesProvided car.
            argNames := argNames cdr.
            valuesProvided := valuesProvided cdr.
            ].

         b := body.

         [b cdr isNil] whileFalse: [
            b car eval: env.
            b := b cdr].

         ^ b car eval: env

Alexandre Bergel                       35
Creation of a new environment

    SchemePrimitiveClosure>>
       apply: listOfArguments withEnv: anEnvironment
       | env b valuesProvided argNames |
       valuesProvided := listOfArguments.
       argNames := arguments.
       env := SchemeEnvironment createFromEnvironment: environment.

         [valuesProvided isNil] whileFalse: [
            env at: argNames car put: valuesProvided car.
            argNames := argNames cdr.
            valuesProvided := valuesProvided cdr.
            ].

         b := body.

         [b cdr isNil] whileFalse: [
            b car eval: env.
            b := b cdr].

         ^ b car eval: env

Alexandre Bergel                       36
Binding variables in the new environment

    SchemePrimitiveClosure>>
       apply: listOfArguments withEnv: anEnvironment
       | env b valuesProvided argNames |
       valuesProvided := listOfArguments.
       argNames := arguments.
       env := SchemeEnvironment createFromEnvironment: environment.

         [valuesProvided isNil] whileFalse: [
            env at: argNames car put: valuesProvided car.
            argNames := argNames cdr.
            valuesProvided := valuesProvided cdr.
            ].

         b := body.

         [b cdr isNil] whileFalse: [
            b car eval: env.
            b := b cdr].

         ^ b car eval: env

Alexandre Bergel                       37
Evaluating the body of the closure

    SchemePrimitiveClosure>>
       apply: listOfArguments withEnv: anEnvironment
       | env b valuesProvided argNames |
       valuesProvided := listOfArguments.
       argNames := arguments.
       env := SchemeEnvironment createFromEnvironment: environment.

         [valuesProvided isNil] whileFalse: [
            env at: argNames car put: valuesProvided car.
            argNames := argNames cdr.
            valuesProvided := valuesProvided cdr.
            ].

         b := body.

         [b cdr isNil] whileFalse: [
            b car eval: env.
            b := b cdr].

         ^ b car eval: env

Alexandre Bergel                       38
And returning the last value...

    SchemePrimitiveClosure>>
       apply: listOfArguments withEnv: anEnvironment
       | env b valuesProvided argNames |
       valuesProvided := listOfArguments.
       argNames := arguments.
       env := SchemeEnvironment createFromEnvironment: environment.

         [valuesProvided isNil] whileFalse: [
            env at: argNames car put: valuesProvided car.
            argNames := argNames cdr.
            valuesProvided := valuesProvided cdr.
            ].

         b := body.

         [b cdr isNil] whileFalse: [
            b car eval: env.
            b := b cdr].

         ^ b car eval: env

Alexandre Bergel                       39
Conditionals

    (if (= 2 3) ‘hello ‘world)
         SchemeObject subclass: #SchemePrimitiveIf

         SchemePrimitiveIf>>initialize
            self doNotEvaluateArguments

         SchemeSymbol>>apply: listOfArguments withEnv: anEnvironment
            | cond then else |
            cond := listOfArguments car.
            then := listOfArguments cdr car.

              listOfArguments cdr cdr
                  ifNotNil: [else := listOfArguments cdr cdr car].

              cond := cond eval: anEnvironment.
              cond isTrue
                  ifTrue: [^ then eval: anEnvironment].
              else notNil
                  ifTrue: [^ else eval: anEnvironment].
              ^ Scheme undefined

Alexandre Bergel                           40
Conditionals explained

    (if (= 2 3) ‘hello ‘world)
         SchemeObject subclass: #SchemePrimitiveIf

         SchemePrimitiveIf>>initialize
            self doNotEvaluateArguments

         SchemeSymbol>>apply: listOfArguments withEnv: anEnvironment
            | cond then else |
            cond := listOfArguments car.
            then := listOfArguments cdr car.

              listOfArguments cdr cdr
                  ifNotNil: [else := listOfArguments cdr cdr car].

              cond := cond eval: anEnvironment.
              cond isTrue
                  ifTrue: [^ then eval: anEnvironment].
              else notNil
                  ifTrue: [^ else eval: anEnvironment].
              ^ Scheme undefined

Alexandre Bergel                           41
Dynamic and static scoping




Alexandre Bergel        42
Dynamic vs static scoping

     Static scoping: variable references are bound to the
 •
     environment in which those variables are defined.

     Dynamic scoping: variables are looked up dynamically,
 •
     according to the current environment




Alexandre Bergel                 43
Static scoping: variable are statically bound

    The result is 15

         (let ((a 10))
           (let ((f (lambda (x) (+ a x))))
              (let ((a 0))
                (f 5))))




Alexandre Bergel             44
Dynamic scoping: variable are dynamically bound

    The result is 5

         (let ((a 10))
           (let ((f (lambda (x) (+ a x))))
              (let ((a 0))
                (f 5))))




Alexandre Bergel             45
Static scoping with SmallScheme

    SchemePrimitiveClosure>>
       apply: listOfArguments withEnv: anEnvironment
       | env b valuesProvided argNames |
       valuesProvided := listOfArguments.
       argNames := arguments.
       env := SchemeEnvironment createFromEnvironment: environment.
       ...



    The variable environment is bound to the environment in which
    the closure was created. And not to the current environment.




Alexandre Bergel                   46
Dynamic scoping with SmallScheme

    SchemePrimitiveFunction>>
       apply: listOfArguments withEnv: anEnvironment
       | env b valuesProvided argNames |
       valuesProvided := listOfArguments.
       argNames := arguments.
       env := SchemeEnvironment createFromEnvironment: anEnvironment.
       ...



    The variable anEnvironment is bound to the current
    environment. Which is not the environment in which the closure
    was created.




Alexandre Bergel                   47
Further notes

 1.    Symbiosis
 2.    Tail recursion
 3.    Grammar
 4.    Useful links




Alexandre Bergel        48
Symbiosis between Smalltalk and Scheme

    Few class extensions:

         Object>>asSmalltalkObject
           ^ self

         SchemeObject>>asSmalltalkObject
           self subclassResponsibility

         SchemeSymbol>>asSmalltalkObject
           ^ self value

         ...




Alexandre Bergel             49
Tail Recursion (R5RS, Section 3.5)

     But... We did not talk about tail recursion...
 •


     “Tail-recursion allows the execution of an iterative
 •
     computation in constant space, even if the iterative
     computation is described by a syntactically recursive
     procedure.”

     Easy to implement in SmallScheme
 •




Alexandre Bergel                  50
Tail Recursion

    Intuitively there is a stack consumption in:
         (define (fac n)
           (if (= n 0)
             1
             (* n (fac (- n 1)))))

    But there is no stack consumption in:
         (define (fac n) (fac2 n 1))

         (define (fac2 n acc)
           (if (= n 0)
              acc
              (fac2 (- n 1) (* acc n))))


Alexandre Bergel                 51
Minimal or large grammar?

     SmallScheme uses a very minimal grammar.
 •


     The fact that we use a small grammar:
 •
       – new primitives can be added without having to modify the
         grammar
       – primitive names can be easily changed

     However we need:
 •
       – evaluation of arguments need to be controlled
         (evaluateArguments var in primitive)
       – need to have a MakeClosure primitive




Alexandre Bergel                     52
Useful links

    SmallScheme:
         http://www.squeaksource.com/Scheme

    Smacc (also available on SqueakMap):
         http://www.refactory.com/Software/SmaCC

    Squeak:
         http://www.squeak.org

    Everything you wish to know about Scheme:
         http://www.schemers.org

    Especially the R5RS:
         http://www.schemers.org/Documents/Standards/R5RS/
Alexandre Bergel               53

Contenu connexe

Tendances

FP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondFP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyond
Mario Fusco
 
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STMConcurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Mario Fusco
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Mario Fusco
 

Tendances (20)

Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014
Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014
Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014
 
Programming in Scala: Notes
Programming in Scala: NotesProgramming in Scala: Notes
Programming in Scala: Notes
 
Scala introduction
Scala introductionScala introduction
Scala introduction
 
How Data Flow analysis works in a static code analyzer
How Data Flow analysis works in a static code analyzerHow Data Flow analysis works in a static code analyzer
How Data Flow analysis works in a static code analyzer
 
FP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondFP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyond
 
Demystifying functional programming with Scala
Demystifying functional programming with ScalaDemystifying functional programming with Scala
Demystifying functional programming with Scala
 
PVS-Studio in 2021 - Error Examples
PVS-Studio in 2021 - Error ExamplesPVS-Studio in 2021 - Error Examples
PVS-Studio in 2021 - Error Examples
 
Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)
 
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STMConcurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
 
Introduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScriptIntroduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScript
 
Map(), flatmap() and reduce() are your new best friends: simpler collections,...
Map(), flatmap() and reduce() are your new best friends: simpler collections,...Map(), flatmap() and reduce() are your new best friends: simpler collections,...
Map(), flatmap() and reduce() are your new best friends: simpler collections,...
 
Java 8 Workshop
Java 8 WorkshopJava 8 Workshop
Java 8 Workshop
 
Functional Programming With Scala
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With Scala
 
Scala is java8.next()
Scala is java8.next()Scala is java8.next()
Scala is java8.next()
 
Design Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesDesign Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on Examples
 
Lambda выражения и Java 8
Lambda выражения и Java 8Lambda выражения и Java 8
Lambda выражения и Java 8
 
OOP and FP - Become a Better Programmer
OOP and FP - Become a Better ProgrammerOOP and FP - Become a Better Programmer
OOP and FP - Become a Better Programmer
 
Aspect Mining for Large Systems
Aspect Mining for Large SystemsAspect Mining for Large Systems
Aspect Mining for Large Systems
 
Advanced JavaScript
Advanced JavaScript Advanced JavaScript
Advanced JavaScript
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...
 

En vedette

Multi dimensional profiling
Multi dimensional profilingMulti dimensional profiling
Multi dimensional profiling
bergel
 

En vedette (7)

Multi dimensional profiling
Multi dimensional profilingMulti dimensional profiling
Multi dimensional profiling
 
Profiling blueprints
Profiling blueprintsProfiling blueprints
Profiling blueprints
 
2008 Sccc Smalltalk
2008 Sccc Smalltalk2008 Sccc Smalltalk
2008 Sccc Smalltalk
 
2006 Seaside
2006 Seaside2006 Seaside
2006 Seaside
 
tres fotos
tres fotostres fotos
tres fotos
 
2006 Esug Omnibrowser
2006 Esug Omnibrowser2006 Esug Omnibrowser
2006 Esug Omnibrowser
 
Power Point
Power PointPower Point
Power Point
 

Similaire à 2006 Small Scheme

Learn a language : LISP
Learn a language : LISPLearn a language : LISP
Learn a language : LISP
Devnology
 
INTRODUCTION TO MATLAB presentation.pptx
INTRODUCTION TO MATLAB presentation.pptxINTRODUCTION TO MATLAB presentation.pptx
INTRODUCTION TO MATLAB presentation.pptx
Devaraj Chilakala
 
Ekeko Technology Showdown at SoTeSoLa 2012
Ekeko Technology Showdown at SoTeSoLa 2012Ekeko Technology Showdown at SoTeSoLa 2012
Ekeko Technology Showdown at SoTeSoLa 2012
Coen De Roover
 
High-Performance Haskell
High-Performance HaskellHigh-Performance Haskell
High-Performance Haskell
Johan Tibell
 

Similaire à 2006 Small Scheme (20)

Introduction To Lisp
Introduction To LispIntroduction To Lisp
Introduction To Lisp
 
NLP on a Billion Documents: Scalable Machine Learning with Apache Spark
NLP on a Billion Documents: Scalable Machine Learning with Apache SparkNLP on a Billion Documents: Scalable Machine Learning with Apache Spark
NLP on a Billion Documents: Scalable Machine Learning with Apache Spark
 
MiamiJS - The Future of JavaScript
MiamiJS - The Future of JavaScriptMiamiJS - The Future of JavaScript
MiamiJS - The Future of JavaScript
 
An Efficient and Parallel Abstract Interpreter in Scala — First Algorithm
An Efficient and Parallel Abstract Interpreter in Scala — First AlgorithmAn Efficient and Parallel Abstract Interpreter in Scala — First Algorithm
An Efficient and Parallel Abstract Interpreter in Scala — First Algorithm
 
Learn a language : LISP
Learn a language : LISPLearn a language : LISP
Learn a language : LISP
 
INTRODUCTION TO MATLAB presentation.pptx
INTRODUCTION TO MATLAB presentation.pptxINTRODUCTION TO MATLAB presentation.pptx
INTRODUCTION TO MATLAB presentation.pptx
 
[FT-11][suhorng] “Poor Man's” Undergraduate Compilers
[FT-11][suhorng] “Poor Man's” Undergraduate Compilers[FT-11][suhorng] “Poor Man's” Undergraduate Compilers
[FT-11][suhorng] “Poor Man's” Undergraduate Compilers
 
Meta Object Protocols
Meta Object ProtocolsMeta Object Protocols
Meta Object Protocols
 
Writing Macros
Writing MacrosWriting Macros
Writing Macros
 
Spark workshop
Spark workshopSpark workshop
Spark workshop
 
Matlab differential
Matlab differentialMatlab differential
Matlab differential
 
Ekeko Technology Showdown at SoTeSoLa 2012
Ekeko Technology Showdown at SoTeSoLa 2012Ekeko Technology Showdown at SoTeSoLa 2012
Ekeko Technology Showdown at SoTeSoLa 2012
 
Matlab algebra
Matlab algebraMatlab algebra
Matlab algebra
 
Faster Python, FOSDEM
Faster Python, FOSDEMFaster Python, FOSDEM
Faster Python, FOSDEM
 
Operations on rdd
Operations on rddOperations on rdd
Operations on rdd
 
High-Performance Haskell
High-Performance HaskellHigh-Performance Haskell
High-Performance Haskell
 
Matlab integration
Matlab integrationMatlab integration
Matlab integration
 
Beauty and the beast - Haskell on JVM
Beauty and the beast  - Haskell on JVMBeauty and the beast  - Haskell on JVM
Beauty and the beast - Haskell on JVM
 
Functional programming with Scala
Functional programming with ScalaFunctional programming with Scala
Functional programming with Scala
 
MATLAB-Introd.ppt
MATLAB-Introd.pptMATLAB-Introd.ppt
MATLAB-Introd.ppt
 

Plus de bergel

Roassal presentation
Roassal presentationRoassal presentation
Roassal presentation
bergel
 

Plus de bergel (10)

Building Neural Network Through Neuroevolution
Building Neural Network Through NeuroevolutionBuilding Neural Network Through Neuroevolution
Building Neural Network Through Neuroevolution
 
Roassal presentation
Roassal presentationRoassal presentation
Roassal presentation
 
2011 famoosr
2011 famoosr2011 famoosr
2011 famoosr
 
2011 ecoop
2011 ecoop2011 ecoop
2011 ecoop
 
Test beautycleanness
Test beautycleannessTest beautycleanness
Test beautycleanness
 
The Pharo Programming Language
The Pharo Programming LanguageThe Pharo Programming Language
The Pharo Programming Language
 
2008 Sccc Inheritance
2008 Sccc Inheritance2008 Sccc Inheritance
2008 Sccc Inheritance
 
Presentation of Traits
Presentation of TraitsPresentation of Traits
Presentation of Traits
 
2004 Esug Prototalk
2004 Esug Prototalk2004 Esug Prototalk
2004 Esug Prototalk
 
2005 Oopsla Classboxj
2005 Oopsla Classboxj2005 Oopsla Classboxj
2005 Oopsla Classboxj
 

Dernier

Call Girls in Tilak Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in Tilak Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in Tilak Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in Tilak Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
MASTERING FOREX: STRATEGIES FOR SUCCESS.pdf
MASTERING FOREX: STRATEGIES FOR SUCCESS.pdfMASTERING FOREX: STRATEGIES FOR SUCCESS.pdf
MASTERING FOREX: STRATEGIES FOR SUCCESS.pdf
Cocity Enterprises
 

Dernier (20)

Test bank for advanced assessment interpreting findings and formulating diffe...
Test bank for advanced assessment interpreting findings and formulating diffe...Test bank for advanced assessment interpreting findings and formulating diffe...
Test bank for advanced assessment interpreting findings and formulating diffe...
 
Kopar Khairane Cheapest Call Girls✔✔✔9833754194 Nerul Premium Call Girls-Navi...
Kopar Khairane Cheapest Call Girls✔✔✔9833754194 Nerul Premium Call Girls-Navi...Kopar Khairane Cheapest Call Girls✔✔✔9833754194 Nerul Premium Call Girls-Navi...
Kopar Khairane Cheapest Call Girls✔✔✔9833754194 Nerul Premium Call Girls-Navi...
 
Toronto dominion bank investor presentation.pdf
Toronto dominion bank investor presentation.pdfToronto dominion bank investor presentation.pdf
Toronto dominion bank investor presentation.pdf
 
Premium Call Girls Bangalore Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...
Premium Call Girls Bangalore Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...Premium Call Girls Bangalore Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...
Premium Call Girls Bangalore Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...
 
Call Girls in Tilak Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in Tilak Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in Tilak Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in Tilak Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
 
Certified Kala Jadu, Black magic specialist in Rawalpindi and Bangali Amil ba...
Certified Kala Jadu, Black magic specialist in Rawalpindi and Bangali Amil ba...Certified Kala Jadu, Black magic specialist in Rawalpindi and Bangali Amil ba...
Certified Kala Jadu, Black magic specialist in Rawalpindi and Bangali Amil ba...
 
MASTERING FOREX: STRATEGIES FOR SUCCESS.pdf
MASTERING FOREX: STRATEGIES FOR SUCCESS.pdfMASTERING FOREX: STRATEGIES FOR SUCCESS.pdf
MASTERING FOREX: STRATEGIES FOR SUCCESS.pdf
 
In Sharjah ௵(+971)558539980 *_௵abortion pills now available.
In Sharjah ௵(+971)558539980 *_௵abortion pills now available.In Sharjah ௵(+971)558539980 *_௵abortion pills now available.
In Sharjah ௵(+971)558539980 *_௵abortion pills now available.
 
Solution Manual For Financial Statement Analysis, 13th Edition By Charles H. ...
Solution Manual For Financial Statement Analysis, 13th Edition By Charles H. ...Solution Manual For Financial Statement Analysis, 13th Edition By Charles H. ...
Solution Manual For Financial Statement Analysis, 13th Edition By Charles H. ...
 
Fixed exchange rate and flexible exchange rate.pptx
Fixed exchange rate and flexible exchange rate.pptxFixed exchange rate and flexible exchange rate.pptx
Fixed exchange rate and flexible exchange rate.pptx
 
Seeman_Fiintouch_LLP_Newsletter_May-2024.pdf
Seeman_Fiintouch_LLP_Newsletter_May-2024.pdfSeeman_Fiintouch_LLP_Newsletter_May-2024.pdf
Seeman_Fiintouch_LLP_Newsletter_May-2024.pdf
 
Mahendragarh Escorts 🥰 8617370543 Call Girls Offer VIP Hot Girls
Mahendragarh Escorts 🥰 8617370543 Call Girls Offer VIP Hot GirlsMahendragarh Escorts 🥰 8617370543 Call Girls Offer VIP Hot Girls
Mahendragarh Escorts 🥰 8617370543 Call Girls Offer VIP Hot Girls
 
Dubai Call Girls Deira O525547819 Dubai Call Girls Bur Dubai Multiple
Dubai Call Girls Deira O525547819 Dubai Call Girls Bur Dubai MultipleDubai Call Girls Deira O525547819 Dubai Call Girls Bur Dubai Multiple
Dubai Call Girls Deira O525547819 Dubai Call Girls Bur Dubai Multiple
 
Technology industry / Finnish economic outlook
Technology industry / Finnish economic outlookTechnology industry / Finnish economic outlook
Technology industry / Finnish economic outlook
 
Virar Best Sex Call Girls Number-📞📞9833754194-Poorbi Nalasopara Housewife Cal...
Virar Best Sex Call Girls Number-📞📞9833754194-Poorbi Nalasopara Housewife Cal...Virar Best Sex Call Girls Number-📞📞9833754194-Poorbi Nalasopara Housewife Cal...
Virar Best Sex Call Girls Number-📞📞9833754194-Poorbi Nalasopara Housewife Cal...
 
Kurla Capable Call Girls ,07506202331, Sion Affordable Call Girls
Kurla Capable Call Girls ,07506202331, Sion Affordable Call GirlsKurla Capable Call Girls ,07506202331, Sion Affordable Call Girls
Kurla Capable Call Girls ,07506202331, Sion Affordable Call Girls
 
Famous Kala Jadu, Black magic expert in Faisalabad and Kala ilam specialist i...
Famous Kala Jadu, Black magic expert in Faisalabad and Kala ilam specialist i...Famous Kala Jadu, Black magic expert in Faisalabad and Kala ilam specialist i...
Famous Kala Jadu, Black magic expert in Faisalabad and Kala ilam specialist i...
 
Famous No1 Amil Baba Love marriage Astrologer Specialist Expert In Pakistan a...
Famous No1 Amil Baba Love marriage Astrologer Specialist Expert In Pakistan a...Famous No1 Amil Baba Love marriage Astrologer Specialist Expert In Pakistan a...
Famous No1 Amil Baba Love marriage Astrologer Specialist Expert In Pakistan a...
 
Business Principles, Tools, and Techniques in Participating in Various Types...
Business Principles, Tools, and Techniques  in Participating in Various Types...Business Principles, Tools, and Techniques  in Participating in Various Types...
Business Principles, Tools, and Techniques in Participating in Various Types...
 
Turbhe Fantastic Escorts📞📞9833754194 Kopar Khairane Marathi Call Girls-Kopar ...
Turbhe Fantastic Escorts📞📞9833754194 Kopar Khairane Marathi Call Girls-Kopar ...Turbhe Fantastic Escorts📞📞9833754194 Kopar Khairane Marathi Call Girls-Kopar ...
Turbhe Fantastic Escorts📞📞9833754194 Kopar Khairane Marathi Call Girls-Kopar ...
 

2006 Small Scheme

  • 1. Functional Languages Interpretation in Smalltalk Alexandre Bergel Lero & DSG, Trinity College Dublin, Ireland Alexandre.Bergel@cs.tcd.ie www.cs.tcd.ie/Alexandre.Bergel November 2006
  • 2. Goal Studying the implementation of Scheme-like languages in an • object-oriented environment. Dynamic versus static scoping • Alexandre Bergel 2
  • 3. Outline 1. Essence of Scheme 2. SmallScheme overview 3. Modeling environment 4. Reading and parsing Scheme 5. Primitive expression types 6. Dynamic and static scoping 7. Further notes... Alexandre Bergel 3
  • 4. Essence of Scheme 1. Idea behind Scheme 2. Examples 3. Evaluation Alexandre Bergel 4
  • 5. Essence of Scheme “Scheme demonstrates that a very small number of rules for forming expression, with no restriction on how they are composed, suffice to form a practical and efficient programming language that is flexible enough to support most of the major programming paradigms in use today.” R5RS, page 2 The first description of Scheme was written in 1975 Alexandre Bergel 5
  • 6. Manipulate parenthesized expressions Example of Scheme expression: 10 => 10 ‘aSymbol => aSymbol (+ 1 2) => 3 (if cond else then) (let ((a 10) (b 15) (+ a (* 2 b))) => 40 (define (fac n) (if (= n 0) 1 (* n (fac (- n 1))))) (fac 10) => 3628800 Alexandre Bergel 6
  • 7. Closure application In such expression: (expr expr1 expr2 ... exprN) 1 - expr is first evaluated into f 2 - expr1 ... exprN are evaluated (most of the time sequentially). Let’s assume results are v1 ... vN 3 - v1 ... vN are applied to f Alexandre Bergel 7
  • 8. Special form (1/3) Exceptions to the previous rule are special forms. Condition of a if statement needs to be evaluated before other arguments. (if (= x 0) ‘error (/ 1 x)) (= x 0) is evaluated first, then, according to this value, ‘error or (/ 1 x) is evaluated. Alexandre Bergel 8
  • 9. Special form (2/3) The body of a function is evaluated only when the function is applied (define (foo x) (+ x (* 2 x))) It binds (lambda (x) (+ x (* 2 x))) to foo Alexandre Bergel 9
  • 10. Special form (3/3) The body of an anonymous function is evaluated only when applied ((lambda (x) (* x x)) 2) Alexandre Bergel 10
  • 11. Manipulate parenthesized expressions Sequence of evaluation (fac 2) Alexandre Bergel 11
  • 12. Manipulate parenthesized expressions Sequence of evaluation (fac 2) ((lambda (n) (if (= n 0) 1 (* n (fac (- n 1))))) 2) Alexandre Bergel 12
  • 13. Manipulate parenthesized expressions Sequence of evaluation (fac 2) ((lambda (n) (if (= n 0) 1 (* n (fac (- n 1))))) 2) (if (= 2 0) 1 (* 2 (fac (- 2 1)))) Alexandre Bergel 13
  • 14. Manipulate parenthesized expressions Sequence of evaluation (fac 2) ((lambda (n) (if (= n 0) 1 (* n (fac (- n 1))))) 2) (if (= 2 0) 1 (* 2 (fac (- 2 1)))) (* 2 (fac (- 2 1))) (* 2 (fac (- 2 1))) (* 2 (fac (- 2 1))) (* 2 ((lambda (n) (if (= n 0) ...)) (- 2 1))) ... Alexandre Bergel 14
  • 16. What is SmallScheme SmallScheme is an interpreter of a subset of Scheme in • Squeak. Alexandre Bergel 16
  • 17. How to use SmallScheme In a programmatic way, Scheme expression can be evaluated: Scheme evalString: ‘(+ 2 3)’ => 5 Scheme evalString: ‘(define (fac n) (if (= n 0) 1 (* n (fac (- n 1))))) (fac 10)’ => 3628800 Alexandre Bergel 17
  • 18. Writing Scheme in Smalltalk with SmallScheme Available on www.squeaksource.com MCHttpRepository location: 'http://www.squeaksource.com/Scheme' user: '' password: '' Alexandre Bergel 18
  • 19. Class Hierarchy SchemeObject eval: anEnv Boolean Number Pair String Symbol Primitive shouldArgsBeEvaluated apply: args withEnv: anEnv Add Begin Car If Closure Cons MakeClosure ... Alexandre Bergel 19
  • 20. Modeling environment 1. Set of bindings 2. Hierarchy of environments 3. Primitive definitions Alexandre Bergel 20
  • 21. Environment (let ((x 5) (y 7)) (let ((a 1) (b 2)) ...)) + -> ... current - -> ... a -> 1 x -> 5 environment if -> ... b -> 2 y -> 7 ... An environment has a parent and a set of bindings Alexandre Bergel 21
  • 22. Modeling Environments (1/3) Each environment has a set of bindings and a parent: Object subclass: #SchemeEnvironment instanceVariableNames: 'dictionary parent' SchemeEnvironment>>get: aSymbol ^ dictionary at: aSymbol asSmalltalkObject ifAbsent: [parent get: aSymbol] SchemeEnvironment>>at: aSymbol put: aValue dictionary at: aSymbol asSmalltalkObject put: aValue Alexandre Bergel 22
  • 23. Modeling Environment (2/3) Creating a new environment: SchemeEnvironment class>>createFromEnv: anEnv ^ self new withEnvironment: anEnv. SchemeEnvironment>>withEnvironment: anEnv parent := anEnv. dictionary := Dictionary new. parent isNil ifTrue: [self initializePrimitives]. The environment root should be aware of different primitives. Alexandre Bergel 23
  • 24. Modeling Environment (3/3) The root of the environment contains the primitives: SchemeEnvironment>>initializePrimitives {{'if' . SchemePrimitiveIf} . {'begin' . SchemePrimitiveBegin} . {'loadfile' . SchemePrimitiveLoadfile} . {'display' . SchemePrimitiveDisplay} . {'newline' . SchemePrimitiveNewline} . {'set!' . SchemePrimitiveSet} . {'let' . SchemePrimitiveLet} . {'>' . SchemePrimitiveGreater} . {'<' . SchemePrimitiveLesser} . {'+' . SchemePrimitiveAdd} . {'/' . SchemePrimitiveDiv} . {'*' . SchemePrimitiveMul} . {'-' . SchemePrimitiveSub} . {'=' . SchemePrimitiveEqual} . {'define' . SchemePrimitiveDefine} . {'quote' . SchemePrimitiveQuote} . {'lambda' . SchemePrimitiveMakeClosure} . {'null?' . SchemePrimitiveNull} . {'car' . SchemePrimitiveCar} . {'cdr' . SchemePrimitiveCdr} . {'cons' . SchemePrimitiveCons}} do: [:pair| self at: pair first asSymbol put: pair second new] Alexandre Bergel 24
  • 25. Reading Scheme (R5RS, Section 1.2, 2, 7) 1. Lexical analysis 2. Parsing Scheme 3. SmallScheme uses a minimal grammar Alexandre Bergel 25
  • 26. Lexical Analysis Defined by the class SchemeLexer Public methods are: SchemeLexer class>>analyseString: aString SchemeLexer >>next SchemeLexer >>getToken SchemeLexer >>getType The type of a token could either be a boolean, (, ), ‘(), an integer, a string, or a symbol. Alexandre Bergel 26
  • 27. Parsing Scheme Defined by the class SchemeParser Public methods are: SchemeParser class>>lexer: aLexer SchemeParser >>next The next method returns a scheme object Alexandre Bergel 27
  • 28. Grammar of SmallScheme Very minimal grammar expr = <number> | <symbol> | <string> | <nil> | ( expr+ ) | ‘ expr Primitive types are not part of the grammar. This gives more flexibility regarding the definition of those types. This is a major difference between SmallScheme and R5RS. Alexandre Bergel 28
  • 29. Example: the quote case ‘x = (quote x) = quote x SchemeParser>>next |l| l := lexer next. ... lexer getType == SchemeLexer quoteID ifTrue: [ ^ SchemePair car: (‘quote’ asSchemeSymbol) cdr: (SchemePair car: self next cdr: nil)] Alexandre Bergel 29
  • 30. Primitive expression types (R5RS, Section 4.1) 1. Variable reference 2. Literal expression (quote) 3. Procedure calls 4. Procedures (lambda) 5. Conditionals Alexandre Bergel 30
  • 31. Variable Reference Class SchemeSymbol: SchemeObject subclass: #SchemeSymbol instanceVariableNames: 'value' SchemeSymbol>>eval: anEnvironment ^anEnvironment get: value Scheme evalString: ‘(define x 5) x’ => 5 Alexandre Bergel 31
  • 32. Literal expression (quote) Class SchemePrimitiveQuote: SchemePrimitive subclass: #SchemePrimitiveQuote SchemePrimitiveQuote>> apply: listOfArguments withEnv: anEnvironment “arguments are not evaluated” ^listOfArguments car SchemePrimitiveQuote>>initialize self doNotEvaluateArguments Scheme evalString: '(quote (+ 1 2)' => (+ 1 2) Alexandre Bergel 32
  • 33. Procedure calls Associated with the lambda keyword SchemePrimitive subclass: #SchemePrimitiveMakeClosure SchemePrimitiveMakeClosure>> apply: listOfArguments withEnv: anEnvironment |args body| args := listOfArguments car. body := listOfArguments cdr. ^ SchemePrimitiveClosure createWithArgs: args body: body env: anEnvironment 33 Alexandre Bergel
  • 34. Procedure calls SchemePrimitive subclass: #SchemePrimitiveClosure instanceVariableNames: 'arguments body env' When we execute the following: Scheme evalString: ‘((lambda (x y) (+ x y)) 2 3)’ SchemePrimitiveClosure>> apply: listOfArguments withEnv: anEnvironment ... listOfArguments is a collection containing 2 and 3 Alexandre Bergel 34
  • 35. Procedure calls SchemePrimitiveClosure>> apply: listOfArguments withEnv: anEnvironment | env b valuesProvided argNames | valuesProvided := listOfArguments. argNames := arguments. env := SchemeEnvironment createFromEnvironment: environment. [valuesProvided isNil] whileFalse: [ env at: argNames car put: valuesProvided car. argNames := argNames cdr. valuesProvided := valuesProvided cdr. ]. b := body. [b cdr isNil] whileFalse: [ b car eval: env. b := b cdr]. ^ b car eval: env Alexandre Bergel 35
  • 36. Creation of a new environment SchemePrimitiveClosure>> apply: listOfArguments withEnv: anEnvironment | env b valuesProvided argNames | valuesProvided := listOfArguments. argNames := arguments. env := SchemeEnvironment createFromEnvironment: environment. [valuesProvided isNil] whileFalse: [ env at: argNames car put: valuesProvided car. argNames := argNames cdr. valuesProvided := valuesProvided cdr. ]. b := body. [b cdr isNil] whileFalse: [ b car eval: env. b := b cdr]. ^ b car eval: env Alexandre Bergel 36
  • 37. Binding variables in the new environment SchemePrimitiveClosure>> apply: listOfArguments withEnv: anEnvironment | env b valuesProvided argNames | valuesProvided := listOfArguments. argNames := arguments. env := SchemeEnvironment createFromEnvironment: environment. [valuesProvided isNil] whileFalse: [ env at: argNames car put: valuesProvided car. argNames := argNames cdr. valuesProvided := valuesProvided cdr. ]. b := body. [b cdr isNil] whileFalse: [ b car eval: env. b := b cdr]. ^ b car eval: env Alexandre Bergel 37
  • 38. Evaluating the body of the closure SchemePrimitiveClosure>> apply: listOfArguments withEnv: anEnvironment | env b valuesProvided argNames | valuesProvided := listOfArguments. argNames := arguments. env := SchemeEnvironment createFromEnvironment: environment. [valuesProvided isNil] whileFalse: [ env at: argNames car put: valuesProvided car. argNames := argNames cdr. valuesProvided := valuesProvided cdr. ]. b := body. [b cdr isNil] whileFalse: [ b car eval: env. b := b cdr]. ^ b car eval: env Alexandre Bergel 38
  • 39. And returning the last value... SchemePrimitiveClosure>> apply: listOfArguments withEnv: anEnvironment | env b valuesProvided argNames | valuesProvided := listOfArguments. argNames := arguments. env := SchemeEnvironment createFromEnvironment: environment. [valuesProvided isNil] whileFalse: [ env at: argNames car put: valuesProvided car. argNames := argNames cdr. valuesProvided := valuesProvided cdr. ]. b := body. [b cdr isNil] whileFalse: [ b car eval: env. b := b cdr]. ^ b car eval: env Alexandre Bergel 39
  • 40. Conditionals (if (= 2 3) ‘hello ‘world) SchemeObject subclass: #SchemePrimitiveIf SchemePrimitiveIf>>initialize self doNotEvaluateArguments SchemeSymbol>>apply: listOfArguments withEnv: anEnvironment | cond then else | cond := listOfArguments car. then := listOfArguments cdr car. listOfArguments cdr cdr ifNotNil: [else := listOfArguments cdr cdr car]. cond := cond eval: anEnvironment. cond isTrue ifTrue: [^ then eval: anEnvironment]. else notNil ifTrue: [^ else eval: anEnvironment]. ^ Scheme undefined Alexandre Bergel 40
  • 41. Conditionals explained (if (= 2 3) ‘hello ‘world) SchemeObject subclass: #SchemePrimitiveIf SchemePrimitiveIf>>initialize self doNotEvaluateArguments SchemeSymbol>>apply: listOfArguments withEnv: anEnvironment | cond then else | cond := listOfArguments car. then := listOfArguments cdr car. listOfArguments cdr cdr ifNotNil: [else := listOfArguments cdr cdr car]. cond := cond eval: anEnvironment. cond isTrue ifTrue: [^ then eval: anEnvironment]. else notNil ifTrue: [^ else eval: anEnvironment]. ^ Scheme undefined Alexandre Bergel 41
  • 42. Dynamic and static scoping Alexandre Bergel 42
  • 43. Dynamic vs static scoping Static scoping: variable references are bound to the • environment in which those variables are defined. Dynamic scoping: variables are looked up dynamically, • according to the current environment Alexandre Bergel 43
  • 44. Static scoping: variable are statically bound The result is 15 (let ((a 10)) (let ((f (lambda (x) (+ a x)))) (let ((a 0)) (f 5)))) Alexandre Bergel 44
  • 45. Dynamic scoping: variable are dynamically bound The result is 5 (let ((a 10)) (let ((f (lambda (x) (+ a x)))) (let ((a 0)) (f 5)))) Alexandre Bergel 45
  • 46. Static scoping with SmallScheme SchemePrimitiveClosure>> apply: listOfArguments withEnv: anEnvironment | env b valuesProvided argNames | valuesProvided := listOfArguments. argNames := arguments. env := SchemeEnvironment createFromEnvironment: environment. ... The variable environment is bound to the environment in which the closure was created. And not to the current environment. Alexandre Bergel 46
  • 47. Dynamic scoping with SmallScheme SchemePrimitiveFunction>> apply: listOfArguments withEnv: anEnvironment | env b valuesProvided argNames | valuesProvided := listOfArguments. argNames := arguments. env := SchemeEnvironment createFromEnvironment: anEnvironment. ... The variable anEnvironment is bound to the current environment. Which is not the environment in which the closure was created. Alexandre Bergel 47
  • 48. Further notes 1. Symbiosis 2. Tail recursion 3. Grammar 4. Useful links Alexandre Bergel 48
  • 49. Symbiosis between Smalltalk and Scheme Few class extensions: Object>>asSmalltalkObject ^ self SchemeObject>>asSmalltalkObject self subclassResponsibility SchemeSymbol>>asSmalltalkObject ^ self value ... Alexandre Bergel 49
  • 50. Tail Recursion (R5RS, Section 3.5) But... We did not talk about tail recursion... • “Tail-recursion allows the execution of an iterative • computation in constant space, even if the iterative computation is described by a syntactically recursive procedure.” Easy to implement in SmallScheme • Alexandre Bergel 50
  • 51. Tail Recursion Intuitively there is a stack consumption in: (define (fac n) (if (= n 0) 1 (* n (fac (- n 1))))) But there is no stack consumption in: (define (fac n) (fac2 n 1)) (define (fac2 n acc) (if (= n 0) acc (fac2 (- n 1) (* acc n)))) Alexandre Bergel 51
  • 52. Minimal or large grammar? SmallScheme uses a very minimal grammar. • The fact that we use a small grammar: • – new primitives can be added without having to modify the grammar – primitive names can be easily changed However we need: • – evaluation of arguments need to be controlled (evaluateArguments var in primitive) – need to have a MakeClosure primitive Alexandre Bergel 52
  • 53. Useful links SmallScheme: http://www.squeaksource.com/Scheme Smacc (also available on SqueakMap): http://www.refactory.com/Software/SmaCC Squeak: http://www.squeak.org Everything you wish to know about Scheme: http://www.schemers.org Especially the R5RS: http://www.schemers.org/Documents/Standards/R5RS/ Alexandre Bergel 53