Types and Type Checking (What is it good for?)

Slides:



Advertisements
Similar presentations
Automated Theorem Proving Lecture 1. Program verification is undecidable! Given program P and specification S, does P satisfy S?
Advertisements

Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
Introducing Formal Methods, Module 1, Version 1.1, Oct., Formal Specification and Analytical Verification L 5.
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
CS7100 (Prasad)L16-7AG1 Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification.
Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.
Winter Compiler Construction T7 – semantic analysis part II type-checking Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv.
Type checking © Marcelo d’Amorim 2010.
ISBN Chapter 3 Describing Syntax and Semantics.
Fall Semantics Juan Carlos Guzmán CS 3123 Programming Languages Concepts Southern Polytechnic State University.
Chapter 6 Type Checking Section 0 Overview 1.Static Checking Check that the source program follows both the syntactic and semantic conventions of the source.
Nathan Brunelle Department of Computer Science University of Virginia Theory of Computation CS3102 – Spring 2014 A tale.
Semantic analysis Parsing only verifies that the program consists of tokens arranged in a syntactically-valid combination, we now move on to semantic analysis,
Chapter 6 Type Checking Section 0 Overview
1 Decidability continued. 2 Undecidable Problems Halting Problem: Does machine halt on input ? State-entry Problem: Does machine enter state halt on input.
1 Combining verification and analysis. 2 CONCLUSIONS ON VERIFICATION  denotational abstract interpreters have the extra-value of being easily transformed.
1 Type Type system for a programming language = –set of types AND – rules that specify how a typed program is allowed to behave Why? –to generate better.
Describing Syntax and Semantics
Fundamentals of Python: From First Programs Through Data Structures
INF5110: Mandatory Exercise 2 Eyvind W. Axelsen @eyvindwa Slides are partly based on.
410/510 1 of 18 Week 5 – Lecture 1 Semantic Analysis Compiler Construction.
D Goforth COSC Translating High Level Languages Note error in assignment 1: #4 - refer to Example grammar 3.4, p. 126.
Programming Languages
Types and Programming Languages Lecture 11 Simon Gay Department of Computing Science University of Glasgow 2006/07.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
SAFE KERNEL EXTENSIONS WITHOUT RUN-TIME CHECKING George C. Necula Peter Lee Carnegie Mellon U.
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
This Week Lecture on relational semantics Exercises on logic and relations Labs on using Isabelle to do proofs.
CMSC 330: Organization of Programming Languages Operational Semantics.
Compilers Computer Symbol Table Output Scanner (lexical analysis)
Language Implementation Overview John Keyser Spring 2016.
Parser: CFG, BNF Backus-Naur Form is notational variant of Context Free Grammar. Invented to specify syntax of ALGOL in late 1950’s Uses ::= to indicate.
Types and Programming Languages Lecture 3 Simon Gay Department of Computing Science University of Glasgow 2006/07.
©SoftMoore ConsultingSlide 1 Structure of Compilers.
FUNCTIONAL PROGRAMING AT WORK - HASKELL AND DOMAIN SPECIFIC LANGUAGES Dr. John Peterson Western State Colorado University.
1 A well-parenthesized string is a string with the same number of (‘s as )’s which has the property that every prefix of the string has at least as many.
Programming Languages Concepts Chapter 1: Programming Languages Concepts Lecture # 4.
Software Engineering Algorithms, Compilers, & Lifecycle.
The Church-Turing Thesis Chapter Are We Done? FSM  PDA  Turing machine Is this the end of the line? There are still problems we cannot solve:
LECTURE 10 Semantic Analysis. REVIEW So far, we’ve covered the following: Compilation methods: compilation vs. interpretation. The overall compilation.
Logical Agents. Outline Knowledge-based agents Logic in general - models and entailment Propositional (Boolean) logic Equivalence, validity, satisfiability.
Chapter 3 – Describing Syntax
Programming Languages
Types for Programs and Proofs
Context-Sensitive Analysis
Proof Carrying Code and Proof Preserving Program Transformations
Representation, Syntax, Paradigms, Types
Busch Complexity Lectures: Reductions
FORMAL LANGUAGES AND AUTOMATA THEORY
Reductions.
PROGRAMMING LANGUAGES
Simone Campanoni Welcome! Simone Campanoni
Emily Leland (Not Nick) Spring 2017
CPSC 388 – Compiler Design and Construction
Typed Arithmetic Expressions
Preparing for MUPL! Justin Harjanto
Programming Languages
Compilers B V Sai Aravind (11CS10008).
CSE 341 Section 7 Winter 2018 Adapted from slides by Eric Mullen, Nicholas Shahan, Dan Grossman, and Tam Dang.
Programming Languages 2nd edition Tucker and Noonan
Undecidable problems:
Representation, Syntax, Paradigms, Types
Adapted from slides by Nicholas Shahan and Dan Grossman
Adapted from slides by Nicholas Shahan, Dan Grossman, and Tam Dang
Representation, Syntax, Paradigms, Types
Nicholas Shahan Spring 2016
High-Level Programming Language
Decidability continued….
A Tour of Language Implementation
Faculty of Computer Science and Information System
Presentation transcript:

