Manifest Contracts for Datatypes

Slides:



Advertisements
Similar presentations
あどべんちゃーにほんご L. 2か にほんごのきょうしつ /Japanese Classroom General goals of the lessons: You will be able to communicate the information below in the given situations.
Advertisements

1 PROPERTIES OF A TYPE ABSTRACT INTERPRETATER. 2 MOTIVATION OF THE EXPERIMENT § a well understood case l type inference in functional programming à la.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
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 Meta-Programming through Typeful Code Representation Chiyan Chen and Hongwei Xi Boston University.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
1 0章 数学基礎. 2 ( 定義)集合 集合については、 3セメスタ開講の「離散数学」で詳しく扱う。 集合 大学では、高校より厳密に議論を行う。そのために、議論の 対象を明確にする必要がある。 ある “ もの ” (基本的な対象、概念)の集まりを、 集合という。 集合に含まれる “ もの ” を、集合の要素または元という。
4.プッシュダウンオートマトンと 文脈自由文法の等価性
1 0章 数学基礎. 2 ( 定義)集合 集合については、 3セメスタ開講の「離散数学」で詳しく扱う。 集合 大学では、高校より厳密に議論を行う。そのために、議論の 対象を明確にする必要がある。 ある “ もの ” (基本的な対象、概念)の集まりを、 集合という。 集合に含まれる “ もの ” を、集合の要素または元という。
ビット. 十進数と二進数 十進数  0から9までの数字を使って 0、1、2、3、4、5、6、7、8、9、 10、11、12 と数える 二進数  0と1を使って 0、1、10、11、100、101、11 0、111 と数える.
3.正方行列(単位行列、逆行列、対称行列、交代行列)
JPN 312 (Fall 2007): Conversation and Composition Contraction (2); 意見を言う (to express your opinion)
SUPJ2010 Japanese Ⅱ( A ) Elementary Japanes e ‐ in twenty hours- Chapter 7.
JPN 311: Conversation and Composition 勧誘 (invitation)
JPN 311: Conversation and Composition 許可 (permission)
地図に親しむ 「しゅくしゃくのちがう 地図を 使ってきょりを調べよ う1」 小学4年 社会. 山口駅裁判所 県立 美術館 サビエル 記念聖堂 山口市役所 地図で探そう 市民会館 県立 図書館.
01/17/20031 Guarded Recursive Datatype Constructors Hongwei Xi and Chiyan Chen and Gang Chen Boston University.
JPN 312 (Fall 2007): Conversation and Composition 文句 ( もんく ) を言う.
SUPJ2010 Japanese Ⅱ( A ) Elementary Japanes e ‐ in twenty hours- Chapter 8.
1 中野研究室 4 年ゼミのイロハ 斉藤(修士 2 年) ( 2009 年 ”4 年ゼミのイロハ ” を参考に作りました)
Elementary Japanese ‐in twenty hours- Chapter 9
漢字練習 第7課第7課第7課第7課. しる 知る まつ 待つ もつ 持つ まつ 待つ しる 知る.
地図に親しむ 「じっさいのけしきと 地図をくらべよう」
SUPJ2010 Japanese Ⅱ( A ) Elementary Japanes e ‐ in twenty hours- Review of Japanese Ⅰ (B)
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
LANG3910 Japanese Ⅲ Lesson 14 依頼・現在進行形. 学習項目 1. 「て -form 」 2. 依頼表現 An expression of request 3. 相手の意向を尋ねる Ask someone’s mind 4. 現在進行形 Actions in Progress.
Cormac Flanagan University of California, Santa Cruz Hybrid Type Checking.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
Joining Adjectives て form for い adjectives and な adjectives.
ISBN Chapter 3 Describing Semantics -Attribute Grammars -Dynamic Semantics.
Type Safety Kangwon National University 임현승 Programming Languages.
2006/12/081 Large Scale Crawling the Web for Parallel Texts Chikayama Taura lab. M1 Dai Saito.
知能ソフトウェア特論 項書換え系(2) 停止性 Term Rewriting Systems(2) Termination Intelligent Software.
日本語きほん文法の復習 Basic Japanese Grammar Review
と. What does it mean???? ビールを 飲むと ねむく なりま す 四月に なると さくらが さきま す たばこを すうと からだに わる いです.
Types and Programming Languages Lecture 12 Simon Gay Department of Computing Science University of Glasgow 2006/07.
B 04 How to Type in Japanese How do you TYPE in Japanese?
Types and Programming Languages Lecture 11 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Event 8: The Old Lady スタート Start. ばあちゃんの質問に答えましょう ( Answer Baachan’s questions ) When the user clicks the help button, this instruction will appear in.
Shifting the Blame A blame calculus with delimited control Taro Sekiyama* Atsushi Igarashi Soichiro Ueda Kyoto University Gradual typing.
CMSC 330: Organization of Programming Languages Operational Semantics.
英語勉強会 (橋本さんの) 10月9日 坂田梨紗. 英語の文章の 成り立ち 言いたいこと 説明 言いたいこと I went to the library to read Harry Potter.
RELATIVE CLAUSES Adjectival Clauses/Modifiers. RELATIVE CLAUSES A relative clause is the part of a sentence which describes a noun Eg. The cake (which)
J3H 9 月 24 日 今日の目標 ( もくひょう) To practice and become more proficient w/ 〜たり〜たり form To learn how to “nominalize” a verb To find a friend (???)
Topics  Direct Predicate Characterization as an evaluation method.  Implementation and Testing of the Approach.  Conclusions and Future Work.
1 Interactive Computer Theorem Proving CS294-9 October 19, 2006 Adam Chlipala UC Berkeley Lecture 9: Beyond Primitive Recursion.
1 Interactive Computer Theorem Proving CS294-9 September 7, 2006 Adam Chlipala UC Berkeley Lecture 3: Data structures and Induction.
An Integrated Theory of Type-Based Static and Dynamic Verification Taro Sekiyama Dept. of Communications and Computer Engineering Graduate School of Informatics.
Functional Programming
String is a synonym for the type [Char].
Recursion Topic 5.
Programming Languages and Compilers (CS 421)
CSE341: Programming Languages Lecture 11 Type Inference
Principles of programming languages 12: Functional programming
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
ML: a quasi-functional language with strong typing
A lightening tour in 45 minutes
Stateful Manifest Contracts
PROGRAMMING IN HASKELL
Propositional Calculus: Boolean Algebra and Simplification
Software Testing (Lecture 11-a)
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
CSCE 314: Programming Languages Dr. Dylan Shell
CSE S. Tanimoto Lambda Calculus
Programming Languages and Compilers (CS 421)
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 11 Type Inference
Static Contract Checking for Haskell
Presentation transcript:

