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

[スクリプト] Swiftの型推論を学ぼう

[スクリプト] Swiftの型推論を学ぼう

スクリプトです

omochimetaru

March 23, 2024
Tweet

More Decks by omochimetaru

Other Decks in Programming

Transcript

  1. PNPDIJNFUBSV!USZ4XJGU5PLZP -FUT-FBSO"CPVU 5ZQF*OGFSFODFJO4XJGU Έͳ͞Μ͜Μʹͪ͸ɻ ͜Ε͔Βɺ4XJGUͷܕਪ࿦Λֶ΅͏ɺͱ͍͏࿩Λ͠·͢ɻ "CPVU.F w *FOKPZSFBEJOHQSPQPTBMTPO4XJGU&WPMVUJPOBOE CSPXTJOHUIF4XJGUP ffi

    DJBMGPSVNBTBIPCCZ w *DPPSHBOJ[FTUVEZHSPVQTGPDVTFEPOUIF4XJGU DPNQJMFS w *BNSFDPHOJ[FEBTBDPNNJUUFSUPUIF4XJGU DPNQJMFS PNPDIJNFUBSV ࢲ͸͓΋ͪϝλϧͱ͍͍·͢ɻ ༑ਓ͔Β͸ʮ͓΋ͪ͞Μʯͱݺ͹Ε͍ͯ·͢ɻ ࢲ͸4XJGU͕େ޷͖Ͱ͢ɻ ͦΕΛಡΈॻ͖͢Δ͚ͩͰͳ͘ɺ4XJGUݴޠͷൃలʹ΋ڵຯ͕͋Γ·͢ɻ ͦΕͰɺެࣜϑΥʔϥϜ΍4XJGU&WPMVUJPOͷϓϩϙʔβϧΛΑ͘ݟ͍ͯ·͢ɻ ·ͨɺݴޠͷ࣮૷ͦͷ΋ͷͰ͋ΔίϯύΠϥʹ΋ڵຯ͕͋Γ·͢ɻ ஥ؒͱڞʹ4XJGUίϯύΠϥʹ͍ͭͯͷษڧձΛӡӦ͍ͯͯ͠ɺௐ΂ͨ͜ͱΛൃ දͨ͠Γ͍ͯ͠·͢ɻ ͦΕͰҎલʹԿ౓͔ίϯύΠϥʹίϯτϦϏϡʔτͨ͜͠ͱ͕͋Γɺͦͷࡍʹ ίϛολʔʹೝఆ͞Ε͍ͯ·͢ɻ ࠓ೔͸ͦ͏ͨ͠׆ಈͷதͰ஌ͬͨ͜ͱΛ࿩͠·͢ɻ
  2. 5ZQF*OGFSFODF w 0OFPG4XJGUTHSFBUFTUBQQFBMT w 5IFDPFYJTUFODFPGTBGFUZBOEFBTFPGXSJUJOH w 6OEFSTUBOEJOHUIF.JOETFUPGUIF$PNQJMFS w *IBWFMPPLFEJOUPJUBOEJNQMFNFOUFEJUJOUIFQBTU https://github.com/omochi/SwiftTypeInferenceHandsOn

    w *IBWFDSFBUFEBTVJUBCMFFYQMBOBUJPOGPSCFHJOOFST Έͳ͞Μ͸4XJGUͷͲΜͳͱ͜Ζ͕޷͖Ͱ͠ΐ͏͔ɻ ஋ܕͱ͔ϓϩτίϧࢦ޲ͱ͔ɺ͍Ζ͍Ζ͋Γ·͕͢ɺܕਪ࿦͸ಛʹॏཁͩͱࢥ ͍·͢ɻ ܕਪ࿦͸ɺ੩తܕ෇͚ʹΑΔ҆શੑͱɺܕͷলུʹΑΔهड़͠΍͢͞Λཱ྆͠ ͯ͘ΕΔૉ੖Β͍͠ػೳͰ͢ɻ ݱ୅ͷϓϩάϥϛϯάݴޠʹ͓͍ͯ͸΋͸΍౰ͨΓલͷػೳͰ͋Γɺٯʹҙࣝ ͍ͯ͠ͳ͍͔΋͠Ε·ͤΜɻ ͦΕ͙Β͍ॏཁ͔ͭجຊతͰ͋Δ͔Βʹ͸ɺ ͦΕΛৄ͘͠஌͍ͬͯΔ͜ͱʹ͸େ͖ͳՁ஋͕͋ΔͰ͠ΐ͏ɻ ίϯύΠϥ͕ܕਪ࿦Λ͢Δͱ͖ɺ ͦΕ͕ͲͷΑ͏ͳॲཧΛ͢Δ͔ͷ஌͕ࣝ͋Ε͹ɺ ίʔυΛॻ͘ͱ͖ʹ΋໾ʹͨͭࣄ͕͋ΔͰ͠ΐ͏ɻ Ҏલʹͦ͏ߟ͑ͨࢲ͸ɺ4XJGUͷܕਪ࿦ʹ͍ͭͯௐ΂ɺ ίϯύΠϥͷ࣮૷ΛಡΈɺҰ෦Λ࠶࣮૷ͯ͠Έ·ͨ͠ɻ ͦͷ࣌͸ͳ͔ͳ͔ۤ࿑͠·ͨ͠ɻ ग़ͯ͘Δ৘ใ͸ઐ໳తͰ೉͘͠ɺίʔυ΋ෳࡶͰಡΈղ͘౒ྗ͕ඞཁͩͬͨͷ Ͱ͢ɻ ͦ͜Ͱࠓ೔ࢲ͸ɺ͜ͷܦݧΛ౿·͑ɺ 4XJGUͷܕਪ࿦ͷجૅΛɺ୭Ͱ΋؆୯ʹཧղͰ͖ΔΑ͏ʹղઆ͠·͢ɻ
  3. 0VUMJOF w #BTJD#JEJSFDUJPOBM*OGFSFODF"MHPSJUIN w &YUFOTJPOTGPS4XJGU w *NQMJDJU5ZQF$POWFSTJPO w 0WFSMPBEJOH w

    )PXUP*OWFTUJHBUF6TJOHUIF$PNQJMFS ࠓ೔ͷ࿩ͷྲྀΕΛࣔ͠·͢ɻ ·ͣɺجૅతͳ૒ํ޲ͷਪ࿦ΞϧΰϦζϜΛઆ໌͠·͢ɻ Ͱ͕͢͜Ε͸ͱͯ΋جૅతͳͨΊɺ4XJGUͷݴޠ࢓༷ʹରԠ͢Δʹ͸େ͖ͳෆ଍ ͕͋Γ·͢ɻ ͦ͜Ͱ࣍ʹɺͦΕΛ4XJGU޲͚ʹ֦ுͯ͠ɺ ҉໧ͷܕม׵΍ΦʔόʔϩʔυʹରԠ͢Δํ๏Λࣔ͠·͢ɻ ͜Ε͕ॏཁͳεςοϓͰɺΑΓൃలతͳ಺༰Λཧղ͢Δ଍͕͔ΓʹͳΓ·͢ɻ ࠷ޙʹɺίϯύΠϥΛ࢖ͬͯܕਪ࿦ͷಈ࡞Λௐ΂Δํ๏Λ঺հ͠·͢ɻ ͜͜·Ͱͷ࿩ʹج͚ͮ͹ɺΑΓߴ౓ͳ಺༰Λࣗ෼ͰֶΜͰ͍͚ΔΑ͏ʹͳΔͰ ͠ΐ͏ɻ #JEJSFDUJPOBM5ZQF*OGFSFODF ͦΕͰ͸࿩Λ࢝Ί·͢ɻ ͸͡Ίʹɺ4XJGUͷܕਪ࿦͕૒ํ޲Ͱ͋Δͱ͍͏࿩Λ͠·͢ɻ
  4. &YBNQMFPG'PSXBSE*OGFSFODF func id<X>(_ x: X) -> X func main(i: Int)

    { let a = id(i) } ͜ͷίʔυΛݟ͍ͯͩ͘͞ɻ ίϯύΠϥ͕BͷܕΛ*OUͩͱਪ࿦͠·͢ɻ ͦͷखॱ͸͜͏ͳΓ·͢ɻ !·ͣJ͕*OUͰ͢ɻ !͜Ε͕JEʹ౉͞ΕΔͷͰɺͦͷฦΓ஋͕*OUͰ͢ɻ !͜Ε͕Bʹ୅ೖ͞ΕΔͷͰɺBͷܕ͕*OUͰ͢ɻ !͜ͷखॱ͸ɺίʔυͷ࣮ߦॱͱಉ͡Ͱ୯७Ͱ͢ɻ ͜ͷΑ͏ʹܕΛॱ൪ʹ௥͍͚ͬͯ͹ਪ࿦͕Ͱ͖ΔͷͰ͠ΐ͏͔ʁ ࢒೦ͳ͕Βͦ͏͸͍͖·ͤΜɻ &YBNQMFPG3FWFSTF*OGFSFODF func id<X>(_ x: X) -> X func main(i: Int) { let a: Int = id(.init()) } ࣍͸͜ͷίʔυΛݟ͍ͯͩ͘͞ɻ υοτࣜͷJOJUͷݺͼग़͕͋͠ΔͷͰɺίϯύΠϥ͸υοτͷલʹলུ͞Εͨܕ ໊Λਪ࿦͠·͢ɻ ͦͷखॱ͸࣍ͷΑ͏ʹͳΓ·͢ɻ !Bͷܕ͕*OUͰ͢ɻ !JEͷฦΓ஋͸ͦΕʹ୅ೖ͞Ε͍ͯΔͷͰ*OUͰ͢ɻ
  5. ͳͷͰɺͦͷҾ਺΋*OUͰ͢ɻ !ͦ͜ʹॻ͔ΕͨυοτࣜͳͷͰɺ*OUͷলུͱΈͳ͞Ε·͢ɻ !͜ͷखॱ͸ઌఔͷྫͱਅٯͷॱ൪Ͱ͢ɻ ͜ͷΑ͏ʹɺܕਪ࿦Ͱ͸૒ํ޲ʹܕ৘ใΛ௥͍͔͚Δඞཁ͕͋Γ·͢ɻ ͜ΕΛͲͷΑ͏ʹ࣮૷͢Ε͹ྑ͍͔આ໌͠·͢ɻ #BTJD5ZQF*OGFSFODF ͦΕͰ͸͔͜͜Β͸ɺجຊతͳܕਪ࿦ͷ࢓૊ΈΛઆ໌͠·͢ɻ "MHPSJUIN8 w "WPJEJOHGPSNBMEJTDVTTJPOT

    *XJMMCSJF ZFYQMBJOUIF PXPG PQFSBUJPOT w ⚠1MFBTFGPSHFUBCPVUlJNQMJDJUUZQFJOGFSFODFzGPSBNPNFOU ΞϧΰϦζϜ8ͱ͍͏ܕਪ࿦ͷํ๏͕͋Γ·͢ɻ ͜Ε͸Α͘஌ΒΕͨجຊతͳ΋ͷͰɺ4XJGU΋ϕʔε͸͜͏ͳ͍ͬͯ·͢ɻ ͜ΕΛ4XJGUΛྫʹͯ͠આ໌͍͖ͯ͠·͢ɻ ͨͩ͠ɺଟ͘ͷਓʹͱͬͯΘ͔Γ΍͍͢Α͏ʹɺݫີͳܗࣜతఆٛʹ͸৮Εͣɺ ͬ͘͟Γͱͨ͠ྲྀΕΛઆ໌͠·͢ɻ ·ͨɺ4XJGUʹ͓͚Δ҉໧ͷܕม׵͸͍ͬͨΜແࢹ͠·͢ɻ
  6. 5ZQF*OGFSFODF1SPDFTT0VUMJOF w 1BSTJOH$PEFJOUPBO"45 "CTUSBDU4ZOUBY5SFF  w #JOEJOH5ZQF7BSJBCMFTUPUIF"45 w (FOFSBUJOH$POTUSBJOUTGSPN"45 w

    3FQFBUFEMZ1FSGPSNJOH6OJ fi DBUJPOBOE4JNQMJ DBUJPO େ·͔ͳྲྀΕ͸࣍ͷΑ͏ʹͳΓ·͢ɻ ·ͣɺίʔυΛύʔεͯ͠"45ʹ͠·͢ɻ ࣍ʹɺ"45ʹܕม਺ΛׂΓ౰ͯ·͢ɻ ͦͯ͠ɺ"45͔Β੍໿Λੜ੒͠·͢ɻ ࠷ޙʹɺ୯ҰԽͱ؆໿ԽΛ܁Γฦͯ͠ਪ࿦ΛਐΊ·͢ɻ ͦΕͰ͸ɺͦΕͧΕͷखॱʹ͍ͭͯઆ໌͠·͢ɻ "45 "CTUSBDU4ZOUBY5SFF w "SFQSFTFOUBUJPOPGUIFDPEFJOBUSFFTUSVDUVSF w 5IFDPNQJMFS fi OETJUFBTZUPIBOEMF ·ͣɺίʔυΛύʔεͯ͠"45ʹ͠·͢ɻ "45ͱ͍͏ͷ͸ίʔυΛ໦ߏ଄Ͱදͨ͠΋ͷͰ͢ɻ ςΩετͰ͋Δίʔυͱҧͬͯɺ ͞·͟·ͳॲཧΛ͠΍͘͢ͳΓ·͢ɻ &YBNQMFPG"45 func id<X>(_ x: X) -> X func main(i: Int) { let a = id(i) } WBSEFDMB DBMM EFDMSFGJE EFDMSFGJ "45ͷྫΛࣔ͠·͢ɻ B΁ͷ୅ೖจΛ"45ʹͨ͠΋ͷΛӈʹද͍ࣔͯ͠·͢ɻ ίʔυͱͷରԠؔ܎Λઆ໌͠·͢ɻ !·ͣɺBͷએݴͱॳظԽ͕࠷্෦ͷWBSEFDMϊʔυͱରԠ͠·͢ɻ !࣍ʹɺJEؔ਺ͷݺͼग़͕͠DBMMϊʔυͱରԠ͠·͢ɻ !ؔ਺ݺͼग़͠ͷର৅ͱͯ͠ɺͦͷࢠϊʔυ͕ɺݺͼग़͞ΕΔؔ਺JE΁ͷࢀরΛ ͍࣋ͬͯ·͢ɻ
  7. !·ͨɺؔ਺ʹ౉͢Ҿ਺ͷJ΁ͷࢀর͕ɺ΋͏ҰͭͷࢠϊʔυͰ͢ɻ !͔͜͜Βઌ͸ɺ͜ͷ"45ʹରͯ͠ॲཧ͠·͢ɻ #JOEJOH5ZQF7BSJBCMFT w 5ZQFWBSJBCMFTBSFWBSJBCMFTXIFSFUZQFTBSFBTTJHOFE w #JOHJOHUZQFWBSJBCMFTUPUIFQBSUTPGUIF"45XIFSFUIFUZQFJTOPU ZFULOPXO w 5ZQFJOGFSFODFJTUIFUBTLPGEFUFSNJOJOHUIFUZQFTGPSBMMUZQF

    WBSJBCMFT ࣍͸ܕม਺ͷׂΓ౰ͯͰ͢ɻ ܕม਺ͱ͸ɺܕ͕ೖΔม਺ͷࣄͰ͢ɻ ͜ΕΛɺ"45ͷதͰɺ·ͩܕ͕Θ͔Βͳ͍෦෼ʹׂΓ౰ͯ·͢ɻ ܕਪ࿦ͱ͸ɺશͯͷܕม਺ͷܕΛܾఆ͢ΔλεΫͳͷͰ͢ɻ &YBNQMFPG#JOEJOH5ZQF7BSJBCMFT WBSEFDMB DBMM EFDMSFGJE EFDMSFGJ $T1 $T2 ($T3) -> $T3 Int <X> (X) -> X func id<X>(_ x: X) -> X func main(i: Int) { let a = id(i) } ઌఔͷ"45ʹܕม਺ΛׂΓ౰ͯΔաఔΛݟͯΈ·͠ΐ͏ɻ !ม਺Bͷܕ͸Θ͔Βͳ͍ͷͰܕม਺5Λ৽ͨʹಋೖ͠·͢ɻ ܕม਺͸ͦͷ໨ҹͱͯ͠ઌ಄ʹυϧه߸Λ෇͚͍ͯ·͢ɻ !DBMMϊʔυͷܕ΋Θ͔Βͳ͍ͷͰɺܕม਺5Λಋೖ͠·͢ɻ !JEؔ਺͸ܕ͕Θ͔Γ·͕͢ɺδΣωϦοΫύϥϝʔλ9ʹ͍ͭͯܕม਺5Λಋ ೖ͠·͢ɻ
  8. !J͸௚઀ॻ͔Ε͍ͯΔܕ͕Θ͔Γ·͢ɻ ! (FOFSBUJOH$POTUSBJOUT w *OEJDBUJOHUIFSFMBUJPOTIJQCFUXFFOUZQFTBOEUZQFWBSJBCMFT w (FOFSBUJOHGSPN"45 w *OGFSFODFQSPHSFTTFTCZNBOJQVMBUJOHUIFTFUPGDPOTUSBJOUT ࣍͸੍໿ͷੜ੒Ͱ͢ɻ

    ੍໿ͱ͍͏ͷ͸ɺܕ΍ܕม਺ʹ͍ͭͯͷؔ܎Λࣔ͢΋ͷͰ͢ɻ ίϯύΠϥ͸"45Λ८ճͯ͠ɺෳ਺ͷ੍໿Λੜ੒͠·͢ɻ ܕਪ࿦͸ɺ͜ͷ੍໿ͷू߹Λૢ࡞͢Δ͜ͱͰܭࢉΛਐΊ·͢ɻ (FOFSBUJOH$POTUSBJOUTGSPN"TTJHONFOU4UBUFNFOU WBSEFDM init A B eq(A, B) ͭ໨ͷ੍໿ͷੜ੒نଇΛࣔ͠·͢ɻ WBSEFDMϊʔυ͔ΒFRVBM੍໿Λੜ੒͠·͢ɻ ม਺ͷܕ"ͱॳظԽจͷܕ#ʹରͯ͠ɺ FR " # ͱॻ͍ͯɺ "ͱ#͕౳͍͜͠ͱΛҙຯ͠·͢ɻ
  9. (FOFSBUJOH$POTUSBJOUTGSPN'VODUJPODBMMJOH DBMM DBMMFF BSHT R F A call(F, A, R)

    ͭ໨ͷੜ੒نଇΛࣔ͠·͢ɻ DBMMϊʔυ͔ΒDBMM੍໿Λੜ੒͠·͢ɻ ϊʔυͷܕ3ͱɺݺͼग़͞ΕΔؔ਺ͷܕ'ɺ༩͑ΔҾ਺ͷܕ"͔ΒɺDBMM ' " 3  ͱॻ͖·͢ɻ "45Λͦͷ··ॻ͖͚ࣸͨͩ͠ͷΑ͏ͳײ͡Ͱ͕͢ɺ ͜ͷΑ͏ʹ੍໿ͷܗʹ͢Δ͜ͱͰ͜ͷޙͷॲཧͰѻ͑ΔΑ͏ʹ͍ͯ͠·͢ɻ &YBNQMFPG(FOFSBUJOH$POTUSBJOUT WBSEFDMB DBMM EFDMSFGJE EFDMSFGJ $T1 $T2 ($T3) -> $T3 Int eq($T1, $T2) call(($T3) -> $T3, Int, $T2) ຊ౰͸΋ͬͱͨ͘͞Μͷ"45ϊʔυ΍੍໿ͷछྨ͕͋ΔͷͰ͕͢ɺ جຊΛཧղ͢Δ্Ͱ͸͜ΕͰे෼Ͱ͢ɻ ઌ΄Ͳྫʹग़ͨ͠"45͔Β੍໿ੜ੒Λͯ͠Έ·͢ɻ !·ͣɺWBSEFDMϊʔυ͔ΒFR੍໿͕ੜ੒͞Ε·͢ɻ !࣍ʹɺDBMMϊʔυ͔ΒDBMM੍໿͕ੜ੒͞Ε·͢ɻ ! *OGFSFODF w 3FQFBUFEMZBQQMZJOHVOJ fi DBUJPOBOETJNQMJ DBUJPOUPUIFTFUPG DPOTUSBJOUT w 5IFQSPDFTTJTDPNQMFUFPODFUIFUZQFTPGBMMUZQFWBSJBCMFTIBWF CFFOEFUFSNJOFE ͦ͏ͨ͠Β͍Α͍Αਪ࿦Λ͠·͢ɻ ͜Ε͸ɺ༻ҙ੍ͨ͠໿ू߹ʹରͯ͠ɺ ୯ҰԽͱ͍͏ૢ࡞ͱɺ ؆໿Խͱ͍͏ૢ࡞Λ܁Γฦ͢खଓ͖Ͱ͢ɻ ͜ͷաఔͰܕม਺ͷܕ͕൑໌͍͖ͯ͠·͢ɻ
  10. 6OJGJDBUJPO w 'JOEFRVBMDPOTUSBJOUTGPSUZQFWBSJBCMFTBOESFQMBDFBMMUIFJSPDDVSSFODFT eq($T1, Int) eq($T2, ($T1) -> $T1) eq($T3,

    ($T1) -> $T2) eq($T1, Int) eq($T2, (Int) -> Int) eq($T3, (Int) -> $T2) eq($T1, Int) eq($T2, (Int) -> Int) eq($T3, (Int) -> (Int) -> Int) ·ͣ୯ҰԽͱ͸ɺܕม਺ʹର͢ΔಉҰ੍໿Λݟ͚ͭɺ ͦΕʹج͍ͮͯଞͷग़ݱΛஔ׵͢Δ͜ͱͰ͢ɻ ྫΛࣔ͠·͠ΐ͏ɻ !͸͡Ίʹɺͭͷܕม਺ͱͭͷಉҰ੍໿͕͋ͬͨͱ͠·͢ɻ ͜ͷ࣌఺Ͱ͸ɺ5ͷܕ͸۩ମతͳ*OUʹͳ͍ͬͯ·͕͢ɺ 5ͱ5͸ܕม਺ΛؚΜͰ͍ͯΑ͘Θ͔Γ·ͤΜɻ ͜ΕΛղ͖·͢ɻ !·ͣɺ5ͷFR੍໿͔Βɺ͜Ε͕*OUܕͩͱΘ͔Γ·͢ɻ !ͦ͜Ͱଞͷ੍໿ʹؚ·ΕΔ5Λɺ !*OUʹஔ׵͠·͢ɻ ͜Ε͕୯ҰԽͰ͢ɻ ͔͜͜Β͞Βʹ΋͏Ұ౓୯ҰԽͰ͖·͢ɻ !5ʹ͍ͭͯͷ੍໿ΛݟΔͱɺ*OUΛड͚औΓ*OUΛฦؔ͢਺ͩͱ෼͔Γ·͢ɻ !ͦ͜Ͱଞͷ੍໿ͷதʹग़ݱ͢Δ5Λɺ !ͦΕʹஔ׵͠·͢ɻ !݁Ռͱͯͭ͠ͷܕม਺ͷܕ͕શͯఆ·Γ·͢ɻ
  11. 4JNQMJGJDBUJPOPGFRVBMDPOTUSBJOUT eq(A?, B?) eq(A, B) 0QUJPOBMT eq((A) -> B, (C)

    -> D) eq(B, D) eq(A, C) 'VODUJPOT eq((A, B), (C, D)) eq(B, D) eq(A, C) 5VQMFT ࣍ʹɺ؆໿Խͱ͍͏ͷ͸ɺ͋Δ੍໿Λ΋ͬͱ୯७ͳ੍໿ʹஔ͖׵͑Δ͜ͱͰ ͢ɻ ྫͱͯ͠ɺಉҰ੍໿ͷ؆໿ԽΛݟͯΈ·͠ΐ͏ɻ !΋͠ɺ͋ΔಉҰ੍໿͕λϓϧಉ࢜Λؔ܎෇͚͍ͯΔ࣌͸ɺ ͜ΕΛͦΕͧΕͷཁૉ͝ͱͷಉҰ੍໿ʹ෼ղ͢Δ͜ͱ͕Ͱ͖·͢ɻ ! ଞʹ΋ؔ਺΍ΦϓγϣφϧͳͲʹରͯ͠؆໿Խ͕Ͱ͖·͢ɻ 4JNQMJGJDBUJPOPGGVODUJPODBMMJOHDPOTUSBJOUT call((A) -> B, C, D) eq(B, D) eq(A, C) 1BSBNFUFST 3FUVSOWBMVF ؔ਺ݺͼग़੍͠໿ͷ؆໿Խ΋ݟͯΈ·͠ΐ͏ɻ ܕ"Λड͚औͬͯܕ#Λฦؔ͢਺ʹରͯ͠ɺ ܕ$Λ౉ͨ݁͠Ռ͕ܕ%Ͱ͋Δɺ ͱؔ܎෇͚ΒΕ͍ͯͨͱ͠·͢ɻ ͦͷ৔߹ɺ͜ΕΛܕ"ͱܕ$ͷಉҰ੍໿ͱɺܕ#ͱܕ%ͷಉҰ੍໿ʹม׵Ͱ͖·
  12. ͢ɻ 5IFJOUFSQMBZCFUXFFOVOJGJDBUJPOBOETJNQMJGJDBUJPO call($T1, $T2, $T3) eq($T1, (Int) -> Bool) call((Int)

    -> Bool, $T2, $T3) eq($T1, (Int) -> Bool) eq(Int, $T2) eq($T1, (Int) -> Bool) eq(Bool, $T3) ͦΕͰ͸ɺ୯ҰԽͱ؆໿ԽΛ૊Έ߹Θͤͯɺܕਪ࿦Λ͢ΔྫΛࣔ͠·͠ΐ͏ɻ !·ͣॳΊʹɺؔ਺ݺͼग़੍͠໿ͱಉҰ੍໿͕͋ͬͨͱ͠·͢ɻ !ؔ਺ݺͼग़੍͠໿ΛΈͯΈΔͱɺ ݺͼग़͞ΕΔࣜͷܕ͕ܕม਺ʹͳ͍ͬͯͯɺ ઌ΄Ͳઆ໌ͨ͠Α͏ͳؔ਺ͷܗʹͳ͍ͬͯͳ͍ͨΊɺ ͜Ε͸؆໿ԽͰ͖·ͤΜɻ !ͱ͜Ζ͕ɺ΋͏ҰͭͷಉҰ੍໿͸؆୯ͳܗʹͳ͍ͬͯΔͷͰɺ !୯ҰԽ͢Δ͜ͱ͕Ͱ͖·͢ɻ !୯ҰԽ͢Δͱࠓ౓͸ɺؔ਺ݺͼग़੍͠໿͕ؔ਺ͷܗʹͳ͍ͬͯΔͷͰɺ؆໿ Խ͢Δ͜ͱ͕Ͱ͖·͢ɻ !
  13. !࠷ऴతʹͭͷܕม਺ͷܕ͕֬ఆ͠·͢ɻ ͜ΕͰਪ࿦͕׬ྃͰ͢ɻ ! &YBNQMFPG*OGFSFODF 1SPCMFN4UBUFNFOU func id<X>(_ x: X) ->

    X func main(i: Int) { let a = id(i) } WBSEFDMB DBMM EFDMSFGJE EFDMSFGJ $T1 $T2 ($T3) -> $T3 Int eq($T1, $T2) call(($T3) -> $T3, Int, $T2) ͦΕͰ͸ɺॳΊʹग़ͨ͠ίʔυͷਪ࿦ΛΈ͍͖ͯ·͠ΐ͏ɻ ίʔυͱɺ"45ͱɺੜ੒੍ͨ͠໿Λ࠶ܝ͠·͢ɻ ͔͜͜Β͸ɺ੍໿͚ͩΛѻ͍·͢ɻ &YBNQMFPG*OGFSFODF eq($T1, $T2) call(($T3) -> $T3, Int, $T2) eq($T1, $T2) eq($T3, Int) eq($T3, $T2) eq($T1, $T2) eq($T3, Int) eq(Int, $T2) eq($T1, Int) eq($T3, Int) eq(Int, $T2) !·ͣɺؔ਺ݺͼग़੍͠໿ΛΈͯΈ·͢ɻ ݺͼग़͞ΕΔؔ਺ͷܕʹܕม਺ؚ͕·Ε͍ͯ·͕͢ɺؔ਺ͷܗ͸͍ͯ͠ΔͷͰ !؆໿ԽͰ͖·͢ɻ !࣍ʹɺ5ͷܕ͕Θ͔ͬͨͷͰɺ !͜ΕͰ୯ҰԽͰ͖·͢ɻ
  14. !͜ΕͰ5ͷܕ͕Θ͔ͬͨͷͰɺ !୯ҰԽ͠·͢ɻ !͜ΕͰશ෦ͷܕม਺ͷܕ͕ղ͚·ͨ͠ɻ ! eq($T3, Int) 7FSJGJDBUJPOPG*OGFSFODF3FTVMUT eq($T1, Int) eq(Int,

    $T2) WBSEFDMB DBMM EFDMSFGJE EFDMSFGJ $T1 $T2 ($T3) -> $T3 Int Int Int (Int) -> Int func id<X>(_ x: X) -> X func main(i: Int) { let a = id(i) } ܕਪ࿦͕ͪΌΜͱղ͚͍ͯΔ͜ͱΛ֬ೝ͠·͢ɻ ͭͷܕม਺͸ݩʑ"45ʹׂΓ౰ͯΒΕ͍ͯΔͷͰɺ ίʔυͷରԠ͢ΔҐஔͷܕΛҙຯ͍ͯ͠·͢ɻ !5͸ !WBSEFDMϊʔυͷܕͳͷͰɺ !B͕*OUͩͱ͍͏ࣄͰ͢ɻ !5͸ !DBMMϊʔυͷܕͳͷͰɺ !JEΛݺͼग़ͨ͠ฦΓ஋͸*OUͩͱ͍͏ࣄͰ͢ɻ !5ʹΑΕ͹ɺ !JEؔ਺ͷδΣωϦοΫύϥϝʔλ9͕ɺ !*OUͩͱ͍͏ࣄͰ͢ɻ
  15. !͜ͷΑ͏ʹίʔυͷܕ͕͢΂ͯ൑໌͍ͯ͠Δ͜ͱ͕Θ͔Γ·͢ɻ $PNQMFUJPOPG5ZQF*OGFSFODF w "MMUZQFWBSJBCMFTUZQFTBSFEFUFSNJOFEˠ5ZQFJOGFSFODFTVDDFTTGVM w $POUSBEJDUJPOPDDVSTˠ5IFDPEFJTJODPSSFDU w 6OLOPXOUZQFWBSJBCMFTSFNBJO CVUJOGFSFODFDBOOPUQSPDFFEˠ5IF DPEFTUZQFJTBNCJHVPVT

    ܕਪ࿦ͷऴྃʹ͍ͭͯઆ໌͠·͢ɻ શͯͷܕม਺ͷܕ͕֬ఆͨ͠Βɺܕਪ࿦੒ޭͰ͢ɻ ్தͰҟͳΔܕಉ࢜ͷಉҰ੍໿͕ੜ͡ΔͳͲໃ६͕ൃੜͨ͠Βɺ ͦͷίʔυ͸ܕ͕ؒҧ͍ͬͯΔࣄ͕൑໌͠·͢ɻ ෆ໌ͳܕม਺͕࢒͍ͬͯΔ͕ɺͦΕҎ্୯ҰԽ΋؆໿Խ΋Ͱ͖ͳ͘ͳͬͯ͠· ͏͜ͱ͕͋Γ·͢ɻ ͜Ε͸ɺίʔυͷܕ͕ᐆດͰ͋Δͱ͍͏͜ͱʹͳΓ·͢ɻ ͜ͷΑ͏ʹɺܕਪ࿦͕ऴྃ͢Δͱ͖ɺ༷ʑͳ݁Ռ͕ಘΒΕ·͢ɻ
  16. #JEJSFDUJPOBMJUZ func readInt() -> Int func read<X>() -> X func

    main() { let a = readInt() let b: Int = read() } eq($T1, $T2) eq(Int, $T3) call(() -> $T4, $T4, $T3) call(() -> Int, (), $T2) ࿩ͷ࢝Ίʹɺਪ࿦ʹ͸૒ํ޲ੑ͕ඞཁͩͱݴ͍·ͨ͠ɻ ͦΕ͕ຬͨ͞Ε͍ͯΔ͜ͱΛ֬ೝ͠·͠ΐ͏ɻ !Bͷࣜ͸ॱํ޲ͷਪ࿦͕ඞཁͰɺ্ʹඳ͍ͨͭͷ੍໿Λੜ੒͠·͢ɻ !Cͷࣜ͸ٯํ޲ͷਪ࿦͕ඞཁͰɺԼʹඳ͍ͨͭͷ੍໿Λੜ੒͠·͢ɻ ! ͲͪΒ΋ಉ͡Α͏ʹ୯ҰԽͱ؆໿ԽͰղ͘͜ͱ͕Ͱ͖·͢ɻ ੍໿ू߹ʹม׵ͨ࣌͠఺Ͱɺ ίʔυͷํ޲ͱ͸ແؔ܎ʹͳ͍ͬͯΔͨΊɺ ࣗવͱ૒ํ޲ੑ͕࣮ݱͰ͖͍ͯΔͷͰ͢ɻ &YUFOTJPOTGPS4XJGU ͜͜·Ͱ͸جຊతͳܕਪ࿦ͷઆ໌Ͱͨ͠ɻ ͔͜͠͠Ε͚ͩͰ͸ɺ4XJGUͷݴޠػೳʹରԠ͖͠Ε·ͤΜɻ ͦͷͨΊʹɺܕਪ࿦΋֦ு͢Δඞཁ͕͋Γ·͢ɻ
  17. *OUSPEVDUJPOPGDPOWFSTJPOSFMBUJPOTBOENPEJGJDBUJPOPGBTTJHONFOU TUBUFNFOUDPOTUSBJOUT w 4XJGUIBTJNQMJDJUUZQFDPOWFSTJPOT func main(i: Int) { let a:

    Int? = i } eq(Int?, Int) conv(Int, Int?) ❌ ✅ ͜͜·Ͱ҉໧ͷܕม׵Λແࢹ͍ͯ͠·ͨ͠ɻ ͔࣮͠͠͸ͦΕͰ͸͜ͷྫͷΑ͏ͳ؆୯ͳ୅ೖจ͢Βѻ͑·ͤΜɻ *OUͱ*OU ͷΑ͏ʹ྆ลͷܕ͕ҟͳΔ৔߹ʹ͸ɺಉҰ੍໿Λ࡞ͬͨ࣌఺Ͱໃ६͠ ͯ͠·͍·͢ɻ ͦ͜Ͱɺ҉໧ͷܕม׵ʹରԠ͢ΔͨΊʹɺ ৽ͨʹม׵੍໿Λಋೖ͠·͢ɻ ม׵੍໿͸ɺҰͭ໨ͷܕΛೋͭ໨ͷܕʹม׵Ͱ͖ΔࣄΛҙຯ͠·͢ɻ ͜ͷྫͷΑ͏ʹɺ୅ೖจ͔Β͸ม׵੍໿Λੜ੒͢ΔΑ͏ʹमਖ਼͠·͢ɻ 4JNQMJGJDBUJPOPG$POWFSTJPO$POTUSBJOUT conv((A) -> B, (C) -> D) conv(B, D) conv(C, A) 1BSBNFUFST 🙃DPOUSBWBSJBODF 3FUVSOWBMVFT 🙂DPWBSJBODF ม׵੍໿΋ಉҰ੍໿ͱࣅͨΑ͏ͳ؆໿Խ͕Ͱ͖·͢ɻ ྫ͑͹ɺؔ਺ಉ࢜ͷม׵੍໿͸Ҿ਺ͱฦΓ஋ͷؔ܎ʹ෼ղͰ͖·͢ɻ
  18. .PEJGJDBUJPOPGTJNQMJGJDBUJPOPGGVODUJPODBMMJOHDPOTUSBJOUT call((A) -> B, C, D) eq(B, D) conv(C, A)

    1BSBNFUFST 🙃 3FUVSOWBMVFT ؔ਺ݺͼग़ؔ͠܎ͷ؆໿Խ΋গ͠มΘΓ·͢ɻ Ҿ਺ʹ͍ͭͯม׵੍໿Λੜ੒͠·͢ɻ 1SPCMFNPG$POWFSTJPO$POTUSBJOUT func main(i: Int) { let a = i } conv(Int, $T1) 🤷 eq($T1, Int) eq($T1, Int?) eq($T1, Any) 0, 0, 0, ͯ͞ɺม׵੍໿ʹ͸େ͖ͳ໰୊͕͋Γ·͢ɻ ؆໿ԽΛਐΊͯɺܕม਺ͱܕͷ୯७ͳؔ܎·Ͱ౸ୡͯ͠΋ɺ ͦΕΛ୯ҰԽʹ࢖͏͜ͱ͕Ͱ͖ͳ͍ͷͰ͢ɻ ͜ͷྫͰݴ͑͹ɺ 5͸*OU͔Βม׵Ͱ͖ΔܕͩͱΘ͔͍ͬͯ·͕͢ɺ ۩ମత͸֬ఆ͍ͯ͠·ͤΜɻ *OU͔΋͠Εͳ͍͠ɺ*OU ͔΋͠Ε·ͤΜɻ"OZͰ΋໰୊͸͋Γ·ͤΜɻ Ͳ͏΍ͬͯਪ࿦ΛਐΊΕ͹ྑ͍ͷͰ͠ΐ͏͔ɻ
  19. (FOFSBUJPOBOE"UUFNQUPG6OJGJDBUJPO$BOEJEBUFT conv(Int, $T1) eq($T1, Int) QPTTJCJMF func main(i: Int) {

    let a = i } func main(i: Int) { let a: Int = i } ͜ͷΑ͏ͳ৔໘Ͱɺܕਪ࿦ث͸୯ҰԽީิͷੜ੒ͱࢼߦΛߦ͍·͢ɻ ཁ͢Δʹɺ౴͑ͷԾઆΛཱͯͯࢼ͠ʹղ͍ͯΈΔͷͰ͢ɻ ͦͯ͠ਪ࿦ΛਐΊͯɺ࠷ऴతʹ੒ޭ͢Ε͹ͦΕΛղͱͯ͠͠·͍·͢ɻ ྫ͑͹ม׵੍໿͕୯७ͳܗʹͳͬͨΒɺͦΕΛࢼ͠ʹಉҰ੍໿ʹஔ͖׵͑ͯ͠ ·͍·͢ɻ ͜ͷྫͰ͋Ε͹ɺ5Λ*OUʹԾఆͯ͠Έ·͢ɻ ͦͷ݁Ռͱͯ͠ಛʹ໰୊͸ى͜Βͳ͍ͷͰɺB͕*OUͰ͋Δͱ݁࿦͠·͢ɻ &YBNQMFPG$BOEJEBUF4FBSDI conv($T1, Int?) conv(Int?, $T2) eq($T1, $T2) conv(Int, $T1) eq($T1, Int) eq($T1, Int?) eq($T2, Int?) ΋ͪΖΜɺ͜ͷΑ͏ͳީิ͕͍ͭͰ΋͏·͍͘͘ͱ͸ݶΓ·ͤΜɻ ͜ͷΑ͏ͳͭͷ੍໿Λղ͘৔߹Λߟ͑ͯΈ·͠ΐ͏ɻ 5ʹ͍ͭͯ͸ͭɺ5ʹ͍ͭͯ͸ͭͷ୯ҰԽީิ͕ੜ੒Ͱ͖·͢ɻ
  20. &YBNQMFPG4FBSDI1MBO 4UBSU 5*OU 5*OU 5*OU 5*OU ͜ͷͭͷީิ͔Βɺ͜ͷΑ͏ͳ୳ࡧܭը͕ੜ͡·͢ɻ ॱ൪ʹݟͯΈ·͠ΐ͏ɻ 4UBSU 5*OU

    5*OU 5*OU 5*OU conv($T1, Int?) conv(Int?, $T2) eq($T1, $T2) eq($T1, Int) conv(Int, Int?) conv(Int?, $T2) eq(Int, $T2) eq($T1, Int) conv(Int, Int?) conv(Int?, Int) eq(Int, $T2) eq($T1, Int) !Ծઆͱͯ͠ɺ5Λ*OUʹͯ͠Έ·͢ɻ !ͦͷಉҰ੍໿͔Β୯ҰԽͰ͖·͢ɻ !͢Δͱɺࠓ౓͸5ͷಉҰ੍໿͕Ͱ͖·ͨ͠ɻ !͞Βʹ୯ҰԽͯ͠Έ·͠ΐ͏ɻ !͢ΔͱͲ͏Ͱ͠ΐ͏ɺໃ६ͨ͠ม׵੍໿͕ग़ݱ͠·ͨ͠ɻ *OU ͔Β*OUʹ͸ม׵Ͱ͖·ͤΜɻ Αͬͯɺ͜ͷԾઆ͸ؒҧ͍ͩͬͨ͜ͱ͕Θ͔Γ·͢ɻ
  21. 4UBSU 5*OU 5*OU 5*OU 5*OU ❌ eq($T1, Int?) conv(Int?, $T2)

    eq($T1, $T2) conv(Int, $T1) eq($T1, Int?) conv(Int?, $T2) eq(Int?, $T2) conv(Int, Int?) eq($T1, Int?) conv(Int?, Int?) eq(Int?, $T2) conv(Int, Int?) !ࠓ౓͸5Λ*OU ʹͯ͠Έ·͢ɻ !͜ͷಉҰ੍໿͔Β୯ҰԽ͕Ͱ͖·͢ɻ !5ͷಉҰ੍໿͕ग़ͯ͘ΔͷͰɺ !࠶ͼ୯ҰԽ͠·͢ɻ !͢Δͱɺશͯͷ੍໿͕੒ཱ͠ɺܕ͕ղ͚·ͨ͠ɻ )BOEMJOH0WFSMPBET func foo() -> Bool func foo() -> Int func foo() -> String func main(i: Int) { let a: Int = foo() } call($T1, (), Int) eq($T1, () -> Bool) eq($T1, () -> Int) eq($T1, () -> String) QPTTJCJMFY ͜ͷΑ͏ͳํ๏ʹΑͬͯɺΦʔόʔϩʔυʹ΋ରԠ͢Δ͜ͱ͕Ͱ͖·͢ɻ !DBMM੍໿Λੜ੒͢Δͱ͖ʹɺ ͦͷݺͼग़͠ର৅͕Φʔόʔϩʔυ͞Ε͍ͯͨΒɺ ؔ਺ͷܕΛ͍ͬͨΜܕม਺ʹ͓͖·͢ɻ !ͦͯ͠ɺΦʔόʔϩʔυ͞Ε͍ͯΔؔ਺Λͦͷܕม਺ͷ୯ҰԽީิͱ͢ΔͷͰ ͢ɻ
  22. "UUFNQUTBOE$PNQVUBUJPOBM$PNQMFYJUZ w 5IFNFDIBOJTNPGlBUUFNQUDBOEJEBUFTzIBTUIFQPUFOUJBMUPFYQMPEF JODPNQVUBUJPOBMDPNQMFYJUZ 💣 code.swift:11:9: error: the compiler is

    unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions Ծઆͱͯ͠ީิΛཱͯͯɺͦΕΛࢼ͢ͱ͍͏ܕਪ࿦ͷ֦ுʹ͍ͭͯઆ໌͠·͠ ͨɻ ͜ͷΑ͏ͳॲཧ͸ɺঢ়گʹΑͬͯ͸ɺܭࢉྔ͕രൃతʹ૿Ճͯ͠͠·͏͜ͱ͕ ͋Γ·͢ɻ ͜ͷΑ͏ͳίϯύΠϧΤϥʔϝοηʔδ͕දࣔ͞ΕΔ৔߹ɺͦΕ͕ؔ܎͍ͯ͠Δ ࣄ͕ଟ͍Ͱ͢ɻ -FBSOJOHGSPNUIF$PNQJMFS ͜͜·Ͱɺ4XJGUʹରԠ͢ΔͨΊʹܕਪ࿦Λ֦ு͢Δํ๏Λઆ໌͠·ͨ͠ɻ ࠷ޙʹɺܕਪ࿦ͷಈ࡞ΛίϯύΠϥ͔Β௚઀ֶͿํ๏Λઆ໌͠·͢ɻ .BLJOHUIF$PNQJMFS0VUQVUUIF0QFSBUJPOTPG5ZQF*OGFSFODF $ swiftc -Xfrontend -debug-constraints code.swift func foo() -> Int { 0 } func foo() -> Bool { true } func main() { let a: Int = foo() } 0VUQVUFYDFFEJOHMJOFT🤯 4XJGUίϯύΠϥʹ͸ɺܕਪ࿦ͷಈ࡞Λग़ྗͤ͞Δػೳ͕͋Γ·͢ɻ ͦΕʹ͸ίϚϯυϥΠϯͰɺ9GSPOUFOEͱEFCVHDPOTUSBJOUTͱ͍͏Φϓγϣ ϯΛ༩͑·͢ɻ ྫ͑͹͜ͷίʔυΛೖྗ͢ΔͱߦΛ௒͑Δग़ྗ͕ൃੜ͠·͢ɻ ͜ͷ಺༰ͷಡΈղ͖ํΛઆ໌͠·͢ɻ
  23. *OGFSFODFQSPDFTT func foo() -> Int { 0 } func foo()

    -> Bool { true } func main() { let a: Int = foo() } 4UBSU 5*OU 5 *OU 5 #PPM 4VDDFTT 'BJMVSF ग़ྗΛݟΔલʹɺ͜ͷίʔυͷਪ࿦खॱΛઆ໌͓͖ͯ͠·͢ɻ !Bʹ͍ͭͯͷ୅ೖจ͕͋ΔͨΊɺม׵੍໿͕ੜ͡·͢ɻ !͜Εʹ*OUͷܕ͕ࢦఆ͞Ε͍ͯΔͨΊɺީิͱͯ͠*OU͕ࢼߦ͞Ε·͢ɻ !·ͨɺGPPʹ͍ͭͯͷΦʔόʔϩʔυ͕͋ΔͨΊɺ !ͦΕͷީิ͕ࢼߦ͞Ε·͢ɻ !͢Δͱɺ*OUͷํ͸੒ޭ͠ɺ !#PPMͷํ͸ࣦഊ͢ΔͷͰɺ ࠷ऴతʹղ͕֬ఆ͠·͢ɻ ! $ swiftc -Xfrontend -debug-constraints code.swift ---Constraint solving at [code.swift:1:21 - line:1:21]--- (…) ---Constraint solving at [code.swift:2:22 - line:2:22]--- (…) ---Constraint solving at [code.swift:5:9 - line:5:22]--- (…) func foo() -> Int { 0 } func foo() -> Bool { true } func main() { let a: Int = foo() } 0VUQVUGPSUIF&OUJSF$PEF ͦΕͰ͸ίϯύΠϥͷग़ྗΛݟ͍͖ͯ·͠ΐ͏ɻ  !ग़ྗ͸͜ͷΑ͏ͳݟग़͠Ͱେ·͔ʹ۠੾ΒΕ͍ͯ·͢ɻ ܕਪ࿦͸ίʔυશମΛҰ౓ʹղ͘Θ͚Ͱ͸ͳ͘ɺ ࣜ͝ͱʹॲཧ͢ΔͨΊɺ ҰͭͷίʔυͷதͰԿճ΋࣮ߦ͞ΕΔ͔ΒͰ͢ɻ
  24. !ࠓ͸BͷॳظԽจΛௐ΂͍ͨͷͰɺ ͜ͷ෦෼ͷߦ൪߸ͷදࣔΛݟͯɺ ͭ໨ͷηΫγϣϯΛݟ͍͖ͯ·͢ɻ 0VUQVUGPS*OEJWJEVBM*OGFSFODFT ---Constraint solving at [code.swift:5:9 - line:5:22]---

    ---Initial constraints for the given expression--- (Initial State of the AST) (Initial State of the Constraints) (Inference Process) ---Solution--- (Solution) ---Type-checked expression--- (Typed AST) ݸผͷਪ࿦ͷग़ྗͷߏ଄Λࣔ͠·͢ɻ ·ͣɺ*OJUJBMDPOTUSBJOUTGPSUIFHJWFOFYQSFTTJPOͱ͍͏ݟग़͕͋͠Γɺ ͦ͜ʹ"45ͷॳظঢ়ଶɺ੍໿ͷॳظঢ়ଶ͕ଓ͖·͢ɻ ͦͷ··ɺਪ࿦աఔͷग़ྗ͕ଓ͖·͢ɻ ͦͷޙΖʹɺ4PMVUJPOͱ͍͏ݟग़͕͋͠Γɺਪ࿦ͷղ͕දࣔ͞Ε·͢ɻ ࠷ޙʹɺ5ZQFDIFDLFEFYQSFTTJPOͱ͍͏ݟग़͕͋͠Γɺ ܕ෇͚͞Εͨ"45͕ग़ྗ͞Ε·͢ɻ *OJUJBM4UBUFPGUIF"45 ---Initial constraints for the given expression— (call_expr type='$T1' location=code.swift:5:18 range=[code.swift:5:18 - line:5:22] isolationCrossing=none (overloaded_decl_ref_expr type='$T0' location=code.swift:5:18 range=[code.swift:5:18 - line:5:18] name=foo number_of_decls=2 function_ref=single decls=[ code.(file).foo()@code.swift:1:6, code.(file).foo()@code.swift:2:6]) (argument_list)) let a: Int = foo() DBMM EFDMSFGJE $T0 $T1 ·ͣ͸"45ͷॳظঢ়ଶͰ͢ɻ "45ͷϊʔυߏ଄͸ؙׅހΛ࢖ͬͯදݱ͞Ε·͢ɻ "45ͱίʔυͷҐஔͱͷରԠؔ܎͸ɺMPDBUJPO΍SBOHFͷ৘ใ͔ΒΘ͔Γ·͢ɻ !·ͣɺGPPͷݺͼग़͠ʹରԠ͢ΔDBMM@FYQSϊʔυ͕͋Γ·͢ɻ ͜ͷࣜͷܕ͕5ͱ͍͏ܕม਺ʹׂΓ౰ͯΒΕ͍ͯ·͢ɻ
  25. !·ͨɺGPPͷࢀরʹରԠ͢ΔɺPWFSMPBEFE@EFDM@SFG@FYQSϊʔυ͕͋Γ· ͢ɻ ͜ͷࣜͷܕ͸5ͱ͍͏ܕม਺ʹׂΓ౰ͯΒΕ͍ͯΔ͜ͱ͕Θ͔Γ·͢ɻ *OJUJBM4UBUFPGUIF$POTUSBJOUT Score: <default 0 0 0 0

    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0> Contextual Type: Int at [code.swift:5:12 - line:5:12] Type Variables: $T0 [allows bindings to: lvalue, noescape] [attributes: delayed] [with possible bindings: <empty>] @ locator@0x11b81ac00 [[email protected]:5:18] $T1 [allows bindings to: noescape] [attributes: delayed] [with possible bindings: (subtypes of) Int] @ locator@0x11b81adb0 [[email protected]:5:18 → function result] $T2 as Int @ locator@0x11b81aef0 [[email protected]:5:18 → contextual type → pattern match → pattern match → named pattern decl] Inactive Constraints: disjunction @ locator@0x11b81ac00 [[email protected]:5:18]: > $T0 bound to decl code.(file).foo()@code.swift:1:6 : () -> Int > $T0 bound to decl code.(file).foo()@code.swift:2:6 : () -> Bool () -> $T1 applicable fn $T0 @ locator@0x11b81ae30 [[email protected]:5:18 → apply function] $T1 conv Int @ locator@0x11b81aec8 [[email protected]:5:18 → contextual type] ੍໿ͷॳظঢ়ଶΛݟ͍͖ͯ·͠ΐ͏ɻ ͨ͘͞Μͷ৘ใ͕͋ΔͷͰɺॏཁͳͱ͜ΖΛൈਮ͠·͢ɻ !·ͣɺ$POUFYUVBM5ZQFͰ͢ɻ ͜Ε͸ࠓղ͜͏ͱ͍ͯ͠Δࣜʹ͔͔͍ͬͯΔࠨลͷܕͰ͢ɻ ࠓճͷίʔυͰ͸ɺࠨลͷม਺ʹ*OU͕ܕࢦఆ͞Ε͍ͯΔͷͰɺͦͷ৘ใ͕දࣔ ͞Ε͍ͯ·͢ɻ !࣍ʹɺ5ZQF7BSJBCMFTͰ͢ɻ ͜ͷηΫγϣϯʹɺܕม਺ͷҰཡ͕දࣔ͞Ε·͢ɻ !ઌ΄Ͳ"45ʹׂΓ౰ͯΒΕ͍ͯͨɺ5ͱ5͕දࣔ͞Ε͍ͯ·͢ɻ !·ͨɺઌ΄Ͳઆ໌ͨ͠$POUFYUVBM5ZQF͕5ͱͯ͠දࣔ͞Ε͍ͯ·͢ɻ ͜Ε͸طʹܕ͕*OUʹ֬ఆͨ͠ঢ়ଶͰ͢ɻ !࣍ʹɺ*OBDUJWF$POTUSBJOUTͰ͢ɻ ͜Ε੍͕໿ͷू߹ͷ͜ͱͰ͢ɻ
  26. !·ͣɺEJTKVODUJPO͕͋Γ·͢ɻ ͜Ε͸GPPͷΦʔόʔϩʔυΛબ୒͢ΔλεΫ੍͕໿ͱͯ͠දݱ͞Ε͍ͯ·͢ɻ !EJTKVODUJPO੍໿ͷ෇ଐ৘ใͱͯ͠ɺ ͦͷީิͷΦʔόʔϩʔυؚ͕ͭ·Ε͍ͯ·͢ɻ ͜Ε͸GPPͷܕΛද͢ܕม਺5ʹର͢ΔׂΓ౰੍ͯ໿ͱͯ͠දݱ͞Ε͍ͯ·͢ɻ !࣍͸BQQMJDBCMFGO੍໿Ͱ͢ɻ ͜͜·Ͱ͸DBMM੍໿ͱͯ͠આ໌͍ͯͨ͠΋ͷͰɺ ܕม਺5Λݺͼग़͢ͱܕม਺5͕ಘΒΕΔͱ͍͏ࣄΛҙຯ͍ͯ͠·͢ɻ !࠷ޙʹDPOW੍໿Ͱ͢ɻ 5͕*OUʹม׵Ͱ͖Δͱ͍͏ࣄͰ͢ɻ

    *OGFSFODF1SPDFTT (attempting type variable binding $T1 := Int ... (attempting disjunction choice $T0 bound to decl code. (file).foo()@code.swift:1:6 : () -> Int @ locator@0x11b81ac00 [[email protected]:5:18] ... (found solution: <default 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0>) ) (attempting disjunction choice $T0 bound to decl code. (file).foo()@code.swift:2:6 : () -> Bool @ locator@0x11b81ac00 [[email protected]:5:18] ... (outcome: error) ) ) 4UBSU 5*OU 5 *OU 5 #PPM 4VDDFTT 'BJMVSF ࣍ʹɺਪ࿦աఔΛݟ͍͖ͯ·͠ΐ͏ɻ ߦ͕ଟ͍ͷͰॏཁͳͱ͜ΖΛൈਮ͍ͯ͠·͢ɻ !·ͣɺ๯಄ʹBUUFNQUJOHUZQFWBSJBCMFCJOEJOHͱॻ͍ͯ͋Γ·͢ɻ ͜͜Ͱɺ5Λ*OUʹ͢ΔࣄΛࢼߦ͍ͯ͠·͢ɻ !ͦͷதʹωετ͞Εͨߏ଄ͱͯ͠ɺ
  27. BUUFNQUJOHEJTKVODUJPODIPJDF͕ͭ͋Γ·͢ɻ ͜Ε͸ɺͭͷΦʔόʔϩʔυͷͦΕͧΕΛࢼߦ͍ͯ͠Δ෼ذΛҙຯ͍ͯ͠· ͢ɻ !*OUΛฦ͢GPPΛࢼߦͨ͠ύλʔϯʹ͍ͭͯɺ ωετ͞Εͨग़ྗͷதʹGPVOETPMVUJPO͕͋Γ·͢ɻ ͜Ε͕ɺਪ࿦͕੒ޭͨ͠ࣄΛ͍ࣔͯ͠·͢ɻ 4PMVUJPO ---Solution--- Fixed score:

    <default 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0> Type variables: $T0 as () -> Int @ locator@0x11b81ac00 [[email protected]:5:18] $T1 as Int @ locator@0x11b81adb0 [[email protected]:5:18 → function result] $T2 as Int @ locator@0x11b81aef0 [[email protected]:5:18 → contextual type → pattern match → pattern match → named pattern decl] Overload choices: locator@0x11b81ac00 [[email protected]:5:18] with code. (file).foo()@code.swift:1:6 as foo: () -> Int Trailing closure matching: locator@0x11b81afe8 [[email protected]:5:18 → apply argument]: forward ղͷग़ྗ෦෼Ͱ͢ɻ ੍໿ͷॳظঢ়ଶͱࣅͨΑ͏ͳܗࣜͰදࣔ͞Ε͍ͯ·͢ɻ !ܕม਺5ɺ5ɺ5ͷܕ͕දࣔ͞Εɺ ܕਪ࿦͕׬͍ྃͯ͠Δ͜ͱ͕Θ͔Γ·͢ɻ 5ZQFE"45 ---Type-checked expression--- (call_expr type=‘Int' location=code.swift:5:18 range=[code.swift:5:18 - line:5:22] isolationCrossing=none (declref_expr type='() -> Int’ location=code.swift:5:18 range=[code.swift:5:18 - line:5:18] decl=code.(file).foo()@code.swift:1:6 function_ref=single) (argument_list)) let a: Int = foo() DBMM EFDMSFGJE () -> Int Int ࠷ޙʹɺܕ෇͚͞Εͨ"45ͷग़ྗͰ͢ɻ !DBMM@FYQSͷܕม਺͸ஔ׵͞Εͯɺ۩ମతͳ*OUܕʹͳ͍ͬͯ·͢ɻ !·ͨɺPWFSMPBEFE@EFDM@SFG@FYQSϊʔυ͸EFDMSFG@FYQSϊʔυʹஔ׵͞Εɺ ܕ΋*OUΛฦؔ͢਺ʹͳ͍ͬͯ·͢ɻ !
  28. Ҏ্ͰίϯύΠϥͷग़ྗͷઆ໌͸ऴΘΓͰ͢ɻ օ͞Μ΋༷ʑͳίʔυͷܕਪ࿦ͷಈ࡞Λௐ΂ͯΈ͍ͯͩ͘͞ɻ 3FDBQ w 5IFCBTJDTPGUZQFJOGFSFODFBSFVOJ DBUJPOBOETJNQMJ DBUJPOPG DPOTUSBJOUT w 4XJGUFYUFOETJUCZBEEJOHTFBSDIUPUIFQSPDFTT

    w 8FDBOVOEFSTUBOEJUTPQFSBUJPOTCZMPPLJOHBUUIFDPNQJMFSTPVUQVU ·ͱΊ·͢ɻ ͸͡Ίʹܕਪ࿦ͷجຊΛઆ໌͠·ͨ͠ɻ ͦΕ͸ɺ੍໿ू߹ʹର͢Δ୯ҰԽͱ؆໿Խͷ܁Γฦ͠ͷखଓ͖Ͱͨ͠ɻ ͦͯ͠ɺ4XJGUʹରԠ͢ΔͨΊͷ֦ுͱͯ͠ɺ୳ࡧΛՃ͑ΔࣄΛઆ໌͠·ͨ͠ɻ ࠷ޙʹɺίϯύΠϥͷग़ྗΛݟͯͦͷಈ࡞Λ஌Δํ๏Λઆ໌͠·ͨ͠ɻ /FYU4UFQT w JNQMFNFOUJOHUZQFJOGFSFODF https://stackoverflow.com/questions/415532/implementing-type- inference w 5ZQF$IFDLFS%FTJHOBOE*NQMFNFOUBUJPO https://github.com/apple/swift/blob/main/docs/TypeChecker.md w 4XJGU5ZQF*OGFSFODF)BOET0O https://github.com/omochi/SwiftTypeInferenceHandsOn w $PNQJMJOH4XJGUHFOFSJDT 1BSU* https://forums.swift.org/t/compiling-swift-generics-part-i/60898 ࠷ޙʹɺ͞Βʹৄֶ͘͠ͿͨΊͷ৘ใΛ঺հ͠·͢ɻ ͭ໨ͷϦϯΫઌͰ͸ɺܕਪ࿦Λֶश͢ΔͨΊͷࢿྉ͕ଟ͘঺հ͞Ε͍ͯ·͢ɻ ࢲ΋͜ΕΛಡΈ·ͨ͠ɻ ͭ໨ͷϦϯΫઌ͸ɺ4XJGUίϯύΠϥͷܕਪ࿦ʹ͍ͭͯͷެࣜͷจॻͰ͢ɻ ͭ໨ͷϦϯΫઌ͸ɺࢲ͕4XJGUͰ࣮૷ͨ͠ɺ4XJGUͷܕਪ࿦ͷίʔυͰ͢ɻ ͭ໨ͷϦϯΫઌ͸ɺ4XJGUίϯύΠϥ։ൃऀͷ4MBWB͞Μ͕ࣥචதͷɺܕγες