Types and Type Checking (What is it good for?) Stephen Chang Racket Summer School 2018 (Thursday morning)

A quick survey ?

The Holy Grail of PL Research … (one of) … is to predict behavior of programs (without running them) Prevent bugs Avoid malware Prove equivalence

Abandon all hope? “All non-trivial, semantic properties of programs are undecidable” --- Rice’s theorem

Enter … Type Systems “A lightweight, syntactic analysis that approximates program behavior” Validate function arguments (Prevent bugs) Check memory safety (Avoid malware) Verify program properties (Prove equivalence)

Compiler (includes macro expander) Typed Languages Surface code Reader / Lexer / Parser Yesterday AST (e.g., syntax objects) Type Checker Today Checked AST Compiler (includes macro expander) Core code / bytecode / machine code

How to create typed languages? Incorporate types into the grammar. Come up with a language of types. Develop type rules for each language construct. Implement a type checker.

How to create typed languages? Incorporate types into the grammar

How to create typed languages? 2. Come up with language of types

Specifying Type Systems: Inference Rules 3. Develop type rules for each language construct. Premise 1 Premise 2 IF: Premise 3 Conclusion THEN:

If type systems were a video game … THEN:

Type Judgements ⊢𝑒 : 𝜏 “It is true, that expression e has type τ”

How to create typed languages? 3. Develop rules for each language construct

A rule for string literals “It is true, that string literals have type String”

Function Application Type Rule “If: it is true, that expression e1 has type 𝜏 𝑖𝑛 → 𝜏 𝑜𝑢𝑡 ” “and e2 has type 𝜏 𝑖𝑛 ” ⊢ 𝑒 1 : 𝜏 𝑖𝑛 → 𝜏 𝑜𝑢𝑡 ⊢ 𝑒 2 : 𝜏 𝑖𝑛 ⊢ 𝑒 1 𝑒 2 : 𝜏 𝑜𝑢𝑡 “Then: it is true that e1 e2 has type 𝜏 𝑜𝑢𝑡 ”

⊢ 𝑒 1 :𝐵𝑜𝑜𝑙 ⊢ 𝑒 2 :𝜏 ⊢ 𝑒 3 :𝜏 ⊢ 𝑖𝑓 𝑒 1 𝑒 2 𝑒 3 :𝜏 If Type Rule ⊢ 𝑒 1 :𝐵𝑜𝑜𝑙 ⊢ 𝑒 2 :𝜏 ⊢ 𝑒 3 :𝜏 ⊢ 𝑖𝑓 𝑒 1 𝑒 2 𝑒 3 :𝜏

⊢ 𝑒 1 :𝐼𝑛𝑡 ⊢ 𝑒 2 :𝐼𝑛𝑡 ⊢ 𝑒 1 + 𝑒 2 :𝐼𝑛𝑡 Plus type rule ⊢ 𝑒 1 :𝐼𝑛𝑡 ⊢ 𝑒 2 :𝐼𝑛𝑡 ⊢ 𝑒 1 + 𝑒 2 :𝐼𝑛𝑡