Manifest Contracts for Datatypes Taro Sekiyama, Atsushi Igarashi, and Yuki Nishida いろ 前を見てしゃべる 飛ばす練習 下を使わないように Kyoto University

Manifest Datatypes for Contracts. Taro Sekiyama et al. Data structures Data structures are crucial to design algorithms Efficient algorithms need fine-grained specifications on data structures E.g., binary search val binary_search : int -> int array -> int option An argument array is required to be sorted! Manifest Datatypes for Contracts. Taro Sekiyama et al.

Specifications for data structures Two styles in giving specifications for data structures as types “Extrinsic” style { x:int list | sorted x } “Intrinsic” style type sorted_list = | SNil | SCons of x:int * {xs:slist|(nil xs) or (x < head xs)} Manifest Datatypes for Contracts. Taro Sekiyama et al.

Manifest Datatypes for Contracts. Taro Sekiyama et al. Extrinsic style Specifications are given to plain structures E.g., sorted lists are represented as { x:int list | sorted x } where “sorted x” expresses that x is sorted Work so far: subset types flat contracts etc. Manifest Datatypes for Contracts. Taro Sekiyama et al.

Intrinsic style Specifications are given to data constructors E.g., sorted lists are represented as type slist = | SNil | SCons of x:int * { xs:slist | (nil xs) or (x < head xs) } Work so far: inductive types [Pfenning & Paulin-Mohring 1989] GADTs [Cheney & Hinze 2003; Xi et al. 2003] etc. 1 the head is less than the head of the tail (if any) Manifest Datatypes for Contracts. Taro Sekiyama et al.

