```-Axiom: ---------
-        A |- A
-
-Structural Rules:
-
-          Γ, A, B, Δ |- C
-Exchange: ---------------------------
-          Γ, B, A, Δ |- C
-
-             Γ, A, A |- B
-Contraction: -------------------
-             Γ, A |- B
-
-           Γ |- B
-Weakening: -----------------
-           Γ, A |- B
+Question: why do thunks work? We know that `blackhole ()` doesn't terminate, so why do expressions like:

-Logical Rules:
+	let f = fun () -> blackhole ()
+	in true

-         Γ, A |- B
---> I:   -------------------
-         Γ |- A --> B
+terminate?

-         Γ |- A --> B         Γ |- A
---> E:   -----------------------------------
-         Γ |- B
-```
+Bottom type, divergence +----------------------- -`A`, `B`, etc. are variables over formulas. -Γ, Δ, etc. are variables over (possibly empty) sequences -of formulas. Γ `|- A` is a sequent, and is interpreted as -claiming that if each of the formulas in Γ is true, then `A` -must also be true. +Expressions that don't terminate all belong to the **bottom type**. This is a subtype of every other type. That is, anything of bottom type belongs to every other type as well. More advanced type systems have more examples of subtyping: for example, they might make `int` a subtype of `real`. But the core type system of OCaml doesn't have any general subtyping relations. (Neither does System F.) Just this one: that expressions of the bottom type also belong to every other type. It's as if every type definition in OCaml, even the built in ones, had an implicit extra clause: -This logic allows derivations of theorems like the following: + type 'a option = None | Some of 'a;; + type 'a option = None | Some of 'a | bottom;; -
```--------  Id
-A |- A
----------- Weak
-A, B |- A
-------------- --> I
-A |- B --> A
------------------ --> I
-|- A --> B --> A
-```
+Here are some exercises that may help better understand this. Figure out what is the type of each of the following: -Should remind you of simple types. (What was `A --> B --> A` the type -of again?) + fun x y -> y;; -The easy way to grasp the Curry-Howard correspondence is to *label* -the proofs. Since we wish to establish a correspondence between this -logic and the lambda calculus, the labels will all be terms from the -simply-typed lambda calculus. Here are the labeling rules: + fun x (y:int) -> y;; -
```-Axiom: -----------
-       x:A |- x:A
+	fun x y : int -> y;;

-Structural Rules:
+	let rec blackhole x = blackhole x in blackhole;;

-          Γ, x:A, y:B, Δ |- R:C
-Exchange: -------------------------------
-          Γ, y:B, x:A, Δ |- R:C
+	let rec blackhole x = blackhole x in blackhole 1;;

-             Γ, x:A, x:A |- R:B
-Contraction: --------------------------
-             Γ, x:A |- R:B
+	let rec blackhole x = blackhole x in fun (y:int) -> blackhole y y y;;

-           Γ |- R:B
-Weakening: ---------------------
-           Γ, x:A |- R:B     [x chosen fresh]
+	let rec blackhole x = blackhole x in (blackhole 1) + 2;;

-Logical Rules:
+	let rec blackhole x = blackhole x in (blackhole 1) || false;;

-         Γ, x:A |- R:B
---> I:   -------------------------
-         Γ |- \xM:A --> B
+	let rec blackhole x = blackhole x in 2 :: (blackhole 1);;

-         Γ |- f:(A --> B)      Γ |- x:A
---> E:   -------------------------------------
-         Γ |- (fx):B
-```
+By the way, what's the type of this: -In these labeling rules, if a sequence Γ in a premise contains -labeled formulas, those labels remain unchanged in the conclusion. + let rec blackhole (x:'a) : 'a = blackhole x in blackhole -What is means for a variable `x` to be chosen *fresh* is that -`x` must be distinct from any other variable in any of the labels -used in the proof. -Using these labeling rules, we can label the proof -just given: +Back to thunks: the reason you'd want to control evaluation with thunks is to +manipulate when "effects" happen. In a strongly normalizing system, like the +simply-typed lambda calculus or System F, there are no "effects." In Scheme and +OCaml, on the other hand, we can write programs that have effects. One sort of +effect is printing (think of the [[damn]] example at the start of term). +Another sort of effect is mutation, which we'll be looking at soon. +Continuations are yet another sort of effect. None of these are yet on the +table though. The only sort of effect we've got so far is *divergence* or +non-termination. So the only thing thunks are useful for yet is controlling +whether an expression that would diverge if we tried to fully evaluate it does +diverge. As we consider richer languages, thunks will become more useful. -
```-------------  Id
-x:A |- x:A
----------------- Weak
-x:A, y:B |- x:A
-------------------------- --> I
-x:A |- (\y.x):(B --> A)
----------------------------- --> I
-|- (\x y. x):A --> B --> A
-```
-We have derived the *K* combinator, and typed it at the same time! +Towards Monads +-------------- -Need a proof that involves application, and a proof with cut that will -show beta reduction, so "normal" proof. - -[To do: add pairs and destructors; unit and negation...] +This has now been moved to the start of [[week7]]. -Excercise: construct a proof whose labeling is the combinator S.