CS 381: Programming Language Fundamentals

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

Denotational Semantics. Domain Theory

Operational Semantics 1 / 13

Scope and Parameter Passing 1 / 19

Syntax and Grammars 1 / 21

These notes are intended exclusively for the personal usage of the students of CS352 at Cal Poly Pomona. Any other usage is prohibited without

Introduction to Functional Programming in Haskell 1 / 56

10 Combining Deep and Shallow DSL Embedding

CMSC 330: Organization of Programming Languages

The Substitution Model

Recap from last time. Programming Languages. CSE 130 : Fall Lecture 3: Data Types. Put it together: a filter function

Propositional Calculus. CS 270: Mathematical Foundations of Computer Science Jeremy Johnson

CSCE 314 Programming Languages

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Semantics of programming languages

Abstract Interpretation

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

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

The Substitution Model. Nate Foster Spring 2018

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

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

Mutable References. Chapter 1

Functional Programming. Pure Functional Programming

CS2104 Prog. Lang. Concepts

02157 Functional Programming Interpreters for two simple languages including exercises

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.

Visual Basic for Applications

Program Representations

Homework 3 COSE212, Fall 2018

CS 457/557: Functional Languages

Mini-ML. CS 502 Lecture 2 8/28/08

Propositional Calculus. Math Foundations of Computer Science

Propositional Calculus: Boolean Functions and Expressions. CS 270: Mathematical Foundations of Computer Science Jeremy Johnson

EECS 700 Functional Programming

Begin at the beginning

Semantics of programming languages

Relation Overriding. Syntax and Semantics. Simple Semantic Domains. Operational Semantics

Introduction to Python and Programming. 1. Python is Like a Calculator. You Type Expressions. Python Computes Their Values /2 2**3 3*4+5*6

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

CS 11 Ocaml track: lecture 7

Semantics of programming languages

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

Programming Languages

Outline. Helper Functions and Reuse. Conditionals. Evaluation Rules for cond. Design Recipe with cond. Compound Data

CSC324 Principles of Programming Languages

Programming Languages Third Edition

Plan (next 4 weeks) 1. Fast forward. 2. Rewind. 3. Slow motion. Rapid introduction to what s in OCaml. Go over the pieces individually

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

Principles of Programming Languages

Module 10: Imperative Programming, Modularization, and The Future

Formal Semantics. Prof. Clarkson Fall Today s music: Down to Earth by Peter Gabriel from the WALL-E soundtrack

CS 360 Programming Languages Interpreters

Programming Paradigms

Chapter 15. Functional Programming Languages

Recap. Recap. If-then-else expressions. If-then-else expressions. If-then-else expressions. If-then-else expressions

InterprocStack analyzer for recursive programs with finite-type and numerical variables

Programming Languages Fall 2013

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

CPS 506 Comparative Programming Languages. Programming Language Paradigm

Chapter 15 Functional Programming Languages

CMSC 330: Organization of Programming Languages. Operational Semantics

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

Programming Languages

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

Recap: Functions as first-class values

Recursive Types and Subtyping

Monty Python and the Holy Grail (1975) BBM 101. Introduction to Programming I. Lecture #03 Introduction to Python and Programming, Control Flow

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

CS558 Programming Languages

Introduction to Python and programming. Ruth Anderson UW CSE 160 Winter 2017

Programming Languages

Please write your name and username here legibly: C212/A592 6W2 Summer 2017 Early Evaluation Exam: Fundamental Programming Structures in Java

7. Introduction to Denotational Semantics. Oscar Nierstrasz

CS 320 Homework One Due 2/5 11:59pm

CS 320: Concepts of Programming Languages

CS558 Programming Languages

CS 360: Programming Languages Lecture 12: More Haskell

Programming Languages and Techniques (CIS120)

Denotational semantics (An introduction)

A general introduction to Functional Programming using Haskell

CS 360: Programming Languages Lecture 10: Introduction to Haskell

CS558 Programming Languages

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

CSE 130 [Winter 2014] Programming Languages

WELCOME! (download slides and.py files and follow along!) LECTURE 1

Module 8: Local and functional abstraction

Recap: ML s Holy Trinity

Outline. Top Down Parsing. SLL(1) Parsing. Where We Are 1/24/2013

Programming Languages Lecture 14: Sum, Product, Recursive Types

CS558 Programming Languages

CS 11 Haskell track: lecture 5. This week: State monads

Boolean expressions. Elements of Programming Languages. Conditionals. What use is this?

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

Functional Programming. Big Picture. Design of Programming Languages