Pros and cons of two styles as contracts Extrinsic style {x:int list|sorted x} easy to write programs poor information on substructures Intrinsic style type slist = … rich information on substructures difficult to write programs Our work: transformations to take the best of both worlds

Pros and cons of two styles E.g., tail parts of sorted lists are merely lists (no specs) Checking specs dynamically can worsen asymptotic time complexity Pros Cons Extrinsic style { x:int list | sorted x } easy to write programs poor information on substructures Intrinsic style type slist = … rich information on substructures difficult to write programs Our work: transformations to take the best of both worlds

Extrinsic style can worsen time complexity let tail (y : { x:int list | sorted x }) : { x:int list | sorted x } = match y with | [] -> [] | z::zs -> zs returns the tail of an argument list (if any) When sorted is checked dynamically, the asymptotic time complexity turns out to be O(length x) Manifest Datatypes for Contracts. Taro Sekiyama et al.

Intrinsic style can preserve time complexity let tail (y : slist) : slist = match y with | SNil -> SNil | SCons (z, zs) -> zs The type of zs is slist, so the asymptotic time complexity is O(1) type slist = | SNil of unit | SCons of x:int * { xs: slist | (nil xs) or (x < head xs) } Manifest Datatypes for Contracts. Taro Sekiyama et al.

Pros and cons of two styles E.g., tail parts of sorted lists are merely lists (no specs) Checking specs dynamically can worsen asymptotic time complexity Pros Cons Extrinsic style { x:int list | sorted x } easy to write programs poor information on substructures Intrinsic style type slist = … rich information on substructures difficult to write programs

Pros and cons of two styles E.g., tail parts of sorted lists are merely lists (no specs) Checking specs dynamically can worsen asymptotic time complexity Pros Cons Extrinsic style { x:int list | sorted x } easy to write programs poor information on substructures Intrinsic style type slist = … rich information on substructures difficult to write programs 1. Refining constructors is unfamiliar for programmers 2. Library problem, e.g., all list-functions cannot be applied to slist head (SCons (1, SNil)) int list -> int slist ✕ Manifest Datatypes for Contracts. Taro Sekiyama et al.

Our ideas for better use of intrinsic style 1. Static translation from a type in extrinsic style to one in intrinsic style {x:int list|sorted x}  type slist = … 2. Dynamic conversion between data structures in both styles head (<int list <= slist>l SCons (1, SNil)) Use case を復習 Manifest Datatypes for Contracts. Taro Sekiyama et al.

How do the ideas encourage use of intrinsic style? Programmers can obtain dynamically efficient datatypes easily, using type translation (the 1st idea) {x:int list|sorted x}  type slist = … They write programs using the generated datatypes If needed, they can reuse functions for original data structures, using dynamic conversion (the 2nd idea) let tail (y : slist) : slist = … head (<int list <= slist>l (SCons (1, SNil))) Manifest Datatypes for Contracts. Taro Sekiyama et al.

Manifest Datatypes for Contracts. Taro Sekiyama et al. This Work We give a lambda-calculus based on manifest contracts and formalize the ideas in the calculus Manifest contracts [Flanagan 2006; Greenberg et al. 2010] are a framework which can combine static and dynamic specification checkings We implement the dynamic conversion mechanism on OCaml, using the extensible preprocessor (Camlp4) Available at http://goo.gl/VMhAv2 url を書く Manifest Datatypes for Contracts. Taro Sekiyama et al.

