Backport rules from simplify

This commit is contained in:
Jeroen Engels 2023-09-03 11:30:45 +02:00
parent 57fa48e5f2
commit b7842e704b
8 changed files with 8164 additions and 4672 deletions

View File

@ -20,7 +20,7 @@
"elm/json": "1.1.3 <= v < 2.0.0",
"elm/project-metadata-utils": "1.0.0 <= v < 2.0.0",
"elm-explorations/test": "2.0.1 <= v < 3.0.0",
"stil4m/elm-syntax": "7.2.7 <= v < 8.0.0"
"stil4m/elm-syntax": "7.3.2 <= v < 8.0.0"
},
"test-dependencies": {
"elm/parser": "1.1.0 <= v < 2.0.0",

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,7 +1,8 @@
module Simplify.Evaluate exposing (getBoolean, getInt, isAlwaysBoolean)
module Simplify.Evaluate exposing (getBoolean, getInt, isAlwaysBoolean, isEqualToSomethingFunction)
import Elm.Syntax.Expression as Expression exposing (Expression)
import Elm.Syntax.Node as Node exposing (Node(..))
import Elm.Syntax.Pattern as Pattern
import Review.ModuleNameLookupTable as ModuleNameLookupTable
import Simplify.AstHelpers as AstHelpers
import Simplify.Infer as Infer
@ -81,6 +82,41 @@ isAlwaysBoolean resources node =
Undetermined
isEqualToSomethingFunction : Node Expression -> Maybe { something : Node Expression }
isEqualToSomethingFunction rawNode =
case Node.value (AstHelpers.removeParens rawNode) of
Expression.Application ((Node _ (Expression.PrefixOperator "==")) :: expr :: []) ->
Just { something = expr }
Expression.LambdaExpression lambda ->
case lambda.args of
[ Node _ (Pattern.VarPattern var) ] ->
case Node.value (AstHelpers.removeParens lambda.expression) of
Expression.OperatorApplication "==" _ left right ->
let
nodeToFind : Expression
nodeToFind =
Expression.FunctionOrValue [] var
in
if Node.value left == nodeToFind then
Just { something = right }
else if Node.value right == nodeToFind then
Just { something = left }
else
Nothing
_ ->
Nothing
_ ->
Nothing
_ ->
Nothing
getInt : Infer.Resources a -> Node Expression -> Maybe Int
getInt resources baseNode =
let

View File

@ -1,14 +1,12 @@
module Simplify.Match exposing
( Match(..)
, map
, maybeAndThen
, map, traverse
)
{-|
@docs Match
@docs map
@docs maybeAndThen
@docs map, traverse
-}
@ -28,11 +26,24 @@ map mapper match =
Undetermined
maybeAndThen : (a -> Match b) -> Maybe a -> Match b
maybeAndThen fn maybe =
case maybe of
Just a ->
fn a
{-| If all mapped elements are Determined, returns a List of the Determined values.
If any match is Undetermined, returns Undetermined
-}
traverse : (a -> Match b) -> List a -> Match (List b)
traverse f list =
traverseHelp f list []
Nothing ->
Undetermined
traverseHelp : (a -> Match b) -> List a -> List b -> Match (List b)
traverseHelp f list acc =
case list of
head :: tail ->
case f head of
Determined a ->
traverseHelp f tail (a :: acc)
Undetermined ->
Undetermined
[] ->
Determined (List.reverse acc)

View File

@ -148,6 +148,9 @@ normalize resources node =
Expression.Floatable float ->
toNode (Expression.Floatable -float)
Expression.Negation subExpr ->
subExpr
_ ->
toNode (Expression.Negation normalized)
@ -378,6 +381,14 @@ compareHelp leftNode right canFlip =
Expression.Floatable left ->
compareNumbers left right
Expression.Negation left ->
case Node.value (removeParens right) of
Expression.Negation rightValue ->
compareHelp left rightValue canFlip
_ ->
fallback ()
Expression.OperatorApplication leftOp _ leftLeft leftRight ->
if List.member leftOp [ "+", "-", "*", "/" ] then
case getNumberValue leftNode of

View File

@ -236,7 +236,7 @@ simpleNormalizationTests =
, n (OperatorApplication "^" Infix.Right (n (FunctionOrValue [] "a")) (n (FunctionOrValue [] "b")))
, n (OperatorApplication "&&" Infix.Right (n (FunctionOrValue [] "a")) (n (FunctionOrValue [] "b")))
, n (OperatorApplication "||" Infix.Right (n (FunctionOrValue [] "a")) (n (FunctionOrValue [] "b")))
, n (OperatorApplication "</>" Infix.Left (n (FunctionOrValue [] "a")) (n (FunctionOrValue [] "b")))
, n (OperatorApplication "</>" Infix.Right (n (FunctionOrValue [] "a")) (n (FunctionOrValue [] "b")))
, n (OperatorApplication "<?>" Infix.Left (n (FunctionOrValue [] "a")) (n (FunctionOrValue [] "b")))
, n (OperatorApplication "|." Infix.Left (n (FunctionOrValue [] "a")) (n (FunctionOrValue [] "b")))
, n (OperatorApplication "|=" Infix.Left (n (FunctionOrValue [] "a")) (n (FunctionOrValue [] "b")))

File diff suppressed because it is too large Load Diff