Data Types Gabriele Keller Ron Vanderfeesten Compound types What - - PowerPoint PPT Presentation

data types
SMART_READER_LITE
LIVE PREVIEW

Data Types Gabriele Keller Ron Vanderfeesten Compound types What - - PowerPoint PPT Presentation

Concepts of Program Design Data Types Gabriele Keller Ron Vanderfeesten Compound types What are types? So far, we only looked at basic types, such as Int, Boolean, as well as function types Int Bool 1 True 2 False 3 4


slide-1
SLIDE 1

Concepts of Program Design

Data Types

Gabriele Keller Ron Vanderfeesten

slide-2
SLIDE 2

Compound types

  • What are types?
  • So far, we only looked at basic types, such as Int, Boolean, as well as function

types

Int Bool Int -> Bool 1 2 3 4 … True False even

  • dd

slide-3
SLIDE 3

Compound types

  • But what if we want to define our own, new types
  • How does it work in different programming languages?
slide-4
SLIDE 4

Defining our own type ‘from scratch’

  • Example: defining a new type to model colours

Colour Green Red Blue

slide-5
SLIDE 5

Defining our own type ‘from scratch’

  • Example: defining a new type to model colours

enum Colour {Red, Green, Blue};

  • Enumeration types in C#
  • In C
  • In Haskell

typedef num Colour {Red, Green, Blue} colour_t; data Colour = Red | Green | Blue

slide-6
SLIDE 6

Product types

Int Bool Int × Bool True False (1, True) (2, False) … 1 2 3 4 …

  • Defining a new type by combining values of existing types:
slide-7
SLIDE 7

Tuples example: modelling a point in a 2D space

  • In C#

public struct Point { public static readonly Point Empty = new Point(); private float x; private float y; public Point(float x, float y) { this.x = x; this.y = y; } public float X { get { return x; } set { x = value; } }

slide-8
SLIDE 8

Tuples example: modelling a point in a 2D space

  • In Java

class Point { private float x; private float y; public Point (float x, float y) { this.x = x; this.y = y;} public float getX () {return this.x;} public float getY () {return this.y;} public float setX (float x) {this.x=x;} public float setY (float y) {this.y=y;} }; Point middlePoint (Point p1, Point p2) { Point mid ((p1.getX() + p2.getX())/2.0, (p1.getY() + p2.getY())/2.0); return mid; }

slide-9
SLIDE 9

Tuples example: modelling a point in a 2D space

  • In Java
  • using degenerate classes in Java:

class Point { public float x; public float y; }; Point middlePoint (Point p1, Point p2) { Point mid; mid.x = (p1.x + p2.x)/2.0; mid.y = (p1.y + p2.y)/2.0; return mid; }

slide-10
SLIDE 10
  • In C

Tuples example: modelling a point in a 2D space

struct point { float x; float y; }; struct point middlePoint ( struct point p1, struct point p2) { struct point mid; mid.x = (p1.x + p2.x)/2.0; mid.y = (p1.y + p2.y)/2.0; return mid; }

slide-11
SLIDE 11
  • In Haskell:
  • using simple pairs:
  • using records (data types with names fields):

Tuples example: modelling a point in a 2D space

type Point = (Float, Float) middlePoint:: Point -> Point -> Point middlePoint (x1, y1) (x2, y2) = ((x1+x2)/2, (y1+y2)/2) middlePoint’ p1 p2 = ((fst p1 + fst p2)/2, (snd p1 + snd p2)/2) data Point = Point {x:: Float, y:: Float} middlePoint (Point x1 y1) (Point x2 y2) = Point ((x1+x2)/2) ((y1+y2)/2) middlePoint’ p1 p2 = Point { x = (x p1 + x p2)/2 y = (x p2 + y p2)/2}

slide-12
SLIDE 12

Compound types

Int Bool Int ∪ Bool True False

  • Composite types that offer alternatives

True False 2 3 4 … 1 2 3 4 …

slide-13
SLIDE 13

Sum types

Int Bool Int ∪ Bool 1 2 3 4 … True False

  • Sum types are composite types that offer alternatives

True False 2 3 4 …

Bool Bool Int Int Int

slide-14
SLIDE 14

Sum types

  • In Haskell

data Value = I Integer | B Bool

slide-15
SLIDE 15

Sum-types

  • Alternatives with varying component types in C:

union { int i; float f; } unsafe; unsafe.f = 1.23456; printf “(“%d”, unsafe.i);

slide-16
SLIDE 16

Sum-types

  • Alternatives with varying component types in C:

typedef enum {I, F} valueTag; typedef struct { value_t tag; union { int intLit; float floatLit; } val; } value_t;

slide-17
SLIDE 17

Sum-types

  • Alternatives with varying component types in object oriented languages:

public abstract class Value { private.Value() {} public class I: Value { public int V; public I(int v) {V = v;} } public class B: Value { public bool V; public B(bool v) {V = v;} }

slide-18
SLIDE 18

Recursive types

  • In Haskell

data IntList = ICons Int IntList | Nil

class ListNode { int data; ListNode next; public ListNode(int d) { data = d; next = null; }}

typedef struct list_node { int elem; struct list_node * next; } int_list_t;

  • C
  • C#
slide-19
SLIDE 19

Extending MinHs with support for compound types

  • We add algebraic data types to MinHs
  • product types
  • sum types
  • as well as support for recursive data types
  • no support for letting the user give new names to these types
  • could be easily added
slide-20
SLIDE 20

Products in MinHs

  • Products aka pairs in MinHs
  • minimal extension
  • no type declaration
  • no named fields
  • only pairs (a1, a2) and
  • nullary tuples/unit ()
  • New MinHs types:
  • Unit: singleton type with element ()
  • τ1 * τ2 : binary product type with element type τ1 and τ2
  • Operations on these types:
  • fst and snd to extract the first/second component
slide-21
SLIDE 21

Products in MinHs: Concrete and Abstract Syntax