Contents Manifest Contracts with Datatypes Dynamic Type Conversion Refinement types Manifest Datatypes Dynamic Type Conversion Syntactic Type Translation <int list <= slist>l {x:int list|sorted x}  type slist = … Manifest Datatypes for Contracts. Taro Sekiyama et al.

Manifest Datatypes for Contracts. Taro Sekiyama et al. Software contracts Specifications of program components impossible to represent as simple types Dynamically enforced written in an executable form, i.e., as programs Manifest Datatypes for Contracts. Taro Sekiyama et al.

Manifest contracts [Flanagan 2006; Greenberg et al. 2010] Contracts are made “manifest” as part of types Refinement types (a.k.a. subset types) {x:T|e} denotes the set of values of type T satisfying the Boolean expression e Contracts are checked statically or dynamically This work concerns only dynamic checking E.g., {x:int|0 < x} means positive integers Manifest Datatypes for Contracts. Taro Sekiyama et al.

Manifest Datatypes for Contracts. Taro Sekiyama et al. Data constructors are given contracts This notion itself is not new type slist = | SNil of unit | SCons of x:int * {xs:slist|(nil xs) or (x < head xs)} Sorted Lists (Manifest Datatype ver.) Manifest Datatypes for Contracts. Taro Sekiyama et al.

Contents Manifest Contracts with Datatypes Dynamic Type Conversion Refinement types Manifest Datatypes Dynamic Type Conversion Syntactic Type Translation <int list <= slist>l {x:int list|sorted x}  type slist = … Manifest Datatypes for Contracts. Taro Sekiyama et al.

Type conversion in manifest contracts [Flanagan 2006] <T1 <= T2>l checks that a value of T2 works as T1 E.g., conversion for base types <{x:int|0 < x} <= int>l 4 4 <{x:int|0 < x} <= int>l 0 l <= を一文字に checks that given integers are positive 4 is positive, but 0 is not; so exception l is raised Manifest Datatypes for Contracts. Taro Sekiyama et al.

Type conversion in manifest contracts [Flanagan 2006] <T1 <= T2>l checks that a value of T2 works as T1 E.g., conversion for dependent pair types <x:int*{y:int| x < y} <= int*int>l (4,6) (4,<{y:int|4 < y} <= int>l 6) (4,6) checks that the first integer is less than the second Manifest Datatypes for Contracts. Taro Sekiyama et al.

Type conversion to sorted lists ≈ < slist <= int list >l checks that integer lists are sorted replaces each constructor of a given list to corresponding one of slist checks the contract in the type of the constructor < slist <= int list >l (1 :: []) ----- 会議メモ (2014/12/29 15:00) ----- compatible を italic に ----- 会議メモ (2015/01/07 14:10) ----- 手順をハイライト type slist = | SNil of unit | SCons of x:int * { xs:slist | (nil xs) or (x < head xs) } Manifest Datatypes for Contracts. Taro Sekiyama et al. 22

Type conversion to sorted lists ≈ < slist <= int list >l replaces each constructor of a given list to corresponding one of slist checks the contract in the type of the constructor < slist <= int list >l (1 :: []) ----- 会議メモ (2014/12/29 15:00) ----- compatible を italic に ----- 会議メモ (2015/01/07 14:10) ----- 手順をハイライト SCons type slist = | SNil of unit | SCons of x:int * { xs:slist | (nil xs) or (x < head xs) } Manifest Datatypes for Contracts. Taro Sekiyama et al. 23

