You are on page 1of 7

Data Structures Data Structures

Interpreters

Advanced Topics
Formal Methods Formal Methods
Formal Methods Basic Recursion / Folding Problems
assumption: Solves the goal if it is already assumed in the context.
reflexivity: Solves the goal if it is a trivial equality
trivial: Solves a variety of easy goals.
auto: Solves a greater variety of easy goals.
discriminate: Solves goal if a trivial inequality and solves any goal if the context contains false
equality
exact: Solves a goal if you know the exact proof term that proves the goal.
contradiction: Solves any goal if the context contains false or contradictory hypotheses.
intros / intro: Introduces variables appearing with forall as well as the premises (left-hand side) of
implications.
simpl: Simplifies the goal or hypotheses in the context.
unfold: Unfolds the definitions of terms.
apply: Uses implications to transform goals and hypotheses.
rewrite: Replaces a term with an equivalent term if the equivalence of the terms has already been
proven.
inversion: Deduces equalities that must be true given an equality between two constructors.
left / right: Replaces a goal consisting of a disjunction P \/ Q with just P or Q.
replace: Replace a term with a equivalent term and generates a subgoal to prove that the equality
holds.
split: Replaces a goal consisting of a conjunction P /\ Q with two subgoals P and Q.
destruct (and/or): Replaces a hypothesis P /\ Q with two hypotheses P and Q. Alternatively, if the
hypothesis is a disjunction P \/ Q, generates two subgoals, one where P holds and one
where Q holds.
destruct (case analysis): Generates a subgoal for every constructor of an inductive type. Modular Programming
induction: Generates a subgoal for every constructor of an inductive type and provides an
induction hypothesis for recursively defined constructors.
ring: Solves goals consisting of addition and multiplication operations.
tauto: Solves goals consisting of tautologies that hold in constructive logic.
field: Solves goals consisting of addition, subtraction (the additive inverse), multiplication, and
division (the multiplicative inverse).
True or False Problems
1. If f : 'a -> 'b then f is guaranteed always to return a value of type 'b. - False
2. Evaluation rules for pattern matching require evaluating each pattern pi to a value vi then
matching against vi. - False
3. In black-box testing, the implementation of a function is used to guide the development tests-F
4. Expression (e1,e2) has type t1*t2 if e1 has type t1 and e2 has type t2. - True
5. Higher-order functions are functions that return types rather than returning values - False
6. @ is tail recursive. - False; 7. None has type option. - False; 9. 3110 : int - True
8. An abstraction function maps from abstract values that do not satisfy the representation
invariant to abstract values that do satisfy the representation invariant - True
10. let f x y = x+y and let f = fun x -> fun y -> x+y are semantically equivalent. - True
11. x::x::_ is a pattern that matches any list in which the first two elements are the same. - False
12. A color modeled by r, g, b components is better represented by a variant than a record. - False
13. 11 + (Some 11) evaluates to Some 22. - False
14. A polymorphic variant type must be defined before it can be used. - False
15. If type t is abstract in signature S, then a structure with type S is not permitted to define t - F
16. A functor takes a signature as input and produces a structure as output. - False
17. A glass-box test is based on the implementation details of a module. - True
18. Every OCaml value that can appear in a .ml file is an expression. - True
19. (fun x -> e) v --> e{v/x} - True; 20. All components of a tuple must have same type - F
21. (int -> int) -> int and int -> (int -> int) are the same type. - False
22. Tail-recursive calls use O(1) stack space. - True
23. <{}, fun x -> 31 + 10> ==> fun x -> 41 - False
24. AF and RI should be documented in a module's interface. - False
25. OCaml forbids aliasing of memory locations. - False
26. Hash tables provide constant worst-case time for insert and find operations. - False
27. The Async scheduler guarantees that callbacks associated with Deferreds are executed in the
order in which those Deferreds become determined.False
28. An Ivar can be filled twice: once before it becomes determined, and once after. - False
29. In a monad, return x >>= f and f x should behave the same. - True
30. Closures can be implemented with objects, objects can be implemented with closures. - True
Type Matching Problems
1. (fun -> 42) : ’a -> int;; 2. ((=) 3) : int -> bool
3. let g x y = Some (x + y) in g : int -> int -> int option
4. let h l = List.fold right (fun x a -> x::a) l l in h : ’a list -> ’a list
5. let rec l x = l x in l : ’a ->’b;; 6. 3110 : int;; 7. List.map (fun x -> x+1) : int list -> int list
8. [[None]; [Some []]] : ’a list option list list;; 9. (() ,[]) : unit * 'a list;
10. let r x y z = y z x in r : ’a -> (’b -> ’a -> ’c) -> ’b -> ’c *);;
11. let y = 3110 in fun x -> failwith " 2024 " : ‘a -> ‘b
12. List.fold_left ( fun a x y -> (a x) - y) ( fun z -> z) [1; 2; 3] 17 : int (evaluations to 21)
More Modular Programming Problems Mutable Structure Problems

Async & Monads

Specification Problems

Objects

Interpreter Evaluation
Extraneous Stuff

# module ListSetNoDupsExtended = ExtendSet(ListSetNoDups);;


Functors >> Include >> Open (Open only contains within current scope)
let fib30long = take 30 fibs |> List.rev |> List.hd STANDARD OCAML ENVIRONMENT MODEL
let fib30lazy = lazy (take 30 fibs |> List.rev |> List.hd) <env,e> is a machine configuration. The machine's environment is env and the code remaining to
let fib30 = Lazy.force fib30lazy be evaluated is e.

