Formalising an intermediate language for POSIX shell Nicolas - - PowerPoint PPT Presentation

formalising an intermediate language for posix shell
SMART_READER_LITE
LIVE PREVIEW

Formalising an intermediate language for POSIX shell Nicolas - - PowerPoint PPT Presentation

Formalising an intermediate language for POSIX shell Nicolas Jeannerod S eminaire Gallium, Septembre 18, 2017 Nicolas Jeannerod S eminaire Gallium September 18, 2017 1 / 35 Big picture Nicolas Jeannerod S eminaire Gallium


slide-1
SLIDE 1

Formalising an intermediate language for POSIX shell

Nicolas Jeannerod S´ eminaire Gallium, Septembre 18, 2017

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 1 / 35

slide-2
SLIDE 2

Big picture

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 2 / 35

slide-3
SLIDE 3

Big picture

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 2 / 35

slide-4
SLIDE 4

Big picture

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 2 / 35

slide-5
SLIDE 5

Big picture

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 2 / 35

slide-6
SLIDE 6

Big picture

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 2 / 35

slide-7
SLIDE 7

Big picture

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 2 / 35

slide-8
SLIDE 8

Gallery of horrors in shell Dynamic!

Table of Contents

  • 1. Gallery of horrors in shell

Dynamic! Expansion Inconstant semantics Control flow

  • 2. The CoLiS language

Requirements Definitions

  • 3. Formalisation

Formulation Proof

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 3 / 35

slide-9
SLIDE 9

Gallery of horrors in shell Dynamic!

Execute arbitrary strings

Execute commands from strings:

a=’echo foo’ $a ## prints "foo"

  • r any code with eval:

eval "if true; then echo foo; fi"

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 4 / 35

slide-10
SLIDE 10

Gallery of horrors in shell Dynamic!

Execute arbitrary strings

Execute commands from strings:

a=’echo foo’ $a ## prints "foo"

  • r any code with eval:

eval "if true; then echo foo; fi"

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 4 / 35

slide-11
SLIDE 11

Gallery of horrors in shell Dynamic!

Dynamic

Everything is dynamic:

f () { g; } g () { a=bar; } a=foo f echo $a ## prints "bar"

I tell ya, everything!

f () { echo $a; } a=foo a=bar f ## prints "bar" echo $a ## prints "bar"

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 5 / 35

slide-12
SLIDE 12

Gallery of horrors in shell Dynamic!

Dynamic

Everything is dynamic:

f () { g; } g () { a=bar; } a=foo f echo $a ## prints "bar"

I tell ya, everything!

f () { echo $a; } a=foo a=bar f ## prints "bar" echo $a ## prints "bar"

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 5 / 35

slide-13
SLIDE 13

Gallery of horrors in shell Dynamic!

Dynamic

Everything is dynamic:

f () { g; } g () { a=bar; } a=foo f echo $a ## prints "bar"

I tell ya, everything!

f () { echo $a; } a=foo a=bar f ## prints "bar" echo $a ## prints "bar"

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 5 / 35

slide-14
SLIDE 14

Gallery of horrors in shell Dynamic!

Dynamic

Everything is dynamic:

f () { g; } g () { a=bar; } a=foo f echo $a ## prints "bar"

I tell ya, everything!

f () { echo $a; } a=foo a=bar f ## prints "bar" echo $a ## prints "bar"

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 5 / 35

slide-15
SLIDE 15

Gallery of horrors in shell Dynamic!

Dynamic

Everything is dynamic:

f () { g; } g () { a=bar; } a=foo f echo $a ## prints "bar"

I tell ya, everything!

f () { echo $a; } a=foo a=bar f ## prints "bar" echo $a ## prints "bar"

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 5 / 35

slide-16
SLIDE 16

Gallery of horrors in shell Dynamic!

Dynamic

Everything is dynamic:

f () { g; } g () { a=bar; } a=foo f echo $a ## prints "bar"

I tell ya, everything!

f () { echo $a; } a=foo a=bar f ## prints "bar" echo $a ## prints "bar"

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 5 / 35

slide-17
SLIDE 17

Gallery of horrors in shell Expansion

Table of Contents

  • 1. Gallery of horrors in shell

Dynamic! Expansion Inconstant semantics Control flow

  • 2. The CoLiS language

Requirements Definitions

  • 3. Formalisation

Formulation Proof

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 6 / 35

slide-18
SLIDE 18

Gallery of horrors in shell Expansion

All it can contain

Literals Tildes Parameters (i.e. variables) Special parameters “Formatted” parameters Arithmetic Globs Commands Quotes

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 7 / 35

slide-19
SLIDE 19

Gallery of horrors in shell Expansion

All it can contain

Literals Tildes

~/ Pictures ~user/Pictures :~/ Download

Parameters (i.e. variables) Special parameters “Formatted” parameters Arithmetic Globs Commands Quotes

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 7 / 35

slide-20
SLIDE 20

Gallery of horrors in shell Expansion

All it can contain

Literals Tildes Parameters (i.e. variables)

$foo $bar

Special parameters “Formatted” parameters Arithmetic Globs Commands Quotes

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 7 / 35

slide-21
SLIDE 21

Gallery of horrors in shell Expansion

All it can contain

Literals Tildes Parameters (i.e. variables) Special parameters

$@ $* $1, $2, ...

“Formatted” parameters Arithmetic Globs Commands Quotes

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 7 / 35

slide-22
SLIDE 22

Gallery of horrors in shell Expansion

All it can contain

Literals Tildes Parameters (i.e. variables) Special parameters “Formatted” parameters

