Programming Languages

Slides:



Advertisements
Similar presentations
Object-Oriented Programming
Advertisements

Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
Chapter 3 DATA: TYPES, CLASSES, AND OBJECTS. Chapter 3 Data Abstraction Abstract data types allow you to work with data without concern for how the data.
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
Defining new types of data. Defining New Datatypes Ability to add new datatypes in a programming language is important. Kinds of datatypes – enumerated.
CPS 506 Comparative Programming Languages Abstract Data Type and Encapsulation.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Ch4: Software Architecture and Design. 1 Modules: Interface vs. Implementation (contd..)  Interface design considerations:
Basic Definitions Data Structures: Data Structures: A data structure is a systematic way of organizing and accessing data. Or, It’s the logical relationship.
Abstract data types & object-oriented paradigm. Abstraction Abstraction: a view of an entity that includes only the attributes of significance in a particular.
The Haskell Refactorer, HaRe, and its API Huiqing Li Claus Reinke Simon Thompson Computing Lab, University of Kent
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
Lecture 9 Concepts of Programming Languages
CS Winter 2011 Abstract Data Types. Container Classes Over the years, programmers have identified a small number of different ways of organizing.
Abstract Data Types and Encapsulation Concepts
Chapter 11 Abstract Data Types and Encapsulation Concepts.
Object Oriented Data Structures
CS 331 Modules Chapter 6. Overview Decomposition and abstraction Program organization Abstract vs. concrete User-defined types Example discussion.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
PrasadCS7761 Haskell Data Types/ADT/Modules Type/Class Hierarchy Lazy Functional Language.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Existential Types and Module Systems Xiaoheng Ji Department of Computing and Software March 19, 2004.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
Chapter 11 Abstract Data Types and Encapsulation Concepts.
P Chapter 2 introduces Object Oriented Programming. p OOP is a relatively new approach to programming which supports the creation of new data types and.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Types and Classes Dr. Hyunyoung Lee.
1 Functional Programming Lecture 6 - Algebraic Data Types.
Overview of the Haskell 98 Programming Language
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Python Functions.
SOFTWARE DESIGN. INTRODUCTION There are 3 distinct types of activities in design 1.External design 2.Architectural design 3.Detailed design Architectural.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Stacks The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
Data Abstaraction Chapter 10.
Abstraction ADTs, Information Hiding and Encapsulation.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Higher-order Functions Dr. Hyunyoung Lee.
Chapter 10, Slide 1 ABSTRACT DATA TYPES Based on the fundamental concept of ABSTRACTION:  process abstraction  data abstraction Both provide:  information.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Interactive Programs: I/O and Monads Dr. Hyunyoung Lee.
Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.
CSE 3302 Programming Languages Chengkai Li, Weimin He Spring 2008 Abstract Data Types and Modules Lecture 11 – ADT and Modules, Spring CSE3302 Programming.
Do-it-yourself dictionaries in Haskell1 Ingmar Brouns & Lukas Spee.
1 Chapter 11 © 1998 by Addison Wesley Longman, Inc The Concept of Abstraction - The concept of abstraction is fundamental in programming - Nearly.
1 CS Programming Languages Class 22 November 14, 2000.
(1) ICS 313: Programming Language Theory Chapter 11: Abstract Data Types (Data Abstraction)
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 10 Abstraction - The concept of abstraction is fundamental in programming - Nearly all programming.
Abstract Data Type EnhanceEdu.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Defining Classes Modules and ADTs CSCE 314 Spring 2016.
Modern Database Techniques Part 1: Object Oriented Databases 2. Concepts of Object Oriented Programming Revisited.
Stacks The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
DDC 2423 DATA STRUCTURE Main text:
Abstract Data Types and Encapsulation Concepts
ABSTRACT DATA TYPES Based on the fundamental concept of ABSTRACTION:
11.1 The Concept of Abstraction
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
Stack ADT & Modularity 2 implementations of the Stack abstract data type: Array Linked List Program design: modularity, abstraction and information hiding.
Abstract Data Types and Encapsulation Concepts
Abstract Data Types and Encapsulation Concepts
Types and Classes in Haskell
Computer Science 312 Making choices Tail Recursion
CSCE 314: Programming Languages Dr. Dylan Shell
11.1 The Concept of Abstraction
Lecture 9 Concepts of Programming Languages
Chapter 11 Abstraction - The concept of abstraction is fundamental in
Presentation transcript:

