Stabel

Check-in [8e523926b1]
Login
Overview
Comment:Implement code that type check array literals, now we need to add an array type.
Timelines: family | ancestors | descendants | both | arrays
Files: files | file ages | folders
SHA3-256: 8e523926b12868ba8da3cf32cce788aef41bb21163d958dea9c2c1d06e3b3df9
User & Date: robin.hansen on 2021-08-22 09:59:38
Other Links: branch diff | manifest | tags
Context
2021-08-22
10:39
I think type checking of arrays work now :) check-in: b5450e1656 user: robin.hansen tags: arrays
09:59
Implement code that type check array literals, now we need to add an array type. check-in: 8e523926b1 user: robin.hansen tags: arrays
2021-08-21
11:03
Refactor in preperation to type check array literals. check-in: e023dcfecb user: robin.hansen tags: arrays
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Modified src/Stabel/TypeChecker.elm from [7c3c57fca4] to [05819b475d].

8
9
10
11
12
13
14

15
16
17
18
19
20
21
....
1183
1184
1185
1186
1187
1188
1189
1190

























































































1191



1192
1193
1194
1195
1196
1197
1198
    , TypeMatch(..)
    , TypeMatchValue(..)
    , run
    )

import Dict exposing (Dict)
import List.Extra as List

import Set exposing (Set)
import Stabel.Data.Builtin as Builtin exposing (Builtin)
import Stabel.Data.SourceLocation as SourceLocation exposing (SourceLocationRange)
import Stabel.Data.Type as Type exposing (FunctionType, Type)
import Stabel.Data.TypeSignature as TypeSignature exposing (TypeSignature)
import Stabel.Qualifier as Qualifier
import Stabel.TypeChecker.Problem exposing (Problem(..))
................................................................................

nodeToStackEffect : Qualifier.FunctionDefinition -> Qualifier.Node -> Context -> Result Problem ( Context, List StackEffect )
nodeToStackEffect currentDef node context =
    case node of
        Qualifier.Integer _ _ ->
            Ok ( context, [ Push Type.Int ] )

        Qualifier.ArrayLiteral _ _ ->

























































































            Ok ( context, [] )




        Qualifier.Function _ untypedDef ->
            let
                ( def, contextWithTypedDef ) =
                    typeCheckDefinition untypedDef context

                newContext =







>







 







|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>







8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
....
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
    , TypeMatch(..)
    , TypeMatchValue(..)
    , run
    )

import Dict exposing (Dict)
import List.Extra as List
import Result.Extra as Result
import Set exposing (Set)
import Stabel.Data.Builtin as Builtin exposing (Builtin)
import Stabel.Data.SourceLocation as SourceLocation exposing (SourceLocationRange)
import Stabel.Data.Type as Type exposing (FunctionType, Type)
import Stabel.Data.TypeSignature as TypeSignature exposing (TypeSignature)
import Stabel.Qualifier as Qualifier
import Stabel.TypeChecker.Problem exposing (Problem(..))
................................................................................

nodeToStackEffect : Qualifier.FunctionDefinition -> Qualifier.Node -> Context -> Result Problem ( Context, List StackEffect )
nodeToStackEffect currentDef node context =
    case node of
        Qualifier.Integer _ _ ->
            Ok ( context, [ Push Type.Int ] )

        Qualifier.ArrayLiteral loc nodes ->
            let
                res =
                    List.foldr
                        (\n acc ->
                            case acc of
                                Err _ ->
                                    acc

                                Ok ( previousStackEffects, previousContext ) ->
                                    case nodeToStackEffect currentDef n previousContext of
                                        Err err ->
                                            Err err

                                        Ok ( nextContext, nodeStackEffects ) ->
                                            Ok ( nodeStackEffects :: previousStackEffects, nextContext )
                        )
                        (Ok ( [], context ))
                        nodes
                        |> Result.map (Tuple.mapFirst (List.map effectsToFunctionType))
                        |> Result.map (Tuple.mapFirst (List.map validateArrayType))
                        |> Result.map (Tuple.mapFirst Result.combine)
                        |> Result.andThen liftTupleFirstResult
                        |> Result.map (Tuple.mapFirst (List.concatMap .output))
                        |> Result.map (Tuple.mapFirst unionizeTypes)

                effectsToFunctionType effects =
                    let
                        ( inputs, outputs ) =
                            List.partition stackEffectIsPop effects
                                |> Tuple.mapBoth (List.map stackEffectType) (List.map stackEffectType)
                    in
                    { input = inputs
                    , output = outputs
                    }

                stackEffectIsPop effect =
                    case effect of
                        Pop _ ->
                            True

                        Push _ ->
                            False

                stackEffectType effect =
                    case effect of
                        Pop t ->
                            t

                        Push t ->
                            t

                validateArrayType type_ =
                    if List.isEmpty type_.input && List.length type_.output == 1 then
                        Ok type_

                    else
                        Err <| BadArrayElement loc type_

                liftTupleFirstResult ( resA, b ) =
                    case resA of
                        Ok a ->
                            Ok ( a, b )

                        Err err ->
                            Err err

                unionizeTypes ts =
                    unionizeTypesHelper ts []

                unionizeTypesHelper ts acc =
                    case ts of
                        [] ->
                            case acc of
                                [ t ] ->
                                    t

                                _ ->
                                    Type.Union Nothing acc

                        t :: rest ->
                            if List.member t acc then
                                unionizeTypesHelper rest acc

                            else
                                unionizeTypesHelper rest (t :: acc)
            in
            case res of
                Ok ( _, newContext ) ->
                    -- TODO: Push array type
                    Ok ( newContext, [] )

                Err err ->
                    Err err

        Qualifier.Function _ untypedDef ->
            let
                ( def, contextWithTypedDef ) =
                    typeCheckDefinition untypedDef context

                newContext =

