Skip to content

Commit

Permalink
Full value resolution support (#67)
Browse files Browse the repository at this point in the history
* Added missing module declarations. #37

* Process only those modules that are reachable from exposed ones. Remove package path from module names. #21

* Renaming concepts based on review feedback. #41

* Moved Advanced module up a level. #43

* Removed unused unindent function.

* Moved name to SDK.

* Added missing module to SDK.

* Partial implementation of value mapping.

* Ignore all generated JS.

* Change extra arg position and naming. #46, #25

* Change extra arg position and naming. #46, #25

* Removed remaining references to extra. #5

* Change extra arg position. #46, #25, #5

* Added more coverage. #46, #25, #5

* Changes suggested in the PR. #46, #25, #5

* Use more explicit names.

* All patterns supported.

* Pattern-match supported.

* Added support for SDK operators. #52

* Fix compile errors.

* Prepare Elm module publishing. #2

* Fix repo name. #2

* Support for let expressions. #54

* Simple join implementations. #64

* Refactoring to make the code more organized.

* Refactoring to make the code more organized.

* Prepare value mapping utils. #53

* More descriptive naming.

* More descriptive naming.

* Removed unused function

* Better naming

* Added utility function and removed unused one.

* Added variable resolution.

* Hooked up variable resolution and updated tests. #53

* Completed reference resolution. #53
  • Loading branch information
AttilaMihaly committed Apr 20, 2020
1 parent 605a5b6 commit f92b5f1
Show file tree
Hide file tree
Showing 10 changed files with 836 additions and 620 deletions.
3 changes: 3 additions & 0 deletions cli/elm.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@
"elm/json": "1.1.3",
"elm/parser": "1.1.0",
"elm/regex": "1.0.0",
"elm-community/graph": "6.0.0",
"elm-community/maybe-extra": "5.2.0",
"elm-explorations/test": "1.2.2",
"stil4m/elm-syntax": "7.1.1"
},
"indirect": {
"avh4/elm-fifo": "1.0.4",
"elm/random": "1.0.0",
"elm/time": "1.0.0",
"elm/url": "1.0.0",
"elm/virtual-dom": "1.0.2",
"elm-community/intdict": "3.0.0",
"elm-community/json-extra": "4.2.0",
"elm-community/list-extra": "8.2.3",
"rtfeldman/elm-hex": "1.0.0",
Expand Down
1,041 changes: 638 additions & 403 deletions src/Morphir/Elm/Frontend.elm

Large diffs are not rendered by default.

43 changes: 36 additions & 7 deletions src/Morphir/Elm/Frontend/Resolve.elm
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import Morphir.IR.Package as Package
import Morphir.IR.Path as Path exposing (Path)
import Morphir.IR.Type as Type
import Morphir.JsonExtra as JsonExtra
import Morphir.Pattern exposing (matchAny)
import Set exposing (Set)


Expand Down Expand Up @@ -86,7 +85,8 @@ type alias ModuleResolver =


type alias PackageResolver =
{ ctorNames : ModuleName -> LocalName -> Result Error (List String)
{ packagePath : Path
, ctorNames : ModuleName -> LocalName -> Result Error (List String)
, exposesType : ModuleName -> LocalName -> Result Error Bool
, exposesValue : ModuleName -> LocalName -> Result Error Bool
, decomposeModuleName : ModuleName -> Result Error ( Path, Path )
Expand Down Expand Up @@ -242,11 +242,11 @@ createPackageResolver dependencies currentPackagePath currentPackageModules =
)
|> Result.fromMaybe (CouldNotDecompose moduleName)
in
PackageResolver ctorNames exposesType exposesValue decomposeModuleName
PackageResolver currentPackagePath ctorNames exposesType exposesValue decomposeModuleName


createModuleResolver : PackageResolver -> List Import -> ModuleResolver
createModuleResolver packageResolver explicitImports =
createModuleResolver : PackageResolver -> List Import -> Path -> Module.Definition a -> ModuleResolver
createModuleResolver packageResolver explicitImports currenctModulePath moduleDef =
let
imports : List Import
imports =
Expand Down Expand Up @@ -402,15 +402,44 @@ createModuleResolver packageResolver explicitImports =
else
Err (ModuleNotImported fullModuleName)

resolve : Bool -> ModuleName -> LocalName -> Result Error FQName
resolve isType moduleName localName =
resolveExternally : Bool -> ModuleName -> LocalName -> Result Error FQName
resolveExternally isType moduleName localName =
resolveModuleName isType moduleName localName
|> Result.andThen packageResolver.decomposeModuleName
|> Result.map
(\( packagePath, modulePath ) ->
fQName packagePath modulePath (Name.fromString localName)
)

resolve : Bool -> ModuleName -> LocalName -> Result Error FQName
resolve isType elmModuleName elmLocalName =
if List.isEmpty elmModuleName then
-- If the name is not prefixed with a module we need to look it up within the module first
let
localNames =
if isType then
moduleDef.types |> Dict.keys

else
moduleDef.values |> Dict.keys

localName =
elmLocalName |> Name.fromString
in
if localNames |> List.member localName then
if Path.isPrefixOf currenctModulePath packageResolver.packagePath then
Ok (fQName packageResolver.packagePath (currenctModulePath |> List.drop (List.length packageResolver.packagePath)) localName)

else
Err (PackageNotPrefixOfModule packageResolver.packagePath currenctModulePath)

else
resolveExternally isType elmModuleName elmLocalName

else
-- If the name is prefixed with a module we can skip the local resolution
resolveExternally isType elmModuleName elmLocalName

resolveType : ModuleName -> LocalName -> Result Error FQName
resolveType =
resolve True
Expand Down
19 changes: 9 additions & 10 deletions src/Morphir/IR/Module.elm
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ module Morphir.IR.Module exposing
-}

import Dict exposing (Dict)
import Json.Decode as Decode
import Json.Encode as Encode
import Morphir.IR.AccessControlled as AccessControlled exposing (AccessControlled, encodeAccessControlled, withPublicAccess)
import Morphir.IR.AccessControlled exposing (AccessControlled, encodeAccessControlled, withPublicAccess)
import Morphir.IR.Name exposing (Name, encodeName)
import Morphir.IR.Path exposing (Path)
import Morphir.IR.Type as Type exposing (Type)
import Morphir.IR.Value as Value exposing (Value)
import Morphir.ResultList as ResultList
import Morphir.ListOfResults as ListOfResults


type alias ModulePath =
Expand Down Expand Up @@ -88,7 +87,7 @@ eraseSpecificationAttributes spec =
spec
|> mapSpecification
(Type.mapTypeAttributes (\_ -> ()) >> Ok)
(Value.mapValueAttributes (\_ -> ()))
(Value.mapValueAttributes (\_ -> ()) >> Ok)
|> Result.withDefault emptySpecification


Expand Down Expand Up @@ -121,7 +120,7 @@ encodeSpecification encodeAttributes spec =
]


