[PDF] Polymorphic Functions with Set-Theoretic Types. Part 2: Local Type





Previous PDF Next PDF



CMSC 838T Lecture

Trees & Binary Search Trees. Department of Computer Science Tree Traversal. Goal. Visit every node in binary tree ... Using a Polymorphic Binary Tree.



RECURSIVE BST OPERATIONS with more Java generics

Let's implement a BST class avoiding iteration. This will give us more practice with trees



Topic 19 Binary Search Trees

A binary search tree is a binary tree in which every simple insertion algorithm). A. O(logN) ... use structural recursion and polymorphism. Binary ...



Lets Verify This with Why3

28 mars 2014 Reference Java implementations were given for the first two ... stance polymorphic lists and binary trees are defined in the.



Polymorphic Functions with Set-Theoretic Types. Part 2: Local Type

26 nov. 2014 in C# and Java type parameters for polymorphic/generic method calls ... A red and black tree is a colored binary search tree in which all ...





Deductive Program Verification with Why3 A Tutorial

So far Why3 has been successfully used that way to verify Java programs [24]



Script:Searching and sorting in Haskell

Binary trees data Tree a = Empty



Efficient Dynamic Dispatch without Virtual Function Tables. The

14 févr. 2011 in HCU91 we use binary search to check the receiver ... other polymorphic call site



Polymorphic Functions with Set-Theoretic Types

in C# and Java type parameters for polymorphic/generic method calls can A red and black tree is a colored binary search tree in which all nodes are ...



Polymorphic Lists & Trees

