Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to built-in rules until new.yaml rules work #619

Merged
merged 19 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 62 additions & 12 deletions eo-phi-normalizer/src/Language/EO/Phi/Dataize.hs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pattern AsObject obj <- (desugarAsBytes -> Left obj)

-- | Perform one step of dataization to the object (if possible).
dataizeStep :: Context -> Object -> (Context, Either Object Bytes)
dataizeStep ctx obj = snd $ head $ runChain (dataizeStepChain obj) ctx -- FIXME: head is bad
dataizeStep ctx obj = snd $ head $ runChain (dataizeStepChain DataizeAll obj) ctx -- FIXME: head is bad

dataizeStep' :: Context -> Object -> Either Object Bytes
dataizeStep' ctx obj = snd (dataizeStep ctx obj)
Expand All @@ -75,12 +75,15 @@ dataizeRecursively :: Context -> Object -> Either Object Bytes
dataizeRecursively ctx obj = snd $ dataizeRecursivelyChain' ctx obj

dataizeStepChain' :: Context -> Object -> ([LogEntry (Either Object Bytes)], Either Object Bytes)
dataizeStepChain' ctx obj = snd <$> head (runChain (dataizeStepChain obj) ctx) -- FIXME: head is bad
dataizeStepChain' ctx obj = snd <$> head (runChain (dataizeStepChain DataizeAll obj) ctx) -- FIXME: head is bad

data DataizeStepMode = DataizeOnlyLambda | DataizeAll