  • Constructors
  • Destructors
  • Types

(e1, e2) Pair e1 e2 () UnitEl fst e Fst e snd e Snd e

τ1 * τ2

Cross e1 e2 Unit Unit

we’ll mostly be using concrete syntax for types

slide-22
SLIDE 22

Products in MinHs

  • Example:

recfun div :: ((Int, Int) -> Int) args = if (fst args < snd args) then 0 else div (fst args - snd args, snd args)

slide-23
SLIDE 23

Products in MinHs: Static Semantics

  • Typing rules:

Γ ⊢ e1 : τ1 Γ ⊢ e2 : τ2 Γ ⊢ Pair e1 e2 : Cross τ1 τ2 Γ ⊢ Fst e : τ1 Γ ⊢ e: Cross τ1 τ2 Γ ⊢ Snd e : τ2 Γ ⊢ e: Cross τ1 τ2 Γ ⊢ UnitEl : Unit

slide-24
SLIDE 24

Products in MinHs: Dynamic Semantics

  • Evaluation rules (M-machine)

P a i r v1 e2 ↦M P a i r v1 e2 ’ e2 ↦M e2’ F s t e ↦M F s t e ’ e ↦M e’ S n d e ↦M S n d e ’ e ↦M e’ P a i r e1 e2 ↦M P a i r e1 ’ e2 e1 ↦M e1’ F s t ( P a i r v1 v2 ) ↦M v1 S n d ( P a i r v1 v2 ) ↦M v2

slide-25
SLIDE 25

Sum-types

  • Sum-types in MinHs
  • we use binary sums:
  • τ1+τ2 : either τ1 or τ2 (products: both τ1 and τ2)
  • n-ary sums can be expressed by nesting
  • similarities to the Haskell type Either:

data Either a b = Left a | Right b

slide-26
SLIDE 26

Sum-types

  • Types
  • Constructors
  • Destructors

Inl e Inl τ1 τ2 e case e of Inl x -> e1 Inr y -> e2 Case τ1 τ2 e (x.e1) (y.e2)

τ1 + τ2

Sum τ1 τ2 Inr τ1 τ2 e Inr e

slide-27
SLIDE 27

Isomorphic Types

  • Type correspondence: which MinHs type corresponds to the following Haskell

type?

  • We cannot define the same type, but we can define an isomorphic type in MinHs
  • a type τ1 is isomorphic to a type τ2 iff there exists a bijection between τ1 and

τ2

  • Colour is isomorphic to
  • Unit + (Unit + Unit) and also to
  • (Unit + Unit) + Unit

data Colour = Red | Green | Blue

since all three types have exactly three elements

slide-28
SLIDE 28

Isomorphic Types

  • In actual programming languages, we want to have named user defined types

which are distinguished by the type checker:

data Direction = North | South | East | West data Colour = Red | Black | Blue | Yellow data Vector = Vector Float Float data Point = Point Float Float

slide-29
SLIDE 29

Sums in MinHs: Static Semantics

  • Typing rules:

Γ ⊢ e1 : τ1 Γ ⊢ Inl τ1 τ2 e1 : Γ ⊢ Case τ1 τ2 e (x.e1) (y.e2): Γ ⊢ e2 : τ2 Γ ⊢ Inr τ1 τ2 e2 : Γ ⊢ e: Sum τ1 τ2 Γ∪ {x : τ1} ⊢ e1 :τ Γ∪ {y : τ2} ⊢ e2 :τ τ Sum τ1 τ2 Sum τ1 τ2

slide-30
SLIDE 30

Sums in MinHs: Dynamic Semantics

  • Evaluation rules (M-machine), omitting the types for brevity

Case e (x.e1) (y.e2) ↦M Case e’ (x.e1) (y.e2) e ↦M e’ I n l e ↦M I n l e ’ e ↦M e’ I n r e ↦M I n r e ’ e ↦M e’ Case(Inl v) (x.e1)(y.e2) ↦M e1 [x:=v] Case(Inr v)(x.e1)(y.e2) ↦M e2 [y:=v]

slide-31
SLIDE 31

Recursive Types

  • What about the list type?
  • Can’t be expressed in MinHs yet - we need explicit recursive types!

data IntList = Nil | ICons Int IntList Unit + (Int, ) Rec t. Unit + (Int, t )

we need a way to recursively refer to a type!

slide-32
SLIDE 32

Recursive types

  • Types
  • Constructor
  • Destructor

Roll e

Rec t = τ

Rec (t . τ) Unroll e Roll e Unroll e

slide-33
SLIDE 33

Examples

  • List of integer values:
  • Type
  • Terms

Rec List = Unit + (Int * List)

Roll (Inl ()) []

Roll(Inr (1, Roll (Inl ()))) [1] Roll (Inr (1, Roll(Inr (2, Roll (Inl ())()) [1,2]

slide-34
SLIDE 34

Recursive Types in MinHs: Static Semantics

  • Typing rules:

Γ ⊢ e : τ [t := Rec(t. τ)] Γ ⊢ Roll e : Γ ⊢ e : Rec(t. τ) Γ ⊢ Unroll e : Rec(t. τ) τ [t:= Rec(t. τ)]

slide-35
SLIDE 35

Sums in MinHs: Dynamic Semantics

  • Evaluation rules (M-machine)

Unroll (Roll e) ↦M e R

  • l

l e ↦M R

  • l

l e ’ e ↦M e’ U n r

  • l

l e ↦M U n r

  • l

l e ’ e ↦M e’