let fib30fast = Lazy.force fib30lazy
<env,e> ==> v means "e evaluates or big-steps to v in environment env"

(|fun x -> e, env|) denotes a closure with environment env.

{x1:v1, x2:v2, ...} denotes the environment with x1 bound to v1, x2 bound to v2 and so on.

env[x->v] denotes the environment env extended with a binding from x to v. If env already has a
binding for x then it is replaced.

env(x) denotes the value associated with x in env.


STANDARD OCAML SUBSTITUTION MODEL e ::= x | e1 e2 | fun x -> e


e ::= x | e1 e2 | fun x -> e | n | b | e1 + e2
| n | b | e1 + e2 | (e1,e2) | fst e1 | snd e2
| (e1,e2) | fst e1 | snd e2 | Left e | Right e
| Left e | Right e | match e with Left x1 -> e1 | Right x2 -> e2
| match e with Left x -> e1 | Right y -> e2 | if e1 then e2 else e3
| if e1 then e2 else e3 | let x = e1 in e2
| let x = e1 in e2
v ::= fun x -> e | n | b | (v1,v2) | Left v | Right v <env, x> ==> v
if env(x) = v
e1 + e2 --> e1' + e2 <env, e1 e2> ==> v'
if e1 --> e1' if <env,e1> ==> (|fun x -> e, newenv|)
v1 + e2 --> v1 + e2' and <env,e2> ==> v
if e2 --> e2' and <newenv[x->v],e> ==> v'
n1 + n2 --> n3 <env, fun x -> e> ==> (|fun x -> e, env|)
where n3 is the result of applying primitive operation + <env, n> ==> n
to n1 and n2 <env, b> ==> b
(e1, e2) --> (e1', e2) <env, e1 + e2> ==> n
if e1 --> e1' if <env,e1> ==> n1
(v1, e2) --> (v1, e2') and <env,e2> ==> n2
if e2 --> e2' and n is the result of applying the primitive operation + to n1 and n2
fst (v1,v2) --> v1 <env, (e1,e2)> ==> (v1,v2)
snd (v1,v2) --> v2 if <env,e1> ==> v1
Left e --> Left e' and <env,e2> ==> v2
if e --> e' <env, fst e> ==> v1
Right e --> Right e' if <env,e> ==> (v1,v2)
if e --> e' <env, snd e> ==> v2
match e with Left x -> e1 | Right y -> e2 if <env,e> ==> (v1,v2)
--> match e' with Left x -> e1 | Right y -> e2 <env, Left e> ==> Left v
if e --> e' if <env,e> ==> v
match Left v with Left x -> e1 | Right y -> e2 <env, Right e> ==> Right v
--> e1{v/x} if <env,e> ==> v
match Right v with Left x -> e1 | Right y -> e2 <env, match e with Left x1 -> e1 | Right x2 -> e2> ==> v1
--> e2{v/y} if <env,e> ==> Left v
if e1 then e2 else e3 --> if e1' then e2 else e3 and <env[x1->v], e1> ==> v1
if e1 --> e1' <env, match e with Left x1 -> e1 | Right x2 -> e2> ==> v2
if true then e2 else e3 --> e2 if <env,e> ==> Right v
if false then e2 else e3 --> e3 and <env[x2->v], e2> ==> v2
let x = e1 in e2 --> let x = e1' in e2 <env, if e1 then e2 else e3> ==> v2
if e1 --> e1' if <env,e1> ==> true
let x = v in e2 --> e2{v/x} and <env,e2> ==> v2
e1 e2 --> e1' e2 <env, if e1 then e2 else e3> ==> v3
if e1 --> e1' if <env,e1> ==> false
v e2 --> v e2' and <env,e3> ==> v3
if e2 --> e2' <env, let x = e1 in e2> ==> v2
(fun x -> e) v2 --> e{v2/x} if <env,e1> ==> v1
and <env[x->v1],e2> ==> v2
x{e/x} = e
y{e/x} = y
(e1 e2){e/x} = e1{e/x} e2{e/x}
(fun x -> e'){e/x} = (fun x -> e')
(fun y -> e'){e/x} = (fun y -> e'{e/x})
n{e/x} = n
b{e/x} = b
(e1 + e2) {e/x} = e1{e/x} + e2{e/x}
(e1,e2){e/x} = (e1{e/x}, e2{e/x})
(fst e'){e/x} = fst e'{e/x}
(snd e'){e/x} = snd e'{e/x}
(Left e'){e/x} = Left e'{e/x}
(Right e'){e/x} = Right e'{e/x}
(match e' with Left y -> e1 | Right z -> e2){e/x}
= match e'{e/x} with Left y -> e1{e/x} | Right z -> e2{e/x}
(match e' with Left x -> e1 | Right z -> e2){e/x}
= match e'{e/x} with Left x -> e1 | Right z -> e2{e/x}
(match e' with Left y -> e1 | Right x -> e2){e/x}
= match e'{e/x} with Left y -> e1{e/x} | Right x -> e2
(match e' with Left x -> e1 | Right x -> e2){e/x}
= match e'{e/x} with Left x -> e1 | Right x -> e2
(if e1 then e2 else e3){e/x}
= if e1{e/x} then e2{e/x} else e3{e/x}
(let x = e1 in e2){v/x} = let x = e1{v/x} in e2
(let y = e1 in e2){v/x} = let y = e1{v/x} in e2{v/x}

You might also like