Type conversion to sorted lists ≈ < slist <= int list >l replaces each constructor of a given list to corresponding one of slist checks the contract in the type of the constructor < slist <= int list >l (1 :: []) ----- 会議メモ (2014/12/29 15:00) ----- compatible を italic に ----- 会議メモ (2015/01/07 14:10) ----- 手順をハイライト SCons (<T <= int*int list>l (1, [])) type slist = | SNil of unit | SCons of x:int * { xs:slist | (nil xs) or (x < head xs) } Manifest Datatypes for Contracts. Taro Sekiyama et al. 24

Type conversion to sorted lists <slist ⇐ int list>l (1 :: []) SCons (<T ⇐ int * int list>l (1, [])) SCons (1, <{ xs:slist | (nil xs) or (1 < head xs) } ⇐ int list>l []) SCons (1, (<slist ⇐ int list>l [])) <{ xs:slist | (nil xs) or (1 < head xs) } ⇐ slist>l (2) (1) SCons (1, <{ xs:slist | (nil xs) or (1 < head xs) } ⇐ slist>l SNil) Split into two: conversion from integer lists to slist conversion to check the contract SCons (1, SNil) type slist = | SNil of unit | SCons of x:int * { xs:slist | (nil xs) or (x < head xs) } “nil xs” means xs = SNil Manifest Datatypes for Contracts. Taro Sekiyama et al.

Nontrivial example: list_containing0 lists containing 0 ≈ type list_containing0 = | C1 of int * list_containing0 | C2 of { x:int | x = 0 } * int list No constructors corresponding to [] Two constructors corresponding to (::) The more meaninig of trial-and-error Either C1 or C2 has to be chosen dynamically Formal semantics: an oracle choice function Implementation: trial-and-error (backtracking) Manifest Datatypes for Contracts. Taro Sekiyama et al.

Manifest Datatypes for Contracts. Taro Sekiyama et al. Formalization We formalize a manifest calculus with manifest datatypes, following the syntactic approach [Belo et al. 2011] The calculus supports dynamic conversion between manifest datatypes We prove type soundness via progress and subject reduction Exceptions are legitimate results We fix a few technical flaws in the previous work Manifest Datatypes for Contracts. Taro Sekiyama et al.

Contents Manifest Contracts with Datatypes Dynamic Type Conversion Refinement types Manifest Datatypes Dynamic Type Conversion Syntactic Type Translation <int list ⇐ slist>l { x:int list | sorted x }  type slist = … Manifest Datatypes for Contracts. Taro Sekiyama et al.

Syntactic type translation Returns whether x contains 0 { x:int list | contains0 x } Refinement Type Manifest Datatype 矢印を横に Manifest Datatypes for Contracts. Taro Sekiyama et al.

Manifest Datatypes for Contracts. Taro Sekiyama et al. Running example An implementation of contains0 is: let contains0 y = match y with | [] -> false | x::xs -> if x = 0 then true else contains0 xs Manifest Datatypes for Contracts. Taro Sekiyama et al.

Ideas of translation To collect guard conditions on execution paths reaching true from branches for [] and (::) There are two execution paths for (::): (1) x = 0 (2) x <> 0 & contains0 xs There are no execution paths reaching true from the branch for [] ----- 会議メモ (2014/12/29 15:00) ----- work well と work の説明 let contains0 y = match y with | [] -> false | x::xs -> if x = 0 then true else contains0 xs Manifest Datatypes for Contracts. Taro Sekiyama et al.

Ideas of translation The new datatype has one constructor for each execution path; the contract of it is conjunction of the guard conditions for the path The new datatype has two constructors: C1 : { x:int | x = 0 } * int list C2 : { x:int | x <> 0 } * { xs:int list | contains0 xs } The guard conditions for (::) (1) x = 0 (2) x <> 0 & contains0 xs let contains0 y = match y with | [] -> false | x::xs -> if x = 0 then true else contains0 xs Manifest Datatypes for Contracts. Taro Sekiyama et al.