${foo:-bar} ${foo -baz} ${foo %.*} ${foo##*/}

Arithmetic Globs Commands Quotes

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 7 / 35

slide-23
SLIDE 23

Gallery of horrors in shell Expansion

All it can contain

Literals Tildes Parameters (i.e. variables) Special parameters “Formatted” parameters Arithmetic

$((1 + x + $x))

Globs Commands Quotes

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 7 / 35

slide-24
SLIDE 24

Gallery of horrors in shell Expansion

All it can contain

Literals Tildes Parameters (i.e. variables) Special parameters “Formatted” parameters Arithmetic Globs

/home /[!a]* *.ml *.ml?

Commands Quotes

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 7 / 35

slide-25
SLIDE 25

Gallery of horrors in shell Expansion

All it can contain

Literals Tildes Parameters (i.e. variables) Special parameters “Formatted” parameters Arithmetic Globs Commands

$(echo foo) ‘echo \‘echo foo\‘‘ $(which curl)

Quotes

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 7 / 35

slide-26
SLIDE 26

Gallery of horrors in shell Expansion

All it can contain

Literals Tildes Parameters (i.e. variables) Special parameters “Formatted” parameters Arithmetic Globs Commands Quotes

foo=’my file ’ rm $foo ’$foo ’ "$foo"

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 7 / 35

slide-27
SLIDE 27

Gallery of horrors in shell Expansion

Dirty uses

Abused to represent both strings and lists of strings:

path=’/home ’ path="$path/nicolas" ## "/ home/nicolas" args=’-l -a’ args="$args -h" ## ["-l"; "-a"; "-h"] ls $args $path

Or lists separated by something else than space:

PATH=’/usr/local/bin:/usr/bin:/bin’ IFS=: for dir in $PATH; do echo $dir done

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 8 / 35

slide-28
SLIDE 28

Gallery of horrors in shell Expansion

Dirty uses

Abused to represent both strings and lists of strings:

path=’/home ’ path="$path/nicolas" ## "/ home/nicolas" args=’-l -a’ args="$args -h" ## ["-l"; "-a"; "-h"] ls $args $path

Or lists separated by something else than space:

PATH=’/usr/local/bin:/usr/bin:/bin’ IFS=: for dir in $PATH; do echo $dir done

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 8 / 35

slide-29
SLIDE 29

Gallery of horrors in shell Expansion

Dirty uses

Abused to represent both strings and lists of strings:

path=’/home ’ path="$path/nicolas" ## "/ home/nicolas" args=’-l -a’ args="$args -h" ## ["-l"; "-a"; "-h"] ls $args $path

Or lists separated by something else than space:

PATH=’/usr/local/bin:/usr/bin:/bin’ IFS=: for dir in $PATH; do echo $dir done

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 8 / 35

slide-30
SLIDE 30

Gallery of horrors in shell Expansion

Dirty uses

Abused to represent both strings and lists of strings:

path=’/home ’ path="$path/nicolas" ## "/ home/nicolas" args=’-l -a’ args="$args -h" ## ["-l"; "-a"; "-h"] ls $args $path

Or lists separated by something else than space:

PATH=’/usr/local/bin:/usr/bin:/bin’ IFS=: for dir in $PATH; do echo $dir done

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 8 / 35

slide-31
SLIDE 31

Gallery of horrors in shell Expansion

Dirty uses

Abused to represent both strings and lists of strings:

path=’/home ’ path="$path/nicolas" ## "/ home/nicolas" args=’-l -a’ args="$args -h" ## ["-l"; "-a"; "-h"] ls $args $path

Or lists separated by something else than space:

PATH=’/usr/local/bin:/usr/bin:/bin’ IFS=: for dir in $PATH; do echo $dir done

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 8 / 35

slide-32
SLIDE 32

Gallery of horrors in shell Expansion

Dirty uses

Abused to represent both strings and lists of strings:

path=’/home ’ path="$path/nicolas" ## "/ home/nicolas" args=’-l -a’ args="$args -h" ## ["-l"; "-a"; "-h"] ls $args $path

Or lists separated by something else than space:

PATH=’/usr/local/bin:/usr/bin:/bin’ IFS=: for dir in $PATH; do echo $dir done

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 8 / 35

slide-33
SLIDE 33

Gallery of horrors in shell Inconstant semantics

Table of Contents

  • 1. Gallery of horrors in shell

Dynamic! Expansion Inconstant semantics Control flow

  • 2. The CoLiS language

Requirements Definitions

  • 3. Formalisation

Formulation Proof

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 9 / 35

slide-34
SLIDE 34

Gallery of horrors in shell Inconstant semantics

Dynamic changes in the semantics: IFS

file=’git -sucks ’ rm -r $file ## deletes "git -sucks" IFS=- rm -r $file ## deletes "git" and "sucks"

Here is what happens:

1 The parsing gives us ["rm"; "-r"; "$file"]; 2 We apply parameter expansion and get ["rm"; "-r"; "git-sucks"]; 3 We apply field splitting, but only where we just applied the

parameter expansion: [["rm"]; ["-r"]; ["git"; "sucks"]];

4 We flatten everything: ["rm"; "-r"; "git"; "sucks"]; 5 We evaluate that so-called simple command. Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 10 / 35

slide-35
SLIDE 35

Gallery of horrors in shell Inconstant semantics

Dynamic changes in the semantics: IFS

file=’git -sucks ’ rm -r $file ## deletes "git -sucks" IFS=- rm -r $file ## deletes "git" and "sucks"

Here is what happens:

1 The parsing gives us ["rm"; "-r"; "$file"]; 2 We apply parameter expansion and get ["rm"; "-r"; "git-sucks"]; 3 We apply field splitting, but only where we just applied the

parameter expansion: [["rm"]; ["-r"]; ["git"; "sucks"]];

4 We flatten everything: ["rm"; "-r"; "git"; "sucks"]; 5 We evaluate that so-called simple command. Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 10 / 35

slide-36
SLIDE 36

Gallery of horrors in shell Inconstant semantics

Dynamic changes in the semantics: IFS

file=’git -sucks ’ rm -r $file ## deletes "git -sucks" IFS=- rm -r $file ## deletes "git" and "sucks"

Here is what happens:

1 The parsing gives us ["rm"; "-r"; "$file"]; 2 We apply parameter expansion and get ["rm"; "-r"; "git-sucks"]; 3 We apply field splitting, but only where we just applied the

parameter expansion: [["rm"]; ["-r"]; ["git"; "sucks"]];

4 We flatten everything: ["rm"; "-r"; "git"; "sucks"]; 5 We evaluate that so-called simple command. Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 10 / 35

slide-37
SLIDE 37

Gallery of horrors in shell Inconstant semantics

Dynamic changes in the semantics: IFS

file=’git -sucks ’ rm -r $file ## deletes "git -sucks" IFS=- rm -r $file ## deletes "git" and "sucks"

Here is what happens:

1 The parsing gives us ["rm"; "-r"; "$file"]; 2 We apply parameter expansion and get ["rm"; "-r"; "git-sucks"]; 3 We apply field splitting, but only where we just applied the

parameter expansion: [["rm"]; ["-r"]; ["git"; "sucks"]];

4 We flatten everything: ["rm"; "-r"; "git"; "sucks"]; 5 We evaluate that so-called simple command. Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 10 / 35

slide-38
SLIDE 38

Gallery of horrors in shell Inconstant semantics

Dynamic changes in the semantics: IFS

file=’git -sucks ’ rm -r $file ## deletes "git -sucks" IFS=- rm -r $file ## deletes "git" and "sucks"

Here is what happens:

1 The parsing gives us ["rm"; "-r"; "$file"]; 2 We apply parameter expansion and get ["rm"; "-r"; "git-sucks"]; 3 We apply field splitting, but only where we just applied the

parameter expansion: [["rm"]; ["-r"]; ["git"; "sucks"]];

4 We flatten everything: ["rm"; "-r"; "git"; "sucks"]; 5 We evaluate that so-called simple command. Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 10 / 35

slide-39
SLIDE 39

Gallery of horrors in shell Inconstant semantics

Dynamic changes in the semantics: IFS

file=’git -sucks ’ rm -r $file ## deletes "git -sucks" IFS=- rm -r $file ## deletes "git" and "sucks"

Here is what happens:

1 The parsing gives us ["rm"; "-r"; "$file"]; 2 We apply parameter expansion and get ["rm"; "-r"; "git-sucks"]; 3 We apply field splitting, but only where we just applied the

parameter expansion: [["rm"]; ["-r"]; ["git"; "sucks"]];

4 We flatten everything: ["rm"; "-r"; "git"; "sucks"]; 5 We evaluate that so-called simple command. Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 10 / 35

slide-40
SLIDE 40

Gallery of horrors in shell Inconstant semantics

Dynamic changes in the semantics: set

With set:

  • a Every assignment becomes an export;
  • C > no longer overwrite existing files. >| still does;
  • e The shell shall exit immediately when a command fails, when this

failure is not caught;

  • f Disables pathname expansion;
  • u The shell shall fail when expanding parameters that are unset.

It makes you wonder why most of these options are disabled by default.

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 11 / 35

slide-41
SLIDE 41

Gallery of horrors in shell Inconstant semantics

Dynamic changes in the semantics: set

With set:

  • a Every assignment becomes an export;
  • C > no longer overwrite existing files. >| still does;

echo foo > file set -C echo bar > file ## fails echo baz >| file ## succeeds

  • e The shell shall exit immediately when a command fails, when this

failure is not caught;

  • f Disables pathname expansion;
  • u The shell shall fail when expanding parameters that are unset.

It makes you wonder why most of these options are disabled by default.

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 11 / 35

slide-42
SLIDE 42

Gallery of horrors in shell Inconstant semantics

Dynamic changes in the semantics: set

With set:

  • a Every assignment becomes an export;
  • C > no longer overwrite existing files. >| still does;
  • e The shell shall exit immediately when a command fails, when this

failure is not caught;

set -e ! true ; echo foo ## prints "foo" false ; echo foo ## exists

  • f Disables pathname expansion;
  • u The shell shall fail when expanding parameters that are unset.

It makes you wonder why most of these options are disabled by default.

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 11 / 35

slide-43
SLIDE 43

Gallery of horrors in shell Inconstant semantics

Dynamic changes in the semantics: set

With set:

  • a Every assignment becomes an export;
  • C > no longer overwrite existing files. >| still does;
  • e The shell shall exit immediately when a command fails, when this

failure is not caught;

  • f Disables pathname expansion;

echo * ## prints the files in $PWD set -f echo * ## prints "*"

  • u The shell shall fail when expanding parameters that are unset.

It makes you wonder why most of these options are disabled by default.

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 11 / 35

slide-44
SLIDE 44

Gallery of horrors in shell Inconstant semantics

Dynamic changes in the semantics: set

With set:

  • a Every assignment becomes an export;
  • C > no longer overwrite existing files. >| still does;
  • e The shell shall exit immediately when a command fails, when this

failure is not caught;

  • f Disables pathname expansion;
  • u The shell shall fail when expanding parameters that are unset.

rm -rf "$dir"/ ## deletes everything set -u rm -rf "$dir"/ ## fails

It makes you wonder why most of these options are disabled by default.

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 11 / 35

slide-45
SLIDE 45

Gallery of horrors in shell Inconstant semantics

Dynamic changes in the semantics: set

With set:

  • a Every assignment becomes an export;
  • C > no longer overwrite existing files. >| still does;
  • e The shell shall exit immediately when a command fails, when this

failure is not caught;

  • f Disables pathname expansion;
  • u The shell shall fail when expanding parameters that are unset.

It makes you wonder why most of these options are disabled by default.

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 11 / 35

slide-46
SLIDE 46

Gallery of horrors in shell Control flow

Table of Contents

  • 1. Gallery of horrors in shell

Dynamic! Expansion Inconstant semantics Control flow

  • 2. The CoLiS language

Requirements Definitions

  • 3. Formalisation

Formulation Proof

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 12 / 35

slide-47
SLIDE 47

Gallery of horrors in shell Control flow

Behaviours

Let us play with exit:

exit | echo ’foo’ ## prints "foo" exit || echo ’foo’ ## exits exit & echo ’foo’ ## prints "foo" exit && echo ’foo’ ## exits echo ’foo’ | exit ## does nothing echo ’foo’ || exit ## prints "foo" echo ’foo’ & exit ## prints "foo" and exits echo ’foo’ && exit ## prints "foo" and exits

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 13 / 35

slide-48
SLIDE 48

Gallery of horrors in shell Control flow

Behaviours

Let us play with exit:

exit | echo ’foo’ ## prints "foo" exit || echo ’foo’ ## exits exit & echo ’foo’ ## prints "foo" exit && echo ’foo’ ## exits echo ’foo’ | exit ## does nothing echo ’foo’ || exit ## prints "foo" echo ’foo’ & exit ## prints "foo" and exits echo ’foo’ && exit ## prints "foo" and exits

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 13 / 35

slide-49
SLIDE 49

Gallery of horrors in shell Control flow

Behaviours

Let us play with exit:

exit | echo ’foo’ ## prints "foo" exit || echo ’foo’ ## exits exit & echo ’foo’ ## prints "foo" exit && echo ’foo’ ## exits echo ’foo’ | exit ## does nothing echo ’foo’ || exit ## prints "foo" echo ’foo’ & exit ## prints "foo" and exits echo ’foo’ && exit ## prints "foo" and exits

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 13 / 35

slide-50
SLIDE 50

Gallery of horrors in shell Control flow

Behaviours

Let us play with exit:

exit | echo ’foo’ ## prints "foo" exit || echo ’foo’ ## exits exit & echo ’foo’ ## prints "foo" exit && echo ’foo’ ## exits echo ’foo’ | exit ## does nothing echo ’foo’ || exit ## prints "foo" echo ’foo’ & exit ## prints "foo" and exits echo ’foo’ && exit ## prints "foo" and exits

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 13 / 35

slide-51
SLIDE 51

Gallery of horrors in shell Control flow

The incredible story of set -e

When this option is on, when any command fails, the shell immediately shall exit, as if by executing the exit special built-in utility with no arguments, with the following exceptions: [...] Snippet 1:

false; echo ’foo’

Snippet 2:

{ false; echo ’foo’; } && echo ’bar’

Snippet 3:

{ false; echo ’foo’; } | echo ’bar’

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 14 / 35

slide-52
SLIDE 52

Gallery of horrors in shell Control flow

The incredible story of set -e

When this option is on, when any command fails, the shell immediately shall exit, as if by executing the exit special built-in utility with no arguments, with the following exceptions: [...] Snippet 1:

false; echo ’foo’

Snippet 2:

{ false; echo ’foo’; } && echo ’bar’

Snippet 3:

{ false; echo ’foo’; } | echo ’bar’

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 14 / 35

slide-53
SLIDE 53

Gallery of horrors in shell Control flow

The incredible story of set -e

When this option is on, when any command fails, the shell immediately shall exit, as if by executing the exit special built-in utility with no arguments, with the following exceptions: [...] Snippet 1 (exits):

false; echo ’foo’

Snippet 2:

{ false; echo ’foo’; } && echo ’bar’

Snippet 3:

{ false; echo ’foo’; } | echo ’bar’

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 14 / 35

slide-54
SLIDE 54

Gallery of horrors in shell Control flow

The incredible story of set -e

When this option is on, when any command fails, the shell immediately shall exit, as if by executing the exit special built-in utility with no arguments, with the following exceptions: [...] Snippet 1 (exits):

false; echo ’foo’

Snippet 2:

{ false; echo ’foo’; } && echo ’bar’

Snippet 3:

{ false; echo ’foo’; } | echo ’bar’

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 14 / 35

slide-55
SLIDE 55

Gallery of horrors in shell Control flow

The incredible story of set -e

When this option is on, when any command fails, the shell immediately shall exit, as if by executing the exit special built-in utility with no arguments, with the following exceptions: [...] Snippet 1 (exits):

false; echo ’foo’

Snippet 2 (prints “foo bar”):

{ false; echo ’foo’; } && echo ’bar’

Snippet 3:

{ false; echo ’foo’; } | echo ’bar’

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 14 / 35

slide-56
SLIDE 56

Gallery of horrors in shell Control flow

The incredible story of set -e

When this option is on, when any command fails, the shell immediately shall exit, as if by executing the exit special built-in utility with no arguments, with the following exceptions: [...] Snippet 1 (exits):

false; echo ’foo’

Snippet 2 (prints “foo bar”):

{ false; echo ’foo’; } && echo ’bar’

Snippet 3:

{ false; echo ’foo’; } | echo ’bar’

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 14 / 35

slide-57
SLIDE 57

Gallery of horrors in shell Control flow

The incredible story of set -e

When this option is on, when any command fails, the shell immediately shall exit, as if by executing the exit special built-in utility with no arguments, with the following exceptions: [...] Snippet 1 (exits):

false; echo ’foo’

Snippet 2 (prints “foo bar”):

{ false; echo ’foo’; } && echo ’bar’

Snippet 3 (prints “bar”):

{ false; echo ’foo’; } | echo ’bar’

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 14 / 35

slide-58
SLIDE 58

The CoLiS language Requirements

Table of Contents

  • 1. Gallery of horrors in shell

Dynamic! Expansion Inconstant semantics Control flow

  • 2. The CoLiS language

Requirements Definitions

  • 3. Formalisation

Formulation Proof

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 15 / 35

slide-59
SLIDE 59

The CoLiS language Requirements

Requirements

Intermediate language for a subset of shell; Not a replacement of shell; Well-defined and easily understandable semantics:

Some typing (strings vs. string lists), Variables and functions declared in a header, Dangers made more explicit;

“Close enough” to shell:

We must be convinced that it shares the same semantics as the shell, Target of an automated translation from shell.

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 16 / 35

slide-60
SLIDE 60

The CoLiS language Requirements

Requirements

Intermediate language for a subset of shell; Not a replacement of shell; Well-defined and easily understandable semantics:

Some typing (strings vs. string lists), Variables and functions declared in a header, Dangers made more explicit;

“Close enough” to shell:

We must be convinced that it shares the same semantics as the shell, Target of an automated translation from shell.

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 16 / 35

slide-61
SLIDE 61

The CoLiS language Requirements

Requirements

Intermediate language for a subset of shell; Not a replacement of shell; Well-defined and easily understandable semantics:

Some typing (strings vs. string lists), Variables and functions declared in a header, Dangers made more explicit;

“Close enough” to shell:

We must be convinced that it shares the same semantics as the shell, Target of an automated translation from shell.

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 16 / 35

slide-62
SLIDE 62

The CoLiS language Requirements

Requirements

Intermediate language for a subset of shell; Not a replacement of shell; Well-defined and easily understandable semantics:

Some typing (strings vs. string lists), Variables and functions declared in a header, Dangers made more explicit;

“Close enough” to shell:

We must be convinced that it shares the same semantics as the shell, Target of an automated translation from shell.

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 16 / 35

slide-63
SLIDE 63

The CoLiS language Requirements

Requirements

Intermediate language for a subset of shell; Not a replacement of shell; Well-defined and easily understandable semantics:

Some typing (strings vs. string lists), Variables and functions declared in a header, Dangers made more explicit;

“Close enough” to shell:

We must be convinced that it shares the same semantics as the shell, Target of an automated translation from shell.

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 16 / 35

slide-64
SLIDE 64

The CoLiS language Requirements

Requirements

Intermediate language for a subset of shell; Not a replacement of shell; Well-defined and easily understandable semantics:

Some typing (strings vs. string lists), Variables and functions declared in a header, Dangers made more explicit;

“Close enough” to shell:

We must be convinced that it shares the same semantics as the shell, Target of an automated translation from shell.

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 16 / 35

slide-65
SLIDE 65

The CoLiS language Requirements

Requirements

Intermediate language for a subset of shell; Not a replacement of shell; Well-defined and easily understandable semantics:

Some typing (strings vs. string lists), Variables and functions declared in a header, Dangers made more explicit;

“Close enough” to shell:

We must be convinced that it shares the same semantics as the shell, Target of an automated translation from shell.

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 16 / 35

slide-66
SLIDE 66

The CoLiS language Requirements

Requirements

Intermediate language for a subset of shell; Not a replacement of shell; Well-defined and easily understandable semantics:

Some typing (strings vs. string lists), Variables and functions declared in a header, Dangers made more explicit;

“Close enough” to shell:

We must be convinced that it shares the same semantics as the shell, Target of an automated translation from shell.

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 16 / 35

slide-67
SLIDE 67

The CoLiS language Requirements

Requirements

Intermediate language for a subset of shell; Not a replacement of shell; Well-defined and easily understandable semantics:

Some typing (strings vs. string lists), Variables and functions declared in a header, Dangers made more explicit;

“Close enough” to shell:

We must be convinced that it shares the same semantics as the shell, Target of an automated translation from shell.

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 16 / 35

slide-68
SLIDE 68

The CoLiS language Definitions

Table of Contents

  • 1. Gallery of horrors in shell

Dynamic! Expansion Inconstant semantics Control flow

  • 2. The CoLiS language

Requirements Definitions

  • 3. Formalisation

Formulation Proof

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 17 / 35

slide-69
SLIDE 69

The CoLiS language Definitions

Syntax

Programs p ::= vdecl∗ pdecl∗ program t Variables decl. vdecl ::= varstring xs | varlist xl Procedures decl. pdecl ::= proc c is t Terms t ::= true | false | fatal | return t | exit t | xs := s | xl := l | t ; t | if t then t else t | for xs in l do t | while t do t | process t | pipe t into t | call l | shift

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 18 / 35

slide-70
SLIDE 70

The CoLiS language Definitions

Syntax

Programs p ::= vdecl∗ pdecl∗ program t Variables decl. vdecl ::= varstring xs | varlist xl Procedures decl. pdecl ::= proc c is t Terms t ::= true | false | fatal | return t | exit t | xs := s | xl := l | t ; t | if t then t else t | for xs in l do t | while t do t | process t | pipe t into t | call l | shift

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 18 / 35

slide-71
SLIDE 71

The CoLiS language Definitions

Syntax

Programs p ::= vdecl∗ pdecl∗ program t Variables decl. vdecl ::= varstring xs | varlist xl Procedures decl. pdecl ::= proc c is t Terms t ::= true | false | fatal | return t | exit t | xs := s | xl := l | t ; t | if t then t else t | for xs in l do t | while t do t | process t | pipe t into t | call l | shift

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 18 / 35

slide-72
SLIDE 72

The CoLiS language Definitions

Syntax

Programs p ::= vdecl∗ pdecl∗ program t Variables decl. vdecl ::= varstring xs | varlist xl Procedures decl. pdecl ::= proc c is t Terms t ::= true | false | fatal | return t | exit t | xs := s | xl := l | t ; t | if t then t else t | for xs in l do t | while t do t | process t | pipe t into t | call l | shift

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 18 / 35

slide-73
SLIDE 73

The CoLiS language Definitions

Syntax

Terms t ::= true | false | fatal | return t | exit t | xs := s | xl := l | t ; t | if t then t else t | for xs in l do t | while t do t | process t | pipe t into t | call l | shift String expressions s ::= nils | fs :: s String fragments fs ::= σ | xs | n | t List expressions l ::= nill | fl :: l List fragments fl ::= s | split s | xl

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 18 / 35

slide-74
SLIDE 74

The CoLiS language Definitions

Syntax

Terms t ::= true | false | fatal | return t | exit t | xs := s | xl := l | t ; t | if t then t else t | for xs in l do t | while t do t | process t | pipe t into t | call l | shift String expressions s ::= nils | fs :: s String fragments fs ::= σ | xs | n | t List expressions l ::= nill | fl :: l List fragments fl ::= s | split s | xl

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 18 / 35

slide-75
SLIDE 75

The CoLiS language Definitions

Semantic judgements

t/Γ ⇓ σ ⋆ b/Γ′ A context Γ contains: flags? a file system, the standard input, the arguments line, environments for string and list variables, an environment for procedures. A behaviour b can be True, False, Fatal, Return (True|False) or Exit (True|False).

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 19 / 35

slide-76
SLIDE 76

The CoLiS language Definitions

Semantic judgements

t/Γ ⇓ σ ⋆ b/Γ′ A context Γ contains: flags? a file system, the standard input, the arguments line, environments for string and list variables, an environment for procedures. A behaviour b can be True, False, Fatal, Return (True|False) or Exit (True|False).

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 19 / 35

slide-77
SLIDE 77

The CoLiS language Definitions

Semantic judgements

t/Γ ⇓ σ ⋆ b/Γ′ A context Γ contains: flags? a file system, the standard input, the arguments line, environments for string and list variables, an environment for procedures. A behaviour b can be True, False, Fatal, Return (True|False) or Exit (True|False).

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 19 / 35

slide-78
SLIDE 78

The CoLiS language Definitions

Semantic judgements

t/Γ ⇓ σ ⋆ b/Γ′ A context Γ contains: flags? a file system, the standard input, the arguments line, environments for string and list variables, an environment for procedures. A behaviour b can be True, False, Fatal, Return (True|False) or Exit (True|False).

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 19 / 35

slide-79
SLIDE 79

The CoLiS language Definitions

Semantic judgements

t/Γ ⇓ σ ⋆ b/Γ′ A context Γ contains: flags? a file system, the standard input, the arguments line, environments for string and list variables, an environment for procedures. A behaviour b can be True, False, Fatal, Return (True|False) or Exit (True|False).

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 19 / 35

slide-80
SLIDE 80

The CoLiS language Definitions

Semantic judgements

t/Γ ⇓ σ ⋆ b/Γ′ A context Γ contains: flags? a file system, the standard input, the arguments line, environments for string and list variables, an environment for procedures. A behaviour b can be True, False, Fatal, Return (True|False) or Exit (True|False).

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 19 / 35

slide-81
SLIDE 81

The CoLiS language Definitions

Semantic judgements

t/Γ ⇓ σ ⋆ b/Γ′ A context Γ contains: flags? a file system, the standard input, the arguments line, environments for string and list variables, an environment for procedures. A behaviour b can be True, False, Fatal, Return (True|False) or Exit (True|False).

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 19 / 35

slide-82
SLIDE 82

The CoLiS language Definitions

Semantic judgements

t/Γ ⇓ σ ⋆ b/Γ′ A context Γ contains: flags? a file system, the standard input, the arguments line, environments for string and list variables, an environment for procedures. A behaviour b can be True, False, Fatal, Return (True|False) or Exit (True|False).

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 19 / 35

slide-83
SLIDE 83

The CoLiS language Definitions

Semantic judgements

t/Γ ⇓ σ ⋆ b/Γ′ A context Γ contains: flags? a file system, the standard input, the arguments line, environments for string and list variables, an environment for procedures. A behaviour b can be True, False, Fatal, Return (True|False) or Exit (True|False).

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 19 / 35

slide-84
SLIDE 84

The CoLiS language Definitions

Semantic judgements

t/Γ ⇓ σ ⋆ b/Γ′ A context Γ contains: flags? a file system, the standard input, the arguments line, environments for string and list variables, an environment for procedures. A behaviour b can be True, False, Fatal, Return (True|False) or Exit (True|False).

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 19 / 35

slide-85
SLIDE 85

The CoLiS language Definitions

Semantic rules – Branching

Branching-True

t1/Γ ⇓ σ1 ⋆ b1/Γ1 b1 = True t2/Γ1 ⇓ σ2 ⋆ b2/Γ2 (if t1 then t2 else t3)/Γ ⇓ σ1σ2 ⋆ b2/Γ2

Branching-False

t1/Γ ⇓ σ1 ⋆ b1/Γ1 b1 ∈ {False, Fatal} t3/Γ1 ⇓ σ3 ⋆ b3/Γ3 (if t1 then t2 else t3)/Γ ⇓ σ1σ3 ⋆ b3/Γ3

Branching-Exception

t1/Γ ⇓ σ1 ⋆ b1/Γ1 b1 ∈ {Return , Exit } (if t1 then t2 else t3)/Γ ⇓ σ1 ⋆ b1/Γ1

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 20 / 35

slide-86
SLIDE 86

The CoLiS language Definitions

Semantic rules – Branching

Branching-True

t1/Γ ⇓ σ1 ⋆ b1/Γ1 b1 = True t2/Γ1 ⇓ σ2 ⋆ b2/Γ2 (if t1 then t2 else t3)/Γ ⇓ σ1σ2 ⋆ b2/Γ2

Branching-False

t1/Γ ⇓ σ1 ⋆ b1/Γ1 b1 ∈ {False, Fatal} t3/Γ1 ⇓ σ3 ⋆ b3/Γ3 (if t1 then t2 else t3)/Γ ⇓ σ1σ3 ⋆ b3/Γ3

Branching-Exception

t1/Γ ⇓ σ1 ⋆ b1/Γ1 b1 ∈ {Return , Exit } (if t1 then t2 else t3)/Γ ⇓ σ1 ⋆ b1/Γ1

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 20 / 35

slide-87
SLIDE 87

The CoLiS language Definitions

Semantic rules – Branching

Branching-True

t1/Γ ⇓ σ1 ⋆ b1/Γ1 b1 = True t2/Γ1 ⇓ σ2 ⋆ b2/Γ2 (if t1 then t2 else t3)/Γ ⇓ σ1σ2 ⋆ b2/Γ2

Branching-False

t1/Γ ⇓ σ1 ⋆ b1/Γ1 b1 ∈ {False, Fatal} t3/Γ1 ⇓ σ3 ⋆ b3/Γ3 (if t1 then t2 else t3)/Γ ⇓ σ1σ3 ⋆ b3/Γ3

Branching-Exception

t1/Γ ⇓ σ1 ⋆ b1/Γ1 b1 ∈ {Return , Exit } (if t1 then t2 else t3)/Γ ⇓ σ1 ⋆ b1/Γ1

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 20 / 35

slide-88
SLIDE 88

Formalisation Formulation

Table of Contents

  • 1. Gallery of horrors in shell

Dynamic! Expansion Inconstant semantics Control flow

  • 2. The CoLiS language

Requirements Definitions

  • 3. Formalisation

Formulation Proof

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 21 / 35

slide-89
SLIDE 89

Formalisation Formulation

Formalisation

Formalised in the proof environment Why3:

The syntax becomes an algebraic data type, The semantics become an inductive predicate;

Interpreter proven sound and complete:

Written in a “natural way”, Helps detecting potential mistakes in the semantics, More easily readable than the semantics, Allows us to validate the translation by testing.

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 22 / 35

slide-90
SLIDE 90

Formalisation Formulation

Formalisation

Formalised in the proof environment Why3:

The syntax becomes an algebraic data type,

type term = TTrue | TFalse | TFatal | TReturn term | TExit term | TSeq term term | TIf term term term | ...

The semantics become an inductive predicate;

Interpreter proven sound and complete:

Written in a “natural way”, Helps detecting potential mistakes in the semantics, More easily readable than the semantics, Allows us to validate the translation by testing.

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 22 / 35

slide-91
SLIDE 91

Formalisation Formulation

Formalisation

Formalised in the proof environment Why3:

The syntax becomes an algebraic data type, The semantics become an inductive predicate;

inductive eval_term term context string behaviour context = | EvalT_Seq_Normal : forall t1 Γ σ1 b1 Γ1 t2 σ2 b2 Γ2. eval_term t1 Γ σ1 (BNormal b1) Γ1 -> eval_term t2 Γ1 σ2 b2 Γ2 -> eval_term (TSeq t1 t2) Γ (concat σ1 σ2) b2 Γ2

Interpreter proven sound and complete:

Written in a “natural way”, Helps detecting potential mistakes in the semantics, More easily readable than the semantics, Allows us to validate the translation by testing.

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 22 / 35

slide-92
SLIDE 92

Formalisation Formulation

Formalisation

Formalised in the proof environment Why3:

The syntax becomes an algebraic data type, The semantics become an inductive predicate;

Interpreter proven sound and complete:

Written in a “natural way”, Helps detecting potential mistakes in the semantics, More easily readable than the semantics, Allows us to validate the translation by testing.

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 22 / 35

slide-93
SLIDE 93

Formalisation Formulation

Formalisation

Formalised in the proof environment Why3:

The syntax becomes an algebraic data type, The semantics become an inductive predicate;

Interpreter proven sound and complete:

Written in a “natural way”,

exception EFatal context exception EReturn (bool , context) exception EExit (bool , context) let rec interp_term (t: term) (Γ: context) (stdout: ref string) : (bool , context)

Helps detecting potential mistakes in the semantics, More easily readable than the semantics, Allows us to validate the translation by testing.

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 22 / 35

slide-94
SLIDE 94

Formalisation Formulation

Formalisation

Formalised in the proof environment Why3:

The syntax becomes an algebraic data type, The semantics become an inductive predicate;

Interpreter proven sound and complete:

Written in a “natural way”,

exception EFatal context exception EReturn (bool , context) exception EExit (bool , context) let rec interp_term (t: term) (Γ: context) (stdout: ref string) : (bool , context)

Helps detecting potential mistakes in the semantics, More easily readable than the semantics, Allows us to validate the translation by testing.

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 22 / 35

slide-95
SLIDE 95

Formalisation Formulation

Formalisation

Formalised in the proof environment Why3:

The syntax becomes an algebraic data type, The semantics become an inductive predicate;

Interpreter proven sound and complete:

Written in a “natural way”, Helps detecting potential mistakes in the semantics, More easily readable than the semantics,

| TIf t1 t2 t3 -> let (b1 , Γ1) = try interp_term t1 Γ stdout with EFatal Γ1 -> (false , Γ1) end in interp_term (if b1 then t2 else t3) Γ1 stdout

Allows us to validate the translation by testing.

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 22 / 35

slide-96
SLIDE 96

Formalisation Formulation

Formalisation

Formalised in the proof environment Why3:

The syntax becomes an algebraic data type, The semantics become an inductive predicate;

Interpreter proven sound and complete:

Written in a “natural way”, Helps detecting potential mistakes in the semantics, More easily readable than the semantics, Allows us to validate the translation by testing.

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 22 / 35

slide-97
SLIDE 97

Formalisation Formulation

Soundness of the interpreter

We write t/Γ → σ ⋆ b/Γ′ for: “on the input consisting of t, Γ and a reference, the interpreter writes σ at the end of that reference and terminates: normally and outputs (b, Γ′); with an exception corresponding to the behaviour b that carries Γ′.” Theorem (Soundness of the interpreter) For all t, Γ, σ, b and Γ′: if t/Γ → σ ⋆ b/Γ′ then t/Γ ⇓ σ ⋆ b/Γ′

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 23 / 35

slide-98
SLIDE 98

Formalisation Formulation

Soundness of the interpreter

We write t/Γ → σ ⋆ b/Γ′ for: “on the input consisting of t, Γ and a reference, the interpreter writes σ at the end of that reference and terminates: normally and outputs (b, Γ′); with an exception corresponding to the behaviour b that carries Γ′.” Theorem (Soundness of the interpreter) For all t, Γ, σ, b and Γ′: if t/Γ → σ ⋆ b/Γ′ then t/Γ ⇓ σ ⋆ b/Γ′

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 23 / 35

slide-99
SLIDE 99

Formalisation Formulation

Completeness of the interpreter

We write t/Γ → σ ⋆ b/Γ′ for: “on the input consisting of t, Γ and a reference, the interpreter writes σ at the end of that reference and terminates: normally and outputs (b, Γ′); with an exception corresponding to the behaviour b that carries Γ′.” Theorem (Completeness of the interpreter) For all t, Γ, σ, b and Γ′: if t/Γ ⇓ σ ⋆ b/Γ′ then t/Γ → σ ⋆ b/Γ′

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 23 / 35

slide-100
SLIDE 100

Formalisation Formulation

Soundness of the interpreter in Why3

let rec interp_term (t: term) (Γ: context) (stdout: ref string) : (bool , context) diverges returns { (b, Γ’) -> exists σ. !stdout = concat (old !stdout) σ /\ eval_term t Γ σ (BNormal b) Γ’ } raises { EFatal Γ’ -> exists σ. !stdout = concat (old !stdout) σ /\ eval_term t Γ σ BFatal Γ’ } raises { EReturn (b, Γ’) -> exists σ. !stdout = concat (old !stdout) σ /\ eval_term t Γ σ (BReturn b) Γ’ } ...

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 24 / 35

slide-101
SLIDE 101

Formalisation Formulation

Soundness of the interpreter in Why3

let rec interp_term (t: term) (Γ: context) (stdout: ref string) : (bool , context) diverges returns { (b, Γ’) -> exists σ. !stdout = concat (old !stdout) σ /\ eval_term t Γ σ (BNormal b) Γ’ } raises { EFatal Γ’ -> exists σ. !stdout = concat (old !stdout) σ /\ eval_term t Γ σ BFatal Γ’ } raises { EReturn (b, Γ’) -> exists σ. !stdout = concat (old !stdout) σ /\ eval_term t Γ σ (BReturn b) Γ’ } ...

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 24 / 35

slide-102
SLIDE 102

Formalisation Formulation

Completeness of the interpreter in Why3

lemma functionality : forall t Γ σ1 σ2 b1 b2 Γ1 Γ2. eval_term t Γ σ1 b1 Γ1 -> eval_term t Γ σ2 b2 Γ2 -> σ1 = σ2 /\ b1 = b2 /\ Γ1 = Γ2 let rec interp_term (t: term) (Γ: context) (stdout: ref string) : (bool , context) requires { exists σ b Γ’. eval_term t Γ σ b Γ’ } variant { ??? } returns { (b, Γ’) -> exists σ. !stdout = concat (old !stdout) σ /\ eval_term t Γ σ (BNormal b) Γ’ }

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 25 / 35

slide-103
SLIDE 103

Formalisation Formulation

Completeness of the interpreter in Why3

lemma functionality : forall t Γ σ1 σ2 b1 b2 Γ1 Γ2. eval_term t Γ σ1 b1 Γ1 -> eval_term t Γ σ2 b2 Γ2 -> σ1 = σ2 /\ b1 = b2 /\ Γ1 = Γ2 let rec interp_term (t: term) (Γ: context) (stdout: ref string) : (bool , context) requires { exists σ b Γ’. eval_term t Γ σ b Γ’ } variant { ??? } returns { (b, Γ’) -> exists σ. !stdout = concat (old !stdout) σ /\ eval_term t Γ σ (BNormal b) Γ’ }

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 25 / 35

slide-104
SLIDE 104

Formalisation Formulation

Completeness of the interpreter in Why3

lemma functionality : forall t Γ σ1 σ2 b1 b2 Γ1 Γ2. eval_term t Γ σ1 b1 Γ1 -> eval_term t Γ σ2 b2 Γ2 -> σ1 = σ2 /\ b1 = b2 /\ Γ1 = Γ2 let rec interp_term (t: term) (Γ: context) (stdout: ref string) : (bool , context) requires { exists σ b Γ’. eval_term t Γ σ b Γ’ } variant { ??? } returns { (b, Γ’) -> exists σ. !stdout = concat (old !stdout) σ /\ eval_term t Γ σ (BNormal b) Γ’ }

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 25 / 35

slide-105
SLIDE 105

Formalisation Formulation

Completeness of the interpreter in Why3

lemma functionality : forall t Γ σ1 σ2 b1 b2 Γ1 Γ2. eval_term t Γ σ1 b1 Γ1 -> eval_term t Γ σ2 b2 Γ2 -> σ1 = σ2 /\ b1 = b2 /\ Γ1 = Γ2 let rec interp_term (t: term) (Γ: context) (stdout: ref string) : (bool , context) requires { exists σ b Γ’. eval_term t Γ σ b Γ’ } variant { ??? } returns { (b, Γ’) -> exists σ. !stdout = concat (old !stdout) σ /\ eval_term t Γ σ (BNormal b) Γ’ }

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 25 / 35

slide-106
SLIDE 106

Formalisation Proof

Table of Contents

  • 1. Gallery of horrors in shell

Dynamic! Expansion Inconstant semantics Control flow

  • 2. The CoLiS language

Requirements Definitions

  • 3. Formalisation

Formulation Proof

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 26 / 35

slide-107
SLIDE 107

Formalisation Proof

Why it is hard

stdout is a reference. exists σ. !stdout = concat (old !stdout) σ /\ eval_term t Γ σ (BNormal b) Γ’

We cannot provide a witness as a return value here, because of exceptions, We (c|sh)ould change it to something more structured. We decided to use superposition provers.

We need a variant:

The term? The derivation tree of the hypothesis? The height of the derivation tree? The size of the derivation tree? What then?

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 27 / 35

slide-108
SLIDE 108

Formalisation Proof

Why it is hard

stdout is a reference: exists σ. !stdout = concat (old !stdout) σ /\ eval_term t Γ σ (BNormal b) Γ’

We cannot provide a witness as a return value here, because of exceptions, We (c|sh)ould change it to something more structured. We decided to use superposition provers.

We need a variant:

The term? The derivation tree of the hypothesis? The height of the derivation tree? The size of the derivation tree? What then?

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 27 / 35

slide-109
SLIDE 109

Formalisation Proof

Why it is hard

stdout is a reference: exists σ. !stdout = concat (old !stdout) σ /\ eval_term t Γ σ (BNormal b) Γ’

We cannot provide a witness as a return value here, because of exceptions, We (c|sh)ould change it to something more structured. We decided to use superposition provers.

We need a variant:

The term? The derivation tree of the hypothesis? The height of the derivation tree? The size of the derivation tree? What then?

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 27 / 35

slide-110
SLIDE 110

Formalisation Proof

Why it is hard

stdout is a reference: exists σ. !stdout = concat (old !stdout) σ /\ eval_term t Γ σ (BNormal b) Γ’

We cannot provide a witness as a return value here, because of exceptions, We (c|sh)ould change it to something more structured. We decided to use superposition provers.

We need a variant:

The term? The derivation tree of the hypothesis? The height of the derivation tree? The size of the derivation tree? What then?

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 27 / 35

slide-111
SLIDE 111

Formalisation Proof

Why it is hard

stdout is a reference: exists σ. !stdout = concat (old !stdout) σ /\ eval_term t Γ σ (BNormal b) Γ’

We cannot provide a witness as a return value here, because of exceptions, We (c|sh)ould change it to something more structured. We decided to use superposition provers.

We need a variant:

The term? The derivation tree of the hypothesis? The height of the derivation tree? The size of the derivation tree? What then?

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 27 / 35

slide-112
SLIDE 112

Formalisation Proof

Why it is hard

stdout is a reference.

We need a variant:

The term? The derivation tree of the hypothesis? The height of the derivation tree? The size of the derivation tree? What then?

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 27 / 35

slide-113
SLIDE 113

Formalisation Proof

Why it is hard

stdout is a reference.

We need a variant:

The term? The derivation tree of the hypothesis? The height of the derivation tree? The size of the derivation tree? What then?

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 27 / 35

slide-114
SLIDE 114

Formalisation Proof

Why it is hard

stdout is a reference.

We need a variant:

The term? No. t1/Γ ⇓ σ1 ⋆ b1/Γ1 b1 = True t2/Γ ⇓ σ2 ⋆ b2/Γ2 b2 ∈ {True, False} (while t1 do t2)/Γ2 ⇓ σ3 ⋆ b3/Γ3 (while t1 do t2)/Γ ⇓ σ1σ2σ3 ⋆ b3/Γ3 The derivation tree of the hypothesis? The height of the derivation tree? The size of the derivation tree? What then?

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 27 / 35

slide-115
SLIDE 115

Formalisation Proof

Why it is hard

stdout is a reference.

We need a variant:

The term? No. The derivation tree of the hypothesis? The height of the derivation tree? The size of the derivation tree? What then?

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 27 / 35

slide-116
SLIDE 116

Formalisation Proof

Why it is hard

stdout is a reference.

We need a variant:

The term? No. The derivation tree of the hypothesis?

True, but we cannot manipulate them in Why3.

The height of the derivation tree? The size of the derivation tree? What then?

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 27 / 35

slide-117
SLIDE 117

Formalisation Proof

Why it is hard

stdout is a reference.

We need a variant:

The term? No. The derivation tree of the hypothesis? True, but no. The height of the derivation tree? The size of the derivation tree? What then?

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 27 / 35

slide-118
SLIDE 118

Formalisation Proof

Why it is hard

stdout is a reference.

We need a variant:

The term? No. The derivation tree of the hypothesis? True, but no. The height of the derivation tree? Err... no.

Superposition provers are bad with arithmetic, and we need the maximum function and inequalities. Given the height of a derivation tree, we cannot deduce the heights of the premises (only an upper bound).

The size of the derivation tree? What then?

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 27 / 35

slide-119
SLIDE 119

Formalisation Proof

Why it is hard

stdout is a reference.

We need a variant:

The term? No. The derivation tree of the hypothesis? True, but no. The height of the derivation tree? Err... no.

Superposition provers are bad with arithmetic, and we need the maximum function and inequalities. Given the height of a derivation tree, we cannot deduce the heights of the premises (only an upper bound).

The size of the derivation tree? What then?

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 27 / 35

slide-120
SLIDE 120

Formalisation Proof

Why it is hard

stdout is a reference.

We need a variant:

The term? No. The derivation tree of the hypothesis? True, but no. The height of the derivation tree? Err... no. The size of the derivation tree? What then?

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 27 / 35

slide-121
SLIDE 121

Formalisation Proof

Why it is hard

stdout is a reference.

We need a variant:

The term? No. The derivation tree of the hypothesis? True, but no. The height of the derivation tree? Err... no. The size of the derivation tree? Err... no.

Superposition provers are bad with arithmetic, and we need addition and substraction. Given the size of a derivation tree, we cannot deduce the size of the premises.

What then?

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 27 / 35

slide-122
SLIDE 122

Formalisation Proof

Why it is hard

stdout is a reference.

We need a variant:

The term? No. The derivation tree of the hypothesis? True, but no. The height of the derivation tree? Err... no. The size of the derivation tree? Err... no.

Superposition provers are bad with arithmetic, and we need addition and substraction. Given the size of a derivation tree, we cannot deduce the size of the premises.

What then?

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 27 / 35

slide-123
SLIDE 123

Formalisation Proof

Why it is hard

stdout is a reference.

We need a variant:

The term? No. The derivation tree of the hypothesis? True, but no. The height of the derivation tree? Err... no. The size of the derivation tree? Err... no. What then?

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 27 / 35

slide-124
SLIDE 124

Formalisation Proof

Skeletons

We add a skeleton type:

type skeleton = | S0 | S1 skeleton | S2 skeleton skeleton | S3 skeleton skeleton skeleton

It represents the “shape” of the proof.

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 28 / 35

slide-125
SLIDE 125

Formalisation Proof

Skeletons

We add a skeleton type:

type skeleton = | S0 | S1 skeleton | S2 skeleton skeleton | S3 skeleton skeleton skeleton

It represents the “shape” of the proof.

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 28 / 35

slide-126
SLIDE 126

Formalisation Proof

Skeletons

We add a skeleton type:

type skeleton = | S0 | S1 skeleton | S2 skeleton skeleton | S3 skeleton skeleton skeleton

It represents the “shape” of the proof.

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 28 / 35

slide-127
SLIDE 127

Formalisation Proof

Put them everywhere – In the predicate

inductive eval_term term context string behaviour context skeleton = | EvalT_Seq_Normal : forall t1 Γ σ1 b1 Γ1 t2 σ2 b2 Γ2 sk1 sk2. eval_term t1 Γ σ1 (BNormal b1) Γ1 sk1 -> eval_term t2 Γ1 σ2 b2 Γ2 sk2 -> eval_term (TSeq t1 t2) Γ (concat σ1 σ2) b2 Γ2 (S2 sk1 sk2) | EvalT_Seq_Error : forall t1 Γ σ1 b1 Γ1 t2 sk. eval_term t1 Γ σ1 b1 Γ1 sk -> (match b1 with BNormal _ -> false | _ -> true end) -> eval_term (TSeq t1 t2) Γ σ1 b1 Γ1 (S1 sk)

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 29 / 35

slide-128
SLIDE 128

Formalisation Proof

Put them everywhere – In the contract

let rec interp_term (t: term) (Γ: context) (stdout: ref string) (ghost sk: skeleton) : (bool , context) requires { exists s b g’. eval_term t g s b g’ sk } variant { sk } returns { (b, Γ’) -> exists σ. !stdout = concat (old !stdout) σ /\ eval_term t Γ σ (BNormal b) Γ’ sk }

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 30 / 35

slide-129
SLIDE 129

Formalisation Proof

Define some helpers

let ghost skeleton12 (sk: skeleton) requires { match sk with S1 _ | S2 _ _ -> true | _ -> false e ensures { match sk with S1 sk1 | S2 sk1 _ -> result = sk1 | _ = match sk with S1 sk1 | S2 sk1 _ -> sk1 | _ -> absurd end

The following:

let ghost sk1 = skeleton12 sk in

reads: “We know that sk can only have one or two premises and we name the first one sk1.”

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 31 / 35

slide-130
SLIDE 130

Formalisation Proof

Define some helpers

let ghost skeleton12 (sk: skeleton) requires { match sk with S1 _ | S2 _ _ -> true | _ -> false e ensures { match sk with S1 sk1 | S2 sk1 _ -> result = sk1 | _ = match sk with S1 sk1 | S2 sk1 _ -> sk1 | _ -> absurd end

The following:

let ghost sk1 = skeleton12 sk in

reads: “We know that sk can only have one or two premises and we name the first one sk1.”

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 31 / 35

slide-131
SLIDE 131

Formalisation Proof

Put them everywhere – In the code

| TSeq t1 t2 -> let ghost sk1 = skeleton12 sk in let (_, Γ1) = interp_term t1 Γ stdout sk1 in let ghost (_, sk2) = skeleton2 sk in interp_term t2 Γ1 stdout sk2 | TIf t1 t2 t3 -> let (b1, Γ1) = try let ghost sk1 = skeleton12 sk in interp_term t1 Γ stdout sk1 with EFatal Γ’ -> (false , Γ’) end in let ghost (_, sk2) = skeleton2 sk in interp_term (if b1 then t2 else t3) Γ1 stdout sk2

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 32 / 35

slide-132
SLIDE 132

Formalisation Proof

Put them everywhere – In the code

| TSeq t1 t2 -> let ghost sk1 = skeleton12 sk in let (_, Γ1) = interp_term t1 Γ stdout sk1 in let ghost (_, sk2) = skeleton2 sk in interp_term t2 Γ1 stdout sk2 | TIf t1 t2 t3 -> let (b1, Γ1) = try let ghost sk1 = skeleton12 sk in interp_term t1 Γ stdout sk1 with EFatal Γ’ -> (false , Γ’) end in let ghost (_, sk2) = skeleton2 sk in interp_term (if b1 then t2 else t3) Γ1 stdout sk2

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 32 / 35

slide-133
SLIDE 133

Formalisation Proof

And it’s all green!

Soundness Completeness Proof obligations 117 233 Time (seconds) 190 510

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 33 / 35

slide-134
SLIDE 134

Formalisation Proof

Other things about skeletons

Generalisable, if we want more than the shape; Help in writing recursion in case of mutually recursive types; Can really be added automatically to inductive predicates; Works because:

the order of the premises is the order of the execution, the proof tree looks pretty much like the recursive calls tree.

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 34 / 35

slide-135
SLIDE 135

Formalisation Proof

Other things about skeletons

Generalisable, if we want more than the shape; Help in writing recursion in case of mutually recursive types; Can really be added automatically to inductive predicates; Works because:

the order of the premises is the order of the execution, the proof tree looks pretty much like the recursive calls tree.

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 34 / 35

slide-136
SLIDE 136

Formalisation Proof

Other things about skeletons

Generalisable, if we want more than the shape; Help in writing recursion in case of mutually recursive types; Can really be added automatically to inductive predicates; Works because:

the order of the premises is the order of the execution, the proof tree looks pretty much like the recursive calls tree.

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 34 / 35

slide-137
SLIDE 137

Formalisation Proof

Other things about skeletons

Generalisable, if we want more than the shape; Help in writing recursion in case of mutually recursive types; Can really be added automatically to inductive predicates; Works because:

the order of the premises is the order of the execution, the proof tree looks pretty much like the recursive calls tree.

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 34 / 35

slide-138
SLIDE 138

Formalisation Proof

Other things about skeletons

Generalisable, if we want more than the shape; Help in writing recursion in case of mutually recursive types; Can really be added automatically to inductive predicates; Works because:

the order of the premises is the order of the execution, the proof tree looks pretty much like the recursive calls tree.

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 34 / 35

slide-139
SLIDE 139

Thank you for your attention!

Questions? Comments? Suggestions? Claude March´ e, Nicolas Jeannerod and Ralf Treinen A Formally Verified Interpreter for a Shell-like Programming Language VSTTE, July 2017

Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 35 / 35