47
Principles of Programming Languages Lecture 5: Exploring non-determinism in K. Threads. Andrei Arusoaie 1 1 Department of Computer Science November 5, 2018

Principles of Programming Languages Lecture 5: Exploring ...arusoaie.andrei/lectures/PLP/...Principles of Programming Languages Lecture 5: Exploring non-determinism in K. Threads

  • Upload
    others

  • View
    10

  • Download
    0

Embed Size (px)

Citation preview

  • Principles of Programming LanguagesLecture 5: Exploring non-determinism in K.

    Threads.

    Andrei Arusoaie1

    1Department of Computer Science

    November 5, 2018

  • Outline

    Exploring non-determinism in K

    Threads

  • Outline

    Exploring non-determinism in K

    Threads

  • Non-determinism

    I What non-determinism means?

    I In programs: multiple possible executionsI Examples?

    I In IMP: Binary expressions combined with side-effectsI Example: y = ++x / (++x / x) ;

    I Exercise: what’s the value of y if x = 1?I DEMO: krun

  • Non-determinism

    I What non-determinism means?I In programs: multiple possible executions

    I Examples?I In IMP: Binary expressions combined with side-effects

    I Example: y = ++x / (++x / x) ;I Exercise: what’s the value of y if x = 1?

    I DEMO: krun

  • Non-determinism

    I What non-determinism means?I In programs: multiple possible executionsI Examples?

    I In IMP: Binary expressions combined with side-effectsI Example: y = ++x / (++x / x) ;

    I Exercise: what’s the value of y if x = 1?I DEMO: krun

  • Non-determinism

    I What non-determinism means?I In programs: multiple possible executionsI Examples?

    I In IMP: Binary expressions combined with side-effects

    I Example: y = ++x / (++x / x) ;I Exercise: what’s the value of y if x = 1?

    I DEMO: krun

  • Non-determinism

    I What non-determinism means?I In programs: multiple possible executionsI Examples?

    I In IMP: Binary expressions combined with side-effectsI Example: y = ++x / (++x / x) ;

    I Exercise: what’s the value of y if x = 1?I DEMO: krun

  • Non-determinism

    I What non-determinism means?I In programs: multiple possible executionsI Examples?

    I In IMP: Binary expressions combined with side-effectsI Example: y = ++x / (++x / x) ;

    I Exercise: what’s the value of y if x = 1?

    I DEMO: krun

  • Non-determinism

    I What non-determinism means?I In programs: multiple possible executionsI Examples?

    I In IMP: Binary expressions combined with side-effectsI Example: y = ++x / (++x / x) ;

    I Exercise: what’s the value of y if x = 1?I DEMO: krun

  • In K

    I By default, krun shows only one solutionI Reason: exploring all executions might take too long

    I In fact, kompile generates an interpreter which picks onepossible execution path

    I Enable non-determinism exploration:kompile --transition

  • In K

    I By default, krun shows only one solutionI Reason: exploring all executions might take too longI In fact, kompile generates an interpreter which picks one

    possible execution path

    I Enable non-determinism exploration:kompile --transition

  • In K

    I By default, krun shows only one solutionI Reason: exploring all executions might take too longI In fact, kompile generates an interpreter which picks one

    possible execution pathI Enable non-determinism exploration:

    kompile --transition

  • Example

    Recall y = ++x / (++x / x) ;

    Steps:1. Tag the division syntax production:

    syntax AExp ::= AExp "/" AExp [left, strict, division]

    2. Compile the defintion::-$ kompile imp.k --transition division

    3. Run::-$ krun test.imp --search

    DEMO

  • Example

    Recall y = ++x / (++x / x) ;

    Steps:1. Tag the division syntax production:

    syntax AExp ::= AExp "/" AExp [left, strict, division]

    2. Compile the defintion::-$ kompile imp.k --transition division

    3. Run::-$ krun test.imp --search

    DEMO

  • Example

    Recall y = ++x / (++x / x) ;

    Steps:1. Tag the division syntax production:

    syntax AExp ::= AExp "/" AExp [left, strict, division]

    2. Compile the defintion::-$ kompile imp.k --transition division

    3. Run::-$ krun test.imp --search

    DEMO

  • Example

    Recall y = ++x / (++x / x) ;

    Steps:1. Tag the division syntax production:

    syntax AExp ::= AExp "/" AExp [left, strict, division]

    2. Compile the defintion::-$ kompile imp.k --transition division

    3. Run::-$ krun test.imp --search

    DEMO

  • Example

    Recall y = ++x / (++x / x) ;

    Steps:1. Tag the division syntax production:

    syntax AExp ::= AExp "/" AExp [left, strict, division]

    2. Compile the defintion::-$ kompile imp.k --transition division

    3. Run::-$ krun test.imp --search

    DEMO

  • Threads in IMP

    I What are threads?

    I The smallest sequence of instructions that are managedby the OS’s scheduler

    I Thread vs. Process:I multiple threads can exist in one processI threads run in a shared memory spaceI processes run in separate memory spaces

    I Recall fork: creates a new child processI Both the caller and the child will execute the instruction

    right after fork system callI In our language we will use spawn:

    I it takes a statement and creates a new concurrent threadI the memory is shared with the parent thread

    I Demo: syntax Stmt ::= "spawn" StmtI Demo: write a program

  • Threads in IMP

    I What are threads?I The smallest sequence of instructions that are managed

    by the OS’s scheduler

    I Thread vs. Process:I multiple threads can exist in one processI threads run in a shared memory spaceI processes run in separate memory spaces

    I Recall fork: creates a new child processI Both the caller and the child will execute the instruction

    right after fork system callI In our language we will use spawn:

    I it takes a statement and creates a new concurrent threadI the memory is shared with the parent thread

    I Demo: syntax Stmt ::= "spawn" StmtI Demo: write a program

  • Threads in IMP

    I What are threads?I The smallest sequence of instructions that are managed

    by the OS’s schedulerI Thread vs. Process:

    I multiple threads can exist in one processI threads run in a shared memory spaceI processes run in separate memory spaces

    I Recall fork: creates a new child processI Both the caller and the child will execute the instruction

    right after fork system callI In our language we will use spawn:

    I it takes a statement and creates a new concurrent threadI the memory is shared with the parent thread

    I Demo: syntax Stmt ::= "spawn" StmtI Demo: write a program

  • Threads in IMP

    I What are threads?I The smallest sequence of instructions that are managed

    by the OS’s schedulerI Thread vs. Process:

    I multiple threads can exist in one processI threads run in a shared memory spaceI processes run in separate memory spaces

    I Recall fork: creates a new child processI Both the caller and the child will execute the instruction

    right after fork system call

    I In our language we will use spawn:I it takes a statement and creates a new concurrent threadI the memory is shared with the parent thread

    I Demo: syntax Stmt ::= "spawn" StmtI Demo: write a program

  • Threads in IMP

    I What are threads?I The smallest sequence of instructions that are managed

    by the OS’s schedulerI Thread vs. Process:

    I multiple threads can exist in one processI threads run in a shared memory spaceI processes run in separate memory spaces

    I Recall fork: creates a new child processI Both the caller and the child will execute the instruction

    right after fork system callI In our language we will use spawn:

    I it takes a statement and creates a new concurrent threadI the memory is shared with the parent thread

    I Demo: syntax Stmt ::= "spawn" StmtI Demo: write a program

  • Threads in IMP

    I What are threads?I The smallest sequence of instructions that are managed

    by the OS’s schedulerI Thread vs. Process:

    I multiple threads can exist in one processI threads run in a shared memory spaceI processes run in separate memory spaces

    I Recall fork: creates a new child processI Both the caller and the child will execute the instruction

    right after fork system callI In our language we will use spawn:

    I it takes a statement and creates a new concurrent threadI the memory is shared with the parent thread

    I Demo: syntax Stmt ::= "spawn" StmtI Demo: write a program

  • spawn

    I spawn S: create a new concurrent thread that executes S

    I The new thread is being passed at creation time itsparent’s environment

    I It shares with its parent the memory locationsI The parent and the child threads can evolve unrestricted

    I they can change their own environmentsI declare or hide variablesI create new threads, etc.

  • spawn

    I spawn S: create a new concurrent thread that executes SI The new thread is being passed at creation time its

    parent’s environmentI It shares with its parent the memory locations

    I The parent and the child threads can evolve unrestrictedI they can change their own environmentsI declare or hide variablesI create new threads, etc.

  • spawn

    I spawn S: create a new concurrent thread that executes SI The new thread is being passed at creation time its

    parent’s environmentI It shares with its parent the memory locationsI The parent and the child threads can evolve unrestricted

    I they can change their own environmentsI declare or hide variablesI create new threads, etc.

  • Configuration Abstraction - again

    I The above suggests that a thread should have its own, , and cells

    I Configuration refinement:

    $PGM:Stmt .Map .List

    .Map .List .List

    I What if we run the semantics with the new configuration?I Demo: look at the rules and show how they should be

    written.

  • Configuration Abstraction - again

    I The above suggests that a thread should have its own, , and cells

    I Configuration refinement:

    $PGM:Stmt .Map .List

    .Map .List .List

    I What if we run the semantics with the new configuration?I Demo: look at the rules and show how they should be

    written.

  • Configuration Abstraction - again

    I The above suggests that a thread should have its own, , and cells

    I Configuration refinement:

    $PGM:Stmt .Map .List

    .Map .List .List

    I What if we run the semantics with the new configuration?I Demo: look at the rules and show how they should be

    written.

  • Configuration Abstraction - again

    I The above suggests that a thread should have its own, , and cells

    I Configuration refinement:

    $PGM:Stmt .Map .List

    .Map .List .List

    I What if we run the semantics with the new configuration?

    I Demo: look at the rules and show how they should bewritten.

  • Configuration Abstraction - again

    I The above suggests that a thread should have its own, , and cells

    I Configuration refinement:

    $PGM:Stmt .Map .List

    .Map .List .List

    I What if we run the semantics with the new configuration?I Demo: look at the rules and show how they should be

    written.

  • Multiplicity

    I We should be able to create multiple threadsI Thus, we specify this in the configuration:

    $PGM:Stmt .Map .List

    .Map .List .List

    I Now, we can start giving semantics to spawn.

  • Multiplicity

    I We should be able to create multiple threads

    I Thus, we specify this in the configuration:

    $PGM:Stmt .Map .List

    .Map .List .List

    I Now, we can start giving semantics to spawn.

  • Multiplicity

    I We should be able to create multiple threadsI Thus, we specify this in the configuration:

    $PGM:Stmt .Map .List

    .Map .List .List

    I Now, we can start giving semantics to spawn.

  • Multiplicity

    I We should be able to create multiple threadsI Thus, we specify this in the configuration:

    $PGM:Stmt .Map .List

    .Map .List .List

    I Now, we can start giving semantics to spawn.

  • Sematics of spawn

    I First, the rule for spawn:// spawnrule (spawn S => .) ... Rho

    (.Bag => ... S Rho

    ...)

    I Note that the configuration abstraction algorithm fills themissing cells!

  • Sematics of spawn

    I First, the rule for spawn:// spawnrule (spawn S => .) ... Rho

    (.Bag => ... S Rho

    ...)

    I Note that the configuration abstraction algorithm fills themissing cells!

  • Execute programs with threads

    I Append rule tags: DEMO.

    :-$ kompile --transition ""

    :-$ krun --search

    I Note: is a list of rule attributes separated bywhitespace

  • Execute programs with threads

    I Append rule tags: DEMO.

    :-$ kompile --transition ""

    :-$ krun --search

    I Note: is a list of rule attributes separated bywhitespace

  • Execute programs with threads

    I Append rule tags: DEMO.

    :-$ kompile --transition ""

    :-$ krun --search

    I Note: is a list of rule attributes separated bywhitespace

  • Execute programs with threads

    I Append rule tags: DEMO.

    :-$ kompile --transition ""

    :-$ krun --search

    I Note: is a list of rule attributes separated bywhitespace

  • Thread termination

    I Thread termination rule:

    rule ( . ... => .Bag)

    I DEMO

  • Thread termination

    I Thread termination rule:

    rule ( . ... => .Bag)

    I DEMO

  • Thread termination

    I Thread termination rule:

    rule ( . ... => .Bag)

    I DEMO

  • Challenge/Exercise:

    halt: a statement that ends the execution immediately

  • Lab this week

    I Continue your work on your language definition.

    Exploring non-determinism in KThreads