mapSpecification : (Type a -> Result e (Type b)) -> (Value a -> Value b) -> Specification a -> Result (List e) (Specification b)
mapSpecification : (Type a -> Result e (Type b)) -> (Value a -> Result e (Value b)) -> Specification a -> Result (List e) (Specification b)
mapSpecification mapType mapValue spec =
let
typesResult : Result (List e) (Dict Name (Type.Specification b))
Expand All @@ -134,7 +133,7 @@ mapSpecification mapType mapValue spec =
|> Type.mapSpecification mapType
|> Result.map (Tuple.pair typeName)
)
|> ResultList.toResult
|> ListOfResults.liftAllErrors
|> Result.map Dict.fromList
|> Result.mapError List.concat

Expand All @@ -148,7 +147,7 @@ mapSpecification mapType mapValue spec =
|> Value.mapSpecification mapType mapValue
|> Result.map (Tuple.pair valueName)
)
|> ResultList.toResult
|> ListOfResults.liftAllErrors
|> Result.map Dict.fromList
|> Result.mapError List.concat
in
Expand All @@ -157,7 +156,7 @@ mapSpecification mapType mapValue spec =
valuesResult


mapDefinition : (Type a -> Result e (Type b)) -> (Value a -> Value b) -> Definition a -> Result (List e) (Definition b)
mapDefinition : (Type a -> Result e (Type b)) -> (Value a -> Result e (Value b)) -> Definition a -> Result (List e) (Definition b)
mapDefinition mapType mapValue def =
let
typesResult : Result (List e) (Dict Name (AccessControlled (Type.Definition b)))
Expand All @@ -171,7 +170,7 @@ mapDefinition mapType mapValue def =
|> Result.map (AccessControlled typeDef.access)
|> Result.map (Tuple.pair typeName)
)
|> ResultList.toResult
|> ListOfResults.liftAllErrors
|> Result.map Dict.fromList
|> Result.mapError List.concat

Expand All @@ -186,7 +185,7 @@ mapDefinition mapType mapValue def =
|> Result.map (AccessControlled valueDef.access)
|> Result.map (Tuple.pair valueName)
)
|> ResultList.toResult
|> ListOfResults.liftAllErrors
|> Result.map Dict.fromList
|> Result.mapError List.concat
in
Expand Down
20 changes: 9 additions & 11 deletions src/Morphir/IR/Package.elm
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@ module Morphir.IR.Package exposing
-}

