From 76830a38781cd9c9190ee31c5f8b86fc3dcdaeaf Mon Sep 17 00:00:00 2001 From: Nwokafor Choongsaeng Date: Sun, 23 Jul 2023 16:52:14 +0000 Subject: [PATCH] replace variable exhaustive evaluation with lambda argument evaluation --- src/Morphir/Value/Interpreter.elm | 27 +++++++------------ .../src/Morphir/Reference/Model/Values.elm | 11 ++++++++ 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/Morphir/Value/Interpreter.elm b/src/Morphir/Value/Interpreter.elm index e9ba4c709..a293f6fca 100644 --- a/src/Morphir/Value/Interpreter.elm +++ b/src/Morphir/Value/Interpreter.elm @@ -183,16 +183,6 @@ evaluateValue ({ allowPartial } as config) nativeFunctions ir variables argument |> Result.fromMaybe (VariableNotFound varName) -- Wrap the error to make it easier to understand where it happened |> Result.mapError (ErrorWhileEvaluatingVariable varName) - |> Result.andThen - (\stateValue -> - if stateValue == value then - -- some variables may remain unresolved - Ok stateValue - - else - -- evaluate value of the variable - evaluateValue config nativeFunctions ir variables [] stateValue - ) Value.Reference _ fQName -> -- We check if there is a native function first @@ -315,13 +305,16 @@ evaluateValue ({ allowPartial } as config) nativeFunctions ir variables argument -- In Morphir (just like in Elm) you can pattern-match on the argument of a lambda. |> Result.andThen (\argumentValue -> - -- To match the pattern we call a helper function that both matches and extracts variables out - -- of the pattern. - matchPattern argumentPattern argumentValue - -- If the pattern does not match we error out. This should never happen with valid - -- expressions as lambda argument patterns should only be used for decomposition not - -- filtering. - |> Result.mapError LambdaArgumentDidNotMatch + evaluateValue config nativeFunctions ir variables [] argumentValue + -- To match the pattern we call a helper function that both matches and extracts variables out + -- of the pattern. + |> Result.andThen + (matchPattern argumentPattern + -- If the pattern does not match we error out. This should never happen with valid + -- expressions as lambda argument patterns should only be used for decomposition not + -- filtering. + >> Result.mapError LambdaArgumentDidNotMatch + ) ) -- Finally we evaluate the body of the lambda using the variables extracted by the pattern. |> Result.andThen diff --git a/tests-integration/reference-model/src/Morphir/Reference/Model/Values.elm b/tests-integration/reference-model/src/Morphir/Reference/Model/Values.elm index 14ebb3144..24370f7a5 100644 --- a/tests-integration/reference-model/src/Morphir/Reference/Model/Values.elm +++ b/tests-integration/reference-model/src/Morphir/Reference/Model/Values.elm @@ -93,6 +93,17 @@ basicRecordMany = , record = basicRecordOne } +type alias Rec = + { foo: Int + , bar: String + } + +calcFoo: Rec -> Int +calcFoo rec = + rec.foo + +res = calcFoo { foo = 2, bar = "hello" } + basicField : { foo : String } -> String basicField rec =