The Programming Language Co re The Programming Language Co - - PDF document

the programming language co re the programming language
SMART_READER_LITE
LIVE PREVIEW

The Programming Language Co re The Programming Language Co - - PDF document

The Programming Language Co re The Programming Language Co re W olfgang Schreiner Resea rch Institute fo r Symb olic Computation (RISC-Linz) Johannes Kepler Universit y , A-4040 Linz, Austria W


slide-1
SLIDE 1 The Programming Language Co re The Programming Language Co re W
  • lfgang
Schreiner Resea rch Institute fo r Symb
  • lic
Computation (RISC-Linz) Johannes Kepler Universit y , A-4040 Linz, Austria W
  • lfgang.Schreiner@risc.un
i-linz.ac.at http://www.risc.uni-linz.ac.at/p eople/schrein e W
  • lfgang
Schreiner RISC-Linz
slide-2
SLIDE 2 The Programming Language Co re The Programming Language Co re
  • \Co
re"
  • f
values and
  • p
erations establish fundamental capabilities
  • f
a language. { Numerical computation: numeric values. { T ext editing: string values. { General purp
  • se:
co re fo r many applications.
  • Sta
rting p
  • int
fo r language design.
  • Design
p rograms and study their computa- tional p
  • w
ers.
  • Later
extend co re b y conveniences. { Sub routines, mo dules, . . . Let's study the nature
  • f
a p rogramming lan- guage co re! W
  • lfgang
Schreiner 1
slide-3
SLIDE 3 The Programming Language Co re A Co re Imp erative Language A while lo
  • p
language
  • Syntax
domains.
  • Syntax
rules. C 2 Command E 2 Exp ression L 2 Lo cation N 2 Numeral C ::= L:=E j C 1 ;C 2 j if E then C 1 else C 2
  • j
while E do C
  • d
j skip E ::= N j @L j E 1 +E 2 j :E j E 1 =E 2 L ::= lo c i , if i > N ::= n, if n 2 Integer Example lo c 1 := 0; while @lo c 1 =0 do lo c 2 :=@lo c 1 +1
  • d
W
  • lfgang
Schreiner 2
slide-4
SLIDE 4 The Programming Language Co re Abstract Syntax
  • Non-terminal
symb
  • ls.
{ C, E, L, N. { V a riables
  • ver
syntax trees.
  • T
erminal symb
  • ls.
{ @, +, :=, skip { Lab els
  • f
syntax trees.
  • Inductive
denition
  • f
syntax trees. Abstract syntax denes syntax trees! W
  • lfgang
Schreiner 3
slide-5
SLIDE 5 The Programming Language Co re Example lo c 1 := 0; while @lo c 1 =0 do lo c 2 :=@lo c 1 +1
  • d

C ; C L := E E C while

  • d

N E = E N @L C L := E E + E N @L do loc_1 loc_2 1 loc_1 loc_1

Semantics gives meaning to syntax trees! W
  • lfgang
Schreiner 4
slide-6
SLIDE 6 The Programming Language Co re T yping Rules
  • Abstruct
syntax do es not dene w ell- fo rmed p rograms
  • nly
. { Phrase \(0=1)+2" allo w ed. { Cannot add b
  • lean
to integer.
  • Rene
abstract syntax denition. { Integer and b
  • lean
exp ressions. { Dene t w
  • distinct
syntax domains? { Better: add t yping annotations!
  • A
ttributed syntax trees { T yp e attributes to all phrase fo rms. { Syntax tree is w ell t yp ed if t yp e attributes can b e attached to all
  • f
its nonterminals. Inference rules used fo r describing t yp e struc- tures. W
  • lfgang
Schreiner 5
slide-7
SLIDE 7 The Programming Language Co re Example

comm C: comm C: comm L:intloc E: intexp := E: intexp E: intexp N: int E: intexp E: intexp E: intexp C: @ L: ; loc_1 N:

  • d

while comm C: boolexp E: do int = intloc intloc loc_2 + L:intloc := N: 1 loc_1 loc_1 @ L:

Each subtree is annotated with its t yp e! W
  • lfgang
Schreiner 6
slide-8
SLIDE 8 The Programming Language Co re T yping Rules Command L: intlo c E: intexp L:=E: comm C 1 : comm C 2 : comm C 1 ;C 2 : comm E: b
  • lexp
C 1 : comm C 2 : comm if E then C 1 else C 2 : comm E: b
  • lexp
C: comm while E do C
  • d:
comm skip: comm Exp ression N: int N: intexp L: intlo c @L: intexp E 1 : intexp E 2 : intexp E 1 +E 2 : intexp E: b
  • lexp
:E: b
  • lexp
E 1 :
  • exp
E 2 :
  • exp
E 1 =E 2 : b
  • lexp
if
  • 2
fint, b
  • l
g Lo cation Numeral lo c i : intlo c, if i > n : int, if n 2 Integer W
  • lfgang
Schreiner 7
slide-9
SLIDE 9 The Programming Language Co re T yping Rules
  • One
t yping rule fo r each construction
  • f
each syntax rule.
  • Conditions
under which constructions a re w ell t yp ed.
  • Linea
r Notation (full t yp e annotation): { ((lo c 1 ) intlo c :=((0) int ) intexp (while ((@(lo c 1 ) intlo c ) intexp =((0) int ) intexp ) b
  • lexp
(do (lo c 2 ) intlo c := ((@(lo c 1 ) intlo c ) intexp + ((1) int ) intexp ) intexp ) comm ) comm ) comm .
  • Abb
reviation (ro
  • t
t yp e annotation): { lo c 1 := 0; while @lo c 1 =0 do lo c 2 :=@lo c 1 +1
  • d:
comm W
  • lfgang
Schreiner 8
slide-10
SLIDE 10 The Programming Language Co re T yping Rules
  • Logic
assertion U: . { T ree U is w ell t yp ed with t yp e
  • .
  • Static
t yping fo r language. { T yp e attributes can b e calculated without evaluating the p rogram.
  • Strongly
t yp ed language. { No run-time incompatibilit y erro rs.
  • Unicit
y
  • f
t yping. { Can a syntax tree b e t yp ed in multiple w a ys?
  • Soundness
  • f
t yping rules. { Are the t yping rules sensible in their assignment
  • f
t yp e attributes to phrases? Questions will b e addressed later. W
  • lfgang
Schreiner 9
slide-11
SLIDE 11 The Programming Language Co re Induction and Recursion Syntax rule E ::= true j :E j E 1 &E 2
  • Inductive
denition: { true is in Exp ression. { If E is in Exp ression, then so is :E. { If E 1 and E 2 a re in Exp ression, then so is E 1 &E 2 . { No
  • ther
trees a re in exp ression.
  • Exp
ression = set
  • f
trees!
  • Generate
all trees in stages. { stage = fg. { stage i+1 = stage i [ f :E j E 2 stage i g [ f E 1 &E 2 j E 1 , E 2 2 stage i g. { Exp ression = [ i0 stage i .
  • Any
tree in Exp ression is constructed in a nite numb er
  • f
stages. W
  • lfgang
Schreiner 10
slide-12
SLIDE 12 The Programming Language Co re Structural Induction
  • Pro
  • f
technique fo r syntax trees. { Goal: p rove P (t) fo r all trees t in a a language. { Inductive base: Prove that P holds fo r all trees in stage 1 . { Inductive hyp
  • thesis:
Assume that P holds fo r all trees in stages stage j with j
  • i.
{ Inductive step: Prove that P holds fo r all trees in stage i .
  • Prove
P (t) fo r all trees t in Exp ression. { P (true) holds. { P (:E) holds assuming that P (E) holds (fo r a rbitra ry E). { P (E 1 &E 2 ) holds assuming that P (E 1 ) holds and P (E 2 ) holds (fo r a rbitra ry E 1 , E 2 ). Syntax rules guide the p ro
  • f
! W
  • lfgang
Schreiner 11
slide-13
SLIDE 13 The Programming Language Co re Unicit y
  • f
T yping Can a syntax tree b e t yp ed in multiple w a ys?
  • Unicit
y
  • f
t yping p rop ert y . { Every syntax tree has as most
  • ne
assignment
  • f
t yping attributes to its no des. { If P : holds, then
  • is
unique.
  • Unicit
y
  • f
T yping holds fo r Numeral. { By single t yping rule, if N : holds, then
  • =
int (fo r all N 2 Numeral).
  • Unicit
y
  • f
T yping holds fo r Lo cation. { By single t yping rule, if L: holds, then
  • =
intlo c (fo r all L 2 Lo cation). W
  • lfgang
Schreiner 12
slide-14
SLIDE 14 The Programming Language Co re Unicit y
  • f
T yping
  • Unicit
y
  • f
t yping holds fo r Exp ression: { Case N. N:int holds. By single t yping rule, N:intexp holds. { Case E 1 +E 2 . By inductive hyp
  • thesis,
E 1 : 1 and E 2 : 2 hold fo r unique
  • 1
and
  • 2
. By single t yping rule, E 1 :intexp and E 2 :intexp must hold. If
  • 1
= 2 =intexp, then E 1 +E 2 :intexp. Otherwise, E 1 +E 2 has no t yping. { . . .
  • Unicit
y
  • f
t yping holds fo r Command: { Case L := E. L:intlo c holds. E: 1 holds fo r unique
  • 1
. By single t yping rule,
  • 1
must b e intexp to have L:=E: comm. Otherwise, L:=E has no t yping. { . . . Unicit y
  • f
t yping holds fo r all four syntax do- mains. W
  • lfgang
Schreiner 13
slide-15
SLIDE 15 The Programming Language Co re T yping Rules Dene a Language
  • T
yping rules (not abstract syntax) dene language. { Only w ell-fo rmed p rograms a re
  • f
value. { Programs a re w ell-t yp ed trees.
  • Signicance
  • f
unicit y
  • f
t yping: { Linea r rep resentation without t yp e annotations rep resents (at most)
  • ne
p rogram. { Example: 0+1.
  • Without
unicit y
  • f
t yping: { Coherence : dierent tree derivations
  • f
a linea r rep resen- tation should have same meaning. { E:intexp E:realexp E 1 :realexp E 2 :realexp E 1 +E 2 :realexp { (((0) int +(1) int ) intexp ) realexp . { ((((0) int ) intexp ) realexp + (((1) int ) intexp ) realexp ) realexp W
  • lfgang
Schreiner 14
slide-16
SLIDE 16 The Programming Language Co re Pro
  • f
T rees Programs ma y b e directly derived from t yping rules.
  • T
yping rules fo rm a logic.
  • Set
  • f
axioms and inference rules.
  • (Inverted)
trees a re logic p ro
  • f
trees.

@loc_1 =0 loc_2 loc_1 :=@ +1 @loc_1 0: intexp 0: int : comm loc_1+1 @loc_1 loc_1: intloc : intexp @loc_1 loc_1: intloc : intexp while do

  • d : comm

: intexp =0 loc_2 :=@ : intloc loc_2 @loc_1+1: intexp : intexp : int 1 1

W
  • lfgang
Schreiner 15
slide-17
SLIDE 17 The Programming Language Co re Semantics
  • f
the Co re Language Denotational semantics
  • Recursively
dened function. { Mapping
  • f
a w ell-t yp ed derivation tree to its mathematical meaning.
  • Semantic
algeb ras. { Meaning sets (domains) and
  • p
erations. { Bo
  • l,
Int, Lo cation, Sto re.
  • F
  • r
each t yping rule, a recursive deni- tion. { L: intlo c E: intexp L:=E: comm { [[L:=E: comm ]] . . . = . . . [[L: intlo c ]] . . . [[E: intexp ]] . . .
  • Comp
  • sitional
semantic denitions. { Meaning
  • f
tree constructed from meanings
  • f
its subtrees. F unction [[.]] is read as \the meaning
  • f
" W
  • lfgang
Schreiner 16
slide-18
SLIDE 18 The Programming Language Co re Semantic Algeb ras Bo
  • l
= ftrue, false g not : Bo
  • l
! Bo
  • l
not (false ) = true ; not (true ) = false equalb
  • l
: Bo
  • l
  • Bo
  • l
! Bo
  • l
equalb
  • l
(m, n) = (m=n) Int = f. . . ,
  • 1,
0, 1, . . . g plus : Int
  • Int
! Int plus (m, n) = m + n equalint : Int
  • Int
! Bo
  • l
equalint (m, n) = (m=n) Lo cation = flo c i j i > 0g W
  • lfgang
Schreiner 17
slide-19
SLIDE 19 The Programming Language Co re Semantic Algeb ras Sto re = f hn 1 ; n 2 ; : : : ; n m i j n i 2 Int, 1
  • i
  • m,
m
  • g
lo
  • kup
: Lo cation
  • Sto
re ! Int lo
  • kup
(lo c j , hn 1 ; n 2 ; : : : ; n j ; : : : ; n m i) = n j (if j > m, then lo
  • kup
(lo c j , hn 1 ; : : : ; n m i) = 0) up date : Lo cation
  • Int
  • Sto
re ! Sto re up date (lo c j , j , hn 1 ; n 2 ; : : : ; n j ; : : : ; n m i) = hn 1 ; n 2 ; : : : ; n; : : : ; n m i (if j > m, then up date (lo c j , n, hn 1 ; : : : ; n m i) = hn 1 ; n 2 ; : : : ; n m i) if : Bo
  • l
  • Sto
re ?
  • Sto
re ? ! Sto re ? if (true, s 1 , s 2 ) = s 1 if (false, s 1 , s 2 ) = s 2 (Sto re ? = Sto re [ f?g, ? = \b
  • ttom"
= non-termination) W
  • lfgang
Schreiner 18
slide-20
SLIDE 20 The Programming Language Co re Comm and Semantics [[.: comm ]]: Sto re ! Sto re ? [[L:=E: comm ]](s) = up date ([[L: intlo c ]], [[E: intexp ]](s), s) [[C 1 ;C 2 : comm ]](s) = [[C 2 : comm ]]([[C 1 : comm ]](s)) [[if E then C 1 else C 2 : comm ]](s) = if ([[E: b
  • lexp
]](s), [[C 1 : comm ]](s), [[C 2 : comm ]](s)) [[while E do C
  • d:
comm ]](s) = w (s) where w (s) = if ([[E: b
  • lexp
]](s), w ([[C: comm ]](s)), s) [[skip: comm ]](s) = s The meaning
  • f
a command is a function from Sto re to Sto re . W
  • lfgang
Schreiner 19
slide-21
SLIDE 21 The Programming Language Co re Exp ression Semantics [[.: exp ]]: Sto re ! (Int [ Bo
  • l
) [[N: intexp ]](s) = [[N: int ]] [[@L: intexp ]](s) = lo
  • kup
([[L: intlo c ]], s) [[:E: b
  • lexp
]](s) = not ([[E: b
  • lexp
]](s)) [[E 1 +E 2 : intexp ]](s) = plus ([[E 1 : intexp ]](s), [[E 2 : intexp ]](s)) [[E 1 =E 2 : b
  • lexp
]](s) = equalb
  • l
([[E 1 : b
  • lexp
]](s), [[E 2 : b
  • lexp
]](s)) [[E 1 =E 2 : b
  • lexp
]](s) = equalint ([[E 1 : intexp ]](s), [[E 2 : intexp ]](s)) [[.: intlo c ]]: Lo cation [[lo c i : intlo c ]] = lo c i [[.: int ]]: Int [[n: int ]] = n The meaning
  • f
an exp ression is a function from Sto re to Int
  • r
Bo
  • l
. W
  • lfgang
Schreiner 20
slide-22
SLIDE 22 The Programming Language Co re Example P = Q; R Q = lo c 2 :=1; R = if @lo c 2 =0 then skip else S
  • S
= lo c 1 :=@lo c 2 +4 [[P: comm ]](s) = [[R: comm ]]([[Q: comm ]](s)) = [[R: comm ]]up date (lo c 2 , 1, s) = if ([[@lo c 2 =0: b
  • lexp
]]up date (lo c 2 , 1, s), [[skip: comm ]]up date (lo c 2 , 1, s), [[S: comm ]]up date (lo c 2 , 1, s)) = if (false, [[skip: comm ]]up date (lo c 2 , 1, s), [[S: comm ]]up date (lo c 2 , 1, s)) = [[S: comm ]]up date (lo c 2 , 1, s) = up date (lo c 1 , [[@lo c 2 +4: intexp ]]up date (lo c 2 , 1, s), up date (lo c 2 , 1, s)) = up date (lo c 1 , 5, up date (lo c 2 , 1, s)) Program semantics can b e studied indep en- dently
  • f
sp ecic sto rage vecto r! W
  • lfgang
Schreiner 21
slide-23
SLIDE 23 The Programming Language Co re Soundness
  • f
the T yping Rules. Are the t yping rules sensible in their assign- ment
  • f
t yp e attributes to phrases?
  • T
yping rules must b e sound. { Every w ell-t yp ed p rogram has a meaning.
  • T
yp e attributes: {
  • ::=
int j b
  • l
{
  • ::=
intlo c j
  • exp
j comm
  • Mapping
  • f
attributes to meanings: { [[int ]] = Int { [[b
  • l
]] = Bo
  • l
{ [[intlo c ]] = Lo cation { [[ exp ]] = Sto re ! [[ ]] { [[comm ]] = Sto re ! Sto re ? Ho w a re [[P: ]] and [[ ]] related? W
  • lfgang
Schreiner 22
slide-24
SLIDE 24 The Programming Language Co re Soundness Theo rem [[P: ]] 2 [[ ]], fo r every w ell-t yp ed phrase P:
  • Case
n: int { [[n: int ]] = n 2 Int = [[int ]]
  • Case
@L: intexp { W e kno w [[L: intlo c ]] = l 2 [[intlo c ]] = Lo cation. Then, fo r every Sto re s, [[@L: intexp ]](s) = lo
  • kup
(l , s) 2 Int i.e. [[@L: intexp ]] 2 Sto re ! Int = [[intexp ]].
  • Case
C 1 ;C 2 : comm { W e kno w [[C 1 : comm ]] and [[C 2 : comm ]] a re ele- ments
  • f
Sto re ! Sto re ? . F
  • r
every Sto re s, w e have [[C 1 ;C 2 : comm ]](s) = [[C 2 : comm ]]([[C 1 : comm ]](s)) and [[C 2 :comm ]](s) = s 1 2 Sto re ? . If s 1 =?, [[C 2 : comm ]](s 1 )=? 2 Sto re ? . If s 1 2 Sto re, then [[C 2 : comm ]](s 1 ) 2 Sto re ? . Hence, [[C 1 ;C 2 : comm ]] 2 Sto re ! Sto re ? = [[comm ]] . Prove
  • ne
case fo r each t yping rule. W
  • lfgang
Schreiner 23
slide-25
SLIDE 25 The Programming Language Co re Op erational Prop erties
  • Denotational
semantics constructs mathe- matical functions. { F unction extensionalit y fo r reasoning.
  • Op
erational semantics reveals computa- tional patterns. { Computation steps undertak en fo r evaluating the p rogram.
  • Denotational
semantics has
  • p
erational
  • w
er. { [[lo c 3 :=@lo c 1 +1: comm ]]h3; 4; 5 i ) up date (lo c 3 , [[@lo c 1 +1: intexp ]]h3; 4; 5i, h3; 4; 5i) ) . . . ) up date (lo c 3 , 4, h3; 4; 5i) ) h3; 4; 4i Can w e use denotational denitions as
  • p
er- ational rewrite rules? W
  • lfgang
Schreiner 24
slide-26
SLIDE 26 The Programming Language Co re Denotations as Rewrite Rules Op. semantics reduces p rograms to values.
  • A
p rogram is a phrase [[C: comm ]]s .
  • V
alues a re from semantic domains. { Bo
  • leans,
numerals, lo cations, sto rage vecto rs.
  • Equational
denitions get rewrite rules. { Denotation: f (x 1 ; x 2 ; : : : ; x n ) = v { Rule: f (x 1 ; x 2 ; : : : ; x n ) ) v
  • Computation
is a sequence
  • f
rewrite steps p )
  • p
n . { p ) p 1 ) : : : ) p n . { Each computation step p i ) p i+1 replaces a subphrase (the redex ) in p i acco rding to some rewrite rule.
  • If
p n is a value, computation terminates. Which p rop erties shall semantics fulll? W
  • lfgang
Schreiner 25
slide-27
SLIDE 27 The Programming Language Co re Prop erties
  • f
Op erational Semantics
  • Soundness.
{ If p has an underlying \meaning" m and p ) p , then p means m as w ell. { By denition
  • f
).
  • Subject
reduction. { If p has an underlying \t yp e"
  • and
p ) p , then p has
  • as
w ell. { By soundness
  • f
t yping.
  • Strong
t yping. { If p is w ell-t yp ed and p ) p , then p contains no
  • p
erato r-
  • p
erand incompatibilities. { By induction
  • ver
computation rules.
  • Computational
adequacy . { A p rogram p's underlying meaning is a p rop er meaning m, if there is some value v such that p ) v and v means m. W
  • lfgang
Schreiner 26
slide-28
SLIDE 28 The Programming Language Co re Computabil it y
  • f
Phrases
  • Predicate
comp
  • (computable
): { comp intlo c (p) := p )
  • v
and v means l where l 2 Lo ca- tion is the meaning
  • f
p. { comp
  • exp
(p) := p(s) )
  • v
and v means n where s 2 Sto re and p(s) means n 2 [[ ]]. { comp comm (p) := p(s) )
  • v
and v means s where s 2 Sto re and p(s) means s 2 Sto re.
  • comp
  • [[U:
  • ]]
holds fo r all w ell-t yp ed phrases U:
  • .
{ Induction
  • n
t yping rules. Computational adequacy follo ws from sound- ness
  • f
t yping, soundness
  • f
  • p
erational se- mantics and the computabilit y
  • f
phrases. W
  • lfgang
Schreiner 27
slide-29
SLIDE 29 The Programming Language Co re Design
  • f
a Language Co re Contradicto ry design
  • bjectives:
  • Oriented
to w a rds a sp ecic p roblem a rea.
  • General
purp
  • se.
  • User
friendly .
  • Ecient
implementation p
  • ssible.
  • Extensible
b y new language features.
  • Secure
agains p rogramming erro rs.
  • Simple
syntax and semantics.
  • Logical
supp
  • rt
fo r verication.
  • .
. . Design is an a rtistic activit y! W
  • lfgang
Schreiner 28
slide-30
SLIDE 30 The Programming Language Co re Orthagonalit y
  • A
language should b e based
  • n
few fun- damental p rinciples that ma y b e combined without unneccessa ry restrictions.
  • Orthagonal
languages a re easier to under- stand fo r p rogrammers and implemento rs.
  • Denotational
semantics ma y help to achieve this. { Dene sets
  • f
meanings and
  • p
erations. { Give syntactic rep resentations. { Organize into abstract syntax denition. In the follo wing w e will study a set
  • f
basic design p rinciples. W
  • lfgang
Schreiner 29