formalising an intermediate language for posix shell
play

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


  1. 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

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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

  26. 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

  27. 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

  28. 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

  29. 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

  30. 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

  31. 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

  32. 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

  33. 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

  34. 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

  35. 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

  36. 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

  37. 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

  38. The CoLiS language Definitions Syntax vdecl ∗ pdecl ∗ program t p ::= Programs Variables decl. vdecl ::= varstring x s | varlist x l pdecl ::= proc c is t Procedures decl. t ::= true | false | fatal Terms | return t | exit t | x s := s | x l := l | t ; t | if t then t else t | for x s 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

  39. The CoLiS language Definitions Syntax vdecl ∗ pdecl ∗ program t p ::= Programs Variables decl. vdecl ::= varstring x s | varlist x l pdecl ::= proc c is t Procedures decl. t ::= true | false | fatal Terms | return t | exit t | x s := s | x l := l | t ; t | if t then t else t | for x s 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

  40. The CoLiS language Definitions Syntax vdecl ∗ pdecl ∗ program t p ::= Programs Variables decl. vdecl ::= varstring x s | varlist x l pdecl ::= proc c is t Procedures decl. t ::= true | false | fatal Terms | return t | exit t | x s := s | x l := l | t ; t | if t then t else t | for x s 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

  41. The CoLiS language Definitions Syntax vdecl ∗ pdecl ∗ program t p ::= Programs Variables decl. vdecl ::= varstring x s | varlist x l pdecl ::= proc c is t Procedures decl. t ::= true | false | fatal Terms | return t | exit t | x s := s | x l := l | t ; t | if t then t else t | for x s 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

  42. The CoLiS language Definitions Syntax t ::= true | false | fatal Terms | return t | exit t | x s := s | x l := l | t ; t | if t then t else t | for x s in l do t | while t do t | process t | pipe t into t | call l | shift s ::= nil s | f s :: s String expressions String fragments f s ::= σ | x s | n | t l ::= nil l | f l :: l List expressions List fragments f l ::= s | split s | x l Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 18 / 35

  43. The CoLiS language Definitions Syntax t ::= true | false | fatal Terms | return t | exit t | x s := s | x l := l | t ; t | if t then t else t | for x s in l do t | while t do t | process t | pipe t into t | call l | shift s ::= nil s | f s :: s String expressions String fragments f s ::= σ | x s | n | t l ::= nil l | f l :: l List expressions List fragments f l ::= s | split s | x l Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 18 / 35

  44. 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

  45. 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

  46. 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

  47. 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

  48. 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

  49. 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

  50. 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

  51. 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

  52. 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

  53. 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

  54. The CoLiS language Definitions Semantic rules – Branching Branching-True t 1 / Γ ⇓ σ 1 ⋆ b 1 / Γ 1 b 1 = True t 2 / Γ 1 ⇓ σ 2 ⋆ b 2 / Γ 2 ( if t 1 then t 2 else t 3 ) / Γ ⇓ σ 1 σ 2 ⋆ b 2 / Γ 2 Branching-False t 1 / Γ ⇓ σ 1 ⋆ b 1 / Γ 1 b 1 ∈ { False , Fatal } t 3 / Γ 1 ⇓ σ 3 ⋆ b 3 / Γ 3 ( if t 1 then t 2 else t 3 ) / Γ ⇓ σ 1 σ 3 ⋆ b 3 / Γ 3 Branching-Exception t 1 / Γ ⇓ σ 1 ⋆ b 1 / Γ 1 b 1 ∈ { Return , Exit } ( if t 1 then t 2 else t 3 ) / Γ ⇓ σ 1 ⋆ b 1 / Γ 1 Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 20 / 35

  55. The CoLiS language Definitions Semantic rules – Branching Branching-True t 1 / Γ ⇓ σ 1 ⋆ b 1 / Γ 1 b 1 = True t 2 / Γ 1 ⇓ σ 2 ⋆ b 2 / Γ 2 ( if t 1 then t 2 else t 3 ) / Γ ⇓ σ 1 σ 2 ⋆ b 2 / Γ 2 Branching-False t 1 / Γ ⇓ σ 1 ⋆ b 1 / Γ 1 b 1 ∈ { False , Fatal } t 3 / Γ 1 ⇓ σ 3 ⋆ b 3 / Γ 3 ( if t 1 then t 2 else t 3 ) / Γ ⇓ σ 1 σ 3 ⋆ b 3 / Γ 3 Branching-Exception t 1 / Γ ⇓ σ 1 ⋆ b 1 / Γ 1 b 1 ∈ { Return , Exit } ( if t 1 then t 2 else t 3 ) / Γ ⇓ σ 1 ⋆ b 1 / Γ 1 Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 20 / 35

  56. The CoLiS language Definitions Semantic rules – Branching Branching-True t 1 / Γ ⇓ σ 1 ⋆ b 1 / Γ 1 b 1 = True t 2 / Γ 1 ⇓ σ 2 ⋆ b 2 / Γ 2 ( if t 1 then t 2 else t 3 ) / Γ ⇓ σ 1 σ 2 ⋆ b 2 / Γ 2 Branching-False t 1 / Γ ⇓ σ 1 ⋆ b 1 / Γ 1 b 1 ∈ { False , Fatal } t 3 / Γ 1 ⇓ σ 3 ⋆ b 3 / Γ 3 ( if t 1 then t 2 else t 3 ) / Γ ⇓ σ 1 σ 3 ⋆ b 3 / Γ 3 Branching-Exception t 1 / Γ ⇓ σ 1 ⋆ b 1 / Γ 1 b 1 ∈ { Return , Exit } ( if t 1 then t 2 else t 3 ) / Γ ⇓ σ 1 ⋆ b 1 / Γ 1 Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 20 / 35

  57. 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

  58. 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

  59. 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

  60. 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 t 1 Γ σ 1 b 1 Γ 1 t 2 σ 2 b 2 Γ 2 . eval_term t 1 Γ σ 1 (BNormal b 1 ) Γ 1 -> eval_term t 2 Γ 1 σ 2 b 2 Γ 2 -> eval_term (TSeq t 1 t 2 ) Γ (concat σ 1 σ 2 ) b 2 Γ 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

  61. 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

  62. 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) interp_term (t: term) ( Γ : context) let rec (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

  63. 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) interp_term (t: term) ( Γ : context) let rec (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

  64. 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 t 1 t 2 t 3 -> let ( b 1 , Γ 1 ) = interp_term t 1 Γ stdout try EFatal Γ 1 -> (false , Γ 1 ) end with in interp_term (if b 1 then t 2 else t 3 ) Γ 1 stdout Allows us to validate the translation by testing. Nicolas Jeannerod S´ eminaire Gallium September 18, 2017 22 / 35

  65. 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

  66. 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

  67. 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

  68. 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

  69. Formalisation Formulation Soundness of the interpreter in Why3 interp_term (t: term) ( Γ : context) let rec (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

Recommend


More recommend