Play

Check-in [fc5483d159]
Login
Overview
Comment:Fix 'Quotations' example in play ground.
Timelines: family | ancestors | descendants | both | trunk | alpha-1 | releases
Files: files | file ages | folders
SHA3-256: fc5483d159bd766b60cf4c36dfcd3aff17ad93dad95913cbe0219abe943cb0fb
User & Date: robin.hansen on 2020-11-19 05:57:25
Other Links: manifest | tags
Context
2021-01-17
10:32
Simplify tests by automating ctor and getter/setter boilerplate. check-in: e9d8932285 user: robin.hansen tags: trunk
2020-11-19
09:45
Create new branch named "releases" check-in: e80d8311d7 user: robin.hansen tags: releases
09:44
Create new branch named "alpha-1" check-in: cc1aacf919 user: robin.hansen tags: alpha-1
05:57
Fix 'Quotations' example in play ground. check-in: fc5483d159 user: robin.hansen tags: trunk, alpha-1, releases
05:56
Make sure code use to reproduce bug works as expected. Closed-Leaf check-in: af3cd0eeff user: robin.hansen tags: bugfix-generics-and-quotations
2020-10-23
04:46
TypeChecker now checks to see if a multiword handles every possible leading type. Fixes [56a0fe1350]... check-in: dbcb88d2fc user: robin.hansen tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Modified src/Play/Data/Type.elm from [80b184fda8] to [7b6e93ee1b].

1
2
3
4

5
6
7
8
9
10
11
..
25
26
27
28
29
30
31







32
33
34
35
36
37
38
module Play.Data.Type exposing
    ( Type(..)
    , WordType
    , compatibleWords

    , genericName
    , genericlyCompatible
    , isGeneric
    , referencedGenerics
    , toDisplayString
    , wordTypeToString
    )
................................................................................


type alias WordType =
    { input : List Type
    , output : List Type
    }









isGeneric : Type -> Bool
isGeneric t =
    case t of
        Generic _ ->
            True





>







 







>
>
>
>
>
>
>







1
2
3
4
5
6
7
8
9
10
11
12
..
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
module Play.Data.Type exposing
    ( Type(..)
    , WordType
    , compatibleWords
    , emptyWordType
    , genericName
    , genericlyCompatible
    , isGeneric
    , referencedGenerics
    , toDisplayString
    , wordTypeToString
    )
................................................................................


type alias WordType =
    { input : List Type
    , output : List Type
    }


emptyWordType : WordType
emptyWordType =
    { input = []
    , output = []
    }


isGeneric : Type -> Bool
isGeneric t =
    case t of
        Generic _ ->
            True

Modified src/Play/TypeChecker.elm from [6269134213] to [fec5dbde4e].

219
220
221
222
223
224
225











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
...
667
668
669
670
671
672
673












674

675


676
677
678
679
680
681
682
683
684






685
686

687
688
689
690
691
692
693
    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 ) =
................................................................................
        ( _ :: annotatedRest, inferredEl :: inferredRest ) ->
            constrainGenericsHelper remappedGenerics annotatedRest inferredRest (inferredEl :: acc)


typeCheckImplementation : Qualifier.WordDefinition -> List Qualifier.Node -> Context -> ( WordType, Context )
typeCheckImplementation untypedDef impl context =
    let












        contextWithCall =

            { context | callStack = Set.insert untypedDef.name context.callStack }



        ( _, contextWithStackEffects ) =
            List.foldl
                (\node ( idx, ctx ) -> ( idx + 1, typeCheckNode idx node ctx ))
                ( 0, contextWithCall )
                impl

        contextWithoutCall =
            { contextWithStackEffects | callStack = Set.remove untypedDef.name contextWithStackEffects.callStack }






    in
    wordTypeFromStackEffects untypedDef contextWithoutCall

        |> simplifyWordType
        |> (\( a, b ) -> ( b, a ))


extractTypeFromTypeMatch : Qualifier.TypeMatch -> Type
extractTypeFromTypeMatch (Qualifier.TypeMatch _ t_ _) =
    t_







>
>
>
>
>
>
>
>
>
>
>








|
>
>
>









|







 







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

>
|
>
>









>
>
>
>
>
>


>







219
220
221
222
223
224
225
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
...
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
    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 ) =