Polymorphic Binary Search Trees Second approach to implement BST What do we mean by polymorphic? Implement two subtypes of Tree EmptyTree NonEmptyTree Use EmptyTreeto represent the empty tree Rather than null Invoke methods on tree nodes Without checking for null (IMPORTANT!) Polymorphic Binary Tree Implementation Interface Tree {



Search in a Binary Search Tree - LeetCode

Polymorphic Binary Search Trees •Second approach to implement BST •What do we mean by polymorphic? •Implement two subtypes of Tree • EmptyTree • NonEmptyTree •Use EmptyTreeto represent the empty tree • Rather than null •Invoke methods on tree nodes • Without checking for null (IMPORTANT!)



Binary Search Trees - Princeton University

Binary Search Trees Reference: Chapter 12 Algorithms in Java 3 rd Edition Robert Sedgewick Binary search trees Randomized BSTs 2 Guaranteeing Performance Symbol table: key-value pair abstraction Insert a value with specified key Search for value given key Delete value with given key Challenge 1: guarantee symbol table performance



42 Binary Search Trees - Princeton University

A BST is a binary tree in symmetric order A binary tree is either: ¥Empty ¥Two disjoint binary trees (left and right) Symmetric order Each node has a key and every nodeÕs key is: ¥Larger than all keys in its left subtree ¥Smaller than all keys in its right subtree 2 Binary search trees right child of root a left link a subtree root



Binary Trees - Stanford University

Binary Search Tree Niche Basically binary search trees are fast at insert and lookup The next section presents the code for these two algorithms On average a binary search tree algorithm can locate a node in an N node tree in order lg(N) time (log base 2) Therefore binary search trees are good for "dictionary" problems where the code



Searches related to polymorphic binary search tree java filetype:pdf

Binary Search Trees in Java A BST is a reference to a node A Node is comprised of four fields: A key and a value A reference to the left and right subtree private class Node { Key key; Val val; Node l leftr; smaller Key and Val are generic types; Key is Comparable right larger 51 root 14 68 12 54 79 5

How to search in a binary search tree?

    Search in a Binary Search Tree - LeetCode Search in a Binary Search Tree - You are given the root of a binary search tree (BST) and an integer val. Find the node in the BST that the node's value equals val and return the subtree rooted with that node. If such a node does not exist, return null.

What is the running time complexity of a binary search tree?

    The running time complexity is O (log (n)) time; the characteristics that distinguish a binary search tree from a basic binary tree are as follows – 1. The nodes of the left subtree are all smaller than the root node. 2. The nodes of the right subtree are all greater than the root node.

What is a binary tree in Java?

    In Java, we will have a BinaryTree object that contains a single root pointer. The root pointer points to an internal Node class that behaves just like the node struct in the C/C++ version. The Node class is private -- it is used only for internal storage inside the BinaryTree and is not exposed to clients.

What is a binary tree OOP?

    With this OOP structure, almost every operation has two methods: a one-line method on the BinaryTree that starts the computation, and a recursive method that works on the Node objects. For the lookup() operation, there is a BinaryTree.lookup() method that the client uses to start a lookup operation.

Polymorphic Functions with Set-Theoretic Types

Part 2: Local Type Inference and Type Reconstruction

Giuseppe Castagna

1Kim Nguyễn2Zhiwu Xu1,3Pietro Abate1

1 CNRS, PPS, Univ Paris Diderot, Sorbonne Paris Cité, Paris, France

2LRI, Université Paris-Sud, Orsay, France3TEC, Guangdong Power Grid Co., Guangzhou, China

Abstract.This article is the second part of a two articles series about the definition of higher-order polymorphic functions in a type system with recursive types and set-theoretic type connectives (unions, intersections, and negations). In the first part, presented in a companion paper, we defined and studied the syntax, semantics, and evaluation of the explicitly- typed version of a calculus, in which type instantiation is driven by explicit instantiation annotations. In this second part we present a local type inference system that allows the programmer to omit explicit instantiation annotations for function applications, and a type reconstruction system that allows the programmer to omit explicit type annotations for function definitions. Categories and Subject DescriptorsD.3.3 [Programming Lan- guages]: Language Constructs and Features-Polymorphism KeywordsTypes, XML, intersection types, type constraints.

1. Introduction

OcamlDuce, XHaskell, XAct, are statically-typed functional lan- guages. However, none of them provides full-fledged parametric polymorphism even though this feature has been repeatedly re- quested in different standardization groups. A major stumbling block to such an extension -ie, the definition of a subtyping re- lation for regular tree types with type variables- was lifted by

Castagna and Xu [

4]. In Part 1 of this work, presented in the previ-

ous edition of POPL [

3], we described how to take full advantage of

Castagna and Xu"s system by defining a calculus with higher-order polymorphic functions and recursive types with union, intersec- tion, and negation connectives. The approach is general and goes well beyond the sole application to XML processing languages. As a matter of fact, the motivating example we gave in Part 1 [ 3] does not involve XML, but looks like a rather classic display of functional programming specimens: map :: (α->β)->[α]->[β]map f l = case l of| [] -> []| (x : xs) -> (f x : map f xs) even :: (Int->Bool)?((α\Int)->(α\Int))even x = case x of| Int -> (x 'mod' 2) == 0| _ -> x The first function is the classicmapfunction defined in Haskell (we use Greek letters to denote type variables). The second would Permission to make digital or hard copies of all or part of this work for personal or classroom use is granted without fee provided that copies are not made or distributed for profit or commercial advantage and that copies bear this notice and the full citation on the first page. Copyrights for components of this work owned by others than ACM must be honored. Abstracting with credit is permitted. To copy otherwise, or republish, to post on servers or to redistribute to lists, requires prior specific permission and/or a fee. Request permissions from Permissions@acm.org.

POPL"15, January 15-17 2015, Mumbai, India.

Copyright is held by the owner/author(s). Publication rights licensed to ACM.

ACM 978-1-4503-3300-9/15/01...$15.00.

http://dx.doi.org/10.1145/2676726.2676991be an Haskell function were it not for two oddities: its type dec-laration contains type connectives (type intersection "?" and type

difference "\"); and the pattern in thecaseexpression is a type, meaning that it matches all values returned by the matched ex- pression that have that type. So what does theevenfunction do? It checks whether its argument is an integer; if it is so it returns whether the integer is even or not, otherwise it returns its argument as it received it. Although the definition ofevenmay seem weird, it follows a very common pattern used to manipulate functional data- structures. Two examples are Okasaki"s functional implementation of red-black trees (for which our system provides a far better typ- ing) andthe transformation ofXML documentswhose elementsare modified or left unchanged according to their tag/type (see actual code in Section

3.3later on and in AppendixA). Furthermore it is a

perfect minimal example to illustrate all the aspects of our system.

In Part1 [

3] we showed that the system presented there is ex-

pressive enough to define the two functions above and to verify that they have the types declared in their signatures. Thatmaphas the declared type will come as no surprise (in practice, we actually want the system to infer this type even in the absence of a signature given by the programmer: see Section

7). Thatevenwas given an

intersection type means that it must have all the types that form the intersection. So it must be a function that when applied to an inte- ger it returns a Boolean and that when applied to an argument of a type that does not contain any integer, it returns a result of the same type. In other terms,evenis a polymorphic (dynamically bounded) overloaded function. However, the system in Part 1 [

3] is not able

toinfer(without the help of the programmer) the type of the partial application ofmaptoeven, which must be equivalent to map even :: ([Int] -> [Bool])? ([γ\Int]->[γ\Int])?(1) ([γ?Int]->[(γ\Int)?Bool]) sincemapevenreturns a function that when applied to a list of integers it returns a list of Booleans; when applied to a list that does not contain any integer, then it returns a list of the same type (actually, the same list); and when it is applied to a list that may contain some integers (eg, a list of reals), then it returns a list of the same type, without the integers but with some Booleans instead (in Typingmapevenis difficult because it demands to infer several different instantiations

1of the type ofmapand then take their

intersection. This is why the calculus in [

3] includes explicit type

substitutions: the programmer must explicitly provide the type- substitutions used to instantiate the types of the terms that form an application, a requirement that makes the system of [

3] not

usable in practice, yet. In this paper we remove this limitation by defining a sound and complete inference system that deduces the type-substitutions that a programmer should insert in a program of [

3] to make it well typed. In other words, we define "local

1Formapevenwe need to infer just two instantiations, namely,

(γ\Int)/α,(γ\Int)/β}and{(γ?Int)/α,(γ\Int)?Bool/β}. The type in (

1) is redundant since the first type of the intersection is an instance (eg,

forγ=Int) of the third. We included it just for the sake of the presentation. type inference"2for [3], namely, we solve the problem of checking whether there exist some type-substitutions that make the types of a function and of its arguments compatible and, if so, of inferring the type of the application as we did for (

1). In particular, we

show that local type inference for [

3] reduces to the problem of

finding twosetsof type substitutions{σi|i?I}and{σ?j|j?J} such that for two given typessandtthe relation? j?Jtσ?jholds, and we give a sound and complete algorithm for this problem. We also show how the same algorithm can be used to perform type reconstruction and infer types more precise than those inferred by the type systems of the ML family. All detailed proofs and complete definitions can be found in the Appendix. The system is fully implemented and, at the moment of writing, in alpha-test. It will be distributed in the next public release of the

CDuce language [

2]. In the meanwhile, the current version can be

tested by compiling the master branch of theCDuce git repository: git clone https://git.cduce.org/cduce(we recommend to check the bugtracker for current issues). Next section outlines the various problems to be faced in this research and succinctly describes the system of [

3]. The reader

