Programming Language Concepts CIS 280 - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Programming Language Concepts CIS 280

Description:

Overloaded functions have one name, but different definition ... Data constructor Set only visible inside the asbtype declaration; type inset visible outside ... – PowerPoint PPT presentation

Number of Views:12
Avg rating:3.0/5.0
Slides: 21
Provided by: cisN9
Category:

less

Transcript and Presenter's Notes

Title: Programming Language Concepts CIS 280


1
Programming Language Concepts (CIS 280)
  • Elsa L Gunter
  • NJIT
  • Fall 2001

2
Polymorphism vs Overloading
  • Overloaded functions have one name, but different
    definition for each allowed signature (sometimes
    called ad-hoc polymorphism)
  • With structural polymorphism (as in SML) one
    definition per function only one code segment
    created
  • Code can be applied uniformly to any data of type
    that matches input types

3
Type Definitions
  • Associates new name with an existing type or type
    construction
  • Main concern Type equality
  • When are to types equal?

4
Type Equality
  • Type checking asks are the types of input values
    the same as (ie equal to) the specified input
    types in operator signature
  • Need to know when two types are equal
  • For integers, string, reals, arrays answer is
    structural do the types have the same name?

5
Type Equality - Records
  • When are two record types equal?
  • Language dependent
  • Must the labels be the same, or just the
    component types?
  • Must the labels be in the same order?

6
Type Equality and Type Definitions
  • Problem
  • type age int
  • type id int
  • Are these the same type?
  • Type abbreviations vs. generative type
    definitions
  • Abbreviation same type as definition
  • Generative creates new type

7
Data Equality
  • When are to objects of the same type equal?
  • For integers, reals, strings, arrays answer is
    structural do they have the same bits
  • For user defined data, structural equality may
    make sense, but often is not what is wanted
    will return to this with abstract data types

8
Information Hiding
  • Consider the C code
  • typedef struct RationalType
  • int numerator
  • int denominator
  • Rational
  • Rational mk_rat (int n,int d)
  • Rational add_rat (Rational x, Rational y)
  • Can use mk_rat, add_rat without knowing the
    details of RationalType

9
Need for Abstract Types
  • Problem with previous example abstract not
    enforced
  • User can create Rationals without using mk_rat
  • User can access and alter numerator and
    denominator directly without using provided
    functions

10
Abstract Types - Example
  • Suppose we need sets of integers
  • Decision implement as lists of int
  • Problem lists have order and repetition, sets
    dont
  • Solution use only lists of int ordered from
    smallest to largest with no repetition (data
    invariant)

11
Abstract Type Example
  • SML code
  • type intset int list
  • val empty_set intset
  • fun insert elt, set elt
  • insert elt, set x xs
  • if elt lt x then elt x xs
  • else if elt x then x xs
  • else x (insert elt elt, set xs)

12
Abstract Type - Example
  • fun union (,ys) ys
  • union (xxs,ys)
  • union(xs,inserteltx,set ys)
  • fun intersect (,ys)
  • intersect (xs,)
  • intersect (xxs,yys)
  • if x lty then intersect(xs, yys)
  • else if y lt x then intersect(xxs,ys)
  • else x (intersect(xs,ys))

13
Abstract Type Example
  • fun elt_of elt, set ) false
  • elt_of elt, set xxs
  • (elt x) orelse
  • (elt gt x andalso
  • elt_ofelt elt, set xs)
  • Notice that all these definitions maintain the
    data invariant for the representation of sets,
    and depend on it.
  • As is, user can create any pair of lists of int
    and apply union to them. The result is
    meaningless.

14
Abstract Type Example
  • Solution abstract datatypes
  • abstype intset Set of int list with
  • val empty_set Set
  • local
  • fun ins elt, set elt
  • ins elt, set x xs
  • if elt lt x then elt x xs
  • else if elt x then x xs
  • else x (ins elt elt, set xs)

15
Abstract Type - Example
  • fun un (,ys) ys
  • un (xxs,ys)
  • un (xs,inseltx,set ys)
  • in
  • fun insert elt, set Set s
  • Set(inselt elt, set s)
  • fun union (Set xs, Set ys) Set(un (xs, ys))
  • end

16
Abstract Type - Example
  • local
  • fun inter (,ys)
  • inter (xs,)
  • inter (xxs,yys)
  • if x lty then inter(xs, yys)
  • else if y lt x then inter(xxs,ys)
  • else x (inter(xs,ys))
  • in
  • fun intersect(Set xs, Set ys) Set(inter(xs,ys))
  • end

17
Abstract Type Example
  • fun elt_of elt, set Set )\ false
  • elt_of elt, set Set (xxs)
  • (elt x) orelse
  • (elt gt x andalso
  • elt_ofelt elt, set Set xs)
  • fun set_to_list (Set xs) xs
  • end ( abstype )

18
Abstract Type Example
  • Functional implementation of integer sets
    insert creates new intset.
  • Creates a new type (not equal to int list)
  • Exports type intset, empty_set, insert, union,
    elt_of, and set_to_list act as primitive
  • Cannot use pattern matching or list functions
    wont type check

19
Abstract Type Example
  • Implementation just use int list, except for
    type checking
  • Data constructor Set only visible inside the
    asbtype declaration type inset visible outside
  • Function set_to_list and data constructor Set
    used only at compile time, in this case since
    there is only one case
  • Data abstraction allows us to prove data
    invariant hold for all objects of type intset

20
Modules
  • Language construct for grouping related types,
    data structures, and operations
  • Provides scope for variable and subprogram names
  • Typically includes interface stating which
    modules it depends upon and what types and
    operations it exports
  • Typically allows at least some encapsulation
  • Compilation unit for separate compilation
Write a Comment
User Comments (0)
About PowerShow.com