"CPVU.F
w *FOKPZSFBEJOHQSPQPTBMTPO4XJGU&WPMVUJPOBOE
CSPXTJOHUIF4XJGUP
ffi
DJBMGPSVNBTBIPCCZ
w *DPPSHBOJ[FTUVEZHSPVQTGPDVTFEPOUIF4XJGU
DPNQJMFS
w *BNSFDPHOJ[FEBTBDPNNJUUFSUPUIF4XJGU
DPNQJMFS
PNPDIJNFUBSV
Slide 3
Slide 3 text
5ZQF*OGFSFODF
w 0OFPG4XJGUTHSFBUFTUBQQFBMT
w 5IFDPFYJTUFODFPGTBGFUZBOEFBTFPGXSJUJOH
w 6OEFSTUBOEJOHUIF.JOETFUPGUIF$PNQJMFS
w *IBWFMPPLFEJOUPJUBOEJNQMFNFOUFEJUJOUIFQBTU
https://github.com/omochi/SwiftTypeInferenceHandsOn
w *IBWFDSFBUFEBTVJUBCMFFYQMBOBUJPOGPSCFHJOOFST
Slide 4
Slide 4 text
0VUMJOF
w #BTJD#JEJSFDUJPOBM*OGFSFODF"MHPSJUIN
w &YUFOTJPOTGPS4XJGU
w *NQMJDJU5ZQF$POWFSTJPO
w 0WFSMPBEJOH
w )PXUP*OWFTUJHBUF6TJOHUIF$PNQJMFS
Slide 5
Slide 5 text
#JEJSFDUJPOBM5ZQF*OGFSFODF
Slide 6
Slide 6 text
&YBNQMFPG'PSXBSE*OGFSFODF
func id(_ x: X) -> X
func main(i: Int) {
let a = id(i)
}
Slide 7
Slide 7 text
&YBNQMFPG'PSXBSE*OGFSFODF
func id(_ x: X) -> X
func main(i: Int) {
let a = id(i)
}
Slide 8
Slide 8 text
&YBNQMFPG'PSXBSE*OGFSFODF
func id(_ x: X) -> X
func main(i: Int) {
let a = id(i)
}
Slide 9
Slide 9 text
&YBNQMFPG'PSXBSE*OGFSFODF
func id(_ x: X) -> X
func main(i: Int) {
let a = id(i)
}
Slide 10
Slide 10 text
&YBNQMFPG'PSXBSE*OGFSFODF
func id(_ x: X) -> X
func main(i: Int) {
let a = id(i)
}
Slide 11
Slide 11 text
&YBNQMFPG3FWFSTF*OGFSFODF
func id(_ x: X) -> X
func main(i: Int) {
let a: Int = id(.init())
}
Slide 12
Slide 12 text
&YBNQMFPG3FWFSTF*OGFSFODF
func id(_ x: X) -> X
func main(i: Int) {
let a: Int = id(.init())
}
Slide 13
Slide 13 text
&YBNQMFPG3FWFSTF*OGFSFODF
func id(_ x: X) -> X
func main(i: Int) {
let a: Int = id(.init())
}
Slide 14
Slide 14 text
&YBNQMFPG3FWFSTF*OGFSFODF
func id(_ x: X) -> X
func main(i: Int) {
let a: Int = id(.init())
}
Slide 15
Slide 15 text
&YBNQMFPG3FWFSTF*OGFSFODF
func id(_ x: X) -> X
func main(i: Int) {
let a: Int = id(.init())
}
Slide 16
Slide 16 text
#BTJD5ZQF*OGFSFODF
Slide 17
Slide 17 text
"MHPSJUIN8
w "WPJEJOHGPSNBMEJTDVTTJPOT
*XJMMCSJF
fl
ZFYQMBJOUIF
fl
PXPG
PQFSBUJPOT
w ⚠1MFBTFGPSHFUBCPVUlJNQMJDJUUZQFJOGFSFODFzGPSBNPNFOU
Slide 18
Slide 18 text
5ZQF*OGFSFODF1SPDFTT0VUMJOF
w 1BSTJOH$PEFJOUPBO"45 "CTUSBDU4ZOUBY5SFF
w #JOEJOH5ZQF7BSJBCMFTUPUIF"45
w (FOFSBUJOH$POTUSBJOUTGSPN"45
w 3FQFBUFEMZ1FSGPSNJOH6OJ
fi
DBUJPOBOE4JNQMJ
fi
DBUJPO
Slide 19
Slide 19 text
"45 "CTUSBDU4ZOUBY5SFF
w "SFQSFTFOUBUJPOPGUIFDPEFJOBUSFFTUSVDUVSF
w 5IFDPNQJMFS
fi
OETJUFBTZUPIBOEMF
Slide 20
Slide 20 text
&YBNQMFPG"45
func id(_ x: X) -> X
func main(i: Int) {
let a = id(i)
}
WBSEFDMB
DBMM
EFDMSFGJE EFDMSFGJ
Slide 21
Slide 21 text
&YBNQMFPG"45
func id(_ x: X) -> X
func main(i: Int) {
let a = id(i)
}
WBSEFDMB
DBMM
EFDMSFGJE EFDMSFGJ
Slide 22
Slide 22 text
&YBNQMFPG"45
func id(_ x: X) -> X
func main(i: Int) {
let a = id(i)
}
WBSEFDMB
DBMM
EFDMSFGJE EFDMSFGJ
Slide 23
Slide 23 text
&YBNQMFPG"45
func id(_ x: X) -> X
func main(i: Int) {
let a = id(i)
}
WBSEFDMB
DBMM
EFDMSFGJE EFDMSFGJ
Slide 24
Slide 24 text
&YBNQMFPG"45
func id(_ x: X) -> X
func main(i: Int) {
let a = id(i)
}
WBSEFDMB
DBMM
EFDMSFGJE EFDMSFGJ
Slide 25
Slide 25 text
&YBNQMFPG"45
func id(_ x: X) -> X
func main(i: Int) {
let a = id(i)
}
WBSEFDMB
DBMM
EFDMSFGJE EFDMSFGJ
Slide 26
Slide 26 text
#JOEJOH5ZQF7BSJBCMFT
w 5ZQFWBSJBCMFTBSFWBSJBCMFTXIFSFUZQFTBSFBTTJHOFE
w #JOHJOHUZQFWBSJBCMFTUPUIFQBSUTPGUIF"45XIFSFUIFUZQFJTOPU
ZFULOPXO
w 5ZQFJOGFSFODFJTUIFUBTLPGEFUFSNJOJOHUIFUZQFTGPSBMMUZQF
WBSJBCMFT
Slide 27
Slide 27 text
&YBNQMFPG#JOEJOH5ZQF7BSJBCMFT
WBSEFDMB
DBMM
EFDMSFGJE EFDMSFGJ
$T1
$T2
($T3) -> $T3
Int
(X) -> X
func id(_ x: X) -> X
func main(i: Int) {
let a = id(i)
}
Slide 28
Slide 28 text
&YBNQMFPG#JOEJOH5ZQF7BSJBCMFT
WBSEFDMB
DBMM
EFDMSFGJE EFDMSFGJ
$T1
$T2
($T3) -> $T3
Int
(X) -> X
func id(_ x: X) -> X
func main(i: Int) {
let a = id(i)
}
Slide 29
Slide 29 text
&YBNQMFPG#JOEJOH5ZQF7BSJBCMFT
WBSEFDMB
DBMM
EFDMSFGJE EFDMSFGJ
$T1
$T2
($T3) -> $T3
Int
(X) -> X
func id(_ x: X) -> X
func main(i: Int) {
let a = id(i)
}
Slide 30
Slide 30 text
&YBNQMFPG#JOEJOH5ZQF7BSJBCMFT
WBSEFDMB
DBMM
EFDMSFGJE EFDMSFGJ
$T1
$T2
($T3) -> $T3
Int
(X) -> X
func id(_ x: X) -> X
func main(i: Int) {
let a = id(i)
}
Slide 31
Slide 31 text
&YBNQMFPG#JOEJOH5ZQF7BSJBCMFT
WBSEFDMB
DBMM
EFDMSFGJE EFDMSFGJ
$T1
$T2
($T3) -> $T3
Int
(X) -> X
func id(_ x: X) -> X
func main(i: Int) {
let a = id(i)
}
Slide 32
Slide 32 text
&YBNQMFPG#JOEJOH5ZQF7BSJBCMFT
WBSEFDMB
DBMM
EFDMSFGJE EFDMSFGJ
$T1
$T2
($T3) -> $T3
Int
(X) -> X
func id(_ x: X) -> X
func main(i: Int) {
let a = id(i)
}
Slide 33
Slide 33 text
(FOFSBUJOH$POTUSBJOUT
w *OEJDBUJOHUIFSFMBUJPOTIJQCFUXFFOUZQFTBOEUZQFWBSJBCMFT
w (FOFSBUJOHGSPN"45
w *OGFSFODFQSPHSFTTFTCZNBOJQVMBUJOHUIFTFUPGDPOTUSBJOUT
Slide 34
Slide 34 text
(FOFSBUJOH$POTUSBJOUTGSPN"TTJHONFOU4UBUFNFOU
WBSEFDM
init
A
B
eq(A, B)
Slide 35
Slide 35 text
(FOFSBUJOH$POTUSBJOUTGSPN'VODUJPODBMMJOH
DBMM
DBMMFF BSHT
R
F A
call(F, A, R)
*OGFSFODF
w 3FQFBUFEMZBQQMZJOHVOJ
fi
DBUJPOBOETJNQMJ
fi
DBUJPOUPUIFTFUPG
DPOTUSBJOUT
w 5IFQSPDFTTJTDPNQMFUFPODFUIFUZQFTPGBMMUZQFWBSJBCMFTIBWF
CFFOEFUFSNJOFE
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
func main(i: Int) {
let a = id(i)
}
Slide 75
Slide 75 text
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
func main(i: Int) {
let a = id(i)
}
Slide 76
Slide 76 text
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
func main(i: Int) {
let a = id(i)
}
Slide 77
Slide 77 text
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
func main(i: Int) {
let a = id(i)
}
Slide 78
Slide 78 text
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
func main(i: Int) {
let a = id(i)
}
Slide 79
Slide 79 text
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
func main(i: Int) {
let a = id(i)
}
Slide 80
Slide 80 text
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
func main(i: Int) {
let a = id(i)
}
Slide 81
Slide 81 text
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
func main(i: Int) {
let a = id(i)
}
Slide 82
Slide 82 text
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
func main(i: Int) {
let a = id(i)
}
Slide 83
Slide 83 text
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
func main(i: Int) {
let a = id(i)
}
Slide 84
Slide 84 text
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
func main(i: Int) {
let a = id(i)
}
Slide 85
Slide 85 text
$PNQMFUJPOPG5ZQF*OGFSFODF
w "MMUZQFWBSJBCMFTUZQFTBSFEFUFSNJOFEˠ5ZQFJOGFSFODFTVDDFTTGVM
w $POUSBEJDUJPOPDDVSTˠ5IFDPEFJTJODPSSFDU
w 6OLOPXOUZQFWBSJBCMFTSFNBJO
CVUJOGFSFODFDBOOPUQSPDFFEˠ5IF
DPEFTUZQFJTBNCJHVPVT
Slide 86
Slide 86 text
#JEJSFDUJPOBMJUZ
func readInt() -> Int
func read() -> X
func main() {
let a = readInt()
let b: Int = read()
}
eq($T1, $T2)
eq(Int, $T3)
call(() -> $T4, $T4, $T3)
call(() -> Int, (), $T2)
Slide 87
Slide 87 text
#JEJSFDUJPOBMJUZ
func readInt() -> Int
func read() -> X
func main() {
let a = readInt()
let b: Int = read()
}
eq($T1, $T2)
eq(Int, $T3)
call(() -> $T4, $T4, $T3)
call(() -> Int, (), $T2)
Slide 88
Slide 88 text
#JEJSFDUJPOBMJUZ
func readInt() -> Int
func read() -> X
func main() {
let a = readInt()
let b: Int = read()
}
eq($T1, $T2)
eq(Int, $T3)
call(() -> $T4, $T4, $T3)
call(() -> Int, (), $T2)
Slide 89
Slide 89 text
#JEJSFDUJPOBMJUZ
func readInt() -> Int
func read() -> X
func main() {
let a = readInt()
let b: Int = read()
}
eq($T1, $T2)
eq(Int, $T3)
call(() -> $T4, $T4, $T3)
call(() -> Int, (), $T2)
Slide 90
Slide 90 text
&YUFOTJPOTGPS4XJGU
Slide 91
Slide 91 text
*OUSPEVDUJPOPGDPOWFSTJPOSFMBUJPOTBOENPEJGJDBUJPOPGBTTJHONFOU
TUBUFNFOUDPOTUSBJOUT
w 4XJGUIBTJNQMJDJUUZQFDPOWFSTJPOT
func main(i: Int) {
let a: Int? = i
}
eq(Int?, Int) conv(Int, Int?)
❌ ✅
"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
Slide 117
Slide 117 text
-FBSOJOHGSPNUIF$PNQJMFS
Slide 118
Slide 118 text
.BLJOHUIF$PNQJMFS0VUQVUUIF0QFSBUJPOTPG5ZQF*OGFSFODF
$ swiftc -Xfrontend -debug-constraints code.swift
func foo() -> Int { 0 }
func foo() -> Bool { true }
func main() {
let a: Int = foo()
}
0VUQVUFYDFFEJOHMJOFT🤯
$ 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
Slide 128
Slide 128 text
$ 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
Slide 129
Slide 129 text
$ 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
Slide 130
Slide 130 text
$ 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
Slide 131
Slide 131 text
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)
Slide 132
Slide 132 text
*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
Slide 133
Slide 133 text
*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
Slide 134
Slide 134 text
*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
Slide 135
Slide 135 text
*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
Slide 136
Slide 136 text
*OJUJBM4UBUFPGUIF$POTUSBJOUT
Score:
Contextual Type: Int at [code.swift:5:12 - line:5:12]
Type Variables:
$T0 [allows bindings to: lvalue, noescape] [attributes: delayed] [with possible bindings:
] @ 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]
Slide 137
Slide 137 text
*OJUJBM4UBUFPGUIF$POTUSBJOUT
Score:
Contextual Type: Int at [code.swift:5:12 - line:5:12]
Type Variables:
$T0 [allows bindings to: lvalue, noescape] [attributes: delayed] [with possible bindings:
] @ 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]
Slide 138
Slide 138 text
*OJUJBM4UBUFPGUIF$POTUSBJOUT
Score:
Contextual Type: Int at [code.swift:5:12 - line:5:12]
Type Variables:
$T0 [allows bindings to: lvalue, noescape] [attributes: delayed] [with possible bindings:
] @ 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]
Slide 139
Slide 139 text
*OJUJBM4UBUFPGUIF$POTUSBJOUT
Score:
Contextual Type: Int at [code.swift:5:12 - line:5:12]
Type Variables:
$T0 [allows bindings to: lvalue, noescape] [attributes: delayed] [with possible bindings:
] @ 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]
Slide 140
Slide 140 text
*OJUJBM4UBUFPGUIF$POTUSBJOUT
Score:
Contextual Type: Int at [code.swift:5:12 - line:5:12]
Type Variables:
$T0 [allows bindings to: lvalue, noescape] [attributes: delayed] [with possible bindings:
] @ 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]
Slide 141
Slide 141 text
*OJUJBM4UBUFPGUIF$POTUSBJOUT
Score:
Contextual Type: Int at [code.swift:5:12 - line:5:12]
Type Variables:
$T0 [allows bindings to: lvalue, noescape] [attributes: delayed] [with possible bindings:
] @ 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]
Slide 142
Slide 142 text
*OJUJBM4UBUFPGUIF$POTUSBJOUT
Score:
Contextual Type: Int at [code.swift:5:12 - line:5:12]
Type Variables:
$T0 [allows bindings to: lvalue, noescape] [attributes: delayed] [with possible bindings:
] @ 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]
Slide 143
Slide 143 text
*OJUJBM4UBUFPGUIF$POTUSBJOUT
Score:
Contextual Type: Int at [code.swift:5:12 - line:5:12]
Type Variables:
$T0 [allows bindings to: lvalue, noescape] [attributes: delayed] [with possible bindings:
] @ 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]
Slide 144
Slide 144 text
*OJUJBM4UBUFPGUIF$POTUSBJOUT
Score:
Contextual Type: Int at [code.swift:5:12 - line:5:12]
Type Variables:
$T0 [allows bindings to: lvalue, noescape] [attributes: delayed] [with possible bindings:
] @ 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]
Slide 145
Slide 145 text
*OJUJBM4UBUFPGUIF$POTUSBJOUT
Score:
Contextual Type: Int at [code.swift:5:12 - line:5:12]
Type Variables:
$T0 [allows bindings to: lvalue, noescape] [attributes: delayed] [with possible bindings:
] @ 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]
Slide 146
Slide 146 text
*OJUJBM4UBUFPGUIF$POTUSBJOUT
Score:
Contextual Type: Int at [code.swift:5:12 - line:5:12]
Type Variables:
$T0 [allows bindings to: lvalue, noescape] [attributes: delayed] [with possible bindings:
] @ 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]
4PMVUJPO
---Solution---
Fixed score:
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
Slide 154
Slide 154 text
4PMVUJPO
---Solution---
Fixed score:
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
Slide 155
Slide 155 text
4PMVUJPO
---Solution---
Fixed score:
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
Slide 156
Slide 156 text
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
Slide 157
Slide 157 text
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
Slide 158
Slide 158 text
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
Slide 159
Slide 159 text
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
Slide 160
Slide 160 text
3FDBQ
w 5IFCBTJDTPGUZQFJOGFSFODFBSFVOJ
fi
DBUJPOBOETJNQMJ
fi
DBUJPOPG
DPOTUSBJOUT
w 4XJGUFYUFOETJUCZBEEJOHTFBSDIUPUIFQSPDFTT
w 8FDBOVOEFSTUBOEJUTPQFSBUJPOTCZMPPLJOHBUUIFDPNQJMFSTPVUQVU
Slide 161
Slide 161 text
/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