10.07.2015 Views

Example 1: Addition of ordinal numbers in SML

Example 1: Addition of ordinal numbers in SML

Example 1: Addition of ordinal numbers in SML

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Slide 1A Semantic Analysis <strong>of</strong> Structural RecursionAndreas AbelMay 10, 1999<strong>Example</strong> 1: <strong>Addition</strong> <strong>of</strong> <strong>ord<strong>in</strong>al</strong> <strong>numbers</strong> <strong>in</strong> <strong>SML</strong>datatype Nat = . . .Slide 2datatype Ord = O| S <strong>of</strong> Ord| Lim <strong>of</strong> Nat –> Ord;fun addord x O = x| addord x (S y ′ ) = S (addord x y ′ )| addord x (Lim f) = Lim (fn z => addord x (f z))WST ’99 Andreas Abel 1


<strong>Example</strong> 2: A pattern match<strong>in</strong>g pro<strong>of</strong> <strong>in</strong> LEGOSlide 3$[leRefl: {T|ClTy}{t|ClTm T}{v:Val T Ts0 t}vLe v v];[[S,T:ClTy][t:ClTm T][v:Val T Ts0 t][s:ClTm S][w:Val S Ts0 s][R:Ty one][r:ClTm (UnfoldRec R)][x:ClV r]leRefl vUnit ==> leUnit|| leRefl (vInl S v) ==> leInl S S (leRefl v)|| leRefl (vInr S v) ==> leInr S S (leRefl v)|| leRefl (vPair v w) ==> lePair (leRefl v) (leRefl w)|| leRefl (vFold R x) ==> leFoldl R (leFoldr R (leRefl x))];Goal: From structural recursiveness . . .∀v. (∀w < v. f(w) ⇓) → f(v) ⇓... <strong>in</strong>fer term<strong>in</strong>ationOutl<strong>in</strong>e:∀v. f(v) ⇓Slide 41. Def. <strong>of</strong> the foetus system: types σ ∈ Ty( ⃗ X), terms t ∈ Tm σ [Γ]2. Def. <strong>of</strong> the evaluation strategy: syntactic values v ∈ Val σ , closures 〈t; e〉 ∈ Cl σ ,op. sem. ⇓⊆ Cl σ × Val σ3. Def. <strong>of</strong> the semantics: “good” values v ∈ [[σ]]4. Def. <strong>of</strong> the structural order<strong>in</strong>g < σ,τ ⊆ [[σ]] × [[τ]]5. Pro<strong>of</strong> <strong>of</strong> the wellfoundedness [[σ]] w.r.t.


The foetus systemType Terms / Values Explanation(Unit) 1 () unit set(Var) X, Y, Z, . . . – type variablesSlide 5(Sum) σ + τ <strong>in</strong>l, <strong>in</strong>r, case disjo<strong>in</strong>t sum(Prod) σ × τ (–,–), fst, snd product(Arr) σ → τ( ⃗ X) λ, rec, – – (app) function space(Rec) Rec X.σ(X) fold, unfold recursive (fixed-po<strong>in</strong>t) typeσ(Rec X.σ(X))fold−−−−−→←−−−−−unfoldRec X.σ(X)<strong>Example</strong> 3: Recursor for Nat <strong>in</strong> foetusNat ≡ Rec X. 1 + XSlide 6O ≡ fold(<strong>in</strong>l())S(v) ≡ fold(<strong>in</strong>r(v))R σ ≡ rec R σ→(Nat→σ→σ)→Nat→σ . λfO σ Nat→σ→σ. λfS . λn Nat .case(unfold(n),1 . f O ,n ′Nat . f S n ′ (R f O f S n ′ ))WST ’99 Andreas Abel 3


<strong>Example</strong> 4: addord <strong>in</strong> foetusOrd ≡ Rec X.(1 + X) + (Nat → X)Slide 7O ≡ fold(<strong>in</strong>l(<strong>in</strong>l()))S(v) ≡ fold(<strong>in</strong>l(<strong>in</strong>r(v)))Lim(f) ≡ fold(<strong>in</strong>r(f))addOrd ≡ rec addOrd Ord→Ord→Ord . λx Ord . λy Ord . case(unfold(y),n 1+Ord . case(n,1 . x,y ′Ord . S(addOrd x y ′ ))f Nat→Ord . Lim(λz Nat . addOrd x (f z)))Operational semanticsClosures Cl σ :〈t σ ; e〉 t term, e environmentSlide 8f ρ→σ @u ρf function value, u argument valueEvaluation relation ⇓ σ ⊆ Cl σ × Val σ :• big step• call-by-value• fixed evaluation strategyWST ’99 Andreas Abel 4