Programming Languages CSCE 314 Programming Languages Haskell: The Module System Dr. Hyunyoung Lee

Modules A Haskell program consists of a collection of modules. The purposes of using a module are: To control namespaces. To create abstract data types. A module contains various declarations: First, import declarations, and then, data and type declarations, class and instance declarations, type signatures, function definitions, and so on (in any order) Module names must begin with an uppercase letter One module per file

Example of a Module export list module Tree ( Tree(Leaf,Branch), fringe ) where
 data Tree a = Leaf a | Branch (Tree a) (Tree a) 

fringe :: Tree a -> [a]
fringe (Leaf x)            = [x]
fringe (Branch left right) = fringe left ++ fringe right A module declaration begins with the keyword module The module name may be the same as that of the type Same indentation rules as with other declarations apply The type name and its constructors need be grouped together, as in Tree(Leaf,Branch); short-hand possible, Tree(..) Now, the Tree module may be imported: import list: omitting it will cause all entities exported from Tree to be imported module Main (main) where import Tree ( Tree(Leaf,Branch), fringe ) main = print (fringe (Branch (Leaf 1) (Leaf 2)))

imported names are prefixed by the name of the module imported Qualified Names module Fringe(fringe) where
import Tree(Tree(..))

fringe :: Tree a -> [a] -- A different definition of fringe
fringe (Leaf x) = [x]
fringe (Branch x y) = fringe x module Main (main) where import Tree ( Tree(Leaf,Branch), fringe ) import qualified Fringe ( fringe ) main = do print (fringe (Branch (Leaf 1) (Leaf 2))) print (Fringe.fringe (Branch (Leaf 1) (Leaf 2))) imported names are prefixed by the name of the module imported Qualifiers are used to resolve conflicts between different entities with the same name

More Features Entities can be hidden in the import declaration. For example, the following explicit import of the Prelude: import Prelude hiding (length, sum) will not import length and sum from the Standard Prelude. Entities can be renamed with as. Used to shorten long names: import AnExtremelyLongModuleName as A myFun n = A.foo n or to easily adapt to a change in module name without changing all qualifiers (the following is possible if there are no name conflicts): import Module1 as M import Module2 as M

Abstract Data Types Modules are Haskell’s mechanism to build abstract data types (ADTs). For example, a suitable ADT for the Tree type might include the following operations: A module supporting this is: data Tree a  -- just the type name 
leaf         :: a -> Tree a
branch       :: Tree a -> Tree a -> Tree a
cell         :: Tree a -> a
left, right  :: Tree a -> Tree a
isLeaf       :: Tree a -> Bool module TreeADT (Tree, leaf, branch, cell, left, right, isLeaf) where data Tree a   = Leaf a | Branch (Tree a) (Tree a) 
leaf          = Leaf
branch        = Branch
cell  (Leaf a) = a
left (Branch l r) = l right (Branch l r) = r isLeaf (Leaf _) = True isLeaf _ = False Leaf and Branch are not exported – information hiding (at a later time the representation type could be changed without affecting users of the type)

Another Example ADT - Stack module Stack ( StkType, push, pop, top, empty ) where data StkType a  = EmptyStk | Stk a (StkType a) push x s = Stk x s pop (Stk _ s) = s top (Stk x _) = x empty = EmptyStk module Stack ( StkType, push, pop, top, empty ) where newtype StkType a  = Stk [a] push x (Stk xs) = Stk (x:xs) pop (Stk (_:xs)) = Stk xs top (Stk (x:_)) = x empty = Stk [] module Main where import Stack myFun = push 3 . push 4 . pop . empty