CSE-321 Programming Languages Dependent Types POSTECH June 5, 2006 박성우.

Slides:



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

Kathleen Fisher cs242 Reading: “Concepts in Programming Languages”, Chapter 6.
Lecture # 21 Chapter 6 Uptill 6.4. Type System A type system is a collection of rules for assigning type expressions to the various parts of the program.
1 Today’s lecture  Last lecture we started talking about control flow in MIPS (branches)  Finish up control-flow (branches) in MIPS —if/then —loops —case/switch.
CSE-321 Programming Languages Predicative Polymorphic -Calculus POSTECH May 23, 2007 박성우.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
Compiler Construction
CIS 101: Computer Programming and Problem Solving Lecture 8 Usman Roshan Department of Computer Science NJIT.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT.
Facilitating Program Verification with Dependent Types Hongwei Xi Boston University.
Pointer. Warning! Dangerous Curves C (and C++) have just about the most powerful, flexible and dangerous pointers in the world. –Most other languages.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
1 A Dependently Typed Assembly Language Hongwei Xi University of Cincinnati and Robert Harper Carnegie Mellon University.
Misc. Announcements Assignment available end of the day today –Due back in 11/03 (after break) Will also update slides on website –Today Midterm next week.
Program Design and Development
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
CSE 130 : Winter 2006 Programming Languages Ranjit Jhala UC San Diego Lecture 7: Polymorphism.
Approaches to Typing Programming Languages Robert Dewar.
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
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.
Compiling with Dependent Types Hongwei Xi University of Cincinnati.
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
CSE-321 Programming Languages Introduction to Functional Programming (Part II) POSTECH March 13, 2006 박성우.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 15: More-Advanced Concepts.
CSE-321 Programming Languages Overview POSTECH March 5, 2007 박성우.
C++ Programming Language Lecture 2 Problem Analysis and Solution Representation By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Pointers OVERVIEW.
Lecture 08 – Documentation, debugging.  docstring  A special kind of string (text) used to provide documentation  Appears at the top of a module 
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
1 Chapter 15-2 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops Barb Ericson Georgia Institute of Technology August 2005.
Java Basics.  To checkout, use: svn co scb07f12/UTORid  Before starting coding always use: svn update.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
1 Splint: A Static Memory Leakage tool Presented By: Krishna Balasubramanian.
An Undergraduate Course on Software Bug Detection Tools and Techniques Eric Larson Seattle University March 3, 2006.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Copyright Curt Hill Arrays in C/C++ More on usage.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 10 – C: the heap and manual memory management.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
CS412/413 Introduction to Compilers Radu Rugina Lecture 11: Symbol Tables 13 Feb 02.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Announcements You will receive your scores back for Assignment 2 this week. You will have an opportunity to correct your code and resubmit it for partial.
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
Debuggers. Errors in Computer Code Errors in computer programs are commonly known as bugs. Three types of errors in computer programs –Syntax errors –Runtime.
Arithmetic Instructions. Integer and Float Conversions.
Language-Based Security: Overview of Types Deepak Garg Foundations of Security and Privacy October 27, 2009.
Code improvement: Coverity static analysis Valgrind dynamic analysis GABRIELE COSMO CERN, EP/SFT.
CSE 3302 Programming Languages
Motivation and Overview
Introduction to Functional Programming in Racket
Variables Title slide variables.
CSE-321 Programming Languages Introduction to Functional Programming
CSE 3302 Programming Languages
Presentation transcript:

CSE-321 Programming Languages Dependent Types POSTECH June 5, 2006 박성우

2 Untyped Lanugage /* takes two integers and returns their sum */ fun add x y = x + y let s = add 1 2 let t = add "Oops" "Darn" runtime error

3 Typed Lanugage /* takes two integers and returns their sum */ val add : int -> int -> int fun add x y = x + y let s = add 1 2 let t = add "Oops" "Darn" compile error

