Play

Check-in [76d7054f19]
Login
Overview
Comment:Instead of inferring types of each branch in a multi-fn and compare the results to type annotation, use type annotation as basis for inference.
Timelines: family | ancestors | descendants | both | stdlib
Files: files | file ages | folders
SHA3-256: 76d7054f19c4c9847243b393ec6e9017bac69906fcee47d9607dad746876c745
User & Date: robin.hansen on 2021-04-13 13:58:53
Other Links: branch diff | manifest | tags
Context
2021-04-15
09:32
Fix bug where type checker would infer a nested union as possible input type. Unions are flattened. check-in: 9353390979 user: robin.hansen tags: stdlib
2021-04-13
13:58
Instead of inferring types of each branch in a multi-fn and compare the results to type annotation, ... check-in: 76d7054f19 user: robin.hansen tags: stdlib
2021-04-11
10:07
Add unit test that reproduces current typechecking bug with stdlib. check-in: 07dba377f5 user: robin.hansen tags: stdlib
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Modified src/Play/TypeChecker.elm from [2a58b70563] to [bbcfdae6cb].

226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
...
398
399
400
401
402
403
404
405
406




















407
408
409
410
411
412
413
414
415
    Context
    -> Qualifier.WordDefinition
    -> List ( Qualifier.TypeMatch, List Qualifier.Node )
    -> List Qualifier.Node
    -> Context
typeCheckMultiImplementation context untypedDef initialWhens defaultImpl =
    let
        untypedDefMetadata =
            untypedDef.metadata

        untypedDefNoTypeAnnotation =
            { untypedDef
                | metadata =
                    { untypedDefMetadata
                        | type_ = TypeSignature.NotProvided
                    }
            }

        whens =
            case defaultImpl of
                [] ->
                    initialWhens

                _ ->
                    let
                        ( inferredDefaultType, _ ) =
                            typeCheckImplementation
                                untypedDefNoTypeAnnotation
                                defaultImpl
                                (cleanContext context)
                    in
                    case inferredDefaultType.input of
                        [] ->
                            Debug.todo "Default impl doesn't have an input argument"

                        firstType :: _ ->
                            ( Qualifier.TypeMatch SourceLocation.emptyRange firstType [], defaultImpl ) :: initialWhens

        ( inferredWhenTypes, newContext ) =
            List.foldr (inferWhenTypes untypedDefNoTypeAnnotation) ( [], context ) whens
                |> Tuple.mapFirst normalizeWhenTypes
                |> (\( wts, ctx ) -> simplifyWhenWordTypes wts ctx)
                |> Tuple.mapFirst (List.map2 Tuple.pair whenPatterns >> List.map replaceFirstTypeWithPatternMatch)
                |> Tuple.mapFirst equalizeWhenTypes
                |> Tuple.mapFirst (\whenTypes -> List.map (constrainGenerics untypedDef.metadata.type_) whenTypes)

        replaceFirstTypeWithPatternMatch ( Qualifier.TypeMatch _ matchType _, typeSignature ) =
................................................................................


inferWhenTypes :
    Qualifier.WordDefinition
    -> ( Qualifier.TypeMatch, List Qualifier.Node )
    -> ( List WordType, Context )
    -> ( List WordType, Context )
inferWhenTypes untypedDef ( _, im ) ( infs, ctx ) =
    let




















        ( inf, newCtx ) =
            typeCheckImplementation untypedDef im (cleanContext ctx)
    in
    ( inf :: infs, newCtx )


normalizeWhenTypes : List WordType -> List WordType
normalizeWhenTypes whenTypes =
    let







<
<
<
<
<
<
<
<
<
<
<








|
<
<
<









|







 







|

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

|







226
227
228
229
230
231
232











233
234
235
236
237
238
239
240
241



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
...
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
    Context
    -> Qualifier.WordDefinition
    -> List ( Qualifier.TypeMatch, List Qualifier.Node )
    -> List Qualifier.Node
    -> Context
