Disjoint Unions (quick review)

Slides:



Advertisements
Similar presentations
Types and Programming Languages Lecture 8 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Advertisements

ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
Programming Languages and Paradigms
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Names and Bindings.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Subprograms A subprogram allows process abstraction (as opposed to data abstraction). Characteristics –single entry point –caller suspended until control.
Prof. Johnnie Baker Module Basic Structures: Sets
Chapter 7:: Data Types Programming Language Pragmatics
Subprograms The basic abstraction mechanism. Functions correspond to the mathematical notion of computation input output procedures affect the environment,
Type Checking.
Compiler Construction
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
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 Names, Scopes and Bindings. 2 Names Kinds of names Kinds of names Variables, functions, classes, types, labels, blocks, operators, tasks, etc. Variables,
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes Names Variables The Concept of Binding Type Checking Strong Typing Type Compatibility.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Composite types Cartesian products –tuples, records, structures disjoint unions –union, discriminated or variant records mappings –arrays, functions recursive.
Types A type consists of –a set of values –a set of operations on those values Types can be –primitive (atomic, non-decomposable) –composite (includes.
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Type Checking  Legality checks  Operator determination  Overload resolution.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Names Variables Type Checking Strong Typing Type Compatibility 1.
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
CSE 425: Data Types II Survey of Common Types I Records –E.g., structs in C++ –If elements are named, a record is projected into its fields (e.g., via.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
CSC 580 – Theory of Programming Languages, Spring, 2009 Week 9: Functional Languages ML and Haskell, Dr. Dale E. Parson.
Basic Semantics Associating meaning with language entities.
1 COMP313A Programming Languages Data Types (2). 2 Overview Type Constructors Type Equivalence Type Checking Type Conversion.
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 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
Programming Languages and Paradigms Imperative Programming.
Sets Define sets in 2 ways  Enumeration  Set comprehension (predicate on membership), e.g., {n | n  N   k  k  N  n = 10  k  0  n  50} the set.
Language: Set of Strings
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Copyright © 2006 Addison-Wesley. All rights reserved. Ambiguity in Grammars A grammar is ambiguous if and only if it generates a sentential form that has.
Copyright © Cengage Learning. All rights reserved.
Chapter SETS DEFINITION OF SET METHODS FOR SPECIFYING SET SUBSETS VENN DIAGRAM SET IDENTITIES SET OPERATIONS.
DISCRETE COMPUTATIONAL STRUCTURES CSE 2353 Fall 2010 Most slides modified from Discrete Mathematical Structures: Theory and Applications by D.S. Malik.
Homogeneous tuples What are they? –S 2 = S x S –S n = S x S x … x S Cardinalities –#(S 2 )= (#S) 2 –#(S n )= (#S) n –#(S 0 )= (#S) 0 =1 What is S 0 ? –It.
+ Structures and Unions. + Introduction We have seen that arrays can be used to represent a group of data items that belong to the same type, such as.
Structure of Programming Languages Names, Bindings, Type Checking, and Scopes.
Sets Definition: A set is an unordered collection of objects, called elements or members of the set. A set is said to contain its elements. We write a.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 10 Abstraction - The concept of abstraction is fundamental in programming - Nearly all programming.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
1 Melikyan/DM/Fall09 Discrete Mathematics Ch. 7 Functions Instructor: Hayk Melikyan Today we will review sections 7.3, 7.4 and 7.5.
PRIME NUMBERS AND FACTOR TREES. DEFINITION Prime Number – An integer whose only factors are 1 and itself 2, 3, 5, 7,11, 13, 17, 19.
Data Types Chapter 6: Data Types Lectures # 12. Topics Chapter 6: Data Types 2 Introduction Primitive Data Types Character String Types Array Types Associative.
1 Structures & Unions. 2 User-Defined Types C provides facilities to define one’s own types. These may be a composite of basic types ( int, double, etc)
Introduction to Set Theory (§1.6) A set is a new type of structure, representing an unordered collection (group, plurality) of zero or more distinct (different)
Data Types Chapter 6: Data Types Lectures # 13. Topics Chapter 6: Data Types 2 Introduction Primitive Data Types Character String Types Array Types Associative.
Logical Database Design and the Rational Model
Data Types In Text: Chapter 6.
Type Checking and Type Inference
ML: a quasi-functional language with strong typing
Chapter 10-1: Structure.
Records Design Issues: 1. What is the form of references?
Cardinality of Sets Section 2.5.
ML’s Type Inference and Polymorphism
The aim of education is to teach students how to think rather than what to think. Sets The set is the fundamental discrete structure on which all other.
ML’s Type Inference and Polymorphism
Compiler Construction
Course Overview PART I: overview material PART II: inside a compiler
ML’s Type Inference and Polymorphism
ML’s Type Inference and Polymorphism
Compiler Construction
Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019
Chapter 11 Abstraction - The concept of abstraction is fundamental in
Presentation transcript:

Disjoint Unions (quick review) Pascal: variant record C/C++: union ML: union type Ada: discriminated record

What is a disjoint union? “Another kind of composite value is the disjoint union, whereby a value is chosen from one of several (usually different) sets.” David Watt Programming Language Design Concepts page 27

S+T Values are tagged to indicate which set they are from: S+T = { left(x) | x in S }  { right(y) | y in T } Cardinality? #(S+T) = #S + #T

ML datatype number = Exact of int | Inexact of real; datatype person = King | Peer of string*string*int | Knight of string | Peasant of string; Values: King, Peer(“Earl”, “Carlisle”, 7), Knight(“Gawain”), Peasant(“Jack Cade”) Paulson, ML for the Working Programmer pages 124-125

OO disjoint unions (new material) The main purpose of a disjoint union is to bring together values from different sets in a way that you can determine from which set a given value is drawn. This type of structure is used very commonly in OO programming. Polymorphism feeds off of this idea: implicit selection based on type.

OO disjoint union example

Recursive types A type defined in terms of itself Examples: lists, strings, trees, … If R is a recursive type, #(R) is infinite. Members of a recursive type are often finite, tough infinite members are possible (the list of all primes). How? They are implicitly represented (think of function vs. array representation of mappings): e.g. lazy lists.

Recursive type membership Consider the list type: datatype ’a List = Nil | Cons of ’a * ’a List; What are the members of this type? List(0) = Nil List(1) = List(0) U {Cons(x,y)| y in List(0)} … List(k) = List(k-1) U {Cons(x,y)| y in List(k-1)} List = Un=0…∞ List(n)

C++ & pointer types Languages like C++ do not directly support recursive types, but rather require the use of an indirection mechanism, the pointer. This has to do with the semantics of assignment. If v is a recursive structure, what are reasonable semantics for w=v? copy semantics – expensive reference semantics – sharing If language does not permit selective updating of a structure (as in a pure functional language) the semantics are indistinguishable).

Type equivalence Type equivalence addresses the question of when two types are considered equivalent. Structural equivalence two types are equivalent if and only if they have the same set of values Name equivalence two types are equivalent if and only if they are defined in the same place

Structural equivalence Types S and T are equivalent iff S and T are both primitive and S and T are identical S=AxB and T=CxD are both cartesian products and A and C are equivalent and B and D are equivalent S=AB and T=CD are both mappings and A and C are equivalent and B and D are equivalent S=A+B and T=C+D are both disjoint unions and A and C are equivalent and B and D are equivalent Otherwise, S and T are not equivalent.

Name equivalence Strictly, types are unique – each new type definition occurs in a different place, and so results in a new distinct type. Pascal technically uses name equivalence, but in practice the rules are relaxed: For example, suppose we wanted to define a file type to permit two programs to communicate via a file. Since the file type is defined in two different places (in two different programs) their files types are considered different (and hence incompatible).

Name vs. Structural equivalence Name equivalence forces each distinct type to be defined in one and only one place. This is sometimes inconvenient, but it helps to make the program more maintainable. (If the same type is defined in several places, and subsequently it has to be changed, the change must be made consistently in several places.) Structural equivalence allows confusion between types that are only coincidentally similar. Watt, page 42

Type Completeness Principle “No operation should be arbitrarily restricted in the types of its operands.” Watt, page 43 Types whose values are unrestricted in their usage are termed “first-class”, whereas those that are restricted are termed “second-class”. In Pascal procedures can be passed as arguments to other procedures, but cannot, for example, be part of a composite value. Pascal procedures are therefore second class. A language like ML imposes no such arbitrary type distinctions. All types are first-class.

Erlang About Erlang Erlang at the movies!