Lambda Calculus and Evaluation Strategies

download Lambda Calculus and Evaluation Strategies

of 27

Transcript of Lambda Calculus and Evaluation Strategies

  • 7/29/2019 Lambda Calculus and Evaluation Strategies

    1/27

    Lambda Calculus

    and

    Evaluation Strategies

    Ali Saeed Khan

    19th December, 2011

  • 7/29/2019 Lambda Calculus and Evaluation Strategies

    2/27

    Logic

    What is logic?

    Formal systematic study of the principles of

    Valid inference and

    correct reasoning

    12/19/2011 EC-215 PLE - Ali Saeed Khan 2

  • 7/29/2019 Lambda Calculus and Evaluation Strategies

    3/27

    Formal System

    A formal system (also called a logical calculus)

    consists of a formal language and a set of

    inference rules, used to derive an expression

    from one or more other premises that are

    supposed or derived.

    12/19/2011 3EC-215 PLE - Ali Saeed Khan

  • 7/29/2019 Lambda Calculus and Evaluation Strategies

    4/27

    The Lambda Calculus

    I t i sa formal system for the study of,

    Computability Theory

    Computable Recursive Functions

    Functions Variable binding and substitution

    To model Evaluation Strategies.

    typed lambda calculus serves as thefoundation of modern programminglanguages.

    12/19/2011 4EC-215 PLE - Ali Saeed Khan

  • 7/29/2019 Lambda Calculus and Evaluation Strategies

    5/27

    The Lambda Calculus

    The -calculus provides simple semantics for

    computation, enabling properties of

    computation to be studied formally.

    12/19/2011 5EC-215 PLE - Ali Saeed Khan

  • 7/29/2019 Lambda Calculus and Evaluation Strategies

    6/27

    Evaluation Strategy

    Set ofrules for evaluating expressions in a

    programming language.

    Usually these rules are deterministic in nature.

    Emphasis is typically placed on functions or

    operators: an evaluation strategy defines

    when and in what order the arguments to a function

    are evaluated, when they are substituted into the function, and

    what form that substitution takes.

    12/19/2011 6EC-215 PLE - Ali Saeed Khan

  • 7/29/2019 Lambda Calculus and Evaluation Strategies

    7/27

    Types of Evaluation Strategy

    Evaluation strategies divide into two basic

    groups, strict and non-strict, based on how

    arguments to a function are handled.

    Strict Evaluation

    Non-Strict Evaluation

    Nondeterministic Strategies

    12/19/2011 EC-215 PLE - Ali Saeed Khan 7

  • 7/29/2019 Lambda Calculus and Evaluation Strategies

    8/27

    Strict Evaluation

    Applicative Order

    Call by Value

    Call by Reference Call by Sharing

    Call by Copy-Restore

    12/19/2011 8EC-215 PLE - Ali Saeed Khan

  • 7/29/2019 Lambda Calculus and Evaluation Strategies

    9/27

    Non-Strict Evaluation

    Normal Order

    Call by Name

    Call by Need/Lazy Evaluation Call by Macro Expansion

    12/19/2011 9EC-215 PLE - Ali Saeed Khan

  • 7/29/2019 Lambda Calculus and Evaluation Strategies

    10/27

    Nondeterministic Strategies

    Full Beta-reduction

    Call by future

    Optimistic Evaluation

    12/19/2011 10EC-215 PLE - Ali Saeed Khan

  • 7/29/2019 Lambda Calculus and Evaluation Strategies

    11/27

    Strict Evaluation

    Strict evaluation is where all arguments to a

    function are evaluated before the body of the

    function is evaluated; and the arguments are

    never re-evaluated.

    In other words, each argument is evaluated

    exactly once.

    12/19/2011 EC-215 PLE - Ali Saeed Khan 11

  • 7/29/2019 Lambda Calculus and Evaluation Strategies

    12/27

    Applicative Order(rightmost innermsot)

    Applicative order evaluation refers to an

    evaluation strategy in which the arguments of

    a function are evaluated from left to right in

    a post-order traversal.

    Unlike call-by-value, applicative order

    evaluation reduces terms within a function

    body as much as possible before the functionis applied.

    12/19/2011 EC-215 PLE - Ali Saeed Khan 12

  • 7/29/2019 Lambda Calculus and Evaluation Strategies

    13/27

    Call by value

    Call-by-value or pass-by-value .

    Disadvantage(s)??

    Performance Degradation in case of large and

    complex structure is passed by value.

    12/19/2011 EC-215 PLE - Ali Saeed Khan 13

  • 7/29/2019 Lambda Calculus and Evaluation Strategies

    14/27

    Call by Reference

    Reference to the parameter is passed instead

    of value.

    This saves time

    Improves memory usage

    Synchronization required

    Suits concurrency

    12/19/2011 EC-215 PLE - Ali Saeed Khan 14

  • 7/29/2019 Lambda Calculus and Evaluation Strategies

    15/27

    Mutable vs. Immutable

    The object whose state can be changed afterits creation is said to be Mutable.

    The objects whose state cannot be changed or

    appears to be unchanged to the external pointof view, is said to be Immutable.

    Immutable objects can be useful in multi-

    threaded applications. Immutable objects are thread-safethan

    mutable objects.

    12/19/2011 EC-215 PLE - Ali Saeed Khan 15

  • 7/29/2019 Lambda Calculus and Evaluation Strategies

    16/27

    Copy On Write (COW)

    User -> System -> copy an object

    System creates a new reference rather than

    new object, pointing to same object

    User -> modifier object , system -> creates a

    new copy and sets reference to the new copy

    Advantage?

    12/19/2011 EC-215 PLE - Ali Saeed Khan 16

  • 7/29/2019 Lambda Calculus and Evaluation Strategies

    17/27

    Copy On Write (COW)

    Under COW -> users -> mutable version of theirobjects,

    Although, users do not modify their objects, the

    space-saving and speed advantages of immutableobjects are preserved.

    string x = "Hello;

    string y = x; // x and y use the same buffer

    y += ", World!"; // now y uses a different buffer

    // x still uses the same old buffer

    12/19/2011 EC-215 PLE - Ali Saeed Khan 17

  • 7/29/2019 Lambda Calculus and Evaluation Strategies

    18/27

    Call by Sharing

    Also known as, Call by Object-Sharing

    Its an object oriented concept

    It works like call by reference with a little

    difference i.e. assignments to functionarguments within the function are not visibleto the caller.

    So if a variable is passed as a variable it is notpossible to simulate an assignment on thatvariable in the callers scope.

    12/19/2011 EC-215 PLE - Ali Saeed Khan 18

  • 7/29/2019 Lambda Calculus and Evaluation Strategies

    19/27

    Call by Copy-Restore

    It is also referred to as Call by-Value-Result

    It is a special case of call-by-reference, whereinstead of directly working on the reference,

    the function works on a copy of thereferenced object, and then assigns("restores") the copy to the referenced objectjust before control is returned to the caller.

    Applicable in Multiprocessing context

    Fail-Safe

    12/19/2011 EC-215 PLE - Ali Saeed Khan 19

  • 7/29/2019 Lambda Calculus and Evaluation Strategies

    20/27

    Call by Copy-Restore

    Example

    int a;void unsafe(int x)

    { x= 2; a= 0; }

    int main() {

    a = 1; unsafe(a); printf(a); } when called by copy-restore? a = 2

    When called by reference? a = 0

    12/19/2011 EC-215 PLE - Ali Saeed Khan 20

  • 7/29/2019 Lambda Calculus and Evaluation Strategies

    21/27

    Non Strict Evaluation

    Here, arguments to a function are not

    evaluated unless they are actually used in the

    evaluation of the function body.

    12/19/2011 EC-215 PLE - Ali Saeed Khan 21

  • 7/29/2019 Lambda Calculus and Evaluation Strategies

    22/27

    Lazy Evaluation

    It is an evaluation strategy which delays theevaluation of an expression until the value ofthis is actually required.

    Performance Increases because unnecessarycalculations are avoided.

    Debugging is difficult why?

    As the order of operations becomesindeterminate, Exception Handling andDebugging becomes difficult.

    12/19/2011 22EC-215 PLE - Ali Saeed Khan

  • 7/29/2019 Lambda Calculus and Evaluation Strategies

    23/27

    Normal Order(leftmost outermost)

    Normal-orderevaluation is the evaluation

    strategy where the outermost reducible

    expressions is always reduced, applying

    functions before evaluating functionarguments

    12/19/2011 EC-215 PLE - Ali Saeed Khan 23

  • 7/29/2019 Lambda Calculus and Evaluation Strategies

    24/27

    Call by-name

    In this evaluation, the arguments to a functionare not evaluated before the function is called they are substituted directly into the

    function body and then left to be evaluatedwhenever they appear in the function.

    If an argument is not used in the functionbody, the argument is never evaluated; if it isused several times, it is re-evaluated eachtime it appears.

    12/19/2011 EC-215 PLE - Ali Saeed Khan 24

  • 7/29/2019 Lambda Calculus and Evaluation Strategies

    25/27

    Call by-need

    This is a memoized version of call-by-name

    where, if the function argument is

    evaluated, that value is stored for subsequent

    uses.

    12/19/2011 EC-215 PLE - Ali Saeed Khan 25

  • 7/29/2019 Lambda Calculus and Evaluation Strategies

    26/27

    Call by Macro-Expansion

    Used in case of macros.

    Same as call-by-name

    12/19/2011 EC-215 PLE - Ali Saeed Khan 26

  • 7/29/2019 Lambda Calculus and Evaluation Strategies

    27/27

    Assignment # 4

    Define the triology, Static

    Const

    Readonly

    You have to demonstrate this assignment througha working example. Student with bestexplanatory example will get the maximummarks. No hard copies needed, just demo

    Submission Date: 12th January, 2012

    12/19/2011 EC-215 PLE - Ali Saeed Khan 27