Denotational Semantics. Domain Theory

Similar documents
Outline. What is semantics? Denotational semantics. Semantics of naming. What is semantics? 2 / 21

CS 381: Programming Language Fundamentals

Graphical Untyped Lambda Calculus Interactive Interpreter

Programming Languages Third Edition

7. Introduction to Denotational Semantics. Oscar Nierstrasz

Introduction to Functional Programming in Haskell 1 / 56

Syntax and Grammars 1 / 21

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

LECTURE 16. Functional Programming

Programming Language Features. CMSC 330: Organization of Programming Languages. Turing Completeness. Turing Machine.

CMSC 330: Organization of Programming Languages

Principles of Programming Languages

M. Snyder, George Mason University LAMBDA CALCULUS. (untyped)

CS 4110 Programming Languages & Logics. Lecture 28 Recursive Types

CMSC 330: Organization of Programming Languages

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

1 Introduction. 3 Syntax

Functional Programming. Pure Functional Programming

Semantics and Concurrence Module on Semantic of Programming Languages

The Substitution Model

CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th

Handout 10: Imperative programs and the Lambda Calculus

Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy

CIS 500 Software Foundations Fall September 25

Programming Languages Lecture 14: Sum, Product, Recursive Types

A Small Interpreted Language

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 4 MODULE, SPRING SEMESTER MATHEMATICAL FOUNDATIONS OF PROGRAMMING ANSWERS

Chapter 11 :: Functional Languages

Relational Algebra. Study Chapter Comp 521 Files and Databases Fall

Functional Programming. Big Picture. Design of Programming Languages

Organization of Programming Languages CS3200/5200N. Lecture 11

CMSC 330: Organization of Programming Languages. Lambda Calculus Encodings

CONVENTIONAL EXECUTABLE SEMANTICS. Grigore Rosu CS522 Programming Language Semantics

Formal Systems and their Applications

axiomatic semantics involving logical rules for deriving relations between preconditions and postconditions.

3.4 Deduction and Evaluation: Tools Conditional-Equational Logic

Semantics and Concurrence Module on Semantic of Programming Languages

CONVENTIONAL EXECUTABLE SEMANTICS. Grigore Rosu CS422 Programming Language Semantics

CSCC24 Functional Programming Scheme Part 2

CIS 500 Software Foundations Midterm I

Functional Programming and λ Calculus. Amey Karkare Dept of CSE, IIT Kanpur

CMSC 330: Organization of Programming Languages

Principles of Programming Languages

CMSC 330: Organization of Programming Languages

CVO103: Programming Languages. Lecture 5 Design and Implementation of PLs (1) Expressions

Outline. Introduction Concepts and terminology The case for static typing. Implementing a static type system Basic typing relations Adding context

CS 6110 S11 Lecture 25 Typed λ-calculus 6 April 2011

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

Homework 3 COSE212, Fall 2018

Mutable References. Chapter 1

λ-calculus Lecture 1 Venanzio Capretta MGS Nottingham

Lambda Calculus.

Introduction to the λ-calculus

Mathematics for Computer Scientists 2 (G52MC2)

CS152: Programming Languages. Lecture 7 Lambda Calculus. Dan Grossman Spring 2011

λ calculus Function application Untyped λ-calculus - Basic Idea Terms, Variables, Syntax β reduction Advanced Formal Methods

Programming Languages Lecture 15: Recursive Types & Subtyping

Abstract Interpretation

Note that in this definition, n + m denotes the syntactic expression with three symbols n, +, and m, not to the number that is the sum of n and m.

A Simple Supercompiler Formally Verified in Coq

Programming Languages

The Lambda Calculus. 27 September. Fall Software Foundations CIS 500. The lambda-calculus. Announcements

CS 6110 S14 Lecture 1 Introduction 24 January 2014

Type Systems Winter Semester 2006

Programming with Math and Logic

Simply-Typed Lambda Calculus

Recap: Functions as first-class values

Lecture 5: Lazy Evaluation and Infinite Data Structures

Programming Language Pragmatics

Lecture Notes on Data Representation

