Lecture 17 Denotational Semantics Decide what you want

  • Slides: 28
Download presentation
Lecture 17: Denotational Semantics Decide what you want to say before you worry about

Lecture 17: Denotational Semantics Decide what you want to say before you worry about how you are going to say it. Strachey’s first law of programming CS 655: Programming Languages David Evans University of Virginia CS 655 http: //www. cs. virginia. edu/~evans Computer Science

Menu • Trial Verdict (and Punishment? ) • Denotational Semantics Everyone should have received

Menu • Trial Verdict (and Punishment? ) • Denotational Semantics Everyone should have received grade record email. Use this to decide whether or not to do Position Paper 5. 9/25/2020 University of Virginia CS 655 2

Trial Verdict 9/25/2020 University of Virginia CS 655 3

Trial Verdict 9/25/2020 University of Virginia CS 655 3

Semantics Summary So Far • • • Operational Semantics Static Semantics (Type Judgments) Axiomatic

Semantics Summary So Far • • • Operational Semantics Static Semantics (Type Judgments) Axiomatic Semantics Lambda Calculus Today: Denotational Semantics Hint: on your final you should be able to decide which of these is best for a particular problem. 9/25/2020 University of Virginia CS 655 4

Apples and Oranges + Denotes 2 = 17 Denotes addition Denotes 3 Juxtaposition denotes

Apples and Oranges + Denotes 2 = 17 Denotes addition Denotes 3 Juxtaposition denotes multiplication Adding apples and oranges is easy! Just decide what they denote. 9/25/2020 University of Virginia CS 655 5

Denotational Semantics • Invented by Strachey and Scott (Oxford, 1970 s); popularized by Stoy

Denotational Semantics • Invented by Strachey and Scott (Oxford, 1970 s); popularized by Stoy and Milne in mid-late 70 s • Map syntactic units in program to semantic units • Combine them using rules to get the meaning of the whole program 9/25/2020 University of Virginia CS 655 6

Meaning Function + M * M M 3 * 2 2 2 Semantic Algebra

Meaning Function + M * M M 3 * 2 2 2 Semantic Algebra Syntactic Algebra Homomorphism – the tree shape is the same 9/25/2020 University of Virginia CS 655 7

Apples & Oranges Language (AOL) Program : : = Expression * Expression | Expression

Apples & Oranges Language (AOL) Program : : = Expression * Expression | Expression + Expression | Int. Literal Semantic domain: Integer = {. . . , -2, -1, 0, 1, 2, . . . } Meaning Functions: P : Program Integer E : Expression Integer N : Int. Literal Integer 9/25/2020 University of Virginia CS 655 8

Defining the Meaning Functions P [[ Expression ]] = E [[ Expression ]] E

Defining the Meaning Functions P [[ Expression ]] = E [[ Expression ]] E [[ E 1 * E 2 ]] = E [[ E 1 ]] E [[ E 2 ]] E [[ E 1 + E 2 ]] = E [[ E 1 ]] E [[ E 2 ]] E [[ Int. Literal ]] = N [[ Int. Literal ]] = the integer number denoted by Int. Literal 9/25/2020 University of Virginia CS 655 9