Variables? ⊢𝑥 : ??? A variable’s meaning depends on its context

⊢ ⊢

Variables : Type depends on context “In context Γ, x has type τ” Γ 𝑥 =𝜏 Γ⊢𝑥 : 𝜏 Context (or Type Environment) Γ= 𝑥 :𝜏,…

Type rule for lambda functions “In context Γ, extended with x, which has type τin, e has type τout” Γ, 𝑥: 𝜏 𝑖𝑛 ⊢𝑒 : 𝜏 𝑜𝑢𝑡 Γ⊢𝜆𝑥: 𝜏 𝑖𝑛 .𝑒 : 𝜏 𝑖𝑛 → 𝜏 𝑜𝑢𝑡

From Type System … to Type Checking These type rules say nothing about how to check types, i.e., they are not an algorithm

If rule: a specification ⊢ 𝑒 1 :𝐵𝑜𝑜𝑙 ⊢ 𝑒 2 :𝜏 ⊢ 𝑒 3 :𝜏 ⊢ 𝑖𝑓 𝑒 1 𝑒 2 𝑒 3 :𝜏 IF: e1 has type Bool, and e2 has type τ, and e3 has type τ THEN: if e1 e2 e3 has type τ

If rule: a checking algorithm Compute e1’s type: τ1 Check that τ1 is Bool Compute e2’s type: τ2 Compute e3’s type: τ3 Check that e2’s type equals e3’s type Assign (if e1 e2 e3) e2’s type ⊢ 𝑒 1 : 𝜏 1 𝜏 1 =𝐵𝑜𝑜𝑙 ⊢ 𝑒 2 : 𝜏 2 ⊢ 𝑒 3 : 𝜏 3 𝜏 2 = 𝜏 3 ⊢ 𝑖𝑓 𝑒 1 𝑒 2 𝑒 3 : 𝜏 2

“Bidirectional” judgements: better for specifying algorithms ⇐ = “check” the type ⇒ = “compute” the type

⊢ 𝑒 1 ⇐𝐵𝑜𝑜𝑙 ⊢ 𝑒 2 ⇒𝜏 ⊢ 𝑒 3 ⇐𝜏 ⊢ 𝑖𝑓 𝑒 1 𝑒 2 𝑒 3 ⇒𝜏 A bidirectional If rule Check that e1 has type Bool Compute e2’s type as τ Check that e3 has type τ Assign (if e1 e2 e3) type τ ⊢ 𝑒 1 ⇐𝐵𝑜𝑜𝑙 ⊢ 𝑒 2 ⇒𝜏 ⊢ 𝑒 3 ⇐𝜏 ⊢ 𝑖𝑓 𝑒 1 𝑒 2 𝑒 3 ⇒𝜏

“Bidirectional” judgements ⇐ = “check” the type ⇒ = “compute” the type But now we have two judgements!

⊢ 𝑒 1 ⇐𝐵𝑜𝑜𝑙 ⊢ 𝑒 2 ⇐𝜏 ⊢ 𝑒 3 ⇐𝜏 ⊢ 𝑖𝑓 𝑒 1 𝑒 2 𝑒 3 ⇐𝜏 Another bidirectional If rule Check that e1 has type Bool Compute e2’s type as τ Check that e3 has type τ Assign (if e1 e2 e3) type τ ⊢ 𝑒 1 ⇐𝐵𝑜𝑜𝑙 ⊢ 𝑒 2 ⇐𝜏 ⊢ 𝑒 3 ⇐𝜏 ⊢ 𝑖𝑓 𝑒 1 𝑒 2 𝑒 3 ⇐𝜏

LAB, part 1 Develop bidirectional rules for our language. Focus on the expressions first. It might help to first review the “conventional” type rules. Make sure to come up with both “check” and “compute” versions of the bidirectional rules.

LAB, part 2 Use your rules to implement a “compute” type checking function.