The Substitution Model. Nate Foster Spring 2018

CSE-321 Programming Languages 2012 Midterm

Recursive Types and Subtyping

Introduction to Scheme

Trees. Solution: type TreeF a t = BinF t a t LeafF 1 point for the right kind; 1 point per constructor.

Comp 411 Principles of Programming Languages Lecture 7 Meta-interpreters. Corky Cartwright January 26, 2018

Overview. Probabilistic Programming. Dijkstra s guarded command language: Syntax. Elementary pgcl ingredients. Lecture #4: Probabilistic GCL

CIS 500 Software Foundations Midterm I Answer key October 8, 2003

Programming Languages and Compilers (CS 421)

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

Fall Lecture 3 September 4. Stephen Brookes

CSE 505: Concepts of Programming Languages

COMP 382: Reasoning about algorithms

1.3. Conditional expressions To express case distinctions like

Functional Programming Languages (FPL)

Parsing. Zhenjiang Hu. May 31, June 7, June 14, All Right Reserved. National Institute of Informatics

COMP 1130 Lambda Calculus. based on slides by Jeff Foster, U Maryland

Functional Languages. Hwansoo Han

Recursive Functions of Symbolic Expressions and Their Application, Part I

Concepts of programming languages

JVM ByteCode Interpreter

COSC252: Programming Languages: Semantic Specification. Jeremy Bolton, PhD Adjunct Professor

Recursion. Tjark Weber. Functional Programming 1. Based on notes by Sven-Olof Nyström. Tjark Weber (UU) Recursion 1 / 37

Whereweare. CS-XXX: Graduate Programming Languages. Lecture 7 Lambda Calculus. Adding data structures. Data + Code. What about functions

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

Principles of Programming Languages

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

n λxy.x n y, [inc] [add] [mul] [exp] λn.λxy.x(nxy) λmn.m[inc]0 λmn.m([add]n)0 λmn.n([mul]m)1

Semantics of programming languages

COMP80 Lambda Calculus Programming Languages Slides Courtesy of Prof. Sam Guyer Tufts University Computer Science History Big ideas Examples:

Transcription:

Denotational Semantics and Domain Theory 1 / 51

Outline Denotational Semantics Basic Domain Theory Introduction and history Primitive and lifted domains Sum and product domains Function domains Meaning of Recursive Definitions Compositionality and well-definedness Least fixed-point construction Internal structure of domains Denotational Semantics 2 / 51

Denotational semantics A denotational semantics relates each term to a denotation an abstract syntax tree a value in some semantic domain Valuation function : abstract syntax semantic domain Valuation function in Haskell eval :: Term -> Value Denotational Semantics 3 / 51

Semantic domains Semantic domain: captures the set of possible meanings of a program/term what is a meaning? it depends on the language! Example semantic domains Language Meaning Boolean expressions Boolean value Arithmetic expressions Integer Imperative language State transformation SQL query Set of relations ActionScript Animation MIDI Sound waves Denotational Semantics 4 / 51

Defining a language with denotational semantics Example encoding in Haskell: 1. Define the abstract syntax, T data Term =... the set of abstract syntax trees 2. Identify or define the semantic domain, V type Value =... the representation of semantic values 3. Define the valuation function, : T V sem :: Term -> Value the mapping from ASTs to semantic values a.k.a. the semantic function Denotational Semantics 5 / 51

Example: simple arithmetic expressions 1. Define abstract syntax n Nat ::= 0 1 2... e Exp ::= add e e mul e e neg e n 2. Define semantic domain Use the set of all integers, Int 3. Define the valuation function Exp : Int add e 1 e 2 = e 1 + e 2 mul e 1 e 2 = e 1 e 2 neg e = e n = toint(n) Comes with some operations: +,,, toint : Nat Int,... Denotational Semantics 6 / 51

Encoding denotational semantics in Haskell 1. abstract syntax: define a new data type, as usual 2. semantic domain: identify and/or define a new type, as needed 3. valuation function: define a function from ASTs to semantic domain Valuation function in Haskell sem :: Exp -> Int sem (Add l r) = sem l + sem r sem (Mul l r) = sem l * sem r sem (Neg e) = negate (sem e) sem (Lit n) = n Denotational Semantics 7 / 51