Example P [[ 2 * 2 + 3 * 3 ]] = E [[2

Example P [[ 2 * 2 + 3 * 3 ]] = E [[2 * 2 * 2]] E [[3 * 3]] = (E [[2]] E [[2 * 2]]) (E[[3]] E [[3]]) = (E [[2]] E [[2]]) (E[[3]] E [[3]]) = (N [[2]] N[[2]]) (N[[3]] N[[3]]) = (2 2 2) (3 3) = 17 9/25/2020 University of Virginia CS 655 10

A More Interesting Language Program : : = Statement. List : : = empty

A More Interesting Language Program : : = Statement. List : : = empty | Statement ; Statement. List Statement : : = Variable : = Expression : : = Expression + Expression | Variable | Int. Literal Semantic domain: = set of States where State = : Variable Integer = {. . . , -2, -1, 0, 1, 2, . . . } 9/25/2020 University of Virginia CS 655 11

Meaning Function Domains P : Program ( ) L : Statement. List ( )

Meaning Function Domains P : Program ( ) L : Statement. List ( ) S : Statement ( ) Programs and Statements map states to states. E : Expression ( Integer) Expressions map states to values. N : Int. Literal ( Integer) 9/25/2020 University of Virginia CS 655 12

Expressions N [[ Int. Literal ]]: Int. Literal ( Integer) = { ( ,

Expressions N [[ Int. Literal ]]: Int. Literal ( Integer) = { ( , n) | & n = number denoted by Int. Literal } this means: . n E [[ Int. Literal ]]: Expression ( Integer) = { ( , N [[ Int. Literal ]] ) | } E [[ Variable ]]: Expression ( Integer) = { ( , (Variable)) | } E [[ E 1 + E 2 ]] : Expression ( Integer) = { ( , n 1 n 2) | ( , n 1) E [[ E 1 ]] & ( , n 2) E [[ E 2 ]] } 9/25/2020 University of Virginia CS 655 13

Statement S [[Variable : = Expression]] : Statement ( ) = { ( ,

Statement S [[Variable : = Expression]] : Statement ( ) = { ( , [n/Variable]) | & n = (E [[ Expression ]]) } 9/25/2020 University of Virginia CS 655 14

Statement Lists L : Statement. List ( ) L [[]] = { ( ,

Statement Lists L : Statement. List ( ) L [[]] = { ( , ) | } L [[Statement ; Statement. List ]] = L [[Statement. List]] S [[Statement]] Why backwards? L [[Statement ; Statement. List ]] = L [[Statement. List]] (S [[Statement]] ) 9/25/2020 University of Virginia CS 655 15

Program P [[Statement. List]] : Program ( ) = { ( , s) |

Program P [[Statement. List]] : Program ( ) = { ( , s) | ( , s) L [[Statement. List]] } 9/25/2020 University of Virginia CS 655 16

Example x : = x + 1; a : = x; L [[x :

Example x : = x + 1; a : = x; L [[x : = x + 1; a : = x; ]] = L [[a : = x; ]] S [[x : = x + 1]] = { ( , [n/x]) | & n = (E [[ x + 1]]) } E [[ x + 1]] = { ( , n 1 n 2) | ( , n 1) E [[x]] & ( , n 2) E [[1]] } 9/25/2020 University of Virginia CS 655 17

So what? • It took 4 slides to understand x : = x +

So what? • It took 4 slides to understand x : = x + 1; a : = x; • Is this better than operational semantics? • Yes, we have produced a state transition function that shows what x : = x + 1; a : = x; does from any initial state. • Observational equivalence to any statement list with the same state transition function. 9/25/2020 University of Virginia CS 655 21

If Statement : : = if Predicate then Statement. List else Statement. List end

If Statement : : = if Predicate then Statement. List else Statement. List end assume there is B [[Predicate]]: ( Boolean) S [[if b then s 1 else s 2 end]] = { ( , ’) | B [[Predicate]] = true & ( , ’) L [[s 1]]} { ( , ’) | B [[Predicate]] = false & ( , ’) L [[s 2]]} 9/25/2020 University of Virginia CS 655 22

While Statement : : = while Predicate do Statement. List end w = while

While Statement : : = while Predicate do Statement. List end w = while B do S end is equivalent to if B then S; w else ; So, S [[w]] must equal S [[if B then S; w else ; ]] 9/25/2020 University of Virginia CS 655 23

Whiling Away S [[w]] = {( , ’) | B [[B]] = true &

Whiling Away S [[w]] = {( , ’) | B [[B]] = true & ( , ’) L [[S; w]]} {( , ’) | B [[B]] = false & ( , ’) L [[; ]]} = {( , ’) | B [[B]] = true & ( , ’) S [[w]] L [[S]]} { ( , ) | B [[B]] = false} Yikes! A recursive definition – what about fixed points? 9/25/2020 University of Virginia CS 655 24

Generating Function G (w) = {( , ’) | B [[B]] = true &

Generating Function G (w) = {( , ’) | B [[B]] = true & ( , ’) w L [[S]]} { ( , ) | B [[B]] = false} The fixed point of G (w) is S [[w]] where w = while B do S end. 9/25/2020 University of Virginia CS 655 25

Challenge • Prove or disprove: i : = 1; while i < n do

Challenge • Prove or disprove: i : = 1; while i < n do i : = i + 1; x : = x * i; end; i : = 0; is observationally equivalent to: i : = n; while i > 0 do x : = x * i; i : = i - 1; end; i : = 0; when n >= 0. • Worth 1 position paper point. 9/25/2020 University of Virginia CS 655 26

Summary • Denotational Semantics: map program fragments to semantic domain • Popular for defining

Summary • Denotational Semantics: map program fragments to semantic domain • Popular for defining languages (in 80 s, less in favor today) • Basis for most type inference algorithms • Good for reasoning about behavior of program fragments • Hard to deal with control flow – while depends on fixed point machinery, goto essentially impossible 9/25/2020 University of Virginia CS 655 27

Charge • Read Cardelli’s “Basic Polymorphic Typechecking” paper for Thursday • If ambitious, try

Charge • Read Cardelli’s “Basic Polymorphic Typechecking” paper for Thursday • If ambitious, try reading section of FL paper that described language using denotational semantics • Keep working on your projects – but don’t get so caught up in implementation you lose big picture 9/25/2020 University of Virginia CS 655 28