CMSC 631: Program Analysis and Understanding - Course Overview and O'Caml Introduction - P, Study notes of Computer Science

An overview of the cmsc 631 graduate course on program analysis and understanding. The course is designed to prepare students for research in compilers and code generation, exploring new programming languages, and using software tools to detect errors or aid in program understanding. Students will be introduced to o'caml, a modern functional programming language, and will be responsible for learning it on their own. The course includes lectures, quizzes, and programming projects. The administrative details of the course, an introduction to o'caml, and some of its basic features such as operators, functions, definitions, lists, and matching.

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-k6g
koofers-user-k6g 🇺🇸

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMSC 631 9/3/02
CMSC 631
Program Analysis and Understanding
Bill Pugh
9/3/02 CMSC 631 2
Administrivia
Designed as a core graduate course
prepares you for more specific, project oriented
graduate courses in programming languages
new course, but expect to offer it once a year
grades based on exams and homeworks
some homeworks will include programming projects
9/3/02 CMSC 631 3
Course material
Designed to prepare you for research in
compilers and code generation
exploring new programming languages
software tools to detect errors or aid in program
understanding
9/3/02 CMSC 631 4
Plchat Seminar
http://www.cs.umd.edu/projects/PLchat/
This semester, Mondays, 11am, CSIC 3120
Mailing list
including other PL-related announcements
First talk next Monday:
Cyclone: A next-generation systems
programming language, by Mike Hicks, Univ.
of Maryland
9/3/02 CMSC 631 5
Quiz: show of hands
How many have programmed in:
some version of ML (SML or O’Caml)?
Haskell or another lazy functional language?
Smalltalk or Squeak?
OO-language other than C++, Java or C#?
Used Generic Polymorphic types in Java?
9/3/02 CMSC 631 6
Quiz: show of hands
Have been exposed to:
contravariant types
data flow analysis (e.g., def-use chains)
abstract syntax trees
register allocation
design patterns
pf3
pf4
pf5

Partial preview of the text

Download CMSC 631: Program Analysis and Understanding - Course Overview and O'Caml Introduction - P and more Study notes Computer Science in PDF only on Docsity!

CMSC 631

Program Analysis and Understanding Bill Pugh 9/3/02 CMSC 631 2

Administrivia

  • Designed as a core graduate course
    • prepares you for more specific, project oriented graduate courses in programming languages
    • new course, but expect to offer it once a year
    • grades based on exams and homeworks
      • some homeworks will include programming projects 9/3/02 CMSC 631 3

Course material

  • Designed to prepare you for research in
    • compilers and code generation
    • exploring new programming languages
    • software tools to detect errors or aid in program understanding 9/3/02 CMSC 631 4

Plchat Seminar

  • http://www.cs.umd.edu/projects/PLchat/
  • This semester, Mondays, 11am, CSIC 3120
  • Mailing list
    • including other PL-related announcements
  • First talk next Monday:
    • Cyclone: A next-generation systems programming language, by Mike Hicks, Univ. of Maryland

Quiz: show of hands

  • How many have programmed in:
    • some version of ML (SML or O’Caml)?
    • Haskell or another lazy functional language?
    • Smalltalk or Squeak?
    • OO-language other than C++, Java or C#?
    • Used Generic Polymorphic types in Java?

Quiz: show of hands

  • Have been exposed to:
    • contravariant types
    • data flow analysis (e.g., def-use chains)
    • abstract syntax trees
    • register allocation
    • design patterns

9/3/02 CMSC 631 7

Course material

  • Tentative topics on web page
  • Initial topics:
    • Types
    • OCaml 9/3/02 CMSC 631 8

O’Caml

  • Objective Caml is a fast modern type- inferring functional programming language descended from the ML (Meta Language) family. The O'Caml compiler was developed at INRIA's projet Cristal. 9/3/02 CMSC 631 9

Learning O’Caml

  • After some intro stuff today, you will be largely responsible for learning O’Caml on your own
  • Installed on /fs/imports on CS Suns
  • May get cluster account for class if needed
    • not a good environment 9/3/02 CMSC 631 10

ocaml

  • Lots of O’Caml tools (e.g. compiler)
  • ocaml is the interactive interpreter
  • Enter expressions, terminated with a ;;
    • I don’t know why ;; is used as a terminator

Operators and Functions

  • Integer math
    • +, -, *, /, mod
  • Real math
    • +., -., *., /., **
  • Logical
    • <, <=, >, >=, !=, =
  • Bitwise
    • land, lor, lxor, lsl, lsr, asl
  • if then else
    • Conversion functions
      • float, int_of_float
      • int_of_char, char_of_int
      • string_of_int, int_of_string

Definitions and Functions

  • let pi = 4.0 *. atan 1.0;;
  • let x = 5 in x*x;;
  • let plus x y = x+y;;
  • let incr x = plus 1 x;;
  • Recursive definition:
    • let rec fib n = if n < 2 then 1 else fib(n-1) + fib(n-2);;

9/3/02 CMSC 631 19

Variant types

  • type ‘a btree = Empty | Node of ‘a * ‘a tree * ‘a tree;;
  • matching variants:
  • let rec member x btree = match btree with Empty -> false | Node (y, left, right) -> if x = y then true else if x < y then member x left else member x right ;; 9/3/02 CMSC 631 20

Matching

  • If multiple cases match, only the first applies
  • Pattern match variables must be unique
    • can’t match [ x; x ]
  • Can use _ for don’t care patterns 9/3/02 CMSC 631 21

Alternative member

  • let member x btree = let rec find = function Empty -> false | Node (y, left, right) -> if x = y then true else if x < y then find left else find right in find btree ;;
  • using function rather than match
  • uses closures: find references variable x from outer scope
    • this use doesn’t require creating closures
    • others do, e.g., let add x = fun y -> x+y;; 9/3/02 CMSC 631 22

insert into binary trees

  • let rec insert x = function Empty -> Node(x, Empty, Empty) | Node(y, left, right) -> if (x < y) then Node(y, insert x left, right) else Node(y, left, insert x right);;

incomplete matches

  • let is_uppercase = function 'A' .. 'Z' -> true | 'a' .. 'z' -> false;;
  • Complains at compile time
    • throws an exception at runtime

Strings

  • Use double quoted string constants
  • Use ^ for concatenation
  • use .[i] for the i’th character (same indexing syntax as arrays).

9/3/02 CMSC 631 25

Printing, access to command line

let args = Sys.argv in if Array.length args = 2 then let n = int_of_string Sys.argv.(1) in print_int (n * n); print_newline () else print_string "Usage: square \n";; 9/3/02 CMSC 631 26

#use

  • Like an include file
  • #use “defs.ml”;;
  • only at top level
  • Other mechanisms for building large systems - won’t be covered today 9/3/02 CMSC 631 27

compiling, debugging

  • ocamlc -o test test.ml
    • compiles test.ml to an executable
  • -g flag adds debugging information
  • ocamldebug provides debugging