Skip to content

Commit

Permalink
Simplify kernel functions unifying integer and logical negation
Browse files Browse the repository at this point in the history
Unify the negation of integer and boolean values in a common kernel function.
  • Loading branch information
Viir committed Oct 21, 2023
1 parent 61c773a commit 05f9018
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1194,7 +1194,7 @@ compileElmSyntaxExpression stack elmExpression =
Ok negatedExpression ->
Ok
(KernelApplicationExpression
{ functionName = "neg_int"
{ functionName = "negate"
, argument = negatedExpression
}
)
Expand Down Expand Up @@ -1841,7 +1841,7 @@ compileElmSyntaxPattern elmPattern =
conditionExpressions =
\deconstructedExpression ->
[ [ KernelApplicationExpression
{ functionName = "logical_not"
{ functionName = "negate"
, argument =
equalCondition
[ deconstructedExpression
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -105,15 +105,15 @@ 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])
]
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])
]
Expand Down
31 changes: 22 additions & 9 deletions implement/elm-time/ElmTime/compile-elm-program/src/Pine.elm
Original file line number Diff line number Diff line change
Expand Up @@ -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) )
Expand Down Expand Up @@ -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)
)
Expand All @@ -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
Expand Down
28 changes: 21 additions & 7 deletions implement/elm-time/Pine/PineVM/KernelFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,27 @@ public static Result<string, PineValue> equal(PineValue valueA, PineValue valueB
Result<string, PineValue>.ok(
PineVM.ValueFromBool(valueA.Equals(valueB)));

public static Result<string, PineValue> logical_not(PineValue value) =>
PineVM.DecodeBoolFromValue(value)
.Map(b => PineVM.ValueFromBool(!b));
public static Result<string, PineValue> negate(PineValue value) =>
Result<string, PineValue>.ok(
value switch
{
PineValue.BlobValue blobValue
when 0 < blobValue.Bytes.Length =>
blobValue.Bytes.Span[0] switch
{
4 =>
PineValue.Blob(CommonConversion.Concat((ReadOnlySpan<byte>)[2], blobValue.Bytes.Span[1..])),

2 =>
PineValue.Blob(CommonConversion.Concat((ReadOnlySpan<byte>)[4], blobValue.Bytes.Span[1..])),

_ =>
PineValue.EmptyList
},

_ =>
PineValue.EmptyList
});

public static Result<string, PineValue> logical_and(PineValue value) =>
KernelFunctionExpectingListOfTypeBool(bools => bools.Aggregate(seed: true, func: (a, b) => a && b), value);
Expand Down Expand Up @@ -131,10 +149,6 @@ public static Result<string, PineValue> list_head(PineValue value) =>
PineVM.DecodePineListValue(value)
.Map(list => list.Count < 1 ? PineValue.EmptyList : list[0]);

public static Result<string, PineValue> neg_int(PineValue value) =>
PineValueAsInteger.SignedIntegerFromValue(value)
.Map(i => PineValueAsInteger.ValueFromSignedInteger(-i));

public static Result<string, PineValue> add_int(PineValue value) =>
KernelFunctionExpectingListOfBigIntWithAtLeastOneAndProducingBigInt(
(firstInt, otherInts) => Result<string, BigInteger>.ok(
Expand Down
3 changes: 1 addition & 2 deletions implement/elm-time/Pine/PineVM/PineVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public Result<string, PineValue> EvaluateConditionalExpression(
private static readonly IReadOnlyDictionary<string, Func<PineValue, Result<string, PineValue>>> NamedKernelFunctions =
ImmutableDictionary<string, Func<PineValue, Result<string, PineValue>>>.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)
Expand All @@ -171,7 +171,6 @@ public Result<string, PineValue> 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)
Expand Down
2 changes: 1 addition & 1 deletion implement/elm-time/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions implement/elm-time/elm-time.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>ElmTime</RootNamespace>
<AssemblyName>elm-time</AssemblyName>
<AssemblyVersion>2023.1020.0.0</AssemblyVersion>
<FileVersion>2023.1020.0.0</FileVersion>
<AssemblyVersion>2023.1021.0.0</AssemblyVersion>
<FileVersion>2023.1021.0.0</FileVersion>
<Nullable>enable</Nullable>
<GenerateEmbeddedFilesManifest>true</GenerateEmbeddedFilesManifest>
</PropertyGroup>
Expand Down

0 comments on commit 05f9018

Please sign in to comment.