import Dict exposing (Dict)
import Json.Decode as Decode
import Json.Encode as Encode
import Morphir.IR.AccessControlled as AccessControlled exposing (AccessControlled, encodeAccessControlled, withPublicAccess)
import Morphir.IR.AccessControlled exposing (AccessControlled, encodeAccessControlled, withPublicAccess)
import Morphir.IR.Module as Module exposing (ModulePath)
import Morphir.IR.Path exposing (Path, encodePath)
import Morphir.IR.QName exposing (QName, encodeQName)
import Morphir.IR.Type as Type exposing (Type)
import Morphir.IR.Value as Value exposing (Value)
import Morphir.ResultList as ResultList
import Morphir.ListOfResults as ListOfResults


type alias PackagePath =
Expand Down Expand Up @@ -76,7 +74,7 @@ definitionToSpecification def =
}


mapSpecification : (Type a -> Result e (Type b)) -> (Value a -> Value b) -> Specification a -> Result (List e) (Specification b)
mapSpecification : (Type a -> Result e (Type b)) -> (Value a -> Result e (Value b)) -> Specification a -> Result (List e) (Specification b)
mapSpecification mapType mapValue spec =
let
modulesResult : Result (List e) (Dict Path (Module.Specification b))
Expand All @@ -89,7 +87,7 @@ mapSpecification mapType mapValue spec =
|> Module.mapSpecification mapType mapValue
|> Result.map (Tuple.pair modulePath)
)
|> ResultList.toResult
|> ListOfResults.liftAllErrors
|> Result.map Dict.fromList
|> Result.mapError List.concat
in
Expand All @@ -101,11 +99,11 @@ eraseSpecificationAttributes spec =
spec
|> mapSpecification
(Type.mapTypeAttributes (\_ -> ()) >> Ok)
(Value.mapValueAttributes (\_ -> ()))
(Value.mapValueAttributes (\_ -> ()) >> Ok)
|> Result.withDefault emptySpecification


mapDefinition : (Type a -> Result e (Type b)) -> (Value a -> Value b) -> Definition a -> Result (List e) (Definition b)
mapDefinition : (Type a -> Result e (Type b)) -> (Value a -> Result e (Value b)) -> Definition a -> Result (List e) (Definition b)
mapDefinition mapType mapValue def =
let
dependenciesResult : Result (List e) (Dict Path (Specification b))
Expand All @@ -118,7 +116,7 @@ mapDefinition mapType mapValue def =
|> mapSpecification mapType mapValue
|> Result.map (Tuple.pair packagePath)
)
|> ResultList.toResult
|> ListOfResults.liftAllErrors
|> Result.map Dict.fromList
|> Result.mapError List.concat

Expand All @@ -133,7 +131,7 @@ mapDefinition mapType mapValue def =
|> Result.map (AccessControlled moduleDef.access)
|> Result.map (Tuple.pair modulePath)
)
|> ResultList.toResult
|> ListOfResults.liftAllErrors
|> Result.map Dict.fromList
|> Result.mapError List.concat
in
Expand All @@ -147,7 +145,7 @@ eraseDefinitionAttributes def =
def
|> mapDefinition
(Type.mapTypeAttributes (\_ -> ()) >> Ok)
(Value.mapValueAttributes (\_ -> ()))
(Value.mapValueAttributes (\_ -> ()) >> Ok)
|> Result.withDefault emptyDefinition


Expand Down
10 changes: 5 additions & 5 deletions src/Morphir/IR/Type.elm
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ import Json.Encode as Encode
import Morphir.IR.AccessControlled as AccessControlled exposing (AccessControlled, encodeAccessControlled, withPublicAccess)
import Morphir.IR.FQName exposing (FQName, decodeFQName, encodeFQName, fuzzFQName)
import Morphir.IR.Name exposing (Name, decodeName, encodeName, fuzzName)
import Morphir.ListOfResults as ListOfResults
import Morphir.Pattern exposing (Pattern)
import Morphir.ResultList as ResultList
import Morphir.Rewrite exposing (Rewrite)


Expand Down Expand Up @@ -171,10 +171,10 @@ mapSpecification f spec =
f argType
|> Result.map (Tuple.pair argName)
)
|> ResultList.toResult
|> ListOfResults.liftAllErrors
|> Result.map (Constructor ctorName)
)
|> ResultList.toResult
|> ListOfResults.liftAllErrors
|> Result.mapError List.concat
in
ctorsResult
Expand Down Expand Up @@ -202,10 +202,10 @@ mapDefinition f def =
f argType
|> Result.map (Tuple.pair argName)
)
|> ResultList.toResult
|> ListOfResults.liftAllErrors
|> Result.map (Constructor ctorName)
)
|> ResultList.toResult
|> ListOfResults.liftAllErrors
|> Result.map (AccessControlled constructors.access)
|> Result.mapError List.concat
in
Expand Down
Loading

0 comments on commit f92b5f1

Please sign in to comment.