Skip to content
This repository has been archived by the owner on Mar 4, 2024. It is now read-only.

Commit

Permalink
feat: nodes for comparing
Browse files Browse the repository at this point in the history
  • Loading branch information
prescientmoon committed May 6, 2020
1 parent e22cb3f commit 2879712
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
48 changes: 48 additions & 0 deletions src/Data/Dataflow/Native/Predicate.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
module Lunarbox.Data.Dataflow.Native.Predicate (predicateNodes) where

import Prelude
import Data.Maybe (Maybe(..))
import Data.Tuple (Tuple(..))
import Lunarbox.Data.Dataflow.Expression (NativeExpression(..))
import Lunarbox.Data.Dataflow.Native.NativeConfig (NativeConfig(..))
import Lunarbox.Data.Dataflow.Runtime (RuntimeValue(..), binaryFunction)
import Lunarbox.Data.Dataflow.Scheme (Scheme(..))
import Lunarbox.Data.Dataflow.Type (createTypeVariable, typeBool, typeFunction)
import Lunarbox.Data.Editor.FunctionData (internal)
import Lunarbox.Data.Editor.FunctionName (FunctionName(..))

-- All the nodes which test stuff returning booleans
predicateNodes :: forall a s m. Array (NativeConfig a s m)
predicateNodes = [ equal, smallerThan, greaterThan, greaterOrEqual, smallerOrEqual ]

-- Type for a function which akes 2 values of the same type and returns a boolean
typeBinaryCompare :: Scheme
typeBinaryCompare = Forall [ a ] $ typeFunction typeA $ typeFunction typeA typeBool
where
Tuple a typeA = createTypeVariable "t0"

-- Helper to generate a config for a predicate of type typeBinaryCompare
createBinaryCompare :: forall a s m. String -> String -> (RuntimeValue -> RuntimeValue -> Boolean) -> NativeConfig a s m
createBinaryCompare name output predicate =
NativeConfig
{ name: FunctionName name
, expression: NativeExpression typeBinaryCompare $ binaryFunction ((Bool <<< _) <<< predicate)
, functionData: internal [ { name: "first value" }, { name: "second value" } ] { name: output }
, component: Nothing
}

-- The actual predicates
equal :: forall a s m. NativeConfig a s m
equal = createBinaryCompare "are equal" "a == b" (==)

smallerThan :: forall a s m. NativeConfig a s m
smallerThan = createBinaryCompare "smaller than" "a < b" (<)

greaterThan :: forall a s m. NativeConfig a s m
greaterThan = createBinaryCompare "greater than" "a > b" (>)

greaterOrEqual :: forall a s m. NativeConfig a s m
greaterOrEqual = createBinaryCompare "greater or equal" "a >= b" (>=)

smallerOrEqual :: forall a s m. NativeConfig a s m
smallerOrEqual = createBinaryCompare "smaller or equal" "a <= b" (<=)
2 changes: 2 additions & 0 deletions src/Data/Dataflow/Native/Prelude.purs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Lunarbox.Data.Dataflow.Native.Literal (literalNodes)
import Lunarbox.Data.Dataflow.Native.Logic (logicNodes)
import Lunarbox.Data.Dataflow.Native.Math (mathNodes)
import Lunarbox.Data.Dataflow.Native.NativeConfig (NativeConfig, loadNativeConfigs)
import Lunarbox.Data.Dataflow.Native.Predicate (predicateNodes)
import Lunarbox.Data.Dataflow.Native.String (stringNodes)
import Lunarbox.Data.Editor.State (State)

Expand All @@ -24,6 +25,7 @@ configs =
<> literalNodes
<> controlFlowNodes
<> arrayNodes
<> predicateNodes

-- Load all the built in nodes
loadPrelude :: forall a s m. State a s m -> State a s m
Expand Down
7 changes: 7 additions & 0 deletions src/Data/Dataflow/Runtime.purs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ instance eqRuntimeValue :: Eq RuntimeValue where
eq Null Null = true
eq _ _ = false

instance ordRuntimeValue :: Ord RuntimeValue where
compare (Number n) (Number n') = compare n n'
compare (String s) (String s') = compare s s'
compare (Bool v) (Bool v') = compare v v'
compare (NArray array) (NArray array') = compare array array'
compare _ _ = EQ

-- helper to ease the creation of binary functions
binaryFunction :: (RuntimeValue -> RuntimeValue -> RuntimeValue) -> RuntimeValue
binaryFunction f = Function $ Function <<< f
Expand Down
9 changes: 8 additions & 1 deletion src/Data/Dataflow/Scheme.purs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
module Lunarbox.Data.Dataflow.Scheme (Scheme(..)) where
module Lunarbox.Data.Dataflow.Scheme
( Scheme(..)
, fromType
) where

import Prelude
import Data.Foldable (fold)
Expand All @@ -12,3 +15,7 @@ derive instance eqScheme :: Eq Scheme
instance showScheme :: Show Scheme where
show (Forall [] t) = show t
show (Forall quantifiers t) = "forall" <> fold (quantifiers <#> (\(TVarName n) -> " " <> n)) <> ". " <> show t

-- Create a scheme with no type variables
fromType :: Type -> Scheme
fromType = Forall []

0 comments on commit 2879712

Please sign in to comment.