................................................................................
        ( _ :: annotatedRest, inferredEl :: inferredRest ) ->
            constrainGenericsHelper remappedGenerics annotatedRest inferredRest (inferredEl :: acc)


typeCheckImplementation : Qualifier.WordDefinition -> List Qualifier.Node -> Context -> ( WordType, Context )
typeCheckImplementation untypedDef impl context =
    let
        startingStackEffects =
            untypedDef.metadata.type_
                |> TypeSignature.toMaybe
                |> Maybe.map reverseWordType
                |> Maybe.withDefault Type.emptyWordType
                |> wordTypeToStackEffects

        reverseWordType wt =
            { input = []
            , output = wt.input
            }

        contextWithCall =
            { context
                | callStack = Set.insert untypedDef.name context.callStack
                , stackEffects = startingStackEffects
            }

        ( _, contextWithStackEffects ) =
            List.foldl
                (\node ( idx, ctx ) -> ( idx + 1, typeCheckNode idx node ctx ))
                ( 0, contextWithCall )
                impl

        contextWithoutCall =
            { contextWithStackEffects | callStack = Set.remove untypedDef.name contextWithStackEffects.callStack }

        annotatedInput =
            untypedDef.metadata.type_
                |> TypeSignature.toMaybe
                |> Maybe.map .input
                |> Maybe.withDefault []
    in
    wordTypeFromStackEffects untypedDef contextWithoutCall
        |> (\( ctx, wt ) -> ( ctx, { wt | input = wt.input ++ annotatedInput } ))
        |> simplifyWordType
        |> (\( a, b ) -> ( b, a ))


extractTypeFromTypeMatch : Qualifier.TypeMatch -> Type
extractTypeFromTypeMatch (Qualifier.TypeMatch _ t_ _) =
    t_

Modified tests/Test/TypeChecker.elm from [ba577d7cfb] to [5ce2f71b87].

5
6
7
8
9
10
11

12
13
14
15
16
17
18
...
382
383
384
385
386
387
388

















































































































389
390
391
392
393
394
395
import Expect
import Play.Data.Builtin as Builtin
import Play.Data.Metadata as Metadata exposing (Metadata)
import Play.Data.SourceLocation exposing (emptyRange)
import Play.Data.Type as Type
import Play.Qualifier as QAST
import Play.TypeChecker exposing (..)

import Test exposing (Test, describe, test)


