-Axiom: --------- - A |- A - -Structural Rules: - -Exchange: Γ, A, B, Δ |- C - --------------------------- - $Gamma;, B, A, Δ |- C +Question: why do thunks work? We know that `blackhole ()` doesn't terminate, so why do expressions like: -Contraction: Γ, A, A |- B - ------------------- - Γ, A |- B + let f = fun () -> blackhole () + in true -Weakening: Γ |- B - ----------------- - Γ, A |- B +terminate? -Logical Rules: +Bottom type, divergence +----------------------- ---> I: Γ, A |- B - ------------------- - Γ |- A --> B +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: ---> E: Γ |- A --> B Γ |- A - ----------------------------------------- - Γ |- B -+ type 'a option = None | Some of 'a;; + type 'a option = None | Some of 'a | bottom;; -`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. +Here are some exercises that may help better understand this. Figure out what is the type of each of the following: -This logic allows derivations of theorems like the following: + fun x y -> y;; -

-------- Id -A |- A ----------- Weak -A, B |- A -------------- --> I -A |- B --> A ------------------ --> I -|- A --> B --> A -+ fun x (y:int) -> y;; -Should remind you of simple types. (What was `A --> B --> A` the type -of again?) + fun x y : int -> 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: + let rec blackhole x = blackhole x in blackhole;; -

-Axiom: ----------- - x:A |- x:A + let rec blackhole x = blackhole x in blackhole 1;; -Structural Rules: + let rec blackhole x = blackhole x in fun (y:int) -> blackhole y y y;; -Exchange: Γ, x:A, y:B, Δ |- R:C - -------------------------------------- - Γ, y:B, x:A, Δ |- R:C + let rec blackhole x = blackhole x in (blackhole 1) + 2;; -Contraction: Γ, x:A, x:A |- R:B - -------------------------- - Γ, x:A |- R:B + let rec blackhole x = blackhole x in (blackhole 1) || false;; -Weakening: Γ |- R:B - --------------------- - Γ, x:A |- R:B [x chosen fresh] + let rec blackhole x = blackhole x in 2 :: (blackhole 1);; -Logical Rules: - ---> I: Γ, x:A |- R:B - ------------------------- - Γ |- \xM:A --> B +By the way, what's the type of this: ---> E: Γ |- f:(A --> B) Γ |- x:A - --------------------------------------------- - Γ |- (fx):B -+ let rec blackhole (x:'a) : 'a = blackhole x in blackhole -In these labeling rules, if a sequence Γ in a premise contains -labeled formulas, those labels remain unchanged in the conclusion. -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 +-------------- -[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.