acquainted with the work in [

3] can skip directly to Section2.1.

2. Overview

The aim of this research is the definition an XML processing func- tional language with high-order polymorphic functions, that is, in the specific, a polymorphic version of the languageCDuce [ 2]. CDuce is a strongly-typed programming language that eases the manipulation of data in XML format. Issued from academic re- search it is used in production, available on different platforms, and included in all major Linux distributions. The essence ofCDuce is aλ-calculus with pairs, explicitly-typed recursive functions, and a type-case expression. Its types can be recursively defined and in- clude basic, arrow, and product typeconstructorsand the intersec- tion, union, and negation typeconnectives.

In this work we omit

for brevity recursive functions and product types constructors and expressions (our results can be easily extended to them as sketched in Section

5and detailed in the appendixes) and add type variables.

So in the rest of this work we study a calculus whose types and expressions are described by the next two following definitions. Definition 2.1(Types).Types are the regular trees coinductively generated by the following productions: t::=b|t→t|t?t|t?t| ¬t|0|1|α(2) and such that every infinite branch contains infinitely many occur- rences of "→" constructor. We useTto denote the set of all types. over type variables, and0and1respectively denote the empty (that types no value) and top (that types all values) types. Coinduction accounts for recursive types and the condition on infinite branches bars out ill-formed types such ast=t?t(which does not carry any information about the set denoted by the type) ort=¬t (which cannot represent any set).

It also ensures that the binary

relation??T2defined byt1?t2?ti,t1?t2?ti,¬t?t is Noetherian. This gives an induction principle onTthat we will use without any further explicit reference to the relation.

We use

var(t)to denote theset of type variablesoccurring in a typet.

2There are different definitions forlocal type inference. Here we use it

with the meaning of finding the type of an expression in which not all type annotations are specified. This is the acceptation used in Scala where, like in C# and Java, type parameters for polymorphic/generic method calls can be omitted. In our specific problem, we will omit -and, thus, infer- the annotations that specify how the types of a function and of its argument can be made compatible. As explained in Section

6it is more general than

Pierce and Turner"s local type inference for arguments types [

20].A typetis said to begroundorclosedif and only ifvar(t)is

empty. The subtyping relation for these types is the one defined by Castagna and Xu [

4]. For this work it suffices to consider that

ground types are interpreted as sets ofvalues(ie, either constants orλ-abstractions) that have that type, and that subtyping is set containment (a ground typesis a subtype of a ground typetif and only iftcontains all the values of types). In particular,s→t contains allλ-abstractions that when applied to a value of type s, if the computation terminates, then they return a result of type t(eg,0→1is the set of all functions

3and1→0is the set of

functions that diverge on every argument). Type connectives (ie, union, intersection, negation) are interpreted as the corresponding set-theoretic operators (eg,s?tis the union of the values of the two types). For what concerns non-ground types (ie, types with variables occurring in them) all the reader needs to know for this work is that the subtyping relation of Castagna and Xu is not hold in general, while it holds forsemantictype-substitutions in convex models: see [quotesdbs_dbs19.pdfusesText_25
[PDF] polymorphism in java example

[PDF] polymorphism in java example javatpoint

[PDF] polymorphism java example stackoverflow

[PDF] polynesie 2016 maths es

[PDF] polynésie 2016 maths es corrigé

[PDF] polynésie juin 2016 maths corrigé es

[PDF] polynesie juin 2016 maths s

[PDF] polynôme caractéristique

[PDF] polynome et fraction rationnelle cours

[PDF] polynomial lens distortion model

[PDF] polynomial solution

[PDF] polynomials and conjugate roots

[PDF] polynomials class 9 worksheet pdf

[PDF] polyphemus pronunciation

[PDF] polypnée definition arabe