suite : Test
suite =
    describe "TypeChecker"
        [ test "Simple program" <|
................................................................................
                                ]
                        }
                in
                case run input of
                    Err _ ->
                        Expect.fail "Did not expect type check to fail."


















































































































                    Ok _ ->
                        Expect.pass
        , test "Generic custom type fails if not generic is listed" <|
            \_ ->
                let
                    input =
                        { types =







>







 







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







5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
...
383
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
import Expect
import Play.Data.Builtin as Builtin
import Play.Data.Metadata as Metadata exposing (Metadata)
import Play.Data.SourceLocation exposing (emptyRange)
import Play.Data.Type as Type
import Play.Qualifier as QAST
import Play.TypeChecker exposing (..)
import Play.TypeChecker.Problem as Problem
import Test exposing (Test, describe, test)


suite : Test
suite =
    describe "TypeChecker"
        [ test "Simple program" <|
................................................................................
                                ]
                        }
                in
                case run input of
                    Err _ ->
                        Expect.fail "Did not expect type check to fail."

                    Ok _ ->
                        Expect.pass
        , test "Generic types with rotations and quotations" <|
            \_ ->
                let
                    input =
                        { types =
                            Dict.fromListBy QAST.typeDefinitionName
                                [ QAST.CustomTypeDef "Coordinate"
                                    emptyRange
                                    []
                                    [ ( "x", Type.Int )
                                    , ( "y", Type.Int )
                                    ]
                                ]
                        , words =
                            Dict.fromListBy .name
                                [ { name = "main"
                                  , metadata =
                                        Metadata.default
                                            |> Metadata.withType [] [ Type.Int ]
                                  , implementation =
                                        QAST.SoloImpl
                                            [ QAST.Integer emptyRange 1
                                            , QAST.Integer emptyRange 2
                                            , QAST.Word emptyRange ">Coordinate"
                                            , QAST.WordRef emptyRange "main__quot1"
                                            , QAST.Word emptyRange "update-x"
                                            , QAST.Word emptyRange "x>"
                                            ]
                                  }
                                , { name = "main__quot1"
                                  , metadata = Metadata.default
                                  , implementation =
                                        QAST.SoloImpl
                                            [ QAST.Integer emptyRange 1
                                            , QAST.Builtin emptyRange Builtin.Plus
                                            ]
                                  }
                                , { name = "update-x"
                                  , metadata =
                                        Metadata.default
                                            |> Metadata.withType
                                                [ Type.Custom "Coordinate"
                                                , Type.Quotation { input = [ Type.Int ], output = [ Type.Int ] }
                                                ]
                                                [ Type.Custom "Coordinate" ]
                                  , implementation =
                                        QAST.SoloImpl
                                            [ QAST.Builtin emptyRange Builtin.StackSwap
                                            , QAST.Builtin emptyRange Builtin.StackDuplicate
                                            , QAST.Word emptyRange "x>"
                                            , QAST.Builtin emptyRange Builtin.StackLeftRotate
                                            , QAST.Builtin emptyRange Builtin.Apply
                                            , QAST.Word emptyRange ">x"
                                            ]
                                  }
                                , { name = ">Coordinate"
                                  , metadata =
                                        Metadata.default
                                            |> Metadata.withType [ Type.Int, Type.Int ] [ Type.Custom "Coordinate" ]
                                  , implementation =
                                        QAST.SoloImpl
                                            [ QAST.ConstructType "Coordinate"
                                            ]
                                  }
                                , { name = ">x"
                                  , metadata =
                                        Metadata.default
                                            |> Metadata.withType
                                                [ Type.Custom "Coordinate", Type.Int ]
                                                [ Type.Custom "Coordinate" ]
                                  , implementation =
                                        QAST.SoloImpl [ QAST.SetMember "Coordinate" "x" ]
                                  }
                                , { name = "x>"
                                  , metadata =
                                        Metadata.default
                                            |> Metadata.withType
                                                [ Type.Custom "Coordinate" ]
                                                [ Type.Int ]
                                  , implementation =
                                        QAST.SoloImpl [ QAST.GetMember "Coordinate" "x" ]
                                  }
                                , { name = ">y"
                                  , metadata =
                                        Metadata.default
                                            |> Metadata.withType
                                                [ Type.Custom "Coordinate", Type.Int ]
                                                [ Type.Custom "Coordinate" ]
                                  , implementation =
                                        QAST.SoloImpl [ QAST.SetMember "Coordinate" "y" ]
                                  }
                                , { name = "y>"
                                  , metadata =
                                        Metadata.default
                                            |> Metadata.withType
                                                [ Type.Custom "Coordinate" ]
                                                [ Type.Int ]
                                  , implementation =
                                        QAST.SoloImpl [ QAST.GetMember "Coordinate" "y" ]
                                  }
                                ]
                        }
                in
                case run input of
                    Err err ->
                        err
                            |> List.map (Problem.toString "")
                            |> String.join "\n"
                            |> (++) "Did not expect type check to fail.\n"
                            |> Expect.fail

                    Ok _ ->
                        Expect.pass
        , test "Generic custom type fails if not generic is listed" <|
            \_ ->
                let
                    input =
                        { types =

Modified wasm_tests/quotes.test.js from [bf404af7fa] to [1ebafeab8a].

13
14
15
16
17
18
19


























        : 1 -
    `);

    const result = await compiler.run(wat, 'main');

    expect(result.stackElement()).toBe(1);
});

































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
        : 1 -
    `);

    const result = await compiler.run(wat, 'main');

    expect(result.stackElement()).toBe(1);
});

test('Basic quotation', async () => {
    const wat = await compiler.toWat(`
        deftype: Coordinate
        : x Int
        : y Int

        def: update-x
        type: Coordinate [ Int -- Int ] -- Coordinate
        : swap
          dup x>
          -rotate
          !
          >x

        def: main
        entry: true
        : 1 2 >Coordinate
          [ 1 + ] update-x
          x>
    `);

    const result = await compiler.run(wat, 'main');

    expect(result.stackElement()).toBe(2);
});