Desirable properties of a denotational semantics Compositionality: a program s denotation is built from the denotations of its parts supports modular reasoning, extensibility supports proof by structural induction Completeness: every value in the semantics domain is denoted by some program ensures that semantics domain and language align if not, language has expressiveness gaps, or semantics domain is too general Soundness: two programs are equivalent iff they have the same denotation equivalence: same w.r.t. to some other definition, e.g. operational semantics ensures that the denotational semantics is correct Denotational Semantics 8 / 51

More on compositionality Compositionality: a program s denotation is built from the denotations of its parts an AST sub-asts Example: What is the meaning of op e 1 e 2 e 3? 1. Determine the meaning of e 1, e 2, e 3 2. Combine these submeanings in some way specific to op Implications: The valuation function is probably recursive We need different valuation functions for each syntactic category Denotational Semantics 9 / 51

Example: move language A language describing movements on a 2D plane a step is an n-unit horizontal or vertical movement a move is described by a sequence of steps Abstract syntax n Nat ::= 0 1 2... d Dir ::= N S E W s Step ::= go d n m Move ::= ɛ s ; m go N 3; go E 4; go S 1; Denotational Semantics 10 / 51

Semantics of move language 1. Abstract syntax n Nat ::= 0 1 2... d Dir ::= N S E W s Step ::= go d n m Move ::= ɛ s ; m 2. Semantic domain Pos = Int Int Domain: Pos Pos 3. Valuation function (Step) S Step : Pos Pos S go N k = λ(x, y). (x, y + k) S go S k = λ(x, y). (x, y k) S go E k = λ(x, y). (x + k, y) S go W k = λ(x, y). (x k, y) 3. Valuation function (Move) M Move : Pos Pos M ɛ = λp. p M s ; m = M m S s Denotational Semantics 11 / 51

Alternative semantics Often multiple interpretations (semantics) of the same language Example: Database schema One declarative spec, used to: initialize the database generate APIs validate queries normalize layout... Distance traveled S D Step : Int S D go d k = k M D Move : Int M D ɛ = 0 M D s ; m = S D s + M D m Combined trip information M C Move : Int (Pos Pos) M C m = (M D m, M m ) Denotational Semantics 12 / 51

Picking the right semantic domain Simple semantics domains can be combined in two ways: product: contains a value from both domains e.g. combined trip information for move language use Haskell (a,b) or define a new data type sum: contains a value from one domain or the other e.g. IntBool language can evaluate to Int or Bool use Haskell Either a b or define a new data type Can errors occur? use Haskell Maybe a or define a new data type Does the language manipulate state or use naming? use a function type Denotational Semantics 13 / 51

Outline Denotational Semantics Basic Domain Theory Introduction and history Primitive and lifted domains Sum and product domains Function domains Meaning of Recursive Definitions Compositionality and well-definedness Least fixed-point construction Internal structure of domains Basic Domain Theory 14 / 51

What is domain theory? Domain theory: a mathematical framework for constructing semantic domains Recall... A denotational semantics relates each term to a denotation an abstract syntax tree a value in some semantic domain Semantic domain: captures the set of possible meanings of a program/term Basic Domain Theory 15 / 51

Historical notes Origins of domain theory: Christopher Strachey, 1964 early work on denotational semantics used lambda calculus for denotations Dana Scott, 1975 goal: denotational semantics for lambda calculus itself created domain theory for meaning of recursive functions Dana Scott More on Dana Scott: Turing award in 1976 for nondeterminism in automata theory PhD advisor: Alonzo Church, 20 years after Alan Turing Basic Domain Theory 16 / 51

Two views of denotational semantics View #1: Translation from one formal system to another e.g. translate object language into lambda calculus View #2: True meaning of a program as a mathematical object e.g. map programs to elements of a semantic domain need domain theory to describe set of meanings Basic Domain Theory 17 / 51

