Added local environments to functions

This commit is contained in:
macocianradu
2021-12-01 15:13:06 +01:00
parent 7c7c5a4a35
commit a8c5438d79
3 changed files with 7 additions and 7 deletions

View File

@@ -36,8 +36,8 @@ func EquityPriceObservation: <"Function specification for the observation of an
func Something: <"asd"> func Something: <"asd">
inputs: inputs:
equity1 boolean (1..1) equity1 boolean (1..1)
something1 boolean (1..1) determinationMethod ObservationPrimitive (1..1)
output: output:
valuation ObservationPrimitive (1..1) valuation ObservationPrimitive (0..1)
assign-output: if True and False then determinationMethod else determinationMethod assign-output: if True and False then determinationMethod

View File

@@ -59,13 +59,12 @@ defaultMap = [
-- |Checks whether a function is valid (inputs, outputs are of valid type and all variables are defined) and adds it to the symbol table -- |Checks whether a function is valid (inputs, outputs are of valid type and all variables are defined) and adds it to the symbol table
addFunction :: ([Type], [Symbol]) -> Function -> Either [TypeCheckError] [Symbol] addFunction :: ([Type], [Symbol]) -> Function -> Either [TypeCheckError] [Symbol]
addFunction (definedTypes, definedSymbols) (MakeFunction name _ inps out _) addFunction (definedTypes, definedSymbols) (MakeFunction name _ inps out _)
| null (lefts checkedInputs) && isRight checkedOutput = Right $ Func name (map typeAndCardinality (rights checkedInputs)) (attributeType $ fromRightUnsafe checkedOutput, Model.Type.cardinality out) : allSymbols | null (lefts checkedInputs) && isRight checkedOutput = Right $ Func name (map typeAndCardinality (rights checkedInputs)) (attributeType $ fromRightUnsafe checkedOutput, Model.Type.cardinality out) : definedSymbols
| isLeft checkedOutput = Left [fromLeftUnsafe checkedOutput] | isLeft checkedOutput = Left [fromLeftUnsafe checkedOutput]
| otherwise = Left $ lefts checkedInputs | otherwise = Left $ lefts checkedInputs
where where
checkedInputs = checkAttributes definedTypes inps checkedInputs = checkAttributes definedTypes inps
checkedOutput = head $ checkAttributes definedTypes [out] checkedOutput = head $ checkAttributes definedTypes [out]
allSymbols = addVariables definedSymbols inps
-- |Adds a newly defined variable to the symbol table -- |Adds a newly defined variable to the symbol table
addVariables :: [Symbol] -> [TypeAttribute] -> [Symbol] addVariables :: [Symbol] -> [TypeAttribute] -> [Symbol]

View File

@@ -9,13 +9,14 @@ import Data.Char
-- |Checks if all the inputs and the out of a function call have valid types, and then checks that the assign-output expression is valid -- |Checks if all the inputs and the out of a function call have valid types, and then checks that the assign-output expression is valid
checkFunction :: ([Type], [Symbol]) -> Function -> Either [TypeCheckError] Function checkFunction :: ([Type], [Symbol]) -> Function -> Either [TypeCheckError] Function
checkFunction (definedTypes, definedFunctions) (MakeFunction name desc inp out ex) checkFunction (definedTypes, symbols) (MakeFunction name desc inp out ex)
| isRight checkedEx && isRight checkedOut && null (lefts checkedIn) = | isRight checkedEx && isRight checkedOut && null (lefts checkedIn) =
case typeMatch (attributeType $ fromRightUnsafe checkedOut, Model.Type.cardinality out) (fromRightUnsafe checkedEx) of case typeMatch (attributeType $ fromRightUnsafe checkedOut, Model.Type.cardinality out) (fromRightUnsafe checkedEx) of
Right _ -> Right $ MakeFunction (toLower (head name) : tail name) desc (rights checkedIn) (fromRightUnsafe checkedOut) ex Right _ -> Right $ MakeFunction (toLower (head name) : tail name) desc (rights checkedIn) (fromRightUnsafe checkedOut) ex
Left err -> Left [err] Left err -> Left [err]
| otherwise = Left $ lefts [checkedOut] ++ lefts checkedIn ++ lefts [checkedEx] | otherwise = Left $ lefts [checkedOut] ++ lefts checkedIn ++ lefts [checkedEx]
where where
checkedEx = checkExpression definedFunctions ex localEnv = addVariables symbols inp
checkedEx = checkExpression localEnv ex
checkedIn = checkAttributes definedTypes inp checkedIn = checkAttributes definedTypes inp
checkedOut = head $ checkAttributes definedTypes [out] checkedOut = head $ checkAttributes definedTypes [out]