-- | Perform one step of dataization to the object (if possible), reporting back individiual steps.
dataizeStepChain :: Object -> DataizeChain (Context, Either Object Bytes)
dataizeStepChain obj@(Formation bs)
| Just (DeltaBinding bytes) <- listToMaybe [b | b@(DeltaBinding _) <- bs]
dataizeStepChain :: DataizeStepMode -> Object -> DataizeChain (Context, Either Object Bytes)
dataizeStepChain mode obj@(Formation bs)
| DataizeAll <- mode
, Just (DeltaBinding bytes) <- listToMaybe [b | b@(DeltaBinding _) <- bs]
, not hasEmpty = do
logStep "Found bytes" (AsBytes bytes)
ctx <- getContext
Expand All @@ -102,7 +105,8 @@ dataizeStepChain obj@(Formation bs)
Just ((obj', _state), _alts) -> do
ctx <- getContext
return (ctx, AsObject obj')
| Just (AlphaBinding Phi decoratee) <- listToMaybe [b | b@(AlphaBinding Phi _) <- bs]
| DataizeAll <- mode
, Just (AlphaBinding Phi decoratee) <- listToMaybe [b | b@(AlphaBinding Phi _) <- bs]
, not hasEmpty = do
let decoratee' = substThis obj decoratee
logStep "Dataizing inside phi" (AsObject decoratee')
Expand All @@ -119,22 +123,22 @@ dataizeStepChain obj@(Formation bs)
isEmpty _ = False
hasEmpty = any isEmpty bs
-- IMPORTANT: dataize the object being copied IF normalization is stuck on it!
dataizeStepChain (Application obj bindings) = incLogLevel $ do
dataizeStepChain _mode (Application obj bindings) = incLogLevel $ do
logStep "Dataizing inside application" (AsObject obj)
modifyContext (\c -> c{dataizePackage = False}) $ do
(ctx, obj') <- dataizeStepChain obj
(ctx, obj') <- dataizeStepChain DataizeOnlyLambda obj
case obj' of
Left obj'' -> return (ctx, AsObject (obj'' `Application` bindings))
Right bytes -> return (ctx, AsObject (Formation [DeltaBinding bytes] `Application` bindings))
-- IMPORTANT: dataize the object being dispatched IF normalization is stuck on it!
dataizeStepChain (ObjectDispatch obj attr) = incLogLevel $ do
dataizeStepChain _mode (ObjectDispatch obj attr) = incLogLevel $ do
logStep "Dataizing inside dispatch" (AsObject obj)
modifyContext (\c -> c{dataizePackage = False}) $ do
(ctx, obj') <- dataizeStepChain obj
(ctx, obj') <- dataizeStepChain DataizeOnlyLambda obj
case obj' of
Left obj'' -> return (ctx, AsObject (obj'' `ObjectDispatch` attr))
Right bytes -> return (ctx, AsObject (Formation [DeltaBinding bytes] `ObjectDispatch` attr))
dataizeStepChain obj = do
dataizeStepChain _mode obj = do
logStep "Nothing to dataize" (AsObject obj)
ctx <- getContext
return (ctx, AsObject obj)
Expand Down Expand Up @@ -166,7 +170,7 @@ dataizeRecursivelyChain = fmap minimizeObject' . go
Just (normObj, _alternatives)
| normObj == obj && normalizeRequired -> return (AsObject obj)
| otherwise -> do
(ctx', step) <- dataizeStepChain normObj
(ctx', step) <- dataizeStepChain DataizeAll normObj
case step of
(AsObject stillObj)
| stillObj == normObj && ctx `sameContext` ctx' -> do
Expand Down Expand Up @@ -250,6 +254,49 @@ evaluateBinaryDataizationFunChain resultToBytes bytesToParam wrapBytes arg1 arg2
fail (name <> ": Couldn't find bytes in RHS: " <> printTree (hideRho r))
return (result, ())

evaluatePartialBinaryDataizationFunChain ::
-- | How to convert the result back to bytes
(res -> Bytes) ->
-- | How to interpret the bytes in terms of the given data type
(Bytes -> a) ->
-- | How to wrap the bytes in an object
(Bytes -> Object) ->
-- | Extract the 1st argument to be dataized
(Object -> Object) ->
-- | Extract the 2nd argument to be dataized
(Object -> Object) ->
-- | A binary function on the argument
(a -> a -> Maybe res) ->
-- | Name of the atom.
String ->
Object ->
EvaluationState ->
DataizeChain (Object, EvaluationState)
evaluatePartialBinaryDataizationFunChain resultToBytes bytesToParam wrapBytes arg1 arg2 func name obj _state = do
let lhsArg = arg1 obj
let rhsArg = arg2 obj
lhs <- incLogLevel $ do
logStep "Evaluating LHS" (AsObject lhsArg)
dataizeRecursivelyChain True lhsArg
rhs <- incLogLevel $ do
logStep "Evaluating RHS" (AsObject rhsArg)
dataizeRecursivelyChain True rhsArg
result <- case (lhs, rhs) of
(AsBytes l, AsBytes r) -> do
case resultToBytes <$> bytesToParam l `func` bytesToParam r of
Nothing -> fail (name <> ": throws an error")
Just bytes -> do
let resultObj = wrapBytes bytes
logStep "Evaluated function" (AsObject resultObj)
return resultObj
(AsObject _l, AsObject _r) ->
fail (name <> ": Couldn't find bytes in both LHS and RHS")
(AsObject l, _) -> do
fail (name <> ": Couldn't find bytes in LHS: " <> printTree (hideRho l))
(_, AsObject r) -> do
fail (name <> ": Couldn't find bytes in RHS: " <> printTree (hideRho r))
return (result, ())

-- | Unary functions operate on the given object without any additional parameters
evaluateUnaryDataizationFunChain ::
-- | How to convert the result back to bytes
Expand All @@ -273,6 +320,9 @@ evaluateUnaryDataizationFunChain resultToBytes bytesToParam wrapBytes extractArg
evaluateIntIntIntFunChain :: (Int -> Int -> Int) -> String -> Object -> EvaluationState -> DataizeChain (Object, EvaluationState)
evaluateIntIntIntFunChain = evaluateBinaryDataizationFunChain intToBytes bytesToInt wrapBytesInConstInt extractRho (extractLabel "x")

evaluateIntIntMaybeIntFunChain :: (Int -> Int -> Maybe Int) -> String -> Object -> EvaluationState -> DataizeChain (Object, EvaluationState)
evaluateIntIntMaybeIntFunChain = evaluatePartialBinaryDataizationFunChain intToBytes bytesToInt wrapBytesInConstInt extractRho (extractLabel "x")

evaluateIntIntBoolFunChain :: (Int -> Int -> Bool) -> String -> Object -> EvaluationState -> DataizeChain (Object, EvaluationState)
evaluateIntIntBoolFunChain = evaluateBinaryDataizationFunChain boolToBytes bytesToInt wrapBytesAsBool extractRho (extractLabel "x")

Expand Down
95 changes: 77 additions & 18 deletions eo-phi-normalizer/src/Language/EO/Phi/Dataize/Atoms.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,17 @@ import Language.EO.Phi.Syntax

knownAtomsList :: [(String, String -> Object -> EvaluationState -> DataizeChain (Object, EvaluationState))]
knownAtomsList =
-- [ ("Lorg_eolang_as_phi", _)
[ ("Lorg_eolang_int_gt", evaluateIntIntBoolFunChain (>))
, ("Lorg_eolang_int_plus", evaluateIntIntIntFunChain (+))
, ("Lorg_eolang_int_times", evaluateIntIntIntFunChain (*))
, ("Lorg_eolang_int_div", evaluateIntIntIntFunChain quot)
, ("Lorg_eolang_bytes_and", evaluateBytesBytesBytesFunChain (.&.))
, ("Lorg_eolang_bytes_concat", evaluateBinaryDataizationFunChain id id wrapBytesInBytes extractRho (extractLabel "b") concatBytes)
, ("Lorg_eolang_bytes_eq", evaluateBinaryDataizationFunChain boolToBytes bytesToInt wrapBytesAsBool extractRho (extractLabel "b") (==))
, ("Lorg_eolang_bytes_not", evaluateBytesBytesFunChain complement)
, ("Lorg_eolang_bytes_or", evaluateBytesBytesBytesFunChain (.|.))
, ("Lorg_eolang_bytes_right", evaluateBinaryDataizationFunChain intToBytes bytesToInt wrapBytesInBytes extractRho (extractLabel "x") (\x i -> shift x (-i)))
,
( "Lorg_eolang_bytes_size"
, let f = evaluateUnaryDataizationFunChain intToBytes id wrapBytesInBytes extractRho (\(Bytes bytes) -> length (words (map dashToSpace bytes)))
Expand All @@ -54,24 +60,71 @@ knownAtomsList =
bytes <- case thisStr of
AsBytes bytes -> pure bytes
AsObject _ -> fail "Couldn't find bytes"
evaluateBinaryDataizationFunChain id bytesToInt wrapBytesInBytes (extractLabel "start") (extractLabel "len") (sliceBytes bytes) name obj state
evaluateBinaryDataizationFunChain id bytesToFloat wrapBytesInBytes (extractLabel "start") (extractLabel "len") (\start len -> sliceBytes bytes (floor start) (floor len)) name obj state
)
, ("Lorg_eolang_bytes_and", evaluateBytesBytesBytesFunChain (.&.))
, ("Lorg_eolang_bytes_or", evaluateBytesBytesBytesFunChain (.|.))
, ("Lorg_eolang_bytes_xor", evaluateBytesBytesBytesFunChain (.^.))
, ("Lorg_eolang_bytes_not", evaluateBytesBytesFunChain complement)
, ("Lorg_eolang_bytes_right", evaluateBinaryDataizationFunChain intToBytes bytesToInt wrapBytesInBytes extractRho (extractLabel "x") (\x i -> shift x (-i)))
, ("Lorg_eolang_bytes_concat", evaluateBinaryDataizationFunChain id id wrapBytesInBytes extractRho (extractLabel "b") concatBytes)
, -- deprecated
("Lorg_eolang_dataized", evaluateUnaryDataizationFunChain id id wrapBytesInBytes (extractLabel "target") id)
, -- , ("Lorg_eolang_cage_encaged_encage", _)
-- , ("Lorg_eolang_cage_encaged_φ", _)
-- , ("Lorg_eolang_cage_φ", _)
("Lorg_eolang_error", evaluateUnaryDataizationFunChain stringToBytes bytesToString wrapBytesInBytes (extractLabel "message") error)
, -- float
-- deprecated
("Lorg_eolang_float_gt", evaluateBinaryDataizationFunChain boolToBytes bytesToFloat wrapBytesInBytes extractRho (extractLabel "x") (>))
, -- deprecated
("Lorg_eolang_float_times", evaluateFloatFloatFloatFunChain (*))
, -- deprecated
("Lorg_eolang_float_plus", evaluateFloatFloatFloatFunChain (+))
, -- deprecated
("Lorg_eolang_float_div", evaluateFloatFloatFloatFunChain (/))
, -- deprecated
("Lorg_eolang_float_gt", evaluateBinaryDataizationFunChain boolToBytes bytesToFloat wrapBytesInBytes extractRho (extractLabel "x") (>))
, ("Lorg_eolang_float_times", evaluateFloatFloatFloatFunChain (*))
, ("Lorg_eolang_float_plus", evaluateFloatFloatFloatFunChain (+))
, ("Lorg_eolang_float_div", evaluateFloatFloatFloatFunChain (/))
, ("Lorg_eolang_float_gt", evaluateBinaryDataizationFunChain boolToBytes bytesToFloat wrapBytesInBytes extractRho (extractLabel "x") (>))
, ("Lorg_eolang_float_times", evaluateFloatFloatFloatFunChain (*))
, ("Lorg_eolang_float_plus", evaluateFloatFloatFloatFunChain (+))
, ("Lorg_eolang_float_div", evaluateFloatFloatFloatFunChain (/))
, -- string
, -- deprecated
("Lorg_eolang_float_times", evaluateFloatFloatFloatFunChain (*))
, -- deprecated
("Lorg_eolang_float_plus", evaluateFloatFloatFloatFunChain (+))
, -- deprecated
("Lorg_eolang_float_div", evaluateFloatFloatFloatFunChain (/))
, -- , ("Lorg_eolang_fs_dir_made_mkdir", _)
-- , ("Lorg_eolang_fs_dir_tmpfile_touch", _)
-- , ("Lorg_eolang_fs_dir_walk", _)
-- , ("Lorg_eolang_fs_file_deleted_delete", _)
-- , ("Lorg_eolang_fs_file_exists", _)
-- , ("Lorg_eolang_fs_file_is_directory", _)
-- , ("Lorg_eolang_fs_file_moved_move", _)
-- , ("Lorg_eolang_fs_file_open_file_stream_read_read_bytes", _)
-- , ("Lorg_eolang_fs_file_open_file_stream_write_written_bytes", _)
-- , ("Lorg_eolang_fs_file_open_process_file", _)
-- , ("Lorg_eolang_fs_file_size", _)
-- , ("Lorg_eolang_fs_file_touched_touch", _)
("Lorg_eolang_i16_as_i32", evaluateUnaryDataizationFunChain int16ToBytes bytesToInt64 wrapBytesInConstInt extractRho fromIntegral)
, ("Lorg_eolang_i32_as_i64", evaluateUnaryDataizationFunChain int32ToBytes bytesToInt64 wrapBytesInConstInt extractRho fromIntegral)
, ("Lorg_eolang_i64_as_number", evaluateUnaryDataizationFunChain floatToBytes bytesToInt64 wrapBytesInConstInt extractRho fromIntegral)
, ("Lorg_eolang_i64_div", evaluateIntIntMaybeIntFunChain (\x y -> if y == 0 then Nothing else Just (x `quot` y)))
, ("Lorg_eolang_i64_gt", evaluateIntIntBoolFunChain (>))
, ("Lorg_eolang_i64_plus", evaluateIntIntIntFunChain (+))
, ("Lorg_eolang_i64_times", evaluateIntIntIntFunChain (*))
, -- , ("Lorg_eolang_malloc_of_allocated_read", _)
-- , ("Lorg_eolang_malloc_of_allocated_resize", _)
-- , ("Lorg_eolang_malloc_of_allocated_size", _)
-- , ("Lorg_eolang_malloc_of_allocated_write", _)
-- , ("Lorg_eolang_malloc_of_φ", _)
("Lorg_eolang_math_angle_cos", evaluateUnaryDataizationFunChain floatToBytes bytesToFloat wrapBytesInConstFloat extractRho cos)
, ("Lorg_eolang_math_angle_sin", evaluateUnaryDataizationFunChain floatToBytes bytesToFloat wrapBytesInConstFloat extractRho sin)
, ("Lorg_eolang_math_real_acos", evaluateUnaryDataizationFunChain floatToBytes bytesToFloat wrapBytesInConstFloat extractRho acos)
, ("Lorg_eolang_math_real_asin", evaluateUnaryDataizationFunChain floatToBytes bytesToFloat wrapBytesInConstFloat extractRho asin)
, ("Lorg_eolang_math_real_ln", evaluateUnaryDataizationFunChain floatToBytes bytesToFloat wrapBytesInConstFloat extractRho log)
, ("Lorg_eolang_math_real_pow", evaluateFloatFloatFloatFunChain (**))
, ("Lorg_eolang_math_real_sqrt", evaluateUnaryDataizationFunChain floatToBytes bytesToFloat wrapBytesInConstFloat extractRho sqrt)
, ("Lorg_eolang_number_as_i64", evaluateUnaryDataizationFunChain int64ToBytes bytesToFloat wrapBytesInConstInt extractRho round)
, ("Lorg_eolang_number_div", evaluateFloatFloatFloatFunChain (/))
, ("Lorg_eolang_number_floor", evaluateUnaryDataizationFunChain int64ToBytes bytesToFloat wrapBytesInConstInt extractRho floor)
, ("Lorg_eolang_number_gt", evaluateBinaryDataizationFunChain boolToBytes bytesToFloat wrapBytesInBytes extractRho (extractLabel "x") (>))
, ("Lorg_eolang_number_plus", evaluateFloatFloatFloatFunChain (+))
, ("Lorg_eolang_number_times", evaluateFloatFloatFloatFunChain (*))
, -- , ("Lorg_eolang_rust", _)
-- string
("Lorg_eolang_string_length", evaluateUnaryDataizationFunChain intToBytes bytesToString wrapBytesInConstInt extractRho length)
,
( "Lorg_eolang_string_slice"
Expand All @@ -80,12 +133,18 @@ knownAtomsList =
string <- case thisStr of
AsBytes bytes -> pure $ bytesToString bytes
AsObject _ -> fail "Couldn't find bytes"
evaluateBinaryDataizationFunChain stringToBytes bytesToInt wrapBytesInConstString (extractLabel "start") (extractLabel "len") (\start len -> take len (drop start string)) name obj state
evaluateBinaryDataizationFunChain stringToBytes bytesToFloat wrapBytesInConstString (extractLabel "start") (extractLabel "len") (\start len -> take (floor len) (drop (floor start) string)) name obj state
)
, -- others
("Lorg_eolang_dataized", evaluateUnaryDataizationFunChain id id wrapBytesInBytes (extractLabel "target") id)
, ("Lorg_eolang_error", evaluateUnaryDataizationFunChain stringToBytes bytesToString wrapBytesInBytes (extractLabel "message") error)
,
-- , ("Lorg_eolang_sys_os_name", _)
-- , ("Lorg_eolang_sys_posix_φ", _)
-- , ("Lorg_eolang_sys_win32_φ", _)
-- , ("Lorg_eolang_try", _)
-- , ("Lorg_eolang_txt_regex_compiled", _)
-- , ("Lorg_eolang_txt_regex_pattern_match_matched_from_index", _)
-- , ("Lorg_eolang_txt_sprintf", _)
-- , ("Lorg_eolang_txt_sscanf", _)

( "Package"
, let
f _name obj@(Formation bindings) = do
Expand Down
Loading