Domains as semantic algebras A semantic domain can be viewed as an algebraic structure a set of values the meanings of the programs a set of operations on the values used to compose meanings of parts Domains also have internal structure: complete partial ordering like a half-lattice any two elements must have a join (later) Basic Domain Theory 18 / 51

Outline Denotational Semantics Basic Domain Theory Introduction and history Primitive and lifted domains Sum and product domains Function domains Meaning of Recursive Definitions Compositionality and well-definedness Least fixed-point construction Internal structure of domains Basic Domain Theory 19 / 51

Primitive domains Values are atomic often correspond to built-in types in Haskell nullary operations for naming values explicitly Domain: Bool true : Bool false : Bool not : Bool Bool and : Bool Bool Bool or : Bool Bool Bool Domain: Int 0, 1, 2,... : Int negate : Int Int plus : Int Int Int times : Int Int Int Also: Nat, Name, Addr,... Domain: Unit () : Unit Basic Domain Theory 20 / 51

Lifted domains Construction: add (bottom) to an existing domain A = A { } New operations : A map : (A B) A B maybe : B (A B) A B Basic Domain Theory 21 / 51

Encoding lifted domains in Haskell Option #1: Maybe data Maybe a = Nothing Just a fmap :: (a -> b) -> Maybe a -> Maybe b maybe :: b -> (a -> b) -> Maybe a -> Maybe b Can also use pattern matching! Option #2: new data type with nullary constructor data Value = Success Int Error Best when combined with other constructions Basic Domain Theory 22 / 51

Outline Denotational Semantics Basic Domain Theory Introduction and history Primitive and lifted domains Sum and product domains Function domains Meaning of Recursive Definitions Compositionality and well-definedness Least fixed-point construction Internal structure of domains Basic Domain Theory 23 / 51

Sum domains Construction: the disjoint union of two existing domains contains a value from either one domain or the other A B = A B New operations inl : A A B inr : B A B case : (A C) (B D) (A B) C D Basic Domain Theory 24 / 51

Encoding sum domains in Haskell Option #1: Either data Either a b = Left a Right b either :: (a -> c) -> (b -> d) -> Either a b -> Either c d Can also use pattern matching! Option #2: new data type with multiple constructors data Value = I Int B Bool Best when combined with other constructions, or more than two options Basic Domain Theory 25 / 51

Example: a language with multiple types b Bool ::= true false n Nat ::= 0 1 2... e Exp ::= add e e neg e equal e e cond e e e n b Design a denotational semantics for Exp 1. How should we define our semantics domain? 2. Define a valuation semantics function neg negates either a numeric or boolean value equal compares two values of the same type for equality cond equivalent to if then else Basic Domain Theory 26 / 51

