Lecture01 DK
Lecture01 DK
Fall 2024
Dilsun Kaynar
LECTURE 1
http://www.cs.cmu.edu/~15150/
We are on Canvas!
Today
• Computation is functional.
• Programming is an explanatory
linguistic process.
Functional
programming
SML
Computation is
functional
• values classi ed with respect to types
• expressions
• functions map values to values
fi
Imperative vs. Functional
command expression
executed evaluated
x := 5 3 + 5
}
• Invariants
High expectation to explain
• Speci cations precisely and concisely
• Proofs of correctness
• Analyze, decompose and t, prove
fi
fi
Parallelism
< 0, 0, 0, 1, 1 > 2
\/
12
sum: int sequence → int
Span
• Parallel computation
• How long would it take if one could have as many
processors as one wants; length of longest critical
path
Introducing ML
• Types t
• Expressions e
• Values v (subset of expressions)
Examples
(3 + 4) * 2
1
==> 7 * 2
1
==> 14
(3 + 4) * (2 + 1)
3
==> 21
(3 + 4) * 2 : int
(3 + 4) * 2 ↪ 14
Types in ML
• Basic types
• int, real, bool, char, string
• Constructed types
• Product types
• Function types
• User-de ned types
fi
Integers, Expressions
• Type int
• Values …, ~1, 0, 1, …,
• Expressions e1 + e2, e1 - e2, e1 * e2,
e1 div e2, e1 mod e2, …
• Example ~4 * 3
Integers, Typing
• Typing rules
• n : int
• e1 + e2 : int if e1 : int and e2 : int
• similar for other operations
(3 + 4) * 2 : int because
(3 + 4): int 2: int
(3 + 4): int because 3: int and 4: int
Integers, Evaluation
1 1
5 div 0 : int
Notation Recap
e: t e has type t
e ==> e’ e reduces to e’
e↪ v e evaluates to v
Extensional
equivalence
≅
An equivalence relation on expressions of the same type
Extensional Equivalence
safe substitution,
compositional reasoning
Extensional Equivalence
(2, (true,"a"), 3.1) int * (bool * string) * real (2, (true,"a"), 3.1)
Functions
In math, one talks about a function f being a mapping
between spaces X and Y.
Second binding of x shadows rst binding. First binding has been shadowed.
fi
Local declarations
let
val m : int = 3
val n : int = m * m
in
m + n
end
}
let
val k : real = 3.0
in Type?
k * k Value?
end
Local declarations
val k : int = 4
}
let
val k : real = 3.0
in Type?
k * k Value?
end
9.0 : real
k
} Type?
Value?
4 : int
Concrete Type
De nitions
type float = real
type point = float * float
val p : point = (1.0, 2.6)
fi
Functions
Function declaration
(* square : int -> int
REQUIRES: true
ENSURES: square(x) evaluates to x * x
*)
[ /square]
• Reduce e to a value v
1