Manifest Datatypes for Contracts. Taro Sekiyama et al. Ideas of translation The recursive call becomes type-level recursion The argument type of C2 transforms from { x:int | x <> 0 } * { xs:int list | contains0 xs } to { x:int | x <> 0 } * list_containing0 let contains0 y = match y with | [] -> false | x::xs -> if x = 0 then true else contains0 xs Manifest Datatypes for Contracts. Taro Sekiyama et al.

Manifest Datatypes for Contracts. Taro Sekiyama et al. Resulting datatype let contains0 y = match y with | [] -> false | x::xs -> if x = 0 then true else contains0 xs { x:int list | contains0 x } type list_containing0’ = | C1 of { x:int | x = 0 } * int list | C2 of { x:int | x <> 0 } * list_containing0’ Manifest Datatypes for Contracts. Taro Sekiyama et al.

Formalization We formalize translation for only integer lists Generalization would be possible but cumbersome We prove its correctness: the generated datatype is equivalent to the original refinement type Dynamic conversion between a refinement type and the new datatype always succeeds in both directions なんかへん E.g., dynamic checks with <list_containing0’ ⇐ { x:int list | contains0 x }>l <{ x:int list | contains0 x } ⇐ list_containing0’>l always succeeds! Manifest Datatypes for Contracts. Taro Sekiyama et al.

FAQ about translation Q. Is the generated datatype dynamically efficient representation? A. Yes, it is at least as efficient as the original refinement type Conversion to the new datatype involves the same computation as checking the contract in the refinement type Q. What predicate functions does translation work well for? Ones written in the fold form (at least) We discuss these in the paper in more details Manifest Datatypes for Contracts. Taro Sekiyama et al.

Manifest Datatypes for Contracts. Taro Sekiyama et al. In the paper … Manifest datatypes abstracted over value variables The translation algorithm supports that form Discussion on extension of type translation to other data structures, e.g., trees Formalization and proofs Our manifest calculus and syntactic type translation A prototype implementation of our calculus (without type translation) http://goo.gl/VMhAv2 Manifest Datatypes for Contracts. Taro Sekiyama et al.

Manifest Datatypes for Contracts. Taro Sekiyama et al. Related work (1) Lazy Contract Checking for Immutable Data Structures (Findler et al., IFL ‘07) The first work (as far as I know) that discussed pros and cons of extrinsic and intrinsic styles They attempted to resolve the inefficiency problem of extrinsic style by introducing lazy contract checking Manifest Datatypes for Contracts. Taro Sekiyama et al.

Manifest Datatypes for Contracts. Taro Sekiyama et al. Related work (2) Ornamental Algebras, Algebraic Ornaments (McBride, ‘12) Refining Inductive Types (Atkey et al., LMCS ’12) They have studied systematic derivation of inductive datatypes They don’t concern dynamic aspects of datatypes Manifest Datatypes for Contracts. Taro Sekiyama et al.

Future work Issue: reusing functions with type conversion could need a significant computation cost Approaches: Lazy Contract Checking for Immutable Data Structures (Findler et al., IFL ‘07) A new calculus where such conversions are available for free The asymptotic time complexity is O(length x), not O(1) head (<int list ⇐ slist>l x) 取り組むは英語で? Manifest Datatypes for Contracts. Taro Sekiyama et al.

Manifest Datatypes for Contracts. Taro Sekiyama et al. Conclusion We formulate specs in extrinsic and intrinsic styles as refinement types and manifest datatypes, resp. We give two ways to take the best of both worlds Dynamic type conversion Syntactic type translation We propose a manifest calculus with manifest datatypes to formalize our ideas Prototype is available at http://goo.gl/VMhAv2 証明の省略 ommit of Cast reflexivity Benjamin へ イントロ を読むようにお願い Authorship Belo について Manifest Datatypes for Contracts. Taro Sekiyama et al.

Manifest Datatypes for Contracts. Taro Sekiyama et al. Thank you! Questions? Slowly, please  Manifest Datatypes for Contracts. Taro Sekiyama et al.