4 Types as Documentations /* takes two integers and returns their sum */ val add : int -> int -> int fun add x y = x + y Types are formal documentations that the compiler recognizes. Types express properties of code. –" add takes two integers and returns an integer."

5 Types = Properties of Code? /* takes two integers and returns their sum */ val add : ??? fun add x y = x + y Then why not express this property instead: –" add takes two integers and returns their sum." What is the type for this property? –???

6 Lesson Rich type systems are always better! –We can express more properties of code. –We can catch more error at compile time. –We can better maintain code. –... Rich type systems are expensive, however. –Eg. System F rich type system but type reconstruction is undecidable.

7 Dependent Types A good compromise between –expressivity can express many useful properties of code. –cost decidable Theoretic foundation –dependent types = first-order logic

8 Outline Introduction V Ex. Array boundary checking Ex. List length Ex. Dimension analysis

A couple of slides from Tim Sweeney's invited talk at POPL 2006

10 Dynamic Failure in Mainstream Languages Solved problems:  Random memory overwrites  Memory leaks Solveable:  Accessing arrays out-of-bounds  Dereferencing null pointers  Integer overflow  Accessing uninitialized variables 50% of the bugs in Unreal can be traced to these problems!

11 Analysis of the Unreal code  Usage of integer variables in Unreal: –90% of integer variables in Unreal exist to index into arrays 80% could be dependently-typed explicitly, guaranteeing safe array access without casting. 10% would require casts upon array access. –The other 10% are used for: Computing summary statistics Encoding bit flags Various forms of low-level hackery  “For” loops in Unreal: –40% are functional comprehensions –50% are functional folds

12 Array Types Without dependent types [| 1; 2; 3 |] : int array With dependent types [| 1; 2; 3 |] : int array [3] Dependent array type –'a array [n] array of type 'a with length n

13 Array Boundary Checking Without dependent types sub : 'a array * int -> 'a update : 'a array * int * 'a -> unit With dependent types sub : 8 n:nat. 8 i:nat. {i 'a update : 8 n:nat. 8 i:nat. {i < n}. 'a array [n] * int [i] * 'a -> unit

14 Outline Introduction V Ex. Array boundary checking V Ex. List length Ex. Dimension analysis

15 List Types Without dependent types [ 1; 2; 3 ] : int list With dependent types [ 1; 2; 3 ] : int list [3] Dependent list type –'a list [n] list of type 'a with length n

16 List Constructors Nil [] : 'a list [0] Cons :: : 8 n:nat 'a -> 'a list [n] -> 'a list [n+1] Append append : 8 m:nat. 8 n:nat 'a list [m] -> 'a list [n] -> 'a list [m+n]

17 Filtering Filter a list filter : ('a -> bool) -> 'a list -> 'a list filter f nil = nil | f (h :: t) = if f h then f :: filter t else filter t With dependent types filter : 8 m:nat. 9 n:nat. {n <= m}. ('a -> bool) -> 'a list [m] -> 'a list [n]

18 Outline Introduction V Ex. Array boundary checking V Ex. List length V Ex. Dimension analysis

A slide from the first lecture, March 6

20 Mars Climate Orbiter Failure Mars Climate Orbiter launched in 1998 Destroyed due to a navigation error Cause? –One module used English units (feet). –The other module expected metric units (meter). Lessons –Both modules were fine in isolation. –Programmers did not even know the existence of the bug until the spacecraft was destroyed. –Stupidity: NASA scientists? No! programming languages they used? Yes!

21 Dependent Types for Dimension Annotate every float value with its dimension –without dependent types 1.0 : float –with dependent types 1.0 meter : float [L] Assign dependent types to arithmetic operators +. : 8 D. float [D] * float [D] -> float [D] *. : 8 D 1. 8 D 2. float [D 1 ] * float [D 2 ] -> float [D 1 * D 2 ]

22 No Mars Climate Orbiter Failure! 1.0 meter feet : float [L] 1.0 meter sec : X 1.0 meter * 1.0 sec : float [LT] mult_list : 8 D. 8 n:nat. float [D] list [n] -> float [D n ]