From 05f9018b4132df7e1585cdaa447bf3348c507ea2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20R=C3=A4tzel?= Date: Sat, 21 Oct 2023 21:03:25 +0000 Subject: [PATCH] Simplify kernel functions unifying integer and logical negation Unify the negation of integer and boolean values in a common kernel function. --- .../src/ElmInteractive.elm | 4 +-- .../src/ElmInteractiveCoreModules.elm | 6 ++-- .../ElmTime/compile-elm-program/src/Pine.elm | 31 +++++++++++++------ .../elm-time/Pine/PineVM/KernelFunction.cs | 28 ++++++++++++----- implement/elm-time/Pine/PineVM/PineVM.cs | 3 +- implement/elm-time/Program.cs | 2 +- implement/elm-time/elm-time.csproj | 4 +-- 7 files changed, 52 insertions(+), 26 deletions(-) diff --git a/implement/elm-time/ElmTime/compile-elm-program/src/ElmInteractive.elm b/implement/elm-time/ElmTime/compile-elm-program/src/ElmInteractive.elm index 006328e5..0a364b8f 100644 --- a/implement/elm-time/ElmTime/compile-elm-program/src/ElmInteractive.elm +++ b/implement/elm-time/ElmTime/compile-elm-program/src/ElmInteractive.elm @@ -1194,7 +1194,7 @@ compileElmSyntaxExpression stack elmExpression = Ok negatedExpression -> Ok (KernelApplicationExpression - { functionName = "neg_int" + { functionName = "negate" , argument = negatedExpression } ) @@ -1841,7 +1841,7 @@ compileElmSyntaxPattern elmPattern = conditionExpressions = \deconstructedExpression -> [ [ KernelApplicationExpression - { functionName = "logical_not" + { functionName = "negate" , argument = equalCondition [ deconstructedExpression diff --git a/implement/elm-time/ElmTime/compile-elm-program/src/ElmInteractiveCoreModules.elm b/implement/elm-time/ElmTime/compile-elm-program/src/ElmInteractiveCoreModules.elm index 44ed4089..9ff69eac 100644 --- a/implement/elm-time/ElmTime/compile-elm-program/src/ElmInteractiveCoreModules.elm +++ b/implement/elm-time/ElmTime/compile-elm-program/src/ElmInteractiveCoreModules.elm @@ -60,7 +60,7 @@ eq a b = neq : a -> a -> Bool neq a b = - Pine_kernel.logical_not (Pine_kernel.equal [ a, b ]) + Pine_kernel.negate (Pine_kernel.equal [ a, b ]) add : number -> number -> number @@ -105,7 +105,7 @@ lt : comparable -> comparable -> Bool lt a b = Pine_kernel.logical_and [ (le a b) - , Pine_kernel.logical_not (Pine_kernel.equal [a, b]) + , Pine_kernel.negate (Pine_kernel.equal [a, b]) ] @@ -113,7 +113,7 @@ gt : comparable -> comparable -> Bool gt a b = Pine_kernel.logical_and [ (ge a b) - , Pine_kernel.logical_not (Pine_kernel.equal [a, b]) + , Pine_kernel.negate (Pine_kernel.equal [a, b]) ] diff --git a/implement/elm-time/ElmTime/compile-elm-program/src/Pine.elm b/implement/elm-time/ElmTime/compile-elm-program/src/Pine.elm index 2bb43c68..a4ba8531 100644 --- a/implement/elm-time/ElmTime/compile-elm-program/src/Pine.elm +++ b/implement/elm-time/ElmTime/compile-elm-program/src/Pine.elm @@ -167,10 +167,8 @@ kernelFunctions = >> valueFromBool >> Ok ) - , ( "logical_not" - , boolFromValue - >> Result.fromMaybe (DescribePathEnd "Value is neither True nor False") - >> Result.map (not >> valueFromBool) + , ( "negate" + , kernelFunction_Negate ) , ( "logical_and", kernelFunctionExpectingListOfTypeBool (List.foldl (&&) True) ) , ( "logical_or", kernelFunctionExpectingListOfTypeBool (List.foldl (||) False) ) @@ -236,11 +234,6 @@ kernelFunctions = >> Result.map (List.head >> Maybe.withDefault (ListValue [])) >> Result.mapError DescribePathEnd ) - , ( "neg_int" - , bigIntFromValue - >> Result.mapError DescribePathEnd - >> Result.map (BigInt.negate >> valueFromBigInt) - ) , ( "add_int" , kernelFunctionExpectingListOfBigIntWithAtLeastOneAndProducingBigInt (List.foldl BigInt.add) ) @@ -260,6 +253,26 @@ kernelFunctions = |> Dict.fromList +kernelFunction_Negate : KernelFunction +kernelFunction_Negate value = + Ok + (case value of + BlobValue blob -> + case blob of + 4 :: rest -> + BlobValue (2 :: rest) + + 2 :: rest -> + BlobValue (4 :: rest) + + _ -> + ListValue [] + + ListValue _ -> + ListValue [] + ) + + list_all_same : List a -> Bool list_all_same list = case list of diff --git a/implement/elm-time/Pine/PineVM/KernelFunction.cs b/implement/elm-time/Pine/PineVM/KernelFunction.cs index 4691a066..bff45795 100644 --- a/implement/elm-time/Pine/PineVM/KernelFunction.cs +++ b/implement/elm-time/Pine/PineVM/KernelFunction.cs @@ -42,9 +42,27 @@ public static Result equal(PineValue valueA, PineValue valueB Result.ok( PineVM.ValueFromBool(valueA.Equals(valueB))); - public static Result logical_not(PineValue value) => - PineVM.DecodeBoolFromValue(value) - .Map(b => PineVM.ValueFromBool(!b)); + public static Result negate(PineValue value) => + Result.ok( + value switch + { + PineValue.BlobValue blobValue + when 0 < blobValue.Bytes.Length => + blobValue.Bytes.Span[0] switch + { + 4 => + PineValue.Blob(CommonConversion.Concat((ReadOnlySpan)[2], blobValue.Bytes.Span[1..])), + + 2 => + PineValue.Blob(CommonConversion.Concat((ReadOnlySpan)[4], blobValue.Bytes.Span[1..])), + + _ => + PineValue.EmptyList + }, + + _ => + PineValue.EmptyList + }); public static Result logical_and(PineValue value) => KernelFunctionExpectingListOfTypeBool(bools => bools.Aggregate(seed: true, func: (a, b) => a && b), value); @@ -131,10 +149,6 @@ public static Result list_head(PineValue value) => PineVM.DecodePineListValue(value) .Map(list => list.Count < 1 ? PineValue.EmptyList : list[0]); - public static Result neg_int(PineValue value) => - PineValueAsInteger.SignedIntegerFromValue(value) - .Map(i => PineValueAsInteger.ValueFromSignedInteger(-i)); - public static Result add_int(PineValue value) => KernelFunctionExpectingListOfBigIntWithAtLeastOneAndProducingBigInt( (firstInt, otherInts) => Result.ok( diff --git a/implement/elm-time/Pine/PineVM/PineVM.cs b/implement/elm-time/Pine/PineVM/PineVM.cs index 2d1c7ef7..fa84a51c 100644 --- a/implement/elm-time/Pine/PineVM/PineVM.cs +++ b/implement/elm-time/Pine/PineVM/PineVM.cs @@ -162,7 +162,7 @@ public Result EvaluateConditionalExpression( private static readonly IReadOnlyDictionary>> NamedKernelFunctions = ImmutableDictionary>>.Empty .SetItem(nameof(KernelFunction.equal), KernelFunction.equal) - .SetItem(nameof(KernelFunction.logical_not), KernelFunction.logical_not) + .SetItem(nameof(KernelFunction.negate), KernelFunction.negate) .SetItem(nameof(KernelFunction.logical_and), KernelFunction.logical_and) .SetItem(nameof(KernelFunction.logical_or), KernelFunction.logical_or) .SetItem(nameof(KernelFunction.length), KernelFunction.length) @@ -171,7 +171,6 @@ public Result EvaluateConditionalExpression( .SetItem(nameof(KernelFunction.reverse), KernelFunction.reverse) .SetItem(nameof(KernelFunction.concat), KernelFunction.concat) .SetItem(nameof(KernelFunction.list_head), KernelFunction.list_head) - .SetItem(nameof(KernelFunction.neg_int), KernelFunction.neg_int) .SetItem(nameof(KernelFunction.add_int), KernelFunction.add_int) .SetItem(nameof(KernelFunction.sub_int), KernelFunction.sub_int) .SetItem(nameof(KernelFunction.mul_int), KernelFunction.mul_int) diff --git a/implement/elm-time/Program.cs b/implement/elm-time/Program.cs index 48f9020c..df0bb67a 100644 --- a/implement/elm-time/Program.cs +++ b/implement/elm-time/Program.cs @@ -18,7 +18,7 @@ namespace ElmTime; public class Program { - public static string AppVersionId => "2023-10-20"; + public static string AppVersionId => "2023-10-21"; private static int AdminInterfaceDefaultPort => 4000; diff --git a/implement/elm-time/elm-time.csproj b/implement/elm-time/elm-time.csproj index 0ec54af1..3d06ba03 100644 --- a/implement/elm-time/elm-time.csproj +++ b/implement/elm-time/elm-time.csproj @@ -5,8 +5,8 @@ net8.0 ElmTime elm-time - 2023.1020.0.0 - 2023.1020.0.0 + 2023.1021.0.0 + 2023.1021.0.0 enable true