Upgrade to Pro — share decks privately, control downloads, hide ads and more …

TaPL 読書会 15. Subtyping

daimatz
November 21, 2012

TaPL 読書会 15. Subtyping

daimatz

November 21, 2012
Tweet

More Decks by daimatz

Other Decks in Science

Transcript

  1. ࣗݾ঺հ ‣ !EBJNBU[  দຊେհ  ౦ژ޻ۀେֶ େֶӃ৘ใཧ޻ֶݚڀՊܭࢉ޻ֶઐ߈  ઐ໳͸

    ओʹϥϜμܭࢉϕʔεͷ ݴޠཧ࿦ w ଔ࿦Ͱ͸1SPMPHͰܕਪ࿦Λ࡞ͬͯ·ͨ͠  ϫʔΫεΞϓϦέʔγϣϯζͰόΠτ )BTLFMM 2
  2. 4VCUZQF3FMBUJPO 6 ` t : S S <: T `

    t : T (T-Sub) S <: S (T-Refl) S <: U U <: T S <: T (T-Refl) ‣ 54VC͸5ZQJOH3FMBUJPOͱ4VCUZQF3FMBUJPOؒͷௐ੔  ઌͷྫͰ͸ ‣ ൓ࣹ཯ͱਪҠ཯Λຬͨ͢ ` { x = 0 , y = 1} : { x : Nat }
  3. 4VCUZQF3FMBUJPO SFDPSE ‣ 43DE8JEUIͱ43DE%FQUI͸ී௨ͷΦϒδΣΫτࢦ޲Ͱ ߟ͑Ε͹௚ײతʹ͸໌Β͔ ͦͯ͜͠ΕͰ5ZQF4BGFͰ͋Δ ‣ 43DE1FSN͸ʮϑΟʔϧυͷॱংͷҧ͍͸5ZQF4BGFUZʹ ͸Өڹ͠ͳ͍ʯ͜ͱΛද͢ 7

    {li : Ti21..n+k i } <: {li : Ti21..n i } (S-RcdWidth) {kj : Tj21..n j } is a permutation of {li : Ti21..n i } {kj : Tj21..n j } < : {li : Ti21..n i } ( S-RcdPerm ) for each i [ Si < : Ti] {li : Si21..n i } < : {li : Ti21..n i } ( S-RcdDepth )
  4. 4VCUZQF3FMBUJPO GVODUJPO ‣ Ҿ਺͸ٯͷॱ൪ɺ໭Γ஋͸ಉ͡ॱ൪  Ͱ͋ΓͰͳ͍ܕʹ͍ͭͯ Ͱ͋Δؔ਺Λߟ͑Δͱ  ͔ͭͷͱ͖ w

    ߲ʹTVCUZQFSFMBUJPO͸ͳ͍ͷͰਖ਼͍͠ॻ͖ํͰ͸ͳ͍ 8 T1 <: S1 S2 <: T2 S1 ! S2 <: T1 ! T2 ( S-Arrow ) R2 <: R1 R1 <: R2 R1, R2 f1 : R1 ! R2, f2 : R2 ! R1 f1, f2 R1 ! R2 <: R2 ! R1 r1 >: r2 f1(r1) <: f2(r2) f1 <: f2
  5. 1SPQFSUJFT ‣ -FNNB4VCTUJUVUJPO  ͔ͭͳΒ͹ w ূ໌ɿܕͷಋग़ʹؔ͢Δ*OEVDUJPO ‣ 5IFPSFN1SFTFSWBUJPO 

    ͔ͭͳΒ͹ w ূ໌ɿܕͷಋग़ʹؔ͢ΔTUSBJHIUGPSXBSEͳJOEVDUJPO 12 , x : S ` t : T ` s : S ` [ x 7! s ] t : T ` t : T t ! t0 ` t0 : T
  6. 1SPQFSUJFT ‣ -FNNB$BOPOJDBM'PSN  ͕ʹܕ෇͚͞ΕͨWBMVFͳΒɺ͸ͷܗ  ͕ʹܕ෇͚͞ΕͨWBMVFͳΒɺ͸ ͷܗ ͨͩ͠ ‣

    5IFPSFN1SPHSFTT  ͕XFMMUZQFEͰDMPTFEͳWBMVFͳΒ͹ɺ͸WBMVF ΋͘͠͸Λຬ͕ͨ͢ଘࡏ͢Δ w ূ໌ɿܕͷಋग़ʹؔ͢ΔTUSBJHIUGPSXBSEͳJOEVDUJPO 13 v T1 ! T2 v x : S1.t2 v {li : Ti21..n i } v {kj = vj21..m j } {li21..n i } ✓ {ka21..m a } t t t ! t0 t0
  7. 5PQܕͱ#PUܕ ‣ ߏจͷ֦ு ‣ ܕ෇͚نଇͷ֦ு 14 T ::= ... types:

    Top maximum type Bot minimum type S < : Top ( S-Top ) Bot < : T ( S-Bot )
  8. #PUܕ ‣ #PUܕ͸͢΂ͯͷܕͷ4VCUZQF  +BWBͰ͍͏OVMMͷܕ ΋͘͠͸Ϋϥε  ͜ͷܕͷ߲͸ͲΜͳܕʹͰ΋ͳΕΔ  DMPTFEWBMVFΛ࣋ͨͳ͍ͷͰɺWBMVFΛฦ͞ͳ͍Α͏ͳૢ࡞

    ͷܕʹར༻Ͱ͖Δ w WBMVFΛฦ͞ͳ͍͜ͱΛϓϩάϥϚʹ௨஌͢Δ w ͍͔ͳΔWBMVFͷܕ͕ظ଴͞Ε͍ͯΔίϯςΩετͰ΋ͦ ͷܕΛ҆શʹར༻Ͱ͖Δ͜ͱΛ5ZQF$IFDLFSʹ௨஌͢Δ w ͋͘·Ͱܕ҆શੑͷ࿩ 16
  9. #PUܕ ‣ ͔͠͠#PUܕʹΑͬͯ5ZQF$IFDLFS͕ෳࡶʹͳͬͯ͠· ͏  #PUܕ͕ͳ͍৔߹ɿ͕XFMMUZQFEͳΒ͸ؔ਺ܕ  #PUܕ͕͋Δ৔߹ɿ͕XFMMUZQFEͳΒ͸ؔ਺ܕ ΋͘͠͸#PUܕ 

    /VMM1PJOUFS&YDFQUJPO  )PBSFʮܕ҆શੑͷͨΊʹOVMMͷ֓೦Λߟ͕͑ͨɺ೥ؒ Ͱԯυϧن໛ͷաͪͩͬͨʯ ೥ 17 t1 t2 t1 t1 t2 t1
  10. EPXODBTUͷྫ ‣ ͲΜͳܕͷҾ਺Ͱ΋ͦΕΛEPXODBTUͯ͠࢖͏ؔ਺  Λద༻͢Δͱ੒ޭ͢Δɻ ͷΑ͏ʹܕ෇͚͞ΕΔ  Λద༻͢Δͱࣦഊͯ͠TUVDLʹؕΔ ‣ ͜͜·Ͱͷ֦ுͰͷQSFTFSWBUJPO͸อͨΕ͍ͯΔ͕ɺ

    QSPHSFTTͷੑ࣭͸ࣦͬͯ͠·ͬͨ  ྫ֎Λಋೖ͢Δ͔ɺEPXODBTUΛಈతͳܕνΣοΫͰஔ ͖׵͑Ε͹QSPHSFTTΛճ෮Ͱ͖Δ ԋश 21 f = ( x : Top ) ( x as { a : Nat }) .a ; {a = 5, b = true} ` v1 : T v1 as T ! v1 ( E-Downcast ) {a = 5, b = true} : {a : Nat} {b = true} <:
  11. 4VCTFU4FNBOUJDTͷ໰୊ ‣ *OU'MPBUͳͲ͕͋Δͱɺ JOU5P'MPBU  ͱ͔ॻ͔ͳ ͖Ό͍͚ͳ͍ͱ͜ΖΛ ͱॻ͚ΔΑ͏ʹͳΔͳͲศར ‣ ͔͠͠*OUͱ'MPBU͸ϝϞϦදݱ͕ҟͳΔͷͰɺ໌ࣔతͳ

    DBTUΛল͘ͱ࣮ߦ࣌ʹPQFSBOEͷܕΛνΣοΫͯ͠ద੾ʹ JOU5P'MPBUΛೖΕΔ͜ͱ͕ඞཁʹͳΔ ‣ ϨίʔυͷϑΟʔϧυΞΫηεʹ͍ͭͯ΋ɺTVCUZQJOH͕ͳ ͚Ε͹ϑΟʔϧυͷ഑ஔ͸੩తʹܾ·͍ͬͯͨ ‣ TVCUZQJOHΛೖΕͨ͜ͱͰϑΟʔϧυͷॱ൪Λม͑ͯ΋Α͘ ͳΓɺϑΟʔϧυΞΫηε͸࣮ߦ࣌ʹઌ಄͔Βॱ࣍νΣοΫ ͍͔ͯ͠ͳ͚Ε͹ͳΒͳ͍ ‣ ͜ΕΒ͸࣮ߦ࣌ͷύϑΥʔϚϯε௿Լʹͭͳ͕Δ 33
  12. $PFSDJPO4FNBOUJDT ‣ -FNNBͳΒ͹  ূ໌ɿ$ʹؔ͢Δؼೲ๏ ‣ 5IFPSFNͳΒ͹ ͨͩ͠͸͔ͭͷΑ͏ʹఆ ٛ͞ΕΔ 

    ূ໌ɿ%ʹؔ͢Δؼೲ๏ ‣ Ҏ্ͷΑ͏ʹDPFSDJPOTFNBOUJDTΛఆٛ͢Ε͹ɺ΋͏ ͷධՁنଇΛ࢖͏ඞཁ͸ͳ͘ɺม׵ޙͷ 6OJU SFDPSE ͷධՁنଇΛ࢖͏͜ͱ͕Ͱ͖Δ 39
  13. $PIFSFODF ‣ DPFSDJPOTFNBOUJDTΛ༩͑Δࡍʹ஫ҙ͢΂͖͜ͱ͕͋Δ ‣ ྫ͑͹#BTFUZQFͱͯ͠*OU #PPM 'MPBU 4USJOH͕͋ΔݴޠͰ ͷΑ͏ͳTFNBOUJDTΛఆٛ͢ΔɻJOU5P4USJOH 

    zz qPBU5P4USJOH  zzͩͱ͢Δͱɺ ͷධՁͷࡍ#PPM*OU4USJOHͱ#PPM'MPBU4USJOHͷ ͭͷϧʔτ͕ߟ͑ΒΕɺ݁Ռ͕ҧͬͯ͠·͏ lzͱlz 40
  14. *OUFSTFDUJPOͱ6OJPO ‣ JOUFSTFDUJPOUZQFT͸ڧ͍ੑ࣭Λ࣋ͪɺ JOUFSTFDUJPO UZQFTʹ͓͍ͯܕ෇͚Ͱ͖Δ߲͸ਖ਼نԽ͞Ε߲ͨͰ͋Γɺͦͷ ٯ΋੒Γཱͭɻ ‣ UZQFSFDPOTUSVDUJPOQSPCMFN ষ ͕ඇܾఆతͰ͋Δ͜ͱ

    Λࣔ͢ ‣ *OUFSTFDUJPOʹΑͬͯΦʔόʔϩʔυͷػೳ΋ఏڙ͢Δ  ྫ͑͹ ͷΑ͏ͳԋࢉ͸*OUͷ଍͠ࢉʹ΋'MPBUͷ଍͠ࢉʹ΋࢖͑Δ ‣ ͔͠͠΋ͪΖΜJOUFSTFDUJPOUZQFTΛಋೖ͢Δ͜ͱͰ೉͍͠ ໰୊΋ੜͯ͡͠·͏ 43