Solution Exp : (Int Bool) { e1 + e add e 1 e 2 = 2 e 1 Int, e 2 Int otherwise e e Int neg e = e e Bool otherwise e 1 = Int e 2 e 1 Int, e 2 Int equal e 1 e 2 = e 1 = Bool e 2 e 1 Bool, e 2 Bool otherwise e 2 e 1 = true cond e 1 e 2 e 3 = e 3 e 1 = false otherwise n = n b = b Basic Domain Theory 27 / 51

Product domains Construction: the cartesian product of two existing domains contains a value from both domains A B = {(a, b) a A, b B} New operations pair : A B A B fst : A B A snd : A B B Basic Domain Theory 28 / 51

Encoding product domains in Haskell Option #1: Tuples type Value a b = (a,b) fst :: (a,b) -> a snd :: (a,b) -> b Can also use pattern matching! Option #2: new data type with multiple arguments data Value = V Int Bool Best when combined with other constructions, or more than two Basic Domain Theory 29 / 51

Outline Denotational Semantics Basic Domain Theory Introduction and history Primitive and lifted domains Sum and product domains Function domains Meaning of Recursive Definitions Compositionality and well-definedness Least fixed-point construction Internal structure of domains Basic Domain Theory 30 / 51

Function space domains Construction: the set of functions from one domain to another A B Create a function: A B Lambda notation: λx. y where Γ, x : A y : B Eliminate a function apply : (A B) A B Basic Domain Theory 31 / 51

Denotational semantics of naming Environment: a function associating names with things Env = Name Thing Naming concepts declaration add a new name to the environment binding set the thing associated with a name reference get the thing associated with a name Example semantic domains for expressions with... immutable variables (Haskell) Env Val mutable variables (C/Java/Python) Env Env Val Basic Domain Theory 32 / 51

Example: Denotational semantics of let language 1. Abstract syntax i Int ::= (any integer) v Var ::= (any variable name) e Exp ::= i add e e let v e e v 2. Identify semantic domain i. Result of evaluation: Int ii. Environment: Env = Var Int iii. Semantic domain: Env Int 3. Define a valuation function Exp : (Var Int ) Int i = λm. i add e 1 e 2 = λm. e 1 (m) + e 2 (m) let v e 1 e 2 = λm. e 2 (λw. if w = v then e 1 (m) else m(w)) v = λm. m(v) { i + j i Int, j Int i + j = otherwise Basic Domain Theory 33 / 51

What is mutable state? Mutable state: stored information that a program can read and write Typical semantic domains with state domain S: S S state mutation as main effect S S Val state mutation as side effect Often: lifted codomain if mutation can fail Examples the memory cell in a calculator the stack in a stack language the store in many programming languages S = Int S = Stack S = Name Val Basic Domain Theory 34 / 51

Example: Single register calculator language 1. Abstract syntax i Int ::= (any integer) e Exp ::= i e + e save e load 2. Identify semantic domain i. State (side effect): Int ii. Result (main effect): Int iii. Semantic domain: Int Int Int Examples: save (3+2) + load 10 save 1 + (save 10 + load) + load 31 Basic Domain Theory 35 / 51

Example: Single register calculator language 1. Abstract syntax i Int ::= (any integer) e Exp ::= i e + e save e load Examples: save (3+2) + load 10 save 1 + (save 10 + load) + load 31 3. Define valuation function Exp : Int Int Int i = λs. (s, i) e 1 + e 2 = λs. let (s 1, i 1 ) = e 1 (s) (s 2, i 2 ) = e 2 (s 1 ) in (s 2, i 1 + i 2 ) save e = λs. let (s, i) = e (s) in (i, i) load e = λs. (s, s) Basic Domain Theory 36 / 51

Outline Denotational Semantics Basic Domain Theory Introduction and history Primitive and lifted domains Sum and product domains Function domains Meaning of Recursive Definitions Compositionality and well-definedness Least fixed-point construction Internal structure of domains Meaning of Recursive Definitions 37 / 51

Compositionality and well-definedness Recall: a denotational semantics must be compositional a term s denotation is built from the denotations of its parts Example: integer expressions i Int ::= (any integer) e Exp ::= i add e e mul e e Exp : Int i = i add e 1 e 2 = e 1 + e 2 mul e 1 e 2 = e 1 e 2 Compositionality ensures the semantics is well-defined by structural induction Each AST has exactly one meaning Meaning of Recursive Definitions 38 / 51

A non-compositional (and ill-defined) semantics Anti-example: while statement t Test ::=... c Cmd ::=... while t c T Test : S Bool C Cmd : S S C while t c = λs. if T t (s) then C while t c (C c (s)) else s Meaning of while t c in state s: 1. evaluate t in state s 2. if true: a. run c to get updated state s b. re-evaluate while in state s (not compositional) 3. otherwise return s unchanged Translational view: meaning is an infinite expression! Mathematical view: may have infinitely many meanings! Meaning of Recursive Definitions 39 / 51

Extensional vs. operational definitions of a function Mathematical function Defined extensionally: a relation between inputs and outputs Computational function (e.g. Haskell) Usually defined operationally: compute output by sequence of reductions Example (intensional definition) { 1 n = 0 fac(n) = n fac(n 1) otherwise Extensional meaning {..., (2, 2), (3, 6), (4, 24),...} Operational meaning fac(3) 3 fac(2) 3 2 fac(1) 3 2 1 fac(0) 3 2 1 1 6 Meaning of Recursive Definitions 40 / 51

Extensional meaning of recursive functions { 1 n = 0 grow(n) = grow(n + 1) 2 otherwise Best extension (use if undefined): {(0, 1), (1, ), (2, ), (3, ), (4, ),...} Other valid extensions: {(0, 1), (1, 2), (2, 4), (3, 6), (4, 8)...} {(0, 1), (1, 5), (2, 7), (3, 9), (4, 11)...}... Goal: best extension = only extension Meaning of Recursive Definitions 41 / 51

Connection back to denotational semantics A function space domain is a set of mathematical functions Anti-example: while statement t Test ::=... c Cmd ::=... while t c T Test : S Bool C Cmd : S S C while t c = λs. if T t (s) then C while t c (C c (s)) else s Ideal semantics of Cmd: semantic domain: S S contains (s, s ) if c terminates contains (s, ) if c diverges Meaning of Recursive Definitions 42 / 51

Outline Denotational Semantics Basic Domain Theory Introduction and history Primitive and lifted domains Sum and product domains Function domains Meaning of Recursive Definitions Compositionality and well-definedness Least fixed-point construction Internal structure of domains Meaning of Recursive Definitions 43 / 51

Least fixed points Basic idea: 1. a recursive function defines a set of non-recursive, finite subfunctions 2. its meaning is the union of the meanings of its subfunctions Iteratively grow the extension until we reach a fixed point essentially encodes computational functions as mathematical functions Meaning of Recursive Definitions 44 / 51

Example: unfolding a recursive definition Recursive definition { 1 n = 0 fac(n) = n fac(n 1) otherwise Non-recursive, finite subfunctions fac 0 (n) = { 1 n = 0 fac 1 (n) = n fac 0 (n 1) otherwise { 1 n = 0 fac 2 (n) = n fac 1 (n 1) otherwise... fac 0 = {} fac 1 = {(0, 1)} fac 2 = {(0, 1), (1, 1)} fac 3 = {(0, 1), (1, 1), (2, 2)}... fac = fac i i=0 Fine print: each fac i maps all other values to operation prefers non- mappings Meaning of Recursive Definitions 45 / 51

Computing the fixed point In general fac 0 (n) = { 1 n = 0 fac i (n) = n fac i 1 (n 1) otherwise A template to represent all fac i functions: { 1 n = 0 F = λf.λn. n f (n 1) otherwise Fixpoint operator fix : (A A) A fix(g) = let x = g(x) in x fix(h) = h(h(h(h(h(...))))) Factorial as a fixed point fac = fix(f) takes fac i 1 as input Meaning of Recursive Definitions 46 / 51

Outline Denotational Semantics Basic Domain Theory Introduction and history Primitive and lifted domains Sum and product domains Function domains Meaning of Recursive Definitions Compositionality and well-definedness Least fixed-point construction Internal structure of domains Meaning of Recursive Definitions 47 / 51

Why domains are not flat sets Internal structure of domains supports the least fixed-point construction Recall fine print from factorial example: each fac i maps all other values to operation prefers non- mappings How can we generalize and formalize this idea? Meaning of Recursive Definitions 48 / 51

Partial orderings and joins Partial ordering: : D D B reflexive: x D. x x antisymmetric: x, y D. x y y x = x = y transitive: x, y, z D. x y y z = x z Join: : D D D a, b D, the element c = a b D, if it exists, is the smallest element that is larger than both a and b i.e. a c and b c, and there is no d = a b D where d c Meaning of Recursive Definitions 49 / 51

Directed-complete partial orderings A directed subset of a domain is a subset of elements related by Every domain is a directed-complete partial ordering (dcpo): every directed subset D has a least element A function is Scott-continuous if it preserves the least element Finally, the meaning of a Scott-continuous recursive function f is: where f i are the finite approximations of f i=0 f i whew! Meaning of Recursive Definitions 50 / 51

Well-defined semantics for the while statement Syntax t Test ::=... c Cmd ::=... while t c Semantics T Test : S Bool C Cmd : S S C while t c = fix(λf.λs. if T t (s) then f (C c (s)) else s) Meaning of Recursive Definitions 51 / 51