Modified src/Stabel/TypeChecker/Problem.elm from [f356902d82] to [541dc50bc8].

12
13
14
15
16
17
18

19
20
21
22
23
24
25
..
99
100
101
102
103
104
105










106
107
108
109
110
111
112
...
123
124
125
126
127
128
129



    = UndeclaredGeneric SourceLocationRange String
    | TypeError SourceLocationRange String FunctionType FunctionType
    | UnexpectedType SourceLocationRange String Type Type
    | InconsistentWhens SourceLocationRange String
    | MissingTypeAnnotationInRecursiveCallStack SourceLocationRange String
    | InexhaustiveMultiFunction SourceLocationRange (List (List Type))
    | BadEntryPoint SourceLocationRange String FunctionType FunctionType



toString : String -> Problem -> String
toString source problem =
    case problem of
        UndeclaredGeneric range generic ->
            ">> "
................................................................................
                ++ "In order to be called from the command line, the type of '"
                ++ name
                ++ "' needs to be:\n\n"
                ++ Type.functionTypeToString actual
                ++ "\n\nHowever, it seems that the actual type is:\n\n"
                ++ Type.functionTypeToString expected












sourceLocationRef : Problem -> String
sourceLocationRef problem =
    case problem of
        UndeclaredGeneric range _ ->
            range.source

................................................................................
            range.source

        InexhaustiveMultiFunction range _ ->
            range.source

        BadEntryPoint range _ _ _ ->
            range.source










>







 







>
>
>
>
>
>
>
>
>
>







 







>
>
>
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
...
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
...
134
135
136
137
138
139
140
141
142
143
    = UndeclaredGeneric SourceLocationRange String
    | TypeError SourceLocationRange String FunctionType FunctionType
    | UnexpectedType SourceLocationRange String Type Type
    | InconsistentWhens SourceLocationRange String
    | MissingTypeAnnotationInRecursiveCallStack SourceLocationRange String
    | InexhaustiveMultiFunction SourceLocationRange (List (List Type))
    | BadEntryPoint SourceLocationRange String FunctionType FunctionType
    | BadArrayElement SourceLocationRange FunctionType


toString : String -> Problem -> String
toString source problem =
    case problem of
        UndeclaredGeneric range generic ->
            ">> "
................................................................................
                ++ "In order to be called from the command line, the type of '"
                ++ name
                ++ "' needs to be:\n\n"
                ++ Type.functionTypeToString actual
                ++ "\n\nHowever, it seems that the actual type is:\n\n"
                ++ Type.functionTypeToString expected

        BadArrayElement range actual ->
            ">> "
                ++ range.source
                ++ "\n\n"
                ++ SourceLocation.extractFromString source range.start range.end
                ++ "\n\n"
                ++ "An Array can only contain elements of type ( -- a ), but found an element with type:"
                ++ "\n\n"
                ++ Type.functionTypeToString actual


sourceLocationRef : Problem -> String
sourceLocationRef problem =
    case problem of
        UndeclaredGeneric range _ ->
            range.source

................................................................................
            range.source

        InexhaustiveMultiFunction range _ ->
            range.source

        BadEntryPoint range _ _ _ ->
            range.source

        BadArrayElement range _ ->
            range.source