SemanticsLet ⃗ V ⊆ Val ⃗τ . Def<strong>in</strong>e [[σ( ⃗ X)]] ⃗V<strong>in</strong>ductively:(Unit) [[1]] := {()}(Var)[[X n ]] ⃗V := V nSlide 9(Sum) [[(σ + τ)( ⃗ X)]] ⃗V := {<strong>in</strong>l(v) : v ∈ [[σ( ⃗ X)]] ⃗V } ∪ {<strong>in</strong>r(v) : v ∈ [[τ( ⃗ X)]] ⃗V }(Arr) [[σ → τ( ⃗ X)]] ⃗V := {f ∈ Val σ→τ(⃗τ) : ∀u ∈ [[σ]]. ∃v ∈ [[τ( ⃗ X)]] ⃗V .f@u ⇓ v}(Rec)[[Rec Y.σ( ⃗ X, Y )]] ⃗V := lfp F, where we def<strong>in</strong>e F as(F : PVal Rec Y.σ(⃗τ,Y )) → PW ↦→ fold([[σ( X, ⃗ )Y )]] ⃗V ,WRec Y.σ(⃗τ,Y(Val))Fixed-po<strong>in</strong>tLet (U, ⊆) be a complete lattice, F : U → U an operator. The least fixed-po<strong>in</strong>tF = lfp F is characterized by:(ispfp)(ismpfp)F(F ) ⊆ F∀A ∈ U. F(A) ⊆ A → F ⊆ ASlide 10Monotonicity∀σ(X). A ⊆ B → [[σ(X)]] A⊆ [[σ(X)]] BPro<strong>of</strong>: Induction on σ:(Arr)Show: For all f ∈ [[σ → τ(X)]] Aand u ∈ [[σ]] there is av ∈ [[τ(X)]] Bsatisfy<strong>in</strong>g f@u ⇓ v.(Rec) Show: [[Rec Z.σ(X, Z)]] A⊆ [[Rec Z.σ(X, Z)]] B.WST ’99 Andreas Abel 5


SubstitutionSlide 11[[σ(X)]] [τ ] = [[σ(τ)]]For V ⊆ [[τ]] we get[[σ(X)]] V⊆ [[σ(τ)]]<strong>Example</strong> 5: All numerals are goodVal Nat = {(fold ◦ <strong>in</strong>r) n (<strong>in</strong>l()) : n ∈ N} ! = [[Nat]]Slide 12Show: Val Nat is smallest fixed-po<strong>in</strong>t <strong>of</strong>(F : P Val Nat) → P(Val Nat)F(W ) := {fold(v) : v ∈ [[1 + X]] W}= {fold(<strong>in</strong>l()), fold(<strong>in</strong>r(v)) : v ∈ W }We must only show (ismpfp):(i)(ii)⋃n∈NF n (∅) ⊆ WVal Nat ⊆ ⋃ n∈NF n (∅)WST ’99 Andreas Abel 6


Structural order<strong>in</strong>gIdea: Values are trees, “


Structural recursive termsSR σ→τ [Γ] := {rec g.t ∈ Tm σ→τ [Γ] : ∀e ∈ [[Γ]], v ∈ [[σ]].(∀[[σ]] ∋ w < v.〈rec g.t; e〉@w ⇓) → 〈rec g.t; e〉@v ⇓}Slide 15Induction pr<strong>in</strong>ciple for wellfounded sets:(acc<strong>in</strong>d)∀v ∈ [[σ]]. (∀[[σ]] ∋ w < v. P (w)) → P (v)∀v ∈ Acc σ . P (v)All structural recursive terms are good:t ∈ SR σ→τ [Γ], e ∈ [[Γ]] → 〈t; e〉 ∈ [[σ → τ]]<strong>Example</strong> 6: addord is structural recursiveRecursive calls:Slide 16. . . S(addOrd x ′ y) . . .lereflv ′ ≤ v ′lt<strong>in</strong>rv ′ < <strong>in</strong>r(v ′ )leltv ′ ≤ <strong>in</strong>r(v ′ )lt<strong>in</strong>lv ′ < <strong>in</strong>l(<strong>in</strong>r(v ′ ))ltfoldv ′ < S(v ′ ) ≡ fold(<strong>in</strong>l(<strong>in</strong>r(v ′ ))). . . Lim(λz Nat . addOrd (f z) y) . . .lereflw ≤ w∃I∃v ′ ∈ CoDom(f). w ≤ v ′ learrw ≤ flt<strong>in</strong>rw < <strong>in</strong>r(f)ltfoldw < Lim(f) ≡ fold(<strong>in</strong>r(f))WST ’99 Andreas Abel 8


NormalizationDef<strong>in</strong>e the good terms TM σ [Γ] ⊂ Tm σ [Γ] <strong>in</strong>ductively <strong>in</strong> the same way as Tm withthe exceptionSlide 17(REC)t ∈ TM σ→τ [Γ, g σ→τ ]rec g.t ∈ TM σ→τ [Γ]rec g.t ∈ SR σ→τ [Γ]Show normalization∀σ, Γ, t ∈ TM σ [Γ], e ∈ [[Γ]]. 〈t; e〉 ⇓by <strong>in</strong>duction on t us<strong>in</strong>g the operational sematics.Extensions and open questionsSlide 18• positive types (?)• polymorphic types √• dependent types• co<strong>in</strong>ductive typesWST ’99 Andreas Abel 9

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!