ITT8060: Advanced Programming (in F#)

CSE341, Spring 2013, Final Examination June 13, 2013

Lecture 2: Big-Step Semantics

Propositional Calculus. Math Foundations of Computer Science

FUNCTIONAL PROGRAMMING 1 HASKELL BASICS

REVIEW. The C++ Programming Language. CS 151 Review #2

Transcription:

CS 381: Programming Language Fundamentals Summer 2017 Semantics July 12, 2017

Outline What is semantics? Semantics of naming 2

Why do we need semantics? Understand what program constructs do Judge the correctness of a program (compare the expected with observed behaviors) Prove properties about languages Compare languages Design languages Specification for implementation 3 What is semantics?

What is the meaning of a program? Recall aspects of a language syntax: the structure of its program semantics: the meaning of its programs 4 What is semantics?

How to define the meaning of a program Formal specifications denotational semantics: relates terms directly to values operational semantics: describes how to evaluate a term axiomatic semantics: describes the effects of evaluating a term Informal/non-specifications reference implementation: execute/compile program in some implementation community/designer intuition: how people think a program should behave 5 What is semantics?

Advantages of a formal semantics A formal semantics is simpler than an implementation, more precise than intuition can answer: is this implementation correct? supports definition of analyses and transformations prove properties about the language prove properties about programs promotes better language design better understand impact of design decisions apply semantic insights to improve elegance (simplicity + power) 6 What is semantics?

Outline What is semantics? Semantics of naming 7

A denotational semantics relates each term to a denotation an abstract syntax tree a value in a semantic domain Semantic function : abstract syntax semantics domain Semantic function in Haskell sem :: Term -> Value 8

Semantics domains Semantics domain: captures the set of possible meanings of a program/term What is a meaning? it depends on the language! Example semantic domains Language Boolean expression Arithmetic expression Imperative SQL query MiniLogo program Meaning Boolean value Integer State Transformation Set of relations Drawing 9

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

Example: simple arithmetic expression language 1. Define abstract syntax data Expr = Add Expr Expr Mul Expr Expr Neg Expr Lit Int 3. Define semantic function sem :: Expr -> 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 ExprSem.hs 2. Identify semantic domain Let s just use Int. 11

Exercise: simple arithmetic expression language Extend the expression language with sub and div operations (abstract syntax and semantics) ExprSem.hs Abstract syntax data Expr = Add Expr Expr Mul Expr Expr Neg Expr Lit Int Semantic function sem :: Expr -> 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 12

Exercise: simple arithmetic expression language Abstract syntax data Expr = Add Expr Expr Mul Expr Expr Neg Expr Semantic function sem :: Expr -> Int sem (Add l r) = sem l + sem r sem (Mul l r) = sem l * sem r Lit Int sem (Neg e) = negate (sem e) Sub Expr Expr sem (Lit n) = n Div Expr Expr sem (Sub l r) = sem l - sem r sem (Div l r) = sem l `div` sem r 13

Example: simple Boolean expression language 1. Define abstract syntax data BExpr = LitB Bool And BExpr BExpr Or BExpr BExpr Not BExpr 3. Define semantic function sem :: BExpr -> Bool sem (LitB b) = b sem (And b b ) = sem b && sem b sem (Or b b ) = sem b sem b sem (Not b) = not (sem b) BoolSem.hs 2. Identify semantic domain Let s just use Bool. 14

Exercise: simple Boolean expression language Define a Haskell function to apply DeMorgan s laws to Boolean expressions (i.e. a function to transform not (x and y) into (not x) or (not y) and accordingly not (x or y)) Semantic function Abstract syntax data BExpr =LitB Bool And BExpr BExpr Or BExpr BExpr Neg BExpr sem :: BExpr -> Bool sem (LitB b) = b sem (And b b ) = sem b && sem b sem (Or b b ) = sem b sem b sem (Neg b) = not (sem b) BoolSem.hs demorgan :: BExpr -> BExpr demorgan (Not (And (LitB x) (LitB y))) = Or (Not (LitB x)) (Not (LitB y)) demorgan (Not (Or (LitB x) (LitB y))) = And (Not (LitB x)) (Not (LitB y)) 15

Example: move language A language describing movements on a 2D plane a step is an n-unit horizontal or vertical shift a move is a sequence of steps Abstract syntax data Dir = N S E W data Step = Go Dir Int type Move = [Step] MoveLang> [Go N 3, Go E 4, Go S 1] 16

Semantics of move language 1. Define abstract syntax data Dir = N S E W data Step = Go Dir Int type Move = [Step] 3. Define semantic function sem :: Move -> Pos sem ss = foldr step (0,0) ss Move.hs 2. Identify semantic domain type Pos = (Int, Int) Define semantics of Step (helper) step :: Step -> Pos -> Pos step (Go N k) (x,y) = (x,y + k) step (Go S k) (x,y) = (x,y - k) step (Go E k) (x,y) = (x + k,y) step (Go W k) (x,y) = (x - k,y) 17

Alternative semantics Often multiple interpretations (semantics) of the same language Distance traveled type Dist = Int dist :: Move -> Dist dist = sum. move dists where dists (Go _ k) = k Example: Recipe language Library estimate time and difficulty Market extract ingredients to buy Kitchen execute to make the dish Combined trip information trip :: Move -> (Dist,Pos) trip m = (dist m,sem m) 18

Picking the right semantic domain Simple semantics domains can be combined in two ways: products: 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 IntBool.hs Can errors occur? use Haskell Maybe a or define a new data type Does the language manipulate the state or use naming? use a function type 19

Exercise: expression language with two types Extend the IntBool language by a cond operation (abstract syntax and semantics) Abstract syntax data Expr = Lit Int Add Expr Expr Equ Expr Expr Not Expr data Val = I Int B Bool TypeError Semantic function sem :: Expr -> Val sem (Lit n) = I n sem (Add l r) = case (sem l, sem r) of (I i, I j) -> I (i + j) _ -> TypeError sem (Equ l r) = case (sem l, sem r) of (I i, I j) -> B (i == j) (B a, B b) -> B (a == b) _ -> TypeError sem (Not e) = case sem e of B b -> B (not b) _ -> TypeError 20

Exercise: expression language with two types Abstract syntax data Expr = Lit Int Add Expr Expr Equ Expr Expr Not Expr Cond Expr Expr Expr data Val = I Int B Bool TypeError Semantic function sem :: Expr -> Val sem (Lit n) = I n sem (Add l r) = case (sem l, sem r) of (I i, I j) -> I (i + j) _ -> TypeError sem (Equ l r) = case (sem l, sem r) of (I i, I j) -> B (i == j) (B a, B b) -> B (a == b) _ -> TypeError sem (Not e) = case sem e of B b -> B (not b) _ -> TypeError sem (Cond f t e) = case sem f of B True -> sem t B False -> sem e 21

Outline What is semantics? Semantics of naming 22

What is naming? Most languages provide a way to name and reuse stuff Naming concepts declaration introduce a new name binding associate a name with a thing reference use the name to stand in for the bound thing C/Java variables int x, int y; x = slow(42) y = x + x + x; In Haskell: Local variables let x = slow 42 in x + x + x Type names type Radius = Float data Shape = Circle Radius Function parameters area r = pi * r * r 23 Semantics of naming

Semantics of naming Environment: a mapping associating names with things RegCalc.hs LetSem.hs type 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 semantics domains for expressions with immutable vars (Haskell) Env -> Val mutable vars (C/Java/Python) Env -> (Env,Val) 24 Semantics of naming

Example: shape language A language for constructing bitmap images, where an image is either a pixel, or a vertical or horizontal composition of images TopDown TD s1 s2 LeftRight LR s1 s2 17

Example: shape language (abstract syntax) Abstract syntax data Shape = X TD Shape Shape LR Shape Shape LR (TD X X) X TD X (LR X X) TD (LR X X) X 17

Example: shape language (semantic domain) Abstract syntax data Shape = X TD Shape Shape LR Shape Shape type Image = Array (Int,Int) Bool Semantic domain Shape -> Image type Pixel = (Int, Int) type Image = [Pixel] LR (TD X X) X semantics [(1,1),(1,2),(2,1)] 17

Example: shape language (semantic domain) Abstract syntax data Shape = X TD Shape Shape LR Shape Shape Identify semantic domain Shape -> Image Define semantic function sem :: Shape -> Image sem X = [(1,1)] sem (TD s1 s2) = adjusty ht p1 ++ p2 where p1 = sem s1 p2 = sem s2 ht = maxy p2 MaxY helper function maxy :: [(Int,Int)] -> Int maxy p = maximum (map snd p) AdjustY helper function adjusty :: Int -> [(Int,Int)] -> [(Int,Int)] adjusty ht p = [(x,y + ht) (x,y) <- p] 17

Exercise: shape language Define functions for sem (LR s1 s2), maxx, and adjustx PixelS.hs PixelSPP.hs Semantic function sem :: Shape -> Image sem (TD s1 s2) = adjusty ht p1 ++ p2 where p1 = sem s1 p2 = sem s2 ht = maxy p2 MaxY helper function maxy :: [(Int,Int)] -> Int maxy p = maximum (map snd p) AdjustY helper function adjusty :: Int -> [(Int,Int)] -> [(Int,Int)] adjusty ht p = [(x,y + ht) (x,y) <- p] 21