typeCheckMultiImplementation context untypedDef initialWhens defaultImpl =
    let











        whens =
            case defaultImpl of
                [] ->
                    initialWhens

                _ ->
                    let
                        ( inferredDefaultType, _ ) =
                            typeCheckImplementation untypedDef defaultImpl (cleanContext context)



                    in
                    case inferredDefaultType.input of
                        [] ->
                            Debug.todo "Default impl doesn't have an input argument"

                        firstType :: _ ->
                            ( Qualifier.TypeMatch SourceLocation.emptyRange firstType [], defaultImpl ) :: initialWhens

        ( inferredWhenTypes, newContext ) =
            List.foldr (inferWhenTypes untypedDef) ( [], context ) whens
                |> Tuple.mapFirst normalizeWhenTypes
                |> (\( wts, ctx ) -> simplifyWhenWordTypes wts ctx)
                |> Tuple.mapFirst (List.map2 Tuple.pair whenPatterns >> List.map replaceFirstTypeWithPatternMatch)
                |> Tuple.mapFirst equalizeWhenTypes
                |> Tuple.mapFirst (\whenTypes -> List.map (constrainGenerics untypedDef.metadata.type_) whenTypes)

        replaceFirstTypeWithPatternMatch ( Qualifier.TypeMatch _ matchType _, typeSignature ) =
................................................................................


inferWhenTypes :
    Qualifier.WordDefinition
    -> ( Qualifier.TypeMatch, List Qualifier.Node )
    -> ( List WordType, Context )
    -> ( List WordType, Context )
inferWhenTypes untypedDef ( Qualifier.TypeMatch _ t _, im ) ( infs, ctx ) =
    let
        alteredTypeSignature =
            case untypedDef.metadata.type_ of
                TypeSignature.UserProvided wt ->
                    TypeSignature.UserProvided <|
                        case wt.input of
                            _ :: rest ->
                                { wt | input = t :: rest }

                            _ ->
                                wt

                x ->
                    x

        metadata =
            untypedDef.metadata

        alteredDef =
            { untypedDef | metadata = { metadata | type_ = alteredTypeSignature } }

        ( inf, newCtx ) =
            typeCheckImplementation alteredDef im (cleanContext ctx)
    in
    ( inf :: infs, newCtx )


normalizeWhenTypes : List WordType -> List WordType
normalizeWhenTypes whenTypes =
    let

Modified tests/Test/TypeChecker.elm from [e9183c444f] to [b208d7f987].

560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
....
1116
1117
1118
1119
1120
1121
1122

1123
1124
1125
1126
1127
1128
1129
....
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
                                        ]
                                        [ QAST.Builtin emptyRange Builtin.StackDrop
                                        , QAST.Integer emptyRange 1
                                        ]
                                }
                    in
                    case run input of
                        Err _ ->
                            Expect.fail "Did not expect type check to fail."

                        Ok _ ->
                            Expect.pass
            , test "With default branch (no type meta)" <|
                \_ ->
                    let
                        input =
................................................................................
                                                        }
                                                    ]
                                                    [ Type.Int ]
                                      , implementation =
                                            QAST.SoloImpl
                                                [ QAST.Integer emptyRange 1
                                                , QAST.Integer emptyRange 2

                                                , QAST.Builtin emptyRange Builtin.Apply
                                                ]
                                      }
                                    , { name = "main"
                                      , metadata =
                                            Metadata.default
                                                |> Metadata.asEntryPoint
................................................................................
                                    ]
                            }
                    in
                    case run input of
                        Ok _ ->
                            Expect.pass

                        Err _ ->
                            Expect.fail "Did not expect type check to fail."
            , test "With generics" <|
                \_ ->
                    let
                        input =
                            { types = Dict.empty
                            , words =
                                Dict.fromListBy .name







|
|







 







>







 







|
|







560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
....
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
....
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
                                        ]
                                        [ QAST.Builtin emptyRange Builtin.StackDrop
                                        , QAST.Integer emptyRange 1
                                        ]
                                }
                    in
                    case run input of
                        Err err ->
                            Expect.fail <| "Did not expect type check to fail." ++ Debug.toString err

                        Ok _ ->
                            Expect.pass
            , test "With default branch (no type meta)" <|
                \_ ->
                    let
                        input =
................................................................................
                                                        }
                                                    ]
                                                    [ Type.Int ]
                                      , implementation =
                                            QAST.SoloImpl
                                                [ QAST.Integer emptyRange 1
                                                , QAST.Integer emptyRange 2
                                                , QAST.Builtin emptyRange Builtin.StackLeftRotate
                                                , QAST.Builtin emptyRange Builtin.Apply
                                                ]
                                      }
                                    , { name = "main"
                                      , metadata =
                                            Metadata.default
                                                |> Metadata.asEntryPoint
................................................................................
                                    ]
                            }
                    in
                    case run input of
                        Ok _ ->
                            Expect.pass

                        Err err ->
                            Expect.fail <| "Did not expect type check to fail:" ++ Debug.toString err
            , test "With generics" <|
                \_ ->
                    let
                        input =
                            { types = Dict.empty
                            , words =
                                Dict.fromListBy .name