From 4f293058f134cb23f46b57759dda5cf7eb927da1 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sat, 18 Jan 2025 13:31:46 -0600 Subject: [PATCH 001/171] add lens note in readme --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 608cfbe..730117e 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,9 @@ # HushGP A PushGP implementation in Haskell +This branch is meant to overhaul the system with the lens +library for more abstraction. + ## Tasks * [ ] Post minimal core of exec to haskell discourse for advice about speed optimization. * [x] Do test-driven development on this one. From 7a33477b0c18349c2112a915b9e4d4bcfcf8930e Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sat, 18 Jan 2025 19:56:06 -0600 Subject: [PATCH 002/171] add lens to .cabal file --- HushGP.cabal | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/HushGP.cabal b/HushGP.cabal index 98afee4..23be124 100644 --- a/HushGP.cabal +++ b/HushGP.cabal @@ -50,7 +50,7 @@ library -- Other library packages from which modules are imported. build-depends: - base, containers + base, containers, lens -- Directories containing source files. hs-source-dirs: src From 1907ac6c877c8da154737268a822d2fc0e519eb2 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sat, 18 Jan 2025 19:56:27 -0600 Subject: [PATCH 003/171] convert to basic lenses, need to abstract --- src/Instructions/ExecInstructions.hs | 54 +++++++++++++-------------- src/Instructions/FloatInstructions.hs | 26 ++++++------- src/Instructions/IntInstructions.hs | 28 +++++++------- 3 files changed, 54 insertions(+), 54 deletions(-) diff --git a/src/Instructions/ExecInstructions.hs b/src/Instructions/ExecInstructions.hs index 8aa243e..bd27ec2 100644 --- a/src/Instructions/ExecInstructions.hs +++ b/src/Instructions/ExecInstructions.hs @@ -4,22 +4,22 @@ import State import Instructions.IntInstructions instructionExecIf :: State -> State -instructionExecIf state@(State {exec = (e1 : e2 : es), bool = (b : _)}) = +instructionExecIf state@(State {_exec = (e1 : e2 : es), _bool = (b : _)}) = if b - then state {exec = e1 : es} - else state {exec = e2 : es} + then state {_exec = e1 : es} + else state {_exec = e2 : es} instructionExecIf state = state instructionExecDup :: State -> State -instructionExecDup state@(State {exec = alles@(e0 : _)}) = - state {exec = e0 : alles} +instructionExecDup state@(State {_exec = alles@(e : _)}) = + state {_exec = e : alles} instructionExecDup state = state instructionExecDoRange :: State -> State -instructionExecDoRange state@(State {exec = (e1 : es), int = (i0 : i1 : is)}) = +instructionExecDoRange state@(State {_exec = (e1 : es), _int = (i0 : i1 : is)}) = if increment i0 i1 /= 0 - then state {exec = e1 : Block [GeneInt (i1 + increment i0 i1), GeneInt i0, StateFunc instructionExecDoRange, e1] : es, int = i1 : is} - else state {exec = e1 : es, int = i1 : is} + then state {_exec = e1 : Block [GeneInt (i1 + increment i0 i1), GeneInt i0, StateFunc instructionExecDoRange, e1] : es, _int = i1 : is} + else state {_exec = e1 : es, _int = i1 : is} where increment :: Int -> Int -> Int increment destIdx currentIdx @@ -29,37 +29,37 @@ instructionExecDoRange state@(State {exec = (e1 : es), int = (i0 : i1 : is)}) = instructionExecDoRange state = state instructionExecDoCount :: State -> State -instructionExecDoCount state@(State {exec = (e1 : es), int = (i1 : is)}) = - if i1 < 1 +instructionExecDoCount state@(State {_exec = (e : es), _int = (i : is)}) = + if i < 1 then state - else state {exec = Block [GeneInt 0, GeneInt $ i1 - 1, StateFunc instructionExecDoRange, e1] : es, int = is} + else state {_exec = Block [GeneInt 0, GeneInt $ i - 1, StateFunc instructionExecDoRange, e] : es, _int = is} instructionExecDoCount state = state instructionExecDoTimes :: State -> State -instructionExecDoTimes state@(State {exec = (e1 : es), int = (i1 : is)}) = - if i1 < 1 +instructionExecDoTimes state@(State {_exec = (e : es), _int = (i : is)}) = + if i < 1 then state - else state {exec = Block [GeneInt 0, GeneInt $ i1 - 1, StateFunc instructionExecDoRange, Block [StateFunc instructionIntPop, e1]] : es, int = is} + else state {_exec = Block [GeneInt 0, GeneInt $ i - 1, StateFunc instructionExecDoRange, Block [StateFunc instructionIntPop, e]] : es, _int = is} instructionExecDoTimes state = state instructionExecWhile :: State -> State -instructionExecWhile state@(State {exec = (_ : es), bool = []}) = - state {exec = es} -instructionExecWhile state@(State {exec = alles@(e1 : es), bool = (b1 : bs)}) = - if b1 - then state {exec = e1 : StateFunc instructionExecWhile : alles, bool = bs} - else state {exec = es} +instructionExecWhile state@(State {_exec = (_ : es), _bool = []}) = + state {_exec = es} +instructionExecWhile state@(State {_exec = alles@(e : es), _bool = (b : bs)}) = + if b + then state {_exec = e : StateFunc instructionExecWhile : alles, _bool = bs} + else state {_exec = es} instructionExecWhile state = state instructionExecDoWhile :: State -> State -instructionExecDoWhile state@(State {exec = alles@(e1 : _)}) = - state {exec = e1 : StateFunc instructionExecWhile : alles} +instructionExecDoWhile state@(State {_exec = alles@(e : _)}) = + state {_exec = e : StateFunc instructionExecWhile : alles} instructionExecDoWhile state = state --- Eats the boolean no matter what +-- Eats the _boolean no matter what instructionExecWhen :: State -> State -instructionExecWhen state@(State {exec = (_ : es), bool = (b1 : bs)}) = - if not b1 - then state {exec = es, bool = bs} - else state {bool = bs} +instructionExecWhen state@(State {_exec = (_ : es), _bool = (b : bs)}) = + if not b + then state {_exec = es, _bool = bs} + else state {_bool = bs} instructionExecWhen state = state diff --git a/src/Instructions/FloatInstructions.hs b/src/Instructions/FloatInstructions.hs index 8199988..8d9c834 100644 --- a/src/Instructions/FloatInstructions.hs +++ b/src/Instructions/FloatInstructions.hs @@ -3,54 +3,54 @@ module Instructions.FloatInstructions where import State instructionFloatAdd :: State -> State -instructionFloatAdd state@(State {float = (f1 : f2 : fs)}) = state {float = f2 + f1 : fs} +instructionFloatAdd state@(State {_float = (f1 : f2 : fs)}) = state {_float = f2 + f1 : fs} instructionFloatAdd state = state instructionFloatSub :: State -> State -instructionFloatSub state@(State {float = (f1 : f2 : fs)}) = state {float = f2 - f1 : fs} +instructionFloatSub state@(State {_float = (f1 : f2 : fs)}) = state {_float = f2 - f1 : fs} instructionFloatSub state = state instructionFloatMul :: State -> State -instructionFloatMul state@(State {float = (f1 : f2 : fs)}) = state {float = f2 * f1 : fs} +instructionFloatMul state@(State {_float = (f1 : f2 : fs)}) = state {_float = f2 * f1 : fs} instructionFloatMul state = state instructionFloatDiv :: State -> State -instructionFloatDiv state@(State {float = (f1 : f2 : fs)}) = state {float = if f1 /= 0 then f2 / f1 : fs else f1 : f2 : fs} +instructionFloatDiv state@(State {_float = (f1 : f2 : fs)}) = state {_float = if f1 /= 0 then f2 / f1 : fs else f1 : f2 : fs} instructionFloatDiv state = state instructionFloatMin :: State -> State -instructionFloatMin state@(State {float = (f1 : f2 : fs)}) = state {float = min f1 f2 : fs} +instructionFloatMin state@(State {_float = (f1 : f2 : fs)}) = state {_float = min f1 f2 : fs} instructionFloatMin state = state instructionFloatMax :: State -> State -instructionFloatMax state@(State {float = (f1 : f2 : fs)}) = state {float = max f1 f2 : fs} +instructionFloatMax state@(State {_float = (f1 : f2 : fs)}) = state {_float = max f1 f2 : fs} instructionFloatMax state = state instructionFloatInc :: State -> State -instructionFloatInc state@(State {float = (f1 : fs)}) = state {float = f1 + 1 : fs} +instructionFloatInc state@(State {_float = (f1 : fs)}) = state {_float = f1 + 1 : fs} instructionFloatInc state = state instructionFloatDec :: State -> State -instructionFloatDec state@(State {float = (f1 : fs)}) = state {float = f1 - 1 : fs} +instructionFloatDec state@(State {_float = (f1 : fs)}) = state {_float = f1 - 1 : fs} instructionFloatDec state = state instructionFloatLT :: State -> State -instructionFloatLT state@(State {float = f1 : f2 : fs, bool = bs}) = state {float = fs, bool = (f1 < f2) : bs} +instructionFloatLT state@(State {_float = f1 : f2 : fs, _bool = bs}) = state {_float = fs, _bool = (f1 < f2) : bs} instructionFloatLT state = state instructionFloatGT :: State -> State -instructionFloatGT state@(State {float = f1 : f2 : fs, bool = bs}) = state {float = fs, bool = (f1 > f2) : bs} +instructionFloatGT state@(State {_float = f1 : f2 : fs, _bool = bs}) = state {_float = fs, _bool = (f1 > f2) : bs} instructionFloatGT state = state instructionFloatLTE :: State -> State -instructionFloatLTE state@(State {float = f1 : f2 : fs, bool = bs}) = state {float = fs, bool = (f1 <= f2) : bs} +instructionFloatLTE state@(State {_float = f1 : f2 : fs, _bool = bs}) = state {_float = fs, _bool = (f1 <= f2) : bs} instructionFloatLTE state = state instructionFloatGTE :: State -> State -instructionFloatGTE state@(State {float = f1 : f2 : fs, bool = bs}) = state {float = fs, bool = (f1 >= f2) : bs} +instructionFloatGTE state@(State {_float = f1 : f2 : fs, _bool = bs}) = state {_float = fs, _bool = (f1 >= f2) : bs} instructionFloatGTE state = state instructionFloatPop :: State -> State -instructionFloatPop state@(State {float = (_ : fs)}) = state {float = fs} +instructionFloatPop state@(State {_float = (_ : fs)}) = state {_float = fs} instructionFloatPop state = state diff --git a/src/Instructions/IntInstructions.hs b/src/Instructions/IntInstructions.hs index d12ee6a..3d25aa4 100644 --- a/src/Instructions/IntInstructions.hs +++ b/src/Instructions/IntInstructions.hs @@ -4,57 +4,57 @@ import State -- import Debug.Trace instructionIntAdd :: State -> State -instructionIntAdd state@(State {int = (i1 : i2 : is)}) = state {int = i2 + i1 : is} +instructionIntAdd state@(State {_int = (i1 : i2 : is)}) = state {_int = i2 + i1 : is} instructionIntAdd state = state instructionIntSub :: State -> State -instructionIntSub state@(State {int = (i1 : i2 : is)}) = state {int = i2 - i1 : is} +instructionIntSub state@(State {_int = (i1 : i2 : is)}) = state {_int = i2 - i1 : is} instructionIntSub state = state instructionIntMul :: State -> State -instructionIntMul state@(State {int = (i1 : i2 : is)}) = state {int = i2 * i1 : is} +instructionIntMul state@(State {_int = (i1 : i2 : is)}) = state {_int = i2 * i1 : is} instructionIntMul state = state instructionIntDiv :: State -> State -instructionIntDiv state@(State {int = (i1 : i2 : is)}) = state {int = if i1 /= 0 then (i2 `div` i1) : is else i1 : i2 : is} +instructionIntDiv state@(State {_int = (i1 : i2 : is)}) = state {_int = if i1 /= 0 then (i2 `div` i1) : is else i1 : i2 : is} instructionIntDiv state = state instructionIntMod :: State -> State -instructionIntMod state@(State {int = (i1 : i2 : is)}) = state {int = i2 `mod` i1 : is} +instructionIntMod state@(State {_int = (i1 : i2 : is)}) = state {_int = i2 `mod` i1 : is} instructionIntMod state = state instructionIntMin :: State -> State -instructionIntMin state@(State {int = (i1 : i2 : is)}) = state {int = min i1 i2 : is} +instructionIntMin state@(State {_int = (i1 : i2 : is)}) = state {_int = min i1 i2 : is} instructionIntMin state = state instructionIntMax :: State -> State -instructionIntMax state@(State {int = (i1 : i2 : is)}) = state {int = max i1 i2 : is} +instructionIntMax state@(State {_int = (i1 : i2 : is)}) = state {_int = max i1 i2 : is} instructionIntMax state = state instructionIntInc :: State -> State -instructionIntInc state@(State {int = (i1 : is)}) = state {int = i1 + 1 : is} +instructionIntInc state@(State {_int = (i1 : is)}) = state {_int = i1 + 1 : is} instructionIntInc state = state instructionIntDec :: State -> State -instructionIntDec state@(State {int = (i1 : is)}) = state {int = i1 - 1 : is} +instructionIntDec state@(State {_int = (i1 : is)}) = state {_int = i1 - 1 : is} instructionIntDec state = state instructionIntLT :: State -> State -instructionIntLT state@(State {int = i1 : i2 : is, bool = bs}) = state {int = is, bool = (i1 < i2) : bs} +instructionIntLT state@(State {_int = i1 : i2 : is, _bool = bs}) = state {_int = is, _bool = (i1 < i2) : bs} instructionIntLT state = state instructionIntGT :: State -> State -instructionIntGT state@(State {int = i1 : i2 : is, bool = bs}) = state {int = is, bool = (i1 > i2) : bs} +instructionIntGT state@(State {_int = i1 : i2 : is, _bool = bs}) = state {_int = is, _bool = (i1 > i2) : bs} instructionIntGT state = state instructionIntLTE :: State -> State -instructionIntLTE state@(State {int = i1 : i2 : is, bool = bs}) = state {int = is, bool = (i1 <= i2) : bs} +instructionIntLTE state@(State {_int = i1 : i2 : is, _bool = bs}) = state {_int = is, _bool = (i1 <= i2) : bs} instructionIntLTE state = state instructionIntGTE :: State -> State -instructionIntGTE state@(State {int = i1 : i2 : is, bool = bs}) = state {int = is, bool = (i1 >= i2) : bs} +instructionIntGTE state@(State {_int = i1 : i2 : is, _bool = bs}) = state {_int = is, _bool = (i1 >= i2) : bs} instructionIntGTE state = state instructionIntPop :: State -> State -instructionIntPop state@(State {int = (_ : is)}) = state {int = is} +instructionIntPop state@(State {_int = (_ : is)}) = state {_int = is} instructionIntPop state = state From 34bf6b38bdeda194cac52fb54f007fb60b427b9f Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sat, 18 Jan 2025 19:56:38 -0600 Subject: [PATCH 004/171] convert to lens --- src/Push.hs | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/src/Push.hs b/src/Push.hs index 0273f46..f0f968c 100644 --- a/src/Push.hs +++ b/src/Push.hs @@ -3,6 +3,7 @@ module Push where import qualified Data.Map as Map +import Control.Lens -- import Instructions.IntInstructions -- import Instructions.ExecInstructions import State @@ -18,20 +19,24 @@ import State -- This is one of the push genome functions itself, not infrastructure. -- Optionally, split this off into independent functions instructionParameterLoad :: State -> State -instructionParameterLoad state@(State {parameter = (p : _), ..}) = case p of - (GeneInt val) -> state {int = val : int} - (GeneFloat val) -> state {float = val : float} - (GeneBool val) -> state {bool = val : bool} - (GeneString val) -> state {string = val : string} +instructionParameterLoad state@(State {_parameter = (p : _)}) = case p of + (GeneInt val) -> state & int .~ val : view int state + (GeneFloat val) -> state & float .~ val : view float state + (GeneBool val) -> state & bool .~ val : view bool state + (GeneString val) -> state & string .~ val : view string state + (GeneIntVector val) -> state & intVector .~ val : view intVector state + (GeneFloatVector val) -> state & floatVector .~ val : view floatVector state + (GeneBoolVector val) -> state & boolVector .~ val : view boolVector state + (GeneStringVector val) -> state & stringVector .~ val : view stringVector state (StateFunc _) -> undefined (PlaceInput _) -> undefined Close -> undefined - (Block xs) -> state {exec = xs <> exec} + (Block xs) -> state & exec .~ xs <> view exec state instructionParameterLoad state = state -- Loads a genome into the exec stack loadProgram :: [Gene] -> State -> State -loadProgram newstack state@(State {exec = _}) = state {exec = newstack} +loadProgram newstack state = state & exec .~ newstack -- Takes a Push state, and generates the next push state via: -- If the first item on the EXEC stack is a single instruction @@ -44,16 +49,20 @@ loadProgram newstack state@(State {exec = _}) = state {exec = newstack} -- ends up on top). -- The empty-stack safety of interpretExec on empty stacks depends on the functions it calls. interpretExec :: State -> State -interpretExec state@(State {exec = []}) = state {exec = []} -interpretExec state@(State {exec = (e : es), ..}) = +interpretExec state@(State {_exec = []}) = state & exec .~ [] +interpretExec state@(State {_exec = (e : es)}) = case e of - (GeneInt val) -> interpretExec state {exec = es, int = val : int} - (GeneFloat val) -> interpretExec (state {exec = es, float = val : float}) - (GeneBool val) -> interpretExec (state {exec = es, bool = val : bool}) - (GeneString val) -> interpretExec (state {exec = es, string = val : string}) - (StateFunc func) -> interpretExec $ func state {exec = es} - (Block block) -> interpretExec (state {exec = block ++ es}) - (PlaceInput val) -> interpretExec (state {exec = (input Map.! val) : es}) + (GeneInt val) -> interpretExec state & exec .~ es & int .~ val : view int state + (GeneFloat val) -> interpretExec state & exec .~ es & float .~ val : view float state + (GeneBool val) -> interpretExec state & exec .~ es & bool .~ val : view bool state + (GeneString val) -> interpretExec state & exec .~ es & string .~ val : view string state + (GeneIntVector val) -> interpretExec state & exec .~ es & intVector .~ val : view intVector state + (GeneFloatVector val) -> interpretExec state & exec .~ es & floatVector .~ val : view floatVector state + (GeneBoolVector val) -> interpretExec state & exec .~ es & boolVector .~ val : view boolVector state + (GeneStringVector val) -> interpretExec state & exec .~ es & stringVector .~ val : view stringVector state + (StateFunc func) -> interpretExec $ func state {_exec = es} + (Block block) -> interpretExec (state {_exec = block ++ es}) + (PlaceInput val) -> interpretExec (state {_exec = (view input state Map.! val) : es}) Close -> undefined -- remove Close constructor later? -- Need to make interpretExec strict, right? From 346cd96d672394b8fec35e687073304e81bcd0fe Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sat, 18 Jan 2025 19:56:51 -0600 Subject: [PATCH 005/171] change to lens --- src/State.hs | 48 ++++++++++++++++++++++++++---------------------- test/Main.hs | 4 ++-- 2 files changed, 28 insertions(+), 24 deletions(-) diff --git a/src/State.hs b/src/State.hs index 43aca0d..33fc05a 100644 --- a/src/State.hs +++ b/src/State.hs @@ -1,6 +1,8 @@ +{-# LANGUAGE TemplateHaskell #-} module State where import qualified Data.Map as Map +import Control.Lens -- The exec stack must store heterogenous types, -- and we must be able to detect that type at runtime. @@ -50,33 +52,35 @@ instance Show Gene where show (Block xs) = "Block: " <> show xs data State = State - { exec :: [Gene], - int :: [Int], - float :: [Float], - bool :: [Bool], - string :: [String], - vectorInt :: [[Int]], - vectorFloat :: [[Float]], - vectorBool :: [[Bool]], - vectorString :: [[String]], - parameter :: [Gene], - input :: Map.Map String Gene + { _exec :: [Gene], + _int :: [Int], + _float :: [Float], + _bool :: [Bool], + _string :: [String], + _intVector :: [[Int]], + _floatVector :: [[Float]], + _boolVector :: [[Bool]], + _stringVector :: [[String]], + _parameter :: [Gene], + _input :: Map.Map String Gene } deriving (Show, Eq) +$(makeLenses ''State) + emptyState :: State emptyState = State - { exec = [], - int = [], - float = [], - bool = [], - string = [], - parameter = [], - vectorInt = [], - vectorFloat = [], - vectorBool = [], - vectorString = [], - input = Map.empty + { _exec = [], + _int = [], + _float = [], + _bool = [], + _string = [], + _parameter = [], + _intVector = [], + _floatVector = [], + _boolVector = [], + _stringVector = [], + _input = Map.empty } diff --git a/test/Main.hs b/test/Main.hs index f60c02f..d1bfe71 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -10,12 +10,12 @@ import Instructions.FloatInstructions intTestFunc :: String -> [Int] -> [Gene] -> State -> IO () intTestFunc name goal genome startState = let state = loadProgram genome startState - in assert (goal == int (interpretExec state)) putStrLn (name ++ " passed test.") + in assert (goal == _int (interpretExec state)) putStrLn (name ++ " passed test.") floatTestFunc :: String -> [Float] -> [Gene] -> State -> IO () floatTestFunc name goal genome startState = let state = loadProgram genome startState - in assert (goal == float (interpretExec state)) putStrLn (name ++ " passed test.") + in assert (goal == _float (interpretExec state)) putStrLn (name ++ " passed test.") main :: IO () main = do From 8feb8cd790147b0a4532a0e363cf03be46e93ac5 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sun, 19 Jan 2025 00:15:04 -0600 Subject: [PATCH 006/171] add generic instructions --- HushGP.cabal | 1 + 1 file changed, 1 insertion(+) diff --git a/HushGP.cabal b/HushGP.cabal index 23be124..dc3e72c 100644 --- a/HushGP.cabal +++ b/HushGP.cabal @@ -41,6 +41,7 @@ library , Instructions.IntInstructions , Instructions.ExecInstructions , Instructions.FloatInstructions + , Instructions.GenericInstructions -- Modules included in this library but not exported. -- other-modules: From 862241d4643f2701e3d6fa6ccd7ffbc5879c247b Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sun, 19 Jan 2025 00:15:31 -0600 Subject: [PATCH 007/171] genericize generic instructions --- src/Instructions/ExecInstructions.hs | 23 +++++++-- src/Instructions/FloatInstructions.hs | 21 +++++++- src/Instructions/GenericInstructions.hs | 65 +++++++++++++++++++++++++ src/Instructions/IntInstructions.hs | 22 ++++++++- 4 files changed, 124 insertions(+), 7 deletions(-) create mode 100644 src/Instructions/GenericInstructions.hs diff --git a/src/Instructions/ExecInstructions.hs b/src/Instructions/ExecInstructions.hs index bd27ec2..cb28fb2 100644 --- a/src/Instructions/ExecInstructions.hs +++ b/src/Instructions/ExecInstructions.hs @@ -2,6 +2,7 @@ module Instructions.ExecInstructions where import State import Instructions.IntInstructions +import Instructions.GenericInstructions instructionExecIf :: State -> State instructionExecIf state@(State {_exec = (e1 : e2 : es), _bool = (b : _)}) = @@ -11,9 +12,25 @@ instructionExecIf state@(State {_exec = (e1 : e2 : es), _bool = (b : _)}) = instructionExecIf state = state instructionExecDup :: State -> State -instructionExecDup state@(State {_exec = alles@(e : _)}) = - state {_exec = e : alles} -instructionExecDup state = state +instructionExecDup state = instructionDup state exec + +instructionExecDupN :: State -> State +instructionExecDupN state = instructionDupN state exec + +instructionExecPop :: State -> State +instructionExecPop state = instructionPop state exec + +instructionExecSwap :: State -> State +instructionExecSwap state = instructionSwap state exec + +instructionExecRot :: State -> State +instructionExecRot state = instructionRot state exec + +instructionExecFlush :: State -> State +instructionExecFlush state = instructionFlush state exec + +instructionExecEq :: State -> State +instructionExecEq state = instructionEq state exec instructionExecDoRange :: State -> State instructionExecDoRange state@(State {_exec = (e1 : es), _int = (i0 : i1 : is)}) = diff --git a/src/Instructions/FloatInstructions.hs b/src/Instructions/FloatInstructions.hs index 8d9c834..fc05e0f 100644 --- a/src/Instructions/FloatInstructions.hs +++ b/src/Instructions/FloatInstructions.hs @@ -1,5 +1,6 @@ module Instructions.FloatInstructions where +import Instructions.GenericInstructions import State instructionFloatAdd :: State -> State @@ -51,6 +52,22 @@ instructionFloatGTE state@(State {_float = f1 : f2 : fs, _bool = bs}) = state {_ instructionFloatGTE state = state instructionFloatPop :: State -> State -instructionFloatPop state@(State {_float = (_ : fs)}) = state {_float = fs} -instructionFloatPop state = state +instructionFloatPop state = instructionPop state float +instructionFloatDup :: State -> State +instructionFloatDup state = instructionPop state float + +instructionFloatDupN :: State -> State +instructionFloatDupN state = instructionDupN state float + +instructionFloatSwap :: State -> State +instructionFloatSwap state = instructionSwap state float + +instructionFloatRot :: State -> State +instructionFloatRot state = instructionRot state float + +instructionFloatFlush :: State -> State +instructionFloatFlush state = instructionFlush state float + +instructionFloatEq :: State -> State +instructionFloatEq state = instructionEq state float diff --git a/src/Instructions/GenericInstructions.hs b/src/Instructions/GenericInstructions.hs new file mode 100644 index 0000000..a1ff7ba --- /dev/null +++ b/src/Instructions/GenericInstructions.hs @@ -0,0 +1,65 @@ +module Instructions.GenericInstructions where + +import Control.Lens +import State + +-- import Debug.Trace + +notEmptyStack :: State -> Lens' State [a] -> Bool +notEmptyStack state accessor = not . null $ view accessor state + +-- This head error should never happen +instructionDup :: State -> Lens' State [a] -> State +instructionDup state accessor = if notEmptyStack state accessor then state & accessor .~ head (view accessor state) : view accessor state else state + +instructionPop :: State -> Lens' State [a] -> State +instructionPop state accessor = if notEmptyStack state accessor then state & accessor .~ drop 1 (view accessor state) else state + +-- I might be able to move some of the int stack error checking +-- to the integer call. For now this may be a tad inefficient. +instructionDupN :: State -> Lens' State [a] -> State +instructionDupN state accessor = + if notEmptyStack state accessor && notEmptyStack state int + then instructionDupNHelper (head (view int state)) accessor (instructionPop state int) + else state + where + instructionDupNHelper :: Int -> Lens' State [a] -> State -> State + instructionDupNHelper count internalAccessor internalState = + if count > 1 && notEmptyStack internalState int + then instructionDupNHelper (count - 1) internalAccessor (instructionDup internalState accessor) + else internalState + +instructionSwap :: State -> Lens' State [a] -> State +instructionSwap state accessor = + if (length . take 2 $ view accessor state) == 2 + then state & accessor .~ swapper (view accessor state) + else state + where + swapper :: [a] -> [a] + swapper (x1 : x2 : xs) = x2 : x1 : xs + swapper xs = xs + +-- Rotates top 3 integers +-- We could use template haskell to rotate any number of these as +-- an instruction later. Template haskell seems very complicated tho. +instructionRot :: State -> Lens' State [a] -> State +instructionRot state accessor = + if (length . take 3 $ view accessor state) == 3 + then state & accessor .~ rotator (view accessor state) + else state + where + rotator :: [a] -> [a] + rotator (x1 : x2 : x3 : xs) = x3 : x1 : x2 : xs + rotator xs = xs + +instructionFlush :: State -> Lens' State [a] -> State +instructionFlush state accessor = state & accessor .~ [] + +instructionEq :: forall a. Eq a => State -> Lens' State [a] -> State +instructionEq state accessor = + if length stackTop == 2 + then state & bool .~ (head stackTop == stackTop !! 1) : view bool state & accessor .~ drop 2 (view accessor state) + else state + where + stackTop :: [a] + stackTop = take 2 $ view accessor state diff --git a/src/Instructions/IntInstructions.hs b/src/Instructions/IntInstructions.hs index 3d25aa4..87d9112 100644 --- a/src/Instructions/IntInstructions.hs +++ b/src/Instructions/IntInstructions.hs @@ -1,6 +1,7 @@ module Instructions.IntInstructions where import State +import Instructions.GenericInstructions -- import Debug.Trace instructionIntAdd :: State -> State @@ -55,6 +56,23 @@ instructionIntGTE :: State -> State instructionIntGTE state@(State {_int = i1 : i2 : is, _bool = bs}) = state {_int = is, _bool = (i1 >= i2) : bs} instructionIntGTE state = state +instructionIntDup :: State -> State +instructionIntDup state = instructionDup state int + instructionIntPop :: State -> State -instructionIntPop state@(State {_int = (_ : is)}) = state {_int = is} -instructionIntPop state = state +instructionIntPop state = instructionPop state int + +instructionIntDupN :: State -> State +instructionIntDupN state = instructionDupN state int + +instructionIntSwap :: State -> State +instructionIntSwap state = instructionSwap state int + +instructionIntRot :: State -> State +instructionIntRot state = instructionRot state int + +instructionIntFlush :: State -> State +instructionIntFlush state = instructionFlush state int + +instructionIntEq :: State -> State +instructionIntEq state = instructionEq state int From 7d802a0fa4ee35d8ca7ce0bc004f3fdb9665d354 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sun, 19 Jan 2025 00:16:03 -0600 Subject: [PATCH 008/171] needed parenthesis for execution priority --- src/Push.hs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Push.hs b/src/Push.hs index f0f968c..8925bf7 100644 --- a/src/Push.hs +++ b/src/Push.hs @@ -8,7 +8,7 @@ import Control.Lens -- import Instructions.ExecInstructions import State --- import Debug.Trace (trace, traceStack) +import Debug.Trace (trace, traceStack) -- Each core func should be: (State -> State -> State) -- but each core function can use abstract helper functions. @@ -52,14 +52,14 @@ interpretExec :: State -> State interpretExec state@(State {_exec = []}) = state & exec .~ [] interpretExec state@(State {_exec = (e : es)}) = case e of - (GeneInt val) -> interpretExec state & exec .~ es & int .~ val : view int state - (GeneFloat val) -> interpretExec state & exec .~ es & float .~ val : view float state - (GeneBool val) -> interpretExec state & exec .~ es & bool .~ val : view bool state - (GeneString val) -> interpretExec state & exec .~ es & string .~ val : view string state - (GeneIntVector val) -> interpretExec state & exec .~ es & intVector .~ val : view intVector state - (GeneFloatVector val) -> interpretExec state & exec .~ es & floatVector .~ val : view floatVector state - (GeneBoolVector val) -> interpretExec state & exec .~ es & boolVector .~ val : view boolVector state - (GeneStringVector val) -> interpretExec state & exec .~ es & stringVector .~ val : view stringVector state + (GeneInt val) -> interpretExec (state & exec .~ es & int .~ val : view int state) + (GeneFloat val) -> interpretExec (state & exec .~ es & float .~ val : view float state) + (GeneBool val) -> interpretExec (state & exec .~ es & bool .~ val : view bool state) + (GeneString val) -> interpretExec (state & exec .~ es & string .~ val : view string state) + (GeneIntVector val) -> interpretExec (state & exec .~ es & intVector .~ val : view intVector state) + (GeneFloatVector val) -> interpretExec (state & exec .~ es & floatVector .~ val : view floatVector state) + (GeneBoolVector val) -> interpretExec (state & exec .~ es & boolVector .~ val : view boolVector state) + (GeneStringVector val) -> interpretExec (state & exec .~ es & stringVector .~ val : view stringVector state) (StateFunc func) -> interpretExec $ func state {_exec = es} (Block block) -> interpretExec (state {_exec = block ++ es}) (PlaceInput val) -> interpretExec (state {_exec = (view input state Map.! val) : es}) From a756e5d584fb81e48df1bc69fd836cc5e5b66d81 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sun, 19 Jan 2025 00:16:21 -0600 Subject: [PATCH 009/171] example state --- src/State.hs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/State.hs b/src/State.hs index 33fc05a..42fc2a3 100644 --- a/src/State.hs +++ b/src/State.hs @@ -84,3 +84,18 @@ emptyState = _input = Map.empty } +exampleState :: State +exampleState = + State + { _exec = [], + _int = [32, 56], + _float = [3.23, 9.235], + _bool = [True, False], + _string = ["abc", "123"], + _parameter = [], + _intVector = [[1,2], [5,6,8]], + _floatVector = [[1.234, 9.21], [5.42, 6.221, 8.5493]], + _boolVector = [[True, False], [False, False, True]], + _stringVector = [["def", "567"], ["gamer", "fellah", "live action how to train your dragon"]], + _input = Map.empty + } From af258b538abeec1ef9a0ea8101192c0472c880bd Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sun, 19 Jan 2025 00:16:46 -0600 Subject: [PATCH 010/171] shitton of tests --- test/Main.hs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/test/Main.hs b/test/Main.hs index d1bfe71..033cf39 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -17,15 +17,29 @@ floatTestFunc name goal genome startState = let state = loadProgram genome startState in assert (goal == _float (interpretExec state)) putStrLn (name ++ " passed test.") +boolTestFunc :: String -> [Bool] -> [Gene] -> State -> IO () +boolTestFunc name goal genome startState = + let state = loadProgram genome startState + in assert (goal == _bool (interpretExec state)) putStrLn (name ++ " passed test.") + main :: IO () main = do + -- Int tests intTestFunc "instructionIntAdd" [8] [GeneInt 6, GeneInt 2, StateFunc instructionIntAdd] emptyState intTestFunc "instructionIntSub" [4] [GeneInt 6, GeneInt 2, StateFunc instructionIntSub] emptyState intTestFunc "instructionIntMul" [12] [GeneInt 6, GeneInt 2, StateFunc instructionIntMul] emptyState intTestFunc "instructionIntDiv" [3] [GeneInt 6, GeneInt 2, StateFunc instructionIntDiv] emptyState intTestFunc "instructionIntDiv0" [0, 2] [GeneInt 2, GeneInt 0, StateFunc instructionIntDiv] emptyState intTestFunc "instructionIntMod" [3] [GeneInt 13, GeneInt 5, StateFunc instructionIntMod] emptyState + intTestFunc "instrucitonIntDup" [3, 3, 2] [GeneInt 2, GeneInt 3, StateFunc instructionIntDup] emptyState + intTestFunc "instructionIntDupN" [2, 2, 2] [GeneInt 2, GeneInt 3, StateFunc instructionIntDup] emptyState + intTestFunc "instructionIntSwap" [2, 0, 3] [GeneInt 3, GeneInt 2, GeneInt 0, StateFunc instructionIntSwap] emptyState + intTestFunc "instructionIntSwapFail" [1] [GeneInt 1, StateFunc instructionIntSwap] emptyState + intTestFunc "instructionIntRot" [1, 3, 2] [GeneInt 1, GeneInt 2, GeneInt 3, StateFunc instructionIntRot] emptyState + intTestFunc "instructionIntRotFail" [7, 8] [GeneInt 8, GeneInt 7, StateFunc instructionIntRot] emptyState + intTestFunc "instructionIntFlush" [] [GeneInt 9696, GeneInt 92, GeneInt 420] emptyState -- I think I'm funny + -- Exec tests intTestFunc "instructionExecIf" [6, 5] [GeneBool True, StateFunc instructionExecIf, Block [GeneInt 5, GeneInt 6], Block [GeneInt 7, GeneInt 8]] emptyState intTestFunc "instructionExecDup" [8] [StateFunc instructionExecDup, GeneInt 4, StateFunc instructionIntAdd] emptyState intTestFunc "instructionExecDoRange" [12] [GeneInt 2, Block [GeneInt 4, GeneInt 1, StateFunc instructionExecDoRange], StateFunc instructionIntAdd] emptyState @@ -38,8 +52,14 @@ main = do let loadedState = loadProgram [GeneBool False, StateFunc instructionExecWhen, GeneInt 71] emptyState assert (emptyState == interpretExec loadedState) putStrLn "instructionExecWhenFalse passed test." + -- Float tests floatTestFunc "instructionFloatAdd" [4.32] [GeneFloat 4.01, GeneFloat 0.31, StateFunc instructionFloatAdd] emptyState floatTestFunc "instructionFloatSub" [3.6900003] [GeneFloat 4.01, GeneFloat 0.32, StateFunc instructionFloatSub] emptyState floatTestFunc "instructionFloatMul" [1.38] [GeneFloat 0.12, GeneFloat 11.5, StateFunc instructionFloatMul] emptyState floatTestFunc "instructionFloatDiv" [57.5] [GeneFloat 11.5, GeneFloat 0.2, StateFunc instructionFloatDiv] emptyState floatTestFunc "instructionFloatDiv0" [0, 69.69] [GeneFloat 69.69, GeneFloat 0.0, StateFunc instructionFloatDiv] emptyState + + -- Bool tests + boolTestFunc "instructionIntEqTrue" [True] [GeneInt 3, GeneInt 3, StateFunc instructionIntEq] emptyState + boolTestFunc "instructionIntEqFalse" [False] [GeneInt 3, GeneInt 5, StateFunc instructionIntEq] emptyState + boolTestFunc "instructionIntEqFail" [] [GeneInt 3, StateFunc instructionIntEq] emptyState From 448b71dab16cc2f67ddea7fdf26adb46b1fce7d9 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sun, 19 Jan 2025 00:40:46 -0600 Subject: [PATCH 011/171] add stack depth --- src/Instructions/ExecInstructions.hs | 3 +++ src/Instructions/FloatInstructions.hs | 3 +++ src/Instructions/GenericInstructions.hs | 3 +++ src/Instructions/IntInstructions.hs | 3 +++ 4 files changed, 12 insertions(+) diff --git a/src/Instructions/ExecInstructions.hs b/src/Instructions/ExecInstructions.hs index cb28fb2..5c5888d 100644 --- a/src/Instructions/ExecInstructions.hs +++ b/src/Instructions/ExecInstructions.hs @@ -32,6 +32,9 @@ instructionExecFlush state = instructionFlush state exec instructionExecEq :: State -> State instructionExecEq state = instructionEq state exec +instructionExecStackDepth :: State -> State +instructionExecStackDepth state = instructionStackDepth state exec + instructionExecDoRange :: State -> State instructionExecDoRange state@(State {_exec = (e1 : es), _int = (i0 : i1 : is)}) = if increment i0 i1 /= 0 diff --git a/src/Instructions/FloatInstructions.hs b/src/Instructions/FloatInstructions.hs index fc05e0f..9064ce2 100644 --- a/src/Instructions/FloatInstructions.hs +++ b/src/Instructions/FloatInstructions.hs @@ -71,3 +71,6 @@ instructionFloatFlush state = instructionFlush state float instructionFloatEq :: State -> State instructionFloatEq state = instructionEq state float + +instructionFloatStackDepth :: State -> State +instructionFloatStackDepth state = instructionStackDepth state float diff --git a/src/Instructions/GenericInstructions.hs b/src/Instructions/GenericInstructions.hs index a1ff7ba..df956d2 100644 --- a/src/Instructions/GenericInstructions.hs +++ b/src/Instructions/GenericInstructions.hs @@ -63,3 +63,6 @@ instructionEq state accessor = where stackTop :: [a] stackTop = take 2 $ view accessor state + +instructionStackDepth :: State -> Lens' State [a] -> State +instructionStackDepth state accessor = state & int .~ (length (view accessor state) : view int state) diff --git a/src/Instructions/IntInstructions.hs b/src/Instructions/IntInstructions.hs index 87d9112..ae58588 100644 --- a/src/Instructions/IntInstructions.hs +++ b/src/Instructions/IntInstructions.hs @@ -76,3 +76,6 @@ instructionIntFlush state = instructionFlush state int instructionIntEq :: State -> State instructionIntEq state = instructionEq state int + +instructionIntStackDepth :: State -> State +instructionIntStackDepth state = instructionStackDepth state int From e84ebee427e0fe316e029ba8c9395930a8c67e45 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sun, 19 Jan 2025 00:41:02 -0600 Subject: [PATCH 012/171] fix up tests --- test/Main.hs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test/Main.hs b/test/Main.hs index 033cf39..f7891d4 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -7,6 +7,8 @@ import Instructions.FloatInstructions -- @TODO: Finish int and float tests +-- TODO: Need a function that can compare states. + intTestFunc :: String -> [Int] -> [Gene] -> State -> IO () intTestFunc name goal genome startState = let state = loadProgram genome startState @@ -31,13 +33,15 @@ main = do intTestFunc "instructionIntDiv" [3] [GeneInt 6, GeneInt 2, StateFunc instructionIntDiv] emptyState intTestFunc "instructionIntDiv0" [0, 2] [GeneInt 2, GeneInt 0, StateFunc instructionIntDiv] emptyState intTestFunc "instructionIntMod" [3] [GeneInt 13, GeneInt 5, StateFunc instructionIntMod] emptyState + intTestFunc "instructionIntPop" [2] [GeneInt 2, GeneInt 0, StateFunc instructionIntPop] emptyState intTestFunc "instrucitonIntDup" [3, 3, 2] [GeneInt 2, GeneInt 3, StateFunc instructionIntDup] emptyState - intTestFunc "instructionIntDupN" [2, 2, 2] [GeneInt 2, GeneInt 3, StateFunc instructionIntDup] emptyState + intTestFunc "instructionIntDupN" [2, 2, 2] [GeneInt 2, GeneInt 3, StateFunc instructionIntDupN] emptyState intTestFunc "instructionIntSwap" [2, 0, 3] [GeneInt 3, GeneInt 2, GeneInt 0, StateFunc instructionIntSwap] emptyState intTestFunc "instructionIntSwapFail" [1] [GeneInt 1, StateFunc instructionIntSwap] emptyState intTestFunc "instructionIntRot" [1, 3, 2] [GeneInt 1, GeneInt 2, GeneInt 3, StateFunc instructionIntRot] emptyState intTestFunc "instructionIntRotFail" [7, 8] [GeneInt 8, GeneInt 7, StateFunc instructionIntRot] emptyState - intTestFunc "instructionIntFlush" [] [GeneInt 9696, GeneInt 92, GeneInt 420] emptyState -- I think I'm funny + intTestFunc "instructionIntFlush" [] [GeneInt 9696, GeneInt 92, GeneInt 420, StateFunc instructionIntFlush] emptyState -- I think I'm funny + intTestFunc "instructionIntStackDepth" [2, 51, 52] [GeneInt 52, GeneInt 51, StateFunc instructionIntStackDepth] emptyState -- Exec tests intTestFunc "instructionExecIf" [6, 5] [GeneBool True, StateFunc instructionExecIf, Block [GeneInt 5, GeneInt 6], Block [GeneInt 7, GeneInt 8]] emptyState From ee1779b6112de1623b6041cbd038bbe620938ca1 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sun, 19 Jan 2025 17:16:23 -0600 Subject: [PATCH 013/171] add more generic instructions, need to fix yank --- src/Instructions/FloatInstructions.hs | 14 ++++++++++++- src/Instructions/GenericInstructions.hs | 28 +++++++++++++++++++++++++ src/Instructions/IntInstructions.hs | 5 +++++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/Instructions/FloatInstructions.hs b/src/Instructions/FloatInstructions.hs index 9064ce2..d1fd0b5 100644 --- a/src/Instructions/FloatInstructions.hs +++ b/src/Instructions/FloatInstructions.hs @@ -55,7 +55,7 @@ instructionFloatPop :: State -> State instructionFloatPop state = instructionPop state float instructionFloatDup :: State -> State -instructionFloatDup state = instructionPop state float +instructionFloatDup state = instructionDup state float instructionFloatDupN :: State -> State instructionFloatDupN state = instructionDupN state float @@ -74,3 +74,15 @@ instructionFloatEq state = instructionEq state float instructionFloatStackDepth :: State -> State instructionFloatStackDepth state = instructionStackDepth state float + +instructionFloatYankDup :: State -> State +instructionFloatYankDup state = instructionYankDup state float + +instructionFloatYank :: State -> State +instructionFloatYank state = instructionYank state float + +instructionFloatShoveDup :: State -> State +instructionFloatShoveDup state = instructionShoveDup state float + +instructionFloatShove :: State -> State +instructionFloatShove state = instructionShove state float diff --git a/src/Instructions/GenericInstructions.hs b/src/Instructions/GenericInstructions.hs index df956d2..9131412 100644 --- a/src/Instructions/GenericInstructions.hs +++ b/src/Instructions/GenericInstructions.hs @@ -66,3 +66,31 @@ instructionEq state accessor = instructionStackDepth :: State -> Lens' State [a] -> State instructionStackDepth state accessor = state & int .~ (length (view accessor state) : view int state) + +-- Will have a non-generic definition for the int stack +instructionYankDup :: State -> Lens' State [a] -> State +instructionYankDup state@(State {_int = i : is}) accessor = + if notEmptyStack state accessor + then (state & accessor .~ (view accessor state !! max 0 (min i (length (view accessor state) - 1))) : view accessor state) {_int = is} + else state +instructionYankDup state@(State {_int = []}) _ = state + +-- Is this optimal? Running instrucitonYankDup twice????? +-- int non generic too +instructionYank :: State -> Lens' State [a] -> State +instructionYank state accessor = instructionYankDup state accessor & accessor .~ init (view accessor (instructionYankDup state accessor)) + +combineTuple :: a -> ([a], [a]) -> [a] +combineTuple val tup = fst tup <> [val] <> snd tup + +-- int non generic :( +instructionShoveDup :: State -> Lens' State [a] -> State +instructionShoveDup state@(State {_int = i : is}) accessor = + if notEmptyStack state accessor + then (state & accessor .~ combineTuple (head $ view accessor state) (splitAt (max 0 (min i (length (view accessor state) - 1))) (view accessor state))) {_int = is} + else state +instructionShoveDup state@(State {_int = []}) _ = state + +-- also also not int generic +instructionShove :: State -> Lens' State [a] -> State +instructionShove state accessor = instructionShoveDup state accessor & accessor .~ drop 1 (view accessor (instructionShoveDup state accessor)) diff --git a/src/Instructions/IntInstructions.hs b/src/Instructions/IntInstructions.hs index ae58588..55ccdb1 100644 --- a/src/Instructions/IntInstructions.hs +++ b/src/Instructions/IntInstructions.hs @@ -79,3 +79,8 @@ instructionIntEq state = instructionEq state int instructionIntStackDepth :: State -> State instructionIntStackDepth state = instructionStackDepth state int + +-- int specific +instructionIntYank :: State -> State +instructionIntYank state@(State {_int = index : i2 : is}) = +instructionIntYank state = state From bc66bc8907f3486def9c48dc162da8d6e99a1756 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sun, 19 Jan 2025 17:17:09 -0600 Subject: [PATCH 014/171] add tests --- test/Main.hs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/Main.hs b/test/Main.hs index f7891d4..06cbeeb 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -62,6 +62,10 @@ main = do floatTestFunc "instructionFloatMul" [1.38] [GeneFloat 0.12, GeneFloat 11.5, StateFunc instructionFloatMul] emptyState floatTestFunc "instructionFloatDiv" [57.5] [GeneFloat 11.5, GeneFloat 0.2, StateFunc instructionFloatDiv] emptyState floatTestFunc "instructionFloatDiv0" [0, 69.69] [GeneFloat 69.69, GeneFloat 0.0, StateFunc instructionFloatDiv] emptyState + floatTestFunc "instructionFloatYank" [1.1, 3.3, 2.2] [GeneInt 3, GeneFloat 1.1, GeneFloat 2.2, GeneFloat 3.3, StateFunc instructionFloatYank] emptyState + floatTestFunc "instructionFloatYankDup" [1.1, 3.3, 2.2, 1.1] [GeneInt 3, GeneFloat 1.1, GeneFloat 2.2, GeneFloat 3.3, StateFunc instructionFloatYankDup] emptyState + floatTestFunc "instructionFloatShove" [3.3,2.2,4.4,1.1] [GeneInt 3, GeneFloat 1.1, GeneFloat 2.2, GeneFloat 3.3, GeneFloat 4.4, StateFunc instructionFloatShove] emptyState + floatTestFunc "instructionFloatShoveDup" [4.4,3.3,2.2,4.4,1.1] [GeneInt 3, GeneFloat 1.1, GeneFloat 2.2, GeneFloat 3.3, GeneFloat 4.4, StateFunc instructionFloatShoveDup] emptyState -- Bool tests boolTestFunc "instructionIntEqTrue" [True] [GeneInt 3, GeneInt 3, StateFunc instructionIntEq] emptyState From 774a0db148cf6e774352febf45f7a90220206cea Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sun, 19 Jan 2025 18:26:33 -0600 Subject: [PATCH 015/171] undefined added so can compile --- src/Instructions/IntInstructions.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Instructions/IntInstructions.hs b/src/Instructions/IntInstructions.hs index 55ccdb1..2a1c7b6 100644 --- a/src/Instructions/IntInstructions.hs +++ b/src/Instructions/IntInstructions.hs @@ -82,5 +82,5 @@ instructionIntStackDepth state = instructionStackDepth state int -- int specific instructionIntYank :: State -> State -instructionIntYank state@(State {_int = index : i2 : is}) = +instructionIntYank state@(State {_int = index : i2 : is}) = undefined instructionIntYank state = state From add9503c88890885d8c21a56ebaddad16bffdc28 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sun, 19 Jan 2025 19:50:27 -0600 Subject: [PATCH 016/171] fix yank --- src/Instructions/GenericInstructions.hs | 18 ++++++++++++++++-- src/Instructions/IntInstructions.hs | 6 +++--- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/Instructions/GenericInstructions.hs b/src/Instructions/GenericInstructions.hs index 9131412..3513a95 100644 --- a/src/Instructions/GenericInstructions.hs +++ b/src/Instructions/GenericInstructions.hs @@ -75,10 +75,24 @@ instructionYankDup state@(State {_int = i : is}) accessor = else state instructionYankDup state@(State {_int = []}) _ = state +deleteAt :: Int -> [a] -> [a] +deleteAt idx xs = take idx xs <> drop 1 (drop idx xs) + -- Is this optimal? Running instrucitonYankDup twice????? -- int non generic too -instructionYank :: State -> Lens' State [a] -> State -instructionYank state accessor = instructionYankDup state accessor & accessor .~ init (view accessor (instructionYankDup state accessor)) +instructionYank :: forall a. State -> Lens' State [a] -> State +-- instructionYank state accessor = instructionYankDup state accessor & accessor .~ init (view accessor (instructionYankDup state accessor)) +instructionYank state@(State {_int = rawIndex : _}) accessor = + let + myIndex :: Int + myIndex = max 0 (min rawIndex (length (view accessor state) - 1)) + item :: a + item = view accessor state !! myIndex + deletedState :: State + deletedState = state & accessor .~ deleteAt myIndex (view accessor state) + in + if notEmptyStack state accessor then deletedState & accessor .~ item : view accessor deletedState else state +instructionYank state _ = state combineTuple :: a -> ([a], [a]) -> [a] combineTuple val tup = fst tup <> [val] <> snd tup diff --git a/src/Instructions/IntInstructions.hs b/src/Instructions/IntInstructions.hs index 2a1c7b6..1227003 100644 --- a/src/Instructions/IntInstructions.hs +++ b/src/Instructions/IntInstructions.hs @@ -81,6 +81,6 @@ instructionIntStackDepth :: State -> State instructionIntStackDepth state = instructionStackDepth state int -- int specific -instructionIntYank :: State -> State -instructionIntYank state@(State {_int = index : i2 : is}) = undefined -instructionIntYank state = state +-- instructionIntYank :: State -> State +-- instructionIntYank state@(State {_int = index : i2 : is}) = undefined +-- instructionIntYank state = state From e11b813d0031ed00625e842cca4bb08e5e263219 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sun, 19 Jan 2025 19:50:37 -0600 Subject: [PATCH 017/171] fix tests as well --- test/Main.hs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/Main.hs b/test/Main.hs index 06cbeeb..831118d 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -62,8 +62,8 @@ main = do floatTestFunc "instructionFloatMul" [1.38] [GeneFloat 0.12, GeneFloat 11.5, StateFunc instructionFloatMul] emptyState floatTestFunc "instructionFloatDiv" [57.5] [GeneFloat 11.5, GeneFloat 0.2, StateFunc instructionFloatDiv] emptyState floatTestFunc "instructionFloatDiv0" [0, 69.69] [GeneFloat 69.69, GeneFloat 0.0, StateFunc instructionFloatDiv] emptyState - floatTestFunc "instructionFloatYank" [1.1, 3.3, 2.2] [GeneInt 3, GeneFloat 1.1, GeneFloat 2.2, GeneFloat 3.3, StateFunc instructionFloatYank] emptyState - floatTestFunc "instructionFloatYankDup" [1.1, 3.3, 2.2, 1.1] [GeneInt 3, GeneFloat 1.1, GeneFloat 2.2, GeneFloat 3.3, StateFunc instructionFloatYankDup] emptyState + floatTestFunc "instructionFloatYank" [1.1, 4.4, 3.3, 2.2] [GeneInt 3, GeneFloat 1.1, GeneFloat 2.2, GeneFloat 3.3, GeneFloat 4.4, StateFunc instructionFloatYank] emptyState + floatTestFunc "instructionFloatYankDup" [1.1, 4.4, 3.3, 2.2, 1.1] [GeneInt 3, GeneFloat 1.1, GeneFloat 2.2, GeneFloat 3.3, GeneFloat 4.4, StateFunc instructionFloatYankDup] emptyState floatTestFunc "instructionFloatShove" [3.3,2.2,4.4,1.1] [GeneInt 3, GeneFloat 1.1, GeneFloat 2.2, GeneFloat 3.3, GeneFloat 4.4, StateFunc instructionFloatShove] emptyState floatTestFunc "instructionFloatShoveDup" [4.4,3.3,2.2,4.4,1.1] [GeneInt 3, GeneFloat 1.1, GeneFloat 2.2, GeneFloat 3.3, GeneFloat 4.4, StateFunc instructionFloatShoveDup] emptyState From f99179278762a9b2212aee4a4b64eb9ddc3f86e6 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sun, 19 Jan 2025 20:50:47 -0600 Subject: [PATCH 018/171] add generic(ish) int instructions --- src/Instructions/IntInstructions.hs | 39 ++++++++++++++++++++++++++--- test/Main.hs | 4 +++ 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/Instructions/IntInstructions.hs b/src/Instructions/IntInstructions.hs index 1227003..8564967 100644 --- a/src/Instructions/IntInstructions.hs +++ b/src/Instructions/IntInstructions.hs @@ -81,6 +81,39 @@ instructionIntStackDepth :: State -> State instructionIntStackDepth state = instructionStackDepth state int -- int specific --- instructionIntYank :: State -> State --- instructionIntYank state@(State {_int = index : i2 : is}) = undefined --- instructionIntYank state = state +instructionIntYank :: State -> State +-- instructionIntYank state = instructionYank state int +instructionIntYank state@(State {_int = rawIndex : i1 : is}) = + let + myIndex :: Int + myIndex = max 0 (min rawIndex (length is - 1)) + in + state {_int = is !! myIndex : i1 : deleteAt myIndex is} +instructionIntYank state = state + +instructionIntYankDup :: State -> State +instructionIntYankDup state@(State {_int = rawIndex : item : is}) = + let + myIndex :: Int + myIndex = max 0 (min rawIndex (length is - 1)) + in + state {_int = is !! myIndex : item : is} +instructionIntYankDup state = state + +instructionIntShove :: State -> State +instructionIntShove state@(State {_int = rawIndex : item : is}) = + let + myIndex :: Int + myIndex = max 0 (min rawIndex (length is - 1)) + in + state {_int = combineTuple item (splitAt myIndex is)} +instructionIntShove state = state + +instructionIntShoveDup :: State -> State +instructionIntShoveDup state@(State {_int = rawIndex : item : is}) = + let + myIndex :: Int + myIndex = max 0 (min rawIndex (length is - 1)) + in + state {_int = item : combineTuple item (splitAt myIndex is)} +instructionIntShoveDup state = state diff --git a/test/Main.hs b/test/Main.hs index 831118d..7941c3a 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -42,6 +42,10 @@ main = do intTestFunc "instructionIntRotFail" [7, 8] [GeneInt 8, GeneInt 7, StateFunc instructionIntRot] emptyState intTestFunc "instructionIntFlush" [] [GeneInt 9696, GeneInt 92, GeneInt 420, StateFunc instructionIntFlush] emptyState -- I think I'm funny intTestFunc "instructionIntStackDepth" [2, 51, 52] [GeneInt 52, GeneInt 51, StateFunc instructionIntStackDepth] emptyState + intTestFunc "instructionIntYank" [3,3,2,1] [GeneInt 3, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 4, StateFunc instructionIntYank] emptyState + intTestFunc "instructionIntYankDup" [3,3,2,1,3] [GeneInt 3, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 4, StateFunc instructionIntYankDup] emptyState + intTestFunc "instructionIntShove" [2,1,3,1] [GeneInt 1, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 2, StateFunc instructionIntShove] emptyState + intTestFunc "instructionIntShoveDup" [3,2,1,3,1] [GeneInt 1, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 2, StateFunc instructionIntShoveDup] emptyState -- Exec tests intTestFunc "instructionExecIf" [6, 5] [GeneBool True, StateFunc instructionExecIf, Block [GeneInt 5, GeneInt 6], Block [GeneInt 7, GeneInt 8]] emptyState From d67ed6a821047f7085f8e07fcfebbf56394c4b1b Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sun, 19 Jan 2025 20:53:28 -0600 Subject: [PATCH 019/171] remove string stack (why do we have this?) --- src/State.hs | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/State.hs b/src/State.hs index 42fc2a3..1c60a29 100644 --- a/src/State.hs +++ b/src/State.hs @@ -12,11 +12,9 @@ data Gene = GeneInt Int | GeneFloat Float | GeneBool Bool - | GeneString String | GeneIntVector [Int] | GeneFloatVector [Float] | GeneBoolVector [Bool] - | GeneStringVector [String] | StateFunc (State -> State) | PlaceInput String | Close @@ -26,12 +24,10 @@ instance Eq Gene where GeneInt x == GeneInt y = x == y GeneFloat x == GeneFloat y = x == y GeneBool x == GeneBool y = x == y - GeneString x == GeneString y = x == y PlaceInput x == PlaceInput y = x == y GeneIntVector xs == GeneIntVector ys = xs == ys GeneFloatVector xs == GeneFloatVector ys = xs == ys GeneBoolVector xs == GeneBoolVector ys = xs == ys - GeneStringVector xs == GeneStringVector ys = xs == ys Close == Close = True StateFunc _ == StateFunc _ = True -- This line is probably not the best thing to do Block [x] == Block [y] = [x] == [y] @@ -41,13 +37,11 @@ instance Show Gene where show (GeneInt x) = "Int: " <> show x show (GeneFloat x) = "Float: " <> show x show (GeneBool x) = "Bool: " <> show x - show (GeneString x) = "String: " <> x show (StateFunc _) = "Func: unnamed" show (PlaceInput x) = "In: " <> x show (GeneIntVector xs) = "Int Vec: " <> show xs show (GeneFloatVector xs) = "Float Vec: " <> show xs show (GeneBoolVector xs) = "Bool Vec: " <> show xs - show (GeneStringVector xs) = "String Vec: " <> show xs show Close = "Close" show (Block xs) = "Block: " <> show xs @@ -56,11 +50,9 @@ data State = State _int :: [Int], _float :: [Float], _bool :: [Bool], - _string :: [String], _intVector :: [[Int]], _floatVector :: [[Float]], _boolVector :: [[Bool]], - _stringVector :: [[String]], _parameter :: [Gene], _input :: Map.Map String Gene } @@ -75,12 +67,10 @@ emptyState = _int = [], _float = [], _bool = [], - _string = [], _parameter = [], _intVector = [], _floatVector = [], _boolVector = [], - _stringVector = [], _input = Map.empty } @@ -91,11 +81,9 @@ exampleState = _int = [32, 56], _float = [3.23, 9.235], _bool = [True, False], - _string = ["abc", "123"], _parameter = [], _intVector = [[1,2], [5,6,8]], _floatVector = [[1.234, 9.21], [5.42, 6.221, 8.5493]], _boolVector = [[True, False], [False, False, True]], - _stringVector = [["def", "567"], ["gamer", "fellah", "live action how to train your dragon"]], _input = Map.empty } From 6dbf4e79051ac03655e3321bdb26d8488dbc72d4 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sun, 19 Jan 2025 20:56:34 -0600 Subject: [PATCH 020/171] comments --- src/Instructions/GenericInstructions.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Instructions/GenericInstructions.hs b/src/Instructions/GenericInstructions.hs index 3513a95..835f510 100644 --- a/src/Instructions/GenericInstructions.hs +++ b/src/Instructions/GenericInstructions.hs @@ -81,7 +81,6 @@ deleteAt idx xs = take idx xs <> drop 1 (drop idx xs) -- Is this optimal? Running instrucitonYankDup twice????? -- int non generic too instructionYank :: forall a. State -> Lens' State [a] -> State --- instructionYank state accessor = instructionYankDup state accessor & accessor .~ init (view accessor (instructionYankDup state accessor)) instructionYank state@(State {_int = rawIndex : _}) accessor = let myIndex :: Int @@ -98,6 +97,7 @@ combineTuple :: a -> ([a], [a]) -> [a] combineTuple val tup = fst tup <> [val] <> snd tup -- int non generic :( +-- Rewrite this eventually? instructionShoveDup :: State -> Lens' State [a] -> State instructionShoveDup state@(State {_int = i : is}) accessor = if notEmptyStack state accessor From 5c8fd8fbd94e810dea760b45d4c05f1039c402c3 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sun, 19 Jan 2025 21:00:19 -0600 Subject: [PATCH 021/171] lenses marked as completed --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 730117e..ed13b66 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ library for more abstraction. * [x] Write tests for every function. * [x] tests/ are just copied from make-grade, need to write for this project. * [ ] Included examples of basic assertions, QuickCheck, Hspec, hspec-quickcheck. -* [ ] Look at Lenses library for abstraction +* [x] Look at Lenses library for abstraction ## Design considerations The biggest design constraint is that for the exec stack (but not data stacks) From 23eddb756d0bc559b5f8fba71903e789bfa0f842 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sun, 19 Jan 2025 21:12:50 -0600 Subject: [PATCH 022/171] add basic trig functions to floats --- src/Instructions/FloatInstructions.hs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Instructions/FloatInstructions.hs b/src/Instructions/FloatInstructions.hs index d1fd0b5..caf2589 100644 --- a/src/Instructions/FloatInstructions.hs +++ b/src/Instructions/FloatInstructions.hs @@ -86,3 +86,15 @@ instructionFloatShoveDup state = instructionShoveDup state float instructionFloatShove :: State -> State instructionFloatShove state = instructionShove state float + +instructionFloatSin :: State -> State +instructionFloatSin state@(State {_float = f1 : fs}) = state {_float = sin f1 : fs} +instructionFloatSin state = state + +instructionFloatCos :: State -> State +instructionFloatCos state@(State {_float = f1 : fs}) = state {_float = cos f1 : fs} +instructionFloatCos state = state + +instructionFloatTan :: State -> State +instructionFloatTan state@(State {_float = f1 : fs}) = state {_float = tan f1 : fs} +instructionFloatTan state = state From 13fc5769fec387376e93cb93d913a31387bef132 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sun, 19 Jan 2025 21:55:22 -0600 Subject: [PATCH 023/171] add logical --- HushGP.cabal | 1 + 1 file changed, 1 insertion(+) diff --git a/HushGP.cabal b/HushGP.cabal index dc3e72c..22b39c6 100644 --- a/HushGP.cabal +++ b/HushGP.cabal @@ -42,6 +42,7 @@ library , Instructions.ExecInstructions , Instructions.FloatInstructions , Instructions.GenericInstructions + , Instructions.LogicalInstructions -- Modules included in this library but not exported. -- other-modules: From 5fb5ea9d7d19d70b132a228ec755c1da71d9d2cd Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sun, 19 Jan 2025 21:55:56 -0600 Subject: [PATCH 024/171] add more floatinstructions --- src/Instructions/FloatInstructions.hs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/Instructions/FloatInstructions.hs b/src/Instructions/FloatInstructions.hs index caf2589..df86fbb 100644 --- a/src/Instructions/FloatInstructions.hs +++ b/src/Instructions/FloatInstructions.hs @@ -1,8 +1,19 @@ module Instructions.FloatInstructions where +import Data.Fixed (mod') import Instructions.GenericInstructions import State +-- stopped here for now: https://erp12.github.io/pyshgp/html/core_instructions.html#bool-invert-first-then-and + +instructionFloatFromInt :: State -> State +instructionFloatFromInt state@(State {_float = fs, _int = (i : is)}) = state {_float = (fromIntegral i :: Float) : fs, _int = is} +instructionFloatFromInt state = state + +instructionFloatFromBool :: State -> State +instructionFloatFromBool state@(State {_bool = (b : bs), _float = fs}) = state {_bool = bs, _float = (if b then 1.0 else 0.0) : fs} +instructionFloatFromBool state = state + instructionFloatAdd :: State -> State instructionFloatAdd state@(State {_float = (f1 : f2 : fs)}) = state {_float = f2 + f1 : fs} instructionFloatAdd state = state @@ -19,6 +30,10 @@ instructionFloatDiv :: State -> State instructionFloatDiv state@(State {_float = (f1 : f2 : fs)}) = state {_float = if f1 /= 0 then f2 / f1 : fs else f1 : f2 : fs} instructionFloatDiv state = state +instructionFloatMod :: State -> State +instructionFloatMod state@(State {_float = (f1 : f2 : fs)}) = state {_float = f2 `mod'` f1 : fs} +instructionFloatMod state = state + instructionFloatMin :: State -> State instructionFloatMin state@(State {_float = (f1 : f2 : fs)}) = state {_float = min f1 f2 : fs} instructionFloatMin state = state From 40797972f3dd51733a346d1952833e9c18f82379 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sun, 19 Jan 2025 21:56:17 -0600 Subject: [PATCH 025/171] int/bool instructions --- src/Instructions/IntInstructions.hs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Instructions/IntInstructions.hs b/src/Instructions/IntInstructions.hs index 8564967..bdbc783 100644 --- a/src/Instructions/IntInstructions.hs +++ b/src/Instructions/IntInstructions.hs @@ -4,6 +4,14 @@ import State import Instructions.GenericInstructions -- import Debug.Trace +instructionIntFromFloat :: State -> State +instructionIntFromFloat state@(State {_float = (f : fs), _int = is}) = state {_float = fs, _int = floor f : is} +instructionIntFromFloat state = state + +instructionIntFromBool :: State -> State +instructionIntFromBool state@(State {_bool = (b : bs), _int = is}) = state {_bool = bs, _int = (if b then 1 else 0) : is} +instructionIntFromBool state = state + instructionIntAdd :: State -> State instructionIntAdd state@(State {_int = (i1 : i2 : is)}) = state {_int = i2 + i1 : is} instructionIntAdd state = state From be4c15cd518ec88ea1e1b1fb0be90d17aaa10e9c Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sun, 19 Jan 2025 21:56:35 -0600 Subject: [PATCH 026/171] logicalInstructions added --- src/Instructions/LogicalInstructions.hs | 31 +++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/Instructions/LogicalInstructions.hs diff --git a/src/Instructions/LogicalInstructions.hs b/src/Instructions/LogicalInstructions.hs new file mode 100644 index 0000000..ad07706 --- /dev/null +++ b/src/Instructions/LogicalInstructions.hs @@ -0,0 +1,31 @@ +module Instructions.LogicalInstructions where + +import State + +instructionBoolFromInt :: State -> State +instructionBoolFromInt state@(State {_int = (i : is), _bool = bs}) = state {_int = is, _bool = (i /= 0) : bs} +instructionBoolFromInt state = state + +instructionBoolFromFloat :: State -> State +instructionBoolFromFloat state@(State {_float = (f : fs), _bool = bs}) = state {_float = fs, _bool = (f /= 0) : bs} +instructionBoolFromFloat state = state + +boolTemplate :: (Bool -> Bool -> Bool) -> State -> State +boolTemplate func state@(State {_bool = (b1 : b2 : bs)}) = state {_bool = func b1 b2 : bs} +boolTemplate _ state = state + +instructionBoolAnd :: State -> State +instructionBoolAnd = boolTemplate (&&) + +instructionBoolOr :: State -> State +instructionBoolOr = boolTemplate (||) + +-- no builtin haskell xor moment +xor :: Bool -> Bool -> Bool +xor b1 b2 + | b1 && not b2 = True + | not b1 && b2 = True + | otherwise = False + +instructionBoolXor :: State -> State +instructionBoolXor = boolTemplate xor From 7059447dcd15630773b4ad5a8e1e50572217d776 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sun, 19 Jan 2025 21:57:31 -0600 Subject: [PATCH 027/171] update readme for this branch --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ed13b66..d2a573d 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,10 @@ # HushGP A PushGP implementation in Haskell -This branch is meant to overhaul the system with the lens -library for more abstraction. +This branch is meant to to go one by one and finish implementing all of +the functions in the pyshgp list. + +https://erp12.github.io/pyshgp/html/core_instructions.html ## Tasks * [ ] Post minimal core of exec to haskell discourse for advice about speed optimization. From 9f6a72939afb168d8e26f26f14794be3d2ab59be Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Mon, 20 Jan 2025 14:05:43 -0600 Subject: [PATCH 028/171] add message about no-ops --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index d2a573d..1fd4505 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,8 @@ the functions in the pyshgp list. https://erp12.github.io/pyshgp/html/core_instructions.html +I'm not going to include no-ops + ## Tasks * [ ] Post minimal core of exec to haskell discourse for advice about speed optimization. * [x] Do test-driven development on this one. From 6a45dec26389060c39d84554919f093cf92cbafc Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Mon, 20 Jan 2025 23:21:33 -0600 Subject: [PATCH 029/171] added more instructions (NEED TO BE TESTED) --- src/Instructions/CodeInstructions.hs | 114 ++++++++++++++++++++++++ src/Instructions/FloatInstructions.hs | 2 - src/Instructions/LogicalInstructions.hs | 8 ++ 3 files changed, 122 insertions(+), 2 deletions(-) create mode 100644 src/Instructions/CodeInstructions.hs diff --git a/src/Instructions/CodeInstructions.hs b/src/Instructions/CodeInstructions.hs new file mode 100644 index 0000000..6a5721a --- /dev/null +++ b/src/Instructions/CodeInstructions.hs @@ -0,0 +1,114 @@ +module Instructions.CodeInstructions where + +import State +import Instructions.GenericInstructions + +isBlock :: Gene -> Bool +isBlock (Block _) = True +isBlock _ = False + +blockLength :: Gene -> Int +blockLength (Block xs) = length xs +blockLength _ = 1 + +-- I think I can abstract the boilerplate base case check for a lot of these +-- with a different function + +-- empty Blocks are a thing but that shouldn't really matter +extractFirstFromBlock :: Gene -> Gene +extractFirstFromBlock (Block (x : _)) = x +extractFirstFromBlock gene = gene + +extractLastFromBlock :: Gene -> Gene +extractLastFromBlock (Block xs) = last xs +extractLastFromBlock gene = gene + +extractInitFromBlock :: Gene -> Gene +extractInitFromBlock (Block []) = Block [] +extractInitFromBlock (Block xs) = Block (init xs) +extractInitFromBlock gene = gene + +extractTailFromBlock :: Gene -> Gene +extractTailFromBlock (Block xs) = Block (drop 1 xs) +extractTailFromBlock gene = gene + +codeCombine :: Gene -> Gene -> Gene +codeCombine (Block xs) (Block ys) = Block (xs <> ys) +codeCombine (Block xs) ygene = Block (xs <> [ygene]) +codeCombine xgene (Block ys) = Block (xgene : ys) +codeCombine xgene ygene = Block [xgene, ygene] + +instructionCodePop :: State -> State +instructionCodePop state = instructionPop state code + +instructionCodeFromExec :: State -> State +instructionCodeFromExec state@(State {_exec = (e1 : es), _code = cs}) = state {_exec = es, _code = e1 : cs} +instructionCodeFromExec state = state + +instructionCodeIsCodeBlock :: State -> State +instructionCodeIsCodeBlock state@(State {_code = (c : cs), _bool = bs}) = state {_code = cs, _bool = isBlock c : bs} +instructionCodeIsCodeBlock state = state + +instructionCodeIsSingular :: State -> State +instructionCodeIsSingular state@(State {_code = (c : cs), _bool = bs}) = state {_code = cs, _bool = not (isBlock c) : bs} +instructionCodeIsSingular state = state + +instructionCodeLength :: State -> State +instructionCodeLength state@(State {_code = (c : cs), _int = is}) = state {_code = cs, _int = blockLength c : is} +instructionCodeLength state = state + +instructionCodeFirst :: State -> State +instructionCodeFirst state@(State {_code = (c : cs)}) = state {_code = extractFirstFromBlock c : cs} +instructionCodeFirst state = state + +instructionCodeLast :: State -> State +instructionCodeLast state@(State {_code = (c : cs)}) = state {_code = extractLastFromBlock c : cs} +instructionCodeLast state = state + +-- https://erp12.github.io/pyshgp/html/core_instructions.html#code-rest +instructionCodeTail :: State -> State +instructionCodeTail state@(State {_code = (c : cs)}) = state {_code = extractTailFromBlock c : cs} +instructionCodeTail state = state + +-- https://erp12.github.io/pyshgp/html/core_instructions.html#code-but-last +instructionCodeInit :: State -> State +instructionCodeInit state@(State {_code = (c : cs)}) = state {_code = extractInitFromBlock c : cs} +instructionCodeInit state = state + +instructionCodeWrap :: State -> State +instructionCodeWrap state@(State {_code = (c : cs)}) = state {_code = Block [c] : cs} +instructionCodeWrap state = state + +instructionCodeList :: State -> State +instructionCodeList state@(State {_code = (c1 : c2 : cs)}) = state {_code = Block [c1, c2] : cs} +instructionCodeList state = state + +instructionCodeCombine :: State -> State +instructionCodeCombine state@(State {_code = (c1 : c2 : cs)}) = state {_code = codeCombine c1 c2 : cs} +instructionCodeCombine state = state + +instructionCodeDo :: State -> State +instructionCodeDo state@(State {_code = (c1 : cs), _exec = es}) = state {_code = cs, _exec = c1: es} +instructionCodeDo state = state + +instructionCodeDoDup :: State -> State +instructionCodeDoDup state@(State {_code = (c1 : cs), _exec = es}) = state {_code = c1 : cs, _exec = c1 : es} +instructionCodeDoDup state = state + +-- https://erp12.github.io/pyshgp/html/core_instructions.html#code-do-then-pop +instructionCodeDoThenPop :: State -> State +instructionCodeDoThenPop state@(State {_code = c1 : _, _exec = es}) = state {_exec = c1 : StateFunc instructionCodePop : es} +instructionCodeDoThenPop state = state + +instructionCodeDoRange :: State -> State +instructionCodeDoRange state@(State {_code = (c1 : cs), _int = (i0 : i1 : is), _exec = es}) = + if increment i0 i1 /= 0 + then state {_exec = c1 : Block [GeneInt (i1 + increment i0 i1), GeneInt i0, StateFunc instructionCodeFromExec, c1, StateFunc instructionCodeDoRange] : es, _int = i1 : is, _code = cs} + else state {_exec = c1: es, _int = i1 : is, _code = cs} + where + increment :: Int -> Int -> Int + increment destIdx currentIdx + | currentIdx < destIdx = 1 + | currentIdx > destIdx = -1 + | otherwise = 0 +instructionCodeDoRange state = state diff --git a/src/Instructions/FloatInstructions.hs b/src/Instructions/FloatInstructions.hs index df86fbb..ede4a47 100644 --- a/src/Instructions/FloatInstructions.hs +++ b/src/Instructions/FloatInstructions.hs @@ -4,8 +4,6 @@ import Data.Fixed (mod') import Instructions.GenericInstructions import State --- stopped here for now: https://erp12.github.io/pyshgp/html/core_instructions.html#bool-invert-first-then-and - instructionFloatFromInt :: State -> State instructionFloatFromInt state@(State {_float = fs, _int = (i : is)}) = state {_float = (fromIntegral i :: Float) : fs, _int = is} instructionFloatFromInt state = state diff --git a/src/Instructions/LogicalInstructions.hs b/src/Instructions/LogicalInstructions.hs index ad07706..e94b66e 100644 --- a/src/Instructions/LogicalInstructions.hs +++ b/src/Instructions/LogicalInstructions.hs @@ -17,6 +17,14 @@ boolTemplate _ state = state instructionBoolAnd :: State -> State instructionBoolAnd = boolTemplate (&&) +instructionBoolInvertFirstThenAnd :: State -> State +instructionBoolInvertFirstThenAnd state@(State {_bool = (b1 : bs)}) = boolTemplate (&&) state {_bool = not b1 : bs} +instructionBoolInvertFirstThenAnd state = state + +instructionBoolInvertSecondThenAnd :: State -> State +instructionBoolInvertSecondThenAnd state@(State {_bool = (b1 : b2 : bs)}) = boolTemplate (&&) state {_bool = b1 : not b2 : bs} +instructionBoolInvertSecondThenAnd state = state + instructionBoolOr :: State -> State instructionBoolOr = boolTemplate (||) From 933c8fc4453124d6b784e3a86897c6c98871fe4e Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Mon, 20 Jan 2025 23:21:52 -0600 Subject: [PATCH 030/171] add code stack instructions --- HushGP.cabal | 1 + 1 file changed, 1 insertion(+) diff --git a/HushGP.cabal b/HushGP.cabal index 22b39c6..42b8f7d 100644 --- a/HushGP.cabal +++ b/HushGP.cabal @@ -43,6 +43,7 @@ library , Instructions.FloatInstructions , Instructions.GenericInstructions , Instructions.LogicalInstructions + , Instructions.CodeInstructions -- Modules included in this library but not exported. -- other-modules: From 32c83c13e3a50a415ea4d457705b4ce57293df95 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Mon, 20 Jan 2025 23:22:04 -0600 Subject: [PATCH 031/171] README note --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1fd4505..ace7b9c 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ the functions in the pyshgp list. https://erp12.github.io/pyshgp/html/core_instructions.html -I'm not going to include no-ops +I'm not going to include no-ops. ## Tasks * [ ] Post minimal core of exec to haskell discourse for advice about speed optimization. From 4e42c4332b19e1f52f5b8443019e2052e452beb0 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Mon, 20 Jan 2025 23:22:31 -0600 Subject: [PATCH 032/171] remove string Gene calls --- src/Push.hs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/Push.hs b/src/Push.hs index 8925bf7..7a76068 100644 --- a/src/Push.hs +++ b/src/Push.hs @@ -2,13 +2,13 @@ module Push where -import qualified Data.Map as Map import Control.Lens +import Data.Map qualified as Map -- import Instructions.IntInstructions -- import Instructions.ExecInstructions import State -import Debug.Trace (trace, traceStack) +-- import Debug.Trace (trace, traceStack) -- Each core func should be: (State -> State -> State) -- but each core function can use abstract helper functions. @@ -23,11 +23,9 @@ instructionParameterLoad state@(State {_parameter = (p : _)}) = case p of (GeneInt val) -> state & int .~ val : view int state (GeneFloat val) -> state & float .~ val : view float state (GeneBool val) -> state & bool .~ val : view bool state - (GeneString val) -> state & string .~ val : view string state (GeneIntVector val) -> state & intVector .~ val : view intVector state (GeneFloatVector val) -> state & floatVector .~ val : view floatVector state (GeneBoolVector val) -> state & boolVector .~ val : view boolVector state - (GeneStringVector val) -> state & stringVector .~ val : view stringVector state (StateFunc _) -> undefined (PlaceInput _) -> undefined Close -> undefined @@ -55,11 +53,9 @@ interpretExec state@(State {_exec = (e : es)}) = (GeneInt val) -> interpretExec (state & exec .~ es & int .~ val : view int state) (GeneFloat val) -> interpretExec (state & exec .~ es & float .~ val : view float state) (GeneBool val) -> interpretExec (state & exec .~ es & bool .~ val : view bool state) - (GeneString val) -> interpretExec (state & exec .~ es & string .~ val : view string state) (GeneIntVector val) -> interpretExec (state & exec .~ es & intVector .~ val : view intVector state) (GeneFloatVector val) -> interpretExec (state & exec .~ es & floatVector .~ val : view floatVector state) (GeneBoolVector val) -> interpretExec (state & exec .~ es & boolVector .~ val : view boolVector state) - (GeneStringVector val) -> interpretExec (state & exec .~ es & stringVector .~ val : view stringVector state) (StateFunc func) -> interpretExec $ func state {_exec = es} (Block block) -> interpretExec (state {_exec = block ++ es}) (PlaceInput val) -> interpretExec (state {_exec = (view input state Map.! val) : es}) From 2ca1f0fe9481829ed095ad73459356a6442931d7 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Mon, 20 Jan 2025 23:22:58 -0600 Subject: [PATCH 033/171] add code stack --- src/State.hs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/State.hs b/src/State.hs index 1c60a29..539a29d 100644 --- a/src/State.hs +++ b/src/State.hs @@ -1,8 +1,9 @@ {-# LANGUAGE TemplateHaskell #-} + module State where -import qualified Data.Map as Map import Control.Lens +import Data.Map qualified as Map -- The exec stack must store heterogenous types, -- and we must be able to detect that type at runtime. @@ -26,7 +27,7 @@ instance Eq Gene where GeneBool x == GeneBool y = x == y PlaceInput x == PlaceInput y = x == y GeneIntVector xs == GeneIntVector ys = xs == ys - GeneFloatVector xs == GeneFloatVector ys = xs == ys + GeneFloatVector xs == GeneFloatVector ys = xs == ys GeneBoolVector xs == GeneBoolVector ys = xs == ys Close == Close = True StateFunc _ == StateFunc _ = True -- This line is probably not the best thing to do @@ -47,6 +48,7 @@ instance Show Gene where data State = State { _exec :: [Gene], + _code :: [Gene], _int :: [Int], _float :: [Float], _bool :: [Bool], @@ -64,6 +66,7 @@ emptyState :: State emptyState = State { _exec = [], + _code = [], _int = [], _float = [], _bool = [], @@ -75,14 +78,15 @@ emptyState = } exampleState :: State -exampleState = +exampleState = State { _exec = [], + _code = [], _int = [32, 56], _float = [3.23, 9.235], _bool = [True, False], _parameter = [], - _intVector = [[1,2], [5,6,8]], + _intVector = [[1, 2], [5, 6, 8]], _floatVector = [[1.234, 9.21], [5.42, 6.221, 8.5493]], _boolVector = [[True, False], [False, False, True]], _input = Map.empty From fec6cc32701813400c7328c30bfedb24cfcfe25e Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Mon, 20 Jan 2025 23:23:32 -0600 Subject: [PATCH 034/171] add/fix tests --- test/Main.hs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/test/Main.hs b/test/Main.hs index 7941c3a..70880a7 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -1,9 +1,9 @@ import Control.Exception (assert) -import Push -import State -import Instructions.IntInstructions import Instructions.ExecInstructions import Instructions.FloatInstructions +import Instructions.IntInstructions +import Push +import State -- @TODO: Finish int and float tests @@ -42,10 +42,10 @@ main = do intTestFunc "instructionIntRotFail" [7, 8] [GeneInt 8, GeneInt 7, StateFunc instructionIntRot] emptyState intTestFunc "instructionIntFlush" [] [GeneInt 9696, GeneInt 92, GeneInt 420, StateFunc instructionIntFlush] emptyState -- I think I'm funny intTestFunc "instructionIntStackDepth" [2, 51, 52] [GeneInt 52, GeneInt 51, StateFunc instructionIntStackDepth] emptyState - intTestFunc "instructionIntYank" [3,3,2,1] [GeneInt 3, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 4, StateFunc instructionIntYank] emptyState - intTestFunc "instructionIntYankDup" [3,3,2,1,3] [GeneInt 3, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 4, StateFunc instructionIntYankDup] emptyState - intTestFunc "instructionIntShove" [2,1,3,1] [GeneInt 1, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 2, StateFunc instructionIntShove] emptyState - intTestFunc "instructionIntShoveDup" [3,2,1,3,1] [GeneInt 1, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 2, StateFunc instructionIntShoveDup] emptyState + intTestFunc "instructionIntYank" [3, 3, 2, 1] [GeneInt 3, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 4, StateFunc instructionIntYank] emptyState + intTestFunc "instructionIntYankDup" [3, 3, 2, 1, 3] [GeneInt 3, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 4, StateFunc instructionIntYankDup] emptyState + intTestFunc "instructionIntShove" [2, 1, 3, 1] [GeneInt 1, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 2, StateFunc instructionIntShove] emptyState + intTestFunc "instructionIntShoveDup" [3, 2, 1, 3, 1] [GeneInt 1, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 2, StateFunc instructionIntShoveDup] emptyState -- Exec tests intTestFunc "instructionExecIf" [6, 5] [GeneBool True, StateFunc instructionExecIf, Block [GeneInt 5, GeneInt 6], Block [GeneInt 7, GeneInt 8]] emptyState @@ -61,15 +61,15 @@ main = do assert (emptyState == interpretExec loadedState) putStrLn "instructionExecWhenFalse passed test." -- Float tests - floatTestFunc "instructionFloatAdd" [4.32] [GeneFloat 4.01, GeneFloat 0.31, StateFunc instructionFloatAdd] emptyState + floatTestFunc "instructionFloatAdd" [4.32] [GeneFloat 4.01, GeneFloat 0.31, StateFunc instructionFloatAdd] emptyState floatTestFunc "instructionFloatSub" [3.6900003] [GeneFloat 4.01, GeneFloat 0.32, StateFunc instructionFloatSub] emptyState floatTestFunc "instructionFloatMul" [1.38] [GeneFloat 0.12, GeneFloat 11.5, StateFunc instructionFloatMul] emptyState floatTestFunc "instructionFloatDiv" [57.5] [GeneFloat 11.5, GeneFloat 0.2, StateFunc instructionFloatDiv] emptyState floatTestFunc "instructionFloatDiv0" [0, 69.69] [GeneFloat 69.69, GeneFloat 0.0, StateFunc instructionFloatDiv] emptyState floatTestFunc "instructionFloatYank" [1.1, 4.4, 3.3, 2.2] [GeneInt 3, GeneFloat 1.1, GeneFloat 2.2, GeneFloat 3.3, GeneFloat 4.4, StateFunc instructionFloatYank] emptyState floatTestFunc "instructionFloatYankDup" [1.1, 4.4, 3.3, 2.2, 1.1] [GeneInt 3, GeneFloat 1.1, GeneFloat 2.2, GeneFloat 3.3, GeneFloat 4.4, StateFunc instructionFloatYankDup] emptyState - floatTestFunc "instructionFloatShove" [3.3,2.2,4.4,1.1] [GeneInt 3, GeneFloat 1.1, GeneFloat 2.2, GeneFloat 3.3, GeneFloat 4.4, StateFunc instructionFloatShove] emptyState - floatTestFunc "instructionFloatShoveDup" [4.4,3.3,2.2,4.4,1.1] [GeneInt 3, GeneFloat 1.1, GeneFloat 2.2, GeneFloat 3.3, GeneFloat 4.4, StateFunc instructionFloatShoveDup] emptyState + floatTestFunc "instructionFloatShove" [3.3, 2.2, 4.4, 1.1] [GeneInt 3, GeneFloat 1.1, GeneFloat 2.2, GeneFloat 3.3, GeneFloat 4.4, StateFunc instructionFloatShove] emptyState + floatTestFunc "instructionFloatShoveDup" [4.4, 3.3, 2.2, 4.4, 1.1] [GeneInt 3, GeneFloat 1.1, GeneFloat 2.2, GeneFloat 3.3, GeneFloat 4.4, StateFunc instructionFloatShoveDup] emptyState -- Bool tests boolTestFunc "instructionIntEqTrue" [True] [GeneInt 3, GeneInt 3, StateFunc instructionIntEq] emptyState From 1e02b2c6ac8c79c66c0bdea08b637aeac9fd6583 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Mon, 20 Jan 2025 23:25:09 -0600 Subject: [PATCH 035/171] pysh readme mention --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index ace7b9c..a9e667b 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,8 @@ https://erp12.github.io/pyshgp/html/core_instructions.html I'm not going to include no-ops. +Interpush doesn't have the code stack. Time to test this against pysh. + ## Tasks * [ ] Post minimal core of exec to haskell discourse for advice about speed optimization. * [x] Do test-driven development on this one. From a61c9e7133f7557d593f8f1aa1ece2f949a35e2d Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Tue, 21 Jan 2025 23:54:25 -0600 Subject: [PATCH 036/171] fix Block == --- src/State.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/State.hs b/src/State.hs index 539a29d..1708221 100644 --- a/src/State.hs +++ b/src/State.hs @@ -31,7 +31,7 @@ instance Eq Gene where GeneBoolVector xs == GeneBoolVector ys = xs == ys Close == Close = True StateFunc _ == StateFunc _ = True -- This line is probably not the best thing to do - Block [x] == Block [y] = [x] == [y] + Block x == Block y = x == y _ == _ = False instance Show Gene where From 85d4fd33b00c9058b46047a4b41118bdb82258be Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Wed, 22 Jan 2025 00:31:42 -0600 Subject: [PATCH 037/171] empty Block for last block instruction --- src/Instructions/CodeInstructions.hs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Instructions/CodeInstructions.hs b/src/Instructions/CodeInstructions.hs index 6a5721a..b29381c 100644 --- a/src/Instructions/CodeInstructions.hs +++ b/src/Instructions/CodeInstructions.hs @@ -20,6 +20,7 @@ extractFirstFromBlock (Block (x : _)) = x extractFirstFromBlock gene = gene extractLastFromBlock :: Gene -> Gene +extractLastFromBlock (Block []) = Block [] extractLastFromBlock (Block xs) = last xs extractLastFromBlock gene = gene From aace8821404ad99c059800b6c7451f221208bb7d Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Wed, 22 Jan 2025 00:32:08 -0600 Subject: [PATCH 038/171] more code tests --- test/Main.hs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/test/Main.hs b/test/Main.hs index 70880a7..c8f426c 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -2,6 +2,7 @@ import Control.Exception (assert) import Instructions.ExecInstructions import Instructions.FloatInstructions import Instructions.IntInstructions +import Instructions.CodeInstructions import Push import State @@ -24,6 +25,11 @@ boolTestFunc name goal genome startState = let state = loadProgram genome startState in assert (goal == _bool (interpretExec state)) putStrLn (name ++ " passed test.") +codeTestFunc :: String -> [Gene] -> [Gene] -> State -> IO () +codeTestFunc name goal genome startState = + let state = loadProgram genome startState + in assert (goal == _code (interpretExec state)) putStrLn (name <> " passed test.") + main :: IO () main = do -- Int tests @@ -75,3 +81,25 @@ main = do boolTestFunc "instructionIntEqTrue" [True] [GeneInt 3, GeneInt 3, StateFunc instructionIntEq] emptyState boolTestFunc "instructionIntEqFalse" [False] [GeneInt 3, GeneInt 5, StateFunc instructionIntEq] emptyState boolTestFunc "instructionIntEqFail" [] [GeneInt 3, StateFunc instructionIntEq] emptyState + + -- Code tests + codeTestFunc "instructionCodeFromExec" [] [StateFunc instructionCodeFromExec, StateFunc instructionFloatFromInt, StateFunc instructionCodePop] emptyState + intTestFunc "instructionCodeDoRange" [18] [GeneInt 3, GeneInt 6, StateFunc instructionCodeFromExec, StateFunc instructionIntAdd, StateFunc instructionCodeDoRange] emptyState + -- How to test instructionCodeDoThenPop????? + codeTestFunc "instructionCodeFirst" [GeneInt 5] [StateFunc instructionCodeFromExec, Block [GeneInt 5, StateFunc instructionIntSub], StateFunc instructionCodeFirst] emptyState + codeTestFunc "instructionCodeLast" [GeneBool True] [StateFunc instructionCodeFromExec, Block [GeneInt 5, StateFunc instructionIntSub, GeneBool True], StateFunc instructionCodeLast] emptyState + codeTestFunc "instructionCodeTail" [Block [GeneFloat 3.2, GeneBool True, GeneInt 3]] [StateFunc instructionCodeFromExec, Block [StateFunc instructionFloatAdd, GeneFloat 3.2, GeneBool True, GeneInt 3], StateFunc instructionCodeTail] emptyState + codeTestFunc "instructionCodeInit" [Block [GeneIntVector [1], GeneFloat 3.2, GeneBool True]] [StateFunc instructionCodeFromExec, Block [GeneIntVector [1], GeneFloat 3.2, GeneBool True, GeneInt 3], StateFunc instructionCodeInit] emptyState + codeTestFunc "instructionCodeWrap" [Block [GeneInt 3]] [StateFunc instructionCodeFromExec, GeneInt 3, StateFunc instructionCodeWrap] emptyState + codeTestFunc "instructionCodeList" [Block [GeneFloat 5.43, GeneInt 3]] [StateFunc instructionCodeFromExec, GeneInt 3, StateFunc instructionCodeFromExec, GeneFloat 5.43, StateFunc instructionCodeList] emptyState + codeTestFunc "instructionCodeCombine2Blocks" [Block [GeneInt 3, GeneInt 4, GeneInt 1, GeneInt 2]] [StateFunc instructionCodeFromExec, Block [GeneInt 1, GeneInt 2], StateFunc instructionCodeFromExec, Block [GeneInt 3, GeneInt 4], StateFunc instructionCodeCombine] emptyState + codeTestFunc "instructionCodeCombine1Block1Single" [Block [GeneInt 3, GeneInt 4, GeneInt 1]] [StateFunc instructionCodeFromExec, GeneInt 1, StateFunc instructionCodeFromExec, Block [GeneInt 3, GeneInt 4], StateFunc instructionCodeCombine] emptyState + codeTestFunc "instructionCodeCombine1Single1Block" [Block [GeneInt 3, GeneInt 1, GeneInt 2]] [StateFunc instructionCodeFromExec, Block [GeneInt 1, GeneInt 2], StateFunc instructionCodeFromExec, GeneInt 3, StateFunc instructionCodeCombine] emptyState + codeTestFunc "instrucitonCodeCombine2Single" [Block [GeneInt 2, GeneInt 1]] [StateFunc instructionCodeFromExec, GeneInt 1, StateFunc instructionCodeFromExec, GeneInt 2, StateFunc instructionCodeCombine] emptyState + intTestFunc "instructionCodeDo" [3] [StateFunc instructionCodeFromExec, GeneInt 3, StateFunc instructionCodeDo] emptyState + -- How to test instructionCodeDoDup??? We would would need a multi stack testing function + boolTestFunc "instructionCodeIsCodeBlockTrue" [True] [StateFunc instructionCodeFromExec, Block [GeneInt 0], StateFunc instructionCodeIsCodeBlock] emptyState + boolTestFunc "instructionCodeIsCodeBlockFalse" [False] [StateFunc instructionCodeFromExec, GeneInt 0, StateFunc instructionCodeIsCodeBlock] emptyState + boolTestFunc "instructionCodeIsSingularTrue" [True] [StateFunc instructionCodeFromExec, GeneInt 0, StateFunc instructionCodeIsSingular] emptyState + boolTestFunc "instructionCodeIsSingularFalse" [False] [StateFunc instructionCodeFromExec, Block [GeneInt 0], StateFunc instructionCodeIsSingular] emptyState + From 59c40c6f0a3f15459d108b1291ec7c979066db02 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Wed, 22 Jan 2025 01:43:46 -0600 Subject: [PATCH 039/171] more instructions --- src/Instructions/CodeInstructions.hs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/Instructions/CodeInstructions.hs b/src/Instructions/CodeInstructions.hs index b29381c..b01f257 100644 --- a/src/Instructions/CodeInstructions.hs +++ b/src/Instructions/CodeInstructions.hs @@ -2,6 +2,7 @@ module Instructions.CodeInstructions where import State import Instructions.GenericInstructions +import Instructions.IntInstructions isBlock :: Gene -> Bool isBlock (Block _) = True @@ -113,3 +114,25 @@ instructionCodeDoRange state@(State {_code = (c1 : cs), _int = (i0 : i1 : is), _ | currentIdx > destIdx = -1 | otherwise = 0 instructionCodeDoRange state = state + +instructionCodeDoCount :: State -> State +instructionCodeDoCount state@(State {_code = (c : cs), _int = (i : is), _exec = es}) = + if i < 1 + then state + else state {_code = cs, _int = is, _exec = Block [GeneInt 0, GeneInt $ i - 1, StateFunc instructionCodeFromExec, c, StateFunc instructionCodeDoRange] : es} +instructionCodeDoCount state = state + +instructionCodeDoTimes :: State -> State +instructionCodeDoTimes state@(State {_code = (c : cs), _int = (i : is), _exec = es}) = + if i < 1 + then state + else state {_code = cs, _int = is, _exec = Block [GeneInt 0, GeneInt $ i - 1, StateFunc instructionCodeFromExec, Block [StateFunc instructionIntPop, c], StateFunc instructionCodeDoRange] : es} +instructionCodeDoTimes state = state + +instructionCodeIf :: State -> State +instructionCodeIf state@(State {_code = (c1 : c2 : cs), _bool = (b1 : bs), _exec = es}) = state{_code = cs, _bool = bs, _exec = (if b1 then c1 else c2) : es} +instructionCodeIf state = state + +instructionCodeWhen :: State -> State +instructionCodeWhen state@(State {_code = (c1 : cs), _bool = (b1 : bs), _exec = es}) = state{_code = cs, _bool = bs, _exec = if b1 then c1 : es else es} +instructionCodeWhen state = state From f59775273a4c023c9aefb22518c0e186d55c819d Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Wed, 22 Jan 2025 01:43:59 -0600 Subject: [PATCH 040/171] more code tests --- test/Main.hs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test/Main.hs b/test/Main.hs index c8f426c..425ae96 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -102,4 +102,9 @@ main = do boolTestFunc "instructionCodeIsCodeBlockFalse" [False] [StateFunc instructionCodeFromExec, GeneInt 0, StateFunc instructionCodeIsCodeBlock] emptyState boolTestFunc "instructionCodeIsSingularTrue" [True] [StateFunc instructionCodeFromExec, GeneInt 0, StateFunc instructionCodeIsSingular] emptyState boolTestFunc "instructionCodeIsSingularFalse" [False] [StateFunc instructionCodeFromExec, Block [GeneInt 0], StateFunc instructionCodeIsSingular] emptyState - + intTestFunc "instructionCodeDoCount" [15] [GeneInt 6, StateFunc instructionCodeFromExec, StateFunc instructionIntAdd, StateFunc instructionCodeDoCount] emptyState + intTestFunc "instructionCodeDoTimes" [13] [GeneInt 6, GeneInt 3, GeneInt 4, GeneInt 2, StateFunc instructionCodeFromExec, StateFunc instructionIntAdd, StateFunc instructionCodeDoTimes] emptyState + intTestFunc "instructionCodeIfTrue" [6] [GeneBool True, StateFunc instructionCodeFromExec, GeneInt 3, StateFunc instructionCodeFromExec, GeneInt 6, StateFunc instructionCodeIf] emptyState + intTestFunc "instructionCodeIfFalse" [3] [GeneBool False, StateFunc instructionCodeFromExec, GeneInt 3, StateFunc instructionCodeFromExec, GeneInt 6, StateFunc instructionCodeIf] emptyState + intTestFunc "instructionCodeWhen" [6, 3, 6] [GeneInt 6, GeneInt 3, GeneInt 4, GeneInt 2, GeneBool True, StateFunc instructionCodeFromExec, StateFunc instructionIntAdd, StateFunc instructionCodeWhen] emptyState + -- stopped for the night at https://erp12.github.io/pyshgp/html/core_instructions.html#code-member From 2ba43396d7536b430562e1e182b8da512fac657d Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Wed, 22 Jan 2025 13:23:42 -0600 Subject: [PATCH 041/171] more instructions, need to test instructionCodeN --- src/Instructions/CodeInstructions.hs | 24 ++++++++++++++++++++++++ test/Main.hs | 5 ++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/Instructions/CodeInstructions.hs b/src/Instructions/CodeInstructions.hs index b01f257..363673c 100644 --- a/src/Instructions/CodeInstructions.hs +++ b/src/Instructions/CodeInstructions.hs @@ -12,6 +12,10 @@ blockLength :: Gene -> Int blockLength (Block xs) = length xs blockLength _ = 1 +blockIsNull :: Gene -> Bool +blockIsNull (Block xs) = null xs +blockIsNull _ = False + -- I think I can abstract the boilerplate base case check for a lot of these -- with a different function @@ -40,6 +44,11 @@ codeCombine (Block xs) ygene = Block (xs <> [ygene]) codeCombine xgene (Block ys) = Block (xgene : ys) codeCombine xgene ygene = Block [xgene, ygene] +codeMember :: Gene -> Gene -> Bool +codeMember (Block _) (Block _) = False -- Can't compare two lists with `elem` +codeMember (Block xs) ygene = ygene `elem` xs +codeMember _ _ = False + instructionCodePop :: State -> State instructionCodePop state = instructionPop state code @@ -136,3 +145,18 @@ instructionCodeIf state = state instructionCodeWhen :: State -> State instructionCodeWhen state@(State {_code = (c1 : cs), _bool = (b1 : bs), _exec = es}) = state{_code = cs, _bool = bs, _exec = if b1 then c1 : es else es} instructionCodeWhen state = state + +instructionCodeMember :: State -> State +instructionCodeMember state@(State {_code = (c1 : c2 : cs), _bool = bs}) = state{_code = cs, _bool = codeMember c1 c2 : bs} +instructionCodeMember state = state + +-- https://erp12.github.io/pyshgp/html/core_instructions.html#code-nth +instructionCodeN :: State -> State +instructionCodeN state@(State {_code = (c1 : cs), _int = (_ : is)}) = + if not $ blockIsNull c1Block + then state {_code = c1Block : cs, _int = is} + else state + where + c1Block :: Gene + c1Block = if not $ isBlock c1 then Block [c1] else c1 +instructionCodeN state = state diff --git a/test/Main.hs b/test/Main.hs index 425ae96..79026f3 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -107,4 +107,7 @@ main = do intTestFunc "instructionCodeIfTrue" [6] [GeneBool True, StateFunc instructionCodeFromExec, GeneInt 3, StateFunc instructionCodeFromExec, GeneInt 6, StateFunc instructionCodeIf] emptyState intTestFunc "instructionCodeIfFalse" [3] [GeneBool False, StateFunc instructionCodeFromExec, GeneInt 3, StateFunc instructionCodeFromExec, GeneInt 6, StateFunc instructionCodeIf] emptyState intTestFunc "instructionCodeWhen" [6, 3, 6] [GeneInt 6, GeneInt 3, GeneInt 4, GeneInt 2, GeneBool True, StateFunc instructionCodeFromExec, StateFunc instructionIntAdd, StateFunc instructionCodeWhen] emptyState - -- stopped for the night at https://erp12.github.io/pyshgp/html/core_instructions.html#code-member + boolTestFunc "instructionCodeMemberTrue" [True] [StateFunc instructionCodeFromExec, GeneInt 2, StateFunc instructionCodeFromExec, Block [GeneFloat 3.6, GeneInt 2, GeneIntVector [8, 9]], StateFunc instructionCodeMember] emptyState + boolTestFunc "instructionCodeMemberFalse" [False] [StateFunc instructionCodeFromExec, GeneInt 7, StateFunc instructionCodeFromExec, Block [GeneFloat 3.6, GeneInt 2, GeneIntVector [8, 9]], StateFunc instructionCodeMember] emptyState + boolTestFunc "instructionCodeMember2Blocks" [False] [StateFunc instructionCodeFromExec, Block [GeneInt 7, GeneInt 0], StateFunc instructionCodeFromExec, Block [GeneFloat 3.6, GeneInt 2, GeneIntVector [8, 9]], StateFunc instructionCodeMember] emptyState + From 58fcf7e46f3040228493ce9b8fc7038fb2a9fe32 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Thu, 23 Jan 2025 01:42:57 -0600 Subject: [PATCH 042/171] instructionExtractCode LOVE IT :))))))))))))) --- src/Instructions/CodeInstructions.hs | 53 +++++++++++++++++++++++++--- test/Main.hs | 19 +++++++--- 2 files changed, 62 insertions(+), 10 deletions(-) diff --git a/src/Instructions/CodeInstructions.hs b/src/Instructions/CodeInstructions.hs index 363673c..39395dc 100644 --- a/src/Instructions/CodeInstructions.hs +++ b/src/Instructions/CodeInstructions.hs @@ -4,6 +4,8 @@ import State import Instructions.GenericInstructions import Instructions.IntInstructions +-- import Debug.Trace + isBlock :: Gene -> Bool isBlock (Block _) = True isBlock _ = False @@ -38,6 +40,15 @@ extractTailFromBlock :: Gene -> Gene extractTailFromBlock (Block xs) = Block (drop 1 xs) extractTailFromBlock gene = gene +-- This function took at least 3 hours to program. +codeAtPoint :: [Gene] -> Int -> Gene +codeAtPoint (gene : _) 0 = gene +codeAtPoint [] _ = Block [] -- Should only happen if an empty block is last Gene in the list of Genes +codeAtPoint ((Block nestedGenes) : genes) index = codeAtPoint (nestedGenes <> genes) (index - 1) +codeAtPoint (_ : genes) index = codeAtPoint genes (index - 1) + +-- This one functions differently than pysh. +-- I like this one because it preserves ordering codeCombine :: Gene -> Gene -> Gene codeCombine (Block xs) (Block ys) = Block (xs <> ys) codeCombine (Block xs) ygene = Block (xs <> [ygene]) @@ -49,6 +60,11 @@ codeMember (Block _) (Block _) = False -- Can't compare two lists with `elem` codeMember (Block xs) ygene = ygene `elem` xs codeMember _ _ = False +-- I love list comprehensions +codeRecursiveSize :: Gene -> Int +codeRecursiveSize (Block xs) = sum [codeRecursiveSize x + if isBlock x then 1 else 0 | x <- xs] +codeRecursiveSize _ = 1 + instructionCodePop :: State -> State instructionCodePop state = instructionPop state code @@ -150,13 +166,40 @@ instructionCodeMember :: State -> State instructionCodeMember state@(State {_code = (c1 : c2 : cs), _bool = bs}) = state{_code = cs, _bool = codeMember c1 c2 : bs} instructionCodeMember state = state +-- This one doesn't count the recursive Blocks while instructionCodeExtract does -- https://erp12.github.io/pyshgp/html/core_instructions.html#code-nth instructionCodeN :: State -> State -instructionCodeN state@(State {_code = (c1 : cs), _int = (_ : is)}) = - if not $ blockIsNull c1Block - then state {_code = c1Block : cs, _int = is} +instructionCodeN state@(State {_code = ((Block c1) : cs), _int = (i1 : is)}) = + if not $ null c1 + then state {_code = c1 !! index : cs, _int = is} else state where - c1Block :: Gene - c1Block = if not $ isBlock c1 then Block [c1] else c1 + index :: Int + index = abs i1 `mod` length c1 +instructionCodeN state@(State {_code = (c1 : cs), _int = _ : is}) = state {_code = c1 : cs, _int = is} instructionCodeN state = state + +instructionMakeEmptyCodeBlock :: State -> State +instructionMakeEmptyCodeBlock state@(State {_code = cs}) = state {_code = Block [] : cs} + +instructionIsEmptyCodeBlock :: State -> State +instructionIsEmptyCodeBlock state@(State {_code = Block c1 : cs, _bool = bs}) = state{_code = cs, _bool = null c1 : bs} +instructionIsEmptyCodeBlock state@(State {_bool = bs}) = state{_bool = False : bs} + +instructionCodeSize :: State -> State +instructionCodeSize state@(State {_code = c1 : cs, _int = is}) = state{_code = cs, _int = codeRecursiveSize c1 : is} +instructionCodeSize state = state + +-- There's a bug for this instruction in pysh where the last item in the +-- top level Block isn't counted, and if passed 0, then the entire codeblock is returned. +-- I designed this function differently so 0 returns the 0th element, and the last item +-- in the codeblock can be returned. +instructionCodeExtract :: State -> State +instructionCodeExtract state@(State {_code = (block@(Block c1) : cs), _int = i1 : is}) = + let + index = abs i1 `mod` codeRecursiveSize block + in + state{_code = codeAtPoint c1 index : cs, _int = is} + +instructionCodeExtract state@(State {_code = cs, _int = _ : is}) = state{_code = cs, _int = is} +instructionCodeExtract state = state diff --git a/test/Main.hs b/test/Main.hs index 79026f3..20a8736 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -1,8 +1,8 @@ import Control.Exception (assert) +import Instructions.CodeInstructions import Instructions.ExecInstructions import Instructions.FloatInstructions import Instructions.IntInstructions -import Instructions.CodeInstructions import Push import State @@ -28,7 +28,7 @@ boolTestFunc name goal genome startState = codeTestFunc :: String -> [Gene] -> [Gene] -> State -> IO () codeTestFunc name goal genome startState = let state = loadProgram genome startState - in assert (goal == _code (interpretExec state)) putStrLn (name <> " passed test.") + in assert (goal == _code (interpretExec state)) putStrLn (name <> " passed test.") main :: IO () main = do @@ -107,7 +107,16 @@ main = do intTestFunc "instructionCodeIfTrue" [6] [GeneBool True, StateFunc instructionCodeFromExec, GeneInt 3, StateFunc instructionCodeFromExec, GeneInt 6, StateFunc instructionCodeIf] emptyState intTestFunc "instructionCodeIfFalse" [3] [GeneBool False, StateFunc instructionCodeFromExec, GeneInt 3, StateFunc instructionCodeFromExec, GeneInt 6, StateFunc instructionCodeIf] emptyState intTestFunc "instructionCodeWhen" [6, 3, 6] [GeneInt 6, GeneInt 3, GeneInt 4, GeneInt 2, GeneBool True, StateFunc instructionCodeFromExec, StateFunc instructionIntAdd, StateFunc instructionCodeWhen] emptyState - boolTestFunc "instructionCodeMemberTrue" [True] [StateFunc instructionCodeFromExec, GeneInt 2, StateFunc instructionCodeFromExec, Block [GeneFloat 3.6, GeneInt 2, GeneIntVector [8, 9]], StateFunc instructionCodeMember] emptyState - boolTestFunc "instructionCodeMemberFalse" [False] [StateFunc instructionCodeFromExec, GeneInt 7, StateFunc instructionCodeFromExec, Block [GeneFloat 3.6, GeneInt 2, GeneIntVector [8, 9]], StateFunc instructionCodeMember] emptyState + boolTestFunc "instructionCodeMemberTrue" [True] [StateFunc instructionCodeFromExec, GeneInt 2, StateFunc instructionCodeFromExec, Block [GeneFloat 3.6, GeneInt 2, GeneIntVector [8, 9]], StateFunc instructionCodeMember] emptyState + boolTestFunc "instructionCodeMemberFalse" [False] [StateFunc instructionCodeFromExec, GeneInt 7, StateFunc instructionCodeFromExec, Block [GeneFloat 3.6, GeneInt 2, GeneIntVector [8, 9]], StateFunc instructionCodeMember] emptyState boolTestFunc "instructionCodeMember2Blocks" [False] [StateFunc instructionCodeFromExec, Block [GeneInt 7, GeneInt 0], StateFunc instructionCodeFromExec, Block [GeneFloat 3.6, GeneInt 2, GeneIntVector [8, 9]], StateFunc instructionCodeMember] emptyState - + codeTestFunc "instructionCodeNInBounds" [GeneInt 0] [StateFunc instructionCodeFromExec, Block [GeneInt 0, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 4, GeneInt 5], GeneInt 0, StateFunc instructionCodeN] emptyState + codeTestFunc "instructionCodeNInBoundsEnd" [GeneInt 5] [StateFunc instructionCodeFromExec, Block [GeneInt 0, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 4, GeneInt 5], GeneInt 5, StateFunc instructionCodeN] emptyState + codeTestFunc "instructionCodeNModded" [GeneInt 3] [StateFunc instructionCodeFromExec, Block [GeneInt 0, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 4, GeneInt 5], GeneInt 9, StateFunc instructionCodeN] emptyState + codeTestFunc "instructionMakeEmptyCodeBlock" [Block []] [StateFunc instructionMakeEmptyCodeBlock] emptyState + boolTestFunc "instructionIsEmptyCodeBlockTrue" [True] [StateFunc instructionCodeFromExec, Block [], StateFunc instructionIsEmptyCodeBlock] emptyState + intTestFunc "instructionCodeSize" [8] [StateFunc instructionCodeFromExec, Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6], StateFunc instructionCodeSize] emptyState + codeTestFunc "instructionCodeExtractInBounds" [GeneInt 3] [StateFunc instructionCodeFromExec, Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6], GeneInt 3, StateFunc instructionCodeExtract] emptyState + codeTestFunc "instructionCodeExtractOutBounds" [GeneInt 3] [StateFunc instructionCodeFromExec, Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6], GeneInt 11, StateFunc instructionCodeExtract] emptyState + codeTestFunc "instructionCodeExtractLastEmptyBlock" [Block []] [StateFunc instructionCodeFromExec, Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], Block []], GeneInt 7, StateFunc instructionCodeExtract] emptyState + codeTestFunc "instructionCodeExtractBlock" [Block [GeneInt 2, GeneInt 3]] [StateFunc instructionCodeFromExec, Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], Block []], GeneInt 1, StateFunc instructionCodeExtract] emptyState From f8233133ea2bf9113d41da7270091ddcf33a7b02 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Thu, 23 Jan 2025 02:05:25 -0600 Subject: [PATCH 043/171] one more test to be safe :))))) --- test/Main.hs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/Main.hs b/test/Main.hs index 20a8736..e549d35 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -120,3 +120,5 @@ main = do codeTestFunc "instructionCodeExtractOutBounds" [GeneInt 3] [StateFunc instructionCodeFromExec, Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6], GeneInt 11, StateFunc instructionCodeExtract] emptyState codeTestFunc "instructionCodeExtractLastEmptyBlock" [Block []] [StateFunc instructionCodeFromExec, Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], Block []], GeneInt 7, StateFunc instructionCodeExtract] emptyState codeTestFunc "instructionCodeExtractBlock" [Block [GeneInt 2, GeneInt 3]] [StateFunc instructionCodeFromExec, Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], Block []], GeneInt 1, StateFunc instructionCodeExtract] emptyState + codeTestFunc "instructionCodeExtractEdgeCase" [Block []] [StateFunc instructionCodeFromExec, Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], Block []], GeneInt 7, StateFunc instructionCodeExtract] emptyState + From 66bb2921e81369df68663a4617aa8de18b42e5b1 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Thu, 23 Jan 2025 17:10:54 -0600 Subject: [PATCH 044/171] insert function, needs testing --- src/Instructions/CodeInstructions.hs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/Instructions/CodeInstructions.hs b/src/Instructions/CodeInstructions.hs index 39395dc..e75f25a 100644 --- a/src/Instructions/CodeInstructions.hs +++ b/src/Instructions/CodeInstructions.hs @@ -47,8 +47,14 @@ codeAtPoint [] _ = Block [] -- Should only happen if an empty block is last Gene codeAtPoint ((Block nestedGenes) : genes) index = codeAtPoint (nestedGenes <> genes) (index - 1) codeAtPoint (_ : genes) index = codeAtPoint genes (index - 1) +codeInsertAtPoint :: [Gene] -> Gene -> Int -> [Gene] +codeInsertAtPoint oldGenes gene 0 = gene : oldGenes +codeInsertAtPoint [] gene _ = [gene] -- This shouldn't happen (lol) +codeInsertAtPoint ((Block genes) : oldGenes) gene index = Block (codeInsertAtPoint genes gene (index - 1)) : oldGenes +codeInsertAtPoint (oldGene : oldGenes) gene index = oldGene : codeInsertAtPoint oldGenes gene (index - 1) + -- This one functions differently than pysh. --- I like this one because it preserves ordering +-- I like this one because it preserves ordering in the second case codeCombine :: Gene -> Gene -> Gene codeCombine (Block xs) (Block ys) = Block (xs <> ys) codeCombine (Block xs) ygene = Block (xs <> [ygene]) @@ -200,6 +206,18 @@ instructionCodeExtract state@(State {_code = (block@(Block c1) : cs), _int = i1 index = abs i1 `mod` codeRecursiveSize block in state{_code = codeAtPoint c1 index : cs, _int = is} - instructionCodeExtract state@(State {_code = cs, _int = _ : is}) = state{_code = cs, _int = is} instructionCodeExtract state = state + +instructionCodeInsert :: State -> State +instructionCodeInsert state@(State {_code = (block@(Block c1) : c2 : cs), _int = i1 : is}) = + let + index = abs i1 `mod` codeRecursiveSize block + in + state{_code = Block (codeInsertAtPoint c1 c2 index) : cs, _int = is} +instructionCodeInsert state@(State {_code = c1 : c2 : cs, _int = i1 : is}) = + let + index = abs i1 `mod` codeRecursiveSize (Block [c1]) + in + state{_code = Block (codeInsertAtPoint [c1] c2 index) : cs, _int = is} +instructionCodeInsert state = state From bf84430957769ae397a8931897a0af6d3c04c18d Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Thu, 23 Jan 2025 17:11:12 -0600 Subject: [PATCH 045/171] todo comments --- test/Main.hs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/Main.hs b/test/Main.hs index e549d35..60095ff 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -121,4 +121,6 @@ main = do codeTestFunc "instructionCodeExtractLastEmptyBlock" [Block []] [StateFunc instructionCodeFromExec, Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], Block []], GeneInt 7, StateFunc instructionCodeExtract] emptyState codeTestFunc "instructionCodeExtractBlock" [Block [GeneInt 2, GeneInt 3]] [StateFunc instructionCodeFromExec, Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], Block []], GeneInt 1, StateFunc instructionCodeExtract] emptyState codeTestFunc "instructionCodeExtractEdgeCase" [Block []] [StateFunc instructionCodeFromExec, Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], Block []], GeneInt 7, StateFunc instructionCodeExtract] emptyState + -- Need a function to test extracting a non-code-block item + -- Need functions to test inserting items. Block and NonBlock From b9e6b07b9842669b35efa14c785a169cb2b6ee93 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Thu, 23 Jan 2025 23:14:36 -0600 Subject: [PATCH 046/171] CodeFirst and CodeReverse --- src/Instructions/CodeInstructions.hs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/Instructions/CodeInstructions.hs b/src/Instructions/CodeInstructions.hs index e75f25a..a88626d 100644 --- a/src/Instructions/CodeInstructions.hs +++ b/src/Instructions/CodeInstructions.hs @@ -1,5 +1,6 @@ module Instructions.CodeInstructions where +import Data.List (elemIndex) import State import Instructions.GenericInstructions import Instructions.IntInstructions @@ -221,3 +222,25 @@ instructionCodeInsert state@(State {_code = c1 : c2 : cs, _int = i1 : is}) = in state{_code = Block (codeInsertAtPoint [c1] c2 index) : cs, _int = is} instructionCodeInsert state = state + +-- How do I test if two functions are the same?????????? +-- This will impact the final case. This implementation can't determine +-- if two functions are the same, so it assumes that they are. +-- Maybe can test for equality by seeing if these two functions affect the current state +-- in the same way. +instructionCodeFirstPosition :: State -> State +instructionCodeFirstPosition state@(State {_code = (Block []) : c2 : cs, _int = is}) = state {_code = cs, _int = (if c2 == Block [] then 0 else -1) : is} +instructionCodeFirstPosition state@(State {_code = (Block c1) : c2 : cs, _int = is}) = state {_code = cs, _int = positionElem c1 c2 : is} + where + -- This is really not gonna be good for StateFunc + positionElem :: [Gene] -> Gene -> Int + positionElem genes gene = + case elemIndex gene genes of + Nothing -> -1 + Just x -> x +instructionCodeFirstPosition state@(State {_code = c1 : c2 : cs, _int = is}) = state {_code = cs, _int = (if c1 == c2 then 0 else -1) : is} +instructionCodeFirstPosition state = state + +instructionCodeReverse :: State -> State +instructionCodeReverse state@(State {_code = (Block c1) : cs}) = state {_code = Block (reverse c1) : cs} +instructionCodeReverse state = state From 44257bd94e1be63bd3af89e57a297a471eac38c2 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Thu, 23 Jan 2025 23:15:01 -0600 Subject: [PATCH 047/171] insert/extract/position/reverse tests --- test/Main.hs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/test/Main.hs b/test/Main.hs index 60095ff..ec17413 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -121,6 +121,20 @@ main = do codeTestFunc "instructionCodeExtractLastEmptyBlock" [Block []] [StateFunc instructionCodeFromExec, Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], Block []], GeneInt 7, StateFunc instructionCodeExtract] emptyState codeTestFunc "instructionCodeExtractBlock" [Block [GeneInt 2, GeneInt 3]] [StateFunc instructionCodeFromExec, Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], Block []], GeneInt 1, StateFunc instructionCodeExtract] emptyState codeTestFunc "instructionCodeExtractEdgeCase" [Block []] [StateFunc instructionCodeFromExec, Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], Block []], GeneInt 7, StateFunc instructionCodeExtract] emptyState - -- Need a function to test extracting a non-code-block item - -- Need functions to test inserting items. Block and NonBlock - + codeTestFunc "instructionCodeExtractNotBlock" [GeneInt 2] [StateFunc instructionCodeFromExec, GeneInt 2, GeneInt 56, StateFunc instructionCodeExtract] emptyState + codeTestFunc "instructionCodeInsertInBounds" + [Block [GeneInt 1, Block [GeneInt 2, GeneInt 9999, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6, Block [GeneInt 7, GeneInt 8], GeneInt 9]] + [StateFunc instructionCodeFromExec, GeneInt 9999, StateFunc instructionCodeFromExec, Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6, Block [GeneInt 7, GeneInt 8], GeneInt 9], GeneInt 3, StateFunc instructionCodeInsert] + emptyState + codeTestFunc "instructionCodeInsertOutBounds" + [Block [GeneInt 1, Block [GeneInt 2, GeneInt 9999, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6, Block [GeneInt 7, GeneInt 8], GeneInt 9]] + [StateFunc instructionCodeFromExec, GeneInt 9999, StateFunc instructionCodeFromExec, Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6, Block [GeneInt 7, GeneInt 8], GeneInt 9], GeneInt 15, StateFunc instructionCodeInsert] + emptyState + codeTestFunc "instructionCodeInsertNotBlock" [Block [GeneInt 2, GeneInt 1]] [StateFunc instructionCodeFromExec, GeneInt 2, StateFunc instructionCodeFromExec, GeneInt 1, GeneInt 1, StateFunc instructionCodeInsert] emptyState + intTestFunc "instructionCodePosition0" [0] [StateFunc instructionCodeFromExec, GeneInt 1, StateFunc instructionCodeFromExec, Block [GeneInt 1, GeneInt 2, GeneInt 3], StateFunc instructionCodeFirstPosition] emptyState + intTestFunc "instructionCodePosition-1" [-1] [StateFunc instructionCodeFromExec, GeneInt 7, StateFunc instructionCodeFromExec, Block [GeneInt 1, GeneInt 2, GeneInt 3], StateFunc instructionCodeFirstPosition] emptyState + intTestFunc "instructionCodePositionEmptyBlock" [0] [StateFunc instructionCodeFromExec, Block [], StateFunc instructionCodeFromExec, Block [], StateFunc instructionCodeFirstPosition] emptyState + codeTestFunc "instructionCodePositionBadStack" [GeneInt 1] [StateFunc instructionCodeFromExec, GeneInt 1, StateFunc instructionCodeFirstPosition] emptyState -- tests to ensure base case of insufficient code stack works. Should do this on more of these instructions. + codeTestFunc "instructionCodeReverse2Args" [Block [GeneInt 2, GeneInt 1]] [StateFunc instructionCodeFromExec, Block [GeneInt 1, GeneInt 2], StateFunc instructionCodeReverse] emptyState + codeTestFunc "instructionCodeReverse3Args" [Block [GeneInt 3, GeneInt 2, GeneInt 1]] [StateFunc instructionCodeFromExec, Block [GeneInt 1, GeneInt 2, GeneInt 3], StateFunc instructionCodeReverse] emptyState + codeTestFunc "instructionCodeReverseNonBlock" [GeneInt 1] [StateFunc instructionCodeFromExec, GeneInt 1, StateFunc instructionCodeReverse] emptyState From 452e7a2b4999bedd0f5c4bc1ee005cda90797ae0 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Fri, 24 Jan 2025 01:38:14 -0600 Subject: [PATCH 048/171] implementing string and char stacks --- HushGP.cabal | 1 + README.md | 4 ---- src/Push.hs | 8 ++++++++ src/State.hs | 24 ++++++++++++++++++++++++ 4 files changed, 33 insertions(+), 4 deletions(-) diff --git a/HushGP.cabal b/HushGP.cabal index 42b8f7d..84cbeb3 100644 --- a/HushGP.cabal +++ b/HushGP.cabal @@ -44,6 +44,7 @@ library , Instructions.GenericInstructions , Instructions.LogicalInstructions , Instructions.CodeInstructions + , Instructions.StringInstructions -- Modules included in this library but not exported. -- other-modules: diff --git a/README.md b/README.md index a9e667b..d2a573d 100644 --- a/README.md +++ b/README.md @@ -6,10 +6,6 @@ the functions in the pyshgp list. https://erp12.github.io/pyshgp/html/core_instructions.html -I'm not going to include no-ops. - -Interpush doesn't have the code stack. Time to test this against pysh. - ## Tasks * [ ] Post minimal core of exec to haskell discourse for advice about speed optimization. * [x] Do test-driven development on this one. diff --git a/src/Push.hs b/src/Push.hs index 7a76068..70e8d9a 100644 --- a/src/Push.hs +++ b/src/Push.hs @@ -23,9 +23,13 @@ instructionParameterLoad state@(State {_parameter = (p : _)}) = case p of (GeneInt val) -> state & int .~ val : view int state (GeneFloat val) -> state & float .~ val : view float state (GeneBool val) -> state & bool .~ val : view bool state + (GeneString val) -> state & string .~ val : view string state + (GeneChar val) -> state & char .~ val : view char state (GeneIntVector val) -> state & intVector .~ val : view intVector state (GeneFloatVector val) -> state & floatVector .~ val : view floatVector state (GeneBoolVector val) -> state & boolVector .~ val : view boolVector state + (GeneStringVector val) -> state & stringVector .~ val : view stringVector state + (GeneCharVector val) -> state & charVector .~ val : view charVector state (StateFunc _) -> undefined (PlaceInput _) -> undefined Close -> undefined @@ -53,9 +57,13 @@ interpretExec state@(State {_exec = (e : es)}) = (GeneInt val) -> interpretExec (state & exec .~ es & int .~ val : view int state) (GeneFloat val) -> interpretExec (state & exec .~ es & float .~ val : view float state) (GeneBool val) -> interpretExec (state & exec .~ es & bool .~ val : view bool state) + (GeneString val) -> interpretExec (state & exec .~ es & string .~ val : view string state) + (GeneChar val) -> interpretExec (state & exec .~ es & char .~ val : view char state) (GeneIntVector val) -> interpretExec (state & exec .~ es & intVector .~ val : view intVector state) (GeneFloatVector val) -> interpretExec (state & exec .~ es & floatVector .~ val : view floatVector state) (GeneBoolVector val) -> interpretExec (state & exec .~ es & boolVector .~ val : view boolVector state) + (GeneStringVector val) -> interpretExec (state & exec .~ es & stringVector .~ val : view stringVector state) + (GeneCharVector val) -> interpretExec (state & exec .~ es & charVector .~ val : view charVector state) (StateFunc func) -> interpretExec $ func state {_exec = es} (Block block) -> interpretExec (state {_exec = block ++ es}) (PlaceInput val) -> interpretExec (state {_exec = (view input state Map.! val) : es}) diff --git a/src/State.hs b/src/State.hs index 1708221..eb61775 100644 --- a/src/State.hs +++ b/src/State.hs @@ -13,9 +13,13 @@ data Gene = GeneInt Int | GeneFloat Float | GeneBool Bool + | GeneString String + | GeneChar Char | GeneIntVector [Int] | GeneFloatVector [Float] | GeneBoolVector [Bool] + | GeneStringVector [String] + | GeneCharVector [Char] | StateFunc (State -> State) | PlaceInput String | Close @@ -25,10 +29,14 @@ instance Eq Gene where GeneInt x == GeneInt y = x == y GeneFloat x == GeneFloat y = x == y GeneBool x == GeneBool y = x == y + GeneString x == GeneString y = x == y + GeneChar x == GeneChar y = x == y PlaceInput x == PlaceInput y = x == y GeneIntVector xs == GeneIntVector ys = xs == ys GeneFloatVector xs == GeneFloatVector ys = xs == ys GeneBoolVector xs == GeneBoolVector ys = xs == ys + GeneStringVector xs == GeneStringVector ys = xs == ys + GeneCharVector xs == GeneCharVector ys = xs == ys Close == Close = True StateFunc _ == StateFunc _ = True -- This line is probably not the best thing to do Block x == Block y = x == y @@ -38,11 +46,15 @@ instance Show Gene where show (GeneInt x) = "Int: " <> show x show (GeneFloat x) = "Float: " <> show x show (GeneBool x) = "Bool: " <> show x + show (GeneString x) = "String: " <> x + show (GeneChar x) = "Char: " <> show x show (StateFunc _) = "Func: unnamed" show (PlaceInput x) = "In: " <> x show (GeneIntVector xs) = "Int Vec: " <> show xs show (GeneFloatVector xs) = "Float Vec: " <> show xs show (GeneBoolVector xs) = "Bool Vec: " <> show xs + show (GeneStringVector xs) = "String Vec: " <> show xs + show (GeneCharVector xs) = "Char Vec: " <> show xs show Close = "Close" show (Block xs) = "Block: " <> show xs @@ -52,9 +64,13 @@ data State = State _int :: [Int], _float :: [Float], _bool :: [Bool], + _string :: [String], + _char :: [Char], _intVector :: [[Int]], _floatVector :: [[Float]], _boolVector :: [[Bool]], + _stringVector :: [[String]], + _charVector :: [[Char]], _parameter :: [Gene], _input :: Map.Map String Gene } @@ -70,10 +86,14 @@ emptyState = _int = [], _float = [], _bool = [], + _string = [], + _char = [], _parameter = [], _intVector = [], _floatVector = [], _boolVector = [], + _stringVector = [], + _charVector = [], _input = Map.empty } @@ -85,9 +105,13 @@ exampleState = _int = [32, 56], _float = [3.23, 9.235], _bool = [True, False], + _string = ["abc", "123"], + _char = ['d', 'e', 'f'], _parameter = [], _intVector = [[1, 2], [5, 6, 8]], _floatVector = [[1.234, 9.21], [5.42, 6.221, 8.5493]], _boolVector = [[True, False], [False, False, True]], + _stringVector = [["this is a sentence", "this is also a sentence"], ["s0", "s1", "s2"]], + _charVector = [['z', 'x'], ['r', 'a', 't', 'l']], _input = Map.empty } From de8fedb5d1b3fea1838c8cd856c154a7471a1391 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Fri, 24 Jan 2025 01:38:54 -0600 Subject: [PATCH 049/171] more instructions --- src/Instructions/GenericInstructions.hs | 15 +++++++++++++++ src/Instructions/StringInstructions.hs | 17 +++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 src/Instructions/StringInstructions.hs diff --git a/src/Instructions/GenericInstructions.hs b/src/Instructions/GenericInstructions.hs index 835f510..308d261 100644 --- a/src/Instructions/GenericInstructions.hs +++ b/src/Instructions/GenericInstructions.hs @@ -108,3 +108,18 @@ instructionShoveDup state@(State {_int = []}) _ = state -- also also not int generic instructionShove :: State -> Lens' State [a] -> State instructionShove state accessor = instructionShoveDup state accessor & accessor .~ drop 1 (view accessor (instructionShoveDup state accessor)) + +-- not char generic +instructionConcat :: Semigroup a => State -> Lens' State [a] -> State +instructionConcat state accessor = + if (length . take 2 $ view accessor state) == 2 + then droppedState & accessor .~ (head (view accessor state) <> view accessor state !! 1) : view accessor droppedState + -- then undefined + else state + where + droppedState :: State + droppedState = state & accessor .~ drop 2 (view accessor state) + +-- evolve fodder??????????? +instructionNoOp :: State -> State +instructionNoOp state = state diff --git a/src/Instructions/StringInstructions.hs b/src/Instructions/StringInstructions.hs new file mode 100644 index 0000000..fd7e5e9 --- /dev/null +++ b/src/Instructions/StringInstructions.hs @@ -0,0 +1,17 @@ +module Instructions.StringInstructions where + +import State +import Instructions.GenericInstructions + +combineString :: String -> (String, String) -> String +combineString toInsert (front, back) = front <> toInsert <> back + +instructionStringConcat :: State -> State +instructionStringConcat state = instructionConcat state string + +instructionStringSwap :: State -> State +instructionStringSwap state = instructionSwap state string + +instructionStringInsertString :: State -> State +instructionStringInsertString state@(State{_string = s1 : s2 : ss, _int = i1 : is}) = state {_string = combineString s2 (splitAt i1 s1) : ss, _int = is} +instructionStringInsertString state = state From f1c6a24181918b1d6024f0aff4274ae88c578598 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Fri, 24 Jan 2025 01:39:15 -0600 Subject: [PATCH 050/171] I wonder how many tests there will be once these are all implemented --- test/Main.hs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/Main.hs b/test/Main.hs index ec17413..b115951 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -3,6 +3,7 @@ import Instructions.CodeInstructions import Instructions.ExecInstructions import Instructions.FloatInstructions import Instructions.IntInstructions +import Instructions.StringInstructions import Push import State @@ -30,6 +31,11 @@ codeTestFunc name goal genome startState = let state = loadProgram genome startState in assert (goal == _code (interpretExec state)) putStrLn (name <> " passed test.") +stringTestFunc :: String -> [String] -> [Gene] -> State -> IO () +stringTestFunc name goal genome startState = + let state = loadProgram genome startState + in assert (goal == _string (interpretExec state)) putStrLn (name <> " passed test.") + main :: IO () main = do -- Int tests @@ -138,3 +144,8 @@ main = do codeTestFunc "instructionCodeReverse2Args" [Block [GeneInt 2, GeneInt 1]] [StateFunc instructionCodeFromExec, Block [GeneInt 1, GeneInt 2], StateFunc instructionCodeReverse] emptyState codeTestFunc "instructionCodeReverse3Args" [Block [GeneInt 3, GeneInt 2, GeneInt 1]] [StateFunc instructionCodeFromExec, Block [GeneInt 1, GeneInt 2, GeneInt 3], StateFunc instructionCodeReverse] emptyState codeTestFunc "instructionCodeReverseNonBlock" [GeneInt 1] [StateFunc instructionCodeFromExec, GeneInt 1, StateFunc instructionCodeReverse] emptyState + + -- String tests + stringTestFunc "instructionStringConcat" ["123abc"] [GeneString "abc", GeneString "123", StateFunc instructionStringConcat] emptyState + stringTestFunc "instructionStringSwap" ["abc", "123"] [GeneString "abc", GeneString "123", StateFunc instructionStringSwap] emptyState + stringTestFunc "instructionStringInsertString" ["123INSabc"] [GeneString "abc", GeneString "123", StateFunc instructionStringConcat, GeneString "INS", StateFunc instructionStringSwap, GeneInt 3, StateFunc instructionStringInsertString] emptyState From 8666a37fc97d5de4f54123fc132acc7b0531c7dc Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Fri, 24 Jan 2025 18:14:54 -0600 Subject: [PATCH 051/171] Data.List.Split --- HushGP.cabal | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/HushGP.cabal b/HushGP.cabal index 84cbeb3..ac88050 100644 --- a/HushGP.cabal +++ b/HushGP.cabal @@ -54,7 +54,7 @@ library -- Other library packages from which modules are imported. build-depends: - base, containers, lens + base, containers, lens, split -- Directories containing source files. hs-source-dirs: src From 13d288b245340df2698b427232f5715947aa8aee Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Fri, 24 Jan 2025 18:15:13 -0600 Subject: [PATCH 052/171] more string tests --- test/Main.hs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/Main.hs b/test/Main.hs index b115951..65d5d8a 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -149,3 +149,9 @@ main = do stringTestFunc "instructionStringConcat" ["123abc"] [GeneString "abc", GeneString "123", StateFunc instructionStringConcat] emptyState stringTestFunc "instructionStringSwap" ["abc", "123"] [GeneString "abc", GeneString "123", StateFunc instructionStringSwap] emptyState stringTestFunc "instructionStringInsertString" ["123INSabc"] [GeneString "abc", GeneString "123", StateFunc instructionStringConcat, GeneString "INS", StateFunc instructionStringSwap, GeneInt 3, StateFunc instructionStringInsertString] emptyState + stringTestFunc "instructionStringFromFirstChar" ["1"] [GeneString "123", StateFunc instructionStringFromFirstChar] emptyState + stringTestFunc "instructionStringFromNthChar" ["a"] [GeneString "123abc", GeneInt 3, StateFunc instructionStringFromNthChar] emptyState + intTestFunc "instructionStringIndexOfString3" [3] [GeneString "a", GeneString "123abc", StateFunc instructionStringIndexOfString] emptyState + intTestFunc "instructionStringIndexOfString3" [-1] [GeneString "z", GeneString "123abc", StateFunc instructionStringIndexOfString] emptyState + boolTestFunc "instructionStringContainsStringTrue" [True] [GeneString "a", GeneString "123abc", StateFunc instructionStringContainsString] emptyState + boolTestFunc "instructionStringContainsStringTrue" [False] [GeneString "z", GeneString "123abc", StateFunc instructionStringContainsString] emptyState From 4e21eee91c33166fb06a47f8b2ac5713c33e5171 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Fri, 24 Jan 2025 18:15:42 -0600 Subject: [PATCH 053/171] start on fillInHoles --- src/Instructions/GenericInstructions.hs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Instructions/GenericInstructions.hs b/src/Instructions/GenericInstructions.hs index 308d261..9cb28a7 100644 --- a/src/Instructions/GenericInstructions.hs +++ b/src/Instructions/GenericInstructions.hs @@ -3,7 +3,11 @@ module Instructions.GenericInstructions where import Control.Lens import State --- import Debug.Trace +-- import Debug.Trace + +-- Files in the spaces in [[a]] with [a] +fillInHoles :: [a] -> [[a]] -> [a] +fillInHoles filler toFill = undefined -- TODO notEmptyStack :: State -> Lens' State [a] -> Bool notEmptyStack state accessor = not . null $ view accessor state From 694565e95d365d1c39f7f40bd4a3a807d906c1f1 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Fri, 24 Jan 2025 18:16:20 -0600 Subject: [PATCH 054/171] more string instructions --- src/Instructions/StringInstructions.hs | 54 ++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/src/Instructions/StringInstructions.hs b/src/Instructions/StringInstructions.hs index fd7e5e9..2ce0b42 100644 --- a/src/Instructions/StringInstructions.hs +++ b/src/Instructions/StringInstructions.hs @@ -2,6 +2,7 @@ module Instructions.StringInstructions where import State import Instructions.GenericInstructions +import Data.List.Split combineString :: String -> (String, String) -> String combineString toInsert (front, back) = front <> toInsert <> back @@ -15,3 +16,56 @@ instructionStringSwap state = instructionSwap state string instructionStringInsertString :: State -> State instructionStringInsertString state@(State{_string = s1 : s2 : ss, _int = i1 : is}) = state {_string = combineString s2 (splitAt i1 s1) : ss, _int = is} instructionStringInsertString state = state + +instructionStringFromFirstChar :: State -> State +instructionStringFromFirstChar state@(State {_string = (schar : _) : ss}) = state {_string = [schar] : ss} +instructionStringFromFirstChar state = state + +instructionStringFromLastChar :: State -> State +instructionStringFromLastChar state@(State {_string = s1 : ss}) = + if not $ null s1 + then state {_string = [last s1] : ss} + else state +instructionStringFromLastChar state = state + +instructionStringFromNthChar :: State -> State +instructionStringFromNthChar state@(State {_string = s1 : ss, _int = i1 : is}) = + let + index = abs i1 `mod` length s1 + in + state{_string = [s1 !! index] : ss, _int = is} +instructionStringFromNthChar state = state + +-- instructionStringContainsString :: State -> State +-- instructionStringContainsString state@(State ) + +-- Haskell is kinda really cool. This can totally be +-- abstracted +findSubString :: String -> String -> Int +findSubString fullString subString + | length fullString < length subString = -1 + | length fullString == length subString = if fullString == subString then 0 else -1 + | otherwise = findSubString' fullString subString 0 + where + findSubString' :: String -> String -> Int -> Int + findSubString' fStr sStr index + | null fStr = -1 + | length sStr > length fStr = -1 + | sStr == take (length sStr) fStr = index + | otherwise = findSubString' (drop 1 fStr) sStr (index + 1) + +instructionStringIndexOfString :: State -> State +instructionStringIndexOfString state@(State {_string = s1 : s2 : ss, _int = is}) = state {_string = ss, _int = findSubString s1 s2 : is} +instructionStringIndexOfString state = state + +instructionStringContainsString :: State -> State +instructionStringContainsString state@(State {_string = s1 : s2 : ss, _bool = bs}) = state {_string = ss, _bool = (findSubString s1 s2 /= -1) : bs} +instructionStringContainsString state = state + +instructionStringSplitOnString :: State -> State +instructionStringSplitOnString state@(State {_string = s1 : s2 : ss}) = state {_string = splitOn s2 s1 <> ss} +instructionStringSplitOnString state = state + +instructionStringReplaceFirstString :: State -> State +instructionStringReplaceFirstString state@(State {_string = s1 : s2 : s3 : ss}) = undefined -- TODO +instructionStringReplaceFirstString state = state From 2fb84b1da925199d1378b4d2bd4cfcde4ea5922f Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sat, 25 Jan 2025 00:14:05 -0600 Subject: [PATCH 055/171] char instructions --- HushGP.cabal | 1 + 1 file changed, 1 insertion(+) diff --git a/HushGP.cabal b/HushGP.cabal index ac88050..3d8acde 100644 --- a/HushGP.cabal +++ b/HushGP.cabal @@ -45,6 +45,7 @@ library , Instructions.LogicalInstructions , Instructions.CodeInstructions , Instructions.StringInstructions + , Instructions.CharInstructions -- Modules included in this library but not exported. -- other-modules: From 35f8a20a9149b88a9656b660185bf3aa5d890ff9 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sat, 25 Jan 2025 00:14:37 -0600 Subject: [PATCH 056/171] I'm proud of these generic instructions --- src/Instructions/GenericInstructions.hs | 50 ++++++++++++++++++++----- 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/src/Instructions/GenericInstructions.hs b/src/Instructions/GenericInstructions.hs index 9cb28a7..561ff77 100644 --- a/src/Instructions/GenericInstructions.hs +++ b/src/Instructions/GenericInstructions.hs @@ -5,9 +5,47 @@ import State -- import Debug.Trace --- Files in the spaces in [[a]] with [a] -fillInHoles :: [a] -> [[a]] -> [a] -fillInHoles filler toFill = undefined -- TODO +deleteAt :: Int -> [a] -> [a] +deleteAt idx xs = take idx xs <> drop 1 (drop idx xs) + +findSubA :: forall a. Eq a => [a] -> [a] -> Int +findSubA fullA subA + | length fullA < length subA = -1 + | length fullA == length subA = if fullA == subA then 0 else -1 + | otherwise = findSubA' fullA subA 0 + where + findSubA' :: [a] -> [a] -> Int -> Int + findSubA' fA sA subIndex + | null fA = -1 + | length sA > length fA = -1 + | sA == take (length sA) fA = subIndex + | otherwise = findSubA' (drop 1 fA) sA (subIndex + 1) + +-- The int is the amount of olds to replace with new +-- Just chain findSubA calls lol +-- Nothing means replace all +-- May not be the most efficient method with the findSubA calls +replace :: Eq a => [a] -> [a] -> [a] -> Maybe Int -> [a] +replace fullA old new (Just amt) = + if findSubA fullA old /= -1 && amt > 0 + then replace (take (findSubA fullA old) fullA <> new <> drop (findSubA fullA old + length old) fullA) old new (Just $ amt - 1) + else fullA +replace fullA old new Nothing = + if findSubA fullA old /= -1 + then replace (take (findSubA fullA old) fullA <> new <> drop (findSubA fullA old + length old) fullA) old new Nothing + else fullA + +amtOccurences :: forall a. Eq a => [a] -> [a] -> Int +amtOccurences fullA subA = amtOccurences' fullA subA 0 + where + amtOccurences' :: [a] -> [a] -> Int -> Int + amtOccurences' fA sA count = + if findSubA fA sA /= -1 + then amtOccurences' (replace fA sA mempty (Just 1)) sA (count + 1) + else count + +combineTuple :: a -> ([a], [a]) -> [a] +combineTuple val tup = fst tup <> [val] <> snd tup notEmptyStack :: State -> Lens' State [a] -> Bool notEmptyStack state accessor = not . null $ view accessor state @@ -79,9 +117,6 @@ instructionYankDup state@(State {_int = i : is}) accessor = else state instructionYankDup state@(State {_int = []}) _ = state -deleteAt :: Int -> [a] -> [a] -deleteAt idx xs = take idx xs <> drop 1 (drop idx xs) - -- Is this optimal? Running instrucitonYankDup twice????? -- int non generic too instructionYank :: forall a. State -> Lens' State [a] -> State @@ -97,9 +132,6 @@ instructionYank state@(State {_int = rawIndex : _}) accessor = if notEmptyStack state accessor then deletedState & accessor .~ item : view accessor deletedState else state instructionYank state _ = state -combineTuple :: a -> ([a], [a]) -> [a] -combineTuple val tup = fst tup <> [val] <> snd tup - -- int non generic :( -- Rewrite this eventually? instructionShoveDup :: State -> Lens' State [a] -> State From 78efab9064fa084d74cacd90010a11d61dd047d7 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sat, 25 Jan 2025 00:15:19 -0600 Subject: [PATCH 057/171] more string instructions --- src/Instructions/StringInstructions.hs | 48 ++++++++++++++++---------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/src/Instructions/StringInstructions.hs b/src/Instructions/StringInstructions.hs index 2ce0b42..b15f3c7 100644 --- a/src/Instructions/StringInstructions.hs +++ b/src/Instructions/StringInstructions.hs @@ -39,33 +39,43 @@ instructionStringFromNthChar state = state -- instructionStringContainsString :: State -> State -- instructionStringContainsString state@(State ) --- Haskell is kinda really cool. This can totally be --- abstracted -findSubString :: String -> String -> Int -findSubString fullString subString - | length fullString < length subString = -1 - | length fullString == length subString = if fullString == subString then 0 else -1 - | otherwise = findSubString' fullString subString 0 - where - findSubString' :: String -> String -> Int -> Int - findSubString' fStr sStr index - | null fStr = -1 - | length sStr > length fStr = -1 - | sStr == take (length sStr) fStr = index - | otherwise = findSubString' (drop 1 fStr) sStr (index + 1) - instructionStringIndexOfString :: State -> State -instructionStringIndexOfString state@(State {_string = s1 : s2 : ss, _int = is}) = state {_string = ss, _int = findSubString s1 s2 : is} +instructionStringIndexOfString state@(State {_string = s1 : s2 : ss, _int = is}) = state {_string = ss, _int = findSubA s1 s2 : is} instructionStringIndexOfString state = state instructionStringContainsString :: State -> State -instructionStringContainsString state@(State {_string = s1 : s2 : ss, _bool = bs}) = state {_string = ss, _bool = (findSubString s1 s2 /= -1) : bs} +instructionStringContainsString state@(State {_string = s1 : s2 : ss, _bool = bs}) = state {_string = ss, _bool = (findSubA s1 s2 /= -1) : bs} instructionStringContainsString state = state +-- pysh reverses this. Check this for propeller instructionStringSplitOnString :: State -> State -instructionStringSplitOnString state@(State {_string = s1 : s2 : ss}) = state {_string = splitOn s2 s1 <> ss} +instructionStringSplitOnString state@(State {_string = s1 : s2 : ss}) = state {_string = reverse $ splitOn s2 s1 <> ss} instructionStringSplitOnString state = state instructionStringReplaceFirstString :: State -> State -instructionStringReplaceFirstString state@(State {_string = s1 : s2 : s3 : ss}) = undefined -- TODO +instructionStringReplaceFirstString state@(State {_string = s1 : s2 : s3 : ss}) = state {_string = replace s1 s2 s3 (Just 1) : ss} instructionStringReplaceFirstString state = state + +instructionStringReplaceNString :: State -> State +instructionStringReplaceNString state@(State {_string = s1 : s2 : s3 : ss, _int = i1 : is}) = state{_string = replace s1 s2 s3 (Just i1) : ss, _int = is} +instructionStringReplaceNString state = state + +instructionStringReplaceAllString :: State -> State +instructionStringReplaceAllString state@(State {_string = s1 : s2 : s3 : ss}) = state{_string = replace s1 s2 s3 Nothing : ss} +instructionStringReplaceAllString state = state + +instructionStringRemoveFirstString :: State -> State +instructionStringRemoveFirstString state@(State {_string = s1 : s2 : ss}) = state{_string = replace s1 s2 "" (Just 1) : ss} +instructionStringRemoveFirstString state = state + +instructionStringRemoveNString :: State -> State +instructionStringRemoveNString state@(State {_string = s1 : s2 : ss, _int = i1 : is}) = state{_string = replace s1 s2 "" (Just i1) : ss, _int = is} +instructionStringRemoveNString state = state + +instructionStringRemoveAllString :: State -> State +instructionStringRemoveAllString state@(State {_string = s1 : s2 : ss}) = state{_string = replace s1 s2 "" Nothing : ss} +instructionStringRemoveAllString state = state + +instructionStringOccurrencesOfString :: State -> State +instructionStringOccurrencesOfString state@(State {_string = s1 : s2 : ss, _int = is}) = state{_string = ss, _int = amtOccurences s1 s2 : is} +instructionStringOccurrencesOfString state = state From 4ad47b53083b0c79c212a1b74825d9f058fdcef1 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sat, 25 Jan 2025 00:15:49 -0600 Subject: [PATCH 058/171] Need to complete add MANY MORE of these lol --- test/Main.hs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/Main.hs b/test/Main.hs index 65d5d8a..e901e82 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -155,3 +155,5 @@ main = do intTestFunc "instructionStringIndexOfString3" [-1] [GeneString "z", GeneString "123abc", StateFunc instructionStringIndexOfString] emptyState boolTestFunc "instructionStringContainsStringTrue" [True] [GeneString "a", GeneString "123abc", StateFunc instructionStringContainsString] emptyState boolTestFunc "instructionStringContainsStringTrue" [False] [GeneString "z", GeneString "123abc", StateFunc instructionStringContainsString] emptyState + stringTestFunc "instructionStringSplitOnString" ["nd", "r fri", "llo gam", "h"] [GeneString "e", GeneString "hello gamer friend", StateFunc instructionStringSplitOnString] emptyState + stringTestFunc "instructionStringSplitOnString" ["", "hello gamer frien"] [GeneString "d", GeneString "hello gamer friend", StateFunc instructionStringSplitOnString] emptyState From c17e0df4da6172d3e75e7edc7d701a60c7d0aca3 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sat, 25 Jan 2025 00:16:08 -0600 Subject: [PATCH 059/171] one basic char instruction --- src/Instructions/CharInstructions.hs | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 src/Instructions/CharInstructions.hs diff --git a/src/Instructions/CharInstructions.hs b/src/Instructions/CharInstructions.hs new file mode 100644 index 0000000..9b59a11 --- /dev/null +++ b/src/Instructions/CharInstructions.hs @@ -0,0 +1,10 @@ +module Instructions.CharInstructions where + +import State +import Instructions.GenericInstructions + +instructionCharConcat :: State -> State +instructionCharConcat state@(State {_char = c1 : c2 : cs, _string = ss}) = state{_char = cs, _string = [c1, c2] : ss} +instructionCharConcat state = state + + From e3cf1d212114d0981a7cdc3a570149e58dd5859e Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sat, 25 Jan 2025 01:50:14 -0600 Subject: [PATCH 060/171] need to write tests --- src/Instructions/CharInstructions.hs | 20 ++++++++++++++++++++ src/Instructions/StringInstructions.hs | 4 ++++ 2 files changed, 24 insertions(+) diff --git a/src/Instructions/CharInstructions.hs b/src/Instructions/CharInstructions.hs index 9b59a11..0f3626f 100644 --- a/src/Instructions/CharInstructions.hs +++ b/src/Instructions/CharInstructions.hs @@ -7,4 +7,24 @@ instructionCharConcat :: State -> State instructionCharConcat state@(State {_char = c1 : c2 : cs, _string = ss}) = state{_char = cs, _string = [c1, c2] : ss} instructionCharConcat state = state +instructionCharFromFirstChar :: State -> State +instructionCharFromFirstChar state@(State {_char = cs, _string = s1 : ss}) = + if not $ null s1 + then state {_char = head s1 : cs, _string = ss} + else state +instructionCharFromFirstChar state = state +instructionCharFromLastChar :: State -> State +instructionCharFromLastChar state@(State {_char = cs, _string = s1 : ss}) = + if not $ null s1 + then state {_char = last s1 : cs, _string = ss} + else state +instructionCharFromLastChar state = state + +instructionCharFromNthChar :: State -> State +instructionCharFromNthChar state@(State {_char = cs, _string = s1 : ss, _int = i1 : is}) = + let + index = abs i1 `mod` length s1 + in + state{_char = s1 !! index : cs, _string = ss, _int = is} +instructionCharFromNthChar state = state diff --git a/src/Instructions/StringInstructions.hs b/src/Instructions/StringInstructions.hs index b15f3c7..0e99477 100644 --- a/src/Instructions/StringInstructions.hs +++ b/src/Instructions/StringInstructions.hs @@ -79,3 +79,7 @@ instructionStringRemoveAllString state = state instructionStringOccurrencesOfString :: State -> State instructionStringOccurrencesOfString state@(State {_string = s1 : s2 : ss, _int = is}) = state{_string = ss, _int = amtOccurences s1 s2 : is} instructionStringOccurrencesOfString state = state + +instructionStringInsertChar :: State -> State +instructionStringInsertChar state@(State {_string = s1 : ss, _char = c1 : cs, _int = i1 : is}) = state {_string = combineString [c1] (splitAt i1 s1) : ss, _char = cs, _int = is} +instructionStringInsertChar state = state From 82d2fa34fe68c5a541ae88ea90fc2edf01220587 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sat, 25 Jan 2025 19:08:49 -0600 Subject: [PATCH 061/171] I love testing strings, still have more to do --- test/Main.hs | 67 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 62 insertions(+), 5 deletions(-) diff --git a/test/Main.hs b/test/Main.hs index e901e82..74adcc4 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -4,9 +4,12 @@ import Instructions.ExecInstructions import Instructions.FloatInstructions import Instructions.IntInstructions import Instructions.StringInstructions +import Instructions.CharInstructions import Push import State +-- import Debug.Trace + -- @TODO: Finish int and float tests -- TODO: Need a function that can compare states. @@ -36,6 +39,11 @@ stringTestFunc name goal genome startState = let state = loadProgram genome startState in assert (goal == _string (interpretExec state)) putStrLn (name <> " passed test.") +charTestFunc :: String -> [Char] -> [Gene] -> State -> IO () +charTestFunc name goal genome startState = + let state = loadProgram genome startState + in assert (goal == _char (interpretExec state)) putStrLn (name <> " passed test.") + main :: IO () main = do -- Int tests @@ -46,7 +54,7 @@ main = do intTestFunc "instructionIntDiv0" [0, 2] [GeneInt 2, GeneInt 0, StateFunc instructionIntDiv] emptyState intTestFunc "instructionIntMod" [3] [GeneInt 13, GeneInt 5, StateFunc instructionIntMod] emptyState intTestFunc "instructionIntPop" [2] [GeneInt 2, GeneInt 0, StateFunc instructionIntPop] emptyState - intTestFunc "instrucitonIntDup" [3, 3, 2] [GeneInt 2, GeneInt 3, StateFunc instructionIntDup] emptyState + intTestFunc "instructionIntDup" [3, 3, 2] [GeneInt 2, GeneInt 3, StateFunc instructionIntDup] emptyState intTestFunc "instructionIntDupN" [2, 2, 2] [GeneInt 2, GeneInt 3, StateFunc instructionIntDupN] emptyState intTestFunc "instructionIntSwap" [2, 0, 3] [GeneInt 3, GeneInt 2, GeneInt 0, StateFunc instructionIntSwap] emptyState intTestFunc "instructionIntSwapFail" [1] [GeneInt 1, StateFunc instructionIntSwap] emptyState @@ -152,8 +160,57 @@ main = do stringTestFunc "instructionStringFromFirstChar" ["1"] [GeneString "123", StateFunc instructionStringFromFirstChar] emptyState stringTestFunc "instructionStringFromNthChar" ["a"] [GeneString "123abc", GeneInt 3, StateFunc instructionStringFromNthChar] emptyState intTestFunc "instructionStringIndexOfString3" [3] [GeneString "a", GeneString "123abc", StateFunc instructionStringIndexOfString] emptyState - intTestFunc "instructionStringIndexOfString3" [-1] [GeneString "z", GeneString "123abc", StateFunc instructionStringIndexOfString] emptyState + intTestFunc "instructionStringIndexOfString-1" [-1] [GeneString "z", GeneString "123abc", StateFunc instructionStringIndexOfString] emptyState boolTestFunc "instructionStringContainsStringTrue" [True] [GeneString "a", GeneString "123abc", StateFunc instructionStringContainsString] emptyState - boolTestFunc "instructionStringContainsStringTrue" [False] [GeneString "z", GeneString "123abc", StateFunc instructionStringContainsString] emptyState - stringTestFunc "instructionStringSplitOnString" ["nd", "r fri", "llo gam", "h"] [GeneString "e", GeneString "hello gamer friend", StateFunc instructionStringSplitOnString] emptyState - stringTestFunc "instructionStringSplitOnString" ["", "hello gamer frien"] [GeneString "d", GeneString "hello gamer friend", StateFunc instructionStringSplitOnString] emptyState + boolTestFunc "instructionStringContainsStringFalse" [False] [GeneString "z", GeneString "123abc", StateFunc instructionStringContainsString] emptyState + stringTestFunc "instructionStringSplitOnStringMult" ["nd", "r fri", "llo gam", "h"] [GeneString "e", GeneString "hello gamer friend", StateFunc instructionStringSplitOnString] emptyState + stringTestFunc "instructionStringSplitOnStringEmpty" ["", "hello gamer frien"] [GeneString "d", GeneString "hello gamer friend", StateFunc instructionStringSplitOnString] emptyState + stringTestFunc "instructionStringReplaceFirstStringSuccess" ["thREPLACEs is a sentence"] [GeneString "REPLACE", GeneString "i", GeneString "this is a sentence", StateFunc instructionStringReplaceFirstString] emptyState + stringTestFunc "instructionStringReplaceFirstStringFail" ["this is a sentence"] [GeneString "REPLACE", GeneString "z", GeneString "this is a sentence", StateFunc instructionStringReplaceFirstString] emptyState + stringTestFunc "instructionStringReplaceNStringSuccess" ["thREPLACEs REPLACEs a sentence i"] [GeneString "REPLACE", GeneString "i", GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringReplaceNString] emptyState + stringTestFunc "instructionStringReplaceNStringFail" ["this is a sentence i"] [GeneString "REPLACE", GeneString "z", GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringReplaceNString] emptyState + stringTestFunc "instructionStringReplaceAllStringSuccess" ["thREPLACEs REPLACEs a sentence REPLACE"] [GeneString "REPLACE", GeneString "i", GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringReplaceAllString] emptyState + stringTestFunc "instructionStringReplaceAllStringFail" ["this is a sentence i"] [GeneString "REPLACE", GeneString "z", GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringReplaceAllString] emptyState + stringTestFunc "instructionStringRemoveFirstStringSuccess" ["ths is a sentence"] [GeneString "i", GeneString "this is a sentence", StateFunc instructionStringRemoveFirstString] emptyState + stringTestFunc "instructionStringRemoveFirstStringFail" ["this is a sentence"] [GeneString "z", GeneString "this is a sentence", StateFunc instructionStringRemoveFirstString] emptyState + stringTestFunc "instructionStringRemoveNStringSuccess" ["ths s a sentence i"] [GeneString "i", GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringRemoveNString] emptyState + stringTestFunc "instructionStringRemoveNStringFail" ["this is a sentence i"] [GeneString "z", GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringRemoveNString] emptyState + stringTestFunc "instructionStringRemoveAllStringSuccess" ["ths s a sentence "] [GeneString "i", GeneString "this is a sentence i", StateFunc instructionStringRemoveAllString] emptyState + stringTestFunc "instructionStringRemoveAllStringFail" ["this is a sentence i"] [GeneString "z", GeneString "this is a sentence i", StateFunc instructionStringRemoveAllString] emptyState + intTestFunc "instructionStringOccurrencesOfString3" [3] [GeneString "i", GeneString "this is a sentence i", StateFunc instructionStringOccurrencesOfString] emptyState + intTestFunc "instructionStringOccurrencesOfString3" [2] [GeneString "is", GeneString "this is a sentence i", StateFunc instructionStringOccurrencesOfString] emptyState + intTestFunc "instructionStringOccurrencesOfString0" [0] [GeneString "z", GeneString "this is a sentence i", StateFunc instructionStringOccurrencesOfString] emptyState + stringTestFunc "instructionStringInsertChar" ["123Zabc"] [GeneString "abc", GeneString "123", StateFunc instructionStringConcat, GeneChar 'Z', GeneInt 3, StateFunc instructionStringInsertChar] emptyState + boolTestFunc "instructionStringContainsCharTrue" [True] [GeneString "abc", GeneChar 'a', StateFunc instructionStringContainsChar] emptyState + boolTestFunc "instructionStringContainsCharFalse" [False] [GeneString "abc", GeneChar 'z', StateFunc instructionStringContainsChar] emptyState + intTestFunc "instructionStringIndexOfChar3" [3] [GeneChar 'a', GeneString "123abc", StateFunc instructionStringIndexOfChar] emptyState + intTestFunc "instructionStringIndexOfChar-1" [-1] [GeneChar 'z', GeneString "123abc", StateFunc instructionStringIndexOfChar] emptyState + stringTestFunc "instructionStringSplitOnCharMult" ["nd", "r fri", "llo gam", "h"] [GeneChar 'e', GeneString "hello gamer friend", StateFunc instructionStringSplitOnChar] emptyState + stringTestFunc "instructionStringSplitOnCharEmpty" ["", "hello gamer frien"] [GeneChar 'd', GeneString "hello gamer friend", StateFunc instructionStringSplitOnChar] emptyState + stringTestFunc "instructionStringReplaceFirstCharSuccess" ["thRs is a sentence"] [GeneChar 'R', GeneChar 'i', GeneString "this is a sentence", StateFunc instructionStringReplaceFirstChar] emptyState + stringTestFunc "instructionStringReplaceFirstCharFail" ["this is a sentence"] [GeneChar 'R', GeneChar 'z', GeneString "this is a sentence", StateFunc instructionStringReplaceFirstChar] emptyState + stringTestFunc "instructionStringReplaceNCharSuccess" ["thRs Rs a sentence i"] [GeneChar 'R', GeneChar 'i', GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringReplaceNChar] emptyState + stringTestFunc "instructionStringReplaceNCharFail" ["this is a sentence i"] [GeneChar 'R', GeneChar 'z', GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringReplaceNChar] emptyState + stringTestFunc "instructionStringReplaceAllCharSuccess" ["thRs Rs a sentence R"] [GeneChar 'R', GeneChar 'i', GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringReplaceAllChar] emptyState + stringTestFunc "instructionStringReplaceAllCharFail" ["this is a sentence i"] [GeneChar 'R', GeneChar 'z', GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringReplaceAllChar] emptyState + stringTestFunc "instructionStringRemoveFirstCharSuccess" ["ths is a sentence"] [GeneChar 'i', GeneString "this is a sentence", StateFunc instructionStringRemoveFirstChar] emptyState + stringTestFunc "instructionStringRemoveFirstCharFail" ["this is a sentence"] [GeneChar 'z', GeneString "this is a sentence", StateFunc instructionStringRemoveFirstChar] emptyState + stringTestFunc "instructionStringRemoveNCharSuccess" ["ths s a sentence i"] [GeneChar 'i', GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringRemoveNChar] emptyState + stringTestFunc "instructionStringRemoveNCharFail" ["this is a sentence i"] [GeneChar 'z', GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringRemoveNChar] emptyState + stringTestFunc "instructionStringRemoveAllCharSuccess" ["ths s a sentence "] [GeneChar 'i', GeneString "this is a sentence i", StateFunc instructionStringRemoveAllChar] emptyState + stringTestFunc "instructionStringRemoveAllCharFail" ["this is a sentence i"] [GeneChar 'z', GeneString "this is a sentence i", StateFunc instructionStringRemoveAllChar] emptyState + intTestFunc "instructionStringOccurrencesOfChar3" [3] [GeneChar 'i', GeneString "this is a sentence i", StateFunc instructionStringOccurrencesOfChar] emptyState + intTestFunc "instructionStringOccurrencesOfChar0" [0] [GeneChar 'z', GeneString "this is a sentence i", StateFunc instructionStringOccurrencesOfChar] emptyState + stringTestFunc "instructionStringReverse" ["321cba"] [GeneString "abc123", StateFunc instructionStringReverse] emptyState + stringTestFunc "instructionStringHead3" ["abc"] [GeneString "abc123", GeneInt 3, StateFunc instructionStringHead] emptyState + stringTestFunc "instructionStringHead0" [""] [GeneString "abc123", GeneInt 0, StateFunc instructionStringHead] emptyState + stringTestFunc "instructionStringTail3" ["123"] [GeneString "abc123", GeneInt 3, StateFunc instructionStringTail] emptyState + stringTestFunc "instructionStringTail0" [""] [GeneString "abc123", GeneInt 0, StateFunc instructionStringTail] emptyState + + -- char instructions + stringTestFunc "instructionCharConcat" ["ab"] [GeneChar 'b', GeneChar 'a', StateFunc instructionCharConcat] emptyState + charTestFunc "instructionCharFromFirstCharSuccess" ['a'] [GeneString "abc123", StateFunc instructionCharFromFirstChar] emptyState + charTestFunc "instructionCharFromFirstCharFail" [] [GeneString "", StateFunc instructionCharFromFirstChar] emptyState + charTestFunc "instructionCharFromLastCharSuccess" ['3'] [GeneString "abc123", StateFunc instructionCharFromLastChar] emptyState + charTestFunc "instructionCharFromLastCharFail" [] [GeneString "", StateFunc instructionCharFromLastChar] emptyState + charTestFunc "instructionCharFromNthCharSuccess" ['c'] [GeneString "abc123", GeneInt 2, StateFunc instructionCharFromNthChar] emptyState From 804da2a23c08a4b9f8e0f9046a1a22d3b13699c2 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sat, 25 Jan 2025 19:09:10 -0600 Subject: [PATCH 062/171] takeR and dropR --- src/Instructions/GenericInstructions.hs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Instructions/GenericInstructions.hs b/src/Instructions/GenericInstructions.hs index 561ff77..8f945a1 100644 --- a/src/Instructions/GenericInstructions.hs +++ b/src/Instructions/GenericInstructions.hs @@ -44,6 +44,12 @@ amtOccurences fullA subA = amtOccurences' fullA subA 0 then amtOccurences' (replace fA sA mempty (Just 1)) sA (count + 1) else count +takeR :: Int -> [a] -> [a] +takeR amt fullA = drop (length fullA - amt) fullA + +dropR :: Int -> [a] -> [a] +dropR amt fullA = take (length fullA - amt) fullA + combineTuple :: a -> ([a], [a]) -> [a] combineTuple val tup = fst tup <> [val] <> snd tup From a55a66f456a1fcb908305f6317c8e2c41c6967be Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sat, 25 Jan 2025 19:09:29 -0600 Subject: [PATCH 063/171] more string functions, need a break --- src/Instructions/StringInstructions.hs | 101 +++++++++++++++++++++++-- 1 file changed, 95 insertions(+), 6 deletions(-) diff --git a/src/Instructions/StringInstructions.hs b/src/Instructions/StringInstructions.hs index 0e99477..2dbbd63 100644 --- a/src/Instructions/StringInstructions.hs +++ b/src/Instructions/StringInstructions.hs @@ -4,6 +4,9 @@ import State import Instructions.GenericInstructions import Data.List.Split +absNum :: Integral a => a -> [b] -> Int +absNum rawNum lst = abs (fromIntegral rawNum) `mod` length lst + combineString :: String -> (String, String) -> String combineString toInsert (front, back) = front <> toInsert <> back @@ -29,11 +32,7 @@ instructionStringFromLastChar state@(State {_string = s1 : ss}) = instructionStringFromLastChar state = state instructionStringFromNthChar :: State -> State -instructionStringFromNthChar state@(State {_string = s1 : ss, _int = i1 : is}) = - let - index = abs i1 `mod` length s1 - in - state{_string = [s1 !! index] : ss, _int = is} +instructionStringFromNthChar state@(State {_string = s1 : ss, _int = i1 : is}) = state{_string = [s1 !! absNum i1 s1] : ss, _int = is} instructionStringFromNthChar state = state -- instructionStringContainsString :: State -> State @@ -81,5 +80,95 @@ instructionStringOccurrencesOfString state@(State {_string = s1 : s2 : ss, _int instructionStringOccurrencesOfString state = state instructionStringInsertChar :: State -> State -instructionStringInsertChar state@(State {_string = s1 : ss, _char = c1 : cs, _int = i1 : is}) = state {_string = combineString [c1] (splitAt i1 s1) : ss, _char = cs, _int = is} +instructionStringInsertChar state@(State {_string = s1 : ss, _char = c1 : cs, _int = i1 : is}) = state{_string = combineString [c1] (splitAt i1 s1) : ss, _char = cs, _int = is} instructionStringInsertChar state = state + +instructionStringContainsChar :: State -> State +instructionStringContainsChar state@(State {_string = s1 : ss, _char = c1 : cs, _bool = bs}) = state{_string = ss, _char = cs, _bool = (findSubA s1 [c1] /= -1) : bs} +instructionStringContainsChar state = state + +instructionStringIndexOfChar :: State -> State +instructionStringIndexOfChar state@(State {_string = s1 : ss, _char = c1 : cs, _int = is}) = state{_string = ss, _char = cs, _int = findSubA s1 [c1] : is} +instructionStringIndexOfChar state = state + +instructionStringSplitOnChar :: State -> State +instructionStringSplitOnChar state@(State {_string = s1 : ss, _char = c1 : cs}) = state {_string = reverse $ splitOn [c1] s1 <> ss, _char = cs} +instructionStringSplitOnChar state = state + +instructionStringReplaceFirstChar :: State -> State +instructionStringReplaceFirstChar state@(State {_string = s1 : ss, _char = c1 : c2 : cs}) = state {_string = replace s1 [c1] [c2] (Just 1) : ss, _char = cs} +instructionStringReplaceFirstChar state = state + +instructionStringReplaceNChar :: State -> State +instructionStringReplaceNChar state@(State {_string = s1 : ss, _char = c1 : c2 : cs, _int = i1 : is}) = state{_string = replace s1 [c1] [c2] (Just i1) : ss, _char = cs, _int = is} +instructionStringReplaceNChar state = state + +instructionStringReplaceAllChar :: State -> State +instructionStringReplaceAllChar state@(State {_string = s1 : ss, _char = c1 : c2 : cs}) = state{_string = replace s1 [c1] [c2] Nothing : ss, _char = cs} +instructionStringReplaceAllChar state = state + +instructionStringRemoveFirstChar :: State -> State +instructionStringRemoveFirstChar state@(State {_string = s1 : ss, _char = c1 : cs}) = state {_string = replace s1 [c1] "" (Just 1) : ss, _char = cs} +instructionStringRemoveFirstChar state = state + +instructionStringRemoveNChar :: State -> State +instructionStringRemoveNChar state@(State {_string = s1 : ss, _char = c1 : cs, _int = i1 : is}) = state{_string = replace s1 [c1] "" (Just i1) : ss, _char = cs, _int = is} +instructionStringRemoveNChar state = state + +instructionStringRemoveAllChar :: State -> State +instructionStringRemoveAllChar state@(State {_string = s1 : ss, _char = c1 : cs}) = state{_string = replace s1 [c1] "" Nothing : ss, _char = cs} +instructionStringRemoveAllChar state = state + +instructionStringOccurrencesOfChar :: State -> State +instructionStringOccurrencesOfChar state@(State {_string = s1 : ss, _char = c1 : cs, _int = is}) = state{_string = ss, _char = cs, _int = amtOccurences s1 [c1] : is} +instructionStringOccurrencesOfChar state = state + +instructionStringReverse :: State -> State +instructionStringReverse state@(State {_string = s1 : ss}) = state{_string = reverse s1 : ss} +instructionStringReverse state = state + +instructionStringHead :: State -> State +instructionStringHead state@(State {_string = s1 : ss, _int = i1 : is}) = state{_string = take (absNum i1 s1) s1 : ss, _int = is} +instructionStringHead state = state + +instructionStringTail :: State -> State +instructionStringTail state@(State {_string = s1 : ss, _int = i1 : is}) = state{_string = takeR (absNum i1 s1) s1 : ss, _int = is} +instructionStringTail state = state + +instructionStringAppendChar :: State -> State +instructionStringAppendChar state@(State {_string = s1 : ss, _char = c1 : cs}) = state{_string = (c1 : s1) : ss, _char = cs} +instructionStringAppendChar state = state + +instructionStringRest :: State -> State +instructionStringRest state@(State {_string = s1 : ss}) = state{_string = drop 1 s1 : ss} +instructionStringRest state = state + +instructionStringButLast :: State -> State +instructionStringButLast state@(State {_string = s1 : ss}) = + if not $ null s1 + then state{_string = init s1 : ss} + else state +instructionStringButLast state = state + +instructionStringDrop :: State -> State +instructionStringDrop state@(State {_string = s1 : ss, _int = i1 : is}) = state{_string = drop (absNum i1 s1) s1 : ss, _int = is} +instructionStringDrop state = state + +instructionStringButLastN :: State -> State +instructionStringButLastN state@(State {_string = s1 : ss, _int = i1 : is}) = state{_string = dropR (absNum i1 s1) s1 : ss, _int = is} +instructionStringButLastN state = state + +instructionStringLength :: State -> State +instructionStringLength state@(State {_string = s1 : ss, _int = is}) = state{_string = ss, _int = length s1 : is} +instructionStringLength state = state + +instructionStringMakeEmpty :: State -> State +instructionStringMakeEmpty state@(State {_string = ss}) = state{_string = "" : ss} + +instructionStringIsEmptyString :: State -> State +instructionStringIsEmptyString state@(State {_string = s1 : ss, _bool = bs}) = state{_string = ss, _bool = null s1 : bs} +instructionStringIsEmptyString state = state + +instructionStringRemoveNth :: State -> State +instructionStringRemoveNth state@(State {_string = s1 : ss, _int = i1 : is}) = state{_string = deleteAt (absNum i1 s1) s1 : ss, _int = is} +instructionStringRemoveNth state = state From 0a6a1fc0cb9716a5b4532b161aea41d4d6ae8c16 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sat, 25 Jan 2025 22:57:20 -0600 Subject: [PATCH 064/171] button of string and char tests --- test/Main.hs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/test/Main.hs b/test/Main.hs index 74adcc4..0a40805 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -206,6 +206,28 @@ main = do stringTestFunc "instructionStringHead0" [""] [GeneString "abc123", GeneInt 0, StateFunc instructionStringHead] emptyState stringTestFunc "instructionStringTail3" ["123"] [GeneString "abc123", GeneInt 3, StateFunc instructionStringTail] emptyState stringTestFunc "instructionStringTail0" [""] [GeneString "abc123", GeneInt 0, StateFunc instructionStringTail] emptyState + stringTestFunc "instructionStringAppendChar" ["Rabc123"] [GeneString "abc123", GeneChar 'R', StateFunc instructionStringAppendChar] emptyState + stringTestFunc "instructionStringRestFull" ["bc123"] [GeneString "abc123", StateFunc instructionStringRest] emptyState + stringTestFunc "instructionStringRestEmpty" [""] [GeneString "", StateFunc instructionStringRest] emptyState + stringTestFunc "instructionStringButLastFull" ["abc12"] [GeneString "abc123", StateFunc instructionStringButLast] emptyState + stringTestFunc "instructionStringButLastEmpty" [""] [GeneString "", StateFunc instructionStringButLast] emptyState + stringTestFunc "instructionStringDrop3" ["123"] [GeneString "abc123", GeneInt 3, StateFunc instructionStringDrop] emptyState + stringTestFunc "instructionStringDrop0" ["abc123"] [GeneString "abc123", GeneInt 0, StateFunc instructionStringDrop] emptyState + stringTestFunc "instructionStringButLastN3" ["abc"] [GeneString "abc123", GeneInt 3, StateFunc instructionStringButLastN] emptyState + stringTestFunc "instructionStringButLastN0" ["abc123"] [GeneString "abc123", GeneInt 0, StateFunc instructionStringButLastN] emptyState + intTestFunc "instructionStringLength6" [6] [GeneString "abc123", StateFunc instructionStringLength] emptyState + stringTestFunc "instructionStringMakeEmpty" ["", "abc123"] [GeneString "abc123", StateFunc instructionStringMakeEmpty] emptyState + stringTestFunc "instructionStringRemoveNth" ["abc23"] [GeneString "abc123", GeneInt 3, StateFunc instructionStringRemoveNth] emptyState + stringTestFunc "instructionStringSetNth" ["abR123"] [GeneString "abc123", GeneInt 2, GeneChar 'R', StateFunc instructionStringSetNth] emptyState + stringTestFunc "instructionStringStripWhitespace" ["abc123"] [GeneString " \r \n abc123 \t", StateFunc instructionStringStripWhitespace] emptyState + stringTestFunc "instructionStringFromBoolTrue" ["True"] [GeneBool True, StateFunc instructionStringFromBool] emptyState + stringTestFunc "instructionStringFromBoolTrue" ["False"] [GeneBool False, StateFunc instructionStringFromBool] emptyState + stringTestFunc "instructionStringFromInt1000" ["1000"] [GeneInt 1000, StateFunc instructionStringFromInt] emptyState + stringTestFunc "instructionStringFromInt-1" ["-1"] [GeneInt (-1), StateFunc instructionStringFromInt] emptyState + stringTestFunc "instructionStringFromFloat3.2" ["3.2"] [GeneFloat 3.2, StateFunc instructionStringFromFloat] emptyState + stringTestFunc "instructionStringFromFloat-99.0" ["-99.0"] [GeneFloat (-99.0), StateFunc instructionStringFromFloat] emptyState + stringTestFunc "instructionStringFromChar" ["Z"] [GeneChar 'Z', StateFunc instructionStringFromChar] emptyState + stringTestFunc "instructionStringFromChar" [" "] [GeneChar ' ', StateFunc instructionStringFromChar] emptyState -- char instructions stringTestFunc "instructionCharConcat" ["ab"] [GeneChar 'b', GeneChar 'a', StateFunc instructionCharConcat] emptyState @@ -214,3 +236,12 @@ main = do charTestFunc "instructionCharFromLastCharSuccess" ['3'] [GeneString "abc123", StateFunc instructionCharFromLastChar] emptyState charTestFunc "instructionCharFromLastCharFail" [] [GeneString "", StateFunc instructionCharFromLastChar] emptyState charTestFunc "instructionCharFromNthCharSuccess" ['c'] [GeneString "abc123", GeneInt 2, StateFunc instructionCharFromNthChar] emptyState + boolTestFunc "instructionCharIsWhitespaceSpace" [True] [GeneChar ' ', StateFunc instructionCharIsWhitespace] emptyState + boolTestFunc "instructionCharIsWhitespacet" [True] [GeneChar '\t', StateFunc instructionCharIsWhitespace] emptyState + boolTestFunc "instructionCharIsWhitespacer" [True] [GeneChar '\r', StateFunc instructionCharIsWhitespace] emptyState + boolTestFunc "instructionCharIsWhitespacen" [True] [GeneChar '\n', StateFunc instructionCharIsWhitespace] emptyState + boolTestFunc "instructionCharIsWhitespaceFalse" [False] [GeneChar 'a', StateFunc instructionCharIsWhitespace] emptyState + boolTestFunc "instructionCharIsLetterTrue" [True] [GeneChar 'a', StateFunc instructionCharIsLetter] emptyState + boolTestFunc "instructionCharIsLetterFalse" [False] [GeneChar '1', StateFunc instructionCharIsLetter] emptyState + boolTestFunc "instructionCharIsDigitTrue" [True] [GeneChar '1', StateFunc instructionCharIsDigit] emptyState + boolTestFunc "instructionCharIsDigitFalse" [False] [GeneChar 'a', StateFunc instructionCharIsDigit] emptyState From f613837ddf1916cc7752cb86b208da89d37f020c Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sat, 25 Jan 2025 22:58:23 -0600 Subject: [PATCH 065/171] generalization/ more instructions/ etc --- src/Instructions/CharInstructions.hs | 33 +++++++++++++++ src/Instructions/ExecInstructions.hs | 6 +-- src/Instructions/GenericInstructions.hs | 17 +++++++- src/Instructions/StringInstructions.hs | 56 +++++++++++++++++++++---- 4 files changed, 98 insertions(+), 14 deletions(-) diff --git a/src/Instructions/CharInstructions.hs b/src/Instructions/CharInstructions.hs index 0f3626f..92e34ee 100644 --- a/src/Instructions/CharInstructions.hs +++ b/src/Instructions/CharInstructions.hs @@ -1,7 +1,12 @@ module Instructions.CharInstructions where +import Data.Char import State import Instructions.GenericInstructions +import Instructions.StringInstructions (wschars) + +intToAscii :: (Integral a) => a -> Char +intToAscii val = chr (abs (fromIntegral val) `mod` 128) instructionCharConcat :: State -> State instructionCharConcat state@(State {_char = c1 : c2 : cs, _string = ss}) = state{_char = cs, _string = [c1, c2] : ss} @@ -28,3 +33,31 @@ instructionCharFromNthChar state@(State {_char = cs, _string = s1 : ss, _int = i in state{_char = s1 !! index : cs, _string = ss, _int = is} instructionCharFromNthChar state = state + +instructionCharIsWhitespace :: State -> State +instructionCharIsWhitespace state@(State {_char = c1 : cs, _bool = bs}) = state{_char = cs, _bool = (c1 `elem` wschars) : bs} +instructionCharIsWhitespace state = state + +instructionCharIsLetter :: State -> State +instructionCharIsLetter state@(State {_char = c1 : cs, _bool = bs}) = state{_char = cs, _bool = isAlpha c1 : bs} +instructionCharIsLetter state = state + +instructionCharIsDigit :: State -> State +instructionCharIsDigit state@(State {_char = c1 : cs, _bool = bs}) = state{_char = cs, _bool = isDigit c1 : bs} +instructionCharIsDigit state = state + +instructionCharFromBool :: State -> State +instructionCharFromBool state@(State {_char = cs, _bool = b1 : bs}) = state{_char = (if b1 then 'T' else 'F') : cs, _bool = bs} +instructionCharFromBool state = state + +instructionCharFromAsciiInt :: State -> State +instructionCharFromAsciiInt state@(State {_char = cs, _int = i1 : is}) = state{_char = intToAscii i1 : cs, _int = is} +instructionCharFromAsciiInt state = state + +instructionCharFromAsciiFloat :: State -> State +instructionCharFromAsciiFloat state@(State {_char = cs, _float = f1 : fs}) = state{_char = intToAscii @Integer (floor f1) : cs, _float = fs} +instructionCharFromAsciiFloat state = state + +instructionCharsFromString :: State -> State +instructionCharsFromString state@(State {_char = cs, _string = s1 : ss}) = state{_char = s1 <> cs, _string = ss} +instructionCharsFromString state = state diff --git a/src/Instructions/ExecInstructions.hs b/src/Instructions/ExecInstructions.hs index 5c5888d..4177292 100644 --- a/src/Instructions/ExecInstructions.hs +++ b/src/Instructions/ExecInstructions.hs @@ -5,10 +5,10 @@ import Instructions.IntInstructions import Instructions.GenericInstructions instructionExecIf :: State -> State -instructionExecIf state@(State {_exec = (e1 : e2 : es), _bool = (b : _)}) = +instructionExecIf state@(State {_exec = (e1 : e2 : es), _bool = (b : bs)}) = if b - then state {_exec = e1 : es} - else state {_exec = e2 : es} + then state {_exec = e1 : es, _bool = bs} + else state {_exec = e2 : es, _bool = bs} instructionExecIf state = state instructionExecDup :: State -> State diff --git a/src/Instructions/GenericInstructions.hs b/src/Instructions/GenericInstructions.hs index 8f945a1..3311a04 100644 --- a/src/Instructions/GenericInstructions.hs +++ b/src/Instructions/GenericInstructions.hs @@ -8,6 +8,19 @@ import State deleteAt :: Int -> [a] -> [a] deleteAt idx xs = take idx xs <> drop 1 (drop idx xs) +combineTuple :: a -> ([a], [a]) -> [a] +combineTuple val tup = fst tup <> [val] <> snd tup + +combineTupleList :: [a] -> ([a], [a]) -> [a] +combineTupleList val tup = fst tup <> val <> snd tup + + +insertAt :: Int -> a -> [a] -> [a] +insertAt idx val xs = combineTuple val (splitAt idx xs) + +replaceAt :: Int -> a -> [a] -> [a] +replaceAt idx val xs = deleteAt (idx + 1) (insertAt idx val xs) + findSubA :: forall a. Eq a => [a] -> [a] -> Int findSubA fullA subA | length fullA < length subA = -1 @@ -50,8 +63,8 @@ takeR amt fullA = drop (length fullA - amt) fullA dropR :: Int -> [a] -> [a] dropR amt fullA = take (length fullA - amt) fullA -combineTuple :: a -> ([a], [a]) -> [a] -combineTuple val tup = fst tup <> [val] <> snd tup +absNum :: Integral a => a -> [b] -> Int +absNum rawNum lst = abs (fromIntegral rawNum) `mod` length lst notEmptyStack :: State -> Lens' State [a] -> Bool notEmptyStack state accessor = not . null $ view accessor state diff --git a/src/Instructions/StringInstructions.hs b/src/Instructions/StringInstructions.hs index 2dbbd63..171007c 100644 --- a/src/Instructions/StringInstructions.hs +++ b/src/Instructions/StringInstructions.hs @@ -3,12 +3,25 @@ module Instructions.StringInstructions where import State import Instructions.GenericInstructions import Data.List.Split +import Control.Lens -absNum :: Integral a => a -> [b] -> Int -absNum rawNum lst = abs (fromIntegral rawNum) `mod` length lst +-- shamelessly stolen from https://hackage.haskell.org/package/MissingH-1.6.0.1/docs/src/Data.String.Utils.html#strip +wschars :: String +wschars = " \t\r\n" -combineString :: String -> (String, String) -> String -combineString toInsert (front, back) = front <> toInsert <> back +strip :: String -> String +strip = lstrip . rstrip + +lstrip :: String -> String +lstrip s = case s of + [] -> [] + (x:xs) -> if x `elem` wschars + then lstrip xs + else s + +-- this is a tad inefficient init +rstrip :: String -> String +rstrip = reverse . lstrip . reverse instructionStringConcat :: State -> State instructionStringConcat state = instructionConcat state string @@ -17,7 +30,7 @@ instructionStringSwap :: State -> State instructionStringSwap state = instructionSwap state string instructionStringInsertString :: State -> State -instructionStringInsertString state@(State{_string = s1 : s2 : ss, _int = i1 : is}) = state {_string = combineString s2 (splitAt i1 s1) : ss, _int = is} +instructionStringInsertString state@(State{_string = s1 : s2 : ss, _int = i1 : is}) = state {_string = combineTupleList s2 (splitAt i1 s1) : ss, _int = is} instructionStringInsertString state = state instructionStringFromFirstChar :: State -> State @@ -35,9 +48,6 @@ instructionStringFromNthChar :: State -> State instructionStringFromNthChar state@(State {_string = s1 : ss, _int = i1 : is}) = state{_string = [s1 !! absNum i1 s1] : ss, _int = is} instructionStringFromNthChar state = state --- instructionStringContainsString :: State -> State --- instructionStringContainsString state@(State ) - instructionStringIndexOfString :: State -> State instructionStringIndexOfString state@(State {_string = s1 : s2 : ss, _int = is}) = state {_string = ss, _int = findSubA s1 s2 : is} instructionStringIndexOfString state = state @@ -80,7 +90,7 @@ instructionStringOccurrencesOfString state@(State {_string = s1 : s2 : ss, _int instructionStringOccurrencesOfString state = state instructionStringInsertChar :: State -> State -instructionStringInsertChar state@(State {_string = s1 : ss, _char = c1 : cs, _int = i1 : is}) = state{_string = combineString [c1] (splitAt i1 s1) : ss, _char = cs, _int = is} +instructionStringInsertChar state@(State {_string = s1 : ss, _char = c1 : cs, _int = i1 : is}) = state{_string = combineTuple c1 (splitAt i1 s1) : ss, _char = cs, _int = is} instructionStringInsertChar state = state instructionStringContainsChar :: State -> State @@ -172,3 +182,31 @@ instructionStringIsEmptyString state = state instructionStringRemoveNth :: State -> State instructionStringRemoveNth state@(State {_string = s1 : ss, _int = i1 : is}) = state{_string = deleteAt (absNum i1 s1) s1 : ss, _int = is} instructionStringRemoveNth state = state + +instructionStringSetNth :: State -> State +instructionStringSetNth state@(State {_string = s1 : ss, _char = c1 : cs, _int = i1 : is}) = state{_string = replaceAt (absNum i1 s1) c1 s1 : ss, _char = cs, _int = is} +instructionStringSetNth state = state + +instructionStringStripWhitespace :: State -> State +instructionStringStripWhitespace state@(State {_string = s1 : ss}) = state{_string = strip s1 : ss} +instructionStringStripWhitespace state = state + +-- Need to do uncons to all of the warnings in this mug +instructionStringFromLens :: Show a => State -> Lens' State [a] -> State +instructionStringFromLens state@(State {_string = ss}) accessor = + case uncons (view accessor state) of + Nothing -> state + Just (x,_) -> state{_string = show x : ss} + +instructionStringFromBool :: State -> State +instructionStringFromBool state = instructionStringFromLens state bool + +instructionStringFromInt :: State -> State +instructionStringFromInt state = instructionStringFromLens state int + +instructionStringFromFloat :: State -> State +instructionStringFromFloat state = instructionStringFromLens state float + +instructionStringFromChar :: State -> State +instructionStringFromChar state@(State {_string = ss, _char = c1 : cs}) = state{_string = [c1] : ss, _char = cs} +instructionStringFromChar state = state From 9e50c71ec785a00082734234fc9e9df15204c19e Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sun, 26 Jan 2025 01:17:56 -0600 Subject: [PATCH 066/171] start of vectorint, bed time --- HushGP.cabal | 1 + src/Instructions/VectorIntInstructions.hs | 7 +++++++ 2 files changed, 8 insertions(+) create mode 100644 src/Instructions/VectorIntInstructions.hs diff --git a/HushGP.cabal b/HushGP.cabal index 3d8acde..36b0d17 100644 --- a/HushGP.cabal +++ b/HushGP.cabal @@ -46,6 +46,7 @@ library , Instructions.CodeInstructions , Instructions.StringInstructions , Instructions.CharInstructions + , Instructions.VectorIntInstructions -- Modules included in this library but not exported. -- other-modules: diff --git a/src/Instructions/VectorIntInstructions.hs b/src/Instructions/VectorIntInstructions.hs new file mode 100644 index 0000000..8582fe7 --- /dev/null +++ b/src/Instructions/VectorIntInstructions.hs @@ -0,0 +1,7 @@ +module Instructions.VectorIntInstructions where + +import Instructions.GenericInstructions +import State + +instructionVectorIntConcat :: State -> State +instructionVectorIntConcat state = instructionConcat state intVector From 2dd054f17e1e3765b233f39c725b5a479b1ce843 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sun, 26 Jan 2025 01:18:23 -0600 Subject: [PATCH 067/171] I should really run make format... --- src/Instructions/GenericInstructions.hs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Instructions/GenericInstructions.hs b/src/Instructions/GenericInstructions.hs index 3311a04..cb7acc7 100644 --- a/src/Instructions/GenericInstructions.hs +++ b/src/Instructions/GenericInstructions.hs @@ -14,7 +14,6 @@ combineTuple val tup = fst tup <> [val] <> snd tup combineTupleList :: [a] -> ([a], [a]) -> [a] combineTupleList val tup = fst tup <> val <> snd tup - insertAt :: Int -> a -> [a] -> [a] insertAt idx val xs = combineTuple val (splitAt idx xs) From 7c9cdb8ed8955b1f21375304a489040623bdf75c Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sun, 26 Jan 2025 20:05:05 -0600 Subject: [PATCH 068/171] cleaning these up, still have work to do --- src/Instructions/CharInstructions.hs | 9 +++++---- src/Instructions/GenericInstructions.hs | 17 ++++++++++++++--- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/Instructions/CharInstructions.hs b/src/Instructions/CharInstructions.hs index 92e34ee..714272c 100644 --- a/src/Instructions/CharInstructions.hs +++ b/src/Instructions/CharInstructions.hs @@ -2,7 +2,8 @@ module Instructions.CharInstructions where import Data.Char import State -import Instructions.GenericInstructions +import Data.List (uncons) +-- import Instructions.GenericInstructions import Instructions.StringInstructions (wschars) intToAscii :: (Integral a) => a -> Char @@ -14,9 +15,9 @@ instructionCharConcat state = state instructionCharFromFirstChar :: State -> State instructionCharFromFirstChar state@(State {_char = cs, _string = s1 : ss}) = - if not $ null s1 - then state {_char = head s1 : cs, _string = ss} - else state + case uncons s1 of + Nothing -> state + Just (x,_) -> state {_char = x : cs, _string = ss} instructionCharFromFirstChar state = state instructionCharFromLastChar :: State -> State diff --git a/src/Instructions/GenericInstructions.hs b/src/Instructions/GenericInstructions.hs index cb7acc7..50856c5 100644 --- a/src/Instructions/GenericInstructions.hs +++ b/src/Instructions/GenericInstructions.hs @@ -70,7 +70,10 @@ notEmptyStack state accessor = not . null $ view accessor state -- This head error should never happen instructionDup :: State -> Lens' State [a] -> State -instructionDup state accessor = if notEmptyStack state accessor then state & accessor .~ head (view accessor state) : view accessor state else state +instructionDup state accessor = + case uncons (view accessor state) of + Nothing -> state + Just (x,_) -> state & accessor .~ x : view accessor state instructionPop :: State -> Lens' State [a] -> State instructionPop state accessor = if notEmptyStack state accessor then state & accessor .~ drop 1 (view accessor state) else state @@ -80,7 +83,10 @@ instructionPop state accessor = if notEmptyStack state accessor then state & acc instructionDupN :: State -> Lens' State [a] -> State instructionDupN state accessor = if notEmptyStack state accessor && notEmptyStack state int - then instructionDupNHelper (head (view int state)) accessor (instructionPop state int) + then + case uncons (view int state) of + Nothing -> state -- is this redundant? + Just (x,_) -> instructionDupNHelper x accessor (instructionPop state int) else state where instructionDupNHelper :: Int -> Lens' State [a] -> State -> State @@ -118,7 +124,12 @@ instructionFlush state accessor = state & accessor .~ [] instructionEq :: forall a. Eq a => State -> Lens' State [a] -> State instructionEq state accessor = if length stackTop == 2 - then state & bool .~ (head stackTop == stackTop !! 1) : view bool state & accessor .~ drop 2 (view accessor state) + -- then state & bool .~ (head stackTop == stackTop !! 1) : view bool state & accessor .~ drop 2 (view accessor state) + then + case uncons stackTop of + Nothing -> state + Just (x1, x2 : _) -> state & bool .~ (x1 == x2) : view bool state & accessor .~ drop 2 (view accessor state) + Just _ -> state else state where stackTop :: [a] From c9923eae0235de8ef917e4879ef241542f8d6292 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Mon, 27 Jan 2025 12:17:40 -0600 Subject: [PATCH 069/171] I'm gonna make these tests states soon --- test/Main.hs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/Main.hs b/test/Main.hs index 0a40805..5463126 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -5,6 +5,7 @@ import Instructions.FloatInstructions import Instructions.IntInstructions import Instructions.StringInstructions import Instructions.CharInstructions +import Instructions.VectorIntInstructions import Push import State @@ -44,6 +45,11 @@ charTestFunc name goal genome startState = let state = loadProgram genome startState in assert (goal == _char (interpretExec state)) putStrLn (name <> " passed test.") +intVectorTestFunc :: String -> [[Int]] -> [Gene] -> State -> IO () +intVectorTestFunc name goal genome startState = + let state = loadProgram genome startState + in assert (goal == _intVector (interpretExec state)) putStrLn (name <> " passed test.") + main :: IO () main = do -- Int tests @@ -245,3 +251,6 @@ main = do boolTestFunc "instructionCharIsLetterFalse" [False] [GeneChar '1', StateFunc instructionCharIsLetter] emptyState boolTestFunc "instructionCharIsDigitTrue" [True] [GeneChar '1', StateFunc instructionCharIsDigit] emptyState boolTestFunc "instructionCharIsDigitFalse" [False] [GeneChar 'a', StateFunc instructionCharIsDigit] emptyState + + -- vector int instructions + intVectorTestFunc "instructionIntVectorConcat" [[4,5,6,1,2,3]] [GeneIntVector [1,2,3], GeneIntVector [4,5,6], StateFunc instructionIntVectorConcat] emptyState From 89287ceaedb03bab892bc40f7956ff71aeb6bdaa Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Mon, 27 Jan 2025 12:19:54 -0600 Subject: [PATCH 070/171] uncons to stop warnings --- src/Instructions/GenericInstructions.hs | 26 +++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/Instructions/GenericInstructions.hs b/src/Instructions/GenericInstructions.hs index 50856c5..44b6826 100644 --- a/src/Instructions/GenericInstructions.hs +++ b/src/Instructions/GenericInstructions.hs @@ -76,17 +76,20 @@ instructionDup state accessor = Just (x,_) -> state & accessor .~ x : view accessor state instructionPop :: State -> Lens' State [a] -> State -instructionPop state accessor = if notEmptyStack state accessor then state & accessor .~ drop 1 (view accessor state) else state +instructionPop state accessor = state & accessor .~ drop 1 (view accessor state) + +-- instructionPop :: State -> Lens' State [a] -> State +-- instructionPop state accessor = if notEmptyStack state accessor then instructionPop state accessor else state -- I might be able to move some of the int stack error checking -- to the integer call. For now this may be a tad inefficient. instructionDupN :: State -> Lens' State [a] -> State instructionDupN state accessor = - if notEmptyStack state accessor && notEmptyStack state int + if notEmptyStack state accessor then - case uncons (view int state) of - Nothing -> state -- is this redundant? - Just (x,_) -> instructionDupNHelper x accessor (instructionPop state int) + case uncons (view int state) of + Just (i1,_) -> instructionDupNHelper i1 accessor (instructionPop state int) + _ -> state else state where instructionDupNHelper :: Int -> Lens' State [a] -> State -> State @@ -165,9 +168,9 @@ instructionYank state _ = state -- Rewrite this eventually? instructionShoveDup :: State -> Lens' State [a] -> State instructionShoveDup state@(State {_int = i : is}) accessor = - if notEmptyStack state accessor - then (state & accessor .~ combineTuple (head $ view accessor state) (splitAt (max 0 (min i (length (view accessor state) - 1))) (view accessor state))) {_int = is} - else state + case uncons (view accessor state) of + Just (x,_) -> (state & accessor .~ combineTuple x (splitAt (max 0 (min i (length (view accessor state) - 1))) (view accessor state))) {_int = is} + _ -> state instructionShoveDup state@(State {_int = []}) _ = state -- also also not int generic @@ -177,10 +180,9 @@ instructionShove state accessor = instructionShoveDup state accessor & accessor -- not char generic instructionConcat :: Semigroup a => State -> Lens' State [a] -> State instructionConcat state accessor = - if (length . take 2 $ view accessor state) == 2 - then droppedState & accessor .~ (head (view accessor state) <> view accessor state !! 1) : view accessor droppedState - -- then undefined - else state + case uncons (view accessor state) of + Just (x1, x2:_) -> droppedState & accessor .~ (x1 <> x2) : view accessor droppedState + _ -> state where droppedState :: State droppedState = state & accessor .~ drop 2 (view accessor state) From 7e7735ae95661bbbc53ee27a394c40bb9a408f45 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Mon, 27 Jan 2025 12:20:23 -0600 Subject: [PATCH 071/171] switch Int and Vector --- src/Instructions/VectorIntInstructions.hs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Instructions/VectorIntInstructions.hs b/src/Instructions/VectorIntInstructions.hs index 8582fe7..1a07d44 100644 --- a/src/Instructions/VectorIntInstructions.hs +++ b/src/Instructions/VectorIntInstructions.hs @@ -3,5 +3,5 @@ module Instructions.VectorIntInstructions where import Instructions.GenericInstructions import State -instructionVectorIntConcat :: State -> State -instructionVectorIntConcat state = instructionConcat state intVector +instructionIntVectorConcat :: State -> State +instructionIntVectorConcat state = instructionConcat state intVector From ef5ce0aaf0d9052a1a6b15610961cdcb83bd1db5 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Mon, 27 Jan 2025 12:57:23 -0600 Subject: [PATCH 072/171] formatting --- test/Main.hs | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/test/Main.hs b/test/Main.hs index 5463126..e8c74cd 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -1,10 +1,10 @@ import Control.Exception (assert) +import Instructions.CharInstructions import Instructions.CodeInstructions import Instructions.ExecInstructions import Instructions.FloatInstructions import Instructions.IntInstructions import Instructions.StringInstructions -import Instructions.CharInstructions import Instructions.VectorIntInstructions import Push import State @@ -142,11 +142,13 @@ main = do codeTestFunc "instructionCodeExtractBlock" [Block [GeneInt 2, GeneInt 3]] [StateFunc instructionCodeFromExec, Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], Block []], GeneInt 1, StateFunc instructionCodeExtract] emptyState codeTestFunc "instructionCodeExtractEdgeCase" [Block []] [StateFunc instructionCodeFromExec, Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], Block []], GeneInt 7, StateFunc instructionCodeExtract] emptyState codeTestFunc "instructionCodeExtractNotBlock" [GeneInt 2] [StateFunc instructionCodeFromExec, GeneInt 2, GeneInt 56, StateFunc instructionCodeExtract] emptyState - codeTestFunc "instructionCodeInsertInBounds" + codeTestFunc + "instructionCodeInsertInBounds" [Block [GeneInt 1, Block [GeneInt 2, GeneInt 9999, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6, Block [GeneInt 7, GeneInt 8], GeneInt 9]] [StateFunc instructionCodeFromExec, GeneInt 9999, StateFunc instructionCodeFromExec, Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6, Block [GeneInt 7, GeneInt 8], GeneInt 9], GeneInt 3, StateFunc instructionCodeInsert] emptyState - codeTestFunc "instructionCodeInsertOutBounds" + codeTestFunc + "instructionCodeInsertOutBounds" [Block [GeneInt 1, Block [GeneInt 2, GeneInt 9999, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6, Block [GeneInt 7, GeneInt 8], GeneInt 9]] [StateFunc instructionCodeFromExec, GeneInt 9999, StateFunc instructionCodeFromExec, Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6, Block [GeneInt 7, GeneInt 8], GeneInt 9], GeneInt 15, StateFunc instructionCodeInsert] emptyState @@ -183,9 +185,9 @@ main = do stringTestFunc "instructionStringRemoveNStringFail" ["this is a sentence i"] [GeneString "z", GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringRemoveNString] emptyState stringTestFunc "instructionStringRemoveAllStringSuccess" ["ths s a sentence "] [GeneString "i", GeneString "this is a sentence i", StateFunc instructionStringRemoveAllString] emptyState stringTestFunc "instructionStringRemoveAllStringFail" ["this is a sentence i"] [GeneString "z", GeneString "this is a sentence i", StateFunc instructionStringRemoveAllString] emptyState - intTestFunc "instructionStringOccurrencesOfString3" [3] [GeneString "i", GeneString "this is a sentence i", StateFunc instructionStringOccurrencesOfString] emptyState - intTestFunc "instructionStringOccurrencesOfString3" [2] [GeneString "is", GeneString "this is a sentence i", StateFunc instructionStringOccurrencesOfString] emptyState - intTestFunc "instructionStringOccurrencesOfString0" [0] [GeneString "z", GeneString "this is a sentence i", StateFunc instructionStringOccurrencesOfString] emptyState + intTestFunc "instructionStringOccurrencesOfString3" [3] [GeneString "i", GeneString "this is a sentence i", StateFunc instructionStringOccurrencesOfString] emptyState + intTestFunc "instructionStringOccurrencesOfString3" [2] [GeneString "is", GeneString "this is a sentence i", StateFunc instructionStringOccurrencesOfString] emptyState + intTestFunc "instructionStringOccurrencesOfString0" [0] [GeneString "z", GeneString "this is a sentence i", StateFunc instructionStringOccurrencesOfString] emptyState stringTestFunc "instructionStringInsertChar" ["123Zabc"] [GeneString "abc", GeneString "123", StateFunc instructionStringConcat, GeneChar 'Z', GeneInt 3, StateFunc instructionStringInsertChar] emptyState boolTestFunc "instructionStringContainsCharTrue" [True] [GeneString "abc", GeneChar 'a', StateFunc instructionStringContainsChar] emptyState boolTestFunc "instructionStringContainsCharFalse" [False] [GeneString "abc", GeneChar 'z', StateFunc instructionStringContainsChar] emptyState @@ -205,8 +207,8 @@ main = do stringTestFunc "instructionStringRemoveNCharFail" ["this is a sentence i"] [GeneChar 'z', GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringRemoveNChar] emptyState stringTestFunc "instructionStringRemoveAllCharSuccess" ["ths s a sentence "] [GeneChar 'i', GeneString "this is a sentence i", StateFunc instructionStringRemoveAllChar] emptyState stringTestFunc "instructionStringRemoveAllCharFail" ["this is a sentence i"] [GeneChar 'z', GeneString "this is a sentence i", StateFunc instructionStringRemoveAllChar] emptyState - intTestFunc "instructionStringOccurrencesOfChar3" [3] [GeneChar 'i', GeneString "this is a sentence i", StateFunc instructionStringOccurrencesOfChar] emptyState - intTestFunc "instructionStringOccurrencesOfChar0" [0] [GeneChar 'z', GeneString "this is a sentence i", StateFunc instructionStringOccurrencesOfChar] emptyState + intTestFunc "instructionStringOccurrencesOfChar3" [3] [GeneChar 'i', GeneString "this is a sentence i", StateFunc instructionStringOccurrencesOfChar] emptyState + intTestFunc "instructionStringOccurrencesOfChar0" [0] [GeneChar 'z', GeneString "this is a sentence i", StateFunc instructionStringOccurrencesOfChar] emptyState stringTestFunc "instructionStringReverse" ["321cba"] [GeneString "abc123", StateFunc instructionStringReverse] emptyState stringTestFunc "instructionStringHead3" ["abc"] [GeneString "abc123", GeneInt 3, StateFunc instructionStringHead] emptyState stringTestFunc "instructionStringHead0" [""] [GeneString "abc123", GeneInt 0, StateFunc instructionStringHead] emptyState @@ -236,7 +238,7 @@ main = do stringTestFunc "instructionStringFromChar" [" "] [GeneChar ' ', StateFunc instructionStringFromChar] emptyState -- char instructions - stringTestFunc "instructionCharConcat" ["ab"] [GeneChar 'b', GeneChar 'a', StateFunc instructionCharConcat] emptyState + stringTestFunc "instructionCharConcat" ["ab"] [GeneChar 'b', GeneChar 'a', StateFunc instructionCharConcat] emptyState charTestFunc "instructionCharFromFirstCharSuccess" ['a'] [GeneString "abc123", StateFunc instructionCharFromFirstChar] emptyState charTestFunc "instructionCharFromFirstCharFail" [] [GeneString "", StateFunc instructionCharFromFirstChar] emptyState charTestFunc "instructionCharFromLastCharSuccess" ['3'] [GeneString "abc123", StateFunc instructionCharFromLastChar] emptyState @@ -253,4 +255,4 @@ main = do boolTestFunc "instructionCharIsDigitFalse" [False] [GeneChar 'a', StateFunc instructionCharIsDigit] emptyState -- vector int instructions - intVectorTestFunc "instructionIntVectorConcat" [[4,5,6,1,2,3]] [GeneIntVector [1,2,3], GeneIntVector [4,5,6], StateFunc instructionIntVectorConcat] emptyState + intVectorTestFunc "instructionIntVectorConcat" [[4, 5, 6, 1, 2, 3]] [GeneIntVector [1, 2, 3], GeneIntVector [4, 5, 6], StateFunc instructionIntVectorConcat] emptyState From 035a7a2f10891f41eefe64b872bbd9951c30a436 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Mon, 27 Jan 2025 15:59:20 -0600 Subject: [PATCH 073/171] add problem to readme --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index d2a573d..78db742 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,15 @@ the functions in the pyshgp list. https://erp12.github.io/pyshgp/html/core_instructions.html +# Big Problem + +There is no easy way to determine equality of two functions in Haskell. No comparing names, no nothing. +We coult compare applying two functions to an example state, but that would get tedious and costly quickly. + +The only idea floating in my head at the moment is to attach a string to the `StateFunc` Gene to +track what the functions are. This would require a painful redefinition of the tests, but I think would be +worth it in the grand scheme. Would mean we could also track the functions when outputting them after training. + ## Tasks * [ ] Post minimal core of exec to haskell discourse for advice about speed optimization. * [x] Do test-driven development on this one. From 363425b39b8c5ac39eeca7db7f3cfb97fcec7659 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Tue, 28 Jan 2025 22:09:30 -0600 Subject: [PATCH 074/171] a lot of genericization and updates --- src/Instructions/CharInstructions.hs | 24 +-- src/Instructions/GenericInstructions.hs | 172 ++++++++++++++++++---- src/Instructions/StringInstructions.hs | 39 ++--- src/Instructions/VectorIntInstructions.hs | 48 ++++++ src/LearnLens.hs | 30 ++++ src/Push.hs | 2 - test/Main.hs | 37 ++++- 7 files changed, 275 insertions(+), 77 deletions(-) create mode 100644 src/LearnLens.hs diff --git a/src/Instructions/CharInstructions.hs b/src/Instructions/CharInstructions.hs index 714272c..7d4d94d 100644 --- a/src/Instructions/CharInstructions.hs +++ b/src/Instructions/CharInstructions.hs @@ -2,11 +2,10 @@ module Instructions.CharInstructions where import Data.Char import State -import Data.List (uncons) --- import Instructions.GenericInstructions import Instructions.StringInstructions (wschars) +import Instructions.GenericInstructions -intToAscii :: (Integral a) => a -> Char +intToAscii :: Integral a => a -> Char intToAscii val = chr (abs (fromIntegral val) `mod` 128) instructionCharConcat :: State -> State @@ -14,26 +13,13 @@ instructionCharConcat state@(State {_char = c1 : c2 : cs, _string = ss}) = state instructionCharConcat state = state instructionCharFromFirstChar :: State -> State -instructionCharFromFirstChar state@(State {_char = cs, _string = s1 : ss}) = - case uncons s1 of - Nothing -> state - Just (x,_) -> state {_char = x : cs, _string = ss} -instructionCharFromFirstChar state = state +instructionCharFromFirstChar state = instructionVectorFirst state char string instructionCharFromLastChar :: State -> State -instructionCharFromLastChar state@(State {_char = cs, _string = s1 : ss}) = - if not $ null s1 - then state {_char = last s1 : cs, _string = ss} - else state -instructionCharFromLastChar state = state +instructionCharFromLastChar state = instructionVectorLast state char string instructionCharFromNthChar :: State -> State -instructionCharFromNthChar state@(State {_char = cs, _string = s1 : ss, _int = i1 : is}) = - let - index = abs i1 `mod` length s1 - in - state{_char = s1 !! index : cs, _string = ss, _int = is} -instructionCharFromNthChar state = state +instructionCharFromNthChar state = instructionVectorNth state char string instructionCharIsWhitespace :: State -> State instructionCharIsWhitespace state@(State {_char = c1 : cs, _bool = bs}) = state{_char = cs, _bool = (c1 `elem` wschars) : bs} diff --git a/src/Instructions/GenericInstructions.hs b/src/Instructions/GenericInstructions.hs index 44b6826..73ed59d 100644 --- a/src/Instructions/GenericInstructions.hs +++ b/src/Instructions/GenericInstructions.hs @@ -20,6 +20,15 @@ insertAt idx val xs = combineTuple val (splitAt idx xs) replaceAt :: Int -> a -> [a] -> [a] replaceAt idx val xs = deleteAt (idx + 1) (insertAt idx val xs) +subList :: Int -> Int -> [a] -> [a] +subList idx0 idx1 xs = + let + (start, end) = if idx0 <= idx1 then (idx0, idx1) else (idx1, idx0) + adjStart = max 0 start + adjEnd = min end (length xs) + in + take adjEnd (drop adjStart xs) + findSubA :: forall a. Eq a => [a] -> [a] -> Int findSubA fullA subA | length fullA < length subA = -1 @@ -62,13 +71,16 @@ takeR amt fullA = drop (length fullA - amt) fullA dropR :: Int -> [a] -> [a] dropR amt fullA = take (length fullA - amt) fullA +safeInit :: [a] -> [a] +safeInit [] = [] +safeInit xs = init xs + absNum :: Integral a => a -> [b] -> Int absNum rawNum lst = abs (fromIntegral rawNum) `mod` length lst notEmptyStack :: State -> Lens' State [a] -> Bool notEmptyStack state accessor = not . null $ view accessor state --- This head error should never happen instructionDup :: State -> Lens' State [a] -> State instructionDup state accessor = case uncons (view accessor state) of @@ -83,26 +95,24 @@ instructionPop state accessor = state & accessor .~ drop 1 (view accessor state) -- I might be able to move some of the int stack error checking -- to the integer call. For now this may be a tad inefficient. -instructionDupN :: State -> Lens' State [a] -> State +instructionDupN :: forall a. State -> Lens' State [a] -> State instructionDupN state accessor = - if notEmptyStack state accessor - then - case uncons (view int state) of - Just (i1,_) -> instructionDupNHelper i1 accessor (instructionPop state int) - _ -> state - else state + case uncons (view int state) of + Just (i1,is) -> + case uncons (view accessor state{_int = is}) of + Just (a1,as) -> instructionDupNHelper i1 a1 accessor (state{_int = is} & accessor .~ as) + _ -> state + _ -> state where - instructionDupNHelper :: Int -> Lens' State [a] -> State -> State - instructionDupNHelper count internalAccessor internalState = - if count > 1 && notEmptyStack internalState int - then instructionDupNHelper (count - 1) internalAccessor (instructionDup internalState accessor) + instructionDupNHelper :: Int -> a -> Lens' State [a] -> State -> State + instructionDupNHelper count instruction internalAccessor internalState = + if count > 0 + then instructionDupNHelper (count - 1) instruction internalAccessor (internalState & accessor .~ (instruction : view accessor internalState)) else internalState instructionSwap :: State -> Lens' State [a] -> State instructionSwap state accessor = - if (length . take 2 $ view accessor state) == 2 - then state & accessor .~ swapper (view accessor state) - else state + state & accessor .~ swapper (view accessor state) where swapper :: [a] -> [a] swapper (x1 : x2 : xs) = x2 : x1 : xs @@ -113,9 +123,7 @@ instructionSwap state accessor = -- an instruction later. Template haskell seems very complicated tho. instructionRot :: State -> Lens' State [a] -> State instructionRot state accessor = - if (length . take 3 $ view accessor state) == 3 - then state & accessor .~ rotator (view accessor state) - else state + state & accessor .~ rotator (view accessor state) where rotator :: [a] -> [a] rotator (x1 : x2 : x3 : xs) = x3 : x1 : x2 : xs @@ -126,20 +134,17 @@ instructionFlush state accessor = state & accessor .~ [] instructionEq :: forall a. Eq a => State -> Lens' State [a] -> State instructionEq state accessor = - if length stackTop == 2 - -- then state & bool .~ (head stackTop == stackTop !! 1) : view bool state & accessor .~ drop 2 (view accessor state) - then - case uncons stackTop of - Nothing -> state - Just (x1, x2 : _) -> state & bool .~ (x1 == x2) : view bool state & accessor .~ drop 2 (view accessor state) - Just _ -> state - else state + case uncons stackTop of + Nothing -> state + Just (x1, x2 : _) -> state & bool .~ (x1 == x2) : view bool state & accessor .~ drop 2 (view accessor state) + Just _ -> state where stackTop :: [a] stackTop = take 2 $ view accessor state instructionStackDepth :: State -> Lens' State [a] -> State -instructionStackDepth state accessor = state & int .~ (length (view accessor state) : view int state) +-- instructionStackDepth state accessor = state & int .~ (length (view accessor state) : view int state) +instructionStackDepth state@(State {_int = is}) accessor = state{_int = length (view accessor state) : is} -- Will have a non-generic definition for the int stack instructionYankDup :: State -> Lens' State [a] -> State @@ -190,3 +195,116 @@ instructionConcat state accessor = -- evolve fodder??????????? instructionNoOp :: State -> State instructionNoOp state = state + +instructionConj :: State -> Lens' State [a] -> Lens' State [[a]] -> State +instructionConj state primAccessor vectorAccessor = + case (uncons (view primAccessor state), uncons (view vectorAccessor state)) of + (Just (p1,ps), Just (v1,vs)) -> state & primAccessor .~ ps & vectorAccessor .~ ((p1 : v1) : vs) + _ -> state + +-- v for vector, vs for vectorstack (also applicable to strings) +-- Could abstract this unconsing even further +instructionTakeN :: State -> Lens' State [[a]] -> State +instructionTakeN state@(State {_int = i1 : is}) accessor = + case uncons (view accessor state) of + Just (v1, vs) -> state{_int = is} & accessor .~ (take (absNum i1 v1) v1 : vs) + _ -> state +instructionTakeN state _ = state + +instructionSubVector :: State -> Lens' State [[a]] -> State +instructionSubVector state@(State {_int = i1 : i2 : is}) accessor = + case uncons (view accessor state) of + Just (v1, vs) -> state{_int = is} & accessor .~ (subList i1 i2 v1 : vs) + _ -> state +instructionSubVector state _ = state + +instructionVectorFirst :: State -> Lens' State [a] -> Lens' State [[a]] -> State +instructionVectorFirst state primAccessor vectorAccessor = + case uncons (view vectorAccessor state) of + Just (v1, vs) -> + case uncons v1 of + Just (vp1, _) -> state & primAccessor .~ (vp1 : view primAccessor state) & vectorAccessor .~ vs + _ -> state + _ -> state + +instructionVectorLast :: State -> Lens' State [a] -> Lens' State [[a]] -> State +instructionVectorLast state primAccessor vectorAccessor = + case uncons (view vectorAccessor state) of + Just (v1, vs) -> + case uncons (drop (length v1 - 1) v1) of -- gonna keep this implementation over using last as this can't error + Just (vplast, _) -> state & primAccessor .~ (vplast : view primAccessor state) & vectorAccessor .~ vs + _ -> state + _ -> state + +instructionVectorNth :: State -> Lens' State [a] -> Lens' State [[a]] -> State +instructionVectorNth state@(State {_int = i1 : is}) primAccessor vectorAccessor = + case uncons (view vectorAccessor state) of + Just (v1, vs) -> state{_int = is} & primAccessor .~ (v1 !! absNum i1 v1 : view primAccessor state{_int = is}) & vectorAccessor .~ vs + _ -> state +instructionVectorNth state _ _ = state + +instructionRest :: State -> Lens' State [[a]] -> State +instructionRest state accessor = + case uncons (view accessor state) of + Just (v1, vs) -> state & accessor .~ (drop 1 v1 : vs) + _ -> state + +instructionButLast :: State -> Lens' State [[a]] -> State +instructionButLast state accessor = + case uncons (view accessor state) of + Just (v1, vs) -> state & accessor .~ (safeInit v1 : vs) + _ -> state + +instructionLength :: State -> Lens' State [[a]] -> State +instructionLength state@(State {_int = is}) accessor = + case uncons (view accessor state) of + Just (v1, vs) -> state{_int = length v1 : is} & accessor .~ vs + _ -> state + +instructionReverse :: State -> Lens' State [[a]] -> State +instructionReverse state accessor = + case uncons (view accessor state) of + Just (v1, vs) -> state & accessor .~ (reverse v1 : vs) + _ -> state + +instructionPushAll :: State -> Lens' State [a] -> Lens' State [[a]] -> State +instructionPushAll state primAccessor vectorAccessor = + case uncons (view vectorAccessor state) of + Just (v1, vs) -> state & vectorAccessor .~ vs & primAccessor .~ (v1 <> view primAccessor state) + _ -> state + +instructionVectorMakeEmpty :: State -> Lens' State [[a]] -> State +instructionVectorMakeEmpty state accessor = state & accessor .~ ([] : view accessor state) + +instructionVectorIsEmpty :: State -> Lens' State [[a]] -> State +instructionVectorIsEmpty state@(State {_bool = bs}) accessor = + case uncons (view accessor state) of + Just (v1, vs) -> state{_bool = null v1 : bs} & accessor .~ vs + _ -> state + +instructionVectorContains :: Eq a => State -> Lens' State [a] -> Lens' State [[a]] -> State +instructionVectorContains state@(State {_bool = bs}) primAccessor vectorAccessor = + case (uncons (view vectorAccessor state), uncons (view primAccessor state)) of + (Just (v1, vs), Just (p1, ps)) -> state{_bool = (findSubA v1 [p1] /= -1) : bs} & vectorAccessor .~ vs & primAccessor .~ ps + _ -> state + +-- I couldn't think of a better way of doing this +instructionVectorIndexOf :: Eq a => State -> Lens' State [a] -> Lens' State [[a]] -> State +instructionVectorIndexOf state primAccessor vectorAccessor = + case (uncons (view vectorAccessor state), uncons (view primAccessor state)) of + (Just (v1, vs), Just (p1, ps)) -> (state & vectorAccessor .~ vs & primAccessor .~ ps) & int .~ (findSubA v1 [p1] : view int (state & vectorAccessor .~ vs & primAccessor .~ ps)) + _ -> state + +instructionVectorOccurrencesOf :: Eq a => State -> Lens' State [a] -> Lens' State [[a]] -> State +instructionVectorOccurrencesOf state primAccessor vectorAccessor = + case (uncons (view vectorAccessor state), uncons (view primAccessor state)) of + (Just (v1, vs), Just (p1, ps)) -> (state & vectorAccessor .~ vs & primAccessor .~ ps) & int .~ (amtOccurences v1 [p1] : view int (state & vectorAccessor .~ vs & primAccessor .~ ps)) + _ -> state + +instructionVectorSetNth :: State -> Lens' State [a] -> Lens' State [[a]] -> State +instructionVectorSetNth state@(State {_int = i1 : is}) primAccessor vectorAccessor = + case (uncons (view vectorAccessor state{_int = is}), uncons (view primAccessor state{_int = is})) of + (Just (v1, vs), Just (p1, ps)) -> state{_int = is} & vectorAccessor .~ (replaceAt (absNum i1 v1) p1 v1 : vs) & primAccessor .~ ps + _ -> state +instructionVectorSetNth state _ _ = state + diff --git a/src/Instructions/StringInstructions.hs b/src/Instructions/StringInstructions.hs index 171007c..ceb47db 100644 --- a/src/Instructions/StringInstructions.hs +++ b/src/Instructions/StringInstructions.hs @@ -94,12 +94,12 @@ instructionStringInsertChar state@(State {_string = s1 : ss, _char = c1 : cs, _i instructionStringInsertChar state = state instructionStringContainsChar :: State -> State -instructionStringContainsChar state@(State {_string = s1 : ss, _char = c1 : cs, _bool = bs}) = state{_string = ss, _char = cs, _bool = (findSubA s1 [c1] /= -1) : bs} -instructionStringContainsChar state = state +-- instructionStringContainsChar state@(State {_string = s1 : ss, _char = c1 : cs, _bool = bs}) = state{_string = ss, _char = cs, _bool = (findSubA s1 [c1] /= -1) : bs} +-- instructionStringContainsChar state = state +instructionStringContainsChar state = instructionVectorContains state char string instructionStringIndexOfChar :: State -> State -instructionStringIndexOfChar state@(State {_string = s1 : ss, _char = c1 : cs, _int = is}) = state{_string = ss, _char = cs, _int = findSubA s1 [c1] : is} -instructionStringIndexOfChar state = state +instructionStringIndexOfChar state = instructionVectorIndexOf state char string instructionStringSplitOnChar :: State -> State instructionStringSplitOnChar state@(State {_string = s1 : ss, _char = c1 : cs}) = state {_string = reverse $ splitOn [c1] s1 <> ss, _char = cs} @@ -130,35 +130,26 @@ instructionStringRemoveAllChar state@(State {_string = s1 : ss, _char = c1 : cs} instructionStringRemoveAllChar state = state instructionStringOccurrencesOfChar :: State -> State -instructionStringOccurrencesOfChar state@(State {_string = s1 : ss, _char = c1 : cs, _int = is}) = state{_string = ss, _char = cs, _int = amtOccurences s1 [c1] : is} -instructionStringOccurrencesOfChar state = state +instructionStringOccurrencesOfChar state = instructionVectorOccurrencesOf state char string instructionStringReverse :: State -> State -instructionStringReverse state@(State {_string = s1 : ss}) = state{_string = reverse s1 : ss} -instructionStringReverse state = state +instructionStringReverse state = instructionReverse state string instructionStringHead :: State -> State -instructionStringHead state@(State {_string = s1 : ss, _int = i1 : is}) = state{_string = take (absNum i1 s1) s1 : ss, _int = is} -instructionStringHead state = state +instructionStringHead state = instructionTakeN state string instructionStringTail :: State -> State instructionStringTail state@(State {_string = s1 : ss, _int = i1 : is}) = state{_string = takeR (absNum i1 s1) s1 : ss, _int = is} instructionStringTail state = state instructionStringAppendChar :: State -> State -instructionStringAppendChar state@(State {_string = s1 : ss, _char = c1 : cs}) = state{_string = (c1 : s1) : ss, _char = cs} -instructionStringAppendChar state = state +instructionStringAppendChar state = instructionConj state char string instructionStringRest :: State -> State -instructionStringRest state@(State {_string = s1 : ss}) = state{_string = drop 1 s1 : ss} -instructionStringRest state = state +instructionStringRest state = instructionRest state string instructionStringButLast :: State -> State -instructionStringButLast state@(State {_string = s1 : ss}) = - if not $ null s1 - then state{_string = init s1 : ss} - else state -instructionStringButLast state = state +instructionStringButLast state = instructionButLast state string instructionStringDrop :: State -> State instructionStringDrop state@(State {_string = s1 : ss, _int = i1 : is}) = state{_string = drop (absNum i1 s1) s1 : ss, _int = is} @@ -169,11 +160,10 @@ instructionStringButLastN state@(State {_string = s1 : ss, _int = i1 : is}) = st instructionStringButLastN state = state instructionStringLength :: State -> State -instructionStringLength state@(State {_string = s1 : ss, _int = is}) = state{_string = ss, _int = length s1 : is} -instructionStringLength state = state +instructionStringLength state = instructionLength state string instructionStringMakeEmpty :: State -> State -instructionStringMakeEmpty state@(State {_string = ss}) = state{_string = "" : ss} +instructionStringMakeEmpty state = instructionVectorMakeEmpty state string instructionStringIsEmptyString :: State -> State instructionStringIsEmptyString state@(State {_string = s1 : ss, _bool = bs}) = state{_string = ss, _bool = null s1 : bs} @@ -184,8 +174,9 @@ instructionStringRemoveNth state@(State {_string = s1 : ss, _int = i1 : is}) = s instructionStringRemoveNth state = state instructionStringSetNth :: State -> State -instructionStringSetNth state@(State {_string = s1 : ss, _char = c1 : cs, _int = i1 : is}) = state{_string = replaceAt (absNum i1 s1) c1 s1 : ss, _char = cs, _int = is} -instructionStringSetNth state = state +-- instructionStringSetNth state@(State {_string = s1 : ss, _char = c1 : cs, _int = i1 : is}) = state{_string = replaceAt (absNum i1 s1) c1 s1 : ss, _char = cs, _int = is} +-- instructionStringSetNth state = state +instructionStringSetNth state = instructionVectorSetNth state char string instructionStringStripWhitespace :: State -> State instructionStringStripWhitespace state@(State {_string = s1 : ss}) = state{_string = strip s1 : ss} diff --git a/src/Instructions/VectorIntInstructions.hs b/src/Instructions/VectorIntInstructions.hs index 1a07d44..b8caf0c 100644 --- a/src/Instructions/VectorIntInstructions.hs +++ b/src/Instructions/VectorIntInstructions.hs @@ -5,3 +5,51 @@ import State instructionIntVectorConcat :: State -> State instructionIntVectorConcat state = instructionConcat state intVector + +instructionIntVectorConj :: State -> State +instructionIntVectorConj state = instructionConj state int intVector + +instructionIntVectorTakeN :: State -> State +instructionIntVectorTakeN state = instructionTakeN state intVector + +instructionIntVectorSubVector :: State -> State +instructionIntVectorSubVector state = instructionSubVector state intVector + +instructionIntVectorFirst :: State -> State +instructionIntVectorFirst state = instructionVectorFirst state int intVector + +instructionIntVectorLast :: State -> State +instructionIntVectorLast state = instructionVectorLast state int intVector + +instructionIntVectorNth :: State -> State +instructionIntVectorNth state = instructionVectorNth state int intVector + +instructionIntVectorRest :: State -> State +instructionIntVectorRest state = instructionRest state intVector + +instructionIntVectorButLast :: State -> State +instructionIntVectorButLast state = instructionButLast state intVector + +instructionIntVectorLength :: State -> State +instructionIntVectorLength state = instructionLength state intVector + +instructionIntVectorReverse :: State -> State +instructionIntVectorReverse state = instructionReverse state intVector + +instructionIntVectorPushAll :: State -> State +instructionIntVectorPushAll state = instructionPushAll state int intVector + +instructionIntVectorMakeEmpty :: State -> State +instructionIntVectorMakeEmpty state = instructionVectorMakeEmpty state intVector + +instructionIntVectorIsEmpty :: State -> State +instructionIntVectorIsEmpty state = instructionVectorIsEmpty state intVector + +instructionIntVectorIndexOf :: State -> State +instructionIntVectorIndexOf state = instructionVectorIndexOf state int intVector + +instructionIntVectorOccurrencesOf :: State -> State +instructionIntVectorOccurrencesOf state = instructionVectorOccurrencesOf state int intVector + +instructionIntVectorSetNth :: State -> State +instructionIntVectorSetNth state = instructionVectorSetNth state int intVector diff --git a/src/LearnLens.hs b/src/LearnLens.hs new file mode 100644 index 0000000..fedd2ab --- /dev/null +++ b/src/LearnLens.hs @@ -0,0 +1,30 @@ +{-# LANGUAGE TemplateHaskell #-} + +module LearnLens where + +import Control.Lens hiding (element) +import Control.Lens.TH + +data Atom = Atom {_element :: String, _point :: Point} deriving (Show) + +data Point = Point {_x :: Double, _Y :: Double} deriving (Show) + +$(makeLenses ''Atom) +$(makeLenses ''Point) + +myAtom :: Atom +myAtom = Atom "climberite" (Point 4.0 3.2) + +shiftAtom :: Atom -> Atom +shiftAtom = over (point . x) (+ 1) + +data Molecule = Molecule {_atoms :: [Atom]} deriving (Show) + +$(makeLenses ''Molecule) + +shiftMolecule :: Molecule -> Molecule +shiftMolecule = over (atoms . traverse . point . x) (+ 1) + +-- Example without template haskell +defPoint :: Lens' Atom Point +defPoint = lens _point (\atom newPoint -> atom {_point = newPoint}) diff --git a/src/Push.hs b/src/Push.hs index 70e8d9a..879b6ca 100644 --- a/src/Push.hs +++ b/src/Push.hs @@ -1,5 +1,3 @@ -{-# LANGUAGE RecordWildCards #-} - module Push where import Control.Lens diff --git a/test/Main.hs b/test/Main.hs index e8c74cd..92afc36 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -11,9 +11,8 @@ import State -- import Debug.Trace --- @TODO: Finish int and float tests - -- TODO: Need a function that can compare states. +-- May look at quickCheck later intTestFunc :: String -> [Int] -> [Gene] -> State -> IO () intTestFunc name goal genome startState = @@ -61,7 +60,8 @@ main = do intTestFunc "instructionIntMod" [3] [GeneInt 13, GeneInt 5, StateFunc instructionIntMod] emptyState intTestFunc "instructionIntPop" [2] [GeneInt 2, GeneInt 0, StateFunc instructionIntPop] emptyState intTestFunc "instructionIntDup" [3, 3, 2] [GeneInt 2, GeneInt 3, StateFunc instructionIntDup] emptyState - intTestFunc "instructionIntDupN" [2, 2, 2] [GeneInt 2, GeneInt 3, StateFunc instructionIntDupN] emptyState + intTestFunc "instructionIntDupN3" [2, 2, 2] [GeneInt 2, GeneInt 3, StateFunc instructionIntDupN] emptyState + intTestFunc "instructionIntDupN-1" [0] [GeneInt 0, GeneInt 2, GeneInt (-1), StateFunc instructionIntDupN] emptyState intTestFunc "instructionIntSwap" [2, 0, 3] [GeneInt 3, GeneInt 2, GeneInt 0, StateFunc instructionIntSwap] emptyState intTestFunc "instructionIntSwapFail" [1] [GeneInt 1, StateFunc instructionIntSwap] emptyState intTestFunc "instructionIntRot" [1, 3, 2] [GeneInt 1, GeneInt 2, GeneInt 3, StateFunc instructionIntRot] emptyState @@ -96,8 +96,10 @@ main = do floatTestFunc "instructionFloatYankDup" [1.1, 4.4, 3.3, 2.2, 1.1] [GeneInt 3, GeneFloat 1.1, GeneFloat 2.2, GeneFloat 3.3, GeneFloat 4.4, StateFunc instructionFloatYankDup] emptyState floatTestFunc "instructionFloatShove" [3.3, 2.2, 4.4, 1.1] [GeneInt 3, GeneFloat 1.1, GeneFloat 2.2, GeneFloat 3.3, GeneFloat 4.4, StateFunc instructionFloatShove] emptyState floatTestFunc "instructionFloatShoveDup" [4.4, 3.3, 2.2, 4.4, 1.1] [GeneInt 3, GeneFloat 1.1, GeneFloat 2.2, GeneFloat 3.3, GeneFloat 4.4, StateFunc instructionFloatShoveDup] emptyState - - -- Bool tests + floatTestFunc "instructionFloatDupNonEmpty" [4.4, 4.4, 3.3] [GeneFloat 3.3, GeneFloat 4.4, StateFunc instructionFloatDup] emptyState + floatTestFunc "instructionFloatDupEmpty" [] [StateFunc instructionFloatDup] emptyState + floatTestFunc "instructionFloatDupN3" [4.4, 4.4, 4.4, 3.3] [GeneFloat 3.3, GeneFloat 4.4, GeneInt 3, StateFunc instructionFloatDupN] emptyState + floatTestFunc "instructionFloatDupN-1" [3.3] [GeneFloat 3.3, GeneFloat 4.4, GeneInt (-1), StateFunc instructionFloatDupN] emptyState boolTestFunc "instructionIntEqTrue" [True] [GeneInt 3, GeneInt 3, StateFunc instructionIntEq] emptyState boolTestFunc "instructionIntEqFalse" [False] [GeneInt 3, GeneInt 5, StateFunc instructionIntEq] emptyState boolTestFunc "instructionIntEqFail" [] [GeneInt 3, StateFunc instructionIntEq] emptyState @@ -256,3 +258,28 @@ main = do -- vector int instructions intVectorTestFunc "instructionIntVectorConcat" [[4, 5, 6, 1, 2, 3]] [GeneIntVector [1, 2, 3], GeneIntVector [4, 5, 6], StateFunc instructionIntVectorConcat] emptyState + intVectorTestFunc "instructionIntVectorConj" [[99, 1, 2, 3]] [GeneIntVector [1, 2, 3], GeneInt 99, StateFunc instructionIntVectorConj] emptyState + intVectorTestFunc "instructionIntTakeN" [[1, 2], [6, 7, 8]] [GeneIntVector [6, 7, 8], GeneIntVector [1, 2, 3], GeneInt 2, StateFunc instructionIntVectorTakeN] emptyState + intVectorTestFunc "instructionIntVectorSubVector" [[1, 2, 3]] [GeneIntVector [0, 1, 2, 3, 4, 5], GeneInt 3, GeneInt 1, StateFunc instructionIntVectorSubVector] emptyState + intTestFunc "instructionIntVectorFirst" [1] [GeneIntVector [1,2,3,4,5], StateFunc instructionIntVectorFirst] emptyState + intTestFunc "instructionIntVectorLast" [5] [GeneIntVector [1,2,3,4,5], StateFunc instructionIntVectorLast] emptyState + intTestFunc "instructionIntVectorNthInBounds" [2] [GeneIntVector [1,2,3,4,5], GeneInt 1, StateFunc instructionIntVectorNth] emptyState + intTestFunc "instructionIntVectorNthOverflow" [2] [GeneIntVector [1,2,3,4,5], GeneInt 6, StateFunc instructionIntVectorNth] emptyState + intVectorTestFunc "instructionIntVectorRestFull" [[2,3,4,5]] [GeneIntVector [1,2,3,4,5], StateFunc instructionIntVectorRest] emptyState + intVectorTestFunc "instructionIntVectorRestEmpty" [[]] [GeneIntVector [], StateFunc instructionIntVectorRest] emptyState + intVectorTestFunc "instructionIntVectorButLastFull" [[1,2,3,4]] [GeneIntVector [1,2,3,4,5], StateFunc instructionIntVectorButLast] emptyState + intVectorTestFunc "instructionIntVectorButLastEmpty" [[]] [GeneIntVector [], StateFunc instructionIntVectorButLast] emptyState + intTestFunc "instructionIntVectorLength3" [3] [GeneIntVector [1,2,3], StateFunc instructionIntVectorLength] emptyState + intTestFunc "instructionIntVectorLength0" [0] [GeneIntVector [], StateFunc instructionIntVectorLength] emptyState + intVectorTestFunc "instructionIntVectorReverse" [[4,3,2,1]] [GeneIntVector [1,2,3,4], StateFunc instructionIntVectorReverse] emptyState + intTestFunc "instructionIntVectorPushAllFull" [1,2,3,4,99] [GeneIntVector [1,2,3,4], GeneInt 99, StateFunc instructionIntVectorPushAll] emptyState + intTestFunc "instructionIntVectorPushAllEmpty" [99] [GeneIntVector [], GeneInt 99, StateFunc instructionIntVectorPushAll] emptyState + intVectorTestFunc "instructionIntVectorMakeEmpty" [[]] [StateFunc instructionIntVectorMakeEmpty] emptyState + boolTestFunc "instructionIntVectorIsEmptyTrue" [True] [GeneIntVector [], StateFunc instructionIntVectorIsEmpty] emptyState + boolTestFunc "instructionIntVectorIsEmptyFalse" [False] [GeneIntVector [1,2,3,4], StateFunc instructionIntVectorIsEmpty] emptyState + intTestFunc "instructionIntVectorIndexOf1" [1] [GeneIntVector [1,2,3,4,5], GeneInt 2, StateFunc instructionIntVectorIndexOf] emptyState + intTestFunc "instructionIntVectorIndexOfFail" [-1] [GeneIntVector [], GeneInt 2, StateFunc instructionIntVectorIndexOf] emptyState + intTestFunc "instructionIntVectorOccurrencesOf2" [2] [GeneIntVector [1,2,3,4,2,6,7], GeneInt 2, StateFunc instructionIntVectorOccurrencesOf] emptyState + intTestFunc "instructionIntVectorOccurrencesOf0" [0] [GeneIntVector [1,2,3,4,2,6,7], GeneInt 0, StateFunc instructionIntVectorOccurrencesOf] emptyState + intVectorTestFunc "instructionIntVectorSetNth3" [[0,1,2,99,4,5]] [GeneIntVector [0,1,2,3,4,5], GeneInt 99, GeneInt 3, StateFunc instructionIntVectorSetNth] emptyState + intVectorTestFunc "instructionIntVectorSetNth9" [[0,1,2,99,4,5]] [GeneIntVector [0,1,2,3,4,5], GeneInt 99, GeneInt 9, StateFunc instructionIntVectorSetNth] emptyState From 60f71bef0783fdf67da61a3691d04e04cadd6a42 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Tue, 28 Jan 2025 22:10:14 -0600 Subject: [PATCH 075/171] forgot to delete some comments --- src/Instructions/StringInstructions.hs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Instructions/StringInstructions.hs b/src/Instructions/StringInstructions.hs index ceb47db..1993721 100644 --- a/src/Instructions/StringInstructions.hs +++ b/src/Instructions/StringInstructions.hs @@ -174,8 +174,6 @@ instructionStringRemoveNth state@(State {_string = s1 : ss, _int = i1 : is}) = s instructionStringRemoveNth state = state instructionStringSetNth :: State -> State --- instructionStringSetNth state@(State {_string = s1 : ss, _char = c1 : cs, _int = i1 : is}) = state{_string = replaceAt (absNum i1 s1) c1 s1 : ss, _char = cs, _int = is} --- instructionStringSetNth state = state instructionStringSetNth state = instructionVectorSetNth state char string instructionStringStripWhitespace :: State -> State From 74d95c26aa6a257ad2c92b9eed76ed557da20e5c Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Tue, 28 Jan 2025 23:11:23 -0600 Subject: [PATCH 076/171] everything to VectorDatatype --- src/Instructions/GenericInstructions.hs | 5 ++ src/Instructions/StringInstructions.hs | 3 +- src/Instructions/VectorIntInstructions.hs | 68 +++++++++++------------ src/Push.hs | 20 +++---- src/State.hs | 60 ++++++++++---------- test/Main.hs | 67 +++++++++++----------- 6 files changed, 114 insertions(+), 109 deletions(-) diff --git a/src/Instructions/GenericInstructions.hs b/src/Instructions/GenericInstructions.hs index 73ed59d..d961b40 100644 --- a/src/Instructions/GenericInstructions.hs +++ b/src/Instructions/GenericInstructions.hs @@ -308,3 +308,8 @@ instructionVectorSetNth state@(State {_int = i1 : is}) primAccessor vectorAccess _ -> state instructionVectorSetNth state _ _ = state +instructionVectorReplace :: Eq a => State -> Lens' State [a] -> Lens' State [[a]] -> State +instructionVectorReplace state primAccessor vectorAccessor = + case (uncons (view vectorAccessor state), uncons (view primAccessor state)) of + (Just (v1, vs), Just (p1, p2 : ps)) -> state & vectorAccessor .~ (replace v1 [p1] [p2] Nothing : vs) & primAccessor .~ ps + _ -> state diff --git a/src/Instructions/StringInstructions.hs b/src/Instructions/StringInstructions.hs index 1993721..840d7fb 100644 --- a/src/Instructions/StringInstructions.hs +++ b/src/Instructions/StringInstructions.hs @@ -114,8 +114,7 @@ instructionStringReplaceNChar state@(State {_string = s1 : ss, _char = c1 : c2 : instructionStringReplaceNChar state = state instructionStringReplaceAllChar :: State -> State -instructionStringReplaceAllChar state@(State {_string = s1 : ss, _char = c1 : c2 : cs}) = state{_string = replace s1 [c1] [c2] Nothing : ss, _char = cs} -instructionStringReplaceAllChar state = state +instructionStringReplaceAllChar state = instructionVectorReplace state char string instructionStringRemoveFirstChar :: State -> State instructionStringRemoveFirstChar state@(State {_string = s1 : ss, _char = c1 : cs}) = state {_string = replace s1 [c1] "" (Just 1) : ss, _char = cs} diff --git a/src/Instructions/VectorIntInstructions.hs b/src/Instructions/VectorIntInstructions.hs index b8caf0c..3feffbe 100644 --- a/src/Instructions/VectorIntInstructions.hs +++ b/src/Instructions/VectorIntInstructions.hs @@ -3,53 +3,53 @@ module Instructions.VectorIntInstructions where import Instructions.GenericInstructions import State -instructionIntVectorConcat :: State -> State -instructionIntVectorConcat state = instructionConcat state intVector +instructionVectorIntConcat :: State -> State +instructionVectorIntConcat state = instructionConcat state vectorInt -instructionIntVectorConj :: State -> State -instructionIntVectorConj state = instructionConj state int intVector +instructionVectorIntConj :: State -> State +instructionVectorIntConj state = instructionConj state int vectorInt -instructionIntVectorTakeN :: State -> State -instructionIntVectorTakeN state = instructionTakeN state intVector +instructionVectorIntTakeN :: State -> State +instructionVectorIntTakeN state = instructionTakeN state vectorInt -instructionIntVectorSubVector :: State -> State -instructionIntVectorSubVector state = instructionSubVector state intVector +instructionVectorIntSubVector :: State -> State +instructionVectorIntSubVector state = instructionSubVector state vectorInt -instructionIntVectorFirst :: State -> State -instructionIntVectorFirst state = instructionVectorFirst state int intVector +instructionVectorIntFirst :: State -> State +instructionVectorIntFirst state = instructionVectorFirst state int vectorInt -instructionIntVectorLast :: State -> State -instructionIntVectorLast state = instructionVectorLast state int intVector +instructionVectorIntLast :: State -> State +instructionVectorIntLast state = instructionVectorLast state int vectorInt -instructionIntVectorNth :: State -> State -instructionIntVectorNth state = instructionVectorNth state int intVector +instructionVectorIntNth :: State -> State +instructionVectorIntNth state = instructionVectorNth state int vectorInt -instructionIntVectorRest :: State -> State -instructionIntVectorRest state = instructionRest state intVector +instructionVectorIntRest :: State -> State +instructionVectorIntRest state = instructionRest state vectorInt -instructionIntVectorButLast :: State -> State -instructionIntVectorButLast state = instructionButLast state intVector +instructionVectorIntButLast :: State -> State +instructionVectorIntButLast state = instructionButLast state vectorInt -instructionIntVectorLength :: State -> State -instructionIntVectorLength state = instructionLength state intVector +instructionVectorIntLength :: State -> State +instructionVectorIntLength state = instructionLength state vectorInt -instructionIntVectorReverse :: State -> State -instructionIntVectorReverse state = instructionReverse state intVector +instructionVectorIntReverse :: State -> State +instructionVectorIntReverse state = instructionReverse state vectorInt -instructionIntVectorPushAll :: State -> State -instructionIntVectorPushAll state = instructionPushAll state int intVector +instructionVectorIntPushAll :: State -> State +instructionVectorIntPushAll state = instructionPushAll state int vectorInt -instructionIntVectorMakeEmpty :: State -> State -instructionIntVectorMakeEmpty state = instructionVectorMakeEmpty state intVector +instructionVectorIntMakeEmpty :: State -> State +instructionVectorIntMakeEmpty state = instructionVectorMakeEmpty state vectorInt -instructionIntVectorIsEmpty :: State -> State -instructionIntVectorIsEmpty state = instructionVectorIsEmpty state intVector +instructionVectorIntIsEmpty :: State -> State +instructionVectorIntIsEmpty state = instructionVectorIsEmpty state vectorInt -instructionIntVectorIndexOf :: State -> State -instructionIntVectorIndexOf state = instructionVectorIndexOf state int intVector +instructionVectorIntIndexOf :: State -> State +instructionVectorIntIndexOf state = instructionVectorIndexOf state int vectorInt -instructionIntVectorOccurrencesOf :: State -> State -instructionIntVectorOccurrencesOf state = instructionVectorOccurrencesOf state int intVector +instructionVectorIntOccurrencesOf :: State -> State +instructionVectorIntOccurrencesOf state = instructionVectorOccurrencesOf state int vectorInt -instructionIntVectorSetNth :: State -> State -instructionIntVectorSetNth state = instructionVectorSetNth state int intVector +instructionVectorIntSetNth :: State -> State +instructionVectorIntSetNth state = instructionVectorSetNth state int vectorInt diff --git a/src/Push.hs b/src/Push.hs index 879b6ca..edbf3f0 100644 --- a/src/Push.hs +++ b/src/Push.hs @@ -23,11 +23,11 @@ instructionParameterLoad state@(State {_parameter = (p : _)}) = case p of (GeneBool val) -> state & bool .~ val : view bool state (GeneString val) -> state & string .~ val : view string state (GeneChar val) -> state & char .~ val : view char state - (GeneIntVector val) -> state & intVector .~ val : view intVector state - (GeneFloatVector val) -> state & floatVector .~ val : view floatVector state - (GeneBoolVector val) -> state & boolVector .~ val : view boolVector state - (GeneStringVector val) -> state & stringVector .~ val : view stringVector state - (GeneCharVector val) -> state & charVector .~ val : view charVector state + (GeneVectorInt val) -> state & vectorInt .~ val : view vectorInt state + (GeneVectorFloat val) -> state & vectorFloat .~ val : view vectorFloat state + (GeneVectorBool val) -> state & vectorBool .~ val : view vectorBool state + (GeneVectorString val) -> state & vectorString .~ val : view vectorString state + (GeneVectorChar val) -> state & vectorChar .~ val : view vectorChar state (StateFunc _) -> undefined (PlaceInput _) -> undefined Close -> undefined @@ -57,11 +57,11 @@ interpretExec state@(State {_exec = (e : es)}) = (GeneBool val) -> interpretExec (state & exec .~ es & bool .~ val : view bool state) (GeneString val) -> interpretExec (state & exec .~ es & string .~ val : view string state) (GeneChar val) -> interpretExec (state & exec .~ es & char .~ val : view char state) - (GeneIntVector val) -> interpretExec (state & exec .~ es & intVector .~ val : view intVector state) - (GeneFloatVector val) -> interpretExec (state & exec .~ es & floatVector .~ val : view floatVector state) - (GeneBoolVector val) -> interpretExec (state & exec .~ es & boolVector .~ val : view boolVector state) - (GeneStringVector val) -> interpretExec (state & exec .~ es & stringVector .~ val : view stringVector state) - (GeneCharVector val) -> interpretExec (state & exec .~ es & charVector .~ val : view charVector state) + (GeneVectorInt val) -> interpretExec (state & exec .~ es & vectorInt .~ val : view vectorInt state) + (GeneVectorFloat val) -> interpretExec (state & exec .~ es & vectorFloat .~ val : view vectorFloat state) + (GeneVectorBool val) -> interpretExec (state & exec .~ es & vectorBool .~ val : view vectorBool state) + (GeneVectorString val) -> interpretExec (state & exec .~ es & vectorString .~ val : view vectorString state) + (GeneVectorChar val) -> interpretExec (state & exec .~ es & vectorChar .~ val : view vectorChar state) (StateFunc func) -> interpretExec $ func state {_exec = es} (Block block) -> interpretExec (state {_exec = block ++ es}) (PlaceInput val) -> interpretExec (state {_exec = (view input state Map.! val) : es}) diff --git a/src/State.hs b/src/State.hs index eb61775..a19516c 100644 --- a/src/State.hs +++ b/src/State.hs @@ -15,11 +15,11 @@ data Gene | GeneBool Bool | GeneString String | GeneChar Char - | GeneIntVector [Int] - | GeneFloatVector [Float] - | GeneBoolVector [Bool] - | GeneStringVector [String] - | GeneCharVector [Char] + | GeneVectorInt [Int] + | GeneVectorFloat [Float] + | GeneVectorBool [Bool] + | GeneVectorString [String] + | GeneVectorChar [Char] | StateFunc (State -> State) | PlaceInput String | Close @@ -32,11 +32,11 @@ instance Eq Gene where GeneString x == GeneString y = x == y GeneChar x == GeneChar y = x == y PlaceInput x == PlaceInput y = x == y - GeneIntVector xs == GeneIntVector ys = xs == ys - GeneFloatVector xs == GeneFloatVector ys = xs == ys - GeneBoolVector xs == GeneBoolVector ys = xs == ys - GeneStringVector xs == GeneStringVector ys = xs == ys - GeneCharVector xs == GeneCharVector ys = xs == ys + GeneVectorInt xs == GeneVectorInt ys = xs == ys + GeneVectorFloat xs == GeneVectorFloat ys = xs == ys + GeneVectorBool xs == GeneVectorBool ys = xs == ys + GeneVectorString xs == GeneVectorString ys = xs == ys + GeneVectorChar xs == GeneVectorChar ys = xs == ys Close == Close = True StateFunc _ == StateFunc _ = True -- This line is probably not the best thing to do Block x == Block y = x == y @@ -50,11 +50,11 @@ instance Show Gene where show (GeneChar x) = "Char: " <> show x show (StateFunc _) = "Func: unnamed" show (PlaceInput x) = "In: " <> x - show (GeneIntVector xs) = "Int Vec: " <> show xs - show (GeneFloatVector xs) = "Float Vec: " <> show xs - show (GeneBoolVector xs) = "Bool Vec: " <> show xs - show (GeneStringVector xs) = "String Vec: " <> show xs - show (GeneCharVector xs) = "Char Vec: " <> show xs + show (GeneVectorInt xs) = "Int Vec: " <> show xs + show (GeneVectorFloat xs) = "Float Vec: " <> show xs + show (GeneVectorBool xs) = "Bool Vec: " <> show xs + show (GeneVectorString xs) = "String Vec: " <> show xs + show (GeneVectorChar xs) = "Char Vec: " <> show xs show Close = "Close" show (Block xs) = "Block: " <> show xs @@ -66,11 +66,11 @@ data State = State _bool :: [Bool], _string :: [String], _char :: [Char], - _intVector :: [[Int]], - _floatVector :: [[Float]], - _boolVector :: [[Bool]], - _stringVector :: [[String]], - _charVector :: [[Char]], + _vectorInt :: [[Int]], + _vectorFloat :: [[Float]], + _vectorBool :: [[Bool]], + _vectorString :: [[String]], + _vectorChar :: [[Char]], _parameter :: [Gene], _input :: Map.Map String Gene } @@ -89,11 +89,11 @@ emptyState = _string = [], _char = [], _parameter = [], - _intVector = [], - _floatVector = [], - _boolVector = [], - _stringVector = [], - _charVector = [], + _vectorInt = [], + _vectorFloat = [], + _vectorBool = [], + _vectorString = [], + _vectorChar = [], _input = Map.empty } @@ -108,10 +108,10 @@ exampleState = _string = ["abc", "123"], _char = ['d', 'e', 'f'], _parameter = [], - _intVector = [[1, 2], [5, 6, 8]], - _floatVector = [[1.234, 9.21], [5.42, 6.221, 8.5493]], - _boolVector = [[True, False], [False, False, True]], - _stringVector = [["this is a sentence", "this is also a sentence"], ["s0", "s1", "s2"]], - _charVector = [['z', 'x'], ['r', 'a', 't', 'l']], + _vectorInt = [[1, 2], [5, 6, 8]], + _vectorFloat = [[1.234, 9.21], [5.42, 6.221, 8.5493]], + _vectorBool = [[True, False], [False, False, True]], + _vectorString = [["this is a sentence", "this is also a sentence"], ["s0", "s1", "s2"]], + _vectorChar = [['z', 'x'], ['r', 'a', 't', 'l']], _input = Map.empty } diff --git a/test/Main.hs b/test/Main.hs index 92afc36..39abe40 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -44,10 +44,10 @@ charTestFunc name goal genome startState = let state = loadProgram genome startState in assert (goal == _char (interpretExec state)) putStrLn (name <> " passed test.") -intVectorTestFunc :: String -> [[Int]] -> [Gene] -> State -> IO () -intVectorTestFunc name goal genome startState = +vectorIntTestFunc :: String -> [[Int]] -> [Gene] -> State -> IO () +vectorIntTestFunc name goal genome startState = let state = loadProgram genome startState - in assert (goal == _intVector (interpretExec state)) putStrLn (name <> " passed test.") + in assert (goal == _vectorInt (interpretExec state)) putStrLn (name <> " passed test.") main :: IO () main = do @@ -111,7 +111,7 @@ main = do codeTestFunc "instructionCodeFirst" [GeneInt 5] [StateFunc instructionCodeFromExec, Block [GeneInt 5, StateFunc instructionIntSub], StateFunc instructionCodeFirst] emptyState codeTestFunc "instructionCodeLast" [GeneBool True] [StateFunc instructionCodeFromExec, Block [GeneInt 5, StateFunc instructionIntSub, GeneBool True], StateFunc instructionCodeLast] emptyState codeTestFunc "instructionCodeTail" [Block [GeneFloat 3.2, GeneBool True, GeneInt 3]] [StateFunc instructionCodeFromExec, Block [StateFunc instructionFloatAdd, GeneFloat 3.2, GeneBool True, GeneInt 3], StateFunc instructionCodeTail] emptyState - codeTestFunc "instructionCodeInit" [Block [GeneIntVector [1], GeneFloat 3.2, GeneBool True]] [StateFunc instructionCodeFromExec, Block [GeneIntVector [1], GeneFloat 3.2, GeneBool True, GeneInt 3], StateFunc instructionCodeInit] emptyState + codeTestFunc "instructionCodeInit" [Block [GeneVectorInt [1], GeneFloat 3.2, GeneBool True]] [StateFunc instructionCodeFromExec, Block [GeneVectorInt [1], GeneFloat 3.2, GeneBool True, GeneInt 3], StateFunc instructionCodeInit] emptyState codeTestFunc "instructionCodeWrap" [Block [GeneInt 3]] [StateFunc instructionCodeFromExec, GeneInt 3, StateFunc instructionCodeWrap] emptyState codeTestFunc "instructionCodeList" [Block [GeneFloat 5.43, GeneInt 3]] [StateFunc instructionCodeFromExec, GeneInt 3, StateFunc instructionCodeFromExec, GeneFloat 5.43, StateFunc instructionCodeList] emptyState codeTestFunc "instructionCodeCombine2Blocks" [Block [GeneInt 3, GeneInt 4, GeneInt 1, GeneInt 2]] [StateFunc instructionCodeFromExec, Block [GeneInt 1, GeneInt 2], StateFunc instructionCodeFromExec, Block [GeneInt 3, GeneInt 4], StateFunc instructionCodeCombine] emptyState @@ -129,9 +129,9 @@ main = do intTestFunc "instructionCodeIfTrue" [6] [GeneBool True, StateFunc instructionCodeFromExec, GeneInt 3, StateFunc instructionCodeFromExec, GeneInt 6, StateFunc instructionCodeIf] emptyState intTestFunc "instructionCodeIfFalse" [3] [GeneBool False, StateFunc instructionCodeFromExec, GeneInt 3, StateFunc instructionCodeFromExec, GeneInt 6, StateFunc instructionCodeIf] emptyState intTestFunc "instructionCodeWhen" [6, 3, 6] [GeneInt 6, GeneInt 3, GeneInt 4, GeneInt 2, GeneBool True, StateFunc instructionCodeFromExec, StateFunc instructionIntAdd, StateFunc instructionCodeWhen] emptyState - boolTestFunc "instructionCodeMemberTrue" [True] [StateFunc instructionCodeFromExec, GeneInt 2, StateFunc instructionCodeFromExec, Block [GeneFloat 3.6, GeneInt 2, GeneIntVector [8, 9]], StateFunc instructionCodeMember] emptyState - boolTestFunc "instructionCodeMemberFalse" [False] [StateFunc instructionCodeFromExec, GeneInt 7, StateFunc instructionCodeFromExec, Block [GeneFloat 3.6, GeneInt 2, GeneIntVector [8, 9]], StateFunc instructionCodeMember] emptyState - boolTestFunc "instructionCodeMember2Blocks" [False] [StateFunc instructionCodeFromExec, Block [GeneInt 7, GeneInt 0], StateFunc instructionCodeFromExec, Block [GeneFloat 3.6, GeneInt 2, GeneIntVector [8, 9]], StateFunc instructionCodeMember] emptyState + boolTestFunc "instructionCodeMemberTrue" [True] [StateFunc instructionCodeFromExec, GeneInt 2, StateFunc instructionCodeFromExec, Block [GeneFloat 3.6, GeneInt 2, GeneVectorInt [8, 9]], StateFunc instructionCodeMember] emptyState + boolTestFunc "instructionCodeMemberFalse" [False] [StateFunc instructionCodeFromExec, GeneInt 7, StateFunc instructionCodeFromExec, Block [GeneFloat 3.6, GeneInt 2, GeneVectorInt [8, 9]], StateFunc instructionCodeMember] emptyState + boolTestFunc "instructionCodeMember2Blocks" [False] [StateFunc instructionCodeFromExec, Block [GeneInt 7, GeneInt 0], StateFunc instructionCodeFromExec, Block [GeneFloat 3.6, GeneInt 2, GeneVectorInt [8, 9]], StateFunc instructionCodeMember] emptyState codeTestFunc "instructionCodeNInBounds" [GeneInt 0] [StateFunc instructionCodeFromExec, Block [GeneInt 0, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 4, GeneInt 5], GeneInt 0, StateFunc instructionCodeN] emptyState codeTestFunc "instructionCodeNInBoundsEnd" [GeneInt 5] [StateFunc instructionCodeFromExec, Block [GeneInt 0, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 4, GeneInt 5], GeneInt 5, StateFunc instructionCodeN] emptyState codeTestFunc "instructionCodeNModded" [GeneInt 3] [StateFunc instructionCodeFromExec, Block [GeneInt 0, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 4, GeneInt 5], GeneInt 9, StateFunc instructionCodeN] emptyState @@ -257,29 +257,30 @@ main = do boolTestFunc "instructionCharIsDigitFalse" [False] [GeneChar 'a', StateFunc instructionCharIsDigit] emptyState -- vector int instructions - intVectorTestFunc "instructionIntVectorConcat" [[4, 5, 6, 1, 2, 3]] [GeneIntVector [1, 2, 3], GeneIntVector [4, 5, 6], StateFunc instructionIntVectorConcat] emptyState - intVectorTestFunc "instructionIntVectorConj" [[99, 1, 2, 3]] [GeneIntVector [1, 2, 3], GeneInt 99, StateFunc instructionIntVectorConj] emptyState - intVectorTestFunc "instructionIntTakeN" [[1, 2], [6, 7, 8]] [GeneIntVector [6, 7, 8], GeneIntVector [1, 2, 3], GeneInt 2, StateFunc instructionIntVectorTakeN] emptyState - intVectorTestFunc "instructionIntVectorSubVector" [[1, 2, 3]] [GeneIntVector [0, 1, 2, 3, 4, 5], GeneInt 3, GeneInt 1, StateFunc instructionIntVectorSubVector] emptyState - intTestFunc "instructionIntVectorFirst" [1] [GeneIntVector [1,2,3,4,5], StateFunc instructionIntVectorFirst] emptyState - intTestFunc "instructionIntVectorLast" [5] [GeneIntVector [1,2,3,4,5], StateFunc instructionIntVectorLast] emptyState - intTestFunc "instructionIntVectorNthInBounds" [2] [GeneIntVector [1,2,3,4,5], GeneInt 1, StateFunc instructionIntVectorNth] emptyState - intTestFunc "instructionIntVectorNthOverflow" [2] [GeneIntVector [1,2,3,4,5], GeneInt 6, StateFunc instructionIntVectorNth] emptyState - intVectorTestFunc "instructionIntVectorRestFull" [[2,3,4,5]] [GeneIntVector [1,2,3,4,5], StateFunc instructionIntVectorRest] emptyState - intVectorTestFunc "instructionIntVectorRestEmpty" [[]] [GeneIntVector [], StateFunc instructionIntVectorRest] emptyState - intVectorTestFunc "instructionIntVectorButLastFull" [[1,2,3,4]] [GeneIntVector [1,2,3,4,5], StateFunc instructionIntVectorButLast] emptyState - intVectorTestFunc "instructionIntVectorButLastEmpty" [[]] [GeneIntVector [], StateFunc instructionIntVectorButLast] emptyState - intTestFunc "instructionIntVectorLength3" [3] [GeneIntVector [1,2,3], StateFunc instructionIntVectorLength] emptyState - intTestFunc "instructionIntVectorLength0" [0] [GeneIntVector [], StateFunc instructionIntVectorLength] emptyState - intVectorTestFunc "instructionIntVectorReverse" [[4,3,2,1]] [GeneIntVector [1,2,3,4], StateFunc instructionIntVectorReverse] emptyState - intTestFunc "instructionIntVectorPushAllFull" [1,2,3,4,99] [GeneIntVector [1,2,3,4], GeneInt 99, StateFunc instructionIntVectorPushAll] emptyState - intTestFunc "instructionIntVectorPushAllEmpty" [99] [GeneIntVector [], GeneInt 99, StateFunc instructionIntVectorPushAll] emptyState - intVectorTestFunc "instructionIntVectorMakeEmpty" [[]] [StateFunc instructionIntVectorMakeEmpty] emptyState - boolTestFunc "instructionIntVectorIsEmptyTrue" [True] [GeneIntVector [], StateFunc instructionIntVectorIsEmpty] emptyState - boolTestFunc "instructionIntVectorIsEmptyFalse" [False] [GeneIntVector [1,2,3,4], StateFunc instructionIntVectorIsEmpty] emptyState - intTestFunc "instructionIntVectorIndexOf1" [1] [GeneIntVector [1,2,3,4,5], GeneInt 2, StateFunc instructionIntVectorIndexOf] emptyState - intTestFunc "instructionIntVectorIndexOfFail" [-1] [GeneIntVector [], GeneInt 2, StateFunc instructionIntVectorIndexOf] emptyState - intTestFunc "instructionIntVectorOccurrencesOf2" [2] [GeneIntVector [1,2,3,4,2,6,7], GeneInt 2, StateFunc instructionIntVectorOccurrencesOf] emptyState - intTestFunc "instructionIntVectorOccurrencesOf0" [0] [GeneIntVector [1,2,3,4,2,6,7], GeneInt 0, StateFunc instructionIntVectorOccurrencesOf] emptyState - intVectorTestFunc "instructionIntVectorSetNth3" [[0,1,2,99,4,5]] [GeneIntVector [0,1,2,3,4,5], GeneInt 99, GeneInt 3, StateFunc instructionIntVectorSetNth] emptyState - intVectorTestFunc "instructionIntVectorSetNth9" [[0,1,2,99,4,5]] [GeneIntVector [0,1,2,3,4,5], GeneInt 99, GeneInt 9, StateFunc instructionIntVectorSetNth] emptyState + vectorIntTestFunc "instructionVectorIntConcat" [[4, 5, 6, 1, 2, 3]] [GeneVectorInt [1, 2, 3], GeneVectorInt [4, 5, 6], StateFunc instructionVectorIntConcat] emptyState + vectorIntTestFunc "instructionVectorIntConj" [[99, 1, 2, 3]] [GeneVectorInt [1, 2, 3], GeneInt 99, StateFunc instructionVectorIntConj] emptyState + vectorIntTestFunc "instructionIntTakeN" [[1, 2], [6, 7, 8]] [GeneVectorInt [6, 7, 8], GeneVectorInt [1, 2, 3], GeneInt 2, StateFunc instructionVectorIntTakeN] emptyState + vectorIntTestFunc "instructionVectorIntSubVector" [[1, 2, 3]] [GeneVectorInt [0, 1, 2, 3, 4, 5], GeneInt 3, GeneInt 1, StateFunc instructionVectorIntSubVector] emptyState + intTestFunc "instructionVectorIntFirst" [1] [GeneVectorInt [1,2,3,4,5], StateFunc instructionVectorIntFirst] emptyState + intTestFunc "instructionVectorIntLast" [5] [GeneVectorInt [1,2,3,4,5], StateFunc instructionVectorIntLast] emptyState + intTestFunc "instructionVectorIntNthInBounds" [2] [GeneVectorInt [1,2,3,4,5], GeneInt 1, StateFunc instructionVectorIntNth] emptyState + intTestFunc "instructionVectorIntNthOverflow" [2] [GeneVectorInt [1,2,3,4,5], GeneInt 6, StateFunc instructionVectorIntNth] emptyState + vectorIntTestFunc "instructionVectorIntRestFull" [[2,3,4,5]] [GeneVectorInt [1,2,3,4,5], StateFunc instructionVectorIntRest] emptyState + vectorIntTestFunc "instructionVectorIntRestEmpty" [[]] [GeneVectorInt [], StateFunc instructionVectorIntRest] emptyState + vectorIntTestFunc "instructionVectorIntButLastFull" [[1,2,3,4]] [GeneVectorInt [1,2,3,4,5], StateFunc instructionVectorIntButLast] emptyState + vectorIntTestFunc "instructionVectorIntButLastEmpty" [[]] [GeneVectorInt [], StateFunc instructionVectorIntButLast] emptyState + intTestFunc "instructionVectorIntLength3" [3] [GeneVectorInt [1,2,3], StateFunc instructionVectorIntLength] emptyState + intTestFunc "instructionVectorIntLength0" [0] [GeneVectorInt [], StateFunc instructionVectorIntLength] emptyState + vectorIntTestFunc "instructionVectorIntReverse" [[4,3,2,1]] [GeneVectorInt [1,2,3,4], StateFunc instructionVectorIntReverse] emptyState + intTestFunc "instructionVectorIntPushAllFull" [1,2,3,4,99] [GeneVectorInt [1,2,3,4], GeneInt 99, StateFunc instructionVectorIntPushAll] emptyState + intTestFunc "instructionVectorIntPushAllEmpty" [99] [GeneVectorInt [], GeneInt 99, StateFunc instructionVectorIntPushAll] emptyState + vectorIntTestFunc "instructionVectorIntMakeEmpty" [[]] [StateFunc instructionVectorIntMakeEmpty] emptyState + boolTestFunc "instructionVectorIntIsEmptyTrue" [True] [GeneVectorInt [], StateFunc instructionVectorIntIsEmpty] emptyState + boolTestFunc "instructionVectorIntIsEmptyFalse" [False] [GeneVectorInt [1,2,3,4], StateFunc instructionVectorIntIsEmpty] emptyState + intTestFunc "instructionVectorIntIndexOf1" [1] [GeneVectorInt [1,2,3,4,5], GeneInt 2, StateFunc instructionVectorIntIndexOf] emptyState + intTestFunc "instructionVectorIntIndexOfFail" [-1] [GeneVectorInt [], GeneInt 2, StateFunc instructionVectorIntIndexOf] emptyState + intTestFunc "instructionVectorIntOccurrencesOf2" [2] [GeneVectorInt [1,2,3,4,2,6,7], GeneInt 2, StateFunc instructionVectorIntOccurrencesOf] emptyState + intTestFunc "instructionVectorIntOccurrencesOf0" [0] [GeneVectorInt [1,2,3,4,2,6,7], GeneInt 0, StateFunc instructionVectorIntOccurrencesOf] emptyState + vectorIntTestFunc "instructionVectorIntSetNth3" [[0,1,2,99,4,5]] [GeneVectorInt [0,1,2,3,4,5], GeneInt 99, GeneInt 3, StateFunc instructionVectorIntSetNth] emptyState + vectorIntTestFunc "instructionVectorIntSetNth9" [[0,1,2,99,4,5]] [GeneVectorInt [0,1,2,3,4,5], GeneInt 99, GeneInt 9, StateFunc instructionVectorIntSetNth] emptyState + -- vectorIntTestFunc "instructionVectorInt" From 34a0afb242c14988bd601786a6b7ead110fd316d Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Tue, 28 Jan 2025 23:47:37 -0600 Subject: [PATCH 077/171] generic vector instructions done --- src/Instructions/GenericInstructions.hs | 13 +++++++++++++ src/Instructions/StringInstructions.hs | 6 ++---- src/Instructions/VectorIntInstructions.hs | 9 +++++++++ test/Main.hs | 6 +++++- 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/Instructions/GenericInstructions.hs b/src/Instructions/GenericInstructions.hs index d961b40..cafeb6c 100644 --- a/src/Instructions/GenericInstructions.hs +++ b/src/Instructions/GenericInstructions.hs @@ -313,3 +313,16 @@ instructionVectorReplace state primAccessor vectorAccessor = case (uncons (view vectorAccessor state), uncons (view primAccessor state)) of (Just (v1, vs), Just (p1, p2 : ps)) -> state & vectorAccessor .~ (replace v1 [p1] [p2] Nothing : vs) & primAccessor .~ ps _ -> state + +instructionVectorReplaceFirst :: Eq a => State -> Lens' State [a] -> Lens' State [[a]] -> State +instructionVectorReplaceFirst state primAccessor vectorAccessor = + case (uncons (view vectorAccessor state), uncons (view primAccessor state)) of + (Just (v1, vs), Just (p1, p2 : ps)) -> state & vectorAccessor .~ (replace v1 [p1] [p2] (Just 1) : vs) & primAccessor .~ ps + _ -> state + +instructionVectorRemove :: Eq a => State -> Lens' State [a] -> Lens' State [[a]] -> State +instructionVectorRemove state primAccessor vectorAccessor = + case (uncons (view vectorAccessor state), uncons (view primAccessor state)) of + (Just (v1, vs), Just (p1, ps)) -> state & vectorAccessor .~ (replace v1 [p1] [] Nothing : vs) & primAccessor .~ ps + _ -> state + diff --git a/src/Instructions/StringInstructions.hs b/src/Instructions/StringInstructions.hs index 840d7fb..e828677 100644 --- a/src/Instructions/StringInstructions.hs +++ b/src/Instructions/StringInstructions.hs @@ -106,8 +106,7 @@ instructionStringSplitOnChar state@(State {_string = s1 : ss, _char = c1 : cs}) instructionStringSplitOnChar state = state instructionStringReplaceFirstChar :: State -> State -instructionStringReplaceFirstChar state@(State {_string = s1 : ss, _char = c1 : c2 : cs}) = state {_string = replace s1 [c1] [c2] (Just 1) : ss, _char = cs} -instructionStringReplaceFirstChar state = state +instructionStringReplaceFirstChar state = instructionVectorReplaceFirst state char string instructionStringReplaceNChar :: State -> State instructionStringReplaceNChar state@(State {_string = s1 : ss, _char = c1 : c2 : cs, _int = i1 : is}) = state{_string = replace s1 [c1] [c2] (Just i1) : ss, _char = cs, _int = is} @@ -125,8 +124,7 @@ instructionStringRemoveNChar state@(State {_string = s1 : ss, _char = c1 : cs, _ instructionStringRemoveNChar state = state instructionStringRemoveAllChar :: State -> State -instructionStringRemoveAllChar state@(State {_string = s1 : ss, _char = c1 : cs}) = state{_string = replace s1 [c1] "" Nothing : ss, _char = cs} -instructionStringRemoveAllChar state = state +instructionStringRemoveAllChar state = instructionVectorRemove state char string instructionStringOccurrencesOfChar :: State -> State instructionStringOccurrencesOfChar state = instructionVectorOccurrencesOf state char string diff --git a/src/Instructions/VectorIntInstructions.hs b/src/Instructions/VectorIntInstructions.hs index 3feffbe..5cf508c 100644 --- a/src/Instructions/VectorIntInstructions.hs +++ b/src/Instructions/VectorIntInstructions.hs @@ -53,3 +53,12 @@ instructionVectorIntOccurrencesOf state = instructionVectorOccurrencesOf state i instructionVectorIntSetNth :: State -> State instructionVectorIntSetNth state = instructionVectorSetNth state int vectorInt + +instructionVectorIntReplace :: State -> State +instructionVectorIntReplace state = instructionVectorReplace state int vectorInt + +instructionVectorIntReplaceFirst :: State -> State +instructionVectorIntReplaceFirst state = instructionVectorReplaceFirst state int vectorInt + +instructionVectorIntRemove :: State -> State +instructionVectorIntRemove state = instructionVectorRemove state int vectorInt diff --git a/test/Main.hs b/test/Main.hs index 39abe40..cdfcd88 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -283,4 +283,8 @@ main = do intTestFunc "instructionVectorIntOccurrencesOf0" [0] [GeneVectorInt [1,2,3,4,2,6,7], GeneInt 0, StateFunc instructionVectorIntOccurrencesOf] emptyState vectorIntTestFunc "instructionVectorIntSetNth3" [[0,1,2,99,4,5]] [GeneVectorInt [0,1,2,3,4,5], GeneInt 99, GeneInt 3, StateFunc instructionVectorIntSetNth] emptyState vectorIntTestFunc "instructionVectorIntSetNth9" [[0,1,2,99,4,5]] [GeneVectorInt [0,1,2,3,4,5], GeneInt 99, GeneInt 9, StateFunc instructionVectorIntSetNth] emptyState - -- vectorIntTestFunc "instructionVectorInt" + vectorIntTestFunc "instructionVectorIntReplace3" [[0,1,2,99,4,5,99,5,99]] [GeneInt 99, GeneInt 3, GeneVectorInt [0,1,2,3,4,5,3,5,3], StateFunc instructionVectorIntReplace] emptyState + vectorIntTestFunc "instructionVectorIntReplace-1" [[0,1,2,3,4,5,3,5,3]] [GeneInt 99, GeneInt (-1), GeneVectorInt [0,1,2,3,4,5,3,5,3], StateFunc instructionVectorIntReplace] emptyState + vectorIntTestFunc "instructionVectorIntReplaceFirst3" [[0,1,2,99,4,5,3,5,3]] [GeneInt 99, GeneInt 3, GeneVectorInt [0,1,2,3,4,5,3,5,3], StateFunc instructionVectorIntReplaceFirst] emptyState + vectorIntTestFunc "instructionVectorIntReplaceFirst-2" [[0,1,2,3,4,5,3,5,3]] [GeneInt 99, GeneInt (-2), GeneVectorInt [0,1,2,3,4,5,3,5,3], StateFunc instructionVectorIntReplaceFirst] emptyState + vectorIntTestFunc "instructionVectorIntRemove" [[0,1,2,4,5,5]] [GeneInt 3, GeneVectorInt [0,1,2,3,4,5,3,5,3], StateFunc instructionVectorIntRemove] emptyState From a53611230ea1925889e3ccdbe8254f9ea9d3d785 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Wed, 29 Jan 2025 01:43:59 -0600 Subject: [PATCH 078/171] float instructions --- HushGP.cabal | 1 + src/Instructions/VectorFloatInstructions.hs | 64 +++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 src/Instructions/VectorFloatInstructions.hs diff --git a/HushGP.cabal b/HushGP.cabal index 36b0d17..e3f45cb 100644 --- a/HushGP.cabal +++ b/HushGP.cabal @@ -47,6 +47,7 @@ library , Instructions.StringInstructions , Instructions.CharInstructions , Instructions.VectorIntInstructions + , Instructions.VectorFloatInstructions -- Modules included in this library but not exported. -- other-modules: diff --git a/src/Instructions/VectorFloatInstructions.hs b/src/Instructions/VectorFloatInstructions.hs new file mode 100644 index 0000000..56472be --- /dev/null +++ b/src/Instructions/VectorFloatInstructions.hs @@ -0,0 +1,64 @@ +module Instructions.VectorFloatInstructions where + +import State +import Instructions.GenericInstructions + +instructionVectorFloatConcat :: State -> State +instructionVectorFloatConcat state = instructionConcat state vectorFloat + +instructionVectorFloatConj :: State -> State +instructionVectorFloatConj state = instructionConj state float vectorFloat + +instructionVectorFloatTakeN :: State -> State +instructionVectorFloatTakeN state = instructionTakeN state vectorInt + +instructionVectorFloatSubVector :: State -> State +instructionVectorFloatSubVector state = instructionSubVector state vectorInt + +instructionVectorFloatFirst :: State -> State +instructionVectorFloatFirst state = instructionVectorFirst state float vectorFloat + +instructionVectorFloatLast :: State -> State +instructionVectorFloatLast state = instructionVectorLast state float vectorFloat + +instructionVectorFloatNth :: State -> State +instructionVectorFloatNth state = instructionVectorNth state float vectorFloat + +instructionVectorFloatRest :: State -> State +instructionVectorFloatRest state = instructionRest state vectorFloat + +instructionVectorFloatButLast :: State -> State +instructionVectorFloatButLast state = instructionButLast state vectorFloat + +instructionVectorFloatLength :: State -> State +instructionVectorFloatLength state = instructionLength state vectorFloat + +instructionVectorFloatReverse :: State -> State +instructionVectorFloatReverse state = instructionReverse state vectorFloat + +instructionVectorFloatPushAll :: State -> State +instructionVectorFloatPushAll state = instructionPushAll state float vectorFloat + +instructionVectorFloatMakeEmpty :: State -> State +instructionVectorFloatMakeEmpty state = instructionVectorMakeEmpty state vectorFloat + +instructionVectorFloatIsEmpty :: State -> State +instructionVectorFloatIsEmpty state = instructionVectorIsEmpty state vectorFloat + +instructionVectorFloatIndexOf :: State -> State +instructionVectorFloatIndexOf state = instructionVectorIndexOf state float vectorFloat + +instructionVectorFloatOccurrencesOf :: State -> State +instructionVectorFloatOccurrencesOf state = instructionVectorOccurrencesOf state float vectorFloat + +instructionVectorFloatSetNth :: State -> State +instructionVectorFloatSetNth state = instructionVectorSetNth state float vectorFloat + +instructionVectorFloatReplace :: State -> State +instructionVectorFloatReplace state = instructionVectorReplace state float vectorFloat + +instructionVectorFloatReplaceFirst :: State -> State +instructionVectorFloatReplaceFirst state = instructionVectorReplaceFirst state float vectorFloat + +instructionVectorFloatRemove :: State -> State +instructionVectorFloatRemove state = instructionVectorRemove state float vectorFloat From 04d54c12a54b9ef16ea7bdddf50d77b5bbce1b86 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Wed, 29 Jan 2025 01:44:23 -0600 Subject: [PATCH 079/171] move some interpretExec functions --- src/Push.hs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Push.hs b/src/Push.hs index edbf3f0..2d3ebd2 100644 --- a/src/Push.hs +++ b/src/Push.hs @@ -49,8 +49,7 @@ loadProgram newstack state = state & exec .~ newstack -- ends up on top). -- The empty-stack safety of interpretExec on empty stacks depends on the functions it calls. interpretExec :: State -> State -interpretExec state@(State {_exec = []}) = state & exec .~ [] -interpretExec state@(State {_exec = (e : es)}) = +interpretExec state@(State {_exec = e : es}) = case e of (GeneInt val) -> interpretExec (state & exec .~ es & int .~ val : view int state) (GeneFloat val) -> interpretExec (state & exec .~ es & float .~ val : view float state) @@ -66,5 +65,6 @@ interpretExec state@(State {_exec = (e : es)}) = (Block block) -> interpretExec (state {_exec = block ++ es}) (PlaceInput val) -> interpretExec (state {_exec = (view input state Map.! val) : es}) Close -> undefined -- remove Close constructor later? +interpretExec state = state -- Need to make interpretExec strict, right? From 67d6125f0328fb9509cec59306be8706f48e1f4b Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Wed, 29 Jan 2025 01:45:42 -0600 Subject: [PATCH 080/171] finish the iterate function --- src/Instructions/GenericInstructions.hs | 11 +++++++++++ src/Instructions/VectorIntInstructions.hs | 3 +++ test/Main.hs | 1 + 3 files changed, 15 insertions(+) diff --git a/src/Instructions/GenericInstructions.hs b/src/Instructions/GenericInstructions.hs index cafeb6c..7f0e850 100644 --- a/src/Instructions/GenericInstructions.hs +++ b/src/Instructions/GenericInstructions.hs @@ -326,3 +326,14 @@ instructionVectorRemove state primAccessor vectorAccessor = (Just (v1, vs), Just (p1, ps)) -> state & vectorAccessor .~ (replace v1 [p1] [] Nothing : vs) & primAccessor .~ ps _ -> state +instructionVectorIterate :: State -> Lens' State [a] -> Lens' State [[a]] -> ([a] -> Gene) -> (State -> State) -> State +instructionVectorIterate state@(State {_exec = e1 : es}) primAccessor vectorAccessor vectorType typeIterateFunction = + case uncons (view vectorAccessor state) of + Just ([], vs) -> state{_exec = es} & vectorAccessor .~ vs + Just ([x], vs) -> state & primAccessor .~ (x : view primAccessor state) & vectorAccessor .~ vs + Just (v1, vs) -> + (case uncons v1 of + Just (nv1, nvs) -> state{_exec = e1 : vectorType nvs : StateFunc typeIterateFunction : e1 : es} & primAccessor .~ (nv1 : view primAccessor state) & vectorAccessor .~ vs + _ -> state) -- This should never happen + _ -> state +instructionVectorIterate state _ _ _ _ = state diff --git a/src/Instructions/VectorIntInstructions.hs b/src/Instructions/VectorIntInstructions.hs index 5cf508c..38c63a9 100644 --- a/src/Instructions/VectorIntInstructions.hs +++ b/src/Instructions/VectorIntInstructions.hs @@ -62,3 +62,6 @@ instructionVectorIntReplaceFirst state = instructionVectorReplaceFirst state int instructionVectorIntRemove :: State -> State instructionVectorIntRemove state = instructionVectorRemove state int vectorInt + +instructionVectorIntIterate :: State -> State +instructionVectorIntIterate state = instructionVectorIterate state int vectorInt GeneVectorInt instructionVectorIntIterate diff --git a/test/Main.hs b/test/Main.hs index cdfcd88..cf578fd 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -288,3 +288,4 @@ main = do vectorIntTestFunc "instructionVectorIntReplaceFirst3" [[0,1,2,99,4,5,3,5,3]] [GeneInt 99, GeneInt 3, GeneVectorInt [0,1,2,3,4,5,3,5,3], StateFunc instructionVectorIntReplaceFirst] emptyState vectorIntTestFunc "instructionVectorIntReplaceFirst-2" [[0,1,2,3,4,5,3,5,3]] [GeneInt 99, GeneInt (-2), GeneVectorInt [0,1,2,3,4,5,3,5,3], StateFunc instructionVectorIntReplaceFirst] emptyState vectorIntTestFunc "instructionVectorIntRemove" [[0,1,2,4,5,5]] [GeneInt 3, GeneVectorInt [0,1,2,3,4,5,3,5,3], StateFunc instructionVectorIntRemove] emptyState + intTestFunc "instructionVectorIntIterate" [66] [GeneInt 40, GeneVectorInt [0,1,2,3,4,5,3,5,3], StateFunc instructionVectorIntIterate, StateFunc instructionIntAdd] emptyState From 048b7fc9fb84af74ffe889339c4dea40c6d03db1 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Wed, 29 Jan 2025 14:49:16 -0600 Subject: [PATCH 081/171] re export into one module --- HushGP.cabal | 1 + test/Main.hs | 9 +-------- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/HushGP.cabal b/HushGP.cabal index e3f45cb..06f47f4 100644 --- a/HushGP.cabal +++ b/HushGP.cabal @@ -38,6 +38,7 @@ library exposed-modules: Push , GP , State + , Instructions , Instructions.IntInstructions , Instructions.ExecInstructions , Instructions.FloatInstructions diff --git a/test/Main.hs b/test/Main.hs index cf578fd..b930ccd 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -1,14 +1,7 @@ import Control.Exception (assert) -import Instructions.CharInstructions -import Instructions.CodeInstructions -import Instructions.ExecInstructions -import Instructions.FloatInstructions -import Instructions.IntInstructions -import Instructions.StringInstructions -import Instructions.VectorIntInstructions +import Instructions import Push import State - -- import Debug.Trace -- TODO: Need a function that can compare states. From 0c9538e2f278685c309885aaadeac2be8c9391e4 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Wed, 29 Jan 2025 14:50:05 -0600 Subject: [PATCH 082/171] delete learnlens --- src/LearnLens.hs | 30 ------------------------------ 1 file changed, 30 deletions(-) delete mode 100644 src/LearnLens.hs diff --git a/src/LearnLens.hs b/src/LearnLens.hs deleted file mode 100644 index fedd2ab..0000000 --- a/src/LearnLens.hs +++ /dev/null @@ -1,30 +0,0 @@ -{-# LANGUAGE TemplateHaskell #-} - -module LearnLens where - -import Control.Lens hiding (element) -import Control.Lens.TH - -data Atom = Atom {_element :: String, _point :: Point} deriving (Show) - -data Point = Point {_x :: Double, _Y :: Double} deriving (Show) - -$(makeLenses ''Atom) -$(makeLenses ''Point) - -myAtom :: Atom -myAtom = Atom "climberite" (Point 4.0 3.2) - -shiftAtom :: Atom -> Atom -shiftAtom = over (point . x) (+ 1) - -data Molecule = Molecule {_atoms :: [Atom]} deriving (Show) - -$(makeLenses ''Molecule) - -shiftMolecule :: Molecule -> Molecule -shiftMolecule = over (atoms . traverse . point . x) (+ 1) - --- Example without template haskell -defPoint :: Lens' Atom Point -defPoint = lens _point (\atom newPoint -> atom {_point = newPoint}) From 53d62f31c7057ee039784a81084d345fb8046681 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Wed, 29 Jan 2025 14:50:42 -0600 Subject: [PATCH 083/171] add instructions to re export --- src/Instructions.hs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/Instructions.hs diff --git a/src/Instructions.hs b/src/Instructions.hs new file mode 100644 index 0000000..4b6597d --- /dev/null +++ b/src/Instructions.hs @@ -0,0 +1,24 @@ +module Instructions ( + module Instructions.GenericInstructions, + module Instructions.IntInstructions, + module Instructions.FloatInstructions, + module Instructions.StringInstructions, + module Instructions.CharInstructions, + module Instructions.CodeInstructions, + module Instructions.ExecInstructions, + module Instructions.LogicalInstructions, + module Instructions.VectorIntInstructions, + module Instructions.VectorFloatInstructions +) +where + +import Instructions.GenericInstructions +import Instructions.IntInstructions +import Instructions.FloatInstructions +import Instructions.StringInstructions +import Instructions.CharInstructions +import Instructions.CodeInstructions +import Instructions.ExecInstructions +import Instructions.LogicalInstructions +import Instructions.VectorIntInstructions +import Instructions.VectorFloatInstructions From cef9c92b56fe896ce1af462b3803f5264df127ca Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Wed, 29 Jan 2025 16:35:05 -0600 Subject: [PATCH 084/171] more instructions, gonna rework the unit testing framework --- HushGP.cabal | 3 + src/Instructions/GenericInstructions.hs | 3 +- src/Instructions/VectorCharInstructions.hs | 67 +++++++++++++++++++ src/Instructions/VectorFloatInstructions.hs | 7 +- src/Instructions/VectorLogicalInstructions.hs | 67 +++++++++++++++++++ src/Instructions/VectorStringInstructions.hs | 67 +++++++++++++++++++ 6 files changed, 210 insertions(+), 4 deletions(-) create mode 100644 src/Instructions/VectorCharInstructions.hs create mode 100644 src/Instructions/VectorLogicalInstructions.hs create mode 100644 src/Instructions/VectorStringInstructions.hs diff --git a/HushGP.cabal b/HushGP.cabal index 06f47f4..7e224d7 100644 --- a/HushGP.cabal +++ b/HushGP.cabal @@ -49,6 +49,9 @@ library , Instructions.CharInstructions , Instructions.VectorIntInstructions , Instructions.VectorFloatInstructions + , Instructions.VectorStringInstructions + , Instructions.VectorLogicalInstructions + , Instructions.VectorCharInstructions -- Modules included in this library but not exported. -- other-modules: diff --git a/src/Instructions/GenericInstructions.hs b/src/Instructions/GenericInstructions.hs index 7f0e850..89bcd4d 100644 --- a/src/Instructions/GenericInstructions.hs +++ b/src/Instructions/GenericInstructions.hs @@ -143,7 +143,6 @@ instructionEq state accessor = stackTop = take 2 $ view accessor state instructionStackDepth :: State -> Lens' State [a] -> State --- instructionStackDepth state accessor = state & int .~ (length (view accessor state) : view int state) instructionStackDepth state@(State {_int = is}) accessor = state{_int = length (view accessor state) : is} -- Will have a non-generic definition for the int stack @@ -152,7 +151,7 @@ instructionYankDup state@(State {_int = i : is}) accessor = if notEmptyStack state accessor then (state & accessor .~ (view accessor state !! max 0 (min i (length (view accessor state) - 1))) : view accessor state) {_int = is} else state -instructionYankDup state@(State {_int = []}) _ = state +instructionYankDup state _ = state -- Is this optimal? Running instrucitonYankDup twice????? -- int non generic too diff --git a/src/Instructions/VectorCharInstructions.hs b/src/Instructions/VectorCharInstructions.hs new file mode 100644 index 0000000..e0c6fd1 --- /dev/null +++ b/src/Instructions/VectorCharInstructions.hs @@ -0,0 +1,67 @@ +module Instructions.VectorCharInstructions where + +import State +import Instructions.GenericInstructions + +instructionVectorCharConcat :: State -> State +instructionVectorCharConcat state = instructionConcat state vectorChar + +instructionVectorCharConj :: State -> State +instructionVectorCharConj state = instructionConj state char vectorChar + +instructionVectorCharTakeN :: State -> State +instructionVectorCharTakeN state = instructionTakeN state vectorChar + +instructionVectorCharSubVector :: State -> State +instructionVectorCharSubVector state = instructionSubVector state vectorChar + +instructionVectorCharFirst :: State -> State +instructionVectorCharFirst state = instructionVectorFirst state char vectorChar + +instructionVectorCharLast :: State -> State +instructionVectorCharLast state = instructionVectorLast state char vectorChar + +instructionVectorCharNth :: State -> State +instructionVectorCharNth state = instructionVectorNth state char vectorChar + +instructionVectorCharRest :: State -> State +instructionVectorCharRest state = instructionRest state vectorChar + +instructionVectorCharButLast :: State -> State +instructionVectorCharButLast state = instructionButLast state vectorChar + +instructionVectorCharLength :: State -> State +instructionVectorCharLength state = instructionLength state vectorChar + +instructionVectorCharReverse :: State -> State +instructionVectorCharReverse state = instructionReverse state vectorChar + +instructionVectorCharPushAll :: State -> State +instructionVectorCharPushAll state = instructionPushAll state char vectorChar + +instructionVectorCharMakeEmpty :: State -> State +instructionVectorCharMakeEmpty state = instructionVectorMakeEmpty state vectorChar + +instructionVectorCharIsEmpty :: State -> State +instructionVectorCharIsEmpty state = instructionVectorIsEmpty state vectorChar + +instructionVectorCharIndexOf :: State -> State +instructionVectorCharIndexOf state = instructionVectorIndexOf state char vectorChar + +instructionVectorCharOccurrencesOf :: State -> State +instructionVectorCharOccurrencesOf state = instructionVectorOccurrencesOf state char vectorChar + +instructionVectorCharSetNth :: State -> State +instructionVectorCharSetNth state = instructionVectorSetNth state char vectorChar + +instructionVectorCharReplace :: State -> State +instructionVectorCharReplace state = instructionVectorReplace state char vectorChar + +instructionVectorCharReplaceFirst :: State -> State +instructionVectorCharReplaceFirst state = instructionVectorReplaceFirst state char vectorChar + +instructionVectorCharRemove :: State -> State +instructionVectorCharRemove state = instructionVectorRemove state char vectorChar + +instructionVectorCharIterate :: State -> State +instructionVectorCharIterate state = instructionVectorIterate state char vectorChar GeneVectorChar instructionVectorCharIterate diff --git a/src/Instructions/VectorFloatInstructions.hs b/src/Instructions/VectorFloatInstructions.hs index 56472be..0006903 100644 --- a/src/Instructions/VectorFloatInstructions.hs +++ b/src/Instructions/VectorFloatInstructions.hs @@ -10,10 +10,10 @@ instructionVectorFloatConj :: State -> State instructionVectorFloatConj state = instructionConj state float vectorFloat instructionVectorFloatTakeN :: State -> State -instructionVectorFloatTakeN state = instructionTakeN state vectorInt +instructionVectorFloatTakeN state = instructionTakeN state vectorFloat instructionVectorFloatSubVector :: State -> State -instructionVectorFloatSubVector state = instructionSubVector state vectorInt +instructionVectorFloatSubVector state = instructionSubVector state vectorFloat instructionVectorFloatFirst :: State -> State instructionVectorFloatFirst state = instructionVectorFirst state float vectorFloat @@ -62,3 +62,6 @@ instructionVectorFloatReplaceFirst state = instructionVectorReplaceFirst state f instructionVectorFloatRemove :: State -> State instructionVectorFloatRemove state = instructionVectorRemove state float vectorFloat + +instructionVectorFloatIterate :: State -> State +instructionVectorFloatIterate state = instructionVectorIterate state float vectorFloat GeneVectorFloat instructionVectorFloatIterate diff --git a/src/Instructions/VectorLogicalInstructions.hs b/src/Instructions/VectorLogicalInstructions.hs new file mode 100644 index 0000000..e9a4a3f --- /dev/null +++ b/src/Instructions/VectorLogicalInstructions.hs @@ -0,0 +1,67 @@ +module Instructions.VectorLogicalInstructions where + +import State +import Instructions.GenericInstructions + +instructionVectorBoolConcat :: State -> State +instructionVectorBoolConcat state = instructionConcat state vectorBool + +instructionVectorBoolConj :: State -> State +instructionVectorBoolConj state = instructionConj state bool vectorBool + +instructionVectorBoolTakeN :: State -> State +instructionVectorBoolTakeN state = instructionTakeN state vectorBool + +instructionVectorBoolSubVector :: State -> State +instructionVectorBoolSubVector state = instructionSubVector state vectorBool + +instructionVectorBoolFirst :: State -> State +instructionVectorBoolFirst state = instructionVectorFirst state bool vectorBool + +instructionVectorBoolLast :: State -> State +instructionVectorBoolLast state = instructionVectorLast state bool vectorBool + +instructionVectorBoolNth :: State -> State +instructionVectorBoolNth state = instructionVectorNth state bool vectorBool + +instructionVectorBoolRest :: State -> State +instructionVectorBoolRest state = instructionRest state vectorBool + +instructionVectorBoolButLast :: State -> State +instructionVectorBoolButLast state = instructionButLast state vectorBool + +instructionVectorBoolLength :: State -> State +instructionVectorBoolLength state = instructionLength state vectorBool + +instructionVectorBoolReverse :: State -> State +instructionVectorBoolReverse state = instructionReverse state vectorBool + +instructionVectorBoolPushAll :: State -> State +instructionVectorBoolPushAll state = instructionPushAll state bool vectorBool + +instructionVectorBoolMakeEmpty :: State -> State +instructionVectorBoolMakeEmpty state = instructionVectorMakeEmpty state vectorBool + +instructionVectorBoolIsEmpty :: State -> State +instructionVectorBoolIsEmpty state = instructionVectorIsEmpty state vectorBool + +instructionVectorBoolIndexOf :: State -> State +instructionVectorBoolIndexOf state = instructionVectorIndexOf state bool vectorBool + +instructionVectorBoolOccurrencesOf :: State -> State +instructionVectorBoolOccurrencesOf state = instructionVectorOccurrencesOf state bool vectorBool + +instructionVectorBoolSetNth :: State -> State +instructionVectorBoolSetNth state = instructionVectorSetNth state bool vectorBool + +instructionVectorBoolReplace :: State -> State +instructionVectorBoolReplace state = instructionVectorReplace state bool vectorBool + +instructionVectorBoolReplaceFirst :: State -> State +instructionVectorBoolReplaceFirst state = instructionVectorReplaceFirst state bool vectorBool + +instructionVectorBoolRemove :: State -> State +instructionVectorBoolRemove state = instructionVectorRemove state bool vectorBool + +instructionVectorBoolIterate :: State -> State +instructionVectorBoolIterate state = instructionVectorIterate state bool vectorBool GeneVectorBool instructionVectorBoolIterate diff --git a/src/Instructions/VectorStringInstructions.hs b/src/Instructions/VectorStringInstructions.hs new file mode 100644 index 0000000..23ec76f --- /dev/null +++ b/src/Instructions/VectorStringInstructions.hs @@ -0,0 +1,67 @@ +module Instructions.VectorStringInstructions where + +import State +import Instructions.GenericInstructions + +instructionVectorStringConcat :: State -> State +instructionVectorStringConcat state = instructionConcat state vectorString + +instructionVectorStringConj :: State -> State +instructionVectorStringConj state = instructionConj state string vectorString + +instructionVectorStringTakeN :: State -> State +instructionVectorStringTakeN state = instructionTakeN state vectorString + +instructionVectorStringSubVector :: State -> State +instructionVectorStringSubVector state = instructionSubVector state vectorString + +instructionVectorStringFirst :: State -> State +instructionVectorStringFirst state = instructionVectorFirst state string vectorString + +instructionVectorStringLast :: State -> State +instructionVectorStringLast state = instructionVectorLast state string vectorString + +instructionVectorStringNth :: State -> State +instructionVectorStringNth state = instructionVectorNth state string vectorString + +instructionVectorStringRest :: State -> State +instructionVectorStringRest state = instructionRest state vectorString + +instructionVectorStringButLast :: State -> State +instructionVectorStringButLast state = instructionButLast state vectorString + +instructionVectorStringLength :: State -> State +instructionVectorStringLength state = instructionLength state vectorString + +instructionVectorStringReverse :: State -> State +instructionVectorStringReverse state = instructionReverse state vectorString + +instructionVectorStringPushAll :: State -> State +instructionVectorStringPushAll state = instructionPushAll state string vectorString + +instructionVectorStringMakeEmpty :: State -> State +instructionVectorStringMakeEmpty state = instructionVectorMakeEmpty state vectorString + +instructionVectorStringIsEmpty :: State -> State +instructionVectorStringIsEmpty state = instructionVectorIsEmpty state vectorString + +instructionVectorStringIndexOf :: State -> State +instructionVectorStringIndexOf state = instructionVectorIndexOf state string vectorString + +instructionVectorStringOccurrencesOf :: State -> State +instructionVectorStringOccurrencesOf state = instructionVectorOccurrencesOf state string vectorString + +instructionVectorStringSetNth :: State -> State +instructionVectorStringSetNth state = instructionVectorSetNth state string vectorString + +instructionVectorStringReplace :: State -> State +instructionVectorStringReplace state = instructionVectorReplace state string vectorString + +instructionVectorStringReplaceFirst :: State -> State +instructionVectorStringReplaceFirst state = instructionVectorReplaceFirst state string vectorString + +instructionVectorStringRemove :: State -> State +instructionVectorStringRemove state = instructionVectorRemove state string vectorString + +instructionVectorStringIterate :: State -> State +instructionVectorStringIterate state = instructionVectorIterate state string vectorString GeneVectorString instructionVectorStringIterate From d7a2a4a6a6b47610bdbd92fb38fb625f6e119f80 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Wed, 29 Jan 2025 16:35:22 -0600 Subject: [PATCH 085/171] add more instructions --- src/Instructions.hs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Instructions.hs b/src/Instructions.hs index 4b6597d..9856818 100644 --- a/src/Instructions.hs +++ b/src/Instructions.hs @@ -8,7 +8,10 @@ module Instructions ( module Instructions.ExecInstructions, module Instructions.LogicalInstructions, module Instructions.VectorIntInstructions, - module Instructions.VectorFloatInstructions + module Instructions.VectorFloatInstructions, + module Instructions.VectorStringInstructions, + module Instructions.VectorLogicalInstructions, + module Instructions.VectorCharInstructions ) where @@ -22,3 +25,6 @@ import Instructions.ExecInstructions import Instructions.LogicalInstructions import Instructions.VectorIntInstructions import Instructions.VectorFloatInstructions +import Instructions.VectorStringInstructions +import Instructions.VectorLogicalInstructions +import Instructions.VectorCharInstructions From 206a3217d9cb2e5e0821e7253a46f30fa3f5f69b Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Wed, 29 Jan 2025 16:35:40 -0600 Subject: [PATCH 086/171] need to redo this, this isn't working. Thinking quickCheck --- test/Main.hs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/test/Main.hs b/test/Main.hs index b930ccd..6104e91 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -42,6 +42,11 @@ vectorIntTestFunc name goal genome startState = let state = loadProgram genome startState in assert (goal == _vectorInt (interpretExec state)) putStrLn (name <> " passed test.") +vectorFloatTestFunc :: String -> [[Float]] -> [Gene] -> State -> IO () +vectorFloatTestFunc name goal genome startState = + let state = loadProgram genome startState + in assert (goal == _vectorFloat (interpretExec state)) putStrLn (name <> " passed test.") + main :: IO () main = do -- Int tests @@ -282,3 +287,37 @@ main = do vectorIntTestFunc "instructionVectorIntReplaceFirst-2" [[0,1,2,3,4,5,3,5,3]] [GeneInt 99, GeneInt (-2), GeneVectorInt [0,1,2,3,4,5,3,5,3], StateFunc instructionVectorIntReplaceFirst] emptyState vectorIntTestFunc "instructionVectorIntRemove" [[0,1,2,4,5,5]] [GeneInt 3, GeneVectorInt [0,1,2,3,4,5,3,5,3], StateFunc instructionVectorIntRemove] emptyState intTestFunc "instructionVectorIntIterate" [66] [GeneInt 40, GeneVectorInt [0,1,2,3,4,5,3,5,3], StateFunc instructionVectorIntIterate, StateFunc instructionIntAdd] emptyState + + -- vector float functions + vectorFloatTestFunc "instructionVectorFloatConcat" [[4.0, 5.0, 6.0, 1.0, 2.0, 3.0]] [GeneVectorFloat [1.0, 2.0, 3.0], GeneVectorFloat [4.0, 5.0, 6.0], StateFunc instructionVectorFloatConcat] emptyState + vectorFloatTestFunc "instructionVectorFloatConj" [[99.0, 1.0, 2.0, 3.0]] [GeneVectorFloat [1.0, 2.0, 3.0], GeneFloat 99.0, StateFunc instructionVectorFloatConj] emptyState + vectorFloatTestFunc "instructionFloatTakeN" [[1.0, 2.0], [6.0, 7.0, 8.0]] [GeneVectorFloat [6.0, 7.0, 8.0], GeneVectorFloat [1.0, 2.0, 3.0], GeneInt 2, StateFunc instructionVectorFloatTakeN] emptyState + vectorFloatTestFunc "instructionVectorFloatSubVector" [[1.0, 2.0, 3.0]] [GeneVectorFloat [0.0, 1.0, 2.0, 3.0, 4.0, 5.0], GeneInt 3, GeneInt 1, StateFunc instructionVectorFloatSubVector] emptyState + floatTestFunc "instructionVectorFloatFirst" [1.0] [GeneVectorFloat [1.0,2.0,3.0,4.0,5.0], StateFunc instructionVectorFloatFirst] emptyState + floatTestFunc "instructionVectorFloatLast" [5.0] [GeneVectorFloat [1.0,2.0,3.0,4.0,5.0], StateFunc instructionVectorFloatLast] emptyState + floatTestFunc "instructionVectorFloatNthInBounds" [2.0] [GeneVectorFloat [1.0,2.0,3.0,4.0,5.0], GeneInt 1, StateFunc instructionVectorFloatNth] emptyState + floatTestFunc "instructionVectorFloatNthOverflow" [2.0] [GeneVectorFloat [1.0,2.0,3.0,4.0,5.0], GeneInt 6, StateFunc instructionVectorFloatNth] emptyState + vectorFloatTestFunc "instructionVectorFloatRestFull" [[2.0,3.0,4.0,5.0]] [GeneVectorFloat [1.0,2.0,3.0,4.0,5.0], StateFunc instructionVectorFloatRest] emptyState + vectorFloatTestFunc "instructionVectorFloatRestEmpty" [[]] [GeneVectorFloat [], StateFunc instructionVectorFloatRest] emptyState + vectorFloatTestFunc "instructionVectorFloatButLastFull" [[1.0,2.0,3.0,4.0]] [GeneVectorFloat [1.0,2.0,3.0,4.0,5.0], StateFunc instructionVectorFloatButLast] emptyState + vectorFloatTestFunc "instructionVectorFloatButLastEmpty" [[]] [GeneVectorFloat [], StateFunc instructionVectorFloatButLast] emptyState + intTestFunc "instructionVectorFloatLength3" [3] [GeneVectorFloat [1.0,2.0,3.0], StateFunc instructionVectorFloatLength] emptyState + intTestFunc "instructionVectorFloatLength0" [0] [GeneVectorFloat [], StateFunc instructionVectorFloatLength] emptyState + vectorFloatTestFunc "instructionVectorFloatReverse" [[4.0,3.0,2.0,1.0]] [GeneVectorFloat [1.0,2.0,3.0,4.0], StateFunc instructionVectorFloatReverse] emptyState + floatTestFunc "instructionVectorFloatPushAllFull" [1.0,2.0,3.0,4.0,99.0] [GeneVectorFloat [1.0,2.0,3.0,4.0], GeneFloat 99.0, StateFunc instructionVectorFloatPushAll] emptyState + floatTestFunc "instructionVectorFloatPushAllEmpty" [99.0] [GeneVectorFloat [], GeneFloat 99.0, StateFunc instructionVectorFloatPushAll] emptyState + vectorFloatTestFunc "instructionVectorFloatMakeEmpty" [[]] [StateFunc instructionVectorFloatMakeEmpty] emptyState + boolTestFunc "instructionVectorFloatIsEmptyTrue" [True] [GeneVectorFloat [], StateFunc instructionVectorFloatIsEmpty] emptyState + boolTestFunc "instructionVectorFloatIsEmptyFalse" [False] [GeneVectorFloat [1.0,2.0,3.0,4.0], StateFunc instructionVectorFloatIsEmpty] emptyState + intTestFunc "instructionVectorFloatIndexOf1" [1] [GeneVectorFloat [1.0,2.0,3.0,4.0,5.0], GeneFloat 2.0, StateFunc instructionVectorFloatIndexOf] emptyState + intTestFunc "instructionVectorFloatIndexOfFail" [-1] [GeneVectorFloat [], GeneFloat 2.0, StateFunc instructionVectorFloatIndexOf] emptyState + intTestFunc "instructionVectorFloatOccurrencesOf2" [2] [GeneVectorFloat [1.0,2.0,3.0,4.0,2.0,6.0,7.0], GeneFloat 2.0, StateFunc instructionVectorFloatOccurrencesOf] emptyState + intTestFunc "instructionVectorFloatOccurrencesOf0" [0] [GeneVectorFloat [1.0,2.0,3.0,4.0,2.0,6.0,7.0], GeneFloat 0.0, StateFunc instructionVectorFloatOccurrencesOf] emptyState + vectorFloatTestFunc "instructionVectorFloatSetNth3" [[0.0,1.0,2.0,99.0,4.0,5.0]] [GeneVectorFloat [0.0,1.0,2.0,3.0,4.0,5.0], GeneFloat 99.0, GeneInt 3, StateFunc instructionVectorFloatSetNth] emptyState + vectorFloatTestFunc "instructionVectorFloatSetNth9" [[0.0,1.0,2.0,99.0,4.0,5.0]] [GeneVectorFloat [0.0,1.0,2.0,3.0,4.0,5.0], GeneFloat 99.0, GeneInt 9, StateFunc instructionVectorFloatSetNth] emptyState + vectorFloatTestFunc "instructionVectorFloatReplace3" [[0.0,1.0,2.0,99.0,4.0,5.0,99.0,5.0,99.0]] [GeneFloat 99.0, GeneFloat 3.0, GeneVectorFloat [0.0,1.0,2.0,3.0,4.0,5.0,3.0,5.0,3.0], StateFunc instructionVectorFloatReplace] emptyState + vectorFloatTestFunc "instructionVectorFloatReplace-1" [[0.0,1.0,2.0,3.0,4.0,5.0,3.0,5.0,3.0]] [GeneFloat 99.0, GeneFloat (-1.0), GeneVectorFloat [0.0,1.0,2.0,3.0,4.0,5.0,3.0,5.0,3.0], StateFunc instructionVectorFloatReplace] emptyState + vectorFloatTestFunc "instructionVectorFloatReplaceFirst3" [[0.0,1.0,2.0,99.0,4.0,5.0,3.0,5.0,3.0]] [GeneFloat 99.0, GeneFloat 3.0, GeneVectorFloat [0.0,1.0,2.0,3.0,4.0,5.0,3.0,5.0,3.0], StateFunc instructionVectorFloatReplaceFirst] emptyState + vectorFloatTestFunc "instructionVectorFloatReplaceFirst-2" [[0.0,1.0,2.0,3.0,4.0,5.0,3.0,5.0,3.0]] [GeneFloat 99.0, GeneFloat (-2.0), GeneVectorFloat [0.0,1.0,2.0,3.0,4.0,5.0,3.0,5.0,3.0], StateFunc instructionVectorFloatReplaceFirst] emptyState + vectorFloatTestFunc "instructionVectorFloatRemove" [[0.0,1.0,2.0,4.0,5.0,5.0]] [GeneFloat 3, GeneVectorFloat [0.0,1.0,2.0,3.0,4.0,5.0,3.0,5.0,3.0], StateFunc instructionVectorFloatRemove] emptyState + floatTestFunc "instructionVectorFloatIterate" [66.0] [GeneFloat 40.0, GeneVectorFloat [0.0,1.0,2.0,3.0,4.0,5.0,3.0,5.0,3.0], StateFunc instructionVectorFloatIterate, StateFunc instructionFloatAdd] emptyState From 63e9cff55ed8cf70924fdd23f96850d381f4c3cb Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Wed, 29 Jan 2025 23:43:05 -0600 Subject: [PATCH 087/171] finish up the instructions --- src/Instructions/CharInstructions.hs | 39 ++++++++++ src/Instructions/CodeInstructions.hs | 76 ++++++++++++++++++- src/Instructions/ExecInstructions.hs | 15 ++++ src/Instructions/FloatInstructions.hs | 3 + src/Instructions/GenericInstructions.hs | 9 +++ src/Instructions/IntInstructions.hs | 3 + src/Instructions/LogicalInstructions.hs | 40 ++++++++++ src/Instructions/StringInstructions.hs | 36 +++++++++ src/Instructions/VectorCharInstructions.hs | 39 ++++++++++ src/Instructions/VectorFloatInstructions.hs | 39 ++++++++++ src/Instructions/VectorIntInstructions.hs | 39 ++++++++++ src/Instructions/VectorLogicalInstructions.hs | 39 ++++++++++ src/Instructions/VectorStringInstructions.hs | 39 ++++++++++ 13 files changed, 413 insertions(+), 3 deletions(-) diff --git a/src/Instructions/CharInstructions.hs b/src/Instructions/CharInstructions.hs index 7d4d94d..29fcdd9 100644 --- a/src/Instructions/CharInstructions.hs +++ b/src/Instructions/CharInstructions.hs @@ -48,3 +48,42 @@ instructionCharFromAsciiFloat state = state instructionCharsFromString :: State -> State instructionCharsFromString state@(State {_char = cs, _string = s1 : ss}) = state{_char = s1 <> cs, _string = ss} instructionCharsFromString state = state + +instructionCharPop :: State -> State +instructionCharPop state = instructionPop state char + +instructionCharDup :: State -> State +instructionCharDup state = instructionDup state char + +instructionCharDupN :: State -> State +instructionCharDupN state = instructionDupN state char + +instructionCharSwap :: State -> State +instructionCharSwap state = instructionSwap state char + +instructionCharRot :: State -> State +instructionCharRot state = instructionRot state char + +instructionCharFlush :: State -> State +instructionCharFlush state = instructionFlush state char + +instructionCharEq :: State -> State +instructionCharEq state = instructionEq state char + +instructionCharStackDepth :: State -> State +instructionCharStackDepth state = instructionStackDepth state char + +instructionCharYank :: State -> State +instructionCharYank state = instructionYank state char + +instructionCharYankDup :: State -> State +instructionCharYankDup state = instructionYankDup state char + +instructionCharIsEmpty :: State -> State +instructionCharIsEmpty state = instructionIsEmpty state char + +instructionCharShove :: State -> State +instructionCharShove state = instructionShove state char + +instructionCharShoveDup :: State -> State +instructionCharShoveDup state = instructionShoveDup state char diff --git a/src/Instructions/CodeInstructions.hs b/src/Instructions/CodeInstructions.hs index a88626d..54cc8de 100644 --- a/src/Instructions/CodeInstructions.hs +++ b/src/Instructions/CodeInstructions.hs @@ -4,6 +4,7 @@ import Data.List (elemIndex) import State import Instructions.GenericInstructions import Instructions.IntInstructions +import Control.Lens -- import Debug.Trace @@ -75,9 +76,9 @@ codeRecursiveSize _ = 1 instructionCodePop :: State -> State instructionCodePop state = instructionPop state code -instructionCodeFromExec :: State -> State -instructionCodeFromExec state@(State {_exec = (e1 : es), _code = cs}) = state {_exec = es, _code = e1 : cs} -instructionCodeFromExec state = state +-- instructionCodeFromExec :: State -> State +-- instructionCodeFromExec state@(State {_exec = (e1 : es), _code = cs}) = state {_exec = es, _code = e1 : cs} +-- instructionCodeFromExec state = state instructionCodeIsCodeBlock :: State -> State instructionCodeIsCodeBlock state@(State {_code = (c : cs), _bool = bs}) = state {_code = cs, _bool = isBlock c : bs} @@ -244,3 +245,72 @@ instructionCodeFirstPosition state = state instructionCodeReverse :: State -> State instructionCodeReverse state@(State {_code = (Block c1) : cs}) = state {_code = Block (reverse c1) : cs} instructionCodeReverse state = state + +instructionCodeDup :: State -> State +instructionCodeDup state = instructionDup state code + +instructionCodeDupN :: State -> State +instructionCodeDupN state = instructionDupN state code + +instructionCodeSwap :: State -> State +instructionCodeSwap state = instructionSwap state code + +instructionCodeRot :: State -> State +instructionCodeRot state = instructionRot state code + +instructionCodeFlush :: State -> State +instructionCodeFlush state = instructionFlush state code + +instructionCodeEq :: State -> State +instructionCodeEq state = instructionEq state code + +instructionCodeStackDepth :: State -> State +instructionCodeStackDepth state = instructionStackDepth state code + +instructionCodeYank :: State -> State +instructionCodeYank state = instructionYank state code + +instructionCodeYankDup :: State -> State +instructionCodeYankDup state = instructionYankDup state code + +instructionCodeStackIsEmpty :: State -> State +instructionCodeStackIsEmpty state = instructionIsEmpty state code + +instructionCodeShove :: State -> State +instructionCodeShove state = instructionShove state code + +instructionCodeShoveDup :: State -> State +instructionCodeShoveDup state = instructionShoveDup state code + +instructionCodeFromBool :: State -> State +instructionCodeFromBool state = instructionCodeFrom state bool GeneBool + +instructionCodeFromInt :: State -> State +instructionCodeFromInt state = instructionCodeFrom state int GeneInt + +instructionCodeFromChar :: State -> State +instructionCodeFromChar state = instructionCodeFrom state char GeneChar + +instructionCodeFromFloat :: State -> State +instructionCodeFromFloat state = instructionCodeFrom state float GeneFloat + +instructionCodeFromString :: State -> State +instructionCodeFromString state = instructionCodeFrom state string GeneString + +instructionCodeFromVectorInt :: State -> State +instructionCodeFromVectorInt state = instructionCodeFrom state vectorInt GeneVectorInt + +instructionCodeFromVectorFloat :: State -> State +instructionCodeFromVectorFloat state = instructionCodeFrom state vectorFloat GeneVectorFloat + +instructionCodeFromVectorString :: State -> State +instructionCodeFromVectorString state = instructionCodeFrom state vectorString GeneVectorString + +instructionCodeFromVectorBool :: State -> State +instructionCodeFromVectorBool state = instructionCodeFrom state vectorBool GeneVectorBool + +instructionCodeFromVectorChar :: State -> State +instructionCodeFromVectorChar state = instructionCodeFrom state vectorChar GeneVectorChar + +instructionCodeFromExec :: State -> State +instructionCodeFromExec state = instructionCodeFrom state exec id diff --git a/src/Instructions/ExecInstructions.hs b/src/Instructions/ExecInstructions.hs index 4177292..9b6357a 100644 --- a/src/Instructions/ExecInstructions.hs +++ b/src/Instructions/ExecInstructions.hs @@ -35,6 +35,21 @@ instructionExecEq state = instructionEq state exec instructionExecStackDepth :: State -> State instructionExecStackDepth state = instructionStackDepth state exec +instructionExecYank :: State -> State +instructionExecYank state = instructionYank state exec + +instructionExecYankDup :: State -> State +instructionExecYankDup state = instructionYankDup state exec + +instructionExecShove :: State -> State +instructionExecShove state = instructionShove state exec + +instructionExecShoveDup :: State -> State +instructionExecShoveDup state = instructionShoveDup state exec + +instructionExecIsEmpty :: State -> State +instructionExecIsEmpty state = instructionIsEmpty state exec + instructionExecDoRange :: State -> State instructionExecDoRange state@(State {_exec = (e1 : es), _int = (i0 : i1 : is)}) = if increment i0 i1 /= 0 diff --git a/src/Instructions/FloatInstructions.hs b/src/Instructions/FloatInstructions.hs index ede4a47..3891ed1 100644 --- a/src/Instructions/FloatInstructions.hs +++ b/src/Instructions/FloatInstructions.hs @@ -100,6 +100,9 @@ instructionFloatShoveDup state = instructionShoveDup state float instructionFloatShove :: State -> State instructionFloatShove state = instructionShove state float +instructionFloatIsEmpty :: State -> State +instructionFloatIsEmpty state = instructionIsEmpty state float + instructionFloatSin :: State -> State instructionFloatSin state@(State {_float = f1 : fs}) = state {_float = sin f1 : fs} instructionFloatSin state = state diff --git a/src/Instructions/GenericInstructions.hs b/src/Instructions/GenericInstructions.hs index 89bcd4d..546e6e3 100644 --- a/src/Instructions/GenericInstructions.hs +++ b/src/Instructions/GenericInstructions.hs @@ -90,6 +90,9 @@ instructionDup state accessor = instructionPop :: State -> Lens' State [a] -> State instructionPop state accessor = state & accessor .~ drop 1 (view accessor state) +instructionIsEmpty :: State -> Lens' State [a] -> State +instructionIsEmpty state@(State {_bool = bs}) accessor = state{_bool = null (view accessor state) : bs} + -- instructionPop :: State -> Lens' State [a] -> State -- instructionPop state accessor = if notEmptyStack state accessor then instructionPop state accessor else state @@ -336,3 +339,9 @@ instructionVectorIterate state@(State {_exec = e1 : es}) primAccessor vectorAcce _ -> state) -- This should never happen _ -> state instructionVectorIterate state _ _ _ _ = state + +instructionCodeFrom :: State -> Lens' State [a] -> (a -> Gene) -> State +instructionCodeFrom state@(State {_code = cs}) accessor geneType = + case uncons (view accessor state) of + Just (x, xs) -> state{_code = geneType x : cs} & accessor .~ xs + _ -> state diff --git a/src/Instructions/IntInstructions.hs b/src/Instructions/IntInstructions.hs index bdbc783..a404028 100644 --- a/src/Instructions/IntInstructions.hs +++ b/src/Instructions/IntInstructions.hs @@ -125,3 +125,6 @@ instructionIntShoveDup state@(State {_int = rawIndex : item : is}) = in state {_int = item : combineTuple item (splitAt myIndex is)} instructionIntShoveDup state = state + +instructionIntIsEmpty :: State -> State +instructionIntIsEmpty state = instructionIsEmpty state int diff --git a/src/Instructions/LogicalInstructions.hs b/src/Instructions/LogicalInstructions.hs index e94b66e..a10e96f 100644 --- a/src/Instructions/LogicalInstructions.hs +++ b/src/Instructions/LogicalInstructions.hs @@ -1,6 +1,7 @@ module Instructions.LogicalInstructions where import State +import Instructions.GenericInstructions instructionBoolFromInt :: State -> State instructionBoolFromInt state@(State {_int = (i : is), _bool = bs}) = state {_int = is, _bool = (i /= 0) : bs} @@ -37,3 +38,42 @@ xor b1 b2 instructionBoolXor :: State -> State instructionBoolXor = boolTemplate xor + +instructionBoolPop :: State -> State +instructionBoolPop state = instructionPop state bool + +instructionBoolDup :: State -> State +instructionBoolDup state = instructionDup state bool + +instructionBoolDupN :: State -> State +instructionBoolDupN state = instructionDupN state bool + +instructionBoolSwap :: State -> State +instructionBoolSwap state = instructionSwap state bool + +instructionBoolRot :: State -> State +instructionBoolRot state = instructionRot state bool + +instructionBoolFlush :: State -> State +instructionBoolFlush state = instructionFlush state bool + +instructionBoolEq :: State -> State +instructionBoolEq state = instructionEq state bool + +instructionBoolStackDepth :: State -> State +instructionBoolStackDepth state = instructionStackDepth state bool + +instructionBoolYank :: State -> State +instructionBoolYank state = instructionYank state bool + +instructionBoolYankDup :: State -> State +instructionBoolYankDup state = instructionYankDup state bool + +instructionBoolShove :: State -> State +instructionBoolShove state = instructionShove state bool + +instructionBoolShoveDup :: State -> State +instructionBoolShoveDup state = instructionShoveDup state bool + +instructionBoolIsEmpty :: State -> State +instructionBoolIsEmpty state = instructionIsEmpty state bool diff --git a/src/Instructions/StringInstructions.hs b/src/Instructions/StringInstructions.hs index e828677..0c7073a 100644 --- a/src/Instructions/StringInstructions.hs +++ b/src/Instructions/StringInstructions.hs @@ -196,3 +196,39 @@ instructionStringFromFloat state = instructionStringFromLens state float instructionStringFromChar :: State -> State instructionStringFromChar state@(State {_string = ss, _char = c1 : cs}) = state{_string = [c1] : ss, _char = cs} instructionStringFromChar state = state + +instructionStringPop :: State -> State +instructionStringPop state = instructionPop state string + +instructionStringDup :: State -> State +instructionStringDup state = instructionDup state string + +instructionStringDupN :: State -> State +instructionStringDupN state = instructionDupN state string + +instructionStringRot :: State -> State +instructionStringRot state = instructionRot state string + +instructionStringFlush :: State -> State +instructionStringFlush state = instructionFlush state string + +instructionStringEq :: State -> State +instructionStringEq state = instructionEq state string + +instructionStringStackDepth :: State -> State +instructionStringStackDepth state = instructionStackDepth state string + +instructionStringYank :: State -> State +instructionStringYank state = instructionYank state string + +instructionStringYankDup :: State -> State +instructionStringYankDup state = instructionYankDup state string + +instructionStringIsEmpty :: State -> State +instructionStringIsEmpty state = instructionIsEmpty state string + +instructionStringShove :: State -> State +instructionStringShove state = instructionShove state string + +instructionStringShoveDup :: State -> State +instructionStringShoveDup state = instructionShoveDup state string diff --git a/src/Instructions/VectorCharInstructions.hs b/src/Instructions/VectorCharInstructions.hs index e0c6fd1..30c9d0b 100644 --- a/src/Instructions/VectorCharInstructions.hs +++ b/src/Instructions/VectorCharInstructions.hs @@ -65,3 +65,42 @@ instructionVectorCharRemove state = instructionVectorRemove state char vectorCha instructionVectorCharIterate :: State -> State instructionVectorCharIterate state = instructionVectorIterate state char vectorChar GeneVectorChar instructionVectorCharIterate + +instructionVectorCharPop :: State -> State +instructionVectorCharPop state = instructionPop state vectorChar + +instructionVectorCharDup :: State -> State +instructionVectorCharDup state = instructionDup state vectorChar + +instructionVectorCharDupN :: State -> State +instructionVectorCharDupN state = instructionDupN state vectorChar + +instructionVectorCharSwap :: State -> State +instructionVectorCharSwap state = instructionSwap state vectorChar + +instructionVectorCharRot :: State -> State +instructionVectorCharRot state = instructionRot state vectorChar + +instructionVectorCharFlush :: State -> State +instructionVectorCharFlush state = instructionFlush state vectorChar + +instructionVectorCharEq :: State -> State +instructionVectorCharEq state = instructionEq state vectorChar + +instructionVectorCharStackDepth :: State -> State +instructionVectorCharStackDepth state = instructionStackDepth state vectorChar + +instructionVectorCharYank :: State -> State +instructionVectorCharYank state = instructionYank state vectorChar + +instructionVectorCharYankDup :: State -> State +instructionVectorCharYankDup state = instructionYankDup state vectorChar + +instructionVectorCharStackIsEmpty :: State -> State +instructionVectorCharStackIsEmpty state = instructionIsEmpty state vectorChar + +instructionVectorCharShove :: State -> State +instructionVectorCharShove state = instructionShove state vectorChar + +instructionVectorCharShoveDup :: State -> State +instructionVectorCharShoveDup state = instructionShoveDup state vectorChar diff --git a/src/Instructions/VectorFloatInstructions.hs b/src/Instructions/VectorFloatInstructions.hs index 0006903..b45f2dc 100644 --- a/src/Instructions/VectorFloatInstructions.hs +++ b/src/Instructions/VectorFloatInstructions.hs @@ -65,3 +65,42 @@ instructionVectorFloatRemove state = instructionVectorRemove state float vectorF instructionVectorFloatIterate :: State -> State instructionVectorFloatIterate state = instructionVectorIterate state float vectorFloat GeneVectorFloat instructionVectorFloatIterate + +instructionVectorFloatPop :: State -> State +instructionVectorFloatPop state = instructionPop state vectorFloat + +instructionVectorFloatDup :: State -> State +instructionVectorFloatDup state = instructionDup state vectorFloat + +instructionVectorFloatDupN :: State -> State +instructionVectorFloatDupN state = instructionDupN state vectorFloat + +instructionVectorFloatSwap :: State -> State +instructionVectorFloatSwap state = instructionSwap state vectorFloat + +instructionVectorFloatRot :: State -> State +instructionVectorFloatRot state = instructionRot state vectorFloat + +instructionVectorFloatFlush :: State -> State +instructionVectorFloatFlush state = instructionFlush state vectorFloat + +instructionVectorFloatEq :: State -> State +instructionVectorFloatEq state = instructionEq state vectorFloat + +instructionVectorFloatStackDepth :: State -> State +instructionVectorFloatStackDepth state = instructionStackDepth state vectorFloat + +instructionVectorFloatYank :: State -> State +instructionVectorFloatYank state = instructionYank state vectorFloat + +instructionVectorFloatYankDup :: State -> State +instructionVectorFloatYankDup state = instructionYankDup state vectorFloat + +instructionVectorFloatStackIsEmpty :: State -> State +instructionVectorFloatStackIsEmpty state = instructionIsEmpty state vectorFloat + +instructionVectorFloatShove :: State -> State +instructionVectorFloatShove state = instructionShove state vectorFloat + +instructionVectorFloatShoveDup :: State -> State +instructionVectorFloatShoveDup state = instructionShoveDup state vectorFloat diff --git a/src/Instructions/VectorIntInstructions.hs b/src/Instructions/VectorIntInstructions.hs index 38c63a9..7bf3bf3 100644 --- a/src/Instructions/VectorIntInstructions.hs +++ b/src/Instructions/VectorIntInstructions.hs @@ -65,3 +65,42 @@ instructionVectorIntRemove state = instructionVectorRemove state int vectorInt instructionVectorIntIterate :: State -> State instructionVectorIntIterate state = instructionVectorIterate state int vectorInt GeneVectorInt instructionVectorIntIterate + +instructionVectorIntPop :: State -> State +instructionVectorIntPop state = instructionPop state vectorChar + +instructionVectorIntDup :: State -> State +instructionVectorIntDup state = instructionDup state vectorChar + +instructionVectorIntDupN :: State -> State +instructionVectorIntDupN state = instructionDupN state vectorChar + +instructionVectorIntSwap :: State -> State +instructionVectorIntSwap state = instructionSwap state vectorChar + +instructionVectorIntRot :: State -> State +instructionVectorIntRot state = instructionRot state vectorChar + +instructionVectorIntFlush :: State -> State +instructionVectorIntFlush state = instructionFlush state vectorChar + +instructionVectorIntEq :: State -> State +instructionVectorIntEq state = instructionEq state vectorChar + +instructionVectorIntStackDepth :: State -> State +instructionVectorIntStackDepth state = instructionStackDepth state vectorChar + +instructionVectorIntYank :: State -> State +instructionVectorIntYank state = instructionYank state vectorChar + +instructionVectorIntYankDup :: State -> State +instructionVectorIntYankDup state = instructionYankDup state vectorChar + +instructionVectorIntStackIsEmpty :: State -> State +instructionVectorIntStackIsEmpty state = instructionIsEmpty state vectorChar + +instructionVectorIntShove :: State -> State +instructionVectorIntShove state = instructionShove state vectorChar + +instructionVectorIntShoveDup :: State -> State +instructionVectorIntShoveDup state = instructionShoveDup state vectorChar diff --git a/src/Instructions/VectorLogicalInstructions.hs b/src/Instructions/VectorLogicalInstructions.hs index e9a4a3f..af5e0f5 100644 --- a/src/Instructions/VectorLogicalInstructions.hs +++ b/src/Instructions/VectorLogicalInstructions.hs @@ -65,3 +65,42 @@ instructionVectorBoolRemove state = instructionVectorRemove state bool vectorBoo instructionVectorBoolIterate :: State -> State instructionVectorBoolIterate state = instructionVectorIterate state bool vectorBool GeneVectorBool instructionVectorBoolIterate + +instructionVectorBoolPop :: State -> State +instructionVectorBoolPop state = instructionPop state vectorBool + +instructionVectorBoolDup :: State -> State +instructionVectorBoolDup state = instructionDup state vectorBool + +instructionVectorBoolDupN :: State -> State +instructionVectorBoolDupN state = instructionDupN state vectorBool + +instructionVectorBoolSwap :: State -> State +instructionVectorBoolSwap state = instructionSwap state vectorBool + +instructionVectorBoolRot :: State -> State +instructionVectorBoolRot state = instructionRot state vectorBool + +instructionVectorBoolFlush :: State -> State +instructionVectorBoolFlush state = instructionFlush state vectorBool + +instructionVectorBoolEq :: State -> State +instructionVectorBoolEq state = instructionEq state vectorBool + +instructionVectorBoolStackDepth :: State -> State +instructionVectorBoolStackDepth state = instructionStackDepth state vectorBool + +instructionVectorBoolYank :: State -> State +instructionVectorBoolYank state = instructionYank state vectorBool + +instructionVectorBoolYankDup :: State -> State +instructionVectorBoolYankDup state = instructionYankDup state vectorBool + +instructionVectorBoolStackIsEmpty :: State -> State +instructionVectorBoolStackIsEmpty state = instructionIsEmpty state vectorBool + +instructionVectorBoolShove :: State -> State +instructionVectorBoolShove state = instructionShove state vectorBool + +instructionVectorBoolShoveDup :: State -> State +instructionVectorBoolShoveDup state = instructionShoveDup state vectorBool diff --git a/src/Instructions/VectorStringInstructions.hs b/src/Instructions/VectorStringInstructions.hs index 23ec76f..ee524d6 100644 --- a/src/Instructions/VectorStringInstructions.hs +++ b/src/Instructions/VectorStringInstructions.hs @@ -65,3 +65,42 @@ instructionVectorStringRemove state = instructionVectorRemove state string vecto instructionVectorStringIterate :: State -> State instructionVectorStringIterate state = instructionVectorIterate state string vectorString GeneVectorString instructionVectorStringIterate + +instructionVectorStringPop :: State -> State +instructionVectorStringPop state = instructionPop state vectorString + +instructionVectorStringDup :: State -> State +instructionVectorStringDup state = instructionDup state vectorString + +instructionVectorStringDupN :: State -> State +instructionVectorStringDupN state = instructionDupN state vectorString + +instructionVectorStringSwap :: State -> State +instructionVectorStringSwap state = instructionSwap state vectorString + +instructionVectorStringRot :: State -> State +instructionVectorStringRot state = instructionRot state vectorString + +instructionVectorStringFlush :: State -> State +instructionVectorStringFlush state = instructionFlush state vectorString + +instructionVectorStringEq :: State -> State +instructionVectorStringEq state = instructionEq state vectorString + +instructionVectorStringStackDepth :: State -> State +instructionVectorStringStackDepth state = instructionStackDepth state vectorString + +instructionVectorStringYank :: State -> State +instructionVectorStringYank state = instructionYank state vectorString + +instructionVectorStringYankDup :: State -> State +instructionVectorStringYankDup state = instructionYankDup state vectorString + +instructionVectorStringStackIsEmpty :: State -> State +instructionVectorStringStackIsEmpty state = instructionIsEmpty state vectorString + +instructionVectorStringShove :: State -> State +instructionVectorStringShove state = instructionShove state vectorString + +instructionVectorStringShoveDup :: State -> State +instructionVectorStringShoveDup state = instructionShoveDup state vectorString From fe70af6e2840d945863f773aaa1ab2695bc3196b Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Thu, 30 Jan 2025 02:44:34 -0600 Subject: [PATCH 088/171] comments --- src/Instructions/CodeInstructions.hs | 4 ---- src/Instructions/GenericInstructions.hs | 1 + 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Instructions/CodeInstructions.hs b/src/Instructions/CodeInstructions.hs index 54cc8de..b61c661 100644 --- a/src/Instructions/CodeInstructions.hs +++ b/src/Instructions/CodeInstructions.hs @@ -76,10 +76,6 @@ codeRecursiveSize _ = 1 instructionCodePop :: State -> State instructionCodePop state = instructionPop state code --- instructionCodeFromExec :: State -> State --- instructionCodeFromExec state@(State {_exec = (e1 : es), _code = cs}) = state {_exec = es, _code = e1 : cs} --- instructionCodeFromExec state = state - instructionCodeIsCodeBlock :: State -> State instructionCodeIsCodeBlock state@(State {_code = (c : cs), _bool = bs}) = state {_code = cs, _bool = isBlock c : bs} instructionCodeIsCodeBlock state = state diff --git a/src/Instructions/GenericInstructions.hs b/src/Instructions/GenericInstructions.hs index 546e6e3..e20a68d 100644 --- a/src/Instructions/GenericInstructions.hs +++ b/src/Instructions/GenericInstructions.hs @@ -29,6 +29,7 @@ subList idx0 idx1 xs = in take adjEnd (drop adjStart xs) +-- Maybe could've used Data.List.isSubsequenceOf :shrug: findSubA :: forall a. Eq a => [a] -> [a] -> Int findSubA fullA subA | length fullA < length subA = -1 From fa2b16443fae521c02df3a0bb8869a9682088a31 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Thu, 30 Jan 2025 13:27:09 -0600 Subject: [PATCH 089/171] fix index & Lens name confliction --- src/Instructions/CodeInstructions.hs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Instructions/CodeInstructions.hs b/src/Instructions/CodeInstructions.hs index b61c661..9fbb5f3 100644 --- a/src/Instructions/CodeInstructions.hs +++ b/src/Instructions/CodeInstructions.hs @@ -4,8 +4,6 @@ import Data.List (elemIndex) import State import Instructions.GenericInstructions import Instructions.IntInstructions -import Control.Lens - -- import Debug.Trace isBlock :: Gene -> Bool From 173c689000146731df12cf8b6fe069b98016635e Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Thu, 30 Jan 2025 15:01:36 -0600 Subject: [PATCH 090/171] Add TODO.md --- TODO.md | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 TODO.md diff --git a/TODO.md b/TODO.md new file mode 100644 index 0000000..951a30a --- /dev/null +++ b/TODO.md @@ -0,0 +1,9 @@ +# TODO + +## Push Language TODO + +- [ ] Make all vector functions applicable to string functions and vice versa +- [ ] Implement Calculus functions as seen in propeller +- [ ] Implement Linear Algebra functions as specified in the previous papers + +## PushGP TODO \ No newline at end of file From c3dbe2b28b3d962cbe1f47a625a5b46ebcd00d05 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Thu, 30 Jan 2025 15:22:16 -0600 Subject: [PATCH 091/171] Add sorting a vector --- TODO.md | 1 + 1 file changed, 1 insertion(+) diff --git a/TODO.md b/TODO.md index 951a30a..1285363 100644 --- a/TODO.md +++ b/TODO.md @@ -5,5 +5,6 @@ - [ ] Make all vector functions applicable to string functions and vice versa - [ ] Implement Calculus functions as seen in propeller - [ ] Implement Linear Algebra functions as specified in the previous papers +- [ ] Add a function to sort a vector ## PushGP TODO \ No newline at end of file From 8d01d9a20837614a8b71578686d0b1df2dbf8234 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Thu, 30 Jan 2025 20:59:36 -0600 Subject: [PATCH 092/171] burned out of quick check for today, more later --- HushGP.cabal | 3 +- src/LearnQuickCheck.hs | 134 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 src/LearnQuickCheck.hs diff --git a/HushGP.cabal b/HushGP.cabal index 7e224d7..1cc07e2 100644 --- a/HushGP.cabal +++ b/HushGP.cabal @@ -52,6 +52,7 @@ library , Instructions.VectorStringInstructions , Instructions.VectorLogicalInstructions , Instructions.VectorCharInstructions + , LearnQuickCheck -- Modules included in this library but not exported. -- other-modules: @@ -61,7 +62,7 @@ library -- Other library packages from which modules are imported. build-depends: - base, containers, lens, split + base, containers, lens, split, QuickCheck -- Directories containing source files. hs-source-dirs: src diff --git a/src/LearnQuickCheck.hs b/src/LearnQuickCheck.hs new file mode 100644 index 0000000..aaef6fc --- /dev/null +++ b/src/LearnQuickCheck.hs @@ -0,0 +1,134 @@ +module LearnQuickCheck where + +-- https://jesper.sikanda.be/posts/quickcheck-intro.html + +import Test.QuickCheck +import Data.List (sort) + +qsort :: Ord a => [a] -> [a] +qsort = sort + +distance :: Int -> Int -> Int +distance x y = abs (x - y) + +prop_dist35 :: Bool +prop_dist35 = distance 3 5 == 2 + +prop_dist_self :: Int -> Bool +prop_dist_self x = distance x x == 0 + +prop_dist_symmetric :: Int -> Int -> Bool +prop_dist_symmetric x y = distance x y == distance y x + +bad_distance :: Int -> Int -> Int +bad_distance x y = y - x + +prop_dist_symmetric_fail :: Int -> Int -> Bool +prop_dist_symmetric_fail x y = bad_distance x y == bad_distance y x + +sorted :: Ord a => [a] -> Bool +sorted (x:y:ys) = x <= y && sorted (y:ys) +sorted _ = True + +prop_sorted :: [Int] -> Bool +prop_sorted xs = sorted xs + +-- roundtrip property +insert :: Int -> [Int] -> [Int] +insert x [] = [x] +insert x (y:ys) | x <= y = x:y:ys + | otherwise = y:insert x ys + +delete :: Int -> [Int] -> [Int] +delete x [] = [] +delete x (y:ys) | x == y = ys + | otherwise = y:delete x ys + +prop_insert_delete :: [Int] -> Int -> Bool +prop_insert_delete xs x = delete x (insert x xs) == xs + +-- Equivalent Property +prop_qsort_sort :: [Int] -> Bool +prop_qsort_sort xs = qsort xs == sort xs + +-- can test this in ghci with verboseCheck +prop_qsort_sort' :: Ord a => [a] -> Bool +prop_qsort_sort' xs = qsort xs == sort xs + +-- Algebraic Laws +vAdd :: (Int, Int) -> (Int, Int) -> (Int, Int) +vAdd tup0 tup1 = (fst tup0 + fst tup1, snd tup0 + snd tup1) + +prop_vAdd_commutative :: (Int,Int) -> (Int,Int) -> Bool +prop_vAdd_commutative v w = vAdd v w == vAdd w v + +prop_vAdd_associative :: (Int,Int) -> (Int,Int) -> (Int,Int) -> Bool +prop_vAdd_associative u v w = vAdd (vAdd u v) w == vAdd u (vAdd v w) + +prop_vAdd_neutral_left :: (Int,Int) -> Bool +prop_vAdd_neutral_left u = vAdd (0,0) u == u + +prop_vAdd_neutral_right :: (Int,Int) -> Bool +prop_vAdd_neutral_right u = vAdd u (0,0) == u + +prop_qsort_idempotent :: [Int] -> Bool +prop_qsort_idempotent xs = qsort (qsort xs) == qsort xs + +-- Testing with different distributions section +-- prop_replicate :: Int -> Int -> Int -> Bool +-- prop_replicate n x i = replicate n x !! i == x + +prop_replicate :: Int -> Int -> Int -> Property +prop_replicate n x i = + (i >= 0 && i < n) ==> replicate n (x :: Int) !! i == x + +prop_insert_sorted :: Int -> [Int] -> Property +prop_insert_sorted x xs = sorted xs ==> sorted (insert x xs) + +-- Quantified properties +prop_insert_sorted' :: Int -> Property +prop_insert_sorted' x = forAll orderedList (\xs -> sorted (insert x xs)) + +-- Testing properties of functions +prop_filter :: Fun Int Bool -> [Int] -> Property +prop_filter p xs = + -- Filter elements not satisfying p. + let ys = [ x | x <- xs , applyFun p x ] + -- If any elements are left... + in ys /= [] ==> + -- ...generate a random index i... + forAll (choose (0,length ys-1)) + -- ...and test if p (ys!!i) holds. + (\i -> applyFun p (ys!!i)) + + + +prop_bananas :: Fun String Int -> Bool +prop_bananas f = + applyFun f "banana" == applyFun f "monkey" || + applyFun f "banana" == applyFun f "elephant" || + applyFun f "monkey" == applyFun f "elephant" + +main :: IO () +main = do + quickCheck prop_dist35 + quickCheck prop_dist_self + quickCheck prop_dist_symmetric + -- Roundtrip tests + quickCheck prop_insert_delete + -- Equivalent tests + quickCheck prop_qsort_sort + -- quickCheck prop_qsort_sort' + -- Algebraic tests + quickCheck prop_vAdd_commutative + quickCheck prop_vAdd_associative + quickCheck prop_vAdd_neutral_left + quickCheck prop_vAdd_neutral_right + -- Testing with different distributions + quickCheck prop_replicate + quickCheck prop_insert_sorted + -- Quantified Properties + quickCheck prop_insert_sorted' + -- Testing properties of functions + quickCheck prop_filter + quickCheck prop_bananas From 8be14314588cfa6f0ca15ac181573a364c8d9282 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Fri, 31 Jan 2025 03:10:39 -0600 Subject: [PATCH 093/171] comments --- README.md | 1 + src/Push.hs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 78db742..7192751 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ worth it in the grand scheme. Would mean we could also track the functions when * [x] tests/ are just copied from make-grade, need to write for this project. * [ ] Included examples of basic assertions, QuickCheck, Hspec, hspec-quickcheck. * [x] Look at Lenses library for abstraction +* [ ] Use Plushy genomes for this project. ## Design considerations The biggest design constraint is that for the exec stack (but not data stacks) diff --git a/src/Push.hs b/src/Push.hs index 2d3ebd2..0367d59 100644 --- a/src/Push.hs +++ b/src/Push.hs @@ -64,7 +64,7 @@ interpretExec state@(State {_exec = e : es}) = (StateFunc func) -> interpretExec $ func state {_exec = es} (Block block) -> interpretExec (state {_exec = block ++ es}) (PlaceInput val) -> interpretExec (state {_exec = (view input state Map.! val) : es}) - Close -> undefined -- remove Close constructor later? + Close -> undefined interpretExec state = state -- Need to make interpretExec strict, right? From 68cc4fc2d114c91e409161ec4ee5e656253b7b1b Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Fri, 31 Jan 2025 03:12:20 -0600 Subject: [PATCH 094/171] comment for Plushy later --- src/Push.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Push.hs b/src/Push.hs index 0367d59..035453f 100644 --- a/src/Push.hs +++ b/src/Push.hs @@ -64,7 +64,7 @@ interpretExec state@(State {_exec = e : es}) = (StateFunc func) -> interpretExec $ func state {_exec = es} (Block block) -> interpretExec (state {_exec = block ++ es}) (PlaceInput val) -> interpretExec (state {_exec = (view input state Map.! val) : es}) - Close -> undefined + Close -> undefined -- This should be removed later. Will be converted to Blocks in the Plushy -> Exec stack process interpretExec state = state -- Need to make interpretExec strict, right? From 5436914f41cf363ed1fc2345453ce74a3cdd797f Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Fri, 31 Jan 2025 03:15:34 -0600 Subject: [PATCH 095/171] move plushy mention --- README.md | 1 - TODO.md | 3 ++- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7192751..78db742 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,6 @@ worth it in the grand scheme. Would mean we could also track the functions when * [x] tests/ are just copied from make-grade, need to write for this project. * [ ] Included examples of basic assertions, QuickCheck, Hspec, hspec-quickcheck. * [x] Look at Lenses library for abstraction -* [ ] Use Plushy genomes for this project. ## Design considerations The biggest design constraint is that for the exec stack (but not data stacks) diff --git a/TODO.md b/TODO.md index 1285363..ee521f8 100644 --- a/TODO.md +++ b/TODO.md @@ -7,4 +7,5 @@ - [ ] Implement Linear Algebra functions as specified in the previous papers - [ ] Add a function to sort a vector -## PushGP TODO \ No newline at end of file +## PushGP TODO +- [ ] Implement a Plushy genome translator From d10df10351926b8abbb41a045b4eb6107b4a68f1 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Fri, 31 Jan 2025 15:42:49 -0600 Subject: [PATCH 096/171] make int instructions generic --- TODO.md | 1 + src/Instructions/GenericInstructions.hs | 29 ++++++++++----------- src/Instructions/IntInstructions.hs | 34 +++---------------------- test/Main.hs | 4 +-- 4 files changed, 21 insertions(+), 47 deletions(-) diff --git a/TODO.md b/TODO.md index ee521f8..1ff2d29 100644 --- a/TODO.md +++ b/TODO.md @@ -6,6 +6,7 @@ - [ ] Implement Calculus functions as seen in propeller - [ ] Implement Linear Algebra functions as specified in the previous papers - [ ] Add a function to sort a vector +- [x] Make int yank, shove, yankdup, and shovedup generic ## PushGP TODO - [ ] Implement a Plushy genome translator diff --git a/src/Instructions/GenericInstructions.hs b/src/Instructions/GenericInstructions.hs index e20a68d..1758e2b 100644 --- a/src/Instructions/GenericInstructions.hs +++ b/src/Instructions/GenericInstructions.hs @@ -57,6 +57,7 @@ replace fullA old new Nothing = then replace (take (findSubA fullA old) fullA <> new <> drop (findSubA fullA old + length old) fullA) old new Nothing else fullA +-- a rather inefficient search amtOccurences :: forall a. Eq a => [a] -> [a] -> Int amtOccurences fullA subA = amtOccurences' fullA subA 0 where @@ -149,39 +150,37 @@ instructionEq state accessor = instructionStackDepth :: State -> Lens' State [a] -> State instructionStackDepth state@(State {_int = is}) accessor = state{_int = length (view accessor state) : is} --- Will have a non-generic definition for the int stack instructionYankDup :: State -> Lens' State [a] -> State instructionYankDup state@(State {_int = i : is}) accessor = if notEmptyStack state accessor - then (state & accessor .~ (view accessor state !! max 0 (min i (length (view accessor state) - 1))) : view accessor state) {_int = is} + then state{_int = is} & accessor .~ (view accessor state{_int = is} !! max 0 (min i (length (view accessor state{_int = is}) - 1))) : view accessor state{_int = is} else state instructionYankDup state _ = state --- Is this optimal? Running instrucitonYankDup twice????? -- int non generic too instructionYank :: forall a. State -> Lens' State [a] -> State -instructionYank state@(State {_int = rawIndex : _}) accessor = +instructionYank state@(State {_int = i : is}) accessor = let myIndex :: Int - myIndex = max 0 (min rawIndex (length (view accessor state) - 1)) + myIndex = max 0 (min i (length (view accessor state{_int = is}) - 1)) item :: a - item = view accessor state !! myIndex + item = view accessor state{_int = is} !! myIndex deletedState :: State - deletedState = state & accessor .~ deleteAt myIndex (view accessor state) + deletedState = state{_int = is} & accessor .~ deleteAt myIndex (view accessor state{_int = is}) in - if notEmptyStack state accessor then deletedState & accessor .~ item : view accessor deletedState else state + if notEmptyStack state{_int = is} accessor then deletedState & accessor .~ item : view accessor deletedState else state instructionYank state _ = state --- int non generic :( --- Rewrite this eventually? +-- instructionShoveDup and instructionShove behave differently when indexing in such a way that +-- the duplicated index matters whether or not it's present in the stack at the moment of calculation. +-- I'm not going to keep this behavior. Check out interpysh examples for how pysh handles it. instructionShoveDup :: State -> Lens' State [a] -> State instructionShoveDup state@(State {_int = i : is}) accessor = - case uncons (view accessor state) of - Just (x,_) -> (state & accessor .~ combineTuple x (splitAt (max 0 (min i (length (view accessor state) - 1))) (view accessor state))) {_int = is} + case uncons (view accessor state{_int = is}) of + Just (x,_) -> state{_int = is} & accessor .~ combineTuple x (splitAt (max 0 (min i (length (view accessor state{_int = is}) - 1))) (view accessor state{_int = is})) _ -> state -instructionShoveDup state@(State {_int = []}) _ = state +instructionShoveDup state _ = state --- also also not int generic instructionShove :: State -> Lens' State [a] -> State instructionShove state accessor = instructionShoveDup state accessor & accessor .~ drop 1 (view accessor (instructionShoveDup state accessor)) @@ -206,7 +205,7 @@ instructionConj state primAccessor vectorAccessor = _ -> state -- v for vector, vs for vectorstack (also applicable to strings) --- Could abstract this unconsing even further +-- Could abstract this unconsing even further in all functions below instructionTakeN :: State -> Lens' State [[a]] -> State instructionTakeN state@(State {_int = i1 : is}) accessor = case uncons (view accessor state) of diff --git a/src/Instructions/IntInstructions.hs b/src/Instructions/IntInstructions.hs index a404028..a033a7f 100644 --- a/src/Instructions/IntInstructions.hs +++ b/src/Instructions/IntInstructions.hs @@ -88,43 +88,17 @@ instructionIntEq state = instructionEq state int instructionIntStackDepth :: State -> State instructionIntStackDepth state = instructionStackDepth state int --- int specific instructionIntYank :: State -> State --- instructionIntYank state = instructionYank state int -instructionIntYank state@(State {_int = rawIndex : i1 : is}) = - let - myIndex :: Int - myIndex = max 0 (min rawIndex (length is - 1)) - in - state {_int = is !! myIndex : i1 : deleteAt myIndex is} -instructionIntYank state = state +instructionIntYank state = instructionYank state int instructionIntYankDup :: State -> State -instructionIntYankDup state@(State {_int = rawIndex : item : is}) = - let - myIndex :: Int - myIndex = max 0 (min rawIndex (length is - 1)) - in - state {_int = is !! myIndex : item : is} -instructionIntYankDup state = state +instructionIntYankDup state = instructionYankDup state int instructionIntShove :: State -> State -instructionIntShove state@(State {_int = rawIndex : item : is}) = - let - myIndex :: Int - myIndex = max 0 (min rawIndex (length is - 1)) - in - state {_int = combineTuple item (splitAt myIndex is)} -instructionIntShove state = state +instructionIntShove state = instructionShove state int instructionIntShoveDup :: State -> State -instructionIntShoveDup state@(State {_int = rawIndex : item : is}) = - let - myIndex :: Int - myIndex = max 0 (min rawIndex (length is - 1)) - in - state {_int = item : combineTuple item (splitAt myIndex is)} -instructionIntShoveDup state = state +instructionIntShoveDup state = instructionShoveDup state int instructionIntIsEmpty :: State -> State instructionIntIsEmpty state = instructionIsEmpty state int diff --git a/test/Main.hs b/test/Main.hs index 6104e91..b88073e 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -68,8 +68,8 @@ main = do intTestFunc "instructionIntStackDepth" [2, 51, 52] [GeneInt 52, GeneInt 51, StateFunc instructionIntStackDepth] emptyState intTestFunc "instructionIntYank" [3, 3, 2, 1] [GeneInt 3, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 4, StateFunc instructionIntYank] emptyState intTestFunc "instructionIntYankDup" [3, 3, 2, 1, 3] [GeneInt 3, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 4, StateFunc instructionIntYankDup] emptyState - intTestFunc "instructionIntShove" [2, 1, 3, 1] [GeneInt 1, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 2, StateFunc instructionIntShove] emptyState - intTestFunc "instructionIntShoveDup" [3, 2, 1, 3, 1] [GeneInt 1, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 2, StateFunc instructionIntShoveDup] emptyState + intTestFunc "instructionIntShove" [2, 3, 1, 1] [GeneInt 1, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 2, StateFunc instructionIntShove] emptyState + intTestFunc "instructionIntShoveDup" [3, 2, 3, 1, 1] [GeneInt 1, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 2, StateFunc instructionIntShoveDup] emptyState -- Exec tests intTestFunc "instructionExecIf" [6, 5] [GeneBool True, StateFunc instructionExecIf, Block [GeneInt 5, GeneInt 6], Block [GeneInt 7, GeneInt 8]] emptyState From 1d561437124041e2c1524376bf88ba7b9f5e8daa Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Fri, 31 Jan 2025 15:49:06 -0600 Subject: [PATCH 097/171] comment cleanup --- src/Instructions/GenericInstructions.hs | 3 +-- src/Instructions/StringInstructions.hs | 2 -- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Instructions/GenericInstructions.hs b/src/Instructions/GenericInstructions.hs index 1758e2b..872fff1 100644 --- a/src/Instructions/GenericInstructions.hs +++ b/src/Instructions/GenericInstructions.hs @@ -157,7 +157,6 @@ instructionYankDup state@(State {_int = i : is}) accessor = else state instructionYankDup state _ = state --- int non generic too instructionYank :: forall a. State -> Lens' State [a] -> State instructionYank state@(State {_int = i : is}) accessor = let @@ -171,7 +170,7 @@ instructionYank state@(State {_int = i : is}) accessor = if notEmptyStack state{_int = is} accessor then deletedState & accessor .~ item : view accessor deletedState else state instructionYank state _ = state --- instructionShoveDup and instructionShove behave differently when indexing in such a way that +-- In pysh, instructionShoveDup and instructionShove behave differently when indexing in such a way that -- the duplicated index matters whether or not it's present in the stack at the moment of calculation. -- I'm not going to keep this behavior. Check out interpysh examples for how pysh handles it. instructionShoveDup :: State -> Lens' State [a] -> State diff --git a/src/Instructions/StringInstructions.hs b/src/Instructions/StringInstructions.hs index 0c7073a..c26aada 100644 --- a/src/Instructions/StringInstructions.hs +++ b/src/Instructions/StringInstructions.hs @@ -94,8 +94,6 @@ instructionStringInsertChar state@(State {_string = s1 : ss, _char = c1 : cs, _i instructionStringInsertChar state = state instructionStringContainsChar :: State -> State --- instructionStringContainsChar state@(State {_string = s1 : ss, _char = c1 : cs, _bool = bs}) = state{_string = ss, _char = cs, _bool = (findSubA s1 [c1] /= -1) : bs} --- instructionStringContainsChar state = state instructionStringContainsChar state = instructionVectorContains state char string instructionStringIndexOfChar :: State -> State From dc9e9fdb199a16c50dac071f0418bf9b33be9d6c Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Fri, 31 Jan 2025 15:52:53 -0600 Subject: [PATCH 098/171] calculus -> all propeller instructions --- TODO.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TODO.md b/TODO.md index 1ff2d29..de4ec89 100644 --- a/TODO.md +++ b/TODO.md @@ -3,7 +3,7 @@ ## Push Language TODO - [ ] Make all vector functions applicable to string functions and vice versa -- [ ] Implement Calculus functions as seen in propeller +- [ ] Implement all functions as seen in propeller - [ ] Implement Linear Algebra functions as specified in the previous papers - [ ] Add a function to sort a vector - [x] Make int yank, shove, yankdup, and shovedup generic From f484da2308f72070cbc878a889ffaa3455fe9fe4 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Fri, 31 Jan 2025 18:34:31 -0600 Subject: [PATCH 099/171] more quickcheck learning, just gonna go for it now --- src/LearnQuickCheck.hs | 67 +++++++++++++++++++++++++++--------------- 1 file changed, 44 insertions(+), 23 deletions(-) diff --git a/src/LearnQuickCheck.hs b/src/LearnQuickCheck.hs index aaef6fc..da25434 100644 --- a/src/LearnQuickCheck.hs +++ b/src/LearnQuickCheck.hs @@ -4,6 +4,7 @@ module LearnQuickCheck where import Test.QuickCheck import Data.List (sort) +import Control.Monad qsort :: Ord a => [a] -> [a] qsort = sort @@ -109,26 +110,46 @@ prop_bananas f = applyFun f "banana" == applyFun f "elephant" || applyFun f "monkey" == applyFun f "elephant" -main :: IO () -main = do - quickCheck prop_dist35 - quickCheck prop_dist_self - quickCheck prop_dist_symmetric - -- Roundtrip tests - quickCheck prop_insert_delete - -- Equivalent tests - quickCheck prop_qsort_sort - -- quickCheck prop_qsort_sort' - -- Algebraic tests - quickCheck prop_vAdd_commutative - quickCheck prop_vAdd_associative - quickCheck prop_vAdd_neutral_left - quickCheck prop_vAdd_neutral_right - -- Testing with different distributions - quickCheck prop_replicate - quickCheck prop_insert_sorted - -- Quantified Properties - quickCheck prop_insert_sorted' - -- Testing properties of functions - quickCheck prop_filter - quickCheck prop_bananas +-- main :: IO () +-- main = do +-- quickCheck prop_dist35 +-- quickCheck prop_dist_self +-- quickCheck prop_dist_symmetric +-- -- Roundtrip tests +-- quickCheck prop_insert_delete +-- -- Equivalent tests +-- quickCheck prop_qsort_sort +-- -- quickCheck prop_qsort_sort' +-- -- Algebraic tests +-- quickCheck prop_vAdd_commutative +-- quickCheck prop_vAdd_associative +-- quickCheck prop_vAdd_neutral_left +-- quickCheck prop_vAdd_neutral_right +-- -- Testing with different distributions +-- quickCheck prop_replicate +-- quickCheck prop_insert_sorted +-- -- Quantified Properties +-- quickCheck prop_insert_sorted' +-- -- Testing properties of functions +-- quickCheck prop_filter +-- quickCheck prop_bananas + +-- This next section is from the Practical Property Testing video on youtube +-- by FP Complete Corporation + +genSuit, genVal :: Gen Char +genSuit = elements "HDCS" +genVal = elements "123456789JQK" + +-- Applicative so can do this +genCard :: Gen (Char, Char) +genCard = (,) <$> genSuit <*> genVal + +-- Monad so can do this +genCards :: Gen [(Char, Char)] +genCards = do + l <- arbitrary + replicateM l genCard + +genListOf15Ints :: Gen [Int] +genListOf15Ints = resize 15 $ sized $ \n -> replicateM n arbitrary From b3e1b96ff5eaec13c64e142485126f868a008185 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Fri, 31 Jan 2025 18:35:39 -0600 Subject: [PATCH 100/171] move/rename --- test/Main.hs | 323 ----------------------------------------------- test/Main_old.hs | 323 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 323 insertions(+), 323 deletions(-) create mode 100644 test/Main_old.hs diff --git a/test/Main.hs b/test/Main.hs index b88073e..e69de29 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -1,323 +0,0 @@ -import Control.Exception (assert) -import Instructions -import Push -import State --- import Debug.Trace - --- TODO: Need a function that can compare states. --- May look at quickCheck later - -intTestFunc :: String -> [Int] -> [Gene] -> State -> IO () -intTestFunc name goal genome startState = - let state = loadProgram genome startState - in assert (goal == _int (interpretExec state)) putStrLn (name ++ " passed test.") - -floatTestFunc :: String -> [Float] -> [Gene] -> State -> IO () -floatTestFunc name goal genome startState = - let state = loadProgram genome startState - in assert (goal == _float (interpretExec state)) putStrLn (name ++ " passed test.") - -boolTestFunc :: String -> [Bool] -> [Gene] -> State -> IO () -boolTestFunc name goal genome startState = - let state = loadProgram genome startState - in assert (goal == _bool (interpretExec state)) putStrLn (name ++ " passed test.") - -codeTestFunc :: String -> [Gene] -> [Gene] -> State -> IO () -codeTestFunc name goal genome startState = - let state = loadProgram genome startState - in assert (goal == _code (interpretExec state)) putStrLn (name <> " passed test.") - -stringTestFunc :: String -> [String] -> [Gene] -> State -> IO () -stringTestFunc name goal genome startState = - let state = loadProgram genome startState - in assert (goal == _string (interpretExec state)) putStrLn (name <> " passed test.") - -charTestFunc :: String -> [Char] -> [Gene] -> State -> IO () -charTestFunc name goal genome startState = - let state = loadProgram genome startState - in assert (goal == _char (interpretExec state)) putStrLn (name <> " passed test.") - -vectorIntTestFunc :: String -> [[Int]] -> [Gene] -> State -> IO () -vectorIntTestFunc name goal genome startState = - let state = loadProgram genome startState - in assert (goal == _vectorInt (interpretExec state)) putStrLn (name <> " passed test.") - -vectorFloatTestFunc :: String -> [[Float]] -> [Gene] -> State -> IO () -vectorFloatTestFunc name goal genome startState = - let state = loadProgram genome startState - in assert (goal == _vectorFloat (interpretExec state)) putStrLn (name <> " passed test.") - -main :: IO () -main = do - -- Int tests - intTestFunc "instructionIntAdd" [8] [GeneInt 6, GeneInt 2, StateFunc instructionIntAdd] emptyState - intTestFunc "instructionIntSub" [4] [GeneInt 6, GeneInt 2, StateFunc instructionIntSub] emptyState - intTestFunc "instructionIntMul" [12] [GeneInt 6, GeneInt 2, StateFunc instructionIntMul] emptyState - intTestFunc "instructionIntDiv" [3] [GeneInt 6, GeneInt 2, StateFunc instructionIntDiv] emptyState - intTestFunc "instructionIntDiv0" [0, 2] [GeneInt 2, GeneInt 0, StateFunc instructionIntDiv] emptyState - intTestFunc "instructionIntMod" [3] [GeneInt 13, GeneInt 5, StateFunc instructionIntMod] emptyState - intTestFunc "instructionIntPop" [2] [GeneInt 2, GeneInt 0, StateFunc instructionIntPop] emptyState - intTestFunc "instructionIntDup" [3, 3, 2] [GeneInt 2, GeneInt 3, StateFunc instructionIntDup] emptyState - intTestFunc "instructionIntDupN3" [2, 2, 2] [GeneInt 2, GeneInt 3, StateFunc instructionIntDupN] emptyState - intTestFunc "instructionIntDupN-1" [0] [GeneInt 0, GeneInt 2, GeneInt (-1), StateFunc instructionIntDupN] emptyState - intTestFunc "instructionIntSwap" [2, 0, 3] [GeneInt 3, GeneInt 2, GeneInt 0, StateFunc instructionIntSwap] emptyState - intTestFunc "instructionIntSwapFail" [1] [GeneInt 1, StateFunc instructionIntSwap] emptyState - intTestFunc "instructionIntRot" [1, 3, 2] [GeneInt 1, GeneInt 2, GeneInt 3, StateFunc instructionIntRot] emptyState - intTestFunc "instructionIntRotFail" [7, 8] [GeneInt 8, GeneInt 7, StateFunc instructionIntRot] emptyState - intTestFunc "instructionIntFlush" [] [GeneInt 9696, GeneInt 92, GeneInt 420, StateFunc instructionIntFlush] emptyState -- I think I'm funny - intTestFunc "instructionIntStackDepth" [2, 51, 52] [GeneInt 52, GeneInt 51, StateFunc instructionIntStackDepth] emptyState - intTestFunc "instructionIntYank" [3, 3, 2, 1] [GeneInt 3, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 4, StateFunc instructionIntYank] emptyState - intTestFunc "instructionIntYankDup" [3, 3, 2, 1, 3] [GeneInt 3, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 4, StateFunc instructionIntYankDup] emptyState - intTestFunc "instructionIntShove" [2, 3, 1, 1] [GeneInt 1, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 2, StateFunc instructionIntShove] emptyState - intTestFunc "instructionIntShoveDup" [3, 2, 3, 1, 1] [GeneInt 1, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 2, StateFunc instructionIntShoveDup] emptyState - - -- Exec tests - intTestFunc "instructionExecIf" [6, 5] [GeneBool True, StateFunc instructionExecIf, Block [GeneInt 5, GeneInt 6], Block [GeneInt 7, GeneInt 8]] emptyState - intTestFunc "instructionExecDup" [8] [StateFunc instructionExecDup, GeneInt 4, StateFunc instructionIntAdd] emptyState - intTestFunc "instructionExecDoRange" [12] [GeneInt 2, Block [GeneInt 4, GeneInt 1, StateFunc instructionExecDoRange], StateFunc instructionIntAdd] emptyState - intTestFunc "instructionExecDoCount" [8] [GeneInt 2, Block [GeneInt 4, StateFunc instructionExecDoCount], StateFunc instructionIntAdd] emptyState - intTestFunc "instructionExecDoTimes" [69, 69, 69, 69, 2] [GeneInt 2, Block [GeneInt 4, StateFunc instructionExecDoTimes], GeneInt 69] emptyState - intTestFunc "instructionExecWhile" [70, 70] [GeneBool False, GeneBool True, GeneBool True, StateFunc instructionExecWhile, GeneInt 70] emptyState - intTestFunc "instructionExecDoWhile" [70, 70, 70] [GeneBool False, GeneBool True, GeneBool True, StateFunc instructionExecDoWhile, GeneInt 70] emptyState - intTestFunc "instructionExecWhenTrue" [71] [GeneBool True, StateFunc instructionExecWhen, GeneInt 71] emptyState - - let loadedState = loadProgram [GeneBool False, StateFunc instructionExecWhen, GeneInt 71] emptyState - assert (emptyState == interpretExec loadedState) putStrLn "instructionExecWhenFalse passed test." - - -- Float tests - floatTestFunc "instructionFloatAdd" [4.32] [GeneFloat 4.01, GeneFloat 0.31, StateFunc instructionFloatAdd] emptyState - floatTestFunc "instructionFloatSub" [3.6900003] [GeneFloat 4.01, GeneFloat 0.32, StateFunc instructionFloatSub] emptyState - floatTestFunc "instructionFloatMul" [1.38] [GeneFloat 0.12, GeneFloat 11.5, StateFunc instructionFloatMul] emptyState - floatTestFunc "instructionFloatDiv" [57.5] [GeneFloat 11.5, GeneFloat 0.2, StateFunc instructionFloatDiv] emptyState - floatTestFunc "instructionFloatDiv0" [0, 69.69] [GeneFloat 69.69, GeneFloat 0.0, StateFunc instructionFloatDiv] emptyState - floatTestFunc "instructionFloatYank" [1.1, 4.4, 3.3, 2.2] [GeneInt 3, GeneFloat 1.1, GeneFloat 2.2, GeneFloat 3.3, GeneFloat 4.4, StateFunc instructionFloatYank] emptyState - floatTestFunc "instructionFloatYankDup" [1.1, 4.4, 3.3, 2.2, 1.1] [GeneInt 3, GeneFloat 1.1, GeneFloat 2.2, GeneFloat 3.3, GeneFloat 4.4, StateFunc instructionFloatYankDup] emptyState - floatTestFunc "instructionFloatShove" [3.3, 2.2, 4.4, 1.1] [GeneInt 3, GeneFloat 1.1, GeneFloat 2.2, GeneFloat 3.3, GeneFloat 4.4, StateFunc instructionFloatShove] emptyState - floatTestFunc "instructionFloatShoveDup" [4.4, 3.3, 2.2, 4.4, 1.1] [GeneInt 3, GeneFloat 1.1, GeneFloat 2.2, GeneFloat 3.3, GeneFloat 4.4, StateFunc instructionFloatShoveDup] emptyState - floatTestFunc "instructionFloatDupNonEmpty" [4.4, 4.4, 3.3] [GeneFloat 3.3, GeneFloat 4.4, StateFunc instructionFloatDup] emptyState - floatTestFunc "instructionFloatDupEmpty" [] [StateFunc instructionFloatDup] emptyState - floatTestFunc "instructionFloatDupN3" [4.4, 4.4, 4.4, 3.3] [GeneFloat 3.3, GeneFloat 4.4, GeneInt 3, StateFunc instructionFloatDupN] emptyState - floatTestFunc "instructionFloatDupN-1" [3.3] [GeneFloat 3.3, GeneFloat 4.4, GeneInt (-1), StateFunc instructionFloatDupN] emptyState - boolTestFunc "instructionIntEqTrue" [True] [GeneInt 3, GeneInt 3, StateFunc instructionIntEq] emptyState - boolTestFunc "instructionIntEqFalse" [False] [GeneInt 3, GeneInt 5, StateFunc instructionIntEq] emptyState - boolTestFunc "instructionIntEqFail" [] [GeneInt 3, StateFunc instructionIntEq] emptyState - - -- Code tests - codeTestFunc "instructionCodeFromExec" [] [StateFunc instructionCodeFromExec, StateFunc instructionFloatFromInt, StateFunc instructionCodePop] emptyState - intTestFunc "instructionCodeDoRange" [18] [GeneInt 3, GeneInt 6, StateFunc instructionCodeFromExec, StateFunc instructionIntAdd, StateFunc instructionCodeDoRange] emptyState - -- How to test instructionCodeDoThenPop????? - codeTestFunc "instructionCodeFirst" [GeneInt 5] [StateFunc instructionCodeFromExec, Block [GeneInt 5, StateFunc instructionIntSub], StateFunc instructionCodeFirst] emptyState - codeTestFunc "instructionCodeLast" [GeneBool True] [StateFunc instructionCodeFromExec, Block [GeneInt 5, StateFunc instructionIntSub, GeneBool True], StateFunc instructionCodeLast] emptyState - codeTestFunc "instructionCodeTail" [Block [GeneFloat 3.2, GeneBool True, GeneInt 3]] [StateFunc instructionCodeFromExec, Block [StateFunc instructionFloatAdd, GeneFloat 3.2, GeneBool True, GeneInt 3], StateFunc instructionCodeTail] emptyState - codeTestFunc "instructionCodeInit" [Block [GeneVectorInt [1], GeneFloat 3.2, GeneBool True]] [StateFunc instructionCodeFromExec, Block [GeneVectorInt [1], GeneFloat 3.2, GeneBool True, GeneInt 3], StateFunc instructionCodeInit] emptyState - codeTestFunc "instructionCodeWrap" [Block [GeneInt 3]] [StateFunc instructionCodeFromExec, GeneInt 3, StateFunc instructionCodeWrap] emptyState - codeTestFunc "instructionCodeList" [Block [GeneFloat 5.43, GeneInt 3]] [StateFunc instructionCodeFromExec, GeneInt 3, StateFunc instructionCodeFromExec, GeneFloat 5.43, StateFunc instructionCodeList] emptyState - codeTestFunc "instructionCodeCombine2Blocks" [Block [GeneInt 3, GeneInt 4, GeneInt 1, GeneInt 2]] [StateFunc instructionCodeFromExec, Block [GeneInt 1, GeneInt 2], StateFunc instructionCodeFromExec, Block [GeneInt 3, GeneInt 4], StateFunc instructionCodeCombine] emptyState - codeTestFunc "instructionCodeCombine1Block1Single" [Block [GeneInt 3, GeneInt 4, GeneInt 1]] [StateFunc instructionCodeFromExec, GeneInt 1, StateFunc instructionCodeFromExec, Block [GeneInt 3, GeneInt 4], StateFunc instructionCodeCombine] emptyState - codeTestFunc "instructionCodeCombine1Single1Block" [Block [GeneInt 3, GeneInt 1, GeneInt 2]] [StateFunc instructionCodeFromExec, Block [GeneInt 1, GeneInt 2], StateFunc instructionCodeFromExec, GeneInt 3, StateFunc instructionCodeCombine] emptyState - codeTestFunc "instrucitonCodeCombine2Single" [Block [GeneInt 2, GeneInt 1]] [StateFunc instructionCodeFromExec, GeneInt 1, StateFunc instructionCodeFromExec, GeneInt 2, StateFunc instructionCodeCombine] emptyState - intTestFunc "instructionCodeDo" [3] [StateFunc instructionCodeFromExec, GeneInt 3, StateFunc instructionCodeDo] emptyState - -- How to test instructionCodeDoDup??? We would would need a multi stack testing function - boolTestFunc "instructionCodeIsCodeBlockTrue" [True] [StateFunc instructionCodeFromExec, Block [GeneInt 0], StateFunc instructionCodeIsCodeBlock] emptyState - boolTestFunc "instructionCodeIsCodeBlockFalse" [False] [StateFunc instructionCodeFromExec, GeneInt 0, StateFunc instructionCodeIsCodeBlock] emptyState - boolTestFunc "instructionCodeIsSingularTrue" [True] [StateFunc instructionCodeFromExec, GeneInt 0, StateFunc instructionCodeIsSingular] emptyState - boolTestFunc "instructionCodeIsSingularFalse" [False] [StateFunc instructionCodeFromExec, Block [GeneInt 0], StateFunc instructionCodeIsSingular] emptyState - intTestFunc "instructionCodeDoCount" [15] [GeneInt 6, StateFunc instructionCodeFromExec, StateFunc instructionIntAdd, StateFunc instructionCodeDoCount] emptyState - intTestFunc "instructionCodeDoTimes" [13] [GeneInt 6, GeneInt 3, GeneInt 4, GeneInt 2, StateFunc instructionCodeFromExec, StateFunc instructionIntAdd, StateFunc instructionCodeDoTimes] emptyState - intTestFunc "instructionCodeIfTrue" [6] [GeneBool True, StateFunc instructionCodeFromExec, GeneInt 3, StateFunc instructionCodeFromExec, GeneInt 6, StateFunc instructionCodeIf] emptyState - intTestFunc "instructionCodeIfFalse" [3] [GeneBool False, StateFunc instructionCodeFromExec, GeneInt 3, StateFunc instructionCodeFromExec, GeneInt 6, StateFunc instructionCodeIf] emptyState - intTestFunc "instructionCodeWhen" [6, 3, 6] [GeneInt 6, GeneInt 3, GeneInt 4, GeneInt 2, GeneBool True, StateFunc instructionCodeFromExec, StateFunc instructionIntAdd, StateFunc instructionCodeWhen] emptyState - boolTestFunc "instructionCodeMemberTrue" [True] [StateFunc instructionCodeFromExec, GeneInt 2, StateFunc instructionCodeFromExec, Block [GeneFloat 3.6, GeneInt 2, GeneVectorInt [8, 9]], StateFunc instructionCodeMember] emptyState - boolTestFunc "instructionCodeMemberFalse" [False] [StateFunc instructionCodeFromExec, GeneInt 7, StateFunc instructionCodeFromExec, Block [GeneFloat 3.6, GeneInt 2, GeneVectorInt [8, 9]], StateFunc instructionCodeMember] emptyState - boolTestFunc "instructionCodeMember2Blocks" [False] [StateFunc instructionCodeFromExec, Block [GeneInt 7, GeneInt 0], StateFunc instructionCodeFromExec, Block [GeneFloat 3.6, GeneInt 2, GeneVectorInt [8, 9]], StateFunc instructionCodeMember] emptyState - codeTestFunc "instructionCodeNInBounds" [GeneInt 0] [StateFunc instructionCodeFromExec, Block [GeneInt 0, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 4, GeneInt 5], GeneInt 0, StateFunc instructionCodeN] emptyState - codeTestFunc "instructionCodeNInBoundsEnd" [GeneInt 5] [StateFunc instructionCodeFromExec, Block [GeneInt 0, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 4, GeneInt 5], GeneInt 5, StateFunc instructionCodeN] emptyState - codeTestFunc "instructionCodeNModded" [GeneInt 3] [StateFunc instructionCodeFromExec, Block [GeneInt 0, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 4, GeneInt 5], GeneInt 9, StateFunc instructionCodeN] emptyState - codeTestFunc "instructionMakeEmptyCodeBlock" [Block []] [StateFunc instructionMakeEmptyCodeBlock] emptyState - boolTestFunc "instructionIsEmptyCodeBlockTrue" [True] [StateFunc instructionCodeFromExec, Block [], StateFunc instructionIsEmptyCodeBlock] emptyState - intTestFunc "instructionCodeSize" [8] [StateFunc instructionCodeFromExec, Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6], StateFunc instructionCodeSize] emptyState - codeTestFunc "instructionCodeExtractInBounds" [GeneInt 3] [StateFunc instructionCodeFromExec, Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6], GeneInt 3, StateFunc instructionCodeExtract] emptyState - codeTestFunc "instructionCodeExtractOutBounds" [GeneInt 3] [StateFunc instructionCodeFromExec, Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6], GeneInt 11, StateFunc instructionCodeExtract] emptyState - codeTestFunc "instructionCodeExtractLastEmptyBlock" [Block []] [StateFunc instructionCodeFromExec, Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], Block []], GeneInt 7, StateFunc instructionCodeExtract] emptyState - codeTestFunc "instructionCodeExtractBlock" [Block [GeneInt 2, GeneInt 3]] [StateFunc instructionCodeFromExec, Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], Block []], GeneInt 1, StateFunc instructionCodeExtract] emptyState - codeTestFunc "instructionCodeExtractEdgeCase" [Block []] [StateFunc instructionCodeFromExec, Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], Block []], GeneInt 7, StateFunc instructionCodeExtract] emptyState - codeTestFunc "instructionCodeExtractNotBlock" [GeneInt 2] [StateFunc instructionCodeFromExec, GeneInt 2, GeneInt 56, StateFunc instructionCodeExtract] emptyState - codeTestFunc - "instructionCodeInsertInBounds" - [Block [GeneInt 1, Block [GeneInt 2, GeneInt 9999, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6, Block [GeneInt 7, GeneInt 8], GeneInt 9]] - [StateFunc instructionCodeFromExec, GeneInt 9999, StateFunc instructionCodeFromExec, Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6, Block [GeneInt 7, GeneInt 8], GeneInt 9], GeneInt 3, StateFunc instructionCodeInsert] - emptyState - codeTestFunc - "instructionCodeInsertOutBounds" - [Block [GeneInt 1, Block [GeneInt 2, GeneInt 9999, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6, Block [GeneInt 7, GeneInt 8], GeneInt 9]] - [StateFunc instructionCodeFromExec, GeneInt 9999, StateFunc instructionCodeFromExec, Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6, Block [GeneInt 7, GeneInt 8], GeneInt 9], GeneInt 15, StateFunc instructionCodeInsert] - emptyState - codeTestFunc "instructionCodeInsertNotBlock" [Block [GeneInt 2, GeneInt 1]] [StateFunc instructionCodeFromExec, GeneInt 2, StateFunc instructionCodeFromExec, GeneInt 1, GeneInt 1, StateFunc instructionCodeInsert] emptyState - intTestFunc "instructionCodePosition0" [0] [StateFunc instructionCodeFromExec, GeneInt 1, StateFunc instructionCodeFromExec, Block [GeneInt 1, GeneInt 2, GeneInt 3], StateFunc instructionCodeFirstPosition] emptyState - intTestFunc "instructionCodePosition-1" [-1] [StateFunc instructionCodeFromExec, GeneInt 7, StateFunc instructionCodeFromExec, Block [GeneInt 1, GeneInt 2, GeneInt 3], StateFunc instructionCodeFirstPosition] emptyState - intTestFunc "instructionCodePositionEmptyBlock" [0] [StateFunc instructionCodeFromExec, Block [], StateFunc instructionCodeFromExec, Block [], StateFunc instructionCodeFirstPosition] emptyState - codeTestFunc "instructionCodePositionBadStack" [GeneInt 1] [StateFunc instructionCodeFromExec, GeneInt 1, StateFunc instructionCodeFirstPosition] emptyState -- tests to ensure base case of insufficient code stack works. Should do this on more of these instructions. - codeTestFunc "instructionCodeReverse2Args" [Block [GeneInt 2, GeneInt 1]] [StateFunc instructionCodeFromExec, Block [GeneInt 1, GeneInt 2], StateFunc instructionCodeReverse] emptyState - codeTestFunc "instructionCodeReverse3Args" [Block [GeneInt 3, GeneInt 2, GeneInt 1]] [StateFunc instructionCodeFromExec, Block [GeneInt 1, GeneInt 2, GeneInt 3], StateFunc instructionCodeReverse] emptyState - codeTestFunc "instructionCodeReverseNonBlock" [GeneInt 1] [StateFunc instructionCodeFromExec, GeneInt 1, StateFunc instructionCodeReverse] emptyState - - -- String tests - stringTestFunc "instructionStringConcat" ["123abc"] [GeneString "abc", GeneString "123", StateFunc instructionStringConcat] emptyState - stringTestFunc "instructionStringSwap" ["abc", "123"] [GeneString "abc", GeneString "123", StateFunc instructionStringSwap] emptyState - stringTestFunc "instructionStringInsertString" ["123INSabc"] [GeneString "abc", GeneString "123", StateFunc instructionStringConcat, GeneString "INS", StateFunc instructionStringSwap, GeneInt 3, StateFunc instructionStringInsertString] emptyState - stringTestFunc "instructionStringFromFirstChar" ["1"] [GeneString "123", StateFunc instructionStringFromFirstChar] emptyState - stringTestFunc "instructionStringFromNthChar" ["a"] [GeneString "123abc", GeneInt 3, StateFunc instructionStringFromNthChar] emptyState - intTestFunc "instructionStringIndexOfString3" [3] [GeneString "a", GeneString "123abc", StateFunc instructionStringIndexOfString] emptyState - intTestFunc "instructionStringIndexOfString-1" [-1] [GeneString "z", GeneString "123abc", StateFunc instructionStringIndexOfString] emptyState - boolTestFunc "instructionStringContainsStringTrue" [True] [GeneString "a", GeneString "123abc", StateFunc instructionStringContainsString] emptyState - boolTestFunc "instructionStringContainsStringFalse" [False] [GeneString "z", GeneString "123abc", StateFunc instructionStringContainsString] emptyState - stringTestFunc "instructionStringSplitOnStringMult" ["nd", "r fri", "llo gam", "h"] [GeneString "e", GeneString "hello gamer friend", StateFunc instructionStringSplitOnString] emptyState - stringTestFunc "instructionStringSplitOnStringEmpty" ["", "hello gamer frien"] [GeneString "d", GeneString "hello gamer friend", StateFunc instructionStringSplitOnString] emptyState - stringTestFunc "instructionStringReplaceFirstStringSuccess" ["thREPLACEs is a sentence"] [GeneString "REPLACE", GeneString "i", GeneString "this is a sentence", StateFunc instructionStringReplaceFirstString] emptyState - stringTestFunc "instructionStringReplaceFirstStringFail" ["this is a sentence"] [GeneString "REPLACE", GeneString "z", GeneString "this is a sentence", StateFunc instructionStringReplaceFirstString] emptyState - stringTestFunc "instructionStringReplaceNStringSuccess" ["thREPLACEs REPLACEs a sentence i"] [GeneString "REPLACE", GeneString "i", GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringReplaceNString] emptyState - stringTestFunc "instructionStringReplaceNStringFail" ["this is a sentence i"] [GeneString "REPLACE", GeneString "z", GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringReplaceNString] emptyState - stringTestFunc "instructionStringReplaceAllStringSuccess" ["thREPLACEs REPLACEs a sentence REPLACE"] [GeneString "REPLACE", GeneString "i", GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringReplaceAllString] emptyState - stringTestFunc "instructionStringReplaceAllStringFail" ["this is a sentence i"] [GeneString "REPLACE", GeneString "z", GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringReplaceAllString] emptyState - stringTestFunc "instructionStringRemoveFirstStringSuccess" ["ths is a sentence"] [GeneString "i", GeneString "this is a sentence", StateFunc instructionStringRemoveFirstString] emptyState - stringTestFunc "instructionStringRemoveFirstStringFail" ["this is a sentence"] [GeneString "z", GeneString "this is a sentence", StateFunc instructionStringRemoveFirstString] emptyState - stringTestFunc "instructionStringRemoveNStringSuccess" ["ths s a sentence i"] [GeneString "i", GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringRemoveNString] emptyState - stringTestFunc "instructionStringRemoveNStringFail" ["this is a sentence i"] [GeneString "z", GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringRemoveNString] emptyState - stringTestFunc "instructionStringRemoveAllStringSuccess" ["ths s a sentence "] [GeneString "i", GeneString "this is a sentence i", StateFunc instructionStringRemoveAllString] emptyState - stringTestFunc "instructionStringRemoveAllStringFail" ["this is a sentence i"] [GeneString "z", GeneString "this is a sentence i", StateFunc instructionStringRemoveAllString] emptyState - intTestFunc "instructionStringOccurrencesOfString3" [3] [GeneString "i", GeneString "this is a sentence i", StateFunc instructionStringOccurrencesOfString] emptyState - intTestFunc "instructionStringOccurrencesOfString3" [2] [GeneString "is", GeneString "this is a sentence i", StateFunc instructionStringOccurrencesOfString] emptyState - intTestFunc "instructionStringOccurrencesOfString0" [0] [GeneString "z", GeneString "this is a sentence i", StateFunc instructionStringOccurrencesOfString] emptyState - stringTestFunc "instructionStringInsertChar" ["123Zabc"] [GeneString "abc", GeneString "123", StateFunc instructionStringConcat, GeneChar 'Z', GeneInt 3, StateFunc instructionStringInsertChar] emptyState - boolTestFunc "instructionStringContainsCharTrue" [True] [GeneString "abc", GeneChar 'a', StateFunc instructionStringContainsChar] emptyState - boolTestFunc "instructionStringContainsCharFalse" [False] [GeneString "abc", GeneChar 'z', StateFunc instructionStringContainsChar] emptyState - intTestFunc "instructionStringIndexOfChar3" [3] [GeneChar 'a', GeneString "123abc", StateFunc instructionStringIndexOfChar] emptyState - intTestFunc "instructionStringIndexOfChar-1" [-1] [GeneChar 'z', GeneString "123abc", StateFunc instructionStringIndexOfChar] emptyState - stringTestFunc "instructionStringSplitOnCharMult" ["nd", "r fri", "llo gam", "h"] [GeneChar 'e', GeneString "hello gamer friend", StateFunc instructionStringSplitOnChar] emptyState - stringTestFunc "instructionStringSplitOnCharEmpty" ["", "hello gamer frien"] [GeneChar 'd', GeneString "hello gamer friend", StateFunc instructionStringSplitOnChar] emptyState - stringTestFunc "instructionStringReplaceFirstCharSuccess" ["thRs is a sentence"] [GeneChar 'R', GeneChar 'i', GeneString "this is a sentence", StateFunc instructionStringReplaceFirstChar] emptyState - stringTestFunc "instructionStringReplaceFirstCharFail" ["this is a sentence"] [GeneChar 'R', GeneChar 'z', GeneString "this is a sentence", StateFunc instructionStringReplaceFirstChar] emptyState - stringTestFunc "instructionStringReplaceNCharSuccess" ["thRs Rs a sentence i"] [GeneChar 'R', GeneChar 'i', GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringReplaceNChar] emptyState - stringTestFunc "instructionStringReplaceNCharFail" ["this is a sentence i"] [GeneChar 'R', GeneChar 'z', GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringReplaceNChar] emptyState - stringTestFunc "instructionStringReplaceAllCharSuccess" ["thRs Rs a sentence R"] [GeneChar 'R', GeneChar 'i', GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringReplaceAllChar] emptyState - stringTestFunc "instructionStringReplaceAllCharFail" ["this is a sentence i"] [GeneChar 'R', GeneChar 'z', GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringReplaceAllChar] emptyState - stringTestFunc "instructionStringRemoveFirstCharSuccess" ["ths is a sentence"] [GeneChar 'i', GeneString "this is a sentence", StateFunc instructionStringRemoveFirstChar] emptyState - stringTestFunc "instructionStringRemoveFirstCharFail" ["this is a sentence"] [GeneChar 'z', GeneString "this is a sentence", StateFunc instructionStringRemoveFirstChar] emptyState - stringTestFunc "instructionStringRemoveNCharSuccess" ["ths s a sentence i"] [GeneChar 'i', GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringRemoveNChar] emptyState - stringTestFunc "instructionStringRemoveNCharFail" ["this is a sentence i"] [GeneChar 'z', GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringRemoveNChar] emptyState - stringTestFunc "instructionStringRemoveAllCharSuccess" ["ths s a sentence "] [GeneChar 'i', GeneString "this is a sentence i", StateFunc instructionStringRemoveAllChar] emptyState - stringTestFunc "instructionStringRemoveAllCharFail" ["this is a sentence i"] [GeneChar 'z', GeneString "this is a sentence i", StateFunc instructionStringRemoveAllChar] emptyState - intTestFunc "instructionStringOccurrencesOfChar3" [3] [GeneChar 'i', GeneString "this is a sentence i", StateFunc instructionStringOccurrencesOfChar] emptyState - intTestFunc "instructionStringOccurrencesOfChar0" [0] [GeneChar 'z', GeneString "this is a sentence i", StateFunc instructionStringOccurrencesOfChar] emptyState - stringTestFunc "instructionStringReverse" ["321cba"] [GeneString "abc123", StateFunc instructionStringReverse] emptyState - stringTestFunc "instructionStringHead3" ["abc"] [GeneString "abc123", GeneInt 3, StateFunc instructionStringHead] emptyState - stringTestFunc "instructionStringHead0" [""] [GeneString "abc123", GeneInt 0, StateFunc instructionStringHead] emptyState - stringTestFunc "instructionStringTail3" ["123"] [GeneString "abc123", GeneInt 3, StateFunc instructionStringTail] emptyState - stringTestFunc "instructionStringTail0" [""] [GeneString "abc123", GeneInt 0, StateFunc instructionStringTail] emptyState - stringTestFunc "instructionStringAppendChar" ["Rabc123"] [GeneString "abc123", GeneChar 'R', StateFunc instructionStringAppendChar] emptyState - stringTestFunc "instructionStringRestFull" ["bc123"] [GeneString "abc123", StateFunc instructionStringRest] emptyState - stringTestFunc "instructionStringRestEmpty" [""] [GeneString "", StateFunc instructionStringRest] emptyState - stringTestFunc "instructionStringButLastFull" ["abc12"] [GeneString "abc123", StateFunc instructionStringButLast] emptyState - stringTestFunc "instructionStringButLastEmpty" [""] [GeneString "", StateFunc instructionStringButLast] emptyState - stringTestFunc "instructionStringDrop3" ["123"] [GeneString "abc123", GeneInt 3, StateFunc instructionStringDrop] emptyState - stringTestFunc "instructionStringDrop0" ["abc123"] [GeneString "abc123", GeneInt 0, StateFunc instructionStringDrop] emptyState - stringTestFunc "instructionStringButLastN3" ["abc"] [GeneString "abc123", GeneInt 3, StateFunc instructionStringButLastN] emptyState - stringTestFunc "instructionStringButLastN0" ["abc123"] [GeneString "abc123", GeneInt 0, StateFunc instructionStringButLastN] emptyState - intTestFunc "instructionStringLength6" [6] [GeneString "abc123", StateFunc instructionStringLength] emptyState - stringTestFunc "instructionStringMakeEmpty" ["", "abc123"] [GeneString "abc123", StateFunc instructionStringMakeEmpty] emptyState - stringTestFunc "instructionStringRemoveNth" ["abc23"] [GeneString "abc123", GeneInt 3, StateFunc instructionStringRemoveNth] emptyState - stringTestFunc "instructionStringSetNth" ["abR123"] [GeneString "abc123", GeneInt 2, GeneChar 'R', StateFunc instructionStringSetNth] emptyState - stringTestFunc "instructionStringStripWhitespace" ["abc123"] [GeneString " \r \n abc123 \t", StateFunc instructionStringStripWhitespace] emptyState - stringTestFunc "instructionStringFromBoolTrue" ["True"] [GeneBool True, StateFunc instructionStringFromBool] emptyState - stringTestFunc "instructionStringFromBoolTrue" ["False"] [GeneBool False, StateFunc instructionStringFromBool] emptyState - stringTestFunc "instructionStringFromInt1000" ["1000"] [GeneInt 1000, StateFunc instructionStringFromInt] emptyState - stringTestFunc "instructionStringFromInt-1" ["-1"] [GeneInt (-1), StateFunc instructionStringFromInt] emptyState - stringTestFunc "instructionStringFromFloat3.2" ["3.2"] [GeneFloat 3.2, StateFunc instructionStringFromFloat] emptyState - stringTestFunc "instructionStringFromFloat-99.0" ["-99.0"] [GeneFloat (-99.0), StateFunc instructionStringFromFloat] emptyState - stringTestFunc "instructionStringFromChar" ["Z"] [GeneChar 'Z', StateFunc instructionStringFromChar] emptyState - stringTestFunc "instructionStringFromChar" [" "] [GeneChar ' ', StateFunc instructionStringFromChar] emptyState - - -- char instructions - stringTestFunc "instructionCharConcat" ["ab"] [GeneChar 'b', GeneChar 'a', StateFunc instructionCharConcat] emptyState - charTestFunc "instructionCharFromFirstCharSuccess" ['a'] [GeneString "abc123", StateFunc instructionCharFromFirstChar] emptyState - charTestFunc "instructionCharFromFirstCharFail" [] [GeneString "", StateFunc instructionCharFromFirstChar] emptyState - charTestFunc "instructionCharFromLastCharSuccess" ['3'] [GeneString "abc123", StateFunc instructionCharFromLastChar] emptyState - charTestFunc "instructionCharFromLastCharFail" [] [GeneString "", StateFunc instructionCharFromLastChar] emptyState - charTestFunc "instructionCharFromNthCharSuccess" ['c'] [GeneString "abc123", GeneInt 2, StateFunc instructionCharFromNthChar] emptyState - boolTestFunc "instructionCharIsWhitespaceSpace" [True] [GeneChar ' ', StateFunc instructionCharIsWhitespace] emptyState - boolTestFunc "instructionCharIsWhitespacet" [True] [GeneChar '\t', StateFunc instructionCharIsWhitespace] emptyState - boolTestFunc "instructionCharIsWhitespacer" [True] [GeneChar '\r', StateFunc instructionCharIsWhitespace] emptyState - boolTestFunc "instructionCharIsWhitespacen" [True] [GeneChar '\n', StateFunc instructionCharIsWhitespace] emptyState - boolTestFunc "instructionCharIsWhitespaceFalse" [False] [GeneChar 'a', StateFunc instructionCharIsWhitespace] emptyState - boolTestFunc "instructionCharIsLetterTrue" [True] [GeneChar 'a', StateFunc instructionCharIsLetter] emptyState - boolTestFunc "instructionCharIsLetterFalse" [False] [GeneChar '1', StateFunc instructionCharIsLetter] emptyState - boolTestFunc "instructionCharIsDigitTrue" [True] [GeneChar '1', StateFunc instructionCharIsDigit] emptyState - boolTestFunc "instructionCharIsDigitFalse" [False] [GeneChar 'a', StateFunc instructionCharIsDigit] emptyState - - -- vector int instructions - vectorIntTestFunc "instructionVectorIntConcat" [[4, 5, 6, 1, 2, 3]] [GeneVectorInt [1, 2, 3], GeneVectorInt [4, 5, 6], StateFunc instructionVectorIntConcat] emptyState - vectorIntTestFunc "instructionVectorIntConj" [[99, 1, 2, 3]] [GeneVectorInt [1, 2, 3], GeneInt 99, StateFunc instructionVectorIntConj] emptyState - vectorIntTestFunc "instructionIntTakeN" [[1, 2], [6, 7, 8]] [GeneVectorInt [6, 7, 8], GeneVectorInt [1, 2, 3], GeneInt 2, StateFunc instructionVectorIntTakeN] emptyState - vectorIntTestFunc "instructionVectorIntSubVector" [[1, 2, 3]] [GeneVectorInt [0, 1, 2, 3, 4, 5], GeneInt 3, GeneInt 1, StateFunc instructionVectorIntSubVector] emptyState - intTestFunc "instructionVectorIntFirst" [1] [GeneVectorInt [1,2,3,4,5], StateFunc instructionVectorIntFirst] emptyState - intTestFunc "instructionVectorIntLast" [5] [GeneVectorInt [1,2,3,4,5], StateFunc instructionVectorIntLast] emptyState - intTestFunc "instructionVectorIntNthInBounds" [2] [GeneVectorInt [1,2,3,4,5], GeneInt 1, StateFunc instructionVectorIntNth] emptyState - intTestFunc "instructionVectorIntNthOverflow" [2] [GeneVectorInt [1,2,3,4,5], GeneInt 6, StateFunc instructionVectorIntNth] emptyState - vectorIntTestFunc "instructionVectorIntRestFull" [[2,3,4,5]] [GeneVectorInt [1,2,3,4,5], StateFunc instructionVectorIntRest] emptyState - vectorIntTestFunc "instructionVectorIntRestEmpty" [[]] [GeneVectorInt [], StateFunc instructionVectorIntRest] emptyState - vectorIntTestFunc "instructionVectorIntButLastFull" [[1,2,3,4]] [GeneVectorInt [1,2,3,4,5], StateFunc instructionVectorIntButLast] emptyState - vectorIntTestFunc "instructionVectorIntButLastEmpty" [[]] [GeneVectorInt [], StateFunc instructionVectorIntButLast] emptyState - intTestFunc "instructionVectorIntLength3" [3] [GeneVectorInt [1,2,3], StateFunc instructionVectorIntLength] emptyState - intTestFunc "instructionVectorIntLength0" [0] [GeneVectorInt [], StateFunc instructionVectorIntLength] emptyState - vectorIntTestFunc "instructionVectorIntReverse" [[4,3,2,1]] [GeneVectorInt [1,2,3,4], StateFunc instructionVectorIntReverse] emptyState - intTestFunc "instructionVectorIntPushAllFull" [1,2,3,4,99] [GeneVectorInt [1,2,3,4], GeneInt 99, StateFunc instructionVectorIntPushAll] emptyState - intTestFunc "instructionVectorIntPushAllEmpty" [99] [GeneVectorInt [], GeneInt 99, StateFunc instructionVectorIntPushAll] emptyState - vectorIntTestFunc "instructionVectorIntMakeEmpty" [[]] [StateFunc instructionVectorIntMakeEmpty] emptyState - boolTestFunc "instructionVectorIntIsEmptyTrue" [True] [GeneVectorInt [], StateFunc instructionVectorIntIsEmpty] emptyState - boolTestFunc "instructionVectorIntIsEmptyFalse" [False] [GeneVectorInt [1,2,3,4], StateFunc instructionVectorIntIsEmpty] emptyState - intTestFunc "instructionVectorIntIndexOf1" [1] [GeneVectorInt [1,2,3,4,5], GeneInt 2, StateFunc instructionVectorIntIndexOf] emptyState - intTestFunc "instructionVectorIntIndexOfFail" [-1] [GeneVectorInt [], GeneInt 2, StateFunc instructionVectorIntIndexOf] emptyState - intTestFunc "instructionVectorIntOccurrencesOf2" [2] [GeneVectorInt [1,2,3,4,2,6,7], GeneInt 2, StateFunc instructionVectorIntOccurrencesOf] emptyState - intTestFunc "instructionVectorIntOccurrencesOf0" [0] [GeneVectorInt [1,2,3,4,2,6,7], GeneInt 0, StateFunc instructionVectorIntOccurrencesOf] emptyState - vectorIntTestFunc "instructionVectorIntSetNth3" [[0,1,2,99,4,5]] [GeneVectorInt [0,1,2,3,4,5], GeneInt 99, GeneInt 3, StateFunc instructionVectorIntSetNth] emptyState - vectorIntTestFunc "instructionVectorIntSetNth9" [[0,1,2,99,4,5]] [GeneVectorInt [0,1,2,3,4,5], GeneInt 99, GeneInt 9, StateFunc instructionVectorIntSetNth] emptyState - vectorIntTestFunc "instructionVectorIntReplace3" [[0,1,2,99,4,5,99,5,99]] [GeneInt 99, GeneInt 3, GeneVectorInt [0,1,2,3,4,5,3,5,3], StateFunc instructionVectorIntReplace] emptyState - vectorIntTestFunc "instructionVectorIntReplace-1" [[0,1,2,3,4,5,3,5,3]] [GeneInt 99, GeneInt (-1), GeneVectorInt [0,1,2,3,4,5,3,5,3], StateFunc instructionVectorIntReplace] emptyState - vectorIntTestFunc "instructionVectorIntReplaceFirst3" [[0,1,2,99,4,5,3,5,3]] [GeneInt 99, GeneInt 3, GeneVectorInt [0,1,2,3,4,5,3,5,3], StateFunc instructionVectorIntReplaceFirst] emptyState - vectorIntTestFunc "instructionVectorIntReplaceFirst-2" [[0,1,2,3,4,5,3,5,3]] [GeneInt 99, GeneInt (-2), GeneVectorInt [0,1,2,3,4,5,3,5,3], StateFunc instructionVectorIntReplaceFirst] emptyState - vectorIntTestFunc "instructionVectorIntRemove" [[0,1,2,4,5,5]] [GeneInt 3, GeneVectorInt [0,1,2,3,4,5,3,5,3], StateFunc instructionVectorIntRemove] emptyState - intTestFunc "instructionVectorIntIterate" [66] [GeneInt 40, GeneVectorInt [0,1,2,3,4,5,3,5,3], StateFunc instructionVectorIntIterate, StateFunc instructionIntAdd] emptyState - - -- vector float functions - vectorFloatTestFunc "instructionVectorFloatConcat" [[4.0, 5.0, 6.0, 1.0, 2.0, 3.0]] [GeneVectorFloat [1.0, 2.0, 3.0], GeneVectorFloat [4.0, 5.0, 6.0], StateFunc instructionVectorFloatConcat] emptyState - vectorFloatTestFunc "instructionVectorFloatConj" [[99.0, 1.0, 2.0, 3.0]] [GeneVectorFloat [1.0, 2.0, 3.0], GeneFloat 99.0, StateFunc instructionVectorFloatConj] emptyState - vectorFloatTestFunc "instructionFloatTakeN" [[1.0, 2.0], [6.0, 7.0, 8.0]] [GeneVectorFloat [6.0, 7.0, 8.0], GeneVectorFloat [1.0, 2.0, 3.0], GeneInt 2, StateFunc instructionVectorFloatTakeN] emptyState - vectorFloatTestFunc "instructionVectorFloatSubVector" [[1.0, 2.0, 3.0]] [GeneVectorFloat [0.0, 1.0, 2.0, 3.0, 4.0, 5.0], GeneInt 3, GeneInt 1, StateFunc instructionVectorFloatSubVector] emptyState - floatTestFunc "instructionVectorFloatFirst" [1.0] [GeneVectorFloat [1.0,2.0,3.0,4.0,5.0], StateFunc instructionVectorFloatFirst] emptyState - floatTestFunc "instructionVectorFloatLast" [5.0] [GeneVectorFloat [1.0,2.0,3.0,4.0,5.0], StateFunc instructionVectorFloatLast] emptyState - floatTestFunc "instructionVectorFloatNthInBounds" [2.0] [GeneVectorFloat [1.0,2.0,3.0,4.0,5.0], GeneInt 1, StateFunc instructionVectorFloatNth] emptyState - floatTestFunc "instructionVectorFloatNthOverflow" [2.0] [GeneVectorFloat [1.0,2.0,3.0,4.0,5.0], GeneInt 6, StateFunc instructionVectorFloatNth] emptyState - vectorFloatTestFunc "instructionVectorFloatRestFull" [[2.0,3.0,4.0,5.0]] [GeneVectorFloat [1.0,2.0,3.0,4.0,5.0], StateFunc instructionVectorFloatRest] emptyState - vectorFloatTestFunc "instructionVectorFloatRestEmpty" [[]] [GeneVectorFloat [], StateFunc instructionVectorFloatRest] emptyState - vectorFloatTestFunc "instructionVectorFloatButLastFull" [[1.0,2.0,3.0,4.0]] [GeneVectorFloat [1.0,2.0,3.0,4.0,5.0], StateFunc instructionVectorFloatButLast] emptyState - vectorFloatTestFunc "instructionVectorFloatButLastEmpty" [[]] [GeneVectorFloat [], StateFunc instructionVectorFloatButLast] emptyState - intTestFunc "instructionVectorFloatLength3" [3] [GeneVectorFloat [1.0,2.0,3.0], StateFunc instructionVectorFloatLength] emptyState - intTestFunc "instructionVectorFloatLength0" [0] [GeneVectorFloat [], StateFunc instructionVectorFloatLength] emptyState - vectorFloatTestFunc "instructionVectorFloatReverse" [[4.0,3.0,2.0,1.0]] [GeneVectorFloat [1.0,2.0,3.0,4.0], StateFunc instructionVectorFloatReverse] emptyState - floatTestFunc "instructionVectorFloatPushAllFull" [1.0,2.0,3.0,4.0,99.0] [GeneVectorFloat [1.0,2.0,3.0,4.0], GeneFloat 99.0, StateFunc instructionVectorFloatPushAll] emptyState - floatTestFunc "instructionVectorFloatPushAllEmpty" [99.0] [GeneVectorFloat [], GeneFloat 99.0, StateFunc instructionVectorFloatPushAll] emptyState - vectorFloatTestFunc "instructionVectorFloatMakeEmpty" [[]] [StateFunc instructionVectorFloatMakeEmpty] emptyState - boolTestFunc "instructionVectorFloatIsEmptyTrue" [True] [GeneVectorFloat [], StateFunc instructionVectorFloatIsEmpty] emptyState - boolTestFunc "instructionVectorFloatIsEmptyFalse" [False] [GeneVectorFloat [1.0,2.0,3.0,4.0], StateFunc instructionVectorFloatIsEmpty] emptyState - intTestFunc "instructionVectorFloatIndexOf1" [1] [GeneVectorFloat [1.0,2.0,3.0,4.0,5.0], GeneFloat 2.0, StateFunc instructionVectorFloatIndexOf] emptyState - intTestFunc "instructionVectorFloatIndexOfFail" [-1] [GeneVectorFloat [], GeneFloat 2.0, StateFunc instructionVectorFloatIndexOf] emptyState - intTestFunc "instructionVectorFloatOccurrencesOf2" [2] [GeneVectorFloat [1.0,2.0,3.0,4.0,2.0,6.0,7.0], GeneFloat 2.0, StateFunc instructionVectorFloatOccurrencesOf] emptyState - intTestFunc "instructionVectorFloatOccurrencesOf0" [0] [GeneVectorFloat [1.0,2.0,3.0,4.0,2.0,6.0,7.0], GeneFloat 0.0, StateFunc instructionVectorFloatOccurrencesOf] emptyState - vectorFloatTestFunc "instructionVectorFloatSetNth3" [[0.0,1.0,2.0,99.0,4.0,5.0]] [GeneVectorFloat [0.0,1.0,2.0,3.0,4.0,5.0], GeneFloat 99.0, GeneInt 3, StateFunc instructionVectorFloatSetNth] emptyState - vectorFloatTestFunc "instructionVectorFloatSetNth9" [[0.0,1.0,2.0,99.0,4.0,5.0]] [GeneVectorFloat [0.0,1.0,2.0,3.0,4.0,5.0], GeneFloat 99.0, GeneInt 9, StateFunc instructionVectorFloatSetNth] emptyState - vectorFloatTestFunc "instructionVectorFloatReplace3" [[0.0,1.0,2.0,99.0,4.0,5.0,99.0,5.0,99.0]] [GeneFloat 99.0, GeneFloat 3.0, GeneVectorFloat [0.0,1.0,2.0,3.0,4.0,5.0,3.0,5.0,3.0], StateFunc instructionVectorFloatReplace] emptyState - vectorFloatTestFunc "instructionVectorFloatReplace-1" [[0.0,1.0,2.0,3.0,4.0,5.0,3.0,5.0,3.0]] [GeneFloat 99.0, GeneFloat (-1.0), GeneVectorFloat [0.0,1.0,2.0,3.0,4.0,5.0,3.0,5.0,3.0], StateFunc instructionVectorFloatReplace] emptyState - vectorFloatTestFunc "instructionVectorFloatReplaceFirst3" [[0.0,1.0,2.0,99.0,4.0,5.0,3.0,5.0,3.0]] [GeneFloat 99.0, GeneFloat 3.0, GeneVectorFloat [0.0,1.0,2.0,3.0,4.0,5.0,3.0,5.0,3.0], StateFunc instructionVectorFloatReplaceFirst] emptyState - vectorFloatTestFunc "instructionVectorFloatReplaceFirst-2" [[0.0,1.0,2.0,3.0,4.0,5.0,3.0,5.0,3.0]] [GeneFloat 99.0, GeneFloat (-2.0), GeneVectorFloat [0.0,1.0,2.0,3.0,4.0,5.0,3.0,5.0,3.0], StateFunc instructionVectorFloatReplaceFirst] emptyState - vectorFloatTestFunc "instructionVectorFloatRemove" [[0.0,1.0,2.0,4.0,5.0,5.0]] [GeneFloat 3, GeneVectorFloat [0.0,1.0,2.0,3.0,4.0,5.0,3.0,5.0,3.0], StateFunc instructionVectorFloatRemove] emptyState - floatTestFunc "instructionVectorFloatIterate" [66.0] [GeneFloat 40.0, GeneVectorFloat [0.0,1.0,2.0,3.0,4.0,5.0,3.0,5.0,3.0], StateFunc instructionVectorFloatIterate, StateFunc instructionFloatAdd] emptyState diff --git a/test/Main_old.hs b/test/Main_old.hs new file mode 100644 index 0000000..b88073e --- /dev/null +++ b/test/Main_old.hs @@ -0,0 +1,323 @@ +import Control.Exception (assert) +import Instructions +import Push +import State +-- import Debug.Trace + +-- TODO: Need a function that can compare states. +-- May look at quickCheck later + +intTestFunc :: String -> [Int] -> [Gene] -> State -> IO () +intTestFunc name goal genome startState = + let state = loadProgram genome startState + in assert (goal == _int (interpretExec state)) putStrLn (name ++ " passed test.") + +floatTestFunc :: String -> [Float] -> [Gene] -> State -> IO () +floatTestFunc name goal genome startState = + let state = loadProgram genome startState + in assert (goal == _float (interpretExec state)) putStrLn (name ++ " passed test.") + +boolTestFunc :: String -> [Bool] -> [Gene] -> State -> IO () +boolTestFunc name goal genome startState = + let state = loadProgram genome startState + in assert (goal == _bool (interpretExec state)) putStrLn (name ++ " passed test.") + +codeTestFunc :: String -> [Gene] -> [Gene] -> State -> IO () +codeTestFunc name goal genome startState = + let state = loadProgram genome startState + in assert (goal == _code (interpretExec state)) putStrLn (name <> " passed test.") + +stringTestFunc :: String -> [String] -> [Gene] -> State -> IO () +stringTestFunc name goal genome startState = + let state = loadProgram genome startState + in assert (goal == _string (interpretExec state)) putStrLn (name <> " passed test.") + +charTestFunc :: String -> [Char] -> [Gene] -> State -> IO () +charTestFunc name goal genome startState = + let state = loadProgram genome startState + in assert (goal == _char (interpretExec state)) putStrLn (name <> " passed test.") + +vectorIntTestFunc :: String -> [[Int]] -> [Gene] -> State -> IO () +vectorIntTestFunc name goal genome startState = + let state = loadProgram genome startState + in assert (goal == _vectorInt (interpretExec state)) putStrLn (name <> " passed test.") + +vectorFloatTestFunc :: String -> [[Float]] -> [Gene] -> State -> IO () +vectorFloatTestFunc name goal genome startState = + let state = loadProgram genome startState + in assert (goal == _vectorFloat (interpretExec state)) putStrLn (name <> " passed test.") + +main :: IO () +main = do + -- Int tests + intTestFunc "instructionIntAdd" [8] [GeneInt 6, GeneInt 2, StateFunc instructionIntAdd] emptyState + intTestFunc "instructionIntSub" [4] [GeneInt 6, GeneInt 2, StateFunc instructionIntSub] emptyState + intTestFunc "instructionIntMul" [12] [GeneInt 6, GeneInt 2, StateFunc instructionIntMul] emptyState + intTestFunc "instructionIntDiv" [3] [GeneInt 6, GeneInt 2, StateFunc instructionIntDiv] emptyState + intTestFunc "instructionIntDiv0" [0, 2] [GeneInt 2, GeneInt 0, StateFunc instructionIntDiv] emptyState + intTestFunc "instructionIntMod" [3] [GeneInt 13, GeneInt 5, StateFunc instructionIntMod] emptyState + intTestFunc "instructionIntPop" [2] [GeneInt 2, GeneInt 0, StateFunc instructionIntPop] emptyState + intTestFunc "instructionIntDup" [3, 3, 2] [GeneInt 2, GeneInt 3, StateFunc instructionIntDup] emptyState + intTestFunc "instructionIntDupN3" [2, 2, 2] [GeneInt 2, GeneInt 3, StateFunc instructionIntDupN] emptyState + intTestFunc "instructionIntDupN-1" [0] [GeneInt 0, GeneInt 2, GeneInt (-1), StateFunc instructionIntDupN] emptyState + intTestFunc "instructionIntSwap" [2, 0, 3] [GeneInt 3, GeneInt 2, GeneInt 0, StateFunc instructionIntSwap] emptyState + intTestFunc "instructionIntSwapFail" [1] [GeneInt 1, StateFunc instructionIntSwap] emptyState + intTestFunc "instructionIntRot" [1, 3, 2] [GeneInt 1, GeneInt 2, GeneInt 3, StateFunc instructionIntRot] emptyState + intTestFunc "instructionIntRotFail" [7, 8] [GeneInt 8, GeneInt 7, StateFunc instructionIntRot] emptyState + intTestFunc "instructionIntFlush" [] [GeneInt 9696, GeneInt 92, GeneInt 420, StateFunc instructionIntFlush] emptyState -- I think I'm funny + intTestFunc "instructionIntStackDepth" [2, 51, 52] [GeneInt 52, GeneInt 51, StateFunc instructionIntStackDepth] emptyState + intTestFunc "instructionIntYank" [3, 3, 2, 1] [GeneInt 3, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 4, StateFunc instructionIntYank] emptyState + intTestFunc "instructionIntYankDup" [3, 3, 2, 1, 3] [GeneInt 3, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 4, StateFunc instructionIntYankDup] emptyState + intTestFunc "instructionIntShove" [2, 3, 1, 1] [GeneInt 1, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 2, StateFunc instructionIntShove] emptyState + intTestFunc "instructionIntShoveDup" [3, 2, 3, 1, 1] [GeneInt 1, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 2, StateFunc instructionIntShoveDup] emptyState + + -- Exec tests + intTestFunc "instructionExecIf" [6, 5] [GeneBool True, StateFunc instructionExecIf, Block [GeneInt 5, GeneInt 6], Block [GeneInt 7, GeneInt 8]] emptyState + intTestFunc "instructionExecDup" [8] [StateFunc instructionExecDup, GeneInt 4, StateFunc instructionIntAdd] emptyState + intTestFunc "instructionExecDoRange" [12] [GeneInt 2, Block [GeneInt 4, GeneInt 1, StateFunc instructionExecDoRange], StateFunc instructionIntAdd] emptyState + intTestFunc "instructionExecDoCount" [8] [GeneInt 2, Block [GeneInt 4, StateFunc instructionExecDoCount], StateFunc instructionIntAdd] emptyState + intTestFunc "instructionExecDoTimes" [69, 69, 69, 69, 2] [GeneInt 2, Block [GeneInt 4, StateFunc instructionExecDoTimes], GeneInt 69] emptyState + intTestFunc "instructionExecWhile" [70, 70] [GeneBool False, GeneBool True, GeneBool True, StateFunc instructionExecWhile, GeneInt 70] emptyState + intTestFunc "instructionExecDoWhile" [70, 70, 70] [GeneBool False, GeneBool True, GeneBool True, StateFunc instructionExecDoWhile, GeneInt 70] emptyState + intTestFunc "instructionExecWhenTrue" [71] [GeneBool True, StateFunc instructionExecWhen, GeneInt 71] emptyState + + let loadedState = loadProgram [GeneBool False, StateFunc instructionExecWhen, GeneInt 71] emptyState + assert (emptyState == interpretExec loadedState) putStrLn "instructionExecWhenFalse passed test." + + -- Float tests + floatTestFunc "instructionFloatAdd" [4.32] [GeneFloat 4.01, GeneFloat 0.31, StateFunc instructionFloatAdd] emptyState + floatTestFunc "instructionFloatSub" [3.6900003] [GeneFloat 4.01, GeneFloat 0.32, StateFunc instructionFloatSub] emptyState + floatTestFunc "instructionFloatMul" [1.38] [GeneFloat 0.12, GeneFloat 11.5, StateFunc instructionFloatMul] emptyState + floatTestFunc "instructionFloatDiv" [57.5] [GeneFloat 11.5, GeneFloat 0.2, StateFunc instructionFloatDiv] emptyState + floatTestFunc "instructionFloatDiv0" [0, 69.69] [GeneFloat 69.69, GeneFloat 0.0, StateFunc instructionFloatDiv] emptyState + floatTestFunc "instructionFloatYank" [1.1, 4.4, 3.3, 2.2] [GeneInt 3, GeneFloat 1.1, GeneFloat 2.2, GeneFloat 3.3, GeneFloat 4.4, StateFunc instructionFloatYank] emptyState + floatTestFunc "instructionFloatYankDup" [1.1, 4.4, 3.3, 2.2, 1.1] [GeneInt 3, GeneFloat 1.1, GeneFloat 2.2, GeneFloat 3.3, GeneFloat 4.4, StateFunc instructionFloatYankDup] emptyState + floatTestFunc "instructionFloatShove" [3.3, 2.2, 4.4, 1.1] [GeneInt 3, GeneFloat 1.1, GeneFloat 2.2, GeneFloat 3.3, GeneFloat 4.4, StateFunc instructionFloatShove] emptyState + floatTestFunc "instructionFloatShoveDup" [4.4, 3.3, 2.2, 4.4, 1.1] [GeneInt 3, GeneFloat 1.1, GeneFloat 2.2, GeneFloat 3.3, GeneFloat 4.4, StateFunc instructionFloatShoveDup] emptyState + floatTestFunc "instructionFloatDupNonEmpty" [4.4, 4.4, 3.3] [GeneFloat 3.3, GeneFloat 4.4, StateFunc instructionFloatDup] emptyState + floatTestFunc "instructionFloatDupEmpty" [] [StateFunc instructionFloatDup] emptyState + floatTestFunc "instructionFloatDupN3" [4.4, 4.4, 4.4, 3.3] [GeneFloat 3.3, GeneFloat 4.4, GeneInt 3, StateFunc instructionFloatDupN] emptyState + floatTestFunc "instructionFloatDupN-1" [3.3] [GeneFloat 3.3, GeneFloat 4.4, GeneInt (-1), StateFunc instructionFloatDupN] emptyState + boolTestFunc "instructionIntEqTrue" [True] [GeneInt 3, GeneInt 3, StateFunc instructionIntEq] emptyState + boolTestFunc "instructionIntEqFalse" [False] [GeneInt 3, GeneInt 5, StateFunc instructionIntEq] emptyState + boolTestFunc "instructionIntEqFail" [] [GeneInt 3, StateFunc instructionIntEq] emptyState + + -- Code tests + codeTestFunc "instructionCodeFromExec" [] [StateFunc instructionCodeFromExec, StateFunc instructionFloatFromInt, StateFunc instructionCodePop] emptyState + intTestFunc "instructionCodeDoRange" [18] [GeneInt 3, GeneInt 6, StateFunc instructionCodeFromExec, StateFunc instructionIntAdd, StateFunc instructionCodeDoRange] emptyState + -- How to test instructionCodeDoThenPop????? + codeTestFunc "instructionCodeFirst" [GeneInt 5] [StateFunc instructionCodeFromExec, Block [GeneInt 5, StateFunc instructionIntSub], StateFunc instructionCodeFirst] emptyState + codeTestFunc "instructionCodeLast" [GeneBool True] [StateFunc instructionCodeFromExec, Block [GeneInt 5, StateFunc instructionIntSub, GeneBool True], StateFunc instructionCodeLast] emptyState + codeTestFunc "instructionCodeTail" [Block [GeneFloat 3.2, GeneBool True, GeneInt 3]] [StateFunc instructionCodeFromExec, Block [StateFunc instructionFloatAdd, GeneFloat 3.2, GeneBool True, GeneInt 3], StateFunc instructionCodeTail] emptyState + codeTestFunc "instructionCodeInit" [Block [GeneVectorInt [1], GeneFloat 3.2, GeneBool True]] [StateFunc instructionCodeFromExec, Block [GeneVectorInt [1], GeneFloat 3.2, GeneBool True, GeneInt 3], StateFunc instructionCodeInit] emptyState + codeTestFunc "instructionCodeWrap" [Block [GeneInt 3]] [StateFunc instructionCodeFromExec, GeneInt 3, StateFunc instructionCodeWrap] emptyState + codeTestFunc "instructionCodeList" [Block [GeneFloat 5.43, GeneInt 3]] [StateFunc instructionCodeFromExec, GeneInt 3, StateFunc instructionCodeFromExec, GeneFloat 5.43, StateFunc instructionCodeList] emptyState + codeTestFunc "instructionCodeCombine2Blocks" [Block [GeneInt 3, GeneInt 4, GeneInt 1, GeneInt 2]] [StateFunc instructionCodeFromExec, Block [GeneInt 1, GeneInt 2], StateFunc instructionCodeFromExec, Block [GeneInt 3, GeneInt 4], StateFunc instructionCodeCombine] emptyState + codeTestFunc "instructionCodeCombine1Block1Single" [Block [GeneInt 3, GeneInt 4, GeneInt 1]] [StateFunc instructionCodeFromExec, GeneInt 1, StateFunc instructionCodeFromExec, Block [GeneInt 3, GeneInt 4], StateFunc instructionCodeCombine] emptyState + codeTestFunc "instructionCodeCombine1Single1Block" [Block [GeneInt 3, GeneInt 1, GeneInt 2]] [StateFunc instructionCodeFromExec, Block [GeneInt 1, GeneInt 2], StateFunc instructionCodeFromExec, GeneInt 3, StateFunc instructionCodeCombine] emptyState + codeTestFunc "instrucitonCodeCombine2Single" [Block [GeneInt 2, GeneInt 1]] [StateFunc instructionCodeFromExec, GeneInt 1, StateFunc instructionCodeFromExec, GeneInt 2, StateFunc instructionCodeCombine] emptyState + intTestFunc "instructionCodeDo" [3] [StateFunc instructionCodeFromExec, GeneInt 3, StateFunc instructionCodeDo] emptyState + -- How to test instructionCodeDoDup??? We would would need a multi stack testing function + boolTestFunc "instructionCodeIsCodeBlockTrue" [True] [StateFunc instructionCodeFromExec, Block [GeneInt 0], StateFunc instructionCodeIsCodeBlock] emptyState + boolTestFunc "instructionCodeIsCodeBlockFalse" [False] [StateFunc instructionCodeFromExec, GeneInt 0, StateFunc instructionCodeIsCodeBlock] emptyState + boolTestFunc "instructionCodeIsSingularTrue" [True] [StateFunc instructionCodeFromExec, GeneInt 0, StateFunc instructionCodeIsSingular] emptyState + boolTestFunc "instructionCodeIsSingularFalse" [False] [StateFunc instructionCodeFromExec, Block [GeneInt 0], StateFunc instructionCodeIsSingular] emptyState + intTestFunc "instructionCodeDoCount" [15] [GeneInt 6, StateFunc instructionCodeFromExec, StateFunc instructionIntAdd, StateFunc instructionCodeDoCount] emptyState + intTestFunc "instructionCodeDoTimes" [13] [GeneInt 6, GeneInt 3, GeneInt 4, GeneInt 2, StateFunc instructionCodeFromExec, StateFunc instructionIntAdd, StateFunc instructionCodeDoTimes] emptyState + intTestFunc "instructionCodeIfTrue" [6] [GeneBool True, StateFunc instructionCodeFromExec, GeneInt 3, StateFunc instructionCodeFromExec, GeneInt 6, StateFunc instructionCodeIf] emptyState + intTestFunc "instructionCodeIfFalse" [3] [GeneBool False, StateFunc instructionCodeFromExec, GeneInt 3, StateFunc instructionCodeFromExec, GeneInt 6, StateFunc instructionCodeIf] emptyState + intTestFunc "instructionCodeWhen" [6, 3, 6] [GeneInt 6, GeneInt 3, GeneInt 4, GeneInt 2, GeneBool True, StateFunc instructionCodeFromExec, StateFunc instructionIntAdd, StateFunc instructionCodeWhen] emptyState + boolTestFunc "instructionCodeMemberTrue" [True] [StateFunc instructionCodeFromExec, GeneInt 2, StateFunc instructionCodeFromExec, Block [GeneFloat 3.6, GeneInt 2, GeneVectorInt [8, 9]], StateFunc instructionCodeMember] emptyState + boolTestFunc "instructionCodeMemberFalse" [False] [StateFunc instructionCodeFromExec, GeneInt 7, StateFunc instructionCodeFromExec, Block [GeneFloat 3.6, GeneInt 2, GeneVectorInt [8, 9]], StateFunc instructionCodeMember] emptyState + boolTestFunc "instructionCodeMember2Blocks" [False] [StateFunc instructionCodeFromExec, Block [GeneInt 7, GeneInt 0], StateFunc instructionCodeFromExec, Block [GeneFloat 3.6, GeneInt 2, GeneVectorInt [8, 9]], StateFunc instructionCodeMember] emptyState + codeTestFunc "instructionCodeNInBounds" [GeneInt 0] [StateFunc instructionCodeFromExec, Block [GeneInt 0, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 4, GeneInt 5], GeneInt 0, StateFunc instructionCodeN] emptyState + codeTestFunc "instructionCodeNInBoundsEnd" [GeneInt 5] [StateFunc instructionCodeFromExec, Block [GeneInt 0, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 4, GeneInt 5], GeneInt 5, StateFunc instructionCodeN] emptyState + codeTestFunc "instructionCodeNModded" [GeneInt 3] [StateFunc instructionCodeFromExec, Block [GeneInt 0, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 4, GeneInt 5], GeneInt 9, StateFunc instructionCodeN] emptyState + codeTestFunc "instructionMakeEmptyCodeBlock" [Block []] [StateFunc instructionMakeEmptyCodeBlock] emptyState + boolTestFunc "instructionIsEmptyCodeBlockTrue" [True] [StateFunc instructionCodeFromExec, Block [], StateFunc instructionIsEmptyCodeBlock] emptyState + intTestFunc "instructionCodeSize" [8] [StateFunc instructionCodeFromExec, Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6], StateFunc instructionCodeSize] emptyState + codeTestFunc "instructionCodeExtractInBounds" [GeneInt 3] [StateFunc instructionCodeFromExec, Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6], GeneInt 3, StateFunc instructionCodeExtract] emptyState + codeTestFunc "instructionCodeExtractOutBounds" [GeneInt 3] [StateFunc instructionCodeFromExec, Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6], GeneInt 11, StateFunc instructionCodeExtract] emptyState + codeTestFunc "instructionCodeExtractLastEmptyBlock" [Block []] [StateFunc instructionCodeFromExec, Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], Block []], GeneInt 7, StateFunc instructionCodeExtract] emptyState + codeTestFunc "instructionCodeExtractBlock" [Block [GeneInt 2, GeneInt 3]] [StateFunc instructionCodeFromExec, Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], Block []], GeneInt 1, StateFunc instructionCodeExtract] emptyState + codeTestFunc "instructionCodeExtractEdgeCase" [Block []] [StateFunc instructionCodeFromExec, Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], Block []], GeneInt 7, StateFunc instructionCodeExtract] emptyState + codeTestFunc "instructionCodeExtractNotBlock" [GeneInt 2] [StateFunc instructionCodeFromExec, GeneInt 2, GeneInt 56, StateFunc instructionCodeExtract] emptyState + codeTestFunc + "instructionCodeInsertInBounds" + [Block [GeneInt 1, Block [GeneInt 2, GeneInt 9999, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6, Block [GeneInt 7, GeneInt 8], GeneInt 9]] + [StateFunc instructionCodeFromExec, GeneInt 9999, StateFunc instructionCodeFromExec, Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6, Block [GeneInt 7, GeneInt 8], GeneInt 9], GeneInt 3, StateFunc instructionCodeInsert] + emptyState + codeTestFunc + "instructionCodeInsertOutBounds" + [Block [GeneInt 1, Block [GeneInt 2, GeneInt 9999, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6, Block [GeneInt 7, GeneInt 8], GeneInt 9]] + [StateFunc instructionCodeFromExec, GeneInt 9999, StateFunc instructionCodeFromExec, Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6, Block [GeneInt 7, GeneInt 8], GeneInt 9], GeneInt 15, StateFunc instructionCodeInsert] + emptyState + codeTestFunc "instructionCodeInsertNotBlock" [Block [GeneInt 2, GeneInt 1]] [StateFunc instructionCodeFromExec, GeneInt 2, StateFunc instructionCodeFromExec, GeneInt 1, GeneInt 1, StateFunc instructionCodeInsert] emptyState + intTestFunc "instructionCodePosition0" [0] [StateFunc instructionCodeFromExec, GeneInt 1, StateFunc instructionCodeFromExec, Block [GeneInt 1, GeneInt 2, GeneInt 3], StateFunc instructionCodeFirstPosition] emptyState + intTestFunc "instructionCodePosition-1" [-1] [StateFunc instructionCodeFromExec, GeneInt 7, StateFunc instructionCodeFromExec, Block [GeneInt 1, GeneInt 2, GeneInt 3], StateFunc instructionCodeFirstPosition] emptyState + intTestFunc "instructionCodePositionEmptyBlock" [0] [StateFunc instructionCodeFromExec, Block [], StateFunc instructionCodeFromExec, Block [], StateFunc instructionCodeFirstPosition] emptyState + codeTestFunc "instructionCodePositionBadStack" [GeneInt 1] [StateFunc instructionCodeFromExec, GeneInt 1, StateFunc instructionCodeFirstPosition] emptyState -- tests to ensure base case of insufficient code stack works. Should do this on more of these instructions. + codeTestFunc "instructionCodeReverse2Args" [Block [GeneInt 2, GeneInt 1]] [StateFunc instructionCodeFromExec, Block [GeneInt 1, GeneInt 2], StateFunc instructionCodeReverse] emptyState + codeTestFunc "instructionCodeReverse3Args" [Block [GeneInt 3, GeneInt 2, GeneInt 1]] [StateFunc instructionCodeFromExec, Block [GeneInt 1, GeneInt 2, GeneInt 3], StateFunc instructionCodeReverse] emptyState + codeTestFunc "instructionCodeReverseNonBlock" [GeneInt 1] [StateFunc instructionCodeFromExec, GeneInt 1, StateFunc instructionCodeReverse] emptyState + + -- String tests + stringTestFunc "instructionStringConcat" ["123abc"] [GeneString "abc", GeneString "123", StateFunc instructionStringConcat] emptyState + stringTestFunc "instructionStringSwap" ["abc", "123"] [GeneString "abc", GeneString "123", StateFunc instructionStringSwap] emptyState + stringTestFunc "instructionStringInsertString" ["123INSabc"] [GeneString "abc", GeneString "123", StateFunc instructionStringConcat, GeneString "INS", StateFunc instructionStringSwap, GeneInt 3, StateFunc instructionStringInsertString] emptyState + stringTestFunc "instructionStringFromFirstChar" ["1"] [GeneString "123", StateFunc instructionStringFromFirstChar] emptyState + stringTestFunc "instructionStringFromNthChar" ["a"] [GeneString "123abc", GeneInt 3, StateFunc instructionStringFromNthChar] emptyState + intTestFunc "instructionStringIndexOfString3" [3] [GeneString "a", GeneString "123abc", StateFunc instructionStringIndexOfString] emptyState + intTestFunc "instructionStringIndexOfString-1" [-1] [GeneString "z", GeneString "123abc", StateFunc instructionStringIndexOfString] emptyState + boolTestFunc "instructionStringContainsStringTrue" [True] [GeneString "a", GeneString "123abc", StateFunc instructionStringContainsString] emptyState + boolTestFunc "instructionStringContainsStringFalse" [False] [GeneString "z", GeneString "123abc", StateFunc instructionStringContainsString] emptyState + stringTestFunc "instructionStringSplitOnStringMult" ["nd", "r fri", "llo gam", "h"] [GeneString "e", GeneString "hello gamer friend", StateFunc instructionStringSplitOnString] emptyState + stringTestFunc "instructionStringSplitOnStringEmpty" ["", "hello gamer frien"] [GeneString "d", GeneString "hello gamer friend", StateFunc instructionStringSplitOnString] emptyState + stringTestFunc "instructionStringReplaceFirstStringSuccess" ["thREPLACEs is a sentence"] [GeneString "REPLACE", GeneString "i", GeneString "this is a sentence", StateFunc instructionStringReplaceFirstString] emptyState + stringTestFunc "instructionStringReplaceFirstStringFail" ["this is a sentence"] [GeneString "REPLACE", GeneString "z", GeneString "this is a sentence", StateFunc instructionStringReplaceFirstString] emptyState + stringTestFunc "instructionStringReplaceNStringSuccess" ["thREPLACEs REPLACEs a sentence i"] [GeneString "REPLACE", GeneString "i", GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringReplaceNString] emptyState + stringTestFunc "instructionStringReplaceNStringFail" ["this is a sentence i"] [GeneString "REPLACE", GeneString "z", GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringReplaceNString] emptyState + stringTestFunc "instructionStringReplaceAllStringSuccess" ["thREPLACEs REPLACEs a sentence REPLACE"] [GeneString "REPLACE", GeneString "i", GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringReplaceAllString] emptyState + stringTestFunc "instructionStringReplaceAllStringFail" ["this is a sentence i"] [GeneString "REPLACE", GeneString "z", GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringReplaceAllString] emptyState + stringTestFunc "instructionStringRemoveFirstStringSuccess" ["ths is a sentence"] [GeneString "i", GeneString "this is a sentence", StateFunc instructionStringRemoveFirstString] emptyState + stringTestFunc "instructionStringRemoveFirstStringFail" ["this is a sentence"] [GeneString "z", GeneString "this is a sentence", StateFunc instructionStringRemoveFirstString] emptyState + stringTestFunc "instructionStringRemoveNStringSuccess" ["ths s a sentence i"] [GeneString "i", GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringRemoveNString] emptyState + stringTestFunc "instructionStringRemoveNStringFail" ["this is a sentence i"] [GeneString "z", GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringRemoveNString] emptyState + stringTestFunc "instructionStringRemoveAllStringSuccess" ["ths s a sentence "] [GeneString "i", GeneString "this is a sentence i", StateFunc instructionStringRemoveAllString] emptyState + stringTestFunc "instructionStringRemoveAllStringFail" ["this is a sentence i"] [GeneString "z", GeneString "this is a sentence i", StateFunc instructionStringRemoveAllString] emptyState + intTestFunc "instructionStringOccurrencesOfString3" [3] [GeneString "i", GeneString "this is a sentence i", StateFunc instructionStringOccurrencesOfString] emptyState + intTestFunc "instructionStringOccurrencesOfString3" [2] [GeneString "is", GeneString "this is a sentence i", StateFunc instructionStringOccurrencesOfString] emptyState + intTestFunc "instructionStringOccurrencesOfString0" [0] [GeneString "z", GeneString "this is a sentence i", StateFunc instructionStringOccurrencesOfString] emptyState + stringTestFunc "instructionStringInsertChar" ["123Zabc"] [GeneString "abc", GeneString "123", StateFunc instructionStringConcat, GeneChar 'Z', GeneInt 3, StateFunc instructionStringInsertChar] emptyState + boolTestFunc "instructionStringContainsCharTrue" [True] [GeneString "abc", GeneChar 'a', StateFunc instructionStringContainsChar] emptyState + boolTestFunc "instructionStringContainsCharFalse" [False] [GeneString "abc", GeneChar 'z', StateFunc instructionStringContainsChar] emptyState + intTestFunc "instructionStringIndexOfChar3" [3] [GeneChar 'a', GeneString "123abc", StateFunc instructionStringIndexOfChar] emptyState + intTestFunc "instructionStringIndexOfChar-1" [-1] [GeneChar 'z', GeneString "123abc", StateFunc instructionStringIndexOfChar] emptyState + stringTestFunc "instructionStringSplitOnCharMult" ["nd", "r fri", "llo gam", "h"] [GeneChar 'e', GeneString "hello gamer friend", StateFunc instructionStringSplitOnChar] emptyState + stringTestFunc "instructionStringSplitOnCharEmpty" ["", "hello gamer frien"] [GeneChar 'd', GeneString "hello gamer friend", StateFunc instructionStringSplitOnChar] emptyState + stringTestFunc "instructionStringReplaceFirstCharSuccess" ["thRs is a sentence"] [GeneChar 'R', GeneChar 'i', GeneString "this is a sentence", StateFunc instructionStringReplaceFirstChar] emptyState + stringTestFunc "instructionStringReplaceFirstCharFail" ["this is a sentence"] [GeneChar 'R', GeneChar 'z', GeneString "this is a sentence", StateFunc instructionStringReplaceFirstChar] emptyState + stringTestFunc "instructionStringReplaceNCharSuccess" ["thRs Rs a sentence i"] [GeneChar 'R', GeneChar 'i', GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringReplaceNChar] emptyState + stringTestFunc "instructionStringReplaceNCharFail" ["this is a sentence i"] [GeneChar 'R', GeneChar 'z', GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringReplaceNChar] emptyState + stringTestFunc "instructionStringReplaceAllCharSuccess" ["thRs Rs a sentence R"] [GeneChar 'R', GeneChar 'i', GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringReplaceAllChar] emptyState + stringTestFunc "instructionStringReplaceAllCharFail" ["this is a sentence i"] [GeneChar 'R', GeneChar 'z', GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringReplaceAllChar] emptyState + stringTestFunc "instructionStringRemoveFirstCharSuccess" ["ths is a sentence"] [GeneChar 'i', GeneString "this is a sentence", StateFunc instructionStringRemoveFirstChar] emptyState + stringTestFunc "instructionStringRemoveFirstCharFail" ["this is a sentence"] [GeneChar 'z', GeneString "this is a sentence", StateFunc instructionStringRemoveFirstChar] emptyState + stringTestFunc "instructionStringRemoveNCharSuccess" ["ths s a sentence i"] [GeneChar 'i', GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringRemoveNChar] emptyState + stringTestFunc "instructionStringRemoveNCharFail" ["this is a sentence i"] [GeneChar 'z', GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringRemoveNChar] emptyState + stringTestFunc "instructionStringRemoveAllCharSuccess" ["ths s a sentence "] [GeneChar 'i', GeneString "this is a sentence i", StateFunc instructionStringRemoveAllChar] emptyState + stringTestFunc "instructionStringRemoveAllCharFail" ["this is a sentence i"] [GeneChar 'z', GeneString "this is a sentence i", StateFunc instructionStringRemoveAllChar] emptyState + intTestFunc "instructionStringOccurrencesOfChar3" [3] [GeneChar 'i', GeneString "this is a sentence i", StateFunc instructionStringOccurrencesOfChar] emptyState + intTestFunc "instructionStringOccurrencesOfChar0" [0] [GeneChar 'z', GeneString "this is a sentence i", StateFunc instructionStringOccurrencesOfChar] emptyState + stringTestFunc "instructionStringReverse" ["321cba"] [GeneString "abc123", StateFunc instructionStringReverse] emptyState + stringTestFunc "instructionStringHead3" ["abc"] [GeneString "abc123", GeneInt 3, StateFunc instructionStringHead] emptyState + stringTestFunc "instructionStringHead0" [""] [GeneString "abc123", GeneInt 0, StateFunc instructionStringHead] emptyState + stringTestFunc "instructionStringTail3" ["123"] [GeneString "abc123", GeneInt 3, StateFunc instructionStringTail] emptyState + stringTestFunc "instructionStringTail0" [""] [GeneString "abc123", GeneInt 0, StateFunc instructionStringTail] emptyState + stringTestFunc "instructionStringAppendChar" ["Rabc123"] [GeneString "abc123", GeneChar 'R', StateFunc instructionStringAppendChar] emptyState + stringTestFunc "instructionStringRestFull" ["bc123"] [GeneString "abc123", StateFunc instructionStringRest] emptyState + stringTestFunc "instructionStringRestEmpty" [""] [GeneString "", StateFunc instructionStringRest] emptyState + stringTestFunc "instructionStringButLastFull" ["abc12"] [GeneString "abc123", StateFunc instructionStringButLast] emptyState + stringTestFunc "instructionStringButLastEmpty" [""] [GeneString "", StateFunc instructionStringButLast] emptyState + stringTestFunc "instructionStringDrop3" ["123"] [GeneString "abc123", GeneInt 3, StateFunc instructionStringDrop] emptyState + stringTestFunc "instructionStringDrop0" ["abc123"] [GeneString "abc123", GeneInt 0, StateFunc instructionStringDrop] emptyState + stringTestFunc "instructionStringButLastN3" ["abc"] [GeneString "abc123", GeneInt 3, StateFunc instructionStringButLastN] emptyState + stringTestFunc "instructionStringButLastN0" ["abc123"] [GeneString "abc123", GeneInt 0, StateFunc instructionStringButLastN] emptyState + intTestFunc "instructionStringLength6" [6] [GeneString "abc123", StateFunc instructionStringLength] emptyState + stringTestFunc "instructionStringMakeEmpty" ["", "abc123"] [GeneString "abc123", StateFunc instructionStringMakeEmpty] emptyState + stringTestFunc "instructionStringRemoveNth" ["abc23"] [GeneString "abc123", GeneInt 3, StateFunc instructionStringRemoveNth] emptyState + stringTestFunc "instructionStringSetNth" ["abR123"] [GeneString "abc123", GeneInt 2, GeneChar 'R', StateFunc instructionStringSetNth] emptyState + stringTestFunc "instructionStringStripWhitespace" ["abc123"] [GeneString " \r \n abc123 \t", StateFunc instructionStringStripWhitespace] emptyState + stringTestFunc "instructionStringFromBoolTrue" ["True"] [GeneBool True, StateFunc instructionStringFromBool] emptyState + stringTestFunc "instructionStringFromBoolTrue" ["False"] [GeneBool False, StateFunc instructionStringFromBool] emptyState + stringTestFunc "instructionStringFromInt1000" ["1000"] [GeneInt 1000, StateFunc instructionStringFromInt] emptyState + stringTestFunc "instructionStringFromInt-1" ["-1"] [GeneInt (-1), StateFunc instructionStringFromInt] emptyState + stringTestFunc "instructionStringFromFloat3.2" ["3.2"] [GeneFloat 3.2, StateFunc instructionStringFromFloat] emptyState + stringTestFunc "instructionStringFromFloat-99.0" ["-99.0"] [GeneFloat (-99.0), StateFunc instructionStringFromFloat] emptyState + stringTestFunc "instructionStringFromChar" ["Z"] [GeneChar 'Z', StateFunc instructionStringFromChar] emptyState + stringTestFunc "instructionStringFromChar" [" "] [GeneChar ' ', StateFunc instructionStringFromChar] emptyState + + -- char instructions + stringTestFunc "instructionCharConcat" ["ab"] [GeneChar 'b', GeneChar 'a', StateFunc instructionCharConcat] emptyState + charTestFunc "instructionCharFromFirstCharSuccess" ['a'] [GeneString "abc123", StateFunc instructionCharFromFirstChar] emptyState + charTestFunc "instructionCharFromFirstCharFail" [] [GeneString "", StateFunc instructionCharFromFirstChar] emptyState + charTestFunc "instructionCharFromLastCharSuccess" ['3'] [GeneString "abc123", StateFunc instructionCharFromLastChar] emptyState + charTestFunc "instructionCharFromLastCharFail" [] [GeneString "", StateFunc instructionCharFromLastChar] emptyState + charTestFunc "instructionCharFromNthCharSuccess" ['c'] [GeneString "abc123", GeneInt 2, StateFunc instructionCharFromNthChar] emptyState + boolTestFunc "instructionCharIsWhitespaceSpace" [True] [GeneChar ' ', StateFunc instructionCharIsWhitespace] emptyState + boolTestFunc "instructionCharIsWhitespacet" [True] [GeneChar '\t', StateFunc instructionCharIsWhitespace] emptyState + boolTestFunc "instructionCharIsWhitespacer" [True] [GeneChar '\r', StateFunc instructionCharIsWhitespace] emptyState + boolTestFunc "instructionCharIsWhitespacen" [True] [GeneChar '\n', StateFunc instructionCharIsWhitespace] emptyState + boolTestFunc "instructionCharIsWhitespaceFalse" [False] [GeneChar 'a', StateFunc instructionCharIsWhitespace] emptyState + boolTestFunc "instructionCharIsLetterTrue" [True] [GeneChar 'a', StateFunc instructionCharIsLetter] emptyState + boolTestFunc "instructionCharIsLetterFalse" [False] [GeneChar '1', StateFunc instructionCharIsLetter] emptyState + boolTestFunc "instructionCharIsDigitTrue" [True] [GeneChar '1', StateFunc instructionCharIsDigit] emptyState + boolTestFunc "instructionCharIsDigitFalse" [False] [GeneChar 'a', StateFunc instructionCharIsDigit] emptyState + + -- vector int instructions + vectorIntTestFunc "instructionVectorIntConcat" [[4, 5, 6, 1, 2, 3]] [GeneVectorInt [1, 2, 3], GeneVectorInt [4, 5, 6], StateFunc instructionVectorIntConcat] emptyState + vectorIntTestFunc "instructionVectorIntConj" [[99, 1, 2, 3]] [GeneVectorInt [1, 2, 3], GeneInt 99, StateFunc instructionVectorIntConj] emptyState + vectorIntTestFunc "instructionIntTakeN" [[1, 2], [6, 7, 8]] [GeneVectorInt [6, 7, 8], GeneVectorInt [1, 2, 3], GeneInt 2, StateFunc instructionVectorIntTakeN] emptyState + vectorIntTestFunc "instructionVectorIntSubVector" [[1, 2, 3]] [GeneVectorInt [0, 1, 2, 3, 4, 5], GeneInt 3, GeneInt 1, StateFunc instructionVectorIntSubVector] emptyState + intTestFunc "instructionVectorIntFirst" [1] [GeneVectorInt [1,2,3,4,5], StateFunc instructionVectorIntFirst] emptyState + intTestFunc "instructionVectorIntLast" [5] [GeneVectorInt [1,2,3,4,5], StateFunc instructionVectorIntLast] emptyState + intTestFunc "instructionVectorIntNthInBounds" [2] [GeneVectorInt [1,2,3,4,5], GeneInt 1, StateFunc instructionVectorIntNth] emptyState + intTestFunc "instructionVectorIntNthOverflow" [2] [GeneVectorInt [1,2,3,4,5], GeneInt 6, StateFunc instructionVectorIntNth] emptyState + vectorIntTestFunc "instructionVectorIntRestFull" [[2,3,4,5]] [GeneVectorInt [1,2,3,4,5], StateFunc instructionVectorIntRest] emptyState + vectorIntTestFunc "instructionVectorIntRestEmpty" [[]] [GeneVectorInt [], StateFunc instructionVectorIntRest] emptyState + vectorIntTestFunc "instructionVectorIntButLastFull" [[1,2,3,4]] [GeneVectorInt [1,2,3,4,5], StateFunc instructionVectorIntButLast] emptyState + vectorIntTestFunc "instructionVectorIntButLastEmpty" [[]] [GeneVectorInt [], StateFunc instructionVectorIntButLast] emptyState + intTestFunc "instructionVectorIntLength3" [3] [GeneVectorInt [1,2,3], StateFunc instructionVectorIntLength] emptyState + intTestFunc "instructionVectorIntLength0" [0] [GeneVectorInt [], StateFunc instructionVectorIntLength] emptyState + vectorIntTestFunc "instructionVectorIntReverse" [[4,3,2,1]] [GeneVectorInt [1,2,3,4], StateFunc instructionVectorIntReverse] emptyState + intTestFunc "instructionVectorIntPushAllFull" [1,2,3,4,99] [GeneVectorInt [1,2,3,4], GeneInt 99, StateFunc instructionVectorIntPushAll] emptyState + intTestFunc "instructionVectorIntPushAllEmpty" [99] [GeneVectorInt [], GeneInt 99, StateFunc instructionVectorIntPushAll] emptyState + vectorIntTestFunc "instructionVectorIntMakeEmpty" [[]] [StateFunc instructionVectorIntMakeEmpty] emptyState + boolTestFunc "instructionVectorIntIsEmptyTrue" [True] [GeneVectorInt [], StateFunc instructionVectorIntIsEmpty] emptyState + boolTestFunc "instructionVectorIntIsEmptyFalse" [False] [GeneVectorInt [1,2,3,4], StateFunc instructionVectorIntIsEmpty] emptyState + intTestFunc "instructionVectorIntIndexOf1" [1] [GeneVectorInt [1,2,3,4,5], GeneInt 2, StateFunc instructionVectorIntIndexOf] emptyState + intTestFunc "instructionVectorIntIndexOfFail" [-1] [GeneVectorInt [], GeneInt 2, StateFunc instructionVectorIntIndexOf] emptyState + intTestFunc "instructionVectorIntOccurrencesOf2" [2] [GeneVectorInt [1,2,3,4,2,6,7], GeneInt 2, StateFunc instructionVectorIntOccurrencesOf] emptyState + intTestFunc "instructionVectorIntOccurrencesOf0" [0] [GeneVectorInt [1,2,3,4,2,6,7], GeneInt 0, StateFunc instructionVectorIntOccurrencesOf] emptyState + vectorIntTestFunc "instructionVectorIntSetNth3" [[0,1,2,99,4,5]] [GeneVectorInt [0,1,2,3,4,5], GeneInt 99, GeneInt 3, StateFunc instructionVectorIntSetNth] emptyState + vectorIntTestFunc "instructionVectorIntSetNth9" [[0,1,2,99,4,5]] [GeneVectorInt [0,1,2,3,4,5], GeneInt 99, GeneInt 9, StateFunc instructionVectorIntSetNth] emptyState + vectorIntTestFunc "instructionVectorIntReplace3" [[0,1,2,99,4,5,99,5,99]] [GeneInt 99, GeneInt 3, GeneVectorInt [0,1,2,3,4,5,3,5,3], StateFunc instructionVectorIntReplace] emptyState + vectorIntTestFunc "instructionVectorIntReplace-1" [[0,1,2,3,4,5,3,5,3]] [GeneInt 99, GeneInt (-1), GeneVectorInt [0,1,2,3,4,5,3,5,3], StateFunc instructionVectorIntReplace] emptyState + vectorIntTestFunc "instructionVectorIntReplaceFirst3" [[0,1,2,99,4,5,3,5,3]] [GeneInt 99, GeneInt 3, GeneVectorInt [0,1,2,3,4,5,3,5,3], StateFunc instructionVectorIntReplaceFirst] emptyState + vectorIntTestFunc "instructionVectorIntReplaceFirst-2" [[0,1,2,3,4,5,3,5,3]] [GeneInt 99, GeneInt (-2), GeneVectorInt [0,1,2,3,4,5,3,5,3], StateFunc instructionVectorIntReplaceFirst] emptyState + vectorIntTestFunc "instructionVectorIntRemove" [[0,1,2,4,5,5]] [GeneInt 3, GeneVectorInt [0,1,2,3,4,5,3,5,3], StateFunc instructionVectorIntRemove] emptyState + intTestFunc "instructionVectorIntIterate" [66] [GeneInt 40, GeneVectorInt [0,1,2,3,4,5,3,5,3], StateFunc instructionVectorIntIterate, StateFunc instructionIntAdd] emptyState + + -- vector float functions + vectorFloatTestFunc "instructionVectorFloatConcat" [[4.0, 5.0, 6.0, 1.0, 2.0, 3.0]] [GeneVectorFloat [1.0, 2.0, 3.0], GeneVectorFloat [4.0, 5.0, 6.0], StateFunc instructionVectorFloatConcat] emptyState + vectorFloatTestFunc "instructionVectorFloatConj" [[99.0, 1.0, 2.0, 3.0]] [GeneVectorFloat [1.0, 2.0, 3.0], GeneFloat 99.0, StateFunc instructionVectorFloatConj] emptyState + vectorFloatTestFunc "instructionFloatTakeN" [[1.0, 2.0], [6.0, 7.0, 8.0]] [GeneVectorFloat [6.0, 7.0, 8.0], GeneVectorFloat [1.0, 2.0, 3.0], GeneInt 2, StateFunc instructionVectorFloatTakeN] emptyState + vectorFloatTestFunc "instructionVectorFloatSubVector" [[1.0, 2.0, 3.0]] [GeneVectorFloat [0.0, 1.0, 2.0, 3.0, 4.0, 5.0], GeneInt 3, GeneInt 1, StateFunc instructionVectorFloatSubVector] emptyState + floatTestFunc "instructionVectorFloatFirst" [1.0] [GeneVectorFloat [1.0,2.0,3.0,4.0,5.0], StateFunc instructionVectorFloatFirst] emptyState + floatTestFunc "instructionVectorFloatLast" [5.0] [GeneVectorFloat [1.0,2.0,3.0,4.0,5.0], StateFunc instructionVectorFloatLast] emptyState + floatTestFunc "instructionVectorFloatNthInBounds" [2.0] [GeneVectorFloat [1.0,2.0,3.0,4.0,5.0], GeneInt 1, StateFunc instructionVectorFloatNth] emptyState + floatTestFunc "instructionVectorFloatNthOverflow" [2.0] [GeneVectorFloat [1.0,2.0,3.0,4.0,5.0], GeneInt 6, StateFunc instructionVectorFloatNth] emptyState + vectorFloatTestFunc "instructionVectorFloatRestFull" [[2.0,3.0,4.0,5.0]] [GeneVectorFloat [1.0,2.0,3.0,4.0,5.0], StateFunc instructionVectorFloatRest] emptyState + vectorFloatTestFunc "instructionVectorFloatRestEmpty" [[]] [GeneVectorFloat [], StateFunc instructionVectorFloatRest] emptyState + vectorFloatTestFunc "instructionVectorFloatButLastFull" [[1.0,2.0,3.0,4.0]] [GeneVectorFloat [1.0,2.0,3.0,4.0,5.0], StateFunc instructionVectorFloatButLast] emptyState + vectorFloatTestFunc "instructionVectorFloatButLastEmpty" [[]] [GeneVectorFloat [], StateFunc instructionVectorFloatButLast] emptyState + intTestFunc "instructionVectorFloatLength3" [3] [GeneVectorFloat [1.0,2.0,3.0], StateFunc instructionVectorFloatLength] emptyState + intTestFunc "instructionVectorFloatLength0" [0] [GeneVectorFloat [], StateFunc instructionVectorFloatLength] emptyState + vectorFloatTestFunc "instructionVectorFloatReverse" [[4.0,3.0,2.0,1.0]] [GeneVectorFloat [1.0,2.0,3.0,4.0], StateFunc instructionVectorFloatReverse] emptyState + floatTestFunc "instructionVectorFloatPushAllFull" [1.0,2.0,3.0,4.0,99.0] [GeneVectorFloat [1.0,2.0,3.0,4.0], GeneFloat 99.0, StateFunc instructionVectorFloatPushAll] emptyState + floatTestFunc "instructionVectorFloatPushAllEmpty" [99.0] [GeneVectorFloat [], GeneFloat 99.0, StateFunc instructionVectorFloatPushAll] emptyState + vectorFloatTestFunc "instructionVectorFloatMakeEmpty" [[]] [StateFunc instructionVectorFloatMakeEmpty] emptyState + boolTestFunc "instructionVectorFloatIsEmptyTrue" [True] [GeneVectorFloat [], StateFunc instructionVectorFloatIsEmpty] emptyState + boolTestFunc "instructionVectorFloatIsEmptyFalse" [False] [GeneVectorFloat [1.0,2.0,3.0,4.0], StateFunc instructionVectorFloatIsEmpty] emptyState + intTestFunc "instructionVectorFloatIndexOf1" [1] [GeneVectorFloat [1.0,2.0,3.0,4.0,5.0], GeneFloat 2.0, StateFunc instructionVectorFloatIndexOf] emptyState + intTestFunc "instructionVectorFloatIndexOfFail" [-1] [GeneVectorFloat [], GeneFloat 2.0, StateFunc instructionVectorFloatIndexOf] emptyState + intTestFunc "instructionVectorFloatOccurrencesOf2" [2] [GeneVectorFloat [1.0,2.0,3.0,4.0,2.0,6.0,7.0], GeneFloat 2.0, StateFunc instructionVectorFloatOccurrencesOf] emptyState + intTestFunc "instructionVectorFloatOccurrencesOf0" [0] [GeneVectorFloat [1.0,2.0,3.0,4.0,2.0,6.0,7.0], GeneFloat 0.0, StateFunc instructionVectorFloatOccurrencesOf] emptyState + vectorFloatTestFunc "instructionVectorFloatSetNth3" [[0.0,1.0,2.0,99.0,4.0,5.0]] [GeneVectorFloat [0.0,1.0,2.0,3.0,4.0,5.0], GeneFloat 99.0, GeneInt 3, StateFunc instructionVectorFloatSetNth] emptyState + vectorFloatTestFunc "instructionVectorFloatSetNth9" [[0.0,1.0,2.0,99.0,4.0,5.0]] [GeneVectorFloat [0.0,1.0,2.0,3.0,4.0,5.0], GeneFloat 99.0, GeneInt 9, StateFunc instructionVectorFloatSetNth] emptyState + vectorFloatTestFunc "instructionVectorFloatReplace3" [[0.0,1.0,2.0,99.0,4.0,5.0,99.0,5.0,99.0]] [GeneFloat 99.0, GeneFloat 3.0, GeneVectorFloat [0.0,1.0,2.0,3.0,4.0,5.0,3.0,5.0,3.0], StateFunc instructionVectorFloatReplace] emptyState + vectorFloatTestFunc "instructionVectorFloatReplace-1" [[0.0,1.0,2.0,3.0,4.0,5.0,3.0,5.0,3.0]] [GeneFloat 99.0, GeneFloat (-1.0), GeneVectorFloat [0.0,1.0,2.0,3.0,4.0,5.0,3.0,5.0,3.0], StateFunc instructionVectorFloatReplace] emptyState + vectorFloatTestFunc "instructionVectorFloatReplaceFirst3" [[0.0,1.0,2.0,99.0,4.0,5.0,3.0,5.0,3.0]] [GeneFloat 99.0, GeneFloat 3.0, GeneVectorFloat [0.0,1.0,2.0,3.0,4.0,5.0,3.0,5.0,3.0], StateFunc instructionVectorFloatReplaceFirst] emptyState + vectorFloatTestFunc "instructionVectorFloatReplaceFirst-2" [[0.0,1.0,2.0,3.0,4.0,5.0,3.0,5.0,3.0]] [GeneFloat 99.0, GeneFloat (-2.0), GeneVectorFloat [0.0,1.0,2.0,3.0,4.0,5.0,3.0,5.0,3.0], StateFunc instructionVectorFloatReplaceFirst] emptyState + vectorFloatTestFunc "instructionVectorFloatRemove" [[0.0,1.0,2.0,4.0,5.0,5.0]] [GeneFloat 3, GeneVectorFloat [0.0,1.0,2.0,3.0,4.0,5.0,3.0,5.0,3.0], StateFunc instructionVectorFloatRemove] emptyState + floatTestFunc "instructionVectorFloatIterate" [66.0] [GeneFloat 40.0, GeneVectorFloat [0.0,1.0,2.0,3.0,4.0,5.0,3.0,5.0,3.0], StateFunc instructionVectorFloatIterate, StateFunc instructionFloatAdd] emptyState From 125f137643c1c8ed4a2503f91b24057259c21809 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Fri, 31 Jan 2025 18:43:15 -0600 Subject: [PATCH 101/171] trying things... --- HushGP.cabal | 2 +- test/{Main_old.hs => MainOld.hs} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename test/{Main_old.hs => MainOld.hs} (100%) diff --git a/HushGP.cabal b/HushGP.cabal index 1cc07e2..1a7b203 100644 --- a/HushGP.cabal +++ b/HushGP.cabal @@ -102,7 +102,7 @@ test-suite HushGP-test default-language: GHC2021 -- Modules included in this executable, other than Main. - -- other-modules: + -- other-modules: MainOld.hs -- LANGUAGE extensions used by modules in this package. -- other-extensions: diff --git a/test/Main_old.hs b/test/MainOld.hs similarity index 100% rename from test/Main_old.hs rename to test/MainOld.hs From e5285e5c8f191be8364032af46945093dddd0dab Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Fri, 31 Jan 2025 23:50:07 -0600 Subject: [PATCH 102/171] quickcheck class implementations done --- HushGP.cabal | 3 +- src/LearnQuickCheck.hs | 35 ++++++++++++++++++++++++ src/State.hs | 62 ++++++++++++++++++++++++++++++++++++++---- test/Main.hs | 8 ++++++ 4 files changed, 101 insertions(+), 7 deletions(-) diff --git a/HushGP.cabal b/HushGP.cabal index 1a7b203..6752025 100644 --- a/HushGP.cabal +++ b/HushGP.cabal @@ -119,4 +119,5 @@ test-suite HushGP-test -- Test dependencies. build-depends: base, - HushGP + HushGP, + QuickCheck diff --git a/src/LearnQuickCheck.hs b/src/LearnQuickCheck.hs index da25434..f015789 100644 --- a/src/LearnQuickCheck.hs +++ b/src/LearnQuickCheck.hs @@ -153,3 +153,38 @@ genCards = do genListOf15Ints :: Gen [Int] genListOf15Ints = resize 15 $ sized $ \n -> replicateM n arbitrary + +-- Next section covers this. QuickCheck with custom data declarations +-- http://geekyplatypus.com/y-u-have-no-code-samples-quickcheck/ + +data Point = Pt Int Int + +instance Show Point where + show (Pt x y) = "{" ++ show x ++ "," ++ show y ++ "}" + +instance Arbitrary Point where + arbitrary = do + x <- arbitrary + -- y <- arbitrary + -- return $ Pt x y + -- could do + Pt x <$> arbitrary + +data Set a = Set [a] + +instance (Show a) => Show (Set a) where + show s = showSet s where + showSet (Set []) = "{}" + showSet (Set (x:xs)) = "{" <> show x <> showSubSet xs <> "}" where + showSubSet [] = "" + showSubSet (ix:ixs) = "," <> show ix <> showSubSet ixs + +instance (Arbitrary a) => Arbitrary (Set a) where + arbitrary = do Set <$> arbitrary + -- list <- arbitrary + -- return $ Set list + +-- sample $ (arbitrary :: Gen (Set Int)) + +-- This link also seems interesting +-- https://devtut.github.io/haskell/quickcheck.html diff --git a/src/State.hs b/src/State.hs index a19516c..242008e 100644 --- a/src/State.hs +++ b/src/State.hs @@ -1,9 +1,11 @@ -{-# LANGUAGE TemplateHaskell #-} +{-# LANGUAGE TemplateHaskell, DeriveGeneric #-} module State where -import Control.Lens +import Control.Lens hiding (elements) import Data.Map qualified as Map +import Test.QuickCheck +import GHC.Generics -- The exec stack must store heterogenous types, -- and we must be able to detect that type at runtime. @@ -24,6 +26,7 @@ data Gene | PlaceInput String | Close | Block [Gene] + deriving Generic instance Eq Gene where GeneInt x == GeneInt y = x == y @@ -48,8 +51,8 @@ instance Show Gene where show (GeneBool x) = "Bool: " <> show x show (GeneString x) = "String: " <> x show (GeneChar x) = "Char: " <> show x - show (StateFunc _) = "Func: unnamed" - show (PlaceInput x) = "In: " <> x + show (StateFunc x) = "Func: " <> show x + show (PlaceInput x) = "In: " <> show x show (GeneVectorInt xs) = "Int Vec: " <> show xs show (GeneVectorFloat xs) = "Float Vec: " <> show xs show (GeneVectorBool xs) = "Bool Vec: " <> show xs @@ -58,6 +61,26 @@ instance Show Gene where show Close = "Close" show (Block xs) = "Block: " <> show xs +instance CoArbitrary Gene + +instance Arbitrary Gene where + arbitrary = oneof [ + GeneInt <$> arbitrary, + GeneFloat <$> arbitrary, + GeneBool <$> arbitrary, + GeneString <$> arbitrary, + GeneChar <$> arbitrary, + StateFunc <$> arbitrary, + PlaceInput <$> arbitrary, + GeneVectorInt <$> arbitrary, + GeneVectorFloat <$> arbitrary, + GeneVectorBool <$> arbitrary, + GeneVectorString <$> arbitrary, + GeneVectorChar <$> arbitrary, + Block <$> arbitrary, + return Close + ] + data State = State { _exec :: [Gene], _code :: [Gene], @@ -74,9 +97,32 @@ data State = State _parameter :: [Gene], _input :: Map.Map String Gene } - deriving (Show, Eq) + deriving (Show, Eq, Generic) -$(makeLenses ''State) +-- This needs to be updated later +instance Show (State -> State) where + show _ = "unnamed" + +instance Arbitrary State where + arbitrary = do + arbExec <- arbitrary + arbCode <- arbitrary + arbInt <- arbitrary + arbFloat <- arbitrary + arbBool <- arbitrary + arbString <- arbitrary + arbChar <- arbitrary + arbVectorInt <- arbitrary + arbVectorFloat <- arbitrary + arbVectorBool <- arbitrary + arbVectorString <- arbitrary + arbVectorChar <- arbitrary + arbParameter <- arbitrary + -- arbInput <- arbitrary + State arbExec arbCode arbInt arbFloat arbBool arbString arbChar arbVectorInt arbVectorFloat arbVectorBool arbVectorString arbVectorChar arbParameter <$> arbitrary + -- Thanks hlint lol + +instance CoArbitrary State emptyState :: State emptyState = @@ -115,3 +161,7 @@ exampleState = _vectorChar = [['z', 'x'], ['r', 'a', 't', 'l']], _input = Map.empty } + +-- This must stay at the end of the file. +-- Template haskell seems to be messing with GHC.Generics +$(makeLenses ''State) diff --git a/test/Main.hs b/test/Main.hs index e69de29..b386a2d 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -0,0 +1,8 @@ +import State +import Test.QuickCheck + +main :: IO () +main = do + putStrLn "hello" + + From 32b48b79d0ca806d06ab156700e9ad195189192d Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sat, 1 Feb 2025 00:18:17 -0600 Subject: [PATCH 103/171] formatting --- HushGP.cabal | 4 +- src/Instructions.hs | 46 +++++++++++----------- src/LearnQuickCheck.hs | 86 ++++++++++++++++++++++-------------------- src/Push.hs | 2 +- src/State.hs | 43 +++++++++++---------- 5 files changed, 95 insertions(+), 86 deletions(-) diff --git a/HushGP.cabal b/HushGP.cabal index 6752025..93e70dd 100644 --- a/HushGP.cabal +++ b/HushGP.cabal @@ -120,4 +120,6 @@ test-suite HushGP-test build-depends: base, HushGP, - QuickCheck + QuickCheck, + lens + diff --git a/src/Instructions.hs b/src/Instructions.hs index 9856818..d93d695 100644 --- a/src/Instructions.hs +++ b/src/Instructions.hs @@ -1,30 +1,30 @@ -module Instructions ( - module Instructions.GenericInstructions, - module Instructions.IntInstructions, - module Instructions.FloatInstructions, - module Instructions.StringInstructions, - module Instructions.CharInstructions, - module Instructions.CodeInstructions, - module Instructions.ExecInstructions, - module Instructions.LogicalInstructions, - module Instructions.VectorIntInstructions, - module Instructions.VectorFloatInstructions, - module Instructions.VectorStringInstructions, - module Instructions.VectorLogicalInstructions, - module Instructions.VectorCharInstructions -) +module Instructions + ( module Instructions.GenericInstructions, + module Instructions.IntInstructions, + module Instructions.FloatInstructions, + module Instructions.StringInstructions, + module Instructions.CharInstructions, + module Instructions.CodeInstructions, + module Instructions.ExecInstructions, + module Instructions.LogicalInstructions, + module Instructions.VectorIntInstructions, + module Instructions.VectorFloatInstructions, + module Instructions.VectorStringInstructions, + module Instructions.VectorLogicalInstructions, + module Instructions.VectorCharInstructions, + ) where -import Instructions.GenericInstructions -import Instructions.IntInstructions -import Instructions.FloatInstructions -import Instructions.StringInstructions import Instructions.CharInstructions import Instructions.CodeInstructions import Instructions.ExecInstructions +import Instructions.FloatInstructions +import Instructions.GenericInstructions +import Instructions.IntInstructions import Instructions.LogicalInstructions -import Instructions.VectorIntInstructions -import Instructions.VectorFloatInstructions -import Instructions.VectorStringInstructions -import Instructions.VectorLogicalInstructions +import Instructions.StringInstructions import Instructions.VectorCharInstructions +import Instructions.VectorFloatInstructions +import Instructions.VectorIntInstructions +import Instructions.VectorLogicalInstructions +import Instructions.VectorStringInstructions diff --git a/src/LearnQuickCheck.hs b/src/LearnQuickCheck.hs index f015789..44ce1f8 100644 --- a/src/LearnQuickCheck.hs +++ b/src/LearnQuickCheck.hs @@ -2,11 +2,11 @@ module LearnQuickCheck where -- https://jesper.sikanda.be/posts/quickcheck-intro.html -import Test.QuickCheck -import Data.List (sort) import Control.Monad +import Data.List (sort) +import Test.QuickCheck -qsort :: Ord a => [a] -> [a] +qsort :: (Ord a) => [a] -> [a] qsort = sort distance :: Int -> Int -> Int @@ -27,8 +27,8 @@ bad_distance x y = y - x prop_dist_symmetric_fail :: Int -> Int -> Bool prop_dist_symmetric_fail x y = bad_distance x y == bad_distance y x -sorted :: Ord a => [a] -> Bool -sorted (x:y:ys) = x <= y && sorted (y:ys) +sorted :: (Ord a) => [a] -> Bool +sorted (x : y : ys) = x <= y && sorted (y : ys) sorted _ = True prop_sorted :: [Int] -> Bool @@ -37,13 +37,15 @@ prop_sorted xs = sorted xs -- roundtrip property insert :: Int -> [Int] -> [Int] insert x [] = [x] -insert x (y:ys) | x <= y = x:y:ys - | otherwise = y:insert x ys +insert x (y : ys) + | x <= y = x : y : ys + | otherwise = y : insert x ys delete :: Int -> [Int] -> [Int] delete x [] = [] -delete x (y:ys) | x == y = ys - | otherwise = y:delete x ys +delete x (y : ys) + | x == y = ys + | otherwise = y : delete x ys prop_insert_delete :: [Int] -> Int -> Bool prop_insert_delete xs x = delete x (insert x xs) == xs @@ -53,24 +55,24 @@ prop_qsort_sort :: [Int] -> Bool prop_qsort_sort xs = qsort xs == sort xs -- can test this in ghci with verboseCheck -prop_qsort_sort' :: Ord a => [a] -> Bool +prop_qsort_sort' :: (Ord a) => [a] -> Bool prop_qsort_sort' xs = qsort xs == sort xs -- Algebraic Laws -vAdd :: (Int, Int) -> (Int, Int) -> (Int, Int) +vAdd :: (Int, Int) -> (Int, Int) -> (Int, Int) vAdd tup0 tup1 = (fst tup0 + fst tup1, snd tup0 + snd tup1) -prop_vAdd_commutative :: (Int,Int) -> (Int,Int) -> Bool +prop_vAdd_commutative :: (Int, Int) -> (Int, Int) -> Bool prop_vAdd_commutative v w = vAdd v w == vAdd w v -prop_vAdd_associative :: (Int,Int) -> (Int,Int) -> (Int,Int) -> Bool +prop_vAdd_associative :: (Int, Int) -> (Int, Int) -> (Int, Int) -> Bool prop_vAdd_associative u v w = vAdd (vAdd u v) w == vAdd u (vAdd v w) -prop_vAdd_neutral_left :: (Int,Int) -> Bool -prop_vAdd_neutral_left u = vAdd (0,0) u == u +prop_vAdd_neutral_left :: (Int, Int) -> Bool +prop_vAdd_neutral_left u = vAdd (0, 0) u == u -prop_vAdd_neutral_right :: (Int,Int) -> Bool -prop_vAdd_neutral_right u = vAdd u (0,0) == u +prop_vAdd_neutral_right :: (Int, Int) -> Bool +prop_vAdd_neutral_right u = vAdd u (0, 0) == u prop_qsort_idempotent :: [Int] -> Bool prop_qsort_idempotent xs = qsort (qsort xs) == qsort xs @@ -80,7 +82,7 @@ prop_qsort_idempotent xs = qsort (qsort xs) == qsort xs -- prop_replicate n x i = replicate n x !! i == x prop_replicate :: Int -> Int -> Int -> Property -prop_replicate n x i = +prop_replicate n x i = (i >= 0 && i < n) ==> replicate n (x :: Int) !! i == x prop_insert_sorted :: Int -> [Int] -> Property @@ -92,23 +94,22 @@ prop_insert_sorted' x = forAll orderedList (\xs -> sorted (insert x xs)) -- Testing properties of functions prop_filter :: Fun Int Bool -> [Int] -> Property -prop_filter p xs = - -- Filter elements not satisfying p. - let ys = [ x | x <- xs , applyFun p x ] - -- If any elements are left... - in ys /= [] ==> - -- ...generate a random index i... - forAll (choose (0,length ys-1)) - -- ...and test if p (ys!!i) holds. - (\i -> applyFun p (ys!!i)) - - +prop_filter p xs = + -- Filter elements not satisfying p. + let ys = [x | x <- xs, applyFun p x] + in -- If any elements are left... + ys /= [] ==> + -- ...generate a random index i... + forAll + (choose (0, length ys - 1)) + -- ...and test if p (ys!!i) holds. + (\i -> applyFun p (ys !! i)) prop_bananas :: Fun String Int -> Bool -prop_bananas f = - applyFun f "banana" == applyFun f "monkey" || - applyFun f "banana" == applyFun f "elephant" || - applyFun f "monkey" == applyFun f "elephant" +prop_bananas f = + applyFun f "banana" == applyFun f "monkey" + || applyFun f "banana" == applyFun f "elephant" + || applyFun f "monkey" == applyFun f "elephant" -- main :: IO () -- main = do @@ -167,22 +168,25 @@ instance Arbitrary Point where x <- arbitrary -- y <- arbitrary -- return $ Pt x y - -- could do + -- could do Pt x <$> arbitrary data Set a = Set [a] instance (Show a) => Show (Set a) where - show s = showSet s where - showSet (Set []) = "{}" - showSet (Set (x:xs)) = "{" <> show x <> showSubSet xs <> "}" where - showSubSet [] = "" - showSubSet (ix:ixs) = "," <> show ix <> showSubSet ixs + show s = showSet s + where + showSet (Set []) = "{}" + showSet (Set (x : xs)) = "{" <> show x <> showSubSet xs <> "}" + where + showSubSet [] = "" + showSubSet (ix : ixs) = "," <> show ix <> showSubSet ixs instance (Arbitrary a) => Arbitrary (Set a) where arbitrary = do Set <$> arbitrary - -- list <- arbitrary - -- return $ Set list + +-- list <- arbitrary +-- return $ Set list -- sample $ (arbitrary :: Gen (Set Int)) diff --git a/src/Push.hs b/src/Push.hs index 035453f..d7755c8 100644 --- a/src/Push.hs +++ b/src/Push.hs @@ -64,7 +64,7 @@ interpretExec state@(State {_exec = e : es}) = (StateFunc func) -> interpretExec $ func state {_exec = es} (Block block) -> interpretExec (state {_exec = block ++ es}) (PlaceInput val) -> interpretExec (state {_exec = (view input state Map.! val) : es}) - Close -> undefined -- This should be removed later. Will be converted to Blocks in the Plushy -> Exec stack process + Close -> undefined -- This should be removed later. Will be converted to Blocks in the Plushy -> Exec stack process interpretExec state = state -- Need to make interpretExec strict, right? diff --git a/src/State.hs b/src/State.hs index 242008e..6cb3b08 100644 --- a/src/State.hs +++ b/src/State.hs @@ -1,11 +1,12 @@ -{-# LANGUAGE TemplateHaskell, DeriveGeneric #-} +{-# LANGUAGE DeriveGeneric #-} +{-# LANGUAGE TemplateHaskell #-} module State where import Control.Lens hiding (elements) import Data.Map qualified as Map -import Test.QuickCheck import GHC.Generics +import Test.QuickCheck -- The exec stack must store heterogenous types, -- and we must be able to detect that type at runtime. @@ -26,7 +27,7 @@ data Gene | PlaceInput String | Close | Block [Gene] - deriving Generic + deriving (Generic) instance Eq Gene where GeneInt x == GeneInt y = x == y @@ -64,22 +65,23 @@ instance Show Gene where instance CoArbitrary Gene instance Arbitrary Gene where - arbitrary = oneof [ - GeneInt <$> arbitrary, - GeneFloat <$> arbitrary, - GeneBool <$> arbitrary, - GeneString <$> arbitrary, - GeneChar <$> arbitrary, - StateFunc <$> arbitrary, - PlaceInput <$> arbitrary, - GeneVectorInt <$> arbitrary, - GeneVectorFloat <$> arbitrary, - GeneVectorBool <$> arbitrary, - GeneVectorString <$> arbitrary, - GeneVectorChar <$> arbitrary, - Block <$> arbitrary, - return Close - ] + arbitrary = + oneof + [ GeneInt <$> arbitrary, + GeneFloat <$> arbitrary, + GeneBool <$> arbitrary, + GeneString <$> arbitrary, + GeneChar <$> arbitrary, + StateFunc <$> arbitrary, + PlaceInput <$> arbitrary, + GeneVectorInt <$> arbitrary, + GeneVectorFloat <$> arbitrary, + GeneVectorBool <$> arbitrary, + GeneVectorString <$> arbitrary, + GeneVectorChar <$> arbitrary, + Block <$> arbitrary, + return Close + ] data State = State { _exec :: [Gene], @@ -120,7 +122,8 @@ instance Arbitrary State where arbParameter <- arbitrary -- arbInput <- arbitrary State arbExec arbCode arbInt arbFloat arbBool arbString arbChar arbVectorInt arbVectorFloat arbVectorBool arbVectorString arbVectorChar arbParameter <$> arbitrary - -- Thanks hlint lol + +-- Thanks hlint lol instance CoArbitrary State From 24442169bfaac116cc1634f6410ae29c44397063 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sat, 1 Feb 2025 00:18:33 -0600 Subject: [PATCH 104/171] also formatting --- test/Main.hs | 22 +++++++++++-- test/MainOld.hs | 85 +++++++++++++++++++++++++------------------------ 2 files changed, 63 insertions(+), 44 deletions(-) diff --git a/test/Main.hs b/test/Main.hs index b386a2d..2717136 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -1,8 +1,26 @@ +import Instructions import State import Test.QuickCheck +-- import Control.Lens + +myArgs = + Args + { replay = Nothing, + maxSuccess = 100, + maxDiscardRatio = 10, + maxSize = 10, + chatty = True, + maxShrinks = 500 + } + +-- quickCheckWith myArgs prop_IntAdd + +-- Running this with a large max size leads quickCheck to hang, and that's bad +prop_IntAdd :: State -> Bool +prop_IntAdd state@(State {_int = i1 : i2 : is}) = i1 + i2 == head (_int (instructionIntAdd state)) +prop_IntAdd state = state == instructionIntAdd state + main :: IO () main = do putStrLn "hello" - - diff --git a/test/MainOld.hs b/test/MainOld.hs index b88073e..48bf7a6 100644 --- a/test/MainOld.hs +++ b/test/MainOld.hs @@ -2,6 +2,7 @@ import Control.Exception (assert) import Instructions import Push import State + -- import Debug.Trace -- TODO: Need a function that can compare states. @@ -259,65 +260,65 @@ main = do vectorIntTestFunc "instructionVectorIntConj" [[99, 1, 2, 3]] [GeneVectorInt [1, 2, 3], GeneInt 99, StateFunc instructionVectorIntConj] emptyState vectorIntTestFunc "instructionIntTakeN" [[1, 2], [6, 7, 8]] [GeneVectorInt [6, 7, 8], GeneVectorInt [1, 2, 3], GeneInt 2, StateFunc instructionVectorIntTakeN] emptyState vectorIntTestFunc "instructionVectorIntSubVector" [[1, 2, 3]] [GeneVectorInt [0, 1, 2, 3, 4, 5], GeneInt 3, GeneInt 1, StateFunc instructionVectorIntSubVector] emptyState - intTestFunc "instructionVectorIntFirst" [1] [GeneVectorInt [1,2,3,4,5], StateFunc instructionVectorIntFirst] emptyState - intTestFunc "instructionVectorIntLast" [5] [GeneVectorInt [1,2,3,4,5], StateFunc instructionVectorIntLast] emptyState - intTestFunc "instructionVectorIntNthInBounds" [2] [GeneVectorInt [1,2,3,4,5], GeneInt 1, StateFunc instructionVectorIntNth] emptyState - intTestFunc "instructionVectorIntNthOverflow" [2] [GeneVectorInt [1,2,3,4,5], GeneInt 6, StateFunc instructionVectorIntNth] emptyState - vectorIntTestFunc "instructionVectorIntRestFull" [[2,3,4,5]] [GeneVectorInt [1,2,3,4,5], StateFunc instructionVectorIntRest] emptyState + intTestFunc "instructionVectorIntFirst" [1] [GeneVectorInt [1, 2, 3, 4, 5], StateFunc instructionVectorIntFirst] emptyState + intTestFunc "instructionVectorIntLast" [5] [GeneVectorInt [1, 2, 3, 4, 5], StateFunc instructionVectorIntLast] emptyState + intTestFunc "instructionVectorIntNthInBounds" [2] [GeneVectorInt [1, 2, 3, 4, 5], GeneInt 1, StateFunc instructionVectorIntNth] emptyState + intTestFunc "instructionVectorIntNthOverflow" [2] [GeneVectorInt [1, 2, 3, 4, 5], GeneInt 6, StateFunc instructionVectorIntNth] emptyState + vectorIntTestFunc "instructionVectorIntRestFull" [[2, 3, 4, 5]] [GeneVectorInt [1, 2, 3, 4, 5], StateFunc instructionVectorIntRest] emptyState vectorIntTestFunc "instructionVectorIntRestEmpty" [[]] [GeneVectorInt [], StateFunc instructionVectorIntRest] emptyState - vectorIntTestFunc "instructionVectorIntButLastFull" [[1,2,3,4]] [GeneVectorInt [1,2,3,4,5], StateFunc instructionVectorIntButLast] emptyState + vectorIntTestFunc "instructionVectorIntButLastFull" [[1, 2, 3, 4]] [GeneVectorInt [1, 2, 3, 4, 5], StateFunc instructionVectorIntButLast] emptyState vectorIntTestFunc "instructionVectorIntButLastEmpty" [[]] [GeneVectorInt [], StateFunc instructionVectorIntButLast] emptyState - intTestFunc "instructionVectorIntLength3" [3] [GeneVectorInt [1,2,3], StateFunc instructionVectorIntLength] emptyState + intTestFunc "instructionVectorIntLength3" [3] [GeneVectorInt [1, 2, 3], StateFunc instructionVectorIntLength] emptyState intTestFunc "instructionVectorIntLength0" [0] [GeneVectorInt [], StateFunc instructionVectorIntLength] emptyState - vectorIntTestFunc "instructionVectorIntReverse" [[4,3,2,1]] [GeneVectorInt [1,2,3,4], StateFunc instructionVectorIntReverse] emptyState - intTestFunc "instructionVectorIntPushAllFull" [1,2,3,4,99] [GeneVectorInt [1,2,3,4], GeneInt 99, StateFunc instructionVectorIntPushAll] emptyState + vectorIntTestFunc "instructionVectorIntReverse" [[4, 3, 2, 1]] [GeneVectorInt [1, 2, 3, 4], StateFunc instructionVectorIntReverse] emptyState + intTestFunc "instructionVectorIntPushAllFull" [1, 2, 3, 4, 99] [GeneVectorInt [1, 2, 3, 4], GeneInt 99, StateFunc instructionVectorIntPushAll] emptyState intTestFunc "instructionVectorIntPushAllEmpty" [99] [GeneVectorInt [], GeneInt 99, StateFunc instructionVectorIntPushAll] emptyState vectorIntTestFunc "instructionVectorIntMakeEmpty" [[]] [StateFunc instructionVectorIntMakeEmpty] emptyState boolTestFunc "instructionVectorIntIsEmptyTrue" [True] [GeneVectorInt [], StateFunc instructionVectorIntIsEmpty] emptyState - boolTestFunc "instructionVectorIntIsEmptyFalse" [False] [GeneVectorInt [1,2,3,4], StateFunc instructionVectorIntIsEmpty] emptyState - intTestFunc "instructionVectorIntIndexOf1" [1] [GeneVectorInt [1,2,3,4,5], GeneInt 2, StateFunc instructionVectorIntIndexOf] emptyState + boolTestFunc "instructionVectorIntIsEmptyFalse" [False] [GeneVectorInt [1, 2, 3, 4], StateFunc instructionVectorIntIsEmpty] emptyState + intTestFunc "instructionVectorIntIndexOf1" [1] [GeneVectorInt [1, 2, 3, 4, 5], GeneInt 2, StateFunc instructionVectorIntIndexOf] emptyState intTestFunc "instructionVectorIntIndexOfFail" [-1] [GeneVectorInt [], GeneInt 2, StateFunc instructionVectorIntIndexOf] emptyState - intTestFunc "instructionVectorIntOccurrencesOf2" [2] [GeneVectorInt [1,2,3,4,2,6,7], GeneInt 2, StateFunc instructionVectorIntOccurrencesOf] emptyState - intTestFunc "instructionVectorIntOccurrencesOf0" [0] [GeneVectorInt [1,2,3,4,2,6,7], GeneInt 0, StateFunc instructionVectorIntOccurrencesOf] emptyState - vectorIntTestFunc "instructionVectorIntSetNth3" [[0,1,2,99,4,5]] [GeneVectorInt [0,1,2,3,4,5], GeneInt 99, GeneInt 3, StateFunc instructionVectorIntSetNth] emptyState - vectorIntTestFunc "instructionVectorIntSetNth9" [[0,1,2,99,4,5]] [GeneVectorInt [0,1,2,3,4,5], GeneInt 99, GeneInt 9, StateFunc instructionVectorIntSetNth] emptyState - vectorIntTestFunc "instructionVectorIntReplace3" [[0,1,2,99,4,5,99,5,99]] [GeneInt 99, GeneInt 3, GeneVectorInt [0,1,2,3,4,5,3,5,3], StateFunc instructionVectorIntReplace] emptyState - vectorIntTestFunc "instructionVectorIntReplace-1" [[0,1,2,3,4,5,3,5,3]] [GeneInt 99, GeneInt (-1), GeneVectorInt [0,1,2,3,4,5,3,5,3], StateFunc instructionVectorIntReplace] emptyState - vectorIntTestFunc "instructionVectorIntReplaceFirst3" [[0,1,2,99,4,5,3,5,3]] [GeneInt 99, GeneInt 3, GeneVectorInt [0,1,2,3,4,5,3,5,3], StateFunc instructionVectorIntReplaceFirst] emptyState - vectorIntTestFunc "instructionVectorIntReplaceFirst-2" [[0,1,2,3,4,5,3,5,3]] [GeneInt 99, GeneInt (-2), GeneVectorInt [0,1,2,3,4,5,3,5,3], StateFunc instructionVectorIntReplaceFirst] emptyState - vectorIntTestFunc "instructionVectorIntRemove" [[0,1,2,4,5,5]] [GeneInt 3, GeneVectorInt [0,1,2,3,4,5,3,5,3], StateFunc instructionVectorIntRemove] emptyState - intTestFunc "instructionVectorIntIterate" [66] [GeneInt 40, GeneVectorInt [0,1,2,3,4,5,3,5,3], StateFunc instructionVectorIntIterate, StateFunc instructionIntAdd] emptyState + intTestFunc "instructionVectorIntOccurrencesOf2" [2] [GeneVectorInt [1, 2, 3, 4, 2, 6, 7], GeneInt 2, StateFunc instructionVectorIntOccurrencesOf] emptyState + intTestFunc "instructionVectorIntOccurrencesOf0" [0] [GeneVectorInt [1, 2, 3, 4, 2, 6, 7], GeneInt 0, StateFunc instructionVectorIntOccurrencesOf] emptyState + vectorIntTestFunc "instructionVectorIntSetNth3" [[0, 1, 2, 99, 4, 5]] [GeneVectorInt [0, 1, 2, 3, 4, 5], GeneInt 99, GeneInt 3, StateFunc instructionVectorIntSetNth] emptyState + vectorIntTestFunc "instructionVectorIntSetNth9" [[0, 1, 2, 99, 4, 5]] [GeneVectorInt [0, 1, 2, 3, 4, 5], GeneInt 99, GeneInt 9, StateFunc instructionVectorIntSetNth] emptyState + vectorIntTestFunc "instructionVectorIntReplace3" [[0, 1, 2, 99, 4, 5, 99, 5, 99]] [GeneInt 99, GeneInt 3, GeneVectorInt [0, 1, 2, 3, 4, 5, 3, 5, 3], StateFunc instructionVectorIntReplace] emptyState + vectorIntTestFunc "instructionVectorIntReplace-1" [[0, 1, 2, 3, 4, 5, 3, 5, 3]] [GeneInt 99, GeneInt (-1), GeneVectorInt [0, 1, 2, 3, 4, 5, 3, 5, 3], StateFunc instructionVectorIntReplace] emptyState + vectorIntTestFunc "instructionVectorIntReplaceFirst3" [[0, 1, 2, 99, 4, 5, 3, 5, 3]] [GeneInt 99, GeneInt 3, GeneVectorInt [0, 1, 2, 3, 4, 5, 3, 5, 3], StateFunc instructionVectorIntReplaceFirst] emptyState + vectorIntTestFunc "instructionVectorIntReplaceFirst-2" [[0, 1, 2, 3, 4, 5, 3, 5, 3]] [GeneInt 99, GeneInt (-2), GeneVectorInt [0, 1, 2, 3, 4, 5, 3, 5, 3], StateFunc instructionVectorIntReplaceFirst] emptyState + vectorIntTestFunc "instructionVectorIntRemove" [[0, 1, 2, 4, 5, 5]] [GeneInt 3, GeneVectorInt [0, 1, 2, 3, 4, 5, 3, 5, 3], StateFunc instructionVectorIntRemove] emptyState + intTestFunc "instructionVectorIntIterate" [66] [GeneInt 40, GeneVectorInt [0, 1, 2, 3, 4, 5, 3, 5, 3], StateFunc instructionVectorIntIterate, StateFunc instructionIntAdd] emptyState -- vector float functions vectorFloatTestFunc "instructionVectorFloatConcat" [[4.0, 5.0, 6.0, 1.0, 2.0, 3.0]] [GeneVectorFloat [1.0, 2.0, 3.0], GeneVectorFloat [4.0, 5.0, 6.0], StateFunc instructionVectorFloatConcat] emptyState vectorFloatTestFunc "instructionVectorFloatConj" [[99.0, 1.0, 2.0, 3.0]] [GeneVectorFloat [1.0, 2.0, 3.0], GeneFloat 99.0, StateFunc instructionVectorFloatConj] emptyState vectorFloatTestFunc "instructionFloatTakeN" [[1.0, 2.0], [6.0, 7.0, 8.0]] [GeneVectorFloat [6.0, 7.0, 8.0], GeneVectorFloat [1.0, 2.0, 3.0], GeneInt 2, StateFunc instructionVectorFloatTakeN] emptyState vectorFloatTestFunc "instructionVectorFloatSubVector" [[1.0, 2.0, 3.0]] [GeneVectorFloat [0.0, 1.0, 2.0, 3.0, 4.0, 5.0], GeneInt 3, GeneInt 1, StateFunc instructionVectorFloatSubVector] emptyState - floatTestFunc "instructionVectorFloatFirst" [1.0] [GeneVectorFloat [1.0,2.0,3.0,4.0,5.0], StateFunc instructionVectorFloatFirst] emptyState - floatTestFunc "instructionVectorFloatLast" [5.0] [GeneVectorFloat [1.0,2.0,3.0,4.0,5.0], StateFunc instructionVectorFloatLast] emptyState - floatTestFunc "instructionVectorFloatNthInBounds" [2.0] [GeneVectorFloat [1.0,2.0,3.0,4.0,5.0], GeneInt 1, StateFunc instructionVectorFloatNth] emptyState - floatTestFunc "instructionVectorFloatNthOverflow" [2.0] [GeneVectorFloat [1.0,2.0,3.0,4.0,5.0], GeneInt 6, StateFunc instructionVectorFloatNth] emptyState - vectorFloatTestFunc "instructionVectorFloatRestFull" [[2.0,3.0,4.0,5.0]] [GeneVectorFloat [1.0,2.0,3.0,4.0,5.0], StateFunc instructionVectorFloatRest] emptyState + floatTestFunc "instructionVectorFloatFirst" [1.0] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0, 5.0], StateFunc instructionVectorFloatFirst] emptyState + floatTestFunc "instructionVectorFloatLast" [5.0] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0, 5.0], StateFunc instructionVectorFloatLast] emptyState + floatTestFunc "instructionVectorFloatNthInBounds" [2.0] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0, 5.0], GeneInt 1, StateFunc instructionVectorFloatNth] emptyState + floatTestFunc "instructionVectorFloatNthOverflow" [2.0] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0, 5.0], GeneInt 6, StateFunc instructionVectorFloatNth] emptyState + vectorFloatTestFunc "instructionVectorFloatRestFull" [[2.0, 3.0, 4.0, 5.0]] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0, 5.0], StateFunc instructionVectorFloatRest] emptyState vectorFloatTestFunc "instructionVectorFloatRestEmpty" [[]] [GeneVectorFloat [], StateFunc instructionVectorFloatRest] emptyState - vectorFloatTestFunc "instructionVectorFloatButLastFull" [[1.0,2.0,3.0,4.0]] [GeneVectorFloat [1.0,2.0,3.0,4.0,5.0], StateFunc instructionVectorFloatButLast] emptyState + vectorFloatTestFunc "instructionVectorFloatButLastFull" [[1.0, 2.0, 3.0, 4.0]] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0, 5.0], StateFunc instructionVectorFloatButLast] emptyState vectorFloatTestFunc "instructionVectorFloatButLastEmpty" [[]] [GeneVectorFloat [], StateFunc instructionVectorFloatButLast] emptyState - intTestFunc "instructionVectorFloatLength3" [3] [GeneVectorFloat [1.0,2.0,3.0], StateFunc instructionVectorFloatLength] emptyState + intTestFunc "instructionVectorFloatLength3" [3] [GeneVectorFloat [1.0, 2.0, 3.0], StateFunc instructionVectorFloatLength] emptyState intTestFunc "instructionVectorFloatLength0" [0] [GeneVectorFloat [], StateFunc instructionVectorFloatLength] emptyState - vectorFloatTestFunc "instructionVectorFloatReverse" [[4.0,3.0,2.0,1.0]] [GeneVectorFloat [1.0,2.0,3.0,4.0], StateFunc instructionVectorFloatReverse] emptyState - floatTestFunc "instructionVectorFloatPushAllFull" [1.0,2.0,3.0,4.0,99.0] [GeneVectorFloat [1.0,2.0,3.0,4.0], GeneFloat 99.0, StateFunc instructionVectorFloatPushAll] emptyState + vectorFloatTestFunc "instructionVectorFloatReverse" [[4.0, 3.0, 2.0, 1.0]] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0], StateFunc instructionVectorFloatReverse] emptyState + floatTestFunc "instructionVectorFloatPushAllFull" [1.0, 2.0, 3.0, 4.0, 99.0] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0], GeneFloat 99.0, StateFunc instructionVectorFloatPushAll] emptyState floatTestFunc "instructionVectorFloatPushAllEmpty" [99.0] [GeneVectorFloat [], GeneFloat 99.0, StateFunc instructionVectorFloatPushAll] emptyState vectorFloatTestFunc "instructionVectorFloatMakeEmpty" [[]] [StateFunc instructionVectorFloatMakeEmpty] emptyState boolTestFunc "instructionVectorFloatIsEmptyTrue" [True] [GeneVectorFloat [], StateFunc instructionVectorFloatIsEmpty] emptyState - boolTestFunc "instructionVectorFloatIsEmptyFalse" [False] [GeneVectorFloat [1.0,2.0,3.0,4.0], StateFunc instructionVectorFloatIsEmpty] emptyState - intTestFunc "instructionVectorFloatIndexOf1" [1] [GeneVectorFloat [1.0,2.0,3.0,4.0,5.0], GeneFloat 2.0, StateFunc instructionVectorFloatIndexOf] emptyState + boolTestFunc "instructionVectorFloatIsEmptyFalse" [False] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0], StateFunc instructionVectorFloatIsEmpty] emptyState + intTestFunc "instructionVectorFloatIndexOf1" [1] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0, 5.0], GeneFloat 2.0, StateFunc instructionVectorFloatIndexOf] emptyState intTestFunc "instructionVectorFloatIndexOfFail" [-1] [GeneVectorFloat [], GeneFloat 2.0, StateFunc instructionVectorFloatIndexOf] emptyState - intTestFunc "instructionVectorFloatOccurrencesOf2" [2] [GeneVectorFloat [1.0,2.0,3.0,4.0,2.0,6.0,7.0], GeneFloat 2.0, StateFunc instructionVectorFloatOccurrencesOf] emptyState - intTestFunc "instructionVectorFloatOccurrencesOf0" [0] [GeneVectorFloat [1.0,2.0,3.0,4.0,2.0,6.0,7.0], GeneFloat 0.0, StateFunc instructionVectorFloatOccurrencesOf] emptyState - vectorFloatTestFunc "instructionVectorFloatSetNth3" [[0.0,1.0,2.0,99.0,4.0,5.0]] [GeneVectorFloat [0.0,1.0,2.0,3.0,4.0,5.0], GeneFloat 99.0, GeneInt 3, StateFunc instructionVectorFloatSetNth] emptyState - vectorFloatTestFunc "instructionVectorFloatSetNth9" [[0.0,1.0,2.0,99.0,4.0,5.0]] [GeneVectorFloat [0.0,1.0,2.0,3.0,4.0,5.0], GeneFloat 99.0, GeneInt 9, StateFunc instructionVectorFloatSetNth] emptyState - vectorFloatTestFunc "instructionVectorFloatReplace3" [[0.0,1.0,2.0,99.0,4.0,5.0,99.0,5.0,99.0]] [GeneFloat 99.0, GeneFloat 3.0, GeneVectorFloat [0.0,1.0,2.0,3.0,4.0,5.0,3.0,5.0,3.0], StateFunc instructionVectorFloatReplace] emptyState - vectorFloatTestFunc "instructionVectorFloatReplace-1" [[0.0,1.0,2.0,3.0,4.0,5.0,3.0,5.0,3.0]] [GeneFloat 99.0, GeneFloat (-1.0), GeneVectorFloat [0.0,1.0,2.0,3.0,4.0,5.0,3.0,5.0,3.0], StateFunc instructionVectorFloatReplace] emptyState - vectorFloatTestFunc "instructionVectorFloatReplaceFirst3" [[0.0,1.0,2.0,99.0,4.0,5.0,3.0,5.0,3.0]] [GeneFloat 99.0, GeneFloat 3.0, GeneVectorFloat [0.0,1.0,2.0,3.0,4.0,5.0,3.0,5.0,3.0], StateFunc instructionVectorFloatReplaceFirst] emptyState - vectorFloatTestFunc "instructionVectorFloatReplaceFirst-2" [[0.0,1.0,2.0,3.0,4.0,5.0,3.0,5.0,3.0]] [GeneFloat 99.0, GeneFloat (-2.0), GeneVectorFloat [0.0,1.0,2.0,3.0,4.0,5.0,3.0,5.0,3.0], StateFunc instructionVectorFloatReplaceFirst] emptyState - vectorFloatTestFunc "instructionVectorFloatRemove" [[0.0,1.0,2.0,4.0,5.0,5.0]] [GeneFloat 3, GeneVectorFloat [0.0,1.0,2.0,3.0,4.0,5.0,3.0,5.0,3.0], StateFunc instructionVectorFloatRemove] emptyState - floatTestFunc "instructionVectorFloatIterate" [66.0] [GeneFloat 40.0, GeneVectorFloat [0.0,1.0,2.0,3.0,4.0,5.0,3.0,5.0,3.0], StateFunc instructionVectorFloatIterate, StateFunc instructionFloatAdd] emptyState + intTestFunc "instructionVectorFloatOccurrencesOf2" [2] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0, 2.0, 6.0, 7.0], GeneFloat 2.0, StateFunc instructionVectorFloatOccurrencesOf] emptyState + intTestFunc "instructionVectorFloatOccurrencesOf0" [0] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0, 2.0, 6.0, 7.0], GeneFloat 0.0, StateFunc instructionVectorFloatOccurrencesOf] emptyState + vectorFloatTestFunc "instructionVectorFloatSetNth3" [[0.0, 1.0, 2.0, 99.0, 4.0, 5.0]] [GeneVectorFloat [0.0, 1.0, 2.0, 3.0, 4.0, 5.0], GeneFloat 99.0, GeneInt 3, StateFunc instructionVectorFloatSetNth] emptyState + vectorFloatTestFunc "instructionVectorFloatSetNth9" [[0.0, 1.0, 2.0, 99.0, 4.0, 5.0]] [GeneVectorFloat [0.0, 1.0, 2.0, 3.0, 4.0, 5.0], GeneFloat 99.0, GeneInt 9, StateFunc instructionVectorFloatSetNth] emptyState + vectorFloatTestFunc "instructionVectorFloatReplace3" [[0.0, 1.0, 2.0, 99.0, 4.0, 5.0, 99.0, 5.0, 99.0]] [GeneFloat 99.0, GeneFloat 3.0, GeneVectorFloat [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 3.0, 5.0, 3.0], StateFunc instructionVectorFloatReplace] emptyState + vectorFloatTestFunc "instructionVectorFloatReplace-1" [[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 3.0, 5.0, 3.0]] [GeneFloat 99.0, GeneFloat (-1.0), GeneVectorFloat [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 3.0, 5.0, 3.0], StateFunc instructionVectorFloatReplace] emptyState + vectorFloatTestFunc "instructionVectorFloatReplaceFirst3" [[0.0, 1.0, 2.0, 99.0, 4.0, 5.0, 3.0, 5.0, 3.0]] [GeneFloat 99.0, GeneFloat 3.0, GeneVectorFloat [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 3.0, 5.0, 3.0], StateFunc instructionVectorFloatReplaceFirst] emptyState + vectorFloatTestFunc "instructionVectorFloatReplaceFirst-2" [[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 3.0, 5.0, 3.0]] [GeneFloat 99.0, GeneFloat (-2.0), GeneVectorFloat [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 3.0, 5.0, 3.0], StateFunc instructionVectorFloatReplaceFirst] emptyState + vectorFloatTestFunc "instructionVectorFloatRemove" [[0.0, 1.0, 2.0, 4.0, 5.0, 5.0]] [GeneFloat 3, GeneVectorFloat [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 3.0, 5.0, 3.0], StateFunc instructionVectorFloatRemove] emptyState + floatTestFunc "instructionVectorFloatIterate" [66.0] [GeneFloat 40.0, GeneVectorFloat [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 3.0, 5.0, 3.0], StateFunc instructionVectorFloatIterate, StateFunc instructionFloatAdd] emptyState From 8fa26fbf2736fb4f3c0a003354382d124f9344c4 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sat, 1 Feb 2025 01:23:45 -0600 Subject: [PATCH 105/171] adjust for name in StateFunc --- src/Instructions/CodeInstructions.hs | 14 ++++++++++---- src/Instructions/ExecInstructions.hs | 16 +++++++++++----- src/Instructions/GenericInstructions.hs | 8 ++++---- src/Instructions/VectorCharInstructions.hs | 2 +- src/Instructions/VectorFloatInstructions.hs | 2 +- src/Instructions/VectorIntInstructions.hs | 2 +- src/Instructions/VectorLogicalInstructions.hs | 2 +- src/Instructions/VectorStringInstructions.hs | 2 +- 8 files changed, 30 insertions(+), 18 deletions(-) diff --git a/src/Instructions/CodeInstructions.hs b/src/Instructions/CodeInstructions.hs index 9fbb5f3..1caf48b 100644 --- a/src/Instructions/CodeInstructions.hs +++ b/src/Instructions/CodeInstructions.hs @@ -126,13 +126,19 @@ instructionCodeDoDup state = state -- https://erp12.github.io/pyshgp/html/core_instructions.html#code-do-then-pop instructionCodeDoThenPop :: State -> State -instructionCodeDoThenPop state@(State {_code = c1 : _, _exec = es}) = state {_exec = c1 : StateFunc instructionCodePop : es} +instructionCodeDoThenPop state@(State {_code = c1 : _, _exec = es}) = state {_exec = c1 : StateFunc (instructionCodePop, "instructionCodePop") : es} instructionCodeDoThenPop state = state +codeFromExec :: Gene +codeFromExec = StateFunc (instructionCodeFromExec, "instructionCodeFromExec") + +codeDoRange :: Gene +codeDoRange = StateFunc (instructionCodeDoRange, "instructionCodeDoRange") + instructionCodeDoRange :: State -> State instructionCodeDoRange state@(State {_code = (c1 : cs), _int = (i0 : i1 : is), _exec = es}) = if increment i0 i1 /= 0 - then state {_exec = c1 : Block [GeneInt (i1 + increment i0 i1), GeneInt i0, StateFunc instructionCodeFromExec, c1, StateFunc instructionCodeDoRange] : es, _int = i1 : is, _code = cs} + then state {_exec = c1 : Block [GeneInt (i1 + increment i0 i1), GeneInt i0, codeFromExec, c1, codeDoRange] : es, _int = i1 : is, _code = cs} else state {_exec = c1: es, _int = i1 : is, _code = cs} where increment :: Int -> Int -> Int @@ -146,14 +152,14 @@ instructionCodeDoCount :: State -> State instructionCodeDoCount state@(State {_code = (c : cs), _int = (i : is), _exec = es}) = if i < 1 then state - else state {_code = cs, _int = is, _exec = Block [GeneInt 0, GeneInt $ i - 1, StateFunc instructionCodeFromExec, c, StateFunc instructionCodeDoRange] : es} + else state {_code = cs, _int = is, _exec = Block [GeneInt 0, GeneInt $ i - 1, codeFromExec, c, codeDoRange] : es} instructionCodeDoCount state = state instructionCodeDoTimes :: State -> State instructionCodeDoTimes state@(State {_code = (c : cs), _int = (i : is), _exec = es}) = if i < 1 then state - else state {_code = cs, _int = is, _exec = Block [GeneInt 0, GeneInt $ i - 1, StateFunc instructionCodeFromExec, Block [StateFunc instructionIntPop, c], StateFunc instructionCodeDoRange] : es} + else state {_code = cs, _int = is, _exec = Block [GeneInt 0, GeneInt $ i - 1, codeFromExec, Block [StateFunc (instructionIntPop, "instructionIntPop"), c], codeDoRange] : es} instructionCodeDoTimes state = state instructionCodeIf :: State -> State diff --git a/src/Instructions/ExecInstructions.hs b/src/Instructions/ExecInstructions.hs index 9b6357a..c0ab519 100644 --- a/src/Instructions/ExecInstructions.hs +++ b/src/Instructions/ExecInstructions.hs @@ -50,10 +50,13 @@ instructionExecShoveDup state = instructionShoveDup state exec instructionExecIsEmpty :: State -> State instructionExecIsEmpty state = instructionIsEmpty state exec +execDoRange :: Gene +execDoRange = StateFunc (instructionExecDoRange, "instructionExecDoRange") + instructionExecDoRange :: State -> State instructionExecDoRange state@(State {_exec = (e1 : es), _int = (i0 : i1 : is)}) = if increment i0 i1 /= 0 - then state {_exec = e1 : Block [GeneInt (i1 + increment i0 i1), GeneInt i0, StateFunc instructionExecDoRange, e1] : es, _int = i1 : is} + then state {_exec = e1 : Block [GeneInt (i1 + increment i0 i1), GeneInt i0, execDoRange, e1] : es, _int = i1 : is} else state {_exec = e1 : es, _int = i1 : is} where increment :: Int -> Int -> Int @@ -67,28 +70,31 @@ instructionExecDoCount :: State -> State instructionExecDoCount state@(State {_exec = (e : es), _int = (i : is)}) = if i < 1 then state - else state {_exec = Block [GeneInt 0, GeneInt $ i - 1, StateFunc instructionExecDoRange, e] : es, _int = is} + else state {_exec = Block [GeneInt 0, GeneInt $ i - 1, execDoRange, e] : es, _int = is} instructionExecDoCount state = state instructionExecDoTimes :: State -> State instructionExecDoTimes state@(State {_exec = (e : es), _int = (i : is)}) = if i < 1 then state - else state {_exec = Block [GeneInt 0, GeneInt $ i - 1, StateFunc instructionExecDoRange, Block [StateFunc instructionIntPop, e]] : es, _int = is} + else state {_exec = Block [GeneInt 0, GeneInt $ i - 1, execDoRange, Block [StateFunc (instructionIntPop, "instructionIntPop"), e]] : es, _int = is} instructionExecDoTimes state = state +execWhile :: Gene +execWhile = StateFunc (instructionExecWhile, "instructionExecWhile") + instructionExecWhile :: State -> State instructionExecWhile state@(State {_exec = (_ : es), _bool = []}) = state {_exec = es} instructionExecWhile state@(State {_exec = alles@(e : es), _bool = (b : bs)}) = if b - then state {_exec = e : StateFunc instructionExecWhile : alles, _bool = bs} + then state {_exec = e : execWhile : alles, _bool = bs} else state {_exec = es} instructionExecWhile state = state instructionExecDoWhile :: State -> State instructionExecDoWhile state@(State {_exec = alles@(e : _)}) = - state {_exec = e : StateFunc instructionExecWhile : alles} + state {_exec = e : execWhile : alles} instructionExecDoWhile state = state -- Eats the _boolean no matter what diff --git a/src/Instructions/GenericInstructions.hs b/src/Instructions/GenericInstructions.hs index 872fff1..aac7bbc 100644 --- a/src/Instructions/GenericInstructions.hs +++ b/src/Instructions/GenericInstructions.hs @@ -327,17 +327,17 @@ instructionVectorRemove state primAccessor vectorAccessor = (Just (v1, vs), Just (p1, ps)) -> state & vectorAccessor .~ (replace v1 [p1] [] Nothing : vs) & primAccessor .~ ps _ -> state -instructionVectorIterate :: State -> Lens' State [a] -> Lens' State [[a]] -> ([a] -> Gene) -> (State -> State) -> State -instructionVectorIterate state@(State {_exec = e1 : es}) primAccessor vectorAccessor vectorType typeIterateFunction = +instructionVectorIterate :: State -> Lens' State [a] -> Lens' State [[a]] -> ([a] -> Gene) -> (State -> State) -> String -> State +instructionVectorIterate state@(State {_exec = e1 : es}) primAccessor vectorAccessor vectorType typeIterateFunction typeIterateFunctionName = case uncons (view vectorAccessor state) of Just ([], vs) -> state{_exec = es} & vectorAccessor .~ vs Just ([x], vs) -> state & primAccessor .~ (x : view primAccessor state) & vectorAccessor .~ vs Just (v1, vs) -> (case uncons v1 of - Just (nv1, nvs) -> state{_exec = e1 : vectorType nvs : StateFunc typeIterateFunction : e1 : es} & primAccessor .~ (nv1 : view primAccessor state) & vectorAccessor .~ vs + Just (nv1, nvs) -> state{_exec = e1 : vectorType nvs : StateFunc (typeIterateFunction, typeIterateFunctionName) : e1 : es} & primAccessor .~ (nv1 : view primAccessor state) & vectorAccessor .~ vs _ -> state) -- This should never happen _ -> state -instructionVectorIterate state _ _ _ _ = state +instructionVectorIterate state _ _ _ _ _ = state instructionCodeFrom :: State -> Lens' State [a] -> (a -> Gene) -> State instructionCodeFrom state@(State {_code = cs}) accessor geneType = diff --git a/src/Instructions/VectorCharInstructions.hs b/src/Instructions/VectorCharInstructions.hs index 30c9d0b..7467163 100644 --- a/src/Instructions/VectorCharInstructions.hs +++ b/src/Instructions/VectorCharInstructions.hs @@ -64,7 +64,7 @@ instructionVectorCharRemove :: State -> State instructionVectorCharRemove state = instructionVectorRemove state char vectorChar instructionVectorCharIterate :: State -> State -instructionVectorCharIterate state = instructionVectorIterate state char vectorChar GeneVectorChar instructionVectorCharIterate +instructionVectorCharIterate state = instructionVectorIterate state char vectorChar GeneVectorChar instructionVectorCharIterate "instructionVectorCharIterate" instructionVectorCharPop :: State -> State instructionVectorCharPop state = instructionPop state vectorChar diff --git a/src/Instructions/VectorFloatInstructions.hs b/src/Instructions/VectorFloatInstructions.hs index b45f2dc..18dabc9 100644 --- a/src/Instructions/VectorFloatInstructions.hs +++ b/src/Instructions/VectorFloatInstructions.hs @@ -64,7 +64,7 @@ instructionVectorFloatRemove :: State -> State instructionVectorFloatRemove state = instructionVectorRemove state float vectorFloat instructionVectorFloatIterate :: State -> State -instructionVectorFloatIterate state = instructionVectorIterate state float vectorFloat GeneVectorFloat instructionVectorFloatIterate +instructionVectorFloatIterate state = instructionVectorIterate state float vectorFloat GeneVectorFloat instructionVectorFloatIterate "instructionVectorFloatIterate" instructionVectorFloatPop :: State -> State instructionVectorFloatPop state = instructionPop state vectorFloat diff --git a/src/Instructions/VectorIntInstructions.hs b/src/Instructions/VectorIntInstructions.hs index 7bf3bf3..bb135ff 100644 --- a/src/Instructions/VectorIntInstructions.hs +++ b/src/Instructions/VectorIntInstructions.hs @@ -64,7 +64,7 @@ instructionVectorIntRemove :: State -> State instructionVectorIntRemove state = instructionVectorRemove state int vectorInt instructionVectorIntIterate :: State -> State -instructionVectorIntIterate state = instructionVectorIterate state int vectorInt GeneVectorInt instructionVectorIntIterate +instructionVectorIntIterate state = instructionVectorIterate state int vectorInt GeneVectorInt instructionVectorIntIterate "instructionVectorIntIterate" instructionVectorIntPop :: State -> State instructionVectorIntPop state = instructionPop state vectorChar diff --git a/src/Instructions/VectorLogicalInstructions.hs b/src/Instructions/VectorLogicalInstructions.hs index af5e0f5..35d7add 100644 --- a/src/Instructions/VectorLogicalInstructions.hs +++ b/src/Instructions/VectorLogicalInstructions.hs @@ -64,7 +64,7 @@ instructionVectorBoolRemove :: State -> State instructionVectorBoolRemove state = instructionVectorRemove state bool vectorBool instructionVectorBoolIterate :: State -> State -instructionVectorBoolIterate state = instructionVectorIterate state bool vectorBool GeneVectorBool instructionVectorBoolIterate +instructionVectorBoolIterate state = instructionVectorIterate state bool vectorBool GeneVectorBool instructionVectorBoolIterate "instructionVectorBoolIterate" instructionVectorBoolPop :: State -> State instructionVectorBoolPop state = instructionPop state vectorBool diff --git a/src/Instructions/VectorStringInstructions.hs b/src/Instructions/VectorStringInstructions.hs index ee524d6..def670a 100644 --- a/src/Instructions/VectorStringInstructions.hs +++ b/src/Instructions/VectorStringInstructions.hs @@ -64,7 +64,7 @@ instructionVectorStringRemove :: State -> State instructionVectorStringRemove state = instructionVectorRemove state string vectorString instructionVectorStringIterate :: State -> State -instructionVectorStringIterate state = instructionVectorIterate state string vectorString GeneVectorString instructionVectorStringIterate +instructionVectorStringIterate state = instructionVectorIterate state string vectorString GeneVectorString instructionVectorStringIterate "instructionVectorStringIterate" instructionVectorStringPop :: State -> State instructionVectorStringPop state = instructionPop state vectorString From 27ee85ae28ee6ef0b7d77086db1ece4927c8f0c0 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sat, 1 Feb 2025 01:24:07 -0600 Subject: [PATCH 106/171] string name in StateFunc --- src/Push.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Push.hs b/src/Push.hs index d7755c8..d25f203 100644 --- a/src/Push.hs +++ b/src/Push.hs @@ -61,7 +61,7 @@ interpretExec state@(State {_exec = e : es}) = (GeneVectorBool val) -> interpretExec (state & exec .~ es & vectorBool .~ val : view vectorBool state) (GeneVectorString val) -> interpretExec (state & exec .~ es & vectorString .~ val : view vectorString state) (GeneVectorChar val) -> interpretExec (state & exec .~ es & vectorChar .~ val : view vectorChar state) - (StateFunc func) -> interpretExec $ func state {_exec = es} + (StateFunc (func, _)) -> interpretExec $ func state {_exec = es} (Block block) -> interpretExec (state {_exec = block ++ es}) (PlaceInput val) -> interpretExec (state {_exec = (view input state Map.! val) : es}) Close -> undefined -- This should be removed later. Will be converted to Blocks in the Plushy -> Exec stack process From 319f682d4ae0d0b0aca11b2d1b851917de17eea3 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sat, 1 Feb 2025 01:24:27 -0600 Subject: [PATCH 107/171] clean up/basic tests --- src/State.hs | 11 +++-------- test/Main.hs | 9 ++++++++- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/State.hs b/src/State.hs index 6cb3b08..959d3b0 100644 --- a/src/State.hs +++ b/src/State.hs @@ -23,7 +23,7 @@ data Gene | GeneVectorBool [Bool] | GeneVectorString [String] | GeneVectorChar [Char] - | StateFunc (State -> State) + | StateFunc (State -> State, String) -- The string stores the name of the function | PlaceInput String | Close | Block [Gene] @@ -42,7 +42,7 @@ instance Eq Gene where GeneVectorString xs == GeneVectorString ys = xs == ys GeneVectorChar xs == GeneVectorChar ys = xs == ys Close == Close = True - StateFunc _ == StateFunc _ = True -- This line is probably not the best thing to do + StateFunc (_, nameX) == StateFunc (_, nameY) = nameX == nameY Block x == Block y = x == y _ == _ = False @@ -52,7 +52,7 @@ instance Show Gene where show (GeneBool x) = "Bool: " <> show x show (GeneString x) = "String: " <> x show (GeneChar x) = "Char: " <> show x - show (StateFunc x) = "Func: " <> show x + show (StateFunc (_, funcName)) = "Func: " <> funcName show (PlaceInput x) = "In: " <> show x show (GeneVectorInt xs) = "Int Vec: " <> show xs show (GeneVectorFloat xs) = "Float Vec: " <> show xs @@ -101,10 +101,6 @@ data State = State } deriving (Show, Eq, Generic) --- This needs to be updated later -instance Show (State -> State) where - show _ = "unnamed" - instance Arbitrary State where arbitrary = do arbExec <- arbitrary @@ -122,7 +118,6 @@ instance Arbitrary State where arbParameter <- arbitrary -- arbInput <- arbitrary State arbExec arbCode arbInt arbFloat arbBool arbString arbChar arbVectorInt arbVectorFloat arbVectorBool arbVectorString arbVectorChar arbParameter <$> arbitrary - -- Thanks hlint lol instance CoArbitrary State diff --git a/test/Main.hs b/test/Main.hs index 2717136..cef8d76 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -16,9 +16,16 @@ myArgs = -- quickCheckWith myArgs prop_IntAdd +-- These two used for ghci testing +qcw :: Testable a => a-> IO () +qcw = quickCheckWith myArgs + +vcw :: Testable a => a-> IO () +vcw = verboseCheckWith myArgs + -- Running this with a large max size leads quickCheck to hang, and that's bad prop_IntAdd :: State -> Bool -prop_IntAdd state@(State {_int = i1 : i2 : is}) = i1 + i2 == head (_int (instructionIntAdd state)) +prop_IntAdd state@(State {_int = i1 : i2 : _}) = i1 + i2 == head (_int (instructionIntAdd state)) prop_IntAdd state = state == instructionIntAdd state main :: IO () From 413b9eee4411d673971d65d0206eada5fc8299f4 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sat, 1 Feb 2025 18:15:09 -0600 Subject: [PATCH 108/171] only keep things I like from learn_quickcheck --- HushGP.cabal | 4 +- src/Push.hs | 3 +- src/State.hs | 45 ------- test/Main.hs | 335 +++++++++++++++++++++++++++++++++++++++++++++++---- 4 files changed, 315 insertions(+), 72 deletions(-) diff --git a/HushGP.cabal b/HushGP.cabal index 93e70dd..d3ff73c 100644 --- a/HushGP.cabal +++ b/HushGP.cabal @@ -52,7 +52,6 @@ library , Instructions.VectorStringInstructions , Instructions.VectorLogicalInstructions , Instructions.VectorCharInstructions - , LearnQuickCheck -- Modules included in this library but not exported. -- other-modules: @@ -62,7 +61,7 @@ library -- Other library packages from which modules are imported. build-depends: - base, containers, lens, split, QuickCheck + base, containers, lens, split -- Directories containing source files. hs-source-dirs: src @@ -120,6 +119,5 @@ test-suite HushGP-test build-depends: base, HushGP, - QuickCheck, lens diff --git a/src/Push.hs b/src/Push.hs index d25f203..86e5326 100644 --- a/src/Push.hs +++ b/src/Push.hs @@ -2,8 +2,7 @@ module Push where import Control.Lens import Data.Map qualified as Map --- import Instructions.IntInstructions --- import Instructions.ExecInstructions + import State -- import Debug.Trace (trace, traceStack) diff --git a/src/State.hs b/src/State.hs index 959d3b0..74d5100 100644 --- a/src/State.hs +++ b/src/State.hs @@ -1,4 +1,3 @@ -{-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE TemplateHaskell #-} module State where @@ -6,7 +5,6 @@ module State where import Control.Lens hiding (elements) import Data.Map qualified as Map import GHC.Generics -import Test.QuickCheck -- The exec stack must store heterogenous types, -- and we must be able to detect that type at runtime. @@ -27,7 +25,6 @@ data Gene | PlaceInput String | Close | Block [Gene] - deriving (Generic) instance Eq Gene where GeneInt x == GeneInt y = x == y @@ -62,27 +59,6 @@ instance Show Gene where show Close = "Close" show (Block xs) = "Block: " <> show xs -instance CoArbitrary Gene - -instance Arbitrary Gene where - arbitrary = - oneof - [ GeneInt <$> arbitrary, - GeneFloat <$> arbitrary, - GeneBool <$> arbitrary, - GeneString <$> arbitrary, - GeneChar <$> arbitrary, - StateFunc <$> arbitrary, - PlaceInput <$> arbitrary, - GeneVectorInt <$> arbitrary, - GeneVectorFloat <$> arbitrary, - GeneVectorBool <$> arbitrary, - GeneVectorString <$> arbitrary, - GeneVectorChar <$> arbitrary, - Block <$> arbitrary, - return Close - ] - data State = State { _exec :: [Gene], _code :: [Gene], @@ -101,27 +77,6 @@ data State = State } deriving (Show, Eq, Generic) -instance Arbitrary State where - arbitrary = do - arbExec <- arbitrary - arbCode <- arbitrary - arbInt <- arbitrary - arbFloat <- arbitrary - arbBool <- arbitrary - arbString <- arbitrary - arbChar <- arbitrary - arbVectorInt <- arbitrary - arbVectorFloat <- arbitrary - arbVectorBool <- arbitrary - arbVectorString <- arbitrary - arbVectorChar <- arbitrary - arbParameter <- arbitrary - -- arbInput <- arbitrary - State arbExec arbCode arbInt arbFloat arbBool arbString arbChar arbVectorInt arbVectorFloat arbVectorBool arbVectorString arbVectorChar arbParameter <$> arbitrary --- Thanks hlint lol - -instance CoArbitrary State - emptyState :: State emptyState = State diff --git a/test/Main.hs b/test/Main.hs index cef8d76..c3728e8 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -1,33 +1,324 @@ +import Control.Exception (assert) import Instructions +import Push import State -import Test.QuickCheck --- import Control.Lens +-- import Debug.Trace -myArgs = - Args - { replay = Nothing, - maxSuccess = 100, - maxDiscardRatio = 10, - maxSize = 10, - chatty = True, - maxShrinks = 500 - } +-- TODO: Need a function that can compare states. +-- May look at quickCheck later --- quickCheckWith myArgs prop_IntAdd +intTestFunc :: String -> [Int] -> [Gene] -> State -> IO () +intTestFunc name goal genome startState = + let state = loadProgram genome startState + in assert (goal == _int (interpretExec state)) putStrLn (name ++ " passed test.") --- These two used for ghci testing -qcw :: Testable a => a-> IO () -qcw = quickCheckWith myArgs +floatTestFunc :: String -> [Float] -> [Gene] -> State -> IO () +floatTestFunc name goal genome startState = + let state = loadProgram genome startState + in assert (goal == _float (interpretExec state)) putStrLn (name ++ " passed test.") -vcw :: Testable a => a-> IO () -vcw = verboseCheckWith myArgs +boolTestFunc :: String -> [Bool] -> [Gene] -> State -> IO () +boolTestFunc name goal genome startState = + let state = loadProgram genome startState + in assert (goal == _bool (interpretExec state)) putStrLn (name ++ " passed test.") --- Running this with a large max size leads quickCheck to hang, and that's bad -prop_IntAdd :: State -> Bool -prop_IntAdd state@(State {_int = i1 : i2 : _}) = i1 + i2 == head (_int (instructionIntAdd state)) -prop_IntAdd state = state == instructionIntAdd state +codeTestFunc :: String -> [Gene] -> [Gene] -> State -> IO () +codeTestFunc name goal genome startState = + let state = loadProgram genome startState + in assert (goal == _code (interpretExec state)) putStrLn (name <> " passed test.") + +stringTestFunc :: String -> [String] -> [Gene] -> State -> IO () +stringTestFunc name goal genome startState = + let state = loadProgram genome startState + in assert (goal == _string (interpretExec state)) putStrLn (name <> " passed test.") + +charTestFunc :: String -> [Char] -> [Gene] -> State -> IO () +charTestFunc name goal genome startState = + let state = loadProgram genome startState + in assert (goal == _char (interpretExec state)) putStrLn (name <> " passed test.") + +vectorIntTestFunc :: String -> [[Int]] -> [Gene] -> State -> IO () +vectorIntTestFunc name goal genome startState = + let state = loadProgram genome startState + in assert (goal == _vectorInt (interpretExec state)) putStrLn (name <> " passed test.") + +vectorFloatTestFunc :: String -> [[Float]] -> [Gene] -> State -> IO () +vectorFloatTestFunc name goal genome startState = + let state = loadProgram genome startState + in assert (goal == _vectorFloat (interpretExec state)) putStrLn (name <> " passed test.") main :: IO () main = do - putStrLn "hello" + -- Int tests + intTestFunc "instructionIntAdd" [8] [GeneInt 6, GeneInt 2, StateFunc (instructionIntAdd, "placeholder")] emptyState + intTestFunc "instructionIntSub" [4] [GeneInt 6, GeneInt 2, StateFunc (instructionIntSub, "placeholder")] emptyState + intTestFunc "instructionIntMul" [12] [GeneInt 6, GeneInt 2, StateFunc (instructionIntMul, "placeholder")] emptyState + intTestFunc "instructionIntDiv" [3] [GeneInt 6, GeneInt 2, StateFunc (instructionIntDiv, "placeholder")] emptyState + intTestFunc "instructionIntDiv0" [0, 2] [GeneInt 2, GeneInt 0, StateFunc (instructionIntDiv, "placeholder")] emptyState + intTestFunc "instructionIntMod" [3] [GeneInt 13, GeneInt 5, StateFunc (instructionIntMod, "placeholder")] emptyState + intTestFunc "instructionIntPop" [2] [GeneInt 2, GeneInt 0, StateFunc (instructionIntPop, "placeholder")] emptyState + intTestFunc "instructionIntDup" [3, 3, 2] [GeneInt 2, GeneInt 3, StateFunc (instructionIntDup, "placeholder")] emptyState + intTestFunc "instructionIntDupN3" [2, 2, 2] [GeneInt 2, GeneInt 3, StateFunc (instructionIntDupN, "placeholder")] emptyState + intTestFunc "instructionIntDupN-1" [0] [GeneInt 0, GeneInt 2, GeneInt (-1), StateFunc (instructionIntDupN, "placeholder")] emptyState + intTestFunc "instructionIntSwap" [2, 0, 3] [GeneInt 3, GeneInt 2, GeneInt 0, StateFunc (instructionIntSwap, "placeholder")] emptyState + intTestFunc "instructionIntSwapFail" [1] [GeneInt 1, StateFunc (instructionIntSwap, "placeholder")] emptyState + intTestFunc "instructionIntRot" [1, 3, 2] [GeneInt 1, GeneInt 2, GeneInt 3, StateFunc (instructionIntRot, "placeholder")] emptyState + intTestFunc "instructionIntRotFail" [7, 8] [GeneInt 8, GeneInt 7, StateFunc (instructionIntRot, "placeholder")] emptyState + intTestFunc "instructionIntFlush" [] [GeneInt 9696, GeneInt 92, GeneInt 420, StateFunc (instructionIntFlush, "placeholder")] emptyState -- I think I'm funny + intTestFunc "instructionIntStackDepth" [2, 51, 52] [GeneInt 52, GeneInt 51, StateFunc (instructionIntStackDepth, "placeholder")] emptyState + intTestFunc "instructionIntYank" [3, 3, 2, 1] [GeneInt 3, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 4, StateFunc (instructionIntYank, "placeholder")] emptyState + intTestFunc "instructionIntYankDup" [3, 3, 2, 1, 3] [GeneInt 3, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 4, StateFunc (instructionIntYankDup, "placeholder")] emptyState + intTestFunc "instructionIntShove" [2, 3, 1, 1] [GeneInt 1, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 2, StateFunc (instructionIntShove, "placeholder")] emptyState + intTestFunc "instructionIntShoveDup" [3, 2, 3, 1, 1] [GeneInt 1, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 2, StateFunc (instructionIntShoveDup, "placeholder")] emptyState + + -- Exec tests + intTestFunc "instructionExecIf" [6, 5] [GeneBool True, StateFunc (instructionExecIf, "placeholder"), Block [GeneInt 5, GeneInt 6], Block [GeneInt 7, GeneInt 8]] emptyState + intTestFunc "instructionExecDup" [8] [StateFunc (instructionExecDup, "placeholder"), GeneInt 4, StateFunc (instructionIntAdd, "placeholder")] emptyState + intTestFunc "instructionExecDoRange" [12] [GeneInt 2, Block [GeneInt 4, GeneInt 1, StateFunc (instructionExecDoRange, "placeholder")], StateFunc (instructionIntAdd, "placeholder")] emptyState + intTestFunc "instructionExecDoCount" [8] [GeneInt 2, Block [GeneInt 4, StateFunc (instructionExecDoCount, "placeholder")], StateFunc (instructionIntAdd, "placeholder")] emptyState + intTestFunc "instructionExecDoTimes" [69, 69, 69, 69, 2] [GeneInt 2, Block [GeneInt 4, StateFunc (instructionExecDoTimes, "placeholder")], GeneInt 69] emptyState + intTestFunc "instructionExecWhile" [70, 70] [GeneBool False, GeneBool True, GeneBool True, StateFunc (instructionExecWhile, "placeholder"), GeneInt 70] emptyState + intTestFunc "instructionExecDoWhile" [70, 70, 70] [GeneBool False, GeneBool True, GeneBool True, StateFunc (instructionExecDoWhile, "placeholder"), GeneInt 70] emptyState + intTestFunc "instructionExecWhenTrue" [71] [GeneBool True, StateFunc (instructionExecWhen, "placeholder"), GeneInt 71] emptyState + + let loadedState = loadProgram [GeneBool False, StateFunc (instructionExecWhen, "placeholder"), GeneInt 71] emptyState + assert (emptyState == interpretExec loadedState) putStrLn "instructionExecWhenFalse passed test." + + -- Float tests + floatTestFunc "instructionFloatAdd" [4.32] [GeneFloat 4.01, GeneFloat 0.31, StateFunc (instructionFloatAdd, "placeholder")] emptyState + floatTestFunc "instructionFloatSub" [3.6900003] [GeneFloat 4.01, GeneFloat 0.32, StateFunc (instructionFloatSub, "placeholder")] emptyState + floatTestFunc "instructionFloatMul" [1.38] [GeneFloat 0.12, GeneFloat 11.5, StateFunc (instructionFloatMul, "placeholder")] emptyState + floatTestFunc "instructionFloatDiv" [57.5] [GeneFloat 11.5, GeneFloat 0.2, StateFunc (instructionFloatDiv, "placeholder")] emptyState + floatTestFunc "instructionFloatDiv0" [0, 69.69] [GeneFloat 69.69, GeneFloat 0.0, StateFunc (instructionFloatDiv, "placeholder")] emptyState + floatTestFunc "instructionFloatYank" [1.1, 4.4, 3.3, 2.2] [GeneInt 3, GeneFloat 1.1, GeneFloat 2.2, GeneFloat 3.3, GeneFloat 4.4, StateFunc (instructionFloatYank, "placeholder")] emptyState + floatTestFunc "instructionFloatYankDup" [1.1, 4.4, 3.3, 2.2, 1.1] [GeneInt 3, GeneFloat 1.1, GeneFloat 2.2, GeneFloat 3.3, GeneFloat 4.4, StateFunc (instructionFloatYankDup, "placeholder")] emptyState + floatTestFunc "instructionFloatShove" [3.3, 2.2, 4.4, 1.1] [GeneInt 3, GeneFloat 1.1, GeneFloat 2.2, GeneFloat 3.3, GeneFloat 4.4, StateFunc (instructionFloatShove, "placeholder")] emptyState + floatTestFunc "instructionFloatShoveDup" [4.4, 3.3, 2.2, 4.4, 1.1] [GeneInt 3, GeneFloat 1.1, GeneFloat 2.2, GeneFloat 3.3, GeneFloat 4.4, StateFunc (instructionFloatShoveDup, "placeholder")] emptyState + floatTestFunc "instructionFloatDupNonEmpty" [4.4, 4.4, 3.3] [GeneFloat 3.3, GeneFloat 4.4, StateFunc (instructionFloatDup, "placeholder")] emptyState + floatTestFunc "instructionFloatDupEmpty" [] [StateFunc (instructionFloatDup, "placeholder")] emptyState + floatTestFunc "instructionFloatDupN3" [4.4, 4.4, 4.4, 3.3] [GeneFloat 3.3, GeneFloat 4.4, GeneInt 3, StateFunc (instructionFloatDupN, "placeholder")] emptyState + floatTestFunc "instructionFloatDupN-1" [3.3] [GeneFloat 3.3, GeneFloat 4.4, GeneInt (-1), StateFunc (instructionFloatDupN, "placeholder")] emptyState + boolTestFunc "instructionIntEqTrue" [True] [GeneInt 3, GeneInt 3, StateFunc (instructionIntEq, "placeholder")] emptyState + boolTestFunc "instructionIntEqFalse" [False] [GeneInt 3, GeneInt 5, StateFunc (instructionIntEq, "placeholder")] emptyState + boolTestFunc "instructionIntEqFail" [] [GeneInt 3, StateFunc (instructionIntEq, "placeholder")] emptyState + + -- Code tests + codeTestFunc "instructionCodeFromExec" [] [StateFunc (instructionCodeFromExec, "placeholder"), StateFunc (instructionFloatFromInt, "placeholder"), StateFunc (instructionCodePop, "placeholder")] emptyState + intTestFunc "instructionCodeDoRange" [18] [GeneInt 3, GeneInt 6, StateFunc (instructionCodeFromExec, "placeholder"), StateFunc (instructionIntAdd, "placeholder"), StateFunc (instructionCodeDoRange, "placeholder")] emptyState + -- How to test instructionCodeDoThenPop????? + codeTestFunc "instructionCodeFirst" [GeneInt 5] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 5, StateFunc (instructionIntSub, "placeholder")], StateFunc (instructionCodeFirst, "placeholder")] emptyState + codeTestFunc "instructionCodeLast" [GeneBool True] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 5, StateFunc (instructionIntSub, "placeholder"), GeneBool True], StateFunc (instructionCodeLast, "placeholder")] emptyState + codeTestFunc "instructionCodeTail" [Block [GeneFloat 3.2, GeneBool True, GeneInt 3]] [StateFunc (instructionCodeFromExec, "placeholder"), Block [StateFunc (instructionFloatAdd, "placeholder"), GeneFloat 3.2, GeneBool True, GeneInt 3], StateFunc (instructionCodeTail, "placeholder")] emptyState + codeTestFunc "instructionCodeInit" [Block [GeneVectorInt [1], GeneFloat 3.2, GeneBool True]] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneVectorInt [1], GeneFloat 3.2, GeneBool True, GeneInt 3], StateFunc (instructionCodeInit, "placeholder")] emptyState + codeTestFunc "instructionCodeWrap" [Block [GeneInt 3]] [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 3, StateFunc (instructionCodeWrap, "placeholder")] emptyState + codeTestFunc "instructionCodeList" [Block [GeneFloat 5.43, GeneInt 3]] [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 3, StateFunc (instructionCodeFromExec, "placeholder"), GeneFloat 5.43, StateFunc (instructionCodeList, "placeholder")] emptyState + codeTestFunc "instructionCodeCombine2Blocks" [Block [GeneInt 3, GeneInt 4, GeneInt 1, GeneInt 2]] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 1, GeneInt 2], StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 3, GeneInt 4], StateFunc (instructionCodeCombine, "placeholder")] emptyState + codeTestFunc "instructionCodeCombine1Block1Single" [Block [GeneInt 3, GeneInt 4, GeneInt 1]] [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 1, StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 3, GeneInt 4], StateFunc (instructionCodeCombine, "placeholder")] emptyState + codeTestFunc "instructionCodeCombine1Single1Block" [Block [GeneInt 3, GeneInt 1, GeneInt 2]] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 1, GeneInt 2], StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 3, StateFunc (instructionCodeCombine, "placeholder")] emptyState + codeTestFunc "instrucitonCodeCombine2Single" [Block [GeneInt 2, GeneInt 1]] [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 1, StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 2, StateFunc (instructionCodeCombine, "placeholder")] emptyState + intTestFunc "instructionCodeDo" [3] [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 3, StateFunc (instructionCodeDo, "placeholder")] emptyState + -- How to test instructionCodeDoDup??? We would would need a multi stack testing function + boolTestFunc "instructionCodeIsCodeBlockTrue" [True] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 0], StateFunc (instructionCodeIsCodeBlock, "placeholder")] emptyState + boolTestFunc "instructionCodeIsCodeBlockFalse" [False] [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 0, StateFunc (instructionCodeIsCodeBlock, "placeholder")] emptyState + boolTestFunc "instructionCodeIsSingularTrue" [True] [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 0, StateFunc (instructionCodeIsSingular, "placeholder")] emptyState + boolTestFunc "instructionCodeIsSingularFalse" [False] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 0], StateFunc (instructionCodeIsSingular, "placeholder")] emptyState + intTestFunc "instructionCodeDoCount" [15] [GeneInt 6, StateFunc (instructionCodeFromExec, "placeholder"), StateFunc (instructionIntAdd, "placeholder"), StateFunc (instructionCodeDoCount, "placeholder")] emptyState + intTestFunc "instructionCodeDoTimes" [13] [GeneInt 6, GeneInt 3, GeneInt 4, GeneInt 2, StateFunc (instructionCodeFromExec, "placeholder"), StateFunc (instructionIntAdd, "placeholder"), StateFunc (instructionCodeDoTimes, "placeholder")] emptyState + intTestFunc "instructionCodeIfTrue" [6] [GeneBool True, StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 3, StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 6, StateFunc (instructionCodeIf, "placeholder")] emptyState + intTestFunc "instructionCodeIfFalse" [3] [GeneBool False, StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 3, StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 6, StateFunc (instructionCodeIf, "placeholder")] emptyState + intTestFunc "instructionCodeWhen" [6, 3, 6] [GeneInt 6, GeneInt 3, GeneInt 4, GeneInt 2, GeneBool True, StateFunc (instructionCodeFromExec, "placeholder"), StateFunc (instructionIntAdd, "placeholder"), StateFunc (instructionCodeWhen, "placeholder")] emptyState + boolTestFunc "instructionCodeMemberTrue" [True] [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 2, StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneFloat 3.6, GeneInt 2, GeneVectorInt [8, 9]], StateFunc (instructionCodeMember, "placeholder")] emptyState + boolTestFunc "instructionCodeMemberFalse" [False] [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 7, StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneFloat 3.6, GeneInt 2, GeneVectorInt [8, 9]], StateFunc (instructionCodeMember, "placeholder")] emptyState + boolTestFunc "instructionCodeMember2Blocks" [False] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 7, GeneInt 0], StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneFloat 3.6, GeneInt 2, GeneVectorInt [8, 9]], StateFunc (instructionCodeMember, "placeholder")] emptyState + codeTestFunc "instructionCodeNInBounds" [GeneInt 0] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 0, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 4, GeneInt 5], GeneInt 0, StateFunc (instructionCodeN, "placeholder")] emptyState + codeTestFunc "instructionCodeNInBoundsEnd" [GeneInt 5] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 0, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 4, GeneInt 5], GeneInt 5, StateFunc (instructionCodeN, "placeholder")] emptyState + codeTestFunc "instructionCodeNModded" [GeneInt 3] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 0, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 4, GeneInt 5], GeneInt 9, StateFunc (instructionCodeN, "placeholder")] emptyState + codeTestFunc "instructionMakeEmptyCodeBlock" [Block []] [StateFunc (instructionMakeEmptyCodeBlock, "placeholder")] emptyState + boolTestFunc "instructionIsEmptyCodeBlockTrue" [True] [StateFunc (instructionCodeFromExec, "placeholder"), Block [], StateFunc (instructionIsEmptyCodeBlock, "placeholder")] emptyState + intTestFunc "instructionCodeSize" [8] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6], StateFunc (instructionCodeSize, "placeholder")] emptyState + codeTestFunc "instructionCodeExtractInBounds" [GeneInt 3] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6], GeneInt 3, StateFunc (instructionCodeExtract, "placeholder")] emptyState + codeTestFunc "instructionCodeExtractOutBounds" [GeneInt 3] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6], GeneInt 11, StateFunc (instructionCodeExtract, "placeholder")] emptyState + codeTestFunc "instructionCodeExtractLastEmptyBlock" [Block []] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], Block []], GeneInt 7, StateFunc (instructionCodeExtract, "placeholder")] emptyState + codeTestFunc "instructionCodeExtractBlock" [Block [GeneInt 2, GeneInt 3]] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], Block []], GeneInt 1, StateFunc (instructionCodeExtract, "placeholder")] emptyState + codeTestFunc "instructionCodeExtractEdgeCase" [Block []] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], Block []], GeneInt 7, StateFunc (instructionCodeExtract, "placeholder")] emptyState + codeTestFunc "instructionCodeExtractNotBlock" [GeneInt 2] [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 2, GeneInt 56, StateFunc (instructionCodeExtract, "placeholder")] emptyState + codeTestFunc + "instructionCodeInsertInBounds" + [Block [GeneInt 1, Block [GeneInt 2, GeneInt 9999, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6, Block [GeneInt 7, GeneInt 8], GeneInt 9]] + [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 9999, StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6, Block [GeneInt 7, GeneInt 8], GeneInt 9], GeneInt 3, StateFunc (instructionCodeInsert, "placeholder")] + emptyState + codeTestFunc + "instructionCodeInsertOutBounds" + [Block [GeneInt 1, Block [GeneInt 2, GeneInt 9999, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6, Block [GeneInt 7, GeneInt 8], GeneInt 9]] + [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 9999, StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6, Block [GeneInt 7, GeneInt 8], GeneInt 9], GeneInt 15, StateFunc (instructionCodeInsert, "placeholder")] + emptyState + codeTestFunc "instructionCodeInsertNotBlock" [Block [GeneInt 2, GeneInt 1]] [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 2, StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 1, GeneInt 1, StateFunc (instructionCodeInsert, "placeholder")] emptyState + intTestFunc "instructionCodePosition0" [0] [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 1, StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 1, GeneInt 2, GeneInt 3], StateFunc (instructionCodeFirstPosition, "placeholder")] emptyState + intTestFunc "instructionCodePosition-1" [-1] [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 7, StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 1, GeneInt 2, GeneInt 3], StateFunc (instructionCodeFirstPosition, "placeholder")] emptyState + intTestFunc "instructionCodePositionEmptyBlock" [0] [StateFunc (instructionCodeFromExec, "placeholder"), Block [], StateFunc (instructionCodeFromExec, "placeholder"), Block [], StateFunc (instructionCodeFirstPosition, "placeholder")] emptyState + codeTestFunc "instructionCodePositionBadStack" [GeneInt 1] [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 1, StateFunc (instructionCodeFirstPosition, "placeholder")] emptyState -- tests to ensure base case of insufficient code stack works. Should do this on more of these instructions. + codeTestFunc "instructionCodeReverse2Args" [Block [GeneInt 2, GeneInt 1]] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 1, GeneInt 2], StateFunc (instructionCodeReverse, "placeholder")] emptyState + codeTestFunc "instructionCodeReverse3Args" [Block [GeneInt 3, GeneInt 2, GeneInt 1]] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 1, GeneInt 2, GeneInt 3], StateFunc (instructionCodeReverse, "placeholder")] emptyState + codeTestFunc "instructionCodeReverseNonBlock" [GeneInt 1] [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 1, StateFunc (instructionCodeReverse, "placeholder")] emptyState + + -- String tests + stringTestFunc "instructionStringConcat" ["123abc"] [GeneString "abc", GeneString "123", StateFunc (instructionStringConcat, "placeholder")] emptyState + stringTestFunc "instructionStringSwap" ["abc", "123"] [GeneString "abc", GeneString "123", StateFunc (instructionStringSwap, "placeholder")] emptyState + stringTestFunc "instructionStringInsertString" ["123INSabc"] [GeneString "abc", GeneString "123", StateFunc (instructionStringConcat, "placeholder"), GeneString "INS", StateFunc (instructionStringSwap, "placeholder"), GeneInt 3, StateFunc (instructionStringInsertString, "placeholder")] emptyState + stringTestFunc "instructionStringFromFirstChar" ["1"] [GeneString "123", StateFunc (instructionStringFromFirstChar, "placeholder")] emptyState + stringTestFunc "instructionStringFromNthChar" ["a"] [GeneString "123abc", GeneInt 3, StateFunc (instructionStringFromNthChar, "placeholder")] emptyState + intTestFunc "instructionStringIndexOfString3" [3] [GeneString "a", GeneString "123abc", StateFunc (instructionStringIndexOfString, "placeholder")] emptyState + intTestFunc "instructionStringIndexOfString-1" [-1] [GeneString "z", GeneString "123abc", StateFunc (instructionStringIndexOfString, "placeholder")] emptyState + boolTestFunc "instructionStringContainsStringTrue" [True] [GeneString "a", GeneString "123abc", StateFunc (instructionStringContainsString, "placeholder")] emptyState + boolTestFunc "instructionStringContainsStringFalse" [False] [GeneString "z", GeneString "123abc", StateFunc (instructionStringContainsString, "placeholder")] emptyState + stringTestFunc "instructionStringSplitOnStringMult" ["nd", "r fri", "llo gam", "h"] [GeneString "e", GeneString "hello gamer friend", StateFunc (instructionStringSplitOnString, "placeholder")] emptyState + stringTestFunc "instructionStringSplitOnStringEmpty" ["", "hello gamer frien"] [GeneString "d", GeneString "hello gamer friend", StateFunc (instructionStringSplitOnString, "placeholder")] emptyState + stringTestFunc "instructionStringReplaceFirstStringSuccess" ["thREPLACEs is a sentence"] [GeneString "REPLACE", GeneString "i", GeneString "this is a sentence", StateFunc (instructionStringReplaceFirstString, "placeholder")] emptyState + stringTestFunc "instructionStringReplaceFirstStringFail" ["this is a sentence"] [GeneString "REPLACE", GeneString "z", GeneString "this is a sentence", StateFunc (instructionStringReplaceFirstString, "placeholder")] emptyState + stringTestFunc "instructionStringReplaceNStringSuccess" ["thREPLACEs REPLACEs a sentence i"] [GeneString "REPLACE", GeneString "i", GeneString "this is a sentence i", GeneInt 2, StateFunc (instructionStringReplaceNString, "placeholder")] emptyState + stringTestFunc "instructionStringReplaceNStringFail" ["this is a sentence i"] [GeneString "REPLACE", GeneString "z", GeneString "this is a sentence i", GeneInt 2, StateFunc (instructionStringReplaceNString, "placeholder")] emptyState + stringTestFunc "instructionStringReplaceAllStringSuccess" ["thREPLACEs REPLACEs a sentence REPLACE"] [GeneString "REPLACE", GeneString "i", GeneString "this is a sentence i", GeneInt 2, StateFunc (instructionStringReplaceAllString, "placeholder")] emptyState + stringTestFunc "instructionStringReplaceAllStringFail" ["this is a sentence i"] [GeneString "REPLACE", GeneString "z", GeneString "this is a sentence i", GeneInt 2, StateFunc (instructionStringReplaceAllString, "placeholder")] emptyState + stringTestFunc "instructionStringRemoveFirstStringSuccess" ["ths is a sentence"] [GeneString "i", GeneString "this is a sentence", StateFunc (instructionStringRemoveFirstString, "placeholder")] emptyState + stringTestFunc "instructionStringRemoveFirstStringFail" ["this is a sentence"] [GeneString "z", GeneString "this is a sentence", StateFunc (instructionStringRemoveFirstString, "placeholder")] emptyState + stringTestFunc "instructionStringRemoveNStringSuccess" ["ths s a sentence i"] [GeneString "i", GeneString "this is a sentence i", GeneInt 2, StateFunc (instructionStringRemoveNString, "placeholder")] emptyState + stringTestFunc "instructionStringRemoveNStringFail" ["this is a sentence i"] [GeneString "z", GeneString "this is a sentence i", GeneInt 2, StateFunc (instructionStringRemoveNString, "placeholder")] emptyState + stringTestFunc "instructionStringRemoveAllStringSuccess" ["ths s a sentence "] [GeneString "i", GeneString "this is a sentence i", StateFunc (instructionStringRemoveAllString, "placeholder")] emptyState + stringTestFunc "instructionStringRemoveAllStringFail" ["this is a sentence i"] [GeneString "z", GeneString "this is a sentence i", StateFunc (instructionStringRemoveAllString, "placeholder")] emptyState + intTestFunc "instructionStringOccurrencesOfString3" [3] [GeneString "i", GeneString "this is a sentence i", StateFunc (instructionStringOccurrencesOfString, "placeholder")] emptyState + intTestFunc "instructionStringOccurrencesOfString3" [2] [GeneString "is", GeneString "this is a sentence i", StateFunc (instructionStringOccurrencesOfString, "placeholder")] emptyState + intTestFunc "instructionStringOccurrencesOfString0" [0] [GeneString "z", GeneString "this is a sentence i", StateFunc (instructionStringOccurrencesOfString, "placeholder")] emptyState + stringTestFunc "instructionStringInsertChar" ["123Zabc"] [GeneString "abc", GeneString "123", StateFunc (instructionStringConcat, "placeholder"), GeneChar 'Z', GeneInt 3, StateFunc (instructionStringInsertChar, "placeholder")] emptyState + boolTestFunc "instructionStringContainsCharTrue" [True] [GeneString "abc", GeneChar 'a', StateFunc (instructionStringContainsChar, "placeholder")] emptyState + boolTestFunc "instructionStringContainsCharFalse" [False] [GeneString "abc", GeneChar 'z', StateFunc (instructionStringContainsChar, "placeholder")] emptyState + intTestFunc "instructionStringIndexOfChar3" [3] [GeneChar 'a', GeneString "123abc", StateFunc (instructionStringIndexOfChar, "placeholder")] emptyState + intTestFunc "instructionStringIndexOfChar-1" [-1] [GeneChar 'z', GeneString "123abc", StateFunc (instructionStringIndexOfChar, "placeholder")] emptyState + stringTestFunc "instructionStringSplitOnCharMult" ["nd", "r fri", "llo gam", "h"] [GeneChar 'e', GeneString "hello gamer friend", StateFunc (instructionStringSplitOnChar, "placeholder")] emptyState + stringTestFunc "instructionStringSplitOnCharEmpty" ["", "hello gamer frien"] [GeneChar 'd', GeneString "hello gamer friend", StateFunc (instructionStringSplitOnChar, "placeholder")] emptyState + stringTestFunc "instructionStringReplaceFirstCharSuccess" ["thRs is a sentence"] [GeneChar 'R', GeneChar 'i', GeneString "this is a sentence", StateFunc (instructionStringReplaceFirstChar, "placeholder")] emptyState + stringTestFunc "instructionStringReplaceFirstCharFail" ["this is a sentence"] [GeneChar 'R', GeneChar 'z', GeneString "this is a sentence", StateFunc (instructionStringReplaceFirstChar, "placeholder")] emptyState + stringTestFunc "instructionStringReplaceNCharSuccess" ["thRs Rs a sentence i"] [GeneChar 'R', GeneChar 'i', GeneString "this is a sentence i", GeneInt 2, StateFunc (instructionStringReplaceNChar, "placeholder")] emptyState + stringTestFunc "instructionStringReplaceNCharFail" ["this is a sentence i"] [GeneChar 'R', GeneChar 'z', GeneString "this is a sentence i", GeneInt 2, StateFunc (instructionStringReplaceNChar, "placeholder")] emptyState + stringTestFunc "instructionStringReplaceAllCharSuccess" ["thRs Rs a sentence R"] [GeneChar 'R', GeneChar 'i', GeneString "this is a sentence i", GeneInt 2, StateFunc (instructionStringReplaceAllChar, "placeholder")] emptyState + stringTestFunc "instructionStringReplaceAllCharFail" ["this is a sentence i"] [GeneChar 'R', GeneChar 'z', GeneString "this is a sentence i", GeneInt 2, StateFunc (instructionStringReplaceAllChar, "placeholder")] emptyState + stringTestFunc "instructionStringRemoveFirstCharSuccess" ["ths is a sentence"] [GeneChar 'i', GeneString "this is a sentence", StateFunc (instructionStringRemoveFirstChar, "placeholder")] emptyState + stringTestFunc "instructionStringRemoveFirstCharFail" ["this is a sentence"] [GeneChar 'z', GeneString "this is a sentence", StateFunc (instructionStringRemoveFirstChar, "placeholder")] emptyState + stringTestFunc "instructionStringRemoveNCharSuccess" ["ths s a sentence i"] [GeneChar 'i', GeneString "this is a sentence i", GeneInt 2, StateFunc (instructionStringRemoveNChar, "placeholder")] emptyState + stringTestFunc "instructionStringRemoveNCharFail" ["this is a sentence i"] [GeneChar 'z', GeneString "this is a sentence i", GeneInt 2, StateFunc (instructionStringRemoveNChar, "placeholder")] emptyState + stringTestFunc "instructionStringRemoveAllCharSuccess" ["ths s a sentence "] [GeneChar 'i', GeneString "this is a sentence i", StateFunc (instructionStringRemoveAllChar, "placeholder")] emptyState + stringTestFunc "instructionStringRemoveAllCharFail" ["this is a sentence i"] [GeneChar 'z', GeneString "this is a sentence i", StateFunc (instructionStringRemoveAllChar, "placeholder")] emptyState + intTestFunc "instructionStringOccurrencesOfChar3" [3] [GeneChar 'i', GeneString "this is a sentence i", StateFunc (instructionStringOccurrencesOfChar, "placeholder")] emptyState + intTestFunc "instructionStringOccurrencesOfChar0" [0] [GeneChar 'z', GeneString "this is a sentence i", StateFunc (instructionStringOccurrencesOfChar, "placeholder")] emptyState + stringTestFunc "instructionStringReverse" ["321cba"] [GeneString "abc123", StateFunc (instructionStringReverse, "placeholder")] emptyState + stringTestFunc "instructionStringHead3" ["abc"] [GeneString "abc123", GeneInt 3, StateFunc (instructionStringHead, "placeholder")] emptyState + stringTestFunc "instructionStringHead0" [""] [GeneString "abc123", GeneInt 0, StateFunc (instructionStringHead, "placeholder")] emptyState + stringTestFunc "instructionStringTail3" ["123"] [GeneString "abc123", GeneInt 3, StateFunc (instructionStringTail, "placeholder")] emptyState + stringTestFunc "instructionStringTail0" [""] [GeneString "abc123", GeneInt 0, StateFunc (instructionStringTail, "placeholder")] emptyState + stringTestFunc "instructionStringAppendChar" ["Rabc123"] [GeneString "abc123", GeneChar 'R', StateFunc (instructionStringAppendChar, "placeholder")] emptyState + stringTestFunc "instructionStringRestFull" ["bc123"] [GeneString "abc123", StateFunc (instructionStringRest, "placeholder")] emptyState + stringTestFunc "instructionStringRestEmpty" [""] [GeneString "", StateFunc (instructionStringRest, "placeholder")] emptyState + stringTestFunc "instructionStringButLastFull" ["abc12"] [GeneString "abc123", StateFunc (instructionStringButLast, "placeholder")] emptyState + stringTestFunc "instructionStringButLastEmpty" [""] [GeneString "", StateFunc (instructionStringButLast, "placeholder")] emptyState + stringTestFunc "instructionStringDrop3" ["123"] [GeneString "abc123", GeneInt 3, StateFunc (instructionStringDrop, "placeholder")] emptyState + stringTestFunc "instructionStringDrop0" ["abc123"] [GeneString "abc123", GeneInt 0, StateFunc (instructionStringDrop, "placeholder")] emptyState + stringTestFunc "instructionStringButLastN3" ["abc"] [GeneString "abc123", GeneInt 3, StateFunc (instructionStringButLastN, "placeholder")] emptyState + stringTestFunc "instructionStringButLastN0" ["abc123"] [GeneString "abc123", GeneInt 0, StateFunc (instructionStringButLastN, "placeholder")] emptyState + intTestFunc "instructionStringLength6" [6] [GeneString "abc123", StateFunc (instructionStringLength, "placeholder")] emptyState + stringTestFunc "instructionStringMakeEmpty" ["", "abc123"] [GeneString "abc123", StateFunc (instructionStringMakeEmpty, "placeholder")] emptyState + stringTestFunc "instructionStringRemoveNth" ["abc23"] [GeneString "abc123", GeneInt 3, StateFunc (instructionStringRemoveNth, "placeholder")] emptyState + stringTestFunc "instructionStringSetNth" ["abR123"] [GeneString "abc123", GeneInt 2, GeneChar 'R', StateFunc (instructionStringSetNth, "placeholder")] emptyState + stringTestFunc "instructionStringStripWhitespace" ["abc123"] [GeneString " \r \n abc123 \t", StateFunc (instructionStringStripWhitespace, "placeholder")] emptyState + stringTestFunc "instructionStringFromBoolTrue" ["True"] [GeneBool True, StateFunc (instructionStringFromBool, "placeholder")] emptyState + stringTestFunc "instructionStringFromBoolTrue" ["False"] [GeneBool False, StateFunc (instructionStringFromBool, "placeholder")] emptyState + stringTestFunc "instructionStringFromInt1000" ["1000"] [GeneInt 1000, StateFunc (instructionStringFromInt, "placeholder")] emptyState + stringTestFunc "instructionStringFromInt-1" ["-1"] [GeneInt (-1), StateFunc (instructionStringFromInt, "placeholder")] emptyState + stringTestFunc "instructionStringFromFloat3.2" ["3.2"] [GeneFloat 3.2, StateFunc (instructionStringFromFloat, "placeholder")] emptyState + stringTestFunc "instructionStringFromFloat-99.0" ["-99.0"] [GeneFloat (-99.0), StateFunc (instructionStringFromFloat, "placeholder")] emptyState + stringTestFunc "instructionStringFromChar" ["Z"] [GeneChar 'Z', StateFunc (instructionStringFromChar, "placeholder")] emptyState + stringTestFunc "instructionStringFromChar" [" "] [GeneChar ' ', StateFunc (instructionStringFromChar, "placeholder")] emptyState + + -- char instructions + stringTestFunc "instructionCharConcat" ["ab"] [GeneChar 'b', GeneChar 'a', StateFunc (instructionCharConcat, "placeholder")] emptyState + charTestFunc "instructionCharFromFirstCharSuccess" ['a'] [GeneString "abc123", StateFunc (instructionCharFromFirstChar, "placeholder")] emptyState + charTestFunc "instructionCharFromFirstCharFail" [] [GeneString "", StateFunc (instructionCharFromFirstChar, "placeholder")] emptyState + charTestFunc "instructionCharFromLastCharSuccess" ['3'] [GeneString "abc123", StateFunc (instructionCharFromLastChar, "placeholder")] emptyState + charTestFunc "instructionCharFromLastCharFail" [] [GeneString "", StateFunc (instructionCharFromLastChar, "placeholder")] emptyState + charTestFunc "instructionCharFromNthCharSuccess" ['c'] [GeneString "abc123", GeneInt 2, StateFunc (instructionCharFromNthChar, "placeholder")] emptyState + boolTestFunc "instructionCharIsWhitespaceSpace" [True] [GeneChar ' ', StateFunc (instructionCharIsWhitespace, "placeholder")] emptyState + boolTestFunc "instructionCharIsWhitespacet" [True] [GeneChar '\t', StateFunc (instructionCharIsWhitespace, "placeholder")] emptyState + boolTestFunc "instructionCharIsWhitespacer" [True] [GeneChar '\r', StateFunc (instructionCharIsWhitespace, "placeholder")] emptyState + boolTestFunc "instructionCharIsWhitespacen" [True] [GeneChar '\n', StateFunc (instructionCharIsWhitespace, "placeholder")] emptyState + boolTestFunc "instructionCharIsWhitespaceFalse" [False] [GeneChar 'a', StateFunc (instructionCharIsWhitespace, "placeholder")] emptyState + boolTestFunc "instructionCharIsLetterTrue" [True] [GeneChar 'a', StateFunc (instructionCharIsLetter, "placeholder")] emptyState + boolTestFunc "instructionCharIsLetterFalse" [False] [GeneChar '1', StateFunc (instructionCharIsLetter, "placeholder")] emptyState + boolTestFunc "instructionCharIsDigitTrue" [True] [GeneChar '1', StateFunc (instructionCharIsDigit, "placeholder")] emptyState + boolTestFunc "instructionCharIsDigitFalse" [False] [GeneChar 'a', StateFunc (instructionCharIsDigit, "placeholder")] emptyState + + -- vector int instructions + vectorIntTestFunc "instructionVectorIntConcat" [[4, 5, 6, 1, 2, 3]] [GeneVectorInt [1, 2, 3], GeneVectorInt [4, 5, 6], StateFunc (instructionVectorIntConcat, "placeholder")] emptyState + vectorIntTestFunc "instructionVectorIntConj" [[99, 1, 2, 3]] [GeneVectorInt [1, 2, 3], GeneInt 99, StateFunc (instructionVectorIntConj, "placeholder")] emptyState + vectorIntTestFunc "instructionIntTakeN" [[1, 2], [6, 7, 8]] [GeneVectorInt [6, 7, 8], GeneVectorInt [1, 2, 3], GeneInt 2, StateFunc (instructionVectorIntTakeN, "placeholder")] emptyState + vectorIntTestFunc "instructionVectorIntSubVector" [[1, 2, 3]] [GeneVectorInt [0, 1, 2, 3, 4, 5], GeneInt 3, GeneInt 1, StateFunc (instructionVectorIntSubVector, "placeholder")] emptyState + intTestFunc "instructionVectorIntFirst" [1] [GeneVectorInt [1, 2, 3, 4, 5], StateFunc (instructionVectorIntFirst, "placeholder")] emptyState + intTestFunc "instructionVectorIntLast" [5] [GeneVectorInt [1, 2, 3, 4, 5], StateFunc (instructionVectorIntLast, "placeholder")] emptyState + intTestFunc "instructionVectorIntNthInBounds" [2] [GeneVectorInt [1, 2, 3, 4, 5], GeneInt 1, StateFunc (instructionVectorIntNth, "placeholder")] emptyState + intTestFunc "instructionVectorIntNthOverflow" [2] [GeneVectorInt [1, 2, 3, 4, 5], GeneInt 6, StateFunc (instructionVectorIntNth, "placeholder")] emptyState + vectorIntTestFunc "instructionVectorIntRestFull" [[2, 3, 4, 5]] [GeneVectorInt [1, 2, 3, 4, 5], StateFunc (instructionVectorIntRest, "placeholder")] emptyState + vectorIntTestFunc "instructionVectorIntRestEmpty" [[]] [GeneVectorInt [], StateFunc (instructionVectorIntRest, "placeholder")] emptyState + vectorIntTestFunc "instructionVectorIntButLastFull" [[1, 2, 3, 4]] [GeneVectorInt [1, 2, 3, 4, 5], StateFunc (instructionVectorIntButLast, "placeholder")] emptyState + vectorIntTestFunc "instructionVectorIntButLastEmpty" [[]] [GeneVectorInt [], StateFunc (instructionVectorIntButLast, "placeholder")] emptyState + intTestFunc "instructionVectorIntLength3" [3] [GeneVectorInt [1, 2, 3], StateFunc (instructionVectorIntLength, "placeholder")] emptyState + intTestFunc "instructionVectorIntLength0" [0] [GeneVectorInt [], StateFunc (instructionVectorIntLength, "placeholder")] emptyState + vectorIntTestFunc "instructionVectorIntReverse" [[4, 3, 2, 1]] [GeneVectorInt [1, 2, 3, 4], StateFunc (instructionVectorIntReverse, "placeholder")] emptyState + intTestFunc "instructionVectorIntPushAllFull" [1, 2, 3, 4, 99] [GeneVectorInt [1, 2, 3, 4], GeneInt 99, StateFunc (instructionVectorIntPushAll, "placeholder")] emptyState + intTestFunc "instructionVectorIntPushAllEmpty" [99] [GeneVectorInt [], GeneInt 99, StateFunc (instructionVectorIntPushAll, "placeholder")] emptyState + vectorIntTestFunc "instructionVectorIntMakeEmpty" [[]] [StateFunc (instructionVectorIntMakeEmpty, "placeholder")] emptyState + boolTestFunc "instructionVectorIntIsEmptyTrue" [True] [GeneVectorInt [], StateFunc (instructionVectorIntIsEmpty, "placeholder")] emptyState + boolTestFunc "instructionVectorIntIsEmptyFalse" [False] [GeneVectorInt [1, 2, 3, 4], StateFunc (instructionVectorIntIsEmpty, "placeholder")] emptyState + intTestFunc "instructionVectorIntIndexOf1" [1] [GeneVectorInt [1, 2, 3, 4, 5], GeneInt 2, StateFunc (instructionVectorIntIndexOf, "placeholder")] emptyState + intTestFunc "instructionVectorIntIndexOfFail" [-1] [GeneVectorInt [], GeneInt 2, StateFunc (instructionVectorIntIndexOf, "placeholder")] emptyState + intTestFunc "instructionVectorIntOccurrencesOf2" [2] [GeneVectorInt [1, 2, 3, 4, 2, 6, 7], GeneInt 2, StateFunc (instructionVectorIntOccurrencesOf, "placeholder")] emptyState + intTestFunc "instructionVectorIntOccurrencesOf0" [0] [GeneVectorInt [1, 2, 3, 4, 2, 6, 7], GeneInt 0, StateFunc (instructionVectorIntOccurrencesOf, "placeholder")] emptyState + vectorIntTestFunc "instructionVectorIntSetNth3" [[0, 1, 2, 99, 4, 5]] [GeneVectorInt [0, 1, 2, 3, 4, 5], GeneInt 99, GeneInt 3, StateFunc (instructionVectorIntSetNth, "placeholder")] emptyState + vectorIntTestFunc "instructionVectorIntSetNth9" [[0, 1, 2, 99, 4, 5]] [GeneVectorInt [0, 1, 2, 3, 4, 5], GeneInt 99, GeneInt 9, StateFunc (instructionVectorIntSetNth, "placeholder")] emptyState + vectorIntTestFunc "instructionVectorIntReplace3" [[0, 1, 2, 99, 4, 5, 99, 5, 99]] [GeneInt 99, GeneInt 3, GeneVectorInt [0, 1, 2, 3, 4, 5, 3, 5, 3], StateFunc (instructionVectorIntReplace, "placeholder")] emptyState + vectorIntTestFunc "instructionVectorIntReplace-1" [[0, 1, 2, 3, 4, 5, 3, 5, 3]] [GeneInt 99, GeneInt (-1), GeneVectorInt [0, 1, 2, 3, 4, 5, 3, 5, 3], StateFunc (instructionVectorIntReplace, "placeholder")] emptyState + vectorIntTestFunc "instructionVectorIntReplaceFirst3" [[0, 1, 2, 99, 4, 5, 3, 5, 3]] [GeneInt 99, GeneInt 3, GeneVectorInt [0, 1, 2, 3, 4, 5, 3, 5, 3], StateFunc (instructionVectorIntReplaceFirst, "placeholder")] emptyState + vectorIntTestFunc "instructionVectorIntReplaceFirst-2" [[0, 1, 2, 3, 4, 5, 3, 5, 3]] [GeneInt 99, GeneInt (-2), GeneVectorInt [0, 1, 2, 3, 4, 5, 3, 5, 3], StateFunc (instructionVectorIntReplaceFirst, "placeholder")] emptyState + vectorIntTestFunc "instructionVectorIntRemove" [[0, 1, 2, 4, 5, 5]] [GeneInt 3, GeneVectorInt [0, 1, 2, 3, 4, 5, 3, 5, 3], StateFunc (instructionVectorIntRemove, "placeholder")] emptyState + intTestFunc "instructionVectorIntIterate" [66] [GeneInt 40, GeneVectorInt [0, 1, 2, 3, 4, 5, 3, 5, 3], StateFunc (instructionVectorIntIterate, "placeholder"), StateFunc (instructionIntAdd, "placeholder")] emptyState + + -- vector float functions + vectorFloatTestFunc "instructionVectorFloatConcat" [[4.0, 5.0, 6.0, 1.0, 2.0, 3.0]] [GeneVectorFloat [1.0, 2.0, 3.0], GeneVectorFloat [4.0, 5.0, 6.0], StateFunc (instructionVectorFloatConcat, "placeholder")] emptyState + vectorFloatTestFunc "instructionVectorFloatConj" [[99.0, 1.0, 2.0, 3.0]] [GeneVectorFloat [1.0, 2.0, 3.0], GeneFloat 99.0, StateFunc (instructionVectorFloatConj, "placeholder")] emptyState + vectorFloatTestFunc "instructionFloatTakeN" [[1.0, 2.0], [6.0, 7.0, 8.0]] [GeneVectorFloat [6.0, 7.0, 8.0], GeneVectorFloat [1.0, 2.0, 3.0], GeneInt 2, StateFunc (instructionVectorFloatTakeN, "placeholder")] emptyState + vectorFloatTestFunc "instructionVectorFloatSubVector" [[1.0, 2.0, 3.0]] [GeneVectorFloat [0.0, 1.0, 2.0, 3.0, 4.0, 5.0], GeneInt 3, GeneInt 1, StateFunc (instructionVectorFloatSubVector, "placeholder")] emptyState + floatTestFunc "instructionVectorFloatFirst" [1.0] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0, 5.0], StateFunc (instructionVectorFloatFirst, "placeholder")] emptyState + floatTestFunc "instructionVectorFloatLast" [5.0] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0, 5.0], StateFunc (instructionVectorFloatLast, "placeholder")] emptyState + floatTestFunc "instructionVectorFloatNthInBounds" [2.0] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0, 5.0], GeneInt 1, StateFunc (instructionVectorFloatNth, "placeholder")] emptyState + floatTestFunc "instructionVectorFloatNthOverflow" [2.0] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0, 5.0], GeneInt 6, StateFunc (instructionVectorFloatNth, "placeholder")] emptyState + vectorFloatTestFunc "instructionVectorFloatRestFull" [[2.0, 3.0, 4.0, 5.0]] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0, 5.0], StateFunc (instructionVectorFloatRest, "placeholder")] emptyState + vectorFloatTestFunc "instructionVectorFloatRestEmpty" [[]] [GeneVectorFloat [], StateFunc (instructionVectorFloatRest, "placeholder")] emptyState + vectorFloatTestFunc "instructionVectorFloatButLastFull" [[1.0, 2.0, 3.0, 4.0]] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0, 5.0], StateFunc (instructionVectorFloatButLast, "placeholder")] emptyState + vectorFloatTestFunc "instructionVectorFloatButLastEmpty" [[]] [GeneVectorFloat [], StateFunc (instructionVectorFloatButLast, "placeholder")] emptyState + intTestFunc "instructionVectorFloatLength3" [3] [GeneVectorFloat [1.0, 2.0, 3.0], StateFunc (instructionVectorFloatLength, "placeholder")] emptyState + intTestFunc "instructionVectorFloatLength0" [0] [GeneVectorFloat [], StateFunc (instructionVectorFloatLength, "placeholder")] emptyState + vectorFloatTestFunc "instructionVectorFloatReverse" [[4.0, 3.0, 2.0, 1.0]] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0], StateFunc (instructionVectorFloatReverse, "placeholder")] emptyState + floatTestFunc "instructionVectorFloatPushAllFull" [1.0, 2.0, 3.0, 4.0, 99.0] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0], GeneFloat 99.0, StateFunc (instructionVectorFloatPushAll, "placeholder")] emptyState + floatTestFunc "instructionVectorFloatPushAllEmpty" [99.0] [GeneVectorFloat [], GeneFloat 99.0, StateFunc (instructionVectorFloatPushAll, "placeholder")] emptyState + vectorFloatTestFunc "instructionVectorFloatMakeEmpty" [[]] [StateFunc (instructionVectorFloatMakeEmpty, "placeholder")] emptyState + boolTestFunc "instructionVectorFloatIsEmptyTrue" [True] [GeneVectorFloat [], StateFunc (instructionVectorFloatIsEmpty, "placeholder")] emptyState + boolTestFunc "instructionVectorFloatIsEmptyFalse" [False] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0], StateFunc (instructionVectorFloatIsEmpty, "placeholder")] emptyState + intTestFunc "instructionVectorFloatIndexOf1" [1] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0, 5.0], GeneFloat 2.0, StateFunc (instructionVectorFloatIndexOf, "placeholder")] emptyState + intTestFunc "instructionVectorFloatIndexOfFail" [-1] [GeneVectorFloat [], GeneFloat 2.0, StateFunc (instructionVectorFloatIndexOf, "placeholder")] emptyState + intTestFunc "instructionVectorFloatOccurrencesOf2" [2] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0, 2.0, 6.0, 7.0], GeneFloat 2.0, StateFunc (instructionVectorFloatOccurrencesOf, "placeholder")] emptyState + intTestFunc "instructionVectorFloatOccurrencesOf0" [0] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0, 2.0, 6.0, 7.0], GeneFloat 0.0, StateFunc (instructionVectorFloatOccurrencesOf, "placeholder")] emptyState + vectorFloatTestFunc "instructionVectorFloatSetNth3" [[0.0, 1.0, 2.0, 99.0, 4.0, 5.0]] [GeneVectorFloat [0.0, 1.0, 2.0, 3.0, 4.0, 5.0], GeneFloat 99.0, GeneInt 3, StateFunc (instructionVectorFloatSetNth, "placeholder")] emptyState + vectorFloatTestFunc "instructionVectorFloatSetNth9" [[0.0, 1.0, 2.0, 99.0, 4.0, 5.0]] [GeneVectorFloat [0.0, 1.0, 2.0, 3.0, 4.0, 5.0], GeneFloat 99.0, GeneInt 9, StateFunc (instructionVectorFloatSetNth, "placeholder")] emptyState + vectorFloatTestFunc "instructionVectorFloatReplace3" [[0.0, 1.0, 2.0, 99.0, 4.0, 5.0, 99.0, 5.0, 99.0]] [GeneFloat 99.0, GeneFloat 3.0, GeneVectorFloat [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 3.0, 5.0, 3.0], StateFunc (instructionVectorFloatReplace, "placeholder")] emptyState + vectorFloatTestFunc "instructionVectorFloatReplace-1" [[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 3.0, 5.0, 3.0]] [GeneFloat 99.0, GeneFloat (-1.0), GeneVectorFloat [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 3.0, 5.0, 3.0], StateFunc (instructionVectorFloatReplace, "placeholder")] emptyState + vectorFloatTestFunc "instructionVectorFloatReplaceFirst3" [[0.0, 1.0, 2.0, 99.0, 4.0, 5.0, 3.0, 5.0, 3.0]] [GeneFloat 99.0, GeneFloat 3.0, GeneVectorFloat [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 3.0, 5.0, 3.0], StateFunc (instructionVectorFloatReplaceFirst, "placeholder")] emptyState + vectorFloatTestFunc "instructionVectorFloatReplaceFirst-2" [[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 3.0, 5.0, 3.0]] [GeneFloat 99.0, GeneFloat (-2.0), GeneVectorFloat [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 3.0, 5.0, 3.0], StateFunc (instructionVectorFloatReplaceFirst, "placeholder")] emptyState + vectorFloatTestFunc "instructionVectorFloatRemove" [[0.0, 1.0, 2.0, 4.0, 5.0, 5.0]] [GeneFloat 3, GeneVectorFloat [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 3.0, 5.0, 3.0], StateFunc (instructionVectorFloatRemove, "placeholder")] emptyState + floatTestFunc "instructionVectorFloatIterate" [66.0] [GeneFloat 40.0, GeneVectorFloat [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 3.0, 5.0, 3.0], StateFunc (instructionVectorFloatIterate, "placeholder"), StateFunc (instructionFloatAdd, "placeholder")] emptyState From 2d70f666e8333e89149d8e681dd5c0d56aeba3d8 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sat, 1 Feb 2025 18:19:07 -0600 Subject: [PATCH 109/171] delete MainOld --- test/MainOld.hs | 324 ------------------------------------------------ 1 file changed, 324 deletions(-) delete mode 100644 test/MainOld.hs diff --git a/test/MainOld.hs b/test/MainOld.hs deleted file mode 100644 index 48bf7a6..0000000 --- a/test/MainOld.hs +++ /dev/null @@ -1,324 +0,0 @@ -import Control.Exception (assert) -import Instructions -import Push -import State - --- import Debug.Trace - --- TODO: Need a function that can compare states. --- May look at quickCheck later - -intTestFunc :: String -> [Int] -> [Gene] -> State -> IO () -intTestFunc name goal genome startState = - let state = loadProgram genome startState - in assert (goal == _int (interpretExec state)) putStrLn (name ++ " passed test.") - -floatTestFunc :: String -> [Float] -> [Gene] -> State -> IO () -floatTestFunc name goal genome startState = - let state = loadProgram genome startState - in assert (goal == _float (interpretExec state)) putStrLn (name ++ " passed test.") - -boolTestFunc :: String -> [Bool] -> [Gene] -> State -> IO () -boolTestFunc name goal genome startState = - let state = loadProgram genome startState - in assert (goal == _bool (interpretExec state)) putStrLn (name ++ " passed test.") - -codeTestFunc :: String -> [Gene] -> [Gene] -> State -> IO () -codeTestFunc name goal genome startState = - let state = loadProgram genome startState - in assert (goal == _code (interpretExec state)) putStrLn (name <> " passed test.") - -stringTestFunc :: String -> [String] -> [Gene] -> State -> IO () -stringTestFunc name goal genome startState = - let state = loadProgram genome startState - in assert (goal == _string (interpretExec state)) putStrLn (name <> " passed test.") - -charTestFunc :: String -> [Char] -> [Gene] -> State -> IO () -charTestFunc name goal genome startState = - let state = loadProgram genome startState - in assert (goal == _char (interpretExec state)) putStrLn (name <> " passed test.") - -vectorIntTestFunc :: String -> [[Int]] -> [Gene] -> State -> IO () -vectorIntTestFunc name goal genome startState = - let state = loadProgram genome startState - in assert (goal == _vectorInt (interpretExec state)) putStrLn (name <> " passed test.") - -vectorFloatTestFunc :: String -> [[Float]] -> [Gene] -> State -> IO () -vectorFloatTestFunc name goal genome startState = - let state = loadProgram genome startState - in assert (goal == _vectorFloat (interpretExec state)) putStrLn (name <> " passed test.") - -main :: IO () -main = do - -- Int tests - intTestFunc "instructionIntAdd" [8] [GeneInt 6, GeneInt 2, StateFunc instructionIntAdd] emptyState - intTestFunc "instructionIntSub" [4] [GeneInt 6, GeneInt 2, StateFunc instructionIntSub] emptyState - intTestFunc "instructionIntMul" [12] [GeneInt 6, GeneInt 2, StateFunc instructionIntMul] emptyState - intTestFunc "instructionIntDiv" [3] [GeneInt 6, GeneInt 2, StateFunc instructionIntDiv] emptyState - intTestFunc "instructionIntDiv0" [0, 2] [GeneInt 2, GeneInt 0, StateFunc instructionIntDiv] emptyState - intTestFunc "instructionIntMod" [3] [GeneInt 13, GeneInt 5, StateFunc instructionIntMod] emptyState - intTestFunc "instructionIntPop" [2] [GeneInt 2, GeneInt 0, StateFunc instructionIntPop] emptyState - intTestFunc "instructionIntDup" [3, 3, 2] [GeneInt 2, GeneInt 3, StateFunc instructionIntDup] emptyState - intTestFunc "instructionIntDupN3" [2, 2, 2] [GeneInt 2, GeneInt 3, StateFunc instructionIntDupN] emptyState - intTestFunc "instructionIntDupN-1" [0] [GeneInt 0, GeneInt 2, GeneInt (-1), StateFunc instructionIntDupN] emptyState - intTestFunc "instructionIntSwap" [2, 0, 3] [GeneInt 3, GeneInt 2, GeneInt 0, StateFunc instructionIntSwap] emptyState - intTestFunc "instructionIntSwapFail" [1] [GeneInt 1, StateFunc instructionIntSwap] emptyState - intTestFunc "instructionIntRot" [1, 3, 2] [GeneInt 1, GeneInt 2, GeneInt 3, StateFunc instructionIntRot] emptyState - intTestFunc "instructionIntRotFail" [7, 8] [GeneInt 8, GeneInt 7, StateFunc instructionIntRot] emptyState - intTestFunc "instructionIntFlush" [] [GeneInt 9696, GeneInt 92, GeneInt 420, StateFunc instructionIntFlush] emptyState -- I think I'm funny - intTestFunc "instructionIntStackDepth" [2, 51, 52] [GeneInt 52, GeneInt 51, StateFunc instructionIntStackDepth] emptyState - intTestFunc "instructionIntYank" [3, 3, 2, 1] [GeneInt 3, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 4, StateFunc instructionIntYank] emptyState - intTestFunc "instructionIntYankDup" [3, 3, 2, 1, 3] [GeneInt 3, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 4, StateFunc instructionIntYankDup] emptyState - intTestFunc "instructionIntShove" [2, 3, 1, 1] [GeneInt 1, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 2, StateFunc instructionIntShove] emptyState - intTestFunc "instructionIntShoveDup" [3, 2, 3, 1, 1] [GeneInt 1, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 2, StateFunc instructionIntShoveDup] emptyState - - -- Exec tests - intTestFunc "instructionExecIf" [6, 5] [GeneBool True, StateFunc instructionExecIf, Block [GeneInt 5, GeneInt 6], Block [GeneInt 7, GeneInt 8]] emptyState - intTestFunc "instructionExecDup" [8] [StateFunc instructionExecDup, GeneInt 4, StateFunc instructionIntAdd] emptyState - intTestFunc "instructionExecDoRange" [12] [GeneInt 2, Block [GeneInt 4, GeneInt 1, StateFunc instructionExecDoRange], StateFunc instructionIntAdd] emptyState - intTestFunc "instructionExecDoCount" [8] [GeneInt 2, Block [GeneInt 4, StateFunc instructionExecDoCount], StateFunc instructionIntAdd] emptyState - intTestFunc "instructionExecDoTimes" [69, 69, 69, 69, 2] [GeneInt 2, Block [GeneInt 4, StateFunc instructionExecDoTimes], GeneInt 69] emptyState - intTestFunc "instructionExecWhile" [70, 70] [GeneBool False, GeneBool True, GeneBool True, StateFunc instructionExecWhile, GeneInt 70] emptyState - intTestFunc "instructionExecDoWhile" [70, 70, 70] [GeneBool False, GeneBool True, GeneBool True, StateFunc instructionExecDoWhile, GeneInt 70] emptyState - intTestFunc "instructionExecWhenTrue" [71] [GeneBool True, StateFunc instructionExecWhen, GeneInt 71] emptyState - - let loadedState = loadProgram [GeneBool False, StateFunc instructionExecWhen, GeneInt 71] emptyState - assert (emptyState == interpretExec loadedState) putStrLn "instructionExecWhenFalse passed test." - - -- Float tests - floatTestFunc "instructionFloatAdd" [4.32] [GeneFloat 4.01, GeneFloat 0.31, StateFunc instructionFloatAdd] emptyState - floatTestFunc "instructionFloatSub" [3.6900003] [GeneFloat 4.01, GeneFloat 0.32, StateFunc instructionFloatSub] emptyState - floatTestFunc "instructionFloatMul" [1.38] [GeneFloat 0.12, GeneFloat 11.5, StateFunc instructionFloatMul] emptyState - floatTestFunc "instructionFloatDiv" [57.5] [GeneFloat 11.5, GeneFloat 0.2, StateFunc instructionFloatDiv] emptyState - floatTestFunc "instructionFloatDiv0" [0, 69.69] [GeneFloat 69.69, GeneFloat 0.0, StateFunc instructionFloatDiv] emptyState - floatTestFunc "instructionFloatYank" [1.1, 4.4, 3.3, 2.2] [GeneInt 3, GeneFloat 1.1, GeneFloat 2.2, GeneFloat 3.3, GeneFloat 4.4, StateFunc instructionFloatYank] emptyState - floatTestFunc "instructionFloatYankDup" [1.1, 4.4, 3.3, 2.2, 1.1] [GeneInt 3, GeneFloat 1.1, GeneFloat 2.2, GeneFloat 3.3, GeneFloat 4.4, StateFunc instructionFloatYankDup] emptyState - floatTestFunc "instructionFloatShove" [3.3, 2.2, 4.4, 1.1] [GeneInt 3, GeneFloat 1.1, GeneFloat 2.2, GeneFloat 3.3, GeneFloat 4.4, StateFunc instructionFloatShove] emptyState - floatTestFunc "instructionFloatShoveDup" [4.4, 3.3, 2.2, 4.4, 1.1] [GeneInt 3, GeneFloat 1.1, GeneFloat 2.2, GeneFloat 3.3, GeneFloat 4.4, StateFunc instructionFloatShoveDup] emptyState - floatTestFunc "instructionFloatDupNonEmpty" [4.4, 4.4, 3.3] [GeneFloat 3.3, GeneFloat 4.4, StateFunc instructionFloatDup] emptyState - floatTestFunc "instructionFloatDupEmpty" [] [StateFunc instructionFloatDup] emptyState - floatTestFunc "instructionFloatDupN3" [4.4, 4.4, 4.4, 3.3] [GeneFloat 3.3, GeneFloat 4.4, GeneInt 3, StateFunc instructionFloatDupN] emptyState - floatTestFunc "instructionFloatDupN-1" [3.3] [GeneFloat 3.3, GeneFloat 4.4, GeneInt (-1), StateFunc instructionFloatDupN] emptyState - boolTestFunc "instructionIntEqTrue" [True] [GeneInt 3, GeneInt 3, StateFunc instructionIntEq] emptyState - boolTestFunc "instructionIntEqFalse" [False] [GeneInt 3, GeneInt 5, StateFunc instructionIntEq] emptyState - boolTestFunc "instructionIntEqFail" [] [GeneInt 3, StateFunc instructionIntEq] emptyState - - -- Code tests - codeTestFunc "instructionCodeFromExec" [] [StateFunc instructionCodeFromExec, StateFunc instructionFloatFromInt, StateFunc instructionCodePop] emptyState - intTestFunc "instructionCodeDoRange" [18] [GeneInt 3, GeneInt 6, StateFunc instructionCodeFromExec, StateFunc instructionIntAdd, StateFunc instructionCodeDoRange] emptyState - -- How to test instructionCodeDoThenPop????? - codeTestFunc "instructionCodeFirst" [GeneInt 5] [StateFunc instructionCodeFromExec, Block [GeneInt 5, StateFunc instructionIntSub], StateFunc instructionCodeFirst] emptyState - codeTestFunc "instructionCodeLast" [GeneBool True] [StateFunc instructionCodeFromExec, Block [GeneInt 5, StateFunc instructionIntSub, GeneBool True], StateFunc instructionCodeLast] emptyState - codeTestFunc "instructionCodeTail" [Block [GeneFloat 3.2, GeneBool True, GeneInt 3]] [StateFunc instructionCodeFromExec, Block [StateFunc instructionFloatAdd, GeneFloat 3.2, GeneBool True, GeneInt 3], StateFunc instructionCodeTail] emptyState - codeTestFunc "instructionCodeInit" [Block [GeneVectorInt [1], GeneFloat 3.2, GeneBool True]] [StateFunc instructionCodeFromExec, Block [GeneVectorInt [1], GeneFloat 3.2, GeneBool True, GeneInt 3], StateFunc instructionCodeInit] emptyState - codeTestFunc "instructionCodeWrap" [Block [GeneInt 3]] [StateFunc instructionCodeFromExec, GeneInt 3, StateFunc instructionCodeWrap] emptyState - codeTestFunc "instructionCodeList" [Block [GeneFloat 5.43, GeneInt 3]] [StateFunc instructionCodeFromExec, GeneInt 3, StateFunc instructionCodeFromExec, GeneFloat 5.43, StateFunc instructionCodeList] emptyState - codeTestFunc "instructionCodeCombine2Blocks" [Block [GeneInt 3, GeneInt 4, GeneInt 1, GeneInt 2]] [StateFunc instructionCodeFromExec, Block [GeneInt 1, GeneInt 2], StateFunc instructionCodeFromExec, Block [GeneInt 3, GeneInt 4], StateFunc instructionCodeCombine] emptyState - codeTestFunc "instructionCodeCombine1Block1Single" [Block [GeneInt 3, GeneInt 4, GeneInt 1]] [StateFunc instructionCodeFromExec, GeneInt 1, StateFunc instructionCodeFromExec, Block [GeneInt 3, GeneInt 4], StateFunc instructionCodeCombine] emptyState - codeTestFunc "instructionCodeCombine1Single1Block" [Block [GeneInt 3, GeneInt 1, GeneInt 2]] [StateFunc instructionCodeFromExec, Block [GeneInt 1, GeneInt 2], StateFunc instructionCodeFromExec, GeneInt 3, StateFunc instructionCodeCombine] emptyState - codeTestFunc "instrucitonCodeCombine2Single" [Block [GeneInt 2, GeneInt 1]] [StateFunc instructionCodeFromExec, GeneInt 1, StateFunc instructionCodeFromExec, GeneInt 2, StateFunc instructionCodeCombine] emptyState - intTestFunc "instructionCodeDo" [3] [StateFunc instructionCodeFromExec, GeneInt 3, StateFunc instructionCodeDo] emptyState - -- How to test instructionCodeDoDup??? We would would need a multi stack testing function - boolTestFunc "instructionCodeIsCodeBlockTrue" [True] [StateFunc instructionCodeFromExec, Block [GeneInt 0], StateFunc instructionCodeIsCodeBlock] emptyState - boolTestFunc "instructionCodeIsCodeBlockFalse" [False] [StateFunc instructionCodeFromExec, GeneInt 0, StateFunc instructionCodeIsCodeBlock] emptyState - boolTestFunc "instructionCodeIsSingularTrue" [True] [StateFunc instructionCodeFromExec, GeneInt 0, StateFunc instructionCodeIsSingular] emptyState - boolTestFunc "instructionCodeIsSingularFalse" [False] [StateFunc instructionCodeFromExec, Block [GeneInt 0], StateFunc instructionCodeIsSingular] emptyState - intTestFunc "instructionCodeDoCount" [15] [GeneInt 6, StateFunc instructionCodeFromExec, StateFunc instructionIntAdd, StateFunc instructionCodeDoCount] emptyState - intTestFunc "instructionCodeDoTimes" [13] [GeneInt 6, GeneInt 3, GeneInt 4, GeneInt 2, StateFunc instructionCodeFromExec, StateFunc instructionIntAdd, StateFunc instructionCodeDoTimes] emptyState - intTestFunc "instructionCodeIfTrue" [6] [GeneBool True, StateFunc instructionCodeFromExec, GeneInt 3, StateFunc instructionCodeFromExec, GeneInt 6, StateFunc instructionCodeIf] emptyState - intTestFunc "instructionCodeIfFalse" [3] [GeneBool False, StateFunc instructionCodeFromExec, GeneInt 3, StateFunc instructionCodeFromExec, GeneInt 6, StateFunc instructionCodeIf] emptyState - intTestFunc "instructionCodeWhen" [6, 3, 6] [GeneInt 6, GeneInt 3, GeneInt 4, GeneInt 2, GeneBool True, StateFunc instructionCodeFromExec, StateFunc instructionIntAdd, StateFunc instructionCodeWhen] emptyState - boolTestFunc "instructionCodeMemberTrue" [True] [StateFunc instructionCodeFromExec, GeneInt 2, StateFunc instructionCodeFromExec, Block [GeneFloat 3.6, GeneInt 2, GeneVectorInt [8, 9]], StateFunc instructionCodeMember] emptyState - boolTestFunc "instructionCodeMemberFalse" [False] [StateFunc instructionCodeFromExec, GeneInt 7, StateFunc instructionCodeFromExec, Block [GeneFloat 3.6, GeneInt 2, GeneVectorInt [8, 9]], StateFunc instructionCodeMember] emptyState - boolTestFunc "instructionCodeMember2Blocks" [False] [StateFunc instructionCodeFromExec, Block [GeneInt 7, GeneInt 0], StateFunc instructionCodeFromExec, Block [GeneFloat 3.6, GeneInt 2, GeneVectorInt [8, 9]], StateFunc instructionCodeMember] emptyState - codeTestFunc "instructionCodeNInBounds" [GeneInt 0] [StateFunc instructionCodeFromExec, Block [GeneInt 0, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 4, GeneInt 5], GeneInt 0, StateFunc instructionCodeN] emptyState - codeTestFunc "instructionCodeNInBoundsEnd" [GeneInt 5] [StateFunc instructionCodeFromExec, Block [GeneInt 0, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 4, GeneInt 5], GeneInt 5, StateFunc instructionCodeN] emptyState - codeTestFunc "instructionCodeNModded" [GeneInt 3] [StateFunc instructionCodeFromExec, Block [GeneInt 0, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 4, GeneInt 5], GeneInt 9, StateFunc instructionCodeN] emptyState - codeTestFunc "instructionMakeEmptyCodeBlock" [Block []] [StateFunc instructionMakeEmptyCodeBlock] emptyState - boolTestFunc "instructionIsEmptyCodeBlockTrue" [True] [StateFunc instructionCodeFromExec, Block [], StateFunc instructionIsEmptyCodeBlock] emptyState - intTestFunc "instructionCodeSize" [8] [StateFunc instructionCodeFromExec, Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6], StateFunc instructionCodeSize] emptyState - codeTestFunc "instructionCodeExtractInBounds" [GeneInt 3] [StateFunc instructionCodeFromExec, Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6], GeneInt 3, StateFunc instructionCodeExtract] emptyState - codeTestFunc "instructionCodeExtractOutBounds" [GeneInt 3] [StateFunc instructionCodeFromExec, Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6], GeneInt 11, StateFunc instructionCodeExtract] emptyState - codeTestFunc "instructionCodeExtractLastEmptyBlock" [Block []] [StateFunc instructionCodeFromExec, Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], Block []], GeneInt 7, StateFunc instructionCodeExtract] emptyState - codeTestFunc "instructionCodeExtractBlock" [Block [GeneInt 2, GeneInt 3]] [StateFunc instructionCodeFromExec, Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], Block []], GeneInt 1, StateFunc instructionCodeExtract] emptyState - codeTestFunc "instructionCodeExtractEdgeCase" [Block []] [StateFunc instructionCodeFromExec, Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], Block []], GeneInt 7, StateFunc instructionCodeExtract] emptyState - codeTestFunc "instructionCodeExtractNotBlock" [GeneInt 2] [StateFunc instructionCodeFromExec, GeneInt 2, GeneInt 56, StateFunc instructionCodeExtract] emptyState - codeTestFunc - "instructionCodeInsertInBounds" - [Block [GeneInt 1, Block [GeneInt 2, GeneInt 9999, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6, Block [GeneInt 7, GeneInt 8], GeneInt 9]] - [StateFunc instructionCodeFromExec, GeneInt 9999, StateFunc instructionCodeFromExec, Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6, Block [GeneInt 7, GeneInt 8], GeneInt 9], GeneInt 3, StateFunc instructionCodeInsert] - emptyState - codeTestFunc - "instructionCodeInsertOutBounds" - [Block [GeneInt 1, Block [GeneInt 2, GeneInt 9999, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6, Block [GeneInt 7, GeneInt 8], GeneInt 9]] - [StateFunc instructionCodeFromExec, GeneInt 9999, StateFunc instructionCodeFromExec, Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6, Block [GeneInt 7, GeneInt 8], GeneInt 9], GeneInt 15, StateFunc instructionCodeInsert] - emptyState - codeTestFunc "instructionCodeInsertNotBlock" [Block [GeneInt 2, GeneInt 1]] [StateFunc instructionCodeFromExec, GeneInt 2, StateFunc instructionCodeFromExec, GeneInt 1, GeneInt 1, StateFunc instructionCodeInsert] emptyState - intTestFunc "instructionCodePosition0" [0] [StateFunc instructionCodeFromExec, GeneInt 1, StateFunc instructionCodeFromExec, Block [GeneInt 1, GeneInt 2, GeneInt 3], StateFunc instructionCodeFirstPosition] emptyState - intTestFunc "instructionCodePosition-1" [-1] [StateFunc instructionCodeFromExec, GeneInt 7, StateFunc instructionCodeFromExec, Block [GeneInt 1, GeneInt 2, GeneInt 3], StateFunc instructionCodeFirstPosition] emptyState - intTestFunc "instructionCodePositionEmptyBlock" [0] [StateFunc instructionCodeFromExec, Block [], StateFunc instructionCodeFromExec, Block [], StateFunc instructionCodeFirstPosition] emptyState - codeTestFunc "instructionCodePositionBadStack" [GeneInt 1] [StateFunc instructionCodeFromExec, GeneInt 1, StateFunc instructionCodeFirstPosition] emptyState -- tests to ensure base case of insufficient code stack works. Should do this on more of these instructions. - codeTestFunc "instructionCodeReverse2Args" [Block [GeneInt 2, GeneInt 1]] [StateFunc instructionCodeFromExec, Block [GeneInt 1, GeneInt 2], StateFunc instructionCodeReverse] emptyState - codeTestFunc "instructionCodeReverse3Args" [Block [GeneInt 3, GeneInt 2, GeneInt 1]] [StateFunc instructionCodeFromExec, Block [GeneInt 1, GeneInt 2, GeneInt 3], StateFunc instructionCodeReverse] emptyState - codeTestFunc "instructionCodeReverseNonBlock" [GeneInt 1] [StateFunc instructionCodeFromExec, GeneInt 1, StateFunc instructionCodeReverse] emptyState - - -- String tests - stringTestFunc "instructionStringConcat" ["123abc"] [GeneString "abc", GeneString "123", StateFunc instructionStringConcat] emptyState - stringTestFunc "instructionStringSwap" ["abc", "123"] [GeneString "abc", GeneString "123", StateFunc instructionStringSwap] emptyState - stringTestFunc "instructionStringInsertString" ["123INSabc"] [GeneString "abc", GeneString "123", StateFunc instructionStringConcat, GeneString "INS", StateFunc instructionStringSwap, GeneInt 3, StateFunc instructionStringInsertString] emptyState - stringTestFunc "instructionStringFromFirstChar" ["1"] [GeneString "123", StateFunc instructionStringFromFirstChar] emptyState - stringTestFunc "instructionStringFromNthChar" ["a"] [GeneString "123abc", GeneInt 3, StateFunc instructionStringFromNthChar] emptyState - intTestFunc "instructionStringIndexOfString3" [3] [GeneString "a", GeneString "123abc", StateFunc instructionStringIndexOfString] emptyState - intTestFunc "instructionStringIndexOfString-1" [-1] [GeneString "z", GeneString "123abc", StateFunc instructionStringIndexOfString] emptyState - boolTestFunc "instructionStringContainsStringTrue" [True] [GeneString "a", GeneString "123abc", StateFunc instructionStringContainsString] emptyState - boolTestFunc "instructionStringContainsStringFalse" [False] [GeneString "z", GeneString "123abc", StateFunc instructionStringContainsString] emptyState - stringTestFunc "instructionStringSplitOnStringMult" ["nd", "r fri", "llo gam", "h"] [GeneString "e", GeneString "hello gamer friend", StateFunc instructionStringSplitOnString] emptyState - stringTestFunc "instructionStringSplitOnStringEmpty" ["", "hello gamer frien"] [GeneString "d", GeneString "hello gamer friend", StateFunc instructionStringSplitOnString] emptyState - stringTestFunc "instructionStringReplaceFirstStringSuccess" ["thREPLACEs is a sentence"] [GeneString "REPLACE", GeneString "i", GeneString "this is a sentence", StateFunc instructionStringReplaceFirstString] emptyState - stringTestFunc "instructionStringReplaceFirstStringFail" ["this is a sentence"] [GeneString "REPLACE", GeneString "z", GeneString "this is a sentence", StateFunc instructionStringReplaceFirstString] emptyState - stringTestFunc "instructionStringReplaceNStringSuccess" ["thREPLACEs REPLACEs a sentence i"] [GeneString "REPLACE", GeneString "i", GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringReplaceNString] emptyState - stringTestFunc "instructionStringReplaceNStringFail" ["this is a sentence i"] [GeneString "REPLACE", GeneString "z", GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringReplaceNString] emptyState - stringTestFunc "instructionStringReplaceAllStringSuccess" ["thREPLACEs REPLACEs a sentence REPLACE"] [GeneString "REPLACE", GeneString "i", GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringReplaceAllString] emptyState - stringTestFunc "instructionStringReplaceAllStringFail" ["this is a sentence i"] [GeneString "REPLACE", GeneString "z", GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringReplaceAllString] emptyState - stringTestFunc "instructionStringRemoveFirstStringSuccess" ["ths is a sentence"] [GeneString "i", GeneString "this is a sentence", StateFunc instructionStringRemoveFirstString] emptyState - stringTestFunc "instructionStringRemoveFirstStringFail" ["this is a sentence"] [GeneString "z", GeneString "this is a sentence", StateFunc instructionStringRemoveFirstString] emptyState - stringTestFunc "instructionStringRemoveNStringSuccess" ["ths s a sentence i"] [GeneString "i", GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringRemoveNString] emptyState - stringTestFunc "instructionStringRemoveNStringFail" ["this is a sentence i"] [GeneString "z", GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringRemoveNString] emptyState - stringTestFunc "instructionStringRemoveAllStringSuccess" ["ths s a sentence "] [GeneString "i", GeneString "this is a sentence i", StateFunc instructionStringRemoveAllString] emptyState - stringTestFunc "instructionStringRemoveAllStringFail" ["this is a sentence i"] [GeneString "z", GeneString "this is a sentence i", StateFunc instructionStringRemoveAllString] emptyState - intTestFunc "instructionStringOccurrencesOfString3" [3] [GeneString "i", GeneString "this is a sentence i", StateFunc instructionStringOccurrencesOfString] emptyState - intTestFunc "instructionStringOccurrencesOfString3" [2] [GeneString "is", GeneString "this is a sentence i", StateFunc instructionStringOccurrencesOfString] emptyState - intTestFunc "instructionStringOccurrencesOfString0" [0] [GeneString "z", GeneString "this is a sentence i", StateFunc instructionStringOccurrencesOfString] emptyState - stringTestFunc "instructionStringInsertChar" ["123Zabc"] [GeneString "abc", GeneString "123", StateFunc instructionStringConcat, GeneChar 'Z', GeneInt 3, StateFunc instructionStringInsertChar] emptyState - boolTestFunc "instructionStringContainsCharTrue" [True] [GeneString "abc", GeneChar 'a', StateFunc instructionStringContainsChar] emptyState - boolTestFunc "instructionStringContainsCharFalse" [False] [GeneString "abc", GeneChar 'z', StateFunc instructionStringContainsChar] emptyState - intTestFunc "instructionStringIndexOfChar3" [3] [GeneChar 'a', GeneString "123abc", StateFunc instructionStringIndexOfChar] emptyState - intTestFunc "instructionStringIndexOfChar-1" [-1] [GeneChar 'z', GeneString "123abc", StateFunc instructionStringIndexOfChar] emptyState - stringTestFunc "instructionStringSplitOnCharMult" ["nd", "r fri", "llo gam", "h"] [GeneChar 'e', GeneString "hello gamer friend", StateFunc instructionStringSplitOnChar] emptyState - stringTestFunc "instructionStringSplitOnCharEmpty" ["", "hello gamer frien"] [GeneChar 'd', GeneString "hello gamer friend", StateFunc instructionStringSplitOnChar] emptyState - stringTestFunc "instructionStringReplaceFirstCharSuccess" ["thRs is a sentence"] [GeneChar 'R', GeneChar 'i', GeneString "this is a sentence", StateFunc instructionStringReplaceFirstChar] emptyState - stringTestFunc "instructionStringReplaceFirstCharFail" ["this is a sentence"] [GeneChar 'R', GeneChar 'z', GeneString "this is a sentence", StateFunc instructionStringReplaceFirstChar] emptyState - stringTestFunc "instructionStringReplaceNCharSuccess" ["thRs Rs a sentence i"] [GeneChar 'R', GeneChar 'i', GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringReplaceNChar] emptyState - stringTestFunc "instructionStringReplaceNCharFail" ["this is a sentence i"] [GeneChar 'R', GeneChar 'z', GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringReplaceNChar] emptyState - stringTestFunc "instructionStringReplaceAllCharSuccess" ["thRs Rs a sentence R"] [GeneChar 'R', GeneChar 'i', GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringReplaceAllChar] emptyState - stringTestFunc "instructionStringReplaceAllCharFail" ["this is a sentence i"] [GeneChar 'R', GeneChar 'z', GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringReplaceAllChar] emptyState - stringTestFunc "instructionStringRemoveFirstCharSuccess" ["ths is a sentence"] [GeneChar 'i', GeneString "this is a sentence", StateFunc instructionStringRemoveFirstChar] emptyState - stringTestFunc "instructionStringRemoveFirstCharFail" ["this is a sentence"] [GeneChar 'z', GeneString "this is a sentence", StateFunc instructionStringRemoveFirstChar] emptyState - stringTestFunc "instructionStringRemoveNCharSuccess" ["ths s a sentence i"] [GeneChar 'i', GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringRemoveNChar] emptyState - stringTestFunc "instructionStringRemoveNCharFail" ["this is a sentence i"] [GeneChar 'z', GeneString "this is a sentence i", GeneInt 2, StateFunc instructionStringRemoveNChar] emptyState - stringTestFunc "instructionStringRemoveAllCharSuccess" ["ths s a sentence "] [GeneChar 'i', GeneString "this is a sentence i", StateFunc instructionStringRemoveAllChar] emptyState - stringTestFunc "instructionStringRemoveAllCharFail" ["this is a sentence i"] [GeneChar 'z', GeneString "this is a sentence i", StateFunc instructionStringRemoveAllChar] emptyState - intTestFunc "instructionStringOccurrencesOfChar3" [3] [GeneChar 'i', GeneString "this is a sentence i", StateFunc instructionStringOccurrencesOfChar] emptyState - intTestFunc "instructionStringOccurrencesOfChar0" [0] [GeneChar 'z', GeneString "this is a sentence i", StateFunc instructionStringOccurrencesOfChar] emptyState - stringTestFunc "instructionStringReverse" ["321cba"] [GeneString "abc123", StateFunc instructionStringReverse] emptyState - stringTestFunc "instructionStringHead3" ["abc"] [GeneString "abc123", GeneInt 3, StateFunc instructionStringHead] emptyState - stringTestFunc "instructionStringHead0" [""] [GeneString "abc123", GeneInt 0, StateFunc instructionStringHead] emptyState - stringTestFunc "instructionStringTail3" ["123"] [GeneString "abc123", GeneInt 3, StateFunc instructionStringTail] emptyState - stringTestFunc "instructionStringTail0" [""] [GeneString "abc123", GeneInt 0, StateFunc instructionStringTail] emptyState - stringTestFunc "instructionStringAppendChar" ["Rabc123"] [GeneString "abc123", GeneChar 'R', StateFunc instructionStringAppendChar] emptyState - stringTestFunc "instructionStringRestFull" ["bc123"] [GeneString "abc123", StateFunc instructionStringRest] emptyState - stringTestFunc "instructionStringRestEmpty" [""] [GeneString "", StateFunc instructionStringRest] emptyState - stringTestFunc "instructionStringButLastFull" ["abc12"] [GeneString "abc123", StateFunc instructionStringButLast] emptyState - stringTestFunc "instructionStringButLastEmpty" [""] [GeneString "", StateFunc instructionStringButLast] emptyState - stringTestFunc "instructionStringDrop3" ["123"] [GeneString "abc123", GeneInt 3, StateFunc instructionStringDrop] emptyState - stringTestFunc "instructionStringDrop0" ["abc123"] [GeneString "abc123", GeneInt 0, StateFunc instructionStringDrop] emptyState - stringTestFunc "instructionStringButLastN3" ["abc"] [GeneString "abc123", GeneInt 3, StateFunc instructionStringButLastN] emptyState - stringTestFunc "instructionStringButLastN0" ["abc123"] [GeneString "abc123", GeneInt 0, StateFunc instructionStringButLastN] emptyState - intTestFunc "instructionStringLength6" [6] [GeneString "abc123", StateFunc instructionStringLength] emptyState - stringTestFunc "instructionStringMakeEmpty" ["", "abc123"] [GeneString "abc123", StateFunc instructionStringMakeEmpty] emptyState - stringTestFunc "instructionStringRemoveNth" ["abc23"] [GeneString "abc123", GeneInt 3, StateFunc instructionStringRemoveNth] emptyState - stringTestFunc "instructionStringSetNth" ["abR123"] [GeneString "abc123", GeneInt 2, GeneChar 'R', StateFunc instructionStringSetNth] emptyState - stringTestFunc "instructionStringStripWhitespace" ["abc123"] [GeneString " \r \n abc123 \t", StateFunc instructionStringStripWhitespace] emptyState - stringTestFunc "instructionStringFromBoolTrue" ["True"] [GeneBool True, StateFunc instructionStringFromBool] emptyState - stringTestFunc "instructionStringFromBoolTrue" ["False"] [GeneBool False, StateFunc instructionStringFromBool] emptyState - stringTestFunc "instructionStringFromInt1000" ["1000"] [GeneInt 1000, StateFunc instructionStringFromInt] emptyState - stringTestFunc "instructionStringFromInt-1" ["-1"] [GeneInt (-1), StateFunc instructionStringFromInt] emptyState - stringTestFunc "instructionStringFromFloat3.2" ["3.2"] [GeneFloat 3.2, StateFunc instructionStringFromFloat] emptyState - stringTestFunc "instructionStringFromFloat-99.0" ["-99.0"] [GeneFloat (-99.0), StateFunc instructionStringFromFloat] emptyState - stringTestFunc "instructionStringFromChar" ["Z"] [GeneChar 'Z', StateFunc instructionStringFromChar] emptyState - stringTestFunc "instructionStringFromChar" [" "] [GeneChar ' ', StateFunc instructionStringFromChar] emptyState - - -- char instructions - stringTestFunc "instructionCharConcat" ["ab"] [GeneChar 'b', GeneChar 'a', StateFunc instructionCharConcat] emptyState - charTestFunc "instructionCharFromFirstCharSuccess" ['a'] [GeneString "abc123", StateFunc instructionCharFromFirstChar] emptyState - charTestFunc "instructionCharFromFirstCharFail" [] [GeneString "", StateFunc instructionCharFromFirstChar] emptyState - charTestFunc "instructionCharFromLastCharSuccess" ['3'] [GeneString "abc123", StateFunc instructionCharFromLastChar] emptyState - charTestFunc "instructionCharFromLastCharFail" [] [GeneString "", StateFunc instructionCharFromLastChar] emptyState - charTestFunc "instructionCharFromNthCharSuccess" ['c'] [GeneString "abc123", GeneInt 2, StateFunc instructionCharFromNthChar] emptyState - boolTestFunc "instructionCharIsWhitespaceSpace" [True] [GeneChar ' ', StateFunc instructionCharIsWhitespace] emptyState - boolTestFunc "instructionCharIsWhitespacet" [True] [GeneChar '\t', StateFunc instructionCharIsWhitespace] emptyState - boolTestFunc "instructionCharIsWhitespacer" [True] [GeneChar '\r', StateFunc instructionCharIsWhitespace] emptyState - boolTestFunc "instructionCharIsWhitespacen" [True] [GeneChar '\n', StateFunc instructionCharIsWhitespace] emptyState - boolTestFunc "instructionCharIsWhitespaceFalse" [False] [GeneChar 'a', StateFunc instructionCharIsWhitespace] emptyState - boolTestFunc "instructionCharIsLetterTrue" [True] [GeneChar 'a', StateFunc instructionCharIsLetter] emptyState - boolTestFunc "instructionCharIsLetterFalse" [False] [GeneChar '1', StateFunc instructionCharIsLetter] emptyState - boolTestFunc "instructionCharIsDigitTrue" [True] [GeneChar '1', StateFunc instructionCharIsDigit] emptyState - boolTestFunc "instructionCharIsDigitFalse" [False] [GeneChar 'a', StateFunc instructionCharIsDigit] emptyState - - -- vector int instructions - vectorIntTestFunc "instructionVectorIntConcat" [[4, 5, 6, 1, 2, 3]] [GeneVectorInt [1, 2, 3], GeneVectorInt [4, 5, 6], StateFunc instructionVectorIntConcat] emptyState - vectorIntTestFunc "instructionVectorIntConj" [[99, 1, 2, 3]] [GeneVectorInt [1, 2, 3], GeneInt 99, StateFunc instructionVectorIntConj] emptyState - vectorIntTestFunc "instructionIntTakeN" [[1, 2], [6, 7, 8]] [GeneVectorInt [6, 7, 8], GeneVectorInt [1, 2, 3], GeneInt 2, StateFunc instructionVectorIntTakeN] emptyState - vectorIntTestFunc "instructionVectorIntSubVector" [[1, 2, 3]] [GeneVectorInt [0, 1, 2, 3, 4, 5], GeneInt 3, GeneInt 1, StateFunc instructionVectorIntSubVector] emptyState - intTestFunc "instructionVectorIntFirst" [1] [GeneVectorInt [1, 2, 3, 4, 5], StateFunc instructionVectorIntFirst] emptyState - intTestFunc "instructionVectorIntLast" [5] [GeneVectorInt [1, 2, 3, 4, 5], StateFunc instructionVectorIntLast] emptyState - intTestFunc "instructionVectorIntNthInBounds" [2] [GeneVectorInt [1, 2, 3, 4, 5], GeneInt 1, StateFunc instructionVectorIntNth] emptyState - intTestFunc "instructionVectorIntNthOverflow" [2] [GeneVectorInt [1, 2, 3, 4, 5], GeneInt 6, StateFunc instructionVectorIntNth] emptyState - vectorIntTestFunc "instructionVectorIntRestFull" [[2, 3, 4, 5]] [GeneVectorInt [1, 2, 3, 4, 5], StateFunc instructionVectorIntRest] emptyState - vectorIntTestFunc "instructionVectorIntRestEmpty" [[]] [GeneVectorInt [], StateFunc instructionVectorIntRest] emptyState - vectorIntTestFunc "instructionVectorIntButLastFull" [[1, 2, 3, 4]] [GeneVectorInt [1, 2, 3, 4, 5], StateFunc instructionVectorIntButLast] emptyState - vectorIntTestFunc "instructionVectorIntButLastEmpty" [[]] [GeneVectorInt [], StateFunc instructionVectorIntButLast] emptyState - intTestFunc "instructionVectorIntLength3" [3] [GeneVectorInt [1, 2, 3], StateFunc instructionVectorIntLength] emptyState - intTestFunc "instructionVectorIntLength0" [0] [GeneVectorInt [], StateFunc instructionVectorIntLength] emptyState - vectorIntTestFunc "instructionVectorIntReverse" [[4, 3, 2, 1]] [GeneVectorInt [1, 2, 3, 4], StateFunc instructionVectorIntReverse] emptyState - intTestFunc "instructionVectorIntPushAllFull" [1, 2, 3, 4, 99] [GeneVectorInt [1, 2, 3, 4], GeneInt 99, StateFunc instructionVectorIntPushAll] emptyState - intTestFunc "instructionVectorIntPushAllEmpty" [99] [GeneVectorInt [], GeneInt 99, StateFunc instructionVectorIntPushAll] emptyState - vectorIntTestFunc "instructionVectorIntMakeEmpty" [[]] [StateFunc instructionVectorIntMakeEmpty] emptyState - boolTestFunc "instructionVectorIntIsEmptyTrue" [True] [GeneVectorInt [], StateFunc instructionVectorIntIsEmpty] emptyState - boolTestFunc "instructionVectorIntIsEmptyFalse" [False] [GeneVectorInt [1, 2, 3, 4], StateFunc instructionVectorIntIsEmpty] emptyState - intTestFunc "instructionVectorIntIndexOf1" [1] [GeneVectorInt [1, 2, 3, 4, 5], GeneInt 2, StateFunc instructionVectorIntIndexOf] emptyState - intTestFunc "instructionVectorIntIndexOfFail" [-1] [GeneVectorInt [], GeneInt 2, StateFunc instructionVectorIntIndexOf] emptyState - intTestFunc "instructionVectorIntOccurrencesOf2" [2] [GeneVectorInt [1, 2, 3, 4, 2, 6, 7], GeneInt 2, StateFunc instructionVectorIntOccurrencesOf] emptyState - intTestFunc "instructionVectorIntOccurrencesOf0" [0] [GeneVectorInt [1, 2, 3, 4, 2, 6, 7], GeneInt 0, StateFunc instructionVectorIntOccurrencesOf] emptyState - vectorIntTestFunc "instructionVectorIntSetNth3" [[0, 1, 2, 99, 4, 5]] [GeneVectorInt [0, 1, 2, 3, 4, 5], GeneInt 99, GeneInt 3, StateFunc instructionVectorIntSetNth] emptyState - vectorIntTestFunc "instructionVectorIntSetNth9" [[0, 1, 2, 99, 4, 5]] [GeneVectorInt [0, 1, 2, 3, 4, 5], GeneInt 99, GeneInt 9, StateFunc instructionVectorIntSetNth] emptyState - vectorIntTestFunc "instructionVectorIntReplace3" [[0, 1, 2, 99, 4, 5, 99, 5, 99]] [GeneInt 99, GeneInt 3, GeneVectorInt [0, 1, 2, 3, 4, 5, 3, 5, 3], StateFunc instructionVectorIntReplace] emptyState - vectorIntTestFunc "instructionVectorIntReplace-1" [[0, 1, 2, 3, 4, 5, 3, 5, 3]] [GeneInt 99, GeneInt (-1), GeneVectorInt [0, 1, 2, 3, 4, 5, 3, 5, 3], StateFunc instructionVectorIntReplace] emptyState - vectorIntTestFunc "instructionVectorIntReplaceFirst3" [[0, 1, 2, 99, 4, 5, 3, 5, 3]] [GeneInt 99, GeneInt 3, GeneVectorInt [0, 1, 2, 3, 4, 5, 3, 5, 3], StateFunc instructionVectorIntReplaceFirst] emptyState - vectorIntTestFunc "instructionVectorIntReplaceFirst-2" [[0, 1, 2, 3, 4, 5, 3, 5, 3]] [GeneInt 99, GeneInt (-2), GeneVectorInt [0, 1, 2, 3, 4, 5, 3, 5, 3], StateFunc instructionVectorIntReplaceFirst] emptyState - vectorIntTestFunc "instructionVectorIntRemove" [[0, 1, 2, 4, 5, 5]] [GeneInt 3, GeneVectorInt [0, 1, 2, 3, 4, 5, 3, 5, 3], StateFunc instructionVectorIntRemove] emptyState - intTestFunc "instructionVectorIntIterate" [66] [GeneInt 40, GeneVectorInt [0, 1, 2, 3, 4, 5, 3, 5, 3], StateFunc instructionVectorIntIterate, StateFunc instructionIntAdd] emptyState - - -- vector float functions - vectorFloatTestFunc "instructionVectorFloatConcat" [[4.0, 5.0, 6.0, 1.0, 2.0, 3.0]] [GeneVectorFloat [1.0, 2.0, 3.0], GeneVectorFloat [4.0, 5.0, 6.0], StateFunc instructionVectorFloatConcat] emptyState - vectorFloatTestFunc "instructionVectorFloatConj" [[99.0, 1.0, 2.0, 3.0]] [GeneVectorFloat [1.0, 2.0, 3.0], GeneFloat 99.0, StateFunc instructionVectorFloatConj] emptyState - vectorFloatTestFunc "instructionFloatTakeN" [[1.0, 2.0], [6.0, 7.0, 8.0]] [GeneVectorFloat [6.0, 7.0, 8.0], GeneVectorFloat [1.0, 2.0, 3.0], GeneInt 2, StateFunc instructionVectorFloatTakeN] emptyState - vectorFloatTestFunc "instructionVectorFloatSubVector" [[1.0, 2.0, 3.0]] [GeneVectorFloat [0.0, 1.0, 2.0, 3.0, 4.0, 5.0], GeneInt 3, GeneInt 1, StateFunc instructionVectorFloatSubVector] emptyState - floatTestFunc "instructionVectorFloatFirst" [1.0] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0, 5.0], StateFunc instructionVectorFloatFirst] emptyState - floatTestFunc "instructionVectorFloatLast" [5.0] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0, 5.0], StateFunc instructionVectorFloatLast] emptyState - floatTestFunc "instructionVectorFloatNthInBounds" [2.0] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0, 5.0], GeneInt 1, StateFunc instructionVectorFloatNth] emptyState - floatTestFunc "instructionVectorFloatNthOverflow" [2.0] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0, 5.0], GeneInt 6, StateFunc instructionVectorFloatNth] emptyState - vectorFloatTestFunc "instructionVectorFloatRestFull" [[2.0, 3.0, 4.0, 5.0]] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0, 5.0], StateFunc instructionVectorFloatRest] emptyState - vectorFloatTestFunc "instructionVectorFloatRestEmpty" [[]] [GeneVectorFloat [], StateFunc instructionVectorFloatRest] emptyState - vectorFloatTestFunc "instructionVectorFloatButLastFull" [[1.0, 2.0, 3.0, 4.0]] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0, 5.0], StateFunc instructionVectorFloatButLast] emptyState - vectorFloatTestFunc "instructionVectorFloatButLastEmpty" [[]] [GeneVectorFloat [], StateFunc instructionVectorFloatButLast] emptyState - intTestFunc "instructionVectorFloatLength3" [3] [GeneVectorFloat [1.0, 2.0, 3.0], StateFunc instructionVectorFloatLength] emptyState - intTestFunc "instructionVectorFloatLength0" [0] [GeneVectorFloat [], StateFunc instructionVectorFloatLength] emptyState - vectorFloatTestFunc "instructionVectorFloatReverse" [[4.0, 3.0, 2.0, 1.0]] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0], StateFunc instructionVectorFloatReverse] emptyState - floatTestFunc "instructionVectorFloatPushAllFull" [1.0, 2.0, 3.0, 4.0, 99.0] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0], GeneFloat 99.0, StateFunc instructionVectorFloatPushAll] emptyState - floatTestFunc "instructionVectorFloatPushAllEmpty" [99.0] [GeneVectorFloat [], GeneFloat 99.0, StateFunc instructionVectorFloatPushAll] emptyState - vectorFloatTestFunc "instructionVectorFloatMakeEmpty" [[]] [StateFunc instructionVectorFloatMakeEmpty] emptyState - boolTestFunc "instructionVectorFloatIsEmptyTrue" [True] [GeneVectorFloat [], StateFunc instructionVectorFloatIsEmpty] emptyState - boolTestFunc "instructionVectorFloatIsEmptyFalse" [False] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0], StateFunc instructionVectorFloatIsEmpty] emptyState - intTestFunc "instructionVectorFloatIndexOf1" [1] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0, 5.0], GeneFloat 2.0, StateFunc instructionVectorFloatIndexOf] emptyState - intTestFunc "instructionVectorFloatIndexOfFail" [-1] [GeneVectorFloat [], GeneFloat 2.0, StateFunc instructionVectorFloatIndexOf] emptyState - intTestFunc "instructionVectorFloatOccurrencesOf2" [2] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0, 2.0, 6.0, 7.0], GeneFloat 2.0, StateFunc instructionVectorFloatOccurrencesOf] emptyState - intTestFunc "instructionVectorFloatOccurrencesOf0" [0] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0, 2.0, 6.0, 7.0], GeneFloat 0.0, StateFunc instructionVectorFloatOccurrencesOf] emptyState - vectorFloatTestFunc "instructionVectorFloatSetNth3" [[0.0, 1.0, 2.0, 99.0, 4.0, 5.0]] [GeneVectorFloat [0.0, 1.0, 2.0, 3.0, 4.0, 5.0], GeneFloat 99.0, GeneInt 3, StateFunc instructionVectorFloatSetNth] emptyState - vectorFloatTestFunc "instructionVectorFloatSetNth9" [[0.0, 1.0, 2.0, 99.0, 4.0, 5.0]] [GeneVectorFloat [0.0, 1.0, 2.0, 3.0, 4.0, 5.0], GeneFloat 99.0, GeneInt 9, StateFunc instructionVectorFloatSetNth] emptyState - vectorFloatTestFunc "instructionVectorFloatReplace3" [[0.0, 1.0, 2.0, 99.0, 4.0, 5.0, 99.0, 5.0, 99.0]] [GeneFloat 99.0, GeneFloat 3.0, GeneVectorFloat [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 3.0, 5.0, 3.0], StateFunc instructionVectorFloatReplace] emptyState - vectorFloatTestFunc "instructionVectorFloatReplace-1" [[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 3.0, 5.0, 3.0]] [GeneFloat 99.0, GeneFloat (-1.0), GeneVectorFloat [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 3.0, 5.0, 3.0], StateFunc instructionVectorFloatReplace] emptyState - vectorFloatTestFunc "instructionVectorFloatReplaceFirst3" [[0.0, 1.0, 2.0, 99.0, 4.0, 5.0, 3.0, 5.0, 3.0]] [GeneFloat 99.0, GeneFloat 3.0, GeneVectorFloat [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 3.0, 5.0, 3.0], StateFunc instructionVectorFloatReplaceFirst] emptyState - vectorFloatTestFunc "instructionVectorFloatReplaceFirst-2" [[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 3.0, 5.0, 3.0]] [GeneFloat 99.0, GeneFloat (-2.0), GeneVectorFloat [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 3.0, 5.0, 3.0], StateFunc instructionVectorFloatReplaceFirst] emptyState - vectorFloatTestFunc "instructionVectorFloatRemove" [[0.0, 1.0, 2.0, 4.0, 5.0, 5.0]] [GeneFloat 3, GeneVectorFloat [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 3.0, 5.0, 3.0], StateFunc instructionVectorFloatRemove] emptyState - floatTestFunc "instructionVectorFloatIterate" [66.0] [GeneFloat 40.0, GeneVectorFloat [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 3.0, 5.0, 3.0], StateFunc instructionVectorFloatIterate, StateFunc instructionFloatAdd] emptyState From 8c95f3ac06d9fedfe3ff5b30cda137f426e8e094 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sat, 1 Feb 2025 20:46:04 -0600 Subject: [PATCH 110/171] remove LearnQuickCheck --- src/LearnQuickCheck.hs | 194 ----------------------------------------- 1 file changed, 194 deletions(-) delete mode 100644 src/LearnQuickCheck.hs diff --git a/src/LearnQuickCheck.hs b/src/LearnQuickCheck.hs deleted file mode 100644 index 44ce1f8..0000000 --- a/src/LearnQuickCheck.hs +++ /dev/null @@ -1,194 +0,0 @@ -module LearnQuickCheck where - --- https://jesper.sikanda.be/posts/quickcheck-intro.html - -import Control.Monad -import Data.List (sort) -import Test.QuickCheck - -qsort :: (Ord a) => [a] -> [a] -qsort = sort - -distance :: Int -> Int -> Int -distance x y = abs (x - y) - -prop_dist35 :: Bool -prop_dist35 = distance 3 5 == 2 - -prop_dist_self :: Int -> Bool -prop_dist_self x = distance x x == 0 - -prop_dist_symmetric :: Int -> Int -> Bool -prop_dist_symmetric x y = distance x y == distance y x - -bad_distance :: Int -> Int -> Int -bad_distance x y = y - x - -prop_dist_symmetric_fail :: Int -> Int -> Bool -prop_dist_symmetric_fail x y = bad_distance x y == bad_distance y x - -sorted :: (Ord a) => [a] -> Bool -sorted (x : y : ys) = x <= y && sorted (y : ys) -sorted _ = True - -prop_sorted :: [Int] -> Bool -prop_sorted xs = sorted xs - --- roundtrip property -insert :: Int -> [Int] -> [Int] -insert x [] = [x] -insert x (y : ys) - | x <= y = x : y : ys - | otherwise = y : insert x ys - -delete :: Int -> [Int] -> [Int] -delete x [] = [] -delete x (y : ys) - | x == y = ys - | otherwise = y : delete x ys - -prop_insert_delete :: [Int] -> Int -> Bool -prop_insert_delete xs x = delete x (insert x xs) == xs - --- Equivalent Property -prop_qsort_sort :: [Int] -> Bool -prop_qsort_sort xs = qsort xs == sort xs - --- can test this in ghci with verboseCheck -prop_qsort_sort' :: (Ord a) => [a] -> Bool -prop_qsort_sort' xs = qsort xs == sort xs - --- Algebraic Laws -vAdd :: (Int, Int) -> (Int, Int) -> (Int, Int) -vAdd tup0 tup1 = (fst tup0 + fst tup1, snd tup0 + snd tup1) - -prop_vAdd_commutative :: (Int, Int) -> (Int, Int) -> Bool -prop_vAdd_commutative v w = vAdd v w == vAdd w v - -prop_vAdd_associative :: (Int, Int) -> (Int, Int) -> (Int, Int) -> Bool -prop_vAdd_associative u v w = vAdd (vAdd u v) w == vAdd u (vAdd v w) - -prop_vAdd_neutral_left :: (Int, Int) -> Bool -prop_vAdd_neutral_left u = vAdd (0, 0) u == u - -prop_vAdd_neutral_right :: (Int, Int) -> Bool -prop_vAdd_neutral_right u = vAdd u (0, 0) == u - -prop_qsort_idempotent :: [Int] -> Bool -prop_qsort_idempotent xs = qsort (qsort xs) == qsort xs - --- Testing with different distributions section --- prop_replicate :: Int -> Int -> Int -> Bool --- prop_replicate n x i = replicate n x !! i == x - -prop_replicate :: Int -> Int -> Int -> Property -prop_replicate n x i = - (i >= 0 && i < n) ==> replicate n (x :: Int) !! i == x - -prop_insert_sorted :: Int -> [Int] -> Property -prop_insert_sorted x xs = sorted xs ==> sorted (insert x xs) - --- Quantified properties -prop_insert_sorted' :: Int -> Property -prop_insert_sorted' x = forAll orderedList (\xs -> sorted (insert x xs)) - --- Testing properties of functions -prop_filter :: Fun Int Bool -> [Int] -> Property -prop_filter p xs = - -- Filter elements not satisfying p. - let ys = [x | x <- xs, applyFun p x] - in -- If any elements are left... - ys /= [] ==> - -- ...generate a random index i... - forAll - (choose (0, length ys - 1)) - -- ...and test if p (ys!!i) holds. - (\i -> applyFun p (ys !! i)) - -prop_bananas :: Fun String Int -> Bool -prop_bananas f = - applyFun f "banana" == applyFun f "monkey" - || applyFun f "banana" == applyFun f "elephant" - || applyFun f "monkey" == applyFun f "elephant" - --- main :: IO () --- main = do --- quickCheck prop_dist35 --- quickCheck prop_dist_self --- quickCheck prop_dist_symmetric --- -- Roundtrip tests --- quickCheck prop_insert_delete --- -- Equivalent tests --- quickCheck prop_qsort_sort --- -- quickCheck prop_qsort_sort' --- -- Algebraic tests --- quickCheck prop_vAdd_commutative --- quickCheck prop_vAdd_associative --- quickCheck prop_vAdd_neutral_left --- quickCheck prop_vAdd_neutral_right --- -- Testing with different distributions --- quickCheck prop_replicate --- quickCheck prop_insert_sorted --- -- Quantified Properties --- quickCheck prop_insert_sorted' --- -- Testing properties of functions --- quickCheck prop_filter --- quickCheck prop_bananas - --- This next section is from the Practical Property Testing video on youtube --- by FP Complete Corporation - -genSuit, genVal :: Gen Char -genSuit = elements "HDCS" -genVal = elements "123456789JQK" - --- Applicative so can do this -genCard :: Gen (Char, Char) -genCard = (,) <$> genSuit <*> genVal - --- Monad so can do this -genCards :: Gen [(Char, Char)] -genCards = do - l <- arbitrary - replicateM l genCard - -genListOf15Ints :: Gen [Int] -genListOf15Ints = resize 15 $ sized $ \n -> replicateM n arbitrary - --- Next section covers this. QuickCheck with custom data declarations --- http://geekyplatypus.com/y-u-have-no-code-samples-quickcheck/ - -data Point = Pt Int Int - -instance Show Point where - show (Pt x y) = "{" ++ show x ++ "," ++ show y ++ "}" - -instance Arbitrary Point where - arbitrary = do - x <- arbitrary - -- y <- arbitrary - -- return $ Pt x y - -- could do - Pt x <$> arbitrary - -data Set a = Set [a] - -instance (Show a) => Show (Set a) where - show s = showSet s - where - showSet (Set []) = "{}" - showSet (Set (x : xs)) = "{" <> show x <> showSubSet xs <> "}" - where - showSubSet [] = "" - showSubSet (ix : ixs) = "," <> show ix <> showSubSet ixs - -instance (Arbitrary a) => Arbitrary (Set a) where - arbitrary = do Set <$> arbitrary - --- list <- arbitrary --- return $ Set list - --- sample $ (arbitrary :: Gen (Set Int)) - --- This link also seems interesting --- https://devtut.github.io/haskell/quickcheck.html From 153e5608016c2ffae27469b65059f0b756307881 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sat, 1 Feb 2025 22:34:31 -0600 Subject: [PATCH 111/171] remove generic reference --- src/State.hs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/State.hs b/src/State.hs index 74d5100..8f9e709 100644 --- a/src/State.hs +++ b/src/State.hs @@ -4,7 +4,6 @@ module State where import Control.Lens hiding (elements) import Data.Map qualified as Map -import GHC.Generics -- The exec stack must store heterogenous types, -- and we must be able to detect that type at runtime. @@ -75,7 +74,7 @@ data State = State _parameter :: [Gene], _input :: Map.Map String Gene } - deriving (Show, Eq, Generic) + deriving (Show, Eq) emptyState :: State emptyState = From 83066eb74ce77ed64bf3cc77ea6e5caafa1c3330 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sun, 2 Feb 2025 22:27:46 -0600 Subject: [PATCH 112/171] start work on using quickcheck to test states --- HushGP.cabal | 38 ++- src/Instructions.hs | 3 + src/Instructions/FloatInstructions.hs | 2 +- src/Instructions/IntInstructions.hs | 2 +- src/Push.hs | 18 ++ src/PushTests.hs | 8 + src/PushTests/GenericTests.hs | 26 ++ src/PushTests/IntTests.hs | 42 ++++ src/State.hs | 51 +++- test/Main.hs | 330 +------------------------- test/MainOld.hs | 324 +++++++++++++++++++++++++ 11 files changed, 520 insertions(+), 324 deletions(-) create mode 100644 src/PushTests.hs create mode 100644 src/PushTests/GenericTests.hs create mode 100644 src/PushTests/IntTests.hs create mode 100644 test/MainOld.hs diff --git a/HushGP.cabal b/HushGP.cabal index d3ff73c..3a391a2 100644 --- a/HushGP.cabal +++ b/HushGP.cabal @@ -52,6 +52,9 @@ library , Instructions.VectorStringInstructions , Instructions.VectorLogicalInstructions , Instructions.VectorCharInstructions + , PushTests + , PushTests.IntTests + , PushTests.GenericTests -- Modules included in this library but not exported. -- other-modules: @@ -61,7 +64,7 @@ library -- Other library packages from which modules are imported. build-depends: - base, containers, lens, split + base, containers, lens, split, QuickCheck -- Directories containing source files. hs-source-dirs: src @@ -101,7 +104,7 @@ test-suite HushGP-test default-language: GHC2021 -- Modules included in this executable, other than Main. - -- other-modules: MainOld.hs + -- other-modules: -- LANGUAGE extensions used by modules in this package. -- other-extensions: @@ -119,5 +122,34 @@ test-suite HushGP-test build-depends: base, HushGP, - lens + lens, + QuickCheck + +test-suite HushGP-test-old + -- Import common warning flags. + import: warnings + + -- Base language which the package is written in. + default-language: GHC2021 + + -- Modules included in this executable, other than Main. + -- other-modules: + + -- LANGUAGE extensions used by modules in this package. + -- other-extensions: + + -- The interface type and version of the test suite. + type: exitcode-stdio-1.0 + + -- Directories containing source files. + hs-source-dirs: test + + -- The entrypoint to the test suite. + main-is: MainOld.hs + + -- Test dependencies. + build-depends: + base, + HushGP, + lens, diff --git a/src/Instructions.hs b/src/Instructions.hs index d93d695..6d54438 100644 --- a/src/Instructions.hs +++ b/src/Instructions.hs @@ -28,3 +28,6 @@ import Instructions.VectorFloatInstructions import Instructions.VectorIntInstructions import Instructions.VectorLogicalInstructions import Instructions.VectorStringInstructions + +-- Will eventually add a list of all instrucitons in this file +-- Use template haskell for this? diff --git a/src/Instructions/FloatInstructions.hs b/src/Instructions/FloatInstructions.hs index 3891ed1..d8d89fa 100644 --- a/src/Instructions/FloatInstructions.hs +++ b/src/Instructions/FloatInstructions.hs @@ -29,7 +29,7 @@ instructionFloatDiv state@(State {_float = (f1 : f2 : fs)}) = state {_float = if instructionFloatDiv state = state instructionFloatMod :: State -> State -instructionFloatMod state@(State {_float = (f1 : f2 : fs)}) = state {_float = f2 `mod'` f1 : fs} +instructionFloatMod state@(State {_float = (f1 : f2 : fs)}) = state {_float = if f1 /= 0 then f2 `mod'` f1 : fs else f1 : f2 : fs} instructionFloatMod state = state instructionFloatMin :: State -> State diff --git a/src/Instructions/IntInstructions.hs b/src/Instructions/IntInstructions.hs index a033a7f..c8a40a0 100644 --- a/src/Instructions/IntInstructions.hs +++ b/src/Instructions/IntInstructions.hs @@ -29,7 +29,7 @@ instructionIntDiv state@(State {_int = (i1 : i2 : is)}) = state {_int = if i1 /= instructionIntDiv state = state instructionIntMod :: State -> State -instructionIntMod state@(State {_int = (i1 : i2 : is)}) = state {_int = i2 `mod` i1 : is} +instructionIntMod state@(State {_int = (i1 : i2 : is)}) = state {_int = if i1 /= 0 then (i2 `mod` i1) : is else i1 : i2 : is} instructionIntMod state = state instructionIntMin :: State -> State diff --git a/src/Push.hs b/src/Push.hs index 86e5326..3e0e1f1 100644 --- a/src/Push.hs +++ b/src/Push.hs @@ -66,4 +66,22 @@ interpretExec state@(State {_exec = e : es}) = Close -> undefined -- This should be removed later. Will be converted to Blocks in the Plushy -> Exec stack process interpretExec state = state +-- interpretOneStep :: State -> State +-- interpretOneStep state@(State {_exec = e : es}) = +-- case e of +-- (GeneInt val) -> state & exec .~ es & int .~ val : view int state +-- (GeneFloat val) -> state & exec .~ es & float .~ val : view float state +-- (GeneBool val) -> state & exec .~ es & bool .~ val : view bool state +-- (GeneString val) -> state & exec .~ es & string .~ val : view string state +-- (GeneChar val) -> state & exec .~ es & char .~ val : view char state +-- (GeneVectorInt val) -> state & exec .~ es & vectorInt .~ val : view vectorInt state +-- (GeneVectorFloat val) -> state & exec .~ es & vectorFloat .~ val : view vectorFloat state +-- (GeneVectorBool val) -> state & exec .~ es & vectorBool .~ val : view vectorBool state +-- (GeneVectorString val) -> state & exec .~ es & vectorString .~ val : view vectorString state +-- (GeneVectorChar val) -> state & exec .~ es & vectorChar .~ val : view vectorChar state +-- (StateFunc (func, _)) -> func state {_exec = es} +-- (Block block) -> (state {_exec = block ++ es}) +-- (PlaceInput val) -> (state {_exec = (view input state Map.! val) : es}) +-- Close -> undefined +-- interpretOneStep state = state -- Need to make interpretExec strict, right? diff --git a/src/PushTests.hs b/src/PushTests.hs new file mode 100644 index 0000000..c0db3b0 --- /dev/null +++ b/src/PushTests.hs @@ -0,0 +1,8 @@ +module PushTests + ( module PushTests.GenericTests + , module PushTests.IntTests + ) +where + +import PushTests.GenericTests +import PushTests.IntTests diff --git a/src/PushTests/GenericTests.hs b/src/PushTests/GenericTests.hs new file mode 100644 index 0000000..a983b94 --- /dev/null +++ b/src/PushTests/GenericTests.hs @@ -0,0 +1,26 @@ +module PushTests.GenericTests where + +import State +import Control.Lens +import Debug.Trace + +arithmeticTest :: (Num a, Eq a) => Lens' State [a] -> (State -> State) -> (a -> a -> a) -> State -> Bool +arithmeticTest accessor instruction func state = + case (uncons (view accessor state), uncons (view accessor $ instruction state)) of + (Just (origx1, origx2 : _), Just (modx1, _)) -> func origx2 origx1 == modx1 && length (view accessor state) == length (view accessor $ instruction state) + 1 + _ -> state == instruction state + +unaryTest :: (Num a, Eq a) => Lens' State [a] -> (State -> State) -> (a -> a) -> State -> Bool +unaryTest accessor instruction func state = + case (uncons (view accessor state), uncons (view accessor $ instruction state)) of + (Just (origx1, _), Just (modx1, _)) -> func origx1 == modx1 && length (view accessor state) == length (view accessor $ instruction state) + _ -> state == instruction state + +typeFromType :: Eq b => Lens' State [a] -> Lens' State [b] -> (State -> State) -> (a -> b) -> State -> Bool +typeFromType accessorFrom accessorTo instruction transformation state = + case (uncons (view accessorTo $ instruction state), uncons (view accessorFrom state)) of + (Just (t1, _), Just (f1, _)) -> + t1 == transformation f1 && + length (view accessorTo $ instruction state) == length (view accessorTo state) + 1 && + length (view accessorFrom $ instruction state) == length (view accessorFrom state) - 1 + _ -> state == instruction state diff --git a/src/PushTests/IntTests.hs b/src/PushTests/IntTests.hs new file mode 100644 index 0000000..0293a5d --- /dev/null +++ b/src/PushTests/IntTests.hs @@ -0,0 +1,42 @@ +module PushTests.IntTests where + +import State +import Instructions.IntInstructions +import PushTests.GenericTests +import Data.List +import Control.Lens hiding (uncons) + +prop_IntAdd :: State -> Bool +prop_IntAdd = arithmeticTest int instructionIntAdd (+) + +prop_IntSub :: State -> Bool +prop_IntSub = arithmeticTest int instructionIntSub (-) + +prop_IntMul :: State -> Bool +prop_IntMul = arithmeticTest int instructionIntMul (*) + +prop_IntDiv :: State -> Bool +prop_IntDiv state@(State {_int = 0 : _}) = state == instructionIntDiv state +prop_IntDiv state = arithmeticTest int instructionIntDiv div state + +prop_IntMod :: State -> Bool +prop_IntMod state@(State {_int = 0 : _}) = state == instructionIntMod state +prop_IntMod state = arithmeticTest int instructionIntMod mod state + +prop_IntFromFloat :: State -> Bool +prop_IntFromFloat = typeFromType float int instructionIntFromFloat floor + +prop_IntFromBool :: State -> Bool +prop_IntFromBool = typeFromType bool int instructionIntFromBool (\x -> if x then 1 else 0) + +prop_IntMin :: State -> Bool +prop_IntMin = arithmeticTest int instructionIntMin min + +prop_IntMax :: State -> Bool +prop_IntMax = arithmeticTest int instructionIntMax max + +prop_IntInc :: State -> Bool +prop_IntInc = unaryTest int instructionIntInc (+1) + +prop_IntDec :: State -> Bool +prop_IntDec = unaryTest int instructionIntDec (\x -> x - 1) diff --git a/src/State.hs b/src/State.hs index 8f9e709..df6737a 100644 --- a/src/State.hs +++ b/src/State.hs @@ -1,9 +1,11 @@ -{-# LANGUAGE TemplateHaskell #-} +{-# LANGUAGE TemplateHaskell, DeriveGeneric #-} module State where import Control.Lens hiding (elements) import Data.Map qualified as Map +import Test.QuickCheck +import GHC.Generics -- The exec stack must store heterogenous types, -- and we must be able to detect that type at runtime. @@ -24,6 +26,7 @@ data Gene | PlaceInput String | Close | Block [Gene] + deriving Generic instance Eq Gene where GeneInt x == GeneInt y = x == y @@ -58,6 +61,29 @@ instance Show Gene where show Close = "Close" show (Block xs) = "Block: " <> show xs + +instance CoArbitrary Gene + +instance Arbitrary Gene where + arbitrary = + oneof + [ GeneInt <$> arbitrary, + GeneFloat <$> arbitrary, + GeneBool <$> arbitrary, + GeneString <$> arbitrary, + GeneChar <$> arbitrary, + StateFunc <$> arbitrary, + PlaceInput <$> arbitrary, + GeneVectorInt <$> arbitrary, + GeneVectorFloat <$> arbitrary, + GeneVectorBool <$> arbitrary, + GeneVectorString <$> arbitrary, + GeneVectorChar <$> arbitrary, + Block <$> arbitrary, + return Close + ] + + data State = State { _exec :: [Gene], _code :: [Gene], @@ -74,7 +100,28 @@ data State = State _parameter :: [Gene], _input :: Map.Map String Gene } - deriving (Show, Eq) + deriving (Show, Eq, Generic) + +instance Arbitrary State where + arbitrary = do + arbExec <- arbitrary + arbCode <- arbitrary + arbInt <- arbitrary + arbFloat <- arbitrary + arbBool <- arbitrary + arbString <- arbitrary + arbChar <- arbitrary + arbVectorInt <- arbitrary + arbVectorFloat <- arbitrary + arbVectorBool <- arbitrary + arbVectorString <- arbitrary + arbVectorChar <- arbitrary + arbParameter <- arbitrary + -- arbInput <- arbitrary + State arbExec arbCode arbInt arbFloat arbBool arbString arbChar arbVectorInt arbVectorFloat arbVectorBool arbVectorString arbVectorChar arbParameter <$> arbitrary +-- Thanks hlint lol + +instance CoArbitrary State emptyState :: State emptyState = diff --git a/test/Main.hs b/test/Main.hs index c3728e8..cadba10 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -1,324 +1,20 @@ -import Control.Exception (assert) import Instructions import Push import State +import Test.QuickCheck +import PushTests +-- import Data.List +-- import Control.Lens -- import Debug.Trace --- TODO: Need a function that can compare states. --- May look at quickCheck later +pushTestArgs :: Args +pushTestArgs = stdArgs{maxSize = 10} + +-- These two used for ghci testing +-- For example (in ghci): qcw prop_myTest +qcw :: Testable a => a -> IO () +qcw = quickCheckWith pushTestArgs -intTestFunc :: String -> [Int] -> [Gene] -> State -> IO () -intTestFunc name goal genome startState = - let state = loadProgram genome startState - in assert (goal == _int (interpretExec state)) putStrLn (name ++ " passed test.") - -floatTestFunc :: String -> [Float] -> [Gene] -> State -> IO () -floatTestFunc name goal genome startState = - let state = loadProgram genome startState - in assert (goal == _float (interpretExec state)) putStrLn (name ++ " passed test.") - -boolTestFunc :: String -> [Bool] -> [Gene] -> State -> IO () -boolTestFunc name goal genome startState = - let state = loadProgram genome startState - in assert (goal == _bool (interpretExec state)) putStrLn (name ++ " passed test.") - -codeTestFunc :: String -> [Gene] -> [Gene] -> State -> IO () -codeTestFunc name goal genome startState = - let state = loadProgram genome startState - in assert (goal == _code (interpretExec state)) putStrLn (name <> " passed test.") - -stringTestFunc :: String -> [String] -> [Gene] -> State -> IO () -stringTestFunc name goal genome startState = - let state = loadProgram genome startState - in assert (goal == _string (interpretExec state)) putStrLn (name <> " passed test.") - -charTestFunc :: String -> [Char] -> [Gene] -> State -> IO () -charTestFunc name goal genome startState = - let state = loadProgram genome startState - in assert (goal == _char (interpretExec state)) putStrLn (name <> " passed test.") - -vectorIntTestFunc :: String -> [[Int]] -> [Gene] -> State -> IO () -vectorIntTestFunc name goal genome startState = - let state = loadProgram genome startState - in assert (goal == _vectorInt (interpretExec state)) putStrLn (name <> " passed test.") - -vectorFloatTestFunc :: String -> [[Float]] -> [Gene] -> State -> IO () -vectorFloatTestFunc name goal genome startState = - let state = loadProgram genome startState - in assert (goal == _vectorFloat (interpretExec state)) putStrLn (name <> " passed test.") - -main :: IO () -main = do - -- Int tests - intTestFunc "instructionIntAdd" [8] [GeneInt 6, GeneInt 2, StateFunc (instructionIntAdd, "placeholder")] emptyState - intTestFunc "instructionIntSub" [4] [GeneInt 6, GeneInt 2, StateFunc (instructionIntSub, "placeholder")] emptyState - intTestFunc "instructionIntMul" [12] [GeneInt 6, GeneInt 2, StateFunc (instructionIntMul, "placeholder")] emptyState - intTestFunc "instructionIntDiv" [3] [GeneInt 6, GeneInt 2, StateFunc (instructionIntDiv, "placeholder")] emptyState - intTestFunc "instructionIntDiv0" [0, 2] [GeneInt 2, GeneInt 0, StateFunc (instructionIntDiv, "placeholder")] emptyState - intTestFunc "instructionIntMod" [3] [GeneInt 13, GeneInt 5, StateFunc (instructionIntMod, "placeholder")] emptyState - intTestFunc "instructionIntPop" [2] [GeneInt 2, GeneInt 0, StateFunc (instructionIntPop, "placeholder")] emptyState - intTestFunc "instructionIntDup" [3, 3, 2] [GeneInt 2, GeneInt 3, StateFunc (instructionIntDup, "placeholder")] emptyState - intTestFunc "instructionIntDupN3" [2, 2, 2] [GeneInt 2, GeneInt 3, StateFunc (instructionIntDupN, "placeholder")] emptyState - intTestFunc "instructionIntDupN-1" [0] [GeneInt 0, GeneInt 2, GeneInt (-1), StateFunc (instructionIntDupN, "placeholder")] emptyState - intTestFunc "instructionIntSwap" [2, 0, 3] [GeneInt 3, GeneInt 2, GeneInt 0, StateFunc (instructionIntSwap, "placeholder")] emptyState - intTestFunc "instructionIntSwapFail" [1] [GeneInt 1, StateFunc (instructionIntSwap, "placeholder")] emptyState - intTestFunc "instructionIntRot" [1, 3, 2] [GeneInt 1, GeneInt 2, GeneInt 3, StateFunc (instructionIntRot, "placeholder")] emptyState - intTestFunc "instructionIntRotFail" [7, 8] [GeneInt 8, GeneInt 7, StateFunc (instructionIntRot, "placeholder")] emptyState - intTestFunc "instructionIntFlush" [] [GeneInt 9696, GeneInt 92, GeneInt 420, StateFunc (instructionIntFlush, "placeholder")] emptyState -- I think I'm funny - intTestFunc "instructionIntStackDepth" [2, 51, 52] [GeneInt 52, GeneInt 51, StateFunc (instructionIntStackDepth, "placeholder")] emptyState - intTestFunc "instructionIntYank" [3, 3, 2, 1] [GeneInt 3, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 4, StateFunc (instructionIntYank, "placeholder")] emptyState - intTestFunc "instructionIntYankDup" [3, 3, 2, 1, 3] [GeneInt 3, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 4, StateFunc (instructionIntYankDup, "placeholder")] emptyState - intTestFunc "instructionIntShove" [2, 3, 1, 1] [GeneInt 1, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 2, StateFunc (instructionIntShove, "placeholder")] emptyState - intTestFunc "instructionIntShoveDup" [3, 2, 3, 1, 1] [GeneInt 1, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 2, StateFunc (instructionIntShoveDup, "placeholder")] emptyState - - -- Exec tests - intTestFunc "instructionExecIf" [6, 5] [GeneBool True, StateFunc (instructionExecIf, "placeholder"), Block [GeneInt 5, GeneInt 6], Block [GeneInt 7, GeneInt 8]] emptyState - intTestFunc "instructionExecDup" [8] [StateFunc (instructionExecDup, "placeholder"), GeneInt 4, StateFunc (instructionIntAdd, "placeholder")] emptyState - intTestFunc "instructionExecDoRange" [12] [GeneInt 2, Block [GeneInt 4, GeneInt 1, StateFunc (instructionExecDoRange, "placeholder")], StateFunc (instructionIntAdd, "placeholder")] emptyState - intTestFunc "instructionExecDoCount" [8] [GeneInt 2, Block [GeneInt 4, StateFunc (instructionExecDoCount, "placeholder")], StateFunc (instructionIntAdd, "placeholder")] emptyState - intTestFunc "instructionExecDoTimes" [69, 69, 69, 69, 2] [GeneInt 2, Block [GeneInt 4, StateFunc (instructionExecDoTimes, "placeholder")], GeneInt 69] emptyState - intTestFunc "instructionExecWhile" [70, 70] [GeneBool False, GeneBool True, GeneBool True, StateFunc (instructionExecWhile, "placeholder"), GeneInt 70] emptyState - intTestFunc "instructionExecDoWhile" [70, 70, 70] [GeneBool False, GeneBool True, GeneBool True, StateFunc (instructionExecDoWhile, "placeholder"), GeneInt 70] emptyState - intTestFunc "instructionExecWhenTrue" [71] [GeneBool True, StateFunc (instructionExecWhen, "placeholder"), GeneInt 71] emptyState - - let loadedState = loadProgram [GeneBool False, StateFunc (instructionExecWhen, "placeholder"), GeneInt 71] emptyState - assert (emptyState == interpretExec loadedState) putStrLn "instructionExecWhenFalse passed test." - - -- Float tests - floatTestFunc "instructionFloatAdd" [4.32] [GeneFloat 4.01, GeneFloat 0.31, StateFunc (instructionFloatAdd, "placeholder")] emptyState - floatTestFunc "instructionFloatSub" [3.6900003] [GeneFloat 4.01, GeneFloat 0.32, StateFunc (instructionFloatSub, "placeholder")] emptyState - floatTestFunc "instructionFloatMul" [1.38] [GeneFloat 0.12, GeneFloat 11.5, StateFunc (instructionFloatMul, "placeholder")] emptyState - floatTestFunc "instructionFloatDiv" [57.5] [GeneFloat 11.5, GeneFloat 0.2, StateFunc (instructionFloatDiv, "placeholder")] emptyState - floatTestFunc "instructionFloatDiv0" [0, 69.69] [GeneFloat 69.69, GeneFloat 0.0, StateFunc (instructionFloatDiv, "placeholder")] emptyState - floatTestFunc "instructionFloatYank" [1.1, 4.4, 3.3, 2.2] [GeneInt 3, GeneFloat 1.1, GeneFloat 2.2, GeneFloat 3.3, GeneFloat 4.4, StateFunc (instructionFloatYank, "placeholder")] emptyState - floatTestFunc "instructionFloatYankDup" [1.1, 4.4, 3.3, 2.2, 1.1] [GeneInt 3, GeneFloat 1.1, GeneFloat 2.2, GeneFloat 3.3, GeneFloat 4.4, StateFunc (instructionFloatYankDup, "placeholder")] emptyState - floatTestFunc "instructionFloatShove" [3.3, 2.2, 4.4, 1.1] [GeneInt 3, GeneFloat 1.1, GeneFloat 2.2, GeneFloat 3.3, GeneFloat 4.4, StateFunc (instructionFloatShove, "placeholder")] emptyState - floatTestFunc "instructionFloatShoveDup" [4.4, 3.3, 2.2, 4.4, 1.1] [GeneInt 3, GeneFloat 1.1, GeneFloat 2.2, GeneFloat 3.3, GeneFloat 4.4, StateFunc (instructionFloatShoveDup, "placeholder")] emptyState - floatTestFunc "instructionFloatDupNonEmpty" [4.4, 4.4, 3.3] [GeneFloat 3.3, GeneFloat 4.4, StateFunc (instructionFloatDup, "placeholder")] emptyState - floatTestFunc "instructionFloatDupEmpty" [] [StateFunc (instructionFloatDup, "placeholder")] emptyState - floatTestFunc "instructionFloatDupN3" [4.4, 4.4, 4.4, 3.3] [GeneFloat 3.3, GeneFloat 4.4, GeneInt 3, StateFunc (instructionFloatDupN, "placeholder")] emptyState - floatTestFunc "instructionFloatDupN-1" [3.3] [GeneFloat 3.3, GeneFloat 4.4, GeneInt (-1), StateFunc (instructionFloatDupN, "placeholder")] emptyState - boolTestFunc "instructionIntEqTrue" [True] [GeneInt 3, GeneInt 3, StateFunc (instructionIntEq, "placeholder")] emptyState - boolTestFunc "instructionIntEqFalse" [False] [GeneInt 3, GeneInt 5, StateFunc (instructionIntEq, "placeholder")] emptyState - boolTestFunc "instructionIntEqFail" [] [GeneInt 3, StateFunc (instructionIntEq, "placeholder")] emptyState - - -- Code tests - codeTestFunc "instructionCodeFromExec" [] [StateFunc (instructionCodeFromExec, "placeholder"), StateFunc (instructionFloatFromInt, "placeholder"), StateFunc (instructionCodePop, "placeholder")] emptyState - intTestFunc "instructionCodeDoRange" [18] [GeneInt 3, GeneInt 6, StateFunc (instructionCodeFromExec, "placeholder"), StateFunc (instructionIntAdd, "placeholder"), StateFunc (instructionCodeDoRange, "placeholder")] emptyState - -- How to test instructionCodeDoThenPop????? - codeTestFunc "instructionCodeFirst" [GeneInt 5] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 5, StateFunc (instructionIntSub, "placeholder")], StateFunc (instructionCodeFirst, "placeholder")] emptyState - codeTestFunc "instructionCodeLast" [GeneBool True] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 5, StateFunc (instructionIntSub, "placeholder"), GeneBool True], StateFunc (instructionCodeLast, "placeholder")] emptyState - codeTestFunc "instructionCodeTail" [Block [GeneFloat 3.2, GeneBool True, GeneInt 3]] [StateFunc (instructionCodeFromExec, "placeholder"), Block [StateFunc (instructionFloatAdd, "placeholder"), GeneFloat 3.2, GeneBool True, GeneInt 3], StateFunc (instructionCodeTail, "placeholder")] emptyState - codeTestFunc "instructionCodeInit" [Block [GeneVectorInt [1], GeneFloat 3.2, GeneBool True]] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneVectorInt [1], GeneFloat 3.2, GeneBool True, GeneInt 3], StateFunc (instructionCodeInit, "placeholder")] emptyState - codeTestFunc "instructionCodeWrap" [Block [GeneInt 3]] [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 3, StateFunc (instructionCodeWrap, "placeholder")] emptyState - codeTestFunc "instructionCodeList" [Block [GeneFloat 5.43, GeneInt 3]] [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 3, StateFunc (instructionCodeFromExec, "placeholder"), GeneFloat 5.43, StateFunc (instructionCodeList, "placeholder")] emptyState - codeTestFunc "instructionCodeCombine2Blocks" [Block [GeneInt 3, GeneInt 4, GeneInt 1, GeneInt 2]] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 1, GeneInt 2], StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 3, GeneInt 4], StateFunc (instructionCodeCombine, "placeholder")] emptyState - codeTestFunc "instructionCodeCombine1Block1Single" [Block [GeneInt 3, GeneInt 4, GeneInt 1]] [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 1, StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 3, GeneInt 4], StateFunc (instructionCodeCombine, "placeholder")] emptyState - codeTestFunc "instructionCodeCombine1Single1Block" [Block [GeneInt 3, GeneInt 1, GeneInt 2]] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 1, GeneInt 2], StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 3, StateFunc (instructionCodeCombine, "placeholder")] emptyState - codeTestFunc "instrucitonCodeCombine2Single" [Block [GeneInt 2, GeneInt 1]] [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 1, StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 2, StateFunc (instructionCodeCombine, "placeholder")] emptyState - intTestFunc "instructionCodeDo" [3] [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 3, StateFunc (instructionCodeDo, "placeholder")] emptyState - -- How to test instructionCodeDoDup??? We would would need a multi stack testing function - boolTestFunc "instructionCodeIsCodeBlockTrue" [True] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 0], StateFunc (instructionCodeIsCodeBlock, "placeholder")] emptyState - boolTestFunc "instructionCodeIsCodeBlockFalse" [False] [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 0, StateFunc (instructionCodeIsCodeBlock, "placeholder")] emptyState - boolTestFunc "instructionCodeIsSingularTrue" [True] [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 0, StateFunc (instructionCodeIsSingular, "placeholder")] emptyState - boolTestFunc "instructionCodeIsSingularFalse" [False] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 0], StateFunc (instructionCodeIsSingular, "placeholder")] emptyState - intTestFunc "instructionCodeDoCount" [15] [GeneInt 6, StateFunc (instructionCodeFromExec, "placeholder"), StateFunc (instructionIntAdd, "placeholder"), StateFunc (instructionCodeDoCount, "placeholder")] emptyState - intTestFunc "instructionCodeDoTimes" [13] [GeneInt 6, GeneInt 3, GeneInt 4, GeneInt 2, StateFunc (instructionCodeFromExec, "placeholder"), StateFunc (instructionIntAdd, "placeholder"), StateFunc (instructionCodeDoTimes, "placeholder")] emptyState - intTestFunc "instructionCodeIfTrue" [6] [GeneBool True, StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 3, StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 6, StateFunc (instructionCodeIf, "placeholder")] emptyState - intTestFunc "instructionCodeIfFalse" [3] [GeneBool False, StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 3, StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 6, StateFunc (instructionCodeIf, "placeholder")] emptyState - intTestFunc "instructionCodeWhen" [6, 3, 6] [GeneInt 6, GeneInt 3, GeneInt 4, GeneInt 2, GeneBool True, StateFunc (instructionCodeFromExec, "placeholder"), StateFunc (instructionIntAdd, "placeholder"), StateFunc (instructionCodeWhen, "placeholder")] emptyState - boolTestFunc "instructionCodeMemberTrue" [True] [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 2, StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneFloat 3.6, GeneInt 2, GeneVectorInt [8, 9]], StateFunc (instructionCodeMember, "placeholder")] emptyState - boolTestFunc "instructionCodeMemberFalse" [False] [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 7, StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneFloat 3.6, GeneInt 2, GeneVectorInt [8, 9]], StateFunc (instructionCodeMember, "placeholder")] emptyState - boolTestFunc "instructionCodeMember2Blocks" [False] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 7, GeneInt 0], StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneFloat 3.6, GeneInt 2, GeneVectorInt [8, 9]], StateFunc (instructionCodeMember, "placeholder")] emptyState - codeTestFunc "instructionCodeNInBounds" [GeneInt 0] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 0, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 4, GeneInt 5], GeneInt 0, StateFunc (instructionCodeN, "placeholder")] emptyState - codeTestFunc "instructionCodeNInBoundsEnd" [GeneInt 5] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 0, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 4, GeneInt 5], GeneInt 5, StateFunc (instructionCodeN, "placeholder")] emptyState - codeTestFunc "instructionCodeNModded" [GeneInt 3] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 0, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 4, GeneInt 5], GeneInt 9, StateFunc (instructionCodeN, "placeholder")] emptyState - codeTestFunc "instructionMakeEmptyCodeBlock" [Block []] [StateFunc (instructionMakeEmptyCodeBlock, "placeholder")] emptyState - boolTestFunc "instructionIsEmptyCodeBlockTrue" [True] [StateFunc (instructionCodeFromExec, "placeholder"), Block [], StateFunc (instructionIsEmptyCodeBlock, "placeholder")] emptyState - intTestFunc "instructionCodeSize" [8] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6], StateFunc (instructionCodeSize, "placeholder")] emptyState - codeTestFunc "instructionCodeExtractInBounds" [GeneInt 3] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6], GeneInt 3, StateFunc (instructionCodeExtract, "placeholder")] emptyState - codeTestFunc "instructionCodeExtractOutBounds" [GeneInt 3] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6], GeneInt 11, StateFunc (instructionCodeExtract, "placeholder")] emptyState - codeTestFunc "instructionCodeExtractLastEmptyBlock" [Block []] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], Block []], GeneInt 7, StateFunc (instructionCodeExtract, "placeholder")] emptyState - codeTestFunc "instructionCodeExtractBlock" [Block [GeneInt 2, GeneInt 3]] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], Block []], GeneInt 1, StateFunc (instructionCodeExtract, "placeholder")] emptyState - codeTestFunc "instructionCodeExtractEdgeCase" [Block []] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], Block []], GeneInt 7, StateFunc (instructionCodeExtract, "placeholder")] emptyState - codeTestFunc "instructionCodeExtractNotBlock" [GeneInt 2] [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 2, GeneInt 56, StateFunc (instructionCodeExtract, "placeholder")] emptyState - codeTestFunc - "instructionCodeInsertInBounds" - [Block [GeneInt 1, Block [GeneInt 2, GeneInt 9999, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6, Block [GeneInt 7, GeneInt 8], GeneInt 9]] - [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 9999, StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6, Block [GeneInt 7, GeneInt 8], GeneInt 9], GeneInt 3, StateFunc (instructionCodeInsert, "placeholder")] - emptyState - codeTestFunc - "instructionCodeInsertOutBounds" - [Block [GeneInt 1, Block [GeneInt 2, GeneInt 9999, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6, Block [GeneInt 7, GeneInt 8], GeneInt 9]] - [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 9999, StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6, Block [GeneInt 7, GeneInt 8], GeneInt 9], GeneInt 15, StateFunc (instructionCodeInsert, "placeholder")] - emptyState - codeTestFunc "instructionCodeInsertNotBlock" [Block [GeneInt 2, GeneInt 1]] [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 2, StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 1, GeneInt 1, StateFunc (instructionCodeInsert, "placeholder")] emptyState - intTestFunc "instructionCodePosition0" [0] [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 1, StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 1, GeneInt 2, GeneInt 3], StateFunc (instructionCodeFirstPosition, "placeholder")] emptyState - intTestFunc "instructionCodePosition-1" [-1] [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 7, StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 1, GeneInt 2, GeneInt 3], StateFunc (instructionCodeFirstPosition, "placeholder")] emptyState - intTestFunc "instructionCodePositionEmptyBlock" [0] [StateFunc (instructionCodeFromExec, "placeholder"), Block [], StateFunc (instructionCodeFromExec, "placeholder"), Block [], StateFunc (instructionCodeFirstPosition, "placeholder")] emptyState - codeTestFunc "instructionCodePositionBadStack" [GeneInt 1] [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 1, StateFunc (instructionCodeFirstPosition, "placeholder")] emptyState -- tests to ensure base case of insufficient code stack works. Should do this on more of these instructions. - codeTestFunc "instructionCodeReverse2Args" [Block [GeneInt 2, GeneInt 1]] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 1, GeneInt 2], StateFunc (instructionCodeReverse, "placeholder")] emptyState - codeTestFunc "instructionCodeReverse3Args" [Block [GeneInt 3, GeneInt 2, GeneInt 1]] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 1, GeneInt 2, GeneInt 3], StateFunc (instructionCodeReverse, "placeholder")] emptyState - codeTestFunc "instructionCodeReverseNonBlock" [GeneInt 1] [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 1, StateFunc (instructionCodeReverse, "placeholder")] emptyState - - -- String tests - stringTestFunc "instructionStringConcat" ["123abc"] [GeneString "abc", GeneString "123", StateFunc (instructionStringConcat, "placeholder")] emptyState - stringTestFunc "instructionStringSwap" ["abc", "123"] [GeneString "abc", GeneString "123", StateFunc (instructionStringSwap, "placeholder")] emptyState - stringTestFunc "instructionStringInsertString" ["123INSabc"] [GeneString "abc", GeneString "123", StateFunc (instructionStringConcat, "placeholder"), GeneString "INS", StateFunc (instructionStringSwap, "placeholder"), GeneInt 3, StateFunc (instructionStringInsertString, "placeholder")] emptyState - stringTestFunc "instructionStringFromFirstChar" ["1"] [GeneString "123", StateFunc (instructionStringFromFirstChar, "placeholder")] emptyState - stringTestFunc "instructionStringFromNthChar" ["a"] [GeneString "123abc", GeneInt 3, StateFunc (instructionStringFromNthChar, "placeholder")] emptyState - intTestFunc "instructionStringIndexOfString3" [3] [GeneString "a", GeneString "123abc", StateFunc (instructionStringIndexOfString, "placeholder")] emptyState - intTestFunc "instructionStringIndexOfString-1" [-1] [GeneString "z", GeneString "123abc", StateFunc (instructionStringIndexOfString, "placeholder")] emptyState - boolTestFunc "instructionStringContainsStringTrue" [True] [GeneString "a", GeneString "123abc", StateFunc (instructionStringContainsString, "placeholder")] emptyState - boolTestFunc "instructionStringContainsStringFalse" [False] [GeneString "z", GeneString "123abc", StateFunc (instructionStringContainsString, "placeholder")] emptyState - stringTestFunc "instructionStringSplitOnStringMult" ["nd", "r fri", "llo gam", "h"] [GeneString "e", GeneString "hello gamer friend", StateFunc (instructionStringSplitOnString, "placeholder")] emptyState - stringTestFunc "instructionStringSplitOnStringEmpty" ["", "hello gamer frien"] [GeneString "d", GeneString "hello gamer friend", StateFunc (instructionStringSplitOnString, "placeholder")] emptyState - stringTestFunc "instructionStringReplaceFirstStringSuccess" ["thREPLACEs is a sentence"] [GeneString "REPLACE", GeneString "i", GeneString "this is a sentence", StateFunc (instructionStringReplaceFirstString, "placeholder")] emptyState - stringTestFunc "instructionStringReplaceFirstStringFail" ["this is a sentence"] [GeneString "REPLACE", GeneString "z", GeneString "this is a sentence", StateFunc (instructionStringReplaceFirstString, "placeholder")] emptyState - stringTestFunc "instructionStringReplaceNStringSuccess" ["thREPLACEs REPLACEs a sentence i"] [GeneString "REPLACE", GeneString "i", GeneString "this is a sentence i", GeneInt 2, StateFunc (instructionStringReplaceNString, "placeholder")] emptyState - stringTestFunc "instructionStringReplaceNStringFail" ["this is a sentence i"] [GeneString "REPLACE", GeneString "z", GeneString "this is a sentence i", GeneInt 2, StateFunc (instructionStringReplaceNString, "placeholder")] emptyState - stringTestFunc "instructionStringReplaceAllStringSuccess" ["thREPLACEs REPLACEs a sentence REPLACE"] [GeneString "REPLACE", GeneString "i", GeneString "this is a sentence i", GeneInt 2, StateFunc (instructionStringReplaceAllString, "placeholder")] emptyState - stringTestFunc "instructionStringReplaceAllStringFail" ["this is a sentence i"] [GeneString "REPLACE", GeneString "z", GeneString "this is a sentence i", GeneInt 2, StateFunc (instructionStringReplaceAllString, "placeholder")] emptyState - stringTestFunc "instructionStringRemoveFirstStringSuccess" ["ths is a sentence"] [GeneString "i", GeneString "this is a sentence", StateFunc (instructionStringRemoveFirstString, "placeholder")] emptyState - stringTestFunc "instructionStringRemoveFirstStringFail" ["this is a sentence"] [GeneString "z", GeneString "this is a sentence", StateFunc (instructionStringRemoveFirstString, "placeholder")] emptyState - stringTestFunc "instructionStringRemoveNStringSuccess" ["ths s a sentence i"] [GeneString "i", GeneString "this is a sentence i", GeneInt 2, StateFunc (instructionStringRemoveNString, "placeholder")] emptyState - stringTestFunc "instructionStringRemoveNStringFail" ["this is a sentence i"] [GeneString "z", GeneString "this is a sentence i", GeneInt 2, StateFunc (instructionStringRemoveNString, "placeholder")] emptyState - stringTestFunc "instructionStringRemoveAllStringSuccess" ["ths s a sentence "] [GeneString "i", GeneString "this is a sentence i", StateFunc (instructionStringRemoveAllString, "placeholder")] emptyState - stringTestFunc "instructionStringRemoveAllStringFail" ["this is a sentence i"] [GeneString "z", GeneString "this is a sentence i", StateFunc (instructionStringRemoveAllString, "placeholder")] emptyState - intTestFunc "instructionStringOccurrencesOfString3" [3] [GeneString "i", GeneString "this is a sentence i", StateFunc (instructionStringOccurrencesOfString, "placeholder")] emptyState - intTestFunc "instructionStringOccurrencesOfString3" [2] [GeneString "is", GeneString "this is a sentence i", StateFunc (instructionStringOccurrencesOfString, "placeholder")] emptyState - intTestFunc "instructionStringOccurrencesOfString0" [0] [GeneString "z", GeneString "this is a sentence i", StateFunc (instructionStringOccurrencesOfString, "placeholder")] emptyState - stringTestFunc "instructionStringInsertChar" ["123Zabc"] [GeneString "abc", GeneString "123", StateFunc (instructionStringConcat, "placeholder"), GeneChar 'Z', GeneInt 3, StateFunc (instructionStringInsertChar, "placeholder")] emptyState - boolTestFunc "instructionStringContainsCharTrue" [True] [GeneString "abc", GeneChar 'a', StateFunc (instructionStringContainsChar, "placeholder")] emptyState - boolTestFunc "instructionStringContainsCharFalse" [False] [GeneString "abc", GeneChar 'z', StateFunc (instructionStringContainsChar, "placeholder")] emptyState - intTestFunc "instructionStringIndexOfChar3" [3] [GeneChar 'a', GeneString "123abc", StateFunc (instructionStringIndexOfChar, "placeholder")] emptyState - intTestFunc "instructionStringIndexOfChar-1" [-1] [GeneChar 'z', GeneString "123abc", StateFunc (instructionStringIndexOfChar, "placeholder")] emptyState - stringTestFunc "instructionStringSplitOnCharMult" ["nd", "r fri", "llo gam", "h"] [GeneChar 'e', GeneString "hello gamer friend", StateFunc (instructionStringSplitOnChar, "placeholder")] emptyState - stringTestFunc "instructionStringSplitOnCharEmpty" ["", "hello gamer frien"] [GeneChar 'd', GeneString "hello gamer friend", StateFunc (instructionStringSplitOnChar, "placeholder")] emptyState - stringTestFunc "instructionStringReplaceFirstCharSuccess" ["thRs is a sentence"] [GeneChar 'R', GeneChar 'i', GeneString "this is a sentence", StateFunc (instructionStringReplaceFirstChar, "placeholder")] emptyState - stringTestFunc "instructionStringReplaceFirstCharFail" ["this is a sentence"] [GeneChar 'R', GeneChar 'z', GeneString "this is a sentence", StateFunc (instructionStringReplaceFirstChar, "placeholder")] emptyState - stringTestFunc "instructionStringReplaceNCharSuccess" ["thRs Rs a sentence i"] [GeneChar 'R', GeneChar 'i', GeneString "this is a sentence i", GeneInt 2, StateFunc (instructionStringReplaceNChar, "placeholder")] emptyState - stringTestFunc "instructionStringReplaceNCharFail" ["this is a sentence i"] [GeneChar 'R', GeneChar 'z', GeneString "this is a sentence i", GeneInt 2, StateFunc (instructionStringReplaceNChar, "placeholder")] emptyState - stringTestFunc "instructionStringReplaceAllCharSuccess" ["thRs Rs a sentence R"] [GeneChar 'R', GeneChar 'i', GeneString "this is a sentence i", GeneInt 2, StateFunc (instructionStringReplaceAllChar, "placeholder")] emptyState - stringTestFunc "instructionStringReplaceAllCharFail" ["this is a sentence i"] [GeneChar 'R', GeneChar 'z', GeneString "this is a sentence i", GeneInt 2, StateFunc (instructionStringReplaceAllChar, "placeholder")] emptyState - stringTestFunc "instructionStringRemoveFirstCharSuccess" ["ths is a sentence"] [GeneChar 'i', GeneString "this is a sentence", StateFunc (instructionStringRemoveFirstChar, "placeholder")] emptyState - stringTestFunc "instructionStringRemoveFirstCharFail" ["this is a sentence"] [GeneChar 'z', GeneString "this is a sentence", StateFunc (instructionStringRemoveFirstChar, "placeholder")] emptyState - stringTestFunc "instructionStringRemoveNCharSuccess" ["ths s a sentence i"] [GeneChar 'i', GeneString "this is a sentence i", GeneInt 2, StateFunc (instructionStringRemoveNChar, "placeholder")] emptyState - stringTestFunc "instructionStringRemoveNCharFail" ["this is a sentence i"] [GeneChar 'z', GeneString "this is a sentence i", GeneInt 2, StateFunc (instructionStringRemoveNChar, "placeholder")] emptyState - stringTestFunc "instructionStringRemoveAllCharSuccess" ["ths s a sentence "] [GeneChar 'i', GeneString "this is a sentence i", StateFunc (instructionStringRemoveAllChar, "placeholder")] emptyState - stringTestFunc "instructionStringRemoveAllCharFail" ["this is a sentence i"] [GeneChar 'z', GeneString "this is a sentence i", StateFunc (instructionStringRemoveAllChar, "placeholder")] emptyState - intTestFunc "instructionStringOccurrencesOfChar3" [3] [GeneChar 'i', GeneString "this is a sentence i", StateFunc (instructionStringOccurrencesOfChar, "placeholder")] emptyState - intTestFunc "instructionStringOccurrencesOfChar0" [0] [GeneChar 'z', GeneString "this is a sentence i", StateFunc (instructionStringOccurrencesOfChar, "placeholder")] emptyState - stringTestFunc "instructionStringReverse" ["321cba"] [GeneString "abc123", StateFunc (instructionStringReverse, "placeholder")] emptyState - stringTestFunc "instructionStringHead3" ["abc"] [GeneString "abc123", GeneInt 3, StateFunc (instructionStringHead, "placeholder")] emptyState - stringTestFunc "instructionStringHead0" [""] [GeneString "abc123", GeneInt 0, StateFunc (instructionStringHead, "placeholder")] emptyState - stringTestFunc "instructionStringTail3" ["123"] [GeneString "abc123", GeneInt 3, StateFunc (instructionStringTail, "placeholder")] emptyState - stringTestFunc "instructionStringTail0" [""] [GeneString "abc123", GeneInt 0, StateFunc (instructionStringTail, "placeholder")] emptyState - stringTestFunc "instructionStringAppendChar" ["Rabc123"] [GeneString "abc123", GeneChar 'R', StateFunc (instructionStringAppendChar, "placeholder")] emptyState - stringTestFunc "instructionStringRestFull" ["bc123"] [GeneString "abc123", StateFunc (instructionStringRest, "placeholder")] emptyState - stringTestFunc "instructionStringRestEmpty" [""] [GeneString "", StateFunc (instructionStringRest, "placeholder")] emptyState - stringTestFunc "instructionStringButLastFull" ["abc12"] [GeneString "abc123", StateFunc (instructionStringButLast, "placeholder")] emptyState - stringTestFunc "instructionStringButLastEmpty" [""] [GeneString "", StateFunc (instructionStringButLast, "placeholder")] emptyState - stringTestFunc "instructionStringDrop3" ["123"] [GeneString "abc123", GeneInt 3, StateFunc (instructionStringDrop, "placeholder")] emptyState - stringTestFunc "instructionStringDrop0" ["abc123"] [GeneString "abc123", GeneInt 0, StateFunc (instructionStringDrop, "placeholder")] emptyState - stringTestFunc "instructionStringButLastN3" ["abc"] [GeneString "abc123", GeneInt 3, StateFunc (instructionStringButLastN, "placeholder")] emptyState - stringTestFunc "instructionStringButLastN0" ["abc123"] [GeneString "abc123", GeneInt 0, StateFunc (instructionStringButLastN, "placeholder")] emptyState - intTestFunc "instructionStringLength6" [6] [GeneString "abc123", StateFunc (instructionStringLength, "placeholder")] emptyState - stringTestFunc "instructionStringMakeEmpty" ["", "abc123"] [GeneString "abc123", StateFunc (instructionStringMakeEmpty, "placeholder")] emptyState - stringTestFunc "instructionStringRemoveNth" ["abc23"] [GeneString "abc123", GeneInt 3, StateFunc (instructionStringRemoveNth, "placeholder")] emptyState - stringTestFunc "instructionStringSetNth" ["abR123"] [GeneString "abc123", GeneInt 2, GeneChar 'R', StateFunc (instructionStringSetNth, "placeholder")] emptyState - stringTestFunc "instructionStringStripWhitespace" ["abc123"] [GeneString " \r \n abc123 \t", StateFunc (instructionStringStripWhitespace, "placeholder")] emptyState - stringTestFunc "instructionStringFromBoolTrue" ["True"] [GeneBool True, StateFunc (instructionStringFromBool, "placeholder")] emptyState - stringTestFunc "instructionStringFromBoolTrue" ["False"] [GeneBool False, StateFunc (instructionStringFromBool, "placeholder")] emptyState - stringTestFunc "instructionStringFromInt1000" ["1000"] [GeneInt 1000, StateFunc (instructionStringFromInt, "placeholder")] emptyState - stringTestFunc "instructionStringFromInt-1" ["-1"] [GeneInt (-1), StateFunc (instructionStringFromInt, "placeholder")] emptyState - stringTestFunc "instructionStringFromFloat3.2" ["3.2"] [GeneFloat 3.2, StateFunc (instructionStringFromFloat, "placeholder")] emptyState - stringTestFunc "instructionStringFromFloat-99.0" ["-99.0"] [GeneFloat (-99.0), StateFunc (instructionStringFromFloat, "placeholder")] emptyState - stringTestFunc "instructionStringFromChar" ["Z"] [GeneChar 'Z', StateFunc (instructionStringFromChar, "placeholder")] emptyState - stringTestFunc "instructionStringFromChar" [" "] [GeneChar ' ', StateFunc (instructionStringFromChar, "placeholder")] emptyState - - -- char instructions - stringTestFunc "instructionCharConcat" ["ab"] [GeneChar 'b', GeneChar 'a', StateFunc (instructionCharConcat, "placeholder")] emptyState - charTestFunc "instructionCharFromFirstCharSuccess" ['a'] [GeneString "abc123", StateFunc (instructionCharFromFirstChar, "placeholder")] emptyState - charTestFunc "instructionCharFromFirstCharFail" [] [GeneString "", StateFunc (instructionCharFromFirstChar, "placeholder")] emptyState - charTestFunc "instructionCharFromLastCharSuccess" ['3'] [GeneString "abc123", StateFunc (instructionCharFromLastChar, "placeholder")] emptyState - charTestFunc "instructionCharFromLastCharFail" [] [GeneString "", StateFunc (instructionCharFromLastChar, "placeholder")] emptyState - charTestFunc "instructionCharFromNthCharSuccess" ['c'] [GeneString "abc123", GeneInt 2, StateFunc (instructionCharFromNthChar, "placeholder")] emptyState - boolTestFunc "instructionCharIsWhitespaceSpace" [True] [GeneChar ' ', StateFunc (instructionCharIsWhitespace, "placeholder")] emptyState - boolTestFunc "instructionCharIsWhitespacet" [True] [GeneChar '\t', StateFunc (instructionCharIsWhitespace, "placeholder")] emptyState - boolTestFunc "instructionCharIsWhitespacer" [True] [GeneChar '\r', StateFunc (instructionCharIsWhitespace, "placeholder")] emptyState - boolTestFunc "instructionCharIsWhitespacen" [True] [GeneChar '\n', StateFunc (instructionCharIsWhitespace, "placeholder")] emptyState - boolTestFunc "instructionCharIsWhitespaceFalse" [False] [GeneChar 'a', StateFunc (instructionCharIsWhitespace, "placeholder")] emptyState - boolTestFunc "instructionCharIsLetterTrue" [True] [GeneChar 'a', StateFunc (instructionCharIsLetter, "placeholder")] emptyState - boolTestFunc "instructionCharIsLetterFalse" [False] [GeneChar '1', StateFunc (instructionCharIsLetter, "placeholder")] emptyState - boolTestFunc "instructionCharIsDigitTrue" [True] [GeneChar '1', StateFunc (instructionCharIsDigit, "placeholder")] emptyState - boolTestFunc "instructionCharIsDigitFalse" [False] [GeneChar 'a', StateFunc (instructionCharIsDigit, "placeholder")] emptyState - - -- vector int instructions - vectorIntTestFunc "instructionVectorIntConcat" [[4, 5, 6, 1, 2, 3]] [GeneVectorInt [1, 2, 3], GeneVectorInt [4, 5, 6], StateFunc (instructionVectorIntConcat, "placeholder")] emptyState - vectorIntTestFunc "instructionVectorIntConj" [[99, 1, 2, 3]] [GeneVectorInt [1, 2, 3], GeneInt 99, StateFunc (instructionVectorIntConj, "placeholder")] emptyState - vectorIntTestFunc "instructionIntTakeN" [[1, 2], [6, 7, 8]] [GeneVectorInt [6, 7, 8], GeneVectorInt [1, 2, 3], GeneInt 2, StateFunc (instructionVectorIntTakeN, "placeholder")] emptyState - vectorIntTestFunc "instructionVectorIntSubVector" [[1, 2, 3]] [GeneVectorInt [0, 1, 2, 3, 4, 5], GeneInt 3, GeneInt 1, StateFunc (instructionVectorIntSubVector, "placeholder")] emptyState - intTestFunc "instructionVectorIntFirst" [1] [GeneVectorInt [1, 2, 3, 4, 5], StateFunc (instructionVectorIntFirst, "placeholder")] emptyState - intTestFunc "instructionVectorIntLast" [5] [GeneVectorInt [1, 2, 3, 4, 5], StateFunc (instructionVectorIntLast, "placeholder")] emptyState - intTestFunc "instructionVectorIntNthInBounds" [2] [GeneVectorInt [1, 2, 3, 4, 5], GeneInt 1, StateFunc (instructionVectorIntNth, "placeholder")] emptyState - intTestFunc "instructionVectorIntNthOverflow" [2] [GeneVectorInt [1, 2, 3, 4, 5], GeneInt 6, StateFunc (instructionVectorIntNth, "placeholder")] emptyState - vectorIntTestFunc "instructionVectorIntRestFull" [[2, 3, 4, 5]] [GeneVectorInt [1, 2, 3, 4, 5], StateFunc (instructionVectorIntRest, "placeholder")] emptyState - vectorIntTestFunc "instructionVectorIntRestEmpty" [[]] [GeneVectorInt [], StateFunc (instructionVectorIntRest, "placeholder")] emptyState - vectorIntTestFunc "instructionVectorIntButLastFull" [[1, 2, 3, 4]] [GeneVectorInt [1, 2, 3, 4, 5], StateFunc (instructionVectorIntButLast, "placeholder")] emptyState - vectorIntTestFunc "instructionVectorIntButLastEmpty" [[]] [GeneVectorInt [], StateFunc (instructionVectorIntButLast, "placeholder")] emptyState - intTestFunc "instructionVectorIntLength3" [3] [GeneVectorInt [1, 2, 3], StateFunc (instructionVectorIntLength, "placeholder")] emptyState - intTestFunc "instructionVectorIntLength0" [0] [GeneVectorInt [], StateFunc (instructionVectorIntLength, "placeholder")] emptyState - vectorIntTestFunc "instructionVectorIntReverse" [[4, 3, 2, 1]] [GeneVectorInt [1, 2, 3, 4], StateFunc (instructionVectorIntReverse, "placeholder")] emptyState - intTestFunc "instructionVectorIntPushAllFull" [1, 2, 3, 4, 99] [GeneVectorInt [1, 2, 3, 4], GeneInt 99, StateFunc (instructionVectorIntPushAll, "placeholder")] emptyState - intTestFunc "instructionVectorIntPushAllEmpty" [99] [GeneVectorInt [], GeneInt 99, StateFunc (instructionVectorIntPushAll, "placeholder")] emptyState - vectorIntTestFunc "instructionVectorIntMakeEmpty" [[]] [StateFunc (instructionVectorIntMakeEmpty, "placeholder")] emptyState - boolTestFunc "instructionVectorIntIsEmptyTrue" [True] [GeneVectorInt [], StateFunc (instructionVectorIntIsEmpty, "placeholder")] emptyState - boolTestFunc "instructionVectorIntIsEmptyFalse" [False] [GeneVectorInt [1, 2, 3, 4], StateFunc (instructionVectorIntIsEmpty, "placeholder")] emptyState - intTestFunc "instructionVectorIntIndexOf1" [1] [GeneVectorInt [1, 2, 3, 4, 5], GeneInt 2, StateFunc (instructionVectorIntIndexOf, "placeholder")] emptyState - intTestFunc "instructionVectorIntIndexOfFail" [-1] [GeneVectorInt [], GeneInt 2, StateFunc (instructionVectorIntIndexOf, "placeholder")] emptyState - intTestFunc "instructionVectorIntOccurrencesOf2" [2] [GeneVectorInt [1, 2, 3, 4, 2, 6, 7], GeneInt 2, StateFunc (instructionVectorIntOccurrencesOf, "placeholder")] emptyState - intTestFunc "instructionVectorIntOccurrencesOf0" [0] [GeneVectorInt [1, 2, 3, 4, 2, 6, 7], GeneInt 0, StateFunc (instructionVectorIntOccurrencesOf, "placeholder")] emptyState - vectorIntTestFunc "instructionVectorIntSetNth3" [[0, 1, 2, 99, 4, 5]] [GeneVectorInt [0, 1, 2, 3, 4, 5], GeneInt 99, GeneInt 3, StateFunc (instructionVectorIntSetNth, "placeholder")] emptyState - vectorIntTestFunc "instructionVectorIntSetNth9" [[0, 1, 2, 99, 4, 5]] [GeneVectorInt [0, 1, 2, 3, 4, 5], GeneInt 99, GeneInt 9, StateFunc (instructionVectorIntSetNth, "placeholder")] emptyState - vectorIntTestFunc "instructionVectorIntReplace3" [[0, 1, 2, 99, 4, 5, 99, 5, 99]] [GeneInt 99, GeneInt 3, GeneVectorInt [0, 1, 2, 3, 4, 5, 3, 5, 3], StateFunc (instructionVectorIntReplace, "placeholder")] emptyState - vectorIntTestFunc "instructionVectorIntReplace-1" [[0, 1, 2, 3, 4, 5, 3, 5, 3]] [GeneInt 99, GeneInt (-1), GeneVectorInt [0, 1, 2, 3, 4, 5, 3, 5, 3], StateFunc (instructionVectorIntReplace, "placeholder")] emptyState - vectorIntTestFunc "instructionVectorIntReplaceFirst3" [[0, 1, 2, 99, 4, 5, 3, 5, 3]] [GeneInt 99, GeneInt 3, GeneVectorInt [0, 1, 2, 3, 4, 5, 3, 5, 3], StateFunc (instructionVectorIntReplaceFirst, "placeholder")] emptyState - vectorIntTestFunc "instructionVectorIntReplaceFirst-2" [[0, 1, 2, 3, 4, 5, 3, 5, 3]] [GeneInt 99, GeneInt (-2), GeneVectorInt [0, 1, 2, 3, 4, 5, 3, 5, 3], StateFunc (instructionVectorIntReplaceFirst, "placeholder")] emptyState - vectorIntTestFunc "instructionVectorIntRemove" [[0, 1, 2, 4, 5, 5]] [GeneInt 3, GeneVectorInt [0, 1, 2, 3, 4, 5, 3, 5, 3], StateFunc (instructionVectorIntRemove, "placeholder")] emptyState - intTestFunc "instructionVectorIntIterate" [66] [GeneInt 40, GeneVectorInt [0, 1, 2, 3, 4, 5, 3, 5, 3], StateFunc (instructionVectorIntIterate, "placeholder"), StateFunc (instructionIntAdd, "placeholder")] emptyState - - -- vector float functions - vectorFloatTestFunc "instructionVectorFloatConcat" [[4.0, 5.0, 6.0, 1.0, 2.0, 3.0]] [GeneVectorFloat [1.0, 2.0, 3.0], GeneVectorFloat [4.0, 5.0, 6.0], StateFunc (instructionVectorFloatConcat, "placeholder")] emptyState - vectorFloatTestFunc "instructionVectorFloatConj" [[99.0, 1.0, 2.0, 3.0]] [GeneVectorFloat [1.0, 2.0, 3.0], GeneFloat 99.0, StateFunc (instructionVectorFloatConj, "placeholder")] emptyState - vectorFloatTestFunc "instructionFloatTakeN" [[1.0, 2.0], [6.0, 7.0, 8.0]] [GeneVectorFloat [6.0, 7.0, 8.0], GeneVectorFloat [1.0, 2.0, 3.0], GeneInt 2, StateFunc (instructionVectorFloatTakeN, "placeholder")] emptyState - vectorFloatTestFunc "instructionVectorFloatSubVector" [[1.0, 2.0, 3.0]] [GeneVectorFloat [0.0, 1.0, 2.0, 3.0, 4.0, 5.0], GeneInt 3, GeneInt 1, StateFunc (instructionVectorFloatSubVector, "placeholder")] emptyState - floatTestFunc "instructionVectorFloatFirst" [1.0] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0, 5.0], StateFunc (instructionVectorFloatFirst, "placeholder")] emptyState - floatTestFunc "instructionVectorFloatLast" [5.0] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0, 5.0], StateFunc (instructionVectorFloatLast, "placeholder")] emptyState - floatTestFunc "instructionVectorFloatNthInBounds" [2.0] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0, 5.0], GeneInt 1, StateFunc (instructionVectorFloatNth, "placeholder")] emptyState - floatTestFunc "instructionVectorFloatNthOverflow" [2.0] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0, 5.0], GeneInt 6, StateFunc (instructionVectorFloatNth, "placeholder")] emptyState - vectorFloatTestFunc "instructionVectorFloatRestFull" [[2.0, 3.0, 4.0, 5.0]] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0, 5.0], StateFunc (instructionVectorFloatRest, "placeholder")] emptyState - vectorFloatTestFunc "instructionVectorFloatRestEmpty" [[]] [GeneVectorFloat [], StateFunc (instructionVectorFloatRest, "placeholder")] emptyState - vectorFloatTestFunc "instructionVectorFloatButLastFull" [[1.0, 2.0, 3.0, 4.0]] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0, 5.0], StateFunc (instructionVectorFloatButLast, "placeholder")] emptyState - vectorFloatTestFunc "instructionVectorFloatButLastEmpty" [[]] [GeneVectorFloat [], StateFunc (instructionVectorFloatButLast, "placeholder")] emptyState - intTestFunc "instructionVectorFloatLength3" [3] [GeneVectorFloat [1.0, 2.0, 3.0], StateFunc (instructionVectorFloatLength, "placeholder")] emptyState - intTestFunc "instructionVectorFloatLength0" [0] [GeneVectorFloat [], StateFunc (instructionVectorFloatLength, "placeholder")] emptyState - vectorFloatTestFunc "instructionVectorFloatReverse" [[4.0, 3.0, 2.0, 1.0]] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0], StateFunc (instructionVectorFloatReverse, "placeholder")] emptyState - floatTestFunc "instructionVectorFloatPushAllFull" [1.0, 2.0, 3.0, 4.0, 99.0] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0], GeneFloat 99.0, StateFunc (instructionVectorFloatPushAll, "placeholder")] emptyState - floatTestFunc "instructionVectorFloatPushAllEmpty" [99.0] [GeneVectorFloat [], GeneFloat 99.0, StateFunc (instructionVectorFloatPushAll, "placeholder")] emptyState - vectorFloatTestFunc "instructionVectorFloatMakeEmpty" [[]] [StateFunc (instructionVectorFloatMakeEmpty, "placeholder")] emptyState - boolTestFunc "instructionVectorFloatIsEmptyTrue" [True] [GeneVectorFloat [], StateFunc (instructionVectorFloatIsEmpty, "placeholder")] emptyState - boolTestFunc "instructionVectorFloatIsEmptyFalse" [False] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0], StateFunc (instructionVectorFloatIsEmpty, "placeholder")] emptyState - intTestFunc "instructionVectorFloatIndexOf1" [1] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0, 5.0], GeneFloat 2.0, StateFunc (instructionVectorFloatIndexOf, "placeholder")] emptyState - intTestFunc "instructionVectorFloatIndexOfFail" [-1] [GeneVectorFloat [], GeneFloat 2.0, StateFunc (instructionVectorFloatIndexOf, "placeholder")] emptyState - intTestFunc "instructionVectorFloatOccurrencesOf2" [2] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0, 2.0, 6.0, 7.0], GeneFloat 2.0, StateFunc (instructionVectorFloatOccurrencesOf, "placeholder")] emptyState - intTestFunc "instructionVectorFloatOccurrencesOf0" [0] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0, 2.0, 6.0, 7.0], GeneFloat 0.0, StateFunc (instructionVectorFloatOccurrencesOf, "placeholder")] emptyState - vectorFloatTestFunc "instructionVectorFloatSetNth3" [[0.0, 1.0, 2.0, 99.0, 4.0, 5.0]] [GeneVectorFloat [0.0, 1.0, 2.0, 3.0, 4.0, 5.0], GeneFloat 99.0, GeneInt 3, StateFunc (instructionVectorFloatSetNth, "placeholder")] emptyState - vectorFloatTestFunc "instructionVectorFloatSetNth9" [[0.0, 1.0, 2.0, 99.0, 4.0, 5.0]] [GeneVectorFloat [0.0, 1.0, 2.0, 3.0, 4.0, 5.0], GeneFloat 99.0, GeneInt 9, StateFunc (instructionVectorFloatSetNth, "placeholder")] emptyState - vectorFloatTestFunc "instructionVectorFloatReplace3" [[0.0, 1.0, 2.0, 99.0, 4.0, 5.0, 99.0, 5.0, 99.0]] [GeneFloat 99.0, GeneFloat 3.0, GeneVectorFloat [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 3.0, 5.0, 3.0], StateFunc (instructionVectorFloatReplace, "placeholder")] emptyState - vectorFloatTestFunc "instructionVectorFloatReplace-1" [[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 3.0, 5.0, 3.0]] [GeneFloat 99.0, GeneFloat (-1.0), GeneVectorFloat [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 3.0, 5.0, 3.0], StateFunc (instructionVectorFloatReplace, "placeholder")] emptyState - vectorFloatTestFunc "instructionVectorFloatReplaceFirst3" [[0.0, 1.0, 2.0, 99.0, 4.0, 5.0, 3.0, 5.0, 3.0]] [GeneFloat 99.0, GeneFloat 3.0, GeneVectorFloat [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 3.0, 5.0, 3.0], StateFunc (instructionVectorFloatReplaceFirst, "placeholder")] emptyState - vectorFloatTestFunc "instructionVectorFloatReplaceFirst-2" [[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 3.0, 5.0, 3.0]] [GeneFloat 99.0, GeneFloat (-2.0), GeneVectorFloat [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 3.0, 5.0, 3.0], StateFunc (instructionVectorFloatReplaceFirst, "placeholder")] emptyState - vectorFloatTestFunc "instructionVectorFloatRemove" [[0.0, 1.0, 2.0, 4.0, 5.0, 5.0]] [GeneFloat 3, GeneVectorFloat [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 3.0, 5.0, 3.0], StateFunc (instructionVectorFloatRemove, "placeholder")] emptyState - floatTestFunc "instructionVectorFloatIterate" [66.0] [GeneFloat 40.0, GeneVectorFloat [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 3.0, 5.0, 3.0], StateFunc (instructionVectorFloatIterate, "placeholder"), StateFunc (instructionFloatAdd, "placeholder")] emptyState +vcw :: Testable a => a -> IO () +vcw = verboseCheckWith pushTestArgs diff --git a/test/MainOld.hs b/test/MainOld.hs new file mode 100644 index 0000000..c3728e8 --- /dev/null +++ b/test/MainOld.hs @@ -0,0 +1,324 @@ +import Control.Exception (assert) +import Instructions +import Push +import State + +-- import Debug.Trace + +-- TODO: Need a function that can compare states. +-- May look at quickCheck later + +intTestFunc :: String -> [Int] -> [Gene] -> State -> IO () +intTestFunc name goal genome startState = + let state = loadProgram genome startState + in assert (goal == _int (interpretExec state)) putStrLn (name ++ " passed test.") + +floatTestFunc :: String -> [Float] -> [Gene] -> State -> IO () +floatTestFunc name goal genome startState = + let state = loadProgram genome startState + in assert (goal == _float (interpretExec state)) putStrLn (name ++ " passed test.") + +boolTestFunc :: String -> [Bool] -> [Gene] -> State -> IO () +boolTestFunc name goal genome startState = + let state = loadProgram genome startState + in assert (goal == _bool (interpretExec state)) putStrLn (name ++ " passed test.") + +codeTestFunc :: String -> [Gene] -> [Gene] -> State -> IO () +codeTestFunc name goal genome startState = + let state = loadProgram genome startState + in assert (goal == _code (interpretExec state)) putStrLn (name <> " passed test.") + +stringTestFunc :: String -> [String] -> [Gene] -> State -> IO () +stringTestFunc name goal genome startState = + let state = loadProgram genome startState + in assert (goal == _string (interpretExec state)) putStrLn (name <> " passed test.") + +charTestFunc :: String -> [Char] -> [Gene] -> State -> IO () +charTestFunc name goal genome startState = + let state = loadProgram genome startState + in assert (goal == _char (interpretExec state)) putStrLn (name <> " passed test.") + +vectorIntTestFunc :: String -> [[Int]] -> [Gene] -> State -> IO () +vectorIntTestFunc name goal genome startState = + let state = loadProgram genome startState + in assert (goal == _vectorInt (interpretExec state)) putStrLn (name <> " passed test.") + +vectorFloatTestFunc :: String -> [[Float]] -> [Gene] -> State -> IO () +vectorFloatTestFunc name goal genome startState = + let state = loadProgram genome startState + in assert (goal == _vectorFloat (interpretExec state)) putStrLn (name <> " passed test.") + +main :: IO () +main = do + -- Int tests + intTestFunc "instructionIntAdd" [8] [GeneInt 6, GeneInt 2, StateFunc (instructionIntAdd, "placeholder")] emptyState + intTestFunc "instructionIntSub" [4] [GeneInt 6, GeneInt 2, StateFunc (instructionIntSub, "placeholder")] emptyState + intTestFunc "instructionIntMul" [12] [GeneInt 6, GeneInt 2, StateFunc (instructionIntMul, "placeholder")] emptyState + intTestFunc "instructionIntDiv" [3] [GeneInt 6, GeneInt 2, StateFunc (instructionIntDiv, "placeholder")] emptyState + intTestFunc "instructionIntDiv0" [0, 2] [GeneInt 2, GeneInt 0, StateFunc (instructionIntDiv, "placeholder")] emptyState + intTestFunc "instructionIntMod" [3] [GeneInt 13, GeneInt 5, StateFunc (instructionIntMod, "placeholder")] emptyState + intTestFunc "instructionIntPop" [2] [GeneInt 2, GeneInt 0, StateFunc (instructionIntPop, "placeholder")] emptyState + intTestFunc "instructionIntDup" [3, 3, 2] [GeneInt 2, GeneInt 3, StateFunc (instructionIntDup, "placeholder")] emptyState + intTestFunc "instructionIntDupN3" [2, 2, 2] [GeneInt 2, GeneInt 3, StateFunc (instructionIntDupN, "placeholder")] emptyState + intTestFunc "instructionIntDupN-1" [0] [GeneInt 0, GeneInt 2, GeneInt (-1), StateFunc (instructionIntDupN, "placeholder")] emptyState + intTestFunc "instructionIntSwap" [2, 0, 3] [GeneInt 3, GeneInt 2, GeneInt 0, StateFunc (instructionIntSwap, "placeholder")] emptyState + intTestFunc "instructionIntSwapFail" [1] [GeneInt 1, StateFunc (instructionIntSwap, "placeholder")] emptyState + intTestFunc "instructionIntRot" [1, 3, 2] [GeneInt 1, GeneInt 2, GeneInt 3, StateFunc (instructionIntRot, "placeholder")] emptyState + intTestFunc "instructionIntRotFail" [7, 8] [GeneInt 8, GeneInt 7, StateFunc (instructionIntRot, "placeholder")] emptyState + intTestFunc "instructionIntFlush" [] [GeneInt 9696, GeneInt 92, GeneInt 420, StateFunc (instructionIntFlush, "placeholder")] emptyState -- I think I'm funny + intTestFunc "instructionIntStackDepth" [2, 51, 52] [GeneInt 52, GeneInt 51, StateFunc (instructionIntStackDepth, "placeholder")] emptyState + intTestFunc "instructionIntYank" [3, 3, 2, 1] [GeneInt 3, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 4, StateFunc (instructionIntYank, "placeholder")] emptyState + intTestFunc "instructionIntYankDup" [3, 3, 2, 1, 3] [GeneInt 3, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 4, StateFunc (instructionIntYankDup, "placeholder")] emptyState + intTestFunc "instructionIntShove" [2, 3, 1, 1] [GeneInt 1, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 2, StateFunc (instructionIntShove, "placeholder")] emptyState + intTestFunc "instructionIntShoveDup" [3, 2, 3, 1, 1] [GeneInt 1, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 2, StateFunc (instructionIntShoveDup, "placeholder")] emptyState + + -- Exec tests + intTestFunc "instructionExecIf" [6, 5] [GeneBool True, StateFunc (instructionExecIf, "placeholder"), Block [GeneInt 5, GeneInt 6], Block [GeneInt 7, GeneInt 8]] emptyState + intTestFunc "instructionExecDup" [8] [StateFunc (instructionExecDup, "placeholder"), GeneInt 4, StateFunc (instructionIntAdd, "placeholder")] emptyState + intTestFunc "instructionExecDoRange" [12] [GeneInt 2, Block [GeneInt 4, GeneInt 1, StateFunc (instructionExecDoRange, "placeholder")], StateFunc (instructionIntAdd, "placeholder")] emptyState + intTestFunc "instructionExecDoCount" [8] [GeneInt 2, Block [GeneInt 4, StateFunc (instructionExecDoCount, "placeholder")], StateFunc (instructionIntAdd, "placeholder")] emptyState + intTestFunc "instructionExecDoTimes" [69, 69, 69, 69, 2] [GeneInt 2, Block [GeneInt 4, StateFunc (instructionExecDoTimes, "placeholder")], GeneInt 69] emptyState + intTestFunc "instructionExecWhile" [70, 70] [GeneBool False, GeneBool True, GeneBool True, StateFunc (instructionExecWhile, "placeholder"), GeneInt 70] emptyState + intTestFunc "instructionExecDoWhile" [70, 70, 70] [GeneBool False, GeneBool True, GeneBool True, StateFunc (instructionExecDoWhile, "placeholder"), GeneInt 70] emptyState + intTestFunc "instructionExecWhenTrue" [71] [GeneBool True, StateFunc (instructionExecWhen, "placeholder"), GeneInt 71] emptyState + + let loadedState = loadProgram [GeneBool False, StateFunc (instructionExecWhen, "placeholder"), GeneInt 71] emptyState + assert (emptyState == interpretExec loadedState) putStrLn "instructionExecWhenFalse passed test." + + -- Float tests + floatTestFunc "instructionFloatAdd" [4.32] [GeneFloat 4.01, GeneFloat 0.31, StateFunc (instructionFloatAdd, "placeholder")] emptyState + floatTestFunc "instructionFloatSub" [3.6900003] [GeneFloat 4.01, GeneFloat 0.32, StateFunc (instructionFloatSub, "placeholder")] emptyState + floatTestFunc "instructionFloatMul" [1.38] [GeneFloat 0.12, GeneFloat 11.5, StateFunc (instructionFloatMul, "placeholder")] emptyState + floatTestFunc "instructionFloatDiv" [57.5] [GeneFloat 11.5, GeneFloat 0.2, StateFunc (instructionFloatDiv, "placeholder")] emptyState + floatTestFunc "instructionFloatDiv0" [0, 69.69] [GeneFloat 69.69, GeneFloat 0.0, StateFunc (instructionFloatDiv, "placeholder")] emptyState + floatTestFunc "instructionFloatYank" [1.1, 4.4, 3.3, 2.2] [GeneInt 3, GeneFloat 1.1, GeneFloat 2.2, GeneFloat 3.3, GeneFloat 4.4, StateFunc (instructionFloatYank, "placeholder")] emptyState + floatTestFunc "instructionFloatYankDup" [1.1, 4.4, 3.3, 2.2, 1.1] [GeneInt 3, GeneFloat 1.1, GeneFloat 2.2, GeneFloat 3.3, GeneFloat 4.4, StateFunc (instructionFloatYankDup, "placeholder")] emptyState + floatTestFunc "instructionFloatShove" [3.3, 2.2, 4.4, 1.1] [GeneInt 3, GeneFloat 1.1, GeneFloat 2.2, GeneFloat 3.3, GeneFloat 4.4, StateFunc (instructionFloatShove, "placeholder")] emptyState + floatTestFunc "instructionFloatShoveDup" [4.4, 3.3, 2.2, 4.4, 1.1] [GeneInt 3, GeneFloat 1.1, GeneFloat 2.2, GeneFloat 3.3, GeneFloat 4.4, StateFunc (instructionFloatShoveDup, "placeholder")] emptyState + floatTestFunc "instructionFloatDupNonEmpty" [4.4, 4.4, 3.3] [GeneFloat 3.3, GeneFloat 4.4, StateFunc (instructionFloatDup, "placeholder")] emptyState + floatTestFunc "instructionFloatDupEmpty" [] [StateFunc (instructionFloatDup, "placeholder")] emptyState + floatTestFunc "instructionFloatDupN3" [4.4, 4.4, 4.4, 3.3] [GeneFloat 3.3, GeneFloat 4.4, GeneInt 3, StateFunc (instructionFloatDupN, "placeholder")] emptyState + floatTestFunc "instructionFloatDupN-1" [3.3] [GeneFloat 3.3, GeneFloat 4.4, GeneInt (-1), StateFunc (instructionFloatDupN, "placeholder")] emptyState + boolTestFunc "instructionIntEqTrue" [True] [GeneInt 3, GeneInt 3, StateFunc (instructionIntEq, "placeholder")] emptyState + boolTestFunc "instructionIntEqFalse" [False] [GeneInt 3, GeneInt 5, StateFunc (instructionIntEq, "placeholder")] emptyState + boolTestFunc "instructionIntEqFail" [] [GeneInt 3, StateFunc (instructionIntEq, "placeholder")] emptyState + + -- Code tests + codeTestFunc "instructionCodeFromExec" [] [StateFunc (instructionCodeFromExec, "placeholder"), StateFunc (instructionFloatFromInt, "placeholder"), StateFunc (instructionCodePop, "placeholder")] emptyState + intTestFunc "instructionCodeDoRange" [18] [GeneInt 3, GeneInt 6, StateFunc (instructionCodeFromExec, "placeholder"), StateFunc (instructionIntAdd, "placeholder"), StateFunc (instructionCodeDoRange, "placeholder")] emptyState + -- How to test instructionCodeDoThenPop????? + codeTestFunc "instructionCodeFirst" [GeneInt 5] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 5, StateFunc (instructionIntSub, "placeholder")], StateFunc (instructionCodeFirst, "placeholder")] emptyState + codeTestFunc "instructionCodeLast" [GeneBool True] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 5, StateFunc (instructionIntSub, "placeholder"), GeneBool True], StateFunc (instructionCodeLast, "placeholder")] emptyState + codeTestFunc "instructionCodeTail" [Block [GeneFloat 3.2, GeneBool True, GeneInt 3]] [StateFunc (instructionCodeFromExec, "placeholder"), Block [StateFunc (instructionFloatAdd, "placeholder"), GeneFloat 3.2, GeneBool True, GeneInt 3], StateFunc (instructionCodeTail, "placeholder")] emptyState + codeTestFunc "instructionCodeInit" [Block [GeneVectorInt [1], GeneFloat 3.2, GeneBool True]] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneVectorInt [1], GeneFloat 3.2, GeneBool True, GeneInt 3], StateFunc (instructionCodeInit, "placeholder")] emptyState + codeTestFunc "instructionCodeWrap" [Block [GeneInt 3]] [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 3, StateFunc (instructionCodeWrap, "placeholder")] emptyState + codeTestFunc "instructionCodeList" [Block [GeneFloat 5.43, GeneInt 3]] [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 3, StateFunc (instructionCodeFromExec, "placeholder"), GeneFloat 5.43, StateFunc (instructionCodeList, "placeholder")] emptyState + codeTestFunc "instructionCodeCombine2Blocks" [Block [GeneInt 3, GeneInt 4, GeneInt 1, GeneInt 2]] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 1, GeneInt 2], StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 3, GeneInt 4], StateFunc (instructionCodeCombine, "placeholder")] emptyState + codeTestFunc "instructionCodeCombine1Block1Single" [Block [GeneInt 3, GeneInt 4, GeneInt 1]] [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 1, StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 3, GeneInt 4], StateFunc (instructionCodeCombine, "placeholder")] emptyState + codeTestFunc "instructionCodeCombine1Single1Block" [Block [GeneInt 3, GeneInt 1, GeneInt 2]] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 1, GeneInt 2], StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 3, StateFunc (instructionCodeCombine, "placeholder")] emptyState + codeTestFunc "instrucitonCodeCombine2Single" [Block [GeneInt 2, GeneInt 1]] [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 1, StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 2, StateFunc (instructionCodeCombine, "placeholder")] emptyState + intTestFunc "instructionCodeDo" [3] [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 3, StateFunc (instructionCodeDo, "placeholder")] emptyState + -- How to test instructionCodeDoDup??? We would would need a multi stack testing function + boolTestFunc "instructionCodeIsCodeBlockTrue" [True] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 0], StateFunc (instructionCodeIsCodeBlock, "placeholder")] emptyState + boolTestFunc "instructionCodeIsCodeBlockFalse" [False] [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 0, StateFunc (instructionCodeIsCodeBlock, "placeholder")] emptyState + boolTestFunc "instructionCodeIsSingularTrue" [True] [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 0, StateFunc (instructionCodeIsSingular, "placeholder")] emptyState + boolTestFunc "instructionCodeIsSingularFalse" [False] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 0], StateFunc (instructionCodeIsSingular, "placeholder")] emptyState + intTestFunc "instructionCodeDoCount" [15] [GeneInt 6, StateFunc (instructionCodeFromExec, "placeholder"), StateFunc (instructionIntAdd, "placeholder"), StateFunc (instructionCodeDoCount, "placeholder")] emptyState + intTestFunc "instructionCodeDoTimes" [13] [GeneInt 6, GeneInt 3, GeneInt 4, GeneInt 2, StateFunc (instructionCodeFromExec, "placeholder"), StateFunc (instructionIntAdd, "placeholder"), StateFunc (instructionCodeDoTimes, "placeholder")] emptyState + intTestFunc "instructionCodeIfTrue" [6] [GeneBool True, StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 3, StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 6, StateFunc (instructionCodeIf, "placeholder")] emptyState + intTestFunc "instructionCodeIfFalse" [3] [GeneBool False, StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 3, StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 6, StateFunc (instructionCodeIf, "placeholder")] emptyState + intTestFunc "instructionCodeWhen" [6, 3, 6] [GeneInt 6, GeneInt 3, GeneInt 4, GeneInt 2, GeneBool True, StateFunc (instructionCodeFromExec, "placeholder"), StateFunc (instructionIntAdd, "placeholder"), StateFunc (instructionCodeWhen, "placeholder")] emptyState + boolTestFunc "instructionCodeMemberTrue" [True] [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 2, StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneFloat 3.6, GeneInt 2, GeneVectorInt [8, 9]], StateFunc (instructionCodeMember, "placeholder")] emptyState + boolTestFunc "instructionCodeMemberFalse" [False] [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 7, StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneFloat 3.6, GeneInt 2, GeneVectorInt [8, 9]], StateFunc (instructionCodeMember, "placeholder")] emptyState + boolTestFunc "instructionCodeMember2Blocks" [False] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 7, GeneInt 0], StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneFloat 3.6, GeneInt 2, GeneVectorInt [8, 9]], StateFunc (instructionCodeMember, "placeholder")] emptyState + codeTestFunc "instructionCodeNInBounds" [GeneInt 0] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 0, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 4, GeneInt 5], GeneInt 0, StateFunc (instructionCodeN, "placeholder")] emptyState + codeTestFunc "instructionCodeNInBoundsEnd" [GeneInt 5] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 0, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 4, GeneInt 5], GeneInt 5, StateFunc (instructionCodeN, "placeholder")] emptyState + codeTestFunc "instructionCodeNModded" [GeneInt 3] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 0, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 4, GeneInt 5], GeneInt 9, StateFunc (instructionCodeN, "placeholder")] emptyState + codeTestFunc "instructionMakeEmptyCodeBlock" [Block []] [StateFunc (instructionMakeEmptyCodeBlock, "placeholder")] emptyState + boolTestFunc "instructionIsEmptyCodeBlockTrue" [True] [StateFunc (instructionCodeFromExec, "placeholder"), Block [], StateFunc (instructionIsEmptyCodeBlock, "placeholder")] emptyState + intTestFunc "instructionCodeSize" [8] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6], StateFunc (instructionCodeSize, "placeholder")] emptyState + codeTestFunc "instructionCodeExtractInBounds" [GeneInt 3] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6], GeneInt 3, StateFunc (instructionCodeExtract, "placeholder")] emptyState + codeTestFunc "instructionCodeExtractOutBounds" [GeneInt 3] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6], GeneInt 11, StateFunc (instructionCodeExtract, "placeholder")] emptyState + codeTestFunc "instructionCodeExtractLastEmptyBlock" [Block []] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], Block []], GeneInt 7, StateFunc (instructionCodeExtract, "placeholder")] emptyState + codeTestFunc "instructionCodeExtractBlock" [Block [GeneInt 2, GeneInt 3]] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], Block []], GeneInt 1, StateFunc (instructionCodeExtract, "placeholder")] emptyState + codeTestFunc "instructionCodeExtractEdgeCase" [Block []] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], Block []], GeneInt 7, StateFunc (instructionCodeExtract, "placeholder")] emptyState + codeTestFunc "instructionCodeExtractNotBlock" [GeneInt 2] [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 2, GeneInt 56, StateFunc (instructionCodeExtract, "placeholder")] emptyState + codeTestFunc + "instructionCodeInsertInBounds" + [Block [GeneInt 1, Block [GeneInt 2, GeneInt 9999, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6, Block [GeneInt 7, GeneInt 8], GeneInt 9]] + [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 9999, StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6, Block [GeneInt 7, GeneInt 8], GeneInt 9], GeneInt 3, StateFunc (instructionCodeInsert, "placeholder")] + emptyState + codeTestFunc + "instructionCodeInsertOutBounds" + [Block [GeneInt 1, Block [GeneInt 2, GeneInt 9999, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6, Block [GeneInt 7, GeneInt 8], GeneInt 9]] + [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 9999, StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 1, Block [GeneInt 2, GeneInt 3], Block [GeneInt 4, GeneInt 5], GeneInt 6, Block [GeneInt 7, GeneInt 8], GeneInt 9], GeneInt 15, StateFunc (instructionCodeInsert, "placeholder")] + emptyState + codeTestFunc "instructionCodeInsertNotBlock" [Block [GeneInt 2, GeneInt 1]] [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 2, StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 1, GeneInt 1, StateFunc (instructionCodeInsert, "placeholder")] emptyState + intTestFunc "instructionCodePosition0" [0] [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 1, StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 1, GeneInt 2, GeneInt 3], StateFunc (instructionCodeFirstPosition, "placeholder")] emptyState + intTestFunc "instructionCodePosition-1" [-1] [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 7, StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 1, GeneInt 2, GeneInt 3], StateFunc (instructionCodeFirstPosition, "placeholder")] emptyState + intTestFunc "instructionCodePositionEmptyBlock" [0] [StateFunc (instructionCodeFromExec, "placeholder"), Block [], StateFunc (instructionCodeFromExec, "placeholder"), Block [], StateFunc (instructionCodeFirstPosition, "placeholder")] emptyState + codeTestFunc "instructionCodePositionBadStack" [GeneInt 1] [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 1, StateFunc (instructionCodeFirstPosition, "placeholder")] emptyState -- tests to ensure base case of insufficient code stack works. Should do this on more of these instructions. + codeTestFunc "instructionCodeReverse2Args" [Block [GeneInt 2, GeneInt 1]] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 1, GeneInt 2], StateFunc (instructionCodeReverse, "placeholder")] emptyState + codeTestFunc "instructionCodeReverse3Args" [Block [GeneInt 3, GeneInt 2, GeneInt 1]] [StateFunc (instructionCodeFromExec, "placeholder"), Block [GeneInt 1, GeneInt 2, GeneInt 3], StateFunc (instructionCodeReverse, "placeholder")] emptyState + codeTestFunc "instructionCodeReverseNonBlock" [GeneInt 1] [StateFunc (instructionCodeFromExec, "placeholder"), GeneInt 1, StateFunc (instructionCodeReverse, "placeholder")] emptyState + + -- String tests + stringTestFunc "instructionStringConcat" ["123abc"] [GeneString "abc", GeneString "123", StateFunc (instructionStringConcat, "placeholder")] emptyState + stringTestFunc "instructionStringSwap" ["abc", "123"] [GeneString "abc", GeneString "123", StateFunc (instructionStringSwap, "placeholder")] emptyState + stringTestFunc "instructionStringInsertString" ["123INSabc"] [GeneString "abc", GeneString "123", StateFunc (instructionStringConcat, "placeholder"), GeneString "INS", StateFunc (instructionStringSwap, "placeholder"), GeneInt 3, StateFunc (instructionStringInsertString, "placeholder")] emptyState + stringTestFunc "instructionStringFromFirstChar" ["1"] [GeneString "123", StateFunc (instructionStringFromFirstChar, "placeholder")] emptyState + stringTestFunc "instructionStringFromNthChar" ["a"] [GeneString "123abc", GeneInt 3, StateFunc (instructionStringFromNthChar, "placeholder")] emptyState + intTestFunc "instructionStringIndexOfString3" [3] [GeneString "a", GeneString "123abc", StateFunc (instructionStringIndexOfString, "placeholder")] emptyState + intTestFunc "instructionStringIndexOfString-1" [-1] [GeneString "z", GeneString "123abc", StateFunc (instructionStringIndexOfString, "placeholder")] emptyState + boolTestFunc "instructionStringContainsStringTrue" [True] [GeneString "a", GeneString "123abc", StateFunc (instructionStringContainsString, "placeholder")] emptyState + boolTestFunc "instructionStringContainsStringFalse" [False] [GeneString "z", GeneString "123abc", StateFunc (instructionStringContainsString, "placeholder")] emptyState + stringTestFunc "instructionStringSplitOnStringMult" ["nd", "r fri", "llo gam", "h"] [GeneString "e", GeneString "hello gamer friend", StateFunc (instructionStringSplitOnString, "placeholder")] emptyState + stringTestFunc "instructionStringSplitOnStringEmpty" ["", "hello gamer frien"] [GeneString "d", GeneString "hello gamer friend", StateFunc (instructionStringSplitOnString, "placeholder")] emptyState + stringTestFunc "instructionStringReplaceFirstStringSuccess" ["thREPLACEs is a sentence"] [GeneString "REPLACE", GeneString "i", GeneString "this is a sentence", StateFunc (instructionStringReplaceFirstString, "placeholder")] emptyState + stringTestFunc "instructionStringReplaceFirstStringFail" ["this is a sentence"] [GeneString "REPLACE", GeneString "z", GeneString "this is a sentence", StateFunc (instructionStringReplaceFirstString, "placeholder")] emptyState + stringTestFunc "instructionStringReplaceNStringSuccess" ["thREPLACEs REPLACEs a sentence i"] [GeneString "REPLACE", GeneString "i", GeneString "this is a sentence i", GeneInt 2, StateFunc (instructionStringReplaceNString, "placeholder")] emptyState + stringTestFunc "instructionStringReplaceNStringFail" ["this is a sentence i"] [GeneString "REPLACE", GeneString "z", GeneString "this is a sentence i", GeneInt 2, StateFunc (instructionStringReplaceNString, "placeholder")] emptyState + stringTestFunc "instructionStringReplaceAllStringSuccess" ["thREPLACEs REPLACEs a sentence REPLACE"] [GeneString "REPLACE", GeneString "i", GeneString "this is a sentence i", GeneInt 2, StateFunc (instructionStringReplaceAllString, "placeholder")] emptyState + stringTestFunc "instructionStringReplaceAllStringFail" ["this is a sentence i"] [GeneString "REPLACE", GeneString "z", GeneString "this is a sentence i", GeneInt 2, StateFunc (instructionStringReplaceAllString, "placeholder")] emptyState + stringTestFunc "instructionStringRemoveFirstStringSuccess" ["ths is a sentence"] [GeneString "i", GeneString "this is a sentence", StateFunc (instructionStringRemoveFirstString, "placeholder")] emptyState + stringTestFunc "instructionStringRemoveFirstStringFail" ["this is a sentence"] [GeneString "z", GeneString "this is a sentence", StateFunc (instructionStringRemoveFirstString, "placeholder")] emptyState + stringTestFunc "instructionStringRemoveNStringSuccess" ["ths s a sentence i"] [GeneString "i", GeneString "this is a sentence i", GeneInt 2, StateFunc (instructionStringRemoveNString, "placeholder")] emptyState + stringTestFunc "instructionStringRemoveNStringFail" ["this is a sentence i"] [GeneString "z", GeneString "this is a sentence i", GeneInt 2, StateFunc (instructionStringRemoveNString, "placeholder")] emptyState + stringTestFunc "instructionStringRemoveAllStringSuccess" ["ths s a sentence "] [GeneString "i", GeneString "this is a sentence i", StateFunc (instructionStringRemoveAllString, "placeholder")] emptyState + stringTestFunc "instructionStringRemoveAllStringFail" ["this is a sentence i"] [GeneString "z", GeneString "this is a sentence i", StateFunc (instructionStringRemoveAllString, "placeholder")] emptyState + intTestFunc "instructionStringOccurrencesOfString3" [3] [GeneString "i", GeneString "this is a sentence i", StateFunc (instructionStringOccurrencesOfString, "placeholder")] emptyState + intTestFunc "instructionStringOccurrencesOfString3" [2] [GeneString "is", GeneString "this is a sentence i", StateFunc (instructionStringOccurrencesOfString, "placeholder")] emptyState + intTestFunc "instructionStringOccurrencesOfString0" [0] [GeneString "z", GeneString "this is a sentence i", StateFunc (instructionStringOccurrencesOfString, "placeholder")] emptyState + stringTestFunc "instructionStringInsertChar" ["123Zabc"] [GeneString "abc", GeneString "123", StateFunc (instructionStringConcat, "placeholder"), GeneChar 'Z', GeneInt 3, StateFunc (instructionStringInsertChar, "placeholder")] emptyState + boolTestFunc "instructionStringContainsCharTrue" [True] [GeneString "abc", GeneChar 'a', StateFunc (instructionStringContainsChar, "placeholder")] emptyState + boolTestFunc "instructionStringContainsCharFalse" [False] [GeneString "abc", GeneChar 'z', StateFunc (instructionStringContainsChar, "placeholder")] emptyState + intTestFunc "instructionStringIndexOfChar3" [3] [GeneChar 'a', GeneString "123abc", StateFunc (instructionStringIndexOfChar, "placeholder")] emptyState + intTestFunc "instructionStringIndexOfChar-1" [-1] [GeneChar 'z', GeneString "123abc", StateFunc (instructionStringIndexOfChar, "placeholder")] emptyState + stringTestFunc "instructionStringSplitOnCharMult" ["nd", "r fri", "llo gam", "h"] [GeneChar 'e', GeneString "hello gamer friend", StateFunc (instructionStringSplitOnChar, "placeholder")] emptyState + stringTestFunc "instructionStringSplitOnCharEmpty" ["", "hello gamer frien"] [GeneChar 'd', GeneString "hello gamer friend", StateFunc (instructionStringSplitOnChar, "placeholder")] emptyState + stringTestFunc "instructionStringReplaceFirstCharSuccess" ["thRs is a sentence"] [GeneChar 'R', GeneChar 'i', GeneString "this is a sentence", StateFunc (instructionStringReplaceFirstChar, "placeholder")] emptyState + stringTestFunc "instructionStringReplaceFirstCharFail" ["this is a sentence"] [GeneChar 'R', GeneChar 'z', GeneString "this is a sentence", StateFunc (instructionStringReplaceFirstChar, "placeholder")] emptyState + stringTestFunc "instructionStringReplaceNCharSuccess" ["thRs Rs a sentence i"] [GeneChar 'R', GeneChar 'i', GeneString "this is a sentence i", GeneInt 2, StateFunc (instructionStringReplaceNChar, "placeholder")] emptyState + stringTestFunc "instructionStringReplaceNCharFail" ["this is a sentence i"] [GeneChar 'R', GeneChar 'z', GeneString "this is a sentence i", GeneInt 2, StateFunc (instructionStringReplaceNChar, "placeholder")] emptyState + stringTestFunc "instructionStringReplaceAllCharSuccess" ["thRs Rs a sentence R"] [GeneChar 'R', GeneChar 'i', GeneString "this is a sentence i", GeneInt 2, StateFunc (instructionStringReplaceAllChar, "placeholder")] emptyState + stringTestFunc "instructionStringReplaceAllCharFail" ["this is a sentence i"] [GeneChar 'R', GeneChar 'z', GeneString "this is a sentence i", GeneInt 2, StateFunc (instructionStringReplaceAllChar, "placeholder")] emptyState + stringTestFunc "instructionStringRemoveFirstCharSuccess" ["ths is a sentence"] [GeneChar 'i', GeneString "this is a sentence", StateFunc (instructionStringRemoveFirstChar, "placeholder")] emptyState + stringTestFunc "instructionStringRemoveFirstCharFail" ["this is a sentence"] [GeneChar 'z', GeneString "this is a sentence", StateFunc (instructionStringRemoveFirstChar, "placeholder")] emptyState + stringTestFunc "instructionStringRemoveNCharSuccess" ["ths s a sentence i"] [GeneChar 'i', GeneString "this is a sentence i", GeneInt 2, StateFunc (instructionStringRemoveNChar, "placeholder")] emptyState + stringTestFunc "instructionStringRemoveNCharFail" ["this is a sentence i"] [GeneChar 'z', GeneString "this is a sentence i", GeneInt 2, StateFunc (instructionStringRemoveNChar, "placeholder")] emptyState + stringTestFunc "instructionStringRemoveAllCharSuccess" ["ths s a sentence "] [GeneChar 'i', GeneString "this is a sentence i", StateFunc (instructionStringRemoveAllChar, "placeholder")] emptyState + stringTestFunc "instructionStringRemoveAllCharFail" ["this is a sentence i"] [GeneChar 'z', GeneString "this is a sentence i", StateFunc (instructionStringRemoveAllChar, "placeholder")] emptyState + intTestFunc "instructionStringOccurrencesOfChar3" [3] [GeneChar 'i', GeneString "this is a sentence i", StateFunc (instructionStringOccurrencesOfChar, "placeholder")] emptyState + intTestFunc "instructionStringOccurrencesOfChar0" [0] [GeneChar 'z', GeneString "this is a sentence i", StateFunc (instructionStringOccurrencesOfChar, "placeholder")] emptyState + stringTestFunc "instructionStringReverse" ["321cba"] [GeneString "abc123", StateFunc (instructionStringReverse, "placeholder")] emptyState + stringTestFunc "instructionStringHead3" ["abc"] [GeneString "abc123", GeneInt 3, StateFunc (instructionStringHead, "placeholder")] emptyState + stringTestFunc "instructionStringHead0" [""] [GeneString "abc123", GeneInt 0, StateFunc (instructionStringHead, "placeholder")] emptyState + stringTestFunc "instructionStringTail3" ["123"] [GeneString "abc123", GeneInt 3, StateFunc (instructionStringTail, "placeholder")] emptyState + stringTestFunc "instructionStringTail0" [""] [GeneString "abc123", GeneInt 0, StateFunc (instructionStringTail, "placeholder")] emptyState + stringTestFunc "instructionStringAppendChar" ["Rabc123"] [GeneString "abc123", GeneChar 'R', StateFunc (instructionStringAppendChar, "placeholder")] emptyState + stringTestFunc "instructionStringRestFull" ["bc123"] [GeneString "abc123", StateFunc (instructionStringRest, "placeholder")] emptyState + stringTestFunc "instructionStringRestEmpty" [""] [GeneString "", StateFunc (instructionStringRest, "placeholder")] emptyState + stringTestFunc "instructionStringButLastFull" ["abc12"] [GeneString "abc123", StateFunc (instructionStringButLast, "placeholder")] emptyState + stringTestFunc "instructionStringButLastEmpty" [""] [GeneString "", StateFunc (instructionStringButLast, "placeholder")] emptyState + stringTestFunc "instructionStringDrop3" ["123"] [GeneString "abc123", GeneInt 3, StateFunc (instructionStringDrop, "placeholder")] emptyState + stringTestFunc "instructionStringDrop0" ["abc123"] [GeneString "abc123", GeneInt 0, StateFunc (instructionStringDrop, "placeholder")] emptyState + stringTestFunc "instructionStringButLastN3" ["abc"] [GeneString "abc123", GeneInt 3, StateFunc (instructionStringButLastN, "placeholder")] emptyState + stringTestFunc "instructionStringButLastN0" ["abc123"] [GeneString "abc123", GeneInt 0, StateFunc (instructionStringButLastN, "placeholder")] emptyState + intTestFunc "instructionStringLength6" [6] [GeneString "abc123", StateFunc (instructionStringLength, "placeholder")] emptyState + stringTestFunc "instructionStringMakeEmpty" ["", "abc123"] [GeneString "abc123", StateFunc (instructionStringMakeEmpty, "placeholder")] emptyState + stringTestFunc "instructionStringRemoveNth" ["abc23"] [GeneString "abc123", GeneInt 3, StateFunc (instructionStringRemoveNth, "placeholder")] emptyState + stringTestFunc "instructionStringSetNth" ["abR123"] [GeneString "abc123", GeneInt 2, GeneChar 'R', StateFunc (instructionStringSetNth, "placeholder")] emptyState + stringTestFunc "instructionStringStripWhitespace" ["abc123"] [GeneString " \r \n abc123 \t", StateFunc (instructionStringStripWhitespace, "placeholder")] emptyState + stringTestFunc "instructionStringFromBoolTrue" ["True"] [GeneBool True, StateFunc (instructionStringFromBool, "placeholder")] emptyState + stringTestFunc "instructionStringFromBoolTrue" ["False"] [GeneBool False, StateFunc (instructionStringFromBool, "placeholder")] emptyState + stringTestFunc "instructionStringFromInt1000" ["1000"] [GeneInt 1000, StateFunc (instructionStringFromInt, "placeholder")] emptyState + stringTestFunc "instructionStringFromInt-1" ["-1"] [GeneInt (-1), StateFunc (instructionStringFromInt, "placeholder")] emptyState + stringTestFunc "instructionStringFromFloat3.2" ["3.2"] [GeneFloat 3.2, StateFunc (instructionStringFromFloat, "placeholder")] emptyState + stringTestFunc "instructionStringFromFloat-99.0" ["-99.0"] [GeneFloat (-99.0), StateFunc (instructionStringFromFloat, "placeholder")] emptyState + stringTestFunc "instructionStringFromChar" ["Z"] [GeneChar 'Z', StateFunc (instructionStringFromChar, "placeholder")] emptyState + stringTestFunc "instructionStringFromChar" [" "] [GeneChar ' ', StateFunc (instructionStringFromChar, "placeholder")] emptyState + + -- char instructions + stringTestFunc "instructionCharConcat" ["ab"] [GeneChar 'b', GeneChar 'a', StateFunc (instructionCharConcat, "placeholder")] emptyState + charTestFunc "instructionCharFromFirstCharSuccess" ['a'] [GeneString "abc123", StateFunc (instructionCharFromFirstChar, "placeholder")] emptyState + charTestFunc "instructionCharFromFirstCharFail" [] [GeneString "", StateFunc (instructionCharFromFirstChar, "placeholder")] emptyState + charTestFunc "instructionCharFromLastCharSuccess" ['3'] [GeneString "abc123", StateFunc (instructionCharFromLastChar, "placeholder")] emptyState + charTestFunc "instructionCharFromLastCharFail" [] [GeneString "", StateFunc (instructionCharFromLastChar, "placeholder")] emptyState + charTestFunc "instructionCharFromNthCharSuccess" ['c'] [GeneString "abc123", GeneInt 2, StateFunc (instructionCharFromNthChar, "placeholder")] emptyState + boolTestFunc "instructionCharIsWhitespaceSpace" [True] [GeneChar ' ', StateFunc (instructionCharIsWhitespace, "placeholder")] emptyState + boolTestFunc "instructionCharIsWhitespacet" [True] [GeneChar '\t', StateFunc (instructionCharIsWhitespace, "placeholder")] emptyState + boolTestFunc "instructionCharIsWhitespacer" [True] [GeneChar '\r', StateFunc (instructionCharIsWhitespace, "placeholder")] emptyState + boolTestFunc "instructionCharIsWhitespacen" [True] [GeneChar '\n', StateFunc (instructionCharIsWhitespace, "placeholder")] emptyState + boolTestFunc "instructionCharIsWhitespaceFalse" [False] [GeneChar 'a', StateFunc (instructionCharIsWhitespace, "placeholder")] emptyState + boolTestFunc "instructionCharIsLetterTrue" [True] [GeneChar 'a', StateFunc (instructionCharIsLetter, "placeholder")] emptyState + boolTestFunc "instructionCharIsLetterFalse" [False] [GeneChar '1', StateFunc (instructionCharIsLetter, "placeholder")] emptyState + boolTestFunc "instructionCharIsDigitTrue" [True] [GeneChar '1', StateFunc (instructionCharIsDigit, "placeholder")] emptyState + boolTestFunc "instructionCharIsDigitFalse" [False] [GeneChar 'a', StateFunc (instructionCharIsDigit, "placeholder")] emptyState + + -- vector int instructions + vectorIntTestFunc "instructionVectorIntConcat" [[4, 5, 6, 1, 2, 3]] [GeneVectorInt [1, 2, 3], GeneVectorInt [4, 5, 6], StateFunc (instructionVectorIntConcat, "placeholder")] emptyState + vectorIntTestFunc "instructionVectorIntConj" [[99, 1, 2, 3]] [GeneVectorInt [1, 2, 3], GeneInt 99, StateFunc (instructionVectorIntConj, "placeholder")] emptyState + vectorIntTestFunc "instructionIntTakeN" [[1, 2], [6, 7, 8]] [GeneVectorInt [6, 7, 8], GeneVectorInt [1, 2, 3], GeneInt 2, StateFunc (instructionVectorIntTakeN, "placeholder")] emptyState + vectorIntTestFunc "instructionVectorIntSubVector" [[1, 2, 3]] [GeneVectorInt [0, 1, 2, 3, 4, 5], GeneInt 3, GeneInt 1, StateFunc (instructionVectorIntSubVector, "placeholder")] emptyState + intTestFunc "instructionVectorIntFirst" [1] [GeneVectorInt [1, 2, 3, 4, 5], StateFunc (instructionVectorIntFirst, "placeholder")] emptyState + intTestFunc "instructionVectorIntLast" [5] [GeneVectorInt [1, 2, 3, 4, 5], StateFunc (instructionVectorIntLast, "placeholder")] emptyState + intTestFunc "instructionVectorIntNthInBounds" [2] [GeneVectorInt [1, 2, 3, 4, 5], GeneInt 1, StateFunc (instructionVectorIntNth, "placeholder")] emptyState + intTestFunc "instructionVectorIntNthOverflow" [2] [GeneVectorInt [1, 2, 3, 4, 5], GeneInt 6, StateFunc (instructionVectorIntNth, "placeholder")] emptyState + vectorIntTestFunc "instructionVectorIntRestFull" [[2, 3, 4, 5]] [GeneVectorInt [1, 2, 3, 4, 5], StateFunc (instructionVectorIntRest, "placeholder")] emptyState + vectorIntTestFunc "instructionVectorIntRestEmpty" [[]] [GeneVectorInt [], StateFunc (instructionVectorIntRest, "placeholder")] emptyState + vectorIntTestFunc "instructionVectorIntButLastFull" [[1, 2, 3, 4]] [GeneVectorInt [1, 2, 3, 4, 5], StateFunc (instructionVectorIntButLast, "placeholder")] emptyState + vectorIntTestFunc "instructionVectorIntButLastEmpty" [[]] [GeneVectorInt [], StateFunc (instructionVectorIntButLast, "placeholder")] emptyState + intTestFunc "instructionVectorIntLength3" [3] [GeneVectorInt [1, 2, 3], StateFunc (instructionVectorIntLength, "placeholder")] emptyState + intTestFunc "instructionVectorIntLength0" [0] [GeneVectorInt [], StateFunc (instructionVectorIntLength, "placeholder")] emptyState + vectorIntTestFunc "instructionVectorIntReverse" [[4, 3, 2, 1]] [GeneVectorInt [1, 2, 3, 4], StateFunc (instructionVectorIntReverse, "placeholder")] emptyState + intTestFunc "instructionVectorIntPushAllFull" [1, 2, 3, 4, 99] [GeneVectorInt [1, 2, 3, 4], GeneInt 99, StateFunc (instructionVectorIntPushAll, "placeholder")] emptyState + intTestFunc "instructionVectorIntPushAllEmpty" [99] [GeneVectorInt [], GeneInt 99, StateFunc (instructionVectorIntPushAll, "placeholder")] emptyState + vectorIntTestFunc "instructionVectorIntMakeEmpty" [[]] [StateFunc (instructionVectorIntMakeEmpty, "placeholder")] emptyState + boolTestFunc "instructionVectorIntIsEmptyTrue" [True] [GeneVectorInt [], StateFunc (instructionVectorIntIsEmpty, "placeholder")] emptyState + boolTestFunc "instructionVectorIntIsEmptyFalse" [False] [GeneVectorInt [1, 2, 3, 4], StateFunc (instructionVectorIntIsEmpty, "placeholder")] emptyState + intTestFunc "instructionVectorIntIndexOf1" [1] [GeneVectorInt [1, 2, 3, 4, 5], GeneInt 2, StateFunc (instructionVectorIntIndexOf, "placeholder")] emptyState + intTestFunc "instructionVectorIntIndexOfFail" [-1] [GeneVectorInt [], GeneInt 2, StateFunc (instructionVectorIntIndexOf, "placeholder")] emptyState + intTestFunc "instructionVectorIntOccurrencesOf2" [2] [GeneVectorInt [1, 2, 3, 4, 2, 6, 7], GeneInt 2, StateFunc (instructionVectorIntOccurrencesOf, "placeholder")] emptyState + intTestFunc "instructionVectorIntOccurrencesOf0" [0] [GeneVectorInt [1, 2, 3, 4, 2, 6, 7], GeneInt 0, StateFunc (instructionVectorIntOccurrencesOf, "placeholder")] emptyState + vectorIntTestFunc "instructionVectorIntSetNth3" [[0, 1, 2, 99, 4, 5]] [GeneVectorInt [0, 1, 2, 3, 4, 5], GeneInt 99, GeneInt 3, StateFunc (instructionVectorIntSetNth, "placeholder")] emptyState + vectorIntTestFunc "instructionVectorIntSetNth9" [[0, 1, 2, 99, 4, 5]] [GeneVectorInt [0, 1, 2, 3, 4, 5], GeneInt 99, GeneInt 9, StateFunc (instructionVectorIntSetNth, "placeholder")] emptyState + vectorIntTestFunc "instructionVectorIntReplace3" [[0, 1, 2, 99, 4, 5, 99, 5, 99]] [GeneInt 99, GeneInt 3, GeneVectorInt [0, 1, 2, 3, 4, 5, 3, 5, 3], StateFunc (instructionVectorIntReplace, "placeholder")] emptyState + vectorIntTestFunc "instructionVectorIntReplace-1" [[0, 1, 2, 3, 4, 5, 3, 5, 3]] [GeneInt 99, GeneInt (-1), GeneVectorInt [0, 1, 2, 3, 4, 5, 3, 5, 3], StateFunc (instructionVectorIntReplace, "placeholder")] emptyState + vectorIntTestFunc "instructionVectorIntReplaceFirst3" [[0, 1, 2, 99, 4, 5, 3, 5, 3]] [GeneInt 99, GeneInt 3, GeneVectorInt [0, 1, 2, 3, 4, 5, 3, 5, 3], StateFunc (instructionVectorIntReplaceFirst, "placeholder")] emptyState + vectorIntTestFunc "instructionVectorIntReplaceFirst-2" [[0, 1, 2, 3, 4, 5, 3, 5, 3]] [GeneInt 99, GeneInt (-2), GeneVectorInt [0, 1, 2, 3, 4, 5, 3, 5, 3], StateFunc (instructionVectorIntReplaceFirst, "placeholder")] emptyState + vectorIntTestFunc "instructionVectorIntRemove" [[0, 1, 2, 4, 5, 5]] [GeneInt 3, GeneVectorInt [0, 1, 2, 3, 4, 5, 3, 5, 3], StateFunc (instructionVectorIntRemove, "placeholder")] emptyState + intTestFunc "instructionVectorIntIterate" [66] [GeneInt 40, GeneVectorInt [0, 1, 2, 3, 4, 5, 3, 5, 3], StateFunc (instructionVectorIntIterate, "placeholder"), StateFunc (instructionIntAdd, "placeholder")] emptyState + + -- vector float functions + vectorFloatTestFunc "instructionVectorFloatConcat" [[4.0, 5.0, 6.0, 1.0, 2.0, 3.0]] [GeneVectorFloat [1.0, 2.0, 3.0], GeneVectorFloat [4.0, 5.0, 6.0], StateFunc (instructionVectorFloatConcat, "placeholder")] emptyState + vectorFloatTestFunc "instructionVectorFloatConj" [[99.0, 1.0, 2.0, 3.0]] [GeneVectorFloat [1.0, 2.0, 3.0], GeneFloat 99.0, StateFunc (instructionVectorFloatConj, "placeholder")] emptyState + vectorFloatTestFunc "instructionFloatTakeN" [[1.0, 2.0], [6.0, 7.0, 8.0]] [GeneVectorFloat [6.0, 7.0, 8.0], GeneVectorFloat [1.0, 2.0, 3.0], GeneInt 2, StateFunc (instructionVectorFloatTakeN, "placeholder")] emptyState + vectorFloatTestFunc "instructionVectorFloatSubVector" [[1.0, 2.0, 3.0]] [GeneVectorFloat [0.0, 1.0, 2.0, 3.0, 4.0, 5.0], GeneInt 3, GeneInt 1, StateFunc (instructionVectorFloatSubVector, "placeholder")] emptyState + floatTestFunc "instructionVectorFloatFirst" [1.0] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0, 5.0], StateFunc (instructionVectorFloatFirst, "placeholder")] emptyState + floatTestFunc "instructionVectorFloatLast" [5.0] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0, 5.0], StateFunc (instructionVectorFloatLast, "placeholder")] emptyState + floatTestFunc "instructionVectorFloatNthInBounds" [2.0] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0, 5.0], GeneInt 1, StateFunc (instructionVectorFloatNth, "placeholder")] emptyState + floatTestFunc "instructionVectorFloatNthOverflow" [2.0] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0, 5.0], GeneInt 6, StateFunc (instructionVectorFloatNth, "placeholder")] emptyState + vectorFloatTestFunc "instructionVectorFloatRestFull" [[2.0, 3.0, 4.0, 5.0]] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0, 5.0], StateFunc (instructionVectorFloatRest, "placeholder")] emptyState + vectorFloatTestFunc "instructionVectorFloatRestEmpty" [[]] [GeneVectorFloat [], StateFunc (instructionVectorFloatRest, "placeholder")] emptyState + vectorFloatTestFunc "instructionVectorFloatButLastFull" [[1.0, 2.0, 3.0, 4.0]] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0, 5.0], StateFunc (instructionVectorFloatButLast, "placeholder")] emptyState + vectorFloatTestFunc "instructionVectorFloatButLastEmpty" [[]] [GeneVectorFloat [], StateFunc (instructionVectorFloatButLast, "placeholder")] emptyState + intTestFunc "instructionVectorFloatLength3" [3] [GeneVectorFloat [1.0, 2.0, 3.0], StateFunc (instructionVectorFloatLength, "placeholder")] emptyState + intTestFunc "instructionVectorFloatLength0" [0] [GeneVectorFloat [], StateFunc (instructionVectorFloatLength, "placeholder")] emptyState + vectorFloatTestFunc "instructionVectorFloatReverse" [[4.0, 3.0, 2.0, 1.0]] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0], StateFunc (instructionVectorFloatReverse, "placeholder")] emptyState + floatTestFunc "instructionVectorFloatPushAllFull" [1.0, 2.0, 3.0, 4.0, 99.0] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0], GeneFloat 99.0, StateFunc (instructionVectorFloatPushAll, "placeholder")] emptyState + floatTestFunc "instructionVectorFloatPushAllEmpty" [99.0] [GeneVectorFloat [], GeneFloat 99.0, StateFunc (instructionVectorFloatPushAll, "placeholder")] emptyState + vectorFloatTestFunc "instructionVectorFloatMakeEmpty" [[]] [StateFunc (instructionVectorFloatMakeEmpty, "placeholder")] emptyState + boolTestFunc "instructionVectorFloatIsEmptyTrue" [True] [GeneVectorFloat [], StateFunc (instructionVectorFloatIsEmpty, "placeholder")] emptyState + boolTestFunc "instructionVectorFloatIsEmptyFalse" [False] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0], StateFunc (instructionVectorFloatIsEmpty, "placeholder")] emptyState + intTestFunc "instructionVectorFloatIndexOf1" [1] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0, 5.0], GeneFloat 2.0, StateFunc (instructionVectorFloatIndexOf, "placeholder")] emptyState + intTestFunc "instructionVectorFloatIndexOfFail" [-1] [GeneVectorFloat [], GeneFloat 2.0, StateFunc (instructionVectorFloatIndexOf, "placeholder")] emptyState + intTestFunc "instructionVectorFloatOccurrencesOf2" [2] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0, 2.0, 6.0, 7.0], GeneFloat 2.0, StateFunc (instructionVectorFloatOccurrencesOf, "placeholder")] emptyState + intTestFunc "instructionVectorFloatOccurrencesOf0" [0] [GeneVectorFloat [1.0, 2.0, 3.0, 4.0, 2.0, 6.0, 7.0], GeneFloat 0.0, StateFunc (instructionVectorFloatOccurrencesOf, "placeholder")] emptyState + vectorFloatTestFunc "instructionVectorFloatSetNth3" [[0.0, 1.0, 2.0, 99.0, 4.0, 5.0]] [GeneVectorFloat [0.0, 1.0, 2.0, 3.0, 4.0, 5.0], GeneFloat 99.0, GeneInt 3, StateFunc (instructionVectorFloatSetNth, "placeholder")] emptyState + vectorFloatTestFunc "instructionVectorFloatSetNth9" [[0.0, 1.0, 2.0, 99.0, 4.0, 5.0]] [GeneVectorFloat [0.0, 1.0, 2.0, 3.0, 4.0, 5.0], GeneFloat 99.0, GeneInt 9, StateFunc (instructionVectorFloatSetNth, "placeholder")] emptyState + vectorFloatTestFunc "instructionVectorFloatReplace3" [[0.0, 1.0, 2.0, 99.0, 4.0, 5.0, 99.0, 5.0, 99.0]] [GeneFloat 99.0, GeneFloat 3.0, GeneVectorFloat [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 3.0, 5.0, 3.0], StateFunc (instructionVectorFloatReplace, "placeholder")] emptyState + vectorFloatTestFunc "instructionVectorFloatReplace-1" [[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 3.0, 5.0, 3.0]] [GeneFloat 99.0, GeneFloat (-1.0), GeneVectorFloat [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 3.0, 5.0, 3.0], StateFunc (instructionVectorFloatReplace, "placeholder")] emptyState + vectorFloatTestFunc "instructionVectorFloatReplaceFirst3" [[0.0, 1.0, 2.0, 99.0, 4.0, 5.0, 3.0, 5.0, 3.0]] [GeneFloat 99.0, GeneFloat 3.0, GeneVectorFloat [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 3.0, 5.0, 3.0], StateFunc (instructionVectorFloatReplaceFirst, "placeholder")] emptyState + vectorFloatTestFunc "instructionVectorFloatReplaceFirst-2" [[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 3.0, 5.0, 3.0]] [GeneFloat 99.0, GeneFloat (-2.0), GeneVectorFloat [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 3.0, 5.0, 3.0], StateFunc (instructionVectorFloatReplaceFirst, "placeholder")] emptyState + vectorFloatTestFunc "instructionVectorFloatRemove" [[0.0, 1.0, 2.0, 4.0, 5.0, 5.0]] [GeneFloat 3, GeneVectorFloat [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 3.0, 5.0, 3.0], StateFunc (instructionVectorFloatRemove, "placeholder")] emptyState + floatTestFunc "instructionVectorFloatIterate" [66.0] [GeneFloat 40.0, GeneVectorFloat [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 3.0, 5.0, 3.0], StateFunc (instructionVectorFloatIterate, "placeholder"), StateFunc (instructionFloatAdd, "placeholder")] emptyState From 12b8cb56a728491e8e2af11fbe5e98b800336da3 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Tue, 4 Feb 2025 03:36:33 -0600 Subject: [PATCH 113/171] formatting and generic tests --- src/Instructions/GenericInstructions.hs | 5 +- src/Push.hs | 3 +- src/PushTests.hs | 6 +- src/PushTests/GenericTests.hs | 88 +++++++++++++++++++++---- src/PushTests/IntTests.hs | 77 +++++++++++++++------- src/State.hs | 10 +-- test/Main.hs | 13 ++-- 7 files changed, 145 insertions(+), 57 deletions(-) diff --git a/src/Instructions/GenericInstructions.hs b/src/Instructions/GenericInstructions.hs index aac7bbc..5189d5b 100644 --- a/src/Instructions/GenericInstructions.hs +++ b/src/Instructions/GenericInstructions.hs @@ -100,12 +100,13 @@ instructionIsEmpty state@(State {_bool = bs}) accessor = state{_bool = null (vie -- I might be able to move some of the int stack error checking -- to the integer call. For now this may be a tad inefficient. -instructionDupN :: forall a. State -> Lens' State [a] -> State +instructionDupN :: forall a. Show a => State -> Lens' State [a] -> State instructionDupN state accessor = case uncons (view int state) of Just (i1,is) -> case uncons (view accessor state{_int = is}) of - Just (a1,as) -> instructionDupNHelper i1 a1 accessor (state{_int = is} & accessor .~ as) + Just (a1,as) -> + instructionDupNHelper i1 a1 accessor (state{_int = is} & accessor .~ as) _ -> state _ -> state where diff --git a/src/Push.hs b/src/Push.hs index 3e0e1f1..44c6bc8 100644 --- a/src/Push.hs +++ b/src/Push.hs @@ -2,7 +2,6 @@ module Push where import Control.Lens import Data.Map qualified as Map - import State -- import Debug.Trace (trace, traceStack) @@ -67,7 +66,7 @@ interpretExec state@(State {_exec = e : es}) = interpretExec state = state -- interpretOneStep :: State -> State --- interpretOneStep state@(State {_exec = e : es}) = +-- interpretOneStep state@(State {_exec = e : es}) = -- case e of -- (GeneInt val) -> state & exec .~ es & int .~ val : view int state -- (GeneFloat val) -> state & exec .~ es & float .~ val : view float state diff --git a/src/PushTests.hs b/src/PushTests.hs index c0db3b0..eca2dc5 100644 --- a/src/PushTests.hs +++ b/src/PushTests.hs @@ -1,6 +1,6 @@ -module PushTests - ( module PushTests.GenericTests - , module PushTests.IntTests +module PushTests + ( module PushTests.GenericTests, + module PushTests.IntTests, ) where diff --git a/src/PushTests/GenericTests.hs b/src/PushTests/GenericTests.hs index a983b94..2ec8820 100644 --- a/src/PushTests/GenericTests.hs +++ b/src/PushTests/GenericTests.hs @@ -3,24 +3,84 @@ module PushTests.GenericTests where import State import Control.Lens import Debug.Trace +import Test.QuickCheck -arithmeticTest :: (Num a, Eq a) => Lens' State [a] -> (State -> State) -> (a -> a -> a) -> State -> Bool -arithmeticTest accessor instruction func state = +-- The naming scheme: +-- the letters at the beginning represent what kind of transformation (the word I'm using for a basic function) to the states is happening +-- for example: the function aaa1Test relays this arg takes a transformation of two as and turns them into one a +-- the numbers represent how many different stacks are used in the function. +-- for example: the aaa1Test relays that it takes one stack as input. These stacks are passed in as Lens + +-- We may be able to get rid of Lens entirely and use haskell's integrated accessors of type State -> [a] +-- You can see what I'm talking about if you go into ghci and type: `:info _int` for example + +aaa1Test :: (Show a, Eq a) => Lens' State [a] -> (State -> State) -> (a -> a -> a) -> State -> Property +aaa1Test accessor instruction transformation state = case (uncons (view accessor state), uncons (view accessor $ instruction state)) of - (Just (origx1, origx2 : _), Just (modx1, _)) -> func origx2 origx1 == modx1 && length (view accessor state) == length (view accessor $ instruction state) + 1 - _ -> state == instruction state + (Just (origx1, origx2 : _), Just (modx1, _)) -> transformation origx2 origx1 === modx1 .&&. length (view accessor state) === length (view accessor $ instruction state) + 1 + _ -> state === instruction state -unaryTest :: (Num a, Eq a) => Lens' State [a] -> (State -> State) -> (a -> a) -> State -> Bool -unaryTest accessor instruction func state = +aa1Test :: (Show a, Eq a) => Lens' State [a] -> (State -> State) -> (a -> a) -> State -> Property +aa1Test accessor instruction transformation state = case (uncons (view accessor state), uncons (view accessor $ instruction state)) of - (Just (origx1, _), Just (modx1, _)) -> func origx1 == modx1 && length (view accessor state) == length (view accessor $ instruction state) - _ -> state == instruction state + (Just (origx1, _), Just (modx1, _)) -> transformation origx1 === modx1 .&&. length (view accessor state) === length (view accessor $ instruction state) + _ -> state === instruction state -typeFromType :: Eq b => Lens' State [a] -> Lens' State [b] -> (State -> State) -> (a -> b) -> State -> Bool -typeFromType accessorFrom accessorTo instruction transformation state = +ab1Test :: (Show b, Eq b) => Lens' State [a] -> Lens' State [b] -> (State -> State) -> (a -> b) -> State -> Property +ab1Test accessorFrom accessorTo instruction transformation state = case (uncons (view accessorTo $ instruction state), uncons (view accessorFrom state)) of (Just (t1, _), Just (f1, _)) -> - t1 == transformation f1 && - length (view accessorTo $ instruction state) == length (view accessorTo state) + 1 && - length (view accessorFrom $ instruction state) == length (view accessorFrom state) - 1 - _ -> state == instruction state + t1 === transformation f1 .&&. + length (view accessorTo $ instruction state) === length (view accessorTo state) + 1 .&&. + length (view accessorFrom $ instruction state) === length (view accessorFrom state) - 1 + _ -> state === instruction state + +aab2Test :: (Show b, Eq b) => Lens' State [a] -> Lens' State [b] -> (State -> State) -> (a -> a -> b) -> State -> Property +aab2Test accessorFrom accessorTo instruction transformation state = + case (uncons (view accessorTo $ instruction state), uncons (view accessorFrom state)) of + (Just (t1, _), Just (f1, f2 : _)) -> + t1 === transformation f1 f2 .&&. + length (view accessorTo $ instruction state) == length (view accessorTo state) + 1 .&&. + length (view accessorFrom $ instruction state) == length (view accessorFrom state) - 2 + _ -> state === instruction state + +popTest :: (Show a) => Lens' State [a] -> (State -> State) -> State -> Property +popTest accessor instruction state = + if null $ view accessor state + then state === instruction state + else length (view accessor $ instruction state) === length (view accessor state) - 1 + +dupTest :: (Eq a, Show a) => Lens' State [a] -> (State -> State) -> State -> Property +dupTest accessor instruction state = + case uncons (view accessor state) of + Just (origx1, _) -> + case uncons (view accessor $ instruction state) of + Just (modx1, modx2 : _) -> + origx1 === modx1 .&&. origx1 === modx2 .&&. length (view accessor $ instruction state) === length (view accessor state) + 1 + _ -> state === instruction state + _ -> state === instruction state + +-- How to test the int stack in particular? +dupTestN :: (Eq a, Show a) => Lens' State [a] -> (State -> State) -> State -> Property +dupTestN accessor instruction state = + case uncons (view int state) of + Just (i1, is) -> + let amt = max i1 0 in + case uncons (view accessor state{_int = is}) of + Just (origx1, _) -> + conjoin (map (origx1 ===) (take amt (view accessor $ instruction state))) .&&. + length (view accessor $ instruction state) === (length (view accessor state{_int = is}) + amt - 1) + _ -> state === instruction state + _ -> state === instruction state + +swapTest :: (Show a, Eq a) => Lens' State [a] -> (State -> State) -> State -> Property +swapTest accessor instruction state = + case (uncons (view accessor state), uncons (view accessor $ instruction state)) of + (Just (origx1, origx2 : _), Just (modx1, modx2 : _)) -> origx1 === modx2 .&&. origx2 === modx1 + _ -> state === instruction state + +rotTest :: (Show a, Eq a) => Lens' State [a] -> (State -> State) -> State -> Property +rotTest accessor instruction state = + case (uncons (view accessor state), uncons (view accessor $ instruction state)) of + (Just (origx1, origx2 : origx3 : _), Just (modx1, modx2 : modx3 : _)) -> (origx1, origx2, origx3) === (modx2, modx3, modx1) + _ -> state === instruction state diff --git a/src/PushTests/IntTests.hs b/src/PushTests/IntTests.hs index 0293a5d..f0249dd 100644 --- a/src/PushTests/IntTests.hs +++ b/src/PushTests/IntTests.hs @@ -3,40 +3,67 @@ module PushTests.IntTests where import State import Instructions.IntInstructions import PushTests.GenericTests -import Data.List import Control.Lens hiding (uncons) +import Test.QuickCheck -prop_IntAdd :: State -> Bool -prop_IntAdd = arithmeticTest int instructionIntAdd (+) +prop_IntAdd :: State -> Property +prop_IntAdd = aaa1Test int instructionIntAdd (+) -prop_IntSub :: State -> Bool -prop_IntSub = arithmeticTest int instructionIntSub (-) +prop_IntSub :: State -> Property +prop_IntSub = aaa1Test int instructionIntSub (-) -prop_IntMul :: State -> Bool -prop_IntMul = arithmeticTest int instructionIntMul (*) +prop_IntMul :: State -> Property +prop_IntMul = aaa1Test int instructionIntMul (*) -prop_IntDiv :: State -> Bool -prop_IntDiv state@(State {_int = 0 : _}) = state == instructionIntDiv state -prop_IntDiv state = arithmeticTest int instructionIntDiv div state +prop_IntDiv :: State -> Property +prop_IntDiv state@(State {_int = 0 : _}) = state === instructionIntDiv state +prop_IntDiv state = aaa1Test int instructionIntDiv div state -prop_IntMod :: State -> Bool -prop_IntMod state@(State {_int = 0 : _}) = state == instructionIntMod state -prop_IntMod state = arithmeticTest int instructionIntMod mod state +prop_IntMod :: State -> Property +prop_IntMod state@(State {_int = 0 : _}) = state === instructionIntMod state +prop_IntMod state = aaa1Test int instructionIntMod mod state -prop_IntFromFloat :: State -> Bool -prop_IntFromFloat = typeFromType float int instructionIntFromFloat floor +prop_IntFromFloat :: State -> Property +prop_IntFromFloat = ab1Test float int instructionIntFromFloat floor -prop_IntFromBool :: State -> Bool -prop_IntFromBool = typeFromType bool int instructionIntFromBool (\x -> if x then 1 else 0) +prop_IntFromProperty :: State -> Property +prop_IntFromProperty = ab1Test bool int instructionIntFromBool (\x -> if x then 1 else 0) -prop_IntMin :: State -> Bool -prop_IntMin = arithmeticTest int instructionIntMin min +prop_IntMin :: State -> Property +prop_IntMin = aaa1Test int instructionIntMin min -prop_IntMax :: State -> Bool -prop_IntMax = arithmeticTest int instructionIntMax max +prop_IntMax :: State -> Property +prop_IntMax = aaa1Test int instructionIntMax max -prop_IntInc :: State -> Bool -prop_IntInc = unaryTest int instructionIntInc (+1) +prop_IntInc :: State -> Property +prop_IntInc = aa1Test int instructionIntInc (+1) -prop_IntDec :: State -> Bool -prop_IntDec = unaryTest int instructionIntDec (\x -> x - 1) +prop_IntDec :: State -> Property +prop_IntDec = aa1Test int instructionIntDec (\x -> x - 1) + +prop_IntLT :: State -> Property +prop_IntLT = aab2Test int bool instructionIntLT (<) + +prop_IntGT :: State -> Property +prop_IntGT = aab2Test int bool instructionIntGT (>) + +prop_IntLTE :: State -> Property +prop_IntLTE = aab2Test int bool instructionIntLTE (<=) + +prop_IntGTE :: State -> Property +prop_IntGTE = aab2Test int bool instructionIntGTE (>=) + +prop_IntDup :: State -> Property +prop_IntDup = dupTest int instructionIntDup + +prop_IntPop :: State -> Property +prop_IntPop = popTest int instructionIntPop + +prop_IntDupN :: State -> Property +prop_IntDupN = dupTestN int instructionIntDupN + +prop_IntSwap :: State -> Property +prop_IntSwap = swapTest int instructionIntSwap + +prop_IntRot :: State -> Property +prop_IntRot = rotTest int instructionIntRot diff --git a/src/State.hs b/src/State.hs index df6737a..cfd4071 100644 --- a/src/State.hs +++ b/src/State.hs @@ -1,11 +1,12 @@ -{-# LANGUAGE TemplateHaskell, DeriveGeneric #-} +{-# LANGUAGE DeriveGeneric #-} +{-# LANGUAGE TemplateHaskell #-} module State where import Control.Lens hiding (elements) import Data.Map qualified as Map -import Test.QuickCheck import GHC.Generics +import Test.QuickCheck -- The exec stack must store heterogenous types, -- and we must be able to detect that type at runtime. @@ -26,7 +27,7 @@ data Gene | PlaceInput String | Close | Block [Gene] - deriving Generic + deriving (Generic) instance Eq Gene where GeneInt x == GeneInt y = x == y @@ -61,7 +62,6 @@ instance Show Gene where show Close = "Close" show (Block xs) = "Block: " <> show xs - instance CoArbitrary Gene instance Arbitrary Gene where @@ -83,7 +83,6 @@ instance Arbitrary Gene where return Close ] - data State = State { _exec :: [Gene], _code :: [Gene], @@ -119,6 +118,7 @@ instance Arbitrary State where arbParameter <- arbitrary -- arbInput <- arbitrary State arbExec arbCode arbInt arbFloat arbBool arbString arbChar arbVectorInt arbVectorFloat arbVectorBool arbVectorString arbVectorChar arbParameter <$> arbitrary + -- Thanks hlint lol instance CoArbitrary State diff --git a/test/Main.hs b/test/Main.hs index cadba10..e923449 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -1,20 +1,21 @@ import Instructions import Push +import PushTests import State import Test.QuickCheck -import PushTests + -- import Data.List --- import Control.Lens +-- import Control.Lens -- import Debug.Trace pushTestArgs :: Args -pushTestArgs = stdArgs{maxSize = 10} - +pushTestArgs = stdArgs {maxSize = 10} + -- These two used for ghci testing -- For example (in ghci): qcw prop_myTest -qcw :: Testable a => a -> IO () +qcw :: (Testable a) => a -> IO () qcw = quickCheckWith pushTestArgs -vcw :: Testable a => a -> IO () +vcw :: (Testable a) => a -> IO () vcw = verboseCheckWith pushTestArgs From 58cb593cffd5c046f44b0cc396071063dc66d71b Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Tue, 4 Feb 2025 14:43:08 -0600 Subject: [PATCH 114/171] more gp notes --- TODO.md | 1 + 1 file changed, 1 insertion(+) diff --git a/TODO.md b/TODO.md index de4ec89..378e98f 100644 --- a/TODO.md +++ b/TODO.md @@ -10,3 +10,4 @@ ## PushGP TODO - [ ] Implement a Plushy genome translator + - [ ] Need to make this reproducable too (Check pysh json files) From e40ef0ce62fa115024a7508e278662a6174acfb7 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Wed, 5 Feb 2025 01:03:24 -0600 Subject: [PATCH 115/171] more tests and many more togo --- HushGP.cabal | 1 + src/PushTests.hs | 2 ++ src/PushTests/GenericTests.hs | 43 +++++++++++++++++++++++++++++++++++ src/PushTests/IntTests.hs | 17 +++++++++++++- src/PushTests/UtilTests.hs | 36 +++++++++++++++++++++++++++++ 5 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 src/PushTests/UtilTests.hs diff --git a/HushGP.cabal b/HushGP.cabal index 3a391a2..f24e378 100644 --- a/HushGP.cabal +++ b/HushGP.cabal @@ -55,6 +55,7 @@ library , PushTests , PushTests.IntTests , PushTests.GenericTests + , PushTests.UtilTests -- Modules included in this library but not exported. -- other-modules: diff --git a/src/PushTests.hs b/src/PushTests.hs index eca2dc5..571b27f 100644 --- a/src/PushTests.hs +++ b/src/PushTests.hs @@ -1,8 +1,10 @@ module PushTests ( module PushTests.GenericTests, module PushTests.IntTests, + module PushTests.UtilTests, ) where import PushTests.GenericTests import PushTests.IntTests +import PushTests.UtilTests diff --git a/src/PushTests/GenericTests.hs b/src/PushTests/GenericTests.hs index 2ec8820..5a8dded 100644 --- a/src/PushTests/GenericTests.hs +++ b/src/PushTests/GenericTests.hs @@ -4,6 +4,7 @@ import State import Control.Lens import Debug.Trace import Test.QuickCheck +import Instructions.GenericInstructions -- The naming scheme: -- the letters at the beginning represent what kind of transformation (the word I'm using for a basic function) to the states is happening @@ -84,3 +85,45 @@ rotTest accessor instruction state = case (uncons (view accessor state), uncons (view accessor $ instruction state)) of (Just (origx1, origx2 : origx3 : _), Just (modx1, modx2 : modx3 : _)) -> (origx1, origx2, origx3) === (modx2, modx3, modx1) _ -> state === instruction state + +flushTest :: (Show a, Eq a) => Lens' State [a] -> (State -> State) -> State -> Property +flushTest accessor instruction state = + property $ null $ view accessor $ instruction state + +stackDepthTest :: (Show a) => Lens' State [a] -> (State -> State) -> State -> Property +stackDepthTest accessor instruction state = + case uncons (view int $ instruction state) of + Just (x1, _) -> x1 === length (view accessor state) + _ -> state === instruction state + +yankTest :: forall a. (Show a, Eq a) => Lens' State [a] -> (State -> State) -> State -> Property +yankTest accessor instruction state@(State {_int = i1 : is}) = + let + myIndex :: Int + myIndex = max 0 (min i1 (length (view accessor state{_int = is}) - 1)) + item :: a + item = view accessor state{_int = is} !! myIndex + in + case (uncons (view accessor $ instruction state), uncons is) of + (Just (x1, _), Just (_, _)) -> x1 === item + _ -> state === instruction state + -- .&&. -- unsure how to get this functional + -- length (view accessor state{_int = is}) === length (view accessor $ instruction state) +yankTest _ instruction state = state === instruction state + +-- Might just make this a unit test +-- Come back to this later +-- yankDupTest :: forall a. (Show a, Eq a) => Lens' State [a] -> (State -> State) -> State -> Property +-- yankDupTest accessor instruction state@(State {_int = i1 : is}) = +-- let +-- myIndex :: Int +-- myIndex = max 0 (min i1 (length (view accessor state{_int = is}) - 1)) +-- item :: a +-- item = view accessor state{_int = is} !! myIndex +-- in +-- case (uncons (view accessor $ instruction state), uncons is) of +-- (Just (x1, xs), Just (_, _)) -> x1 === item .&&. (x1 : xs) !! === item +-- _ -> state === instruction state +-- yankDupTest _ instruction state = state === instruction state + +-- shoveTest diff --git a/src/PushTests/IntTests.hs b/src/PushTests/IntTests.hs index f0249dd..07432f1 100644 --- a/src/PushTests/IntTests.hs +++ b/src/PushTests/IntTests.hs @@ -3,7 +3,7 @@ module PushTests.IntTests where import State import Instructions.IntInstructions import PushTests.GenericTests -import Control.Lens hiding (uncons) +-- import Control.Lens hiding (uncons) import Test.QuickCheck prop_IntAdd :: State -> Property @@ -67,3 +67,18 @@ prop_IntSwap = swapTest int instructionIntSwap prop_IntRot :: State -> Property prop_IntRot = rotTest int instructionIntRot + +prop_IntFlush :: State -> Property +prop_IntFlush = flushTest int instructionIntFlush + +prop_IntEq :: State -> Property +prop_IntEq = aab2Test int bool instructionIntEq (==) + +prop_IntStackDepth :: State -> Property +prop_IntStackDepth = stackDepthTest int instructionIntStackDepth + +prop_IntYank :: State -> Property +prop_IntYank = yankTest int instructionIntYank + +-- prop_IntYankDup :: State -> Property +-- prop_IntYankDup = yankDupTest int instructionIntYankDup diff --git a/src/PushTests/UtilTests.hs b/src/PushTests/UtilTests.hs new file mode 100644 index 0000000..07b49da --- /dev/null +++ b/src/PushTests/UtilTests.hs @@ -0,0 +1,36 @@ +module PushTests.UtilTests where + +import Instructions.GenericInstructions +import Test.QuickCheck + +prop_DeleteAtTest :: Int -> [Int] -> Property +prop_DeleteAtTest idx lst = + idx >= 0 && idx < length lst ==> + if null lst + then length lst === length (deleteAt idx lst) + else length lst === length (deleteAt idx lst) + 1 + +prop_CombineTupleTest :: Int -> ([Int], [Int]) -> Property +prop_CombineTupleTest val tup = + length (fst tup) + length (snd tup) === length (combineTuple val tup) - 1 + +prop_CombineTupleListTest :: [Int] -> ([Int], [Int]) -> Property +prop_CombineTupleListTest lst tup = + length (fst tup) + length (snd tup) === length (combineTupleList lst tup) - length lst + +-- Could use forAll to only generate valid tests +prop_InsertAt :: Int -> Int -> [Int] -> Property +prop_InsertAt idx val lst = + idx >= 0 && idx < length lst ==> + length lst === length (insertAt idx val lst) - 1 .&&. + insertAt idx val lst !! idx === val + +prop_ReplaceAt :: Int -> Int -> [Int] -> Property +prop_ReplaceAt idx val lst = + idx >= 0 && idx < length lst ==> + length lst === length (replaceAt idx val lst) .&&. + replaceAt idx val lst !! idx === val + +-- prop_SubList :: Int -> Int -> [Int] -> Property +-- prop_SubList idx0 idx1 lst = + -- idx From add949ed05f83f3a5573c65d58f69f9c61cf0e5d Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Wed, 5 Feb 2025 01:05:51 -0600 Subject: [PATCH 116/171] modify Eq function --- src/Instructions/GenericInstructions.hs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Instructions/GenericInstructions.hs b/src/Instructions/GenericInstructions.hs index 5189d5b..8a60233 100644 --- a/src/Instructions/GenericInstructions.hs +++ b/src/Instructions/GenericInstructions.hs @@ -8,6 +8,7 @@ import State deleteAt :: Int -> [a] -> [a] deleteAt idx xs = take idx xs <> drop 1 (drop idx xs) +-- I could probably just combine these functions combineTuple :: a -> ([a], [a]) -> [a] combineTuple val tup = fst tup <> [val] <> snd tup @@ -140,13 +141,13 @@ instructionFlush state accessor = state & accessor .~ [] instructionEq :: forall a. Eq a => State -> Lens' State [a] -> State instructionEq state accessor = - case uncons stackTop of + case uncons $ view accessor state of Nothing -> state - Just (x1, x2 : _) -> state & bool .~ (x1 == x2) : view bool state & accessor .~ drop 2 (view accessor state) + Just (x1, x2 : _) -> droppedState & bool .~ (x1 == x2) : view bool droppedState Just _ -> state where - stackTop :: [a] - stackTop = take 2 $ view accessor state + droppedState :: State + droppedState = state & accessor .~ drop 2 (view accessor state) instructionStackDepth :: State -> Lens' State [a] -> State instructionStackDepth state@(State {_int = is}) accessor = state{_int = length (view accessor state) : is} From 194c025486059cc02106a8751f6ad30563a14f4d Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Wed, 5 Feb 2025 23:46:37 -0600 Subject: [PATCH 117/171] the start to implementing plushy interpretation --- src/Instructions.hs | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/src/Instructions.hs b/src/Instructions.hs index 6d54438..dea6e53 100644 --- a/src/Instructions.hs +++ b/src/Instructions.hs @@ -28,6 +28,34 @@ import Instructions.VectorFloatInstructions import Instructions.VectorIntInstructions import Instructions.VectorLogicalInstructions import Instructions.VectorStringInstructions +import State --- Will eventually add a list of all instrucitons in this file --- Use template haskell for this? +intInstructions :: [Gene] +intInstructions = [ + StateFunc (instructionIntFromFloat, "instructionIntFromFloat"), + StateFunc (instructionIntFromBool, "instructionIntFromBool"), + StateFunc (instructionIntAdd, "instructionIntAdd"), + StateFunc (instructionIntSub, "instructionIntSub"), + StateFunc (instructionIntMul, "instructionIntMul"), + StateFunc (instructionIntDiv, "instructionIntDiv"), + StateFunc (instructionIntMod, "instructionIntMod"), + StateFunc (instructionIntMin, "instructionIntMin"), + StateFunc (instructionIntMax, "instructionIntMax"), + StateFunc (instructionIntInc, "instructionIntInc"), + StateFunc (instructionIntDec, "instructionIntDec"), + StateFunc (instructionIntLT, "instructionIntLT"), + StateFunc (instructionIntGT, "instructionIntGT"), + StateFunc (instructionIntLTE, "instructionIntLTE"), + StateFunc (instructionIntGTE, "instructionIntGTE"), + StateFunc (instructionIntDup, "instructionIntDup"), + StateFunc (instructionIntPop, "instructionIntPop"), + StateFunc (instructionIntDupN, "instructionIntDupN"), + StateFunc (instructionIntSwap, "instructionIntSwap"), + StateFunc (instructionIntRot, "instructionIntRot"), + StateFunc (instructionIntFlush, "instructionIntFlush"), + StateFunc (instructionIntEq, "instructionIntEq"), + StateFunc (instructionIntYank, "instructionIntYank"), + StateFunc (instructionIntYankDup, "instructionIntYankDup"), + StateFunc (instructionIntShove, "instructionIntShove"), + StateFunc (instructionIntIsEmpty, "instructionIntIsEmpty") + ] From 2404e7e5e14dc5d628d6abd4a0120c2c46376343 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Thu, 6 Feb 2025 15:49:06 -0600 Subject: [PATCH 118/171] instruction list, comment cleanup, todos --- TODO.md | 5 +- src/Instructions.hs | 499 +++++++++++++++++++++++-- src/Instructions/CodeInstructions.hs | 6 - src/Instructions/StringInstructions.hs | 1 - 4 files changed, 475 insertions(+), 36 deletions(-) diff --git a/TODO.md b/TODO.md index 378e98f..f3b61eb 100644 --- a/TODO.md +++ b/TODO.md @@ -4,8 +4,11 @@ - [ ] Make all vector functions applicable to string functions and vice versa - [ ] Implement all functions as seen in propeller +- [ ] Implement all functions as seen in the specification - [ ] Implement Linear Algebra functions as specified in the previous papers -- [ ] Add a function to sort a vector +- [ ] Add a function to sort a vector forward and backwards +- [ ] Disambiguate isEmpty and stackIsEmpty +- [ ] Rename Logical to Bool - [x] Make int yank, shove, yankdup, and shovedup generic ## PushGP TODO diff --git a/src/Instructions.hs b/src/Instructions.hs index dea6e53..c9d3e46 100644 --- a/src/Instructions.hs +++ b/src/Instructions.hs @@ -12,6 +12,19 @@ module Instructions module Instructions.VectorStringInstructions, module Instructions.VectorLogicalInstructions, module Instructions.VectorCharInstructions, + allIntInstructions, + allFloatInstructions, + allBoolInstructions, + allCharInstructions, + allCodeInstructions, + allExecInstructions, + allStringInstructions, + allVectorIntInstructions, + allVectorFloatInstructions, + allVectorCharInstructions, + allVectorStringInstructions, + allVectorBoolInstructions, + allInstructions ) where @@ -30,32 +43,462 @@ import Instructions.VectorLogicalInstructions import Instructions.VectorStringInstructions import State -intInstructions :: [Gene] -intInstructions = [ - StateFunc (instructionIntFromFloat, "instructionIntFromFloat"), - StateFunc (instructionIntFromBool, "instructionIntFromBool"), - StateFunc (instructionIntAdd, "instructionIntAdd"), - StateFunc (instructionIntSub, "instructionIntSub"), - StateFunc (instructionIntMul, "instructionIntMul"), - StateFunc (instructionIntDiv, "instructionIntDiv"), - StateFunc (instructionIntMod, "instructionIntMod"), - StateFunc (instructionIntMin, "instructionIntMin"), - StateFunc (instructionIntMax, "instructionIntMax"), - StateFunc (instructionIntInc, "instructionIntInc"), - StateFunc (instructionIntDec, "instructionIntDec"), - StateFunc (instructionIntLT, "instructionIntLT"), - StateFunc (instructionIntGT, "instructionIntGT"), - StateFunc (instructionIntLTE, "instructionIntLTE"), - StateFunc (instructionIntGTE, "instructionIntGTE"), - StateFunc (instructionIntDup, "instructionIntDup"), - StateFunc (instructionIntPop, "instructionIntPop"), - StateFunc (instructionIntDupN, "instructionIntDupN"), - StateFunc (instructionIntSwap, "instructionIntSwap"), - StateFunc (instructionIntRot, "instructionIntRot"), - StateFunc (instructionIntFlush, "instructionIntFlush"), - StateFunc (instructionIntEq, "instructionIntEq"), - StateFunc (instructionIntYank, "instructionIntYank"), - StateFunc (instructionIntYankDup, "instructionIntYankDup"), - StateFunc (instructionIntShove, "instructionIntShove"), - StateFunc (instructionIntIsEmpty, "instructionIntIsEmpty") +allIntInstructions :: [Gene] +allIntInstructions = map StateFunc [ + (instructionIntFromFloat, "instructionIntFromFloat"), + (instructionIntFromBool, "instructionIntFromBool"), + (instructionIntAdd, "instructionIntAdd"), + (instructionIntSub, "instructionIntSub"), + (instructionIntMul, "instructionIntMul"), + (instructionIntDiv, "instructionIntDiv"), + (instructionIntMod, "instructionIntMod"), + (instructionIntMin, "instructionIntMin"), + (instructionIntMax, "instructionIntMax"), + (instructionIntInc, "instructionIntInc"), + (instructionIntDec, "instructionIntDec"), + (instructionIntLT, "instructionIntLT"), + (instructionIntGT, "instructionIntGT"), + (instructionIntLTE, "instructionIntLTE"), + (instructionIntGTE, "instructionIntGTE"), + (instructionIntDup, "instructionIntDup"), + (instructionIntPop, "instructionIntPop"), + (instructionIntDupN, "instructionIntDupN"), + (instructionIntSwap, "instructionIntSwap"), + (instructionIntRot, "instructionIntRot"), + (instructionIntFlush, "instructionIntFlush"), + (instructionIntEq, "instructionIntEq"), + (instructionIntYank, "instructionIntYank"), + (instructionIntYankDup, "instructionIntYankDup"), + (instructionIntShove, "instructionIntShove"), + (instructionIntIsEmpty, "instructionIntIsEmpty") ] + +allFloatInstructions :: [Gene] +allFloatInstructions = map StateFunc [ + (instructionFloatFromInt, "instructionFloatFromInt"), + (instructionFloatFromBool, "instructionFloatFromBool"), + (instructionFloatAdd, "instructionFloatAdd"), + (instructionFloatSub, "instructionFloatSub"), + (instructionFloatMul, "instructionFloatMul"), + (instructionFloatDiv, "instructionFloatDiv"), + (instructionFloatMod, "instructionFloatMod"), + (instructionFloatMin, "instructionFloatMin"), + (instructionFloatMax, "instructionFloatMax"), + (instructionFloatInc, "instructionFloatInc"), + (instructionFloatDec, "instructionFloatDec"), + (instructionFloatLT, "instructionFloatLT"), + (instructionFloatGT, "instructionFloatGT"), + (instructionFloatLTE, "instructionFloatLTE"), + (instructionFloatGTE, "instructionFloatGTE"), + (instructionFloatDup, "instructionFloatDup"), + (instructionFloatPop, "instructionFloatPop"), + (instructionFloatDupN, "instructionFloatDupN"), + (instructionFloatSwap, "instructionFloatSwap"), + (instructionFloatRot, "instructionFloatRot"), + (instructionFloatFlush, "instructionFloatFlush"), + (instructionFloatEq, "instructionFloatEq"), + (instructionFloatYank, "instructionFloatYank"), + (instructionFloatYankDup, "instructionFloatYankDup"), + (instructionFloatShove, "instructionFloatShove"), + (instructionFloatIsEmpty, "instructionFloatIsEmpty") + ] + +allBoolInstructions :: [Gene] +allBoolInstructions = map StateFunc [ + (instructionBoolFromInt, "instructionBoolFromInt"), + (instructionBoolFromFloat, "instructionBoolFromFloat"), + (instructionBoolAnd, "instructionBoolAnd"), + (instructionBoolInvertFirstThenAnd, "instructionBoolInvertFirstThenAnd"), + (instructionBoolInvertSecondThenAnd, "instructionBoolInvertSecondThenAnd"), + (instructionBoolOr, "instructionBoolOr"), + (instructionBoolXor, "instructionBoolXor"), + (instructionBoolPop, "instructionBoolPop"), + (instructionBoolDup, "instructionBoolDup"), + (instructionBoolDupN, "instructionBoolDupN"), + (instructionBoolSwap, "instructionBoolSwap"), + (instructionBoolRot, "instructionBoolRot"), + (instructionBoolFlush, "instructionBoolFlush"), + (instructionBoolEq, "instructionBoolEq"), + (instructionBoolStackDepth, "instructionBoolStackDepth"), + (instructionBoolYank, "instructionBoolYank"), + (instructionBoolYankDup, "instructionBoolYankDup"), + (instructionBoolShove, "instructionBoolShove"), + (instructionBoolShoveDup, "instructionBoolShoveDup"), + (instructionBoolIsEmpty, "instructionBoolIsEmpty") + ] + +allCharInstructions :: [Gene] +allCharInstructions = map StateFunc [ + (instructionCharConcat, "instructionCharConcat"), + (instructionCharFromFirstChar, "instructionCharFromFirstChar"), + (instructionCharFromLastChar, "instructionCharFromLastChar"), + (instructionCharFromNthChar, "instructionCharFromNthChar"), + (instructionCharIsWhitespace, "instructionCharIsWhitespace"), + (instructionCharIsLetter, "instructionCharIsLetter"), + (instructionCharIsDigit, "instructionCharIsDigit"), + (instructionCharFromBool, "instructionCharFromBool"), + (instructionCharFromAsciiInt, "instructionCharFromAsciiInt"), + (instructionCharFromAsciiFloat, "instructionCharFromAsciiFloat"), + (instructionCharsFromString, "instructionCharsFromString"), + (instructionCharPop, "instructionCharPop"), + (instructionCharDup, "instructionCharDup"), + (instructionCharDupN, "instructionCharDupN"), + (instructionCharSwap, "instructionCharSwap"), + (instructionCharRot, "instructionCharRot"), + (instructionCharFlush, "instructionCharFlush"), + (instructionCharEq, "instructionCharEq"), + (instructionCharStackDepth, "instructionCharStackDepth"), + (instructionCharYank, "instructionCharYank"), + (instructionCharYankDup, "instructionCharYankDup"), + (instructionCharShove, "instructionCharShove"), + (instructionCharShoveDup, "instructionCharShoveDup"), + (instructionCharIsEmpty, "instructionCharIsEmpty") + ] + +allCodeInstructions :: [Gene] +allCodeInstructions = map StateFunc [ + (instructionCodePop, "instructionCodePop"), + (instructionCodeIsCodeBlock, "instructionCodeIsCodeBlock"), + (instructionCodeIsSingular, "instructionCodeIsSingular"), + (instructionCodeLength, "instructionCodeLength"), + (instructionCodeFirst, "instructionCodeFirst"), + (instructionCodeLast, "instructionCodeLast"), + (instructionCodeTail, "instructionCodeTail"), + (instructionCodeInit, "instructionCodeInit"), + (instructionCodeWrap, "instructionCodeWrap"), + (instructionCodeList, "instructionCodeList"), + (instructionCodeCombine, "instructionCodeCombine"), + (instructionCodeDo, "instructionCodeDo"), + (instructionCodeDoDup, "instructionCodeDoDup"), + (instructionCodeDoThenPop, "instructionCodeDoThenPop"), + (instructionCodeDoRange, "instructionCodeDoRange"), + (instructionCodeDoCount, "instructionCodeDoCount"), + (instructionCodeDoTimes, "instructionCodeDoTimes"), + (instructionCodeIf, "instructionCodeIf"), + (instructionCodeWhen, "instructionCodeWhen"), + (instructionCodeMember, "instructionCodeMember"), + (instructionCodeN, "instructionCodeN"), + (instructionMakeEmptyCodeBlock, "instructionMakeEmptyCodeBlock"), + (instructionIsEmptyCodeBlock, "instructionIsEmptyCodeBlock"), + (instructionCodeSize, "instructionCodeSize"), + (instructionCodeExtract, "instructionCodeExtract"), + (instructionCodeInsert, "instructionCodeInsert"), + (instructionCodeFirstPosition, "instructionCodeFirstPosition"), + (instructionCodeReverse, "instructionCodeReverse"), + (instructionCodeDup, "instructionCodeDup"), + (instructionCodeDupN, "instructionCodeDupN"), + (instructionCodeDup, "instructionCodeDup"), + (instructionCodeDupN, "instructionCodeDupN"), + (instructionCodeSwap, "instructionCodeSwap"), + (instructionCodeRot, "instructionCodeRot"), + (instructionCodeFlush, "instructionCodeFlush"), + (instructionCodeEq, "instructionCodeEq"), + (instructionCodeStackDepth, "instructionCodeStackDepth"), + (instructionCodeYank, "instructionCodeYank"), + (instructionCodeYankDup, "instructionCodeYankDup"), + (instructionCodeShove, "instructionCodeShove"), + (instructionCodeShoveDup, "instructionCodeShoveDup"), + (instructionCodeStackIsEmpty, "instructionCodeStackIsEmpty"), + (instructionCodeFromBool, "instructionCodeFromBool"), + (instructionCodeFromInt, "instructionCodeFromInt"), + (instructionCodeFromChar, "instructionCodeFromChar"), + (instructionCodeFromFloat, "instructionCodeFromFloat"), + (instructionCodeFromString, "instructionCodeFromString"), + (instructionCodeFromVectorInt, "instructionCodeFromVectorInt"), + (instructionCodeFromVectorFloat, "instructionCodeFromVectorFloat"), + (instructionCodeFromVectorString, "instructionCodeFromVectorString"), + (instructionCodeFromVectorBool, "instructionCodeFromVectorBool"), + (instructionCodeFromVectorChar, "instructionCodeFromVectorChar"), + (instructionCodeFromExec, "instructionCodeFromExec") + ] + +allExecInstructions :: [Gene] +allExecInstructions = map StateFunc [ + (instructionExecIf, "instructionExecIf"), + (instructionExecDup, "instructionExecDup"), + (instructionExecDupN, "instructionExecDupN"), + (instructionExecPop, "instructionExecPop"), + (instructionExecSwap, "instructionExecSwap"), + (instructionExecRot, "instructionExecRot"), + (instructionExecFlush, "instructionExecFlush"), + (instructionExecEq, "instructionExecEq"), + (instructionExecStackDepth, "instructionExecStackDepth"), + (instructionExecYank, "instructionExecYank"), + (instructionExecYankDup, "instructionExecYankDup"), + (instructionExecShove, "instructionExecShove"), + (instructionExecShoveDup, "instructionExecShoveDup"), + (instructionExecIsEmpty, "instructionExecIsEmpty"), + (instructionExecDoRange, "instructionExecDoRange"), + (instructionExecDoCount, "instructionExecDoCount"), + (instructionExecDoTimes, "instructionExecDoTimes"), + (instructionExecWhile, "instructionExecWhile"), + (instructionExecDoWhile, "instructionExecDoWhile"), + (instructionExecWhen, "instructionExecWhen") + ] + +allStringInstructions :: [Gene] +allStringInstructions = map StateFunc [ + (instructionStringConcat, "instructionStringConcat"), + (instructionStringSwap, "instructionStringSwap"), + (instructionStringInsertString, "instructionStringInsertString"), + (instructionStringFromFirstChar, "instructionStringFromFirstChar"), + (instructionStringFromLastChar, "instructionStringFromLastChar"), + (instructionStringFromNthChar, "instructionStringFromNthChar"), + (instructionStringIndexOfString, "instructionStringIndexOfString"), + (instructionStringContainsString, "instructionStringContainsString"), + (instructionStringSplitOnString, "instructionStringSplitOnString"), + (instructionStringReplaceFirstString, "instructionStringReplaceFirstString"), + (instructionStringReplaceNString, "instructionStringReplaceNString"), + (instructionStringReplaceAllString, "instructionStringReplaceAllString"), + (instructionStringRemoveFirstString, "instructionStringRemoveFirstString"), + (instructionStringRemoveNString, "instructionStringRemoveNString"), + (instructionStringRemoveAllString, "instructionStringRemoveAllString"), + (instructionStringOccurrencesOfString, "instructionStringOccurrencesOfString"), + (instructionStringInsertChar, "instructionStringInsertChar"), + (instructionStringContainsChar, "instructionStringContainsChar"), + (instructionStringIndexOfChar, "instructionStringIndexOfChar"), + (instructionStringSplitOnChar, "instructionStringSplitOnChar"), + (instructionStringReplaceFirstChar, "instructionStringReplaceFirstChar"), + (instructionStringReplaceNChar, "instructionStringReplaceNChar"), + (instructionStringReplaceAllChar, "instructionStringReplaceAllChar"), + (instructionStringRemoveFirstChar, "instructionStringRemoveFirstChar"), + (instructionStringRemoveNChar, "instructionStringRemoveNChar"), + (instructionStringRemoveAllChar, "instructionStringRemoveAllChar"), + (instructionStringOccurrencesOfChar, "instructionStringOccurrencesOfChar"), + (instructionStringReverse, "instructionStringReverse"), + (instructionStringHead, "instructionStringHead"), + (instructionStringTail, "instructionStringTail"), + (instructionStringAppendChar, "instructionStringAppendChar"), + (instructionStringRest, "instructionStringRest"), + (instructionStringButLast, "instructionStringButLast"), + (instructionStringDrop, "instructionStringDrop"), + (instructionStringButLastN, "instructionStringButLastN"), + (instructionStringLength, "instructionStringLength"), + (instructionStringMakeEmpty, "instructionStringMakeEmpty"), + (instructionStringIsEmptyString, "instructionStringIsEmptyString"), + (instructionStringRemoveNth, "instructionStringRemoveNth"), + (instructionStringSetNth, "instructionStringSetNth"), + (instructionStringStripWhitespace, "instructionStringStripWhitespace"), + (instructionStringFromBool, "instructionStringFromBool"), + (instructionStringFromInt, "instructionStringFromInt"), + (instructionStringFromFloat, "instructionStringFromFloat"), + (instructionStringFromChar, "instructionStringFromChar"), + (instructionStringPop, "instructionStringPop"), + (instructionStringDup, "instructionStringDup"), + (instructionStringDupN, "instructionStringDupN"), + (instructionStringSwap, "instructionStringSwap"), + (instructionStringRot, "instructionStringRot"), + (instructionStringFlush, "instructionStringFlush"), + (instructionStringEq, "instructionStringEq"), + (instructionStringStackDepth, "instructionStringStackDepth"), + (instructionStringYank, "instructionStringYank"), + (instructionStringYankDup, "instructionStringYankDup"), + (instructionStringShove, "instructionStringShove"), + (instructionStringShoveDup, "instructionStringShoveDup"), + (instructionStringIsEmpty, "instructionStringIsEmpty") + ] + +allVectorIntInstructions :: [Gene] +allVectorIntInstructions = map StateFunc [ + (instructionVectorIntConcat, "instructionVectorIntConcat"), + (instructionVectorIntConj, "instructionVectorIntConj"), + (instructionVectorIntTakeN, "instructionVectorIntTakeN"), + (instructionVectorIntSubVector, "instructionVectorIntSubVector"), + (instructionVectorIntFirst, "instructionVectorIntFirst"), + (instructionVectorIntLast, "instructionVectorIntLast"), + (instructionVectorIntNth, "instructionVectorIntNth"), + (instructionVectorIntRest, "instructionVectorIntRest"), + (instructionVectorIntButLast, "instructionVectorIntButLast"), + (instructionVectorIntLength, "instructionVectorIntLength"), + (instructionVectorIntReverse, "instructionVectorIntReverse"), + (instructionVectorIntPushAll, "instructionVectorIntPushAll"), + (instructionVectorIntMakeEmpty, "instructionVectorIntMakeEmpty"), + (instructionVectorIntIsEmpty, "instructionVectorIntIsEmpty"), + (instructionVectorIntIndexOf, "instructionVectorIntIndexOf"), + (instructionVectorIntOccurrencesOf, "instructionVectorIntOccurrencesOf"), + (instructionVectorIntSetNth, "instructionVectorIntSetNth"), + (instructionVectorIntReplace, "instructionVectorIntReplace"), + (instructionVectorIntReplaceFirst, "instructionVectorIntReplaceFirst"), + (instructionVectorIntRemove, "instructionVectorIntRemove"), + (instructionVectorIntIterate, "instructionVectorIntIterate"), + (instructionVectorIntPop, "instructionVectorIntPop"), + (instructionVectorIntDup, "instructionVectorIntDup"), + (instructionVectorIntDupN, "instructionVectorIntDupN"), + (instructionVectorIntSwap, "instructionVectorIntSwap"), + (instructionVectorIntRot, "instructionVectorIntRot"), + (instructionVectorIntFlush, "instructionVectorIntFlush"), + (instructionVectorIntEq, "instructionVectorIntEq"), + (instructionVectorIntStackDepth, "instructionVectorIntStackDepth"), + (instructionVectorIntYank, "instructionVectorIntYank"), + (instructionVectorIntYankDup, "instructionVectorIntYankDup"), + (instructionVectorIntShove, "instructionVectorIntShove"), + (instructionVectorIntShoveDup, "instructionVectorIntShoveDup"), + (instructionVectorIntStackIsEmpty, "instructionVectorIntStackIsEmpty") + ] + +allVectorFloatInstructions :: [Gene] +allVectorFloatInstructions = map StateFunc [ + (instructionVectorFloatConcat, "instructionVectorFloatConcat"), + (instructionVectorFloatConj, "instructionVectorFloatConj"), + (instructionVectorFloatTakeN, "instructionVectorFloatTakeN"), + (instructionVectorFloatSubVector, "instructionVectorFloatSubVector"), + (instructionVectorFloatFirst, "instructionVectorFloatFirst"), + (instructionVectorFloatLast, "instructionVectorFloatLast"), + (instructionVectorFloatNth, "instructionVectorFloatNth"), + (instructionVectorFloatRest, "instructionVectorFloatRest"), + (instructionVectorFloatButLast, "instructionVectorFloatButLast"), + (instructionVectorFloatLength, "instructionVectorFloatLength"), + (instructionVectorFloatReverse, "instructionVectorFloatReverse"), + (instructionVectorFloatPushAll, "instructionVectorFloatPushAll"), + (instructionVectorFloatMakeEmpty, "instructionVectorFloatMakeEmpty"), + (instructionVectorFloatIsEmpty, "instructionVectorFloatIsEmpty"), + (instructionVectorFloatIndexOf, "instructionVectorFloatIndexOf"), + (instructionVectorFloatOccurrencesOf, "instructionVectorFloatOccurrencesOf"), + (instructionVectorFloatSetNth, "instructionVectorFloatSetNth"), + (instructionVectorFloatReplace, "instructionVectorFloatReplace"), + (instructionVectorFloatReplaceFirst, "instructionVectorFloatReplaceFirst"), + (instructionVectorFloatRemove, "instructionVectorFloatRemove"), + (instructionVectorFloatIterate, "instructionVectorFloatIterate"), + (instructionVectorFloatPop, "instructionVectorFloatPop"), + (instructionVectorFloatDup, "instructionVectorFloatDup"), + (instructionVectorFloatDupN, "instructionVectorFloatDupN"), + (instructionVectorFloatSwap, "instructionVectorFloatSwap"), + (instructionVectorFloatRot, "instructionVectorFloatRot"), + (instructionVectorFloatFlush, "instructionVectorFloatFlush"), + (instructionVectorFloatEq, "instructionVectorFloatEq"), + (instructionVectorFloatStackDepth, "instructionVectorFloatStackDepth"), + (instructionVectorFloatYank, "instructionVectorFloatYank"), + (instructionVectorFloatYankDup, "instructionVectorFloatYankDup"), + (instructionVectorFloatShove, "instructionVectorFloatShove"), + (instructionVectorFloatShoveDup, "instructionVectorFloatShoveDup"), + (instructionVectorFloatStackIsEmpty, "instructionVectorFloatStackIsEmpty") + ] + +allVectorCharInstructions :: [Gene] +allVectorCharInstructions = map StateFunc [ + (instructionVectorCharConcat, "instructionVectorCharConcat"), + (instructionVectorCharConj, "instructionVectorCharConj"), + (instructionVectorCharTakeN, "instructionVectorCharTakeN"), + (instructionVectorCharSubVector, "instructionVectorCharSubVector"), + (instructionVectorCharFirst, "instructionVectorCharFirst"), + (instructionVectorCharLast, "instructionVectorCharLast"), + (instructionVectorCharNth, "instructionVectorCharNth"), + (instructionVectorCharRest, "instructionVectorCharRest"), + (instructionVectorCharButLast, "instructionVectorCharButLast"), + (instructionVectorCharLength, "instructionVectorCharLength"), + (instructionVectorCharReverse, "instructionVectorCharReverse"), + (instructionVectorCharPushAll, "instructionVectorCharPushAll"), + (instructionVectorCharMakeEmpty, "instructionVectorCharMakeEmpty"), + (instructionVectorCharIsEmpty, "instructionVectorCharIsEmpty"), + (instructionVectorCharIndexOf, "instructionVectorCharIndexOf"), + (instructionVectorCharOccurrencesOf, "instructionVectorCharOccurrencesOf"), + (instructionVectorCharSetNth, "instructionVectorCharSetNth"), + (instructionVectorCharReplace, "instructionVectorCharReplace"), + (instructionVectorCharReplaceFirst, "instructionVectorCharReplaceFirst"), + (instructionVectorCharRemove, "instructionVectorCharRemove"), + (instructionVectorCharIterate, "instructionVectorCharIterate"), + (instructionVectorCharPop, "instructionVectorCharPop"), + (instructionVectorCharDup, "instructionVectorCharDup"), + (instructionVectorCharDupN, "instructionVectorCharDupN"), + (instructionVectorCharSwap, "instructionVectorCharSwap"), + (instructionVectorCharRot, "instructionVectorCharRot"), + (instructionVectorCharFlush, "instructionVectorCharFlush"), + (instructionVectorCharEq, "instructionVectorCharEq"), + (instructionVectorCharStackDepth, "instructionVectorCharStackDepth"), + (instructionVectorCharYank, "instructionVectorCharYank"), + (instructionVectorCharYankDup, "instructionVectorCharYankDup"), + (instructionVectorCharShove, "instructionVectorCharShove"), + (instructionVectorCharShoveDup, "instructionVectorCharShoveDup"), + (instructionVectorCharStackIsEmpty, "instructionVectorCharStackIsEmpty") + ] + +allVectorStringInstructions :: [Gene] +allVectorStringInstructions = map StateFunc [ + (instructionVectorStringConcat, "instructionVectorStringConcat"), + (instructionVectorStringConj, "instructionVectorStringConj"), + (instructionVectorStringTakeN, "instructionVectorStringTakeN"), + (instructionVectorStringSubVector, "instructionVectorStringSubVector"), + (instructionVectorStringFirst, "instructionVectorStringFirst"), + (instructionVectorStringLast, "instructionVectorStringLast"), + (instructionVectorStringNth, "instructionVectorStringNth"), + (instructionVectorStringRest, "instructionVectorStringRest"), + (instructionVectorStringButLast, "instructionVectorStringButLast"), + (instructionVectorStringLength, "instructionVectorStringLength"), + (instructionVectorStringReverse, "instructionVectorStringReverse"), + (instructionVectorStringPushAll, "instructionVectorStringPushAll"), + (instructionVectorStringMakeEmpty, "instructionVectorStringMakeEmpty"), + (instructionVectorStringIsEmpty, "instructionVectorStringIsEmpty"), + (instructionVectorStringIndexOf, "instructionVectorStringIndexOf"), + (instructionVectorStringOccurrencesOf, "instructionVectorStringOccurrencesOf"), + (instructionVectorStringSetNth, "instructionVectorStringSetNth"), + (instructionVectorStringReplace, "instructionVectorStringReplace"), + (instructionVectorStringReplaceFirst, "instructionVectorStringReplaceFirst"), + (instructionVectorStringRemove, "instructionVectorStringRemove"), + (instructionVectorStringIterate, "instructionVectorStringIterate"), + (instructionVectorStringPop, "instructionVectorStringPop"), + (instructionVectorStringDup, "instructionVectorStringDup"), + (instructionVectorStringDupN, "instructionVectorStringDupN"), + (instructionVectorStringSwap, "instructionVectorStringSwap"), + (instructionVectorStringRot, "instructionVectorStringRot"), + (instructionVectorStringFlush, "instructionVectorStringFlush"), + (instructionVectorStringEq, "instructionVectorStringEq"), + (instructionVectorStringStackDepth, "instructionVectorStringStackDepth"), + (instructionVectorStringYank, "instructionVectorStringYank"), + (instructionVectorStringYankDup, "instructionVectorStringYankDup"), + (instructionVectorStringShove, "instructionVectorStringShove"), + (instructionVectorStringShoveDup, "instructionVectorStringShoveDup"), + (instructionVectorStringStackIsEmpty, "instructionVectorStringStackIsEmpty") + ] + +allVectorBoolInstructions :: [Gene] +allVectorBoolInstructions = map StateFunc [ + (instructionVectorBoolConcat, "instructionVectorBoolConcat"), + (instructionVectorBoolConj, "instructionVectorBoolConj"), + (instructionVectorBoolTakeN, "instructionVectorBoolTakeN"), + (instructionVectorBoolSubVector, "instructionVectorBoolSubVector"), + (instructionVectorBoolFirst, "instructionVectorBoolFirst"), + (instructionVectorBoolLast, "instructionVectorBoolLast"), + (instructionVectorBoolNth, "instructionVectorBoolNth"), + (instructionVectorBoolRest, "instructionVectorBoolRest"), + (instructionVectorBoolButLast, "instructionVectorBoolButLast"), + (instructionVectorBoolLength, "instructionVectorBoolLength"), + (instructionVectorBoolReverse, "instructionVectorBoolReverse"), + (instructionVectorBoolPushAll, "instructionVectorBoolPushAll"), + (instructionVectorBoolMakeEmpty, "instructionVectorBoolMakeEmpty"), + (instructionVectorBoolIsEmpty, "instructionVectorBoolIsEmpty"), + (instructionVectorBoolIndexOf, "instructionVectorBoolIndexOf"), + (instructionVectorBoolOccurrencesOf, "instructionVectorBoolOccurrencesOf"), + (instructionVectorBoolSetNth, "instructionVectorBoolSetNth"), + (instructionVectorBoolReplace, "instructionVectorBoolReplace"), + (instructionVectorBoolReplaceFirst, "instructionVectorBoolReplaceFirst"), + (instructionVectorBoolRemove, "instructionVectorBoolRemove"), + (instructionVectorBoolIterate, "instructionVectorBoolIterate"), + (instructionVectorBoolPop, "instructionVectorBoolPop"), + (instructionVectorBoolDup, "instructionVectorBoolDup"), + (instructionVectorBoolDupN, "instructionVectorBoolDupN"), + (instructionVectorBoolSwap, "instructionVectorBoolSwap"), + (instructionVectorBoolRot, "instructionVectorBoolRot"), + (instructionVectorBoolFlush, "instructionVectorBoolFlush"), + (instructionVectorBoolEq, "instructionVectorBoolEq"), + (instructionVectorBoolStackDepth, "instructionVectorBoolStackDepth"), + (instructionVectorBoolYank, "instructionVectorBoolYank"), + (instructionVectorBoolYankDup, "instructionVectorBoolYankDup"), + (instructionVectorBoolShove, "instructionVectorBoolShove"), + (instructionVectorBoolShoveDup, "instructionVectorBoolShoveDup"), + (instructionVectorBoolStackIsEmpty, "instructionVectorBoolStackIsEmpty") + ] + +allInstructions :: [Gene] +allInstructions = + allIntInstructions <> + allFloatInstructions <> + allBoolInstructions <> + allCharInstructions <> + allCodeInstructions <> + allExecInstructions <> + allStringInstructions <> + allVectorIntInstructions <> + allVectorFloatInstructions <> + allVectorCharInstructions <> + allVectorStringInstructions <> + allVectorBoolInstructions diff --git a/src/Instructions/CodeInstructions.hs b/src/Instructions/CodeInstructions.hs index 1caf48b..654673e 100644 --- a/src/Instructions/CodeInstructions.hs +++ b/src/Instructions/CodeInstructions.hs @@ -66,7 +66,6 @@ codeMember (Block _) (Block _) = False -- Can't compare two lists with `elem` codeMember (Block xs) ygene = ygene `elem` xs codeMember _ _ = False --- I love list comprehensions codeRecursiveSize :: Gene -> Int codeRecursiveSize (Block xs) = sum [codeRecursiveSize x + if isBlock x then 1 else 0 | x <- xs] codeRecursiveSize _ = 1 @@ -224,11 +223,6 @@ instructionCodeInsert state@(State {_code = c1 : c2 : cs, _int = i1 : is}) = state{_code = Block (codeInsertAtPoint [c1] c2 index) : cs, _int = is} instructionCodeInsert state = state --- How do I test if two functions are the same?????????? --- This will impact the final case. This implementation can't determine --- if two functions are the same, so it assumes that they are. --- Maybe can test for equality by seeing if these two functions affect the current state --- in the same way. instructionCodeFirstPosition :: State -> State instructionCodeFirstPosition state@(State {_code = (Block []) : c2 : cs, _int = is}) = state {_code = cs, _int = (if c2 == Block [] then 0 else -1) : is} instructionCodeFirstPosition state@(State {_code = (Block c1) : c2 : cs, _int = is}) = state {_code = cs, _int = positionElem c1 c2 : is} diff --git a/src/Instructions/StringInstructions.hs b/src/Instructions/StringInstructions.hs index c26aada..88b2344 100644 --- a/src/Instructions/StringInstructions.hs +++ b/src/Instructions/StringInstructions.hs @@ -175,7 +175,6 @@ instructionStringStripWhitespace :: State -> State instructionStringStripWhitespace state@(State {_string = s1 : ss}) = state{_string = strip s1 : ss} instructionStringStripWhitespace state = state --- Need to do uncons to all of the warnings in this mug instructionStringFromLens :: Show a => State -> Lens' State [a] -> State instructionStringFromLens state@(State {_string = ss}) accessor = case uncons (view accessor state) of From 7c7de9f3e84d04971cf668a1d3bf03f247d096b3 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Thu, 6 Feb 2025 16:52:42 -0600 Subject: [PATCH 119/171] restructuring/logical -> bool --- HushGP.cabal | 42 +- TODO.md | 2 +- src/HushGP/GP.hs | 3 + src/HushGP/Instructions.hs | 504 ++++++++++++++++++ src/HushGP/Instructions/BoolInstructions.hs | 79 +++ src/HushGP/Instructions/CharInstructions.hs | 89 ++++ src/HushGP/Instructions/CodeInstructions.hs | 310 +++++++++++ src/HushGP/Instructions/ExecInstructions.hs | 106 ++++ src/HushGP/Instructions/FloatInstructions.hs | 116 ++++ .../Instructions/GenericInstructions.hs | 348 ++++++++++++ src/HushGP/Instructions/IntInstructions.hs | 104 ++++ src/HushGP/Instructions/StringInstructions.hs | 231 ++++++++ .../Instructions/VectorBoolInstructions.hs | 106 ++++ .../Instructions/VectorCharInstructions.hs | 106 ++++ .../Instructions/VectorFloatInstructions.hs | 106 ++++ .../Instructions/VectorIntInstructions.hs | 106 ++++ .../Instructions/VectorStringInstructions.hs | 106 ++++ src/HushGP/Push.hs | 86 +++ src/HushGP/PushTests.hs | 10 + src/HushGP/PushTests/GenericTests.hs | 129 +++++ src/HushGP/PushTests/IntTests.hs | 84 +++ src/HushGP/PushTests/UtilTests.hs | 36 ++ src/HushGP/State.hs | 166 ++++++ 23 files changed, 2953 insertions(+), 22 deletions(-) create mode 100644 src/HushGP/GP.hs create mode 100644 src/HushGP/Instructions.hs create mode 100644 src/HushGP/Instructions/BoolInstructions.hs create mode 100644 src/HushGP/Instructions/CharInstructions.hs create mode 100644 src/HushGP/Instructions/CodeInstructions.hs create mode 100644 src/HushGP/Instructions/ExecInstructions.hs create mode 100644 src/HushGP/Instructions/FloatInstructions.hs create mode 100644 src/HushGP/Instructions/GenericInstructions.hs create mode 100644 src/HushGP/Instructions/IntInstructions.hs create mode 100644 src/HushGP/Instructions/StringInstructions.hs create mode 100644 src/HushGP/Instructions/VectorBoolInstructions.hs create mode 100644 src/HushGP/Instructions/VectorCharInstructions.hs create mode 100644 src/HushGP/Instructions/VectorFloatInstructions.hs create mode 100644 src/HushGP/Instructions/VectorIntInstructions.hs create mode 100644 src/HushGP/Instructions/VectorStringInstructions.hs create mode 100644 src/HushGP/Push.hs create mode 100644 src/HushGP/PushTests.hs create mode 100644 src/HushGP/PushTests/GenericTests.hs create mode 100644 src/HushGP/PushTests/IntTests.hs create mode 100644 src/HushGP/PushTests/UtilTests.hs create mode 100644 src/HushGP/State.hs diff --git a/HushGP.cabal b/HushGP.cabal index f24e378..fa30da6 100644 --- a/HushGP.cabal +++ b/HushGP.cabal @@ -35,27 +35,27 @@ library import: warnings -- Modules exported by the library. - exposed-modules: Push - , GP - , State - , Instructions - , Instructions.IntInstructions - , Instructions.ExecInstructions - , Instructions.FloatInstructions - , Instructions.GenericInstructions - , Instructions.LogicalInstructions - , Instructions.CodeInstructions - , Instructions.StringInstructions - , Instructions.CharInstructions - , Instructions.VectorIntInstructions - , Instructions.VectorFloatInstructions - , Instructions.VectorStringInstructions - , Instructions.VectorLogicalInstructions - , Instructions.VectorCharInstructions - , PushTests - , PushTests.IntTests - , PushTests.GenericTests - , PushTests.UtilTests + exposed-modules: HushGP.Push + , HushGP.GP + , HushGP.State + , HushGP.Instructions + , HushGP.Instructions.IntInstructions + , HushGP.Instructions.ExecInstructions + , HushGP.Instructions.FloatInstructions + , HushGP.Instructions.GenericInstructions + , HushGP.Instructions.BoolInstructions + , HushGP.Instructions.CodeInstructions + , HushGP.Instructions.StringInstructions + , HushGP.Instructions.CharInstructions + , HushGP.Instructions.VectorIntInstructions + , HushGP.Instructions.VectorFloatInstructions + , HushGP.Instructions.VectorStringInstructions + , HushGP.Instructions.VectorBoolInstructions + , HushGP.Instructions.VectorCharInstructions + , HushGP.PushTests + , HushGP.PushTests.IntTests + , HushGP.PushTests.GenericTests + , HushGP.PushTests.UtilTests -- Modules included in this library but not exported. -- other-modules: diff --git a/TODO.md b/TODO.md index f3b61eb..7ca127c 100644 --- a/TODO.md +++ b/TODO.md @@ -8,7 +8,7 @@ - [ ] Implement Linear Algebra functions as specified in the previous papers - [ ] Add a function to sort a vector forward and backwards - [ ] Disambiguate isEmpty and stackIsEmpty -- [ ] Rename Logical to Bool +- [X] Rename Logical to Bool - [x] Make int yank, shove, yankdup, and shovedup generic ## PushGP TODO diff --git a/src/HushGP/GP.hs b/src/HushGP/GP.hs new file mode 100644 index 0000000..d2be570 --- /dev/null +++ b/src/HushGP/GP.hs @@ -0,0 +1,3 @@ +module HushGP.GP where + +-- import Debug.Trace (trace, traceStack) diff --git a/src/HushGP/Instructions.hs b/src/HushGP/Instructions.hs new file mode 100644 index 0000000..a296aeb --- /dev/null +++ b/src/HushGP/Instructions.hs @@ -0,0 +1,504 @@ +module HushGP.Instructions + ( module HushGP.Instructions.GenericInstructions, + module HushGP.Instructions.IntInstructions, + module HushGP.Instructions.FloatInstructions, + module HushGP.Instructions.StringInstructions, + module HushGP.Instructions.CharInstructions, + module HushGP.Instructions.CodeInstructions, + module HushGP.Instructions.ExecInstructions, + module HushGP.Instructions.BoolInstructions, + module HushGP.Instructions.VectorIntInstructions, + module HushGP.Instructions.VectorFloatInstructions, + module HushGP.Instructions.VectorStringInstructions, + module HushGP.Instructions.VectorBoolInstructions, + module HushGP.Instructions.VectorCharInstructions, + allIntInstructions, + allFloatInstructions, + allBoolInstructions, + allCharInstructions, + allCodeInstructions, + allExecInstructions, + allStringInstructions, + allVectorIntInstructions, + allVectorFloatInstructions, + allVectorCharInstructions, + allVectorStringInstructions, + allVectorBoolInstructions, + allInstructions + ) +where + +import HushGP.Instructions.CharInstructions +import HushGP.Instructions.CodeInstructions +import HushGP.Instructions.ExecInstructions +import HushGP.Instructions.FloatInstructions +import HushGP.Instructions.GenericInstructions +import HushGP.Instructions.IntInstructions +import HushGP.Instructions.BoolInstructions +import HushGP.Instructions.StringInstructions +import HushGP.Instructions.VectorCharInstructions +import HushGP.Instructions.VectorFloatInstructions +import HushGP.Instructions.VectorIntInstructions +import HushGP.Instructions.VectorBoolInstructions +import HushGP.Instructions.VectorStringInstructions +import HushGP.State + +allIntInstructions :: [Gene] +allIntInstructions = map StateFunc [ + (instructionIntFromFloat, "instructionIntFromFloat"), + (instructionIntFromBool, "instructionIntFromBool"), + (instructionIntAdd, "instructionIntAdd"), + (instructionIntSub, "instructionIntSub"), + (instructionIntMul, "instructionIntMul"), + (instructionIntDiv, "instructionIntDiv"), + (instructionIntMod, "instructionIntMod"), + (instructionIntMin, "instructionIntMin"), + (instructionIntMax, "instructionIntMax"), + (instructionIntInc, "instructionIntInc"), + (instructionIntDec, "instructionIntDec"), + (instructionIntLT, "instructionIntLT"), + (instructionIntGT, "instructionIntGT"), + (instructionIntLTE, "instructionIntLTE"), + (instructionIntGTE, "instructionIntGTE"), + (instructionIntDup, "instructionIntDup"), + (instructionIntPop, "instructionIntPop"), + (instructionIntDupN, "instructionIntDupN"), + (instructionIntSwap, "instructionIntSwap"), + (instructionIntRot, "instructionIntRot"), + (instructionIntFlush, "instructionIntFlush"), + (instructionIntEq, "instructionIntEq"), + (instructionIntYank, "instructionIntYank"), + (instructionIntYankDup, "instructionIntYankDup"), + (instructionIntShove, "instructionIntShove"), + (instructionIntIsEmpty, "instructionIntIsEmpty") + ] + +allFloatInstructions :: [Gene] +allFloatInstructions = map StateFunc [ + (instructionFloatFromInt, "instructionFloatFromInt"), + (instructionFloatFromBool, "instructionFloatFromBool"), + (instructionFloatAdd, "instructionFloatAdd"), + (instructionFloatSub, "instructionFloatSub"), + (instructionFloatMul, "instructionFloatMul"), + (instructionFloatDiv, "instructionFloatDiv"), + (instructionFloatMod, "instructionFloatMod"), + (instructionFloatMin, "instructionFloatMin"), + (instructionFloatMax, "instructionFloatMax"), + (instructionFloatInc, "instructionFloatInc"), + (instructionFloatDec, "instructionFloatDec"), + (instructionFloatLT, "instructionFloatLT"), + (instructionFloatGT, "instructionFloatGT"), + (instructionFloatLTE, "instructionFloatLTE"), + (instructionFloatGTE, "instructionFloatGTE"), + (instructionFloatDup, "instructionFloatDup"), + (instructionFloatPop, "instructionFloatPop"), + (instructionFloatDupN, "instructionFloatDupN"), + (instructionFloatSwap, "instructionFloatSwap"), + (instructionFloatRot, "instructionFloatRot"), + (instructionFloatFlush, "instructionFloatFlush"), + (instructionFloatEq, "instructionFloatEq"), + (instructionFloatYank, "instructionFloatYank"), + (instructionFloatYankDup, "instructionFloatYankDup"), + (instructionFloatShove, "instructionFloatShove"), + (instructionFloatIsEmpty, "instructionFloatIsEmpty") + ] + +allBoolInstructions :: [Gene] +allBoolInstructions = map StateFunc [ + (instructionBoolFromInt, "instructionBoolFromInt"), + (instructionBoolFromFloat, "instructionBoolFromFloat"), + (instructionBoolAnd, "instructionBoolAnd"), + (instructionBoolInvertFirstThenAnd, "instructionBoolInvertFirstThenAnd"), + (instructionBoolInvertSecondThenAnd, "instructionBoolInvertSecondThenAnd"), + (instructionBoolOr, "instructionBoolOr"), + (instructionBoolXor, "instructionBoolXor"), + (instructionBoolPop, "instructionBoolPop"), + (instructionBoolDup, "instructionBoolDup"), + (instructionBoolDupN, "instructionBoolDupN"), + (instructionBoolSwap, "instructionBoolSwap"), + (instructionBoolRot, "instructionBoolRot"), + (instructionBoolFlush, "instructionBoolFlush"), + (instructionBoolEq, "instructionBoolEq"), + (instructionBoolStackDepth, "instructionBoolStackDepth"), + (instructionBoolYank, "instructionBoolYank"), + (instructionBoolYankDup, "instructionBoolYankDup"), + (instructionBoolShove, "instructionBoolShove"), + (instructionBoolShoveDup, "instructionBoolShoveDup"), + (instructionBoolIsEmpty, "instructionBoolIsEmpty") + ] + +allCharInstructions :: [Gene] +allCharInstructions = map StateFunc [ + (instructionCharConcat, "instructionCharConcat"), + (instructionCharFromFirstChar, "instructionCharFromFirstChar"), + (instructionCharFromLastChar, "instructionCharFromLastChar"), + (instructionCharFromNthChar, "instructionCharFromNthChar"), + (instructionCharIsWhitespace, "instructionCharIsWhitespace"), + (instructionCharIsLetter, "instructionCharIsLetter"), + (instructionCharIsDigit, "instructionCharIsDigit"), + (instructionCharFromBool, "instructionCharFromBool"), + (instructionCharFromAsciiInt, "instructionCharFromAsciiInt"), + (instructionCharFromAsciiFloat, "instructionCharFromAsciiFloat"), + (instructionCharsFromString, "instructionCharsFromString"), + (instructionCharPop, "instructionCharPop"), + (instructionCharDup, "instructionCharDup"), + (instructionCharDupN, "instructionCharDupN"), + (instructionCharSwap, "instructionCharSwap"), + (instructionCharRot, "instructionCharRot"), + (instructionCharFlush, "instructionCharFlush"), + (instructionCharEq, "instructionCharEq"), + (instructionCharStackDepth, "instructionCharStackDepth"), + (instructionCharYank, "instructionCharYank"), + (instructionCharYankDup, "instructionCharYankDup"), + (instructionCharShove, "instructionCharShove"), + (instructionCharShoveDup, "instructionCharShoveDup"), + (instructionCharIsEmpty, "instructionCharIsEmpty") + ] + +allCodeInstructions :: [Gene] +allCodeInstructions = map StateFunc [ + (instructionCodePop, "instructionCodePop"), + (instructionCodeIsCodeBlock, "instructionCodeIsCodeBlock"), + (instructionCodeIsSingular, "instructionCodeIsSingular"), + (instructionCodeLength, "instructionCodeLength"), + (instructionCodeFirst, "instructionCodeFirst"), + (instructionCodeLast, "instructionCodeLast"), + (instructionCodeTail, "instructionCodeTail"), + (instructionCodeInit, "instructionCodeInit"), + (instructionCodeWrap, "instructionCodeWrap"), + (instructionCodeList, "instructionCodeList"), + (instructionCodeCombine, "instructionCodeCombine"), + (instructionCodeDo, "instructionCodeDo"), + (instructionCodeDoDup, "instructionCodeDoDup"), + (instructionCodeDoThenPop, "instructionCodeDoThenPop"), + (instructionCodeDoRange, "instructionCodeDoRange"), + (instructionCodeDoCount, "instructionCodeDoCount"), + (instructionCodeDoTimes, "instructionCodeDoTimes"), + (instructionCodeIf, "instructionCodeIf"), + (instructionCodeWhen, "instructionCodeWhen"), + (instructionCodeMember, "instructionCodeMember"), + (instructionCodeN, "instructionCodeN"), + (instructionMakeEmptyCodeBlock, "instructionMakeEmptyCodeBlock"), + (instructionIsEmptyCodeBlock, "instructionIsEmptyCodeBlock"), + (instructionCodeSize, "instructionCodeSize"), + (instructionCodeExtract, "instructionCodeExtract"), + (instructionCodeInsert, "instructionCodeInsert"), + (instructionCodeFirstPosition, "instructionCodeFirstPosition"), + (instructionCodeReverse, "instructionCodeReverse"), + (instructionCodeDup, "instructionCodeDup"), + (instructionCodeDupN, "instructionCodeDupN"), + (instructionCodeDup, "instructionCodeDup"), + (instructionCodeDupN, "instructionCodeDupN"), + (instructionCodeSwap, "instructionCodeSwap"), + (instructionCodeRot, "instructionCodeRot"), + (instructionCodeFlush, "instructionCodeFlush"), + (instructionCodeEq, "instructionCodeEq"), + (instructionCodeStackDepth, "instructionCodeStackDepth"), + (instructionCodeYank, "instructionCodeYank"), + (instructionCodeYankDup, "instructionCodeYankDup"), + (instructionCodeShove, "instructionCodeShove"), + (instructionCodeShoveDup, "instructionCodeShoveDup"), + (instructionCodeStackIsEmpty, "instructionCodeStackIsEmpty"), + (instructionCodeFromBool, "instructionCodeFromBool"), + (instructionCodeFromInt, "instructionCodeFromInt"), + (instructionCodeFromChar, "instructionCodeFromChar"), + (instructionCodeFromFloat, "instructionCodeFromFloat"), + (instructionCodeFromString, "instructionCodeFromString"), + (instructionCodeFromVectorInt, "instructionCodeFromVectorInt"), + (instructionCodeFromVectorFloat, "instructionCodeFromVectorFloat"), + (instructionCodeFromVectorString, "instructionCodeFromVectorString"), + (instructionCodeFromVectorBool, "instructionCodeFromVectorBool"), + (instructionCodeFromVectorChar, "instructionCodeFromVectorChar"), + (instructionCodeFromExec, "instructionCodeFromExec") + ] + +allExecInstructions :: [Gene] +allExecInstructions = map StateFunc [ + (instructionExecIf, "instructionExecIf"), + (instructionExecDup, "instructionExecDup"), + (instructionExecDupN, "instructionExecDupN"), + (instructionExecPop, "instructionExecPop"), + (instructionExecSwap, "instructionExecSwap"), + (instructionExecRot, "instructionExecRot"), + (instructionExecFlush, "instructionExecFlush"), + (instructionExecEq, "instructionExecEq"), + (instructionExecStackDepth, "instructionExecStackDepth"), + (instructionExecYank, "instructionExecYank"), + (instructionExecYankDup, "instructionExecYankDup"), + (instructionExecShove, "instructionExecShove"), + (instructionExecShoveDup, "instructionExecShoveDup"), + (instructionExecIsEmpty, "instructionExecIsEmpty"), + (instructionExecDoRange, "instructionExecDoRange"), + (instructionExecDoCount, "instructionExecDoCount"), + (instructionExecDoTimes, "instructionExecDoTimes"), + (instructionExecWhile, "instructionExecWhile"), + (instructionExecDoWhile, "instructionExecDoWhile"), + (instructionExecWhen, "instructionExecWhen") + ] + +allStringInstructions :: [Gene] +allStringInstructions = map StateFunc [ + (instructionStringConcat, "instructionStringConcat"), + (instructionStringSwap, "instructionStringSwap"), + (instructionStringInsertString, "instructionStringInsertString"), + (instructionStringFromFirstChar, "instructionStringFromFirstChar"), + (instructionStringFromLastChar, "instructionStringFromLastChar"), + (instructionStringFromNthChar, "instructionStringFromNthChar"), + (instructionStringIndexOfString, "instructionStringIndexOfString"), + (instructionStringContainsString, "instructionStringContainsString"), + (instructionStringSplitOnString, "instructionStringSplitOnString"), + (instructionStringReplaceFirstString, "instructionStringReplaceFirstString"), + (instructionStringReplaceNString, "instructionStringReplaceNString"), + (instructionStringReplaceAllString, "instructionStringReplaceAllString"), + (instructionStringRemoveFirstString, "instructionStringRemoveFirstString"), + (instructionStringRemoveNString, "instructionStringRemoveNString"), + (instructionStringRemoveAllString, "instructionStringRemoveAllString"), + (instructionStringOccurrencesOfString, "instructionStringOccurrencesOfString"), + (instructionStringInsertChar, "instructionStringInsertChar"), + (instructionStringContainsChar, "instructionStringContainsChar"), + (instructionStringIndexOfChar, "instructionStringIndexOfChar"), + (instructionStringSplitOnChar, "instructionStringSplitOnChar"), + (instructionStringReplaceFirstChar, "instructionStringReplaceFirstChar"), + (instructionStringReplaceNChar, "instructionStringReplaceNChar"), + (instructionStringReplaceAllChar, "instructionStringReplaceAllChar"), + (instructionStringRemoveFirstChar, "instructionStringRemoveFirstChar"), + (instructionStringRemoveNChar, "instructionStringRemoveNChar"), + (instructionStringRemoveAllChar, "instructionStringRemoveAllChar"), + (instructionStringOccurrencesOfChar, "instructionStringOccurrencesOfChar"), + (instructionStringReverse, "instructionStringReverse"), + (instructionStringHead, "instructionStringHead"), + (instructionStringTail, "instructionStringTail"), + (instructionStringAppendChar, "instructionStringAppendChar"), + (instructionStringRest, "instructionStringRest"), + (instructionStringButLast, "instructionStringButLast"), + (instructionStringDrop, "instructionStringDrop"), + (instructionStringButLastN, "instructionStringButLastN"), + (instructionStringLength, "instructionStringLength"), + (instructionStringMakeEmpty, "instructionStringMakeEmpty"), + (instructionStringIsEmptyString, "instructionStringIsEmptyString"), + (instructionStringRemoveNth, "instructionStringRemoveNth"), + (instructionStringSetNth, "instructionStringSetNth"), + (instructionStringStripWhitespace, "instructionStringStripWhitespace"), + (instructionStringFromBool, "instructionStringFromBool"), + (instructionStringFromInt, "instructionStringFromInt"), + (instructionStringFromFloat, "instructionStringFromFloat"), + (instructionStringFromChar, "instructionStringFromChar"), + (instructionStringPop, "instructionStringPop"), + (instructionStringDup, "instructionStringDup"), + (instructionStringDupN, "instructionStringDupN"), + (instructionStringSwap, "instructionStringSwap"), + (instructionStringRot, "instructionStringRot"), + (instructionStringFlush, "instructionStringFlush"), + (instructionStringEq, "instructionStringEq"), + (instructionStringStackDepth, "instructionStringStackDepth"), + (instructionStringYank, "instructionStringYank"), + (instructionStringYankDup, "instructionStringYankDup"), + (instructionStringShove, "instructionStringShove"), + (instructionStringShoveDup, "instructionStringShoveDup"), + (instructionStringIsEmpty, "instructionStringIsEmpty") + ] + +allVectorIntInstructions :: [Gene] +allVectorIntInstructions = map StateFunc [ + (instructionVectorIntConcat, "instructionVectorIntConcat"), + (instructionVectorIntConj, "instructionVectorIntConj"), + (instructionVectorIntTakeN, "instructionVectorIntTakeN"), + (instructionVectorIntSubVector, "instructionVectorIntSubVector"), + (instructionVectorIntFirst, "instructionVectorIntFirst"), + (instructionVectorIntLast, "instructionVectorIntLast"), + (instructionVectorIntNth, "instructionVectorIntNth"), + (instructionVectorIntRest, "instructionVectorIntRest"), + (instructionVectorIntButLast, "instructionVectorIntButLast"), + (instructionVectorIntLength, "instructionVectorIntLength"), + (instructionVectorIntReverse, "instructionVectorIntReverse"), + (instructionVectorIntPushAll, "instructionVectorIntPushAll"), + (instructionVectorIntMakeEmpty, "instructionVectorIntMakeEmpty"), + (instructionVectorIntIsEmpty, "instructionVectorIntIsEmpty"), + (instructionVectorIntIndexOf, "instructionVectorIntIndexOf"), + (instructionVectorIntOccurrencesOf, "instructionVectorIntOccurrencesOf"), + (instructionVectorIntSetNth, "instructionVectorIntSetNth"), + (instructionVectorIntReplace, "instructionVectorIntReplace"), + (instructionVectorIntReplaceFirst, "instructionVectorIntReplaceFirst"), + (instructionVectorIntRemove, "instructionVectorIntRemove"), + (instructionVectorIntIterate, "instructionVectorIntIterate"), + (instructionVectorIntPop, "instructionVectorIntPop"), + (instructionVectorIntDup, "instructionVectorIntDup"), + (instructionVectorIntDupN, "instructionVectorIntDupN"), + (instructionVectorIntSwap, "instructionVectorIntSwap"), + (instructionVectorIntRot, "instructionVectorIntRot"), + (instructionVectorIntFlush, "instructionVectorIntFlush"), + (instructionVectorIntEq, "instructionVectorIntEq"), + (instructionVectorIntStackDepth, "instructionVectorIntStackDepth"), + (instructionVectorIntYank, "instructionVectorIntYank"), + (instructionVectorIntYankDup, "instructionVectorIntYankDup"), + (instructionVectorIntShove, "instructionVectorIntShove"), + (instructionVectorIntShoveDup, "instructionVectorIntShoveDup"), + (instructionVectorIntStackIsEmpty, "instructionVectorIntStackIsEmpty") + ] + +allVectorFloatInstructions :: [Gene] +allVectorFloatInstructions = map StateFunc [ + (instructionVectorFloatConcat, "instructionVectorFloatConcat"), + (instructionVectorFloatConj, "instructionVectorFloatConj"), + (instructionVectorFloatTakeN, "instructionVectorFloatTakeN"), + (instructionVectorFloatSubVector, "instructionVectorFloatSubVector"), + (instructionVectorFloatFirst, "instructionVectorFloatFirst"), + (instructionVectorFloatLast, "instructionVectorFloatLast"), + (instructionVectorFloatNth, "instructionVectorFloatNth"), + (instructionVectorFloatRest, "instructionVectorFloatRest"), + (instructionVectorFloatButLast, "instructionVectorFloatButLast"), + (instructionVectorFloatLength, "instructionVectorFloatLength"), + (instructionVectorFloatReverse, "instructionVectorFloatReverse"), + (instructionVectorFloatPushAll, "instructionVectorFloatPushAll"), + (instructionVectorFloatMakeEmpty, "instructionVectorFloatMakeEmpty"), + (instructionVectorFloatIsEmpty, "instructionVectorFloatIsEmpty"), + (instructionVectorFloatIndexOf, "instructionVectorFloatIndexOf"), + (instructionVectorFloatOccurrencesOf, "instructionVectorFloatOccurrencesOf"), + (instructionVectorFloatSetNth, "instructionVectorFloatSetNth"), + (instructionVectorFloatReplace, "instructionVectorFloatReplace"), + (instructionVectorFloatReplaceFirst, "instructionVectorFloatReplaceFirst"), + (instructionVectorFloatRemove, "instructionVectorFloatRemove"), + (instructionVectorFloatIterate, "instructionVectorFloatIterate"), + (instructionVectorFloatPop, "instructionVectorFloatPop"), + (instructionVectorFloatDup, "instructionVectorFloatDup"), + (instructionVectorFloatDupN, "instructionVectorFloatDupN"), + (instructionVectorFloatSwap, "instructionVectorFloatSwap"), + (instructionVectorFloatRot, "instructionVectorFloatRot"), + (instructionVectorFloatFlush, "instructionVectorFloatFlush"), + (instructionVectorFloatEq, "instructionVectorFloatEq"), + (instructionVectorFloatStackDepth, "instructionVectorFloatStackDepth"), + (instructionVectorFloatYank, "instructionVectorFloatYank"), + (instructionVectorFloatYankDup, "instructionVectorFloatYankDup"), + (instructionVectorFloatShove, "instructionVectorFloatShove"), + (instructionVectorFloatShoveDup, "instructionVectorFloatShoveDup"), + (instructionVectorFloatStackIsEmpty, "instructionVectorFloatStackIsEmpty") + ] + +allVectorCharInstructions :: [Gene] +allVectorCharInstructions = map StateFunc [ + (instructionVectorCharConcat, "instructionVectorCharConcat"), + (instructionVectorCharConj, "instructionVectorCharConj"), + (instructionVectorCharTakeN, "instructionVectorCharTakeN"), + (instructionVectorCharSubVector, "instructionVectorCharSubVector"), + (instructionVectorCharFirst, "instructionVectorCharFirst"), + (instructionVectorCharLast, "instructionVectorCharLast"), + (instructionVectorCharNth, "instructionVectorCharNth"), + (instructionVectorCharRest, "instructionVectorCharRest"), + (instructionVectorCharButLast, "instructionVectorCharButLast"), + (instructionVectorCharLength, "instructionVectorCharLength"), + (instructionVectorCharReverse, "instructionVectorCharReverse"), + (instructionVectorCharPushAll, "instructionVectorCharPushAll"), + (instructionVectorCharMakeEmpty, "instructionVectorCharMakeEmpty"), + (instructionVectorCharIsEmpty, "instructionVectorCharIsEmpty"), + (instructionVectorCharIndexOf, "instructionVectorCharIndexOf"), + (instructionVectorCharOccurrencesOf, "instructionVectorCharOccurrencesOf"), + (instructionVectorCharSetNth, "instructionVectorCharSetNth"), + (instructionVectorCharReplace, "instructionVectorCharReplace"), + (instructionVectorCharReplaceFirst, "instructionVectorCharReplaceFirst"), + (instructionVectorCharRemove, "instructionVectorCharRemove"), + (instructionVectorCharIterate, "instructionVectorCharIterate"), + (instructionVectorCharPop, "instructionVectorCharPop"), + (instructionVectorCharDup, "instructionVectorCharDup"), + (instructionVectorCharDupN, "instructionVectorCharDupN"), + (instructionVectorCharSwap, "instructionVectorCharSwap"), + (instructionVectorCharRot, "instructionVectorCharRot"), + (instructionVectorCharFlush, "instructionVectorCharFlush"), + (instructionVectorCharEq, "instructionVectorCharEq"), + (instructionVectorCharStackDepth, "instructionVectorCharStackDepth"), + (instructionVectorCharYank, "instructionVectorCharYank"), + (instructionVectorCharYankDup, "instructionVectorCharYankDup"), + (instructionVectorCharShove, "instructionVectorCharShove"), + (instructionVectorCharShoveDup, "instructionVectorCharShoveDup"), + (instructionVectorCharStackIsEmpty, "instructionVectorCharStackIsEmpty") + ] + +allVectorStringInstructions :: [Gene] +allVectorStringInstructions = map StateFunc [ + (instructionVectorStringConcat, "instructionVectorStringConcat"), + (instructionVectorStringConj, "instructionVectorStringConj"), + (instructionVectorStringTakeN, "instructionVectorStringTakeN"), + (instructionVectorStringSubVector, "instructionVectorStringSubVector"), + (instructionVectorStringFirst, "instructionVectorStringFirst"), + (instructionVectorStringLast, "instructionVectorStringLast"), + (instructionVectorStringNth, "instructionVectorStringNth"), + (instructionVectorStringRest, "instructionVectorStringRest"), + (instructionVectorStringButLast, "instructionVectorStringButLast"), + (instructionVectorStringLength, "instructionVectorStringLength"), + (instructionVectorStringReverse, "instructionVectorStringReverse"), + (instructionVectorStringPushAll, "instructionVectorStringPushAll"), + (instructionVectorStringMakeEmpty, "instructionVectorStringMakeEmpty"), + (instructionVectorStringIsEmpty, "instructionVectorStringIsEmpty"), + (instructionVectorStringIndexOf, "instructionVectorStringIndexOf"), + (instructionVectorStringOccurrencesOf, "instructionVectorStringOccurrencesOf"), + (instructionVectorStringSetNth, "instructionVectorStringSetNth"), + (instructionVectorStringReplace, "instructionVectorStringReplace"), + (instructionVectorStringReplaceFirst, "instructionVectorStringReplaceFirst"), + (instructionVectorStringRemove, "instructionVectorStringRemove"), + (instructionVectorStringIterate, "instructionVectorStringIterate"), + (instructionVectorStringPop, "instructionVectorStringPop"), + (instructionVectorStringDup, "instructionVectorStringDup"), + (instructionVectorStringDupN, "instructionVectorStringDupN"), + (instructionVectorStringSwap, "instructionVectorStringSwap"), + (instructionVectorStringRot, "instructionVectorStringRot"), + (instructionVectorStringFlush, "instructionVectorStringFlush"), + (instructionVectorStringEq, "instructionVectorStringEq"), + (instructionVectorStringStackDepth, "instructionVectorStringStackDepth"), + (instructionVectorStringYank, "instructionVectorStringYank"), + (instructionVectorStringYankDup, "instructionVectorStringYankDup"), + (instructionVectorStringShove, "instructionVectorStringShove"), + (instructionVectorStringShoveDup, "instructionVectorStringShoveDup"), + (instructionVectorStringStackIsEmpty, "instructionVectorStringStackIsEmpty") + ] + +allVectorBoolInstructions :: [Gene] +allVectorBoolInstructions = map StateFunc [ + (instructionVectorBoolConcat, "instructionVectorBoolConcat"), + (instructionVectorBoolConj, "instructionVectorBoolConj"), + (instructionVectorBoolTakeN, "instructionVectorBoolTakeN"), + (instructionVectorBoolSubVector, "instructionVectorBoolSubVector"), + (instructionVectorBoolFirst, "instructionVectorBoolFirst"), + (instructionVectorBoolLast, "instructionVectorBoolLast"), + (instructionVectorBoolNth, "instructionVectorBoolNth"), + (instructionVectorBoolRest, "instructionVectorBoolRest"), + (instructionVectorBoolButLast, "instructionVectorBoolButLast"), + (instructionVectorBoolLength, "instructionVectorBoolLength"), + (instructionVectorBoolReverse, "instructionVectorBoolReverse"), + (instructionVectorBoolPushAll, "instructionVectorBoolPushAll"), + (instructionVectorBoolMakeEmpty, "instructionVectorBoolMakeEmpty"), + (instructionVectorBoolIsEmpty, "instructionVectorBoolIsEmpty"), + (instructionVectorBoolIndexOf, "instructionVectorBoolIndexOf"), + (instructionVectorBoolOccurrencesOf, "instructionVectorBoolOccurrencesOf"), + (instructionVectorBoolSetNth, "instructionVectorBoolSetNth"), + (instructionVectorBoolReplace, "instructionVectorBoolReplace"), + (instructionVectorBoolReplaceFirst, "instructionVectorBoolReplaceFirst"), + (instructionVectorBoolRemove, "instructionVectorBoolRemove"), + (instructionVectorBoolIterate, "instructionVectorBoolIterate"), + (instructionVectorBoolPop, "instructionVectorBoolPop"), + (instructionVectorBoolDup, "instructionVectorBoolDup"), + (instructionVectorBoolDupN, "instructionVectorBoolDupN"), + (instructionVectorBoolSwap, "instructionVectorBoolSwap"), + (instructionVectorBoolRot, "instructionVectorBoolRot"), + (instructionVectorBoolFlush, "instructionVectorBoolFlush"), + (instructionVectorBoolEq, "instructionVectorBoolEq"), + (instructionVectorBoolStackDepth, "instructionVectorBoolStackDepth"), + (instructionVectorBoolYank, "instructionVectorBoolYank"), + (instructionVectorBoolYankDup, "instructionVectorBoolYankDup"), + (instructionVectorBoolShove, "instructionVectorBoolShove"), + (instructionVectorBoolShoveDup, "instructionVectorBoolShoveDup"), + (instructionVectorBoolStackIsEmpty, "instructionVectorBoolStackIsEmpty") + ] + +allInstructions :: [Gene] +allInstructions = + allIntInstructions <> + allFloatInstructions <> + allBoolInstructions <> + allCharInstructions <> + allCodeInstructions <> + allExecInstructions <> + allStringInstructions <> + allVectorIntInstructions <> + allVectorFloatInstructions <> + allVectorCharInstructions <> + allVectorStringInstructions <> + allVectorBoolInstructions diff --git a/src/HushGP/Instructions/BoolInstructions.hs b/src/HushGP/Instructions/BoolInstructions.hs new file mode 100644 index 0000000..5689349 --- /dev/null +++ b/src/HushGP/Instructions/BoolInstructions.hs @@ -0,0 +1,79 @@ +module HushGP.Instructions.BoolInstructions where + +import HushGP.State +import HushGP.Instructions.GenericInstructions + +instructionBoolFromInt :: State -> State +instructionBoolFromInt state@(State {_int = (i : is), _bool = bs}) = state {_int = is, _bool = (i /= 0) : bs} +instructionBoolFromInt state = state + +instructionBoolFromFloat :: State -> State +instructionBoolFromFloat state@(State {_float = (f : fs), _bool = bs}) = state {_float = fs, _bool = (f /= 0) : bs} +instructionBoolFromFloat state = state + +boolTemplate :: (Bool -> Bool -> Bool) -> State -> State +boolTemplate func state@(State {_bool = (b1 : b2 : bs)}) = state {_bool = func b1 b2 : bs} +boolTemplate _ state = state + +instructionBoolAnd :: State -> State +instructionBoolAnd = boolTemplate (&&) + +instructionBoolInvertFirstThenAnd :: State -> State +instructionBoolInvertFirstThenAnd state@(State {_bool = (b1 : bs)}) = boolTemplate (&&) state {_bool = not b1 : bs} +instructionBoolInvertFirstThenAnd state = state + +instructionBoolInvertSecondThenAnd :: State -> State +instructionBoolInvertSecondThenAnd state@(State {_bool = (b1 : b2 : bs)}) = boolTemplate (&&) state {_bool = b1 : not b2 : bs} +instructionBoolInvertSecondThenAnd state = state + +instructionBoolOr :: State -> State +instructionBoolOr = boolTemplate (||) + +-- no builtin haskell xor moment +xor :: Bool -> Bool -> Bool +xor b1 b2 + | b1 && not b2 = True + | not b1 && b2 = True + | otherwise = False + +instructionBoolXor :: State -> State +instructionBoolXor = boolTemplate xor + +instructionBoolPop :: State -> State +instructionBoolPop state = instructionPop state bool + +instructionBoolDup :: State -> State +instructionBoolDup state = instructionDup state bool + +instructionBoolDupN :: State -> State +instructionBoolDupN state = instructionDupN state bool + +instructionBoolSwap :: State -> State +instructionBoolSwap state = instructionSwap state bool + +instructionBoolRot :: State -> State +instructionBoolRot state = instructionRot state bool + +instructionBoolFlush :: State -> State +instructionBoolFlush state = instructionFlush state bool + +instructionBoolEq :: State -> State +instructionBoolEq state = instructionEq state bool + +instructionBoolStackDepth :: State -> State +instructionBoolStackDepth state = instructionStackDepth state bool + +instructionBoolYank :: State -> State +instructionBoolYank state = instructionYank state bool + +instructionBoolYankDup :: State -> State +instructionBoolYankDup state = instructionYankDup state bool + +instructionBoolShove :: State -> State +instructionBoolShove state = instructionShove state bool + +instructionBoolShoveDup :: State -> State +instructionBoolShoveDup state = instructionShoveDup state bool + +instructionBoolIsEmpty :: State -> State +instructionBoolIsEmpty state = instructionIsEmpty state bool diff --git a/src/HushGP/Instructions/CharInstructions.hs b/src/HushGP/Instructions/CharInstructions.hs new file mode 100644 index 0000000..3150ba3 --- /dev/null +++ b/src/HushGP/Instructions/CharInstructions.hs @@ -0,0 +1,89 @@ +module HushGP.Instructions.CharInstructions where + +import Data.Char +import HushGP.State +import HushGP.Instructions.StringInstructions (wschars) +import HushGP.Instructions.GenericInstructions + +intToAscii :: Integral a => a -> Char +intToAscii val = chr (abs (fromIntegral val) `mod` 128) + +instructionCharConcat :: State -> State +instructionCharConcat state@(State {_char = c1 : c2 : cs, _string = ss}) = state{_char = cs, _string = [c1, c2] : ss} +instructionCharConcat state = state + +instructionCharFromFirstChar :: State -> State +instructionCharFromFirstChar state = instructionVectorFirst state char string + +instructionCharFromLastChar :: State -> State +instructionCharFromLastChar state = instructionVectorLast state char string + +instructionCharFromNthChar :: State -> State +instructionCharFromNthChar state = instructionVectorNth state char string + +instructionCharIsWhitespace :: State -> State +instructionCharIsWhitespace state@(State {_char = c1 : cs, _bool = bs}) = state{_char = cs, _bool = (c1 `elem` wschars) : bs} +instructionCharIsWhitespace state = state + +instructionCharIsLetter :: State -> State +instructionCharIsLetter state@(State {_char = c1 : cs, _bool = bs}) = state{_char = cs, _bool = isAlpha c1 : bs} +instructionCharIsLetter state = state + +instructionCharIsDigit :: State -> State +instructionCharIsDigit state@(State {_char = c1 : cs, _bool = bs}) = state{_char = cs, _bool = isDigit c1 : bs} +instructionCharIsDigit state = state + +instructionCharFromBool :: State -> State +instructionCharFromBool state@(State {_char = cs, _bool = b1 : bs}) = state{_char = (if b1 then 'T' else 'F') : cs, _bool = bs} +instructionCharFromBool state = state + +instructionCharFromAsciiInt :: State -> State +instructionCharFromAsciiInt state@(State {_char = cs, _int = i1 : is}) = state{_char = intToAscii i1 : cs, _int = is} +instructionCharFromAsciiInt state = state + +instructionCharFromAsciiFloat :: State -> State +instructionCharFromAsciiFloat state@(State {_char = cs, _float = f1 : fs}) = state{_char = intToAscii @Integer (floor f1) : cs, _float = fs} +instructionCharFromAsciiFloat state = state + +instructionCharsFromString :: State -> State +instructionCharsFromString state@(State {_char = cs, _string = s1 : ss}) = state{_char = s1 <> cs, _string = ss} +instructionCharsFromString state = state + +instructionCharPop :: State -> State +instructionCharPop state = instructionPop state char + +instructionCharDup :: State -> State +instructionCharDup state = instructionDup state char + +instructionCharDupN :: State -> State +instructionCharDupN state = instructionDupN state char + +instructionCharSwap :: State -> State +instructionCharSwap state = instructionSwap state char + +instructionCharRot :: State -> State +instructionCharRot state = instructionRot state char + +instructionCharFlush :: State -> State +instructionCharFlush state = instructionFlush state char + +instructionCharEq :: State -> State +instructionCharEq state = instructionEq state char + +instructionCharStackDepth :: State -> State +instructionCharStackDepth state = instructionStackDepth state char + +instructionCharYank :: State -> State +instructionCharYank state = instructionYank state char + +instructionCharYankDup :: State -> State +instructionCharYankDup state = instructionYankDup state char + +instructionCharIsEmpty :: State -> State +instructionCharIsEmpty state = instructionIsEmpty state char + +instructionCharShove :: State -> State +instructionCharShove state = instructionShove state char + +instructionCharShoveDup :: State -> State +instructionCharShoveDup state = instructionShoveDup state char diff --git a/src/HushGP/Instructions/CodeInstructions.hs b/src/HushGP/Instructions/CodeInstructions.hs new file mode 100644 index 0000000..f7f069c --- /dev/null +++ b/src/HushGP/Instructions/CodeInstructions.hs @@ -0,0 +1,310 @@ +module HushGP.Instructions.CodeInstructions where + +import Data.List (elemIndex) +import HushGP.State +import HushGP.Instructions.GenericInstructions +import HushGP.Instructions.IntInstructions +-- import Debug.Trace + +isBlock :: Gene -> Bool +isBlock (Block _) = True +isBlock _ = False + +blockLength :: Gene -> Int +blockLength (Block xs) = length xs +blockLength _ = 1 + +blockIsNull :: Gene -> Bool +blockIsNull (Block xs) = null xs +blockIsNull _ = False + +-- I think I can abstract the boilerplate base case check for a lot of these +-- with a different function + +-- empty Blocks are a thing but that shouldn't really matter +extractFirstFromBlock :: Gene -> Gene +extractFirstFromBlock (Block (x : _)) = x +extractFirstFromBlock gene = gene + +extractLastFromBlock :: Gene -> Gene +extractLastFromBlock (Block []) = Block [] +extractLastFromBlock (Block xs) = last xs +extractLastFromBlock gene = gene + +extractInitFromBlock :: Gene -> Gene +extractInitFromBlock (Block []) = Block [] +extractInitFromBlock (Block xs) = Block (init xs) +extractInitFromBlock gene = gene + +extractTailFromBlock :: Gene -> Gene +extractTailFromBlock (Block xs) = Block (drop 1 xs) +extractTailFromBlock gene = gene + +-- This function took at least 3 hours to program. +codeAtPoint :: [Gene] -> Int -> Gene +codeAtPoint (gene : _) 0 = gene +codeAtPoint [] _ = Block [] -- Should only happen if an empty block is last Gene in the list of Genes +codeAtPoint ((Block nestedGenes) : genes) index = codeAtPoint (nestedGenes <> genes) (index - 1) +codeAtPoint (_ : genes) index = codeAtPoint genes (index - 1) + +codeInsertAtPoint :: [Gene] -> Gene -> Int -> [Gene] +codeInsertAtPoint oldGenes gene 0 = gene : oldGenes +codeInsertAtPoint [] gene _ = [gene] -- This shouldn't happen (lol) +codeInsertAtPoint ((Block genes) : oldGenes) gene index = Block (codeInsertAtPoint genes gene (index - 1)) : oldGenes +codeInsertAtPoint (oldGene : oldGenes) gene index = oldGene : codeInsertAtPoint oldGenes gene (index - 1) + +-- This one functions differently than pysh. +-- I like this one because it preserves ordering in the second case +codeCombine :: Gene -> Gene -> Gene +codeCombine (Block xs) (Block ys) = Block (xs <> ys) +codeCombine (Block xs) ygene = Block (xs <> [ygene]) +codeCombine xgene (Block ys) = Block (xgene : ys) +codeCombine xgene ygene = Block [xgene, ygene] + +codeMember :: Gene -> Gene -> Bool +codeMember (Block _) (Block _) = False -- Can't compare two lists with `elem` +codeMember (Block xs) ygene = ygene `elem` xs +codeMember _ _ = False + +codeRecursiveSize :: Gene -> Int +codeRecursiveSize (Block xs) = sum [codeRecursiveSize x + if isBlock x then 1 else 0 | x <- xs] +codeRecursiveSize _ = 1 + +instructionCodePop :: State -> State +instructionCodePop state = instructionPop state code + +instructionCodeIsCodeBlock :: State -> State +instructionCodeIsCodeBlock state@(State {_code = (c : cs), _bool = bs}) = state {_code = cs, _bool = isBlock c : bs} +instructionCodeIsCodeBlock state = state + +instructionCodeIsSingular :: State -> State +instructionCodeIsSingular state@(State {_code = (c : cs), _bool = bs}) = state {_code = cs, _bool = not (isBlock c) : bs} +instructionCodeIsSingular state = state + +instructionCodeLength :: State -> State +instructionCodeLength state@(State {_code = (c : cs), _int = is}) = state {_code = cs, _int = blockLength c : is} +instructionCodeLength state = state + +instructionCodeFirst :: State -> State +instructionCodeFirst state@(State {_code = (c : cs)}) = state {_code = extractFirstFromBlock c : cs} +instructionCodeFirst state = state + +instructionCodeLast :: State -> State +instructionCodeLast state@(State {_code = (c : cs)}) = state {_code = extractLastFromBlock c : cs} +instructionCodeLast state = state + +-- https://erp12.github.io/pyshgp/html/core_instructions.html#code-rest +instructionCodeTail :: State -> State +instructionCodeTail state@(State {_code = (c : cs)}) = state {_code = extractTailFromBlock c : cs} +instructionCodeTail state = state + +-- https://erp12.github.io/pyshgp/html/core_instructions.html#code-but-last +instructionCodeInit :: State -> State +instructionCodeInit state@(State {_code = (c : cs)}) = state {_code = extractInitFromBlock c : cs} +instructionCodeInit state = state + +instructionCodeWrap :: State -> State +instructionCodeWrap state@(State {_code = (c : cs)}) = state {_code = Block [c] : cs} +instructionCodeWrap state = state + +instructionCodeList :: State -> State +instructionCodeList state@(State {_code = (c1 : c2 : cs)}) = state {_code = Block [c1, c2] : cs} +instructionCodeList state = state + +instructionCodeCombine :: State -> State +instructionCodeCombine state@(State {_code = (c1 : c2 : cs)}) = state {_code = codeCombine c1 c2 : cs} +instructionCodeCombine state = state + +instructionCodeDo :: State -> State +instructionCodeDo state@(State {_code = (c1 : cs), _exec = es}) = state {_code = cs, _exec = c1: es} +instructionCodeDo state = state + +instructionCodeDoDup :: State -> State +instructionCodeDoDup state@(State {_code = (c1 : cs), _exec = es}) = state {_code = c1 : cs, _exec = c1 : es} +instructionCodeDoDup state = state + +-- https://erp12.github.io/pyshgp/html/core_instructions.html#code-do-then-pop +instructionCodeDoThenPop :: State -> State +instructionCodeDoThenPop state@(State {_code = c1 : _, _exec = es}) = state {_exec = c1 : StateFunc (instructionCodePop, "instructionCodePop") : es} +instructionCodeDoThenPop state = state + +codeFromExec :: Gene +codeFromExec = StateFunc (instructionCodeFromExec, "instructionCodeFromExec") + +codeDoRange :: Gene +codeDoRange = StateFunc (instructionCodeDoRange, "instructionCodeDoRange") + +instructionCodeDoRange :: State -> State +instructionCodeDoRange state@(State {_code = (c1 : cs), _int = (i0 : i1 : is), _exec = es}) = + if increment i0 i1 /= 0 + then state {_exec = c1 : Block [GeneInt (i1 + increment i0 i1), GeneInt i0, codeFromExec, c1, codeDoRange] : es, _int = i1 : is, _code = cs} + else state {_exec = c1: es, _int = i1 : is, _code = cs} + where + increment :: Int -> Int -> Int + increment destIdx currentIdx + | currentIdx < destIdx = 1 + | currentIdx > destIdx = -1 + | otherwise = 0 +instructionCodeDoRange state = state + +instructionCodeDoCount :: State -> State +instructionCodeDoCount state@(State {_code = (c : cs), _int = (i : is), _exec = es}) = + if i < 1 + then state + else state {_code = cs, _int = is, _exec = Block [GeneInt 0, GeneInt $ i - 1, codeFromExec, c, codeDoRange] : es} +instructionCodeDoCount state = state + +instructionCodeDoTimes :: State -> State +instructionCodeDoTimes state@(State {_code = (c : cs), _int = (i : is), _exec = es}) = + if i < 1 + then state + else state {_code = cs, _int = is, _exec = Block [GeneInt 0, GeneInt $ i - 1, codeFromExec, Block [StateFunc (instructionIntPop, "instructionIntPop"), c], codeDoRange] : es} +instructionCodeDoTimes state = state + +instructionCodeIf :: State -> State +instructionCodeIf state@(State {_code = (c1 : c2 : cs), _bool = (b1 : bs), _exec = es}) = state{_code = cs, _bool = bs, _exec = (if b1 then c1 else c2) : es} +instructionCodeIf state = state + +instructionCodeWhen :: State -> State +instructionCodeWhen state@(State {_code = (c1 : cs), _bool = (b1 : bs), _exec = es}) = state{_code = cs, _bool = bs, _exec = if b1 then c1 : es else es} +instructionCodeWhen state = state + +instructionCodeMember :: State -> State +instructionCodeMember state@(State {_code = (c1 : c2 : cs), _bool = bs}) = state{_code = cs, _bool = codeMember c1 c2 : bs} +instructionCodeMember state = state + +-- This one doesn't count the recursive Blocks while instructionCodeExtract does +-- https://erp12.github.io/pyshgp/html/core_instructions.html#code-nth +instructionCodeN :: State -> State +instructionCodeN state@(State {_code = ((Block c1) : cs), _int = (i1 : is)}) = + if not $ null c1 + then state {_code = c1 !! index : cs, _int = is} + else state + where + index :: Int + index = abs i1 `mod` length c1 +instructionCodeN state@(State {_code = (c1 : cs), _int = _ : is}) = state {_code = c1 : cs, _int = is} +instructionCodeN state = state + +instructionMakeEmptyCodeBlock :: State -> State +instructionMakeEmptyCodeBlock state@(State {_code = cs}) = state {_code = Block [] : cs} + +instructionIsEmptyCodeBlock :: State -> State +instructionIsEmptyCodeBlock state@(State {_code = Block c1 : cs, _bool = bs}) = state{_code = cs, _bool = null c1 : bs} +instructionIsEmptyCodeBlock state@(State {_bool = bs}) = state{_bool = False : bs} + +instructionCodeSize :: State -> State +instructionCodeSize state@(State {_code = c1 : cs, _int = is}) = state{_code = cs, _int = codeRecursiveSize c1 : is} +instructionCodeSize state = state + +-- There's a bug for this instruction in pysh where the last item in the +-- top level Block isn't counted, and if passed 0, then the entire codeblock is returned. +-- I designed this function differently so 0 returns the 0th element, and the last item +-- in the codeblock can be returned. +instructionCodeExtract :: State -> State +instructionCodeExtract state@(State {_code = (block@(Block c1) : cs), _int = i1 : is}) = + let + index = abs i1 `mod` codeRecursiveSize block + in + state{_code = codeAtPoint c1 index : cs, _int = is} +instructionCodeExtract state@(State {_code = cs, _int = _ : is}) = state{_code = cs, _int = is} +instructionCodeExtract state = state + +instructionCodeInsert :: State -> State +instructionCodeInsert state@(State {_code = (block@(Block c1) : c2 : cs), _int = i1 : is}) = + let + index = abs i1 `mod` codeRecursiveSize block + in + state{_code = Block (codeInsertAtPoint c1 c2 index) : cs, _int = is} +instructionCodeInsert state@(State {_code = c1 : c2 : cs, _int = i1 : is}) = + let + index = abs i1 `mod` codeRecursiveSize (Block [c1]) + in + state{_code = Block (codeInsertAtPoint [c1] c2 index) : cs, _int = is} +instructionCodeInsert state = state + +instructionCodeFirstPosition :: State -> State +instructionCodeFirstPosition state@(State {_code = (Block []) : c2 : cs, _int = is}) = state {_code = cs, _int = (if c2 == Block [] then 0 else -1) : is} +instructionCodeFirstPosition state@(State {_code = (Block c1) : c2 : cs, _int = is}) = state {_code = cs, _int = positionElem c1 c2 : is} + where + -- This is really not gonna be good for StateFunc + positionElem :: [Gene] -> Gene -> Int + positionElem genes gene = + case elemIndex gene genes of + Nothing -> -1 + Just x -> x +instructionCodeFirstPosition state@(State {_code = c1 : c2 : cs, _int = is}) = state {_code = cs, _int = (if c1 == c2 then 0 else -1) : is} +instructionCodeFirstPosition state = state + +instructionCodeReverse :: State -> State +instructionCodeReverse state@(State {_code = (Block c1) : cs}) = state {_code = Block (reverse c1) : cs} +instructionCodeReverse state = state + +instructionCodeDup :: State -> State +instructionCodeDup state = instructionDup state code + +instructionCodeDupN :: State -> State +instructionCodeDupN state = instructionDupN state code + +instructionCodeSwap :: State -> State +instructionCodeSwap state = instructionSwap state code + +instructionCodeRot :: State -> State +instructionCodeRot state = instructionRot state code + +instructionCodeFlush :: State -> State +instructionCodeFlush state = instructionFlush state code + +instructionCodeEq :: State -> State +instructionCodeEq state = instructionEq state code + +instructionCodeStackDepth :: State -> State +instructionCodeStackDepth state = instructionStackDepth state code + +instructionCodeYank :: State -> State +instructionCodeYank state = instructionYank state code + +instructionCodeYankDup :: State -> State +instructionCodeYankDup state = instructionYankDup state code + +instructionCodeStackIsEmpty :: State -> State +instructionCodeStackIsEmpty state = instructionIsEmpty state code + +instructionCodeShove :: State -> State +instructionCodeShove state = instructionShove state code + +instructionCodeShoveDup :: State -> State +instructionCodeShoveDup state = instructionShoveDup state code + +instructionCodeFromBool :: State -> State +instructionCodeFromBool state = instructionCodeFrom state bool GeneBool + +instructionCodeFromInt :: State -> State +instructionCodeFromInt state = instructionCodeFrom state int GeneInt + +instructionCodeFromChar :: State -> State +instructionCodeFromChar state = instructionCodeFrom state char GeneChar + +instructionCodeFromFloat :: State -> State +instructionCodeFromFloat state = instructionCodeFrom state float GeneFloat + +instructionCodeFromString :: State -> State +instructionCodeFromString state = instructionCodeFrom state string GeneString + +instructionCodeFromVectorInt :: State -> State +instructionCodeFromVectorInt state = instructionCodeFrom state vectorInt GeneVectorInt + +instructionCodeFromVectorFloat :: State -> State +instructionCodeFromVectorFloat state = instructionCodeFrom state vectorFloat GeneVectorFloat + +instructionCodeFromVectorString :: State -> State +instructionCodeFromVectorString state = instructionCodeFrom state vectorString GeneVectorString + +instructionCodeFromVectorBool :: State -> State +instructionCodeFromVectorBool state = instructionCodeFrom state vectorBool GeneVectorBool + +instructionCodeFromVectorChar :: State -> State +instructionCodeFromVectorChar state = instructionCodeFrom state vectorChar GeneVectorChar + +instructionCodeFromExec :: State -> State +instructionCodeFromExec state = instructionCodeFrom state exec id diff --git a/src/HushGP/Instructions/ExecInstructions.hs b/src/HushGP/Instructions/ExecInstructions.hs new file mode 100644 index 0000000..4b77aa7 --- /dev/null +++ b/src/HushGP/Instructions/ExecInstructions.hs @@ -0,0 +1,106 @@ +module HushGP.Instructions.ExecInstructions where + +import HushGP.State +import HushGP.Instructions.IntInstructions +import HushGP.Instructions.GenericInstructions + +instructionExecIf :: State -> State +instructionExecIf state@(State {_exec = (e1 : e2 : es), _bool = (b : bs)}) = + if b + then state {_exec = e1 : es, _bool = bs} + else state {_exec = e2 : es, _bool = bs} +instructionExecIf state = state + +instructionExecDup :: State -> State +instructionExecDup state = instructionDup state exec + +instructionExecDupN :: State -> State +instructionExecDupN state = instructionDupN state exec + +instructionExecPop :: State -> State +instructionExecPop state = instructionPop state exec + +instructionExecSwap :: State -> State +instructionExecSwap state = instructionSwap state exec + +instructionExecRot :: State -> State +instructionExecRot state = instructionRot state exec + +instructionExecFlush :: State -> State +instructionExecFlush state = instructionFlush state exec + +instructionExecEq :: State -> State +instructionExecEq state = instructionEq state exec + +instructionExecStackDepth :: State -> State +instructionExecStackDepth state = instructionStackDepth state exec + +instructionExecYank :: State -> State +instructionExecYank state = instructionYank state exec + +instructionExecYankDup :: State -> State +instructionExecYankDup state = instructionYankDup state exec + +instructionExecShove :: State -> State +instructionExecShove state = instructionShove state exec + +instructionExecShoveDup :: State -> State +instructionExecShoveDup state = instructionShoveDup state exec + +instructionExecIsEmpty :: State -> State +instructionExecIsEmpty state = instructionIsEmpty state exec + +execDoRange :: Gene +execDoRange = StateFunc (instructionExecDoRange, "instructionExecDoRange") + +instructionExecDoRange :: State -> State +instructionExecDoRange state@(State {_exec = (e1 : es), _int = (i0 : i1 : is)}) = + if increment i0 i1 /= 0 + then state {_exec = e1 : Block [GeneInt (i1 + increment i0 i1), GeneInt i0, execDoRange, e1] : es, _int = i1 : is} + else state {_exec = e1 : es, _int = i1 : is} + where + increment :: Int -> Int -> Int + increment destIdx currentIdx + | currentIdx < destIdx = 1 + | currentIdx > destIdx = -1 + | otherwise = 0 +instructionExecDoRange state = state + +instructionExecDoCount :: State -> State +instructionExecDoCount state@(State {_exec = (e : es), _int = (i : is)}) = + if i < 1 + then state + else state {_exec = Block [GeneInt 0, GeneInt $ i - 1, execDoRange, e] : es, _int = is} +instructionExecDoCount state = state + +instructionExecDoTimes :: State -> State +instructionExecDoTimes state@(State {_exec = (e : es), _int = (i : is)}) = + if i < 1 + then state + else state {_exec = Block [GeneInt 0, GeneInt $ i - 1, execDoRange, Block [StateFunc (instructionIntPop, "instructionIntPop"), e]] : es, _int = is} +instructionExecDoTimes state = state + +execWhile :: Gene +execWhile = StateFunc (instructionExecWhile, "instructionExecWhile") + +instructionExecWhile :: State -> State +instructionExecWhile state@(State {_exec = (_ : es), _bool = []}) = + state {_exec = es} +instructionExecWhile state@(State {_exec = alles@(e : es), _bool = (b : bs)}) = + if b + then state {_exec = e : execWhile : alles, _bool = bs} + else state {_exec = es} +instructionExecWhile state = state + +instructionExecDoWhile :: State -> State +instructionExecDoWhile state@(State {_exec = alles@(e : _)}) = + state {_exec = e : execWhile : alles} +instructionExecDoWhile state = state + +-- Eats the _boolean no matter what +instructionExecWhen :: State -> State +instructionExecWhen state@(State {_exec = (_ : es), _bool = (b : bs)}) = + if not b + then state {_exec = es, _bool = bs} + else state {_bool = bs} +instructionExecWhen state = state diff --git a/src/HushGP/Instructions/FloatInstructions.hs b/src/HushGP/Instructions/FloatInstructions.hs new file mode 100644 index 0000000..a9eb409 --- /dev/null +++ b/src/HushGP/Instructions/FloatInstructions.hs @@ -0,0 +1,116 @@ +module HushGP.Instructions.FloatInstructions where + +import Data.Fixed (mod') +import HushGP.Instructions.GenericInstructions +import HushGP.State + +instructionFloatFromInt :: State -> State +instructionFloatFromInt state@(State {_float = fs, _int = (i : is)}) = state {_float = (fromIntegral i :: Float) : fs, _int = is} +instructionFloatFromInt state = state + +instructionFloatFromBool :: State -> State +instructionFloatFromBool state@(State {_bool = (b : bs), _float = fs}) = state {_bool = bs, _float = (if b then 1.0 else 0.0) : fs} +instructionFloatFromBool state = state + +instructionFloatAdd :: State -> State +instructionFloatAdd state@(State {_float = (f1 : f2 : fs)}) = state {_float = f2 + f1 : fs} +instructionFloatAdd state = state + +instructionFloatSub :: State -> State +instructionFloatSub state@(State {_float = (f1 : f2 : fs)}) = state {_float = f2 - f1 : fs} +instructionFloatSub state = state + +instructionFloatMul :: State -> State +instructionFloatMul state@(State {_float = (f1 : f2 : fs)}) = state {_float = f2 * f1 : fs} +instructionFloatMul state = state + +instructionFloatDiv :: State -> State +instructionFloatDiv state@(State {_float = (f1 : f2 : fs)}) = state {_float = if f1 /= 0 then f2 / f1 : fs else f1 : f2 : fs} +instructionFloatDiv state = state + +instructionFloatMod :: State -> State +instructionFloatMod state@(State {_float = (f1 : f2 : fs)}) = state {_float = if f1 /= 0 then f2 `mod'` f1 : fs else f1 : f2 : fs} +instructionFloatMod state = state + +instructionFloatMin :: State -> State +instructionFloatMin state@(State {_float = (f1 : f2 : fs)}) = state {_float = min f1 f2 : fs} +instructionFloatMin state = state + +instructionFloatMax :: State -> State +instructionFloatMax state@(State {_float = (f1 : f2 : fs)}) = state {_float = max f1 f2 : fs} +instructionFloatMax state = state + +instructionFloatInc :: State -> State +instructionFloatInc state@(State {_float = (f1 : fs)}) = state {_float = f1 + 1 : fs} +instructionFloatInc state = state + +instructionFloatDec :: State -> State +instructionFloatDec state@(State {_float = (f1 : fs)}) = state {_float = f1 - 1 : fs} +instructionFloatDec state = state + +instructionFloatLT :: State -> State +instructionFloatLT state@(State {_float = f1 : f2 : fs, _bool = bs}) = state {_float = fs, _bool = (f1 < f2) : bs} +instructionFloatLT state = state + +instructionFloatGT :: State -> State +instructionFloatGT state@(State {_float = f1 : f2 : fs, _bool = bs}) = state {_float = fs, _bool = (f1 > f2) : bs} +instructionFloatGT state = state + +instructionFloatLTE :: State -> State +instructionFloatLTE state@(State {_float = f1 : f2 : fs, _bool = bs}) = state {_float = fs, _bool = (f1 <= f2) : bs} +instructionFloatLTE state = state + +instructionFloatGTE :: State -> State +instructionFloatGTE state@(State {_float = f1 : f2 : fs, _bool = bs}) = state {_float = fs, _bool = (f1 >= f2) : bs} +instructionFloatGTE state = state + +instructionFloatPop :: State -> State +instructionFloatPop state = instructionPop state float + +instructionFloatDup :: State -> State +instructionFloatDup state = instructionDup state float + +instructionFloatDupN :: State -> State +instructionFloatDupN state = instructionDupN state float + +instructionFloatSwap :: State -> State +instructionFloatSwap state = instructionSwap state float + +instructionFloatRot :: State -> State +instructionFloatRot state = instructionRot state float + +instructionFloatFlush :: State -> State +instructionFloatFlush state = instructionFlush state float + +instructionFloatEq :: State -> State +instructionFloatEq state = instructionEq state float + +instructionFloatStackDepth :: State -> State +instructionFloatStackDepth state = instructionStackDepth state float + +instructionFloatYankDup :: State -> State +instructionFloatYankDup state = instructionYankDup state float + +instructionFloatYank :: State -> State +instructionFloatYank state = instructionYank state float + +instructionFloatShoveDup :: State -> State +instructionFloatShoveDup state = instructionShoveDup state float + +instructionFloatShove :: State -> State +instructionFloatShove state = instructionShove state float + +instructionFloatIsEmpty :: State -> State +instructionFloatIsEmpty state = instructionIsEmpty state float + +instructionFloatSin :: State -> State +instructionFloatSin state@(State {_float = f1 : fs}) = state {_float = sin f1 : fs} +instructionFloatSin state = state + +instructionFloatCos :: State -> State +instructionFloatCos state@(State {_float = f1 : fs}) = state {_float = cos f1 : fs} +instructionFloatCos state = state + +instructionFloatTan :: State -> State +instructionFloatTan state@(State {_float = f1 : fs}) = state {_float = tan f1 : fs} +instructionFloatTan state = state diff --git a/src/HushGP/Instructions/GenericInstructions.hs b/src/HushGP/Instructions/GenericInstructions.hs new file mode 100644 index 0000000..8c474ef --- /dev/null +++ b/src/HushGP/Instructions/GenericInstructions.hs @@ -0,0 +1,348 @@ +module HushGP.Instructions.GenericInstructions where + +import Control.Lens +import HushGP.State + +-- import Debug.Trace + +deleteAt :: Int -> [a] -> [a] +deleteAt idx xs = take idx xs <> drop 1 (drop idx xs) + +-- I could probably just combine these functions +combineTuple :: a -> ([a], [a]) -> [a] +combineTuple val tup = fst tup <> [val] <> snd tup + +combineTupleList :: [a] -> ([a], [a]) -> [a] +combineTupleList val tup = fst tup <> val <> snd tup + +insertAt :: Int -> a -> [a] -> [a] +insertAt idx val xs = combineTuple val (splitAt idx xs) + +replaceAt :: Int -> a -> [a] -> [a] +replaceAt idx val xs = deleteAt (idx + 1) (insertAt idx val xs) + +subList :: Int -> Int -> [a] -> [a] +subList idx0 idx1 xs = + let + (start, end) = if idx0 <= idx1 then (idx0, idx1) else (idx1, idx0) + adjStart = max 0 start + adjEnd = min end (length xs) + in + take adjEnd (drop adjStart xs) + +-- Maybe could've used Data.List.isSubsequenceOf :shrug: +findSubA :: forall a. Eq a => [a] -> [a] -> Int +findSubA fullA subA + | length fullA < length subA = -1 + | length fullA == length subA = if fullA == subA then 0 else -1 + | otherwise = findSubA' fullA subA 0 + where + findSubA' :: [a] -> [a] -> Int -> Int + findSubA' fA sA subIndex + | null fA = -1 + | length sA > length fA = -1 + | sA == take (length sA) fA = subIndex + | otherwise = findSubA' (drop 1 fA) sA (subIndex + 1) + +-- The int is the amount of olds to replace with new +-- Just chain findSubA calls lol +-- Nothing means replace all +-- May not be the most efficient method with the findSubA calls +replace :: Eq a => [a] -> [a] -> [a] -> Maybe Int -> [a] +replace fullA old new (Just amt) = + if findSubA fullA old /= -1 && amt > 0 + then replace (take (findSubA fullA old) fullA <> new <> drop (findSubA fullA old + length old) fullA) old new (Just $ amt - 1) + else fullA +replace fullA old new Nothing = + if findSubA fullA old /= -1 + then replace (take (findSubA fullA old) fullA <> new <> drop (findSubA fullA old + length old) fullA) old new Nothing + else fullA + +-- a rather inefficient search +amtOccurences :: forall a. Eq a => [a] -> [a] -> Int +amtOccurences fullA subA = amtOccurences' fullA subA 0 + where + amtOccurences' :: [a] -> [a] -> Int -> Int + amtOccurences' fA sA count = + if findSubA fA sA /= -1 + then amtOccurences' (replace fA sA mempty (Just 1)) sA (count + 1) + else count + +takeR :: Int -> [a] -> [a] +takeR amt fullA = drop (length fullA - amt) fullA + +dropR :: Int -> [a] -> [a] +dropR amt fullA = take (length fullA - amt) fullA + +safeInit :: [a] -> [a] +safeInit [] = [] +safeInit xs = init xs + +absNum :: Integral a => a -> [b] -> Int +absNum rawNum lst = abs (fromIntegral rawNum) `mod` length lst + +notEmptyStack :: State -> Lens' State [a] -> Bool +notEmptyStack state accessor = not . null $ view accessor state + +instructionDup :: State -> Lens' State [a] -> State +instructionDup state accessor = + case uncons (view accessor state) of + Nothing -> state + Just (x,_) -> state & accessor .~ x : view accessor state + +instructionPop :: State -> Lens' State [a] -> State +instructionPop state accessor = state & accessor .~ drop 1 (view accessor state) + +instructionIsEmpty :: State -> Lens' State [a] -> State +instructionIsEmpty state@(State {_bool = bs}) accessor = state{_bool = null (view accessor state) : bs} + +-- instructionPop :: State -> Lens' State [a] -> State +-- instructionPop state accessor = if notEmptyStack state accessor then instructionPop state accessor else state + +-- I might be able to move some of the int stack error checking +-- to the integer call. For now this may be a tad inefficient. +instructionDupN :: forall a. Show a => State -> Lens' State [a] -> State +instructionDupN state accessor = + case uncons (view int state) of + Just (i1,is) -> + case uncons (view accessor state{_int = is}) of + Just (a1,as) -> + instructionDupNHelper i1 a1 accessor (state{_int = is} & accessor .~ as) + _ -> state + _ -> state + where + instructionDupNHelper :: Int -> a -> Lens' State [a] -> State -> State + instructionDupNHelper count instruction internalAccessor internalState = + if count > 0 + then instructionDupNHelper (count - 1) instruction internalAccessor (internalState & accessor .~ (instruction : view accessor internalState)) + else internalState + +instructionSwap :: State -> Lens' State [a] -> State +instructionSwap state accessor = + state & accessor .~ swapper (view accessor state) + where + swapper :: [a] -> [a] + swapper (x1 : x2 : xs) = x2 : x1 : xs + swapper xs = xs + +-- Rotates top 3 integers +-- We could use template haskell to rotate any number of these as +-- an instruction later. Template haskell seems very complicated tho. +instructionRot :: State -> Lens' State [a] -> State +instructionRot state accessor = + state & accessor .~ rotator (view accessor state) + where + rotator :: [a] -> [a] + rotator (x1 : x2 : x3 : xs) = x3 : x1 : x2 : xs + rotator xs = xs + +instructionFlush :: State -> Lens' State [a] -> State +instructionFlush state accessor = state & accessor .~ [] + +instructionEq :: forall a. Eq a => State -> Lens' State [a] -> State +instructionEq state accessor = + case uncons $ view accessor state of + Nothing -> state + Just (x1, x2 : _) -> droppedState & bool .~ (x1 == x2) : view bool droppedState + Just _ -> state + where + droppedState :: State + droppedState = state & accessor .~ drop 2 (view accessor state) + +instructionStackDepth :: State -> Lens' State [a] -> State +instructionStackDepth state@(State {_int = is}) accessor = state{_int = length (view accessor state) : is} + +instructionYankDup :: State -> Lens' State [a] -> State +instructionYankDup state@(State {_int = i : is}) accessor = + if notEmptyStack state accessor + then state{_int = is} & accessor .~ (view accessor state{_int = is} !! max 0 (min i (length (view accessor state{_int = is}) - 1))) : view accessor state{_int = is} + else state +instructionYankDup state _ = state + +instructionYank :: forall a. State -> Lens' State [a] -> State +instructionYank state@(State {_int = i : is}) accessor = + let + myIndex :: Int + myIndex = max 0 (min i (length (view accessor state{_int = is}) - 1)) + item :: a + item = view accessor state{_int = is} !! myIndex + deletedState :: State + deletedState = state{_int = is} & accessor .~ deleteAt myIndex (view accessor state{_int = is}) + in + if notEmptyStack state{_int = is} accessor then deletedState & accessor .~ item : view accessor deletedState else state +instructionYank state _ = state + +-- In pysh, instructionShoveDup and instructionShove behave differently when indexing in such a way that +-- the duplicated index matters whether or not it's present in the stack at the moment of calculation. +-- I'm not going to keep this behavior. Check out interpysh examples for how pysh handles it. +instructionShoveDup :: State -> Lens' State [a] -> State +instructionShoveDup state@(State {_int = i : is}) accessor = + case uncons (view accessor state{_int = is}) of + Just (x,_) -> state{_int = is} & accessor .~ combineTuple x (splitAt (max 0 (min i (length (view accessor state{_int = is}) - 1))) (view accessor state{_int = is})) + _ -> state +instructionShoveDup state _ = state + +instructionShove :: State -> Lens' State [a] -> State +instructionShove state accessor = instructionShoveDup state accessor & accessor .~ drop 1 (view accessor (instructionShoveDup state accessor)) + +-- not char generic +instructionConcat :: Semigroup a => State -> Lens' State [a] -> State +instructionConcat state accessor = + case uncons (view accessor state) of + Just (x1, x2:_) -> droppedState & accessor .~ (x1 <> x2) : view accessor droppedState + _ -> state + where + droppedState :: State + droppedState = state & accessor .~ drop 2 (view accessor state) + +-- evolve fodder??????????? +instructionNoOp :: State -> State +instructionNoOp state = state + +instructionConj :: State -> Lens' State [a] -> Lens' State [[a]] -> State +instructionConj state primAccessor vectorAccessor = + case (uncons (view primAccessor state), uncons (view vectorAccessor state)) of + (Just (p1,ps), Just (v1,vs)) -> state & primAccessor .~ ps & vectorAccessor .~ ((p1 : v1) : vs) + _ -> state + +-- v for vector, vs for vectorstack (also applicable to strings) +-- Could abstract this unconsing even further in all functions below +instructionTakeN :: State -> Lens' State [[a]] -> State +instructionTakeN state@(State {_int = i1 : is}) accessor = + case uncons (view accessor state) of + Just (v1, vs) -> state{_int = is} & accessor .~ (take (absNum i1 v1) v1 : vs) + _ -> state +instructionTakeN state _ = state + +instructionSubVector :: State -> Lens' State [[a]] -> State +instructionSubVector state@(State {_int = i1 : i2 : is}) accessor = + case uncons (view accessor state) of + Just (v1, vs) -> state{_int = is} & accessor .~ (subList i1 i2 v1 : vs) + _ -> state +instructionSubVector state _ = state + +instructionVectorFirst :: State -> Lens' State [a] -> Lens' State [[a]] -> State +instructionVectorFirst state primAccessor vectorAccessor = + case uncons (view vectorAccessor state) of + Just (v1, vs) -> + case uncons v1 of + Just (vp1, _) -> state & primAccessor .~ (vp1 : view primAccessor state) & vectorAccessor .~ vs + _ -> state + _ -> state + +instructionVectorLast :: State -> Lens' State [a] -> Lens' State [[a]] -> State +instructionVectorLast state primAccessor vectorAccessor = + case uncons (view vectorAccessor state) of + Just (v1, vs) -> + case uncons (drop (length v1 - 1) v1) of -- gonna keep this implementation over using last as this can't error + Just (vplast, _) -> state & primAccessor .~ (vplast : view primAccessor state) & vectorAccessor .~ vs + _ -> state + _ -> state + +instructionVectorNth :: State -> Lens' State [a] -> Lens' State [[a]] -> State +instructionVectorNth state@(State {_int = i1 : is}) primAccessor vectorAccessor = + case uncons (view vectorAccessor state) of + Just (v1, vs) -> state{_int = is} & primAccessor .~ (v1 !! absNum i1 v1 : view primAccessor state{_int = is}) & vectorAccessor .~ vs + _ -> state +instructionVectorNth state _ _ = state + +instructionRest :: State -> Lens' State [[a]] -> State +instructionRest state accessor = + case uncons (view accessor state) of + Just (v1, vs) -> state & accessor .~ (drop 1 v1 : vs) + _ -> state + +instructionButLast :: State -> Lens' State [[a]] -> State +instructionButLast state accessor = + case uncons (view accessor state) of + Just (v1, vs) -> state & accessor .~ (safeInit v1 : vs) + _ -> state + +instructionLength :: State -> Lens' State [[a]] -> State +instructionLength state@(State {_int = is}) accessor = + case uncons (view accessor state) of + Just (v1, vs) -> state{_int = length v1 : is} & accessor .~ vs + _ -> state + +instructionReverse :: State -> Lens' State [[a]] -> State +instructionReverse state accessor = + case uncons (view accessor state) of + Just (v1, vs) -> state & accessor .~ (reverse v1 : vs) + _ -> state + +instructionPushAll :: State -> Lens' State [a] -> Lens' State [[a]] -> State +instructionPushAll state primAccessor vectorAccessor = + case uncons (view vectorAccessor state) of + Just (v1, vs) -> state & vectorAccessor .~ vs & primAccessor .~ (v1 <> view primAccessor state) + _ -> state + +instructionVectorMakeEmpty :: State -> Lens' State [[a]] -> State +instructionVectorMakeEmpty state accessor = state & accessor .~ ([] : view accessor state) + +instructionVectorIsEmpty :: State -> Lens' State [[a]] -> State +instructionVectorIsEmpty state@(State {_bool = bs}) accessor = + case uncons (view accessor state) of + Just (v1, vs) -> state{_bool = null v1 : bs} & accessor .~ vs + _ -> state + +instructionVectorContains :: Eq a => State -> Lens' State [a] -> Lens' State [[a]] -> State +instructionVectorContains state@(State {_bool = bs}) primAccessor vectorAccessor = + case (uncons (view vectorAccessor state), uncons (view primAccessor state)) of + (Just (v1, vs), Just (p1, ps)) -> state{_bool = (findSubA v1 [p1] /= -1) : bs} & vectorAccessor .~ vs & primAccessor .~ ps + _ -> state + +-- I couldn't think of a better way of doing this +instructionVectorIndexOf :: Eq a => State -> Lens' State [a] -> Lens' State [[a]] -> State +instructionVectorIndexOf state primAccessor vectorAccessor = + case (uncons (view vectorAccessor state), uncons (view primAccessor state)) of + (Just (v1, vs), Just (p1, ps)) -> (state & vectorAccessor .~ vs & primAccessor .~ ps) & int .~ (findSubA v1 [p1] : view int (state & vectorAccessor .~ vs & primAccessor .~ ps)) + _ -> state + +instructionVectorOccurrencesOf :: Eq a => State -> Lens' State [a] -> Lens' State [[a]] -> State +instructionVectorOccurrencesOf state primAccessor vectorAccessor = + case (uncons (view vectorAccessor state), uncons (view primAccessor state)) of + (Just (v1, vs), Just (p1, ps)) -> (state & vectorAccessor .~ vs & primAccessor .~ ps) & int .~ (amtOccurences v1 [p1] : view int (state & vectorAccessor .~ vs & primAccessor .~ ps)) + _ -> state + +instructionVectorSetNth :: State -> Lens' State [a] -> Lens' State [[a]] -> State +instructionVectorSetNth state@(State {_int = i1 : is}) primAccessor vectorAccessor = + case (uncons (view vectorAccessor state{_int = is}), uncons (view primAccessor state{_int = is})) of + (Just (v1, vs), Just (p1, ps)) -> state{_int = is} & vectorAccessor .~ (replaceAt (absNum i1 v1) p1 v1 : vs) & primAccessor .~ ps + _ -> state +instructionVectorSetNth state _ _ = state + +instructionVectorReplace :: Eq a => State -> Lens' State [a] -> Lens' State [[a]] -> State +instructionVectorReplace state primAccessor vectorAccessor = + case (uncons (view vectorAccessor state), uncons (view primAccessor state)) of + (Just (v1, vs), Just (p1, p2 : ps)) -> state & vectorAccessor .~ (replace v1 [p1] [p2] Nothing : vs) & primAccessor .~ ps + _ -> state + +instructionVectorReplaceFirst :: Eq a => State -> Lens' State [a] -> Lens' State [[a]] -> State +instructionVectorReplaceFirst state primAccessor vectorAccessor = + case (uncons (view vectorAccessor state), uncons (view primAccessor state)) of + (Just (v1, vs), Just (p1, p2 : ps)) -> state & vectorAccessor .~ (replace v1 [p1] [p2] (Just 1) : vs) & primAccessor .~ ps + _ -> state + +instructionVectorRemove :: Eq a => State -> Lens' State [a] -> Lens' State [[a]] -> State +instructionVectorRemove state primAccessor vectorAccessor = + case (uncons (view vectorAccessor state), uncons (view primAccessor state)) of + (Just (v1, vs), Just (p1, ps)) -> state & vectorAccessor .~ (replace v1 [p1] [] Nothing : vs) & primAccessor .~ ps + _ -> state + +instructionVectorIterate :: State -> Lens' State [a] -> Lens' State [[a]] -> ([a] -> Gene) -> (State -> State) -> String -> State +instructionVectorIterate state@(State {_exec = e1 : es}) primAccessor vectorAccessor vectorType typeIterateFunction typeIterateFunctionName = + case uncons (view vectorAccessor state) of + Just ([], vs) -> state{_exec = es} & vectorAccessor .~ vs + Just ([x], vs) -> state & primAccessor .~ (x : view primAccessor state) & vectorAccessor .~ vs + Just (v1, vs) -> + (case uncons v1 of + Just (nv1, nvs) -> state{_exec = e1 : vectorType nvs : StateFunc (typeIterateFunction, typeIterateFunctionName) : e1 : es} & primAccessor .~ (nv1 : view primAccessor state) & vectorAccessor .~ vs + _ -> state) -- This should never happen + _ -> state +instructionVectorIterate state _ _ _ _ _ = state + +instructionCodeFrom :: State -> Lens' State [a] -> (a -> Gene) -> State +instructionCodeFrom state@(State {_code = cs}) accessor geneType = + case uncons (view accessor state) of + Just (x, xs) -> state{_code = geneType x : cs} & accessor .~ xs + _ -> state diff --git a/src/HushGP/Instructions/IntInstructions.hs b/src/HushGP/Instructions/IntInstructions.hs new file mode 100644 index 0000000..55a3180 --- /dev/null +++ b/src/HushGP/Instructions/IntInstructions.hs @@ -0,0 +1,104 @@ +module HushGP.Instructions.IntInstructions where + +import HushGP.State +import HushGP.Instructions.GenericInstructions +-- import Debug.Trace + +instructionIntFromFloat :: State -> State +instructionIntFromFloat state@(State {_float = (f : fs), _int = is}) = state {_float = fs, _int = floor f : is} +instructionIntFromFloat state = state + +instructionIntFromBool :: State -> State +instructionIntFromBool state@(State {_bool = (b : bs), _int = is}) = state {_bool = bs, _int = (if b then 1 else 0) : is} +instructionIntFromBool state = state + +instructionIntAdd :: State -> State +instructionIntAdd state@(State {_int = (i1 : i2 : is)}) = state {_int = i2 + i1 : is} +instructionIntAdd state = state + +instructionIntSub :: State -> State +instructionIntSub state@(State {_int = (i1 : i2 : is)}) = state {_int = i2 - i1 : is} +instructionIntSub state = state + +instructionIntMul :: State -> State +instructionIntMul state@(State {_int = (i1 : i2 : is)}) = state {_int = i2 * i1 : is} +instructionIntMul state = state + +instructionIntDiv :: State -> State +instructionIntDiv state@(State {_int = (i1 : i2 : is)}) = state {_int = if i1 /= 0 then (i2 `div` i1) : is else i1 : i2 : is} +instructionIntDiv state = state + +instructionIntMod :: State -> State +instructionIntMod state@(State {_int = (i1 : i2 : is)}) = state {_int = if i1 /= 0 then (i2 `mod` i1) : is else i1 : i2 : is} +instructionIntMod state = state + +instructionIntMin :: State -> State +instructionIntMin state@(State {_int = (i1 : i2 : is)}) = state {_int = min i1 i2 : is} +instructionIntMin state = state + +instructionIntMax :: State -> State +instructionIntMax state@(State {_int = (i1 : i2 : is)}) = state {_int = max i1 i2 : is} +instructionIntMax state = state + +instructionIntInc :: State -> State +instructionIntInc state@(State {_int = (i1 : is)}) = state {_int = i1 + 1 : is} +instructionIntInc state = state + +instructionIntDec :: State -> State +instructionIntDec state@(State {_int = (i1 : is)}) = state {_int = i1 - 1 : is} +instructionIntDec state = state + +instructionIntLT :: State -> State +instructionIntLT state@(State {_int = i1 : i2 : is, _bool = bs}) = state {_int = is, _bool = (i1 < i2) : bs} +instructionIntLT state = state + +instructionIntGT :: State -> State +instructionIntGT state@(State {_int = i1 : i2 : is, _bool = bs}) = state {_int = is, _bool = (i1 > i2) : bs} +instructionIntGT state = state + +instructionIntLTE :: State -> State +instructionIntLTE state@(State {_int = i1 : i2 : is, _bool = bs}) = state {_int = is, _bool = (i1 <= i2) : bs} +instructionIntLTE state = state + +instructionIntGTE :: State -> State +instructionIntGTE state@(State {_int = i1 : i2 : is, _bool = bs}) = state {_int = is, _bool = (i1 >= i2) : bs} +instructionIntGTE state = state + +instructionIntDup :: State -> State +instructionIntDup state = instructionDup state int + +instructionIntPop :: State -> State +instructionIntPop state = instructionPop state int + +instructionIntDupN :: State -> State +instructionIntDupN state = instructionDupN state int + +instructionIntSwap :: State -> State +instructionIntSwap state = instructionSwap state int + +instructionIntRot :: State -> State +instructionIntRot state = instructionRot state int + +instructionIntFlush :: State -> State +instructionIntFlush state = instructionFlush state int + +instructionIntEq :: State -> State +instructionIntEq state = instructionEq state int + +instructionIntStackDepth :: State -> State +instructionIntStackDepth state = instructionStackDepth state int + +instructionIntYank :: State -> State +instructionIntYank state = instructionYank state int + +instructionIntYankDup :: State -> State +instructionIntYankDup state = instructionYankDup state int + +instructionIntShove :: State -> State +instructionIntShove state = instructionShove state int + +instructionIntShoveDup :: State -> State +instructionIntShoveDup state = instructionShoveDup state int + +instructionIntIsEmpty :: State -> State +instructionIntIsEmpty state = instructionIsEmpty state int diff --git a/src/HushGP/Instructions/StringInstructions.hs b/src/HushGP/Instructions/StringInstructions.hs new file mode 100644 index 0000000..d893027 --- /dev/null +++ b/src/HushGP/Instructions/StringInstructions.hs @@ -0,0 +1,231 @@ +module HushGP.Instructions.StringInstructions where + +import HushGP.State +import HushGP.Instructions.GenericInstructions +import Data.List.Split +import Control.Lens + +-- shamelessly stolen from https://hackage.haskell.org/package/MissingH-1.6.0.1/docs/src/Data.String.Utils.html#strip +wschars :: String +wschars = " \t\r\n" + +strip :: String -> String +strip = lstrip . rstrip + +lstrip :: String -> String +lstrip s = case s of + [] -> [] + (x:xs) -> if x `elem` wschars + then lstrip xs + else s + +-- this is a tad inefficient init +rstrip :: String -> String +rstrip = reverse . lstrip . reverse + +instructionStringConcat :: State -> State +instructionStringConcat state = instructionConcat state string + +instructionStringSwap :: State -> State +instructionStringSwap state = instructionSwap state string + +instructionStringInsertString :: State -> State +instructionStringInsertString state@(State{_string = s1 : s2 : ss, _int = i1 : is}) = state {_string = combineTupleList s2 (splitAt i1 s1) : ss, _int = is} +instructionStringInsertString state = state + +instructionStringFromFirstChar :: State -> State +instructionStringFromFirstChar state@(State {_string = (schar : _) : ss}) = state {_string = [schar] : ss} +instructionStringFromFirstChar state = state + +instructionStringFromLastChar :: State -> State +instructionStringFromLastChar state@(State {_string = s1 : ss}) = + if not $ null s1 + then state {_string = [last s1] : ss} + else state +instructionStringFromLastChar state = state + +instructionStringFromNthChar :: State -> State +instructionStringFromNthChar state@(State {_string = s1 : ss, _int = i1 : is}) = state{_string = [s1 !! absNum i1 s1] : ss, _int = is} +instructionStringFromNthChar state = state + +instructionStringIndexOfString :: State -> State +instructionStringIndexOfString state@(State {_string = s1 : s2 : ss, _int = is}) = state {_string = ss, _int = findSubA s1 s2 : is} +instructionStringIndexOfString state = state + +instructionStringContainsString :: State -> State +instructionStringContainsString state@(State {_string = s1 : s2 : ss, _bool = bs}) = state {_string = ss, _bool = (findSubA s1 s2 /= -1) : bs} +instructionStringContainsString state = state + +-- pysh reverses this. Check this for propeller +instructionStringSplitOnString :: State -> State +instructionStringSplitOnString state@(State {_string = s1 : s2 : ss}) = state {_string = reverse $ splitOn s2 s1 <> ss} +instructionStringSplitOnString state = state + +instructionStringReplaceFirstString :: State -> State +instructionStringReplaceFirstString state@(State {_string = s1 : s2 : s3 : ss}) = state {_string = replace s1 s2 s3 (Just 1) : ss} +instructionStringReplaceFirstString state = state + +instructionStringReplaceNString :: State -> State +instructionStringReplaceNString state@(State {_string = s1 : s2 : s3 : ss, _int = i1 : is}) = state{_string = replace s1 s2 s3 (Just i1) : ss, _int = is} +instructionStringReplaceNString state = state + +instructionStringReplaceAllString :: State -> State +instructionStringReplaceAllString state@(State {_string = s1 : s2 : s3 : ss}) = state{_string = replace s1 s2 s3 Nothing : ss} +instructionStringReplaceAllString state = state + +instructionStringRemoveFirstString :: State -> State +instructionStringRemoveFirstString state@(State {_string = s1 : s2 : ss}) = state{_string = replace s1 s2 "" (Just 1) : ss} +instructionStringRemoveFirstString state = state + +instructionStringRemoveNString :: State -> State +instructionStringRemoveNString state@(State {_string = s1 : s2 : ss, _int = i1 : is}) = state{_string = replace s1 s2 "" (Just i1) : ss, _int = is} +instructionStringRemoveNString state = state + +instructionStringRemoveAllString :: State -> State +instructionStringRemoveAllString state@(State {_string = s1 : s2 : ss}) = state{_string = replace s1 s2 "" Nothing : ss} +instructionStringRemoveAllString state = state + +instructionStringOccurrencesOfString :: State -> State +instructionStringOccurrencesOfString state@(State {_string = s1 : s2 : ss, _int = is}) = state{_string = ss, _int = amtOccurences s1 s2 : is} +instructionStringOccurrencesOfString state = state + +instructionStringInsertChar :: State -> State +instructionStringInsertChar state@(State {_string = s1 : ss, _char = c1 : cs, _int = i1 : is}) = state{_string = combineTuple c1 (splitAt i1 s1) : ss, _char = cs, _int = is} +instructionStringInsertChar state = state + +instructionStringContainsChar :: State -> State +instructionStringContainsChar state = instructionVectorContains state char string + +instructionStringIndexOfChar :: State -> State +instructionStringIndexOfChar state = instructionVectorIndexOf state char string + +instructionStringSplitOnChar :: State -> State +instructionStringSplitOnChar state@(State {_string = s1 : ss, _char = c1 : cs}) = state {_string = reverse $ splitOn [c1] s1 <> ss, _char = cs} +instructionStringSplitOnChar state = state + +instructionStringReplaceFirstChar :: State -> State +instructionStringReplaceFirstChar state = instructionVectorReplaceFirst state char string + +instructionStringReplaceNChar :: State -> State +instructionStringReplaceNChar state@(State {_string = s1 : ss, _char = c1 : c2 : cs, _int = i1 : is}) = state{_string = replace s1 [c1] [c2] (Just i1) : ss, _char = cs, _int = is} +instructionStringReplaceNChar state = state + +instructionStringReplaceAllChar :: State -> State +instructionStringReplaceAllChar state = instructionVectorReplace state char string + +instructionStringRemoveFirstChar :: State -> State +instructionStringRemoveFirstChar state@(State {_string = s1 : ss, _char = c1 : cs}) = state {_string = replace s1 [c1] "" (Just 1) : ss, _char = cs} +instructionStringRemoveFirstChar state = state + +instructionStringRemoveNChar :: State -> State +instructionStringRemoveNChar state@(State {_string = s1 : ss, _char = c1 : cs, _int = i1 : is}) = state{_string = replace s1 [c1] "" (Just i1) : ss, _char = cs, _int = is} +instructionStringRemoveNChar state = state + +instructionStringRemoveAllChar :: State -> State +instructionStringRemoveAllChar state = instructionVectorRemove state char string + +instructionStringOccurrencesOfChar :: State -> State +instructionStringOccurrencesOfChar state = instructionVectorOccurrencesOf state char string + +instructionStringReverse :: State -> State +instructionStringReverse state = instructionReverse state string + +instructionStringHead :: State -> State +instructionStringHead state = instructionTakeN state string + +instructionStringTail :: State -> State +instructionStringTail state@(State {_string = s1 : ss, _int = i1 : is}) = state{_string = takeR (absNum i1 s1) s1 : ss, _int = is} +instructionStringTail state = state + +instructionStringAppendChar :: State -> State +instructionStringAppendChar state = instructionConj state char string + +instructionStringRest :: State -> State +instructionStringRest state = instructionRest state string + +instructionStringButLast :: State -> State +instructionStringButLast state = instructionButLast state string + +instructionStringDrop :: State -> State +instructionStringDrop state@(State {_string = s1 : ss, _int = i1 : is}) = state{_string = drop (absNum i1 s1) s1 : ss, _int = is} +instructionStringDrop state = state + +instructionStringButLastN :: State -> State +instructionStringButLastN state@(State {_string = s1 : ss, _int = i1 : is}) = state{_string = dropR (absNum i1 s1) s1 : ss, _int = is} +instructionStringButLastN state = state + +instructionStringLength :: State -> State +instructionStringLength state = instructionLength state string + +instructionStringMakeEmpty :: State -> State +instructionStringMakeEmpty state = instructionVectorMakeEmpty state string + +instructionStringIsEmptyString :: State -> State +instructionStringIsEmptyString state@(State {_string = s1 : ss, _bool = bs}) = state{_string = ss, _bool = null s1 : bs} +instructionStringIsEmptyString state = state + +instructionStringRemoveNth :: State -> State +instructionStringRemoveNth state@(State {_string = s1 : ss, _int = i1 : is}) = state{_string = deleteAt (absNum i1 s1) s1 : ss, _int = is} +instructionStringRemoveNth state = state + +instructionStringSetNth :: State -> State +instructionStringSetNth state = instructionVectorSetNth state char string + +instructionStringStripWhitespace :: State -> State +instructionStringStripWhitespace state@(State {_string = s1 : ss}) = state{_string = strip s1 : ss} +instructionStringStripWhitespace state = state + +instructionStringFromLens :: Show a => State -> Lens' State [a] -> State +instructionStringFromLens state@(State {_string = ss}) accessor = + case uncons (view accessor state) of + Nothing -> state + Just (x,_) -> state{_string = show x : ss} + +instructionStringFromBool :: State -> State +instructionStringFromBool state = instructionStringFromLens state bool + +instructionStringFromInt :: State -> State +instructionStringFromInt state = instructionStringFromLens state int + +instructionStringFromFloat :: State -> State +instructionStringFromFloat state = instructionStringFromLens state float + +instructionStringFromChar :: State -> State +instructionStringFromChar state@(State {_string = ss, _char = c1 : cs}) = state{_string = [c1] : ss, _char = cs} +instructionStringFromChar state = state + +instructionStringPop :: State -> State +instructionStringPop state = instructionPop state string + +instructionStringDup :: State -> State +instructionStringDup state = instructionDup state string + +instructionStringDupN :: State -> State +instructionStringDupN state = instructionDupN state string + +instructionStringRot :: State -> State +instructionStringRot state = instructionRot state string + +instructionStringFlush :: State -> State +instructionStringFlush state = instructionFlush state string + +instructionStringEq :: State -> State +instructionStringEq state = instructionEq state string + +instructionStringStackDepth :: State -> State +instructionStringStackDepth state = instructionStackDepth state string + +instructionStringYank :: State -> State +instructionStringYank state = instructionYank state string + +instructionStringYankDup :: State -> State +instructionStringYankDup state = instructionYankDup state string + +instructionStringIsEmpty :: State -> State +instructionStringIsEmpty state = instructionIsEmpty state string + +instructionStringShove :: State -> State +instructionStringShove state = instructionShove state string + +instructionStringShoveDup :: State -> State +instructionStringShoveDup state = instructionShoveDup state string diff --git a/src/HushGP/Instructions/VectorBoolInstructions.hs b/src/HushGP/Instructions/VectorBoolInstructions.hs new file mode 100644 index 0000000..6f226c1 --- /dev/null +++ b/src/HushGP/Instructions/VectorBoolInstructions.hs @@ -0,0 +1,106 @@ +module HushGP.Instructions.VectorBoolInstructions where + +import HushGP.State +import HushGP.Instructions.GenericInstructions + +instructionVectorBoolConcat :: State -> State +instructionVectorBoolConcat state = instructionConcat state vectorBool + +instructionVectorBoolConj :: State -> State +instructionVectorBoolConj state = instructionConj state bool vectorBool + +instructionVectorBoolTakeN :: State -> State +instructionVectorBoolTakeN state = instructionTakeN state vectorBool + +instructionVectorBoolSubVector :: State -> State +instructionVectorBoolSubVector state = instructionSubVector state vectorBool + +instructionVectorBoolFirst :: State -> State +instructionVectorBoolFirst state = instructionVectorFirst state bool vectorBool + +instructionVectorBoolLast :: State -> State +instructionVectorBoolLast state = instructionVectorLast state bool vectorBool + +instructionVectorBoolNth :: State -> State +instructionVectorBoolNth state = instructionVectorNth state bool vectorBool + +instructionVectorBoolRest :: State -> State +instructionVectorBoolRest state = instructionRest state vectorBool + +instructionVectorBoolButLast :: State -> State +instructionVectorBoolButLast state = instructionButLast state vectorBool + +instructionVectorBoolLength :: State -> State +instructionVectorBoolLength state = instructionLength state vectorBool + +instructionVectorBoolReverse :: State -> State +instructionVectorBoolReverse state = instructionReverse state vectorBool + +instructionVectorBoolPushAll :: State -> State +instructionVectorBoolPushAll state = instructionPushAll state bool vectorBool + +instructionVectorBoolMakeEmpty :: State -> State +instructionVectorBoolMakeEmpty state = instructionVectorMakeEmpty state vectorBool + +instructionVectorBoolIsEmpty :: State -> State +instructionVectorBoolIsEmpty state = instructionVectorIsEmpty state vectorBool + +instructionVectorBoolIndexOf :: State -> State +instructionVectorBoolIndexOf state = instructionVectorIndexOf state bool vectorBool + +instructionVectorBoolOccurrencesOf :: State -> State +instructionVectorBoolOccurrencesOf state = instructionVectorOccurrencesOf state bool vectorBool + +instructionVectorBoolSetNth :: State -> State +instructionVectorBoolSetNth state = instructionVectorSetNth state bool vectorBool + +instructionVectorBoolReplace :: State -> State +instructionVectorBoolReplace state = instructionVectorReplace state bool vectorBool + +instructionVectorBoolReplaceFirst :: State -> State +instructionVectorBoolReplaceFirst state = instructionVectorReplaceFirst state bool vectorBool + +instructionVectorBoolRemove :: State -> State +instructionVectorBoolRemove state = instructionVectorRemove state bool vectorBool + +instructionVectorBoolIterate :: State -> State +instructionVectorBoolIterate state = instructionVectorIterate state bool vectorBool GeneVectorBool instructionVectorBoolIterate "instructionVectorBoolIterate" + +instructionVectorBoolPop :: State -> State +instructionVectorBoolPop state = instructionPop state vectorBool + +instructionVectorBoolDup :: State -> State +instructionVectorBoolDup state = instructionDup state vectorBool + +instructionVectorBoolDupN :: State -> State +instructionVectorBoolDupN state = instructionDupN state vectorBool + +instructionVectorBoolSwap :: State -> State +instructionVectorBoolSwap state = instructionSwap state vectorBool + +instructionVectorBoolRot :: State -> State +instructionVectorBoolRot state = instructionRot state vectorBool + +instructionVectorBoolFlush :: State -> State +instructionVectorBoolFlush state = instructionFlush state vectorBool + +instructionVectorBoolEq :: State -> State +instructionVectorBoolEq state = instructionEq state vectorBool + +instructionVectorBoolStackDepth :: State -> State +instructionVectorBoolStackDepth state = instructionStackDepth state vectorBool + +instructionVectorBoolYank :: State -> State +instructionVectorBoolYank state = instructionYank state vectorBool + +instructionVectorBoolYankDup :: State -> State +instructionVectorBoolYankDup state = instructionYankDup state vectorBool + +instructionVectorBoolStackIsEmpty :: State -> State +instructionVectorBoolStackIsEmpty state = instructionIsEmpty state vectorBool + +instructionVectorBoolShove :: State -> State +instructionVectorBoolShove state = instructionShove state vectorBool + +instructionVectorBoolShoveDup :: State -> State +instructionVectorBoolShoveDup state = instructionShoveDup state vectorBool diff --git a/src/HushGP/Instructions/VectorCharInstructions.hs b/src/HushGP/Instructions/VectorCharInstructions.hs new file mode 100644 index 0000000..12b083e --- /dev/null +++ b/src/HushGP/Instructions/VectorCharInstructions.hs @@ -0,0 +1,106 @@ +module HushGP.Instructions.VectorCharInstructions where + +import HushGP.State +import HushGP.Instructions.GenericInstructions + +instructionVectorCharConcat :: State -> State +instructionVectorCharConcat state = instructionConcat state vectorChar + +instructionVectorCharConj :: State -> State +instructionVectorCharConj state = instructionConj state char vectorChar + +instructionVectorCharTakeN :: State -> State +instructionVectorCharTakeN state = instructionTakeN state vectorChar + +instructionVectorCharSubVector :: State -> State +instructionVectorCharSubVector state = instructionSubVector state vectorChar + +instructionVectorCharFirst :: State -> State +instructionVectorCharFirst state = instructionVectorFirst state char vectorChar + +instructionVectorCharLast :: State -> State +instructionVectorCharLast state = instructionVectorLast state char vectorChar + +instructionVectorCharNth :: State -> State +instructionVectorCharNth state = instructionVectorNth state char vectorChar + +instructionVectorCharRest :: State -> State +instructionVectorCharRest state = instructionRest state vectorChar + +instructionVectorCharButLast :: State -> State +instructionVectorCharButLast state = instructionButLast state vectorChar + +instructionVectorCharLength :: State -> State +instructionVectorCharLength state = instructionLength state vectorChar + +instructionVectorCharReverse :: State -> State +instructionVectorCharReverse state = instructionReverse state vectorChar + +instructionVectorCharPushAll :: State -> State +instructionVectorCharPushAll state = instructionPushAll state char vectorChar + +instructionVectorCharMakeEmpty :: State -> State +instructionVectorCharMakeEmpty state = instructionVectorMakeEmpty state vectorChar + +instructionVectorCharIsEmpty :: State -> State +instructionVectorCharIsEmpty state = instructionVectorIsEmpty state vectorChar + +instructionVectorCharIndexOf :: State -> State +instructionVectorCharIndexOf state = instructionVectorIndexOf state char vectorChar + +instructionVectorCharOccurrencesOf :: State -> State +instructionVectorCharOccurrencesOf state = instructionVectorOccurrencesOf state char vectorChar + +instructionVectorCharSetNth :: State -> State +instructionVectorCharSetNth state = instructionVectorSetNth state char vectorChar + +instructionVectorCharReplace :: State -> State +instructionVectorCharReplace state = instructionVectorReplace state char vectorChar + +instructionVectorCharReplaceFirst :: State -> State +instructionVectorCharReplaceFirst state = instructionVectorReplaceFirst state char vectorChar + +instructionVectorCharRemove :: State -> State +instructionVectorCharRemove state = instructionVectorRemove state char vectorChar + +instructionVectorCharIterate :: State -> State +instructionVectorCharIterate state = instructionVectorIterate state char vectorChar GeneVectorChar instructionVectorCharIterate "instructionVectorCharIterate" + +instructionVectorCharPop :: State -> State +instructionVectorCharPop state = instructionPop state vectorChar + +instructionVectorCharDup :: State -> State +instructionVectorCharDup state = instructionDup state vectorChar + +instructionVectorCharDupN :: State -> State +instructionVectorCharDupN state = instructionDupN state vectorChar + +instructionVectorCharSwap :: State -> State +instructionVectorCharSwap state = instructionSwap state vectorChar + +instructionVectorCharRot :: State -> State +instructionVectorCharRot state = instructionRot state vectorChar + +instructionVectorCharFlush :: State -> State +instructionVectorCharFlush state = instructionFlush state vectorChar + +instructionVectorCharEq :: State -> State +instructionVectorCharEq state = instructionEq state vectorChar + +instructionVectorCharStackDepth :: State -> State +instructionVectorCharStackDepth state = instructionStackDepth state vectorChar + +instructionVectorCharYank :: State -> State +instructionVectorCharYank state = instructionYank state vectorChar + +instructionVectorCharYankDup :: State -> State +instructionVectorCharYankDup state = instructionYankDup state vectorChar + +instructionVectorCharStackIsEmpty :: State -> State +instructionVectorCharStackIsEmpty state = instructionIsEmpty state vectorChar + +instructionVectorCharShove :: State -> State +instructionVectorCharShove state = instructionShove state vectorChar + +instructionVectorCharShoveDup :: State -> State +instructionVectorCharShoveDup state = instructionShoveDup state vectorChar diff --git a/src/HushGP/Instructions/VectorFloatInstructions.hs b/src/HushGP/Instructions/VectorFloatInstructions.hs new file mode 100644 index 0000000..3f21566 --- /dev/null +++ b/src/HushGP/Instructions/VectorFloatInstructions.hs @@ -0,0 +1,106 @@ +module HushGP.Instructions.VectorFloatInstructions where + +import HushGP.State +import HushGP.Instructions.GenericInstructions + +instructionVectorFloatConcat :: State -> State +instructionVectorFloatConcat state = instructionConcat state vectorFloat + +instructionVectorFloatConj :: State -> State +instructionVectorFloatConj state = instructionConj state float vectorFloat + +instructionVectorFloatTakeN :: State -> State +instructionVectorFloatTakeN state = instructionTakeN state vectorFloat + +instructionVectorFloatSubVector :: State -> State +instructionVectorFloatSubVector state = instructionSubVector state vectorFloat + +instructionVectorFloatFirst :: State -> State +instructionVectorFloatFirst state = instructionVectorFirst state float vectorFloat + +instructionVectorFloatLast :: State -> State +instructionVectorFloatLast state = instructionVectorLast state float vectorFloat + +instructionVectorFloatNth :: State -> State +instructionVectorFloatNth state = instructionVectorNth state float vectorFloat + +instructionVectorFloatRest :: State -> State +instructionVectorFloatRest state = instructionRest state vectorFloat + +instructionVectorFloatButLast :: State -> State +instructionVectorFloatButLast state = instructionButLast state vectorFloat + +instructionVectorFloatLength :: State -> State +instructionVectorFloatLength state = instructionLength state vectorFloat + +instructionVectorFloatReverse :: State -> State +instructionVectorFloatReverse state = instructionReverse state vectorFloat + +instructionVectorFloatPushAll :: State -> State +instructionVectorFloatPushAll state = instructionPushAll state float vectorFloat + +instructionVectorFloatMakeEmpty :: State -> State +instructionVectorFloatMakeEmpty state = instructionVectorMakeEmpty state vectorFloat + +instructionVectorFloatIsEmpty :: State -> State +instructionVectorFloatIsEmpty state = instructionVectorIsEmpty state vectorFloat + +instructionVectorFloatIndexOf :: State -> State +instructionVectorFloatIndexOf state = instructionVectorIndexOf state float vectorFloat + +instructionVectorFloatOccurrencesOf :: State -> State +instructionVectorFloatOccurrencesOf state = instructionVectorOccurrencesOf state float vectorFloat + +instructionVectorFloatSetNth :: State -> State +instructionVectorFloatSetNth state = instructionVectorSetNth state float vectorFloat + +instructionVectorFloatReplace :: State -> State +instructionVectorFloatReplace state = instructionVectorReplace state float vectorFloat + +instructionVectorFloatReplaceFirst :: State -> State +instructionVectorFloatReplaceFirst state = instructionVectorReplaceFirst state float vectorFloat + +instructionVectorFloatRemove :: State -> State +instructionVectorFloatRemove state = instructionVectorRemove state float vectorFloat + +instructionVectorFloatIterate :: State -> State +instructionVectorFloatIterate state = instructionVectorIterate state float vectorFloat GeneVectorFloat instructionVectorFloatIterate "instructionVectorFloatIterate" + +instructionVectorFloatPop :: State -> State +instructionVectorFloatPop state = instructionPop state vectorFloat + +instructionVectorFloatDup :: State -> State +instructionVectorFloatDup state = instructionDup state vectorFloat + +instructionVectorFloatDupN :: State -> State +instructionVectorFloatDupN state = instructionDupN state vectorFloat + +instructionVectorFloatSwap :: State -> State +instructionVectorFloatSwap state = instructionSwap state vectorFloat + +instructionVectorFloatRot :: State -> State +instructionVectorFloatRot state = instructionRot state vectorFloat + +instructionVectorFloatFlush :: State -> State +instructionVectorFloatFlush state = instructionFlush state vectorFloat + +instructionVectorFloatEq :: State -> State +instructionVectorFloatEq state = instructionEq state vectorFloat + +instructionVectorFloatStackDepth :: State -> State +instructionVectorFloatStackDepth state = instructionStackDepth state vectorFloat + +instructionVectorFloatYank :: State -> State +instructionVectorFloatYank state = instructionYank state vectorFloat + +instructionVectorFloatYankDup :: State -> State +instructionVectorFloatYankDup state = instructionYankDup state vectorFloat + +instructionVectorFloatStackIsEmpty :: State -> State +instructionVectorFloatStackIsEmpty state = instructionIsEmpty state vectorFloat + +instructionVectorFloatShove :: State -> State +instructionVectorFloatShove state = instructionShove state vectorFloat + +instructionVectorFloatShoveDup :: State -> State +instructionVectorFloatShoveDup state = instructionShoveDup state vectorFloat diff --git a/src/HushGP/Instructions/VectorIntInstructions.hs b/src/HushGP/Instructions/VectorIntInstructions.hs new file mode 100644 index 0000000..1bac705 --- /dev/null +++ b/src/HushGP/Instructions/VectorIntInstructions.hs @@ -0,0 +1,106 @@ +module HushGP.Instructions.VectorIntInstructions where + +import HushGP.Instructions.GenericInstructions +import HushGP.State + +instructionVectorIntConcat :: State -> State +instructionVectorIntConcat state = instructionConcat state vectorInt + +instructionVectorIntConj :: State -> State +instructionVectorIntConj state = instructionConj state int vectorInt + +instructionVectorIntTakeN :: State -> State +instructionVectorIntTakeN state = instructionTakeN state vectorInt + +instructionVectorIntSubVector :: State -> State +instructionVectorIntSubVector state = instructionSubVector state vectorInt + +instructionVectorIntFirst :: State -> State +instructionVectorIntFirst state = instructionVectorFirst state int vectorInt + +instructionVectorIntLast :: State -> State +instructionVectorIntLast state = instructionVectorLast state int vectorInt + +instructionVectorIntNth :: State -> State +instructionVectorIntNth state = instructionVectorNth state int vectorInt + +instructionVectorIntRest :: State -> State +instructionVectorIntRest state = instructionRest state vectorInt + +instructionVectorIntButLast :: State -> State +instructionVectorIntButLast state = instructionButLast state vectorInt + +instructionVectorIntLength :: State -> State +instructionVectorIntLength state = instructionLength state vectorInt + +instructionVectorIntReverse :: State -> State +instructionVectorIntReverse state = instructionReverse state vectorInt + +instructionVectorIntPushAll :: State -> State +instructionVectorIntPushAll state = instructionPushAll state int vectorInt + +instructionVectorIntMakeEmpty :: State -> State +instructionVectorIntMakeEmpty state = instructionVectorMakeEmpty state vectorInt + +instructionVectorIntIsEmpty :: State -> State +instructionVectorIntIsEmpty state = instructionVectorIsEmpty state vectorInt + +instructionVectorIntIndexOf :: State -> State +instructionVectorIntIndexOf state = instructionVectorIndexOf state int vectorInt + +instructionVectorIntOccurrencesOf :: State -> State +instructionVectorIntOccurrencesOf state = instructionVectorOccurrencesOf state int vectorInt + +instructionVectorIntSetNth :: State -> State +instructionVectorIntSetNth state = instructionVectorSetNth state int vectorInt + +instructionVectorIntReplace :: State -> State +instructionVectorIntReplace state = instructionVectorReplace state int vectorInt + +instructionVectorIntReplaceFirst :: State -> State +instructionVectorIntReplaceFirst state = instructionVectorReplaceFirst state int vectorInt + +instructionVectorIntRemove :: State -> State +instructionVectorIntRemove state = instructionVectorRemove state int vectorInt + +instructionVectorIntIterate :: State -> State +instructionVectorIntIterate state = instructionVectorIterate state int vectorInt GeneVectorInt instructionVectorIntIterate "instructionVectorIntIterate" + +instructionVectorIntPop :: State -> State +instructionVectorIntPop state = instructionPop state vectorChar + +instructionVectorIntDup :: State -> State +instructionVectorIntDup state = instructionDup state vectorChar + +instructionVectorIntDupN :: State -> State +instructionVectorIntDupN state = instructionDupN state vectorChar + +instructionVectorIntSwap :: State -> State +instructionVectorIntSwap state = instructionSwap state vectorChar + +instructionVectorIntRot :: State -> State +instructionVectorIntRot state = instructionRot state vectorChar + +instructionVectorIntFlush :: State -> State +instructionVectorIntFlush state = instructionFlush state vectorChar + +instructionVectorIntEq :: State -> State +instructionVectorIntEq state = instructionEq state vectorChar + +instructionVectorIntStackDepth :: State -> State +instructionVectorIntStackDepth state = instructionStackDepth state vectorChar + +instructionVectorIntYank :: State -> State +instructionVectorIntYank state = instructionYank state vectorChar + +instructionVectorIntYankDup :: State -> State +instructionVectorIntYankDup state = instructionYankDup state vectorChar + +instructionVectorIntStackIsEmpty :: State -> State +instructionVectorIntStackIsEmpty state = instructionIsEmpty state vectorChar + +instructionVectorIntShove :: State -> State +instructionVectorIntShove state = instructionShove state vectorChar + +instructionVectorIntShoveDup :: State -> State +instructionVectorIntShoveDup state = instructionShoveDup state vectorChar diff --git a/src/HushGP/Instructions/VectorStringInstructions.hs b/src/HushGP/Instructions/VectorStringInstructions.hs new file mode 100644 index 0000000..39d0b69 --- /dev/null +++ b/src/HushGP/Instructions/VectorStringInstructions.hs @@ -0,0 +1,106 @@ +module HushGP.Instructions.VectorStringInstructions where + +import HushGP.State +import HushGP.Instructions.GenericInstructions + +instructionVectorStringConcat :: State -> State +instructionVectorStringConcat state = instructionConcat state vectorString + +instructionVectorStringConj :: State -> State +instructionVectorStringConj state = instructionConj state string vectorString + +instructionVectorStringTakeN :: State -> State +instructionVectorStringTakeN state = instructionTakeN state vectorString + +instructionVectorStringSubVector :: State -> State +instructionVectorStringSubVector state = instructionSubVector state vectorString + +instructionVectorStringFirst :: State -> State +instructionVectorStringFirst state = instructionVectorFirst state string vectorString + +instructionVectorStringLast :: State -> State +instructionVectorStringLast state = instructionVectorLast state string vectorString + +instructionVectorStringNth :: State -> State +instructionVectorStringNth state = instructionVectorNth state string vectorString + +instructionVectorStringRest :: State -> State +instructionVectorStringRest state = instructionRest state vectorString + +instructionVectorStringButLast :: State -> State +instructionVectorStringButLast state = instructionButLast state vectorString + +instructionVectorStringLength :: State -> State +instructionVectorStringLength state = instructionLength state vectorString + +instructionVectorStringReverse :: State -> State +instructionVectorStringReverse state = instructionReverse state vectorString + +instructionVectorStringPushAll :: State -> State +instructionVectorStringPushAll state = instructionPushAll state string vectorString + +instructionVectorStringMakeEmpty :: State -> State +instructionVectorStringMakeEmpty state = instructionVectorMakeEmpty state vectorString + +instructionVectorStringIsEmpty :: State -> State +instructionVectorStringIsEmpty state = instructionVectorIsEmpty state vectorString + +instructionVectorStringIndexOf :: State -> State +instructionVectorStringIndexOf state = instructionVectorIndexOf state string vectorString + +instructionVectorStringOccurrencesOf :: State -> State +instructionVectorStringOccurrencesOf state = instructionVectorOccurrencesOf state string vectorString + +instructionVectorStringSetNth :: State -> State +instructionVectorStringSetNth state = instructionVectorSetNth state string vectorString + +instructionVectorStringReplace :: State -> State +instructionVectorStringReplace state = instructionVectorReplace state string vectorString + +instructionVectorStringReplaceFirst :: State -> State +instructionVectorStringReplaceFirst state = instructionVectorReplaceFirst state string vectorString + +instructionVectorStringRemove :: State -> State +instructionVectorStringRemove state = instructionVectorRemove state string vectorString + +instructionVectorStringIterate :: State -> State +instructionVectorStringIterate state = instructionVectorIterate state string vectorString GeneVectorString instructionVectorStringIterate "instructionVectorStringIterate" + +instructionVectorStringPop :: State -> State +instructionVectorStringPop state = instructionPop state vectorString + +instructionVectorStringDup :: State -> State +instructionVectorStringDup state = instructionDup state vectorString + +instructionVectorStringDupN :: State -> State +instructionVectorStringDupN state = instructionDupN state vectorString + +instructionVectorStringSwap :: State -> State +instructionVectorStringSwap state = instructionSwap state vectorString + +instructionVectorStringRot :: State -> State +instructionVectorStringRot state = instructionRot state vectorString + +instructionVectorStringFlush :: State -> State +instructionVectorStringFlush state = instructionFlush state vectorString + +instructionVectorStringEq :: State -> State +instructionVectorStringEq state = instructionEq state vectorString + +instructionVectorStringStackDepth :: State -> State +instructionVectorStringStackDepth state = instructionStackDepth state vectorString + +instructionVectorStringYank :: State -> State +instructionVectorStringYank state = instructionYank state vectorString + +instructionVectorStringYankDup :: State -> State +instructionVectorStringYankDup state = instructionYankDup state vectorString + +instructionVectorStringStackIsEmpty :: State -> State +instructionVectorStringStackIsEmpty state = instructionIsEmpty state vectorString + +instructionVectorStringShove :: State -> State +instructionVectorStringShove state = instructionShove state vectorString + +instructionVectorStringShoveDup :: State -> State +instructionVectorStringShoveDup state = instructionShoveDup state vectorString diff --git a/src/HushGP/Push.hs b/src/HushGP/Push.hs new file mode 100644 index 0000000..1c9b75f --- /dev/null +++ b/src/HushGP/Push.hs @@ -0,0 +1,86 @@ +module HushGP.Push where + +import Control.Lens +import Data.Map qualified as Map +import HushGP.State + +-- import Debug.Trace (trace, traceStack) + +-- Each core func should be: (State -> State -> State) +-- but each core function can use abstract helper functions. +-- That is more efficient than checking length. +-- Everntually, this can be part of the apply func to state helpers, +-- which should take the number and type of parameter they have. + +-- This is one of the push genome functions itself, not infrastructure. +-- Optionally, split this off into independent functions +instructionParameterLoad :: State -> State +instructionParameterLoad state@(State {_parameter = (p : _)}) = case p of + (GeneInt val) -> state & int .~ val : view int state + (GeneFloat val) -> state & float .~ val : view float state + (GeneBool val) -> state & bool .~ val : view bool state + (GeneString val) -> state & string .~ val : view string state + (GeneChar val) -> state & char .~ val : view char state + (GeneVectorInt val) -> state & vectorInt .~ val : view vectorInt state + (GeneVectorFloat val) -> state & vectorFloat .~ val : view vectorFloat state + (GeneVectorBool val) -> state & vectorBool .~ val : view vectorBool state + (GeneVectorString val) -> state & vectorString .~ val : view vectorString state + (GeneVectorChar val) -> state & vectorChar .~ val : view vectorChar state + (StateFunc _) -> undefined + (PlaceInput _) -> undefined + Close -> undefined + (Block xs) -> state & exec .~ xs <> view exec state +instructionParameterLoad state = state + +-- Loads a genome into the exec stack +loadProgram :: [Gene] -> State -> State +loadProgram newstack state = state & exec .~ newstack + +-- Takes a Push state, and generates the next push state via: +-- If the first item on the EXEC stack is a single instruction +-- then pop it and execute it. +-- Else if the first item on the EXEC stack is a literal +-- then pop it and push it onto the appropriate stack. +-- Else (the first item must be a list) pop it and push all of the +-- items that it contains back onto the EXEC stack individually, +-- in reverse order (so that the item that was first in the list +-- ends up on top). +-- The empty-stack safety of interpretExec on empty stacks depends on the functions it calls. +interpretExec :: State -> State +interpretExec state@(State {_exec = e : es}) = + case e of + (GeneInt val) -> interpretExec (state & exec .~ es & int .~ val : view int state) + (GeneFloat val) -> interpretExec (state & exec .~ es & float .~ val : view float state) + (GeneBool val) -> interpretExec (state & exec .~ es & bool .~ val : view bool state) + (GeneString val) -> interpretExec (state & exec .~ es & string .~ val : view string state) + (GeneChar val) -> interpretExec (state & exec .~ es & char .~ val : view char state) + (GeneVectorInt val) -> interpretExec (state & exec .~ es & vectorInt .~ val : view vectorInt state) + (GeneVectorFloat val) -> interpretExec (state & exec .~ es & vectorFloat .~ val : view vectorFloat state) + (GeneVectorBool val) -> interpretExec (state & exec .~ es & vectorBool .~ val : view vectorBool state) + (GeneVectorString val) -> interpretExec (state & exec .~ es & vectorString .~ val : view vectorString state) + (GeneVectorChar val) -> interpretExec (state & exec .~ es & vectorChar .~ val : view vectorChar state) + (StateFunc (func, _)) -> interpretExec $ func state {_exec = es} + (Block block) -> interpretExec (state {_exec = block ++ es}) + (PlaceInput val) -> interpretExec (state {_exec = (view input state Map.! val) : es}) + Close -> undefined -- This should be removed later. Will be converted to Blocks in the Plushy -> Exec stack process +interpretExec state = state + +-- interpretOneStep :: State -> State +-- interpretOneStep state@(State {_exec = e : es}) = +-- case e of +-- (GeneInt val) -> state & exec .~ es & int .~ val : view int state +-- (GeneFloat val) -> state & exec .~ es & float .~ val : view float state +-- (GeneBool val) -> state & exec .~ es & bool .~ val : view bool state +-- (GeneString val) -> state & exec .~ es & string .~ val : view string state +-- (GeneChar val) -> state & exec .~ es & char .~ val : view char state +-- (GeneVectorInt val) -> state & exec .~ es & vectorInt .~ val : view vectorInt state +-- (GeneVectorFloat val) -> state & exec .~ es & vectorFloat .~ val : view vectorFloat state +-- (GeneVectorBool val) -> state & exec .~ es & vectorBool .~ val : view vectorBool state +-- (GeneVectorString val) -> state & exec .~ es & vectorString .~ val : view vectorString state +-- (GeneVectorChar val) -> state & exec .~ es & vectorChar .~ val : view vectorChar state +-- (StateFunc (func, _)) -> func state {_exec = es} +-- (Block block) -> (state {_exec = block ++ es}) +-- (PlaceInput val) -> (state {_exec = (view input state Map.! val) : es}) +-- Close -> undefined +-- interpretOneStep state = state +-- Need to make interpretExec strict, right? diff --git a/src/HushGP/PushTests.hs b/src/HushGP/PushTests.hs new file mode 100644 index 0000000..24f356e --- /dev/null +++ b/src/HushGP/PushTests.hs @@ -0,0 +1,10 @@ +module HushGP.PushTests + ( module HushGP.PushTests.GenericTests, + module HushGP.PushTests.IntTests, + module HushGP.PushTests.UtilTests, + ) +where + +import HushGP.PushTests.GenericTests +import HushGP.PushTests.IntTests +import HushGP.PushTests.UtilTests diff --git a/src/HushGP/PushTests/GenericTests.hs b/src/HushGP/PushTests/GenericTests.hs new file mode 100644 index 0000000..807af7b --- /dev/null +++ b/src/HushGP/PushTests/GenericTests.hs @@ -0,0 +1,129 @@ +module HushGP.PushTests.GenericTests where + +import HushGP.State +import Control.Lens +-- import Debug.Trace +import Test.QuickCheck +-- import HushGP.Instructions.GenericInstructions + +-- The naming scheme: +-- the letters at the beginning represent what kind of transformation (the word I'm using for a basic function) to the states is happening +-- for example: the function aaa1Test relays this arg takes a transformation of two as and turns them into one a +-- the numbers represent how many different stacks are used in the function. +-- for example: the aaa1Test relays that it takes one stack as input. These stacks are passed in as Lens + +-- We may be able to get rid of Lens entirely and use haskell's integrated accessors of type State -> [a] +-- You can see what I'm talking about if you go into ghci and type: `:info _int` for example + +aaa1Test :: (Show a, Eq a) => Lens' State [a] -> (State -> State) -> (a -> a -> a) -> State -> Property +aaa1Test accessor instruction transformation state = + case (uncons (view accessor state), uncons (view accessor $ instruction state)) of + (Just (origx1, origx2 : _), Just (modx1, _)) -> transformation origx2 origx1 === modx1 .&&. length (view accessor state) === length (view accessor $ instruction state) + 1 + _ -> state === instruction state + +aa1Test :: (Show a, Eq a) => Lens' State [a] -> (State -> State) -> (a -> a) -> State -> Property +aa1Test accessor instruction transformation state = + case (uncons (view accessor state), uncons (view accessor $ instruction state)) of + (Just (origx1, _), Just (modx1, _)) -> transformation origx1 === modx1 .&&. length (view accessor state) === length (view accessor $ instruction state) + _ -> state === instruction state + +ab1Test :: (Show b, Eq b) => Lens' State [a] -> Lens' State [b] -> (State -> State) -> (a -> b) -> State -> Property +ab1Test accessorFrom accessorTo instruction transformation state = + case (uncons (view accessorTo $ instruction state), uncons (view accessorFrom state)) of + (Just (t1, _), Just (f1, _)) -> + t1 === transformation f1 .&&. + length (view accessorTo $ instruction state) === length (view accessorTo state) + 1 .&&. + length (view accessorFrom $ instruction state) === length (view accessorFrom state) - 1 + _ -> state === instruction state + +aab2Test :: (Show b, Eq b) => Lens' State [a] -> Lens' State [b] -> (State -> State) -> (a -> a -> b) -> State -> Property +aab2Test accessorFrom accessorTo instruction transformation state = + case (uncons (view accessorTo $ instruction state), uncons (view accessorFrom state)) of + (Just (t1, _), Just (f1, f2 : _)) -> + t1 === transformation f1 f2 .&&. + length (view accessorTo $ instruction state) == length (view accessorTo state) + 1 .&&. + length (view accessorFrom $ instruction state) == length (view accessorFrom state) - 2 + _ -> state === instruction state + +popTest :: (Show a) => Lens' State [a] -> (State -> State) -> State -> Property +popTest accessor instruction state = + if null $ view accessor state + then state === instruction state + else length (view accessor $ instruction state) === length (view accessor state) - 1 + +dupTest :: (Eq a, Show a) => Lens' State [a] -> (State -> State) -> State -> Property +dupTest accessor instruction state = + case uncons (view accessor state) of + Just (origx1, _) -> + case uncons (view accessor $ instruction state) of + Just (modx1, modx2 : _) -> + origx1 === modx1 .&&. origx1 === modx2 .&&. length (view accessor $ instruction state) === length (view accessor state) + 1 + _ -> state === instruction state + _ -> state === instruction state + +-- How to test the int stack in particular? +dupTestN :: (Eq a, Show a) => Lens' State [a] -> (State -> State) -> State -> Property +dupTestN accessor instruction state = + case uncons (view int state) of + Just (i1, is) -> + let amt = max i1 0 in + case uncons (view accessor state{_int = is}) of + Just (origx1, _) -> + conjoin (map (origx1 ===) (take amt (view accessor $ instruction state))) .&&. + length (view accessor $ instruction state) === (length (view accessor state{_int = is}) + amt - 1) + _ -> state === instruction state + _ -> state === instruction state + +swapTest :: (Show a, Eq a) => Lens' State [a] -> (State -> State) -> State -> Property +swapTest accessor instruction state = + case (uncons (view accessor state), uncons (view accessor $ instruction state)) of + (Just (origx1, origx2 : _), Just (modx1, modx2 : _)) -> origx1 === modx2 .&&. origx2 === modx1 + _ -> state === instruction state + +rotTest :: (Show a, Eq a) => Lens' State [a] -> (State -> State) -> State -> Property +rotTest accessor instruction state = + case (uncons (view accessor state), uncons (view accessor $ instruction state)) of + (Just (origx1, origx2 : origx3 : _), Just (modx1, modx2 : modx3 : _)) -> (origx1, origx2, origx3) === (modx2, modx3, modx1) + _ -> state === instruction state + +flushTest :: (Show a, Eq a) => Lens' State [a] -> (State -> State) -> State -> Property +flushTest accessor instruction state = + property $ null $ view accessor $ instruction state + +stackDepthTest :: (Show a) => Lens' State [a] -> (State -> State) -> State -> Property +stackDepthTest accessor instruction state = + case uncons (view int $ instruction state) of + Just (x1, _) -> x1 === length (view accessor state) + _ -> state === instruction state + +yankTest :: forall a. (Show a, Eq a) => Lens' State [a] -> (State -> State) -> State -> Property +yankTest accessor instruction state@(State {_int = i1 : is}) = + let + myIndex :: Int + myIndex = max 0 (min i1 (length (view accessor state{_int = is}) - 1)) + item :: a + item = view accessor state{_int = is} !! myIndex + in + case (uncons (view accessor $ instruction state), uncons is) of + (Just (x1, _), Just (_, _)) -> x1 === item + _ -> state === instruction state + -- .&&. -- unsure how to get this functional + -- length (view accessor state{_int = is}) === length (view accessor $ instruction state) +yankTest _ instruction state = state === instruction state + +-- Might just make this a unit test +-- Come back to this later +-- yankDupTest :: forall a. (Show a, Eq a) => Lens' State [a] -> (State -> State) -> State -> Property +-- yankDupTest accessor instruction state@(State {_int = i1 : is}) = +-- let +-- myIndex :: Int +-- myIndex = max 0 (min i1 (length (view accessor state{_int = is}) - 1)) +-- item :: a +-- item = view accessor state{_int = is} !! myIndex +-- in +-- case (uncons (view accessor $ instruction state), uncons is) of +-- (Just (x1, xs), Just (_, _)) -> x1 === item .&&. (x1 : xs) !! === item +-- _ -> state === instruction state +-- yankDupTest _ instruction state = state === instruction state + +-- shoveTest diff --git a/src/HushGP/PushTests/IntTests.hs b/src/HushGP/PushTests/IntTests.hs new file mode 100644 index 0000000..acd1cdd --- /dev/null +++ b/src/HushGP/PushTests/IntTests.hs @@ -0,0 +1,84 @@ +module HushGP.PushTests.IntTests where + +import HushGP.State +import HushGP.Instructions.IntInstructions +import HushGP.PushTests.GenericTests +-- import Control.Lens hiding (uncons) +import Test.QuickCheck + +prop_IntAdd :: State -> Property +prop_IntAdd = aaa1Test int instructionIntAdd (+) + +prop_IntSub :: State -> Property +prop_IntSub = aaa1Test int instructionIntSub (-) + +prop_IntMul :: State -> Property +prop_IntMul = aaa1Test int instructionIntMul (*) + +prop_IntDiv :: State -> Property +prop_IntDiv state@(State {_int = 0 : _}) = state === instructionIntDiv state +prop_IntDiv state = aaa1Test int instructionIntDiv div state + +prop_IntMod :: State -> Property +prop_IntMod state@(State {_int = 0 : _}) = state === instructionIntMod state +prop_IntMod state = aaa1Test int instructionIntMod mod state + +prop_IntFromFloat :: State -> Property +prop_IntFromFloat = ab1Test float int instructionIntFromFloat floor + +prop_IntFromProperty :: State -> Property +prop_IntFromProperty = ab1Test bool int instructionIntFromBool (\x -> if x then 1 else 0) + +prop_IntMin :: State -> Property +prop_IntMin = aaa1Test int instructionIntMin min + +prop_IntMax :: State -> Property +prop_IntMax = aaa1Test int instructionIntMax max + +prop_IntInc :: State -> Property +prop_IntInc = aa1Test int instructionIntInc (+1) + +prop_IntDec :: State -> Property +prop_IntDec = aa1Test int instructionIntDec (\x -> x - 1) + +prop_IntLT :: State -> Property +prop_IntLT = aab2Test int bool instructionIntLT (<) + +prop_IntGT :: State -> Property +prop_IntGT = aab2Test int bool instructionIntGT (>) + +prop_IntLTE :: State -> Property +prop_IntLTE = aab2Test int bool instructionIntLTE (<=) + +prop_IntGTE :: State -> Property +prop_IntGTE = aab2Test int bool instructionIntGTE (>=) + +prop_IntDup :: State -> Property +prop_IntDup = dupTest int instructionIntDup + +prop_IntPop :: State -> Property +prop_IntPop = popTest int instructionIntPop + +prop_IntDupN :: State -> Property +prop_IntDupN = dupTestN int instructionIntDupN + +prop_IntSwap :: State -> Property +prop_IntSwap = swapTest int instructionIntSwap + +prop_IntRot :: State -> Property +prop_IntRot = rotTest int instructionIntRot + +prop_IntFlush :: State -> Property +prop_IntFlush = flushTest int instructionIntFlush + +prop_IntEq :: State -> Property +prop_IntEq = aab2Test int bool instructionIntEq (==) + +prop_IntStackDepth :: State -> Property +prop_IntStackDepth = stackDepthTest int instructionIntStackDepth + +prop_IntYank :: State -> Property +prop_IntYank = yankTest int instructionIntYank + +-- prop_IntYankDup :: State -> Property +-- prop_IntYankDup = yankDupTest int instructionIntYankDup diff --git a/src/HushGP/PushTests/UtilTests.hs b/src/HushGP/PushTests/UtilTests.hs new file mode 100644 index 0000000..ea2ce60 --- /dev/null +++ b/src/HushGP/PushTests/UtilTests.hs @@ -0,0 +1,36 @@ +module HushGP.PushTests.UtilTests where + +import HushGP.Instructions.GenericInstructions +import Test.QuickCheck + +prop_DeleteAtTest :: Int -> [Int] -> Property +prop_DeleteAtTest idx lst = + idx >= 0 && idx < length lst ==> + if null lst + then length lst === length (deleteAt idx lst) + else length lst === length (deleteAt idx lst) + 1 + +prop_CombineTupleTest :: Int -> ([Int], [Int]) -> Property +prop_CombineTupleTest val tup = + length (fst tup) + length (snd tup) === length (combineTuple val tup) - 1 + +prop_CombineTupleListTest :: [Int] -> ([Int], [Int]) -> Property +prop_CombineTupleListTest lst tup = + length (fst tup) + length (snd tup) === length (combineTupleList lst tup) - length lst + +-- Could use forAll to only generate valid tests +prop_InsertAt :: Int -> Int -> [Int] -> Property +prop_InsertAt idx val lst = + idx >= 0 && idx < length lst ==> + length lst === length (insertAt idx val lst) - 1 .&&. + insertAt idx val lst !! idx === val + +prop_ReplaceAt :: Int -> Int -> [Int] -> Property +prop_ReplaceAt idx val lst = + idx >= 0 && idx < length lst ==> + length lst === length (replaceAt idx val lst) .&&. + replaceAt idx val lst !! idx === val + +-- prop_SubList :: Int -> Int -> [Int] -> Property +-- prop_SubList idx0 idx1 lst = + -- idx diff --git a/src/HushGP/State.hs b/src/HushGP/State.hs new file mode 100644 index 0000000..acccbc2 --- /dev/null +++ b/src/HushGP/State.hs @@ -0,0 +1,166 @@ +{-# LANGUAGE DeriveGeneric #-} +{-# LANGUAGE TemplateHaskell #-} + +module HushGP.State where + +import Control.Lens hiding (elements) +import Data.Map qualified as Map +import GHC.Generics +import Test.QuickCheck + +-- The exec stack must store heterogenous types, +-- and we must be able to detect that type at runtime. +-- One solution is for the exec stack to be a list of [Gene]. +-- The parameter stack could be singular [Gene] or multiple [atomic] types. +data Gene + = GeneInt Int + | GeneFloat Float + | GeneBool Bool + | GeneString String + | GeneChar Char + | GeneVectorInt [Int] + | GeneVectorFloat [Float] + | GeneVectorBool [Bool] + | GeneVectorString [String] + | GeneVectorChar [Char] + | StateFunc (State -> State, String) -- The string stores the name of the function + | PlaceInput String + | Close + | Block [Gene] + deriving (Generic) + +instance Eq Gene where + GeneInt x == GeneInt y = x == y + GeneFloat x == GeneFloat y = x == y + GeneBool x == GeneBool y = x == y + GeneString x == GeneString y = x == y + GeneChar x == GeneChar y = x == y + PlaceInput x == PlaceInput y = x == y + GeneVectorInt xs == GeneVectorInt ys = xs == ys + GeneVectorFloat xs == GeneVectorFloat ys = xs == ys + GeneVectorBool xs == GeneVectorBool ys = xs == ys + GeneVectorString xs == GeneVectorString ys = xs == ys + GeneVectorChar xs == GeneVectorChar ys = xs == ys + Close == Close = True + StateFunc (_, nameX) == StateFunc (_, nameY) = nameX == nameY + Block x == Block y = x == y + _ == _ = False + +instance Show Gene where + show (GeneInt x) = "Int: " <> show x + show (GeneFloat x) = "Float: " <> show x + show (GeneBool x) = "Bool: " <> show x + show (GeneString x) = "String: " <> x + show (GeneChar x) = "Char: " <> show x + show (StateFunc (_, funcName)) = "Func: " <> funcName + show (PlaceInput x) = "In: " <> show x + show (GeneVectorInt xs) = "Int Vec: " <> show xs + show (GeneVectorFloat xs) = "Float Vec: " <> show xs + show (GeneVectorBool xs) = "Bool Vec: " <> show xs + show (GeneVectorString xs) = "String Vec: " <> show xs + show (GeneVectorChar xs) = "Char Vec: " <> show xs + show Close = "Close" + show (Block xs) = "Block: " <> show xs + +instance CoArbitrary Gene + +instance Arbitrary Gene where + arbitrary = + oneof + [ GeneInt <$> arbitrary, + GeneFloat <$> arbitrary, + GeneBool <$> arbitrary, + GeneString <$> arbitrary, + GeneChar <$> arbitrary, + StateFunc <$> arbitrary, + PlaceInput <$> arbitrary, + GeneVectorInt <$> arbitrary, + GeneVectorFloat <$> arbitrary, + GeneVectorBool <$> arbitrary, + GeneVectorString <$> arbitrary, + GeneVectorChar <$> arbitrary, + Block <$> arbitrary, + return Close + ] + +data State = State + { _exec :: [Gene], + _code :: [Gene], + _int :: [Int], + _float :: [Float], + _bool :: [Bool], + _string :: [String], + _char :: [Char], + _vectorInt :: [[Int]], + _vectorFloat :: [[Float]], + _vectorBool :: [[Bool]], + _vectorString :: [[String]], + _vectorChar :: [[Char]], + _parameter :: [Gene], + _input :: Map.Map String Gene + } + deriving (Show, Eq, Generic) + +instance Arbitrary State where + arbitrary = do + arbExec <- arbitrary + arbCode <- arbitrary + arbInt <- arbitrary + arbFloat <- arbitrary + arbBool <- arbitrary + arbString <- arbitrary + arbChar <- arbitrary + arbVectorInt <- arbitrary + arbVectorFloat <- arbitrary + arbVectorBool <- arbitrary + arbVectorString <- arbitrary + arbVectorChar <- arbitrary + arbParameter <- arbitrary + -- arbInput <- arbitrary + State arbExec arbCode arbInt arbFloat arbBool arbString arbChar arbVectorInt arbVectorFloat arbVectorBool arbVectorString arbVectorChar arbParameter <$> arbitrary + +-- Thanks hlint lol + +instance CoArbitrary State + +emptyState :: State +emptyState = + State + { _exec = [], + _code = [], + _int = [], + _float = [], + _bool = [], + _string = [], + _char = [], + _parameter = [], + _vectorInt = [], + _vectorFloat = [], + _vectorBool = [], + _vectorString = [], + _vectorChar = [], + _input = Map.empty + } + +exampleState :: State +exampleState = + State + { _exec = [], + _code = [], + _int = [32, 56], + _float = [3.23, 9.235], + _bool = [True, False], + _string = ["abc", "123"], + _char = ['d', 'e', 'f'], + _parameter = [], + _vectorInt = [[1, 2], [5, 6, 8]], + _vectorFloat = [[1.234, 9.21], [5.42, 6.221, 8.5493]], + _vectorBool = [[True, False], [False, False, True]], + _vectorString = [["this is a sentence", "this is also a sentence"], ["s0", "s1", "s2"]], + _vectorChar = [['z', 'x'], ['r', 'a', 't', 'l']], + _input = Map.empty + } + +-- This must stay at the end of the file. +-- Template haskell seems to be messing with GHC.Generics +$(makeLenses ''State) From 813b4db5411b94932c92d4a3628a570858d270a7 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Thu, 6 Feb 2025 18:37:09 -0600 Subject: [PATCH 120/171] moved --- src/GP.hs | 3 - src/Instructions.hs | 504 ------------------ src/Instructions/CharInstructions.hs | 89 ---- src/Instructions/CodeInstructions.hs | 310 ----------- src/Instructions/ExecInstructions.hs | 106 ---- src/Instructions/FloatInstructions.hs | 116 ---- src/Instructions/GenericInstructions.hs | 348 ------------ src/Instructions/IntInstructions.hs | 104 ---- src/Instructions/LogicalInstructions.hs | 79 --- src/Instructions/StringInstructions.hs | 231 -------- src/Instructions/VectorCharInstructions.hs | 106 ---- src/Instructions/VectorFloatInstructions.hs | 106 ---- src/Instructions/VectorIntInstructions.hs | 106 ---- src/Instructions/VectorLogicalInstructions.hs | 106 ---- src/Instructions/VectorStringInstructions.hs | 106 ---- src/Push.hs | 86 --- src/PushTests.hs | 10 - src/PushTests/GenericTests.hs | 129 ----- src/PushTests/IntTests.hs | 84 --- src/PushTests/UtilTests.hs | 36 -- src/State.hs | 166 ------ 21 files changed, 2931 deletions(-) delete mode 100644 src/GP.hs delete mode 100644 src/Instructions.hs delete mode 100644 src/Instructions/CharInstructions.hs delete mode 100644 src/Instructions/CodeInstructions.hs delete mode 100644 src/Instructions/ExecInstructions.hs delete mode 100644 src/Instructions/FloatInstructions.hs delete mode 100644 src/Instructions/GenericInstructions.hs delete mode 100644 src/Instructions/IntInstructions.hs delete mode 100644 src/Instructions/LogicalInstructions.hs delete mode 100644 src/Instructions/StringInstructions.hs delete mode 100644 src/Instructions/VectorCharInstructions.hs delete mode 100644 src/Instructions/VectorFloatInstructions.hs delete mode 100644 src/Instructions/VectorIntInstructions.hs delete mode 100644 src/Instructions/VectorLogicalInstructions.hs delete mode 100644 src/Instructions/VectorStringInstructions.hs delete mode 100644 src/Push.hs delete mode 100644 src/PushTests.hs delete mode 100644 src/PushTests/GenericTests.hs delete mode 100644 src/PushTests/IntTests.hs delete mode 100644 src/PushTests/UtilTests.hs delete mode 100644 src/State.hs diff --git a/src/GP.hs b/src/GP.hs deleted file mode 100644 index 3b0f83a..0000000 --- a/src/GP.hs +++ /dev/null @@ -1,3 +0,0 @@ -module GP where - --- import Debug.Trace (trace, traceStack) diff --git a/src/Instructions.hs b/src/Instructions.hs deleted file mode 100644 index c9d3e46..0000000 --- a/src/Instructions.hs +++ /dev/null @@ -1,504 +0,0 @@ -module Instructions - ( module Instructions.GenericInstructions, - module Instructions.IntInstructions, - module Instructions.FloatInstructions, - module Instructions.StringInstructions, - module Instructions.CharInstructions, - module Instructions.CodeInstructions, - module Instructions.ExecInstructions, - module Instructions.LogicalInstructions, - module Instructions.VectorIntInstructions, - module Instructions.VectorFloatInstructions, - module Instructions.VectorStringInstructions, - module Instructions.VectorLogicalInstructions, - module Instructions.VectorCharInstructions, - allIntInstructions, - allFloatInstructions, - allBoolInstructions, - allCharInstructions, - allCodeInstructions, - allExecInstructions, - allStringInstructions, - allVectorIntInstructions, - allVectorFloatInstructions, - allVectorCharInstructions, - allVectorStringInstructions, - allVectorBoolInstructions, - allInstructions - ) -where - -import Instructions.CharInstructions -import Instructions.CodeInstructions -import Instructions.ExecInstructions -import Instructions.FloatInstructions -import Instructions.GenericInstructions -import Instructions.IntInstructions -import Instructions.LogicalInstructions -import Instructions.StringInstructions -import Instructions.VectorCharInstructions -import Instructions.VectorFloatInstructions -import Instructions.VectorIntInstructions -import Instructions.VectorLogicalInstructions -import Instructions.VectorStringInstructions -import State - -allIntInstructions :: [Gene] -allIntInstructions = map StateFunc [ - (instructionIntFromFloat, "instructionIntFromFloat"), - (instructionIntFromBool, "instructionIntFromBool"), - (instructionIntAdd, "instructionIntAdd"), - (instructionIntSub, "instructionIntSub"), - (instructionIntMul, "instructionIntMul"), - (instructionIntDiv, "instructionIntDiv"), - (instructionIntMod, "instructionIntMod"), - (instructionIntMin, "instructionIntMin"), - (instructionIntMax, "instructionIntMax"), - (instructionIntInc, "instructionIntInc"), - (instructionIntDec, "instructionIntDec"), - (instructionIntLT, "instructionIntLT"), - (instructionIntGT, "instructionIntGT"), - (instructionIntLTE, "instructionIntLTE"), - (instructionIntGTE, "instructionIntGTE"), - (instructionIntDup, "instructionIntDup"), - (instructionIntPop, "instructionIntPop"), - (instructionIntDupN, "instructionIntDupN"), - (instructionIntSwap, "instructionIntSwap"), - (instructionIntRot, "instructionIntRot"), - (instructionIntFlush, "instructionIntFlush"), - (instructionIntEq, "instructionIntEq"), - (instructionIntYank, "instructionIntYank"), - (instructionIntYankDup, "instructionIntYankDup"), - (instructionIntShove, "instructionIntShove"), - (instructionIntIsEmpty, "instructionIntIsEmpty") - ] - -allFloatInstructions :: [Gene] -allFloatInstructions = map StateFunc [ - (instructionFloatFromInt, "instructionFloatFromInt"), - (instructionFloatFromBool, "instructionFloatFromBool"), - (instructionFloatAdd, "instructionFloatAdd"), - (instructionFloatSub, "instructionFloatSub"), - (instructionFloatMul, "instructionFloatMul"), - (instructionFloatDiv, "instructionFloatDiv"), - (instructionFloatMod, "instructionFloatMod"), - (instructionFloatMin, "instructionFloatMin"), - (instructionFloatMax, "instructionFloatMax"), - (instructionFloatInc, "instructionFloatInc"), - (instructionFloatDec, "instructionFloatDec"), - (instructionFloatLT, "instructionFloatLT"), - (instructionFloatGT, "instructionFloatGT"), - (instructionFloatLTE, "instructionFloatLTE"), - (instructionFloatGTE, "instructionFloatGTE"), - (instructionFloatDup, "instructionFloatDup"), - (instructionFloatPop, "instructionFloatPop"), - (instructionFloatDupN, "instructionFloatDupN"), - (instructionFloatSwap, "instructionFloatSwap"), - (instructionFloatRot, "instructionFloatRot"), - (instructionFloatFlush, "instructionFloatFlush"), - (instructionFloatEq, "instructionFloatEq"), - (instructionFloatYank, "instructionFloatYank"), - (instructionFloatYankDup, "instructionFloatYankDup"), - (instructionFloatShove, "instructionFloatShove"), - (instructionFloatIsEmpty, "instructionFloatIsEmpty") - ] - -allBoolInstructions :: [Gene] -allBoolInstructions = map StateFunc [ - (instructionBoolFromInt, "instructionBoolFromInt"), - (instructionBoolFromFloat, "instructionBoolFromFloat"), - (instructionBoolAnd, "instructionBoolAnd"), - (instructionBoolInvertFirstThenAnd, "instructionBoolInvertFirstThenAnd"), - (instructionBoolInvertSecondThenAnd, "instructionBoolInvertSecondThenAnd"), - (instructionBoolOr, "instructionBoolOr"), - (instructionBoolXor, "instructionBoolXor"), - (instructionBoolPop, "instructionBoolPop"), - (instructionBoolDup, "instructionBoolDup"), - (instructionBoolDupN, "instructionBoolDupN"), - (instructionBoolSwap, "instructionBoolSwap"), - (instructionBoolRot, "instructionBoolRot"), - (instructionBoolFlush, "instructionBoolFlush"), - (instructionBoolEq, "instructionBoolEq"), - (instructionBoolStackDepth, "instructionBoolStackDepth"), - (instructionBoolYank, "instructionBoolYank"), - (instructionBoolYankDup, "instructionBoolYankDup"), - (instructionBoolShove, "instructionBoolShove"), - (instructionBoolShoveDup, "instructionBoolShoveDup"), - (instructionBoolIsEmpty, "instructionBoolIsEmpty") - ] - -allCharInstructions :: [Gene] -allCharInstructions = map StateFunc [ - (instructionCharConcat, "instructionCharConcat"), - (instructionCharFromFirstChar, "instructionCharFromFirstChar"), - (instructionCharFromLastChar, "instructionCharFromLastChar"), - (instructionCharFromNthChar, "instructionCharFromNthChar"), - (instructionCharIsWhitespace, "instructionCharIsWhitespace"), - (instructionCharIsLetter, "instructionCharIsLetter"), - (instructionCharIsDigit, "instructionCharIsDigit"), - (instructionCharFromBool, "instructionCharFromBool"), - (instructionCharFromAsciiInt, "instructionCharFromAsciiInt"), - (instructionCharFromAsciiFloat, "instructionCharFromAsciiFloat"), - (instructionCharsFromString, "instructionCharsFromString"), - (instructionCharPop, "instructionCharPop"), - (instructionCharDup, "instructionCharDup"), - (instructionCharDupN, "instructionCharDupN"), - (instructionCharSwap, "instructionCharSwap"), - (instructionCharRot, "instructionCharRot"), - (instructionCharFlush, "instructionCharFlush"), - (instructionCharEq, "instructionCharEq"), - (instructionCharStackDepth, "instructionCharStackDepth"), - (instructionCharYank, "instructionCharYank"), - (instructionCharYankDup, "instructionCharYankDup"), - (instructionCharShove, "instructionCharShove"), - (instructionCharShoveDup, "instructionCharShoveDup"), - (instructionCharIsEmpty, "instructionCharIsEmpty") - ] - -allCodeInstructions :: [Gene] -allCodeInstructions = map StateFunc [ - (instructionCodePop, "instructionCodePop"), - (instructionCodeIsCodeBlock, "instructionCodeIsCodeBlock"), - (instructionCodeIsSingular, "instructionCodeIsSingular"), - (instructionCodeLength, "instructionCodeLength"), - (instructionCodeFirst, "instructionCodeFirst"), - (instructionCodeLast, "instructionCodeLast"), - (instructionCodeTail, "instructionCodeTail"), - (instructionCodeInit, "instructionCodeInit"), - (instructionCodeWrap, "instructionCodeWrap"), - (instructionCodeList, "instructionCodeList"), - (instructionCodeCombine, "instructionCodeCombine"), - (instructionCodeDo, "instructionCodeDo"), - (instructionCodeDoDup, "instructionCodeDoDup"), - (instructionCodeDoThenPop, "instructionCodeDoThenPop"), - (instructionCodeDoRange, "instructionCodeDoRange"), - (instructionCodeDoCount, "instructionCodeDoCount"), - (instructionCodeDoTimes, "instructionCodeDoTimes"), - (instructionCodeIf, "instructionCodeIf"), - (instructionCodeWhen, "instructionCodeWhen"), - (instructionCodeMember, "instructionCodeMember"), - (instructionCodeN, "instructionCodeN"), - (instructionMakeEmptyCodeBlock, "instructionMakeEmptyCodeBlock"), - (instructionIsEmptyCodeBlock, "instructionIsEmptyCodeBlock"), - (instructionCodeSize, "instructionCodeSize"), - (instructionCodeExtract, "instructionCodeExtract"), - (instructionCodeInsert, "instructionCodeInsert"), - (instructionCodeFirstPosition, "instructionCodeFirstPosition"), - (instructionCodeReverse, "instructionCodeReverse"), - (instructionCodeDup, "instructionCodeDup"), - (instructionCodeDupN, "instructionCodeDupN"), - (instructionCodeDup, "instructionCodeDup"), - (instructionCodeDupN, "instructionCodeDupN"), - (instructionCodeSwap, "instructionCodeSwap"), - (instructionCodeRot, "instructionCodeRot"), - (instructionCodeFlush, "instructionCodeFlush"), - (instructionCodeEq, "instructionCodeEq"), - (instructionCodeStackDepth, "instructionCodeStackDepth"), - (instructionCodeYank, "instructionCodeYank"), - (instructionCodeYankDup, "instructionCodeYankDup"), - (instructionCodeShove, "instructionCodeShove"), - (instructionCodeShoveDup, "instructionCodeShoveDup"), - (instructionCodeStackIsEmpty, "instructionCodeStackIsEmpty"), - (instructionCodeFromBool, "instructionCodeFromBool"), - (instructionCodeFromInt, "instructionCodeFromInt"), - (instructionCodeFromChar, "instructionCodeFromChar"), - (instructionCodeFromFloat, "instructionCodeFromFloat"), - (instructionCodeFromString, "instructionCodeFromString"), - (instructionCodeFromVectorInt, "instructionCodeFromVectorInt"), - (instructionCodeFromVectorFloat, "instructionCodeFromVectorFloat"), - (instructionCodeFromVectorString, "instructionCodeFromVectorString"), - (instructionCodeFromVectorBool, "instructionCodeFromVectorBool"), - (instructionCodeFromVectorChar, "instructionCodeFromVectorChar"), - (instructionCodeFromExec, "instructionCodeFromExec") - ] - -allExecInstructions :: [Gene] -allExecInstructions = map StateFunc [ - (instructionExecIf, "instructionExecIf"), - (instructionExecDup, "instructionExecDup"), - (instructionExecDupN, "instructionExecDupN"), - (instructionExecPop, "instructionExecPop"), - (instructionExecSwap, "instructionExecSwap"), - (instructionExecRot, "instructionExecRot"), - (instructionExecFlush, "instructionExecFlush"), - (instructionExecEq, "instructionExecEq"), - (instructionExecStackDepth, "instructionExecStackDepth"), - (instructionExecYank, "instructionExecYank"), - (instructionExecYankDup, "instructionExecYankDup"), - (instructionExecShove, "instructionExecShove"), - (instructionExecShoveDup, "instructionExecShoveDup"), - (instructionExecIsEmpty, "instructionExecIsEmpty"), - (instructionExecDoRange, "instructionExecDoRange"), - (instructionExecDoCount, "instructionExecDoCount"), - (instructionExecDoTimes, "instructionExecDoTimes"), - (instructionExecWhile, "instructionExecWhile"), - (instructionExecDoWhile, "instructionExecDoWhile"), - (instructionExecWhen, "instructionExecWhen") - ] - -allStringInstructions :: [Gene] -allStringInstructions = map StateFunc [ - (instructionStringConcat, "instructionStringConcat"), - (instructionStringSwap, "instructionStringSwap"), - (instructionStringInsertString, "instructionStringInsertString"), - (instructionStringFromFirstChar, "instructionStringFromFirstChar"), - (instructionStringFromLastChar, "instructionStringFromLastChar"), - (instructionStringFromNthChar, "instructionStringFromNthChar"), - (instructionStringIndexOfString, "instructionStringIndexOfString"), - (instructionStringContainsString, "instructionStringContainsString"), - (instructionStringSplitOnString, "instructionStringSplitOnString"), - (instructionStringReplaceFirstString, "instructionStringReplaceFirstString"), - (instructionStringReplaceNString, "instructionStringReplaceNString"), - (instructionStringReplaceAllString, "instructionStringReplaceAllString"), - (instructionStringRemoveFirstString, "instructionStringRemoveFirstString"), - (instructionStringRemoveNString, "instructionStringRemoveNString"), - (instructionStringRemoveAllString, "instructionStringRemoveAllString"), - (instructionStringOccurrencesOfString, "instructionStringOccurrencesOfString"), - (instructionStringInsertChar, "instructionStringInsertChar"), - (instructionStringContainsChar, "instructionStringContainsChar"), - (instructionStringIndexOfChar, "instructionStringIndexOfChar"), - (instructionStringSplitOnChar, "instructionStringSplitOnChar"), - (instructionStringReplaceFirstChar, "instructionStringReplaceFirstChar"), - (instructionStringReplaceNChar, "instructionStringReplaceNChar"), - (instructionStringReplaceAllChar, "instructionStringReplaceAllChar"), - (instructionStringRemoveFirstChar, "instructionStringRemoveFirstChar"), - (instructionStringRemoveNChar, "instructionStringRemoveNChar"), - (instructionStringRemoveAllChar, "instructionStringRemoveAllChar"), - (instructionStringOccurrencesOfChar, "instructionStringOccurrencesOfChar"), - (instructionStringReverse, "instructionStringReverse"), - (instructionStringHead, "instructionStringHead"), - (instructionStringTail, "instructionStringTail"), - (instructionStringAppendChar, "instructionStringAppendChar"), - (instructionStringRest, "instructionStringRest"), - (instructionStringButLast, "instructionStringButLast"), - (instructionStringDrop, "instructionStringDrop"), - (instructionStringButLastN, "instructionStringButLastN"), - (instructionStringLength, "instructionStringLength"), - (instructionStringMakeEmpty, "instructionStringMakeEmpty"), - (instructionStringIsEmptyString, "instructionStringIsEmptyString"), - (instructionStringRemoveNth, "instructionStringRemoveNth"), - (instructionStringSetNth, "instructionStringSetNth"), - (instructionStringStripWhitespace, "instructionStringStripWhitespace"), - (instructionStringFromBool, "instructionStringFromBool"), - (instructionStringFromInt, "instructionStringFromInt"), - (instructionStringFromFloat, "instructionStringFromFloat"), - (instructionStringFromChar, "instructionStringFromChar"), - (instructionStringPop, "instructionStringPop"), - (instructionStringDup, "instructionStringDup"), - (instructionStringDupN, "instructionStringDupN"), - (instructionStringSwap, "instructionStringSwap"), - (instructionStringRot, "instructionStringRot"), - (instructionStringFlush, "instructionStringFlush"), - (instructionStringEq, "instructionStringEq"), - (instructionStringStackDepth, "instructionStringStackDepth"), - (instructionStringYank, "instructionStringYank"), - (instructionStringYankDup, "instructionStringYankDup"), - (instructionStringShove, "instructionStringShove"), - (instructionStringShoveDup, "instructionStringShoveDup"), - (instructionStringIsEmpty, "instructionStringIsEmpty") - ] - -allVectorIntInstructions :: [Gene] -allVectorIntInstructions = map StateFunc [ - (instructionVectorIntConcat, "instructionVectorIntConcat"), - (instructionVectorIntConj, "instructionVectorIntConj"), - (instructionVectorIntTakeN, "instructionVectorIntTakeN"), - (instructionVectorIntSubVector, "instructionVectorIntSubVector"), - (instructionVectorIntFirst, "instructionVectorIntFirst"), - (instructionVectorIntLast, "instructionVectorIntLast"), - (instructionVectorIntNth, "instructionVectorIntNth"), - (instructionVectorIntRest, "instructionVectorIntRest"), - (instructionVectorIntButLast, "instructionVectorIntButLast"), - (instructionVectorIntLength, "instructionVectorIntLength"), - (instructionVectorIntReverse, "instructionVectorIntReverse"), - (instructionVectorIntPushAll, "instructionVectorIntPushAll"), - (instructionVectorIntMakeEmpty, "instructionVectorIntMakeEmpty"), - (instructionVectorIntIsEmpty, "instructionVectorIntIsEmpty"), - (instructionVectorIntIndexOf, "instructionVectorIntIndexOf"), - (instructionVectorIntOccurrencesOf, "instructionVectorIntOccurrencesOf"), - (instructionVectorIntSetNth, "instructionVectorIntSetNth"), - (instructionVectorIntReplace, "instructionVectorIntReplace"), - (instructionVectorIntReplaceFirst, "instructionVectorIntReplaceFirst"), - (instructionVectorIntRemove, "instructionVectorIntRemove"), - (instructionVectorIntIterate, "instructionVectorIntIterate"), - (instructionVectorIntPop, "instructionVectorIntPop"), - (instructionVectorIntDup, "instructionVectorIntDup"), - (instructionVectorIntDupN, "instructionVectorIntDupN"), - (instructionVectorIntSwap, "instructionVectorIntSwap"), - (instructionVectorIntRot, "instructionVectorIntRot"), - (instructionVectorIntFlush, "instructionVectorIntFlush"), - (instructionVectorIntEq, "instructionVectorIntEq"), - (instructionVectorIntStackDepth, "instructionVectorIntStackDepth"), - (instructionVectorIntYank, "instructionVectorIntYank"), - (instructionVectorIntYankDup, "instructionVectorIntYankDup"), - (instructionVectorIntShove, "instructionVectorIntShove"), - (instructionVectorIntShoveDup, "instructionVectorIntShoveDup"), - (instructionVectorIntStackIsEmpty, "instructionVectorIntStackIsEmpty") - ] - -allVectorFloatInstructions :: [Gene] -allVectorFloatInstructions = map StateFunc [ - (instructionVectorFloatConcat, "instructionVectorFloatConcat"), - (instructionVectorFloatConj, "instructionVectorFloatConj"), - (instructionVectorFloatTakeN, "instructionVectorFloatTakeN"), - (instructionVectorFloatSubVector, "instructionVectorFloatSubVector"), - (instructionVectorFloatFirst, "instructionVectorFloatFirst"), - (instructionVectorFloatLast, "instructionVectorFloatLast"), - (instructionVectorFloatNth, "instructionVectorFloatNth"), - (instructionVectorFloatRest, "instructionVectorFloatRest"), - (instructionVectorFloatButLast, "instructionVectorFloatButLast"), - (instructionVectorFloatLength, "instructionVectorFloatLength"), - (instructionVectorFloatReverse, "instructionVectorFloatReverse"), - (instructionVectorFloatPushAll, "instructionVectorFloatPushAll"), - (instructionVectorFloatMakeEmpty, "instructionVectorFloatMakeEmpty"), - (instructionVectorFloatIsEmpty, "instructionVectorFloatIsEmpty"), - (instructionVectorFloatIndexOf, "instructionVectorFloatIndexOf"), - (instructionVectorFloatOccurrencesOf, "instructionVectorFloatOccurrencesOf"), - (instructionVectorFloatSetNth, "instructionVectorFloatSetNth"), - (instructionVectorFloatReplace, "instructionVectorFloatReplace"), - (instructionVectorFloatReplaceFirst, "instructionVectorFloatReplaceFirst"), - (instructionVectorFloatRemove, "instructionVectorFloatRemove"), - (instructionVectorFloatIterate, "instructionVectorFloatIterate"), - (instructionVectorFloatPop, "instructionVectorFloatPop"), - (instructionVectorFloatDup, "instructionVectorFloatDup"), - (instructionVectorFloatDupN, "instructionVectorFloatDupN"), - (instructionVectorFloatSwap, "instructionVectorFloatSwap"), - (instructionVectorFloatRot, "instructionVectorFloatRot"), - (instructionVectorFloatFlush, "instructionVectorFloatFlush"), - (instructionVectorFloatEq, "instructionVectorFloatEq"), - (instructionVectorFloatStackDepth, "instructionVectorFloatStackDepth"), - (instructionVectorFloatYank, "instructionVectorFloatYank"), - (instructionVectorFloatYankDup, "instructionVectorFloatYankDup"), - (instructionVectorFloatShove, "instructionVectorFloatShove"), - (instructionVectorFloatShoveDup, "instructionVectorFloatShoveDup"), - (instructionVectorFloatStackIsEmpty, "instructionVectorFloatStackIsEmpty") - ] - -allVectorCharInstructions :: [Gene] -allVectorCharInstructions = map StateFunc [ - (instructionVectorCharConcat, "instructionVectorCharConcat"), - (instructionVectorCharConj, "instructionVectorCharConj"), - (instructionVectorCharTakeN, "instructionVectorCharTakeN"), - (instructionVectorCharSubVector, "instructionVectorCharSubVector"), - (instructionVectorCharFirst, "instructionVectorCharFirst"), - (instructionVectorCharLast, "instructionVectorCharLast"), - (instructionVectorCharNth, "instructionVectorCharNth"), - (instructionVectorCharRest, "instructionVectorCharRest"), - (instructionVectorCharButLast, "instructionVectorCharButLast"), - (instructionVectorCharLength, "instructionVectorCharLength"), - (instructionVectorCharReverse, "instructionVectorCharReverse"), - (instructionVectorCharPushAll, "instructionVectorCharPushAll"), - (instructionVectorCharMakeEmpty, "instructionVectorCharMakeEmpty"), - (instructionVectorCharIsEmpty, "instructionVectorCharIsEmpty"), - (instructionVectorCharIndexOf, "instructionVectorCharIndexOf"), - (instructionVectorCharOccurrencesOf, "instructionVectorCharOccurrencesOf"), - (instructionVectorCharSetNth, "instructionVectorCharSetNth"), - (instructionVectorCharReplace, "instructionVectorCharReplace"), - (instructionVectorCharReplaceFirst, "instructionVectorCharReplaceFirst"), - (instructionVectorCharRemove, "instructionVectorCharRemove"), - (instructionVectorCharIterate, "instructionVectorCharIterate"), - (instructionVectorCharPop, "instructionVectorCharPop"), - (instructionVectorCharDup, "instructionVectorCharDup"), - (instructionVectorCharDupN, "instructionVectorCharDupN"), - (instructionVectorCharSwap, "instructionVectorCharSwap"), - (instructionVectorCharRot, "instructionVectorCharRot"), - (instructionVectorCharFlush, "instructionVectorCharFlush"), - (instructionVectorCharEq, "instructionVectorCharEq"), - (instructionVectorCharStackDepth, "instructionVectorCharStackDepth"), - (instructionVectorCharYank, "instructionVectorCharYank"), - (instructionVectorCharYankDup, "instructionVectorCharYankDup"), - (instructionVectorCharShove, "instructionVectorCharShove"), - (instructionVectorCharShoveDup, "instructionVectorCharShoveDup"), - (instructionVectorCharStackIsEmpty, "instructionVectorCharStackIsEmpty") - ] - -allVectorStringInstructions :: [Gene] -allVectorStringInstructions = map StateFunc [ - (instructionVectorStringConcat, "instructionVectorStringConcat"), - (instructionVectorStringConj, "instructionVectorStringConj"), - (instructionVectorStringTakeN, "instructionVectorStringTakeN"), - (instructionVectorStringSubVector, "instructionVectorStringSubVector"), - (instructionVectorStringFirst, "instructionVectorStringFirst"), - (instructionVectorStringLast, "instructionVectorStringLast"), - (instructionVectorStringNth, "instructionVectorStringNth"), - (instructionVectorStringRest, "instructionVectorStringRest"), - (instructionVectorStringButLast, "instructionVectorStringButLast"), - (instructionVectorStringLength, "instructionVectorStringLength"), - (instructionVectorStringReverse, "instructionVectorStringReverse"), - (instructionVectorStringPushAll, "instructionVectorStringPushAll"), - (instructionVectorStringMakeEmpty, "instructionVectorStringMakeEmpty"), - (instructionVectorStringIsEmpty, "instructionVectorStringIsEmpty"), - (instructionVectorStringIndexOf, "instructionVectorStringIndexOf"), - (instructionVectorStringOccurrencesOf, "instructionVectorStringOccurrencesOf"), - (instructionVectorStringSetNth, "instructionVectorStringSetNth"), - (instructionVectorStringReplace, "instructionVectorStringReplace"), - (instructionVectorStringReplaceFirst, "instructionVectorStringReplaceFirst"), - (instructionVectorStringRemove, "instructionVectorStringRemove"), - (instructionVectorStringIterate, "instructionVectorStringIterate"), - (instructionVectorStringPop, "instructionVectorStringPop"), - (instructionVectorStringDup, "instructionVectorStringDup"), - (instructionVectorStringDupN, "instructionVectorStringDupN"), - (instructionVectorStringSwap, "instructionVectorStringSwap"), - (instructionVectorStringRot, "instructionVectorStringRot"), - (instructionVectorStringFlush, "instructionVectorStringFlush"), - (instructionVectorStringEq, "instructionVectorStringEq"), - (instructionVectorStringStackDepth, "instructionVectorStringStackDepth"), - (instructionVectorStringYank, "instructionVectorStringYank"), - (instructionVectorStringYankDup, "instructionVectorStringYankDup"), - (instructionVectorStringShove, "instructionVectorStringShove"), - (instructionVectorStringShoveDup, "instructionVectorStringShoveDup"), - (instructionVectorStringStackIsEmpty, "instructionVectorStringStackIsEmpty") - ] - -allVectorBoolInstructions :: [Gene] -allVectorBoolInstructions = map StateFunc [ - (instructionVectorBoolConcat, "instructionVectorBoolConcat"), - (instructionVectorBoolConj, "instructionVectorBoolConj"), - (instructionVectorBoolTakeN, "instructionVectorBoolTakeN"), - (instructionVectorBoolSubVector, "instructionVectorBoolSubVector"), - (instructionVectorBoolFirst, "instructionVectorBoolFirst"), - (instructionVectorBoolLast, "instructionVectorBoolLast"), - (instructionVectorBoolNth, "instructionVectorBoolNth"), - (instructionVectorBoolRest, "instructionVectorBoolRest"), - (instructionVectorBoolButLast, "instructionVectorBoolButLast"), - (instructionVectorBoolLength, "instructionVectorBoolLength"), - (instructionVectorBoolReverse, "instructionVectorBoolReverse"), - (instructionVectorBoolPushAll, "instructionVectorBoolPushAll"), - (instructionVectorBoolMakeEmpty, "instructionVectorBoolMakeEmpty"), - (instructionVectorBoolIsEmpty, "instructionVectorBoolIsEmpty"), - (instructionVectorBoolIndexOf, "instructionVectorBoolIndexOf"), - (instructionVectorBoolOccurrencesOf, "instructionVectorBoolOccurrencesOf"), - (instructionVectorBoolSetNth, "instructionVectorBoolSetNth"), - (instructionVectorBoolReplace, "instructionVectorBoolReplace"), - (instructionVectorBoolReplaceFirst, "instructionVectorBoolReplaceFirst"), - (instructionVectorBoolRemove, "instructionVectorBoolRemove"), - (instructionVectorBoolIterate, "instructionVectorBoolIterate"), - (instructionVectorBoolPop, "instructionVectorBoolPop"), - (instructionVectorBoolDup, "instructionVectorBoolDup"), - (instructionVectorBoolDupN, "instructionVectorBoolDupN"), - (instructionVectorBoolSwap, "instructionVectorBoolSwap"), - (instructionVectorBoolRot, "instructionVectorBoolRot"), - (instructionVectorBoolFlush, "instructionVectorBoolFlush"), - (instructionVectorBoolEq, "instructionVectorBoolEq"), - (instructionVectorBoolStackDepth, "instructionVectorBoolStackDepth"), - (instructionVectorBoolYank, "instructionVectorBoolYank"), - (instructionVectorBoolYankDup, "instructionVectorBoolYankDup"), - (instructionVectorBoolShove, "instructionVectorBoolShove"), - (instructionVectorBoolShoveDup, "instructionVectorBoolShoveDup"), - (instructionVectorBoolStackIsEmpty, "instructionVectorBoolStackIsEmpty") - ] - -allInstructions :: [Gene] -allInstructions = - allIntInstructions <> - allFloatInstructions <> - allBoolInstructions <> - allCharInstructions <> - allCodeInstructions <> - allExecInstructions <> - allStringInstructions <> - allVectorIntInstructions <> - allVectorFloatInstructions <> - allVectorCharInstructions <> - allVectorStringInstructions <> - allVectorBoolInstructions diff --git a/src/Instructions/CharInstructions.hs b/src/Instructions/CharInstructions.hs deleted file mode 100644 index 29fcdd9..0000000 --- a/src/Instructions/CharInstructions.hs +++ /dev/null @@ -1,89 +0,0 @@ -module Instructions.CharInstructions where - -import Data.Char -import State -import Instructions.StringInstructions (wschars) -import Instructions.GenericInstructions - -intToAscii :: Integral a => a -> Char -intToAscii val = chr (abs (fromIntegral val) `mod` 128) - -instructionCharConcat :: State -> State -instructionCharConcat state@(State {_char = c1 : c2 : cs, _string = ss}) = state{_char = cs, _string = [c1, c2] : ss} -instructionCharConcat state = state - -instructionCharFromFirstChar :: State -> State -instructionCharFromFirstChar state = instructionVectorFirst state char string - -instructionCharFromLastChar :: State -> State -instructionCharFromLastChar state = instructionVectorLast state char string - -instructionCharFromNthChar :: State -> State -instructionCharFromNthChar state = instructionVectorNth state char string - -instructionCharIsWhitespace :: State -> State -instructionCharIsWhitespace state@(State {_char = c1 : cs, _bool = bs}) = state{_char = cs, _bool = (c1 `elem` wschars) : bs} -instructionCharIsWhitespace state = state - -instructionCharIsLetter :: State -> State -instructionCharIsLetter state@(State {_char = c1 : cs, _bool = bs}) = state{_char = cs, _bool = isAlpha c1 : bs} -instructionCharIsLetter state = state - -instructionCharIsDigit :: State -> State -instructionCharIsDigit state@(State {_char = c1 : cs, _bool = bs}) = state{_char = cs, _bool = isDigit c1 : bs} -instructionCharIsDigit state = state - -instructionCharFromBool :: State -> State -instructionCharFromBool state@(State {_char = cs, _bool = b1 : bs}) = state{_char = (if b1 then 'T' else 'F') : cs, _bool = bs} -instructionCharFromBool state = state - -instructionCharFromAsciiInt :: State -> State -instructionCharFromAsciiInt state@(State {_char = cs, _int = i1 : is}) = state{_char = intToAscii i1 : cs, _int = is} -instructionCharFromAsciiInt state = state - -instructionCharFromAsciiFloat :: State -> State -instructionCharFromAsciiFloat state@(State {_char = cs, _float = f1 : fs}) = state{_char = intToAscii @Integer (floor f1) : cs, _float = fs} -instructionCharFromAsciiFloat state = state - -instructionCharsFromString :: State -> State -instructionCharsFromString state@(State {_char = cs, _string = s1 : ss}) = state{_char = s1 <> cs, _string = ss} -instructionCharsFromString state = state - -instructionCharPop :: State -> State -instructionCharPop state = instructionPop state char - -instructionCharDup :: State -> State -instructionCharDup state = instructionDup state char - -instructionCharDupN :: State -> State -instructionCharDupN state = instructionDupN state char - -instructionCharSwap :: State -> State -instructionCharSwap state = instructionSwap state char - -instructionCharRot :: State -> State -instructionCharRot state = instructionRot state char - -instructionCharFlush :: State -> State -instructionCharFlush state = instructionFlush state char - -instructionCharEq :: State -> State -instructionCharEq state = instructionEq state char - -instructionCharStackDepth :: State -> State -instructionCharStackDepth state = instructionStackDepth state char - -instructionCharYank :: State -> State -instructionCharYank state = instructionYank state char - -instructionCharYankDup :: State -> State -instructionCharYankDup state = instructionYankDup state char - -instructionCharIsEmpty :: State -> State -instructionCharIsEmpty state = instructionIsEmpty state char - -instructionCharShove :: State -> State -instructionCharShove state = instructionShove state char - -instructionCharShoveDup :: State -> State -instructionCharShoveDup state = instructionShoveDup state char diff --git a/src/Instructions/CodeInstructions.hs b/src/Instructions/CodeInstructions.hs deleted file mode 100644 index 654673e..0000000 --- a/src/Instructions/CodeInstructions.hs +++ /dev/null @@ -1,310 +0,0 @@ -module Instructions.CodeInstructions where - -import Data.List (elemIndex) -import State -import Instructions.GenericInstructions -import Instructions.IntInstructions --- import Debug.Trace - -isBlock :: Gene -> Bool -isBlock (Block _) = True -isBlock _ = False - -blockLength :: Gene -> Int -blockLength (Block xs) = length xs -blockLength _ = 1 - -blockIsNull :: Gene -> Bool -blockIsNull (Block xs) = null xs -blockIsNull _ = False - --- I think I can abstract the boilerplate base case check for a lot of these --- with a different function - --- empty Blocks are a thing but that shouldn't really matter -extractFirstFromBlock :: Gene -> Gene -extractFirstFromBlock (Block (x : _)) = x -extractFirstFromBlock gene = gene - -extractLastFromBlock :: Gene -> Gene -extractLastFromBlock (Block []) = Block [] -extractLastFromBlock (Block xs) = last xs -extractLastFromBlock gene = gene - -extractInitFromBlock :: Gene -> Gene -extractInitFromBlock (Block []) = Block [] -extractInitFromBlock (Block xs) = Block (init xs) -extractInitFromBlock gene = gene - -extractTailFromBlock :: Gene -> Gene -extractTailFromBlock (Block xs) = Block (drop 1 xs) -extractTailFromBlock gene = gene - --- This function took at least 3 hours to program. -codeAtPoint :: [Gene] -> Int -> Gene -codeAtPoint (gene : _) 0 = gene -codeAtPoint [] _ = Block [] -- Should only happen if an empty block is last Gene in the list of Genes -codeAtPoint ((Block nestedGenes) : genes) index = codeAtPoint (nestedGenes <> genes) (index - 1) -codeAtPoint (_ : genes) index = codeAtPoint genes (index - 1) - -codeInsertAtPoint :: [Gene] -> Gene -> Int -> [Gene] -codeInsertAtPoint oldGenes gene 0 = gene : oldGenes -codeInsertAtPoint [] gene _ = [gene] -- This shouldn't happen (lol) -codeInsertAtPoint ((Block genes) : oldGenes) gene index = Block (codeInsertAtPoint genes gene (index - 1)) : oldGenes -codeInsertAtPoint (oldGene : oldGenes) gene index = oldGene : codeInsertAtPoint oldGenes gene (index - 1) - --- This one functions differently than pysh. --- I like this one because it preserves ordering in the second case -codeCombine :: Gene -> Gene -> Gene -codeCombine (Block xs) (Block ys) = Block (xs <> ys) -codeCombine (Block xs) ygene = Block (xs <> [ygene]) -codeCombine xgene (Block ys) = Block (xgene : ys) -codeCombine xgene ygene = Block [xgene, ygene] - -codeMember :: Gene -> Gene -> Bool -codeMember (Block _) (Block _) = False -- Can't compare two lists with `elem` -codeMember (Block xs) ygene = ygene `elem` xs -codeMember _ _ = False - -codeRecursiveSize :: Gene -> Int -codeRecursiveSize (Block xs) = sum [codeRecursiveSize x + if isBlock x then 1 else 0 | x <- xs] -codeRecursiveSize _ = 1 - -instructionCodePop :: State -> State -instructionCodePop state = instructionPop state code - -instructionCodeIsCodeBlock :: State -> State -instructionCodeIsCodeBlock state@(State {_code = (c : cs), _bool = bs}) = state {_code = cs, _bool = isBlock c : bs} -instructionCodeIsCodeBlock state = state - -instructionCodeIsSingular :: State -> State -instructionCodeIsSingular state@(State {_code = (c : cs), _bool = bs}) = state {_code = cs, _bool = not (isBlock c) : bs} -instructionCodeIsSingular state = state - -instructionCodeLength :: State -> State -instructionCodeLength state@(State {_code = (c : cs), _int = is}) = state {_code = cs, _int = blockLength c : is} -instructionCodeLength state = state - -instructionCodeFirst :: State -> State -instructionCodeFirst state@(State {_code = (c : cs)}) = state {_code = extractFirstFromBlock c : cs} -instructionCodeFirst state = state - -instructionCodeLast :: State -> State -instructionCodeLast state@(State {_code = (c : cs)}) = state {_code = extractLastFromBlock c : cs} -instructionCodeLast state = state - --- https://erp12.github.io/pyshgp/html/core_instructions.html#code-rest -instructionCodeTail :: State -> State -instructionCodeTail state@(State {_code = (c : cs)}) = state {_code = extractTailFromBlock c : cs} -instructionCodeTail state = state - --- https://erp12.github.io/pyshgp/html/core_instructions.html#code-but-last -instructionCodeInit :: State -> State -instructionCodeInit state@(State {_code = (c : cs)}) = state {_code = extractInitFromBlock c : cs} -instructionCodeInit state = state - -instructionCodeWrap :: State -> State -instructionCodeWrap state@(State {_code = (c : cs)}) = state {_code = Block [c] : cs} -instructionCodeWrap state = state - -instructionCodeList :: State -> State -instructionCodeList state@(State {_code = (c1 : c2 : cs)}) = state {_code = Block [c1, c2] : cs} -instructionCodeList state = state - -instructionCodeCombine :: State -> State -instructionCodeCombine state@(State {_code = (c1 : c2 : cs)}) = state {_code = codeCombine c1 c2 : cs} -instructionCodeCombine state = state - -instructionCodeDo :: State -> State -instructionCodeDo state@(State {_code = (c1 : cs), _exec = es}) = state {_code = cs, _exec = c1: es} -instructionCodeDo state = state - -instructionCodeDoDup :: State -> State -instructionCodeDoDup state@(State {_code = (c1 : cs), _exec = es}) = state {_code = c1 : cs, _exec = c1 : es} -instructionCodeDoDup state = state - --- https://erp12.github.io/pyshgp/html/core_instructions.html#code-do-then-pop -instructionCodeDoThenPop :: State -> State -instructionCodeDoThenPop state@(State {_code = c1 : _, _exec = es}) = state {_exec = c1 : StateFunc (instructionCodePop, "instructionCodePop") : es} -instructionCodeDoThenPop state = state - -codeFromExec :: Gene -codeFromExec = StateFunc (instructionCodeFromExec, "instructionCodeFromExec") - -codeDoRange :: Gene -codeDoRange = StateFunc (instructionCodeDoRange, "instructionCodeDoRange") - -instructionCodeDoRange :: State -> State -instructionCodeDoRange state@(State {_code = (c1 : cs), _int = (i0 : i1 : is), _exec = es}) = - if increment i0 i1 /= 0 - then state {_exec = c1 : Block [GeneInt (i1 + increment i0 i1), GeneInt i0, codeFromExec, c1, codeDoRange] : es, _int = i1 : is, _code = cs} - else state {_exec = c1: es, _int = i1 : is, _code = cs} - where - increment :: Int -> Int -> Int - increment destIdx currentIdx - | currentIdx < destIdx = 1 - | currentIdx > destIdx = -1 - | otherwise = 0 -instructionCodeDoRange state = state - -instructionCodeDoCount :: State -> State -instructionCodeDoCount state@(State {_code = (c : cs), _int = (i : is), _exec = es}) = - if i < 1 - then state - else state {_code = cs, _int = is, _exec = Block [GeneInt 0, GeneInt $ i - 1, codeFromExec, c, codeDoRange] : es} -instructionCodeDoCount state = state - -instructionCodeDoTimes :: State -> State -instructionCodeDoTimes state@(State {_code = (c : cs), _int = (i : is), _exec = es}) = - if i < 1 - then state - else state {_code = cs, _int = is, _exec = Block [GeneInt 0, GeneInt $ i - 1, codeFromExec, Block [StateFunc (instructionIntPop, "instructionIntPop"), c], codeDoRange] : es} -instructionCodeDoTimes state = state - -instructionCodeIf :: State -> State -instructionCodeIf state@(State {_code = (c1 : c2 : cs), _bool = (b1 : bs), _exec = es}) = state{_code = cs, _bool = bs, _exec = (if b1 then c1 else c2) : es} -instructionCodeIf state = state - -instructionCodeWhen :: State -> State -instructionCodeWhen state@(State {_code = (c1 : cs), _bool = (b1 : bs), _exec = es}) = state{_code = cs, _bool = bs, _exec = if b1 then c1 : es else es} -instructionCodeWhen state = state - -instructionCodeMember :: State -> State -instructionCodeMember state@(State {_code = (c1 : c2 : cs), _bool = bs}) = state{_code = cs, _bool = codeMember c1 c2 : bs} -instructionCodeMember state = state - --- This one doesn't count the recursive Blocks while instructionCodeExtract does --- https://erp12.github.io/pyshgp/html/core_instructions.html#code-nth -instructionCodeN :: State -> State -instructionCodeN state@(State {_code = ((Block c1) : cs), _int = (i1 : is)}) = - if not $ null c1 - then state {_code = c1 !! index : cs, _int = is} - else state - where - index :: Int - index = abs i1 `mod` length c1 -instructionCodeN state@(State {_code = (c1 : cs), _int = _ : is}) = state {_code = c1 : cs, _int = is} -instructionCodeN state = state - -instructionMakeEmptyCodeBlock :: State -> State -instructionMakeEmptyCodeBlock state@(State {_code = cs}) = state {_code = Block [] : cs} - -instructionIsEmptyCodeBlock :: State -> State -instructionIsEmptyCodeBlock state@(State {_code = Block c1 : cs, _bool = bs}) = state{_code = cs, _bool = null c1 : bs} -instructionIsEmptyCodeBlock state@(State {_bool = bs}) = state{_bool = False : bs} - -instructionCodeSize :: State -> State -instructionCodeSize state@(State {_code = c1 : cs, _int = is}) = state{_code = cs, _int = codeRecursiveSize c1 : is} -instructionCodeSize state = state - --- There's a bug for this instruction in pysh where the last item in the --- top level Block isn't counted, and if passed 0, then the entire codeblock is returned. --- I designed this function differently so 0 returns the 0th element, and the last item --- in the codeblock can be returned. -instructionCodeExtract :: State -> State -instructionCodeExtract state@(State {_code = (block@(Block c1) : cs), _int = i1 : is}) = - let - index = abs i1 `mod` codeRecursiveSize block - in - state{_code = codeAtPoint c1 index : cs, _int = is} -instructionCodeExtract state@(State {_code = cs, _int = _ : is}) = state{_code = cs, _int = is} -instructionCodeExtract state = state - -instructionCodeInsert :: State -> State -instructionCodeInsert state@(State {_code = (block@(Block c1) : c2 : cs), _int = i1 : is}) = - let - index = abs i1 `mod` codeRecursiveSize block - in - state{_code = Block (codeInsertAtPoint c1 c2 index) : cs, _int = is} -instructionCodeInsert state@(State {_code = c1 : c2 : cs, _int = i1 : is}) = - let - index = abs i1 `mod` codeRecursiveSize (Block [c1]) - in - state{_code = Block (codeInsertAtPoint [c1] c2 index) : cs, _int = is} -instructionCodeInsert state = state - -instructionCodeFirstPosition :: State -> State -instructionCodeFirstPosition state@(State {_code = (Block []) : c2 : cs, _int = is}) = state {_code = cs, _int = (if c2 == Block [] then 0 else -1) : is} -instructionCodeFirstPosition state@(State {_code = (Block c1) : c2 : cs, _int = is}) = state {_code = cs, _int = positionElem c1 c2 : is} - where - -- This is really not gonna be good for StateFunc - positionElem :: [Gene] -> Gene -> Int - positionElem genes gene = - case elemIndex gene genes of - Nothing -> -1 - Just x -> x -instructionCodeFirstPosition state@(State {_code = c1 : c2 : cs, _int = is}) = state {_code = cs, _int = (if c1 == c2 then 0 else -1) : is} -instructionCodeFirstPosition state = state - -instructionCodeReverse :: State -> State -instructionCodeReverse state@(State {_code = (Block c1) : cs}) = state {_code = Block (reverse c1) : cs} -instructionCodeReverse state = state - -instructionCodeDup :: State -> State -instructionCodeDup state = instructionDup state code - -instructionCodeDupN :: State -> State -instructionCodeDupN state = instructionDupN state code - -instructionCodeSwap :: State -> State -instructionCodeSwap state = instructionSwap state code - -instructionCodeRot :: State -> State -instructionCodeRot state = instructionRot state code - -instructionCodeFlush :: State -> State -instructionCodeFlush state = instructionFlush state code - -instructionCodeEq :: State -> State -instructionCodeEq state = instructionEq state code - -instructionCodeStackDepth :: State -> State -instructionCodeStackDepth state = instructionStackDepth state code - -instructionCodeYank :: State -> State -instructionCodeYank state = instructionYank state code - -instructionCodeYankDup :: State -> State -instructionCodeYankDup state = instructionYankDup state code - -instructionCodeStackIsEmpty :: State -> State -instructionCodeStackIsEmpty state = instructionIsEmpty state code - -instructionCodeShove :: State -> State -instructionCodeShove state = instructionShove state code - -instructionCodeShoveDup :: State -> State -instructionCodeShoveDup state = instructionShoveDup state code - -instructionCodeFromBool :: State -> State -instructionCodeFromBool state = instructionCodeFrom state bool GeneBool - -instructionCodeFromInt :: State -> State -instructionCodeFromInt state = instructionCodeFrom state int GeneInt - -instructionCodeFromChar :: State -> State -instructionCodeFromChar state = instructionCodeFrom state char GeneChar - -instructionCodeFromFloat :: State -> State -instructionCodeFromFloat state = instructionCodeFrom state float GeneFloat - -instructionCodeFromString :: State -> State -instructionCodeFromString state = instructionCodeFrom state string GeneString - -instructionCodeFromVectorInt :: State -> State -instructionCodeFromVectorInt state = instructionCodeFrom state vectorInt GeneVectorInt - -instructionCodeFromVectorFloat :: State -> State -instructionCodeFromVectorFloat state = instructionCodeFrom state vectorFloat GeneVectorFloat - -instructionCodeFromVectorString :: State -> State -instructionCodeFromVectorString state = instructionCodeFrom state vectorString GeneVectorString - -instructionCodeFromVectorBool :: State -> State -instructionCodeFromVectorBool state = instructionCodeFrom state vectorBool GeneVectorBool - -instructionCodeFromVectorChar :: State -> State -instructionCodeFromVectorChar state = instructionCodeFrom state vectorChar GeneVectorChar - -instructionCodeFromExec :: State -> State -instructionCodeFromExec state = instructionCodeFrom state exec id diff --git a/src/Instructions/ExecInstructions.hs b/src/Instructions/ExecInstructions.hs deleted file mode 100644 index c0ab519..0000000 --- a/src/Instructions/ExecInstructions.hs +++ /dev/null @@ -1,106 +0,0 @@ -module Instructions.ExecInstructions where - -import State -import Instructions.IntInstructions -import Instructions.GenericInstructions - -instructionExecIf :: State -> State -instructionExecIf state@(State {_exec = (e1 : e2 : es), _bool = (b : bs)}) = - if b - then state {_exec = e1 : es, _bool = bs} - else state {_exec = e2 : es, _bool = bs} -instructionExecIf state = state - -instructionExecDup :: State -> State -instructionExecDup state = instructionDup state exec - -instructionExecDupN :: State -> State -instructionExecDupN state = instructionDupN state exec - -instructionExecPop :: State -> State -instructionExecPop state = instructionPop state exec - -instructionExecSwap :: State -> State -instructionExecSwap state = instructionSwap state exec - -instructionExecRot :: State -> State -instructionExecRot state = instructionRot state exec - -instructionExecFlush :: State -> State -instructionExecFlush state = instructionFlush state exec - -instructionExecEq :: State -> State -instructionExecEq state = instructionEq state exec - -instructionExecStackDepth :: State -> State -instructionExecStackDepth state = instructionStackDepth state exec - -instructionExecYank :: State -> State -instructionExecYank state = instructionYank state exec - -instructionExecYankDup :: State -> State -instructionExecYankDup state = instructionYankDup state exec - -instructionExecShove :: State -> State -instructionExecShove state = instructionShove state exec - -instructionExecShoveDup :: State -> State -instructionExecShoveDup state = instructionShoveDup state exec - -instructionExecIsEmpty :: State -> State -instructionExecIsEmpty state = instructionIsEmpty state exec - -execDoRange :: Gene -execDoRange = StateFunc (instructionExecDoRange, "instructionExecDoRange") - -instructionExecDoRange :: State -> State -instructionExecDoRange state@(State {_exec = (e1 : es), _int = (i0 : i1 : is)}) = - if increment i0 i1 /= 0 - then state {_exec = e1 : Block [GeneInt (i1 + increment i0 i1), GeneInt i0, execDoRange, e1] : es, _int = i1 : is} - else state {_exec = e1 : es, _int = i1 : is} - where - increment :: Int -> Int -> Int - increment destIdx currentIdx - | currentIdx < destIdx = 1 - | currentIdx > destIdx = -1 - | otherwise = 0 -instructionExecDoRange state = state - -instructionExecDoCount :: State -> State -instructionExecDoCount state@(State {_exec = (e : es), _int = (i : is)}) = - if i < 1 - then state - else state {_exec = Block [GeneInt 0, GeneInt $ i - 1, execDoRange, e] : es, _int = is} -instructionExecDoCount state = state - -instructionExecDoTimes :: State -> State -instructionExecDoTimes state@(State {_exec = (e : es), _int = (i : is)}) = - if i < 1 - then state - else state {_exec = Block [GeneInt 0, GeneInt $ i - 1, execDoRange, Block [StateFunc (instructionIntPop, "instructionIntPop"), e]] : es, _int = is} -instructionExecDoTimes state = state - -execWhile :: Gene -execWhile = StateFunc (instructionExecWhile, "instructionExecWhile") - -instructionExecWhile :: State -> State -instructionExecWhile state@(State {_exec = (_ : es), _bool = []}) = - state {_exec = es} -instructionExecWhile state@(State {_exec = alles@(e : es), _bool = (b : bs)}) = - if b - then state {_exec = e : execWhile : alles, _bool = bs} - else state {_exec = es} -instructionExecWhile state = state - -instructionExecDoWhile :: State -> State -instructionExecDoWhile state@(State {_exec = alles@(e : _)}) = - state {_exec = e : execWhile : alles} -instructionExecDoWhile state = state - --- Eats the _boolean no matter what -instructionExecWhen :: State -> State -instructionExecWhen state@(State {_exec = (_ : es), _bool = (b : bs)}) = - if not b - then state {_exec = es, _bool = bs} - else state {_bool = bs} -instructionExecWhen state = state diff --git a/src/Instructions/FloatInstructions.hs b/src/Instructions/FloatInstructions.hs deleted file mode 100644 index d8d89fa..0000000 --- a/src/Instructions/FloatInstructions.hs +++ /dev/null @@ -1,116 +0,0 @@ -module Instructions.FloatInstructions where - -import Data.Fixed (mod') -import Instructions.GenericInstructions -import State - -instructionFloatFromInt :: State -> State -instructionFloatFromInt state@(State {_float = fs, _int = (i : is)}) = state {_float = (fromIntegral i :: Float) : fs, _int = is} -instructionFloatFromInt state = state - -instructionFloatFromBool :: State -> State -instructionFloatFromBool state@(State {_bool = (b : bs), _float = fs}) = state {_bool = bs, _float = (if b then 1.0 else 0.0) : fs} -instructionFloatFromBool state = state - -instructionFloatAdd :: State -> State -instructionFloatAdd state@(State {_float = (f1 : f2 : fs)}) = state {_float = f2 + f1 : fs} -instructionFloatAdd state = state - -instructionFloatSub :: State -> State -instructionFloatSub state@(State {_float = (f1 : f2 : fs)}) = state {_float = f2 - f1 : fs} -instructionFloatSub state = state - -instructionFloatMul :: State -> State -instructionFloatMul state@(State {_float = (f1 : f2 : fs)}) = state {_float = f2 * f1 : fs} -instructionFloatMul state = state - -instructionFloatDiv :: State -> State -instructionFloatDiv state@(State {_float = (f1 : f2 : fs)}) = state {_float = if f1 /= 0 then f2 / f1 : fs else f1 : f2 : fs} -instructionFloatDiv state = state - -instructionFloatMod :: State -> State -instructionFloatMod state@(State {_float = (f1 : f2 : fs)}) = state {_float = if f1 /= 0 then f2 `mod'` f1 : fs else f1 : f2 : fs} -instructionFloatMod state = state - -instructionFloatMin :: State -> State -instructionFloatMin state@(State {_float = (f1 : f2 : fs)}) = state {_float = min f1 f2 : fs} -instructionFloatMin state = state - -instructionFloatMax :: State -> State -instructionFloatMax state@(State {_float = (f1 : f2 : fs)}) = state {_float = max f1 f2 : fs} -instructionFloatMax state = state - -instructionFloatInc :: State -> State -instructionFloatInc state@(State {_float = (f1 : fs)}) = state {_float = f1 + 1 : fs} -instructionFloatInc state = state - -instructionFloatDec :: State -> State -instructionFloatDec state@(State {_float = (f1 : fs)}) = state {_float = f1 - 1 : fs} -instructionFloatDec state = state - -instructionFloatLT :: State -> State -instructionFloatLT state@(State {_float = f1 : f2 : fs, _bool = bs}) = state {_float = fs, _bool = (f1 < f2) : bs} -instructionFloatLT state = state - -instructionFloatGT :: State -> State -instructionFloatGT state@(State {_float = f1 : f2 : fs, _bool = bs}) = state {_float = fs, _bool = (f1 > f2) : bs} -instructionFloatGT state = state - -instructionFloatLTE :: State -> State -instructionFloatLTE state@(State {_float = f1 : f2 : fs, _bool = bs}) = state {_float = fs, _bool = (f1 <= f2) : bs} -instructionFloatLTE state = state - -instructionFloatGTE :: State -> State -instructionFloatGTE state@(State {_float = f1 : f2 : fs, _bool = bs}) = state {_float = fs, _bool = (f1 >= f2) : bs} -instructionFloatGTE state = state - -instructionFloatPop :: State -> State -instructionFloatPop state = instructionPop state float - -instructionFloatDup :: State -> State -instructionFloatDup state = instructionDup state float - -instructionFloatDupN :: State -> State -instructionFloatDupN state = instructionDupN state float - -instructionFloatSwap :: State -> State -instructionFloatSwap state = instructionSwap state float - -instructionFloatRot :: State -> State -instructionFloatRot state = instructionRot state float - -instructionFloatFlush :: State -> State -instructionFloatFlush state = instructionFlush state float - -instructionFloatEq :: State -> State -instructionFloatEq state = instructionEq state float - -instructionFloatStackDepth :: State -> State -instructionFloatStackDepth state = instructionStackDepth state float - -instructionFloatYankDup :: State -> State -instructionFloatYankDup state = instructionYankDup state float - -instructionFloatYank :: State -> State -instructionFloatYank state = instructionYank state float - -instructionFloatShoveDup :: State -> State -instructionFloatShoveDup state = instructionShoveDup state float - -instructionFloatShove :: State -> State -instructionFloatShove state = instructionShove state float - -instructionFloatIsEmpty :: State -> State -instructionFloatIsEmpty state = instructionIsEmpty state float - -instructionFloatSin :: State -> State -instructionFloatSin state@(State {_float = f1 : fs}) = state {_float = sin f1 : fs} -instructionFloatSin state = state - -instructionFloatCos :: State -> State -instructionFloatCos state@(State {_float = f1 : fs}) = state {_float = cos f1 : fs} -instructionFloatCos state = state - -instructionFloatTan :: State -> State -instructionFloatTan state@(State {_float = f1 : fs}) = state {_float = tan f1 : fs} -instructionFloatTan state = state diff --git a/src/Instructions/GenericInstructions.hs b/src/Instructions/GenericInstructions.hs deleted file mode 100644 index 8a60233..0000000 --- a/src/Instructions/GenericInstructions.hs +++ /dev/null @@ -1,348 +0,0 @@ -module Instructions.GenericInstructions where - -import Control.Lens -import State - --- import Debug.Trace - -deleteAt :: Int -> [a] -> [a] -deleteAt idx xs = take idx xs <> drop 1 (drop idx xs) - --- I could probably just combine these functions -combineTuple :: a -> ([a], [a]) -> [a] -combineTuple val tup = fst tup <> [val] <> snd tup - -combineTupleList :: [a] -> ([a], [a]) -> [a] -combineTupleList val tup = fst tup <> val <> snd tup - -insertAt :: Int -> a -> [a] -> [a] -insertAt idx val xs = combineTuple val (splitAt idx xs) - -replaceAt :: Int -> a -> [a] -> [a] -replaceAt idx val xs = deleteAt (idx + 1) (insertAt idx val xs) - -subList :: Int -> Int -> [a] -> [a] -subList idx0 idx1 xs = - let - (start, end) = if idx0 <= idx1 then (idx0, idx1) else (idx1, idx0) - adjStart = max 0 start - adjEnd = min end (length xs) - in - take adjEnd (drop adjStart xs) - --- Maybe could've used Data.List.isSubsequenceOf :shrug: -findSubA :: forall a. Eq a => [a] -> [a] -> Int -findSubA fullA subA - | length fullA < length subA = -1 - | length fullA == length subA = if fullA == subA then 0 else -1 - | otherwise = findSubA' fullA subA 0 - where - findSubA' :: [a] -> [a] -> Int -> Int - findSubA' fA sA subIndex - | null fA = -1 - | length sA > length fA = -1 - | sA == take (length sA) fA = subIndex - | otherwise = findSubA' (drop 1 fA) sA (subIndex + 1) - --- The int is the amount of olds to replace with new --- Just chain findSubA calls lol --- Nothing means replace all --- May not be the most efficient method with the findSubA calls -replace :: Eq a => [a] -> [a] -> [a] -> Maybe Int -> [a] -replace fullA old new (Just amt) = - if findSubA fullA old /= -1 && amt > 0 - then replace (take (findSubA fullA old) fullA <> new <> drop (findSubA fullA old + length old) fullA) old new (Just $ amt - 1) - else fullA -replace fullA old new Nothing = - if findSubA fullA old /= -1 - then replace (take (findSubA fullA old) fullA <> new <> drop (findSubA fullA old + length old) fullA) old new Nothing - else fullA - --- a rather inefficient search -amtOccurences :: forall a. Eq a => [a] -> [a] -> Int -amtOccurences fullA subA = amtOccurences' fullA subA 0 - where - amtOccurences' :: [a] -> [a] -> Int -> Int - amtOccurences' fA sA count = - if findSubA fA sA /= -1 - then amtOccurences' (replace fA sA mempty (Just 1)) sA (count + 1) - else count - -takeR :: Int -> [a] -> [a] -takeR amt fullA = drop (length fullA - amt) fullA - -dropR :: Int -> [a] -> [a] -dropR amt fullA = take (length fullA - amt) fullA - -safeInit :: [a] -> [a] -safeInit [] = [] -safeInit xs = init xs - -absNum :: Integral a => a -> [b] -> Int -absNum rawNum lst = abs (fromIntegral rawNum) `mod` length lst - -notEmptyStack :: State -> Lens' State [a] -> Bool -notEmptyStack state accessor = not . null $ view accessor state - -instructionDup :: State -> Lens' State [a] -> State -instructionDup state accessor = - case uncons (view accessor state) of - Nothing -> state - Just (x,_) -> state & accessor .~ x : view accessor state - -instructionPop :: State -> Lens' State [a] -> State -instructionPop state accessor = state & accessor .~ drop 1 (view accessor state) - -instructionIsEmpty :: State -> Lens' State [a] -> State -instructionIsEmpty state@(State {_bool = bs}) accessor = state{_bool = null (view accessor state) : bs} - --- instructionPop :: State -> Lens' State [a] -> State --- instructionPop state accessor = if notEmptyStack state accessor then instructionPop state accessor else state - --- I might be able to move some of the int stack error checking --- to the integer call. For now this may be a tad inefficient. -instructionDupN :: forall a. Show a => State -> Lens' State [a] -> State -instructionDupN state accessor = - case uncons (view int state) of - Just (i1,is) -> - case uncons (view accessor state{_int = is}) of - Just (a1,as) -> - instructionDupNHelper i1 a1 accessor (state{_int = is} & accessor .~ as) - _ -> state - _ -> state - where - instructionDupNHelper :: Int -> a -> Lens' State [a] -> State -> State - instructionDupNHelper count instruction internalAccessor internalState = - if count > 0 - then instructionDupNHelper (count - 1) instruction internalAccessor (internalState & accessor .~ (instruction : view accessor internalState)) - else internalState - -instructionSwap :: State -> Lens' State [a] -> State -instructionSwap state accessor = - state & accessor .~ swapper (view accessor state) - where - swapper :: [a] -> [a] - swapper (x1 : x2 : xs) = x2 : x1 : xs - swapper xs = xs - --- Rotates top 3 integers --- We could use template haskell to rotate any number of these as --- an instruction later. Template haskell seems very complicated tho. -instructionRot :: State -> Lens' State [a] -> State -instructionRot state accessor = - state & accessor .~ rotator (view accessor state) - where - rotator :: [a] -> [a] - rotator (x1 : x2 : x3 : xs) = x3 : x1 : x2 : xs - rotator xs = xs - -instructionFlush :: State -> Lens' State [a] -> State -instructionFlush state accessor = state & accessor .~ [] - -instructionEq :: forall a. Eq a => State -> Lens' State [a] -> State -instructionEq state accessor = - case uncons $ view accessor state of - Nothing -> state - Just (x1, x2 : _) -> droppedState & bool .~ (x1 == x2) : view bool droppedState - Just _ -> state - where - droppedState :: State - droppedState = state & accessor .~ drop 2 (view accessor state) - -instructionStackDepth :: State -> Lens' State [a] -> State -instructionStackDepth state@(State {_int = is}) accessor = state{_int = length (view accessor state) : is} - -instructionYankDup :: State -> Lens' State [a] -> State -instructionYankDup state@(State {_int = i : is}) accessor = - if notEmptyStack state accessor - then state{_int = is} & accessor .~ (view accessor state{_int = is} !! max 0 (min i (length (view accessor state{_int = is}) - 1))) : view accessor state{_int = is} - else state -instructionYankDup state _ = state - -instructionYank :: forall a. State -> Lens' State [a] -> State -instructionYank state@(State {_int = i : is}) accessor = - let - myIndex :: Int - myIndex = max 0 (min i (length (view accessor state{_int = is}) - 1)) - item :: a - item = view accessor state{_int = is} !! myIndex - deletedState :: State - deletedState = state{_int = is} & accessor .~ deleteAt myIndex (view accessor state{_int = is}) - in - if notEmptyStack state{_int = is} accessor then deletedState & accessor .~ item : view accessor deletedState else state -instructionYank state _ = state - --- In pysh, instructionShoveDup and instructionShove behave differently when indexing in such a way that --- the duplicated index matters whether or not it's present in the stack at the moment of calculation. --- I'm not going to keep this behavior. Check out interpysh examples for how pysh handles it. -instructionShoveDup :: State -> Lens' State [a] -> State -instructionShoveDup state@(State {_int = i : is}) accessor = - case uncons (view accessor state{_int = is}) of - Just (x,_) -> state{_int = is} & accessor .~ combineTuple x (splitAt (max 0 (min i (length (view accessor state{_int = is}) - 1))) (view accessor state{_int = is})) - _ -> state -instructionShoveDup state _ = state - -instructionShove :: State -> Lens' State [a] -> State -instructionShove state accessor = instructionShoveDup state accessor & accessor .~ drop 1 (view accessor (instructionShoveDup state accessor)) - --- not char generic -instructionConcat :: Semigroup a => State -> Lens' State [a] -> State -instructionConcat state accessor = - case uncons (view accessor state) of - Just (x1, x2:_) -> droppedState & accessor .~ (x1 <> x2) : view accessor droppedState - _ -> state - where - droppedState :: State - droppedState = state & accessor .~ drop 2 (view accessor state) - --- evolve fodder??????????? -instructionNoOp :: State -> State -instructionNoOp state = state - -instructionConj :: State -> Lens' State [a] -> Lens' State [[a]] -> State -instructionConj state primAccessor vectorAccessor = - case (uncons (view primAccessor state), uncons (view vectorAccessor state)) of - (Just (p1,ps), Just (v1,vs)) -> state & primAccessor .~ ps & vectorAccessor .~ ((p1 : v1) : vs) - _ -> state - --- v for vector, vs for vectorstack (also applicable to strings) --- Could abstract this unconsing even further in all functions below -instructionTakeN :: State -> Lens' State [[a]] -> State -instructionTakeN state@(State {_int = i1 : is}) accessor = - case uncons (view accessor state) of - Just (v1, vs) -> state{_int = is} & accessor .~ (take (absNum i1 v1) v1 : vs) - _ -> state -instructionTakeN state _ = state - -instructionSubVector :: State -> Lens' State [[a]] -> State -instructionSubVector state@(State {_int = i1 : i2 : is}) accessor = - case uncons (view accessor state) of - Just (v1, vs) -> state{_int = is} & accessor .~ (subList i1 i2 v1 : vs) - _ -> state -instructionSubVector state _ = state - -instructionVectorFirst :: State -> Lens' State [a] -> Lens' State [[a]] -> State -instructionVectorFirst state primAccessor vectorAccessor = - case uncons (view vectorAccessor state) of - Just (v1, vs) -> - case uncons v1 of - Just (vp1, _) -> state & primAccessor .~ (vp1 : view primAccessor state) & vectorAccessor .~ vs - _ -> state - _ -> state - -instructionVectorLast :: State -> Lens' State [a] -> Lens' State [[a]] -> State -instructionVectorLast state primAccessor vectorAccessor = - case uncons (view vectorAccessor state) of - Just (v1, vs) -> - case uncons (drop (length v1 - 1) v1) of -- gonna keep this implementation over using last as this can't error - Just (vplast, _) -> state & primAccessor .~ (vplast : view primAccessor state) & vectorAccessor .~ vs - _ -> state - _ -> state - -instructionVectorNth :: State -> Lens' State [a] -> Lens' State [[a]] -> State -instructionVectorNth state@(State {_int = i1 : is}) primAccessor vectorAccessor = - case uncons (view vectorAccessor state) of - Just (v1, vs) -> state{_int = is} & primAccessor .~ (v1 !! absNum i1 v1 : view primAccessor state{_int = is}) & vectorAccessor .~ vs - _ -> state -instructionVectorNth state _ _ = state - -instructionRest :: State -> Lens' State [[a]] -> State -instructionRest state accessor = - case uncons (view accessor state) of - Just (v1, vs) -> state & accessor .~ (drop 1 v1 : vs) - _ -> state - -instructionButLast :: State -> Lens' State [[a]] -> State -instructionButLast state accessor = - case uncons (view accessor state) of - Just (v1, vs) -> state & accessor .~ (safeInit v1 : vs) - _ -> state - -instructionLength :: State -> Lens' State [[a]] -> State -instructionLength state@(State {_int = is}) accessor = - case uncons (view accessor state) of - Just (v1, vs) -> state{_int = length v1 : is} & accessor .~ vs - _ -> state - -instructionReverse :: State -> Lens' State [[a]] -> State -instructionReverse state accessor = - case uncons (view accessor state) of - Just (v1, vs) -> state & accessor .~ (reverse v1 : vs) - _ -> state - -instructionPushAll :: State -> Lens' State [a] -> Lens' State [[a]] -> State -instructionPushAll state primAccessor vectorAccessor = - case uncons (view vectorAccessor state) of - Just (v1, vs) -> state & vectorAccessor .~ vs & primAccessor .~ (v1 <> view primAccessor state) - _ -> state - -instructionVectorMakeEmpty :: State -> Lens' State [[a]] -> State -instructionVectorMakeEmpty state accessor = state & accessor .~ ([] : view accessor state) - -instructionVectorIsEmpty :: State -> Lens' State [[a]] -> State -instructionVectorIsEmpty state@(State {_bool = bs}) accessor = - case uncons (view accessor state) of - Just (v1, vs) -> state{_bool = null v1 : bs} & accessor .~ vs - _ -> state - -instructionVectorContains :: Eq a => State -> Lens' State [a] -> Lens' State [[a]] -> State -instructionVectorContains state@(State {_bool = bs}) primAccessor vectorAccessor = - case (uncons (view vectorAccessor state), uncons (view primAccessor state)) of - (Just (v1, vs), Just (p1, ps)) -> state{_bool = (findSubA v1 [p1] /= -1) : bs} & vectorAccessor .~ vs & primAccessor .~ ps - _ -> state - --- I couldn't think of a better way of doing this -instructionVectorIndexOf :: Eq a => State -> Lens' State [a] -> Lens' State [[a]] -> State -instructionVectorIndexOf state primAccessor vectorAccessor = - case (uncons (view vectorAccessor state), uncons (view primAccessor state)) of - (Just (v1, vs), Just (p1, ps)) -> (state & vectorAccessor .~ vs & primAccessor .~ ps) & int .~ (findSubA v1 [p1] : view int (state & vectorAccessor .~ vs & primAccessor .~ ps)) - _ -> state - -instructionVectorOccurrencesOf :: Eq a => State -> Lens' State [a] -> Lens' State [[a]] -> State -instructionVectorOccurrencesOf state primAccessor vectorAccessor = - case (uncons (view vectorAccessor state), uncons (view primAccessor state)) of - (Just (v1, vs), Just (p1, ps)) -> (state & vectorAccessor .~ vs & primAccessor .~ ps) & int .~ (amtOccurences v1 [p1] : view int (state & vectorAccessor .~ vs & primAccessor .~ ps)) - _ -> state - -instructionVectorSetNth :: State -> Lens' State [a] -> Lens' State [[a]] -> State -instructionVectorSetNth state@(State {_int = i1 : is}) primAccessor vectorAccessor = - case (uncons (view vectorAccessor state{_int = is}), uncons (view primAccessor state{_int = is})) of - (Just (v1, vs), Just (p1, ps)) -> state{_int = is} & vectorAccessor .~ (replaceAt (absNum i1 v1) p1 v1 : vs) & primAccessor .~ ps - _ -> state -instructionVectorSetNth state _ _ = state - -instructionVectorReplace :: Eq a => State -> Lens' State [a] -> Lens' State [[a]] -> State -instructionVectorReplace state primAccessor vectorAccessor = - case (uncons (view vectorAccessor state), uncons (view primAccessor state)) of - (Just (v1, vs), Just (p1, p2 : ps)) -> state & vectorAccessor .~ (replace v1 [p1] [p2] Nothing : vs) & primAccessor .~ ps - _ -> state - -instructionVectorReplaceFirst :: Eq a => State -> Lens' State [a] -> Lens' State [[a]] -> State -instructionVectorReplaceFirst state primAccessor vectorAccessor = - case (uncons (view vectorAccessor state), uncons (view primAccessor state)) of - (Just (v1, vs), Just (p1, p2 : ps)) -> state & vectorAccessor .~ (replace v1 [p1] [p2] (Just 1) : vs) & primAccessor .~ ps - _ -> state - -instructionVectorRemove :: Eq a => State -> Lens' State [a] -> Lens' State [[a]] -> State -instructionVectorRemove state primAccessor vectorAccessor = - case (uncons (view vectorAccessor state), uncons (view primAccessor state)) of - (Just (v1, vs), Just (p1, ps)) -> state & vectorAccessor .~ (replace v1 [p1] [] Nothing : vs) & primAccessor .~ ps - _ -> state - -instructionVectorIterate :: State -> Lens' State [a] -> Lens' State [[a]] -> ([a] -> Gene) -> (State -> State) -> String -> State -instructionVectorIterate state@(State {_exec = e1 : es}) primAccessor vectorAccessor vectorType typeIterateFunction typeIterateFunctionName = - case uncons (view vectorAccessor state) of - Just ([], vs) -> state{_exec = es} & vectorAccessor .~ vs - Just ([x], vs) -> state & primAccessor .~ (x : view primAccessor state) & vectorAccessor .~ vs - Just (v1, vs) -> - (case uncons v1 of - Just (nv1, nvs) -> state{_exec = e1 : vectorType nvs : StateFunc (typeIterateFunction, typeIterateFunctionName) : e1 : es} & primAccessor .~ (nv1 : view primAccessor state) & vectorAccessor .~ vs - _ -> state) -- This should never happen - _ -> state -instructionVectorIterate state _ _ _ _ _ = state - -instructionCodeFrom :: State -> Lens' State [a] -> (a -> Gene) -> State -instructionCodeFrom state@(State {_code = cs}) accessor geneType = - case uncons (view accessor state) of - Just (x, xs) -> state{_code = geneType x : cs} & accessor .~ xs - _ -> state diff --git a/src/Instructions/IntInstructions.hs b/src/Instructions/IntInstructions.hs deleted file mode 100644 index c8a40a0..0000000 --- a/src/Instructions/IntInstructions.hs +++ /dev/null @@ -1,104 +0,0 @@ -module Instructions.IntInstructions where - -import State -import Instructions.GenericInstructions --- import Debug.Trace - -instructionIntFromFloat :: State -> State -instructionIntFromFloat state@(State {_float = (f : fs), _int = is}) = state {_float = fs, _int = floor f : is} -instructionIntFromFloat state = state - -instructionIntFromBool :: State -> State -instructionIntFromBool state@(State {_bool = (b : bs), _int = is}) = state {_bool = bs, _int = (if b then 1 else 0) : is} -instructionIntFromBool state = state - -instructionIntAdd :: State -> State -instructionIntAdd state@(State {_int = (i1 : i2 : is)}) = state {_int = i2 + i1 : is} -instructionIntAdd state = state - -instructionIntSub :: State -> State -instructionIntSub state@(State {_int = (i1 : i2 : is)}) = state {_int = i2 - i1 : is} -instructionIntSub state = state - -instructionIntMul :: State -> State -instructionIntMul state@(State {_int = (i1 : i2 : is)}) = state {_int = i2 * i1 : is} -instructionIntMul state = state - -instructionIntDiv :: State -> State -instructionIntDiv state@(State {_int = (i1 : i2 : is)}) = state {_int = if i1 /= 0 then (i2 `div` i1) : is else i1 : i2 : is} -instructionIntDiv state = state - -instructionIntMod :: State -> State -instructionIntMod state@(State {_int = (i1 : i2 : is)}) = state {_int = if i1 /= 0 then (i2 `mod` i1) : is else i1 : i2 : is} -instructionIntMod state = state - -instructionIntMin :: State -> State -instructionIntMin state@(State {_int = (i1 : i2 : is)}) = state {_int = min i1 i2 : is} -instructionIntMin state = state - -instructionIntMax :: State -> State -instructionIntMax state@(State {_int = (i1 : i2 : is)}) = state {_int = max i1 i2 : is} -instructionIntMax state = state - -instructionIntInc :: State -> State -instructionIntInc state@(State {_int = (i1 : is)}) = state {_int = i1 + 1 : is} -instructionIntInc state = state - -instructionIntDec :: State -> State -instructionIntDec state@(State {_int = (i1 : is)}) = state {_int = i1 - 1 : is} -instructionIntDec state = state - -instructionIntLT :: State -> State -instructionIntLT state@(State {_int = i1 : i2 : is, _bool = bs}) = state {_int = is, _bool = (i1 < i2) : bs} -instructionIntLT state = state - -instructionIntGT :: State -> State -instructionIntGT state@(State {_int = i1 : i2 : is, _bool = bs}) = state {_int = is, _bool = (i1 > i2) : bs} -instructionIntGT state = state - -instructionIntLTE :: State -> State -instructionIntLTE state@(State {_int = i1 : i2 : is, _bool = bs}) = state {_int = is, _bool = (i1 <= i2) : bs} -instructionIntLTE state = state - -instructionIntGTE :: State -> State -instructionIntGTE state@(State {_int = i1 : i2 : is, _bool = bs}) = state {_int = is, _bool = (i1 >= i2) : bs} -instructionIntGTE state = state - -instructionIntDup :: State -> State -instructionIntDup state = instructionDup state int - -instructionIntPop :: State -> State -instructionIntPop state = instructionPop state int - -instructionIntDupN :: State -> State -instructionIntDupN state = instructionDupN state int - -instructionIntSwap :: State -> State -instructionIntSwap state = instructionSwap state int - -instructionIntRot :: State -> State -instructionIntRot state = instructionRot state int - -instructionIntFlush :: State -> State -instructionIntFlush state = instructionFlush state int - -instructionIntEq :: State -> State -instructionIntEq state = instructionEq state int - -instructionIntStackDepth :: State -> State -instructionIntStackDepth state = instructionStackDepth state int - -instructionIntYank :: State -> State -instructionIntYank state = instructionYank state int - -instructionIntYankDup :: State -> State -instructionIntYankDup state = instructionYankDup state int - -instructionIntShove :: State -> State -instructionIntShove state = instructionShove state int - -instructionIntShoveDup :: State -> State -instructionIntShoveDup state = instructionShoveDup state int - -instructionIntIsEmpty :: State -> State -instructionIntIsEmpty state = instructionIsEmpty state int diff --git a/src/Instructions/LogicalInstructions.hs b/src/Instructions/LogicalInstructions.hs deleted file mode 100644 index a10e96f..0000000 --- a/src/Instructions/LogicalInstructions.hs +++ /dev/null @@ -1,79 +0,0 @@ -module Instructions.LogicalInstructions where - -import State -import Instructions.GenericInstructions - -instructionBoolFromInt :: State -> State -instructionBoolFromInt state@(State {_int = (i : is), _bool = bs}) = state {_int = is, _bool = (i /= 0) : bs} -instructionBoolFromInt state = state - -instructionBoolFromFloat :: State -> State -instructionBoolFromFloat state@(State {_float = (f : fs), _bool = bs}) = state {_float = fs, _bool = (f /= 0) : bs} -instructionBoolFromFloat state = state - -boolTemplate :: (Bool -> Bool -> Bool) -> State -> State -boolTemplate func state@(State {_bool = (b1 : b2 : bs)}) = state {_bool = func b1 b2 : bs} -boolTemplate _ state = state - -instructionBoolAnd :: State -> State -instructionBoolAnd = boolTemplate (&&) - -instructionBoolInvertFirstThenAnd :: State -> State -instructionBoolInvertFirstThenAnd state@(State {_bool = (b1 : bs)}) = boolTemplate (&&) state {_bool = not b1 : bs} -instructionBoolInvertFirstThenAnd state = state - -instructionBoolInvertSecondThenAnd :: State -> State -instructionBoolInvertSecondThenAnd state@(State {_bool = (b1 : b2 : bs)}) = boolTemplate (&&) state {_bool = b1 : not b2 : bs} -instructionBoolInvertSecondThenAnd state = state - -instructionBoolOr :: State -> State -instructionBoolOr = boolTemplate (||) - --- no builtin haskell xor moment -xor :: Bool -> Bool -> Bool -xor b1 b2 - | b1 && not b2 = True - | not b1 && b2 = True - | otherwise = False - -instructionBoolXor :: State -> State -instructionBoolXor = boolTemplate xor - -instructionBoolPop :: State -> State -instructionBoolPop state = instructionPop state bool - -instructionBoolDup :: State -> State -instructionBoolDup state = instructionDup state bool - -instructionBoolDupN :: State -> State -instructionBoolDupN state = instructionDupN state bool - -instructionBoolSwap :: State -> State -instructionBoolSwap state = instructionSwap state bool - -instructionBoolRot :: State -> State -instructionBoolRot state = instructionRot state bool - -instructionBoolFlush :: State -> State -instructionBoolFlush state = instructionFlush state bool - -instructionBoolEq :: State -> State -instructionBoolEq state = instructionEq state bool - -instructionBoolStackDepth :: State -> State -instructionBoolStackDepth state = instructionStackDepth state bool - -instructionBoolYank :: State -> State -instructionBoolYank state = instructionYank state bool - -instructionBoolYankDup :: State -> State -instructionBoolYankDup state = instructionYankDup state bool - -instructionBoolShove :: State -> State -instructionBoolShove state = instructionShove state bool - -instructionBoolShoveDup :: State -> State -instructionBoolShoveDup state = instructionShoveDup state bool - -instructionBoolIsEmpty :: State -> State -instructionBoolIsEmpty state = instructionIsEmpty state bool diff --git a/src/Instructions/StringInstructions.hs b/src/Instructions/StringInstructions.hs deleted file mode 100644 index 88b2344..0000000 --- a/src/Instructions/StringInstructions.hs +++ /dev/null @@ -1,231 +0,0 @@ -module Instructions.StringInstructions where - -import State -import Instructions.GenericInstructions -import Data.List.Split -import Control.Lens - --- shamelessly stolen from https://hackage.haskell.org/package/MissingH-1.6.0.1/docs/src/Data.String.Utils.html#strip -wschars :: String -wschars = " \t\r\n" - -strip :: String -> String -strip = lstrip . rstrip - -lstrip :: String -> String -lstrip s = case s of - [] -> [] - (x:xs) -> if x `elem` wschars - then lstrip xs - else s - --- this is a tad inefficient init -rstrip :: String -> String -rstrip = reverse . lstrip . reverse - -instructionStringConcat :: State -> State -instructionStringConcat state = instructionConcat state string - -instructionStringSwap :: State -> State -instructionStringSwap state = instructionSwap state string - -instructionStringInsertString :: State -> State -instructionStringInsertString state@(State{_string = s1 : s2 : ss, _int = i1 : is}) = state {_string = combineTupleList s2 (splitAt i1 s1) : ss, _int = is} -instructionStringInsertString state = state - -instructionStringFromFirstChar :: State -> State -instructionStringFromFirstChar state@(State {_string = (schar : _) : ss}) = state {_string = [schar] : ss} -instructionStringFromFirstChar state = state - -instructionStringFromLastChar :: State -> State -instructionStringFromLastChar state@(State {_string = s1 : ss}) = - if not $ null s1 - then state {_string = [last s1] : ss} - else state -instructionStringFromLastChar state = state - -instructionStringFromNthChar :: State -> State -instructionStringFromNthChar state@(State {_string = s1 : ss, _int = i1 : is}) = state{_string = [s1 !! absNum i1 s1] : ss, _int = is} -instructionStringFromNthChar state = state - -instructionStringIndexOfString :: State -> State -instructionStringIndexOfString state@(State {_string = s1 : s2 : ss, _int = is}) = state {_string = ss, _int = findSubA s1 s2 : is} -instructionStringIndexOfString state = state - -instructionStringContainsString :: State -> State -instructionStringContainsString state@(State {_string = s1 : s2 : ss, _bool = bs}) = state {_string = ss, _bool = (findSubA s1 s2 /= -1) : bs} -instructionStringContainsString state = state - --- pysh reverses this. Check this for propeller -instructionStringSplitOnString :: State -> State -instructionStringSplitOnString state@(State {_string = s1 : s2 : ss}) = state {_string = reverse $ splitOn s2 s1 <> ss} -instructionStringSplitOnString state = state - -instructionStringReplaceFirstString :: State -> State -instructionStringReplaceFirstString state@(State {_string = s1 : s2 : s3 : ss}) = state {_string = replace s1 s2 s3 (Just 1) : ss} -instructionStringReplaceFirstString state = state - -instructionStringReplaceNString :: State -> State -instructionStringReplaceNString state@(State {_string = s1 : s2 : s3 : ss, _int = i1 : is}) = state{_string = replace s1 s2 s3 (Just i1) : ss, _int = is} -instructionStringReplaceNString state = state - -instructionStringReplaceAllString :: State -> State -instructionStringReplaceAllString state@(State {_string = s1 : s2 : s3 : ss}) = state{_string = replace s1 s2 s3 Nothing : ss} -instructionStringReplaceAllString state = state - -instructionStringRemoveFirstString :: State -> State -instructionStringRemoveFirstString state@(State {_string = s1 : s2 : ss}) = state{_string = replace s1 s2 "" (Just 1) : ss} -instructionStringRemoveFirstString state = state - -instructionStringRemoveNString :: State -> State -instructionStringRemoveNString state@(State {_string = s1 : s2 : ss, _int = i1 : is}) = state{_string = replace s1 s2 "" (Just i1) : ss, _int = is} -instructionStringRemoveNString state = state - -instructionStringRemoveAllString :: State -> State -instructionStringRemoveAllString state@(State {_string = s1 : s2 : ss}) = state{_string = replace s1 s2 "" Nothing : ss} -instructionStringRemoveAllString state = state - -instructionStringOccurrencesOfString :: State -> State -instructionStringOccurrencesOfString state@(State {_string = s1 : s2 : ss, _int = is}) = state{_string = ss, _int = amtOccurences s1 s2 : is} -instructionStringOccurrencesOfString state = state - -instructionStringInsertChar :: State -> State -instructionStringInsertChar state@(State {_string = s1 : ss, _char = c1 : cs, _int = i1 : is}) = state{_string = combineTuple c1 (splitAt i1 s1) : ss, _char = cs, _int = is} -instructionStringInsertChar state = state - -instructionStringContainsChar :: State -> State -instructionStringContainsChar state = instructionVectorContains state char string - -instructionStringIndexOfChar :: State -> State -instructionStringIndexOfChar state = instructionVectorIndexOf state char string - -instructionStringSplitOnChar :: State -> State -instructionStringSplitOnChar state@(State {_string = s1 : ss, _char = c1 : cs}) = state {_string = reverse $ splitOn [c1] s1 <> ss, _char = cs} -instructionStringSplitOnChar state = state - -instructionStringReplaceFirstChar :: State -> State -instructionStringReplaceFirstChar state = instructionVectorReplaceFirst state char string - -instructionStringReplaceNChar :: State -> State -instructionStringReplaceNChar state@(State {_string = s1 : ss, _char = c1 : c2 : cs, _int = i1 : is}) = state{_string = replace s1 [c1] [c2] (Just i1) : ss, _char = cs, _int = is} -instructionStringReplaceNChar state = state - -instructionStringReplaceAllChar :: State -> State -instructionStringReplaceAllChar state = instructionVectorReplace state char string - -instructionStringRemoveFirstChar :: State -> State -instructionStringRemoveFirstChar state@(State {_string = s1 : ss, _char = c1 : cs}) = state {_string = replace s1 [c1] "" (Just 1) : ss, _char = cs} -instructionStringRemoveFirstChar state = state - -instructionStringRemoveNChar :: State -> State -instructionStringRemoveNChar state@(State {_string = s1 : ss, _char = c1 : cs, _int = i1 : is}) = state{_string = replace s1 [c1] "" (Just i1) : ss, _char = cs, _int = is} -instructionStringRemoveNChar state = state - -instructionStringRemoveAllChar :: State -> State -instructionStringRemoveAllChar state = instructionVectorRemove state char string - -instructionStringOccurrencesOfChar :: State -> State -instructionStringOccurrencesOfChar state = instructionVectorOccurrencesOf state char string - -instructionStringReverse :: State -> State -instructionStringReverse state = instructionReverse state string - -instructionStringHead :: State -> State -instructionStringHead state = instructionTakeN state string - -instructionStringTail :: State -> State -instructionStringTail state@(State {_string = s1 : ss, _int = i1 : is}) = state{_string = takeR (absNum i1 s1) s1 : ss, _int = is} -instructionStringTail state = state - -instructionStringAppendChar :: State -> State -instructionStringAppendChar state = instructionConj state char string - -instructionStringRest :: State -> State -instructionStringRest state = instructionRest state string - -instructionStringButLast :: State -> State -instructionStringButLast state = instructionButLast state string - -instructionStringDrop :: State -> State -instructionStringDrop state@(State {_string = s1 : ss, _int = i1 : is}) = state{_string = drop (absNum i1 s1) s1 : ss, _int = is} -instructionStringDrop state = state - -instructionStringButLastN :: State -> State -instructionStringButLastN state@(State {_string = s1 : ss, _int = i1 : is}) = state{_string = dropR (absNum i1 s1) s1 : ss, _int = is} -instructionStringButLastN state = state - -instructionStringLength :: State -> State -instructionStringLength state = instructionLength state string - -instructionStringMakeEmpty :: State -> State -instructionStringMakeEmpty state = instructionVectorMakeEmpty state string - -instructionStringIsEmptyString :: State -> State -instructionStringIsEmptyString state@(State {_string = s1 : ss, _bool = bs}) = state{_string = ss, _bool = null s1 : bs} -instructionStringIsEmptyString state = state - -instructionStringRemoveNth :: State -> State -instructionStringRemoveNth state@(State {_string = s1 : ss, _int = i1 : is}) = state{_string = deleteAt (absNum i1 s1) s1 : ss, _int = is} -instructionStringRemoveNth state = state - -instructionStringSetNth :: State -> State -instructionStringSetNth state = instructionVectorSetNth state char string - -instructionStringStripWhitespace :: State -> State -instructionStringStripWhitespace state@(State {_string = s1 : ss}) = state{_string = strip s1 : ss} -instructionStringStripWhitespace state = state - -instructionStringFromLens :: Show a => State -> Lens' State [a] -> State -instructionStringFromLens state@(State {_string = ss}) accessor = - case uncons (view accessor state) of - Nothing -> state - Just (x,_) -> state{_string = show x : ss} - -instructionStringFromBool :: State -> State -instructionStringFromBool state = instructionStringFromLens state bool - -instructionStringFromInt :: State -> State -instructionStringFromInt state = instructionStringFromLens state int - -instructionStringFromFloat :: State -> State -instructionStringFromFloat state = instructionStringFromLens state float - -instructionStringFromChar :: State -> State -instructionStringFromChar state@(State {_string = ss, _char = c1 : cs}) = state{_string = [c1] : ss, _char = cs} -instructionStringFromChar state = state - -instructionStringPop :: State -> State -instructionStringPop state = instructionPop state string - -instructionStringDup :: State -> State -instructionStringDup state = instructionDup state string - -instructionStringDupN :: State -> State -instructionStringDupN state = instructionDupN state string - -instructionStringRot :: State -> State -instructionStringRot state = instructionRot state string - -instructionStringFlush :: State -> State -instructionStringFlush state = instructionFlush state string - -instructionStringEq :: State -> State -instructionStringEq state = instructionEq state string - -instructionStringStackDepth :: State -> State -instructionStringStackDepth state = instructionStackDepth state string - -instructionStringYank :: State -> State -instructionStringYank state = instructionYank state string - -instructionStringYankDup :: State -> State -instructionStringYankDup state = instructionYankDup state string - -instructionStringIsEmpty :: State -> State -instructionStringIsEmpty state = instructionIsEmpty state string - -instructionStringShove :: State -> State -instructionStringShove state = instructionShove state string - -instructionStringShoveDup :: State -> State -instructionStringShoveDup state = instructionShoveDup state string diff --git a/src/Instructions/VectorCharInstructions.hs b/src/Instructions/VectorCharInstructions.hs deleted file mode 100644 index 7467163..0000000 --- a/src/Instructions/VectorCharInstructions.hs +++ /dev/null @@ -1,106 +0,0 @@ -module Instructions.VectorCharInstructions where - -import State -import Instructions.GenericInstructions - -instructionVectorCharConcat :: State -> State -instructionVectorCharConcat state = instructionConcat state vectorChar - -instructionVectorCharConj :: State -> State -instructionVectorCharConj state = instructionConj state char vectorChar - -instructionVectorCharTakeN :: State -> State -instructionVectorCharTakeN state = instructionTakeN state vectorChar - -instructionVectorCharSubVector :: State -> State -instructionVectorCharSubVector state = instructionSubVector state vectorChar - -instructionVectorCharFirst :: State -> State -instructionVectorCharFirst state = instructionVectorFirst state char vectorChar - -instructionVectorCharLast :: State -> State -instructionVectorCharLast state = instructionVectorLast state char vectorChar - -instructionVectorCharNth :: State -> State -instructionVectorCharNth state = instructionVectorNth state char vectorChar - -instructionVectorCharRest :: State -> State -instructionVectorCharRest state = instructionRest state vectorChar - -instructionVectorCharButLast :: State -> State -instructionVectorCharButLast state = instructionButLast state vectorChar - -instructionVectorCharLength :: State -> State -instructionVectorCharLength state = instructionLength state vectorChar - -instructionVectorCharReverse :: State -> State -instructionVectorCharReverse state = instructionReverse state vectorChar - -instructionVectorCharPushAll :: State -> State -instructionVectorCharPushAll state = instructionPushAll state char vectorChar - -instructionVectorCharMakeEmpty :: State -> State -instructionVectorCharMakeEmpty state = instructionVectorMakeEmpty state vectorChar - -instructionVectorCharIsEmpty :: State -> State -instructionVectorCharIsEmpty state = instructionVectorIsEmpty state vectorChar - -instructionVectorCharIndexOf :: State -> State -instructionVectorCharIndexOf state = instructionVectorIndexOf state char vectorChar - -instructionVectorCharOccurrencesOf :: State -> State -instructionVectorCharOccurrencesOf state = instructionVectorOccurrencesOf state char vectorChar - -instructionVectorCharSetNth :: State -> State -instructionVectorCharSetNth state = instructionVectorSetNth state char vectorChar - -instructionVectorCharReplace :: State -> State -instructionVectorCharReplace state = instructionVectorReplace state char vectorChar - -instructionVectorCharReplaceFirst :: State -> State -instructionVectorCharReplaceFirst state = instructionVectorReplaceFirst state char vectorChar - -instructionVectorCharRemove :: State -> State -instructionVectorCharRemove state = instructionVectorRemove state char vectorChar - -instructionVectorCharIterate :: State -> State -instructionVectorCharIterate state = instructionVectorIterate state char vectorChar GeneVectorChar instructionVectorCharIterate "instructionVectorCharIterate" - -instructionVectorCharPop :: State -> State -instructionVectorCharPop state = instructionPop state vectorChar - -instructionVectorCharDup :: State -> State -instructionVectorCharDup state = instructionDup state vectorChar - -instructionVectorCharDupN :: State -> State -instructionVectorCharDupN state = instructionDupN state vectorChar - -instructionVectorCharSwap :: State -> State -instructionVectorCharSwap state = instructionSwap state vectorChar - -instructionVectorCharRot :: State -> State -instructionVectorCharRot state = instructionRot state vectorChar - -instructionVectorCharFlush :: State -> State -instructionVectorCharFlush state = instructionFlush state vectorChar - -instructionVectorCharEq :: State -> State -instructionVectorCharEq state = instructionEq state vectorChar - -instructionVectorCharStackDepth :: State -> State -instructionVectorCharStackDepth state = instructionStackDepth state vectorChar - -instructionVectorCharYank :: State -> State -instructionVectorCharYank state = instructionYank state vectorChar - -instructionVectorCharYankDup :: State -> State -instructionVectorCharYankDup state = instructionYankDup state vectorChar - -instructionVectorCharStackIsEmpty :: State -> State -instructionVectorCharStackIsEmpty state = instructionIsEmpty state vectorChar - -instructionVectorCharShove :: State -> State -instructionVectorCharShove state = instructionShove state vectorChar - -instructionVectorCharShoveDup :: State -> State -instructionVectorCharShoveDup state = instructionShoveDup state vectorChar diff --git a/src/Instructions/VectorFloatInstructions.hs b/src/Instructions/VectorFloatInstructions.hs deleted file mode 100644 index 18dabc9..0000000 --- a/src/Instructions/VectorFloatInstructions.hs +++ /dev/null @@ -1,106 +0,0 @@ -module Instructions.VectorFloatInstructions where - -import State -import Instructions.GenericInstructions - -instructionVectorFloatConcat :: State -> State -instructionVectorFloatConcat state = instructionConcat state vectorFloat - -instructionVectorFloatConj :: State -> State -instructionVectorFloatConj state = instructionConj state float vectorFloat - -instructionVectorFloatTakeN :: State -> State -instructionVectorFloatTakeN state = instructionTakeN state vectorFloat - -instructionVectorFloatSubVector :: State -> State -instructionVectorFloatSubVector state = instructionSubVector state vectorFloat - -instructionVectorFloatFirst :: State -> State -instructionVectorFloatFirst state = instructionVectorFirst state float vectorFloat - -instructionVectorFloatLast :: State -> State -instructionVectorFloatLast state = instructionVectorLast state float vectorFloat - -instructionVectorFloatNth :: State -> State -instructionVectorFloatNth state = instructionVectorNth state float vectorFloat - -instructionVectorFloatRest :: State -> State -instructionVectorFloatRest state = instructionRest state vectorFloat - -instructionVectorFloatButLast :: State -> State -instructionVectorFloatButLast state = instructionButLast state vectorFloat - -instructionVectorFloatLength :: State -> State -instructionVectorFloatLength state = instructionLength state vectorFloat - -instructionVectorFloatReverse :: State -> State -instructionVectorFloatReverse state = instructionReverse state vectorFloat - -instructionVectorFloatPushAll :: State -> State -instructionVectorFloatPushAll state = instructionPushAll state float vectorFloat - -instructionVectorFloatMakeEmpty :: State -> State -instructionVectorFloatMakeEmpty state = instructionVectorMakeEmpty state vectorFloat - -instructionVectorFloatIsEmpty :: State -> State -instructionVectorFloatIsEmpty state = instructionVectorIsEmpty state vectorFloat - -instructionVectorFloatIndexOf :: State -> State -instructionVectorFloatIndexOf state = instructionVectorIndexOf state float vectorFloat - -instructionVectorFloatOccurrencesOf :: State -> State -instructionVectorFloatOccurrencesOf state = instructionVectorOccurrencesOf state float vectorFloat - -instructionVectorFloatSetNth :: State -> State -instructionVectorFloatSetNth state = instructionVectorSetNth state float vectorFloat - -instructionVectorFloatReplace :: State -> State -instructionVectorFloatReplace state = instructionVectorReplace state float vectorFloat - -instructionVectorFloatReplaceFirst :: State -> State -instructionVectorFloatReplaceFirst state = instructionVectorReplaceFirst state float vectorFloat - -instructionVectorFloatRemove :: State -> State -instructionVectorFloatRemove state = instructionVectorRemove state float vectorFloat - -instructionVectorFloatIterate :: State -> State -instructionVectorFloatIterate state = instructionVectorIterate state float vectorFloat GeneVectorFloat instructionVectorFloatIterate "instructionVectorFloatIterate" - -instructionVectorFloatPop :: State -> State -instructionVectorFloatPop state = instructionPop state vectorFloat - -instructionVectorFloatDup :: State -> State -instructionVectorFloatDup state = instructionDup state vectorFloat - -instructionVectorFloatDupN :: State -> State -instructionVectorFloatDupN state = instructionDupN state vectorFloat - -instructionVectorFloatSwap :: State -> State -instructionVectorFloatSwap state = instructionSwap state vectorFloat - -instructionVectorFloatRot :: State -> State -instructionVectorFloatRot state = instructionRot state vectorFloat - -instructionVectorFloatFlush :: State -> State -instructionVectorFloatFlush state = instructionFlush state vectorFloat - -instructionVectorFloatEq :: State -> State -instructionVectorFloatEq state = instructionEq state vectorFloat - -instructionVectorFloatStackDepth :: State -> State -instructionVectorFloatStackDepth state = instructionStackDepth state vectorFloat - -instructionVectorFloatYank :: State -> State -instructionVectorFloatYank state = instructionYank state vectorFloat - -instructionVectorFloatYankDup :: State -> State -instructionVectorFloatYankDup state = instructionYankDup state vectorFloat - -instructionVectorFloatStackIsEmpty :: State -> State -instructionVectorFloatStackIsEmpty state = instructionIsEmpty state vectorFloat - -instructionVectorFloatShove :: State -> State -instructionVectorFloatShove state = instructionShove state vectorFloat - -instructionVectorFloatShoveDup :: State -> State -instructionVectorFloatShoveDup state = instructionShoveDup state vectorFloat diff --git a/src/Instructions/VectorIntInstructions.hs b/src/Instructions/VectorIntInstructions.hs deleted file mode 100644 index bb135ff..0000000 --- a/src/Instructions/VectorIntInstructions.hs +++ /dev/null @@ -1,106 +0,0 @@ -module Instructions.VectorIntInstructions where - -import Instructions.GenericInstructions -import State - -instructionVectorIntConcat :: State -> State -instructionVectorIntConcat state = instructionConcat state vectorInt - -instructionVectorIntConj :: State -> State -instructionVectorIntConj state = instructionConj state int vectorInt - -instructionVectorIntTakeN :: State -> State -instructionVectorIntTakeN state = instructionTakeN state vectorInt - -instructionVectorIntSubVector :: State -> State -instructionVectorIntSubVector state = instructionSubVector state vectorInt - -instructionVectorIntFirst :: State -> State -instructionVectorIntFirst state = instructionVectorFirst state int vectorInt - -instructionVectorIntLast :: State -> State -instructionVectorIntLast state = instructionVectorLast state int vectorInt - -instructionVectorIntNth :: State -> State -instructionVectorIntNth state = instructionVectorNth state int vectorInt - -instructionVectorIntRest :: State -> State -instructionVectorIntRest state = instructionRest state vectorInt - -instructionVectorIntButLast :: State -> State -instructionVectorIntButLast state = instructionButLast state vectorInt - -instructionVectorIntLength :: State -> State -instructionVectorIntLength state = instructionLength state vectorInt - -instructionVectorIntReverse :: State -> State -instructionVectorIntReverse state = instructionReverse state vectorInt - -instructionVectorIntPushAll :: State -> State -instructionVectorIntPushAll state = instructionPushAll state int vectorInt - -instructionVectorIntMakeEmpty :: State -> State -instructionVectorIntMakeEmpty state = instructionVectorMakeEmpty state vectorInt - -instructionVectorIntIsEmpty :: State -> State -instructionVectorIntIsEmpty state = instructionVectorIsEmpty state vectorInt - -instructionVectorIntIndexOf :: State -> State -instructionVectorIntIndexOf state = instructionVectorIndexOf state int vectorInt - -instructionVectorIntOccurrencesOf :: State -> State -instructionVectorIntOccurrencesOf state = instructionVectorOccurrencesOf state int vectorInt - -instructionVectorIntSetNth :: State -> State -instructionVectorIntSetNth state = instructionVectorSetNth state int vectorInt - -instructionVectorIntReplace :: State -> State -instructionVectorIntReplace state = instructionVectorReplace state int vectorInt - -instructionVectorIntReplaceFirst :: State -> State -instructionVectorIntReplaceFirst state = instructionVectorReplaceFirst state int vectorInt - -instructionVectorIntRemove :: State -> State -instructionVectorIntRemove state = instructionVectorRemove state int vectorInt - -instructionVectorIntIterate :: State -> State -instructionVectorIntIterate state = instructionVectorIterate state int vectorInt GeneVectorInt instructionVectorIntIterate "instructionVectorIntIterate" - -instructionVectorIntPop :: State -> State -instructionVectorIntPop state = instructionPop state vectorChar - -instructionVectorIntDup :: State -> State -instructionVectorIntDup state = instructionDup state vectorChar - -instructionVectorIntDupN :: State -> State -instructionVectorIntDupN state = instructionDupN state vectorChar - -instructionVectorIntSwap :: State -> State -instructionVectorIntSwap state = instructionSwap state vectorChar - -instructionVectorIntRot :: State -> State -instructionVectorIntRot state = instructionRot state vectorChar - -instructionVectorIntFlush :: State -> State -instructionVectorIntFlush state = instructionFlush state vectorChar - -instructionVectorIntEq :: State -> State -instructionVectorIntEq state = instructionEq state vectorChar - -instructionVectorIntStackDepth :: State -> State -instructionVectorIntStackDepth state = instructionStackDepth state vectorChar - -instructionVectorIntYank :: State -> State -instructionVectorIntYank state = instructionYank state vectorChar - -instructionVectorIntYankDup :: State -> State -instructionVectorIntYankDup state = instructionYankDup state vectorChar - -instructionVectorIntStackIsEmpty :: State -> State -instructionVectorIntStackIsEmpty state = instructionIsEmpty state vectorChar - -instructionVectorIntShove :: State -> State -instructionVectorIntShove state = instructionShove state vectorChar - -instructionVectorIntShoveDup :: State -> State -instructionVectorIntShoveDup state = instructionShoveDup state vectorChar diff --git a/src/Instructions/VectorLogicalInstructions.hs b/src/Instructions/VectorLogicalInstructions.hs deleted file mode 100644 index 35d7add..0000000 --- a/src/Instructions/VectorLogicalInstructions.hs +++ /dev/null @@ -1,106 +0,0 @@ -module Instructions.VectorLogicalInstructions where - -import State -import Instructions.GenericInstructions - -instructionVectorBoolConcat :: State -> State -instructionVectorBoolConcat state = instructionConcat state vectorBool - -instructionVectorBoolConj :: State -> State -instructionVectorBoolConj state = instructionConj state bool vectorBool - -instructionVectorBoolTakeN :: State -> State -instructionVectorBoolTakeN state = instructionTakeN state vectorBool - -instructionVectorBoolSubVector :: State -> State -instructionVectorBoolSubVector state = instructionSubVector state vectorBool - -instructionVectorBoolFirst :: State -> State -instructionVectorBoolFirst state = instructionVectorFirst state bool vectorBool - -instructionVectorBoolLast :: State -> State -instructionVectorBoolLast state = instructionVectorLast state bool vectorBool - -instructionVectorBoolNth :: State -> State -instructionVectorBoolNth state = instructionVectorNth state bool vectorBool - -instructionVectorBoolRest :: State -> State -instructionVectorBoolRest state = instructionRest state vectorBool - -instructionVectorBoolButLast :: State -> State -instructionVectorBoolButLast state = instructionButLast state vectorBool - -instructionVectorBoolLength :: State -> State -instructionVectorBoolLength state = instructionLength state vectorBool - -instructionVectorBoolReverse :: State -> State -instructionVectorBoolReverse state = instructionReverse state vectorBool - -instructionVectorBoolPushAll :: State -> State -instructionVectorBoolPushAll state = instructionPushAll state bool vectorBool - -instructionVectorBoolMakeEmpty :: State -> State -instructionVectorBoolMakeEmpty state = instructionVectorMakeEmpty state vectorBool - -instructionVectorBoolIsEmpty :: State -> State -instructionVectorBoolIsEmpty state = instructionVectorIsEmpty state vectorBool - -instructionVectorBoolIndexOf :: State -> State -instructionVectorBoolIndexOf state = instructionVectorIndexOf state bool vectorBool - -instructionVectorBoolOccurrencesOf :: State -> State -instructionVectorBoolOccurrencesOf state = instructionVectorOccurrencesOf state bool vectorBool - -instructionVectorBoolSetNth :: State -> State -instructionVectorBoolSetNth state = instructionVectorSetNth state bool vectorBool - -instructionVectorBoolReplace :: State -> State -instructionVectorBoolReplace state = instructionVectorReplace state bool vectorBool - -instructionVectorBoolReplaceFirst :: State -> State -instructionVectorBoolReplaceFirst state = instructionVectorReplaceFirst state bool vectorBool - -instructionVectorBoolRemove :: State -> State -instructionVectorBoolRemove state = instructionVectorRemove state bool vectorBool - -instructionVectorBoolIterate :: State -> State -instructionVectorBoolIterate state = instructionVectorIterate state bool vectorBool GeneVectorBool instructionVectorBoolIterate "instructionVectorBoolIterate" - -instructionVectorBoolPop :: State -> State -instructionVectorBoolPop state = instructionPop state vectorBool - -instructionVectorBoolDup :: State -> State -instructionVectorBoolDup state = instructionDup state vectorBool - -instructionVectorBoolDupN :: State -> State -instructionVectorBoolDupN state = instructionDupN state vectorBool - -instructionVectorBoolSwap :: State -> State -instructionVectorBoolSwap state = instructionSwap state vectorBool - -instructionVectorBoolRot :: State -> State -instructionVectorBoolRot state = instructionRot state vectorBool - -instructionVectorBoolFlush :: State -> State -instructionVectorBoolFlush state = instructionFlush state vectorBool - -instructionVectorBoolEq :: State -> State -instructionVectorBoolEq state = instructionEq state vectorBool - -instructionVectorBoolStackDepth :: State -> State -instructionVectorBoolStackDepth state = instructionStackDepth state vectorBool - -instructionVectorBoolYank :: State -> State -instructionVectorBoolYank state = instructionYank state vectorBool - -instructionVectorBoolYankDup :: State -> State -instructionVectorBoolYankDup state = instructionYankDup state vectorBool - -instructionVectorBoolStackIsEmpty :: State -> State -instructionVectorBoolStackIsEmpty state = instructionIsEmpty state vectorBool - -instructionVectorBoolShove :: State -> State -instructionVectorBoolShove state = instructionShove state vectorBool - -instructionVectorBoolShoveDup :: State -> State -instructionVectorBoolShoveDup state = instructionShoveDup state vectorBool diff --git a/src/Instructions/VectorStringInstructions.hs b/src/Instructions/VectorStringInstructions.hs deleted file mode 100644 index def670a..0000000 --- a/src/Instructions/VectorStringInstructions.hs +++ /dev/null @@ -1,106 +0,0 @@ -module Instructions.VectorStringInstructions where - -import State -import Instructions.GenericInstructions - -instructionVectorStringConcat :: State -> State -instructionVectorStringConcat state = instructionConcat state vectorString - -instructionVectorStringConj :: State -> State -instructionVectorStringConj state = instructionConj state string vectorString - -instructionVectorStringTakeN :: State -> State -instructionVectorStringTakeN state = instructionTakeN state vectorString - -instructionVectorStringSubVector :: State -> State -instructionVectorStringSubVector state = instructionSubVector state vectorString - -instructionVectorStringFirst :: State -> State -instructionVectorStringFirst state = instructionVectorFirst state string vectorString - -instructionVectorStringLast :: State -> State -instructionVectorStringLast state = instructionVectorLast state string vectorString - -instructionVectorStringNth :: State -> State -instructionVectorStringNth state = instructionVectorNth state string vectorString - -instructionVectorStringRest :: State -> State -instructionVectorStringRest state = instructionRest state vectorString - -instructionVectorStringButLast :: State -> State -instructionVectorStringButLast state = instructionButLast state vectorString - -instructionVectorStringLength :: State -> State -instructionVectorStringLength state = instructionLength state vectorString - -instructionVectorStringReverse :: State -> State -instructionVectorStringReverse state = instructionReverse state vectorString - -instructionVectorStringPushAll :: State -> State -instructionVectorStringPushAll state = instructionPushAll state string vectorString - -instructionVectorStringMakeEmpty :: State -> State -instructionVectorStringMakeEmpty state = instructionVectorMakeEmpty state vectorString - -instructionVectorStringIsEmpty :: State -> State -instructionVectorStringIsEmpty state = instructionVectorIsEmpty state vectorString - -instructionVectorStringIndexOf :: State -> State -instructionVectorStringIndexOf state = instructionVectorIndexOf state string vectorString - -instructionVectorStringOccurrencesOf :: State -> State -instructionVectorStringOccurrencesOf state = instructionVectorOccurrencesOf state string vectorString - -instructionVectorStringSetNth :: State -> State -instructionVectorStringSetNth state = instructionVectorSetNth state string vectorString - -instructionVectorStringReplace :: State -> State -instructionVectorStringReplace state = instructionVectorReplace state string vectorString - -instructionVectorStringReplaceFirst :: State -> State -instructionVectorStringReplaceFirst state = instructionVectorReplaceFirst state string vectorString - -instructionVectorStringRemove :: State -> State -instructionVectorStringRemove state = instructionVectorRemove state string vectorString - -instructionVectorStringIterate :: State -> State -instructionVectorStringIterate state = instructionVectorIterate state string vectorString GeneVectorString instructionVectorStringIterate "instructionVectorStringIterate" - -instructionVectorStringPop :: State -> State -instructionVectorStringPop state = instructionPop state vectorString - -instructionVectorStringDup :: State -> State -instructionVectorStringDup state = instructionDup state vectorString - -instructionVectorStringDupN :: State -> State -instructionVectorStringDupN state = instructionDupN state vectorString - -instructionVectorStringSwap :: State -> State -instructionVectorStringSwap state = instructionSwap state vectorString - -instructionVectorStringRot :: State -> State -instructionVectorStringRot state = instructionRot state vectorString - -instructionVectorStringFlush :: State -> State -instructionVectorStringFlush state = instructionFlush state vectorString - -instructionVectorStringEq :: State -> State -instructionVectorStringEq state = instructionEq state vectorString - -instructionVectorStringStackDepth :: State -> State -instructionVectorStringStackDepth state = instructionStackDepth state vectorString - -instructionVectorStringYank :: State -> State -instructionVectorStringYank state = instructionYank state vectorString - -instructionVectorStringYankDup :: State -> State -instructionVectorStringYankDup state = instructionYankDup state vectorString - -instructionVectorStringStackIsEmpty :: State -> State -instructionVectorStringStackIsEmpty state = instructionIsEmpty state vectorString - -instructionVectorStringShove :: State -> State -instructionVectorStringShove state = instructionShove state vectorString - -instructionVectorStringShoveDup :: State -> State -instructionVectorStringShoveDup state = instructionShoveDup state vectorString diff --git a/src/Push.hs b/src/Push.hs deleted file mode 100644 index 44c6bc8..0000000 --- a/src/Push.hs +++ /dev/null @@ -1,86 +0,0 @@ -module Push where - -import Control.Lens -import Data.Map qualified as Map -import State - --- import Debug.Trace (trace, traceStack) - --- Each core func should be: (State -> State -> State) --- but each core function can use abstract helper functions. --- That is more efficient than checking length. --- Everntually, this can be part of the apply func to state helpers, --- which should take the number and type of parameter they have. - --- This is one of the push genome functions itself, not infrastructure. --- Optionally, split this off into independent functions -instructionParameterLoad :: State -> State -instructionParameterLoad state@(State {_parameter = (p : _)}) = case p of - (GeneInt val) -> state & int .~ val : view int state - (GeneFloat val) -> state & float .~ val : view float state - (GeneBool val) -> state & bool .~ val : view bool state - (GeneString val) -> state & string .~ val : view string state - (GeneChar val) -> state & char .~ val : view char state - (GeneVectorInt val) -> state & vectorInt .~ val : view vectorInt state - (GeneVectorFloat val) -> state & vectorFloat .~ val : view vectorFloat state - (GeneVectorBool val) -> state & vectorBool .~ val : view vectorBool state - (GeneVectorString val) -> state & vectorString .~ val : view vectorString state - (GeneVectorChar val) -> state & vectorChar .~ val : view vectorChar state - (StateFunc _) -> undefined - (PlaceInput _) -> undefined - Close -> undefined - (Block xs) -> state & exec .~ xs <> view exec state -instructionParameterLoad state = state - --- Loads a genome into the exec stack -loadProgram :: [Gene] -> State -> State -loadProgram newstack state = state & exec .~ newstack - --- Takes a Push state, and generates the next push state via: --- If the first item on the EXEC stack is a single instruction --- then pop it and execute it. --- Else if the first item on the EXEC stack is a literal --- then pop it and push it onto the appropriate stack. --- Else (the first item must be a list) pop it and push all of the --- items that it contains back onto the EXEC stack individually, --- in reverse order (so that the item that was first in the list --- ends up on top). --- The empty-stack safety of interpretExec on empty stacks depends on the functions it calls. -interpretExec :: State -> State -interpretExec state@(State {_exec = e : es}) = - case e of - (GeneInt val) -> interpretExec (state & exec .~ es & int .~ val : view int state) - (GeneFloat val) -> interpretExec (state & exec .~ es & float .~ val : view float state) - (GeneBool val) -> interpretExec (state & exec .~ es & bool .~ val : view bool state) - (GeneString val) -> interpretExec (state & exec .~ es & string .~ val : view string state) - (GeneChar val) -> interpretExec (state & exec .~ es & char .~ val : view char state) - (GeneVectorInt val) -> interpretExec (state & exec .~ es & vectorInt .~ val : view vectorInt state) - (GeneVectorFloat val) -> interpretExec (state & exec .~ es & vectorFloat .~ val : view vectorFloat state) - (GeneVectorBool val) -> interpretExec (state & exec .~ es & vectorBool .~ val : view vectorBool state) - (GeneVectorString val) -> interpretExec (state & exec .~ es & vectorString .~ val : view vectorString state) - (GeneVectorChar val) -> interpretExec (state & exec .~ es & vectorChar .~ val : view vectorChar state) - (StateFunc (func, _)) -> interpretExec $ func state {_exec = es} - (Block block) -> interpretExec (state {_exec = block ++ es}) - (PlaceInput val) -> interpretExec (state {_exec = (view input state Map.! val) : es}) - Close -> undefined -- This should be removed later. Will be converted to Blocks in the Plushy -> Exec stack process -interpretExec state = state - --- interpretOneStep :: State -> State --- interpretOneStep state@(State {_exec = e : es}) = --- case e of --- (GeneInt val) -> state & exec .~ es & int .~ val : view int state --- (GeneFloat val) -> state & exec .~ es & float .~ val : view float state --- (GeneBool val) -> state & exec .~ es & bool .~ val : view bool state --- (GeneString val) -> state & exec .~ es & string .~ val : view string state --- (GeneChar val) -> state & exec .~ es & char .~ val : view char state --- (GeneVectorInt val) -> state & exec .~ es & vectorInt .~ val : view vectorInt state --- (GeneVectorFloat val) -> state & exec .~ es & vectorFloat .~ val : view vectorFloat state --- (GeneVectorBool val) -> state & exec .~ es & vectorBool .~ val : view vectorBool state --- (GeneVectorString val) -> state & exec .~ es & vectorString .~ val : view vectorString state --- (GeneVectorChar val) -> state & exec .~ es & vectorChar .~ val : view vectorChar state --- (StateFunc (func, _)) -> func state {_exec = es} --- (Block block) -> (state {_exec = block ++ es}) --- (PlaceInput val) -> (state {_exec = (view input state Map.! val) : es}) --- Close -> undefined --- interpretOneStep state = state --- Need to make interpretExec strict, right? diff --git a/src/PushTests.hs b/src/PushTests.hs deleted file mode 100644 index 571b27f..0000000 --- a/src/PushTests.hs +++ /dev/null @@ -1,10 +0,0 @@ -module PushTests - ( module PushTests.GenericTests, - module PushTests.IntTests, - module PushTests.UtilTests, - ) -where - -import PushTests.GenericTests -import PushTests.IntTests -import PushTests.UtilTests diff --git a/src/PushTests/GenericTests.hs b/src/PushTests/GenericTests.hs deleted file mode 100644 index 5a8dded..0000000 --- a/src/PushTests/GenericTests.hs +++ /dev/null @@ -1,129 +0,0 @@ -module PushTests.GenericTests where - -import State -import Control.Lens -import Debug.Trace -import Test.QuickCheck -import Instructions.GenericInstructions - --- The naming scheme: --- the letters at the beginning represent what kind of transformation (the word I'm using for a basic function) to the states is happening --- for example: the function aaa1Test relays this arg takes a transformation of two as and turns them into one a --- the numbers represent how many different stacks are used in the function. --- for example: the aaa1Test relays that it takes one stack as input. These stacks are passed in as Lens - --- We may be able to get rid of Lens entirely and use haskell's integrated accessors of type State -> [a] --- You can see what I'm talking about if you go into ghci and type: `:info _int` for example - -aaa1Test :: (Show a, Eq a) => Lens' State [a] -> (State -> State) -> (a -> a -> a) -> State -> Property -aaa1Test accessor instruction transformation state = - case (uncons (view accessor state), uncons (view accessor $ instruction state)) of - (Just (origx1, origx2 : _), Just (modx1, _)) -> transformation origx2 origx1 === modx1 .&&. length (view accessor state) === length (view accessor $ instruction state) + 1 - _ -> state === instruction state - -aa1Test :: (Show a, Eq a) => Lens' State [a] -> (State -> State) -> (a -> a) -> State -> Property -aa1Test accessor instruction transformation state = - case (uncons (view accessor state), uncons (view accessor $ instruction state)) of - (Just (origx1, _), Just (modx1, _)) -> transformation origx1 === modx1 .&&. length (view accessor state) === length (view accessor $ instruction state) - _ -> state === instruction state - -ab1Test :: (Show b, Eq b) => Lens' State [a] -> Lens' State [b] -> (State -> State) -> (a -> b) -> State -> Property -ab1Test accessorFrom accessorTo instruction transformation state = - case (uncons (view accessorTo $ instruction state), uncons (view accessorFrom state)) of - (Just (t1, _), Just (f1, _)) -> - t1 === transformation f1 .&&. - length (view accessorTo $ instruction state) === length (view accessorTo state) + 1 .&&. - length (view accessorFrom $ instruction state) === length (view accessorFrom state) - 1 - _ -> state === instruction state - -aab2Test :: (Show b, Eq b) => Lens' State [a] -> Lens' State [b] -> (State -> State) -> (a -> a -> b) -> State -> Property -aab2Test accessorFrom accessorTo instruction transformation state = - case (uncons (view accessorTo $ instruction state), uncons (view accessorFrom state)) of - (Just (t1, _), Just (f1, f2 : _)) -> - t1 === transformation f1 f2 .&&. - length (view accessorTo $ instruction state) == length (view accessorTo state) + 1 .&&. - length (view accessorFrom $ instruction state) == length (view accessorFrom state) - 2 - _ -> state === instruction state - -popTest :: (Show a) => Lens' State [a] -> (State -> State) -> State -> Property -popTest accessor instruction state = - if null $ view accessor state - then state === instruction state - else length (view accessor $ instruction state) === length (view accessor state) - 1 - -dupTest :: (Eq a, Show a) => Lens' State [a] -> (State -> State) -> State -> Property -dupTest accessor instruction state = - case uncons (view accessor state) of - Just (origx1, _) -> - case uncons (view accessor $ instruction state) of - Just (modx1, modx2 : _) -> - origx1 === modx1 .&&. origx1 === modx2 .&&. length (view accessor $ instruction state) === length (view accessor state) + 1 - _ -> state === instruction state - _ -> state === instruction state - --- How to test the int stack in particular? -dupTestN :: (Eq a, Show a) => Lens' State [a] -> (State -> State) -> State -> Property -dupTestN accessor instruction state = - case uncons (view int state) of - Just (i1, is) -> - let amt = max i1 0 in - case uncons (view accessor state{_int = is}) of - Just (origx1, _) -> - conjoin (map (origx1 ===) (take amt (view accessor $ instruction state))) .&&. - length (view accessor $ instruction state) === (length (view accessor state{_int = is}) + amt - 1) - _ -> state === instruction state - _ -> state === instruction state - -swapTest :: (Show a, Eq a) => Lens' State [a] -> (State -> State) -> State -> Property -swapTest accessor instruction state = - case (uncons (view accessor state), uncons (view accessor $ instruction state)) of - (Just (origx1, origx2 : _), Just (modx1, modx2 : _)) -> origx1 === modx2 .&&. origx2 === modx1 - _ -> state === instruction state - -rotTest :: (Show a, Eq a) => Lens' State [a] -> (State -> State) -> State -> Property -rotTest accessor instruction state = - case (uncons (view accessor state), uncons (view accessor $ instruction state)) of - (Just (origx1, origx2 : origx3 : _), Just (modx1, modx2 : modx3 : _)) -> (origx1, origx2, origx3) === (modx2, modx3, modx1) - _ -> state === instruction state - -flushTest :: (Show a, Eq a) => Lens' State [a] -> (State -> State) -> State -> Property -flushTest accessor instruction state = - property $ null $ view accessor $ instruction state - -stackDepthTest :: (Show a) => Lens' State [a] -> (State -> State) -> State -> Property -stackDepthTest accessor instruction state = - case uncons (view int $ instruction state) of - Just (x1, _) -> x1 === length (view accessor state) - _ -> state === instruction state - -yankTest :: forall a. (Show a, Eq a) => Lens' State [a] -> (State -> State) -> State -> Property -yankTest accessor instruction state@(State {_int = i1 : is}) = - let - myIndex :: Int - myIndex = max 0 (min i1 (length (view accessor state{_int = is}) - 1)) - item :: a - item = view accessor state{_int = is} !! myIndex - in - case (uncons (view accessor $ instruction state), uncons is) of - (Just (x1, _), Just (_, _)) -> x1 === item - _ -> state === instruction state - -- .&&. -- unsure how to get this functional - -- length (view accessor state{_int = is}) === length (view accessor $ instruction state) -yankTest _ instruction state = state === instruction state - --- Might just make this a unit test --- Come back to this later --- yankDupTest :: forall a. (Show a, Eq a) => Lens' State [a] -> (State -> State) -> State -> Property --- yankDupTest accessor instruction state@(State {_int = i1 : is}) = --- let --- myIndex :: Int --- myIndex = max 0 (min i1 (length (view accessor state{_int = is}) - 1)) --- item :: a --- item = view accessor state{_int = is} !! myIndex --- in --- case (uncons (view accessor $ instruction state), uncons is) of --- (Just (x1, xs), Just (_, _)) -> x1 === item .&&. (x1 : xs) !! === item --- _ -> state === instruction state --- yankDupTest _ instruction state = state === instruction state - --- shoveTest diff --git a/src/PushTests/IntTests.hs b/src/PushTests/IntTests.hs deleted file mode 100644 index 07432f1..0000000 --- a/src/PushTests/IntTests.hs +++ /dev/null @@ -1,84 +0,0 @@ -module PushTests.IntTests where - -import State -import Instructions.IntInstructions -import PushTests.GenericTests --- import Control.Lens hiding (uncons) -import Test.QuickCheck - -prop_IntAdd :: State -> Property -prop_IntAdd = aaa1Test int instructionIntAdd (+) - -prop_IntSub :: State -> Property -prop_IntSub = aaa1Test int instructionIntSub (-) - -prop_IntMul :: State -> Property -prop_IntMul = aaa1Test int instructionIntMul (*) - -prop_IntDiv :: State -> Property -prop_IntDiv state@(State {_int = 0 : _}) = state === instructionIntDiv state -prop_IntDiv state = aaa1Test int instructionIntDiv div state - -prop_IntMod :: State -> Property -prop_IntMod state@(State {_int = 0 : _}) = state === instructionIntMod state -prop_IntMod state = aaa1Test int instructionIntMod mod state - -prop_IntFromFloat :: State -> Property -prop_IntFromFloat = ab1Test float int instructionIntFromFloat floor - -prop_IntFromProperty :: State -> Property -prop_IntFromProperty = ab1Test bool int instructionIntFromBool (\x -> if x then 1 else 0) - -prop_IntMin :: State -> Property -prop_IntMin = aaa1Test int instructionIntMin min - -prop_IntMax :: State -> Property -prop_IntMax = aaa1Test int instructionIntMax max - -prop_IntInc :: State -> Property -prop_IntInc = aa1Test int instructionIntInc (+1) - -prop_IntDec :: State -> Property -prop_IntDec = aa1Test int instructionIntDec (\x -> x - 1) - -prop_IntLT :: State -> Property -prop_IntLT = aab2Test int bool instructionIntLT (<) - -prop_IntGT :: State -> Property -prop_IntGT = aab2Test int bool instructionIntGT (>) - -prop_IntLTE :: State -> Property -prop_IntLTE = aab2Test int bool instructionIntLTE (<=) - -prop_IntGTE :: State -> Property -prop_IntGTE = aab2Test int bool instructionIntGTE (>=) - -prop_IntDup :: State -> Property -prop_IntDup = dupTest int instructionIntDup - -prop_IntPop :: State -> Property -prop_IntPop = popTest int instructionIntPop - -prop_IntDupN :: State -> Property -prop_IntDupN = dupTestN int instructionIntDupN - -prop_IntSwap :: State -> Property -prop_IntSwap = swapTest int instructionIntSwap - -prop_IntRot :: State -> Property -prop_IntRot = rotTest int instructionIntRot - -prop_IntFlush :: State -> Property -prop_IntFlush = flushTest int instructionIntFlush - -prop_IntEq :: State -> Property -prop_IntEq = aab2Test int bool instructionIntEq (==) - -prop_IntStackDepth :: State -> Property -prop_IntStackDepth = stackDepthTest int instructionIntStackDepth - -prop_IntYank :: State -> Property -prop_IntYank = yankTest int instructionIntYank - --- prop_IntYankDup :: State -> Property --- prop_IntYankDup = yankDupTest int instructionIntYankDup diff --git a/src/PushTests/UtilTests.hs b/src/PushTests/UtilTests.hs deleted file mode 100644 index 07b49da..0000000 --- a/src/PushTests/UtilTests.hs +++ /dev/null @@ -1,36 +0,0 @@ -module PushTests.UtilTests where - -import Instructions.GenericInstructions -import Test.QuickCheck - -prop_DeleteAtTest :: Int -> [Int] -> Property -prop_DeleteAtTest idx lst = - idx >= 0 && idx < length lst ==> - if null lst - then length lst === length (deleteAt idx lst) - else length lst === length (deleteAt idx lst) + 1 - -prop_CombineTupleTest :: Int -> ([Int], [Int]) -> Property -prop_CombineTupleTest val tup = - length (fst tup) + length (snd tup) === length (combineTuple val tup) - 1 - -prop_CombineTupleListTest :: [Int] -> ([Int], [Int]) -> Property -prop_CombineTupleListTest lst tup = - length (fst tup) + length (snd tup) === length (combineTupleList lst tup) - length lst - --- Could use forAll to only generate valid tests -prop_InsertAt :: Int -> Int -> [Int] -> Property -prop_InsertAt idx val lst = - idx >= 0 && idx < length lst ==> - length lst === length (insertAt idx val lst) - 1 .&&. - insertAt idx val lst !! idx === val - -prop_ReplaceAt :: Int -> Int -> [Int] -> Property -prop_ReplaceAt idx val lst = - idx >= 0 && idx < length lst ==> - length lst === length (replaceAt idx val lst) .&&. - replaceAt idx val lst !! idx === val - --- prop_SubList :: Int -> Int -> [Int] -> Property --- prop_SubList idx0 idx1 lst = - -- idx diff --git a/src/State.hs b/src/State.hs deleted file mode 100644 index cfd4071..0000000 --- a/src/State.hs +++ /dev/null @@ -1,166 +0,0 @@ -{-# LANGUAGE DeriveGeneric #-} -{-# LANGUAGE TemplateHaskell #-} - -module State where - -import Control.Lens hiding (elements) -import Data.Map qualified as Map -import GHC.Generics -import Test.QuickCheck - --- The exec stack must store heterogenous types, --- and we must be able to detect that type at runtime. --- One solution is for the exec stack to be a list of [Gene]. --- The parameter stack could be singular [Gene] or multiple [atomic] types. -data Gene - = GeneInt Int - | GeneFloat Float - | GeneBool Bool - | GeneString String - | GeneChar Char - | GeneVectorInt [Int] - | GeneVectorFloat [Float] - | GeneVectorBool [Bool] - | GeneVectorString [String] - | GeneVectorChar [Char] - | StateFunc (State -> State, String) -- The string stores the name of the function - | PlaceInput String - | Close - | Block [Gene] - deriving (Generic) - -instance Eq Gene where - GeneInt x == GeneInt y = x == y - GeneFloat x == GeneFloat y = x == y - GeneBool x == GeneBool y = x == y - GeneString x == GeneString y = x == y - GeneChar x == GeneChar y = x == y - PlaceInput x == PlaceInput y = x == y - GeneVectorInt xs == GeneVectorInt ys = xs == ys - GeneVectorFloat xs == GeneVectorFloat ys = xs == ys - GeneVectorBool xs == GeneVectorBool ys = xs == ys - GeneVectorString xs == GeneVectorString ys = xs == ys - GeneVectorChar xs == GeneVectorChar ys = xs == ys - Close == Close = True - StateFunc (_, nameX) == StateFunc (_, nameY) = nameX == nameY - Block x == Block y = x == y - _ == _ = False - -instance Show Gene where - show (GeneInt x) = "Int: " <> show x - show (GeneFloat x) = "Float: " <> show x - show (GeneBool x) = "Bool: " <> show x - show (GeneString x) = "String: " <> x - show (GeneChar x) = "Char: " <> show x - show (StateFunc (_, funcName)) = "Func: " <> funcName - show (PlaceInput x) = "In: " <> show x - show (GeneVectorInt xs) = "Int Vec: " <> show xs - show (GeneVectorFloat xs) = "Float Vec: " <> show xs - show (GeneVectorBool xs) = "Bool Vec: " <> show xs - show (GeneVectorString xs) = "String Vec: " <> show xs - show (GeneVectorChar xs) = "Char Vec: " <> show xs - show Close = "Close" - show (Block xs) = "Block: " <> show xs - -instance CoArbitrary Gene - -instance Arbitrary Gene where - arbitrary = - oneof - [ GeneInt <$> arbitrary, - GeneFloat <$> arbitrary, - GeneBool <$> arbitrary, - GeneString <$> arbitrary, - GeneChar <$> arbitrary, - StateFunc <$> arbitrary, - PlaceInput <$> arbitrary, - GeneVectorInt <$> arbitrary, - GeneVectorFloat <$> arbitrary, - GeneVectorBool <$> arbitrary, - GeneVectorString <$> arbitrary, - GeneVectorChar <$> arbitrary, - Block <$> arbitrary, - return Close - ] - -data State = State - { _exec :: [Gene], - _code :: [Gene], - _int :: [Int], - _float :: [Float], - _bool :: [Bool], - _string :: [String], - _char :: [Char], - _vectorInt :: [[Int]], - _vectorFloat :: [[Float]], - _vectorBool :: [[Bool]], - _vectorString :: [[String]], - _vectorChar :: [[Char]], - _parameter :: [Gene], - _input :: Map.Map String Gene - } - deriving (Show, Eq, Generic) - -instance Arbitrary State where - arbitrary = do - arbExec <- arbitrary - arbCode <- arbitrary - arbInt <- arbitrary - arbFloat <- arbitrary - arbBool <- arbitrary - arbString <- arbitrary - arbChar <- arbitrary - arbVectorInt <- arbitrary - arbVectorFloat <- arbitrary - arbVectorBool <- arbitrary - arbVectorString <- arbitrary - arbVectorChar <- arbitrary - arbParameter <- arbitrary - -- arbInput <- arbitrary - State arbExec arbCode arbInt arbFloat arbBool arbString arbChar arbVectorInt arbVectorFloat arbVectorBool arbVectorString arbVectorChar arbParameter <$> arbitrary - --- Thanks hlint lol - -instance CoArbitrary State - -emptyState :: State -emptyState = - State - { _exec = [], - _code = [], - _int = [], - _float = [], - _bool = [], - _string = [], - _char = [], - _parameter = [], - _vectorInt = [], - _vectorFloat = [], - _vectorBool = [], - _vectorString = [], - _vectorChar = [], - _input = Map.empty - } - -exampleState :: State -exampleState = - State - { _exec = [], - _code = [], - _int = [32, 56], - _float = [3.23, 9.235], - _bool = [True, False], - _string = ["abc", "123"], - _char = ['d', 'e', 'f'], - _parameter = [], - _vectorInt = [[1, 2], [5, 6, 8]], - _vectorFloat = [[1.234, 9.21], [5.42, 6.221, 8.5493]], - _vectorBool = [[True, False], [False, False, True]], - _vectorString = [["this is a sentence", "this is also a sentence"], ["s0", "s1", "s2"]], - _vectorChar = [['z', 'x'], ['r', 'a', 't', 'l']], - _input = Map.empty - } - --- This must stay at the end of the file. --- Template haskell seems to be messing with GHC.Generics -$(makeLenses ''State) From b47371a2fd2b5d8409e83536d67f9622f021ff63 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Thu, 6 Feb 2025 23:42:15 -0600 Subject: [PATCH 121/171] more changes/todo --- TODO.md | 10 +++- src/HushGP/Instructions.hs | 11 +++- src/HushGP/Instructions/CodeInstructions.hs | 56 ++++++++++++++++--- src/HushGP/Instructions/ExecInstructions.hs | 15 +++++ .../Instructions/GenericInstructions.hs | 16 ++++++ 5 files changed, 94 insertions(+), 14 deletions(-) diff --git a/TODO.md b/TODO.md index 7ca127c..58f96e6 100644 --- a/TODO.md +++ b/TODO.md @@ -4,12 +4,16 @@ - [ ] Make all vector functions applicable to string functions and vice versa - [ ] Implement all functions as seen in propeller -- [ ] Implement all functions as seen in the specification +- [X] Implement all functions as seen in the specification - [ ] Implement Linear Algebra functions as specified in the previous papers -- [ ] Add a function to sort a vector forward and backwards +- [X] Add a function to sort a vector forward and backwards - [ ] Disambiguate isEmpty and stackIsEmpty - [X] Rename Logical to Bool -- [x] Make int yank, shove, yankdup, and shovedup generic +- [X] Make int yank, shove, yankdup, and shovedup generic +- [ ] Write hackage documentation for each function +- [ ] Refactor all functions to take state as the final parameter +- [ ] Standardize the pattern matching parameters +- [ ] Write unit/quickcheck tests for all of the instructions ## PushGP TODO - [ ] Implement a Plushy genome translator diff --git a/src/HushGP/Instructions.hs b/src/HushGP/Instructions.hs index a296aeb..c912de3 100644 --- a/src/HushGP/Instructions.hs +++ b/src/HushGP/Instructions.hs @@ -209,7 +209,11 @@ allCodeInstructions = map StateFunc [ (instructionCodeFromVectorString, "instructionCodeFromVectorString"), (instructionCodeFromVectorBool, "instructionCodeFromVectorBool"), (instructionCodeFromVectorChar, "instructionCodeFromVectorChar"), - (instructionCodeFromExec, "instructionCodeFromExec") + (instructionCodeFromExec, "instructionCodeFromExec"), + (instructionCodeContainer, "instructionCodeContainer"), + (instructionCodeDiscrepancy, "instructionCodeDiscrepancy"), + (instructionCodeNoOp, "instructionCodeNoOp"), + (instructionCodeTailN, "instructionCodeTailN") ] allExecInstructions :: [Gene] @@ -233,7 +237,10 @@ allExecInstructions = map StateFunc [ (instructionExecDoTimes, "instructionExecDoTimes"), (instructionExecWhile, "instructionExecWhile"), (instructionExecDoWhile, "instructionExecDoWhile"), - (instructionExecWhen, "instructionExecWhen") + (instructionExecWhen, "instructionExecWhen"), + (instructionExecK, "instructionExecK"), + (instructionExecS, "instructionExecS"), + (instructionExecY, "instrucitonExecY") ] allStringInstructions :: [Gene] diff --git a/src/HushGP/Instructions/CodeInstructions.hs b/src/HushGP/Instructions/CodeInstructions.hs index f7f069c..0ce67b2 100644 --- a/src/HushGP/Instructions/CodeInstructions.hs +++ b/src/HushGP/Instructions/CodeInstructions.hs @@ -18,10 +18,25 @@ blockIsNull :: Gene -> Bool blockIsNull (Block xs) = null xs blockIsNull _ = False --- I think I can abstract the boilerplate base case check for a lot of these --- with a different function +-- https://faculty.hampshire.edu/lspector/push3-description.html#Type +-- CODE.CONTAINER +findContainer :: Gene -> Gene -> Gene +findContainer (Block fullA) gene + | length fullA <= blockLength gene = Block [] + | gene `elem` fullA = Block [] -- Not allowed to be top level + | any isBlock fullA = findContainer' (filter isBlock fullA) gene + | otherwise = Block [] + where + findContainer' :: [Gene] -> Gene -> Gene + findContainer' [] _ = Block [] + findContainer' ((Block x) : xs) g = if g `elem` x then Block x else findContainer' xs g + findContainer' _ _ = Block [] -- This should never happen +findContainer _ _ = Block [] + +countDiscrepancy :: Gene -> Gene -> Int +countDiscrepancy (Block xs) (Block ys) = sum [if uncurry (==) tup then 0 else 1 | tup <- zip xs ys] + abs (length xs - length ys) +countDiscrepancy xgene ygene = if xgene == ygene then 1 else 0 --- empty Blocks are a thing but that shouldn't really matter extractFirstFromBlock :: Gene -> Gene extractFirstFromBlock (Block (x : _)) = x extractFirstFromBlock gene = gene @@ -38,9 +53,8 @@ extractInitFromBlock gene = gene extractTailFromBlock :: Gene -> Gene extractTailFromBlock (Block xs) = Block (drop 1 xs) -extractTailFromBlock gene = gene +extractTailFromBlock _ = Block [] --- This function took at least 3 hours to program. codeAtPoint :: [Gene] -> Int -> Gene codeAtPoint (gene : _) 0 = gene codeAtPoint [] _ = Block [] -- Should only happen if an empty block is last Gene in the list of Genes @@ -53,11 +67,9 @@ codeInsertAtPoint [] gene _ = [gene] -- This shouldn't happen (lol) codeInsertAtPoint ((Block genes) : oldGenes) gene index = Block (codeInsertAtPoint genes gene (index - 1)) : oldGenes codeInsertAtPoint (oldGene : oldGenes) gene index = oldGene : codeInsertAtPoint oldGenes gene (index - 1) --- This one functions differently than pysh. --- I like this one because it preserves ordering in the second case codeCombine :: Gene -> Gene -> Gene codeCombine (Block xs) (Block ys) = Block (xs <> ys) -codeCombine (Block xs) ygene = Block (xs <> [ygene]) +codeCombine (Block xs) ygene = Block (ygene : xs) codeCombine xgene (Block ys) = Block (xgene : ys) codeCombine xgene ygene = Block [xgene, ygene] @@ -85,6 +97,7 @@ instructionCodeLength :: State -> State instructionCodeLength state@(State {_code = (c : cs), _int = is}) = state {_code = cs, _int = blockLength c : is} instructionCodeLength state = state +-- CODE.CAR instructionCodeFirst :: State -> State instructionCodeFirst state@(State {_code = (c : cs)}) = state {_code = extractFirstFromBlock c : cs} instructionCodeFirst state = state @@ -93,11 +106,22 @@ instructionCodeLast :: State -> State instructionCodeLast state@(State {_code = (c : cs)}) = state {_code = extractLastFromBlock c : cs} instructionCodeLast state = state +-- CODE.CDR -- https://erp12.github.io/pyshgp/html/core_instructions.html#code-rest instructionCodeTail :: State -> State instructionCodeTail state@(State {_code = (c : cs)}) = state {_code = extractTailFromBlock c : cs} instructionCodeTail state = state +-- |Takes the tail of a block starting at an index determined by the int stack +-- https://faculty.hampshire.edu/lspector/push3-description.html#Type +-- This is the CODE.NTHCDR command +instructionCodeTailN :: State -> State +instructionCodeTailN state@(State {_code = Block bc : cs, _int = i : is}) = state {_code = Block (drop index bc) : cs, _int = is} + where + index :: Int + index = abs i `mod` length bc +instructionCodeTailN state = state + -- https://erp12.github.io/pyshgp/html/core_instructions.html#code-but-last instructionCodeInit :: State -> State instructionCodeInit state@(State {_code = (c : cs)}) = state {_code = extractInitFromBlock c : cs} @@ -116,7 +140,7 @@ instructionCodeCombine state@(State {_code = (c1 : c2 : cs)}) = state {_code = c instructionCodeCombine state = state instructionCodeDo :: State -> State -instructionCodeDo state@(State {_code = (c1 : cs), _exec = es}) = state {_code = cs, _exec = c1: es} +instructionCodeDo state@(State {_code = c1 : cs, _exec = es}) = state {_code = cs, _exec = c1 : es} instructionCodeDo state = state instructionCodeDoDup :: State -> State @@ -197,6 +221,9 @@ instructionCodeSize :: State -> State instructionCodeSize state@(State {_code = c1 : cs, _int = is}) = state{_code = cs, _int = codeRecursiveSize c1 : is} instructionCodeSize state = state +-- instructionCodeContainer :: State -> State +-- instructionCodeContainer + -- There's a bug for this instruction in pysh where the last item in the -- top level Block isn't counted, and if passed 0, then the entire codeblock is returned. -- I designed this function differently so 0 returns the 0th element, and the last item @@ -308,3 +335,14 @@ instructionCodeFromVectorChar state = instructionCodeFrom state vectorChar GeneV instructionCodeFromExec :: State -> State instructionCodeFromExec state = instructionCodeFrom state exec id + +instructionCodeContainer :: State -> State +instructionCodeContainer state@(State {_code = c1 : c2 : cs}) = state {_code = findContainer c1 c2 : cs} +instructionCodeContainer state = state + +instructionCodeDiscrepancy :: State -> State +instructionCodeDiscrepancy state@(State {_code = c1 : c2 : cs, _int = is}) = state {_code = cs, _int = countDiscrepancy c1 c2 : is} +instructionCodeDiscrepancy state = state + +instructionCodeNoOp :: State -> State +instructionCodeNoOp state = state diff --git a/src/HushGP/Instructions/ExecInstructions.hs b/src/HushGP/Instructions/ExecInstructions.hs index 4b77aa7..d0fe1ad 100644 --- a/src/HushGP/Instructions/ExecInstructions.hs +++ b/src/HushGP/Instructions/ExecInstructions.hs @@ -104,3 +104,18 @@ instructionExecWhen state@(State {_exec = (_ : es), _bool = (b : bs)}) = then state {_exec = es, _bool = bs} else state {_bool = bs} instructionExecWhen state = state + +-- |The K combinator +instructionExecK :: State -> State +instructionExecK state@(State {_exec = e1 : _ : es}) = state{_exec = e1 : es} +instructionExecK state = state + +-- |The S combinator +instructionExecS :: State -> State +instructionExecS state@(State {_exec = e1 : e2 : e3 : es}) = state{_exec = e1 : e3 : Block [e2, e3] : es} +instructionExecS state = state + +-- |The Y combinator +instructionExecY :: State -> State +instructionExecY state@(State {_exec = e1 : es}) = state{_exec = e1 : Block [StateFunc (instructionExecY, "instructionExecY"), e1] : es} +instructionExecY state = state diff --git a/src/HushGP/Instructions/GenericInstructions.hs b/src/HushGP/Instructions/GenericInstructions.hs index 8c474ef..ffa79e7 100644 --- a/src/HushGP/Instructions/GenericInstructions.hs +++ b/src/HushGP/Instructions/GenericInstructions.hs @@ -2,6 +2,8 @@ module HushGP.Instructions.GenericInstructions where import Control.Lens import HushGP.State +import Data.List (sort, sortBy) +import Data.Ord -- import Debug.Trace @@ -346,3 +348,17 @@ instructionCodeFrom state@(State {_code = cs}) accessor geneType = case uncons (view accessor state) of Just (x, xs) -> state{_code = geneType x : cs} & accessor .~ xs _ -> state + +-- |A function that sorts the first vector for a vectorType +instructionVectorSort :: Ord a => Lens' State [[a]] -> State -> State +instructionVectorSort accessor state = + case uncons (view accessor state) of + Just (x, xs) -> state & accessor .~ (sort x : xs) + _ -> state + +-- |A function that sorts the first vector in reverse order for a vectorType +instructionVectorSortReverse :: Ord a => Lens' State [[a]] -> State -> State +instructionVectorSortReverse accessor state = + case uncons (view accessor state) of + Just (x, xs) -> state & accessor .~ (sortBy (comparing Data.Ord.Down) x : xs) + _ -> state From ad4b7a2341477d84dd94f2140a407015ffbb0a90 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Fri, 7 Feb 2025 01:03:14 -0600 Subject: [PATCH 122/171] more instructions/knocking off the TODO --- TODO.md | 4 +- src/HushGP/Instructions.hs | 53 ++++++++++++++----- src/HushGP/Instructions/BoolInstructions.hs | 3 ++ src/HushGP/Instructions/CharInstructions.hs | 3 ++ src/HushGP/Instructions/CodeInstructions.hs | 3 ++ src/HushGP/Instructions/ExecInstructions.hs | 3 ++ src/HushGP/Instructions/FloatInstructions.hs | 15 ++++++ .../Instructions/GenericInstructions.hs | 26 ++++++++- src/HushGP/Instructions/IntInstructions.hs | 15 ++++++ src/HushGP/Instructions/StringInstructions.hs | 15 ++++++ .../Instructions/VectorBoolInstructions.hs | 9 ++++ .../Instructions/VectorCharInstructions.hs | 9 ++++ .../Instructions/VectorFloatInstructions.hs | 9 ++++ .../Instructions/VectorIntInstructions.hs | 9 ++++ .../Instructions/VectorStringInstructions.hs | 9 ++++ 15 files changed, 170 insertions(+), 15 deletions(-) diff --git a/TODO.md b/TODO.md index 58f96e6..79efdc2 100644 --- a/TODO.md +++ b/TODO.md @@ -3,7 +3,7 @@ ## Push Language TODO - [ ] Make all vector functions applicable to string functions and vice versa -- [ ] Implement all functions as seen in propeller +- [X] Implement all functions as seen in propeller - [X] Implement all functions as seen in the specification - [ ] Implement Linear Algebra functions as specified in the previous papers - [X] Add a function to sort a vector forward and backwards @@ -12,7 +12,7 @@ - [X] Make int yank, shove, yankdup, and shovedup generic - [ ] Write hackage documentation for each function - [ ] Refactor all functions to take state as the final parameter -- [ ] Standardize the pattern matching parameters +- [ ] Standardize the pattern matching parameter names, such as c1 : cs - [ ] Write unit/quickcheck tests for all of the instructions ## PushGP TODO diff --git a/src/HushGP/Instructions.hs b/src/HushGP/Instructions.hs index c912de3..53772dc 100644 --- a/src/HushGP/Instructions.hs +++ b/src/HushGP/Instructions.hs @@ -70,7 +70,10 @@ allIntInstructions = map StateFunc [ (instructionIntYank, "instructionIntYank"), (instructionIntYankDup, "instructionIntYankDup"), (instructionIntShove, "instructionIntShove"), - (instructionIntIsEmpty, "instructionIntIsEmpty") + (instructionIntIsEmpty, "instructionIntIsEmpty"), + (instructionIntFromChar, "instructionIntFromChar"), + (instructionIntFromString, "instructionIntFromString"), + (instructionIntDupItems, "instructionIntDupItems") ] allFloatInstructions :: [Gene] @@ -100,7 +103,10 @@ allFloatInstructions = map StateFunc [ (instructionFloatYank, "instructionFloatYank"), (instructionFloatYankDup, "instructionFloatYankDup"), (instructionFloatShove, "instructionFloatShove"), - (instructionFloatIsEmpty, "instructionFloatIsEmpty") + (instructionFloatIsEmpty, "instructionFloatIsEmpty"), + (instructionFloatFromChar, "instructionFloatFromChar"), + (instructionFloatFromString, "instructionFloatFromString"), + (instructionFloatDupItems, "instructionFloatDupItems") ] allBoolInstructions :: [Gene] @@ -124,7 +130,8 @@ allBoolInstructions = map StateFunc [ (instructionBoolYankDup, "instructionBoolYankDup"), (instructionBoolShove, "instructionBoolShove"), (instructionBoolShoveDup, "instructionBoolShoveDup"), - (instructionBoolIsEmpty, "instructionBoolIsEmpty") + (instructionBoolIsEmpty, "instructionBoolIsEmpty"), + (instructionBoolDupItems, "instructionBoolDupItems") ] allCharInstructions :: [Gene] @@ -152,7 +159,8 @@ allCharInstructions = map StateFunc [ (instructionCharYankDup, "instructionCharYankDup"), (instructionCharShove, "instructionCharShove"), (instructionCharShoveDup, "instructionCharShoveDup"), - (instructionCharIsEmpty, "instructionCharIsEmpty") + (instructionCharIsEmpty, "instructionCharIsEmpty"), + (instructionCharDupItems, "instructionCharDupItems") ] allCodeInstructions :: [Gene] @@ -213,7 +221,8 @@ allCodeInstructions = map StateFunc [ (instructionCodeContainer, "instructionCodeContainer"), (instructionCodeDiscrepancy, "instructionCodeDiscrepancy"), (instructionCodeNoOp, "instructionCodeNoOp"), - (instructionCodeTailN, "instructionCodeTailN") + (instructionCodeTailN, "instructionCodeTailN"), + (instructionCodeDupItems, "instructionCodeDupItems") ] allExecInstructions :: [Gene] @@ -240,7 +249,8 @@ allExecInstructions = map StateFunc [ (instructionExecWhen, "instructionExecWhen"), (instructionExecK, "instructionExecK"), (instructionExecS, "instructionExecS"), - (instructionExecY, "instrucitonExecY") + (instructionExecY, "instrucitonExecY"), + (instructionExecDupItems, "instructionExecDupItems") ] allStringInstructions :: [Gene] @@ -302,7 +312,11 @@ allStringInstructions = map StateFunc [ (instructionStringYankDup, "instructionStringYankDup"), (instructionStringShove, "instructionStringShove"), (instructionStringShoveDup, "instructionStringShoveDup"), - (instructionStringIsEmpty, "instructionStringIsEmpty") + (instructionStringIsEmpty, "instructionStringIsEmpty"), + (instructionStringSort, "instructionStringSort"), + (instructionStringSortReverse, "instructionStringSortReverse"), + (instructionStringDupItems, "instructionStringDupItems"), + (instructionStringParseToChar, "instructionStringParseToChar") ] allVectorIntInstructions :: [Gene] @@ -340,7 +354,10 @@ allVectorIntInstructions = map StateFunc [ (instructionVectorIntYankDup, "instructionVectorIntYankDup"), (instructionVectorIntShove, "instructionVectorIntShove"), (instructionVectorIntShoveDup, "instructionVectorIntShoveDup"), - (instructionVectorIntStackIsEmpty, "instructionVectorIntStackIsEmpty") + (instructionVectorIntStackIsEmpty, "instructionVectorIntStackIsEmpty"), + (instructionVectorIntSort, "instructionVectorIntSort"), + (instructionVectorIntSortReverse, "instructionVectorIntSortReverse"), + (instructionVectorIntDupItems, "instructionVectorIntDupItems") ] allVectorFloatInstructions :: [Gene] @@ -378,7 +395,10 @@ allVectorFloatInstructions = map StateFunc [ (instructionVectorFloatYankDup, "instructionVectorFloatYankDup"), (instructionVectorFloatShove, "instructionVectorFloatShove"), (instructionVectorFloatShoveDup, "instructionVectorFloatShoveDup"), - (instructionVectorFloatStackIsEmpty, "instructionVectorFloatStackIsEmpty") + (instructionVectorFloatStackIsEmpty, "instructionVectorFloatStackIsEmpty"), + (instructionVectorFloatSort, "instructionVectorFloatSort"), + (instructionVectorFloatSortReverse, "instructionVectorFloatSortReverse"), + (instructionVectorFloatDupItems, "instructionVectorFloatDupItems") ] allVectorCharInstructions :: [Gene] @@ -416,7 +436,10 @@ allVectorCharInstructions = map StateFunc [ (instructionVectorCharYankDup, "instructionVectorCharYankDup"), (instructionVectorCharShove, "instructionVectorCharShove"), (instructionVectorCharShoveDup, "instructionVectorCharShoveDup"), - (instructionVectorCharStackIsEmpty, "instructionVectorCharStackIsEmpty") + (instructionVectorCharStackIsEmpty, "instructionVectorCharStackIsEmpty"), + (instructionVectorCharSort, "instructionVectorCharSort"), + (instructionVectorCharSortReverse, "instructionVectorCharSortReverse"), + (instructionVectorCharDupItems, "instructionVectorCharDupItems") ] allVectorStringInstructions :: [Gene] @@ -454,7 +477,10 @@ allVectorStringInstructions = map StateFunc [ (instructionVectorStringYankDup, "instructionVectorStringYankDup"), (instructionVectorStringShove, "instructionVectorStringShove"), (instructionVectorStringShoveDup, "instructionVectorStringShoveDup"), - (instructionVectorStringStackIsEmpty, "instructionVectorStringStackIsEmpty") + (instructionVectorStringStackIsEmpty, "instructionVectorStringStackIsEmpty"), + (instructionVectorStringSort, "instructionVectorStringSort"), + (instructionVectorStringSortReverse, "instructionVectorStringSortReverse"), + (instructionVectorStringDupItems, "instructionVectorStringDupItems") ] allVectorBoolInstructions :: [Gene] @@ -492,7 +518,10 @@ allVectorBoolInstructions = map StateFunc [ (instructionVectorBoolYankDup, "instructionVectorBoolYankDup"), (instructionVectorBoolShove, "instructionVectorBoolShove"), (instructionVectorBoolShoveDup, "instructionVectorBoolShoveDup"), - (instructionVectorBoolStackIsEmpty, "instructionVectorBoolStackIsEmpty") + (instructionVectorBoolStackIsEmpty, "instructionVectorBoolStackIsEmpty"), + (instructionVectorBoolSort, "instructionVectorBoolSort"), + (instructionVectorBoolSortReverse, "instructionVectorBoolSortReverse"), + (instructionVectorBoolDupItems, "instructionVectorBoolDupItems") ] allInstructions :: [Gene] diff --git a/src/HushGP/Instructions/BoolInstructions.hs b/src/HushGP/Instructions/BoolInstructions.hs index 5689349..eff7e5a 100644 --- a/src/HushGP/Instructions/BoolInstructions.hs +++ b/src/HushGP/Instructions/BoolInstructions.hs @@ -77,3 +77,6 @@ instructionBoolShoveDup state = instructionShoveDup state bool instructionBoolIsEmpty :: State -> State instructionBoolIsEmpty state = instructionIsEmpty state bool + +instructionBoolDupItems :: State -> State +instructionBoolDupItems = instructionDupItems bool diff --git a/src/HushGP/Instructions/CharInstructions.hs b/src/HushGP/Instructions/CharInstructions.hs index 3150ba3..f2f4864 100644 --- a/src/HushGP/Instructions/CharInstructions.hs +++ b/src/HushGP/Instructions/CharInstructions.hs @@ -87,3 +87,6 @@ instructionCharShove state = instructionShove state char instructionCharShoveDup :: State -> State instructionCharShoveDup state = instructionShoveDup state char + +instructionCharDupItems :: State -> State +instructionCharDupItems = instructionDupItems char diff --git a/src/HushGP/Instructions/CodeInstructions.hs b/src/HushGP/Instructions/CodeInstructions.hs index 0ce67b2..2078992 100644 --- a/src/HushGP/Instructions/CodeInstructions.hs +++ b/src/HushGP/Instructions/CodeInstructions.hs @@ -346,3 +346,6 @@ instructionCodeDiscrepancy state = state instructionCodeNoOp :: State -> State instructionCodeNoOp state = state + +instructionCodeDupItems :: State -> State +instructionCodeDupItems = instructionDupItems code diff --git a/src/HushGP/Instructions/ExecInstructions.hs b/src/HushGP/Instructions/ExecInstructions.hs index d0fe1ad..e4ced54 100644 --- a/src/HushGP/Instructions/ExecInstructions.hs +++ b/src/HushGP/Instructions/ExecInstructions.hs @@ -119,3 +119,6 @@ instructionExecS state = state instructionExecY :: State -> State instructionExecY state@(State {_exec = e1 : es}) = state{_exec = e1 : Block [StateFunc (instructionExecY, "instructionExecY"), e1] : es} instructionExecY state = state + +instructionExecDupItems :: State -> State +instructionExecDupItems = instructionDupItems exec diff --git a/src/HushGP/Instructions/FloatInstructions.hs b/src/HushGP/Instructions/FloatInstructions.hs index a9eb409..6b27896 100644 --- a/src/HushGP/Instructions/FloatInstructions.hs +++ b/src/HushGP/Instructions/FloatInstructions.hs @@ -3,6 +3,7 @@ module HushGP.Instructions.FloatInstructions where import Data.Fixed (mod') import HushGP.Instructions.GenericInstructions import HushGP.State +import Data.Char instructionFloatFromInt :: State -> State instructionFloatFromInt state@(State {_float = fs, _int = (i : is)}) = state {_float = (fromIntegral i :: Float) : fs, _int = is} @@ -12,6 +13,17 @@ instructionFloatFromBool :: State -> State instructionFloatFromBool state@(State {_bool = (b : bs), _float = fs}) = state {_bool = bs, _float = (if b then 1.0 else 0.0) : fs} instructionFloatFromBool state = state +instructionFloatFromChar :: State -> State +instructionFloatFromChar state@(State {_char = c : cs, _float = fs}) = state {_char = cs, _float = (fromIntegral (ord c) :: Float) : fs} +instructionFloatFromChar state = state + +instructionFloatFromString :: State -> State +instructionFloatFromString state@(State {_string = s1 : ss, _float = fs}) = + if all isDigit s1 + then state{_string = ss, _float = read @Float s1 : fs} + else state +instructionFloatFromString state = state + instructionFloatAdd :: State -> State instructionFloatAdd state@(State {_float = (f1 : f2 : fs)}) = state {_float = f2 + f1 : fs} instructionFloatAdd state = state @@ -114,3 +126,6 @@ instructionFloatCos state = state instructionFloatTan :: State -> State instructionFloatTan state@(State {_float = f1 : fs}) = state {_float = tan f1 : fs} instructionFloatTan state = state + +instructionFloatDupItems :: State -> State +instructionFloatDupItems = instructionDupItems float diff --git a/src/HushGP/Instructions/GenericInstructions.hs b/src/HushGP/Instructions/GenericInstructions.hs index ffa79e7..cf6479c 100644 --- a/src/HushGP/Instructions/GenericInstructions.hs +++ b/src/HushGP/Instructions/GenericInstructions.hs @@ -4,6 +4,7 @@ import Control.Lens import HushGP.State import Data.List (sort, sortBy) import Data.Ord +import Data.List.Split -- import Debug.Trace @@ -103,7 +104,7 @@ instructionIsEmpty state@(State {_bool = bs}) accessor = state{_bool = null (vie -- I might be able to move some of the int stack error checking -- to the integer call. For now this may be a tad inefficient. -instructionDupN :: forall a. Show a => State -> Lens' State [a] -> State +instructionDupN :: forall a. State -> Lens' State [a] -> State instructionDupN state accessor = case uncons (view int state) of Just (i1,is) -> @@ -119,6 +120,15 @@ instructionDupN state accessor = then instructionDupNHelper (count - 1) instruction internalAccessor (internalState & accessor .~ (instruction : view accessor internalState)) else internalState +-- |Duplicates the top N items on a stack. If n <= 0 nothing happens +-- TODO: Will need to implement a max stack items at some point +instructionDupItems :: Lens' State [a] -> State -> State +instructionDupItems accessor state@(State {_int = i1 : is}) = + if i1 <= 0 + then state{_int = is} + else state{_int = is} & accessor .~ (take i1 (view accessor state{_int = is}) <> view accessor state{_int = is}) +instructionDupItems _ state = state + instructionSwap :: State -> Lens' State [a] -> State instructionSwap state accessor = state & accessor .~ swapper (view accessor state) @@ -207,6 +217,12 @@ instructionConj state primAccessor vectorAccessor = (Just (p1,ps), Just (v1,vs)) -> state & primAccessor .~ ps & vectorAccessor .~ ((p1 : v1) : vs) _ -> state +instructionConjEnd :: Lens' State [a] -> Lens' State [[a]] -> State -> State +instructionConjEnd primAccessor vectorAccessor state = + case (uncons (view primAccessor state), uncons (view vectorAccessor state)) of + (Just (p1,ps), Just (v1,vs)) -> state & primAccessor .~ ps & vectorAccessor .~ ((v1 <> [p1]) : vs) + _ -> state + -- v for vector, vs for vectorstack (also applicable to strings) -- Could abstract this unconsing even further in all functions below instructionTakeN :: State -> Lens' State [[a]] -> State @@ -306,6 +322,14 @@ instructionVectorOccurrencesOf state primAccessor vectorAccessor = (Just (v1, vs), Just (p1, ps)) -> (state & vectorAccessor .~ vs & primAccessor .~ ps) & int .~ (amtOccurences v1 [p1] : view int (state & vectorAccessor .~ vs & primAccessor .~ ps)) _ -> state +-- | This function parses the primitives of a vector type and pushes split up onto their +-- respective stack +instructionVectorParseToPrim :: Lens' State [[a]] -> State -> State +instructionVectorParseToPrim accessor state = + case uncons (view accessor state) of + Just (x1, xs) -> state & accessor .~ (chunksOf 1 x1 <> xs) + _ -> state + instructionVectorSetNth :: State -> Lens' State [a] -> Lens' State [[a]] -> State instructionVectorSetNth state@(State {_int = i1 : is}) primAccessor vectorAccessor = case (uncons (view vectorAccessor state{_int = is}), uncons (view primAccessor state{_int = is})) of diff --git a/src/HushGP/Instructions/IntInstructions.hs b/src/HushGP/Instructions/IntInstructions.hs index 55a3180..ea3877b 100644 --- a/src/HushGP/Instructions/IntInstructions.hs +++ b/src/HushGP/Instructions/IntInstructions.hs @@ -2,6 +2,7 @@ module HushGP.Instructions.IntInstructions where import HushGP.State import HushGP.Instructions.GenericInstructions +import Data.Char -- import Debug.Trace instructionIntFromFloat :: State -> State @@ -12,6 +13,17 @@ instructionIntFromBool :: State -> State instructionIntFromBool state@(State {_bool = (b : bs), _int = is}) = state {_bool = bs, _int = (if b then 1 else 0) : is} instructionIntFromBool state = state +instructionIntFromChar :: State -> State +instructionIntFromChar state@(State {_char = c : cs, _int = is}) = state {_char = cs, _int = ord c : is} +instructionIntFromChar state = state + +instructionIntFromString :: State -> State +instructionIntFromString state@(State {_string = s1 : ss, _int = is}) = + if all isDigit s1 + then state{_string = ss, _int = read @Int s1 : is} + else state +instructionIntFromString state = state + instructionIntAdd :: State -> State instructionIntAdd state@(State {_int = (i1 : i2 : is)}) = state {_int = i2 + i1 : is} instructionIntAdd state = state @@ -102,3 +114,6 @@ instructionIntShoveDup state = instructionShoveDup state int instructionIntIsEmpty :: State -> State instructionIntIsEmpty state = instructionIsEmpty state int + +instructionIntDupItems :: State -> State +instructionIntDupItems = instructionDupItems int diff --git a/src/HushGP/Instructions/StringInstructions.hs b/src/HushGP/Instructions/StringInstructions.hs index d893027..59d492a 100644 --- a/src/HushGP/Instructions/StringInstructions.hs +++ b/src/HushGP/Instructions/StringInstructions.hs @@ -140,6 +140,9 @@ instructionStringTail state = state instructionStringAppendChar :: State -> State instructionStringAppendChar state = instructionConj state char string +instructionStringConjEndChar :: State -> State +instructionStringConjEndChar = instructionConjEnd char string + instructionStringRest :: State -> State instructionStringRest state = instructionRest state string @@ -229,3 +232,15 @@ instructionStringShove state = instructionShove state string instructionStringShoveDup :: State -> State instructionStringShoveDup state = instructionShoveDup state string + +instructionStringSort :: State -> State +instructionStringSort = instructionVectorSort string + +instructionStringSortReverse :: State -> State +instructionStringSortReverse = instructionVectorSortReverse string + +instructionStringDupItems :: State -> State +instructionStringDupItems = instructionDupItems string + +instructionStringParseToChar :: State -> State +instructionStringParseToChar = instructionVectorParseToPrim string diff --git a/src/HushGP/Instructions/VectorBoolInstructions.hs b/src/HushGP/Instructions/VectorBoolInstructions.hs index 6f226c1..2a07ea2 100644 --- a/src/HushGP/Instructions/VectorBoolInstructions.hs +++ b/src/HushGP/Instructions/VectorBoolInstructions.hs @@ -104,3 +104,12 @@ instructionVectorBoolShove state = instructionShove state vectorBool instructionVectorBoolShoveDup :: State -> State instructionVectorBoolShoveDup state = instructionShoveDup state vectorBool + +instructionVectorBoolSort :: State -> State +instructionVectorBoolSort = instructionVectorSort vectorBool + +instructionVectorBoolSortReverse :: State -> State +instructionVectorBoolSortReverse = instructionVectorSortReverse vectorBool + +instructionVectorBoolDupItems :: State -> State +instructionVectorBoolDupItems = instructionDupItems vectorBool diff --git a/src/HushGP/Instructions/VectorCharInstructions.hs b/src/HushGP/Instructions/VectorCharInstructions.hs index 12b083e..b84fef7 100644 --- a/src/HushGP/Instructions/VectorCharInstructions.hs +++ b/src/HushGP/Instructions/VectorCharInstructions.hs @@ -104,3 +104,12 @@ instructionVectorCharShove state = instructionShove state vectorChar instructionVectorCharShoveDup :: State -> State instructionVectorCharShoveDup state = instructionShoveDup state vectorChar + +instructionVectorCharSort :: State -> State +instructionVectorCharSort = instructionVectorSort vectorChar + +instructionVectorCharSortReverse :: State -> State +instructionVectorCharSortReverse = instructionVectorSortReverse vectorChar + +instructionVectorCharDupItems :: State -> State +instructionVectorCharDupItems = instructionDupItems vectorChar diff --git a/src/HushGP/Instructions/VectorFloatInstructions.hs b/src/HushGP/Instructions/VectorFloatInstructions.hs index 3f21566..da7e0de 100644 --- a/src/HushGP/Instructions/VectorFloatInstructions.hs +++ b/src/HushGP/Instructions/VectorFloatInstructions.hs @@ -104,3 +104,12 @@ instructionVectorFloatShove state = instructionShove state vectorFloat instructionVectorFloatShoveDup :: State -> State instructionVectorFloatShoveDup state = instructionShoveDup state vectorFloat + +instructionVectorFloatSort :: State -> State +instructionVectorFloatSort = instructionVectorSort vectorFloat + +instructionVectorFloatSortReverse :: State -> State +instructionVectorFloatSortReverse = instructionVectorSortReverse vectorFloat + +instructionVectorFloatDupItems :: State -> State +instructionVectorFloatDupItems = instructionDupItems vectorFloat diff --git a/src/HushGP/Instructions/VectorIntInstructions.hs b/src/HushGP/Instructions/VectorIntInstructions.hs index 1bac705..6ce893f 100644 --- a/src/HushGP/Instructions/VectorIntInstructions.hs +++ b/src/HushGP/Instructions/VectorIntInstructions.hs @@ -104,3 +104,12 @@ instructionVectorIntShove state = instructionShove state vectorChar instructionVectorIntShoveDup :: State -> State instructionVectorIntShoveDup state = instructionShoveDup state vectorChar + +instructionVectorIntSort :: State -> State +instructionVectorIntSort = instructionVectorSort vectorInt + +instructionVectorIntSortReverse :: State -> State +instructionVectorIntSortReverse = instructionVectorSortReverse vectorInt + +instructionVectorIntDupItems :: State -> State +instructionVectorIntDupItems = instructionDupItems vectorInt diff --git a/src/HushGP/Instructions/VectorStringInstructions.hs b/src/HushGP/Instructions/VectorStringInstructions.hs index 39d0b69..0914d87 100644 --- a/src/HushGP/Instructions/VectorStringInstructions.hs +++ b/src/HushGP/Instructions/VectorStringInstructions.hs @@ -104,3 +104,12 @@ instructionVectorStringShove state = instructionShove state vectorString instructionVectorStringShoveDup :: State -> State instructionVectorStringShoveDup state = instructionShoveDup state vectorString + +instructionVectorStringSort :: State -> State +instructionVectorStringSort = instructionVectorSort vectorString + +instructionVectorStringSortReverse :: State -> State +instructionVectorStringSortReverse = instructionVectorSortReverse vectorString + +instructionVectorStringDupItems :: State -> State +instructionVectorStringDupItems = instructionDupItems vectorString From 0b280af5912876dd1dff128f140733af10c5dde7 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Fri, 7 Feb 2025 14:46:18 -0600 Subject: [PATCH 123/171] disambiguate isEmpty and isStackEmpty --- TODO.md | 2 +- src/HushGP/Instructions.hs | 24 +++++++++---------- src/HushGP/Instructions/BoolInstructions.hs | 4 ++-- src/HushGP/Instructions/CharInstructions.hs | 4 ++-- src/HushGP/Instructions/CodeInstructions.hs | 4 ++-- src/HushGP/Instructions/ExecInstructions.hs | 4 ++-- src/HushGP/Instructions/FloatInstructions.hs | 4 ++-- .../Instructions/GenericInstructions.hs | 4 ++-- src/HushGP/Instructions/IntInstructions.hs | 4 ++-- src/HushGP/Instructions/StringInstructions.hs | 4 ++-- .../Instructions/VectorBoolInstructions.hs | 4 ++-- .../Instructions/VectorCharInstructions.hs | 4 ++-- .../Instructions/VectorFloatInstructions.hs | 4 ++-- .../Instructions/VectorIntInstructions.hs | 4 ++-- .../Instructions/VectorStringInstructions.hs | 4 ++-- 15 files changed, 39 insertions(+), 39 deletions(-) diff --git a/TODO.md b/TODO.md index 79efdc2..3ed2d12 100644 --- a/TODO.md +++ b/TODO.md @@ -7,7 +7,7 @@ - [X] Implement all functions as seen in the specification - [ ] Implement Linear Algebra functions as specified in the previous papers - [X] Add a function to sort a vector forward and backwards -- [ ] Disambiguate isEmpty and stackIsEmpty +- [X] Disambiguate isEmpty and stackIsEmpty - [X] Rename Logical to Bool - [X] Make int yank, shove, yankdup, and shovedup generic - [ ] Write hackage documentation for each function diff --git a/src/HushGP/Instructions.hs b/src/HushGP/Instructions.hs index 53772dc..b2b4a99 100644 --- a/src/HushGP/Instructions.hs +++ b/src/HushGP/Instructions.hs @@ -70,7 +70,7 @@ allIntInstructions = map StateFunc [ (instructionIntYank, "instructionIntYank"), (instructionIntYankDup, "instructionIntYankDup"), (instructionIntShove, "instructionIntShove"), - (instructionIntIsEmpty, "instructionIntIsEmpty"), + (instructionIntIsStackEmpty, "instructionIntIsStackEmpty"), (instructionIntFromChar, "instructionIntFromChar"), (instructionIntFromString, "instructionIntFromString"), (instructionIntDupItems, "instructionIntDupItems") @@ -103,7 +103,7 @@ allFloatInstructions = map StateFunc [ (instructionFloatYank, "instructionFloatYank"), (instructionFloatYankDup, "instructionFloatYankDup"), (instructionFloatShove, "instructionFloatShove"), - (instructionFloatIsEmpty, "instructionFloatIsEmpty"), + (instructionFloatIsStackEmpty, "instructionFloatIsStackEmpty"), (instructionFloatFromChar, "instructionFloatFromChar"), (instructionFloatFromString, "instructionFloatFromString"), (instructionFloatDupItems, "instructionFloatDupItems") @@ -130,7 +130,7 @@ allBoolInstructions = map StateFunc [ (instructionBoolYankDup, "instructionBoolYankDup"), (instructionBoolShove, "instructionBoolShove"), (instructionBoolShoveDup, "instructionBoolShoveDup"), - (instructionBoolIsEmpty, "instructionBoolIsEmpty"), + (instructionBoolIsStackEmpty, "instructionBoolIsStackEmpty"), (instructionBoolDupItems, "instructionBoolDupItems") ] @@ -159,7 +159,7 @@ allCharInstructions = map StateFunc [ (instructionCharYankDup, "instructionCharYankDup"), (instructionCharShove, "instructionCharShove"), (instructionCharShoveDup, "instructionCharShoveDup"), - (instructionCharIsEmpty, "instructionCharIsEmpty"), + (instructionCharIsStackEmpty, "instructionCharIsStackEmpty"), (instructionCharDupItems, "instructionCharDupItems") ] @@ -206,7 +206,7 @@ allCodeInstructions = map StateFunc [ (instructionCodeYankDup, "instructionCodeYankDup"), (instructionCodeShove, "instructionCodeShove"), (instructionCodeShoveDup, "instructionCodeShoveDup"), - (instructionCodeStackIsEmpty, "instructionCodeStackIsEmpty"), + (instructionCodeIsStackEmpty, "instructionCodeIsStackEmpty"), (instructionCodeFromBool, "instructionCodeFromBool"), (instructionCodeFromInt, "instructionCodeFromInt"), (instructionCodeFromChar, "instructionCodeFromChar"), @@ -240,7 +240,7 @@ allExecInstructions = map StateFunc [ (instructionExecYankDup, "instructionExecYankDup"), (instructionExecShove, "instructionExecShove"), (instructionExecShoveDup, "instructionExecShoveDup"), - (instructionExecIsEmpty, "instructionExecIsEmpty"), + (instructionExecIsStackEmpty, "instructionExecIsStackEmpty"), (instructionExecDoRange, "instructionExecDoRange"), (instructionExecDoCount, "instructionExecDoCount"), (instructionExecDoTimes, "instructionExecDoTimes"), @@ -312,7 +312,7 @@ allStringInstructions = map StateFunc [ (instructionStringYankDup, "instructionStringYankDup"), (instructionStringShove, "instructionStringShove"), (instructionStringShoveDup, "instructionStringShoveDup"), - (instructionStringIsEmpty, "instructionStringIsEmpty"), + (instructionStringIsStackEmpty, "instructionStringIsStackEmpty"), (instructionStringSort, "instructionStringSort"), (instructionStringSortReverse, "instructionStringSortReverse"), (instructionStringDupItems, "instructionStringDupItems"), @@ -354,7 +354,7 @@ allVectorIntInstructions = map StateFunc [ (instructionVectorIntYankDup, "instructionVectorIntYankDup"), (instructionVectorIntShove, "instructionVectorIntShove"), (instructionVectorIntShoveDup, "instructionVectorIntShoveDup"), - (instructionVectorIntStackIsEmpty, "instructionVectorIntStackIsEmpty"), + (instructionVectorIntIsStackEmpty, "instructionVectorIntIsStackEmpty"), (instructionVectorIntSort, "instructionVectorIntSort"), (instructionVectorIntSortReverse, "instructionVectorIntSortReverse"), (instructionVectorIntDupItems, "instructionVectorIntDupItems") @@ -395,7 +395,7 @@ allVectorFloatInstructions = map StateFunc [ (instructionVectorFloatYankDup, "instructionVectorFloatYankDup"), (instructionVectorFloatShove, "instructionVectorFloatShove"), (instructionVectorFloatShoveDup, "instructionVectorFloatShoveDup"), - (instructionVectorFloatStackIsEmpty, "instructionVectorFloatStackIsEmpty"), + (instructionVectorFloatIsStackEmpty, "instructionVectorFloatIsStackEmpty"), (instructionVectorFloatSort, "instructionVectorFloatSort"), (instructionVectorFloatSortReverse, "instructionVectorFloatSortReverse"), (instructionVectorFloatDupItems, "instructionVectorFloatDupItems") @@ -436,7 +436,7 @@ allVectorCharInstructions = map StateFunc [ (instructionVectorCharYankDup, "instructionVectorCharYankDup"), (instructionVectorCharShove, "instructionVectorCharShove"), (instructionVectorCharShoveDup, "instructionVectorCharShoveDup"), - (instructionVectorCharStackIsEmpty, "instructionVectorCharStackIsEmpty"), + (instructionVectorCharIsStackEmpty, "instructionVectorCharIsStackEmpty"), (instructionVectorCharSort, "instructionVectorCharSort"), (instructionVectorCharSortReverse, "instructionVectorCharSortReverse"), (instructionVectorCharDupItems, "instructionVectorCharDupItems") @@ -477,7 +477,7 @@ allVectorStringInstructions = map StateFunc [ (instructionVectorStringYankDup, "instructionVectorStringYankDup"), (instructionVectorStringShove, "instructionVectorStringShove"), (instructionVectorStringShoveDup, "instructionVectorStringShoveDup"), - (instructionVectorStringStackIsEmpty, "instructionVectorStringStackIsEmpty"), + (instructionVectorStringIsStackEmpty, "instructionVectorStringIsStackEmpty"), (instructionVectorStringSort, "instructionVectorStringSort"), (instructionVectorStringSortReverse, "instructionVectorStringSortReverse"), (instructionVectorStringDupItems, "instructionVectorStringDupItems") @@ -518,7 +518,7 @@ allVectorBoolInstructions = map StateFunc [ (instructionVectorBoolYankDup, "instructionVectorBoolYankDup"), (instructionVectorBoolShove, "instructionVectorBoolShove"), (instructionVectorBoolShoveDup, "instructionVectorBoolShoveDup"), - (instructionVectorBoolStackIsEmpty, "instructionVectorBoolStackIsEmpty"), + (instructionVectorBoolIsStackEmpty, "instructionVectorBoolIsStackEmpty"), (instructionVectorBoolSort, "instructionVectorBoolSort"), (instructionVectorBoolSortReverse, "instructionVectorBoolSortReverse"), (instructionVectorBoolDupItems, "instructionVectorBoolDupItems") diff --git a/src/HushGP/Instructions/BoolInstructions.hs b/src/HushGP/Instructions/BoolInstructions.hs index eff7e5a..0d65609 100644 --- a/src/HushGP/Instructions/BoolInstructions.hs +++ b/src/HushGP/Instructions/BoolInstructions.hs @@ -75,8 +75,8 @@ instructionBoolShove state = instructionShove state bool instructionBoolShoveDup :: State -> State instructionBoolShoveDup state = instructionShoveDup state bool -instructionBoolIsEmpty :: State -> State -instructionBoolIsEmpty state = instructionIsEmpty state bool +instructionBoolIsStackEmpty :: State -> State +instructionBoolIsStackEmpty state = instructionIsStackEmpty state bool instructionBoolDupItems :: State -> State instructionBoolDupItems = instructionDupItems bool diff --git a/src/HushGP/Instructions/CharInstructions.hs b/src/HushGP/Instructions/CharInstructions.hs index f2f4864..ba40350 100644 --- a/src/HushGP/Instructions/CharInstructions.hs +++ b/src/HushGP/Instructions/CharInstructions.hs @@ -79,8 +79,8 @@ instructionCharYank state = instructionYank state char instructionCharYankDup :: State -> State instructionCharYankDup state = instructionYankDup state char -instructionCharIsEmpty :: State -> State -instructionCharIsEmpty state = instructionIsEmpty state char +instructionCharIsStackEmpty :: State -> State +instructionCharIsStackEmpty state = instructionIsStackEmpty state char instructionCharShove :: State -> State instructionCharShove state = instructionShove state char diff --git a/src/HushGP/Instructions/CodeInstructions.hs b/src/HushGP/Instructions/CodeInstructions.hs index 2078992..b24b093 100644 --- a/src/HushGP/Instructions/CodeInstructions.hs +++ b/src/HushGP/Instructions/CodeInstructions.hs @@ -294,8 +294,8 @@ instructionCodeYank state = instructionYank state code instructionCodeYankDup :: State -> State instructionCodeYankDup state = instructionYankDup state code -instructionCodeStackIsEmpty :: State -> State -instructionCodeStackIsEmpty state = instructionIsEmpty state code +instructionCodeIsStackEmpty :: State -> State +instructionCodeIsStackEmpty state = instructionIsStackEmpty state code instructionCodeShove :: State -> State instructionCodeShove state = instructionShove state code diff --git a/src/HushGP/Instructions/ExecInstructions.hs b/src/HushGP/Instructions/ExecInstructions.hs index e4ced54..2234aea 100644 --- a/src/HushGP/Instructions/ExecInstructions.hs +++ b/src/HushGP/Instructions/ExecInstructions.hs @@ -47,8 +47,8 @@ instructionExecShove state = instructionShove state exec instructionExecShoveDup :: State -> State instructionExecShoveDup state = instructionShoveDup state exec -instructionExecIsEmpty :: State -> State -instructionExecIsEmpty state = instructionIsEmpty state exec +instructionExecIsStackEmpty :: State -> State +instructionExecIsStackEmpty state = instructionIsStackEmpty state exec execDoRange :: Gene execDoRange = StateFunc (instructionExecDoRange, "instructionExecDoRange") diff --git a/src/HushGP/Instructions/FloatInstructions.hs b/src/HushGP/Instructions/FloatInstructions.hs index 6b27896..a9984f2 100644 --- a/src/HushGP/Instructions/FloatInstructions.hs +++ b/src/HushGP/Instructions/FloatInstructions.hs @@ -112,8 +112,8 @@ instructionFloatShoveDup state = instructionShoveDup state float instructionFloatShove :: State -> State instructionFloatShove state = instructionShove state float -instructionFloatIsEmpty :: State -> State -instructionFloatIsEmpty state = instructionIsEmpty state float +instructionFloatIsStackEmpty :: State -> State +instructionFloatIsStackEmpty state = instructionIsStackEmpty state float instructionFloatSin :: State -> State instructionFloatSin state@(State {_float = f1 : fs}) = state {_float = sin f1 : fs} diff --git a/src/HushGP/Instructions/GenericInstructions.hs b/src/HushGP/Instructions/GenericInstructions.hs index cf6479c..1c2ca5d 100644 --- a/src/HushGP/Instructions/GenericInstructions.hs +++ b/src/HushGP/Instructions/GenericInstructions.hs @@ -96,8 +96,8 @@ instructionDup state accessor = instructionPop :: State -> Lens' State [a] -> State instructionPop state accessor = state & accessor .~ drop 1 (view accessor state) -instructionIsEmpty :: State -> Lens' State [a] -> State -instructionIsEmpty state@(State {_bool = bs}) accessor = state{_bool = null (view accessor state) : bs} +instructionIsStackEmpty :: State -> Lens' State [a] -> State +instructionIsStackEmpty state@(State {_bool = bs}) accessor = state{_bool = null (view accessor state) : bs} -- instructionPop :: State -> Lens' State [a] -> State -- instructionPop state accessor = if notEmptyStack state accessor then instructionPop state accessor else state diff --git a/src/HushGP/Instructions/IntInstructions.hs b/src/HushGP/Instructions/IntInstructions.hs index ea3877b..cf6019b 100644 --- a/src/HushGP/Instructions/IntInstructions.hs +++ b/src/HushGP/Instructions/IntInstructions.hs @@ -112,8 +112,8 @@ instructionIntShove state = instructionShove state int instructionIntShoveDup :: State -> State instructionIntShoveDup state = instructionShoveDup state int -instructionIntIsEmpty :: State -> State -instructionIntIsEmpty state = instructionIsEmpty state int +instructionIntIsStackEmpty :: State -> State +instructionIntIsStackEmpty state = instructionIsStackEmpty state int instructionIntDupItems :: State -> State instructionIntDupItems = instructionDupItems int diff --git a/src/HushGP/Instructions/StringInstructions.hs b/src/HushGP/Instructions/StringInstructions.hs index 59d492a..47eaed0 100644 --- a/src/HushGP/Instructions/StringInstructions.hs +++ b/src/HushGP/Instructions/StringInstructions.hs @@ -224,8 +224,8 @@ instructionStringYank state = instructionYank state string instructionStringYankDup :: State -> State instructionStringYankDup state = instructionYankDup state string -instructionStringIsEmpty :: State -> State -instructionStringIsEmpty state = instructionIsEmpty state string +instructionStringIsStackEmpty :: State -> State +instructionStringIsStackEmpty state = instructionIsStackEmpty state string instructionStringShove :: State -> State instructionStringShove state = instructionShove state string diff --git a/src/HushGP/Instructions/VectorBoolInstructions.hs b/src/HushGP/Instructions/VectorBoolInstructions.hs index 2a07ea2..a8ce980 100644 --- a/src/HushGP/Instructions/VectorBoolInstructions.hs +++ b/src/HushGP/Instructions/VectorBoolInstructions.hs @@ -96,8 +96,8 @@ instructionVectorBoolYank state = instructionYank state vectorBool instructionVectorBoolYankDup :: State -> State instructionVectorBoolYankDup state = instructionYankDup state vectorBool -instructionVectorBoolStackIsEmpty :: State -> State -instructionVectorBoolStackIsEmpty state = instructionIsEmpty state vectorBool +instructionVectorBoolIsStackEmpty :: State -> State +instructionVectorBoolIsStackEmpty state = instructionIsStackEmpty state vectorBool instructionVectorBoolShove :: State -> State instructionVectorBoolShove state = instructionShove state vectorBool diff --git a/src/HushGP/Instructions/VectorCharInstructions.hs b/src/HushGP/Instructions/VectorCharInstructions.hs index b84fef7..458cdda 100644 --- a/src/HushGP/Instructions/VectorCharInstructions.hs +++ b/src/HushGP/Instructions/VectorCharInstructions.hs @@ -96,8 +96,8 @@ instructionVectorCharYank state = instructionYank state vectorChar instructionVectorCharYankDup :: State -> State instructionVectorCharYankDup state = instructionYankDup state vectorChar -instructionVectorCharStackIsEmpty :: State -> State -instructionVectorCharStackIsEmpty state = instructionIsEmpty state vectorChar +instructionVectorCharIsStackEmpty :: State -> State +instructionVectorCharIsStackEmpty state = instructionIsStackEmpty state vectorChar instructionVectorCharShove :: State -> State instructionVectorCharShove state = instructionShove state vectorChar diff --git a/src/HushGP/Instructions/VectorFloatInstructions.hs b/src/HushGP/Instructions/VectorFloatInstructions.hs index da7e0de..424afea 100644 --- a/src/HushGP/Instructions/VectorFloatInstructions.hs +++ b/src/HushGP/Instructions/VectorFloatInstructions.hs @@ -96,8 +96,8 @@ instructionVectorFloatYank state = instructionYank state vectorFloat instructionVectorFloatYankDup :: State -> State instructionVectorFloatYankDup state = instructionYankDup state vectorFloat -instructionVectorFloatStackIsEmpty :: State -> State -instructionVectorFloatStackIsEmpty state = instructionIsEmpty state vectorFloat +instructionVectorFloatIsStackEmpty :: State -> State +instructionVectorFloatIsStackEmpty state = instructionIsStackEmpty state vectorFloat instructionVectorFloatShove :: State -> State instructionVectorFloatShove state = instructionShove state vectorFloat diff --git a/src/HushGP/Instructions/VectorIntInstructions.hs b/src/HushGP/Instructions/VectorIntInstructions.hs index 6ce893f..1f294cf 100644 --- a/src/HushGP/Instructions/VectorIntInstructions.hs +++ b/src/HushGP/Instructions/VectorIntInstructions.hs @@ -96,8 +96,8 @@ instructionVectorIntYank state = instructionYank state vectorChar instructionVectorIntYankDup :: State -> State instructionVectorIntYankDup state = instructionYankDup state vectorChar -instructionVectorIntStackIsEmpty :: State -> State -instructionVectorIntStackIsEmpty state = instructionIsEmpty state vectorChar +instructionVectorIntIsStackEmpty :: State -> State +instructionVectorIntIsStackEmpty state = instructionIsStackEmpty state vectorChar instructionVectorIntShove :: State -> State instructionVectorIntShove state = instructionShove state vectorChar diff --git a/src/HushGP/Instructions/VectorStringInstructions.hs b/src/HushGP/Instructions/VectorStringInstructions.hs index 0914d87..db68782 100644 --- a/src/HushGP/Instructions/VectorStringInstructions.hs +++ b/src/HushGP/Instructions/VectorStringInstructions.hs @@ -96,8 +96,8 @@ instructionVectorStringYank state = instructionYank state vectorString instructionVectorStringYankDup :: State -> State instructionVectorStringYankDup state = instructionYankDup state vectorString -instructionVectorStringStackIsEmpty :: State -> State -instructionVectorStringStackIsEmpty state = instructionIsEmpty state vectorString +instructionVectorStringIsStackEmpty :: State -> State +instructionVectorStringIsStackEmpty state = instructionIsStackEmpty state vectorString instructionVectorStringShove :: State -> State instructionVectorStringShove state = instructionShove state vectorString From ff31a8fa350bfd3a693180fce996fba7e6c72c70 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Fri, 7 Feb 2025 15:21:17 -0600 Subject: [PATCH 124/171] move the state parameter to final position in all functions --- TODO.md | 2 +- src/HushGP/Instructions/BoolInstructions.hs | 26 +-- src/HushGP/Instructions/CharInstructions.hs | 32 ++-- src/HushGP/Instructions/CodeInstructions.hs | 51 +++-- src/HushGP/Instructions/ExecInstructions.hs | 26 +-- src/HushGP/Instructions/FloatInstructions.hs | 26 +-- .../Instructions/GenericInstructions.hs | 175 +++++++++--------- src/HushGP/Instructions/IntInstructions.hs | 26 +-- src/HushGP/Instructions/StringInstructions.hs | 56 +++--- .../Instructions/VectorBoolInstructions.hs | 68 +++---- .../Instructions/VectorCharInstructions.hs | 68 +++---- .../Instructions/VectorFloatInstructions.hs | 68 +++---- .../Instructions/VectorIntInstructions.hs | 68 +++---- .../Instructions/VectorStringInstructions.hs | 68 +++---- 14 files changed, 375 insertions(+), 385 deletions(-) diff --git a/TODO.md b/TODO.md index 3ed2d12..e0bb48e 100644 --- a/TODO.md +++ b/TODO.md @@ -11,7 +11,7 @@ - [X] Rename Logical to Bool - [X] Make int yank, shove, yankdup, and shovedup generic - [ ] Write hackage documentation for each function -- [ ] Refactor all functions to take state as the final parameter +- [X] Refactor all functions to take state as the final parameter - [ ] Standardize the pattern matching parameter names, such as c1 : cs - [ ] Write unit/quickcheck tests for all of the instructions diff --git a/src/HushGP/Instructions/BoolInstructions.hs b/src/HushGP/Instructions/BoolInstructions.hs index 0d65609..0a48136 100644 --- a/src/HushGP/Instructions/BoolInstructions.hs +++ b/src/HushGP/Instructions/BoolInstructions.hs @@ -40,43 +40,43 @@ instructionBoolXor :: State -> State instructionBoolXor = boolTemplate xor instructionBoolPop :: State -> State -instructionBoolPop state = instructionPop state bool +instructionBoolPop = instructionPop bool instructionBoolDup :: State -> State -instructionBoolDup state = instructionDup state bool +instructionBoolDup = instructionDup bool instructionBoolDupN :: State -> State -instructionBoolDupN state = instructionDupN state bool +instructionBoolDupN = instructionDupN bool instructionBoolSwap :: State -> State -instructionBoolSwap state = instructionSwap state bool +instructionBoolSwap = instructionSwap bool instructionBoolRot :: State -> State -instructionBoolRot state = instructionRot state bool +instructionBoolRot = instructionRot bool instructionBoolFlush :: State -> State -instructionBoolFlush state = instructionFlush state bool +instructionBoolFlush = instructionFlush bool instructionBoolEq :: State -> State -instructionBoolEq state = instructionEq state bool +instructionBoolEq = instructionEq bool instructionBoolStackDepth :: State -> State -instructionBoolStackDepth state = instructionStackDepth state bool +instructionBoolStackDepth = instructionStackDepth bool instructionBoolYank :: State -> State -instructionBoolYank state = instructionYank state bool +instructionBoolYank = instructionYank bool instructionBoolYankDup :: State -> State -instructionBoolYankDup state = instructionYankDup state bool +instructionBoolYankDup = instructionYankDup bool instructionBoolShove :: State -> State -instructionBoolShove state = instructionShove state bool +instructionBoolShove = instructionShove bool instructionBoolShoveDup :: State -> State -instructionBoolShoveDup state = instructionShoveDup state bool +instructionBoolShoveDup = instructionShoveDup bool instructionBoolIsStackEmpty :: State -> State -instructionBoolIsStackEmpty state = instructionIsStackEmpty state bool +instructionBoolIsStackEmpty = instructionIsStackEmpty bool instructionBoolDupItems :: State -> State instructionBoolDupItems = instructionDupItems bool diff --git a/src/HushGP/Instructions/CharInstructions.hs b/src/HushGP/Instructions/CharInstructions.hs index ba40350..79c0e00 100644 --- a/src/HushGP/Instructions/CharInstructions.hs +++ b/src/HushGP/Instructions/CharInstructions.hs @@ -13,13 +13,13 @@ instructionCharConcat state@(State {_char = c1 : c2 : cs, _string = ss}) = state instructionCharConcat state = state instructionCharFromFirstChar :: State -> State -instructionCharFromFirstChar state = instructionVectorFirst state char string +instructionCharFromFirstChar = instructionVectorFirst char string instructionCharFromLastChar :: State -> State -instructionCharFromLastChar state = instructionVectorLast state char string +instructionCharFromLastChar = instructionVectorLast char string instructionCharFromNthChar :: State -> State -instructionCharFromNthChar state = instructionVectorNth state char string +instructionCharFromNthChar = instructionVectorNth char string instructionCharIsWhitespace :: State -> State instructionCharIsWhitespace state@(State {_char = c1 : cs, _bool = bs}) = state{_char = cs, _bool = (c1 `elem` wschars) : bs} @@ -50,43 +50,43 @@ instructionCharsFromString state@(State {_char = cs, _string = s1 : ss}) = state instructionCharsFromString state = state instructionCharPop :: State -> State -instructionCharPop state = instructionPop state char +instructionCharPop = instructionPop char instructionCharDup :: State -> State -instructionCharDup state = instructionDup state char +instructionCharDup = instructionDup char instructionCharDupN :: State -> State -instructionCharDupN state = instructionDupN state char +instructionCharDupN = instructionDupN char instructionCharSwap :: State -> State -instructionCharSwap state = instructionSwap state char +instructionCharSwap = instructionSwap char instructionCharRot :: State -> State -instructionCharRot state = instructionRot state char +instructionCharRot = instructionRot char instructionCharFlush :: State -> State -instructionCharFlush state = instructionFlush state char +instructionCharFlush = instructionFlush char instructionCharEq :: State -> State -instructionCharEq state = instructionEq state char +instructionCharEq = instructionEq char instructionCharStackDepth :: State -> State -instructionCharStackDepth state = instructionStackDepth state char +instructionCharStackDepth = instructionStackDepth char instructionCharYank :: State -> State -instructionCharYank state = instructionYank state char +instructionCharYank = instructionYank char instructionCharYankDup :: State -> State -instructionCharYankDup state = instructionYankDup state char +instructionCharYankDup = instructionYankDup char instructionCharIsStackEmpty :: State -> State -instructionCharIsStackEmpty state = instructionIsStackEmpty state char +instructionCharIsStackEmpty = instructionIsStackEmpty char instructionCharShove :: State -> State -instructionCharShove state = instructionShove state char +instructionCharShove = instructionShove char instructionCharShoveDup :: State -> State -instructionCharShoveDup state = instructionShoveDup state char +instructionCharShoveDup = instructionShoveDup char instructionCharDupItems :: State -> State instructionCharDupItems = instructionDupItems char diff --git a/src/HushGP/Instructions/CodeInstructions.hs b/src/HushGP/Instructions/CodeInstructions.hs index b24b093..ebe5f00 100644 --- a/src/HushGP/Instructions/CodeInstructions.hs +++ b/src/HushGP/Instructions/CodeInstructions.hs @@ -83,7 +83,7 @@ codeRecursiveSize (Block xs) = sum [codeRecursiveSize x + if isBlock x then 1 el codeRecursiveSize _ = 1 instructionCodePop :: State -> State -instructionCodePop state = instructionPop state code +instructionCodePop = instructionPop code instructionCodeIsCodeBlock :: State -> State instructionCodeIsCodeBlock state@(State {_code = (c : cs), _bool = bs}) = state {_code = cs, _bool = isBlock c : bs} @@ -221,9 +221,6 @@ instructionCodeSize :: State -> State instructionCodeSize state@(State {_code = c1 : cs, _int = is}) = state{_code = cs, _int = codeRecursiveSize c1 : is} instructionCodeSize state = state --- instructionCodeContainer :: State -> State --- instructionCodeContainer - -- There's a bug for this instruction in pysh where the last item in the -- top level Block isn't counted, and if passed 0, then the entire codeblock is returned. -- I designed this function differently so 0 returns the 0th element, and the last item @@ -268,73 +265,73 @@ instructionCodeReverse state@(State {_code = (Block c1) : cs}) = state {_code = instructionCodeReverse state = state instructionCodeDup :: State -> State -instructionCodeDup state = instructionDup state code +instructionCodeDup = instructionDup code instructionCodeDupN :: State -> State -instructionCodeDupN state = instructionDupN state code +instructionCodeDupN = instructionDupN code instructionCodeSwap :: State -> State -instructionCodeSwap state = instructionSwap state code +instructionCodeSwap = instructionSwap code instructionCodeRot :: State -> State -instructionCodeRot state = instructionRot state code +instructionCodeRot = instructionRot code instructionCodeFlush :: State -> State -instructionCodeFlush state = instructionFlush state code +instructionCodeFlush = instructionFlush code instructionCodeEq :: State -> State -instructionCodeEq state = instructionEq state code +instructionCodeEq = instructionEq code instructionCodeStackDepth :: State -> State -instructionCodeStackDepth state = instructionStackDepth state code +instructionCodeStackDepth = instructionStackDepth code instructionCodeYank :: State -> State -instructionCodeYank state = instructionYank state code +instructionCodeYank = instructionYank code instructionCodeYankDup :: State -> State -instructionCodeYankDup state = instructionYankDup state code +instructionCodeYankDup = instructionYankDup code instructionCodeIsStackEmpty :: State -> State -instructionCodeIsStackEmpty state = instructionIsStackEmpty state code +instructionCodeIsStackEmpty = instructionIsStackEmpty code instructionCodeShove :: State -> State -instructionCodeShove state = instructionShove state code +instructionCodeShove = instructionShove code instructionCodeShoveDup :: State -> State -instructionCodeShoveDup state = instructionShoveDup state code +instructionCodeShoveDup = instructionShoveDup code instructionCodeFromBool :: State -> State -instructionCodeFromBool state = instructionCodeFrom state bool GeneBool +instructionCodeFromBool = instructionCodeFrom bool GeneBool instructionCodeFromInt :: State -> State -instructionCodeFromInt state = instructionCodeFrom state int GeneInt +instructionCodeFromInt = instructionCodeFrom int GeneInt instructionCodeFromChar :: State -> State -instructionCodeFromChar state = instructionCodeFrom state char GeneChar +instructionCodeFromChar = instructionCodeFrom char GeneChar instructionCodeFromFloat :: State -> State -instructionCodeFromFloat state = instructionCodeFrom state float GeneFloat +instructionCodeFromFloat = instructionCodeFrom float GeneFloat instructionCodeFromString :: State -> State -instructionCodeFromString state = instructionCodeFrom state string GeneString +instructionCodeFromString = instructionCodeFrom string GeneString instructionCodeFromVectorInt :: State -> State -instructionCodeFromVectorInt state = instructionCodeFrom state vectorInt GeneVectorInt +instructionCodeFromVectorInt = instructionCodeFrom vectorInt GeneVectorInt instructionCodeFromVectorFloat :: State -> State -instructionCodeFromVectorFloat state = instructionCodeFrom state vectorFloat GeneVectorFloat +instructionCodeFromVectorFloat = instructionCodeFrom vectorFloat GeneVectorFloat instructionCodeFromVectorString :: State -> State -instructionCodeFromVectorString state = instructionCodeFrom state vectorString GeneVectorString +instructionCodeFromVectorString = instructionCodeFrom vectorString GeneVectorString instructionCodeFromVectorBool :: State -> State -instructionCodeFromVectorBool state = instructionCodeFrom state vectorBool GeneVectorBool +instructionCodeFromVectorBool = instructionCodeFrom vectorBool GeneVectorBool instructionCodeFromVectorChar :: State -> State -instructionCodeFromVectorChar state = instructionCodeFrom state vectorChar GeneVectorChar +instructionCodeFromVectorChar = instructionCodeFrom vectorChar GeneVectorChar instructionCodeFromExec :: State -> State -instructionCodeFromExec state = instructionCodeFrom state exec id +instructionCodeFromExec = instructionCodeFrom exec id instructionCodeContainer :: State -> State instructionCodeContainer state@(State {_code = c1 : c2 : cs}) = state {_code = findContainer c1 c2 : cs} diff --git a/src/HushGP/Instructions/ExecInstructions.hs b/src/HushGP/Instructions/ExecInstructions.hs index 2234aea..703c783 100644 --- a/src/HushGP/Instructions/ExecInstructions.hs +++ b/src/HushGP/Instructions/ExecInstructions.hs @@ -12,43 +12,43 @@ instructionExecIf state@(State {_exec = (e1 : e2 : es), _bool = (b : bs)}) = instructionExecIf state = state instructionExecDup :: State -> State -instructionExecDup state = instructionDup state exec +instructionExecDup = instructionDup exec instructionExecDupN :: State -> State -instructionExecDupN state = instructionDupN state exec +instructionExecDupN = instructionDupN exec instructionExecPop :: State -> State -instructionExecPop state = instructionPop state exec +instructionExecPop = instructionPop exec instructionExecSwap :: State -> State -instructionExecSwap state = instructionSwap state exec +instructionExecSwap = instructionSwap exec instructionExecRot :: State -> State -instructionExecRot state = instructionRot state exec +instructionExecRot = instructionRot exec instructionExecFlush :: State -> State -instructionExecFlush state = instructionFlush state exec +instructionExecFlush = instructionFlush exec instructionExecEq :: State -> State -instructionExecEq state = instructionEq state exec +instructionExecEq = instructionEq exec instructionExecStackDepth :: State -> State -instructionExecStackDepth state = instructionStackDepth state exec +instructionExecStackDepth = instructionStackDepth exec instructionExecYank :: State -> State -instructionExecYank state = instructionYank state exec +instructionExecYank = instructionYank exec instructionExecYankDup :: State -> State -instructionExecYankDup state = instructionYankDup state exec +instructionExecYankDup = instructionYankDup exec instructionExecShove :: State -> State -instructionExecShove state = instructionShove state exec +instructionExecShove = instructionShove exec instructionExecShoveDup :: State -> State -instructionExecShoveDup state = instructionShoveDup state exec +instructionExecShoveDup = instructionShoveDup exec instructionExecIsStackEmpty :: State -> State -instructionExecIsStackEmpty state = instructionIsStackEmpty state exec +instructionExecIsStackEmpty = instructionIsStackEmpty exec execDoRange :: Gene execDoRange = StateFunc (instructionExecDoRange, "instructionExecDoRange") diff --git a/src/HushGP/Instructions/FloatInstructions.hs b/src/HushGP/Instructions/FloatInstructions.hs index a9984f2..ee4bb14 100644 --- a/src/HushGP/Instructions/FloatInstructions.hs +++ b/src/HushGP/Instructions/FloatInstructions.hs @@ -77,43 +77,43 @@ instructionFloatGTE state@(State {_float = f1 : f2 : fs, _bool = bs}) = state {_ instructionFloatGTE state = state instructionFloatPop :: State -> State -instructionFloatPop state = instructionPop state float +instructionFloatPop = instructionPop float instructionFloatDup :: State -> State -instructionFloatDup state = instructionDup state float +instructionFloatDup = instructionDup float instructionFloatDupN :: State -> State -instructionFloatDupN state = instructionDupN state float +instructionFloatDupN = instructionDupN float instructionFloatSwap :: State -> State -instructionFloatSwap state = instructionSwap state float +instructionFloatSwap = instructionSwap float instructionFloatRot :: State -> State -instructionFloatRot state = instructionRot state float +instructionFloatRot = instructionRot float instructionFloatFlush :: State -> State -instructionFloatFlush state = instructionFlush state float +instructionFloatFlush = instructionFlush float instructionFloatEq :: State -> State -instructionFloatEq state = instructionEq state float +instructionFloatEq = instructionEq float instructionFloatStackDepth :: State -> State -instructionFloatStackDepth state = instructionStackDepth state float +instructionFloatStackDepth = instructionStackDepth float instructionFloatYankDup :: State -> State -instructionFloatYankDup state = instructionYankDup state float +instructionFloatYankDup = instructionYankDup float instructionFloatYank :: State -> State -instructionFloatYank state = instructionYank state float +instructionFloatYank = instructionYank float instructionFloatShoveDup :: State -> State -instructionFloatShoveDup state = instructionShoveDup state float +instructionFloatShoveDup = instructionShoveDup float instructionFloatShove :: State -> State -instructionFloatShove state = instructionShove state float +instructionFloatShove = instructionShove float instructionFloatIsStackEmpty :: State -> State -instructionFloatIsStackEmpty state = instructionIsStackEmpty state float +instructionFloatIsStackEmpty = instructionIsStackEmpty float instructionFloatSin :: State -> State instructionFloatSin state@(State {_float = f1 : fs}) = state {_float = sin f1 : fs} diff --git a/src/HushGP/Instructions/GenericInstructions.hs b/src/HushGP/Instructions/GenericInstructions.hs index 1c2ca5d..37d23fd 100644 --- a/src/HushGP/Instructions/GenericInstructions.hs +++ b/src/HushGP/Instructions/GenericInstructions.hs @@ -84,28 +84,25 @@ safeInit xs = init xs absNum :: Integral a => a -> [b] -> Int absNum rawNum lst = abs (fromIntegral rawNum) `mod` length lst -notEmptyStack :: State -> Lens' State [a] -> Bool -notEmptyStack state accessor = not . null $ view accessor state +notEmptyStack :: Lens' State [a] -> State -> Bool +notEmptyStack accessor state = not . null $ view accessor state -instructionDup :: State -> Lens' State [a] -> State -instructionDup state accessor = +instructionDup :: Lens' State [a] -> State -> State +instructionDup accessor state = case uncons (view accessor state) of Nothing -> state Just (x,_) -> state & accessor .~ x : view accessor state -instructionPop :: State -> Lens' State [a] -> State -instructionPop state accessor = state & accessor .~ drop 1 (view accessor state) +instructionPop :: Lens' State [a] -> State -> State +instructionPop accessor state = state & accessor .~ drop 1 (view accessor state) -instructionIsStackEmpty :: State -> Lens' State [a] -> State -instructionIsStackEmpty state@(State {_bool = bs}) accessor = state{_bool = null (view accessor state) : bs} - --- instructionPop :: State -> Lens' State [a] -> State --- instructionPop state accessor = if notEmptyStack state accessor then instructionPop state accessor else state +instructionIsStackEmpty :: Lens' State [a] -> State -> State +instructionIsStackEmpty accessor state@(State {_bool = bs}) = state{_bool = null (view accessor state) : bs} -- I might be able to move some of the int stack error checking -- to the integer call. For now this may be a tad inefficient. -instructionDupN :: forall a. State -> Lens' State [a] -> State -instructionDupN state accessor = +instructionDupN :: forall a. Lens' State [a] -> State -> State +instructionDupN accessor state = case uncons (view int state) of Just (i1,is) -> case uncons (view accessor state{_int = is}) of @@ -129,8 +126,8 @@ instructionDupItems accessor state@(State {_int = i1 : is}) = else state{_int = is} & accessor .~ (take i1 (view accessor state{_int = is}) <> view accessor state{_int = is}) instructionDupItems _ state = state -instructionSwap :: State -> Lens' State [a] -> State -instructionSwap state accessor = +instructionSwap :: Lens' State [a] -> State -> State +instructionSwap accessor state = state & accessor .~ swapper (view accessor state) where swapper :: [a] -> [a] @@ -140,19 +137,19 @@ instructionSwap state accessor = -- Rotates top 3 integers -- We could use template haskell to rotate any number of these as -- an instruction later. Template haskell seems very complicated tho. -instructionRot :: State -> Lens' State [a] -> State -instructionRot state accessor = +instructionRot :: Lens' State [a] -> State -> State +instructionRot accessor state = state & accessor .~ rotator (view accessor state) where rotator :: [a] -> [a] rotator (x1 : x2 : x3 : xs) = x3 : x1 : x2 : xs rotator xs = xs -instructionFlush :: State -> Lens' State [a] -> State -instructionFlush state accessor = state & accessor .~ [] +instructionFlush :: Lens' State [a] -> State -> State +instructionFlush accessor state = state & accessor .~ [] -instructionEq :: forall a. Eq a => State -> Lens' State [a] -> State -instructionEq state accessor = +instructionEq :: forall a. Eq a => Lens' State [a] -> State -> State +instructionEq accessor state = case uncons $ view accessor state of Nothing -> state Just (x1, x2 : _) -> droppedState & bool .~ (x1 == x2) : view bool droppedState @@ -161,18 +158,18 @@ instructionEq state accessor = droppedState :: State droppedState = state & accessor .~ drop 2 (view accessor state) -instructionStackDepth :: State -> Lens' State [a] -> State -instructionStackDepth state@(State {_int = is}) accessor = state{_int = length (view accessor state) : is} +instructionStackDepth :: Lens' State [a] -> State -> State +instructionStackDepth accessor state@(State {_int = is}) = state{_int = length (view accessor state) : is} -instructionYankDup :: State -> Lens' State [a] -> State -instructionYankDup state@(State {_int = i : is}) accessor = - if notEmptyStack state accessor +instructionYankDup :: Lens' State [a] -> State -> State +instructionYankDup accessor state@(State {_int = i : is}) = + if notEmptyStack accessor state then state{_int = is} & accessor .~ (view accessor state{_int = is} !! max 0 (min i (length (view accessor state{_int = is}) - 1))) : view accessor state{_int = is} else state -instructionYankDup state _ = state +instructionYankDup _ state = state -instructionYank :: forall a. State -> Lens' State [a] -> State -instructionYank state@(State {_int = i : is}) accessor = +instructionYank :: forall a. Lens' State [a] -> State -> State +instructionYank accessor state@(State {_int = i : is}) = let myIndex :: Int myIndex = max 0 (min i (length (view accessor state{_int = is}) - 1)) @@ -181,25 +178,25 @@ instructionYank state@(State {_int = i : is}) accessor = deletedState :: State deletedState = state{_int = is} & accessor .~ deleteAt myIndex (view accessor state{_int = is}) in - if notEmptyStack state{_int = is} accessor then deletedState & accessor .~ item : view accessor deletedState else state -instructionYank state _ = state + if notEmptyStack accessor state{_int = is} then deletedState & accessor .~ item : view accessor deletedState else state +instructionYank _ state = state -- In pysh, instructionShoveDup and instructionShove behave differently when indexing in such a way that -- the duplicated index matters whether or not it's present in the stack at the moment of calculation. -- I'm not going to keep this behavior. Check out interpysh examples for how pysh handles it. -instructionShoveDup :: State -> Lens' State [a] -> State -instructionShoveDup state@(State {_int = i : is}) accessor = +instructionShoveDup :: Lens' State [a] -> State -> State +instructionShoveDup accessor state@(State {_int = i : is}) = case uncons (view accessor state{_int = is}) of Just (x,_) -> state{_int = is} & accessor .~ combineTuple x (splitAt (max 0 (min i (length (view accessor state{_int = is}) - 1))) (view accessor state{_int = is})) _ -> state -instructionShoveDup state _ = state +instructionShoveDup _ state = state -instructionShove :: State -> Lens' State [a] -> State -instructionShove state accessor = instructionShoveDup state accessor & accessor .~ drop 1 (view accessor (instructionShoveDup state accessor)) +instructionShove :: Lens' State [a] -> State -> State +instructionShove accessor state = instructionShoveDup accessor state & accessor .~ drop 1 (view accessor (instructionShoveDup accessor state )) -- not char generic -instructionConcat :: Semigroup a => State -> Lens' State [a] -> State -instructionConcat state accessor = +instructionConcat :: Semigroup a => Lens' State [a] -> State -> State +instructionConcat accessor state = case uncons (view accessor state) of Just (x1, x2:_) -> droppedState & accessor .~ (x1 <> x2) : view accessor droppedState _ -> state @@ -207,12 +204,8 @@ instructionConcat state accessor = droppedState :: State droppedState = state & accessor .~ drop 2 (view accessor state) --- evolve fodder??????????? -instructionNoOp :: State -> State -instructionNoOp state = state - -instructionConj :: State -> Lens' State [a] -> Lens' State [[a]] -> State -instructionConj state primAccessor vectorAccessor = +instructionConj :: Lens' State [a] -> Lens' State [[a]] -> State -> State +instructionConj primAccessor vectorAccessor state = case (uncons (view primAccessor state), uncons (view vectorAccessor state)) of (Just (p1,ps), Just (v1,vs)) -> state & primAccessor .~ ps & vectorAccessor .~ ((p1 : v1) : vs) _ -> state @@ -225,22 +218,22 @@ instructionConjEnd primAccessor vectorAccessor state = -- v for vector, vs for vectorstack (also applicable to strings) -- Could abstract this unconsing even further in all functions below -instructionTakeN :: State -> Lens' State [[a]] -> State -instructionTakeN state@(State {_int = i1 : is}) accessor = +instructionTakeN :: Lens' State [[a]] -> State -> State +instructionTakeN accessor state@(State {_int = i1 : is}) = case uncons (view accessor state) of Just (v1, vs) -> state{_int = is} & accessor .~ (take (absNum i1 v1) v1 : vs) _ -> state -instructionTakeN state _ = state +instructionTakeN _ state = state -instructionSubVector :: State -> Lens' State [[a]] -> State -instructionSubVector state@(State {_int = i1 : i2 : is}) accessor = +instructionSubVector :: Lens' State [[a]] -> State -> State +instructionSubVector accessor state@(State {_int = i1 : i2 : is}) = case uncons (view accessor state) of Just (v1, vs) -> state{_int = is} & accessor .~ (subList i1 i2 v1 : vs) _ -> state -instructionSubVector state _ = state +instructionSubVector _ state = state -instructionVectorFirst :: State -> Lens' State [a] -> Lens' State [[a]] -> State -instructionVectorFirst state primAccessor vectorAccessor = +instructionVectorFirst :: Lens' State [a] -> Lens' State [[a]] -> State -> State +instructionVectorFirst primAccessor vectorAccessor state = case uncons (view vectorAccessor state) of Just (v1, vs) -> case uncons v1 of @@ -248,8 +241,8 @@ instructionVectorFirst state primAccessor vectorAccessor = _ -> state _ -> state -instructionVectorLast :: State -> Lens' State [a] -> Lens' State [[a]] -> State -instructionVectorLast state primAccessor vectorAccessor = +instructionVectorLast :: Lens' State [a] -> Lens' State [[a]] -> State -> State +instructionVectorLast primAccessor vectorAccessor state = case uncons (view vectorAccessor state) of Just (v1, vs) -> case uncons (drop (length v1 - 1) v1) of -- gonna keep this implementation over using last as this can't error @@ -257,67 +250,67 @@ instructionVectorLast state primAccessor vectorAccessor = _ -> state _ -> state -instructionVectorNth :: State -> Lens' State [a] -> Lens' State [[a]] -> State -instructionVectorNth state@(State {_int = i1 : is}) primAccessor vectorAccessor = +instructionVectorNth :: Lens' State [a] -> Lens' State [[a]] -> State -> State +instructionVectorNth primAccessor vectorAccessor state@(State {_int = i1 : is}) = case uncons (view vectorAccessor state) of Just (v1, vs) -> state{_int = is} & primAccessor .~ (v1 !! absNum i1 v1 : view primAccessor state{_int = is}) & vectorAccessor .~ vs _ -> state -instructionVectorNth state _ _ = state +instructionVectorNth _ _ state= state -instructionRest :: State -> Lens' State [[a]] -> State -instructionRest state accessor = +instructionRest :: Lens' State [[a]] -> State -> State +instructionRest accessor state = case uncons (view accessor state) of Just (v1, vs) -> state & accessor .~ (drop 1 v1 : vs) _ -> state -instructionButLast :: State -> Lens' State [[a]] -> State -instructionButLast state accessor = +instructionButLast :: Lens' State [[a]] -> State -> State +instructionButLast accessor state = case uncons (view accessor state) of Just (v1, vs) -> state & accessor .~ (safeInit v1 : vs) _ -> state -instructionLength :: State -> Lens' State [[a]] -> State -instructionLength state@(State {_int = is}) accessor = +instructionLength :: Lens' State [[a]] -> State -> State +instructionLength accessor state@(State {_int = is}) = case uncons (view accessor state) of Just (v1, vs) -> state{_int = length v1 : is} & accessor .~ vs _ -> state -instructionReverse :: State -> Lens' State [[a]] -> State -instructionReverse state accessor = +instructionReverse :: Lens' State [[a]] -> State -> State +instructionReverse accessor state = case uncons (view accessor state) of Just (v1, vs) -> state & accessor .~ (reverse v1 : vs) _ -> state -instructionPushAll :: State -> Lens' State [a] -> Lens' State [[a]] -> State -instructionPushAll state primAccessor vectorAccessor = +instructionPushAll :: Lens' State [a] -> Lens' State [[a]] -> State -> State +instructionPushAll primAccessor vectorAccessor state = case uncons (view vectorAccessor state) of Just (v1, vs) -> state & vectorAccessor .~ vs & primAccessor .~ (v1 <> view primAccessor state) _ -> state -instructionVectorMakeEmpty :: State -> Lens' State [[a]] -> State -instructionVectorMakeEmpty state accessor = state & accessor .~ ([] : view accessor state) +instructionVectorMakeEmpty :: Lens' State [[a]] -> State -> State +instructionVectorMakeEmpty accessor state = state & accessor .~ ([] : view accessor state) -instructionVectorIsEmpty :: State -> Lens' State [[a]] -> State -instructionVectorIsEmpty state@(State {_bool = bs}) accessor = +instructionVectorIsEmpty :: Lens' State [[a]] -> State -> State +instructionVectorIsEmpty accessor state@(State {_bool = bs}) = case uncons (view accessor state) of Just (v1, vs) -> state{_bool = null v1 : bs} & accessor .~ vs _ -> state -instructionVectorContains :: Eq a => State -> Lens' State [a] -> Lens' State [[a]] -> State -instructionVectorContains state@(State {_bool = bs}) primAccessor vectorAccessor = +instructionVectorContains :: Eq a => Lens' State [a] -> Lens' State [[a]] -> State -> State +instructionVectorContains primAccessor vectorAccessor state@(State {_bool = bs}) = case (uncons (view vectorAccessor state), uncons (view primAccessor state)) of (Just (v1, vs), Just (p1, ps)) -> state{_bool = (findSubA v1 [p1] /= -1) : bs} & vectorAccessor .~ vs & primAccessor .~ ps _ -> state -- I couldn't think of a better way of doing this -instructionVectorIndexOf :: Eq a => State -> Lens' State [a] -> Lens' State [[a]] -> State -instructionVectorIndexOf state primAccessor vectorAccessor = +instructionVectorIndexOf :: Eq a => Lens' State [a] -> Lens' State [[a]] -> State -> State +instructionVectorIndexOf primAccessor vectorAccessor state = case (uncons (view vectorAccessor state), uncons (view primAccessor state)) of (Just (v1, vs), Just (p1, ps)) -> (state & vectorAccessor .~ vs & primAccessor .~ ps) & int .~ (findSubA v1 [p1] : view int (state & vectorAccessor .~ vs & primAccessor .~ ps)) _ -> state -instructionVectorOccurrencesOf :: Eq a => State -> Lens' State [a] -> Lens' State [[a]] -> State -instructionVectorOccurrencesOf state primAccessor vectorAccessor = +instructionVectorOccurrencesOf :: Eq a => Lens' State [a] -> Lens' State [[a]] -> State -> State +instructionVectorOccurrencesOf primAccessor vectorAccessor state = case (uncons (view vectorAccessor state), uncons (view primAccessor state)) of (Just (v1, vs), Just (p1, ps)) -> (state & vectorAccessor .~ vs & primAccessor .~ ps) & int .~ (amtOccurences v1 [p1] : view int (state & vectorAccessor .~ vs & primAccessor .~ ps)) _ -> state @@ -330,33 +323,33 @@ instructionVectorParseToPrim accessor state = Just (x1, xs) -> state & accessor .~ (chunksOf 1 x1 <> xs) _ -> state -instructionVectorSetNth :: State -> Lens' State [a] -> Lens' State [[a]] -> State -instructionVectorSetNth state@(State {_int = i1 : is}) primAccessor vectorAccessor = +instructionVectorSetNth :: Lens' State [a] -> Lens' State [[a]] -> State -> State +instructionVectorSetNth primAccessor vectorAccessor state@(State {_int = i1 : is}) = case (uncons (view vectorAccessor state{_int = is}), uncons (view primAccessor state{_int = is})) of (Just (v1, vs), Just (p1, ps)) -> state{_int = is} & vectorAccessor .~ (replaceAt (absNum i1 v1) p1 v1 : vs) & primAccessor .~ ps _ -> state -instructionVectorSetNth state _ _ = state +instructionVectorSetNth _ _ state = state -instructionVectorReplace :: Eq a => State -> Lens' State [a] -> Lens' State [[a]] -> State -instructionVectorReplace state primAccessor vectorAccessor = +instructionVectorReplace :: Eq a => Lens' State [a] -> Lens' State [[a]] -> State -> State +instructionVectorReplace primAccessor vectorAccessor state = case (uncons (view vectorAccessor state), uncons (view primAccessor state)) of (Just (v1, vs), Just (p1, p2 : ps)) -> state & vectorAccessor .~ (replace v1 [p1] [p2] Nothing : vs) & primAccessor .~ ps _ -> state -instructionVectorReplaceFirst :: Eq a => State -> Lens' State [a] -> Lens' State [[a]] -> State -instructionVectorReplaceFirst state primAccessor vectorAccessor = +instructionVectorReplaceFirst :: Eq a => Lens' State [a] -> Lens' State [[a]] -> State -> State +instructionVectorReplaceFirst primAccessor vectorAccessor state = case (uncons (view vectorAccessor state), uncons (view primAccessor state)) of (Just (v1, vs), Just (p1, p2 : ps)) -> state & vectorAccessor .~ (replace v1 [p1] [p2] (Just 1) : vs) & primAccessor .~ ps _ -> state -instructionVectorRemove :: Eq a => State -> Lens' State [a] -> Lens' State [[a]] -> State -instructionVectorRemove state primAccessor vectorAccessor = +instructionVectorRemove :: Eq a => Lens' State [a] -> Lens' State [[a]] -> State -> State +instructionVectorRemove primAccessor vectorAccessor state = case (uncons (view vectorAccessor state), uncons (view primAccessor state)) of (Just (v1, vs), Just (p1, ps)) -> state & vectorAccessor .~ (replace v1 [p1] [] Nothing : vs) & primAccessor .~ ps _ -> state -instructionVectorIterate :: State -> Lens' State [a] -> Lens' State [[a]] -> ([a] -> Gene) -> (State -> State) -> String -> State -instructionVectorIterate state@(State {_exec = e1 : es}) primAccessor vectorAccessor vectorType typeIterateFunction typeIterateFunctionName = +instructionVectorIterate :: Lens' State [a] -> Lens' State [[a]] -> ([a] -> Gene) -> (State -> State) -> String -> State -> State +instructionVectorIterate primAccessor vectorAccessor vectorType typeIterateFunction typeIterateFunctionName state@(State {_exec = e1 : es}) = case uncons (view vectorAccessor state) of Just ([], vs) -> state{_exec = es} & vectorAccessor .~ vs Just ([x], vs) -> state & primAccessor .~ (x : view primAccessor state) & vectorAccessor .~ vs @@ -365,10 +358,10 @@ instructionVectorIterate state@(State {_exec = e1 : es}) primAccessor vectorAcce Just (nv1, nvs) -> state{_exec = e1 : vectorType nvs : StateFunc (typeIterateFunction, typeIterateFunctionName) : e1 : es} & primAccessor .~ (nv1 : view primAccessor state) & vectorAccessor .~ vs _ -> state) -- This should never happen _ -> state -instructionVectorIterate state _ _ _ _ _ = state +instructionVectorIterate _ _ _ _ _ state = state -instructionCodeFrom :: State -> Lens' State [a] -> (a -> Gene) -> State -instructionCodeFrom state@(State {_code = cs}) accessor geneType = +instructionCodeFrom :: Lens' State [a] -> (a -> Gene) -> State -> State +instructionCodeFrom accessor geneType state@(State {_code = cs}) = case uncons (view accessor state) of Just (x, xs) -> state{_code = geneType x : cs} & accessor .~ xs _ -> state diff --git a/src/HushGP/Instructions/IntInstructions.hs b/src/HushGP/Instructions/IntInstructions.hs index cf6019b..2418775 100644 --- a/src/HushGP/Instructions/IntInstructions.hs +++ b/src/HushGP/Instructions/IntInstructions.hs @@ -77,43 +77,43 @@ instructionIntGTE state@(State {_int = i1 : i2 : is, _bool = bs}) = state {_int instructionIntGTE state = state instructionIntDup :: State -> State -instructionIntDup state = instructionDup state int +instructionIntDup = instructionDup int instructionIntPop :: State -> State -instructionIntPop state = instructionPop state int +instructionIntPop = instructionPop int instructionIntDupN :: State -> State -instructionIntDupN state = instructionDupN state int +instructionIntDupN = instructionDupN int instructionIntSwap :: State -> State -instructionIntSwap state = instructionSwap state int +instructionIntSwap = instructionSwap int instructionIntRot :: State -> State -instructionIntRot state = instructionRot state int +instructionIntRot = instructionRot int instructionIntFlush :: State -> State -instructionIntFlush state = instructionFlush state int +instructionIntFlush = instructionFlush int instructionIntEq :: State -> State -instructionIntEq state = instructionEq state int +instructionIntEq = instructionEq int instructionIntStackDepth :: State -> State -instructionIntStackDepth state = instructionStackDepth state int +instructionIntStackDepth = instructionStackDepth int instructionIntYank :: State -> State -instructionIntYank state = instructionYank state int +instructionIntYank = instructionYank int instructionIntYankDup :: State -> State -instructionIntYankDup state = instructionYankDup state int +instructionIntYankDup = instructionYankDup int instructionIntShove :: State -> State -instructionIntShove state = instructionShove state int +instructionIntShove = instructionShove int instructionIntShoveDup :: State -> State -instructionIntShoveDup state = instructionShoveDup state int +instructionIntShoveDup = instructionShoveDup int instructionIntIsStackEmpty :: State -> State -instructionIntIsStackEmpty state = instructionIsStackEmpty state int +instructionIntIsStackEmpty = instructionIsStackEmpty int instructionIntDupItems :: State -> State instructionIntDupItems = instructionDupItems int diff --git a/src/HushGP/Instructions/StringInstructions.hs b/src/HushGP/Instructions/StringInstructions.hs index 47eaed0..0d0caff 100644 --- a/src/HushGP/Instructions/StringInstructions.hs +++ b/src/HushGP/Instructions/StringInstructions.hs @@ -24,10 +24,10 @@ rstrip :: String -> String rstrip = reverse . lstrip . reverse instructionStringConcat :: State -> State -instructionStringConcat state = instructionConcat state string +instructionStringConcat = instructionConcat string instructionStringSwap :: State -> State -instructionStringSwap state = instructionSwap state string +instructionStringSwap = instructionSwap string instructionStringInsertString :: State -> State instructionStringInsertString state@(State{_string = s1 : s2 : ss, _int = i1 : is}) = state {_string = combineTupleList s2 (splitAt i1 s1) : ss, _int = is} @@ -94,24 +94,24 @@ instructionStringInsertChar state@(State {_string = s1 : ss, _char = c1 : cs, _i instructionStringInsertChar state = state instructionStringContainsChar :: State -> State -instructionStringContainsChar state = instructionVectorContains state char string +instructionStringContainsChar = instructionVectorContains char string instructionStringIndexOfChar :: State -> State -instructionStringIndexOfChar state = instructionVectorIndexOf state char string +instructionStringIndexOfChar = instructionVectorIndexOf char string instructionStringSplitOnChar :: State -> State instructionStringSplitOnChar state@(State {_string = s1 : ss, _char = c1 : cs}) = state {_string = reverse $ splitOn [c1] s1 <> ss, _char = cs} instructionStringSplitOnChar state = state instructionStringReplaceFirstChar :: State -> State -instructionStringReplaceFirstChar state = instructionVectorReplaceFirst state char string +instructionStringReplaceFirstChar = instructionVectorReplaceFirst char string instructionStringReplaceNChar :: State -> State instructionStringReplaceNChar state@(State {_string = s1 : ss, _char = c1 : c2 : cs, _int = i1 : is}) = state{_string = replace s1 [c1] [c2] (Just i1) : ss, _char = cs, _int = is} instructionStringReplaceNChar state = state instructionStringReplaceAllChar :: State -> State -instructionStringReplaceAllChar state = instructionVectorReplace state char string +instructionStringReplaceAllChar = instructionVectorReplace char string instructionStringRemoveFirstChar :: State -> State instructionStringRemoveFirstChar state@(State {_string = s1 : ss, _char = c1 : cs}) = state {_string = replace s1 [c1] "" (Just 1) : ss, _char = cs} @@ -122,32 +122,32 @@ instructionStringRemoveNChar state@(State {_string = s1 : ss, _char = c1 : cs, _ instructionStringRemoveNChar state = state instructionStringRemoveAllChar :: State -> State -instructionStringRemoveAllChar state = instructionVectorRemove state char string +instructionStringRemoveAllChar = instructionVectorRemove char string instructionStringOccurrencesOfChar :: State -> State -instructionStringOccurrencesOfChar state = instructionVectorOccurrencesOf state char string +instructionStringOccurrencesOfChar = instructionVectorOccurrencesOf char string instructionStringReverse :: State -> State -instructionStringReverse state = instructionReverse state string +instructionStringReverse = instructionReverse string instructionStringHead :: State -> State -instructionStringHead state = instructionTakeN state string +instructionStringHead = instructionTakeN string instructionStringTail :: State -> State instructionStringTail state@(State {_string = s1 : ss, _int = i1 : is}) = state{_string = takeR (absNum i1 s1) s1 : ss, _int = is} instructionStringTail state = state instructionStringAppendChar :: State -> State -instructionStringAppendChar state = instructionConj state char string +instructionStringAppendChar = instructionConj char string instructionStringConjEndChar :: State -> State instructionStringConjEndChar = instructionConjEnd char string instructionStringRest :: State -> State -instructionStringRest state = instructionRest state string +instructionStringRest = instructionRest string instructionStringButLast :: State -> State -instructionStringButLast state = instructionButLast state string +instructionStringButLast = instructionButLast string instructionStringDrop :: State -> State instructionStringDrop state@(State {_string = s1 : ss, _int = i1 : is}) = state{_string = drop (absNum i1 s1) s1 : ss, _int = is} @@ -158,10 +158,10 @@ instructionStringButLastN state@(State {_string = s1 : ss, _int = i1 : is}) = st instructionStringButLastN state = state instructionStringLength :: State -> State -instructionStringLength state = instructionLength state string +instructionStringLength = instructionLength string instructionStringMakeEmpty :: State -> State -instructionStringMakeEmpty state = instructionVectorMakeEmpty state string +instructionStringMakeEmpty = instructionVectorMakeEmpty string instructionStringIsEmptyString :: State -> State instructionStringIsEmptyString state@(State {_string = s1 : ss, _bool = bs}) = state{_string = ss, _bool = null s1 : bs} @@ -172,7 +172,7 @@ instructionStringRemoveNth state@(State {_string = s1 : ss, _int = i1 : is}) = s instructionStringRemoveNth state = state instructionStringSetNth :: State -> State -instructionStringSetNth state = instructionVectorSetNth state char string +instructionStringSetNth = instructionVectorSetNth char string instructionStringStripWhitespace :: State -> State instructionStringStripWhitespace state@(State {_string = s1 : ss}) = state{_string = strip s1 : ss} @@ -198,40 +198,40 @@ instructionStringFromChar state@(State {_string = ss, _char = c1 : cs}) = state{ instructionStringFromChar state = state instructionStringPop :: State -> State -instructionStringPop state = instructionPop state string +instructionStringPop = instructionPop string instructionStringDup :: State -> State -instructionStringDup state = instructionDup state string +instructionStringDup = instructionDup string instructionStringDupN :: State -> State -instructionStringDupN state = instructionDupN state string +instructionStringDupN = instructionDupN string instructionStringRot :: State -> State -instructionStringRot state = instructionRot state string +instructionStringRot = instructionRot string instructionStringFlush :: State -> State -instructionStringFlush state = instructionFlush state string +instructionStringFlush = instructionFlush string instructionStringEq :: State -> State -instructionStringEq state = instructionEq state string +instructionStringEq = instructionEq string instructionStringStackDepth :: State -> State -instructionStringStackDepth state = instructionStackDepth state string +instructionStringStackDepth = instructionStackDepth string instructionStringYank :: State -> State -instructionStringYank state = instructionYank state string +instructionStringYank = instructionYank string instructionStringYankDup :: State -> State -instructionStringYankDup state = instructionYankDup state string +instructionStringYankDup = instructionYankDup string instructionStringIsStackEmpty :: State -> State -instructionStringIsStackEmpty state = instructionIsStackEmpty state string +instructionStringIsStackEmpty = instructionIsStackEmpty string instructionStringShove :: State -> State -instructionStringShove state = instructionShove state string +instructionStringShove = instructionShove string instructionStringShoveDup :: State -> State -instructionStringShoveDup state = instructionShoveDup state string +instructionStringShoveDup = instructionShoveDup string instructionStringSort :: State -> State instructionStringSort = instructionVectorSort string diff --git a/src/HushGP/Instructions/VectorBoolInstructions.hs b/src/HushGP/Instructions/VectorBoolInstructions.hs index a8ce980..50348d0 100644 --- a/src/HushGP/Instructions/VectorBoolInstructions.hs +++ b/src/HushGP/Instructions/VectorBoolInstructions.hs @@ -4,106 +4,106 @@ import HushGP.State import HushGP.Instructions.GenericInstructions instructionVectorBoolConcat :: State -> State -instructionVectorBoolConcat state = instructionConcat state vectorBool +instructionVectorBoolConcat = instructionConcat vectorBool instructionVectorBoolConj :: State -> State -instructionVectorBoolConj state = instructionConj state bool vectorBool +instructionVectorBoolConj = instructionConj bool vectorBool instructionVectorBoolTakeN :: State -> State -instructionVectorBoolTakeN state = instructionTakeN state vectorBool +instructionVectorBoolTakeN = instructionTakeN vectorBool instructionVectorBoolSubVector :: State -> State -instructionVectorBoolSubVector state = instructionSubVector state vectorBool +instructionVectorBoolSubVector = instructionSubVector vectorBool instructionVectorBoolFirst :: State -> State -instructionVectorBoolFirst state = instructionVectorFirst state bool vectorBool +instructionVectorBoolFirst = instructionVectorFirst bool vectorBool instructionVectorBoolLast :: State -> State -instructionVectorBoolLast state = instructionVectorLast state bool vectorBool +instructionVectorBoolLast = instructionVectorLast bool vectorBool instructionVectorBoolNth :: State -> State -instructionVectorBoolNth state = instructionVectorNth state bool vectorBool +instructionVectorBoolNth = instructionVectorNth bool vectorBool instructionVectorBoolRest :: State -> State -instructionVectorBoolRest state = instructionRest state vectorBool +instructionVectorBoolRest = instructionRest vectorBool instructionVectorBoolButLast :: State -> State -instructionVectorBoolButLast state = instructionButLast state vectorBool +instructionVectorBoolButLast = instructionButLast vectorBool instructionVectorBoolLength :: State -> State -instructionVectorBoolLength state = instructionLength state vectorBool +instructionVectorBoolLength = instructionLength vectorBool instructionVectorBoolReverse :: State -> State -instructionVectorBoolReverse state = instructionReverse state vectorBool +instructionVectorBoolReverse = instructionReverse vectorBool instructionVectorBoolPushAll :: State -> State -instructionVectorBoolPushAll state = instructionPushAll state bool vectorBool +instructionVectorBoolPushAll = instructionPushAll bool vectorBool instructionVectorBoolMakeEmpty :: State -> State -instructionVectorBoolMakeEmpty state = instructionVectorMakeEmpty state vectorBool +instructionVectorBoolMakeEmpty = instructionVectorMakeEmpty vectorBool instructionVectorBoolIsEmpty :: State -> State -instructionVectorBoolIsEmpty state = instructionVectorIsEmpty state vectorBool +instructionVectorBoolIsEmpty = instructionVectorIsEmpty vectorBool instructionVectorBoolIndexOf :: State -> State -instructionVectorBoolIndexOf state = instructionVectorIndexOf state bool vectorBool +instructionVectorBoolIndexOf = instructionVectorIndexOf bool vectorBool instructionVectorBoolOccurrencesOf :: State -> State -instructionVectorBoolOccurrencesOf state = instructionVectorOccurrencesOf state bool vectorBool +instructionVectorBoolOccurrencesOf = instructionVectorOccurrencesOf bool vectorBool instructionVectorBoolSetNth :: State -> State -instructionVectorBoolSetNth state = instructionVectorSetNth state bool vectorBool +instructionVectorBoolSetNth = instructionVectorSetNth bool vectorBool instructionVectorBoolReplace :: State -> State -instructionVectorBoolReplace state = instructionVectorReplace state bool vectorBool +instructionVectorBoolReplace = instructionVectorReplace bool vectorBool instructionVectorBoolReplaceFirst :: State -> State -instructionVectorBoolReplaceFirst state = instructionVectorReplaceFirst state bool vectorBool +instructionVectorBoolReplaceFirst = instructionVectorReplaceFirst bool vectorBool instructionVectorBoolRemove :: State -> State -instructionVectorBoolRemove state = instructionVectorRemove state bool vectorBool +instructionVectorBoolRemove = instructionVectorRemove bool vectorBool instructionVectorBoolIterate :: State -> State -instructionVectorBoolIterate state = instructionVectorIterate state bool vectorBool GeneVectorBool instructionVectorBoolIterate "instructionVectorBoolIterate" +instructionVectorBoolIterate = instructionVectorIterate bool vectorBool GeneVectorBool instructionVectorBoolIterate "instructionVectorBoolIterate" instructionVectorBoolPop :: State -> State -instructionVectorBoolPop state = instructionPop state vectorBool +instructionVectorBoolPop = instructionPop vectorBool instructionVectorBoolDup :: State -> State -instructionVectorBoolDup state = instructionDup state vectorBool +instructionVectorBoolDup = instructionDup vectorBool instructionVectorBoolDupN :: State -> State -instructionVectorBoolDupN state = instructionDupN state vectorBool +instructionVectorBoolDupN = instructionDupN vectorBool instructionVectorBoolSwap :: State -> State -instructionVectorBoolSwap state = instructionSwap state vectorBool +instructionVectorBoolSwap = instructionSwap vectorBool instructionVectorBoolRot :: State -> State -instructionVectorBoolRot state = instructionRot state vectorBool +instructionVectorBoolRot = instructionRot vectorBool instructionVectorBoolFlush :: State -> State -instructionVectorBoolFlush state = instructionFlush state vectorBool +instructionVectorBoolFlush = instructionFlush vectorBool instructionVectorBoolEq :: State -> State -instructionVectorBoolEq state = instructionEq state vectorBool +instructionVectorBoolEq = instructionEq vectorBool instructionVectorBoolStackDepth :: State -> State -instructionVectorBoolStackDepth state = instructionStackDepth state vectorBool +instructionVectorBoolStackDepth = instructionStackDepth vectorBool instructionVectorBoolYank :: State -> State -instructionVectorBoolYank state = instructionYank state vectorBool +instructionVectorBoolYank = instructionYank vectorBool instructionVectorBoolYankDup :: State -> State -instructionVectorBoolYankDup state = instructionYankDup state vectorBool +instructionVectorBoolYankDup = instructionYankDup vectorBool instructionVectorBoolIsStackEmpty :: State -> State -instructionVectorBoolIsStackEmpty state = instructionIsStackEmpty state vectorBool +instructionVectorBoolIsStackEmpty = instructionIsStackEmpty vectorBool instructionVectorBoolShove :: State -> State -instructionVectorBoolShove state = instructionShove state vectorBool +instructionVectorBoolShove = instructionShove vectorBool instructionVectorBoolShoveDup :: State -> State -instructionVectorBoolShoveDup state = instructionShoveDup state vectorBool +instructionVectorBoolShoveDup = instructionShoveDup vectorBool instructionVectorBoolSort :: State -> State instructionVectorBoolSort = instructionVectorSort vectorBool diff --git a/src/HushGP/Instructions/VectorCharInstructions.hs b/src/HushGP/Instructions/VectorCharInstructions.hs index 458cdda..5b4423a 100644 --- a/src/HushGP/Instructions/VectorCharInstructions.hs +++ b/src/HushGP/Instructions/VectorCharInstructions.hs @@ -4,106 +4,106 @@ import HushGP.State import HushGP.Instructions.GenericInstructions instructionVectorCharConcat :: State -> State -instructionVectorCharConcat state = instructionConcat state vectorChar +instructionVectorCharConcat = instructionConcat vectorChar instructionVectorCharConj :: State -> State -instructionVectorCharConj state = instructionConj state char vectorChar +instructionVectorCharConj = instructionConj char vectorChar instructionVectorCharTakeN :: State -> State -instructionVectorCharTakeN state = instructionTakeN state vectorChar +instructionVectorCharTakeN = instructionTakeN vectorChar instructionVectorCharSubVector :: State -> State -instructionVectorCharSubVector state = instructionSubVector state vectorChar +instructionVectorCharSubVector = instructionSubVector vectorChar instructionVectorCharFirst :: State -> State -instructionVectorCharFirst state = instructionVectorFirst state char vectorChar +instructionVectorCharFirst = instructionVectorFirst char vectorChar instructionVectorCharLast :: State -> State -instructionVectorCharLast state = instructionVectorLast state char vectorChar +instructionVectorCharLast = instructionVectorLast char vectorChar instructionVectorCharNth :: State -> State -instructionVectorCharNth state = instructionVectorNth state char vectorChar +instructionVectorCharNth = instructionVectorNth char vectorChar instructionVectorCharRest :: State -> State -instructionVectorCharRest state = instructionRest state vectorChar +instructionVectorCharRest = instructionRest vectorChar instructionVectorCharButLast :: State -> State -instructionVectorCharButLast state = instructionButLast state vectorChar +instructionVectorCharButLast = instructionButLast vectorChar instructionVectorCharLength :: State -> State -instructionVectorCharLength state = instructionLength state vectorChar +instructionVectorCharLength = instructionLength vectorChar instructionVectorCharReverse :: State -> State -instructionVectorCharReverse state = instructionReverse state vectorChar +instructionVectorCharReverse = instructionReverse vectorChar instructionVectorCharPushAll :: State -> State -instructionVectorCharPushAll state = instructionPushAll state char vectorChar +instructionVectorCharPushAll = instructionPushAll char vectorChar instructionVectorCharMakeEmpty :: State -> State -instructionVectorCharMakeEmpty state = instructionVectorMakeEmpty state vectorChar +instructionVectorCharMakeEmpty = instructionVectorMakeEmpty vectorChar instructionVectorCharIsEmpty :: State -> State -instructionVectorCharIsEmpty state = instructionVectorIsEmpty state vectorChar +instructionVectorCharIsEmpty = instructionVectorIsEmpty vectorChar instructionVectorCharIndexOf :: State -> State -instructionVectorCharIndexOf state = instructionVectorIndexOf state char vectorChar +instructionVectorCharIndexOf = instructionVectorIndexOf char vectorChar instructionVectorCharOccurrencesOf :: State -> State -instructionVectorCharOccurrencesOf state = instructionVectorOccurrencesOf state char vectorChar +instructionVectorCharOccurrencesOf = instructionVectorOccurrencesOf char vectorChar instructionVectorCharSetNth :: State -> State -instructionVectorCharSetNth state = instructionVectorSetNth state char vectorChar +instructionVectorCharSetNth = instructionVectorSetNth char vectorChar instructionVectorCharReplace :: State -> State -instructionVectorCharReplace state = instructionVectorReplace state char vectorChar +instructionVectorCharReplace = instructionVectorReplace char vectorChar instructionVectorCharReplaceFirst :: State -> State -instructionVectorCharReplaceFirst state = instructionVectorReplaceFirst state char vectorChar +instructionVectorCharReplaceFirst = instructionVectorReplaceFirst char vectorChar instructionVectorCharRemove :: State -> State -instructionVectorCharRemove state = instructionVectorRemove state char vectorChar +instructionVectorCharRemove = instructionVectorRemove char vectorChar instructionVectorCharIterate :: State -> State -instructionVectorCharIterate state = instructionVectorIterate state char vectorChar GeneVectorChar instructionVectorCharIterate "instructionVectorCharIterate" +instructionVectorCharIterate = instructionVectorIterate char vectorChar GeneVectorChar instructionVectorCharIterate "instructionVectorCharIterate" instructionVectorCharPop :: State -> State -instructionVectorCharPop state = instructionPop state vectorChar +instructionVectorCharPop = instructionPop vectorChar instructionVectorCharDup :: State -> State -instructionVectorCharDup state = instructionDup state vectorChar +instructionVectorCharDup = instructionDup vectorChar instructionVectorCharDupN :: State -> State -instructionVectorCharDupN state = instructionDupN state vectorChar +instructionVectorCharDupN = instructionDupN vectorChar instructionVectorCharSwap :: State -> State -instructionVectorCharSwap state = instructionSwap state vectorChar +instructionVectorCharSwap = instructionSwap vectorChar instructionVectorCharRot :: State -> State -instructionVectorCharRot state = instructionRot state vectorChar +instructionVectorCharRot = instructionRot vectorChar instructionVectorCharFlush :: State -> State -instructionVectorCharFlush state = instructionFlush state vectorChar +instructionVectorCharFlush = instructionFlush vectorChar instructionVectorCharEq :: State -> State -instructionVectorCharEq state = instructionEq state vectorChar +instructionVectorCharEq = instructionEq vectorChar instructionVectorCharStackDepth :: State -> State -instructionVectorCharStackDepth state = instructionStackDepth state vectorChar +instructionVectorCharStackDepth = instructionStackDepth vectorChar instructionVectorCharYank :: State -> State -instructionVectorCharYank state = instructionYank state vectorChar +instructionVectorCharYank = instructionYank vectorChar instructionVectorCharYankDup :: State -> State -instructionVectorCharYankDup state = instructionYankDup state vectorChar +instructionVectorCharYankDup = instructionYankDup vectorChar instructionVectorCharIsStackEmpty :: State -> State -instructionVectorCharIsStackEmpty state = instructionIsStackEmpty state vectorChar +instructionVectorCharIsStackEmpty = instructionIsStackEmpty vectorChar instructionVectorCharShove :: State -> State -instructionVectorCharShove state = instructionShove state vectorChar +instructionVectorCharShove = instructionShove vectorChar instructionVectorCharShoveDup :: State -> State -instructionVectorCharShoveDup state = instructionShoveDup state vectorChar +instructionVectorCharShoveDup = instructionShoveDup vectorChar instructionVectorCharSort :: State -> State instructionVectorCharSort = instructionVectorSort vectorChar diff --git a/src/HushGP/Instructions/VectorFloatInstructions.hs b/src/HushGP/Instructions/VectorFloatInstructions.hs index 424afea..59a2ba4 100644 --- a/src/HushGP/Instructions/VectorFloatInstructions.hs +++ b/src/HushGP/Instructions/VectorFloatInstructions.hs @@ -4,106 +4,106 @@ import HushGP.State import HushGP.Instructions.GenericInstructions instructionVectorFloatConcat :: State -> State -instructionVectorFloatConcat state = instructionConcat state vectorFloat +instructionVectorFloatConcat = instructionConcat vectorFloat instructionVectorFloatConj :: State -> State -instructionVectorFloatConj state = instructionConj state float vectorFloat +instructionVectorFloatConj = instructionConj float vectorFloat instructionVectorFloatTakeN :: State -> State -instructionVectorFloatTakeN state = instructionTakeN state vectorFloat +instructionVectorFloatTakeN = instructionTakeN vectorFloat instructionVectorFloatSubVector :: State -> State -instructionVectorFloatSubVector state = instructionSubVector state vectorFloat +instructionVectorFloatSubVector = instructionSubVector vectorFloat instructionVectorFloatFirst :: State -> State -instructionVectorFloatFirst state = instructionVectorFirst state float vectorFloat +instructionVectorFloatFirst = instructionVectorFirst float vectorFloat instructionVectorFloatLast :: State -> State -instructionVectorFloatLast state = instructionVectorLast state float vectorFloat +instructionVectorFloatLast = instructionVectorLast float vectorFloat instructionVectorFloatNth :: State -> State -instructionVectorFloatNth state = instructionVectorNth state float vectorFloat +instructionVectorFloatNth = instructionVectorNth float vectorFloat instructionVectorFloatRest :: State -> State -instructionVectorFloatRest state = instructionRest state vectorFloat +instructionVectorFloatRest = instructionRest vectorFloat instructionVectorFloatButLast :: State -> State -instructionVectorFloatButLast state = instructionButLast state vectorFloat +instructionVectorFloatButLast = instructionButLast vectorFloat instructionVectorFloatLength :: State -> State -instructionVectorFloatLength state = instructionLength state vectorFloat +instructionVectorFloatLength = instructionLength vectorFloat instructionVectorFloatReverse :: State -> State -instructionVectorFloatReverse state = instructionReverse state vectorFloat +instructionVectorFloatReverse = instructionReverse vectorFloat instructionVectorFloatPushAll :: State -> State -instructionVectorFloatPushAll state = instructionPushAll state float vectorFloat +instructionVectorFloatPushAll = instructionPushAll float vectorFloat instructionVectorFloatMakeEmpty :: State -> State -instructionVectorFloatMakeEmpty state = instructionVectorMakeEmpty state vectorFloat +instructionVectorFloatMakeEmpty = instructionVectorMakeEmpty vectorFloat instructionVectorFloatIsEmpty :: State -> State -instructionVectorFloatIsEmpty state = instructionVectorIsEmpty state vectorFloat +instructionVectorFloatIsEmpty = instructionVectorIsEmpty vectorFloat instructionVectorFloatIndexOf :: State -> State -instructionVectorFloatIndexOf state = instructionVectorIndexOf state float vectorFloat +instructionVectorFloatIndexOf = instructionVectorIndexOf float vectorFloat instructionVectorFloatOccurrencesOf :: State -> State -instructionVectorFloatOccurrencesOf state = instructionVectorOccurrencesOf state float vectorFloat +instructionVectorFloatOccurrencesOf = instructionVectorOccurrencesOf float vectorFloat instructionVectorFloatSetNth :: State -> State -instructionVectorFloatSetNth state = instructionVectorSetNth state float vectorFloat +instructionVectorFloatSetNth = instructionVectorSetNth float vectorFloat instructionVectorFloatReplace :: State -> State -instructionVectorFloatReplace state = instructionVectorReplace state float vectorFloat +instructionVectorFloatReplace = instructionVectorReplace float vectorFloat instructionVectorFloatReplaceFirst :: State -> State -instructionVectorFloatReplaceFirst state = instructionVectorReplaceFirst state float vectorFloat +instructionVectorFloatReplaceFirst = instructionVectorReplaceFirst float vectorFloat instructionVectorFloatRemove :: State -> State -instructionVectorFloatRemove state = instructionVectorRemove state float vectorFloat +instructionVectorFloatRemove = instructionVectorRemove float vectorFloat instructionVectorFloatIterate :: State -> State -instructionVectorFloatIterate state = instructionVectorIterate state float vectorFloat GeneVectorFloat instructionVectorFloatIterate "instructionVectorFloatIterate" +instructionVectorFloatIterate = instructionVectorIterate float vectorFloat GeneVectorFloat instructionVectorFloatIterate "instructionVectorFloatIterate" instructionVectorFloatPop :: State -> State -instructionVectorFloatPop state = instructionPop state vectorFloat +instructionVectorFloatPop = instructionPop vectorFloat instructionVectorFloatDup :: State -> State -instructionVectorFloatDup state = instructionDup state vectorFloat +instructionVectorFloatDup = instructionDup vectorFloat instructionVectorFloatDupN :: State -> State -instructionVectorFloatDupN state = instructionDupN state vectorFloat +instructionVectorFloatDupN = instructionDupN vectorFloat instructionVectorFloatSwap :: State -> State -instructionVectorFloatSwap state = instructionSwap state vectorFloat +instructionVectorFloatSwap = instructionSwap vectorFloat instructionVectorFloatRot :: State -> State -instructionVectorFloatRot state = instructionRot state vectorFloat +instructionVectorFloatRot = instructionRot vectorFloat instructionVectorFloatFlush :: State -> State -instructionVectorFloatFlush state = instructionFlush state vectorFloat +instructionVectorFloatFlush = instructionFlush vectorFloat instructionVectorFloatEq :: State -> State -instructionVectorFloatEq state = instructionEq state vectorFloat +instructionVectorFloatEq = instructionEq vectorFloat instructionVectorFloatStackDepth :: State -> State -instructionVectorFloatStackDepth state = instructionStackDepth state vectorFloat +instructionVectorFloatStackDepth = instructionStackDepth vectorFloat instructionVectorFloatYank :: State -> State -instructionVectorFloatYank state = instructionYank state vectorFloat +instructionVectorFloatYank = instructionYank vectorFloat instructionVectorFloatYankDup :: State -> State -instructionVectorFloatYankDup state = instructionYankDup state vectorFloat +instructionVectorFloatYankDup = instructionYankDup vectorFloat instructionVectorFloatIsStackEmpty :: State -> State -instructionVectorFloatIsStackEmpty state = instructionIsStackEmpty state vectorFloat +instructionVectorFloatIsStackEmpty = instructionIsStackEmpty vectorFloat instructionVectorFloatShove :: State -> State -instructionVectorFloatShove state = instructionShove state vectorFloat +instructionVectorFloatShove = instructionShove vectorFloat instructionVectorFloatShoveDup :: State -> State -instructionVectorFloatShoveDup state = instructionShoveDup state vectorFloat +instructionVectorFloatShoveDup = instructionShoveDup vectorFloat instructionVectorFloatSort :: State -> State instructionVectorFloatSort = instructionVectorSort vectorFloat diff --git a/src/HushGP/Instructions/VectorIntInstructions.hs b/src/HushGP/Instructions/VectorIntInstructions.hs index 1f294cf..c7e79e2 100644 --- a/src/HushGP/Instructions/VectorIntInstructions.hs +++ b/src/HushGP/Instructions/VectorIntInstructions.hs @@ -4,106 +4,106 @@ import HushGP.Instructions.GenericInstructions import HushGP.State instructionVectorIntConcat :: State -> State -instructionVectorIntConcat state = instructionConcat state vectorInt +instructionVectorIntConcat = instructionConcat vectorInt instructionVectorIntConj :: State -> State -instructionVectorIntConj state = instructionConj state int vectorInt +instructionVectorIntConj = instructionConj int vectorInt instructionVectorIntTakeN :: State -> State -instructionVectorIntTakeN state = instructionTakeN state vectorInt +instructionVectorIntTakeN = instructionTakeN vectorInt instructionVectorIntSubVector :: State -> State -instructionVectorIntSubVector state = instructionSubVector state vectorInt +instructionVectorIntSubVector = instructionSubVector vectorInt instructionVectorIntFirst :: State -> State -instructionVectorIntFirst state = instructionVectorFirst state int vectorInt +instructionVectorIntFirst = instructionVectorFirst int vectorInt instructionVectorIntLast :: State -> State -instructionVectorIntLast state = instructionVectorLast state int vectorInt +instructionVectorIntLast = instructionVectorLast int vectorInt instructionVectorIntNth :: State -> State -instructionVectorIntNth state = instructionVectorNth state int vectorInt +instructionVectorIntNth = instructionVectorNth int vectorInt instructionVectorIntRest :: State -> State -instructionVectorIntRest state = instructionRest state vectorInt +instructionVectorIntRest = instructionRest vectorInt instructionVectorIntButLast :: State -> State -instructionVectorIntButLast state = instructionButLast state vectorInt +instructionVectorIntButLast = instructionButLast vectorInt instructionVectorIntLength :: State -> State -instructionVectorIntLength state = instructionLength state vectorInt +instructionVectorIntLength = instructionLength vectorInt instructionVectorIntReverse :: State -> State -instructionVectorIntReverse state = instructionReverse state vectorInt +instructionVectorIntReverse = instructionReverse vectorInt instructionVectorIntPushAll :: State -> State -instructionVectorIntPushAll state = instructionPushAll state int vectorInt +instructionVectorIntPushAll = instructionPushAll int vectorInt instructionVectorIntMakeEmpty :: State -> State -instructionVectorIntMakeEmpty state = instructionVectorMakeEmpty state vectorInt +instructionVectorIntMakeEmpty = instructionVectorMakeEmpty vectorInt instructionVectorIntIsEmpty :: State -> State -instructionVectorIntIsEmpty state = instructionVectorIsEmpty state vectorInt +instructionVectorIntIsEmpty = instructionVectorIsEmpty vectorInt instructionVectorIntIndexOf :: State -> State -instructionVectorIntIndexOf state = instructionVectorIndexOf state int vectorInt +instructionVectorIntIndexOf = instructionVectorIndexOf int vectorInt instructionVectorIntOccurrencesOf :: State -> State -instructionVectorIntOccurrencesOf state = instructionVectorOccurrencesOf state int vectorInt +instructionVectorIntOccurrencesOf = instructionVectorOccurrencesOf int vectorInt instructionVectorIntSetNth :: State -> State -instructionVectorIntSetNth state = instructionVectorSetNth state int vectorInt +instructionVectorIntSetNth = instructionVectorSetNth int vectorInt instructionVectorIntReplace :: State -> State -instructionVectorIntReplace state = instructionVectorReplace state int vectorInt +instructionVectorIntReplace = instructionVectorReplace int vectorInt instructionVectorIntReplaceFirst :: State -> State -instructionVectorIntReplaceFirst state = instructionVectorReplaceFirst state int vectorInt +instructionVectorIntReplaceFirst = instructionVectorReplaceFirst int vectorInt instructionVectorIntRemove :: State -> State -instructionVectorIntRemove state = instructionVectorRemove state int vectorInt +instructionVectorIntRemove = instructionVectorRemove int vectorInt instructionVectorIntIterate :: State -> State -instructionVectorIntIterate state = instructionVectorIterate state int vectorInt GeneVectorInt instructionVectorIntIterate "instructionVectorIntIterate" +instructionVectorIntIterate = instructionVectorIterate int vectorInt GeneVectorInt instructionVectorIntIterate "instructionVectorIntIterate" instructionVectorIntPop :: State -> State -instructionVectorIntPop state = instructionPop state vectorChar +instructionVectorIntPop = instructionPop vectorChar instructionVectorIntDup :: State -> State -instructionVectorIntDup state = instructionDup state vectorChar +instructionVectorIntDup = instructionDup vectorChar instructionVectorIntDupN :: State -> State -instructionVectorIntDupN state = instructionDupN state vectorChar +instructionVectorIntDupN = instructionDupN vectorChar instructionVectorIntSwap :: State -> State -instructionVectorIntSwap state = instructionSwap state vectorChar +instructionVectorIntSwap = instructionSwap vectorChar instructionVectorIntRot :: State -> State -instructionVectorIntRot state = instructionRot state vectorChar +instructionVectorIntRot = instructionRot vectorChar instructionVectorIntFlush :: State -> State -instructionVectorIntFlush state = instructionFlush state vectorChar +instructionVectorIntFlush = instructionFlush vectorChar instructionVectorIntEq :: State -> State -instructionVectorIntEq state = instructionEq state vectorChar +instructionVectorIntEq = instructionEq vectorChar instructionVectorIntStackDepth :: State -> State -instructionVectorIntStackDepth state = instructionStackDepth state vectorChar +instructionVectorIntStackDepth = instructionStackDepth vectorChar instructionVectorIntYank :: State -> State -instructionVectorIntYank state = instructionYank state vectorChar +instructionVectorIntYank = instructionYank vectorChar instructionVectorIntYankDup :: State -> State -instructionVectorIntYankDup state = instructionYankDup state vectorChar +instructionVectorIntYankDup = instructionYankDup vectorChar instructionVectorIntIsStackEmpty :: State -> State -instructionVectorIntIsStackEmpty state = instructionIsStackEmpty state vectorChar +instructionVectorIntIsStackEmpty = instructionIsStackEmpty vectorChar instructionVectorIntShove :: State -> State -instructionVectorIntShove state = instructionShove state vectorChar +instructionVectorIntShove = instructionShove vectorChar instructionVectorIntShoveDup :: State -> State -instructionVectorIntShoveDup state = instructionShoveDup state vectorChar +instructionVectorIntShoveDup = instructionShoveDup vectorChar instructionVectorIntSort :: State -> State instructionVectorIntSort = instructionVectorSort vectorInt diff --git a/src/HushGP/Instructions/VectorStringInstructions.hs b/src/HushGP/Instructions/VectorStringInstructions.hs index db68782..faf6af2 100644 --- a/src/HushGP/Instructions/VectorStringInstructions.hs +++ b/src/HushGP/Instructions/VectorStringInstructions.hs @@ -4,106 +4,106 @@ import HushGP.State import HushGP.Instructions.GenericInstructions instructionVectorStringConcat :: State -> State -instructionVectorStringConcat state = instructionConcat state vectorString +instructionVectorStringConcat = instructionConcat vectorString instructionVectorStringConj :: State -> State -instructionVectorStringConj state = instructionConj state string vectorString +instructionVectorStringConj = instructionConj string vectorString instructionVectorStringTakeN :: State -> State -instructionVectorStringTakeN state = instructionTakeN state vectorString +instructionVectorStringTakeN = instructionTakeN vectorString instructionVectorStringSubVector :: State -> State -instructionVectorStringSubVector state = instructionSubVector state vectorString +instructionVectorStringSubVector = instructionSubVector vectorString instructionVectorStringFirst :: State -> State -instructionVectorStringFirst state = instructionVectorFirst state string vectorString +instructionVectorStringFirst = instructionVectorFirst string vectorString instructionVectorStringLast :: State -> State -instructionVectorStringLast state = instructionVectorLast state string vectorString +instructionVectorStringLast = instructionVectorLast string vectorString instructionVectorStringNth :: State -> State -instructionVectorStringNth state = instructionVectorNth state string vectorString +instructionVectorStringNth = instructionVectorNth string vectorString instructionVectorStringRest :: State -> State -instructionVectorStringRest state = instructionRest state vectorString +instructionVectorStringRest = instructionRest vectorString instructionVectorStringButLast :: State -> State -instructionVectorStringButLast state = instructionButLast state vectorString +instructionVectorStringButLast = instructionButLast vectorString instructionVectorStringLength :: State -> State -instructionVectorStringLength state = instructionLength state vectorString +instructionVectorStringLength = instructionLength vectorString instructionVectorStringReverse :: State -> State -instructionVectorStringReverse state = instructionReverse state vectorString +instructionVectorStringReverse = instructionReverse vectorString instructionVectorStringPushAll :: State -> State -instructionVectorStringPushAll state = instructionPushAll state string vectorString +instructionVectorStringPushAll = instructionPushAll string vectorString instructionVectorStringMakeEmpty :: State -> State -instructionVectorStringMakeEmpty state = instructionVectorMakeEmpty state vectorString +instructionVectorStringMakeEmpty = instructionVectorMakeEmpty vectorString instructionVectorStringIsEmpty :: State -> State -instructionVectorStringIsEmpty state = instructionVectorIsEmpty state vectorString +instructionVectorStringIsEmpty = instructionVectorIsEmpty vectorString instructionVectorStringIndexOf :: State -> State -instructionVectorStringIndexOf state = instructionVectorIndexOf state string vectorString +instructionVectorStringIndexOf = instructionVectorIndexOf string vectorString instructionVectorStringOccurrencesOf :: State -> State -instructionVectorStringOccurrencesOf state = instructionVectorOccurrencesOf state string vectorString +instructionVectorStringOccurrencesOf = instructionVectorOccurrencesOf string vectorString instructionVectorStringSetNth :: State -> State -instructionVectorStringSetNth state = instructionVectorSetNth state string vectorString +instructionVectorStringSetNth = instructionVectorSetNth string vectorString instructionVectorStringReplace :: State -> State -instructionVectorStringReplace state = instructionVectorReplace state string vectorString +instructionVectorStringReplace = instructionVectorReplace string vectorString instructionVectorStringReplaceFirst :: State -> State -instructionVectorStringReplaceFirst state = instructionVectorReplaceFirst state string vectorString +instructionVectorStringReplaceFirst = instructionVectorReplaceFirst string vectorString instructionVectorStringRemove :: State -> State -instructionVectorStringRemove state = instructionVectorRemove state string vectorString +instructionVectorStringRemove = instructionVectorRemove string vectorString instructionVectorStringIterate :: State -> State -instructionVectorStringIterate state = instructionVectorIterate state string vectorString GeneVectorString instructionVectorStringIterate "instructionVectorStringIterate" +instructionVectorStringIterate = instructionVectorIterate string vectorString GeneVectorString instructionVectorStringIterate "instructionVectorStringIterate" instructionVectorStringPop :: State -> State -instructionVectorStringPop state = instructionPop state vectorString +instructionVectorStringPop = instructionPop vectorString instructionVectorStringDup :: State -> State -instructionVectorStringDup state = instructionDup state vectorString +instructionVectorStringDup = instructionDup vectorString instructionVectorStringDupN :: State -> State -instructionVectorStringDupN state = instructionDupN state vectorString +instructionVectorStringDupN = instructionDupN vectorString instructionVectorStringSwap :: State -> State -instructionVectorStringSwap state = instructionSwap state vectorString +instructionVectorStringSwap = instructionSwap vectorString instructionVectorStringRot :: State -> State -instructionVectorStringRot state = instructionRot state vectorString +instructionVectorStringRot = instructionRot vectorString instructionVectorStringFlush :: State -> State -instructionVectorStringFlush state = instructionFlush state vectorString +instructionVectorStringFlush = instructionFlush vectorString instructionVectorStringEq :: State -> State -instructionVectorStringEq state = instructionEq state vectorString +instructionVectorStringEq = instructionEq vectorString instructionVectorStringStackDepth :: State -> State -instructionVectorStringStackDepth state = instructionStackDepth state vectorString +instructionVectorStringStackDepth = instructionStackDepth vectorString instructionVectorStringYank :: State -> State -instructionVectorStringYank state = instructionYank state vectorString +instructionVectorStringYank = instructionYank vectorString instructionVectorStringYankDup :: State -> State -instructionVectorStringYankDup state = instructionYankDup state vectorString +instructionVectorStringYankDup = instructionYankDup vectorString instructionVectorStringIsStackEmpty :: State -> State -instructionVectorStringIsStackEmpty state = instructionIsStackEmpty state vectorString +instructionVectorStringIsStackEmpty = instructionIsStackEmpty vectorString instructionVectorStringShove :: State -> State -instructionVectorStringShove state = instructionShove state vectorString +instructionVectorStringShove = instructionShove vectorString instructionVectorStringShoveDup :: State -> State -instructionVectorStringShoveDup state = instructionShoveDup state vectorString +instructionVectorStringShoveDup = instructionShoveDup vectorString instructionVectorStringSort :: State -> State instructionVectorStringSort = instructionVectorSort vectorString From 867f3ac4409d3858716872e61b81d58d5834492e Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Fri, 7 Feb 2025 15:51:24 -0600 Subject: [PATCH 125/171] make pattern matching parameters consistent --- TODO.md | 2 +- src/HushGP/Instructions/BoolInstructions.hs | 10 +-- src/HushGP/Instructions/CodeInstructions.hs | 76 +++++++++---------- src/HushGP/Instructions/ExecInstructions.hs | 34 ++++----- src/HushGP/Instructions/FloatInstructions.hs | 24 +++--- .../Instructions/GenericInstructions.hs | 16 ++-- src/HushGP/Instructions/IntInstructions.hs | 24 +++--- src/HushGP/Instructions/StringInstructions.hs | 2 +- 8 files changed, 94 insertions(+), 94 deletions(-) diff --git a/TODO.md b/TODO.md index e0bb48e..8e6509c 100644 --- a/TODO.md +++ b/TODO.md @@ -12,7 +12,7 @@ - [X] Make int yank, shove, yankdup, and shovedup generic - [ ] Write hackage documentation for each function - [X] Refactor all functions to take state as the final parameter -- [ ] Standardize the pattern matching parameter names, such as c1 : cs +- [X] Standardize the pattern matching parameter names, such as c1 : cs - [ ] Write unit/quickcheck tests for all of the instructions ## PushGP TODO diff --git a/src/HushGP/Instructions/BoolInstructions.hs b/src/HushGP/Instructions/BoolInstructions.hs index 0a48136..2d55641 100644 --- a/src/HushGP/Instructions/BoolInstructions.hs +++ b/src/HushGP/Instructions/BoolInstructions.hs @@ -4,26 +4,26 @@ import HushGP.State import HushGP.Instructions.GenericInstructions instructionBoolFromInt :: State -> State -instructionBoolFromInt state@(State {_int = (i : is), _bool = bs}) = state {_int = is, _bool = (i /= 0) : bs} +instructionBoolFromInt state@(State {_int = i1 : is, _bool = bs}) = state {_int = is, _bool = (i1 /= 0) : bs} instructionBoolFromInt state = state instructionBoolFromFloat :: State -> State -instructionBoolFromFloat state@(State {_float = (f : fs), _bool = bs}) = state {_float = fs, _bool = (f /= 0) : bs} +instructionBoolFromFloat state@(State {_float = f1 : fs, _bool = bs}) = state {_float = fs, _bool = (f1 /= 0) : bs} instructionBoolFromFloat state = state boolTemplate :: (Bool -> Bool -> Bool) -> State -> State -boolTemplate func state@(State {_bool = (b1 : b2 : bs)}) = state {_bool = func b1 b2 : bs} +boolTemplate func state@(State {_bool = b1 : b2 : bs}) = state {_bool = func b1 b2 : bs} boolTemplate _ state = state instructionBoolAnd :: State -> State instructionBoolAnd = boolTemplate (&&) instructionBoolInvertFirstThenAnd :: State -> State -instructionBoolInvertFirstThenAnd state@(State {_bool = (b1 : bs)}) = boolTemplate (&&) state {_bool = not b1 : bs} +instructionBoolInvertFirstThenAnd state@(State {_bool = b1 : bs}) = boolTemplate (&&) state {_bool = not b1 : bs} instructionBoolInvertFirstThenAnd state = state instructionBoolInvertSecondThenAnd :: State -> State -instructionBoolInvertSecondThenAnd state@(State {_bool = (b1 : b2 : bs)}) = boolTemplate (&&) state {_bool = b1 : not b2 : bs} +instructionBoolInvertSecondThenAnd state@(State {_bool = b1 : b2 : bs}) = boolTemplate (&&) state {_bool = b1 : not b2 : bs} instructionBoolInvertSecondThenAnd state = state instructionBoolOr :: State -> State diff --git a/src/HushGP/Instructions/CodeInstructions.hs b/src/HushGP/Instructions/CodeInstructions.hs index ebe5f00..016cafe 100644 --- a/src/HushGP/Instructions/CodeInstructions.hs +++ b/src/HushGP/Instructions/CodeInstructions.hs @@ -11,11 +11,11 @@ isBlock (Block _) = True isBlock _ = False blockLength :: Gene -> Int -blockLength (Block xs) = length xs +blockLength (Block bxs) = length bxs blockLength _ = 1 blockIsNull :: Gene -> Bool -blockIsNull (Block xs) = null xs +blockIsNull (Block bxs) = null bxs blockIsNull _ = False -- https://faculty.hampshire.edu/lspector/push3-description.html#Type @@ -29,7 +29,7 @@ findContainer (Block fullA) gene where findContainer' :: [Gene] -> Gene -> Gene findContainer' [] _ = Block [] - findContainer' ((Block x) : xs) g = if g `elem` x then Block x else findContainer' xs g + findContainer' ((Block bx1) : bxs) g = if g `elem` bx1 then Block bx1 else findContainer' bxs g findContainer' _ _ = Block [] -- This should never happen findContainer _ _ = Block [] @@ -38,21 +38,21 @@ countDiscrepancy (Block xs) (Block ys) = sum [if uncurry (==) tup then 0 else 1 countDiscrepancy xgene ygene = if xgene == ygene then 1 else 0 extractFirstFromBlock :: Gene -> Gene -extractFirstFromBlock (Block (x : _)) = x +extractFirstFromBlock (Block (bx1 : _)) = bx1 extractFirstFromBlock gene = gene extractLastFromBlock :: Gene -> Gene extractLastFromBlock (Block []) = Block [] -extractLastFromBlock (Block xs) = last xs +extractLastFromBlock (Block bxs) = last bxs extractLastFromBlock gene = gene extractInitFromBlock :: Gene -> Gene extractInitFromBlock (Block []) = Block [] -extractInitFromBlock (Block xs) = Block (init xs) +extractInitFromBlock (Block bxs) = Block (init bxs) extractInitFromBlock gene = gene extractTailFromBlock :: Gene -> Gene -extractTailFromBlock (Block xs) = Block (drop 1 xs) +extractTailFromBlock (Block bxs) = Block (drop 1 bxs) extractTailFromBlock _ = Block [] codeAtPoint :: [Gene] -> Int -> Gene @@ -68,48 +68,48 @@ codeInsertAtPoint ((Block genes) : oldGenes) gene index = Block (codeInsertAtPoi codeInsertAtPoint (oldGene : oldGenes) gene index = oldGene : codeInsertAtPoint oldGenes gene (index - 1) codeCombine :: Gene -> Gene -> Gene -codeCombine (Block xs) (Block ys) = Block (xs <> ys) -codeCombine (Block xs) ygene = Block (ygene : xs) -codeCombine xgene (Block ys) = Block (xgene : ys) +codeCombine (Block bxs) (Block bys) = Block (bxs <> bys) +codeCombine (Block bxs) ygene = Block (ygene : bxs) +codeCombine xgene (Block bys) = Block (xgene : bys) codeCombine xgene ygene = Block [xgene, ygene] codeMember :: Gene -> Gene -> Bool -codeMember (Block _) (Block _) = False -- Can't compare two lists with `elem` -codeMember (Block xs) ygene = ygene `elem` xs +codeMember (Block bxs) (Block bys) = findSubA bxs bys /= (-1) +codeMember (Block bxs) ygene = ygene `elem` bxs codeMember _ _ = False codeRecursiveSize :: Gene -> Int -codeRecursiveSize (Block xs) = sum [codeRecursiveSize x + if isBlock x then 1 else 0 | x <- xs] +codeRecursiveSize (Block bxs) = sum [codeRecursiveSize x + if isBlock x then 1 else 0 | x <- bxs] codeRecursiveSize _ = 1 instructionCodePop :: State -> State instructionCodePop = instructionPop code instructionCodeIsCodeBlock :: State -> State -instructionCodeIsCodeBlock state@(State {_code = (c : cs), _bool = bs}) = state {_code = cs, _bool = isBlock c : bs} +instructionCodeIsCodeBlock state@(State {_code = c1 : cs, _bool = bs}) = state {_code = cs, _bool = isBlock c1 : bs} instructionCodeIsCodeBlock state = state instructionCodeIsSingular :: State -> State -instructionCodeIsSingular state@(State {_code = (c : cs), _bool = bs}) = state {_code = cs, _bool = not (isBlock c) : bs} +instructionCodeIsSingular state@(State {_code = c1 : cs, _bool = bs}) = state {_code = cs, _bool = not (isBlock c1) : bs} instructionCodeIsSingular state = state instructionCodeLength :: State -> State -instructionCodeLength state@(State {_code = (c : cs), _int = is}) = state {_code = cs, _int = blockLength c : is} +instructionCodeLength state@(State {_code = c1 : cs, _int = is}) = state {_code = cs, _int = blockLength c1 : is} instructionCodeLength state = state -- CODE.CAR instructionCodeFirst :: State -> State -instructionCodeFirst state@(State {_code = (c : cs)}) = state {_code = extractFirstFromBlock c : cs} +instructionCodeFirst state@(State {_code = c1 : cs}) = state {_code = extractFirstFromBlock c1 : cs} instructionCodeFirst state = state instructionCodeLast :: State -> State -instructionCodeLast state@(State {_code = (c : cs)}) = state {_code = extractLastFromBlock c : cs} +instructionCodeLast state@(State {_code = c1 : cs}) = state {_code = extractLastFromBlock c1 : cs} instructionCodeLast state = state -- CODE.CDR -- https://erp12.github.io/pyshgp/html/core_instructions.html#code-rest instructionCodeTail :: State -> State -instructionCodeTail state@(State {_code = (c : cs)}) = state {_code = extractTailFromBlock c : cs} +instructionCodeTail state@(State {_code = c1 : cs}) = state {_code = extractTailFromBlock c1 : cs} instructionCodeTail state = state -- |Takes the tail of a block starting at an index determined by the int stack @@ -124,19 +124,19 @@ instructionCodeTailN state = state -- https://erp12.github.io/pyshgp/html/core_instructions.html#code-but-last instructionCodeInit :: State -> State -instructionCodeInit state@(State {_code = (c : cs)}) = state {_code = extractInitFromBlock c : cs} +instructionCodeInit state@(State {_code = c1 : cs}) = state {_code = extractInitFromBlock c1 : cs} instructionCodeInit state = state instructionCodeWrap :: State -> State -instructionCodeWrap state@(State {_code = (c : cs)}) = state {_code = Block [c] : cs} +instructionCodeWrap state@(State {_code = c1 : cs}) = state {_code = Block [c1] : cs} instructionCodeWrap state = state instructionCodeList :: State -> State -instructionCodeList state@(State {_code = (c1 : c2 : cs)}) = state {_code = Block [c1, c2] : cs} +instructionCodeList state@(State {_code = c1 : c2 : cs}) = state {_code = Block [c1, c2] : cs} instructionCodeList state = state instructionCodeCombine :: State -> State -instructionCodeCombine state@(State {_code = (c1 : c2 : cs)}) = state {_code = codeCombine c1 c2 : cs} +instructionCodeCombine state@(State {_code = c1 : c2 : cs}) = state {_code = codeCombine c1 c2 : cs} instructionCodeCombine state = state instructionCodeDo :: State -> State @@ -144,7 +144,7 @@ instructionCodeDo state@(State {_code = c1 : cs, _exec = es}) = state {_code = c instructionCodeDo state = state instructionCodeDoDup :: State -> State -instructionCodeDoDup state@(State {_code = (c1 : cs), _exec = es}) = state {_code = c1 : cs, _exec = c1 : es} +instructionCodeDoDup state@(State {_code = c1 : cs, _exec = es}) = state {_code = c1 : cs, _exec = c1 : es} instructionCodeDoDup state = state -- https://erp12.github.io/pyshgp/html/core_instructions.html#code-do-then-pop @@ -159,7 +159,7 @@ codeDoRange :: Gene codeDoRange = StateFunc (instructionCodeDoRange, "instructionCodeDoRange") instructionCodeDoRange :: State -> State -instructionCodeDoRange state@(State {_code = (c1 : cs), _int = (i0 : i1 : is), _exec = es}) = +instructionCodeDoRange state@(State {_code = c1 : cs, _int = i0 : i1 : is, _exec = es}) = if increment i0 i1 /= 0 then state {_exec = c1 : Block [GeneInt (i1 + increment i0 i1), GeneInt i0, codeFromExec, c1, codeDoRange] : es, _int = i1 : is, _code = cs} else state {_exec = c1: es, _int = i1 : is, _code = cs} @@ -172,42 +172,42 @@ instructionCodeDoRange state@(State {_code = (c1 : cs), _int = (i0 : i1 : is), _ instructionCodeDoRange state = state instructionCodeDoCount :: State -> State -instructionCodeDoCount state@(State {_code = (c : cs), _int = (i : is), _exec = es}) = - if i < 1 +instructionCodeDoCount state@(State {_code = c : cs, _int = i1 : is, _exec = es}) = + if i1 < 1 then state - else state {_code = cs, _int = is, _exec = Block [GeneInt 0, GeneInt $ i - 1, codeFromExec, c, codeDoRange] : es} + else state {_code = cs, _int = is, _exec = Block [GeneInt 0, GeneInt $ i1 - 1, codeFromExec, c, codeDoRange] : es} instructionCodeDoCount state = state instructionCodeDoTimes :: State -> State -instructionCodeDoTimes state@(State {_code = (c : cs), _int = (i : is), _exec = es}) = - if i < 1 +instructionCodeDoTimes state@(State {_code = c : cs, _int = i1 : is, _exec = es}) = + if i1 < 1 then state - else state {_code = cs, _int = is, _exec = Block [GeneInt 0, GeneInt $ i - 1, codeFromExec, Block [StateFunc (instructionIntPop, "instructionIntPop"), c], codeDoRange] : es} + else state {_code = cs, _int = is, _exec = Block [GeneInt 0, GeneInt $ i1 - 1, codeFromExec, Block [StateFunc (instructionIntPop, "instructionIntPop"), c], codeDoRange] : es} instructionCodeDoTimes state = state instructionCodeIf :: State -> State -instructionCodeIf state@(State {_code = (c1 : c2 : cs), _bool = (b1 : bs), _exec = es}) = state{_code = cs, _bool = bs, _exec = (if b1 then c1 else c2) : es} +instructionCodeIf state@(State {_code = c1 : c2 : cs, _bool = b1 : bs, _exec = es}) = state{_code = cs, _bool = bs, _exec = (if b1 then c1 else c2) : es} instructionCodeIf state = state instructionCodeWhen :: State -> State -instructionCodeWhen state@(State {_code = (c1 : cs), _bool = (b1 : bs), _exec = es}) = state{_code = cs, _bool = bs, _exec = if b1 then c1 : es else es} +instructionCodeWhen state@(State {_code = c1 : cs, _bool = b1 : bs, _exec = es}) = state{_code = cs, _bool = bs, _exec = if b1 then c1 : es else es} instructionCodeWhen state = state instructionCodeMember :: State -> State -instructionCodeMember state@(State {_code = (c1 : c2 : cs), _bool = bs}) = state{_code = cs, _bool = codeMember c1 c2 : bs} +instructionCodeMember state@(State {_code = c1 : c2 : cs, _bool = bs}) = state{_code = cs, _bool = codeMember c1 c2 : bs} instructionCodeMember state = state -- This one doesn't count the recursive Blocks while instructionCodeExtract does -- https://erp12.github.io/pyshgp/html/core_instructions.html#code-nth instructionCodeN :: State -> State -instructionCodeN state@(State {_code = ((Block c1) : cs), _int = (i1 : is)}) = +instructionCodeN state@(State {_code = (Block c1) : cs, _int = i1 : is}) = if not $ null c1 then state {_code = c1 !! index : cs, _int = is} else state where index :: Int index = abs i1 `mod` length c1 -instructionCodeN state@(State {_code = (c1 : cs), _int = _ : is}) = state {_code = c1 : cs, _int = is} +instructionCodeN state@(State {_code = c1 : cs, _int = _ : is}) = state {_code = c1 : cs, _int = is} instructionCodeN state = state instructionMakeEmptyCodeBlock :: State -> State @@ -226,7 +226,7 @@ instructionCodeSize state = state -- I designed this function differently so 0 returns the 0th element, and the last item -- in the codeblock can be returned. instructionCodeExtract :: State -> State -instructionCodeExtract state@(State {_code = (block@(Block c1) : cs), _int = i1 : is}) = +instructionCodeExtract state@(State {_code = block@(Block c1) : cs, _int = i1 : is}) = let index = abs i1 `mod` codeRecursiveSize block in @@ -235,7 +235,7 @@ instructionCodeExtract state@(State {_code = cs, _int = _ : is}) = state{_code = instructionCodeExtract state = state instructionCodeInsert :: State -> State -instructionCodeInsert state@(State {_code = (block@(Block c1) : c2 : cs), _int = i1 : is}) = +instructionCodeInsert state@(State {_code = block@(Block c1) : c2 : cs, _int = i1 : is}) = let index = abs i1 `mod` codeRecursiveSize block in diff --git a/src/HushGP/Instructions/ExecInstructions.hs b/src/HushGP/Instructions/ExecInstructions.hs index 703c783..5602db2 100644 --- a/src/HushGP/Instructions/ExecInstructions.hs +++ b/src/HushGP/Instructions/ExecInstructions.hs @@ -5,8 +5,8 @@ import HushGP.Instructions.IntInstructions import HushGP.Instructions.GenericInstructions instructionExecIf :: State -> State -instructionExecIf state@(State {_exec = (e1 : e2 : es), _bool = (b : bs)}) = - if b +instructionExecIf state@(State {_exec = e1 : e2 : es, _bool = b1 : bs}) = + if b1 then state {_exec = e1 : es, _bool = bs} else state {_exec = e2 : es, _bool = bs} instructionExecIf state = state @@ -54,7 +54,7 @@ execDoRange :: Gene execDoRange = StateFunc (instructionExecDoRange, "instructionExecDoRange") instructionExecDoRange :: State -> State -instructionExecDoRange state@(State {_exec = (e1 : es), _int = (i0 : i1 : is)}) = +instructionExecDoRange state@(State {_exec = e1 : es, _int = i0 : i1 : is}) = if increment i0 i1 /= 0 then state {_exec = e1 : Block [GeneInt (i1 + increment i0 i1), GeneInt i0, execDoRange, e1] : es, _int = i1 : is} else state {_exec = e1 : es, _int = i1 : is} @@ -67,40 +67,40 @@ instructionExecDoRange state@(State {_exec = (e1 : es), _int = (i0 : i1 : is)}) instructionExecDoRange state = state instructionExecDoCount :: State -> State -instructionExecDoCount state@(State {_exec = (e : es), _int = (i : is)}) = - if i < 1 +instructionExecDoCount state@(State {_exec = e1 : es, _int = i1 : is}) = + if i1 < 1 then state - else state {_exec = Block [GeneInt 0, GeneInt $ i - 1, execDoRange, e] : es, _int = is} + else state {_exec = Block [GeneInt 0, GeneInt $ i1 - 1, execDoRange, e1] : es, _int = is} instructionExecDoCount state = state instructionExecDoTimes :: State -> State -instructionExecDoTimes state@(State {_exec = (e : es), _int = (i : is)}) = - if i < 1 +instructionExecDoTimes state@(State {_exec = e1 : es, _int = i1 : is}) = + if i1 < 1 then state - else state {_exec = Block [GeneInt 0, GeneInt $ i - 1, execDoRange, Block [StateFunc (instructionIntPop, "instructionIntPop"), e]] : es, _int = is} + else state {_exec = Block [GeneInt 0, GeneInt $ i1 - 1, execDoRange, Block [StateFunc (instructionIntPop, "instructionIntPop"), e1]] : es, _int = is} instructionExecDoTimes state = state execWhile :: Gene execWhile = StateFunc (instructionExecWhile, "instructionExecWhile") instructionExecWhile :: State -> State -instructionExecWhile state@(State {_exec = (_ : es), _bool = []}) = +instructionExecWhile state@(State {_exec = _ : es, _bool = []}) = state {_exec = es} -instructionExecWhile state@(State {_exec = alles@(e : es), _bool = (b : bs)}) = - if b - then state {_exec = e : execWhile : alles, _bool = bs} +instructionExecWhile state@(State {_exec = alles@(e1 : es), _bool = b1 : bs}) = + if b1 + then state {_exec = e1 : execWhile : alles, _bool = bs} else state {_exec = es} instructionExecWhile state = state instructionExecDoWhile :: State -> State -instructionExecDoWhile state@(State {_exec = alles@(e : _)}) = - state {_exec = e : execWhile : alles} +instructionExecDoWhile state@(State {_exec = alles@(e1 : _)}) = + state {_exec = e1 : execWhile : alles} instructionExecDoWhile state = state -- Eats the _boolean no matter what instructionExecWhen :: State -> State -instructionExecWhen state@(State {_exec = (_ : es), _bool = (b : bs)}) = - if not b +instructionExecWhen state@(State {_exec = _ : es, _bool = b1 : bs}) = + if not b1 then state {_exec = es, _bool = bs} else state {_bool = bs} instructionExecWhen state = state diff --git a/src/HushGP/Instructions/FloatInstructions.hs b/src/HushGP/Instructions/FloatInstructions.hs index ee4bb14..8548399 100644 --- a/src/HushGP/Instructions/FloatInstructions.hs +++ b/src/HushGP/Instructions/FloatInstructions.hs @@ -6,15 +6,15 @@ import HushGP.State import Data.Char instructionFloatFromInt :: State -> State -instructionFloatFromInt state@(State {_float = fs, _int = (i : is)}) = state {_float = (fromIntegral i :: Float) : fs, _int = is} +instructionFloatFromInt state@(State {_float = fs, _int = i1 : is}) = state {_float = (fromIntegral i1 :: Float) : fs, _int = is} instructionFloatFromInt state = state instructionFloatFromBool :: State -> State -instructionFloatFromBool state@(State {_bool = (b : bs), _float = fs}) = state {_bool = bs, _float = (if b then 1.0 else 0.0) : fs} +instructionFloatFromBool state@(State {_bool = b1 : bs, _float = fs}) = state {_bool = bs, _float = (if b1 then 1.0 else 0.0) : fs} instructionFloatFromBool state = state instructionFloatFromChar :: State -> State -instructionFloatFromChar state@(State {_char = c : cs, _float = fs}) = state {_char = cs, _float = (fromIntegral (ord c) :: Float) : fs} +instructionFloatFromChar state@(State {_char = c1 : cs, _float = fs}) = state {_char = cs, _float = (fromIntegral (ord c1) :: Float) : fs} instructionFloatFromChar state = state instructionFloatFromString :: State -> State @@ -25,39 +25,39 @@ instructionFloatFromString state@(State {_string = s1 : ss, _float = fs}) = instructionFloatFromString state = state instructionFloatAdd :: State -> State -instructionFloatAdd state@(State {_float = (f1 : f2 : fs)}) = state {_float = f2 + f1 : fs} +instructionFloatAdd state@(State {_float = f1 : f2 : fs}) = state {_float = f2 + f1 : fs} instructionFloatAdd state = state instructionFloatSub :: State -> State -instructionFloatSub state@(State {_float = (f1 : f2 : fs)}) = state {_float = f2 - f1 : fs} +instructionFloatSub state@(State {_float = f1 : f2 : fs}) = state {_float = f2 - f1 : fs} instructionFloatSub state = state instructionFloatMul :: State -> State -instructionFloatMul state@(State {_float = (f1 : f2 : fs)}) = state {_float = f2 * f1 : fs} +instructionFloatMul state@(State {_float = f1 : f2 : fs}) = state {_float = f2 * f1 : fs} instructionFloatMul state = state instructionFloatDiv :: State -> State -instructionFloatDiv state@(State {_float = (f1 : f2 : fs)}) = state {_float = if f1 /= 0 then f2 / f1 : fs else f1 : f2 : fs} +instructionFloatDiv state@(State {_float = f1 : f2 : fs}) = state {_float = if f1 /= 0 then f2 / f1 : fs else f1 : f2 : fs} instructionFloatDiv state = state instructionFloatMod :: State -> State -instructionFloatMod state@(State {_float = (f1 : f2 : fs)}) = state {_float = if f1 /= 0 then f2 `mod'` f1 : fs else f1 : f2 : fs} +instructionFloatMod state@(State {_float = f1 : f2 : fs}) = state {_float = if f1 /= 0 then f2 `mod'` f1 : fs else f1 : f2 : fs} instructionFloatMod state = state instructionFloatMin :: State -> State -instructionFloatMin state@(State {_float = (f1 : f2 : fs)}) = state {_float = min f1 f2 : fs} +instructionFloatMin state@(State {_float = f1 : f2 : fs}) = state {_float = min f1 f2 : fs} instructionFloatMin state = state instructionFloatMax :: State -> State -instructionFloatMax state@(State {_float = (f1 : f2 : fs)}) = state {_float = max f1 f2 : fs} +instructionFloatMax state@(State {_float = f1 : f2 : fs}) = state {_float = max f1 f2 : fs} instructionFloatMax state = state instructionFloatInc :: State -> State -instructionFloatInc state@(State {_float = (f1 : fs)}) = state {_float = f1 + 1 : fs} +instructionFloatInc state@(State {_float = f1 : fs}) = state {_float = f1 + 1 : fs} instructionFloatInc state = state instructionFloatDec :: State -> State -instructionFloatDec state@(State {_float = (f1 : fs)}) = state {_float = f1 - 1 : fs} +instructionFloatDec state@(State {_float = f1 : fs}) = state {_float = f1 - 1 : fs} instructionFloatDec state = state instructionFloatLT :: State -> State diff --git a/src/HushGP/Instructions/GenericInstructions.hs b/src/HushGP/Instructions/GenericInstructions.hs index 37d23fd..d529ead 100644 --- a/src/HushGP/Instructions/GenericInstructions.hs +++ b/src/HushGP/Instructions/GenericInstructions.hs @@ -91,7 +91,7 @@ instructionDup :: Lens' State [a] -> State -> State instructionDup accessor state = case uncons (view accessor state) of Nothing -> state - Just (x,_) -> state & accessor .~ x : view accessor state + Just (x1,_) -> state & accessor .~ x1 : view accessor state instructionPop :: Lens' State [a] -> State -> State instructionPop accessor state = state & accessor .~ drop 1 (view accessor state) @@ -117,7 +117,7 @@ instructionDupN accessor state = then instructionDupNHelper (count - 1) instruction internalAccessor (internalState & accessor .~ (instruction : view accessor internalState)) else internalState --- |Duplicates the top N items on a stack. If n <= 0 nothing happens +-- |Duplicates the top N items on a stack. If n <= 0, nothing happens -- TODO: Will need to implement a max stack items at some point instructionDupItems :: Lens' State [a] -> State -> State instructionDupItems accessor state@(State {_int = i1 : is}) = @@ -162,17 +162,17 @@ instructionStackDepth :: Lens' State [a] -> State -> State instructionStackDepth accessor state@(State {_int = is}) = state{_int = length (view accessor state) : is} instructionYankDup :: Lens' State [a] -> State -> State -instructionYankDup accessor state@(State {_int = i : is}) = +instructionYankDup accessor state@(State {_int = i1 : is}) = if notEmptyStack accessor state - then state{_int = is} & accessor .~ (view accessor state{_int = is} !! max 0 (min i (length (view accessor state{_int = is}) - 1))) : view accessor state{_int = is} + then state{_int = is} & accessor .~ (view accessor state{_int = is} !! max 0 (min i1 (length (view accessor state{_int = is}) - 1))) : view accessor state{_int = is} else state instructionYankDup _ state = state instructionYank :: forall a. Lens' State [a] -> State -> State -instructionYank accessor state@(State {_int = i : is}) = +instructionYank accessor state@(State {_int = i1 : is}) = let myIndex :: Int - myIndex = max 0 (min i (length (view accessor state{_int = is}) - 1)) + myIndex = max 0 (min i1 (length (view accessor state{_int = is}) - 1)) item :: a item = view accessor state{_int = is} !! myIndex deletedState :: State @@ -185,9 +185,9 @@ instructionYank _ state = state -- the duplicated index matters whether or not it's present in the stack at the moment of calculation. -- I'm not going to keep this behavior. Check out interpysh examples for how pysh handles it. instructionShoveDup :: Lens' State [a] -> State -> State -instructionShoveDup accessor state@(State {_int = i : is}) = +instructionShoveDup accessor state@(State {_int = i1 : is}) = case uncons (view accessor state{_int = is}) of - Just (x,_) -> state{_int = is} & accessor .~ combineTuple x (splitAt (max 0 (min i (length (view accessor state{_int = is}) - 1))) (view accessor state{_int = is})) + Just (x,_) -> state{_int = is} & accessor .~ combineTuple x (splitAt (max 0 (min i1 (length (view accessor state{_int = is}) - 1))) (view accessor state{_int = is})) _ -> state instructionShoveDup _ state = state diff --git a/src/HushGP/Instructions/IntInstructions.hs b/src/HushGP/Instructions/IntInstructions.hs index 2418775..4d0401b 100644 --- a/src/HushGP/Instructions/IntInstructions.hs +++ b/src/HushGP/Instructions/IntInstructions.hs @@ -6,15 +6,15 @@ import Data.Char -- import Debug.Trace instructionIntFromFloat :: State -> State -instructionIntFromFloat state@(State {_float = (f : fs), _int = is}) = state {_float = fs, _int = floor f : is} +instructionIntFromFloat state@(State {_float = f1 : fs, _int = is}) = state {_float = fs, _int = floor f1 : is} instructionIntFromFloat state = state instructionIntFromBool :: State -> State -instructionIntFromBool state@(State {_bool = (b : bs), _int = is}) = state {_bool = bs, _int = (if b then 1 else 0) : is} +instructionIntFromBool state@(State {_bool = b1 : bs, _int = is}) = state {_bool = bs, _int = (if b1 then 1 else 0) : is} instructionIntFromBool state = state instructionIntFromChar :: State -> State -instructionIntFromChar state@(State {_char = c : cs, _int = is}) = state {_char = cs, _int = ord c : is} +instructionIntFromChar state@(State {_char = c1 : cs, _int = is}) = state {_char = cs, _int = ord c1 : is} instructionIntFromChar state = state instructionIntFromString :: State -> State @@ -25,39 +25,39 @@ instructionIntFromString state@(State {_string = s1 : ss, _int = is}) = instructionIntFromString state = state instructionIntAdd :: State -> State -instructionIntAdd state@(State {_int = (i1 : i2 : is)}) = state {_int = i2 + i1 : is} +instructionIntAdd state@(State {_int = i1 : i2 : is}) = state {_int = i2 + i1 : is} instructionIntAdd state = state instructionIntSub :: State -> State -instructionIntSub state@(State {_int = (i1 : i2 : is)}) = state {_int = i2 - i1 : is} +instructionIntSub state@(State {_int = i1 : i2 : is}) = state {_int = i2 - i1 : is} instructionIntSub state = state instructionIntMul :: State -> State -instructionIntMul state@(State {_int = (i1 : i2 : is)}) = state {_int = i2 * i1 : is} +instructionIntMul state@(State {_int = i1 : i2 : is}) = state {_int = i2 * i1 : is} instructionIntMul state = state instructionIntDiv :: State -> State -instructionIntDiv state@(State {_int = (i1 : i2 : is)}) = state {_int = if i1 /= 0 then (i2 `div` i1) : is else i1 : i2 : is} +instructionIntDiv state@(State {_int = i1 : i2 : is}) = state {_int = if i1 /= 0 then (i2 `div` i1) : is else i1 : i2 : is} instructionIntDiv state = state instructionIntMod :: State -> State -instructionIntMod state@(State {_int = (i1 : i2 : is)}) = state {_int = if i1 /= 0 then (i2 `mod` i1) : is else i1 : i2 : is} +instructionIntMod state@(State {_int = i1 : i2 : is}) = state {_int = if i1 /= 0 then (i2 `mod` i1) : is else i1 : i2 : is} instructionIntMod state = state instructionIntMin :: State -> State -instructionIntMin state@(State {_int = (i1 : i2 : is)}) = state {_int = min i1 i2 : is} +instructionIntMin state@(State {_int = i1 : i2 : is}) = state {_int = min i1 i2 : is} instructionIntMin state = state instructionIntMax :: State -> State -instructionIntMax state@(State {_int = (i1 : i2 : is)}) = state {_int = max i1 i2 : is} +instructionIntMax state@(State {_int = i1 : i2 : is}) = state {_int = max i1 i2 : is} instructionIntMax state = state instructionIntInc :: State -> State -instructionIntInc state@(State {_int = (i1 : is)}) = state {_int = i1 + 1 : is} +instructionIntInc state@(State {_int = i1 : is}) = state {_int = i1 + 1 : is} instructionIntInc state = state instructionIntDec :: State -> State -instructionIntDec state@(State {_int = (i1 : is)}) = state {_int = i1 - 1 : is} +instructionIntDec state@(State {_int = i1 : is}) = state {_int = i1 - 1 : is} instructionIntDec state = state instructionIntLT :: State -> State diff --git a/src/HushGP/Instructions/StringInstructions.hs b/src/HushGP/Instructions/StringInstructions.hs index 0d0caff..e32df23 100644 --- a/src/HushGP/Instructions/StringInstructions.hs +++ b/src/HushGP/Instructions/StringInstructions.hs @@ -182,7 +182,7 @@ instructionStringFromLens :: Show a => State -> Lens' State [a] -> State instructionStringFromLens state@(State {_string = ss}) accessor = case uncons (view accessor state) of Nothing -> state - Just (x,_) -> state{_string = show x : ss} + Just (x1,_) -> state{_string = show x1 : ss} instructionStringFromBool :: State -> State instructionStringFromBool state = instructionStringFromLens state bool From ac8d1974f2b840b19929b787ad8371efe9684d80 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Fri, 7 Feb 2025 17:53:01 -0600 Subject: [PATCH 126/171] remove lens note --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 78db742..894f2d3 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,6 @@ worth it in the grand scheme. Would mean we could also track the functions when * [x] Write tests for every function. * [x] tests/ are just copied from make-grade, need to write for this project. * [ ] Included examples of basic assertions, QuickCheck, Hspec, hspec-quickcheck. -* [x] Look at Lenses library for abstraction ## Design considerations The biggest design constraint is that for the exec stack (but not data stacks) From 9a3453ad5f1d44db082119e12856a55d07f797ca Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Fri, 7 Feb 2025 17:53:16 -0600 Subject: [PATCH 127/171] more todo --- TODO.md | 1 + 1 file changed, 1 insertion(+) diff --git a/TODO.md b/TODO.md index 8e6509c..fb2c8e9 100644 --- a/TODO.md +++ b/TODO.md @@ -14,6 +14,7 @@ - [X] Refactor all functions to take state as the final parameter - [X] Standardize the pattern matching parameter names, such as c1 : cs - [ ] Write unit/quickcheck tests for all of the instructions +- [ ] Move utility functions to their own file ## PushGP TODO - [ ] Implement a Plushy genome translator From de18d828a930df98d3b9a69b9893f98d41e2fdeb Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Fri, 7 Feb 2025 17:55:12 -0600 Subject: [PATCH 128/171] documentation, time to fix my sleep schedule --- src/HushGP/Instructions.hs | 3 +- src/HushGP/Instructions/BoolInstructions.hs | 28 ++++++++++++- src/HushGP/Instructions/CharInstructions.hs | 41 +++++++++++++++++++ src/HushGP/Instructions/CodeInstructions.hs | 20 +++++++++ .../Instructions/GenericInstructions.hs | 4 +- src/HushGP/Instructions/StringInstructions.hs | 3 ++ 6 files changed, 95 insertions(+), 4 deletions(-) diff --git a/src/HushGP/Instructions.hs b/src/HushGP/Instructions.hs index b2b4a99..2e4d6a1 100644 --- a/src/HushGP/Instructions.hs +++ b/src/HushGP/Instructions.hs @@ -316,7 +316,8 @@ allStringInstructions = map StateFunc [ (instructionStringSort, "instructionStringSort"), (instructionStringSortReverse, "instructionStringSortReverse"), (instructionStringDupItems, "instructionStringDupItems"), - (instructionStringParseToChar, "instructionStringParseToChar") + (instructionStringParseToChar, "instructionStringParseToChar"), + (instructionStringSubString, "instructionStringSubString") ] allVectorIntInstructions :: [Gene] diff --git a/src/HushGP/Instructions/BoolInstructions.hs b/src/HushGP/Instructions/BoolInstructions.hs index 2d55641..e77eb07 100644 --- a/src/HushGP/Instructions/BoolInstructions.hs +++ b/src/HushGP/Instructions/BoolInstructions.hs @@ -3,80 +3,106 @@ module HushGP.Instructions.BoolInstructions where import HushGP.State import HushGP.Instructions.GenericInstructions +-- |If top of int stack /= 0 pushes true to bool stack, else false instructionBoolFromInt :: State -> State instructionBoolFromInt state@(State {_int = i1 : is, _bool = bs}) = state {_int = is, _bool = (i1 /= 0) : bs} instructionBoolFromInt state = state +-- |If top of float stack /= 0 pushes true to bool stack, else false instructionBoolFromFloat :: State -> State instructionBoolFromFloat state@(State {_float = f1 : fs, _bool = bs}) = state {_float = fs, _bool = (f1 /= 0) : bs} instructionBoolFromFloat state = state +-- |A template function to make bool comparisons concise boolTemplate :: (Bool -> Bool -> Bool) -> State -> State boolTemplate func state@(State {_bool = b1 : b2 : bs}) = state {_bool = func b1 b2 : bs} boolTemplate _ state = state +-- |Takes the top two bools and ands them instructionBoolAnd :: State -> State instructionBoolAnd = boolTemplate (&&) +-- |Takes the top two bools, inverts the first bool and then ands the modified state instructionBoolInvertFirstThenAnd :: State -> State instructionBoolInvertFirstThenAnd state@(State {_bool = b1 : bs}) = boolTemplate (&&) state {_bool = not b1 : bs} instructionBoolInvertFirstThenAnd state = state +-- |Takes the top two bools, inverts the second bool and then ands the modified state instructionBoolInvertSecondThenAnd :: State -> State instructionBoolInvertSecondThenAnd state@(State {_bool = b1 : b2 : bs}) = boolTemplate (&&) state {_bool = b1 : not b2 : bs} instructionBoolInvertSecondThenAnd state = state +-- |Takes the top two bools and ors them instructionBoolOr :: State -> State instructionBoolOr = boolTemplate (||) --- no builtin haskell xor moment +-- |Utility function. Haskell doesn't have its own xor operation. xor :: Bool -> Bool -> Bool xor b1 b2 | b1 && not b2 = True | not b1 && b2 = True | otherwise = False +-- |Takes the xor of the top two bools instructionBoolXor :: State -> State instructionBoolXor = boolTemplate xor +-- |Pops the top of the bool stack instructionBoolPop :: State -> State instructionBoolPop = instructionPop bool +-- |Duplicates the top of the bool stack instructionBoolDup :: State -> State instructionBoolDup = instructionDup bool +-- |Duplicates the top of the bool stack based on the top int from the int stack instructionBoolDupN :: State -> State instructionBoolDupN = instructionDupN bool +-- |Swaps the top two bools instructionBoolSwap :: State -> State instructionBoolSwap = instructionSwap bool +-- |Rotates the top three bools instructionBoolRot :: State -> State instructionBoolRot = instructionRot bool +-- |Sets the bool stack to [] instructionBoolFlush :: State -> State instructionBoolFlush = instructionFlush bool +-- |Tests if the top two bools are equal and pushes the result to the bool stack instructionBoolEq :: State -> State instructionBoolEq = instructionEq bool +-- |Calculates the size of a stack and pushes the result to the int stack instructionBoolStackDepth :: State -> State instructionBoolStackDepth = instructionStackDepth bool +-- |Moves an item from deep within the bool stack to the top of the bool stack based on +-- the top int from the int stack instructionBoolYank :: State -> State instructionBoolYank = instructionYank bool +-- |Copies an item from deep within the bool stack to the top of the bool stack based on +-- the top int from the int stack instructionBoolYankDup :: State -> State instructionBoolYankDup = instructionYankDup bool +-- |Moves an item from the top of the bool stack to deep within the bool stack based on +-- the top int from the int stack instructionBoolShove :: State -> State instructionBoolShove = instructionShove bool +-- |Copies an item from the top of the bool stack to deep within the bool stack based on +-- the top int from the int stack instructionBoolShoveDup :: State -> State instructionBoolShoveDup = instructionShoveDup bool +-- |If the bool stack is empty, pushes true to bool stack, else false instructionBoolIsStackEmpty :: State -> State instructionBoolIsStackEmpty = instructionIsStackEmpty bool +-- |Duplicate the top N items from the bool stack based on the top int from the int stack instructionBoolDupItems :: State -> State instructionBoolDupItems = instructionDupItems bool diff --git a/src/HushGP/Instructions/CharInstructions.hs b/src/HushGP/Instructions/CharInstructions.hs index 79c0e00..42d9153 100644 --- a/src/HushGP/Instructions/CharInstructions.hs +++ b/src/HushGP/Instructions/CharInstructions.hs @@ -5,88 +5,129 @@ import HushGP.State import HushGP.Instructions.StringInstructions (wschars) import HushGP.Instructions.GenericInstructions +-- |Converts a whole number `mod` 128 to a char intToAscii :: Integral a => a -> Char intToAscii val = chr (abs (fromIntegral val) `mod` 128) +-- |Combines the top two chars into a string and pushes the result to the string stack instructionCharConcat :: State -> State instructionCharConcat state@(State {_char = c1 : c2 : cs, _string = ss}) = state{_char = cs, _string = [c1, c2] : ss} instructionCharConcat state = state +-- |Takes the first char from the top string and pushes it to the char stack. +-- If the string is empty, acts as a no-op instructionCharFromFirstChar :: State -> State instructionCharFromFirstChar = instructionVectorFirst char string +-- |Takes the last char from the top string and pushes it to the char stack. +-- If the string is empty, acts as a no-op instructionCharFromLastChar :: State -> State instructionCharFromLastChar = instructionVectorLast char string +-- |Takes the Nth char from the top string and pushes it to the char stack +-- based on the top int from the int stack. If the string is empty, acts as a no-op instructionCharFromNthChar :: State -> State instructionCharFromNthChar = instructionVectorNth char string +-- |Takes the top of the char stack, checks to see if it is whitespace, and then +-- pushes true to the bool stack if so, else false instructionCharIsWhitespace :: State -> State instructionCharIsWhitespace state@(State {_char = c1 : cs, _bool = bs}) = state{_char = cs, _bool = (c1 `elem` wschars) : bs} instructionCharIsWhitespace state = state +-- |Takes the top of the char stack, checks to see if it is an alphabetic character, and +-- then pushes true to the bool stack if alphabetic, false if not instructionCharIsLetter :: State -> State instructionCharIsLetter state@(State {_char = c1 : cs, _bool = bs}) = state{_char = cs, _bool = isAlpha c1 : bs} instructionCharIsLetter state = state +-- |Takes the top of the char stack, checks to see if it is a digit, and then pushes true if it is +-- a digit, false if not instructionCharIsDigit :: State -> State instructionCharIsDigit state@(State {_char = c1 : cs, _bool = bs}) = state{_char = cs, _bool = isDigit c1 : bs} instructionCharIsDigit state = state +-- |Takes the top of the bool stack, pushes 'T' to the char stack if true, 'F' to the char stack if false instructionCharFromBool :: State -> State instructionCharFromBool state@(State {_char = cs, _bool = b1 : bs}) = state{_char = (if b1 then 'T' else 'F') : cs, _bool = bs} instructionCharFromBool state = state +-- |Takes the top of the int stack, pushes the ascii representation of the int to the char stack instructionCharFromAsciiInt :: State -> State instructionCharFromAsciiInt state@(State {_char = cs, _int = i1 : is}) = state{_char = intToAscii i1 : cs, _int = is} instructionCharFromAsciiInt state = state +-- |Takes the top of the float stack, pushes the ascii representation of the floored float to the char stack instructionCharFromAsciiFloat :: State -> State instructionCharFromAsciiFloat state@(State {_char = cs, _float = f1 : fs}) = state{_char = intToAscii @Integer (floor f1) : cs, _float = fs} instructionCharFromAsciiFloat state = state +-- |Pushes the top string to the char stack split up into individual chars +-- For example: have the string "hello" and the char stack ['a', 'b', 'c'], the char stack +-- looks like ['h', 'e', 'l', 'l', 'o', 'a', 'b', 'c'] instructionCharsFromString :: State -> State instructionCharsFromString state@(State {_char = cs, _string = s1 : ss}) = state{_char = s1 <> cs, _string = ss} instructionCharsFromString state = state +-- |Pops the top of the char stack instructionCharPop :: State -> State instructionCharPop = instructionPop char +-- |Duplicates the top of the char stack instructionCharDup :: State -> State instructionCharDup = instructionDup char +-- |Duplicates the top of the char stack N times based on the top of +-- int stack instructionCharDupN :: State -> State instructionCharDupN = instructionDupN char +-- |Swaps the top two chars of the char stack. instructionCharSwap :: State -> State instructionCharSwap = instructionSwap char +-- |Rotates the top three chars of the char stack instructionCharRot :: State -> State instructionCharRot = instructionRot char +-- |Sets the char stack to [] instructionCharFlush :: State -> State instructionCharFlush = instructionFlush char +-- |Checks to see if the top two chars to equal and pushes the result +-- to the bool stack instructionCharEq :: State -> State instructionCharEq = instructionEq char +-- |Calculates the stack depth of the char stack. Pushes the result +-- to the int stack. instructionCharStackDepth :: State -> State instructionCharStackDepth = instructionStackDepth char +-- |Moves an item from deep within the char stack to the top of the char stack based on +-- the top int from the int stack instructionCharYank :: State -> State instructionCharYank = instructionYank char +-- |Copies an item from deep within the char stack to the top of the char stack based on +-- the top int from the int stack instructionCharYankDup :: State -> State instructionCharYankDup = instructionYankDup char +-- |Pushes the size of the char stack to the int stack instructionCharIsStackEmpty :: State -> State instructionCharIsStackEmpty = instructionIsStackEmpty char +-- |Moves an item from the top of the char stack to deep within the char stack based on +-- the top int from the int stack instructionCharShove :: State -> State instructionCharShove = instructionShove char +-- |Copies an item from the top of the char stack to deep within the char stack based on +-- the top int from the int stack instructionCharShoveDup :: State -> State instructionCharShoveDup = instructionShoveDup char +-- |Duplicate the top N items from the char stack based on the top int from the int stack instructionCharDupItems :: State -> State instructionCharDupItems = instructionDupItems char diff --git a/src/HushGP/Instructions/CodeInstructions.hs b/src/HushGP/Instructions/CodeInstructions.hs index 016cafe..c5cc085 100644 --- a/src/HushGP/Instructions/CodeInstructions.hs +++ b/src/HushGP/Instructions/CodeInstructions.hs @@ -6,18 +6,25 @@ import HushGP.Instructions.GenericInstructions import HushGP.Instructions.IntInstructions -- import Debug.Trace +-- |Utility function: Checks to see if a gene is a code block. +-- If it is a block, returns true, else returns false isBlock :: Gene -> Bool isBlock (Block _) = True isBlock _ = False +-- |Utility function: Returns the length of the passed block. +-- If the gene isn't a block, returns 1 blockLength :: Gene -> Int blockLength (Block bxs) = length bxs blockLength _ = 1 +-- |Utility function: Returns true if the passed block is empty, false is not. +-- If the passed gene is not a block, returns false blockIsNull :: Gene -> Bool blockIsNull (Block bxs) = null bxs blockIsNull _ = False +-- |Utility Function: A helper function for instructionCodeContainer. The full description is there. -- https://faculty.hampshire.edu/lspector/push3-description.html#Type -- CODE.CONTAINER findContainer :: Gene -> Gene -> Gene @@ -33,6 +40,7 @@ findContainer (Block fullA) gene findContainer' _ _ = Block [] -- This should never happen findContainer _ _ = Block [] +-- |Utility Function: A helper function for instructionCodeDiscrepancy. The full description is there. countDiscrepancy :: Gene -> Gene -> Int countDiscrepancy (Block xs) (Block ys) = sum [if uncurry (==) tup then 0 else 1 | tup <- zip xs ys] + abs (length xs - length ys) countDiscrepancy xgene ygene = if xgene == ygene then 1 else 0 @@ -333,10 +341,22 @@ instructionCodeFromVectorChar = instructionCodeFrom vectorChar GeneVectorChar instructionCodeFromExec :: State -> State instructionCodeFromExec = instructionCodeFrom exec id +-- |Pushes the "container" of the second code stack item within +-- the first code stack item onto the code stack. If second item contains the first +-- anywhere (i.e. in any nested list) then the container is the smallest sub-list that +-- contains but is not equal to the first instance. For example, if the top piece of code +-- is "( B ( C ( A ) ) ( D ( A ) ) )" and the second piece of code is "( A )" then +-- this pushes ( C ( A ) ). Pushes an empty list if there is no such container. instructionCodeContainer :: State -> State instructionCodeContainer state@(State {_code = c1 : c2 : cs}) = state {_code = findContainer c1 c2 : cs} instructionCodeContainer state = state +-- |Pushes a measure of the discrepancy between the top two CODE stack items onto the INTEGER stack. This will be zero if the top two items +-- are equivalent, and will be higher the 'more different' the items are from one another. The calculation is as follows: +-- 1. Construct a list of all of the unique items in both of the lists (where uniqueness is determined by equalp). Sub-lists and atoms all count as items. +-- 2. Initialize the result to zero. +-- 3. For each unique item increment the result by the difference between the number of occurrences of the item in the two pieces of code. +-- 4. Push the result. instructionCodeDiscrepancy :: State -> State instructionCodeDiscrepancy state@(State {_code = c1 : c2 : cs, _int = is}) = state {_code = cs, _int = countDiscrepancy c1 c2 : is} instructionCodeDiscrepancy state = state diff --git a/src/HushGP/Instructions/GenericInstructions.hs b/src/HushGP/Instructions/GenericInstructions.hs index d529ead..09026f4 100644 --- a/src/HushGP/Instructions/GenericInstructions.hs +++ b/src/HushGP/Instructions/GenericInstructions.hs @@ -315,8 +315,8 @@ instructionVectorOccurrencesOf primAccessor vectorAccessor state = (Just (v1, vs), Just (p1, ps)) -> (state & vectorAccessor .~ vs & primAccessor .~ ps) & int .~ (amtOccurences v1 [p1] : view int (state & vectorAccessor .~ vs & primAccessor .~ ps)) _ -> state --- | This function parses the primitives of a vector type and pushes split up onto their --- respective stack +-- | This function parses the primitives of a vector type and pushes that vector split into +-- lists of size one onto the respective vector stack. instructionVectorParseToPrim :: Lens' State [[a]] -> State -> State instructionVectorParseToPrim accessor state = case uncons (view accessor state) of diff --git a/src/HushGP/Instructions/StringInstructions.hs b/src/HushGP/Instructions/StringInstructions.hs index e32df23..cd241f1 100644 --- a/src/HushGP/Instructions/StringInstructions.hs +++ b/src/HushGP/Instructions/StringInstructions.hs @@ -244,3 +244,6 @@ instructionStringDupItems = instructionDupItems string instructionStringParseToChar :: State -> State instructionStringParseToChar = instructionVectorParseToPrim string + +instructionStringSubString :: State -> State +instructionStringSubString = instructionSubVector string From ebaf1dfc20c32aecf11a009e4c3a8c268b69fa34 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sat, 8 Feb 2025 20:57:24 -0600 Subject: [PATCH 129/171] more documentation, more to go --- src/HushGP/Instructions/BoolInstructions.hs | 40 ++++---- src/HushGP/Instructions/CharInstructions.hs | 52 +++++------ src/HushGP/Instructions/CodeInstructions.hs | 93 ++++++++++++++++++- src/HushGP/Instructions/ExecInstructions.hs | 43 ++++++++- src/HushGP/Instructions/FloatInstructions.hs | 38 ++++++++ .../Instructions/GenericInstructions.hs | 76 +++++++++++---- 6 files changed, 270 insertions(+), 72 deletions(-) diff --git a/src/HushGP/Instructions/BoolInstructions.hs b/src/HushGP/Instructions/BoolInstructions.hs index e77eb07..d6a96ae 100644 --- a/src/HushGP/Instructions/BoolInstructions.hs +++ b/src/HushGP/Instructions/BoolInstructions.hs @@ -3,36 +3,36 @@ module HushGP.Instructions.BoolInstructions where import HushGP.State import HushGP.Instructions.GenericInstructions --- |If top of int stack /= 0 pushes true to bool stack, else false +-- |If top of int stack /= 0 pushes True to bool stack, else false. instructionBoolFromInt :: State -> State instructionBoolFromInt state@(State {_int = i1 : is, _bool = bs}) = state {_int = is, _bool = (i1 /= 0) : bs} instructionBoolFromInt state = state --- |If top of float stack /= 0 pushes true to bool stack, else false +-- |If top of float stack /= 0 pushes True to bool stack, else false. instructionBoolFromFloat :: State -> State instructionBoolFromFloat state@(State {_float = f1 : fs, _bool = bs}) = state {_float = fs, _bool = (f1 /= 0) : bs} instructionBoolFromFloat state = state --- |A template function to make bool comparisons concise +-- |A template function to make bool comparisons concise. boolTemplate :: (Bool -> Bool -> Bool) -> State -> State boolTemplate func state@(State {_bool = b1 : b2 : bs}) = state {_bool = func b1 b2 : bs} boolTemplate _ state = state --- |Takes the top two bools and ands them +-- |Takes the top two bools and Ands them. instructionBoolAnd :: State -> State instructionBoolAnd = boolTemplate (&&) --- |Takes the top two bools, inverts the first bool and then ands the modified state +-- |Takes the top two bools, inverts the first bool and then Ands the modified state. instructionBoolInvertFirstThenAnd :: State -> State instructionBoolInvertFirstThenAnd state@(State {_bool = b1 : bs}) = boolTemplate (&&) state {_bool = not b1 : bs} instructionBoolInvertFirstThenAnd state = state --- |Takes the top two bools, inverts the second bool and then ands the modified state +-- |Takes the top two bools, inverts the second bool and then Ands the modified state. instructionBoolInvertSecondThenAnd :: State -> State instructionBoolInvertSecondThenAnd state@(State {_bool = b1 : b2 : bs}) = boolTemplate (&&) state {_bool = b1 : not b2 : bs} instructionBoolInvertSecondThenAnd state = state --- |Takes the top two bools and ors them +-- |Takes the top two bools and Ors them. instructionBoolOr :: State -> State instructionBoolOr = boolTemplate (||) @@ -43,27 +43,27 @@ xor b1 b2 | not b1 && b2 = True | otherwise = False --- |Takes the xor of the top two bools +-- |Takes the xor of the top two bools. instructionBoolXor :: State -> State instructionBoolXor = boolTemplate xor --- |Pops the top of the bool stack +-- |Pops the top of the bool stack. instructionBoolPop :: State -> State instructionBoolPop = instructionPop bool --- |Duplicates the top of the bool stack +-- |Duplicates the top of the bool stack. instructionBoolDup :: State -> State instructionBoolDup = instructionDup bool --- |Duplicates the top of the bool stack based on the top int from the int stack +-- |Duplicates the top of the bool stack based on the top int from the int stack. instructionBoolDupN :: State -> State instructionBoolDupN = instructionDupN bool --- |Swaps the top two bools +-- |Swaps the top two bools. instructionBoolSwap :: State -> State instructionBoolSwap = instructionSwap bool --- |Rotates the top three bools +-- |Rotates the top three bools. instructionBoolRot :: State -> State instructionBoolRot = instructionRot bool @@ -71,11 +71,11 @@ instructionBoolRot = instructionRot bool instructionBoolFlush :: State -> State instructionBoolFlush = instructionFlush bool --- |Tests if the top two bools are equal and pushes the result to the bool stack +-- |Tests if the top two bools are equal and pushes the result to the bool stack. instructionBoolEq :: State -> State instructionBoolEq = instructionEq bool --- |Calculates the size of a stack and pushes the result to the int stack +-- |Calculates the size of a stack and pushes the result to the int stack. instructionBoolStackDepth :: State -> State instructionBoolStackDepth = instructionStackDepth bool @@ -85,24 +85,24 @@ instructionBoolYank :: State -> State instructionBoolYank = instructionYank bool -- |Copies an item from deep within the bool stack to the top of the bool stack based on --- the top int from the int stack +-- the top int from the int stack. instructionBoolYankDup :: State -> State instructionBoolYankDup = instructionYankDup bool -- |Moves an item from the top of the bool stack to deep within the bool stack based on --- the top int from the int stack +-- the top int from the int stack. instructionBoolShove :: State -> State instructionBoolShove = instructionShove bool -- |Copies an item from the top of the bool stack to deep within the bool stack based on --- the top int from the int stack +-- the top int from the int stack. instructionBoolShoveDup :: State -> State instructionBoolShoveDup = instructionShoveDup bool --- |If the bool stack is empty, pushes true to bool stack, else false +-- |If the bool stack is empty, pushes True to bool stack, else False. instructionBoolIsStackEmpty :: State -> State instructionBoolIsStackEmpty = instructionIsStackEmpty bool --- |Duplicate the top N items from the bool stack based on the top int from the int stack +-- |Duplicate the top N items from the bool stack based on the top int from the int stack. instructionBoolDupItems :: State -> State instructionBoolDupItems = instructionDupItems bool diff --git a/src/HushGP/Instructions/CharInstructions.hs b/src/HushGP/Instructions/CharInstructions.hs index 42d9153..20f81c8 100644 --- a/src/HushGP/Instructions/CharInstructions.hs +++ b/src/HushGP/Instructions/CharInstructions.hs @@ -5,80 +5,80 @@ import HushGP.State import HushGP.Instructions.StringInstructions (wschars) import HushGP.Instructions.GenericInstructions --- |Converts a whole number `mod` 128 to a char +-- |Converts a whole number `mod` 128 to a char. intToAscii :: Integral a => a -> Char intToAscii val = chr (abs (fromIntegral val) `mod` 128) --- |Combines the top two chars into a string and pushes the result to the string stack +-- |Combines the top two chars into a string and pushes the result to the string stack. instructionCharConcat :: State -> State instructionCharConcat state@(State {_char = c1 : c2 : cs, _string = ss}) = state{_char = cs, _string = [c1, c2] : ss} instructionCharConcat state = state -- |Takes the first char from the top string and pushes it to the char stack. --- If the string is empty, acts as a no-op +-- If the string is empty, acts as a no-op. instructionCharFromFirstChar :: State -> State instructionCharFromFirstChar = instructionVectorFirst char string -- |Takes the last char from the top string and pushes it to the char stack. --- If the string is empty, acts as a no-op +-- If the string is empty, acts as a no-op. instructionCharFromLastChar :: State -> State instructionCharFromLastChar = instructionVectorLast char string -- |Takes the Nth char from the top string and pushes it to the char stack --- based on the top int from the int stack. If the string is empty, acts as a no-op +-- based on the top int from the int stack. If the string is empty, acts as a no-op. instructionCharFromNthChar :: State -> State instructionCharFromNthChar = instructionVectorNth char string -- |Takes the top of the char stack, checks to see if it is whitespace, and then --- pushes true to the bool stack if so, else false +-- pushes True to the bool stack if so, else false. instructionCharIsWhitespace :: State -> State instructionCharIsWhitespace state@(State {_char = c1 : cs, _bool = bs}) = state{_char = cs, _bool = (c1 `elem` wschars) : bs} instructionCharIsWhitespace state = state -- |Takes the top of the char stack, checks to see if it is an alphabetic character, and --- then pushes true to the bool stack if alphabetic, false if not +-- then pushes True to the bool stack if alphabetic, false if not. instructionCharIsLetter :: State -> State instructionCharIsLetter state@(State {_char = c1 : cs, _bool = bs}) = state{_char = cs, _bool = isAlpha c1 : bs} instructionCharIsLetter state = state --- |Takes the top of the char stack, checks to see if it is a digit, and then pushes true if it is --- a digit, false if not +-- |Takes the top of the char stack, checks to see if it is a digit, and then pushes True if it is +-- a digit, False if not. instructionCharIsDigit :: State -> State instructionCharIsDigit state@(State {_char = c1 : cs, _bool = bs}) = state{_char = cs, _bool = isDigit c1 : bs} instructionCharIsDigit state = state --- |Takes the top of the bool stack, pushes 'T' to the char stack if true, 'F' to the char stack if false +-- |Takes the top of the bool stack, pushes 'T' to the char stack if True, 'F' to the char stack if False. instructionCharFromBool :: State -> State instructionCharFromBool state@(State {_char = cs, _bool = b1 : bs}) = state{_char = (if b1 then 'T' else 'F') : cs, _bool = bs} instructionCharFromBool state = state --- |Takes the top of the int stack, pushes the ascii representation of the int to the char stack +-- |Takes the top of the int stack, pushes the ascii representation of the int to the char stack. instructionCharFromAsciiInt :: State -> State instructionCharFromAsciiInt state@(State {_char = cs, _int = i1 : is}) = state{_char = intToAscii i1 : cs, _int = is} instructionCharFromAsciiInt state = state --- |Takes the top of the float stack, pushes the ascii representation of the floored float to the char stack +-- |Takes the top of the float stack, pushes the ascii representation of the floored float to the char stack. instructionCharFromAsciiFloat :: State -> State instructionCharFromAsciiFloat state@(State {_char = cs, _float = f1 : fs}) = state{_char = intToAscii @Integer (floor f1) : cs, _float = fs} instructionCharFromAsciiFloat state = state --- |Pushes the top string to the char stack split up into individual chars +-- |Pushes the top string to the char stack split up into individual chars. -- For example: have the string "hello" and the char stack ['a', 'b', 'c'], the char stack --- looks like ['h', 'e', 'l', 'l', 'o', 'a', 'b', 'c'] +-- looks like ['h', 'e', 'l', 'l', 'o', 'a', 'b', 'c']. instructionCharsFromString :: State -> State instructionCharsFromString state@(State {_char = cs, _string = s1 : ss}) = state{_char = s1 <> cs, _string = ss} instructionCharsFromString state = state --- |Pops the top of the char stack +-- |Pops the top of the char stack. instructionCharPop :: State -> State instructionCharPop = instructionPop char --- |Duplicates the top of the char stack +-- |Duplicates the top of the char stack. instructionCharDup :: State -> State instructionCharDup = instructionDup char -- |Duplicates the top of the char stack N times based on the top of --- int stack +-- int stack. instructionCharDupN :: State -> State instructionCharDupN = instructionDupN char @@ -86,16 +86,16 @@ instructionCharDupN = instructionDupN char instructionCharSwap :: State -> State instructionCharSwap = instructionSwap char --- |Rotates the top three chars of the char stack +-- |Rotates the top three chars of the char stack. instructionCharRot :: State -> State instructionCharRot = instructionRot char --- |Sets the char stack to [] +-- |Sets the char stack to []. instructionCharFlush :: State -> State instructionCharFlush = instructionFlush char -- |Checks to see if the top two chars to equal and pushes the result --- to the bool stack +-- to the bool stack. instructionCharEq :: State -> State instructionCharEq = instructionEq char @@ -105,29 +105,29 @@ instructionCharStackDepth :: State -> State instructionCharStackDepth = instructionStackDepth char -- |Moves an item from deep within the char stack to the top of the char stack based on --- the top int from the int stack +-- the top int from the int stack. instructionCharYank :: State -> State instructionCharYank = instructionYank char -- |Copies an item from deep within the char stack to the top of the char stack based on --- the top int from the int stack +-- the top int from the int stack. instructionCharYankDup :: State -> State instructionCharYankDup = instructionYankDup char --- |Pushes the size of the char stack to the int stack +-- |Pushes True to the bool stack if the char stack is empty. False if not. instructionCharIsStackEmpty :: State -> State instructionCharIsStackEmpty = instructionIsStackEmpty char -- |Moves an item from the top of the char stack to deep within the char stack based on --- the top int from the int stack +-- the top int from the int stack. instructionCharShove :: State -> State instructionCharShove = instructionShove char -- |Copies an item from the top of the char stack to deep within the char stack based on --- the top int from the int stack +-- the top int from the int stack. instructionCharShoveDup :: State -> State instructionCharShoveDup = instructionShoveDup char --- |Duplicate the top N items from the char stack based on the top int from the int stack +-- |Duplicate the top N items from the char stack based on the top int from the int stack. instructionCharDupItems :: State -> State instructionCharDupItems = instructionDupItems char diff --git a/src/HushGP/Instructions/CodeInstructions.hs b/src/HushGP/Instructions/CodeInstructions.hs index c5cc085..2248de5 100644 --- a/src/HushGP/Instructions/CodeInstructions.hs +++ b/src/HushGP/Instructions/CodeInstructions.hs @@ -45,82 +45,104 @@ countDiscrepancy :: Gene -> Gene -> Int countDiscrepancy (Block xs) (Block ys) = sum [if uncurry (==) tup then 0 else 1 | tup <- zip xs ys] + abs (length xs - length ys) countDiscrepancy xgene ygene = if xgene == ygene then 1 else 0 +-- |Utility Function: Extracts the first gene from a block. Returns itself if not a block extractFirstFromBlock :: Gene -> Gene extractFirstFromBlock (Block (bx1 : _)) = bx1 extractFirstFromBlock gene = gene +-- |Utility Function: Returns the last gene from a block, [] if the block is empty, and itself if not a block extractLastFromBlock :: Gene -> Gene extractLastFromBlock (Block []) = Block [] extractLastFromBlock (Block bxs) = last bxs extractLastFromBlock gene = gene +-- |Utility Function: Calls init on a block. If the block is empty, returns []. If gene isn't a block, returns itself extractInitFromBlock :: Gene -> Gene -extractInitFromBlock (Block []) = Block [] -extractInitFromBlock (Block bxs) = Block (init bxs) +extractInitFromBlock (Block bxs) = Block (safeInit bxs) extractInitFromBlock gene = gene +-- |Utility Function: Calls `drop 1` on a block. If gene isn't a block, returns itself extractTailFromBlock :: Gene -> Gene extractTailFromBlock (Block bxs) = Block (drop 1 bxs) extractTailFromBlock _ = Block [] +-- |Utility Function: Extracts the code at a point in the genome. Recurses into a nested Block if found. The +-- point is based on an int. codeAtPoint :: [Gene] -> Int -> Gene codeAtPoint (gene : _) 0 = gene codeAtPoint [] _ = Block [] -- Should only happen if an empty block is last Gene in the list of Genes codeAtPoint ((Block nestedGenes) : genes) index = codeAtPoint (nestedGenes <> genes) (index - 1) codeAtPoint (_ : genes) index = codeAtPoint genes (index - 1) +-- |Utility Function: Inserts code at a point in the genome. Recurses into a block if found. The point is based +-- on an integer codeInsertAtPoint :: [Gene] -> Gene -> Int -> [Gene] codeInsertAtPoint oldGenes gene 0 = gene : oldGenes codeInsertAtPoint [] gene _ = [gene] -- This shouldn't happen (lol) codeInsertAtPoint ((Block genes) : oldGenes) gene index = Block (codeInsertAtPoint genes gene (index - 1)) : oldGenes codeInsertAtPoint (oldGene : oldGenes) gene index = oldGene : codeInsertAtPoint oldGenes gene (index - 1) +-- |Utility Function: Combines two genes together into a block. codeCombine :: Gene -> Gene -> Gene codeCombine (Block bxs) (Block bys) = Block (bxs <> bys) codeCombine (Block bxs) ygene = Block (ygene : bxs) codeCombine xgene (Block bys) = Block (xgene : bys) codeCombine xgene ygene = Block [xgene, ygene] +-- |Utility Function: Determines if the second gene is a member of the first gene. +-- If the first gene is a Block and the second gene is also a Block, does a sublist search for the second block in the first block. +-- if the first gene is a Block and the second gene is not, the block is searched for the second gene. +-- If neither of the genes are blocks, returns False. codeMember :: Gene -> Gene -> Bool codeMember (Block bxs) (Block bys) = findSubA bxs bys /= (-1) codeMember (Block bxs) ygene = ygene `elem` bxs codeMember _ _ = False +-- |Utility Function: Calculates the size of a Block including counting the nested Blocks recursively codeRecursiveSize :: Gene -> Int codeRecursiveSize (Block bxs) = sum [codeRecursiveSize x + if isBlock x then 1 else 0 | x <- bxs] codeRecursiveSize _ = 1 +-- |Pops the top of the code stack instructionCodePop :: State -> State instructionCodePop = instructionPop code +-- |Checks if the top code item is a Block instructionCodeIsCodeBlock :: State -> State instructionCodeIsCodeBlock state@(State {_code = c1 : cs, _bool = bs}) = state {_code = cs, _bool = isBlock c1 : bs} instructionCodeIsCodeBlock state = state +-- |Checks if the top code item is not a Block instructionCodeIsSingular :: State -> State instructionCodeIsSingular state@(State {_code = c1 : cs, _bool = bs}) = state {_code = cs, _bool = not (isBlock c1) : bs} instructionCodeIsSingular state = state +-- |Checks the length of the top code item. If item is a block, counts the size, if not, returns 1 instructionCodeLength :: State -> State instructionCodeLength state@(State {_code = c1 : cs, _int = is}) = state {_code = cs, _int = blockLength c1 : is} instructionCodeLength state = state -- CODE.CAR +-- |If the top item on the code stack is a Block, extracts the first item and places it onto the code stack. Acts as a NoOp otherwise. instructionCodeFirst :: State -> State instructionCodeFirst state@(State {_code = c1 : cs}) = state {_code = extractFirstFromBlock c1 : cs} instructionCodeFirst state = state +-- |If the top item on the code stack is a Block, extracts the last item and places it onto the code stack. Acts as a NoOp otherwise. instructionCodeLast :: State -> State instructionCodeLast state@(State {_code = c1 : cs}) = state {_code = extractLastFromBlock c1 : cs} instructionCodeLast state = state +-- |If the top item on the code stack is a Block, extracts the tail of said Block and places it onto the code stace. Acts as a NoOp otherwise. -- CODE.CDR -- https://erp12.github.io/pyshgp/html/core_instructions.html#code-rest instructionCodeTail :: State -> State instructionCodeTail state@(State {_code = c1 : cs}) = state {_code = extractTailFromBlock c1 : cs} instructionCodeTail state = state --- |Takes the tail of a block starting at an index determined by the int stack +-- |If the top item on the code stack is a Block, takes the tail of said block starting at an index determined by the int stack +-- and pushes the result to the code stack. +-- Acts as a NoOp if not a Block. -- https://faculty.hampshire.edu/lspector/push3-description.html#Type -- This is the CODE.NTHCDR command instructionCodeTailN :: State -> State @@ -130,42 +152,55 @@ instructionCodeTailN state@(State {_code = Block bc : cs, _int = i : is}) = stat index = abs i `mod` length bc instructionCodeTailN state = state +-- |If the top item on the code stack is a Block, takes the init of said Block and places the result on top of the code stack. +-- Acts as a NoOp otherwise -- https://erp12.github.io/pyshgp/html/core_instructions.html#code-but-last instructionCodeInit :: State -> State instructionCodeInit state@(State {_code = c1 : cs}) = state {_code = extractInitFromBlock c1 : cs} instructionCodeInit state = state +-- |Wraps the top item in the code stack in a Block no matter the type. instructionCodeWrap :: State -> State instructionCodeWrap state@(State {_code = c1 : cs}) = state {_code = Block [c1] : cs} instructionCodeWrap state = state +-- |Wraps the top two items in the code stack in a Block no matter the type. instructionCodeList :: State -> State instructionCodeList state@(State {_code = c1 : c2 : cs}) = state {_code = Block [c1, c2] : cs} instructionCodeList state = state +-- |Combines the top two items on the code stack based on whether they are a block or not. +-- Check out the codeCombine utility function for how this works. instructionCodeCombine :: State -> State instructionCodeCombine state@(State {_code = c1 : c2 : cs}) = state {_code = codeCombine c1 c2 : cs} instructionCodeCombine state = state +-- |Moves the top item from the code stack to the exec stack instructionCodeDo :: State -> State instructionCodeDo state@(State {_code = c1 : cs, _exec = es}) = state {_code = cs, _exec = c1 : es} instructionCodeDo state = state +-- |Moves the top item from the code stack to the exec stack, doesn't delete the original item from the code stack. instructionCodeDoDup :: State -> State instructionCodeDoDup state@(State {_code = c1 : cs, _exec = es}) = state {_code = c1 : cs, _exec = c1 : es} instructionCodeDoDup state = state +-- |Places the top code item onto the exec stack (doesn't delete it from the code stack), then places an instructionCodePop onto +-- the exec stack. -- https://erp12.github.io/pyshgp/html/core_instructions.html#code-do-then-pop instructionCodeDoThenPop :: State -> State instructionCodeDoThenPop state@(State {_code = c1 : _, _exec = es}) = state {_exec = c1 : StateFunc (instructionCodePop, "instructionCodePop") : es} instructionCodeDoThenPop state = state +-- |Utility: A shorthand for instrucitonCodeFromExec to make code instructions less bloated codeFromExec :: Gene codeFromExec = StateFunc (instructionCodeFromExec, "instructionCodeFromExec") +-- |Utility: A shorthand for instructionCodoDoRange to make code instructions less bloated codeDoRange :: Gene codeDoRange = StateFunc (instructionCodeDoRange, "instructionCodeDoRange") +-- |Evaluates the top item on the code stack for each step along the range i to j. Both i and j are taken from the int stack. instructionCodeDoRange :: State -> State instructionCodeDoRange state@(State {_code = c1 : cs, _int = i0 : i1 : is, _exec = es}) = if increment i0 i1 /= 0 @@ -179,6 +214,7 @@ instructionCodeDoRange state@(State {_code = c1 : cs, _int = i0 : i1 : is, _exec | otherwise = 0 instructionCodeDoRange state = state +-- |Evaluates the top item on the code stack for each step along the range i to j. Both i and j are taken from the int stack. instructionCodeDoCount :: State -> State instructionCodeDoCount state@(State {_code = c : cs, _int = i1 : is, _exec = es}) = if i1 < 1 @@ -186,6 +222,7 @@ instructionCodeDoCount state@(State {_code = c : cs, _int = i1 : is, _exec = es} else state {_code = cs, _int = is, _exec = Block [GeneInt 0, GeneInt $ i1 - 1, codeFromExec, c, codeDoRange] : es} instructionCodeDoCount state = state +-- |Evaluates the top item on the code stack n times, where n comes from the n comes from the top of the int stack. instructionCodeDoTimes :: State -> State instructionCodeDoTimes state@(State {_code = c : cs, _int = i1 : is, _exec = es}) = if i1 < 1 @@ -193,18 +230,23 @@ instructionCodeDoTimes state@(State {_code = c : cs, _int = i1 : is, _exec = es} else state {_code = cs, _int = is, _exec = Block [GeneInt 0, GeneInt $ i1 - 1, codeFromExec, Block [StateFunc (instructionIntPop, "instructionIntPop"), c], codeDoRange] : es} instructionCodeDoTimes state = state +-- |If the top boolean is true, execute the top element of the code stack and skip the second. Otherwise, skip the top element of the code stack and execute the second. instructionCodeIf :: State -> State instructionCodeIf state@(State {_code = c1 : c2 : cs, _bool = b1 : bs, _exec = es}) = state{_code = cs, _bool = bs, _exec = (if b1 then c1 else c2) : es} instructionCodeIf state = state +-- |Evalutates the top code item if the top bool is true. Otherwise the top code is popped. instructionCodeWhen :: State -> State instructionCodeWhen state@(State {_code = c1 : cs, _bool = b1 : bs, _exec = es}) = state{_code = cs, _bool = bs, _exec = if b1 then c1 : es else es} instructionCodeWhen state = state +-- |Pushes true to the bool stack if the second to top code item is found within the first code item. Pushes False if not. instructionCodeMember :: State -> State instructionCodeMember state@(State {_code = c1 : c2 : cs, _bool = bs}) = state{_code = cs, _bool = codeMember c1 c2 : bs} instructionCodeMember state = state +-- |Pushes the nth element from a Block onto the code stack based on an index from the int stack. +-- If the top of the code stack is not a block, the int is still eaten. -- This one doesn't count the recursive Blocks while instructionCodeExtract does -- https://erp12.github.io/pyshgp/html/core_instructions.html#code-nth instructionCodeN :: State -> State @@ -218,17 +260,24 @@ instructionCodeN state@(State {_code = (Block c1) : cs, _int = i1 : is}) = instructionCodeN state@(State {_code = c1 : cs, _int = _ : is}) = state {_code = c1 : cs, _int = is} instructionCodeN state = state +-- |Makes an empty Block and pushes it to the top of the code stack. instructionMakeEmptyCodeBlock :: State -> State instructionMakeEmptyCodeBlock state@(State {_code = cs}) = state {_code = Block [] : cs} +-- |If the top of the code stack is a Block, pushes True to the bool stack if it is and False if it's not. +-- If the top item of the code stack is not a Block, False gets pushed to the bool stack instructionIsEmptyCodeBlock :: State -> State instructionIsEmptyCodeBlock state@(State {_code = Block c1 : cs, _bool = bs}) = state{_code = cs, _bool = null c1 : bs} -instructionIsEmptyCodeBlock state@(State {_bool = bs}) = state{_bool = False : bs} +instructionIsEmptyCodeBlock state@(State {_code = _ : cs, _bool = bs}) = state{_code = cs, _bool = False : bs} +instructionIsEmptyCodeBlock state = state +-- |Pushes the size of the top code item to the int stack. If it's a Block, the size is counted recursively. If +-- it's not a Block, 1 gets pushed to the int stack. instructionCodeSize :: State -> State instructionCodeSize state@(State {_code = c1 : cs, _int = is}) = state{_code = cs, _int = codeRecursiveSize c1 : is} instructionCodeSize state = state +-- |Pushes the size of the top code item recursively counting the nested Blocks. -- There's a bug for this instruction in pysh where the last item in the -- top level Block isn't counted, and if passed 0, then the entire codeblock is returned. -- I designed this function differently so 0 returns the 0th element, and the last item @@ -242,6 +291,8 @@ instructionCodeExtract state@(State {_code = block@(Block c1) : cs, _int = i1 : instructionCodeExtract state@(State {_code = cs, _int = _ : is}) = state{_code = cs, _int = is} instructionCodeExtract state = state +-- |Inserts a code item into a block recursively entering the nested Blocks if needed based on the top +-- int from the int stack. If the top code item isn't a Block, coerces the top item into a Block. instructionCodeInsert :: State -> State instructionCodeInsert state@(State {_code = block@(Block c1) : c2 : cs, _int = i1 : is}) = let @@ -255,11 +306,13 @@ instructionCodeInsert state@(State {_code = c1 : c2 : cs, _int = i1 : is}) = state{_code = Block (codeInsertAtPoint [c1] c2 index) : cs, _int = is} instructionCodeInsert state = state +-- |If the top code item is a Block that is empty, pushes 0 to the int stack if c2 is also an empty Block and -1 if not. +-- If the top code item is a Block that is not empty, pushes the index found of the second code item if found, -1 if not. +-- If neither the top code item or second code item are Blocks, checks equality. If equal, pushes 1 to int stack, pushes 0 if not. instructionCodeFirstPosition :: State -> State instructionCodeFirstPosition state@(State {_code = (Block []) : c2 : cs, _int = is}) = state {_code = cs, _int = (if c2 == Block [] then 0 else -1) : is} instructionCodeFirstPosition state@(State {_code = (Block c1) : c2 : cs, _int = is}) = state {_code = cs, _int = positionElem c1 c2 : is} where - -- This is really not gonna be good for StateFunc positionElem :: [Gene] -> Gene -> Int positionElem genes gene = case elemIndex gene genes of @@ -268,76 +321,104 @@ instructionCodeFirstPosition state@(State {_code = (Block c1) : c2 : cs, _int = instructionCodeFirstPosition state@(State {_code = c1 : c2 : cs, _int = is}) = state {_code = cs, _int = (if c1 == c2 then 0 else -1) : is} instructionCodeFirstPosition state = state +-- |If the top of the code stack is a Block, reverses the elements of the Block. Acts as a NoOp otherwise. instructionCodeReverse :: State -> State instructionCodeReverse state@(State {_code = (Block c1) : cs}) = state {_code = Block (reverse c1) : cs} instructionCodeReverse state = state +-- |Duplicates the top of the code stack. instructionCodeDup :: State -> State instructionCodeDup = instructionDup code +-- |Duplicates the top of the code stack N times based on the top int. instructionCodeDupN :: State -> State instructionCodeDupN = instructionDupN code +-- |Swaps the top two code items. instructionCodeSwap :: State -> State instructionCodeSwap = instructionSwap code +-- |Rotates the top three code items. instructionCodeRot :: State -> State instructionCodeRot = instructionRot code +-- |Sets the code stack to [] instructionCodeFlush :: State -> State instructionCodeFlush = instructionFlush code +-- |Checks if the top code items are equal. Pushes true to the bool stack if so, False if not. instructionCodeEq :: State -> State instructionCodeEq = instructionEq code +-- |Pushes the size of the code stack to the int stack. instructionCodeStackDepth :: State -> State instructionCodeStackDepth = instructionStackDepth code +-- |Moves an item from deep within the code stack to the top of the code stack based on +-- the top int from the int stack. instructionCodeYank :: State -> State instructionCodeYank = instructionYank code +-- |Copies an item from deep within the code stack to the top of the code stack based on +-- the top int from the int stack. instructionCodeYankDup :: State -> State instructionCodeYankDup = instructionYankDup code +-- |If the code stack is empty, pushes True to bool stack, else False. instructionCodeIsStackEmpty :: State -> State instructionCodeIsStackEmpty = instructionIsStackEmpty code +-- |Moves an item from the top of the code stack to deep within the code stack based on +-- the top int from the int stack. instructionCodeShove :: State -> State instructionCodeShove = instructionShove code +-- |Copies an item from the top of the code stack to deep within the code stack based on +-- the top int from the int stack. instructionCodeShoveDup :: State -> State instructionCodeShoveDup = instructionShoveDup code +-- |Takes the top bool from the bool stack and places said GeneBool on the code stack. instructionCodeFromBool :: State -> State instructionCodeFromBool = instructionCodeFrom bool GeneBool +-- |Takes the top int from the int stack and places said GeneInt on the code stack. instructionCodeFromInt :: State -> State instructionCodeFromInt = instructionCodeFrom int GeneInt +-- |Takes the top char from the char stack and places said GeneChar on the code stack. instructionCodeFromChar :: State -> State instructionCodeFromChar = instructionCodeFrom char GeneChar +-- |Takes the top float from the float stack and places said GeneFloat on the code stack. instructionCodeFromFloat :: State -> State instructionCodeFromFloat = instructionCodeFrom float GeneFloat +-- |Takes the top string from the string stack and places said GeneString on the code stack. instructionCodeFromString :: State -> State instructionCodeFromString = instructionCodeFrom string GeneString +-- |Takes the top vectorInt from the vectorInt stack and places said GeneVectorInt on the code stack. instructionCodeFromVectorInt :: State -> State instructionCodeFromVectorInt = instructionCodeFrom vectorInt GeneVectorInt +-- |Takes the top vectorFloat from the vectorFloat stack and places said GeneVectorFloat on the code stack. instructionCodeFromVectorFloat :: State -> State instructionCodeFromVectorFloat = instructionCodeFrom vectorFloat GeneVectorFloat +-- |Takes the top vectorString from the vectorString stack and places said GeneVectorString on the code stack. instructionCodeFromVectorString :: State -> State instructionCodeFromVectorString = instructionCodeFrom vectorString GeneVectorString +-- |Takes the top vectorBool from the vectorBool stack and places said GeneVectorBool on the code stack. instructionCodeFromVectorBool :: State -> State instructionCodeFromVectorBool = instructionCodeFrom vectorBool GeneVectorBool +-- |Takes the top vectorChar from the vectorChar stack and places said GeneVectorChar on the code stack. instructionCodeFromVectorChar :: State -> State instructionCodeFromVectorChar = instructionCodeFrom vectorChar GeneVectorChar +-- |Takes the top gene from the exec stack and places a gene on the code stack. instructionCodeFromExec :: State -> State instructionCodeFromExec = instructionCodeFrom exec id @@ -361,8 +442,10 @@ instructionCodeDiscrepancy :: State -> State instructionCodeDiscrepancy state@(State {_code = c1 : c2 : cs, _int = is}) = state {_code = cs, _int = countDiscrepancy c1 c2 : is} instructionCodeDiscrepancy state = state +-- |Just a NoOp instructionCodeNoOp :: State -> State instructionCodeNoOp state = state +-- |Duplicates the top N items of the code stack based on the top of the int stack. instructionCodeDupItems :: State -> State instructionCodeDupItems = instructionDupItems code diff --git a/src/HushGP/Instructions/ExecInstructions.hs b/src/HushGP/Instructions/ExecInstructions.hs index 5602db2..8e5f53e 100644 --- a/src/HushGP/Instructions/ExecInstructions.hs +++ b/src/HushGP/Instructions/ExecInstructions.hs @@ -4,6 +4,8 @@ import HushGP.State import HushGP.Instructions.IntInstructions import HushGP.Instructions.GenericInstructions +-- |Removes the second item from the exec stack if the top of the bool stack is True. +-- Removes the first item from the exec stack if the top of the bool stack is False. instructionExecIf :: State -> State instructionExecIf state@(State {_exec = e1 : e2 : es, _bool = b1 : bs}) = if b1 @@ -11,48 +13,69 @@ instructionExecIf state@(State {_exec = e1 : e2 : es, _bool = b1 : bs}) = else state {_exec = e2 : es, _bool = bs} instructionExecIf state = state +-- |Duplicates the top exec instruction (the one after this one on the stack). instructionExecDup :: State -> State instructionExecDup = instructionDup exec +-- |Duplicates the top of the exec stack N times based on the top of +-- int stack (the exec instruction after this one). instructionExecDupN :: State -> State instructionExecDupN = instructionDupN exec +-- |Pops the top of the exec stack (the one after this on on the stack). instructionExecPop :: State -> State instructionExecPop = instructionPop exec +-- |Swaps the top two instructions on the exec stack (the two after this on the exec stack). instructionExecSwap :: State -> State instructionExecSwap = instructionSwap exec +-- |Rotates the top three instructions on the exec stack (the three after this on the exec stack). instructionExecRot :: State -> State instructionExecRot = instructionRot exec +-- |Sets the exec stack to []. This stops the program. instructionExecFlush :: State -> State instructionExecFlush = instructionFlush exec +-- |Checks if the top two exec instructions are True. instructionExecEq :: State -> State instructionExecEq = instructionEq exec +-- |Calculates the size of the exec stack and pushes the result to the int stack. instructionExecStackDepth :: State -> State instructionExecStackDepth = instructionStackDepth exec +-- |Moves an item from deep within the exec stack to the top of the exec stack based on +-- the top int from the int stack. instructionExecYank :: State -> State instructionExecYank = instructionYank exec +-- |Copies an item from deep within the exec stack to the top of the exec stack based on +-- the top int from the int stack. instructionExecYankDup :: State -> State instructionExecYankDup = instructionYankDup exec +-- |Moves an item from the top of the shove stack to deep within the shove stack based on +-- the top int from the int stack. instructionExecShove :: State -> State instructionExecShove = instructionShove exec +-- |Copies an item from the top of the shove stack to deep within the shove stack based on +-- the top int from the int stack. instructionExecShoveDup :: State -> State instructionExecShoveDup = instructionShoveDup exec +-- |If the code stack is empty, pushes True to bool stack, else False. instructionExecIsStackEmpty :: State -> State instructionExecIsStackEmpty = instructionIsStackEmpty exec +-- |Utility: Shorthand for instructionExecDoRange execDoRange :: Gene execDoRange = StateFunc (instructionExecDoRange, "instructionExecDoRange") +-- |Evaluates the top item on the exec stack for each step along the range i to j. Both i and j are +-- taken from the int stack. Differs from code_do_range only in the source of the code and the recursive call. instructionExecDoRange :: State -> State instructionExecDoRange state@(State {_exec = e1 : es, _int = i0 : i1 : is}) = if increment i0 i1 /= 0 @@ -66,6 +89,8 @@ instructionExecDoRange state@(State {_exec = e1 : es, _int = i0 : i1 : is}) = | otherwise = 0 instructionExecDoRange state = state +-- |Evaluates the top item on the exec stack n times, where n comes from the n comes from the top +-- of the int stack. Differs from code.do*count only in the source of the code and the recursive call. instructionExecDoCount :: State -> State instructionExecDoCount state@(State {_exec = e1 : es, _int = i1 : is}) = if i1 < 1 @@ -73,6 +98,7 @@ instructionExecDoCount state@(State {_exec = e1 : es, _int = i1 : is}) = else state {_exec = Block [GeneInt 0, GeneInt $ i1 - 1, execDoRange, e1] : es, _int = is} instructionExecDoCount state = state +-- |Evaluates the top item on the code stack n times, where n comes from the n comes from the top of the int stack. instructionExecDoTimes :: State -> State instructionExecDoTimes state@(State {_exec = e1 : es, _int = i1 : is}) = if i1 < 1 @@ -80,9 +106,11 @@ instructionExecDoTimes state@(State {_exec = e1 : es, _int = i1 : is}) = else state {_exec = Block [GeneInt 0, GeneInt $ i1 - 1, execDoRange, Block [StateFunc (instructionIntPop, "instructionIntPop"), e1]] : es, _int = is} instructionExecDoTimes state = state +-- |Utility: A shorthand for instructionExecWhile execWhile :: Gene execWhile = StateFunc (instructionExecWhile, "instructionExecWhile") +-- |Evaluates the top item on the exec stack repeated until the top bool is no longer True. instructionExecWhile :: State -> State instructionExecWhile state@(State {_exec = _ : es, _bool = []}) = state {_exec = es} @@ -92,12 +120,16 @@ instructionExecWhile state@(State {_exec = alles@(e1 : es), _bool = b1 : bs}) = else state {_exec = es} instructionExecWhile state = state +-- |Evaluates the top item on the exec stack repeated until the top bool is no longer True. +-- Executes at least once. instructionExecDoWhile :: State -> State instructionExecDoWhile state@(State {_exec = alles@(e1 : _)}) = state {_exec = e1 : execWhile : alles} instructionExecDoWhile state = state --- Eats the _boolean no matter what +-- |Pops the next item on the exec stack without evaluating it +-- if the top bool is False. Otherwise, has no effect. +-- Eats the top bool no matter what. instructionExecWhen :: State -> State instructionExecWhen state@(State {_exec = _ : es, _bool = b1 : bs}) = if not b1 @@ -105,20 +137,23 @@ instructionExecWhen state@(State {_exec = _ : es, _bool = b1 : bs}) = else state {_bool = bs} instructionExecWhen state = state --- |The K combinator +-- |The K combinator. Deletes the second to top exec item. instructionExecK :: State -> State instructionExecK state@(State {_exec = e1 : _ : es}) = state{_exec = e1 : es} instructionExecK state = state --- |The S combinator +-- |The S combinator. Takes the top three top exec items, pushes a Block of the second and third instruction, +-- then the third instruction, and then the first instruction. instructionExecS :: State -> State instructionExecS state@(State {_exec = e1 : e2 : e3 : es}) = state{_exec = e1 : e3 : Block [e2, e3] : es} instructionExecS state = state --- |The Y combinator +-- |The Y combinator. Takes the top exec item. Pushes a Block containing the Y combinator instruction and the top exec item. +-- Then pushes that top exec item again. instructionExecY :: State -> State instructionExecY state@(State {_exec = e1 : es}) = state{_exec = e1 : Block [StateFunc (instructionExecY, "instructionExecY"), e1] : es} instructionExecY state = state +-- |Duplicates the top N items of the exec stack based on the top of the int stack. instructionExecDupItems :: State -> State instructionExecDupItems = instructionDupItems exec diff --git a/src/HushGP/Instructions/FloatInstructions.hs b/src/HushGP/Instructions/FloatInstructions.hs index 8548399..2b7e6b5 100644 --- a/src/HushGP/Instructions/FloatInstructions.hs +++ b/src/HushGP/Instructions/FloatInstructions.hs @@ -5,18 +5,22 @@ import HushGP.Instructions.GenericInstructions import HushGP.State import Data.Char +-- |Converts the top int to a float and pushes the result to the float stack. instructionFloatFromInt :: State -> State instructionFloatFromInt state@(State {_float = fs, _int = i1 : is}) = state {_float = (fromIntegral i1 :: Float) : fs, _int = is} instructionFloatFromInt state = state +-- |If the top bool True, pushes 1.0 to the float stack. Pushes 0.0 if False. instructionFloatFromBool :: State -> State instructionFloatFromBool state@(State {_bool = b1 : bs, _float = fs}) = state {_bool = bs, _float = (if b1 then 1.0 else 0.0) : fs} instructionFloatFromBool state = state +-- |Takes the top char and converts it to int representation. That int then gets casted to a float. instructionFloatFromChar :: State -> State instructionFloatFromChar state@(State {_char = c1 : cs, _float = fs}) = state {_char = cs, _float = (fromIntegral (ord c1) :: Float) : fs} instructionFloatFromChar state = state +-- |Reads the top string and converts it to a float if possible. If not, acts as a NoOp. instructionFloatFromString :: State -> State instructionFloatFromString state@(State {_string = s1 : ss, _float = fs}) = if all isDigit s1 @@ -24,108 +28,142 @@ instructionFloatFromString state@(State {_string = s1 : ss, _float = fs}) = else state instructionFloatFromString state = state +-- |Adds the top two floats from the float stack. instructionFloatAdd :: State -> State instructionFloatAdd state@(State {_float = f1 : f2 : fs}) = state {_float = f2 + f1 : fs} instructionFloatAdd state = state +-- |Subtracts the first float from the second float on the float stack. instructionFloatSub :: State -> State instructionFloatSub state@(State {_float = f1 : f2 : fs}) = state {_float = f2 - f1 : fs} instructionFloatSub state = state +-- |Multiplies the top two floats on the float stack. instructionFloatMul :: State -> State instructionFloatMul state@(State {_float = f1 : f2 : fs}) = state {_float = f2 * f1 : fs} instructionFloatMul state = state +-- |Divides the first float from the second float on the float stack. instructionFloatDiv :: State -> State instructionFloatDiv state@(State {_float = f1 : f2 : fs}) = state {_float = if f1 /= 0 then f2 / f1 : fs else f1 : f2 : fs} instructionFloatDiv state = state +-- |Mods the first float from the second float on the float stack. instructionFloatMod :: State -> State instructionFloatMod state@(State {_float = f1 : f2 : fs}) = state {_float = if f1 /= 0 then f2 `mod'` f1 : fs else f1 : f2 : fs} instructionFloatMod state = state +-- |Takes the top two floats from the float stack and pushes the minimum of the two back on top. instructionFloatMin :: State -> State instructionFloatMin state@(State {_float = f1 : f2 : fs}) = state {_float = min f1 f2 : fs} instructionFloatMin state = state +-- |Takes the top two floats from the float stack and pushes the maximum of the two back on top. instructionFloatMax :: State -> State instructionFloatMax state@(State {_float = f1 : f2 : fs}) = state {_float = max f1 f2 : fs} instructionFloatMax state = state +-- |Adds one to the top float from the float stack. instructionFloatInc :: State -> State instructionFloatInc state@(State {_float = f1 : fs}) = state {_float = f1 + 1 : fs} instructionFloatInc state = state +-- |Subtracts one from the top float from the float stack. instructionFloatDec :: State -> State instructionFloatDec state@(State {_float = f1 : fs}) = state {_float = f1 - 1 : fs} instructionFloatDec state = state +-- |Takes the top two floats from the float stack and pushes the result of: the top float item < the second float item instructionFloatLT :: State -> State instructionFloatLT state@(State {_float = f1 : f2 : fs, _bool = bs}) = state {_float = fs, _bool = (f1 < f2) : bs} instructionFloatLT state = state +-- |Takes the top two floats from the float stack and pushes the result of: the top float item > the second float item instructionFloatGT :: State -> State instructionFloatGT state@(State {_float = f1 : f2 : fs, _bool = bs}) = state {_float = fs, _bool = (f1 > f2) : bs} instructionFloatGT state = state +-- |Takes the top two floats from the float stack and pushes the result of: the top float item <= the second float item instructionFloatLTE :: State -> State instructionFloatLTE state@(State {_float = f1 : f2 : fs, _bool = bs}) = state {_float = fs, _bool = (f1 <= f2) : bs} instructionFloatLTE state = state +-- |Takes the top two floats from the float stack and pushes the result of: the top float item >= the second float item instructionFloatGTE :: State -> State instructionFloatGTE state@(State {_float = f1 : f2 : fs, _bool = bs}) = state {_float = fs, _bool = (f1 >= f2) : bs} instructionFloatGTE state = state +-- |Pops the top float from the float stack. instructionFloatPop :: State -> State instructionFloatPop = instructionPop float +-- |Duplicates the top float on the float stack. instructionFloatDup :: State -> State instructionFloatDup = instructionDup float +-- |Duplicates the top float on the float stack N times based off the top of the int stack. instructionFloatDupN :: State -> State instructionFloatDupN = instructionDupN float +-- |Swaps the top two floats on the float stack. instructionFloatSwap :: State -> State instructionFloatSwap = instructionSwap float +-- |Rotates the top three floats on the float stack. instructionFloatRot :: State -> State instructionFloatRot = instructionRot float +-- |Sets the float stack to [] instructionFloatFlush :: State -> State instructionFloatFlush = instructionFlush float +-- |Checks if the top two floats are equal. Pushes the result to the float stack. instructionFloatEq :: State -> State instructionFloatEq = instructionEq float +-- |Pushes the depth of the stack to the int stack. instructionFloatStackDepth :: State -> State instructionFloatStackDepth = instructionStackDepth float +-- |Moves an item from deep within the float stack to the top of the float stack based on +-- the top int from the int stack. instructionFloatYankDup :: State -> State instructionFloatYankDup = instructionYankDup float +-- |Copies an item from deep within the char stack to the top of the char stack based on +-- the top int from the int stack. instructionFloatYank :: State -> State instructionFloatYank = instructionYank float +-- |Copies an item from the top of the float stack to deep within the float stack based on +-- the top int from the int stack. instructionFloatShoveDup :: State -> State instructionFloatShoveDup = instructionShoveDup float +-- |Moves an item from the top of the float stack to deep within the float stack based on +-- the top int from the int stack. instructionFloatShove :: State -> State instructionFloatShove = instructionShove float +-- |Pushes True to the bool stack if the float stack is empty. False if not. instructionFloatIsStackEmpty :: State -> State instructionFloatIsStackEmpty = instructionIsStackEmpty float +-- |Pushes the sin of the top float to the float stack. instructionFloatSin :: State -> State instructionFloatSin state@(State {_float = f1 : fs}) = state {_float = sin f1 : fs} instructionFloatSin state = state +-- |Pushes the cos of the top float to the float stack. instructionFloatCos :: State -> State instructionFloatCos state@(State {_float = f1 : fs}) = state {_float = cos f1 : fs} instructionFloatCos state = state +-- |Pushes the tan of the top float to the float stack. instructionFloatTan :: State -> State instructionFloatTan state@(State {_float = f1 : fs}) = state {_float = tan f1 : fs} instructionFloatTan state = state +-- |Duplicate the top N items from the float stack based on the top int from the int stack. instructionFloatDupItems :: State -> State instructionFloatDupItems = instructionDupItems float diff --git a/src/HushGP/Instructions/GenericInstructions.hs b/src/HushGP/Instructions/GenericInstructions.hs index 09026f4..93bdb9e 100644 --- a/src/HushGP/Instructions/GenericInstructions.hs +++ b/src/HushGP/Instructions/GenericInstructions.hs @@ -8,22 +8,29 @@ import Data.List.Split -- import Debug.Trace +-- |Utility Function: Deletes an item from a list at a specified index. deleteAt :: Int -> [a] -> [a] deleteAt idx xs = take idx xs <> drop 1 (drop idx xs) --- I could probably just combine these functions +-- |Utility Function: Combines two tuples containing lists with a value placed between them. combineTuple :: a -> ([a], [a]) -> [a] -combineTuple val tup = fst tup <> [val] <> snd tup +combineTuple val = combineTupleList [val] +-- |Utility Function: Combines two tuples containing lists with a list placed between them. combineTupleList :: [a] -> ([a], [a]) -> [a] combineTupleList val tup = fst tup <> val <> snd tup +-- |Utility Function: Inserts a value based on an int at a specified index. insertAt :: Int -> a -> [a] -> [a] insertAt idx val xs = combineTuple val (splitAt idx xs) +-- |Utility Function: Replaces a value based on an int at a specified index. replaceAt :: Int -> a -> [a] -> [a] replaceAt idx val xs = deleteAt (idx + 1) (insertAt idx val xs) +-- |Utility Function: Takes two ints as indicies. Sorts them low to high, sets the start to +-- 0 if the lowest start is less than 0 and the end to the length of the list - 1 if the end +-- if larger than the list. Grabs the sub list of adjusted indicies. subList :: Int -> Int -> [a] -> [a] subList idx0 idx1 xs = let @@ -33,7 +40,10 @@ subList idx0 idx1 xs = in take adjEnd (drop adjStart xs) --- Maybe could've used Data.List.isSubsequenceOf :shrug: +-- |Utility Function: Finds the index of the second list inside of the first index. +-- If the sublist passed is larger than the full list, returns -1 +-- If the lists are of equal length, and then contents are equal, returns 0. If not equal, returns -1 +-- Recursively shortens the full list until the sub list is found. findSubA :: forall a. Eq a => [a] -> [a] -> Int findSubA fullA subA | length fullA < length subA = -1 @@ -47,10 +57,10 @@ findSubA fullA subA | sA == take (length sA) fA = subIndex | otherwise = findSubA' (drop 1 fA) sA (subIndex + 1) --- The int is the amount of olds to replace with new --- Just chain findSubA calls lol --- Nothing means replace all --- May not be the most efficient method with the findSubA calls +-- |Utility Function: Replaces a number of instances of old with new in a list. +-- The Maybe Int is the amount of olds to replace with new. Nothing means replace all. +-- Just chain findSubA calls. +-- May not be the most efficient method with the findSubA calls. replace :: Eq a => [a] -> [a] -> [a] -> Maybe Int -> [a] replace fullA old new (Just amt) = if findSubA fullA old /= -1 && amt > 0 @@ -61,7 +71,8 @@ replace fullA old new Nothing = then replace (take (findSubA fullA old) fullA <> new <> drop (findSubA fullA old + length old) fullA) old new Nothing else fullA --- a rather inefficient search +-- |Utility Function: Counts the amount of occurrences of a sub list inside +-- of a larger list. amtOccurences :: forall a. Eq a => [a] -> [a] -> Int amtOccurences fullA subA = amtOccurences' fullA subA 0 where @@ -71,36 +82,47 @@ amtOccurences fullA subA = amtOccurences' fullA subA 0 then amtOccurences' (replace fA sA mempty (Just 1)) sA (count + 1) else count +-- |Utility Function: Takes the last N elements of a list. takeR :: Int -> [a] -> [a] takeR amt fullA = drop (length fullA - amt) fullA +-- |Utility Function: Drops the last N elements of a list. dropR :: Int -> [a] -> [a] dropR amt fullA = take (length fullA - amt) fullA +-- |Utility Function: A safe version of init. If the list is empty, returns the empty list. +-- If the list has items, takes the init of the list. safeInit :: [a] -> [a] safeInit [] = [] safeInit xs = init xs +-- |Utility Function: An indexing strategy used in parts of Hush. Takes the absolute value +-- of the passed number `mod` the length of the passed list. absNum :: Integral a => a -> [b] -> Int absNum rawNum lst = abs (fromIntegral rawNum) `mod` length lst +-- |Utility Function: Checks to see if a list is empty. +-- If the list is empty, returns False. +-- If the list is not empty, returns True. notEmptyStack :: Lens' State [a] -> State -> Bool notEmptyStack accessor state = not . null $ view accessor state +-- |Duplicates the top of a stack based on a lens. instructionDup :: Lens' State [a] -> State -> State instructionDup accessor state = case uncons (view accessor state) of Nothing -> state Just (x1,_) -> state & accessor .~ x1 : view accessor state +-- |Pops the top of the stack based on a lens. instructionPop :: Lens' State [a] -> State -> State instructionPop accessor state = state & accessor .~ drop 1 (view accessor state) +-- |Pushes True if the lens' stack is empty, False if not. instructionIsStackEmpty :: Lens' State [a] -> State -> State instructionIsStackEmpty accessor state@(State {_bool = bs}) = state{_bool = null (view accessor state) : bs} --- I might be able to move some of the int stack error checking --- to the integer call. For now this may be a tad inefficient. +-- |Duplicates the top of a stack based on a lens and the top of the int stack. instructionDupN :: forall a. Lens' State [a] -> State -> State instructionDupN accessor state = case uncons (view int state) of @@ -126,6 +148,7 @@ instructionDupItems accessor state@(State {_int = i1 : is}) = else state{_int = is} & accessor .~ (take i1 (view accessor state{_int = is}) <> view accessor state{_int = is}) instructionDupItems _ state = state +-- |Swaps the top two instructions based on a lens instructionSwap :: Lens' State [a] -> State -> State instructionSwap accessor state = state & accessor .~ swapper (view accessor state) @@ -134,9 +157,9 @@ instructionSwap accessor state = swapper (x1 : x2 : xs) = x2 : x1 : xs swapper xs = xs --- Rotates top 3 integers +-- |Rotates top 3 integers based on a lens. -- We could use template haskell to rotate any number of these as --- an instruction later. Template haskell seems very complicated tho. +-- an instruction later. instructionRot :: Lens' State [a] -> State -> State instructionRot accessor state = state & accessor .~ rotator (view accessor state) @@ -145,9 +168,12 @@ instructionRot accessor state = rotator (x1 : x2 : x3 : xs) = x3 : x1 : x2 : xs rotator xs = xs +-- |Deletes all instructions in a stack based on a lens. instructionFlush :: Lens' State [a] -> State -> State instructionFlush accessor state = state & accessor .~ [] +-- |Checks if the two top instructions are equal based on a lens. +-- Pushes the result to the bool stack. instructionEq :: forall a. Eq a => Lens' State [a] -> State -> State instructionEq accessor state = case uncons $ view accessor state of @@ -158,9 +184,12 @@ instructionEq accessor state = droppedState :: State droppedState = state & accessor .~ drop 2 (view accessor state) +-- |Calculates the stack depth based on a lens and pushes the result to the int stackk. instructionStackDepth :: Lens' State [a] -> State -> State instructionStackDepth accessor state@(State {_int = is}) = state{_int = length (view accessor state) : is} +-- |Copies an item from deep within a lens' stack to the top of the lens' stack based on +-- the top int from the int stack. instructionYankDup :: Lens' State [a] -> State -> State instructionYankDup accessor state@(State {_int = i1 : is}) = if notEmptyStack accessor state @@ -168,6 +197,8 @@ instructionYankDup accessor state@(State {_int = i1 : is}) = else state instructionYankDup _ state = state +-- |Moves an item from deep within a lens' stack to the top of the lens' stack based on +-- the top int from the int stack. instructionYank :: forall a. Lens' State [a] -> State -> State instructionYank accessor state@(State {_int = i1 : is}) = let @@ -181,6 +212,8 @@ instructionYank accessor state@(State {_int = i1 : is}) = if notEmptyStack accessor state{_int = is} then deletedState & accessor .~ item : view accessor deletedState else state instructionYank _ state = state +-- |Copies an item from the top of a lens' stack to deep within the lens' stack based on +-- the top int from the int stack. -- In pysh, instructionShoveDup and instructionShove behave differently when indexing in such a way that -- the duplicated index matters whether or not it's present in the stack at the moment of calculation. -- I'm not going to keep this behavior. Check out interpysh examples for how pysh handles it. @@ -191,10 +224,12 @@ instructionShoveDup accessor state@(State {_int = i1 : is}) = _ -> state instructionShoveDup _ state = state +-- |Moves an item from the top of a lens' stack to deep within the lens' stack based on +-- the top int from the int stack. instructionShove :: Lens' State [a] -> State -> State instructionShove accessor state = instructionShoveDup accessor state & accessor .~ drop 1 (view accessor (instructionShoveDup accessor state )) --- not char generic +-- |Concats two semigroupable items together based on a lens. Not char generic. instructionConcat :: Semigroup a => Lens' State [a] -> State -> State instructionConcat accessor state = case uncons (view accessor state) of @@ -204,20 +239,26 @@ instructionConcat accessor state = droppedState :: State droppedState = state & accessor .~ drop 2 (view accessor state) +-- |Based on two lenses, one of a primitive type and the next of a vector type, +-- takes the top item of the primitive stack and prepends it to the first vector in +-- the vector stack if there is one. instructionConj :: Lens' State [a] -> Lens' State [[a]] -> State -> State instructionConj primAccessor vectorAccessor state = case (uncons (view primAccessor state), uncons (view vectorAccessor state)) of (Just (p1,ps), Just (v1,vs)) -> state & primAccessor .~ ps & vectorAccessor .~ ((p1 : v1) : vs) _ -> state +-- |Based on two lenses, one of a primitive type and the next of a vector type, +-- takes the top item of the primitive stack and appends it to the first vector in +-- the vector stack if there is one. instructionConjEnd :: Lens' State [a] -> Lens' State [[a]] -> State -> State instructionConjEnd primAccessor vectorAccessor state = case (uncons (view primAccessor state), uncons (view vectorAccessor state)) of (Just (p1,ps), Just (v1,vs)) -> state & primAccessor .~ ps & vectorAccessor .~ ((v1 <> [p1]) : vs) _ -> state --- v for vector, vs for vectorstack (also applicable to strings) --- Could abstract this unconsing even further in all functions below +-- |Takes the first N items from the first vector on the top of a vector stack and +-- pushes the result to said vector stack. instructionTakeN :: Lens' State [[a]] -> State -> State instructionTakeN accessor state@(State {_int = i1 : is}) = case uncons (view accessor state) of @@ -360,20 +401,21 @@ instructionVectorIterate primAccessor vectorAccessor vectorType typeIterateFunct _ -> state instructionVectorIterate _ _ _ _ _ state = state +-- |Moves a type from a stack and places it onto the code stack. instructionCodeFrom :: Lens' State [a] -> (a -> Gene) -> State -> State instructionCodeFrom accessor geneType state@(State {_code = cs}) = case uncons (view accessor state) of Just (x, xs) -> state{_code = geneType x : cs} & accessor .~ xs _ -> state --- |A function that sorts the first vector for a vectorType +-- |Sorts the first vector for a vectorType instructionVectorSort :: Ord a => Lens' State [[a]] -> State -> State instructionVectorSort accessor state = case uncons (view accessor state) of Just (x, xs) -> state & accessor .~ (sort x : xs) _ -> state --- |A function that sorts the first vector in reverse order for a vectorType +-- |Sorts the first vector in reverse order for a vectorType instructionVectorSortReverse :: Ord a => Lens' State [[a]] -> State -> State instructionVectorSortReverse accessor state = case uncons (view accessor state) of From c4417cf22fc291566118d1287788aeee1ff4ff0a Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sun, 9 Feb 2025 14:00:34 -0600 Subject: [PATCH 130/171] GP todo --- TODO.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/TODO.md b/TODO.md index fb2c8e9..298eddc 100644 --- a/TODO.md +++ b/TODO.md @@ -19,3 +19,5 @@ ## PushGP TODO - [ ] Implement a Plushy genome translator - [ ] Need to make this reproducable too (Check pysh json files) +- [ ] Add Memory +- [ ] Add history stack(s), like a call stack From 6a78fd0ba6ef8008758044293eb8789f44e1e6b1 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sun, 9 Feb 2025 14:00:50 -0600 Subject: [PATCH 131/171] Generic documentation done --- .../Instructions/GenericInstructions.hs | 60 ++++++++++++++++--- 1 file changed, 53 insertions(+), 7 deletions(-) diff --git a/src/HushGP/Instructions/GenericInstructions.hs b/src/HushGP/Instructions/GenericInstructions.hs index 93bdb9e..4c86711 100644 --- a/src/HushGP/Instructions/GenericInstructions.hs +++ b/src/HushGP/Instructions/GenericInstructions.hs @@ -266,6 +266,8 @@ instructionTakeN accessor state@(State {_int = i1 : is}) = _ -> state instructionTakeN _ state = state +-- |Takes the sublist of the top vector based on a passed lens. Check out the +-- subList documentation for information on how this works. instructionSubVector :: Lens' State [[a]] -> State -> State instructionSubVector accessor state@(State {_int = i1 : i2 : is}) = case uncons (view accessor state) of @@ -273,6 +275,8 @@ instructionSubVector accessor state@(State {_int = i1 : i2 : is}) = _ -> state instructionSubVector _ state = state +-- |Based on two lenses, one of a primitive type and the next of a vector type, +-- Takes the first item from the top vector and places it onto the passed primitive stack. instructionVectorFirst :: Lens' State [a] -> Lens' State [[a]] -> State -> State instructionVectorFirst primAccessor vectorAccessor state = case uncons (view vectorAccessor state) of @@ -282,6 +286,8 @@ instructionVectorFirst primAccessor vectorAccessor state = _ -> state _ -> state +-- |Based on two lenses, one of a primitive type and the next of a vector type, +-- Takes the last item from the top vector and places it onto the passed primitive stack. instructionVectorLast :: Lens' State [a] -> Lens' State [[a]] -> State -> State instructionVectorLast primAccessor vectorAccessor state = case uncons (view vectorAccessor state) of @@ -291,6 +297,9 @@ instructionVectorLast primAccessor vectorAccessor state = _ -> state _ -> state +-- |Based on two lenses, one of a primitive type and the next of a vector type, +-- Takes the Nth item from the top vector and places it onto the passed primitive stack +-- based on an int from the int stack. instructionVectorNth :: Lens' State [a] -> Lens' State [[a]] -> State -> State instructionVectorNth primAccessor vectorAccessor state@(State {_int = i1 : is}) = case uncons (view vectorAccessor state) of @@ -298,97 +307,133 @@ instructionVectorNth primAccessor vectorAccessor state@(State {_int = i1 : is}) _ -> state instructionVectorNth _ _ state= state +-- |Takes the top vector, removes the first item of said vector, and pushes the result back to top +-- of the stack, based on a lens. instructionRest :: Lens' State [[a]] -> State -> State instructionRest accessor state = case uncons (view accessor state) of Just (v1, vs) -> state & accessor .~ (drop 1 v1 : vs) _ -> state +-- |Takes the top vector, removes the last item of said vector, and pushes the result back to top +-- of the stack, based on a lens. instructionButLast :: Lens' State [[a]] -> State -> State instructionButLast accessor state = case uncons (view accessor state) of Just (v1, vs) -> state & accessor .~ (safeInit v1 : vs) _ -> state +-- |Takes the top vector, pushes the length of that vector to the int stack, based on a lens. instructionLength :: Lens' State [[a]] -> State -> State instructionLength accessor state@(State {_int = is}) = case uncons (view accessor state) of Just (v1, vs) -> state{_int = length v1 : is} & accessor .~ vs _ -> state +-- |Takes the top vector, reverses it, based on a lens. instructionReverse :: Lens' State [[a]] -> State -> State instructionReverse accessor state = case uncons (view accessor state) of Just (v1, vs) -> state & accessor .~ (reverse v1 : vs) _ -> state +-- |Based on two lenses, one of a primitive type and the next of a vector type, +-- takes the vector and individually pushes its indicies to the passed primitive stack. instructionPushAll :: Lens' State [a] -> Lens' State [[a]] -> State -> State instructionPushAll primAccessor vectorAccessor state = case uncons (view vectorAccessor state) of Just (v1, vs) -> state & vectorAccessor .~ vs & primAccessor .~ (v1 <> view primAccessor state) _ -> state +-- |Based on a vector lens, makes an empty vector and pushes it to the passed stack. instructionVectorMakeEmpty :: Lens' State [[a]] -> State -> State instructionVectorMakeEmpty accessor state = state & accessor .~ ([] : view accessor state) +-- |Based on a vector lens, checks if the top vector is empty. If so, pushes True to the +-- bool stack. If not, pushes False. instructionVectorIsEmpty :: Lens' State [[a]] -> State -> State instructionVectorIsEmpty accessor state@(State {_bool = bs}) = case uncons (view accessor state) of Just (v1, vs) -> state{_bool = null v1 : bs} & accessor .~ vs _ -> state +-- |Based on two lenses, one of a primitive type and the next of a vector type, +-- If the vector on the top of the vector stack contains the top item on the primitive stack, +-- pushes True to the bool stack. Pushes False otherwise. instructionVectorContains :: Eq a => Lens' State [a] -> Lens' State [[a]] -> State -> State instructionVectorContains primAccessor vectorAccessor state@(State {_bool = bs}) = case (uncons (view vectorAccessor state), uncons (view primAccessor state)) of (Just (v1, vs), Just (p1, ps)) -> state{_bool = (findSubA v1 [p1] /= -1) : bs} & vectorAccessor .~ vs & primAccessor .~ ps _ -> state --- I couldn't think of a better way of doing this +-- |Based on two lenses, one of a primitive type and the next of a vector type, +-- finds the first index of the top item in the primitive stack inside of the +-- top vector from the vector stack and pushes the result to the int stack. instructionVectorIndexOf :: Eq a => Lens' State [a] -> Lens' State [[a]] -> State -> State instructionVectorIndexOf primAccessor vectorAccessor state = case (uncons (view vectorAccessor state), uncons (view primAccessor state)) of (Just (v1, vs), Just (p1, ps)) -> (state & vectorAccessor .~ vs & primAccessor .~ ps) & int .~ (findSubA v1 [p1] : view int (state & vectorAccessor .~ vs & primAccessor .~ ps)) _ -> state +-- |Based on two lenses, one of a primitive type and the next of a vector type, +-- finds the amount of times the top item in the primitive stack occurs inside of the +-- top vector from the vector stack and pushes the result to the int stack. instructionVectorOccurrencesOf :: Eq a => Lens' State [a] -> Lens' State [[a]] -> State -> State instructionVectorOccurrencesOf primAccessor vectorAccessor state = case (uncons (view vectorAccessor state), uncons (view primAccessor state)) of (Just (v1, vs), Just (p1, ps)) -> (state & vectorAccessor .~ vs & primAccessor .~ ps) & int .~ (amtOccurences v1 [p1] : view int (state & vectorAccessor .~ vs & primAccessor .~ ps)) _ -> state --- | This function parses the primitives of a vector type and pushes that vector split into --- lists of size one onto the respective vector stack. +-- |This function parses the primitives inside a vector type and pushes that vector split into +-- lists of size one onto the respective vector stack. Based on a vector lens. instructionVectorParseToPrim :: Lens' State [[a]] -> State -> State instructionVectorParseToPrim accessor state = case uncons (view accessor state) of Just (x1, xs) -> state & accessor .~ (chunksOf 1 x1 <> xs) _ -> state +-- |Based on two lenses, one of a primitive type and the next of a vector type, +-- Sets the Nth index inside of the top vector from the vector stack to the top value +-- from the primitive stack. N is based on an int from the top of the int stack. instructionVectorSetNth :: Lens' State [a] -> Lens' State [[a]] -> State -> State instructionVectorSetNth primAccessor vectorAccessor state@(State {_int = i1 : is}) = case (uncons (view vectorAccessor state{_int = is}), uncons (view primAccessor state{_int = is})) of (Just (v1, vs), Just (p1, ps)) -> state{_int = is} & vectorAccessor .~ (replaceAt (absNum i1 v1) p1 v1 : vs) & primAccessor .~ ps _ -> state instructionVectorSetNth _ _ state = state - + +-- |Based on two lenses, one of a primitive type and the next of a vector type, +-- replaces all occurrences inside of the top vector from the vector stack with two values from +-- the primitive stack. The top of the primitive stack is the old value to be replaced. The second item +-- in the primitive stack is the new value to replace the old one. instructionVectorReplace :: Eq a => Lens' State [a] -> Lens' State [[a]] -> State -> State instructionVectorReplace primAccessor vectorAccessor state = case (uncons (view vectorAccessor state), uncons (view primAccessor state)) of (Just (v1, vs), Just (p1, p2 : ps)) -> state & vectorAccessor .~ (replace v1 [p1] [p2] Nothing : vs) & primAccessor .~ ps _ -> state +-- |Based on two lenses, one of a primitive type and the next of a vector type, +-- replaces the first occurrence inside of the top vector from the vector stack with two values from +-- the primitive stack. The top of the primitive stack is the old value to be replaced. The second item +-- in the primitive stack is the new value to replace the old one. instructionVectorReplaceFirst :: Eq a => Lens' State [a] -> Lens' State [[a]] -> State -> State instructionVectorReplaceFirst primAccessor vectorAccessor state = case (uncons (view vectorAccessor state), uncons (view primAccessor state)) of (Just (v1, vs), Just (p1, p2 : ps)) -> state & vectorAccessor .~ (replace v1 [p1] [p2] (Just 1) : vs) & primAccessor .~ ps _ -> state +-- |Based on two lenses, one of a primitive type and the next of a vector type, +-- removes all occurrences inside of the top vector from the vector stack where the top +-- item from the primitive stack equals a primitive inside of the vector stack. instructionVectorRemove :: Eq a => Lens' State [a] -> Lens' State [[a]] -> State -> State instructionVectorRemove primAccessor vectorAccessor state = case (uncons (view vectorAccessor state), uncons (view primAccessor state)) of (Just (v1, vs), Just (p1, ps)) -> state & vectorAccessor .~ (replace v1 [p1] [] Nothing : vs) & primAccessor .~ ps _ -> state +-- |Based on two lenses, one of a primitive type and the next of a vector type, +-- removes the first occurrence inside of the top vector from the vector stack where the top +-- item from the primitive stack equals a primitive inside of the vector stack. instructionVectorIterate :: Lens' State [a] -> Lens' State [[a]] -> ([a] -> Gene) -> (State -> State) -> String -> State -> State instructionVectorIterate primAccessor vectorAccessor vectorType typeIterateFunction typeIterateFunctionName state@(State {_exec = e1 : es}) = case uncons (view vectorAccessor state) of @@ -401,21 +446,22 @@ instructionVectorIterate primAccessor vectorAccessor vectorType typeIterateFunct _ -> state instructionVectorIterate _ _ _ _ _ state = state --- |Moves a type from a stack and places it onto the code stack. +-- |Moves a type from a stack and places it onto the code stack. Based on a primitive stack. +-- The (a -> Gene) is something like GeneBool or GeneInt for example. instructionCodeFrom :: Lens' State [a] -> (a -> Gene) -> State -> State instructionCodeFrom accessor geneType state@(State {_code = cs}) = case uncons (view accessor state) of Just (x, xs) -> state{_code = geneType x : cs} & accessor .~ xs _ -> state --- |Sorts the first vector for a vectorType +-- |Sorts the top vector in a vector stack, based on a vector lens. instructionVectorSort :: Ord a => Lens' State [[a]] -> State -> State instructionVectorSort accessor state = case uncons (view accessor state) of Just (x, xs) -> state & accessor .~ (sort x : xs) _ -> state --- |Sorts the first vector in reverse order for a vectorType +-- |Sorts the top vector in a vector stack in reverse order for a vectorType, based on a vector lens. instructionVectorSortReverse :: Ord a => Lens' State [[a]] -> State -> State instructionVectorSortReverse accessor state = case uncons (view accessor state) of From efb4d809623837a34dbc0ab5a57b27caea0f7c15 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sun, 9 Feb 2025 14:27:45 -0600 Subject: [PATCH 132/171] float docs, typo fixes, string -> float fix --- src/HushGP/Instructions/CodeInstructions.hs | 2 +- src/HushGP/Instructions/FloatInstructions.hs | 9 ++--- src/HushGP/Instructions/IntInstructions.hs | 38 ++++++++++++++++++++ 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/HushGP/Instructions/CodeInstructions.hs b/src/HushGP/Instructions/CodeInstructions.hs index 2248de5..599619a 100644 --- a/src/HushGP/Instructions/CodeInstructions.hs +++ b/src/HushGP/Instructions/CodeInstructions.hs @@ -346,7 +346,7 @@ instructionCodeRot = instructionRot code instructionCodeFlush :: State -> State instructionCodeFlush = instructionFlush code --- |Checks if the top code items are equal. Pushes true to the bool stack if so, False if not. +-- |Checks if the top code items are equal. Pushes True to the bool stack if so, False if not. instructionCodeEq :: State -> State instructionCodeEq = instructionEq code diff --git a/src/HushGP/Instructions/FloatInstructions.hs b/src/HushGP/Instructions/FloatInstructions.hs index 2b7e6b5..a0efe95 100644 --- a/src/HushGP/Instructions/FloatInstructions.hs +++ b/src/HushGP/Instructions/FloatInstructions.hs @@ -15,7 +15,7 @@ instructionFloatFromBool :: State -> State instructionFloatFromBool state@(State {_bool = b1 : bs, _float = fs}) = state {_bool = bs, _float = (if b1 then 1.0 else 0.0) : fs} instructionFloatFromBool state = state --- |Takes the top char and converts it to int representation. That int then gets casted to a float. +-- |Takes the top char and converts it to int representation. That int then gets casted to a float and pushed to the float stack. instructionFloatFromChar :: State -> State instructionFloatFromChar state@(State {_char = c1 : cs, _float = fs}) = state {_char = cs, _float = (fromIntegral (ord c1) :: Float) : fs} instructionFloatFromChar state = state @@ -23,7 +23,7 @@ instructionFloatFromChar state = state -- |Reads the top string and converts it to a float if possible. If not, acts as a NoOp. instructionFloatFromString :: State -> State instructionFloatFromString state@(State {_string = s1 : ss, _float = fs}) = - if all isDigit s1 + if all (\x -> isDigit x || x == '.') s1 && amtOccurences "." s1 <= 1 then state{_string = ss, _float = read @Float s1 : fs} else state instructionFloatFromString state = state @@ -117,7 +117,8 @@ instructionFloatRot = instructionRot float instructionFloatFlush :: State -> State instructionFloatFlush = instructionFlush float --- |Checks if the top two floats are equal. Pushes the result to the float stack. +-- |Checks if the top two floats are equal. Pushes the result to the bool stack. +-- Might override this later to check for equality in a range rather than exact equality. instructionFloatEq :: State -> State instructionFloatEq = instructionEq float @@ -130,7 +131,7 @@ instructionFloatStackDepth = instructionStackDepth float instructionFloatYankDup :: State -> State instructionFloatYankDup = instructionYankDup float --- |Copies an item from deep within the char stack to the top of the char stack based on +-- |Copies an item from deep within the float stack to the top of the float stack based on -- the top int from the int stack. instructionFloatYank :: State -> State instructionFloatYank = instructionYank float diff --git a/src/HushGP/Instructions/IntInstructions.hs b/src/HushGP/Instructions/IntInstructions.hs index 4d0401b..d1a2682 100644 --- a/src/HushGP/Instructions/IntInstructions.hs +++ b/src/HushGP/Instructions/IntInstructions.hs @@ -5,18 +5,22 @@ import HushGP.Instructions.GenericInstructions import Data.Char -- import Debug.Trace +-- |Converts the top float to an int and pushes the result to the int stack. instructionIntFromFloat :: State -> State instructionIntFromFloat state@(State {_float = f1 : fs, _int = is}) = state {_float = fs, _int = floor f1 : is} instructionIntFromFloat state = state +-- |If the top bool True, pushes 1 to the int stack. Pushes 0 if False. instructionIntFromBool :: State -> State instructionIntFromBool state@(State {_bool = b1 : bs, _int = is}) = state {_bool = bs, _int = (if b1 then 1 else 0) : is} instructionIntFromBool state = state +-- |Takes the top char and converts it to int representation. The result is pushed to the int stack. instructionIntFromChar :: State -> State instructionIntFromChar state@(State {_char = c1 : cs, _int = is}) = state {_char = cs, _int = ord c1 : is} instructionIntFromChar state = state +-- |Reads the top string and converts it to a int if possible. If not, acts as a NoOp. instructionIntFromString :: State -> State instructionIntFromString state@(State {_string = s1 : ss, _int = is}) = if all isDigit s1 @@ -24,96 +28,130 @@ instructionIntFromString state@(State {_string = s1 : ss, _int = is}) = else state instructionIntFromString state = state +-- |Adds the top two ints from the int stack and pushes the result to the int stack. instructionIntAdd :: State -> State instructionIntAdd state@(State {_int = i1 : i2 : is}) = state {_int = i2 + i1 : is} instructionIntAdd state = state +-- |Subtracts the first int from the second int and pushes the result to the int stack. instructionIntSub :: State -> State instructionIntSub state@(State {_int = i1 : i2 : is}) = state {_int = i2 - i1 : is} instructionIntSub state = state +-- |Multiplies the top two ints from the int stack and pushes the result to the int stack. instructionIntMul :: State -> State instructionIntMul state@(State {_int = i1 : i2 : is}) = state {_int = i2 * i1 : is} instructionIntMul state = state +-- |Divides the first float from the second float and pushes the result to the int stack. +-- This does truncate. instructionIntDiv :: State -> State instructionIntDiv state@(State {_int = i1 : i2 : is}) = state {_int = if i1 /= 0 then (i2 `div` i1) : is else i1 : i2 : is} instructionIntDiv state = state +-- |Mods the first float from the second float and pushes the result to the int stack. +-- This does truncate. instructionIntMod :: State -> State instructionIntMod state@(State {_int = i1 : i2 : is}) = state {_int = if i1 /= 0 then (i2 `mod` i1) : is else i1 : i2 : is} instructionIntMod state = state +-- |Takes the top two ints from the int stack and pushes the minimum of the two back on top. instructionIntMin :: State -> State instructionIntMin state@(State {_int = i1 : i2 : is}) = state {_int = min i1 i2 : is} instructionIntMin state = state +-- |Takes the top two ints from the int stack and pushes the maximum of the two back on top. instructionIntMax :: State -> State instructionIntMax state@(State {_int = i1 : i2 : is}) = state {_int = max i1 i2 : is} instructionIntMax state = state +-- |Adds one to the top of the int stack and pushes the result back to the int stack. instructionIntInc :: State -> State instructionIntInc state@(State {_int = i1 : is}) = state {_int = i1 + 1 : is} instructionIntInc state = state +-- |Subtracts one from the top of the int stack and pushes the result back to the int stack. instructionIntDec :: State -> State instructionIntDec state@(State {_int = i1 : is}) = state {_int = i1 - 1 : is} instructionIntDec state = state +-- |Takes the top two ints from the int stack and pushes the result of: the top int item < the second int item instructionIntLT :: State -> State instructionIntLT state@(State {_int = i1 : i2 : is, _bool = bs}) = state {_int = is, _bool = (i1 < i2) : bs} instructionIntLT state = state +-- |Takes the top two ints from the int stack and pushes the result of: the top int item > the second int item instructionIntGT :: State -> State instructionIntGT state@(State {_int = i1 : i2 : is, _bool = bs}) = state {_int = is, _bool = (i1 > i2) : bs} instructionIntGT state = state +-- |Takes the top two ints from the int stack and pushes the result of: the top int item <= the second int item instructionIntLTE :: State -> State instructionIntLTE state@(State {_int = i1 : i2 : is, _bool = bs}) = state {_int = is, _bool = (i1 <= i2) : bs} instructionIntLTE state = state +-- |Takes the top two ints from the int stack and pushes the result of: the top int item >= the second int item instructionIntGTE :: State -> State instructionIntGTE state@(State {_int = i1 : i2 : is, _bool = bs}) = state {_int = is, _bool = (i1 >= i2) : bs} instructionIntGTE state = state +-- |Pops the top int from the int stack. instructionIntDup :: State -> State instructionIntDup = instructionDup int +-- |Duplicates the top int on the int stack. instructionIntPop :: State -> State instructionIntPop = instructionPop int +-- |Duplicates the second to top int on the int stack based on the top int +-- and pushes the result to the int stack. instructionIntDupN :: State -> State instructionIntDupN = instructionDupN int +-- |Swaps the top two ints on the int stack. instructionIntSwap :: State -> State instructionIntSwap = instructionSwap int +-- |Rotates the top three ints and pushes the result to the int stack. instructionIntRot :: State -> State instructionIntRot = instructionRot int +-- |Sets the int stack to []. instructionIntFlush :: State -> State instructionIntFlush = instructionFlush int +-- |Checks if the top two floats are equal instructionIntEq :: State -> State instructionIntEq = instructionEq int +-- |Pushes the depth of the int stack to top of the int stack after the caluculation. instructionIntStackDepth :: State -> State instructionIntStackDepth = instructionStackDepth int +-- |Moves an item from deep within the int stack to the top of the int stack based on +-- the top int from the int stack. instructionIntYank :: State -> State instructionIntYank = instructionYank int +-- |Copies an item from deep within the float stack to the top of the float stack based on +-- the top int from the int stack. instructionIntYankDup :: State -> State instructionIntYankDup = instructionYankDup int +-- |Moves an item from the top of the int stack to deep within the int stack based on +-- the top int from the int stack. instructionIntShove :: State -> State instructionIntShove = instructionShove int +-- |Copies an item from the top of the int stack to deep within the int stack based on +-- the top int from the int stack. instructionIntShoveDup :: State -> State instructionIntShoveDup = instructionShoveDup int +-- |Pushes True to the bool stack if the int stack is empty. False if not. instructionIntIsStackEmpty :: State -> State instructionIntIsStackEmpty = instructionIsStackEmpty int +-- |Duplicate the top N items from the int stack based on the top int from the int stack. instructionIntDupItems :: State -> State instructionIntDupItems = instructionDupItems int From 91bd09c00f40b6a06e62f7a9522304983a1428a0 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sun, 9 Feb 2025 19:33:58 -0600 Subject: [PATCH 133/171] more TODO --- TODO.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/TODO.md b/TODO.md index 298eddc..d0e1d2e 100644 --- a/TODO.md +++ b/TODO.md @@ -10,7 +10,7 @@ - [X] Disambiguate isEmpty and stackIsEmpty - [X] Rename Logical to Bool - [X] Make int yank, shove, yankdup, and shovedup generic -- [ ] Write hackage documentation for each function +- [ ] Write haddock documentation for each function - [X] Refactor all functions to take state as the final parameter - [X] Standardize the pattern matching parameter names, such as c1 : cs - [ ] Write unit/quickcheck tests for all of the instructions @@ -21,3 +21,4 @@ - [ ] Need to make this reproducable too (Check pysh json files) - [ ] Add Memory - [ ] Add history stack(s), like a call stack +- [ ] Implement interpreter options (could probably just place this all into a map or something) From 2d6b888e2e54dc2b068b7207b336e94c1859e8a4 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sun, 9 Feb 2025 19:34:23 -0600 Subject: [PATCH 134/171] generic string instructions and more documentation --- .../Instructions/GenericInstructions.hs | 146 ++++++++++++++++-- src/HushGP/Instructions/StringInstructions.hs | 116 +++++++++----- .../Instructions/VectorBoolInstructions.hs | 4 +- .../Instructions/VectorCharInstructions.hs | 4 +- .../Instructions/VectorFloatInstructions.hs | 4 +- .../Instructions/VectorIntInstructions.hs | 4 +- .../Instructions/VectorStringInstructions.hs | 4 +- 7 files changed, 221 insertions(+), 61 deletions(-) diff --git a/src/HushGP/Instructions/GenericInstructions.hs b/src/HushGP/Instructions/GenericInstructions.hs index 4c86711..fdca24a 100644 --- a/src/HushGP/Instructions/GenericInstructions.hs +++ b/src/HushGP/Instructions/GenericInstructions.hs @@ -286,6 +286,17 @@ instructionVectorFirst primAccessor vectorAccessor state = _ -> state _ -> state +-- |Based on a vector lens, takes the first item from the top vector on the vector stack +-- and creates a vector wrapping that first item, pushing it back onto the stack. +instructionVectorFromFirstPrim :: Lens' State [[a]] -> State -> State +instructionVectorFromFirstPrim accessor state = + case uncons (view accessor state) of + Just (v1, vs) -> + case uncons v1 of + Just (vp1, _) -> state & accessor .~ ([vp1] : vs) + _ -> state + _ -> state + -- |Based on two lenses, one of a primitive type and the next of a vector type, -- Takes the last item from the top vector and places it onto the passed primitive stack. instructionVectorLast :: Lens' State [a] -> Lens' State [[a]] -> State -> State @@ -297,6 +308,17 @@ instructionVectorLast primAccessor vectorAccessor state = _ -> state _ -> state +-- |Based on a vector lens, takes the last item from the top vector on the vector stack +-- and creates a vector wrapping that last item, pushing it back onto the stack. +instructionVectorFromLastPrim :: Lens' State [[a]] -> State -> State +instructionVectorFromLastPrim accessor state = + case uncons (view accessor state) of + Just (v1, vs) -> + case uncons (drop (length v1 - 1) v1) of + Just (vp1, _) -> state & accessor .~ ([vp1] : vs) + _ -> state + _ -> state + -- |Based on two lenses, one of a primitive type and the next of a vector type, -- Takes the Nth item from the top vector and places it onto the passed primitive stack -- based on an int from the int stack. @@ -307,6 +329,16 @@ instructionVectorNth primAccessor vectorAccessor state@(State {_int = i1 : is}) _ -> state instructionVectorNth _ _ state= state +-- |Based on a vector lens, takes the Nth item from the top vector on the vector stack +-- and creates a vector wrapping that Nth item, pushing it back onto the stack. N is +-- the top item on the int stack. +instructionVectorFromNthPrim :: Lens' State [[a]] -> State -> State +instructionVectorFromNthPrim accessor state@(State {_int = i1 : is}) = + case uncons (view accessor state) of + Just (v1, vs) -> state{_int = is} & accessor .~ ([v1 !! absNum i1 v1] : vs) + _ -> state +instructionVectorFromNthPrim _ state = state + -- |Takes the top vector, removes the first item of said vector, and pushes the result back to top -- of the stack, based on a lens. instructionRest :: Lens' State [[a]] -> State -> State @@ -366,6 +398,15 @@ instructionVectorContains primAccessor vectorAccessor state@(State {_bool = bs}) (Just (v1, vs), Just (p1, ps)) -> state{_bool = (findSubA v1 [p1] /= -1) : bs} & vectorAccessor .~ vs & primAccessor .~ ps _ -> state +-- |Based on a vector lens and the two vectors on the top of said stack. +-- If the second vector can be found within the first vector, True is pushed to the +-- bool stack. If not, False is pushed to the bool stack. +instructionVectorContainsVector :: Eq a => Lens' State [[a]] -> State -> State +instructionVectorContainsVector accessor state@(State {_bool = bs}) = + case uncons (view accessor state) of + Just (v1, v2 : vs) -> state & accessor .~ vs & bool .~ ((findSubA v1 v2 /= (-1)) : bs) + _ -> state + -- |Based on two lenses, one of a primitive type and the next of a vector type, -- finds the first index of the top item in the primitive stack inside of the -- top vector from the vector stack and pushes the result to the int stack. @@ -375,6 +416,14 @@ instructionVectorIndexOf primAccessor vectorAccessor state = (Just (v1, vs), Just (p1, ps)) -> (state & vectorAccessor .~ vs & primAccessor .~ ps) & int .~ (findSubA v1 [p1] : view int (state & vectorAccessor .~ vs & primAccessor .~ ps)) _ -> state +-- |Based on a vector lens and the two vectors on top of said stack. Searches and pushes the +-- index of the second vector inside of the first vector to the int stack. Pushes -1 if not found. +instructionVectorIndexOfVector :: Eq a => Lens' State [[a]] -> State -> State +instructionVectorIndexOfVector accessor state@(State {_int = is}) = + case uncons (view accessor state) of + Just (v1, v2 : vs) -> state & accessor .~ vs & int .~ (findSubA v1 v2 : is) + _ -> state + -- |Based on two lenses, one of a primitive type and the next of a vector type, -- finds the amount of times the top item in the primitive stack occurs inside of the -- top vector from the vector stack and pushes the result to the int stack. @@ -384,6 +433,15 @@ instructionVectorOccurrencesOf primAccessor vectorAccessor state = (Just (v1, vs), Just (p1, ps)) -> (state & vectorAccessor .~ vs & primAccessor .~ ps) & int .~ (amtOccurences v1 [p1] : view int (state & vectorAccessor .~ vs & primAccessor .~ ps)) _ -> state +-- |Based on a vector lens and the top two vectors in said stack, +-- Counts the amount of occurrences of the second vector in the first +-- vector. Pushes the result to the string stack. +instructionVectorOccurrencesOfVector :: Eq a => Lens' State [[a]] -> State -> State +instructionVectorOccurrencesOfVector accessor state@(State {_int = is}) = + case uncons (view accessor state) of + Just (v1, v2 : vs) -> state & accessor .~ vs & int .~ (amtOccurences v1 v2 : is) + _ -> state + -- |This function parses the primitives inside a vector type and pushes that vector split into -- lists of size one onto the respective vector stack. Based on a vector lens. instructionVectorParseToPrim :: Lens' State [[a]] -> State -> State @@ -392,7 +450,7 @@ instructionVectorParseToPrim accessor state = Just (x1, xs) -> state & accessor .~ (chunksOf 1 x1 <> xs) _ -> state --- |Based on two lenses, one of a primitive type and the next of a vector type, +-- |Based on two lenses, one of a primitive type and the next of a vector type. -- Sets the Nth index inside of the top vector from the vector stack to the top value -- from the primitive stack. N is based on an int from the top of the int stack. instructionVectorSetNth :: Lens' State [a] -> Lens' State [[a]] -> State -> State @@ -402,34 +460,75 @@ instructionVectorSetNth primAccessor vectorAccessor state@(State {_int = i1 : is _ -> state instructionVectorSetNth _ _ state = state +-- |Based on two lenses, one of a primitive type and the next of a vector type. +-- Splits the vector on top of the vector stack with the top primitive and pushes the +-- result to the original vector stack. +instructionVectorSplitOn :: Eq a => Lens' State [a] -> Lens' State [[a]] -> State -> State +instructionVectorSplitOn primAccessor vectorAccessor state = + case (uncons (view vectorAccessor state), uncons (view primAccessor state)) of + (Just (v1, vs), Just (p1, ps)) -> state & primAccessor .~ ps & vectorAccessor .~ (reverse (splitOn [p1] v1) <> vs) + _ -> state + +-- |Based on a vector lens and top two items of said stack, splits the +-- first vector based on the second vector and pushes the result to the +-- original vector stack. +instructionVectorSplitOnVector :: Eq a => Lens' State [[a]] -> State -> State +instructionVectorSplitOnVector accessor state = + case uncons (view accessor state) of + Just (v1, v2 : vs) -> state & accessor .~ (reverse (splitOn v2 v1) <> vs) + _ -> state + -- |Based on two lenses, one of a primitive type and the next of a vector type, -- replaces all occurrences inside of the top vector from the vector stack with two values from -- the primitive stack. The top of the primitive stack is the old value to be replaced. The second item -- in the primitive stack is the new value to replace the old one. -instructionVectorReplace :: Eq a => Lens' State [a] -> Lens' State [[a]] -> State -> State -instructionVectorReplace primAccessor vectorAccessor state = +instructionVectorReplace :: Eq a => Lens' State [a] -> Lens' State [[a]] -> Maybe Int -> State -> State +instructionVectorReplace primAccessor vectorAccessor amt state = case (uncons (view vectorAccessor state), uncons (view primAccessor state)) of - (Just (v1, vs), Just (p1, p2 : ps)) -> state & vectorAccessor .~ (replace v1 [p1] [p2] Nothing : vs) & primAccessor .~ ps + (Just (v1, vs), Just (p1, p2 : ps)) -> state & vectorAccessor .~ (replace v1 [p1] [p2] amt: vs) & primAccessor .~ ps _ -> state --- |Based on two lenses, one of a primitive type and the next of a vector type, --- replaces the first occurrence inside of the top vector from the vector stack with two values from --- the primitive stack. The top of the primitive stack is the old value to be replaced. The second item --- in the primitive stack is the new value to replace the old one. -instructionVectorReplaceFirst :: Eq a => Lens' State [a] -> Lens' State [[a]] -> State -> State -instructionVectorReplaceFirst primAccessor vectorAccessor state = - case (uncons (view vectorAccessor state), uncons (view primAccessor state)) of - (Just (v1, vs), Just (p1, p2 : ps)) -> state & vectorAccessor .~ (replace v1 [p1] [p2] (Just 1) : vs) & primAccessor .~ ps +-- |Based on a vector lens and the top three vectors on said stack. +-- Inside of the first vector, replaces the number of instances specified +-- by the Maybe Int parameter of the second vector with the third vector. +-- If amt is Nothing, replaces all instances. +instructionVectorReplaceVector :: Eq a => Lens' State [[a]] -> Maybe Int -> State -> State +instructionVectorReplaceVector accessor amt state = + case uncons (view accessor state) of + Just (v1, v2 : v3 : vs) -> state & accessor .~ (replace v1 v2 v3 amt : vs) _ -> state +-- |Based on a vector lens, the top three vectors on said stack, and the top int on the int stack. +-- Inside of the first vector, replaces the number of instances specified +-- by the top of the int stack of the second vector with the third vector. +instructionVectorReplaceVectorN :: Eq a => Lens' State [[a]] -> State -> State +instructionVectorReplaceVectorN accessor state@(State {_int = i1 : is}) = instructionVectorReplaceVector accessor (Just i1) state{_int = is} +instructionVectorReplaceVectorN _ state = state + -- |Based on two lenses, one of a primitive type and the next of a vector type, --- removes all occurrences inside of the top vector from the vector stack where the top +-- Removes all occurrences inside of the top vector from the vector stack where the top -- item from the primitive stack equals a primitive inside of the vector stack. instructionVectorRemove :: Eq a => Lens' State [a] -> Lens' State [[a]] -> State -> State instructionVectorRemove primAccessor vectorAccessor state = case (uncons (view vectorAccessor state), uncons (view primAccessor state)) of (Just (v1, vs), Just (p1, ps)) -> state & vectorAccessor .~ (replace v1 [p1] [] Nothing : vs) & primAccessor .~ ps _ -> state + +-- |Based on a vector lens and the two vectors on top of said stack. +-- Inside of the first vector, removes the number of instances specified +-- by the Maybe Int parameter of the second vector. Nothing removes all instances. +instructionVectorRemoveVector :: Eq a => Lens' State [[a]] -> Maybe Int -> State -> State +instructionVectorRemoveVector accessor amt state = + case uncons (view accessor state) of + Just (v1, v2 : vs) -> state & accessor .~ (replace v1 v2 [] amt : vs) + _ -> state + +-- |Based on a vector lens, the top two vectors on said stack, and the top int on the int stack. +-- Inside of the first vector, removes the number of instances specified +-- by the top of the int stack of the second vector. +instructionVectorRemoveVectorN :: Eq a => Lens' State [[a]] -> State -> State +instructionVectorRemoveVectorN accessor state@(State {_int = i1 : is}) = instructionVectorRemoveVector accessor (Just i1) state{_int = is} +instructionVectorRemoveVectorN _ state = state -- |Based on two lenses, one of a primitive type and the next of a vector type, -- removes the first occurrence inside of the top vector from the vector stack where the top @@ -467,3 +566,24 @@ instructionVectorSortReverse accessor state = case uncons (view accessor state) of Just (x, xs) -> state & accessor .~ (sortBy (comparing Data.Ord.Down) x : xs) _ -> state + +-- |Takes a vector lens, a primitive lens, and the top of the int stack +-- Inserts the top of the primitive stack into a index specified by the +-- top of the int stack into the top vector from the vector stack. +instructionVectorInsert :: Lens' State [a] -> Lens' State [[a]] -> State -> State +instructionVectorInsert primAccessor vectorAccessor state@(State {_int = i1 : is}) = + case (uncons (view vectorAccessor state{_int = is}), uncons (view primAccessor state{_int = is})) of + (Just (v1, vs), Just (p1, ps)) -> state{_int = is} & primAccessor .~ ps & vectorAccessor .~ (combineTuple p1 (splitAt i1 v1) : vs) + _ -> state +instructionVectorInsert _ _ state = state + +-- |Takes a vector lens and inserts the second vector on the vector stack +-- into the first vector on the vector stack based on an int from the +-- int stack. +instructionVectorInsertVector :: Lens' State [[a]] -> State -> State +instructionVectorInsertVector accessor state@(State {_int = i1 : is}) = + case uncons (view accessor state) of + Just (v1, v2 : vs) -> + state{_int = is} & accessor .~ (combineTupleList v2 (splitAt i1 v1) : vs) + _ -> state +instructionVectorInsertVector _ state = state diff --git a/src/HushGP/Instructions/StringInstructions.hs b/src/HushGP/Instructions/StringInstructions.hs index cd241f1..4a56b38 100644 --- a/src/HushGP/Instructions/StringInstructions.hs +++ b/src/HushGP/Instructions/StringInstructions.hs @@ -5,13 +5,16 @@ import HushGP.Instructions.GenericInstructions import Data.List.Split import Control.Lens +-- |Utility String: Whitespack characters. -- shamelessly stolen from https://hackage.haskell.org/package/MissingH-1.6.0.1/docs/src/Data.String.Utils.html#strip wschars :: String wschars = " \t\r\n" +-- |Utility Function: Strips a string of its whitespace on both sides. strip :: String -> String strip = lstrip . rstrip +-- |Utility Function: Strips a string of its whitespace on the left side. lstrip :: String -> String lstrip s = case s of [] -> [] @@ -19,99 +22,136 @@ lstrip s = case s of then lstrip xs else s --- this is a tad inefficient init +-- |Utility Function: Strips a string of its whitespace on the right side. +-- this is a tad inefficient rstrip :: String -> String rstrip = reverse . lstrip . reverse +-- |Concats the top two strings on the string stack and pushes the result. instructionStringConcat :: State -> State instructionStringConcat = instructionConcat string +-- |Swaps the top two strings on the string stack. instructionStringSwap :: State -> State instructionStringSwap = instructionSwap string +-- |Inserts the second string on the string stack into the first string +-- on the string stack based on an int from the int stack. instructionStringInsertString :: State -> State -instructionStringInsertString state@(State{_string = s1 : s2 : ss, _int = i1 : is}) = state {_string = combineTupleList s2 (splitAt i1 s1) : ss, _int = is} -instructionStringInsertString state = state +instructionStringInsertString = instructionVectorInsertVector string +-- instructionStringInsertString state@(State{_string = s1 : s2 : ss, _int = i1 : is}) = state {_string = combineTupleList s2 (splitAt i1 s1) : ss, _int = is} +-- instructionStringInsertString state = state +-- |Takes the first string from the string stack and pushes the first character +-- back to the string stack as a string. instructionStringFromFirstChar :: State -> State -instructionStringFromFirstChar state@(State {_string = (schar : _) : ss}) = state {_string = [schar] : ss} -instructionStringFromFirstChar state = state +instructionStringFromFirstChar = instructionVectorFromFirstPrim string +-- instructionStringFromFirstChar state@(State {_string = (schar : _) : ss}) = state {_string = [schar] : ss} +-- instructionStringFromFirstChar state = state +-- |Takes the first string from the string stack and pushes the last character +-- back to the string stack as a string. instructionStringFromLastChar :: State -> State -instructionStringFromLastChar state@(State {_string = s1 : ss}) = - if not $ null s1 - then state {_string = [last s1] : ss} - else state -instructionStringFromLastChar state = state +instructionStringFromLastChar = instructionVectorFromLastPrim string +-- instructionStringFromLastChar state@(State {_string = s1 : ss}) = + -- if not $ null s1 + -- then state {_string = [last s1] : ss} + -- else state +-- instructionStringFromLastChar state = state +-- |Takes the first string from the string stack and pushes the Nth character +-- back to the string stack as a string. N in is the top int of the int stack. instructionStringFromNthChar :: State -> State -instructionStringFromNthChar state@(State {_string = s1 : ss, _int = i1 : is}) = state{_string = [s1 !! absNum i1 s1] : ss, _int = is} -instructionStringFromNthChar state = state +instructionStringFromNthChar = instructionVectorFromNthPrim string +-- instructionStringFromNthChar state@(State {_string = s1 : ss, _int = i1 : is}) = state{_string = [s1 !! absNum i1 s1] : ss, _int = is} +-- instructionStringFromNthChar state = state +-- |Takes the first two strings from the top of the string stack. Looks for and pushed the +-- index of the second substring inside of the first substring to the int stack. +-- If not found, returns -1. instructionStringIndexOfString :: State -> State -instructionStringIndexOfString state@(State {_string = s1 : s2 : ss, _int = is}) = state {_string = ss, _int = findSubA s1 s2 : is} -instructionStringIndexOfString state = state +instructionStringIndexOfString = instructionVectorIndexOfVector string +-- instructionStringIndexOfString state@(State {_string = s1 : s2 : ss, _int = is}) = state {_string = ss, _int = findSubA s1 s2 : is} +-- instructionStringIndexOfString state = state +-- |Takes the first two strings from the top of the string stack. Pushes True to the +-- bool stack if the second string is contained within the first string. Pushes False otherwise. instructionStringContainsString :: State -> State -instructionStringContainsString state@(State {_string = s1 : s2 : ss, _bool = bs}) = state {_string = ss, _bool = (findSubA s1 s2 /= -1) : bs} -instructionStringContainsString state = state +instructionStringContainsString = instructionVectorContainsVector string +-- instructionStringContainsString state@(State {_string = s1 : s2 : ss, _bool = bs}) = state {_string = ss, _bool = (findSubA s1 s2 /= -1) : bs} +-- instructionStringContainsString state = state +-- |Takes the first two strings from the top of the string stack. Splits the first string +-- based on the second string and pushes the result to the string stack. -- pysh reverses this. Check this for propeller instructionStringSplitOnString :: State -> State -instructionStringSplitOnString state@(State {_string = s1 : s2 : ss}) = state {_string = reverse $ splitOn s2 s1 <> ss} -instructionStringSplitOnString state = state +instructionStringSplitOnString = instructionVectorSplitOnVector string +-- instructionStringSplitOnString state@(State {_string = s1 : s2 : ss}) = state {_string = reverse $ splitOn s2 s1 <> ss} +-- instructionStringSplitOnString state = state +-- |Takes the first three strings from the top of the string stack. Replaces the first instance of +-- the second string within the first string with the third string. Pushes the result to the string stack. instructionStringReplaceFirstString :: State -> State -instructionStringReplaceFirstString state@(State {_string = s1 : s2 : s3 : ss}) = state {_string = replace s1 s2 s3 (Just 1) : ss} -instructionStringReplaceFirstString state = state +instructionStringReplaceFirstString = instructionVectorReplaceVector string (Just 1) +-- |Takes the first three strings from the top of the string stack. Replaces the number of instances based on the of the int stack of +-- the second string within the first string with the third string. Pushes the result to the string stack. instructionStringReplaceNString :: State -> State -instructionStringReplaceNString state@(State {_string = s1 : s2 : s3 : ss, _int = i1 : is}) = state{_string = replace s1 s2 s3 (Just i1) : ss, _int = is} -instructionStringReplaceNString state = state +instructionStringReplaceNString = instructionVectorReplaceVectorN string +-- |Takes the first three strings from the top of the string stack. Replaces all instances of +-- the second string within the first string with the third string. Pushes the result to the string stack. instructionStringReplaceAllString :: State -> State -instructionStringReplaceAllString state@(State {_string = s1 : s2 : s3 : ss}) = state{_string = replace s1 s2 s3 Nothing : ss} -instructionStringReplaceAllString state = state +instructionStringReplaceAllString = instructionVectorReplaceVector string Nothing +-- |Takes the first two strings from the top of the string stack. Removes the first instance of +-- the second string. Pushes the result to the string stack. instructionStringRemoveFirstString :: State -> State -instructionStringRemoveFirstString state@(State {_string = s1 : s2 : ss}) = state{_string = replace s1 s2 "" (Just 1) : ss} -instructionStringRemoveFirstString state = state +instructionStringRemoveFirstString = instructionVectorRemoveVector string (Just 1) +-- |Takes the first two strings from the top of the string stack. Removes N instances +-- based on the top int from the int stack of the second string. Pushes the result to the string stack. instructionStringRemoveNString :: State -> State -instructionStringRemoveNString state@(State {_string = s1 : s2 : ss, _int = i1 : is}) = state{_string = replace s1 s2 "" (Just i1) : ss, _int = is} -instructionStringRemoveNString state = state +instructionStringRemoveNString = instructionVectorRemoveVectorN string +-- |Takes the first two strings from the top of the string stack. Removes all instances of +-- the second string. Pushes the result to the string stack. instructionStringRemoveAllString :: State -> State -instructionStringRemoveAllString state@(State {_string = s1 : s2 : ss}) = state{_string = replace s1 s2 "" Nothing : ss} -instructionStringRemoveAllString state = state +instructionStringRemoveAllString = instructionVectorRemoveVector string Nothing +-- |Counts the amount of occurrences of the second string in the first +-- string. Pushes the result to the string stack. instructionStringOccurrencesOfString :: State -> State -instructionStringOccurrencesOfString state@(State {_string = s1 : s2 : ss, _int = is}) = state{_string = ss, _int = amtOccurences s1 s2 : is} -instructionStringOccurrencesOfString state = state +instructionStringOccurrencesOfString = instructionVectorOccurrencesOfVector string +-- |Inserts the top char of the char stack into the top string of the string +-- stack based on an index from the top int of the int stack. instructionStringInsertChar :: State -> State -instructionStringInsertChar state@(State {_string = s1 : ss, _char = c1 : cs, _int = i1 : is}) = state{_string = combineTuple c1 (splitAt i1 s1) : ss, _char = cs, _int = is} -instructionStringInsertChar state = state +instructionStringInsertChar = instructionVectorInsert char string +-- |Pushes True to the bool stack if the top char on the char stack is within the +-- top string on the string stack. Pushes False otherwise. instructionStringContainsChar :: State -> State instructionStringContainsChar = instructionVectorContains char string +-- |Pushes the first index found of the top char of the char stack within the +-- first string in the string stack to the int stack. instructionStringIndexOfChar :: State -> State instructionStringIndexOfChar = instructionVectorIndexOf char string +-- |@TODO instructionStringSplitOnChar :: State -> State -instructionStringSplitOnChar state@(State {_string = s1 : ss, _char = c1 : cs}) = state {_string = reverse $ splitOn [c1] s1 <> ss, _char = cs} -instructionStringSplitOnChar state = state +instructionStringSplitOnChar = instructionVectorSplitOn char string instructionStringReplaceFirstChar :: State -> State -instructionStringReplaceFirstChar = instructionVectorReplaceFirst char string +instructionStringReplaceFirstChar = instructionVectorReplace char string (Just 1) instructionStringReplaceNChar :: State -> State instructionStringReplaceNChar state@(State {_string = s1 : ss, _char = c1 : c2 : cs, _int = i1 : is}) = state{_string = replace s1 [c1] [c2] (Just i1) : ss, _char = cs, _int = is} instructionStringReplaceNChar state = state instructionStringReplaceAllChar :: State -> State -instructionStringReplaceAllChar = instructionVectorReplace char string +instructionStringReplaceAllChar = instructionVectorReplace char string Nothing instructionStringRemoveFirstChar :: State -> State instructionStringRemoveFirstChar state@(State {_string = s1 : ss, _char = c1 : cs}) = state {_string = replace s1 [c1] "" (Just 1) : ss, _char = cs} diff --git a/src/HushGP/Instructions/VectorBoolInstructions.hs b/src/HushGP/Instructions/VectorBoolInstructions.hs index 50348d0..a69d00d 100644 --- a/src/HushGP/Instructions/VectorBoolInstructions.hs +++ b/src/HushGP/Instructions/VectorBoolInstructions.hs @@ -55,10 +55,10 @@ instructionVectorBoolSetNth :: State -> State instructionVectorBoolSetNth = instructionVectorSetNth bool vectorBool instructionVectorBoolReplace :: State -> State -instructionVectorBoolReplace = instructionVectorReplace bool vectorBool +instructionVectorBoolReplace = instructionVectorReplace bool vectorBool Nothing instructionVectorBoolReplaceFirst :: State -> State -instructionVectorBoolReplaceFirst = instructionVectorReplaceFirst bool vectorBool +instructionVectorBoolReplaceFirst = instructionVectorReplace bool vectorBool (Just 1) instructionVectorBoolRemove :: State -> State instructionVectorBoolRemove = instructionVectorRemove bool vectorBool diff --git a/src/HushGP/Instructions/VectorCharInstructions.hs b/src/HushGP/Instructions/VectorCharInstructions.hs index 5b4423a..c7a4fe7 100644 --- a/src/HushGP/Instructions/VectorCharInstructions.hs +++ b/src/HushGP/Instructions/VectorCharInstructions.hs @@ -55,10 +55,10 @@ instructionVectorCharSetNth :: State -> State instructionVectorCharSetNth = instructionVectorSetNth char vectorChar instructionVectorCharReplace :: State -> State -instructionVectorCharReplace = instructionVectorReplace char vectorChar +instructionVectorCharReplace = instructionVectorReplace char vectorChar Nothing instructionVectorCharReplaceFirst :: State -> State -instructionVectorCharReplaceFirst = instructionVectorReplaceFirst char vectorChar +instructionVectorCharReplaceFirst = instructionVectorReplace char vectorChar (Just 1) instructionVectorCharRemove :: State -> State instructionVectorCharRemove = instructionVectorRemove char vectorChar diff --git a/src/HushGP/Instructions/VectorFloatInstructions.hs b/src/HushGP/Instructions/VectorFloatInstructions.hs index 59a2ba4..b972685 100644 --- a/src/HushGP/Instructions/VectorFloatInstructions.hs +++ b/src/HushGP/Instructions/VectorFloatInstructions.hs @@ -55,10 +55,10 @@ instructionVectorFloatSetNth :: State -> State instructionVectorFloatSetNth = instructionVectorSetNth float vectorFloat instructionVectorFloatReplace :: State -> State -instructionVectorFloatReplace = instructionVectorReplace float vectorFloat +instructionVectorFloatReplace = instructionVectorReplace float vectorFloat Nothing instructionVectorFloatReplaceFirst :: State -> State -instructionVectorFloatReplaceFirst = instructionVectorReplaceFirst float vectorFloat +instructionVectorFloatReplaceFirst = instructionVectorReplace float vectorFloat (Just 1) instructionVectorFloatRemove :: State -> State instructionVectorFloatRemove = instructionVectorRemove float vectorFloat diff --git a/src/HushGP/Instructions/VectorIntInstructions.hs b/src/HushGP/Instructions/VectorIntInstructions.hs index c7e79e2..1eca9ac 100644 --- a/src/HushGP/Instructions/VectorIntInstructions.hs +++ b/src/HushGP/Instructions/VectorIntInstructions.hs @@ -55,10 +55,10 @@ instructionVectorIntSetNth :: State -> State instructionVectorIntSetNth = instructionVectorSetNth int vectorInt instructionVectorIntReplace :: State -> State -instructionVectorIntReplace = instructionVectorReplace int vectorInt +instructionVectorIntReplace = instructionVectorReplace int vectorInt Nothing instructionVectorIntReplaceFirst :: State -> State -instructionVectorIntReplaceFirst = instructionVectorReplaceFirst int vectorInt +instructionVectorIntReplaceFirst = instructionVectorReplace int vectorInt (Just 1) instructionVectorIntRemove :: State -> State instructionVectorIntRemove = instructionVectorRemove int vectorInt diff --git a/src/HushGP/Instructions/VectorStringInstructions.hs b/src/HushGP/Instructions/VectorStringInstructions.hs index faf6af2..9e0348c 100644 --- a/src/HushGP/Instructions/VectorStringInstructions.hs +++ b/src/HushGP/Instructions/VectorStringInstructions.hs @@ -55,10 +55,10 @@ instructionVectorStringSetNth :: State -> State instructionVectorStringSetNth = instructionVectorSetNth string vectorString instructionVectorStringReplace :: State -> State -instructionVectorStringReplace = instructionVectorReplace string vectorString +instructionVectorStringReplace = instructionVectorReplace string vectorString Nothing instructionVectorStringReplaceFirst :: State -> State -instructionVectorStringReplaceFirst = instructionVectorReplaceFirst string vectorString +instructionVectorStringReplaceFirst = instructionVectorReplace string vectorString (Just 1) instructionVectorStringRemove :: State -> State instructionVectorStringRemove = instructionVectorRemove string vectorString From 6e40f3d3c270f31a80c4727b58ab0e6f8c5c2787 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sun, 9 Feb 2025 22:12:32 -0600 Subject: [PATCH 135/171] template haskell to generate function lists --- TODO.md | 1 + 1 file changed, 1 insertion(+) diff --git a/TODO.md b/TODO.md index d0e1d2e..d5902e5 100644 --- a/TODO.md +++ b/TODO.md @@ -14,6 +14,7 @@ - [X] Refactor all functions to take state as the final parameter - [X] Standardize the pattern matching parameter names, such as c1 : cs - [ ] Write unit/quickcheck tests for all of the instructions +- [ ] Use template haskell to generate function lists - [ ] Move utility functions to their own file ## PushGP TODO From b936dda857a15cfcd004959e9a88b4451323f1c6 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sun, 9 Feb 2025 22:17:18 -0600 Subject: [PATCH 136/171] more documentation/generic string functions --- .../Instructions/GenericInstructions.hs | 76 +++++++++++++---- src/HushGP/Instructions/StringInstructions.hs | 85 +++++++++++++------ 2 files changed, 119 insertions(+), 42 deletions(-) diff --git a/src/HushGP/Instructions/GenericInstructions.hs b/src/HushGP/Instructions/GenericInstructions.hs index fdca24a..a5173da 100644 --- a/src/HushGP/Instructions/GenericInstructions.hs +++ b/src/HushGP/Instructions/GenericInstructions.hs @@ -242,8 +242,8 @@ instructionConcat accessor state = -- |Based on two lenses, one of a primitive type and the next of a vector type, -- takes the top item of the primitive stack and prepends it to the first vector in -- the vector stack if there is one. -instructionConj :: Lens' State [a] -> Lens' State [[a]] -> State -> State -instructionConj primAccessor vectorAccessor state = +instructionVectorConj :: Lens' State [a] -> Lens' State [[a]] -> State -> State +instructionVectorConj primAccessor vectorAccessor state = case (uncons (view primAccessor state), uncons (view vectorAccessor state)) of (Just (p1,ps), Just (v1,vs)) -> state & primAccessor .~ ps & vectorAccessor .~ ((p1 : v1) : vs) _ -> state @@ -251,20 +251,27 @@ instructionConj primAccessor vectorAccessor state = -- |Based on two lenses, one of a primitive type and the next of a vector type, -- takes the top item of the primitive stack and appends it to the first vector in -- the vector stack if there is one. -instructionConjEnd :: Lens' State [a] -> Lens' State [[a]] -> State -> State -instructionConjEnd primAccessor vectorAccessor state = +instructionVectorConjEnd :: Lens' State [a] -> Lens' State [[a]] -> State -> State +instructionVectorConjEnd primAccessor vectorAccessor state = case (uncons (view primAccessor state), uncons (view vectorAccessor state)) of (Just (p1,ps), Just (v1,vs)) -> state & primAccessor .~ ps & vectorAccessor .~ ((v1 <> [p1]) : vs) _ -> state -- |Takes the first N items from the first vector on the top of a vector stack and -- pushes the result to said vector stack. -instructionTakeN :: Lens' State [[a]] -> State -> State -instructionTakeN accessor state@(State {_int = i1 : is}) = +instructionVectorTakeN :: Lens' State [[a]] -> State -> State +instructionVectorTakeN accessor state@(State {_int = i1 : is}) = case uncons (view accessor state) of Just (v1, vs) -> state{_int = is} & accessor .~ (take (absNum i1 v1) v1 : vs) _ -> state -instructionTakeN _ state = state +instructionVectorTakeN _ state = state + +instructionVectorTakeRN :: Lens' State [[a]] -> State -> State +instructionVectorTakeRN accessor state@(State {_int = i1 : is}) = + case uncons (view accessor state) of + Just (v1, vs) -> state{_int = is} & accessor .~ (takeR (absNum i1 v1) v1 : vs) + _ -> state +instructionVectorTakeRN _ state = state -- |Takes the sublist of the top vector based on a passed lens. Check out the -- subList documentation for information on how this works. @@ -341,21 +348,38 @@ instructionVectorFromNthPrim _ state = state -- |Takes the top vector, removes the first item of said vector, and pushes the result back to top -- of the stack, based on a lens. -instructionRest :: Lens' State [[a]] -> State -> State -instructionRest accessor state = +instructionVectorRest :: Lens' State [[a]] -> State -> State +instructionVectorRest accessor state = case uncons (view accessor state) of Just (v1, vs) -> state & accessor .~ (drop 1 v1 : vs) _ -> state -- |Takes the top vector, removes the last item of said vector, and pushes the result back to top --- of the stack, based on a lens. -instructionButLast :: Lens' State [[a]] -> State -> State -instructionButLast accessor state = +-- of the stack, based on a vector lens. +instructionVectorButLast :: Lens' State [[a]] -> State -> State +instructionVectorButLast accessor state = case uncons (view accessor state) of Just (v1, vs) -> state & accessor .~ (safeInit v1 : vs) _ -> state --- |Takes the top vector, pushes the length of that vector to the int stack, based on a lens. +-- |Based on a vector lens, drops the first N items from the top vector. +-- Pushes the result back to the vector stack. N is pulled from the top +-- of the int stack. +instructionVectorDrop :: Lens' State [[a]] -> State -> State +instructionVectorDrop accessor state@(State {_int = i1 : is}) = + case uncons (view accessor state{_int = is}) of + Just (v1, vs) -> state{_int = is} & accessor .~ (drop (absNum i1 v1) v1 : vs) + _ -> state +instructionVectorDrop _ state = state + +instructionVectorDropR :: Lens' State [[a]] -> State -> State +instructionVectorDropR accessor state@(State {_int = i1 : is}) = + case uncons (view accessor state{_int = is}) of + Just (v1, vs) -> state{_int = is} & accessor .~ (dropR (absNum i1 v1) v1 : vs) + _ -> state +instructionVectorDropR _ state = state + +-- |Takes the top vector, pushes the length of that vector to the int stack, based on a vector lens. instructionLength :: Lens' State [[a]] -> State -> State instructionLength accessor state@(State {_int = is}) = case uncons (view accessor state) of @@ -479,15 +503,23 @@ instructionVectorSplitOnVector accessor state = _ -> state -- |Based on two lenses, one of a primitive type and the next of a vector type, --- replaces all occurrences inside of the top vector from the vector stack with two values from +-- replaces Maybe Int occurrences inside of the top vector from the vector stack with two values from -- the primitive stack. The top of the primitive stack is the old value to be replaced. The second item --- in the primitive stack is the new value to replace the old one. +-- in the primitive stack is the new value to replace the old one. Nothing replaces all occurrences. instructionVectorReplace :: Eq a => Lens' State [a] -> Lens' State [[a]] -> Maybe Int -> State -> State instructionVectorReplace primAccessor vectorAccessor amt state = case (uncons (view vectorAccessor state), uncons (view primAccessor state)) of (Just (v1, vs), Just (p1, p2 : ps)) -> state & vectorAccessor .~ (replace v1 [p1] [p2] amt: vs) & primAccessor .~ ps _ -> state +-- |Based on two lenses, one of a primitive type and the next of a vector type, +-- replaces N occurrences inside of the top vector from the vector stack with two values from +-- the primitive stack. The top of the primitive stack is the old value to be replaced. The second item +-- in the primitive stack is the new value to replace the old one. N is pulled from the top of the int stack. +instructionVectorReplaceN :: Eq a => Lens' State [a] -> Lens' State [[a]] -> State -> State +instructionVectorReplaceN primAccessor vectorAccessor state@(State {_int = i1 : is}) = instructionVectorReplace primAccessor vectorAccessor (Just i1) state{_int = is} +instructionVectorReplaceN _ _ state = state + -- |Based on a vector lens and the top three vectors on said stack. -- Inside of the first vector, replaces the number of instances specified -- by the Maybe Int parameter of the second vector with the third vector. @@ -508,12 +540,20 @@ instructionVectorReplaceVectorN _ state = state -- |Based on two lenses, one of a primitive type and the next of a vector type, -- Removes all occurrences inside of the top vector from the vector stack where the top -- item from the primitive stack equals a primitive inside of the vector stack. -instructionVectorRemove :: Eq a => Lens' State [a] -> Lens' State [[a]] -> State -> State -instructionVectorRemove primAccessor vectorAccessor state = +instructionVectorRemove :: Eq a => Lens' State [a] -> Lens' State [[a]] -> Maybe Int -> State -> State +instructionVectorRemove primAccessor vectorAccessor amt state = case (uncons (view vectorAccessor state), uncons (view primAccessor state)) of - (Just (v1, vs), Just (p1, ps)) -> state & vectorAccessor .~ (replace v1 [p1] [] Nothing : vs) & primAccessor .~ ps + (Just (v1, vs), Just (p1, ps)) -> state & vectorAccessor .~ (replace v1 [p1] [] amt: vs) & primAccessor .~ ps _ -> state +-- |Based on two lenses, one of a primitive type and the next of a vector type, +-- Removes N occurrences inside of the top vector from the vector stack where the top +-- item from the primitive stack equals a primitive inside of the vector stack. N is pulled +-- from the top of the int stack. +instructionVectorRemoveN :: Eq a => Lens' State [a] -> Lens' State [[a]] -> State -> State +instructionVectorRemoveN primAccessor vectorAccessor state@(State {_int = i1 : is}) = instructionVectorRemove primAccessor vectorAccessor (Just i1) state{_int = is} +instructionVectorRemoveN _ _ state = state + -- |Based on a vector lens and the two vectors on top of said stack. -- Inside of the first vector, removes the number of instances specified -- by the Maybe Int parameter of the second vector. Nothing removes all instances. diff --git a/src/HushGP/Instructions/StringInstructions.hs b/src/HushGP/Instructions/StringInstructions.hs index 4a56b38..7abbc13 100644 --- a/src/HushGP/Instructions/StringInstructions.hs +++ b/src/HushGP/Instructions/StringInstructions.hs @@ -2,7 +2,6 @@ module HushGP.Instructions.StringInstructions where import HushGP.State import HushGP.Instructions.GenericInstructions -import Data.List.Split import Control.Lens -- |Utility String: Whitespack characters. @@ -139,74 +138,112 @@ instructionStringContainsChar = instructionVectorContains char string instructionStringIndexOfChar :: State -> State instructionStringIndexOfChar = instructionVectorIndexOf char string --- |@TODO +-- |Takes the top string from the string stack and the top +-- char from the char stack. Splits the top string based on +-- the top char and pushes the result to the string stack. instructionStringSplitOnChar :: State -> State instructionStringSplitOnChar = instructionVectorSplitOn char string +-- |Takes the top string from the string stack and the two top char from the char stack. +-- Replaces the first instance of the top char with the second char. instructionStringReplaceFirstChar :: State -> State instructionStringReplaceFirstChar = instructionVectorReplace char string (Just 1) +-- |Takes the top string from the string stack and the two top char from the char stack. +-- Replaces N instances of the top char with the second char. N is determined by the +-- top int on the int stack. instructionStringReplaceNChar :: State -> State -instructionStringReplaceNChar state@(State {_string = s1 : ss, _char = c1 : c2 : cs, _int = i1 : is}) = state{_string = replace s1 [c1] [c2] (Just i1) : ss, _char = cs, _int = is} -instructionStringReplaceNChar state = state +instructionStringReplaceNChar = instructionVectorReplaceN char string +-- |Takes the top string from the string stack and the two top char from the char stack. +-- Replaces all instances of the top char with the second char. instructionStringReplaceAllChar :: State -> State instructionStringReplaceAllChar = instructionVectorReplace char string Nothing +-- |Takes the top string from the string stack and the top char from the char stack. +-- Removes the first instance of the top char with the second char. instructionStringRemoveFirstChar :: State -> State -instructionStringRemoveFirstChar state@(State {_string = s1 : ss, _char = c1 : cs}) = state {_string = replace s1 [c1] "" (Just 1) : ss, _char = cs} -instructionStringRemoveFirstChar state = state +instructionStringRemoveFirstChar = instructionVectorRemove char string (Just 1) +-- |Takes the top string from the string stack and the top char from the char stack. +-- Removes N instances of the top char with the second char. N is pulled from the top +-- of the int stack. instructionStringRemoveNChar :: State -> State -instructionStringRemoveNChar state@(State {_string = s1 : ss, _char = c1 : cs, _int = i1 : is}) = state{_string = replace s1 [c1] "" (Just i1) : ss, _char = cs, _int = is} -instructionStringRemoveNChar state = state +instructionStringRemoveNChar = instructionVectorRemoveN char string +-- |Takes the top string from the string stack and the top char from the char stack. +-- Removes all instances of the top char with the second char. instructionStringRemoveAllChar :: State -> State -instructionStringRemoveAllChar = instructionVectorRemove char string +instructionStringRemoveAllChar = instructionVectorRemove char string Nothing +-- |Takes the top string from the string stack and the top char from the char stack. +-- Counts the amount of occurrences of the top char inside of the top string. Pushes +-- this result to the int stack. instructionStringOccurrencesOfChar :: State -> State instructionStringOccurrencesOfChar = instructionVectorOccurrencesOf char string +-- |Takes the top string from the string stack and reverses it. Pushes the reversed string +-- to the top of the stack. instructionStringReverse :: State -> State instructionStringReverse = instructionReverse string +-- |Takes the top string from the string stack, takes the first N chars from the top string, +-- and pushes the result to the string stack. N is pulled from the top of the int stack. instructionStringHead :: State -> State -instructionStringHead = instructionTakeN string +instructionStringHead = instructionVectorTakeN string +-- |Takes the top string from the string stack, takes the last N chars from the top string, +-- and pushes the result to the string stack. N is pulled from the top of the int stack. instructionStringTail :: State -> State -instructionStringTail state@(State {_string = s1 : ss, _int = i1 : is}) = state{_string = takeR (absNum i1 s1) s1 : ss, _int = is} -instructionStringTail state = state +instructionStringTail = instructionVectorTakeRN string +-- |Takes the top string from the string stack and the top char from the char stack. +-- Prepends the top char to the top string. Pushes the result to the string stack. +instructionStringPrependChar :: State -> State +instructionStringPrependChar = instructionVectorConj char string + +-- |Takes the top string from the string stack and the top char from the char stack. +-- Appends the top char to the top string. Pushes the result to the string stack. instructionStringAppendChar :: State -> State -instructionStringAppendChar = instructionConj char string - -instructionStringConjEndChar :: State -> State -instructionStringConjEndChar = instructionConjEnd char string +instructionStringAppendChar = instructionVectorConjEnd char string +-- |Takes the top string from the string stack and removes the first char +-- from said string. Pushes the result to the string stack. instructionStringRest :: State -> State -instructionStringRest = instructionRest string +instructionStringRest = instructionVectorRest string +-- |Takes the top string from the string stack and removes the last char +-- from said string. Pushes the result to the string stack. instructionStringButLast :: State -> State -instructionStringButLast = instructionButLast string +instructionStringButLast = instructionVectorButLast string +-- |Takes the top string from the string stack and drops the first N characters +-- from said string. Pushes the result to the string stack. N is pulled from the top +-- of the int stack. instructionStringDrop :: State -> State -instructionStringDrop state@(State {_string = s1 : ss, _int = i1 : is}) = state{_string = drop (absNum i1 s1) s1 : ss, _int = is} -instructionStringDrop state = state +instructionStringDrop = instructionVectorDrop string +-- |Takes the top string from the string stack and drops the last N characters +-- from said string. Pushes the result to the string stack. N is pulled from the top +-- of the int stack. instructionStringButLastN :: State -> State -instructionStringButLastN state@(State {_string = s1 : ss, _int = i1 : is}) = state{_string = dropR (absNum i1 s1) s1 : ss, _int = is} -instructionStringButLastN state = state +instructionStringButLastN = instructionVectorDropR string +-- |Takes the top string from the string stack and calculates the length. The length +-- is then pushed to the int stack. instructionStringLength :: State -> State instructionStringLength = instructionLength string +-- |Makes an empty string and pushes it to the top of the string stack. instructionStringMakeEmpty :: State -> State instructionStringMakeEmpty = instructionVectorMakeEmpty string +-- |Checks to see if the top string is empty on the string stack. +-- Pushes True to the bool stack if empty. Pushes False if not. instructionStringIsEmptyString :: State -> State -instructionStringIsEmptyString state@(State {_string = s1 : ss, _bool = bs}) = state{_string = ss, _bool = null s1 : bs} -instructionStringIsEmptyString state = state +instructionStringIsEmptyString = instructionVectorIsEmpty string +-- TODO: Make this generic instructionStringRemoveNth :: State -> State instructionStringRemoveNth state@(State {_string = s1 : ss, _int = i1 : is}) = state{_string = deleteAt (absNum i1 s1) s1 : ss, _int = is} instructionStringRemoveNth state = state From 058b019ccdd7d428497baf4453978d3e532c22b6 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Mon, 10 Feb 2025 14:43:29 -0600 Subject: [PATCH 137/171] modify maintainer status --- HushGP.cabal | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/HushGP.cabal b/HushGP.cabal index fa30da6..5679300 100644 --- a/HushGP.cabal +++ b/HushGP.cabal @@ -19,10 +19,10 @@ version: 0.1.0.0 synopsis: A PushGP implementation in Haskell. -- The package author(s). -author: Taylor +author: Rowan Torbitzky-Lane, Taylor -- An email address to which users can send suggestions, bug reports, and patches. -maintainer: behindthebrain@zoho.eu +maintainer: rowan.a.tl@protonmail.com category: Data build-type: Simple From 14ec3b727e505db8d214e236fc9d9b3cc06f44da Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Mon, 10 Feb 2025 14:44:12 -0600 Subject: [PATCH 138/171] most string funcs generic/doced --- src/HushGP/Instructions/FloatInstructions.hs | 4 +- .../Instructions/GenericInstructions.hs | 26 ++++++-- src/HushGP/Instructions/StringInstructions.hs | 60 ++++++++++++++++--- .../Instructions/VectorBoolInstructions.hs | 12 ++-- .../Instructions/VectorCharInstructions.hs | 12 ++-- .../Instructions/VectorFloatInstructions.hs | 12 ++-- .../Instructions/VectorIntInstructions.hs | 12 ++-- .../Instructions/VectorStringInstructions.hs | 12 ++-- 8 files changed, 104 insertions(+), 46 deletions(-) diff --git a/src/HushGP/Instructions/FloatInstructions.hs b/src/HushGP/Instructions/FloatInstructions.hs index a0efe95..c7566a4 100644 --- a/src/HushGP/Instructions/FloatInstructions.hs +++ b/src/HushGP/Instructions/FloatInstructions.hs @@ -126,12 +126,12 @@ instructionFloatEq = instructionEq float instructionFloatStackDepth :: State -> State instructionFloatStackDepth = instructionStackDepth float --- |Moves an item from deep within the float stack to the top of the float stack based on +-- |Copies an item from deep within the float stack to the top of the float stack based on -- the top int from the int stack. instructionFloatYankDup :: State -> State instructionFloatYankDup = instructionYankDup float --- |Copies an item from deep within the float stack to the top of the float stack based on +-- |Moves an item from deep within the float stack to the top of the float stack based on -- the top int from the int stack. instructionFloatYank :: State -> State instructionFloatYank = instructionYank float diff --git a/src/HushGP/Instructions/GenericInstructions.hs b/src/HushGP/Instructions/GenericInstructions.hs index a5173da..ad35a7a 100644 --- a/src/HushGP/Instructions/GenericInstructions.hs +++ b/src/HushGP/Instructions/GenericInstructions.hs @@ -230,8 +230,8 @@ instructionShove :: Lens' State [a] -> State -> State instructionShove accessor state = instructionShoveDup accessor state & accessor .~ drop 1 (view accessor (instructionShoveDup accessor state )) -- |Concats two semigroupable items together based on a lens. Not char generic. -instructionConcat :: Semigroup a => Lens' State [a] -> State -> State -instructionConcat accessor state = +instructionVectorConcat :: Semigroup a => Lens' State [a] -> State -> State +instructionVectorConcat accessor state = case uncons (view accessor state) of Just (x1, x2:_) -> droppedState & accessor .~ (x1 <> x2) : view accessor droppedState _ -> state @@ -266,6 +266,8 @@ instructionVectorTakeN accessor state@(State {_int = i1 : is}) = _ -> state instructionVectorTakeN _ state = state +-- |Takes the last N items from the first vector on the top of a vector stack and +-- pushes the result to said vector stack. instructionVectorTakeRN :: Lens' State [[a]] -> State -> State instructionVectorTakeRN accessor state@(State {_int = i1 : is}) = case uncons (view accessor state) of @@ -372,6 +374,9 @@ instructionVectorDrop accessor state@(State {_int = i1 : is}) = _ -> state instructionVectorDrop _ state = state +-- |Based on a vector lens, drops the last N items from the top vector. +-- Pushes the result back to the vector stack. N is pulled from the top +-- of the int stack. instructionVectorDropR :: Lens' State [[a]] -> State -> State instructionVectorDropR accessor state@(State {_int = i1 : is}) = case uncons (view accessor state{_int = is}) of @@ -538,8 +543,9 @@ instructionVectorReplaceVectorN accessor state@(State {_int = i1 : is}) = instru instructionVectorReplaceVectorN _ state = state -- |Based on two lenses, one of a primitive type and the next of a vector type, --- Removes all occurrences inside of the top vector from the vector stack where the top --- item from the primitive stack equals a primitive inside of the vector stack. +-- Removes Maybe Int occurrences inside of the top vector from the vector stack where the top +-- item from the primitive stack equals a primitive inside of the vector stack. If Nothing is passed +-- rather than a Just Int, will remove all occurrences. instructionVectorRemove :: Eq a => Lens' State [a] -> Lens' State [[a]] -> Maybe Int -> State -> State instructionVectorRemove primAccessor vectorAccessor amt state = case (uncons (view vectorAccessor state), uncons (view primAccessor state)) of @@ -549,11 +555,21 @@ instructionVectorRemove primAccessor vectorAccessor amt state = -- |Based on two lenses, one of a primitive type and the next of a vector type, -- Removes N occurrences inside of the top vector from the vector stack where the top -- item from the primitive stack equals a primitive inside of the vector stack. N is pulled --- from the top of the int stack. +-- from the top of the int stack. Not to be confused with instructionVectorRemoveNth. instructionVectorRemoveN :: Eq a => Lens' State [a] -> Lens' State [[a]] -> State -> State instructionVectorRemoveN primAccessor vectorAccessor state@(State {_int = i1 : is}) = instructionVectorRemove primAccessor vectorAccessor (Just i1) state{_int = is} instructionVectorRemoveN _ _ state = state +-- |Based on a vector lens. Removes the Nth index of the top vector of the passed +-- vector stack. N is pulled from the top of the int stack. Not to be confused with +-- instructionVectorRemoveN. +instructionVectorRemoveNth :: Lens' State [[a]] -> State -> State +instructionVectorRemoveNth accessor state@(State {_int = i1 : is}) = + case uncons (view accessor state{_int = is}) of + Just (v1, vs) -> state{_int = is} & accessor .~ (deleteAt (absNum i1 v1) v1 : vs) + _ -> state +instructionVectorRemoveNth _ state = state + -- |Based on a vector lens and the two vectors on top of said stack. -- Inside of the first vector, removes the number of instances specified -- by the Maybe Int parameter of the second vector. Nothing removes all instances. diff --git a/src/HushGP/Instructions/StringInstructions.hs b/src/HushGP/Instructions/StringInstructions.hs index 7abbc13..1804b4d 100644 --- a/src/HushGP/Instructions/StringInstructions.hs +++ b/src/HushGP/Instructions/StringInstructions.hs @@ -28,7 +28,7 @@ rstrip = reverse . lstrip . reverse -- |Concats the top two strings on the string stack and pushes the result. instructionStringConcat :: State -> State -instructionStringConcat = instructionConcat string +instructionStringConcat = instructionVectorConcat string -- |Swaps the top two strings on the string stack. instructionStringSwap :: State -> State @@ -243,84 +243,126 @@ instructionStringMakeEmpty = instructionVectorMakeEmpty string instructionStringIsEmptyString :: State -> State instructionStringIsEmptyString = instructionVectorIsEmpty string --- TODO: Make this generic +-- |Removes the Nth char from the top string of the string stack. N is pulled +-- from the top of the int stack. instructionStringRemoveNth :: State -> State -instructionStringRemoveNth state@(State {_string = s1 : ss, _int = i1 : is}) = state{_string = deleteAt (absNum i1 s1) s1 : ss, _int = is} -instructionStringRemoveNth state = state +instructionStringRemoveNth = instructionVectorRemoveNth string +-- |Sets the Nth char from the top string of the string stack to the top char from +-- the char stack. N is pulled from the top of the int stack. instructionStringSetNth :: State -> State instructionStringSetNth = instructionVectorSetNth char string +-- |Strips the whitespace of the top string on the string stack and pushes the result +-- back to the string stack. instructionStringStripWhitespace :: State -> State instructionStringStripWhitespace state@(State {_string = s1 : ss}) = state{_string = strip s1 : ss} instructionStringStripWhitespace state = state -instructionStringFromLens :: Show a => State -> Lens' State [a] -> State -instructionStringFromLens state@(State {_string = ss}) accessor = +-- |Utility Function: Casts a type based on a lens to a string. Pushes the result +-- to the string stack. +instructionStringFromLens :: Show a => Lens' State [a] -> State -> State +instructionStringFromLens accessor state@(State {_string = ss}) = case uncons (view accessor state) of Nothing -> state Just (x1,_) -> state{_string = show x1 : ss} +-- |Converts the top bool from the bool stack to a string. Pushes the result to +-- the string stack. instructionStringFromBool :: State -> State -instructionStringFromBool state = instructionStringFromLens state bool +instructionStringFromBool = instructionStringFromLens bool +-- |Converts the top int from the int stack to a string. Pushes the result to +-- the string stack. instructionStringFromInt :: State -> State -instructionStringFromInt state = instructionStringFromLens state int +instructionStringFromInt = instructionStringFromLens int +-- |Converts the top float from the float stack to a string. Pushes the result to +-- the string stack. instructionStringFromFloat :: State -> State -instructionStringFromFloat state = instructionStringFromLens state float +instructionStringFromFloat = instructionStringFromLens float +-- |Converts the top char from the char stack to a string. Pushes the result to +-- the string stack. instructionStringFromChar :: State -> State instructionStringFromChar state@(State {_string = ss, _char = c1 : cs}) = state{_string = [c1] : ss, _char = cs} instructionStringFromChar state = state +-- |Removes the top string from the string stack. instructionStringPop :: State -> State instructionStringPop = instructionPop string +-- |Duplicates the top string on the string stack. instructionStringDup :: State -> State instructionStringDup = instructionDup string +-- |Duplicates the top string on the string stack N times based off the top of the int stack. instructionStringDupN :: State -> State instructionStringDupN = instructionDupN string +-- |Rotates the top three strings on the string stack. instructionStringRot :: State -> State instructionStringRot = instructionRot string +-- |Sets the string stack to [] instructionStringFlush :: State -> State instructionStringFlush = instructionFlush string +-- |Checks to see if the top two strings are equal and pushes the result +-- to the bool stack. instructionStringEq :: State -> State instructionStringEq = instructionEq string +-- |Calculates the size of the string stack and pushes the result +-- to the int stack. instructionStringStackDepth :: State -> State instructionStringStackDepth = instructionStackDepth string +-- |Moves an item from deep within the string stack to the top of the string stack based on +-- the top int from the int stack. instructionStringYank :: State -> State instructionStringYank = instructionYank string +-- |Copies an item from deep within the string stack to the top of the string stack based on +-- the top int from the int stack. instructionStringYankDup :: State -> State instructionStringYankDup = instructionYankDup string +-- |Pushes True to the bool stack if the string stack is empty. Pushes False otherwise. instructionStringIsStackEmpty :: State -> State instructionStringIsStackEmpty = instructionIsStackEmpty string +-- |Moves an item from the top of the string stack to deep within the string stack based on +-- the top int from the int stack. instructionStringShove :: State -> State instructionStringShove = instructionShove string +-- |Copies an item from the top of the string stack to deep within the string stack based on +-- the top int from the int stack. instructionStringShoveDup :: State -> State instructionStringShoveDup = instructionShoveDup string +-- |Sorts the top string on the string stack by their ascii value and pushes the result +-- back to the string stack. instructionStringSort :: State -> State instructionStringSort = instructionVectorSort string +-- |Sorts the top string on the string stack backwards by their ascii value and pushes the result +-- back to the string stack. instructionStringSortReverse :: State -> State instructionStringSortReverse = instructionVectorSortReverse string +-- |Duplicate the top N items from the string stack based on the top int from the int stack. instructionStringDupItems :: State -> State instructionStringDupItems = instructionDupItems string +-- |Takes the top string and splits its up into strings of size 1 and pushes all of those +-- strings back onto the string stack. instructionStringParseToChar :: State -> State instructionStringParseToChar = instructionVectorParseToPrim string +-- |Uses the top two ints from the top of the int stack to pull a sub string +-- from the top string on the string stack. Pushes the result back to the +-- string stack. instructionStringSubString :: State -> State instructionStringSubString = instructionSubVector string diff --git a/src/HushGP/Instructions/VectorBoolInstructions.hs b/src/HushGP/Instructions/VectorBoolInstructions.hs index a69d00d..4294885 100644 --- a/src/HushGP/Instructions/VectorBoolInstructions.hs +++ b/src/HushGP/Instructions/VectorBoolInstructions.hs @@ -4,13 +4,13 @@ import HushGP.State import HushGP.Instructions.GenericInstructions instructionVectorBoolConcat :: State -> State -instructionVectorBoolConcat = instructionConcat vectorBool +instructionVectorBoolConcat = instructionVectorConcat vectorBool instructionVectorBoolConj :: State -> State -instructionVectorBoolConj = instructionConj bool vectorBool +instructionVectorBoolConj = instructionVectorConj bool vectorBool instructionVectorBoolTakeN :: State -> State -instructionVectorBoolTakeN = instructionTakeN vectorBool +instructionVectorBoolTakeN = instructionVectorTakeN vectorBool instructionVectorBoolSubVector :: State -> State instructionVectorBoolSubVector = instructionSubVector vectorBool @@ -25,10 +25,10 @@ instructionVectorBoolNth :: State -> State instructionVectorBoolNth = instructionVectorNth bool vectorBool instructionVectorBoolRest :: State -> State -instructionVectorBoolRest = instructionRest vectorBool +instructionVectorBoolRest = instructionVectorRest vectorBool instructionVectorBoolButLast :: State -> State -instructionVectorBoolButLast = instructionButLast vectorBool +instructionVectorBoolButLast = instructionVectorButLast vectorBool instructionVectorBoolLength :: State -> State instructionVectorBoolLength = instructionLength vectorBool @@ -61,7 +61,7 @@ instructionVectorBoolReplaceFirst :: State -> State instructionVectorBoolReplaceFirst = instructionVectorReplace bool vectorBool (Just 1) instructionVectorBoolRemove :: State -> State -instructionVectorBoolRemove = instructionVectorRemove bool vectorBool +instructionVectorBoolRemove = instructionVectorRemove bool vectorBool Nothing instructionVectorBoolIterate :: State -> State instructionVectorBoolIterate = instructionVectorIterate bool vectorBool GeneVectorBool instructionVectorBoolIterate "instructionVectorBoolIterate" diff --git a/src/HushGP/Instructions/VectorCharInstructions.hs b/src/HushGP/Instructions/VectorCharInstructions.hs index c7a4fe7..d5e0d29 100644 --- a/src/HushGP/Instructions/VectorCharInstructions.hs +++ b/src/HushGP/Instructions/VectorCharInstructions.hs @@ -4,13 +4,13 @@ import HushGP.State import HushGP.Instructions.GenericInstructions instructionVectorCharConcat :: State -> State -instructionVectorCharConcat = instructionConcat vectorChar +instructionVectorCharConcat = instructionVectorConcat vectorChar instructionVectorCharConj :: State -> State -instructionVectorCharConj = instructionConj char vectorChar +instructionVectorCharConj = instructionVectorConj char vectorChar instructionVectorCharTakeN :: State -> State -instructionVectorCharTakeN = instructionTakeN vectorChar +instructionVectorCharTakeN = instructionVectorTakeN vectorChar instructionVectorCharSubVector :: State -> State instructionVectorCharSubVector = instructionSubVector vectorChar @@ -25,10 +25,10 @@ instructionVectorCharNth :: State -> State instructionVectorCharNth = instructionVectorNth char vectorChar instructionVectorCharRest :: State -> State -instructionVectorCharRest = instructionRest vectorChar +instructionVectorCharRest = instructionVectorRest vectorChar instructionVectorCharButLast :: State -> State -instructionVectorCharButLast = instructionButLast vectorChar +instructionVectorCharButLast = instructionVectorButLast vectorChar instructionVectorCharLength :: State -> State instructionVectorCharLength = instructionLength vectorChar @@ -61,7 +61,7 @@ instructionVectorCharReplaceFirst :: State -> State instructionVectorCharReplaceFirst = instructionVectorReplace char vectorChar (Just 1) instructionVectorCharRemove :: State -> State -instructionVectorCharRemove = instructionVectorRemove char vectorChar +instructionVectorCharRemove = instructionVectorRemove char vectorChar Nothing instructionVectorCharIterate :: State -> State instructionVectorCharIterate = instructionVectorIterate char vectorChar GeneVectorChar instructionVectorCharIterate "instructionVectorCharIterate" diff --git a/src/HushGP/Instructions/VectorFloatInstructions.hs b/src/HushGP/Instructions/VectorFloatInstructions.hs index b972685..05a7848 100644 --- a/src/HushGP/Instructions/VectorFloatInstructions.hs +++ b/src/HushGP/Instructions/VectorFloatInstructions.hs @@ -4,13 +4,13 @@ import HushGP.State import HushGP.Instructions.GenericInstructions instructionVectorFloatConcat :: State -> State -instructionVectorFloatConcat = instructionConcat vectorFloat +instructionVectorFloatConcat = instructionVectorConcat vectorFloat instructionVectorFloatConj :: State -> State -instructionVectorFloatConj = instructionConj float vectorFloat +instructionVectorFloatConj = instructionVectorConj float vectorFloat instructionVectorFloatTakeN :: State -> State -instructionVectorFloatTakeN = instructionTakeN vectorFloat +instructionVectorFloatTakeN = instructionVectorTakeN vectorFloat instructionVectorFloatSubVector :: State -> State instructionVectorFloatSubVector = instructionSubVector vectorFloat @@ -25,10 +25,10 @@ instructionVectorFloatNth :: State -> State instructionVectorFloatNth = instructionVectorNth float vectorFloat instructionVectorFloatRest :: State -> State -instructionVectorFloatRest = instructionRest vectorFloat +instructionVectorFloatRest = instructionVectorRest vectorFloat instructionVectorFloatButLast :: State -> State -instructionVectorFloatButLast = instructionButLast vectorFloat +instructionVectorFloatButLast = instructionVectorButLast vectorFloat instructionVectorFloatLength :: State -> State instructionVectorFloatLength = instructionLength vectorFloat @@ -61,7 +61,7 @@ instructionVectorFloatReplaceFirst :: State -> State instructionVectorFloatReplaceFirst = instructionVectorReplace float vectorFloat (Just 1) instructionVectorFloatRemove :: State -> State -instructionVectorFloatRemove = instructionVectorRemove float vectorFloat +instructionVectorFloatRemove = instructionVectorRemove float vectorFloat Nothing instructionVectorFloatIterate :: State -> State instructionVectorFloatIterate = instructionVectorIterate float vectorFloat GeneVectorFloat instructionVectorFloatIterate "instructionVectorFloatIterate" diff --git a/src/HushGP/Instructions/VectorIntInstructions.hs b/src/HushGP/Instructions/VectorIntInstructions.hs index 1eca9ac..04c7b5e 100644 --- a/src/HushGP/Instructions/VectorIntInstructions.hs +++ b/src/HushGP/Instructions/VectorIntInstructions.hs @@ -4,13 +4,13 @@ import HushGP.Instructions.GenericInstructions import HushGP.State instructionVectorIntConcat :: State -> State -instructionVectorIntConcat = instructionConcat vectorInt +instructionVectorIntConcat = instructionVectorConcat vectorInt instructionVectorIntConj :: State -> State -instructionVectorIntConj = instructionConj int vectorInt +instructionVectorIntConj = instructionVectorConj int vectorInt instructionVectorIntTakeN :: State -> State -instructionVectorIntTakeN = instructionTakeN vectorInt +instructionVectorIntTakeN = instructionVectorTakeN vectorInt instructionVectorIntSubVector :: State -> State instructionVectorIntSubVector = instructionSubVector vectorInt @@ -25,10 +25,10 @@ instructionVectorIntNth :: State -> State instructionVectorIntNth = instructionVectorNth int vectorInt instructionVectorIntRest :: State -> State -instructionVectorIntRest = instructionRest vectorInt +instructionVectorIntRest = instructionVectorRest vectorInt instructionVectorIntButLast :: State -> State -instructionVectorIntButLast = instructionButLast vectorInt +instructionVectorIntButLast = instructionVectorButLast vectorInt instructionVectorIntLength :: State -> State instructionVectorIntLength = instructionLength vectorInt @@ -61,7 +61,7 @@ instructionVectorIntReplaceFirst :: State -> State instructionVectorIntReplaceFirst = instructionVectorReplace int vectorInt (Just 1) instructionVectorIntRemove :: State -> State -instructionVectorIntRemove = instructionVectorRemove int vectorInt +instructionVectorIntRemove = instructionVectorRemove int vectorInt Nothing instructionVectorIntIterate :: State -> State instructionVectorIntIterate = instructionVectorIterate int vectorInt GeneVectorInt instructionVectorIntIterate "instructionVectorIntIterate" diff --git a/src/HushGP/Instructions/VectorStringInstructions.hs b/src/HushGP/Instructions/VectorStringInstructions.hs index 9e0348c..64966f9 100644 --- a/src/HushGP/Instructions/VectorStringInstructions.hs +++ b/src/HushGP/Instructions/VectorStringInstructions.hs @@ -4,13 +4,13 @@ import HushGP.State import HushGP.Instructions.GenericInstructions instructionVectorStringConcat :: State -> State -instructionVectorStringConcat = instructionConcat vectorString +instructionVectorStringConcat = instructionVectorConcat vectorString instructionVectorStringConj :: State -> State -instructionVectorStringConj = instructionConj string vectorString +instructionVectorStringConj = instructionVectorConj string vectorString instructionVectorStringTakeN :: State -> State -instructionVectorStringTakeN = instructionTakeN vectorString +instructionVectorStringTakeN = instructionVectorTakeN vectorString instructionVectorStringSubVector :: State -> State instructionVectorStringSubVector = instructionSubVector vectorString @@ -25,10 +25,10 @@ instructionVectorStringNth :: State -> State instructionVectorStringNth = instructionVectorNth string vectorString instructionVectorStringRest :: State -> State -instructionVectorStringRest = instructionRest vectorString +instructionVectorStringRest = instructionVectorRest vectorString instructionVectorStringButLast :: State -> State -instructionVectorStringButLast = instructionButLast vectorString +instructionVectorStringButLast = instructionVectorButLast vectorString instructionVectorStringLength :: State -> State instructionVectorStringLength = instructionLength vectorString @@ -61,7 +61,7 @@ instructionVectorStringReplaceFirst :: State -> State instructionVectorStringReplaceFirst = instructionVectorReplace string vectorString (Just 1) instructionVectorStringRemove :: State -> State -instructionVectorStringRemove = instructionVectorRemove string vectorString +instructionVectorStringRemove = instructionVectorRemove string vectorString Nothing instructionVectorStringIterate :: State -> State instructionVectorStringIterate = instructionVectorIterate string vectorString GeneVectorString instructionVectorStringIterate "instructionVectorStringIterate" From 7bb825991c63414d0e7b5f8a66e7fac6a965cf24 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Mon, 10 Feb 2025 16:10:53 -0600 Subject: [PATCH 139/171] fix docs/more string functions --- src/HushGP/Instructions/CharInstructions.hs | 7 +++++- .../Instructions/GenericInstructions.hs | 14 +++++++++-- src/HushGP/Instructions/StringInstructions.hs | 25 +++++-------------- 3 files changed, 24 insertions(+), 22 deletions(-) diff --git a/src/HushGP/Instructions/CharInstructions.hs b/src/HushGP/Instructions/CharInstructions.hs index 20f81c8..902dc46 100644 --- a/src/HushGP/Instructions/CharInstructions.hs +++ b/src/HushGP/Instructions/CharInstructions.hs @@ -64,7 +64,7 @@ instructionCharFromAsciiFloat state = state -- |Pushes the top string to the char stack split up into individual chars. -- For example: have the string "hello" and the char stack ['a', 'b', 'c'], the char stack --- looks like ['h', 'e', 'l', 'l', 'o', 'a', 'b', 'c']. +-- looks like ['h', 'e', 'l', 'l', 'o', 'a', 'b', 'c'] after this instruction executes. instructionCharsFromString :: State -> State instructionCharsFromString state@(State {_char = cs, _string = s1 : ss}) = state{_char = s1 <> cs, _string = ss} instructionCharsFromString state = state @@ -131,3 +131,8 @@ instructionCharShoveDup = instructionShoveDup char -- |Duplicate the top N items from the char stack based on the top int from the int stack. instructionCharDupItems :: State -> State instructionCharDupItems = instructionDupItems char + +-- |Takes the top string from the string stack and invidually pushes +-- all chars in said string to the char stack. +instructionCharFromAllString :: State -> State +instructionCharFromAllString = instructionPushAll char string diff --git a/src/HushGP/Instructions/GenericInstructions.hs b/src/HushGP/Instructions/GenericInstructions.hs index ad35a7a..e7e25af 100644 --- a/src/HushGP/Instructions/GenericInstructions.hs +++ b/src/HushGP/Instructions/GenericInstructions.hs @@ -297,6 +297,7 @@ instructionVectorFirst primAccessor vectorAccessor state = -- |Based on a vector lens, takes the first item from the top vector on the vector stack -- and creates a vector wrapping that first item, pushing it back onto the stack. +-- Not to be confused with instructionVectorFromFirstPrim. instructionVectorFromFirstPrim :: Lens' State [[a]] -> State -> State instructionVectorFromFirstPrim accessor state = case uncons (view accessor state) of @@ -306,6 +307,16 @@ instructionVectorFromFirstPrim accessor state = _ -> state _ -> state +-- |Based on two lenses, one of a primitive type and the next of a vector type, +-- pushes the top item of the primitive stack wrapped in a list to the top of the +-- vector stack. Not to be confused with instructionVectorFromFirstPrim. +instructionVectorFromPrim :: Lens' State [a] -> Lens' State [[a]] -> State -> State +instructionVectorFromPrim primAccessor vectorAccessor state = + case uncons (view primAccessor state) of + Just (p1, ps) -> state & primAccessor .~ ps & vectorAccessor .~ ([p1] : view vectorAccessor state) + _ -> state + + -- |Based on two lenses, one of a primitive type and the next of a vector type, -- Takes the last item from the top vector and places it onto the passed primitive stack. instructionVectorLast :: Lens' State [a] -> Lens' State [[a]] -> State -> State @@ -587,8 +598,7 @@ instructionVectorRemoveVectorN accessor state@(State {_int = i1 : is}) = instruc instructionVectorRemoveVectorN _ state = state -- |Based on two lenses, one of a primitive type and the next of a vector type, --- removes the first occurrence inside of the top vector from the vector stack where the top --- item from the primitive stack equals a primitive inside of the vector stack. +-- iterates over the top vector from the vector stack using the top code from the code stack. instructionVectorIterate :: Lens' State [a] -> Lens' State [[a]] -> ([a] -> Gene) -> (State -> State) -> String -> State -> State instructionVectorIterate primAccessor vectorAccessor vectorType typeIterateFunction typeIterateFunctionName state@(State {_exec = e1 : es}) = case uncons (view vectorAccessor state) of diff --git a/src/HushGP/Instructions/StringInstructions.hs b/src/HushGP/Instructions/StringInstructions.hs index 1804b4d..5f22327 100644 --- a/src/HushGP/Instructions/StringInstructions.hs +++ b/src/HushGP/Instructions/StringInstructions.hs @@ -38,55 +38,38 @@ instructionStringSwap = instructionSwap string -- on the string stack based on an int from the int stack. instructionStringInsertString :: State -> State instructionStringInsertString = instructionVectorInsertVector string --- instructionStringInsertString state@(State{_string = s1 : s2 : ss, _int = i1 : is}) = state {_string = combineTupleList s2 (splitAt i1 s1) : ss, _int = is} --- instructionStringInsertString state = state -- |Takes the first string from the string stack and pushes the first character -- back to the string stack as a string. instructionStringFromFirstChar :: State -> State instructionStringFromFirstChar = instructionVectorFromFirstPrim string --- instructionStringFromFirstChar state@(State {_string = (schar : _) : ss}) = state {_string = [schar] : ss} --- instructionStringFromFirstChar state = state -- |Takes the first string from the string stack and pushes the last character -- back to the string stack as a string. instructionStringFromLastChar :: State -> State instructionStringFromLastChar = instructionVectorFromLastPrim string --- instructionStringFromLastChar state@(State {_string = s1 : ss}) = - -- if not $ null s1 - -- then state {_string = [last s1] : ss} - -- else state --- instructionStringFromLastChar state = state -- |Takes the first string from the string stack and pushes the Nth character -- back to the string stack as a string. N in is the top int of the int stack. instructionStringFromNthChar :: State -> State instructionStringFromNthChar = instructionVectorFromNthPrim string --- instructionStringFromNthChar state@(State {_string = s1 : ss, _int = i1 : is}) = state{_string = [s1 !! absNum i1 s1] : ss, _int = is} --- instructionStringFromNthChar state = state -- |Takes the first two strings from the top of the string stack. Looks for and pushed the -- index of the second substring inside of the first substring to the int stack. -- If not found, returns -1. instructionStringIndexOfString :: State -> State instructionStringIndexOfString = instructionVectorIndexOfVector string --- instructionStringIndexOfString state@(State {_string = s1 : s2 : ss, _int = is}) = state {_string = ss, _int = findSubA s1 s2 : is} --- instructionStringIndexOfString state = state -- |Takes the first two strings from the top of the string stack. Pushes True to the -- bool stack if the second string is contained within the first string. Pushes False otherwise. instructionStringContainsString :: State -> State instructionStringContainsString = instructionVectorContainsVector string --- instructionStringContainsString state@(State {_string = s1 : s2 : ss, _bool = bs}) = state {_string = ss, _bool = (findSubA s1 s2 /= -1) : bs} --- instructionStringContainsString state = state -- |Takes the first two strings from the top of the string stack. Splits the first string -- based on the second string and pushes the result to the string stack. -- pysh reverses this. Check this for propeller instructionStringSplitOnString :: State -> State instructionStringSplitOnString = instructionVectorSplitOnVector string --- instructionStringSplitOnString state@(State {_string = s1 : s2 : ss}) = state {_string = reverse $ splitOn s2 s1 <> ss} --- instructionStringSplitOnString state = state -- |Takes the first three strings from the top of the string stack. Replaces the first instance of -- the second string within the first string with the third string. Pushes the result to the string stack. @@ -285,8 +268,7 @@ instructionStringFromFloat = instructionStringFromLens float -- |Converts the top char from the char stack to a string. Pushes the result to -- the string stack. instructionStringFromChar :: State -> State -instructionStringFromChar state@(State {_string = ss, _char = c1 : cs}) = state{_string = [c1] : ss, _char = cs} -instructionStringFromChar state = state +instructionStringFromChar = instructionVectorFromPrim char string -- |Removes the top string from the string stack. instructionStringPop :: State -> State @@ -366,3 +348,8 @@ instructionStringParseToChar = instructionVectorParseToPrim string -- string stack. instructionStringSubString :: State -> State instructionStringSubString = instructionSubVector string + +-- |Iterates over the top string on the string stack, applying the top instruction of the +-- exec stack along the way. +instructionStringIterate :: State -> State +instructionStringIterate = instructionVectorIterate char string GeneString instructionStringIterate "instructionStringIterate" From a4c04711b20f7df1dfe2b028ad345df339d0ab12 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Mon, 10 Feb 2025 16:58:40 -0600 Subject: [PATCH 140/171] vectorBool docs/add generic vector instructions --- .../Instructions/VectorBoolInstructions.hs | 219 ++++++++++++------ 1 file changed, 154 insertions(+), 65 deletions(-) diff --git a/src/HushGP/Instructions/VectorBoolInstructions.hs b/src/HushGP/Instructions/VectorBoolInstructions.hs index 4294885..d6542c0 100644 --- a/src/HushGP/Instructions/VectorBoolInstructions.hs +++ b/src/HushGP/Instructions/VectorBoolInstructions.hs @@ -3,90 +3,38 @@ module HushGP.Instructions.VectorBoolInstructions where import HushGP.State import HushGP.Instructions.GenericInstructions -instructionVectorBoolConcat :: State -> State -instructionVectorBoolConcat = instructionVectorConcat vectorBool - -instructionVectorBoolConj :: State -> State -instructionVectorBoolConj = instructionVectorConj bool vectorBool - -instructionVectorBoolTakeN :: State -> State -instructionVectorBoolTakeN = instructionVectorTakeN vectorBool - -instructionVectorBoolSubVector :: State -> State -instructionVectorBoolSubVector = instructionSubVector vectorBool - -instructionVectorBoolFirst :: State -> State -instructionVectorBoolFirst = instructionVectorFirst bool vectorBool - -instructionVectorBoolLast :: State -> State -instructionVectorBoolLast = instructionVectorLast bool vectorBool - -instructionVectorBoolNth :: State -> State -instructionVectorBoolNth = instructionVectorNth bool vectorBool - -instructionVectorBoolRest :: State -> State -instructionVectorBoolRest = instructionVectorRest vectorBool - -instructionVectorBoolButLast :: State -> State -instructionVectorBoolButLast = instructionVectorButLast vectorBool - -instructionVectorBoolLength :: State -> State -instructionVectorBoolLength = instructionLength vectorBool - -instructionVectorBoolReverse :: State -> State -instructionVectorBoolReverse = instructionReverse vectorBool - -instructionVectorBoolPushAll :: State -> State -instructionVectorBoolPushAll = instructionPushAll bool vectorBool - -instructionVectorBoolMakeEmpty :: State -> State -instructionVectorBoolMakeEmpty = instructionVectorMakeEmpty vectorBool - -instructionVectorBoolIsEmpty :: State -> State -instructionVectorBoolIsEmpty = instructionVectorIsEmpty vectorBool - -instructionVectorBoolIndexOf :: State -> State -instructionVectorBoolIndexOf = instructionVectorIndexOf bool vectorBool - -instructionVectorBoolOccurrencesOf :: State -> State -instructionVectorBoolOccurrencesOf = instructionVectorOccurrencesOf bool vectorBool - -instructionVectorBoolSetNth :: State -> State -instructionVectorBoolSetNth = instructionVectorSetNth bool vectorBool - -instructionVectorBoolReplace :: State -> State -instructionVectorBoolReplace = instructionVectorReplace bool vectorBool Nothing - -instructionVectorBoolReplaceFirst :: State -> State -instructionVectorBoolReplaceFirst = instructionVectorReplace bool vectorBool (Just 1) - -instructionVectorBoolRemove :: State -> State -instructionVectorBoolRemove = instructionVectorRemove bool vectorBool Nothing - -instructionVectorBoolIterate :: State -> State -instructionVectorBoolIterate = instructionVectorIterate bool vectorBool GeneVectorBool instructionVectorBoolIterate "instructionVectorBoolIterate" - +-- |Pops the top bool vector from the bool vector stack. instructionVectorBoolPop :: State -> State instructionVectorBoolPop = instructionPop vectorBool +-- |Duplicates the top bool vector from the bool vector stack. instructionVectorBoolDup :: State -> State instructionVectorBoolDup = instructionDup vectorBool +-- |Duplicates the top bool vector from the bool vector stack N times +-- based on the top int from the int stack. instructionVectorBoolDupN :: State -> State instructionVectorBoolDupN = instructionDupN vectorBool +-- |Swaps the top two bool vectors from the bool vector stack. instructionVectorBoolSwap :: State -> State instructionVectorBoolSwap = instructionSwap vectorBool +-- |Rotates the top three bool vectors from the bool vector stack. instructionVectorBoolRot :: State -> State instructionVectorBoolRot = instructionRot vectorBool +-- |Sets the vector bool stack to [] instructionVectorBoolFlush :: State -> State instructionVectorBoolFlush = instructionFlush vectorBool +-- |Pushes True to the bool stack if the top two bool vectors from +-- the vector bool stack are equal. Pushes False otherwise. instructionVectorBoolEq :: State -> State instructionVectorBoolEq = instructionEq vectorBool +-- |Calculates the size of the vector bool stack and pushes that number +-- to the int stack. instructionVectorBoolStackDepth :: State -> State instructionVectorBoolStackDepth = instructionStackDepth vectorBool @@ -105,11 +53,152 @@ instructionVectorBoolShove = instructionShove vectorBool instructionVectorBoolShoveDup :: State -> State instructionVectorBoolShoveDup = instructionShoveDup vectorBool +instructionVectorBoolDupItems :: State -> State +instructionVectorBoolDupItems = instructionDupItems vectorBool + +instructionVectorBoolConcat :: State -> State +instructionVectorBoolConcat = instructionVectorConcat vectorBool + +instructionVectorBoolConj :: State -> State +instructionVectorBoolConj = instructionVectorConj bool vectorBool + +instructionVectorBoolConjEnd :: State -> State +instructionVectorBoolConjEnd = instructionVectorConjEnd bool vectorBool + +instructionVectorBoolTakeN :: State -> State +instructionVectorBoolTakeN = instructionVectorTakeN vectorBool + +instructionVectorBoolTakeRN :: State -> State +instructionVectorBoolTakeRN = instructionVectorTakeRN vectorBool + +instructionVectorBoolSubVector :: State -> State +instructionVectorBoolSubVector = instructionSubVector vectorBool + +instructionVectorBoolFirst :: State -> State +instructionVectorBoolFirst = instructionVectorFirst bool vectorBool + +instructionVectorBoolFromFirstPrim :: State -> State +instructionVectorBoolFromFirstPrim = instructionVectorFromFirstPrim vectorBool + +instructionVectorBoolFromPrim :: State -> State +instructionVectorBoolFromPrim = instructionVectorFromPrim bool vectorBool + +instructionVectorBoolLast :: State -> State +instructionVectorBoolLast = instructionVectorLast bool vectorBool + +instructionVectorBoolFromLastPrim :: State -> State +instructionVectorBoolFromLastPrim = instructionVectorFromLastPrim vectorBool + +instructionVectorBoolNth :: State -> State +instructionVectorBoolNth = instructionVectorNth bool vectorBool + +instructionVectorBoolFromNthPrim :: State -> State +instructionVectorBoolFromNthPrim = instructionVectorFromNthPrim vectorBool + +instructionVectorBoolRest :: State -> State +instructionVectorBoolRest = instructionVectorRest vectorBool + +instructionVectorBoolButLast :: State -> State +instructionVectorBoolButLast = instructionVectorButLast vectorBool + +instructionVectorBoolDrop :: State -> State +instructionVectorBoolDrop = instructionVectorDrop vectorBool + +instructionVectorBoolDropR :: State -> State +instructionVectorBoolDropR = instructionVectorDropR vectorBool + +instructionVectorBoolLength :: State -> State +instructionVectorBoolLength = instructionLength vectorBool + +instructionVectorBoolReverse :: State -> State +instructionVectorBoolReverse = instructionReverse vectorBool + +instructionVectorBoolPushAll :: State -> State +instructionVectorBoolPushAll = instructionPushAll bool vectorBool + +instructionVectorBoolMakeEmpty :: State -> State +instructionVectorBoolMakeEmpty = instructionVectorMakeEmpty vectorBool + +instructionVectorBoolIsEmpty :: State -> State +instructionVectorBoolIsEmpty = instructionVectorIsEmpty vectorBool + +instructionVectorBoolContains :: State -> State +instructionVectorBoolContains = instructionVectorContains bool vectorBool + +instructionVectorBoolContainsVectorBool :: State -> State +instructionVectorBoolContainsVectorBool = instructionVectorContainsVector vectorBool + +instructionVectorBoolIndexOf :: State -> State +instructionVectorBoolIndexOf = instructionVectorIndexOf bool vectorBool + +instructionVectorBoolIndexOfVectorBool :: State -> State +instructionVectorBoolIndexOfVectorBool = instructionVectorIndexOfVector vectorBool + +instructionVectorBoolOccurrencesOf :: State -> State +instructionVectorBoolOccurrencesOf = instructionVectorOccurrencesOf bool vectorBool + +instructionVectorBoolOccurrencesOfVectorBool :: State -> State +instructionVectorBoolOccurrencesOfVectorBool = instructionVectorOccurrencesOfVector vectorBool + +instructionVectorBoolParseToBool :: State -> State +instructionVectorBoolParseToBool = instructionVectorParseToPrim vectorBool + +instructionVectorBoolSetNth :: State -> State +instructionVectorBoolSetNth = instructionVectorSetNth bool vectorBool + +instructionVectorBoolSplitOn :: State -> State +instructionVectorBoolSplitOn = instructionVectorSplitOn bool vectorBool + +instructionVectorBoolSplitOnVectorBool :: State -> State +instructionVectorBoolSplitOnVectorBool = instructionVectorSplitOnVector vectorBool + +instructionVectorBoolReplaceFirst :: State -> State +instructionVectorBoolReplaceFirst = instructionVectorReplace bool vectorBool (Just 1) + +instructionVectorBoolReplaceAll :: State -> State +instructionVectorBoolReplaceAll = instructionVectorReplace bool vectorBool Nothing + +instructionVectorBoolReplaceN :: State -> State +instructionVectorBoolReplaceN = instructionVectorReplaceN bool vectorBool + +instructionVectorBoolReplaceFirstVectorBool :: State -> State +instructionVectorBoolReplaceFirstVectorBool = instructionVectorReplaceVector vectorBool (Just 1) + +instructionVectorBoolReplaceAllVectorBool :: State -> State +instructionVectorBoolReplaceAllVectorBool = instructionVectorReplaceVector vectorBool Nothing + +instructionVectorBoolReplaceVectorBoolN :: State -> State +instructionVectorBoolReplaceVectorBoolN = instructionVectorReplaceVectorN vectorBool + +instructionVectorBoolRemoveFirst :: State -> State +instructionVectorBoolRemoveFirst = instructionVectorRemove bool vectorBool (Just 1) + +instructionVectorBoolRemoveAll :: State -> State +instructionVectorBoolRemoveAll = instructionVectorRemove bool vectorBool Nothing + +instructionVectorBoolRemoveN :: State -> State +instructionVectorBoolRemoveN = instructionVectorRemoveN bool vectorBool + +instructionVectorBoolRemoveFirstVectorBool :: State -> State +instructionVectorBoolRemoveFirstVectorBool = instructionVectorRemoveVector vectorBool (Just 1) + +instructionVectorBoolRemoveAllVectorBool :: State -> State +instructionVectorBoolRemoveAllVectorBool = instructionVectorRemoveVector vectorBool Nothing + +instructionVectorBoolRemoveNVectorBool :: State -> State +instructionVectorBoolRemoveNVectorBool = instructionVectorRemoveVectorN vectorBool + +instructionVectorBoolIterate :: State -> State +instructionVectorBoolIterate = instructionVectorIterate bool vectorBool GeneVectorBool instructionVectorBoolIterate "instructionVectorBoolIterate" + instructionVectorBoolSort :: State -> State instructionVectorBoolSort = instructionVectorSort vectorBool instructionVectorBoolSortReverse :: State -> State instructionVectorBoolSortReverse = instructionVectorSortReverse vectorBool -instructionVectorBoolDupItems :: State -> State -instructionVectorBoolDupItems = instructionDupItems vectorBool +instructionVectorBoolInsert :: State -> State +instructionVectorBoolInsert = instructionVectorInsert bool vectorBool + +instructionVectorBoolInsertVectorBool :: State -> State +instructionVectorBoolInsertVectorBool = instructionVectorInsertVector vectorBool From 3dce0daf4ecea9590981ead953a01a92d5059e4e Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Mon, 10 Feb 2025 22:47:06 -0600 Subject: [PATCH 141/171] docs written for vector bools --- .../Instructions/GenericInstructions.hs | 3 +- .../Instructions/VectorBoolInstructions.hs | 126 ++++++++++++++++++ 2 files changed, 128 insertions(+), 1 deletion(-) diff --git a/src/HushGP/Instructions/GenericInstructions.hs b/src/HushGP/Instructions/GenericInstructions.hs index e7e25af..31b8b7e 100644 --- a/src/HushGP/Instructions/GenericInstructions.hs +++ b/src/HushGP/Instructions/GenericInstructions.hs @@ -483,7 +483,7 @@ instructionVectorOccurrencesOfVector accessor state@(State {_int = is}) = _ -> state -- |This function parses the primitives inside a vector type and pushes that vector split into --- lists of size one onto the respective vector stack. Based on a vector lens. +-- lists of size one and pushes the result onto the respective vector stack. Based on a vector lens. instructionVectorParseToPrim :: Lens' State [[a]] -> State -> State instructionVectorParseToPrim accessor state = case uncons (view accessor state) of @@ -599,6 +599,7 @@ instructionVectorRemoveVectorN _ state = state -- |Based on two lenses, one of a primitive type and the next of a vector type, -- iterates over the top vector from the vector stack using the top code from the code stack. +-- Pysh explains this better. instructionVectorIterate :: Lens' State [a] -> Lens' State [[a]] -> ([a] -> Gene) -> (State -> State) -> String -> State -> State instructionVectorIterate primAccessor vectorAccessor vectorType typeIterateFunction typeIterateFunctionName state@(State {_exec = e1 : es}) = case uncons (view vectorAccessor state) of diff --git a/src/HushGP/Instructions/VectorBoolInstructions.hs b/src/HushGP/Instructions/VectorBoolInstructions.hs index d6542c0..934495a 100644 --- a/src/HushGP/Instructions/VectorBoolInstructions.hs +++ b/src/HushGP/Instructions/VectorBoolInstructions.hs @@ -38,167 +38,293 @@ instructionVectorBoolEq = instructionEq vectorBool instructionVectorBoolStackDepth :: State -> State instructionVectorBoolStackDepth = instructionStackDepth vectorBool +-- |Moves an item from deep within the vector bool stack to the top of the vector bool stack based on +-- the top int from the int stack. instructionVectorBoolYank :: State -> State instructionVectorBoolYank = instructionYank vectorBool +-- |Copies an item from deep within the vector bool stack to the top of the vector bool stack based on +-- the top int from the int stack. instructionVectorBoolYankDup :: State -> State instructionVectorBoolYankDup = instructionYankDup vectorBool +-- |Pushes True to the bool stack if the vector bool stack is empty. False if not. instructionVectorBoolIsStackEmpty :: State -> State instructionVectorBoolIsStackEmpty = instructionIsStackEmpty vectorBool +-- |Moves an item from the top of the vector bool stack to deep within the vector bool stack based on +-- the top int from the int stack. instructionVectorBoolShove :: State -> State instructionVectorBoolShove = instructionShove vectorBool +-- |Copies an item from the top of the vector bool stack to deep within the vector bool stack based on +-- the top int from the int stack. instructionVectorBoolShoveDup :: State -> State instructionVectorBoolShoveDup = instructionShoveDup vectorBool +-- |Duplicate the top N items from the vector bool stack based on the top int from the int stack. instructionVectorBoolDupItems :: State -> State instructionVectorBoolDupItems = instructionDupItems vectorBool +-- |Concats the top two vectors on top of the vector bool stack. instructionVectorBoolConcat :: State -> State instructionVectorBoolConcat = instructionVectorConcat vectorBool +-- |Takes the top bool from the bool stack and prepends it to top bool vector +-- on the bool vector stack. instructionVectorBoolConj :: State -> State instructionVectorBoolConj = instructionVectorConj bool vectorBool +-- |Takes the top bool from the bool stack and appends it to top bool vector +-- on the bool vector stack. instructionVectorBoolConjEnd :: State -> State instructionVectorBoolConjEnd = instructionVectorConjEnd bool vectorBool +-- |Takes the first N bools from the top of the bool vector from the bool vector +-- and pushes the result to the bool vector stack. N is pulled from the top of +-- the int stack. instructionVectorBoolTakeN :: State -> State instructionVectorBoolTakeN = instructionVectorTakeN vectorBool +-- |Takes the last N bools from the top of the bool vector from the bool vector +-- and pushes the result to the bool vector stack. N is pulled from the top of +-- the int stack. instructionVectorBoolTakeRN :: State -> State instructionVectorBoolTakeRN = instructionVectorTakeRN vectorBool +-- |Takes a sublist of the top bool vector on top of the vector bool stack. +-- The two ints to determine bounds are pulled from the top of the int stack. instructionVectorBoolSubVector :: State -> State instructionVectorBoolSubVector = instructionSubVector vectorBool +-- |Takes the first bool from the top of the vector bool stack and places +-- it on the bool stack. instructionVectorBoolFirst :: State -> State instructionVectorBoolFirst = instructionVectorFirst bool vectorBool +-- |Takes the first bool from the top of the vector bool stack and places +-- it wrapped in a list on top of the vector bool stack. instructionVectorBoolFromFirstPrim :: State -> State instructionVectorBoolFromFirstPrim = instructionVectorFromFirstPrim vectorBool +-- |Takes the first bool from the top of the bool stack and places it +-- wrapped in a list on top of the vector bool stack. instructionVectorBoolFromPrim :: State -> State instructionVectorBoolFromPrim = instructionVectorFromPrim bool vectorBool +-- |Takes the last bool from the top of the vector bool stack and places +-- it on the bool stack. instructionVectorBoolLast :: State -> State instructionVectorBoolLast = instructionVectorLast bool vectorBool +-- |Takes the last bool from the top bool vector on the vector bool stack and +-- places it on the bool stack. instructionVectorBoolFromLastPrim :: State -> State instructionVectorBoolFromLastPrim = instructionVectorFromLastPrim vectorBool +-- |Takes the Nth bool from the top bool vector and places it onto the bool stack +-- based on an int from the top of the int stack. instructionVectorBoolNth :: State -> State instructionVectorBoolNth = instructionVectorNth bool vectorBool +-- |Takes the Nth bool from the top bool vector on the vector bool stack and +-- creates a vector wrapping that Nth item, pushing it back onto the vector bool stack. +-- N is the top item on the int stack. instructionVectorBoolFromNthPrim :: State -> State instructionVectorBoolFromNthPrim = instructionVectorFromNthPrim vectorBool +-- |Removes the first bool from the top bool vector on the vector bool stack and +-- places the result back onto the vector bool stack. instructionVectorBoolRest :: State -> State instructionVectorBoolRest = instructionVectorRest vectorBool +-- |Removes the last bool from the top bool vector on the vector bool stack and +-- places the result back onto the vector bool stack. instructionVectorBoolButLast :: State -> State instructionVectorBoolButLast = instructionVectorButLast vectorBool +-- |Drops the first N items from the top bool vector and pushes the result +-- back to the vector bool stack. N is pulled from the top of the int stack. instructionVectorBoolDrop :: State -> State instructionVectorBoolDrop = instructionVectorDrop vectorBool +-- |Drops the last N items from the top bool vector and pushes the result +-- back to the vector bool stack. N is pulled from the top of the int stack. instructionVectorBoolDropR :: State -> State instructionVectorBoolDropR = instructionVectorDropR vectorBool +-- |Pushes the length of the top bool vector from the vector bool stack +-- to the top of the int stack. instructionVectorBoolLength :: State -> State instructionVectorBoolLength = instructionLength vectorBool +-- |Reverses the top bool vector from the vector bool stack and pushes the +-- result to the vector bool stack. instructionVectorBoolReverse :: State -> State instructionVectorBoolReverse = instructionReverse vectorBool +-- |Takes the top bool vector from the vector bool stack and pushes the +-- individual bools to the vector bool stack. instructionVectorBoolPushAll :: State -> State instructionVectorBoolPushAll = instructionPushAll bool vectorBool +-- |Makes an empty vector and pushes it to the vector bool stack. instructionVectorBoolMakeEmpty :: State -> State instructionVectorBoolMakeEmpty = instructionVectorMakeEmpty vectorBool +-- |Checks if the top bool vector from the vector bool stack is empty. +-- Pushes True if the bool vector is empty to the bool stack. False otherwise. instructionVectorBoolIsEmpty :: State -> State instructionVectorBoolIsEmpty = instructionVectorIsEmpty vectorBool +-- |If the top bool vector from the vector bool stack contains the top bool from the bool +-- stack, pushes True to the bool stack and pushes False otherwise. instructionVectorBoolContains :: State -> State instructionVectorBoolContains = instructionVectorContains bool vectorBool +-- |If the second to top bool vector can be found within the first bool vector from the +-- vector bool stack, pushes True to the bool stack if is found, else False. instructionVectorBoolContainsVectorBool :: State -> State instructionVectorBoolContainsVectorBool = instructionVectorContainsVector vectorBool +-- |Finds the first index of the top bool in the bool stack inside of the +-- top bool vector from the vector bool stack and pushes the result to the int stack. instructionVectorBoolIndexOf :: State -> State instructionVectorBoolIndexOf = instructionVectorIndexOf bool vectorBool +-- |Searches and pushes the index of the second bool vector inside of the first +-- bool vector to the int stack from the vector bool stack. Pushes -1 if not found. instructionVectorBoolIndexOfVectorBool :: State -> State instructionVectorBoolIndexOfVectorBool = instructionVectorIndexOfVector vectorBool +-- |Finds the amount of times the top bool on the bool stack occurs inside of +-- the top bool vector from the vector bool stack and pushes the result to the +-- int stack. instructionVectorBoolOccurrencesOf :: State -> State instructionVectorBoolOccurrencesOf = instructionVectorOccurrencesOf bool vectorBool +-- |Counts the amount of occurrences of the second bool vector within the first +-- bool vector. Pushes the result to the int stack. instructionVectorBoolOccurrencesOfVectorBool :: State -> State instructionVectorBoolOccurrencesOfVectorBool = instructionVectorOccurrencesOfVector vectorBool +-- |Splits the top bool vector from the vector bool stack into lists of size one and pushes +-- the result back one the vector bool stack. instructionVectorBoolParseToBool :: State -> State instructionVectorBoolParseToBool = instructionVectorParseToPrim vectorBool +-- |Sets the Nth index inside of the top bool vector from the vector bool stack to the +-- top value from the primitive stack. N is pulled from the top of the int stack. instructionVectorBoolSetNth :: State -> State instructionVectorBoolSetNth = instructionVectorSetNth bool vectorBool +-- |Splits the bool vector on top of the vector bool stack with the bool from the top +-- of the bool stack and pushes the result to the original vector stack. instructionVectorBoolSplitOn :: State -> State instructionVectorBoolSplitOn = instructionVectorSplitOn bool vectorBool +-- |Splits the first bool vector based on the second bool vector from the vector +-- bool stack and pushes the result to the vector bool stack. instructionVectorBoolSplitOnVectorBool :: State -> State instructionVectorBoolSplitOnVectorBool = instructionVectorSplitOnVector vectorBool +-- |Replaces the first occurrence of the top bool with the second bool from +-- the bool stack inside of the top bool vector from the vector bool stack. +-- Pushes the modified bool vector to the vector bool stack. instructionVectorBoolReplaceFirst :: State -> State instructionVectorBoolReplaceFirst = instructionVectorReplace bool vectorBool (Just 1) +-- |Replaces all occurrences of the top bool with the second bool from +-- the bool stack inside of the top bool vector from the vector bool stack. +-- Pushes the modified bool vector to the vector bool stack. instructionVectorBoolReplaceAll :: State -> State instructionVectorBoolReplaceAll = instructionVectorReplace bool vectorBool Nothing +-- |Replaces N occurrences of the top bool with the second bool from +-- the bool stack inside of the top bool vector from the vector bool stack. +-- Pushes the modified bool vector to the vector bool stack. N is pulled from +-- the top of the int stack. instructionVectorBoolReplaceN :: State -> State instructionVectorBoolReplaceN = instructionVectorReplaceN bool vectorBool +-- |Replaces the first occurrence of the second bool vector with the third bool vector +-- inside of the first bool vector from the vector bool stack. Pushes the result to the +-- vector bool stack. instructionVectorBoolReplaceFirstVectorBool :: State -> State instructionVectorBoolReplaceFirstVectorBool = instructionVectorReplaceVector vectorBool (Just 1) +-- |Replaces all occurrences of the second bool vector with the third bool vector +-- inside of the first bool vector from the vector bool stack. Pushes the result to the +-- vector bool stack. instructionVectorBoolReplaceAllVectorBool :: State -> State instructionVectorBoolReplaceAllVectorBool = instructionVectorReplaceVector vectorBool Nothing +-- |Replaces N occurrences of the second bool vector with the third bool vector +-- inside of the first bool vector from the vector bool stack. Pushes the result to the +-- vector bool stack. N is pulled from the top of the int stack. instructionVectorBoolReplaceVectorBoolN :: State -> State instructionVectorBoolReplaceVectorBoolN = instructionVectorReplaceVectorN vectorBool +-- |Removes the first occurrence of the top bool from +-- the bool stack inside of the top bool vector from the vector bool stack. +-- Pushes the modified bool vector to the vector bool stack. instructionVectorBoolRemoveFirst :: State -> State instructionVectorBoolRemoveFirst = instructionVectorRemove bool vectorBool (Just 1) +-- |Removes the all occurrences of the top bool from +-- the bool stack inside of the top bool vector from the vector bool stack. +-- Pushes the modified bool vector to the vector bool stack. instructionVectorBoolRemoveAll :: State -> State instructionVectorBoolRemoveAll = instructionVectorRemove bool vectorBool Nothing +-- |Removes N occurrences of the top bool from +-- the bool stack inside of the top bool vector from the vector bool stack. +-- Pushes the modified bool vector to the vector bool stack. N is pulled +-- from the top of the int stack. instructionVectorBoolRemoveN :: State -> State instructionVectorBoolRemoveN = instructionVectorRemoveN bool vectorBool +-- |Removes the first occurrence of the second bool vector +-- inside of the first bool vector from the vector bool stack. Pushes the result to the +-- vector bool stack. instructionVectorBoolRemoveFirstVectorBool :: State -> State instructionVectorBoolRemoveFirstVectorBool = instructionVectorRemoveVector vectorBool (Just 1) +-- |Removes all occurrences of the second bool vector +-- inside of the first bool vector from the vector bool stack. Pushes the result to the +-- vector bool stack. instructionVectorBoolRemoveAllVectorBool :: State -> State instructionVectorBoolRemoveAllVectorBool = instructionVectorRemoveVector vectorBool Nothing +-- |Removes N occurrences of the second bool vector +-- inside of the first bool vector from the vector bool stack. Pushes the result to the +-- vector bool stack. N is pulled from the top of the int stack. instructionVectorBoolRemoveNVectorBool :: State -> State instructionVectorBoolRemoveNVectorBool = instructionVectorRemoveVectorN vectorBool +-- |Iterates over the top bool vector on the vector bool stack, applying the top instruction of the +-- exec stack along the way. instructionVectorBoolIterate :: State -> State instructionVectorBoolIterate = instructionVectorIterate bool vectorBool GeneVectorBool instructionVectorBoolIterate "instructionVectorBoolIterate" +-- |Sorts the top bool vector on the vector bool stack and pushes the result back to the +-- vector bool stack. instructionVectorBoolSort :: State -> State instructionVectorBoolSort = instructionVectorSort vectorBool +-- |Sorts the top bool vector on the vector bool stack, reverses it, and pushes the result back to the +-- vector bool stack. instructionVectorBoolSortReverse :: State -> State instructionVectorBoolSortReverse = instructionVectorSortReverse vectorBool +-- |Inserts the top bool from the bool stack into the top bool vector from the +-- vector bool stack at a specified index and pushes the result to the vector +-- bool stack. The index is pulled from the top of the int stack. instructionVectorBoolInsert :: State -> State instructionVectorBoolInsert = instructionVectorInsert bool vectorBool +-- |Inserts the second bool vector into the first bool vector from the vector bool stack +-- at a specified index and pushes the result to the vector bool stack. The index is +-- pulled from the top of the int stack. instructionVectorBoolInsertVectorBool :: State -> State instructionVectorBoolInsertVectorBool = instructionVectorInsertVector vectorBool From 7d6d8bf23dec9368824a2ab74420c8306ea602ab Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Mon, 10 Feb 2025 23:17:59 -0600 Subject: [PATCH 142/171] vector functions done, all documented --- TODO.md | 4 +- .../Instructions/VectorCharInstructions.hs | 345 ++++++++++++++---- .../Instructions/VectorFloatInstructions.hs | 345 ++++++++++++++---- .../Instructions/VectorIntInstructions.hs | 305 +++++++++++++--- .../Instructions/VectorStringInstructions.hs | 345 ++++++++++++++---- src/HushGP/Push.hs | 26 +- src/HushGP/State.hs | 3 +- 7 files changed, 1107 insertions(+), 266 deletions(-) diff --git a/TODO.md b/TODO.md index d5902e5..becfb29 100644 --- a/TODO.md +++ b/TODO.md @@ -2,7 +2,7 @@ ## Push Language TODO -- [ ] Make all vector functions applicable to string functions and vice versa +- [X] Make all vector functions applicable to string functions and vice versa - [X] Implement all functions as seen in propeller - [X] Implement all functions as seen in the specification - [ ] Implement Linear Algebra functions as specified in the previous papers @@ -10,7 +10,7 @@ - [X] Disambiguate isEmpty and stackIsEmpty - [X] Rename Logical to Bool - [X] Make int yank, shove, yankdup, and shovedup generic -- [ ] Write haddock documentation for each function +- [X] Write haddock documentation for each function - [X] Refactor all functions to take state as the final parameter - [X] Standardize the pattern matching parameter names, such as c1 : cs - [ ] Write unit/quickcheck tests for all of the instructions diff --git a/src/HushGP/Instructions/VectorCharInstructions.hs b/src/HushGP/Instructions/VectorCharInstructions.hs index d5e0d29..09084a3 100644 --- a/src/HushGP/Instructions/VectorCharInstructions.hs +++ b/src/HushGP/Instructions/VectorCharInstructions.hs @@ -3,113 +3,328 @@ module HushGP.Instructions.VectorCharInstructions where import HushGP.State import HushGP.Instructions.GenericInstructions -instructionVectorCharConcat :: State -> State -instructionVectorCharConcat = instructionVectorConcat vectorChar - -instructionVectorCharConj :: State -> State -instructionVectorCharConj = instructionVectorConj char vectorChar - -instructionVectorCharTakeN :: State -> State -instructionVectorCharTakeN = instructionVectorTakeN vectorChar - -instructionVectorCharSubVector :: State -> State -instructionVectorCharSubVector = instructionSubVector vectorChar - -instructionVectorCharFirst :: State -> State -instructionVectorCharFirst = instructionVectorFirst char vectorChar - -instructionVectorCharLast :: State -> State -instructionVectorCharLast = instructionVectorLast char vectorChar - -instructionVectorCharNth :: State -> State -instructionVectorCharNth = instructionVectorNth char vectorChar - -instructionVectorCharRest :: State -> State -instructionVectorCharRest = instructionVectorRest vectorChar - -instructionVectorCharButLast :: State -> State -instructionVectorCharButLast = instructionVectorButLast vectorChar - -instructionVectorCharLength :: State -> State -instructionVectorCharLength = instructionLength vectorChar - -instructionVectorCharReverse :: State -> State -instructionVectorCharReverse = instructionReverse vectorChar - -instructionVectorCharPushAll :: State -> State -instructionVectorCharPushAll = instructionPushAll char vectorChar - -instructionVectorCharMakeEmpty :: State -> State -instructionVectorCharMakeEmpty = instructionVectorMakeEmpty vectorChar - -instructionVectorCharIsEmpty :: State -> State -instructionVectorCharIsEmpty = instructionVectorIsEmpty vectorChar - -instructionVectorCharIndexOf :: State -> State -instructionVectorCharIndexOf = instructionVectorIndexOf char vectorChar - -instructionVectorCharOccurrencesOf :: State -> State -instructionVectorCharOccurrencesOf = instructionVectorOccurrencesOf char vectorChar - -instructionVectorCharSetNth :: State -> State -instructionVectorCharSetNth = instructionVectorSetNth char vectorChar - -instructionVectorCharReplace :: State -> State -instructionVectorCharReplace = instructionVectorReplace char vectorChar Nothing - -instructionVectorCharReplaceFirst :: State -> State -instructionVectorCharReplaceFirst = instructionVectorReplace char vectorChar (Just 1) - -instructionVectorCharRemove :: State -> State -instructionVectorCharRemove = instructionVectorRemove char vectorChar Nothing - -instructionVectorCharIterate :: State -> State -instructionVectorCharIterate = instructionVectorIterate char vectorChar GeneVectorChar instructionVectorCharIterate "instructionVectorCharIterate" - +-- |Pops the top char vector from the char vector stack. instructionVectorCharPop :: State -> State instructionVectorCharPop = instructionPop vectorChar +-- |Duplicates the top char vector from the char vector stack. instructionVectorCharDup :: State -> State instructionVectorCharDup = instructionDup vectorChar +-- |Duplicates the top char vector from the char vector stack N times +-- based on the top int from the int stack. instructionVectorCharDupN :: State -> State instructionVectorCharDupN = instructionDupN vectorChar +-- |Swaps the top two char vectors from the char vector stack. instructionVectorCharSwap :: State -> State instructionVectorCharSwap = instructionSwap vectorChar +-- |Rotates the top three char vectors from the char vector stack. instructionVectorCharRot :: State -> State instructionVectorCharRot = instructionRot vectorChar +-- |Sets the vector char stack to [] instructionVectorCharFlush :: State -> State instructionVectorCharFlush = instructionFlush vectorChar +-- |Pushes True to the bool stack if the top two char vectors from +-- the vector char stack are equal. Pushes False otherwise. instructionVectorCharEq :: State -> State instructionVectorCharEq = instructionEq vectorChar +-- |Calculates the size of the vector char stack and pushes that number +-- to the int stack. instructionVectorCharStackDepth :: State -> State instructionVectorCharStackDepth = instructionStackDepth vectorChar +-- |Moves an item from deep within the vector char stack to the top of the vector char stack based on +-- the top int from the int stack. instructionVectorCharYank :: State -> State instructionVectorCharYank = instructionYank vectorChar +-- |Copies an item from deep within the vector char stack to the top of the vector char stack based on +-- the top int from the int stack. instructionVectorCharYankDup :: State -> State instructionVectorCharYankDup = instructionYankDup vectorChar +-- |Pushes True to the bool stack if the vector char stack is empty. False if not. instructionVectorCharIsStackEmpty :: State -> State instructionVectorCharIsStackEmpty = instructionIsStackEmpty vectorChar +-- |Moves an item from the top of the vector char stack to deep within the vector char stack based on +-- the top int from the int stack. instructionVectorCharShove :: State -> State instructionVectorCharShove = instructionShove vectorChar +-- |Copies an item from the top of the vector char stack to deep within the vector char stack based on +-- the top int from the int stack. instructionVectorCharShoveDup :: State -> State instructionVectorCharShoveDup = instructionShoveDup vectorChar +-- |Duplicate the top N items from the vector char stack based on the top int from the int stack. +instructionVectorCharDupItems :: State -> State +instructionVectorCharDupItems = instructionDupItems vectorChar + +-- |Concats the top two vectors on top of the vector char stack. +instructionVectorCharConcat :: State -> State +instructionVectorCharConcat = instructionVectorConcat vectorChar + +-- |Takes the top char from the char stack and prepends it to top char vector +-- on the char vector stack. +instructionVectorCharConj :: State -> State +instructionVectorCharConj = instructionVectorConj char vectorChar + +-- |Takes the top char from the char stack and appends it to top char vector +-- on the char vector stack. +instructionVectorCharConjEnd :: State -> State +instructionVectorCharConjEnd = instructionVectorConjEnd char vectorChar + +-- |Takes the first N chars from the top of the char vector from the char vector +-- and pushes the result to the char vector stack. N is pulled from the top of +-- the int stack. +instructionVectorCharTakeN :: State -> State +instructionVectorCharTakeN = instructionVectorTakeN vectorChar + +-- |Takes the last N chars from the top of the char vector from the char vector +-- and pushes the result to the char vector stack. N is pulled from the top of +-- the int stack. +instructionVectorCharTakeRN :: State -> State +instructionVectorCharTakeRN = instructionVectorTakeRN vectorChar + +-- |Takes a sublist of the top char vector on top of the vector char stack. +-- The two ints to determine bounds are pulled from the top of the int stack. +instructionVectorCharSubVector :: State -> State +instructionVectorCharSubVector = instructionSubVector vectorChar + +-- |Takes the first char from the top of the vector char stack and places +-- it on the char stack. +instructionVectorCharFirst :: State -> State +instructionVectorCharFirst = instructionVectorFirst char vectorChar + +-- |Takes the first char from the top of the vector char stack and places +-- it wrapped in a list on top of the vector char stack. +instructionVectorCharFromFirstPrim :: State -> State +instructionVectorCharFromFirstPrim = instructionVectorFromFirstPrim vectorChar + +-- |Takes the first char from the top of the char stack and places it +-- wrapped in a list on top of the vector char stack. +instructionVectorCharFromPrim :: State -> State +instructionVectorCharFromPrim = instructionVectorFromPrim char vectorChar + +-- |Takes the last char from the top of the vector char stack and places +-- it on the char stack. +instructionVectorCharLast :: State -> State +instructionVectorCharLast = instructionVectorLast char vectorChar + +-- |Takes the last char from the top char vector on the vector char stack and +-- places it on the char stack. +instructionVectorCharFromLastPrim :: State -> State +instructionVectorCharFromLastPrim = instructionVectorFromLastPrim vectorChar + +-- |Takes the Nth char from the top char vector and places it onto the char stack +-- based on an int from the top of the int stack. +instructionVectorCharNth :: State -> State +instructionVectorCharNth = instructionVectorNth char vectorChar + +-- |Takes the Nth char from the top char vector on the vector char stack and +-- creates a vector wrapping that Nth item, pushing it back onto the vector char stack. +-- N is the top item on the int stack. +instructionVectorCharFromNthPrim :: State -> State +instructionVectorCharFromNthPrim = instructionVectorFromNthPrim vectorChar + +-- |Removes the first char from the top char vector on the vector char stack and +-- places the result back onto the vector char stack. +instructionVectorCharRest :: State -> State +instructionVectorCharRest = instructionVectorRest vectorChar + +-- |Removes the last char from the top char vector on the vector char stack and +-- places the result back onto the vector char stack. +instructionVectorCharButLast :: State -> State +instructionVectorCharButLast = instructionVectorButLast vectorChar + +-- |Drops the first N items from the top char vector and pushes the result +-- back to the vector char stack. N is pulled from the top of the int stack. +instructionVectorCharDrop :: State -> State +instructionVectorCharDrop = instructionVectorDrop vectorChar + +-- |Drops the last N items from the top char vector and pushes the result +-- back to the vector char stack. N is pulled from the top of the int stack. +instructionVectorCharDropR :: State -> State +instructionVectorCharDropR = instructionVectorDropR vectorChar + +-- |Pushes the length of the top char vector from the vector char stack +-- to the top of the int stack. +instructionVectorCharLength :: State -> State +instructionVectorCharLength = instructionLength vectorChar + +-- |Reverses the top char vector from the vector char stack and pushes the +-- result to the vector char stack. +instructionVectorCharReverse :: State -> State +instructionVectorCharReverse = instructionReverse vectorChar + +-- |Takes the top char vector from the vector char stack and pushes the +-- individual chars to the vector char stack. +instructionVectorCharPushAll :: State -> State +instructionVectorCharPushAll = instructionPushAll char vectorChar + +-- |Makes an empty vector and pushes it to the vector char stack. +instructionVectorCharMakeEmpty :: State -> State +instructionVectorCharMakeEmpty = instructionVectorMakeEmpty vectorChar + +-- |Checks if the top char vector from the vector char stack is empty. +-- Pushes True if the char vector is empty to the bool stack. False otherwise. +instructionVectorCharIsEmpty :: State -> State +instructionVectorCharIsEmpty = instructionVectorIsEmpty vectorChar + +-- |If the top char vector from the vector char stack contains the top char from the char +-- stack, pushes True to the bool stack and pushes False otherwise. +instructionVectorCharContains :: State -> State +instructionVectorCharContains = instructionVectorContains char vectorChar + +-- |If the second to top char vector can be found within the first char vector from the +-- vector char stack, pushes True to the bool stack if is found, else False. +instructionVectorCharContainsVectorChar :: State -> State +instructionVectorCharContainsVectorChar = instructionVectorContainsVector vectorChar + +-- |Finds the first index of the top char in the char stack inside of the +-- top char vector from the vector char stack and pushes the result to the int stack. +instructionVectorCharIndexOf :: State -> State +instructionVectorCharIndexOf = instructionVectorIndexOf char vectorChar + +-- |Searches and pushes the index of the second char vector inside of the first +-- char vector to the int stack from the vector char stack. Pushes -1 if not found. +instructionVectorCharIndexOfVectorChar :: State -> State +instructionVectorCharIndexOfVectorChar = instructionVectorIndexOfVector vectorChar + +-- |Finds the amount of times the top char on the char stack occurs inside of +-- the top char vector from the vector char stack and pushes the result to the +-- int stack. +instructionVectorCharOccurrencesOf :: State -> State +instructionVectorCharOccurrencesOf = instructionVectorOccurrencesOf char vectorChar + +-- |Counts the amount of occurrences of the second char vector within the first +-- char vector. Pushes the result to the int stack. +instructionVectorCharOccurrencesOfVectorChar :: State -> State +instructionVectorCharOccurrencesOfVectorChar = instructionVectorOccurrencesOfVector vectorChar + +-- |Splits the top char vector from the vector char stack into lists of size one and pushes +-- the result back one the vector char stack. +instructionVectorCharParseToChar :: State -> State +instructionVectorCharParseToChar = instructionVectorParseToPrim vectorChar + +-- |Sets the Nth index inside of the top char vector from the vector char stack to the +-- top value from the primitive stack. N is pulled from the top of the int stack. +instructionVectorCharSetNth :: State -> State +instructionVectorCharSetNth = instructionVectorSetNth char vectorChar + +-- |Splits the char vector on top of the vector char stack with the char from the top +-- of the char stack and pushes the result to the original vector stack. +instructionVectorCharSplitOn :: State -> State +instructionVectorCharSplitOn = instructionVectorSplitOn char vectorChar + +-- |Splits the first char vector based on the second char vector from the vector +-- char stack and pushes the result to the vector char stack. +instructionVectorCharSplitOnVectorChar :: State -> State +instructionVectorCharSplitOnVectorChar = instructionVectorSplitOnVector vectorChar + +-- |Replaces the first occurrence of the top char with the second char from +-- the char stack inside of the top char vector from the vector char stack. +-- Pushes the modified char vector to the vector char stack. +instructionVectorCharReplaceFirst :: State -> State +instructionVectorCharReplaceFirst = instructionVectorReplace char vectorChar (Just 1) + +-- |Replaces all occurrences of the top char with the second char from +-- the char stack inside of the top char vector from the vector char stack. +-- Pushes the modified char vector to the vector char stack. +instructionVectorCharReplaceAll :: State -> State +instructionVectorCharReplaceAll = instructionVectorReplace char vectorChar Nothing + +-- |Replaces N occurrences of the top char with the second char from +-- the char stack inside of the top char vector from the vector char stack. +-- Pushes the modified char vector to the vector char stack. N is pulled from +-- the top of the int stack. +instructionVectorCharReplaceN :: State -> State +instructionVectorCharReplaceN = instructionVectorReplaceN char vectorChar + +-- |Replaces the first occurrence of the second char vector with the third char vector +-- inside of the first char vector from the vector char stack. Pushes the result to the +-- vector char stack. +instructionVectorCharReplaceFirstVectorChar :: State -> State +instructionVectorCharReplaceFirstVectorChar = instructionVectorReplaceVector vectorChar (Just 1) + +-- |Replaces all occurrences of the second char vector with the third char vector +-- inside of the first char vector from the vector char stack. Pushes the result to the +-- vector char stack. +instructionVectorCharReplaceAllVectorChar :: State -> State +instructionVectorCharReplaceAllVectorChar = instructionVectorReplaceVector vectorChar Nothing + +-- |Replaces N occurrences of the second char vector with the third char vector +-- inside of the first char vector from the vector char stack. Pushes the result to the +-- vector char stack. N is pulled from the top of the int stack. +instructionVectorCharReplaceVectorCharN :: State -> State +instructionVectorCharReplaceVectorCharN = instructionVectorReplaceVectorN vectorChar + +-- |Removes the first occurrence of the top char from +-- the char stack inside of the top char vector from the vector char stack. +-- Pushes the modified char vector to the vector char stack. +instructionVectorCharRemoveFirst :: State -> State +instructionVectorCharRemoveFirst = instructionVectorRemove char vectorChar (Just 1) + +-- |Removes the all occurrences of the top char from +-- the char stack inside of the top char vector from the vector char stack. +-- Pushes the modified char vector to the vector char stack. +instructionVectorCharRemoveAll :: State -> State +instructionVectorCharRemoveAll = instructionVectorRemove char vectorChar Nothing + +-- |Removes N occurrences of the top char from +-- the char stack inside of the top char vector from the vector char stack. +-- Pushes the modified char vector to the vector char stack. N is pulled +-- from the top of the int stack. +instructionVectorCharRemoveN :: State -> State +instructionVectorCharRemoveN = instructionVectorRemoveN char vectorChar + +-- |Removes the first occurrence of the second char vector +-- inside of the first char vector from the vector char stack. Pushes the result to the +-- vector char stack. +instructionVectorCharRemoveFirstVectorChar :: State -> State +instructionVectorCharRemoveFirstVectorChar = instructionVectorRemoveVector vectorChar (Just 1) + +-- |Removes all occurrences of the second char vector +-- inside of the first char vector from the vector char stack. Pushes the result to the +-- vector char stack. +instructionVectorCharRemoveAllVectorChar :: State -> State +instructionVectorCharRemoveAllVectorChar = instructionVectorRemoveVector vectorChar Nothing + +-- |Removes N occurrences of the second char vector +-- inside of the first char vector from the vector char stack. Pushes the result to the +-- vector char stack. N is pulled from the top of the int stack. +instructionVectorCharRemoveNVectorChar :: State -> State +instructionVectorCharRemoveNVectorChar = instructionVectorRemoveVectorN vectorChar + +-- |Iterates over the top char vector on the vector char stack, applying the top instruction of the +-- exec stack along the way. +instructionVectorCharIterate :: State -> State +instructionVectorCharIterate = instructionVectorIterate char vectorChar GeneVectorChar instructionVectorCharIterate "instructionVectorCharIterate" + +-- |Sorts the top char vector on the vector char stack and pushes the result back to the +-- vector char stack. instructionVectorCharSort :: State -> State instructionVectorCharSort = instructionVectorSort vectorChar +-- |Sorts the top char vector on the vector char stack, reverses it, and pushes the result back to the +-- vector char stack. instructionVectorCharSortReverse :: State -> State instructionVectorCharSortReverse = instructionVectorSortReverse vectorChar -instructionVectorCharDupItems :: State -> State -instructionVectorCharDupItems = instructionDupItems vectorChar +-- |Inserts the top char from the char stack into the top char vector from the +-- vector char stack at a specified index and pushes the result to the vector +-- char stack. The index is pulled from the top of the int stack. +instructionVectorCharInsert :: State -> State +instructionVectorCharInsert = instructionVectorInsert char vectorChar + +-- |Inserts the second char vector into the first char vector from the vector char stack +-- at a specified index and pushes the result to the vector char stack. The index is +-- pulled from the top of the int stack. +instructionVectorCharInsertVectorChar :: State -> State +instructionVectorCharInsertVectorChar = instructionVectorInsertVector vectorChar diff --git a/src/HushGP/Instructions/VectorFloatInstructions.hs b/src/HushGP/Instructions/VectorFloatInstructions.hs index 05a7848..a2a8531 100644 --- a/src/HushGP/Instructions/VectorFloatInstructions.hs +++ b/src/HushGP/Instructions/VectorFloatInstructions.hs @@ -3,113 +3,328 @@ module HushGP.Instructions.VectorFloatInstructions where import HushGP.State import HushGP.Instructions.GenericInstructions -instructionVectorFloatConcat :: State -> State -instructionVectorFloatConcat = instructionVectorConcat vectorFloat - -instructionVectorFloatConj :: State -> State -instructionVectorFloatConj = instructionVectorConj float vectorFloat - -instructionVectorFloatTakeN :: State -> State -instructionVectorFloatTakeN = instructionVectorTakeN vectorFloat - -instructionVectorFloatSubVector :: State -> State -instructionVectorFloatSubVector = instructionSubVector vectorFloat - -instructionVectorFloatFirst :: State -> State -instructionVectorFloatFirst = instructionVectorFirst float vectorFloat - -instructionVectorFloatLast :: State -> State -instructionVectorFloatLast = instructionVectorLast float vectorFloat - -instructionVectorFloatNth :: State -> State -instructionVectorFloatNth = instructionVectorNth float vectorFloat - -instructionVectorFloatRest :: State -> State -instructionVectorFloatRest = instructionVectorRest vectorFloat - -instructionVectorFloatButLast :: State -> State -instructionVectorFloatButLast = instructionVectorButLast vectorFloat - -instructionVectorFloatLength :: State -> State -instructionVectorFloatLength = instructionLength vectorFloat - -instructionVectorFloatReverse :: State -> State -instructionVectorFloatReverse = instructionReverse vectorFloat - -instructionVectorFloatPushAll :: State -> State -instructionVectorFloatPushAll = instructionPushAll float vectorFloat - -instructionVectorFloatMakeEmpty :: State -> State -instructionVectorFloatMakeEmpty = instructionVectorMakeEmpty vectorFloat - -instructionVectorFloatIsEmpty :: State -> State -instructionVectorFloatIsEmpty = instructionVectorIsEmpty vectorFloat - -instructionVectorFloatIndexOf :: State -> State -instructionVectorFloatIndexOf = instructionVectorIndexOf float vectorFloat - -instructionVectorFloatOccurrencesOf :: State -> State -instructionVectorFloatOccurrencesOf = instructionVectorOccurrencesOf float vectorFloat - -instructionVectorFloatSetNth :: State -> State -instructionVectorFloatSetNth = instructionVectorSetNth float vectorFloat - -instructionVectorFloatReplace :: State -> State -instructionVectorFloatReplace = instructionVectorReplace float vectorFloat Nothing - -instructionVectorFloatReplaceFirst :: State -> State -instructionVectorFloatReplaceFirst = instructionVectorReplace float vectorFloat (Just 1) - -instructionVectorFloatRemove :: State -> State -instructionVectorFloatRemove = instructionVectorRemove float vectorFloat Nothing - -instructionVectorFloatIterate :: State -> State -instructionVectorFloatIterate = instructionVectorIterate float vectorFloat GeneVectorFloat instructionVectorFloatIterate "instructionVectorFloatIterate" - +-- |Pops the top float vector from the float vector stack. instructionVectorFloatPop :: State -> State instructionVectorFloatPop = instructionPop vectorFloat +-- |Duplicates the top float vector from the float vector stack. instructionVectorFloatDup :: State -> State instructionVectorFloatDup = instructionDup vectorFloat +-- |Duplicates the top float vector from the float vector stack N times +-- based on the top int from the int stack. instructionVectorFloatDupN :: State -> State instructionVectorFloatDupN = instructionDupN vectorFloat +-- |Swaps the top two float vectors from the float vector stack. instructionVectorFloatSwap :: State -> State instructionVectorFloatSwap = instructionSwap vectorFloat +-- |Rotates the top three float vectors from the float vector stack. instructionVectorFloatRot :: State -> State instructionVectorFloatRot = instructionRot vectorFloat +-- |Sets the vector float stack to [] instructionVectorFloatFlush :: State -> State instructionVectorFloatFlush = instructionFlush vectorFloat +-- |Pushes True to the bool stack if the top two float vectors from +-- the vector float stack are equal. Pushes False otherwise. instructionVectorFloatEq :: State -> State instructionVectorFloatEq = instructionEq vectorFloat +-- |Calculates the size of the vector float stack and pushes that number +-- to the int stack. instructionVectorFloatStackDepth :: State -> State instructionVectorFloatStackDepth = instructionStackDepth vectorFloat +-- |Moves an item from deep within the vector float stack to the top of the vector float stack based on +-- the top int from the int stack. instructionVectorFloatYank :: State -> State instructionVectorFloatYank = instructionYank vectorFloat +-- |Copies an item from deep within the vector float stack to the top of the vector float stack based on +-- the top int from the int stack. instructionVectorFloatYankDup :: State -> State instructionVectorFloatYankDup = instructionYankDup vectorFloat +-- |Pushes True to the bool stack if the vector float stack is empty. False if not. instructionVectorFloatIsStackEmpty :: State -> State instructionVectorFloatIsStackEmpty = instructionIsStackEmpty vectorFloat +-- |Moves an item from the top of the vector float stack to deep within the vector float stack based on +-- the top int from the int stack. instructionVectorFloatShove :: State -> State instructionVectorFloatShove = instructionShove vectorFloat +-- |Copies an item from the top of the vector float stack to deep within the vector float stack based on +-- the top int from the int stack. instructionVectorFloatShoveDup :: State -> State instructionVectorFloatShoveDup = instructionShoveDup vectorFloat +-- |Duplicate the top N items from the vector float stack based on the top int from the int stack. +instructionVectorFloatDupItems :: State -> State +instructionVectorFloatDupItems = instructionDupItems vectorFloat + +-- |Concats the top two vectors on top of the vector float stack. +instructionVectorFloatConcat :: State -> State +instructionVectorFloatConcat = instructionVectorConcat vectorFloat + +-- |Takes the top float from the float stack and prepends it to top float vector +-- on the float vector stack. +instructionVectorFloatConj :: State -> State +instructionVectorFloatConj = instructionVectorConj float vectorFloat + +-- |Takes the top float from the float stack and appends it to top float vector +-- on the float vector stack. +instructionVectorFloatConjEnd :: State -> State +instructionVectorFloatConjEnd = instructionVectorConjEnd float vectorFloat + +-- |Takes the first N floats from the top of the float vector from the float vector +-- and pushes the result to the float vector stack. N is pulled from the top of +-- the int stack. +instructionVectorFloatTakeN :: State -> State +instructionVectorFloatTakeN = instructionVectorTakeN vectorFloat + +-- |Takes the last N floats from the top of the float vector from the float vector +-- and pushes the result to the float vector stack. N is pulled from the top of +-- the int stack. +instructionVectorFloatTakeRN :: State -> State +instructionVectorFloatTakeRN = instructionVectorTakeRN vectorFloat + +-- |Takes a sublist of the top float vector on top of the vector float stack. +-- The two ints to determine bounds are pulled from the top of the int stack. +instructionVectorFloatSubVector :: State -> State +instructionVectorFloatSubVector = instructionSubVector vectorFloat + +-- |Takes the first float from the top of the vector float stack and places +-- it on the float stack. +instructionVectorFloatFirst :: State -> State +instructionVectorFloatFirst = instructionVectorFirst float vectorFloat + +-- |Takes the first float from the top of the vector float stack and places +-- it wrapped in a list on top of the vector float stack. +instructionVectorFloatFromFirstPrim :: State -> State +instructionVectorFloatFromFirstPrim = instructionVectorFromFirstPrim vectorFloat + +-- |Takes the first float from the top of the float stack and places it +-- wrapped in a list on top of the vector float stack. +instructionVectorFloatFromPrim :: State -> State +instructionVectorFloatFromPrim = instructionVectorFromPrim float vectorFloat + +-- |Takes the last float from the top of the vector float stack and places +-- it on the float stack. +instructionVectorFloatLast :: State -> State +instructionVectorFloatLast = instructionVectorLast float vectorFloat + +-- |Takes the last float from the top float vector on the vector float stack and +-- places it on the float stack. +instructionVectorFloatFromLastPrim :: State -> State +instructionVectorFloatFromLastPrim = instructionVectorFromLastPrim vectorFloat + +-- |Takes the Nth float from the top float vector and places it onto the float stack +-- based on an int from the top of the int stack. +instructionVectorFloatNth :: State -> State +instructionVectorFloatNth = instructionVectorNth float vectorFloat + +-- |Takes the Nth float from the top float vector on the vector float stack and +-- creates a vector wrapping that Nth item, pushing it back onto the vector float stack. +-- N is the top item on the int stack. +instructionVectorFloatFromNthPrim :: State -> State +instructionVectorFloatFromNthPrim = instructionVectorFromNthPrim vectorFloat + +-- |Removes the first float from the top float vector on the vector float stack and +-- places the result back onto the vector float stack. +instructionVectorFloatRest :: State -> State +instructionVectorFloatRest = instructionVectorRest vectorFloat + +-- |Removes the last float from the top float vector on the vector float stack and +-- places the result back onto the vector float stack. +instructionVectorFloatButLast :: State -> State +instructionVectorFloatButLast = instructionVectorButLast vectorFloat + +-- |Drops the first N items from the top float vector and pushes the result +-- back to the vector float stack. N is pulled from the top of the int stack. +instructionVectorFloatDrop :: State -> State +instructionVectorFloatDrop = instructionVectorDrop vectorFloat + +-- |Drops the last N items from the top float vector and pushes the result +-- back to the vector float stack. N is pulled from the top of the int stack. +instructionVectorFloatDropR :: State -> State +instructionVectorFloatDropR = instructionVectorDropR vectorFloat + +-- |Pushes the length of the top float vector from the vector float stack +-- to the top of the int stack. +instructionVectorFloatLength :: State -> State +instructionVectorFloatLength = instructionLength vectorFloat + +-- |Reverses the top float vector from the vector float stack and pushes the +-- result to the vector float stack. +instructionVectorFloatReverse :: State -> State +instructionVectorFloatReverse = instructionReverse vectorFloat + +-- |Takes the top float vector from the vector float stack and pushes the +-- individual floats to the vector float stack. +instructionVectorFloatPushAll :: State -> State +instructionVectorFloatPushAll = instructionPushAll float vectorFloat + +-- |Makes an empty vector and pushes it to the vector float stack. +instructionVectorFloatMakeEmpty :: State -> State +instructionVectorFloatMakeEmpty = instructionVectorMakeEmpty vectorFloat + +-- |Checks if the top float vector from the vector float stack is empty. +-- Pushes True if the float vector is empty to the bool stack. False otherwise. +instructionVectorFloatIsEmpty :: State -> State +instructionVectorFloatIsEmpty = instructionVectorIsEmpty vectorFloat + +-- |If the top float vector from the vector float stack contains the top float from the float +-- stack, pushes True to the bool stack and pushes False otherwise. +instructionVectorFloatContains :: State -> State +instructionVectorFloatContains = instructionVectorContains float vectorFloat + +-- |If the second to top float vector can be found within the first float vector from the +-- vector float stack, pushes True to the bool stack if is found, else False. +instructionVectorFloatContainsVectorFloat :: State -> State +instructionVectorFloatContainsVectorFloat = instructionVectorContainsVector vectorFloat + +-- |Finds the first index of the top float in the float stack inside of the +-- top float vector from the vector float stack and pushes the result to the int stack. +instructionVectorFloatIndexOf :: State -> State +instructionVectorFloatIndexOf = instructionVectorIndexOf float vectorFloat + +-- |Searches and pushes the index of the second float vector inside of the first +-- float vector to the int stack from the vector float stack. Pushes -1 if not found. +instructionVectorFloatIndexOfVectorFloat :: State -> State +instructionVectorFloatIndexOfVectorFloat = instructionVectorIndexOfVector vectorFloat + +-- |Finds the amount of times the top float on the float stack occurs inside of +-- the top float vector from the vector float stack and pushes the result to the +-- int stack. +instructionVectorFloatOccurrencesOf :: State -> State +instructionVectorFloatOccurrencesOf = instructionVectorOccurrencesOf float vectorFloat + +-- |Counts the amount of occurrences of the second float vector within the first +-- float vector. Pushes the result to the int stack. +instructionVectorFloatOccurrencesOfVectorFloat :: State -> State +instructionVectorFloatOccurrencesOfVectorFloat = instructionVectorOccurrencesOfVector vectorFloat + +-- |Splits the top float vector from the vector float stack into lists of size one and pushes +-- the result back one the vector float stack. +instructionVectorFloatParseToFloat :: State -> State +instructionVectorFloatParseToFloat = instructionVectorParseToPrim vectorFloat + +-- |Sets the Nth index inside of the top float vector from the vector float stack to the +-- top value from the primitive stack. N is pulled from the top of the int stack. +instructionVectorFloatSetNth :: State -> State +instructionVectorFloatSetNth = instructionVectorSetNth float vectorFloat + +-- |Splits the float vector on top of the vector float stack with the float from the top +-- of the float stack and pushes the result to the original vector stack. +instructionVectorFloatSplitOn :: State -> State +instructionVectorFloatSplitOn = instructionVectorSplitOn float vectorFloat + +-- |Splits the first float vector based on the second float vector from the vector +-- float stack and pushes the result to the vector float stack. +instructionVectorFloatSplitOnVectorFloat :: State -> State +instructionVectorFloatSplitOnVectorFloat = instructionVectorSplitOnVector vectorFloat + +-- |Replaces the first occurrence of the top float with the second float from +-- the float stack inside of the top float vector from the vector float stack. +-- Pushes the modified float vector to the vector float stack. +instructionVectorFloatReplaceFirst :: State -> State +instructionVectorFloatReplaceFirst = instructionVectorReplace float vectorFloat (Just 1) + +-- |Replaces all occurrences of the top float with the second float from +-- the float stack inside of the top float vector from the vector float stack. +-- Pushes the modified float vector to the vector float stack. +instructionVectorFloatReplaceAll :: State -> State +instructionVectorFloatReplaceAll = instructionVectorReplace float vectorFloat Nothing + +-- |Replaces N occurrences of the top float with the second float from +-- the float stack inside of the top float vector from the vector float stack. +-- Pushes the modified float vector to the vector float stack. N is pulled from +-- the top of the int stack. +instructionVectorFloatReplaceN :: State -> State +instructionVectorFloatReplaceN = instructionVectorReplaceN float vectorFloat + +-- |Replaces the first occurrence of the second float vector with the third float vector +-- inside of the first float vector from the vector float stack. Pushes the result to the +-- vector float stack. +instructionVectorFloatReplaceFirstVectorFloat :: State -> State +instructionVectorFloatReplaceFirstVectorFloat = instructionVectorReplaceVector vectorFloat (Just 1) + +-- |Replaces all occurrences of the second float vector with the third float vector +-- inside of the first float vector from the vector float stack. Pushes the result to the +-- vector float stack. +instructionVectorFloatReplaceAllVectorFloat :: State -> State +instructionVectorFloatReplaceAllVectorFloat = instructionVectorReplaceVector vectorFloat Nothing + +-- |Replaces N occurrences of the second float vector with the third float vector +-- inside of the first float vector from the vector float stack. Pushes the result to the +-- vector float stack. N is pulled from the top of the int stack. +instructionVectorFloatReplaceVectorFloatN :: State -> State +instructionVectorFloatReplaceVectorFloatN = instructionVectorReplaceVectorN vectorFloat + +-- |Removes the first occurrence of the top float from +-- the float stack inside of the top float vector from the vector float stack. +-- Pushes the modified float vector to the vector float stack. +instructionVectorFloatRemoveFirst :: State -> State +instructionVectorFloatRemoveFirst = instructionVectorRemove float vectorFloat (Just 1) + +-- |Removes the all occurrences of the top float from +-- the float stack inside of the top float vector from the vector float stack. +-- Pushes the modified float vector to the vector float stack. +instructionVectorFloatRemoveAll :: State -> State +instructionVectorFloatRemoveAll = instructionVectorRemove float vectorFloat Nothing + +-- |Removes N occurrences of the top float from +-- the float stack inside of the top float vector from the vector float stack. +-- Pushes the modified float vector to the vector float stack. N is pulled +-- from the top of the int stack. +instructionVectorFloatRemoveN :: State -> State +instructionVectorFloatRemoveN = instructionVectorRemoveN float vectorFloat + +-- |Removes the first occurrence of the second float vector +-- inside of the first float vector from the vector float stack. Pushes the result to the +-- vector float stack. +instructionVectorFloatRemoveFirstVectorFloat :: State -> State +instructionVectorFloatRemoveFirstVectorFloat = instructionVectorRemoveVector vectorFloat (Just 1) + +-- |Removes all occurrences of the second float vector +-- inside of the first float vector from the vector float stack. Pushes the result to the +-- vector float stack. +instructionVectorFloatRemoveAllVectorFloat :: State -> State +instructionVectorFloatRemoveAllVectorFloat = instructionVectorRemoveVector vectorFloat Nothing + +-- |Removes N occurrences of the second float vector +-- inside of the first float vector from the vector float stack. Pushes the result to the +-- vector float stack. N is pulled from the top of the int stack. +instructionVectorFloatRemoveNVectorFloat :: State -> State +instructionVectorFloatRemoveNVectorFloat = instructionVectorRemoveVectorN vectorFloat + +-- |Iterates over the top float vector on the vector float stack, applying the top instruction of the +-- exec stack along the way. +instructionVectorFloatIterate :: State -> State +instructionVectorFloatIterate = instructionVectorIterate float vectorFloat GeneVectorFloat instructionVectorFloatIterate "instructionVectorFloatIterate" + +-- |Sorts the top float vector on the vector float stack and pushes the result back to the +-- vector float stack. instructionVectorFloatSort :: State -> State instructionVectorFloatSort = instructionVectorSort vectorFloat +-- |Sorts the top float vector on the vector float stack, reverses it, and pushes the result back to the +-- vector float stack. instructionVectorFloatSortReverse :: State -> State instructionVectorFloatSortReverse = instructionVectorSortReverse vectorFloat -instructionVectorFloatDupItems :: State -> State -instructionVectorFloatDupItems = instructionDupItems vectorFloat +-- |Inserts the top float from the float stack into the top float vector from the +-- vector float stack at a specified index and pushes the result to the vector +-- float stack. The index is pulled from the top of the int stack. +instructionVectorFloatInsert :: State -> State +instructionVectorFloatInsert = instructionVectorInsert float vectorFloat + +-- |Inserts the second float vector into the first float vector from the vector float stack +-- at a specified index and pushes the result to the vector float stack. The index is +-- pulled from the top of the int stack. +instructionVectorFloatInsertVectorFloat :: State -> State +instructionVectorFloatInsertVectorFloat = instructionVectorInsertVector vectorFloat diff --git a/src/HushGP/Instructions/VectorIntInstructions.hs b/src/HushGP/Instructions/VectorIntInstructions.hs index 04c7b5e..cddf728 100644 --- a/src/HushGP/Instructions/VectorIntInstructions.hs +++ b/src/HushGP/Instructions/VectorIntInstructions.hs @@ -3,113 +3,328 @@ module HushGP.Instructions.VectorIntInstructions where import HushGP.Instructions.GenericInstructions import HushGP.State +-- |Pops the top int vector from the int vector stack. +instructionVectorIntPop :: State -> State +instructionVectorIntPop = instructionPop vectorInt + +-- |Duplicates the top int vector from the int vector stack. +instructionVectorIntDup :: State -> State +instructionVectorIntDup = instructionDup vectorInt + +-- |Duplicates the top int vector from the int vector stack N times +-- based on the top int from the int stack. +instructionVectorIntDupN :: State -> State +instructionVectorIntDupN = instructionDupN vectorInt + +-- |Swaps the top two int vectors from the int vector stack. +instructionVectorIntSwap :: State -> State +instructionVectorIntSwap = instructionSwap vectorInt + +-- |Rotates the top three int vectors from the int vector stack. +instructionVectorIntRot :: State -> State +instructionVectorIntRot = instructionRot vectorInt + +-- |Sets the vector int stack to [] +instructionVectorIntFlush :: State -> State +instructionVectorIntFlush = instructionFlush vectorInt + +-- |Pushes True to the bool stack if the top two int vectors from +-- the vector int stack are equal. Pushes False otherwise. +instructionVectorIntEq :: State -> State +instructionVectorIntEq = instructionEq vectorInt + +-- |Calculates the size of the vector int stack and pushes that number +-- to the int stack. +instructionVectorIntStackDepth :: State -> State +instructionVectorIntStackDepth = instructionStackDepth vectorInt + +-- |Moves an item from deep within the vector int stack to the top of the vector int stack based on +-- the top int from the int stack. +instructionVectorIntYank :: State -> State +instructionVectorIntYank = instructionYank vectorInt + +-- |Copies an item from deep within the vector int stack to the top of the vector int stack based on +-- the top int from the int stack. +instructionVectorIntYankDup :: State -> State +instructionVectorIntYankDup = instructionYankDup vectorInt + +-- |Pushes True to the bool stack if the vector int stack is empty. False if not. +instructionVectorIntIsStackEmpty :: State -> State +instructionVectorIntIsStackEmpty = instructionIsStackEmpty vectorInt + +-- |Moves an item from the top of the vector int stack to deep within the vector int stack based on +-- the top int from the int stack. +instructionVectorIntShove :: State -> State +instructionVectorIntShove = instructionShove vectorInt + +-- |Copies an item from the top of the vector int stack to deep within the vector int stack based on +-- the top int from the int stack. +instructionVectorIntShoveDup :: State -> State +instructionVectorIntShoveDup = instructionShoveDup vectorInt + +-- |Duplicate the top N items from the vector int stack based on the top int from the int stack. +instructionVectorIntDupItems :: State -> State +instructionVectorIntDupItems = instructionDupItems vectorInt + +-- |Concats the top two vectors on top of the vector int stack. instructionVectorIntConcat :: State -> State instructionVectorIntConcat = instructionVectorConcat vectorInt +-- |Takes the top int from the int stack and prepends it to top int vector +-- on the int vector stack. instructionVectorIntConj :: State -> State instructionVectorIntConj = instructionVectorConj int vectorInt +-- |Takes the top int from the int stack and appends it to top int vector +-- on the int vector stack. +instructionVectorIntConjEnd :: State -> State +instructionVectorIntConjEnd = instructionVectorConjEnd int vectorInt + +-- |Takes the first N ints from the top of the int vector from the int vector +-- and pushes the result to the int vector stack. N is pulled from the top of +-- the int stack. instructionVectorIntTakeN :: State -> State instructionVectorIntTakeN = instructionVectorTakeN vectorInt +-- |Takes the last N ints from the top of the int vector from the int vector +-- and pushes the result to the int vector stack. N is pulled from the top of +-- the int stack. +instructionVectorIntTakeRN :: State -> State +instructionVectorIntTakeRN = instructionVectorTakeRN vectorInt + +-- |Takes a sublist of the top int vector on top of the vector int stack. +-- The two ints to determine bounds are pulled from the top of the int stack. instructionVectorIntSubVector :: State -> State instructionVectorIntSubVector = instructionSubVector vectorInt +-- |Takes the first int from the top of the vector int stack and places +-- it on the int stack. instructionVectorIntFirst :: State -> State instructionVectorIntFirst = instructionVectorFirst int vectorInt +-- |Takes the first int from the top of the vector int stack and places +-- it wrapped in a list on top of the vector int stack. +instructionVectorIntFromFirstPrim :: State -> State +instructionVectorIntFromFirstPrim = instructionVectorFromFirstPrim vectorInt + +-- |Takes the first int from the top of the int stack and places it +-- wrapped in a list on top of the vector int stack. +instructionVectorIntFromPrim :: State -> State +instructionVectorIntFromPrim = instructionVectorFromPrim int vectorInt + +-- |Takes the last int from the top of the vector int stack and places +-- it on the int stack. instructionVectorIntLast :: State -> State instructionVectorIntLast = instructionVectorLast int vectorInt +-- |Takes the last int from the top int vector on the vector int stack and +-- places it on the int stack. +instructionVectorIntFromLastPrim :: State -> State +instructionVectorIntFromLastPrim = instructionVectorFromLastPrim vectorInt + +-- |Takes the Nth int from the top int vector and places it onto the int stack +-- based on an int from the top of the int stack. instructionVectorIntNth :: State -> State instructionVectorIntNth = instructionVectorNth int vectorInt +-- |Takes the Nth int from the top int vector on the vector int stack and +-- creates a vector wrapping that Nth item, pushing it back onto the vector int stack. +-- N is the top item on the int stack. +instructionVectorIntFromNthPrim :: State -> State +instructionVectorIntFromNthPrim = instructionVectorFromNthPrim vectorInt + +-- |Removes the first int from the top int vector on the vector int stack and +-- places the result back onto the vector int stack. instructionVectorIntRest :: State -> State instructionVectorIntRest = instructionVectorRest vectorInt +-- |Removes the last int from the top int vector on the vector int stack and +-- places the result back onto the vector int stack. instructionVectorIntButLast :: State -> State instructionVectorIntButLast = instructionVectorButLast vectorInt +-- |Drops the first N items from the top int vector and pushes the result +-- back to the vector int stack. N is pulled from the top of the int stack. +instructionVectorIntDrop :: State -> State +instructionVectorIntDrop = instructionVectorDrop vectorInt + +-- |Drops the last N items from the top int vector and pushes the result +-- back to the vector int stack. N is pulled from the top of the int stack. +instructionVectorIntDropR :: State -> State +instructionVectorIntDropR = instructionVectorDropR vectorInt + +-- |Pushes the length of the top int vector from the vector int stack +-- to the top of the int stack. instructionVectorIntLength :: State -> State instructionVectorIntLength = instructionLength vectorInt +-- |Reverses the top int vector from the vector int stack and pushes the +-- result to the vector int stack. instructionVectorIntReverse :: State -> State instructionVectorIntReverse = instructionReverse vectorInt +-- |Takes the top int vector from the vector int stack and pushes the +-- individual ints to the vector int stack. instructionVectorIntPushAll :: State -> State instructionVectorIntPushAll = instructionPushAll int vectorInt +-- |Makes an empty vector and pushes it to the vector int stack. instructionVectorIntMakeEmpty :: State -> State instructionVectorIntMakeEmpty = instructionVectorMakeEmpty vectorInt +-- |Checks if the top int vector from the vector int stack is empty. +-- Pushes True if the int vector is empty to the bool stack. False otherwise. instructionVectorIntIsEmpty :: State -> State instructionVectorIntIsEmpty = instructionVectorIsEmpty vectorInt +-- |If the top int vector from the vector int stack contains the top int from the int +-- stack, pushes True to the bool stack and pushes False otherwise. +instructionVectorIntContains :: State -> State +instructionVectorIntContains = instructionVectorContains int vectorInt + +-- |If the second to top int vector can be found within the first int vector from the +-- vector int stack, pushes True to the bool stack if is found, else False. +instructionVectorIntContainsVectorInt :: State -> State +instructionVectorIntContainsVectorInt = instructionVectorContainsVector vectorInt + +-- |Finds the first index of the top int in the int stack inside of the +-- top int vector from the vector int stack and pushes the result to the int stack. instructionVectorIntIndexOf :: State -> State instructionVectorIntIndexOf = instructionVectorIndexOf int vectorInt +-- |Searches and pushes the index of the second int vector inside of the first +-- int vector to the int stack from the vector int stack. Pushes -1 if not found. +instructionVectorIntIndexOfVectorInt :: State -> State +instructionVectorIntIndexOfVectorInt = instructionVectorIndexOfVector vectorInt + +-- |Finds the amount of times the top int on the int stack occurs inside of +-- the top int vector from the vector int stack and pushes the result to the +-- int stack. instructionVectorIntOccurrencesOf :: State -> State instructionVectorIntOccurrencesOf = instructionVectorOccurrencesOf int vectorInt +-- |Counts the amount of occurrences of the second int vector within the first +-- int vector. Pushes the result to the int stack. +instructionVectorIntOccurrencesOfVectorInt :: State -> State +instructionVectorIntOccurrencesOfVectorInt = instructionVectorOccurrencesOfVector vectorInt + +-- |Splits the top int vector from the vector int stack into lists of size one and pushes +-- the result back one the vector int stack. +instructionVectorIntParseToInt :: State -> State +instructionVectorIntParseToInt = instructionVectorParseToPrim vectorInt + +-- |Sets the Nth index inside of the top int vector from the vector int stack to the +-- top value from the primitive stack. N is pulled from the top of the int stack. instructionVectorIntSetNth :: State -> State instructionVectorIntSetNth = instructionVectorSetNth int vectorInt -instructionVectorIntReplace :: State -> State -instructionVectorIntReplace = instructionVectorReplace int vectorInt Nothing +-- |Splits the int vector on top of the vector int stack with the int from the top +-- of the int stack and pushes the result to the original vector stack. +instructionVectorIntSplitOn :: State -> State +instructionVectorIntSplitOn = instructionVectorSplitOn int vectorInt +-- |Splits the first int vector based on the second int vector from the vector +-- int stack and pushes the result to the vector int stack. +instructionVectorIntSplitOnVectorInt :: State -> State +instructionVectorIntSplitOnVectorInt = instructionVectorSplitOnVector vectorInt + +-- |Replaces the first occurrence of the top int with the second int from +-- the int stack inside of the top int vector from the vector int stack. +-- Pushes the modified int vector to the vector int stack. instructionVectorIntReplaceFirst :: State -> State instructionVectorIntReplaceFirst = instructionVectorReplace int vectorInt (Just 1) -instructionVectorIntRemove :: State -> State -instructionVectorIntRemove = instructionVectorRemove int vectorInt Nothing +-- |Replaces all occurrences of the top int with the second int from +-- the int stack inside of the top int vector from the vector int stack. +-- Pushes the modified int vector to the vector int stack. +instructionVectorIntReplaceAll :: State -> State +instructionVectorIntReplaceAll = instructionVectorReplace int vectorInt Nothing +-- |Replaces N occurrences of the top int with the second int from +-- the int stack inside of the top int vector from the vector int stack. +-- Pushes the modified int vector to the vector int stack. N is pulled from +-- the top of the int stack. +instructionVectorIntReplaceN :: State -> State +instructionVectorIntReplaceN = instructionVectorReplaceN int vectorInt + +-- |Replaces the first occurrence of the second int vector with the third int vector +-- inside of the first int vector from the vector int stack. Pushes the result to the +-- vector int stack. +instructionVectorIntReplaceFirstVectorInt :: State -> State +instructionVectorIntReplaceFirstVectorInt = instructionVectorReplaceVector vectorInt (Just 1) + +-- |Replaces all occurrences of the second int vector with the third int vector +-- inside of the first int vector from the vector int stack. Pushes the result to the +-- vector int stack. +instructionVectorIntReplaceAllVectorInt :: State -> State +instructionVectorIntReplaceAllVectorInt = instructionVectorReplaceVector vectorInt Nothing + +-- |Replaces N occurrences of the second int vector with the third int vector +-- inside of the first int vector from the vector int stack. Pushes the result to the +-- vector int stack. N is pulled from the top of the int stack. +instructionVectorIntReplaceVectorIntN :: State -> State +instructionVectorIntReplaceVectorIntN = instructionVectorReplaceVectorN vectorInt + +-- |Removes the first occurrence of the top int from +-- the int stack inside of the top int vector from the vector int stack. +-- Pushes the modified int vector to the vector int stack. +instructionVectorIntRemoveFirst :: State -> State +instructionVectorIntRemoveFirst = instructionVectorRemove int vectorInt (Just 1) + +-- |Removes the all occurrences of the top int from +-- the int stack inside of the top int vector from the vector int stack. +-- Pushes the modified int vector to the vector int stack. +instructionVectorIntRemoveAll :: State -> State +instructionVectorIntRemoveAll = instructionVectorRemove int vectorInt Nothing + +-- |Removes N occurrences of the top int from +-- the int stack inside of the top int vector from the vector int stack. +-- Pushes the modified int vector to the vector int stack. N is pulled +-- from the top of the int stack. +instructionVectorIntRemoveN :: State -> State +instructionVectorIntRemoveN = instructionVectorRemoveN int vectorInt + +-- |Removes the first occurrence of the second int vector +-- inside of the first int vector from the vector int stack. Pushes the result to the +-- vector int stack. +instructionVectorIntRemoveFirstVectorInt :: State -> State +instructionVectorIntRemoveFirstVectorInt = instructionVectorRemoveVector vectorInt (Just 1) + +-- |Removes all occurrences of the second int vector +-- inside of the first int vector from the vector int stack. Pushes the result to the +-- vector int stack. +instructionVectorIntRemoveAllVectorInt :: State -> State +instructionVectorIntRemoveAllVectorInt = instructionVectorRemoveVector vectorInt Nothing + +-- |Removes N occurrences of the second int vector +-- inside of the first int vector from the vector int stack. Pushes the result to the +-- vector int stack. N is pulled from the top of the int stack. +instructionVectorIntRemoveNVectorInt :: State -> State +instructionVectorIntRemoveNVectorInt = instructionVectorRemoveVectorN vectorInt + +-- |Iterates over the top int vector on the vector int stack, applying the top instruction of the +-- exec stack along the way. instructionVectorIntIterate :: State -> State instructionVectorIntIterate = instructionVectorIterate int vectorInt GeneVectorInt instructionVectorIntIterate "instructionVectorIntIterate" -instructionVectorIntPop :: State -> State -instructionVectorIntPop = instructionPop vectorChar - -instructionVectorIntDup :: State -> State -instructionVectorIntDup = instructionDup vectorChar - -instructionVectorIntDupN :: State -> State -instructionVectorIntDupN = instructionDupN vectorChar - -instructionVectorIntSwap :: State -> State -instructionVectorIntSwap = instructionSwap vectorChar - -instructionVectorIntRot :: State -> State -instructionVectorIntRot = instructionRot vectorChar - -instructionVectorIntFlush :: State -> State -instructionVectorIntFlush = instructionFlush vectorChar - -instructionVectorIntEq :: State -> State -instructionVectorIntEq = instructionEq vectorChar - -instructionVectorIntStackDepth :: State -> State -instructionVectorIntStackDepth = instructionStackDepth vectorChar - -instructionVectorIntYank :: State -> State -instructionVectorIntYank = instructionYank vectorChar - -instructionVectorIntYankDup :: State -> State -instructionVectorIntYankDup = instructionYankDup vectorChar - -instructionVectorIntIsStackEmpty :: State -> State -instructionVectorIntIsStackEmpty = instructionIsStackEmpty vectorChar - -instructionVectorIntShove :: State -> State -instructionVectorIntShove = instructionShove vectorChar - -instructionVectorIntShoveDup :: State -> State -instructionVectorIntShoveDup = instructionShoveDup vectorChar - +-- |Sorts the top int vector on the vector int stack and pushes the result back to the +-- vector int stack. instructionVectorIntSort :: State -> State instructionVectorIntSort = instructionVectorSort vectorInt +-- |Sorts the top int vector on the vector int stack, reverses it, and pushes the result back to the +-- vector int stack. instructionVectorIntSortReverse :: State -> State instructionVectorIntSortReverse = instructionVectorSortReverse vectorInt -instructionVectorIntDupItems :: State -> State -instructionVectorIntDupItems = instructionDupItems vectorInt +-- |Inserts the top int from the int stack into the top int vector from the +-- vector int stack at a specified index and pushes the result to the vector +-- int stack. The index is pulled from the top of the int stack. +instructionVectorIntInsert :: State -> State +instructionVectorIntInsert = instructionVectorInsert int vectorInt + +-- |Inserts the second int vector into the first int vector from the vector int stack +-- at a specified index and pushes the result to the vector int stack. The index is +-- pulled from the top of the int stack. +instructionVectorIntInsertVectorInt :: State -> State +instructionVectorIntInsertVectorInt = instructionVectorInsertVector vectorInt diff --git a/src/HushGP/Instructions/VectorStringInstructions.hs b/src/HushGP/Instructions/VectorStringInstructions.hs index 64966f9..f4b904e 100644 --- a/src/HushGP/Instructions/VectorStringInstructions.hs +++ b/src/HushGP/Instructions/VectorStringInstructions.hs @@ -3,113 +3,328 @@ module HushGP.Instructions.VectorStringInstructions where import HushGP.State import HushGP.Instructions.GenericInstructions -instructionVectorStringConcat :: State -> State -instructionVectorStringConcat = instructionVectorConcat vectorString - -instructionVectorStringConj :: State -> State -instructionVectorStringConj = instructionVectorConj string vectorString - -instructionVectorStringTakeN :: State -> State -instructionVectorStringTakeN = instructionVectorTakeN vectorString - -instructionVectorStringSubVector :: State -> State -instructionVectorStringSubVector = instructionSubVector vectorString - -instructionVectorStringFirst :: State -> State -instructionVectorStringFirst = instructionVectorFirst string vectorString - -instructionVectorStringLast :: State -> State -instructionVectorStringLast = instructionVectorLast string vectorString - -instructionVectorStringNth :: State -> State -instructionVectorStringNth = instructionVectorNth string vectorString - -instructionVectorStringRest :: State -> State -instructionVectorStringRest = instructionVectorRest vectorString - -instructionVectorStringButLast :: State -> State -instructionVectorStringButLast = instructionVectorButLast vectorString - -instructionVectorStringLength :: State -> State -instructionVectorStringLength = instructionLength vectorString - -instructionVectorStringReverse :: State -> State -instructionVectorStringReverse = instructionReverse vectorString - -instructionVectorStringPushAll :: State -> State -instructionVectorStringPushAll = instructionPushAll string vectorString - -instructionVectorStringMakeEmpty :: State -> State -instructionVectorStringMakeEmpty = instructionVectorMakeEmpty vectorString - -instructionVectorStringIsEmpty :: State -> State -instructionVectorStringIsEmpty = instructionVectorIsEmpty vectorString - -instructionVectorStringIndexOf :: State -> State -instructionVectorStringIndexOf = instructionVectorIndexOf string vectorString - -instructionVectorStringOccurrencesOf :: State -> State -instructionVectorStringOccurrencesOf = instructionVectorOccurrencesOf string vectorString - -instructionVectorStringSetNth :: State -> State -instructionVectorStringSetNth = instructionVectorSetNth string vectorString - -instructionVectorStringReplace :: State -> State -instructionVectorStringReplace = instructionVectorReplace string vectorString Nothing - -instructionVectorStringReplaceFirst :: State -> State -instructionVectorStringReplaceFirst = instructionVectorReplace string vectorString (Just 1) - -instructionVectorStringRemove :: State -> State -instructionVectorStringRemove = instructionVectorRemove string vectorString Nothing - -instructionVectorStringIterate :: State -> State -instructionVectorStringIterate = instructionVectorIterate string vectorString GeneVectorString instructionVectorStringIterate "instructionVectorStringIterate" - +-- |Pops the top string vector from the string vector stack. instructionVectorStringPop :: State -> State instructionVectorStringPop = instructionPop vectorString +-- |Duplicates the top string vector from the string vector stack. instructionVectorStringDup :: State -> State instructionVectorStringDup = instructionDup vectorString +-- |Duplicates the top string vector from the string vector stack N times +-- based on the top int from the int stack. instructionVectorStringDupN :: State -> State instructionVectorStringDupN = instructionDupN vectorString +-- |Swaps the top two string vectors from the string vector stack. instructionVectorStringSwap :: State -> State instructionVectorStringSwap = instructionSwap vectorString +-- |Rotates the top three string vectors from the string vector stack. instructionVectorStringRot :: State -> State instructionVectorStringRot = instructionRot vectorString +-- |Sets the vector string stack to [] instructionVectorStringFlush :: State -> State instructionVectorStringFlush = instructionFlush vectorString +-- |Pushes True to the bool stack if the top two string vectors from +-- the vector string stack are equal. Pushes False otherwise. instructionVectorStringEq :: State -> State instructionVectorStringEq = instructionEq vectorString +-- |Calculates the size of the vector string stack and pushes that number +-- to the int stack. instructionVectorStringStackDepth :: State -> State instructionVectorStringStackDepth = instructionStackDepth vectorString +-- |Moves an item from deep within the vector string stack to the top of the vector string stack based on +-- the top int from the int stack. instructionVectorStringYank :: State -> State instructionVectorStringYank = instructionYank vectorString +-- |Copies an item from deep within the vector string stack to the top of the vector string stack based on +-- the top int from the int stack. instructionVectorStringYankDup :: State -> State instructionVectorStringYankDup = instructionYankDup vectorString +-- |Pushes True to the bool stack if the vector string stack is empty. False if not. instructionVectorStringIsStackEmpty :: State -> State instructionVectorStringIsStackEmpty = instructionIsStackEmpty vectorString +-- |Moves an item from the top of the vector string stack to deep within the vector string stack based on +-- the top int from the int stack. instructionVectorStringShove :: State -> State instructionVectorStringShove = instructionShove vectorString +-- |Copies an item from the top of the vector string stack to deep within the vector string stack based on +-- the top int from the int stack. instructionVectorStringShoveDup :: State -> State instructionVectorStringShoveDup = instructionShoveDup vectorString +-- |Duplicate the top N items from the vector string stack based on the top int from the int stack. +instructionVectorStringDupItems :: State -> State +instructionVectorStringDupItems = instructionDupItems vectorString + +-- |Concats the top two vectors on top of the vector string stack. +instructionVectorStringConcat :: State -> State +instructionVectorStringConcat = instructionVectorConcat vectorString + +-- |Takes the top string from the string stack and prepends it to top string vector +-- on the string vector stack. +instructionVectorStringConj :: State -> State +instructionVectorStringConj = instructionVectorConj string vectorString + +-- |Takes the top string from the string stack and appends it to top string vector +-- on the string vector stack. +instructionVectorStringConjEnd :: State -> State +instructionVectorStringConjEnd = instructionVectorConjEnd string vectorString + +-- |Takes the first N strings from the top of the string vector from the string vector +-- and pushes the result to the string vector stack. N is pulled from the top of +-- the int stack. +instructionVectorStringTakeN :: State -> State +instructionVectorStringTakeN = instructionVectorTakeN vectorString + +-- |Takes the last N strings from the top of the string vector from the string vector +-- and pushes the result to the string vector stack. N is pulled from the top of +-- the int stack. +instructionVectorStringTakeRN :: State -> State +instructionVectorStringTakeRN = instructionVectorTakeRN vectorString + +-- |Takes a sublist of the top string vector on top of the vector string stack. +-- The two ints to determine bounds are pulled from the top of the int stack. +instructionVectorStringSubVector :: State -> State +instructionVectorStringSubVector = instructionSubVector vectorString + +-- |Takes the first string from the top of the vector string stack and places +-- it on the string stack. +instructionVectorStringFirst :: State -> State +instructionVectorStringFirst = instructionVectorFirst string vectorString + +-- |Takes the first string from the top of the vector string stack and places +-- it wrapped in a list on top of the vector string stack. +instructionVectorStringFromFirstPrim :: State -> State +instructionVectorStringFromFirstPrim = instructionVectorFromFirstPrim vectorString + +-- |Takes the first string from the top of the string stack and places it +-- wrapped in a list on top of the vector string stack. +instructionVectorStringFromPrim :: State -> State +instructionVectorStringFromPrim = instructionVectorFromPrim string vectorString + +-- |Takes the last string from the top of the vector string stack and places +-- it on the string stack. +instructionVectorStringLast :: State -> State +instructionVectorStringLast = instructionVectorLast string vectorString + +-- |Takes the last string from the top string vector on the vector string stack and +-- places it on the string stack. +instructionVectorStringFromLastPrim :: State -> State +instructionVectorStringFromLastPrim = instructionVectorFromLastPrim vectorString + +-- |Takes the Nth string from the top string vector and places it onto the string stack +-- based on an int from the top of the int stack. +instructionVectorStringNth :: State -> State +instructionVectorStringNth = instructionVectorNth string vectorString + +-- |Takes the Nth string from the top string vector on the vector string stack and +-- creates a vector wrapping that Nth item, pushing it back onto the vector string stack. +-- N is the top item on the int stack. +instructionVectorStringFromNthPrim :: State -> State +instructionVectorStringFromNthPrim = instructionVectorFromNthPrim vectorString + +-- |Removes the first string from the top string vector on the vector string stack and +-- places the result back onto the vector string stack. +instructionVectorStringRest :: State -> State +instructionVectorStringRest = instructionVectorRest vectorString + +-- |Removes the last string from the top string vector on the vector string stack and +-- places the result back onto the vector string stack. +instructionVectorStringButLast :: State -> State +instructionVectorStringButLast = instructionVectorButLast vectorString + +-- |Drops the first N items from the top string vector and pushes the result +-- back to the vector string stack. N is pulled from the top of the int stack. +instructionVectorStringDrop :: State -> State +instructionVectorStringDrop = instructionVectorDrop vectorString + +-- |Drops the last N items from the top string vector and pushes the result +-- back to the vector string stack. N is pulled from the top of the int stack. +instructionVectorStringDropR :: State -> State +instructionVectorStringDropR = instructionVectorDropR vectorString + +-- |Pushes the length of the top string vector from the vector string stack +-- to the top of the int stack. +instructionVectorStringLength :: State -> State +instructionVectorStringLength = instructionLength vectorString + +-- |Reverses the top string vector from the vector string stack and pushes the +-- result to the vector string stack. +instructionVectorStringReverse :: State -> State +instructionVectorStringReverse = instructionReverse vectorString + +-- |Takes the top string vector from the vector string stack and pushes the +-- individual strings to the vector string stack. +instructionVectorStringPushAll :: State -> State +instructionVectorStringPushAll = instructionPushAll string vectorString + +-- |Makes an empty vector and pushes it to the vector string stack. +instructionVectorStringMakeEmpty :: State -> State +instructionVectorStringMakeEmpty = instructionVectorMakeEmpty vectorString + +-- |Checks if the top string vector from the vector string stack is empty. +-- Pushes True if the string vector is empty to the bool stack. False otherwise. +instructionVectorStringIsEmpty :: State -> State +instructionVectorStringIsEmpty = instructionVectorIsEmpty vectorString + +-- |If the top string vector from the vector string stack contains the top string from the string +-- stack, pushes True to the bool stack and pushes False otherwise. +instructionVectorStringContains :: State -> State +instructionVectorStringContains = instructionVectorContains string vectorString + +-- |If the second to top string vector can be found within the first string vector from the +-- vector string stack, pushes True to the bool stack if is found, else False. +instructionVectorStringContainsVectorString :: State -> State +instructionVectorStringContainsVectorString = instructionVectorContainsVector vectorString + +-- |Finds the first index of the top string in the string stack inside of the +-- top string vector from the vector string stack and pushes the result to the int stack. +instructionVectorStringIndexOf :: State -> State +instructionVectorStringIndexOf = instructionVectorIndexOf string vectorString + +-- |Searches and pushes the index of the second string vector inside of the first +-- string vector to the int stack from the vector string stack. Pushes -1 if not found. +instructionVectorStringIndexOfVectorString :: State -> State +instructionVectorStringIndexOfVectorString = instructionVectorIndexOfVector vectorString + +-- |Finds the amount of times the top string on the string stack occurs inside of +-- the top string vector from the vector string stack and pushes the result to the +-- int stack. +instructionVectorStringOccurrencesOf :: State -> State +instructionVectorStringOccurrencesOf = instructionVectorOccurrencesOf string vectorString + +-- |Counts the amount of occurrences of the second string vector within the first +-- string vector. Pushes the result to the int stack. +instructionVectorStringOccurrencesOfVectorString :: State -> State +instructionVectorStringOccurrencesOfVectorString = instructionVectorOccurrencesOfVector vectorString + +-- |Splits the top string vector from the vector string stack into lists of size one and pushes +-- the result back one the vector string stack. +instructionVectorStringParseToString :: State -> State +instructionVectorStringParseToString = instructionVectorParseToPrim vectorString + +-- |Sets the Nth index inside of the top string vector from the vector string stack to the +-- top value from the primitive stack. N is pulled from the top of the int stack. +instructionVectorStringSetNth :: State -> State +instructionVectorStringSetNth = instructionVectorSetNth string vectorString + +-- |Splits the string vector on top of the vector string stack with the string from the top +-- of the string stack and pushes the result to the original vector stack. +instructionVectorStringSplitOn :: State -> State +instructionVectorStringSplitOn = instructionVectorSplitOn string vectorString + +-- |Splits the first string vector based on the second string vector from the vector +-- string stack and pushes the result to the vector string stack. +instructionVectorStringSplitOnVectorString :: State -> State +instructionVectorStringSplitOnVectorString = instructionVectorSplitOnVector vectorString + +-- |Replaces the first occurrence of the top string with the second string from +-- the string stack inside of the top string vector from the vector string stack. +-- Pushes the modified string vector to the vector string stack. +instructionVectorStringReplaceFirst :: State -> State +instructionVectorStringReplaceFirst = instructionVectorReplace string vectorString (Just 1) + +-- |Replaces all occurrences of the top string with the second string from +-- the string stack inside of the top string vector from the vector string stack. +-- Pushes the modified string vector to the vector string stack. +instructionVectorStringReplaceAll :: State -> State +instructionVectorStringReplaceAll = instructionVectorReplace string vectorString Nothing + +-- |Replaces N occurrences of the top string with the second string from +-- the string stack inside of the top string vector from the vector string stack. +-- Pushes the modified string vector to the vector string stack. N is pulled from +-- the top of the int stack. +instructionVectorStringReplaceN :: State -> State +instructionVectorStringReplaceN = instructionVectorReplaceN string vectorString + +-- |Replaces the first occurrence of the second string vector with the third string vector +-- inside of the first string vector from the vector string stack. Pushes the result to the +-- vector string stack. +instructionVectorStringReplaceFirstVectorString :: State -> State +instructionVectorStringReplaceFirstVectorString = instructionVectorReplaceVector vectorString (Just 1) + +-- |Replaces all occurrences of the second string vector with the third string vector +-- inside of the first string vector from the vector string stack. Pushes the result to the +-- vector string stack. +instructionVectorStringReplaceAllVectorString :: State -> State +instructionVectorStringReplaceAllVectorString = instructionVectorReplaceVector vectorString Nothing + +-- |Replaces N occurrences of the second string vector with the third string vector +-- inside of the first string vector from the vector string stack. Pushes the result to the +-- vector string stack. N is pulled from the top of the int stack. +instructionVectorStringReplaceVectorStringN :: State -> State +instructionVectorStringReplaceVectorStringN = instructionVectorReplaceVectorN vectorString + +-- |Removes the first occurrence of the top string from +-- the string stack inside of the top string vector from the vector string stack. +-- Pushes the modified string vector to the vector string stack. +instructionVectorStringRemoveFirst :: State -> State +instructionVectorStringRemoveFirst = instructionVectorRemove string vectorString (Just 1) + +-- |Removes the all occurrences of the top string from +-- the string stack inside of the top string vector from the vector string stack. +-- Pushes the modified string vector to the vector string stack. +instructionVectorStringRemoveAll :: State -> State +instructionVectorStringRemoveAll = instructionVectorRemove string vectorString Nothing + +-- |Removes N occurrences of the top string from +-- the string stack inside of the top string vector from the vector string stack. +-- Pushes the modified string vector to the vector string stack. N is pulled +-- from the top of the int stack. +instructionVectorStringRemoveN :: State -> State +instructionVectorStringRemoveN = instructionVectorRemoveN string vectorString + +-- |Removes the first occurrence of the second string vector +-- inside of the first string vector from the vector string stack. Pushes the result to the +-- vector string stack. +instructionVectorStringRemoveFirstVectorString :: State -> State +instructionVectorStringRemoveFirstVectorString = instructionVectorRemoveVector vectorString (Just 1) + +-- |Removes all occurrences of the second string vector +-- inside of the first string vector from the vector string stack. Pushes the result to the +-- vector string stack. +instructionVectorStringRemoveAllVectorString :: State -> State +instructionVectorStringRemoveAllVectorString = instructionVectorRemoveVector vectorString Nothing + +-- |Removes N occurrences of the second string vector +-- inside of the first string vector from the vector string stack. Pushes the result to the +-- vector string stack. N is pulled from the top of the int stack. +instructionVectorStringRemoveNVectorString :: State -> State +instructionVectorStringRemoveNVectorString = instructionVectorRemoveVectorN vectorString + +-- |Iterates over the top string vector on the vector string stack, applying the top instruction of the +-- exec stack along the way. +instructionVectorStringIterate :: State -> State +instructionVectorStringIterate = instructionVectorIterate string vectorString GeneVectorString instructionVectorStringIterate "instructionVectorStringIterate" + +-- |Sorts the top string vector on the vector string stack and pushes the result back to the +-- vector string stack. instructionVectorStringSort :: State -> State instructionVectorStringSort = instructionVectorSort vectorString +-- |Sorts the top string vector on the vector string stack, reverses it, and pushes the result back to the +-- vector string stack. instructionVectorStringSortReverse :: State -> State instructionVectorStringSortReverse = instructionVectorSortReverse vectorString -instructionVectorStringDupItems :: State -> State -instructionVectorStringDupItems = instructionDupItems vectorString +-- |Inserts the top string from the string stack into the top string vector from the +-- vector string stack at a specified index and pushes the result to the vector +-- string stack. The index is pulled from the top of the int stack. +instructionVectorStringInsert :: State -> State +instructionVectorStringInsert = instructionVectorInsert string vectorString + +-- |Inserts the second string vector into the first string vector from the vector string stack +-- at a specified index and pushes the result to the vector string stack. The index is +-- pulled from the top of the int stack. +instructionVectorStringInsertVectorString :: State -> State +instructionVectorStringInsertVectorString = instructionVectorInsertVector vectorString diff --git a/src/HushGP/Push.hs b/src/HushGP/Push.hs index 1c9b75f..f243be5 100644 --- a/src/HushGP/Push.hs +++ b/src/HushGP/Push.hs @@ -12,7 +12,7 @@ import HushGP.State -- Everntually, this can be part of the apply func to state helpers, -- which should take the number and type of parameter they have. --- This is one of the push genome functions itself, not infrastructure. +-- |This is one of the push genome functions itself, not infrastructure. -- Optionally, split this off into independent functions instructionParameterLoad :: State -> State instructionParameterLoad state@(State {_parameter = (p : _)}) = case p of @@ -32,11 +32,11 @@ instructionParameterLoad state@(State {_parameter = (p : _)}) = case p of (Block xs) -> state & exec .~ xs <> view exec state instructionParameterLoad state = state --- Loads a genome into the exec stack +-- |Loads a genome into the exec stack loadProgram :: [Gene] -> State -> State loadProgram newstack state = state & exec .~ newstack --- Takes a Push state, and generates the next push state via: +-- |Takes a Push state, and generates the next push state via: -- If the first item on the EXEC stack is a single instruction -- then pop it and execute it. -- Else if the first item on the EXEC stack is a literal @@ -64,23 +64,3 @@ interpretExec state@(State {_exec = e : es}) = (PlaceInput val) -> interpretExec (state {_exec = (view input state Map.! val) : es}) Close -> undefined -- This should be removed later. Will be converted to Blocks in the Plushy -> Exec stack process interpretExec state = state - --- interpretOneStep :: State -> State --- interpretOneStep state@(State {_exec = e : es}) = --- case e of --- (GeneInt val) -> state & exec .~ es & int .~ val : view int state --- (GeneFloat val) -> state & exec .~ es & float .~ val : view float state --- (GeneBool val) -> state & exec .~ es & bool .~ val : view bool state --- (GeneString val) -> state & exec .~ es & string .~ val : view string state --- (GeneChar val) -> state & exec .~ es & char .~ val : view char state --- (GeneVectorInt val) -> state & exec .~ es & vectorInt .~ val : view vectorInt state --- (GeneVectorFloat val) -> state & exec .~ es & vectorFloat .~ val : view vectorFloat state --- (GeneVectorBool val) -> state & exec .~ es & vectorBool .~ val : view vectorBool state --- (GeneVectorString val) -> state & exec .~ es & vectorString .~ val : view vectorString state --- (GeneVectorChar val) -> state & exec .~ es & vectorChar .~ val : view vectorChar state --- (StateFunc (func, _)) -> func state {_exec = es} --- (Block block) -> (state {_exec = block ++ es}) --- (PlaceInput val) -> (state {_exec = (view input state Map.! val) : es}) --- Close -> undefined --- interpretOneStep state = state --- Need to make interpretExec strict, right? diff --git a/src/HushGP/State.hs b/src/HushGP/State.hs index acccbc2..62afec4 100644 --- a/src/HushGP/State.hs +++ b/src/HushGP/State.hs @@ -8,7 +8,7 @@ import Data.Map qualified as Map import GHC.Generics import Test.QuickCheck --- The exec stack must store heterogenous types, +-- |The exec stack must store heterogenous types, -- and we must be able to detect that type at runtime. -- One solution is for the exec stack to be a list of [Gene]. -- The parameter stack could be singular [Gene] or multiple [atomic] types. @@ -83,6 +83,7 @@ instance Arbitrary Gene where return Close ] +-- |The structure that holds all of the values. data State = State { _exec :: [Gene], _code :: [Gene], From 1155905be3a44426619dc4618091786ade84093b Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Mon, 10 Feb 2025 23:39:52 -0600 Subject: [PATCH 143/171] move moveable utility functions to utility file --- HushGP.cabal | 1 + src/HushGP/Instructions.hs | 996 +++++++++--------- src/HushGP/Instructions/BoolInstructions.hs | 14 +- src/HushGP/Instructions/CharInstructions.hs | 6 +- src/HushGP/Instructions/CodeInstructions.hs | 98 +- src/HushGP/Instructions/FloatInstructions.hs | 1 + .../Instructions/GenericInstructions.hs | 100 +- src/HushGP/Instructions/StringInstructions.hs | 23 +- src/HushGP/Instructions/Utility.hs | 249 +++++ src/HushGP/PushTests/UtilTests.hs | 2 +- 10 files changed, 755 insertions(+), 735 deletions(-) create mode 100644 src/HushGP/Instructions/Utility.hs diff --git a/HushGP.cabal b/HushGP.cabal index 5679300..49c8ce5 100644 --- a/HushGP.cabal +++ b/HushGP.cabal @@ -52,6 +52,7 @@ library , HushGP.Instructions.VectorStringInstructions , HushGP.Instructions.VectorBoolInstructions , HushGP.Instructions.VectorCharInstructions + , HushGP.Instructions.Utility , HushGP.PushTests , HushGP.PushTests.IntTests , HushGP.PushTests.GenericTests diff --git a/src/HushGP/Instructions.hs b/src/HushGP/Instructions.hs index 2e4d6a1..acfa87c 100644 --- a/src/HushGP/Instructions.hs +++ b/src/HushGP/Instructions.hs @@ -12,19 +12,19 @@ module HushGP.Instructions module HushGP.Instructions.VectorStringInstructions, module HushGP.Instructions.VectorBoolInstructions, module HushGP.Instructions.VectorCharInstructions, - allIntInstructions, - allFloatInstructions, - allBoolInstructions, - allCharInstructions, - allCodeInstructions, - allExecInstructions, - allStringInstructions, - allVectorIntInstructions, - allVectorFloatInstructions, - allVectorCharInstructions, - allVectorStringInstructions, - allVectorBoolInstructions, - allInstructions + -- allIntInstructions, + -- allFloatInstructions, + -- allBoolInstructions, + -- allCharInstructions, + -- allCodeInstructions, + -- allExecInstructions, + -- allStringInstructions, + -- allVectorIntInstructions, + -- allVectorFloatInstructions, + -- allVectorCharInstructions, + -- allVectorStringInstructions, + -- allVectorBoolInstructions, + -- allInstructions ) where @@ -41,501 +41,501 @@ import HushGP.Instructions.VectorFloatInstructions import HushGP.Instructions.VectorIntInstructions import HushGP.Instructions.VectorBoolInstructions import HushGP.Instructions.VectorStringInstructions -import HushGP.State +-- import HushGP.State -allIntInstructions :: [Gene] -allIntInstructions = map StateFunc [ - (instructionIntFromFloat, "instructionIntFromFloat"), - (instructionIntFromBool, "instructionIntFromBool"), - (instructionIntAdd, "instructionIntAdd"), - (instructionIntSub, "instructionIntSub"), - (instructionIntMul, "instructionIntMul"), - (instructionIntDiv, "instructionIntDiv"), - (instructionIntMod, "instructionIntMod"), - (instructionIntMin, "instructionIntMin"), - (instructionIntMax, "instructionIntMax"), - (instructionIntInc, "instructionIntInc"), - (instructionIntDec, "instructionIntDec"), - (instructionIntLT, "instructionIntLT"), - (instructionIntGT, "instructionIntGT"), - (instructionIntLTE, "instructionIntLTE"), - (instructionIntGTE, "instructionIntGTE"), - (instructionIntDup, "instructionIntDup"), - (instructionIntPop, "instructionIntPop"), - (instructionIntDupN, "instructionIntDupN"), - (instructionIntSwap, "instructionIntSwap"), - (instructionIntRot, "instructionIntRot"), - (instructionIntFlush, "instructionIntFlush"), - (instructionIntEq, "instructionIntEq"), - (instructionIntYank, "instructionIntYank"), - (instructionIntYankDup, "instructionIntYankDup"), - (instructionIntShove, "instructionIntShove"), - (instructionIntIsStackEmpty, "instructionIntIsStackEmpty"), - (instructionIntFromChar, "instructionIntFromChar"), - (instructionIntFromString, "instructionIntFromString"), - (instructionIntDupItems, "instructionIntDupItems") - ] +-- allIntInstructions :: [Gene] +-- allIntInstructions = map StateFunc [ +-- (instructionIntFromFloat, "instructionIntFromFloat"), +-- (instructionIntFromBool, "instructionIntFromBool"), +-- (instructionIntAdd, "instructionIntAdd"), +-- (instructionIntSub, "instructionIntSub"), +-- (instructionIntMul, "instructionIntMul"), +-- (instructionIntDiv, "instructionIntDiv"), +-- (instructionIntMod, "instructionIntMod"), +-- (instructionIntMin, "instructionIntMin"), +-- (instructionIntMax, "instructionIntMax"), +-- (instructionIntInc, "instructionIntInc"), +-- (instructionIntDec, "instructionIntDec"), +-- (instructionIntLT, "instructionIntLT"), +-- (instructionIntGT, "instructionIntGT"), +-- (instructionIntLTE, "instructionIntLTE"), +-- (instructionIntGTE, "instructionIntGTE"), +-- (instructionIntDup, "instructionIntDup"), +-- (instructionIntPop, "instructionIntPop"), +-- (instructionIntDupN, "instructionIntDupN"), +-- (instructionIntSwap, "instructionIntSwap"), +-- (instructionIntRot, "instructionIntRot"), +-- (instructionIntFlush, "instructionIntFlush"), +-- (instructionIntEq, "instructionIntEq"), +-- (instructionIntYank, "instructionIntYank"), +-- (instructionIntYankDup, "instructionIntYankDup"), +-- (instructionIntShove, "instructionIntShove"), +-- (instructionIntIsStackEmpty, "instructionIntIsStackEmpty"), +-- (instructionIntFromChar, "instructionIntFromChar"), +-- (instructionIntFromString, "instructionIntFromString"), +-- (instructionIntDupItems, "instructionIntDupItems") +-- ] -allFloatInstructions :: [Gene] -allFloatInstructions = map StateFunc [ - (instructionFloatFromInt, "instructionFloatFromInt"), - (instructionFloatFromBool, "instructionFloatFromBool"), - (instructionFloatAdd, "instructionFloatAdd"), - (instructionFloatSub, "instructionFloatSub"), - (instructionFloatMul, "instructionFloatMul"), - (instructionFloatDiv, "instructionFloatDiv"), - (instructionFloatMod, "instructionFloatMod"), - (instructionFloatMin, "instructionFloatMin"), - (instructionFloatMax, "instructionFloatMax"), - (instructionFloatInc, "instructionFloatInc"), - (instructionFloatDec, "instructionFloatDec"), - (instructionFloatLT, "instructionFloatLT"), - (instructionFloatGT, "instructionFloatGT"), - (instructionFloatLTE, "instructionFloatLTE"), - (instructionFloatGTE, "instructionFloatGTE"), - (instructionFloatDup, "instructionFloatDup"), - (instructionFloatPop, "instructionFloatPop"), - (instructionFloatDupN, "instructionFloatDupN"), - (instructionFloatSwap, "instructionFloatSwap"), - (instructionFloatRot, "instructionFloatRot"), - (instructionFloatFlush, "instructionFloatFlush"), - (instructionFloatEq, "instructionFloatEq"), - (instructionFloatYank, "instructionFloatYank"), - (instructionFloatYankDup, "instructionFloatYankDup"), - (instructionFloatShove, "instructionFloatShove"), - (instructionFloatIsStackEmpty, "instructionFloatIsStackEmpty"), - (instructionFloatFromChar, "instructionFloatFromChar"), - (instructionFloatFromString, "instructionFloatFromString"), - (instructionFloatDupItems, "instructionFloatDupItems") - ] +-- allFloatInstructions :: [Gene] +-- allFloatInstructions = map StateFunc [ +-- (instructionFloatFromInt, "instructionFloatFromInt"), +-- (instructionFloatFromBool, "instructionFloatFromBool"), +-- (instructionFloatAdd, "instructionFloatAdd"), +-- (instructionFloatSub, "instructionFloatSub"), +-- (instructionFloatMul, "instructionFloatMul"), +-- (instructionFloatDiv, "instructionFloatDiv"), +-- (instructionFloatMod, "instructionFloatMod"), +-- (instructionFloatMin, "instructionFloatMin"), +-- (instructionFloatMax, "instructionFloatMax"), +-- (instructionFloatInc, "instructionFloatInc"), +-- (instructionFloatDec, "instructionFloatDec"), +-- (instructionFloatLT, "instructionFloatLT"), +-- (instructionFloatGT, "instructionFloatGT"), +-- (instructionFloatLTE, "instructionFloatLTE"), +-- (instructionFloatGTE, "instructionFloatGTE"), +-- (instructionFloatDup, "instructionFloatDup"), +-- (instructionFloatPop, "instructionFloatPop"), +-- (instructionFloatDupN, "instructionFloatDupN"), +-- (instructionFloatSwap, "instructionFloatSwap"), +-- (instructionFloatRot, "instructionFloatRot"), +-- (instructionFloatFlush, "instructionFloatFlush"), +-- (instructionFloatEq, "instructionFloatEq"), +-- (instructionFloatYank, "instructionFloatYank"), +-- (instructionFloatYankDup, "instructionFloatYankDup"), +-- (instructionFloatShove, "instructionFloatShove"), +-- (instructionFloatIsStackEmpty, "instructionFloatIsStackEmpty"), +-- (instructionFloatFromChar, "instructionFloatFromChar"), +-- (instructionFloatFromString, "instructionFloatFromString"), +-- (instructionFloatDupItems, "instructionFloatDupItems") +-- ] -allBoolInstructions :: [Gene] -allBoolInstructions = map StateFunc [ - (instructionBoolFromInt, "instructionBoolFromInt"), - (instructionBoolFromFloat, "instructionBoolFromFloat"), - (instructionBoolAnd, "instructionBoolAnd"), - (instructionBoolInvertFirstThenAnd, "instructionBoolInvertFirstThenAnd"), - (instructionBoolInvertSecondThenAnd, "instructionBoolInvertSecondThenAnd"), - (instructionBoolOr, "instructionBoolOr"), - (instructionBoolXor, "instructionBoolXor"), - (instructionBoolPop, "instructionBoolPop"), - (instructionBoolDup, "instructionBoolDup"), - (instructionBoolDupN, "instructionBoolDupN"), - (instructionBoolSwap, "instructionBoolSwap"), - (instructionBoolRot, "instructionBoolRot"), - (instructionBoolFlush, "instructionBoolFlush"), - (instructionBoolEq, "instructionBoolEq"), - (instructionBoolStackDepth, "instructionBoolStackDepth"), - (instructionBoolYank, "instructionBoolYank"), - (instructionBoolYankDup, "instructionBoolYankDup"), - (instructionBoolShove, "instructionBoolShove"), - (instructionBoolShoveDup, "instructionBoolShoveDup"), - (instructionBoolIsStackEmpty, "instructionBoolIsStackEmpty"), - (instructionBoolDupItems, "instructionBoolDupItems") - ] +-- allBoolInstructions :: [Gene] +-- allBoolInstructions = map StateFunc [ +-- (instructionBoolFromInt, "instructionBoolFromInt"), +-- (instructionBoolFromFloat, "instructionBoolFromFloat"), +-- (instructionBoolAnd, "instructionBoolAnd"), +-- (instructionBoolInvertFirstThenAnd, "instructionBoolInvertFirstThenAnd"), +-- (instructionBoolInvertSecondThenAnd, "instructionBoolInvertSecondThenAnd"), +-- (instructionBoolOr, "instructionBoolOr"), +-- (instructionBoolXor, "instructionBoolXor"), +-- (instructionBoolPop, "instructionBoolPop"), +-- (instructionBoolDup, "instructionBoolDup"), +-- (instructionBoolDupN, "instructionBoolDupN"), +-- (instructionBoolSwap, "instructionBoolSwap"), +-- (instructionBoolRot, "instructionBoolRot"), +-- (instructionBoolFlush, "instructionBoolFlush"), +-- (instructionBoolEq, "instructionBoolEq"), +-- (instructionBoolStackDepth, "instructionBoolStackDepth"), +-- (instructionBoolYank, "instructionBoolYank"), +-- (instructionBoolYankDup, "instructionBoolYankDup"), +-- (instructionBoolShove, "instructionBoolShove"), +-- (instructionBoolShoveDup, "instructionBoolShoveDup"), +-- (instructionBoolIsStackEmpty, "instructionBoolIsStackEmpty"), +-- (instructionBoolDupItems, "instructionBoolDupItems") +-- ] -allCharInstructions :: [Gene] -allCharInstructions = map StateFunc [ - (instructionCharConcat, "instructionCharConcat"), - (instructionCharFromFirstChar, "instructionCharFromFirstChar"), - (instructionCharFromLastChar, "instructionCharFromLastChar"), - (instructionCharFromNthChar, "instructionCharFromNthChar"), - (instructionCharIsWhitespace, "instructionCharIsWhitespace"), - (instructionCharIsLetter, "instructionCharIsLetter"), - (instructionCharIsDigit, "instructionCharIsDigit"), - (instructionCharFromBool, "instructionCharFromBool"), - (instructionCharFromAsciiInt, "instructionCharFromAsciiInt"), - (instructionCharFromAsciiFloat, "instructionCharFromAsciiFloat"), - (instructionCharsFromString, "instructionCharsFromString"), - (instructionCharPop, "instructionCharPop"), - (instructionCharDup, "instructionCharDup"), - (instructionCharDupN, "instructionCharDupN"), - (instructionCharSwap, "instructionCharSwap"), - (instructionCharRot, "instructionCharRot"), - (instructionCharFlush, "instructionCharFlush"), - (instructionCharEq, "instructionCharEq"), - (instructionCharStackDepth, "instructionCharStackDepth"), - (instructionCharYank, "instructionCharYank"), - (instructionCharYankDup, "instructionCharYankDup"), - (instructionCharShove, "instructionCharShove"), - (instructionCharShoveDup, "instructionCharShoveDup"), - (instructionCharIsStackEmpty, "instructionCharIsStackEmpty"), - (instructionCharDupItems, "instructionCharDupItems") - ] +-- allCharInstructions :: [Gene] +-- allCharInstructions = map StateFunc [ +-- (instructionCharConcat, "instructionCharConcat"), +-- (instructionCharFromFirstChar, "instructionCharFromFirstChar"), +-- (instructionCharFromLastChar, "instructionCharFromLastChar"), +-- (instructionCharFromNthChar, "instructionCharFromNthChar"), +-- (instructionCharIsWhitespace, "instructionCharIsWhitespace"), +-- (instructionCharIsLetter, "instructionCharIsLetter"), +-- (instructionCharIsDigit, "instructionCharIsDigit"), +-- (instructionCharFromBool, "instructionCharFromBool"), +-- (instructionCharFromAsciiInt, "instructionCharFromAsciiInt"), +-- (instructionCharFromAsciiFloat, "instructionCharFromAsciiFloat"), +-- (instructionCharsFromString, "instructionCharsFromString"), +-- (instructionCharPop, "instructionCharPop"), +-- (instructionCharDup, "instructionCharDup"), +-- (instructionCharDupN, "instructionCharDupN"), +-- (instructionCharSwap, "instructionCharSwap"), +-- (instructionCharRot, "instructionCharRot"), +-- (instructionCharFlush, "instructionCharFlush"), +-- (instructionCharEq, "instructionCharEq"), +-- (instructionCharStackDepth, "instructionCharStackDepth"), +-- (instructionCharYank, "instructionCharYank"), +-- (instructionCharYankDup, "instructionCharYankDup"), +-- (instructionCharShove, "instructionCharShove"), +-- (instructionCharShoveDup, "instructionCharShoveDup"), +-- (instructionCharIsStackEmpty, "instructionCharIsStackEmpty"), +-- (instructionCharDupItems, "instructionCharDupItems") +-- ] -allCodeInstructions :: [Gene] -allCodeInstructions = map StateFunc [ - (instructionCodePop, "instructionCodePop"), - (instructionCodeIsCodeBlock, "instructionCodeIsCodeBlock"), - (instructionCodeIsSingular, "instructionCodeIsSingular"), - (instructionCodeLength, "instructionCodeLength"), - (instructionCodeFirst, "instructionCodeFirst"), - (instructionCodeLast, "instructionCodeLast"), - (instructionCodeTail, "instructionCodeTail"), - (instructionCodeInit, "instructionCodeInit"), - (instructionCodeWrap, "instructionCodeWrap"), - (instructionCodeList, "instructionCodeList"), - (instructionCodeCombine, "instructionCodeCombine"), - (instructionCodeDo, "instructionCodeDo"), - (instructionCodeDoDup, "instructionCodeDoDup"), - (instructionCodeDoThenPop, "instructionCodeDoThenPop"), - (instructionCodeDoRange, "instructionCodeDoRange"), - (instructionCodeDoCount, "instructionCodeDoCount"), - (instructionCodeDoTimes, "instructionCodeDoTimes"), - (instructionCodeIf, "instructionCodeIf"), - (instructionCodeWhen, "instructionCodeWhen"), - (instructionCodeMember, "instructionCodeMember"), - (instructionCodeN, "instructionCodeN"), - (instructionMakeEmptyCodeBlock, "instructionMakeEmptyCodeBlock"), - (instructionIsEmptyCodeBlock, "instructionIsEmptyCodeBlock"), - (instructionCodeSize, "instructionCodeSize"), - (instructionCodeExtract, "instructionCodeExtract"), - (instructionCodeInsert, "instructionCodeInsert"), - (instructionCodeFirstPosition, "instructionCodeFirstPosition"), - (instructionCodeReverse, "instructionCodeReverse"), - (instructionCodeDup, "instructionCodeDup"), - (instructionCodeDupN, "instructionCodeDupN"), - (instructionCodeDup, "instructionCodeDup"), - (instructionCodeDupN, "instructionCodeDupN"), - (instructionCodeSwap, "instructionCodeSwap"), - (instructionCodeRot, "instructionCodeRot"), - (instructionCodeFlush, "instructionCodeFlush"), - (instructionCodeEq, "instructionCodeEq"), - (instructionCodeStackDepth, "instructionCodeStackDepth"), - (instructionCodeYank, "instructionCodeYank"), - (instructionCodeYankDup, "instructionCodeYankDup"), - (instructionCodeShove, "instructionCodeShove"), - (instructionCodeShoveDup, "instructionCodeShoveDup"), - (instructionCodeIsStackEmpty, "instructionCodeIsStackEmpty"), - (instructionCodeFromBool, "instructionCodeFromBool"), - (instructionCodeFromInt, "instructionCodeFromInt"), - (instructionCodeFromChar, "instructionCodeFromChar"), - (instructionCodeFromFloat, "instructionCodeFromFloat"), - (instructionCodeFromString, "instructionCodeFromString"), - (instructionCodeFromVectorInt, "instructionCodeFromVectorInt"), - (instructionCodeFromVectorFloat, "instructionCodeFromVectorFloat"), - (instructionCodeFromVectorString, "instructionCodeFromVectorString"), - (instructionCodeFromVectorBool, "instructionCodeFromVectorBool"), - (instructionCodeFromVectorChar, "instructionCodeFromVectorChar"), - (instructionCodeFromExec, "instructionCodeFromExec"), - (instructionCodeContainer, "instructionCodeContainer"), - (instructionCodeDiscrepancy, "instructionCodeDiscrepancy"), - (instructionCodeNoOp, "instructionCodeNoOp"), - (instructionCodeTailN, "instructionCodeTailN"), - (instructionCodeDupItems, "instructionCodeDupItems") - ] +-- allCodeInstructions :: [Gene] +-- allCodeInstructions = map StateFunc [ +-- (instructionCodePop, "instructionCodePop"), +-- (instructionCodeIsCodeBlock, "instructionCodeIsCodeBlock"), +-- (instructionCodeIsSingular, "instructionCodeIsSingular"), +-- (instructionCodeLength, "instructionCodeLength"), +-- (instructionCodeFirst, "instructionCodeFirst"), +-- (instructionCodeLast, "instructionCodeLast"), +-- (instructionCodeTail, "instructionCodeTail"), +-- (instructionCodeInit, "instructionCodeInit"), +-- (instructionCodeWrap, "instructionCodeWrap"), +-- (instructionCodeList, "instructionCodeList"), +-- (instructionCodeCombine, "instructionCodeCombine"), +-- (instructionCodeDo, "instructionCodeDo"), +-- (instructionCodeDoDup, "instructionCodeDoDup"), +-- (instructionCodeDoThenPop, "instructionCodeDoThenPop"), +-- (instructionCodeDoRange, "instructionCodeDoRange"), +-- (instructionCodeDoCount, "instructionCodeDoCount"), +-- (instructionCodeDoTimes, "instructionCodeDoTimes"), +-- (instructionCodeIf, "instructionCodeIf"), +-- (instructionCodeWhen, "instructionCodeWhen"), +-- (instructionCodeMember, "instructionCodeMember"), +-- (instructionCodeN, "instructionCodeN"), +-- (instructionMakeEmptyCodeBlock, "instructionMakeEmptyCodeBlock"), +-- (instructionIsEmptyCodeBlock, "instructionIsEmptyCodeBlock"), +-- (instructionCodeSize, "instructionCodeSize"), +-- (instructionCodeExtract, "instructionCodeExtract"), +-- (instructionCodeInsert, "instructionCodeInsert"), +-- (instructionCodeFirstPosition, "instructionCodeFirstPosition"), +-- (instructionCodeReverse, "instructionCodeReverse"), +-- (instructionCodeDup, "instructionCodeDup"), +-- (instructionCodeDupN, "instructionCodeDupN"), +-- (instructionCodeDup, "instructionCodeDup"), +-- (instructionCodeDupN, "instructionCodeDupN"), +-- (instructionCodeSwap, "instructionCodeSwap"), +-- (instructionCodeRot, "instructionCodeRot"), +-- (instructionCodeFlush, "instructionCodeFlush"), +-- (instructionCodeEq, "instructionCodeEq"), +-- (instructionCodeStackDepth, "instructionCodeStackDepth"), +-- (instructionCodeYank, "instructionCodeYank"), +-- (instructionCodeYankDup, "instructionCodeYankDup"), +-- (instructionCodeShove, "instructionCodeShove"), +-- (instructionCodeShoveDup, "instructionCodeShoveDup"), +-- (instructionCodeIsStackEmpty, "instructionCodeIsStackEmpty"), +-- (instructionCodeFromBool, "instructionCodeFromBool"), +-- (instructionCodeFromInt, "instructionCodeFromInt"), +-- (instructionCodeFromChar, "instructionCodeFromChar"), +-- (instructionCodeFromFloat, "instructionCodeFromFloat"), +-- (instructionCodeFromString, "instructionCodeFromString"), +-- (instructionCodeFromVectorInt, "instructionCodeFromVectorInt"), +-- (instructionCodeFromVectorFloat, "instructionCodeFromVectorFloat"), +-- (instructionCodeFromVectorString, "instructionCodeFromVectorString"), +-- (instructionCodeFromVectorBool, "instructionCodeFromVectorBool"), +-- (instructionCodeFromVectorChar, "instructionCodeFromVectorChar"), +-- (instructionCodeFromExec, "instructionCodeFromExec"), +-- (instructionCodeContainer, "instructionCodeContainer"), +-- (instructionCodeDiscrepancy, "instructionCodeDiscrepancy"), +-- (instructionCodeNoOp, "instructionCodeNoOp"), +-- (instructionCodeTailN, "instructionCodeTailN"), +-- (instructionCodeDupItems, "instructionCodeDupItems") +-- ] -allExecInstructions :: [Gene] -allExecInstructions = map StateFunc [ - (instructionExecIf, "instructionExecIf"), - (instructionExecDup, "instructionExecDup"), - (instructionExecDupN, "instructionExecDupN"), - (instructionExecPop, "instructionExecPop"), - (instructionExecSwap, "instructionExecSwap"), - (instructionExecRot, "instructionExecRot"), - (instructionExecFlush, "instructionExecFlush"), - (instructionExecEq, "instructionExecEq"), - (instructionExecStackDepth, "instructionExecStackDepth"), - (instructionExecYank, "instructionExecYank"), - (instructionExecYankDup, "instructionExecYankDup"), - (instructionExecShove, "instructionExecShove"), - (instructionExecShoveDup, "instructionExecShoveDup"), - (instructionExecIsStackEmpty, "instructionExecIsStackEmpty"), - (instructionExecDoRange, "instructionExecDoRange"), - (instructionExecDoCount, "instructionExecDoCount"), - (instructionExecDoTimes, "instructionExecDoTimes"), - (instructionExecWhile, "instructionExecWhile"), - (instructionExecDoWhile, "instructionExecDoWhile"), - (instructionExecWhen, "instructionExecWhen"), - (instructionExecK, "instructionExecK"), - (instructionExecS, "instructionExecS"), - (instructionExecY, "instrucitonExecY"), - (instructionExecDupItems, "instructionExecDupItems") - ] +-- allExecInstructions :: [Gene] +-- allExecInstructions = map StateFunc [ +-- (instructionExecIf, "instructionExecIf"), +-- (instructionExecDup, "instructionExecDup"), +-- (instructionExecDupN, "instructionExecDupN"), +-- (instructionExecPop, "instructionExecPop"), +-- (instructionExecSwap, "instructionExecSwap"), +-- (instructionExecRot, "instructionExecRot"), +-- (instructionExecFlush, "instructionExecFlush"), +-- (instructionExecEq, "instructionExecEq"), +-- (instructionExecStackDepth, "instructionExecStackDepth"), +-- (instructionExecYank, "instructionExecYank"), +-- (instructionExecYankDup, "instructionExecYankDup"), +-- (instructionExecShove, "instructionExecShove"), +-- (instructionExecShoveDup, "instructionExecShoveDup"), +-- (instructionExecIsStackEmpty, "instructionExecIsStackEmpty"), +-- (instructionExecDoRange, "instructionExecDoRange"), +-- (instructionExecDoCount, "instructionExecDoCount"), +-- (instructionExecDoTimes, "instructionExecDoTimes"), +-- (instructionExecWhile, "instructionExecWhile"), +-- (instructionExecDoWhile, "instructionExecDoWhile"), +-- (instructionExecWhen, "instructionExecWhen"), +-- (instructionExecK, "instructionExecK"), +-- (instructionExecS, "instructionExecS"), +-- (instructionExecY, "instrucitonExecY"), +-- (instructionExecDupItems, "instructionExecDupItems") +-- ] -allStringInstructions :: [Gene] -allStringInstructions = map StateFunc [ - (instructionStringConcat, "instructionStringConcat"), - (instructionStringSwap, "instructionStringSwap"), - (instructionStringInsertString, "instructionStringInsertString"), - (instructionStringFromFirstChar, "instructionStringFromFirstChar"), - (instructionStringFromLastChar, "instructionStringFromLastChar"), - (instructionStringFromNthChar, "instructionStringFromNthChar"), - (instructionStringIndexOfString, "instructionStringIndexOfString"), - (instructionStringContainsString, "instructionStringContainsString"), - (instructionStringSplitOnString, "instructionStringSplitOnString"), - (instructionStringReplaceFirstString, "instructionStringReplaceFirstString"), - (instructionStringReplaceNString, "instructionStringReplaceNString"), - (instructionStringReplaceAllString, "instructionStringReplaceAllString"), - (instructionStringRemoveFirstString, "instructionStringRemoveFirstString"), - (instructionStringRemoveNString, "instructionStringRemoveNString"), - (instructionStringRemoveAllString, "instructionStringRemoveAllString"), - (instructionStringOccurrencesOfString, "instructionStringOccurrencesOfString"), - (instructionStringInsertChar, "instructionStringInsertChar"), - (instructionStringContainsChar, "instructionStringContainsChar"), - (instructionStringIndexOfChar, "instructionStringIndexOfChar"), - (instructionStringSplitOnChar, "instructionStringSplitOnChar"), - (instructionStringReplaceFirstChar, "instructionStringReplaceFirstChar"), - (instructionStringReplaceNChar, "instructionStringReplaceNChar"), - (instructionStringReplaceAllChar, "instructionStringReplaceAllChar"), - (instructionStringRemoveFirstChar, "instructionStringRemoveFirstChar"), - (instructionStringRemoveNChar, "instructionStringRemoveNChar"), - (instructionStringRemoveAllChar, "instructionStringRemoveAllChar"), - (instructionStringOccurrencesOfChar, "instructionStringOccurrencesOfChar"), - (instructionStringReverse, "instructionStringReverse"), - (instructionStringHead, "instructionStringHead"), - (instructionStringTail, "instructionStringTail"), - (instructionStringAppendChar, "instructionStringAppendChar"), - (instructionStringRest, "instructionStringRest"), - (instructionStringButLast, "instructionStringButLast"), - (instructionStringDrop, "instructionStringDrop"), - (instructionStringButLastN, "instructionStringButLastN"), - (instructionStringLength, "instructionStringLength"), - (instructionStringMakeEmpty, "instructionStringMakeEmpty"), - (instructionStringIsEmptyString, "instructionStringIsEmptyString"), - (instructionStringRemoveNth, "instructionStringRemoveNth"), - (instructionStringSetNth, "instructionStringSetNth"), - (instructionStringStripWhitespace, "instructionStringStripWhitespace"), - (instructionStringFromBool, "instructionStringFromBool"), - (instructionStringFromInt, "instructionStringFromInt"), - (instructionStringFromFloat, "instructionStringFromFloat"), - (instructionStringFromChar, "instructionStringFromChar"), - (instructionStringPop, "instructionStringPop"), - (instructionStringDup, "instructionStringDup"), - (instructionStringDupN, "instructionStringDupN"), - (instructionStringSwap, "instructionStringSwap"), - (instructionStringRot, "instructionStringRot"), - (instructionStringFlush, "instructionStringFlush"), - (instructionStringEq, "instructionStringEq"), - (instructionStringStackDepth, "instructionStringStackDepth"), - (instructionStringYank, "instructionStringYank"), - (instructionStringYankDup, "instructionStringYankDup"), - (instructionStringShove, "instructionStringShove"), - (instructionStringShoveDup, "instructionStringShoveDup"), - (instructionStringIsStackEmpty, "instructionStringIsStackEmpty"), - (instructionStringSort, "instructionStringSort"), - (instructionStringSortReverse, "instructionStringSortReverse"), - (instructionStringDupItems, "instructionStringDupItems"), - (instructionStringParseToChar, "instructionStringParseToChar"), - (instructionStringSubString, "instructionStringSubString") - ] +-- allStringInstructions :: [Gene] +-- allStringInstructions = map StateFunc [ +-- (instructionStringConcat, "instructionStringConcat"), +-- (instructionStringSwap, "instructionStringSwap"), +-- (instructionStringInsertString, "instructionStringInsertString"), +-- (instructionStringFromFirstChar, "instructionStringFromFirstChar"), +-- (instructionStringFromLastChar, "instructionStringFromLastChar"), +-- (instructionStringFromNthChar, "instructionStringFromNthChar"), +-- (instructionStringIndexOfString, "instructionStringIndexOfString"), +-- (instructionStringContainsString, "instructionStringContainsString"), +-- (instructionStringSplitOnString, "instructionStringSplitOnString"), +-- (instructionStringReplaceFirstString, "instructionStringReplaceFirstString"), +-- (instructionStringReplaceNString, "instructionStringReplaceNString"), +-- (instructionStringReplaceAllString, "instructionStringReplaceAllString"), +-- (instructionStringRemoveFirstString, "instructionStringRemoveFirstString"), +-- (instructionStringRemoveNString, "instructionStringRemoveNString"), +-- (instructionStringRemoveAllString, "instructionStringRemoveAllString"), +-- (instructionStringOccurrencesOfString, "instructionStringOccurrencesOfString"), +-- (instructionStringInsertChar, "instructionStringInsertChar"), +-- (instructionStringContainsChar, "instructionStringContainsChar"), +-- (instructionStringIndexOfChar, "instructionStringIndexOfChar"), +-- (instructionStringSplitOnChar, "instructionStringSplitOnChar"), +-- (instructionStringReplaceFirstChar, "instructionStringReplaceFirstChar"), +-- (instructionStringReplaceNChar, "instructionStringReplaceNChar"), +-- (instructionStringReplaceAllChar, "instructionStringReplaceAllChar"), +-- (instructionStringRemoveFirstChar, "instructionStringRemoveFirstChar"), +-- (instructionStringRemoveNChar, "instructionStringRemoveNChar"), +-- (instructionStringRemoveAllChar, "instructionStringRemoveAllChar"), +-- (instructionStringOccurrencesOfChar, "instructionStringOccurrencesOfChar"), +-- (instructionStringReverse, "instructionStringReverse"), +-- (instructionStringHead, "instructionStringHead"), +-- (instructionStringTail, "instructionStringTail"), +-- (instructionStringAppendChar, "instructionStringAppendChar"), +-- (instructionStringRest, "instructionStringRest"), +-- (instructionStringButLast, "instructionStringButLast"), +-- (instructionStringDrop, "instructionStringDrop"), +-- (instructionStringButLastN, "instructionStringButLastN"), +-- (instructionStringLength, "instructionStringLength"), +-- (instructionStringMakeEmpty, "instructionStringMakeEmpty"), +-- (instructionStringIsEmptyString, "instructionStringIsEmptyString"), +-- (instructionStringRemoveNth, "instructionStringRemoveNth"), +-- (instructionStringSetNth, "instructionStringSetNth"), +-- (instructionStringStripWhitespace, "instructionStringStripWhitespace"), +-- (instructionStringFromBool, "instructionStringFromBool"), +-- (instructionStringFromInt, "instructionStringFromInt"), +-- (instructionStringFromFloat, "instructionStringFromFloat"), +-- (instructionStringFromChar, "instructionStringFromChar"), +-- (instructionStringPop, "instructionStringPop"), +-- (instructionStringDup, "instructionStringDup"), +-- (instructionStringDupN, "instructionStringDupN"), +-- (instructionStringSwap, "instructionStringSwap"), +-- (instructionStringRot, "instructionStringRot"), +-- (instructionStringFlush, "instructionStringFlush"), +-- (instructionStringEq, "instructionStringEq"), +-- (instructionStringStackDepth, "instructionStringStackDepth"), +-- (instructionStringYank, "instructionStringYank"), +-- (instructionStringYankDup, "instructionStringYankDup"), +-- (instructionStringShove, "instructionStringShove"), +-- (instructionStringShoveDup, "instructionStringShoveDup"), +-- (instructionStringIsStackEmpty, "instructionStringIsStackEmpty"), +-- (instructionStringSort, "instructionStringSort"), +-- (instructionStringSortReverse, "instructionStringSortReverse"), +-- (instructionStringDupItems, "instructionStringDupItems"), +-- (instructionStringParseToChar, "instructionStringParseToChar"), +-- (instructionStringSubString, "instructionStringSubString") +-- ] -allVectorIntInstructions :: [Gene] -allVectorIntInstructions = map StateFunc [ - (instructionVectorIntConcat, "instructionVectorIntConcat"), - (instructionVectorIntConj, "instructionVectorIntConj"), - (instructionVectorIntTakeN, "instructionVectorIntTakeN"), - (instructionVectorIntSubVector, "instructionVectorIntSubVector"), - (instructionVectorIntFirst, "instructionVectorIntFirst"), - (instructionVectorIntLast, "instructionVectorIntLast"), - (instructionVectorIntNth, "instructionVectorIntNth"), - (instructionVectorIntRest, "instructionVectorIntRest"), - (instructionVectorIntButLast, "instructionVectorIntButLast"), - (instructionVectorIntLength, "instructionVectorIntLength"), - (instructionVectorIntReverse, "instructionVectorIntReverse"), - (instructionVectorIntPushAll, "instructionVectorIntPushAll"), - (instructionVectorIntMakeEmpty, "instructionVectorIntMakeEmpty"), - (instructionVectorIntIsEmpty, "instructionVectorIntIsEmpty"), - (instructionVectorIntIndexOf, "instructionVectorIntIndexOf"), - (instructionVectorIntOccurrencesOf, "instructionVectorIntOccurrencesOf"), - (instructionVectorIntSetNth, "instructionVectorIntSetNth"), - (instructionVectorIntReplace, "instructionVectorIntReplace"), - (instructionVectorIntReplaceFirst, "instructionVectorIntReplaceFirst"), - (instructionVectorIntRemove, "instructionVectorIntRemove"), - (instructionVectorIntIterate, "instructionVectorIntIterate"), - (instructionVectorIntPop, "instructionVectorIntPop"), - (instructionVectorIntDup, "instructionVectorIntDup"), - (instructionVectorIntDupN, "instructionVectorIntDupN"), - (instructionVectorIntSwap, "instructionVectorIntSwap"), - (instructionVectorIntRot, "instructionVectorIntRot"), - (instructionVectorIntFlush, "instructionVectorIntFlush"), - (instructionVectorIntEq, "instructionVectorIntEq"), - (instructionVectorIntStackDepth, "instructionVectorIntStackDepth"), - (instructionVectorIntYank, "instructionVectorIntYank"), - (instructionVectorIntYankDup, "instructionVectorIntYankDup"), - (instructionVectorIntShove, "instructionVectorIntShove"), - (instructionVectorIntShoveDup, "instructionVectorIntShoveDup"), - (instructionVectorIntIsStackEmpty, "instructionVectorIntIsStackEmpty"), - (instructionVectorIntSort, "instructionVectorIntSort"), - (instructionVectorIntSortReverse, "instructionVectorIntSortReverse"), - (instructionVectorIntDupItems, "instructionVectorIntDupItems") - ] +-- allVectorIntInstructions :: [Gene] +-- allVectorIntInstructions = map StateFunc [ +-- (instructionVectorIntConcat, "instructionVectorIntConcat"), +-- (instructionVectorIntConj, "instructionVectorIntConj"), +-- (instructionVectorIntTakeN, "instructionVectorIntTakeN"), +-- (instructionVectorIntSubVector, "instructionVectorIntSubVector"), +-- (instructionVectorIntFirst, "instructionVectorIntFirst"), +-- (instructionVectorIntLast, "instructionVectorIntLast"), +-- (instructionVectorIntNth, "instructionVectorIntNth"), +-- (instructionVectorIntRest, "instructionVectorIntRest"), +-- (instructionVectorIntButLast, "instructionVectorIntButLast"), +-- (instructionVectorIntLength, "instructionVectorIntLength"), +-- (instructionVectorIntReverse, "instructionVectorIntReverse"), +-- (instructionVectorIntPushAll, "instructionVectorIntPushAll"), +-- (instructionVectorIntMakeEmpty, "instructionVectorIntMakeEmpty"), +-- (instructionVectorIntIsEmpty, "instructionVectorIntIsEmpty"), +-- (instructionVectorIntIndexOf, "instructionVectorIntIndexOf"), +-- (instructionVectorIntOccurrencesOf, "instructionVectorIntOccurrencesOf"), +-- (instructionVectorIntSetNth, "instructionVectorIntSetNth"), +-- (instructionVectorIntReplace, "instructionVectorIntReplace"), +-- (instructionVectorIntReplaceFirst, "instructionVectorIntReplaceFirst"), +-- (instructionVectorIntRemove, "instructionVectorIntRemove"), +-- (instructionVectorIntIterate, "instructionVectorIntIterate"), +-- (instructionVectorIntPop, "instructionVectorIntPop"), +-- (instructionVectorIntDup, "instructionVectorIntDup"), +-- (instructionVectorIntDupN, "instructionVectorIntDupN"), +-- (instructionVectorIntSwap, "instructionVectorIntSwap"), +-- (instructionVectorIntRot, "instructionVectorIntRot"), +-- (instructionVectorIntFlush, "instructionVectorIntFlush"), +-- (instructionVectorIntEq, "instructionVectorIntEq"), +-- (instructionVectorIntStackDepth, "instructionVectorIntStackDepth"), +-- (instructionVectorIntYank, "instructionVectorIntYank"), +-- (instructionVectorIntYankDup, "instructionVectorIntYankDup"), +-- (instructionVectorIntShove, "instructionVectorIntShove"), +-- (instructionVectorIntShoveDup, "instructionVectorIntShoveDup"), +-- (instructionVectorIntIsStackEmpty, "instructionVectorIntIsStackEmpty"), +-- (instructionVectorIntSort, "instructionVectorIntSort"), +-- (instructionVectorIntSortReverse, "instructionVectorIntSortReverse"), +-- (instructionVectorIntDupItems, "instructionVectorIntDupItems") +-- ] -allVectorFloatInstructions :: [Gene] -allVectorFloatInstructions = map StateFunc [ - (instructionVectorFloatConcat, "instructionVectorFloatConcat"), - (instructionVectorFloatConj, "instructionVectorFloatConj"), - (instructionVectorFloatTakeN, "instructionVectorFloatTakeN"), - (instructionVectorFloatSubVector, "instructionVectorFloatSubVector"), - (instructionVectorFloatFirst, "instructionVectorFloatFirst"), - (instructionVectorFloatLast, "instructionVectorFloatLast"), - (instructionVectorFloatNth, "instructionVectorFloatNth"), - (instructionVectorFloatRest, "instructionVectorFloatRest"), - (instructionVectorFloatButLast, "instructionVectorFloatButLast"), - (instructionVectorFloatLength, "instructionVectorFloatLength"), - (instructionVectorFloatReverse, "instructionVectorFloatReverse"), - (instructionVectorFloatPushAll, "instructionVectorFloatPushAll"), - (instructionVectorFloatMakeEmpty, "instructionVectorFloatMakeEmpty"), - (instructionVectorFloatIsEmpty, "instructionVectorFloatIsEmpty"), - (instructionVectorFloatIndexOf, "instructionVectorFloatIndexOf"), - (instructionVectorFloatOccurrencesOf, "instructionVectorFloatOccurrencesOf"), - (instructionVectorFloatSetNth, "instructionVectorFloatSetNth"), - (instructionVectorFloatReplace, "instructionVectorFloatReplace"), - (instructionVectorFloatReplaceFirst, "instructionVectorFloatReplaceFirst"), - (instructionVectorFloatRemove, "instructionVectorFloatRemove"), - (instructionVectorFloatIterate, "instructionVectorFloatIterate"), - (instructionVectorFloatPop, "instructionVectorFloatPop"), - (instructionVectorFloatDup, "instructionVectorFloatDup"), - (instructionVectorFloatDupN, "instructionVectorFloatDupN"), - (instructionVectorFloatSwap, "instructionVectorFloatSwap"), - (instructionVectorFloatRot, "instructionVectorFloatRot"), - (instructionVectorFloatFlush, "instructionVectorFloatFlush"), - (instructionVectorFloatEq, "instructionVectorFloatEq"), - (instructionVectorFloatStackDepth, "instructionVectorFloatStackDepth"), - (instructionVectorFloatYank, "instructionVectorFloatYank"), - (instructionVectorFloatYankDup, "instructionVectorFloatYankDup"), - (instructionVectorFloatShove, "instructionVectorFloatShove"), - (instructionVectorFloatShoveDup, "instructionVectorFloatShoveDup"), - (instructionVectorFloatIsStackEmpty, "instructionVectorFloatIsStackEmpty"), - (instructionVectorFloatSort, "instructionVectorFloatSort"), - (instructionVectorFloatSortReverse, "instructionVectorFloatSortReverse"), - (instructionVectorFloatDupItems, "instructionVectorFloatDupItems") - ] +-- allVectorFloatInstructions :: [Gene] +-- allVectorFloatInstructions = map StateFunc [ +-- (instructionVectorFloatConcat, "instructionVectorFloatConcat"), +-- (instructionVectorFloatConj, "instructionVectorFloatConj"), +-- (instructionVectorFloatTakeN, "instructionVectorFloatTakeN"), +-- (instructionVectorFloatSubVector, "instructionVectorFloatSubVector"), +-- (instructionVectorFloatFirst, "instructionVectorFloatFirst"), +-- (instructionVectorFloatLast, "instructionVectorFloatLast"), +-- (instructionVectorFloatNth, "instructionVectorFloatNth"), +-- (instructionVectorFloatRest, "instructionVectorFloatRest"), +-- (instructionVectorFloatButLast, "instructionVectorFloatButLast"), +-- (instructionVectorFloatLength, "instructionVectorFloatLength"), +-- (instructionVectorFloatReverse, "instructionVectorFloatReverse"), +-- (instructionVectorFloatPushAll, "instructionVectorFloatPushAll"), +-- (instructionVectorFloatMakeEmpty, "instructionVectorFloatMakeEmpty"), +-- (instructionVectorFloatIsEmpty, "instructionVectorFloatIsEmpty"), +-- (instructionVectorFloatIndexOf, "instructionVectorFloatIndexOf"), +-- (instructionVectorFloatOccurrencesOf, "instructionVectorFloatOccurrencesOf"), +-- (instructionVectorFloatSetNth, "instructionVectorFloatSetNth"), +-- (instructionVectorFloatReplace, "instructionVectorFloatReplace"), +-- (instructionVectorFloatReplaceFirst, "instructionVectorFloatReplaceFirst"), +-- (instructionVectorFloatRemove, "instructionVectorFloatRemove"), +-- (instructionVectorFloatIterate, "instructionVectorFloatIterate"), +-- (instructionVectorFloatPop, "instructionVectorFloatPop"), +-- (instructionVectorFloatDup, "instructionVectorFloatDup"), +-- (instructionVectorFloatDupN, "instructionVectorFloatDupN"), +-- (instructionVectorFloatSwap, "instructionVectorFloatSwap"), +-- (instructionVectorFloatRot, "instructionVectorFloatRot"), +-- (instructionVectorFloatFlush, "instructionVectorFloatFlush"), +-- (instructionVectorFloatEq, "instructionVectorFloatEq"), +-- (instructionVectorFloatStackDepth, "instructionVectorFloatStackDepth"), +-- (instructionVectorFloatYank, "instructionVectorFloatYank"), +-- (instructionVectorFloatYankDup, "instructionVectorFloatYankDup"), +-- (instructionVectorFloatShove, "instructionVectorFloatShove"), +-- (instructionVectorFloatShoveDup, "instructionVectorFloatShoveDup"), +-- (instructionVectorFloatIsStackEmpty, "instructionVectorFloatIsStackEmpty"), +-- (instructionVectorFloatSort, "instructionVectorFloatSort"), +-- (instructionVectorFloatSortReverse, "instructionVectorFloatSortReverse"), +-- (instructionVectorFloatDupItems, "instructionVectorFloatDupItems") +-- ] -allVectorCharInstructions :: [Gene] -allVectorCharInstructions = map StateFunc [ - (instructionVectorCharConcat, "instructionVectorCharConcat"), - (instructionVectorCharConj, "instructionVectorCharConj"), - (instructionVectorCharTakeN, "instructionVectorCharTakeN"), - (instructionVectorCharSubVector, "instructionVectorCharSubVector"), - (instructionVectorCharFirst, "instructionVectorCharFirst"), - (instructionVectorCharLast, "instructionVectorCharLast"), - (instructionVectorCharNth, "instructionVectorCharNth"), - (instructionVectorCharRest, "instructionVectorCharRest"), - (instructionVectorCharButLast, "instructionVectorCharButLast"), - (instructionVectorCharLength, "instructionVectorCharLength"), - (instructionVectorCharReverse, "instructionVectorCharReverse"), - (instructionVectorCharPushAll, "instructionVectorCharPushAll"), - (instructionVectorCharMakeEmpty, "instructionVectorCharMakeEmpty"), - (instructionVectorCharIsEmpty, "instructionVectorCharIsEmpty"), - (instructionVectorCharIndexOf, "instructionVectorCharIndexOf"), - (instructionVectorCharOccurrencesOf, "instructionVectorCharOccurrencesOf"), - (instructionVectorCharSetNth, "instructionVectorCharSetNth"), - (instructionVectorCharReplace, "instructionVectorCharReplace"), - (instructionVectorCharReplaceFirst, "instructionVectorCharReplaceFirst"), - (instructionVectorCharRemove, "instructionVectorCharRemove"), - (instructionVectorCharIterate, "instructionVectorCharIterate"), - (instructionVectorCharPop, "instructionVectorCharPop"), - (instructionVectorCharDup, "instructionVectorCharDup"), - (instructionVectorCharDupN, "instructionVectorCharDupN"), - (instructionVectorCharSwap, "instructionVectorCharSwap"), - (instructionVectorCharRot, "instructionVectorCharRot"), - (instructionVectorCharFlush, "instructionVectorCharFlush"), - (instructionVectorCharEq, "instructionVectorCharEq"), - (instructionVectorCharStackDepth, "instructionVectorCharStackDepth"), - (instructionVectorCharYank, "instructionVectorCharYank"), - (instructionVectorCharYankDup, "instructionVectorCharYankDup"), - (instructionVectorCharShove, "instructionVectorCharShove"), - (instructionVectorCharShoveDup, "instructionVectorCharShoveDup"), - (instructionVectorCharIsStackEmpty, "instructionVectorCharIsStackEmpty"), - (instructionVectorCharSort, "instructionVectorCharSort"), - (instructionVectorCharSortReverse, "instructionVectorCharSortReverse"), - (instructionVectorCharDupItems, "instructionVectorCharDupItems") - ] +-- allVectorCharInstructions :: [Gene] +-- allVectorCharInstructions = map StateFunc [ +-- (instructionVectorCharConcat, "instructionVectorCharConcat"), +-- (instructionVectorCharConj, "instructionVectorCharConj"), +-- (instructionVectorCharTakeN, "instructionVectorCharTakeN"), +-- (instructionVectorCharSubVector, "instructionVectorCharSubVector"), +-- (instructionVectorCharFirst, "instructionVectorCharFirst"), +-- (instructionVectorCharLast, "instructionVectorCharLast"), +-- (instructionVectorCharNth, "instructionVectorCharNth"), +-- (instructionVectorCharRest, "instructionVectorCharRest"), +-- (instructionVectorCharButLast, "instructionVectorCharButLast"), +-- (instructionVectorCharLength, "instructionVectorCharLength"), +-- (instructionVectorCharReverse, "instructionVectorCharReverse"), +-- (instructionVectorCharPushAll, "instructionVectorCharPushAll"), +-- (instructionVectorCharMakeEmpty, "instructionVectorCharMakeEmpty"), +-- (instructionVectorCharIsEmpty, "instructionVectorCharIsEmpty"), +-- (instructionVectorCharIndexOf, "instructionVectorCharIndexOf"), +-- (instructionVectorCharOccurrencesOf, "instructionVectorCharOccurrencesOf"), +-- (instructionVectorCharSetNth, "instructionVectorCharSetNth"), +-- (instructionVectorCharReplace, "instructionVectorCharReplace"), +-- (instructionVectorCharReplaceFirst, "instructionVectorCharReplaceFirst"), +-- (instructionVectorCharRemove, "instructionVectorCharRemove"), +-- (instructionVectorCharIterate, "instructionVectorCharIterate"), +-- (instructionVectorCharPop, "instructionVectorCharPop"), +-- (instructionVectorCharDup, "instructionVectorCharDup"), +-- (instructionVectorCharDupN, "instructionVectorCharDupN"), +-- (instructionVectorCharSwap, "instructionVectorCharSwap"), +-- (instructionVectorCharRot, "instructionVectorCharRot"), +-- (instructionVectorCharFlush, "instructionVectorCharFlush"), +-- (instructionVectorCharEq, "instructionVectorCharEq"), +-- (instructionVectorCharStackDepth, "instructionVectorCharStackDepth"), +-- (instructionVectorCharYank, "instructionVectorCharYank"), +-- (instructionVectorCharYankDup, "instructionVectorCharYankDup"), +-- (instructionVectorCharShove, "instructionVectorCharShove"), +-- (instructionVectorCharShoveDup, "instructionVectorCharShoveDup"), +-- (instructionVectorCharIsStackEmpty, "instructionVectorCharIsStackEmpty"), +-- (instructionVectorCharSort, "instructionVectorCharSort"), +-- (instructionVectorCharSortReverse, "instructionVectorCharSortReverse"), +-- (instructionVectorCharDupItems, "instructionVectorCharDupItems") +-- ] -allVectorStringInstructions :: [Gene] -allVectorStringInstructions = map StateFunc [ - (instructionVectorStringConcat, "instructionVectorStringConcat"), - (instructionVectorStringConj, "instructionVectorStringConj"), - (instructionVectorStringTakeN, "instructionVectorStringTakeN"), - (instructionVectorStringSubVector, "instructionVectorStringSubVector"), - (instructionVectorStringFirst, "instructionVectorStringFirst"), - (instructionVectorStringLast, "instructionVectorStringLast"), - (instructionVectorStringNth, "instructionVectorStringNth"), - (instructionVectorStringRest, "instructionVectorStringRest"), - (instructionVectorStringButLast, "instructionVectorStringButLast"), - (instructionVectorStringLength, "instructionVectorStringLength"), - (instructionVectorStringReverse, "instructionVectorStringReverse"), - (instructionVectorStringPushAll, "instructionVectorStringPushAll"), - (instructionVectorStringMakeEmpty, "instructionVectorStringMakeEmpty"), - (instructionVectorStringIsEmpty, "instructionVectorStringIsEmpty"), - (instructionVectorStringIndexOf, "instructionVectorStringIndexOf"), - (instructionVectorStringOccurrencesOf, "instructionVectorStringOccurrencesOf"), - (instructionVectorStringSetNth, "instructionVectorStringSetNth"), - (instructionVectorStringReplace, "instructionVectorStringReplace"), - (instructionVectorStringReplaceFirst, "instructionVectorStringReplaceFirst"), - (instructionVectorStringRemove, "instructionVectorStringRemove"), - (instructionVectorStringIterate, "instructionVectorStringIterate"), - (instructionVectorStringPop, "instructionVectorStringPop"), - (instructionVectorStringDup, "instructionVectorStringDup"), - (instructionVectorStringDupN, "instructionVectorStringDupN"), - (instructionVectorStringSwap, "instructionVectorStringSwap"), - (instructionVectorStringRot, "instructionVectorStringRot"), - (instructionVectorStringFlush, "instructionVectorStringFlush"), - (instructionVectorStringEq, "instructionVectorStringEq"), - (instructionVectorStringStackDepth, "instructionVectorStringStackDepth"), - (instructionVectorStringYank, "instructionVectorStringYank"), - (instructionVectorStringYankDup, "instructionVectorStringYankDup"), - (instructionVectorStringShove, "instructionVectorStringShove"), - (instructionVectorStringShoveDup, "instructionVectorStringShoveDup"), - (instructionVectorStringIsStackEmpty, "instructionVectorStringIsStackEmpty"), - (instructionVectorStringSort, "instructionVectorStringSort"), - (instructionVectorStringSortReverse, "instructionVectorStringSortReverse"), - (instructionVectorStringDupItems, "instructionVectorStringDupItems") - ] +-- allVectorStringInstructions :: [Gene] +-- allVectorStringInstructions = map StateFunc [ +-- (instructionVectorStringConcat, "instructionVectorStringConcat"), +-- (instructionVectorStringConj, "instructionVectorStringConj"), +-- (instructionVectorStringTakeN, "instructionVectorStringTakeN"), +-- (instructionVectorStringSubVector, "instructionVectorStringSubVector"), +-- (instructionVectorStringFirst, "instructionVectorStringFirst"), +-- (instructionVectorStringLast, "instructionVectorStringLast"), +-- (instructionVectorStringNth, "instructionVectorStringNth"), +-- (instructionVectorStringRest, "instructionVectorStringRest"), +-- (instructionVectorStringButLast, "instructionVectorStringButLast"), +-- (instructionVectorStringLength, "instructionVectorStringLength"), +-- (instructionVectorStringReverse, "instructionVectorStringReverse"), +-- (instructionVectorStringPushAll, "instructionVectorStringPushAll"), +-- (instructionVectorStringMakeEmpty, "instructionVectorStringMakeEmpty"), +-- (instructionVectorStringIsEmpty, "instructionVectorStringIsEmpty"), +-- (instructionVectorStringIndexOf, "instructionVectorStringIndexOf"), +-- (instructionVectorStringOccurrencesOf, "instructionVectorStringOccurrencesOf"), +-- (instructionVectorStringSetNth, "instructionVectorStringSetNth"), +-- (instructionVectorStringReplace, "instructionVectorStringReplace"), +-- (instructionVectorStringReplaceFirst, "instructionVectorStringReplaceFirst"), +-- (instructionVectorStringRemove, "instructionVectorStringRemove"), +-- (instructionVectorStringIterate, "instructionVectorStringIterate"), +-- (instructionVectorStringPop, "instructionVectorStringPop"), +-- (instructionVectorStringDup, "instructionVectorStringDup"), +-- (instructionVectorStringDupN, "instructionVectorStringDupN"), +-- (instructionVectorStringSwap, "instructionVectorStringSwap"), +-- (instructionVectorStringRot, "instructionVectorStringRot"), +-- (instructionVectorStringFlush, "instructionVectorStringFlush"), +-- (instructionVectorStringEq, "instructionVectorStringEq"), +-- (instructionVectorStringStackDepth, "instructionVectorStringStackDepth"), +-- (instructionVectorStringYank, "instructionVectorStringYank"), +-- (instructionVectorStringYankDup, "instructionVectorStringYankDup"), +-- (instructionVectorStringShove, "instructionVectorStringShove"), +-- (instructionVectorStringShoveDup, "instructionVectorStringShoveDup"), +-- (instructionVectorStringIsStackEmpty, "instructionVectorStringIsStackEmpty"), +-- (instructionVectorStringSort, "instructionVectorStringSort"), +-- (instructionVectorStringSortReverse, "instructionVectorStringSortReverse"), +-- (instructionVectorStringDupItems, "instructionVectorStringDupItems") +-- ] -allVectorBoolInstructions :: [Gene] -allVectorBoolInstructions = map StateFunc [ - (instructionVectorBoolConcat, "instructionVectorBoolConcat"), - (instructionVectorBoolConj, "instructionVectorBoolConj"), - (instructionVectorBoolTakeN, "instructionVectorBoolTakeN"), - (instructionVectorBoolSubVector, "instructionVectorBoolSubVector"), - (instructionVectorBoolFirst, "instructionVectorBoolFirst"), - (instructionVectorBoolLast, "instructionVectorBoolLast"), - (instructionVectorBoolNth, "instructionVectorBoolNth"), - (instructionVectorBoolRest, "instructionVectorBoolRest"), - (instructionVectorBoolButLast, "instructionVectorBoolButLast"), - (instructionVectorBoolLength, "instructionVectorBoolLength"), - (instructionVectorBoolReverse, "instructionVectorBoolReverse"), - (instructionVectorBoolPushAll, "instructionVectorBoolPushAll"), - (instructionVectorBoolMakeEmpty, "instructionVectorBoolMakeEmpty"), - (instructionVectorBoolIsEmpty, "instructionVectorBoolIsEmpty"), - (instructionVectorBoolIndexOf, "instructionVectorBoolIndexOf"), - (instructionVectorBoolOccurrencesOf, "instructionVectorBoolOccurrencesOf"), - (instructionVectorBoolSetNth, "instructionVectorBoolSetNth"), - (instructionVectorBoolReplace, "instructionVectorBoolReplace"), - (instructionVectorBoolReplaceFirst, "instructionVectorBoolReplaceFirst"), - (instructionVectorBoolRemove, "instructionVectorBoolRemove"), - (instructionVectorBoolIterate, "instructionVectorBoolIterate"), - (instructionVectorBoolPop, "instructionVectorBoolPop"), - (instructionVectorBoolDup, "instructionVectorBoolDup"), - (instructionVectorBoolDupN, "instructionVectorBoolDupN"), - (instructionVectorBoolSwap, "instructionVectorBoolSwap"), - (instructionVectorBoolRot, "instructionVectorBoolRot"), - (instructionVectorBoolFlush, "instructionVectorBoolFlush"), - (instructionVectorBoolEq, "instructionVectorBoolEq"), - (instructionVectorBoolStackDepth, "instructionVectorBoolStackDepth"), - (instructionVectorBoolYank, "instructionVectorBoolYank"), - (instructionVectorBoolYankDup, "instructionVectorBoolYankDup"), - (instructionVectorBoolShove, "instructionVectorBoolShove"), - (instructionVectorBoolShoveDup, "instructionVectorBoolShoveDup"), - (instructionVectorBoolIsStackEmpty, "instructionVectorBoolIsStackEmpty"), - (instructionVectorBoolSort, "instructionVectorBoolSort"), - (instructionVectorBoolSortReverse, "instructionVectorBoolSortReverse"), - (instructionVectorBoolDupItems, "instructionVectorBoolDupItems") - ] +-- allVectorBoolInstructions :: [Gene] +-- allVectorBoolInstructions = map StateFunc [ +-- (instructionVectorBoolConcat, "instructionVectorBoolConcat"), +-- (instructionVectorBoolConj, "instructionVectorBoolConj"), +-- (instructionVectorBoolTakeN, "instructionVectorBoolTakeN"), +-- (instructionVectorBoolSubVector, "instructionVectorBoolSubVector"), +-- (instructionVectorBoolFirst, "instructionVectorBoolFirst"), +-- (instructionVectorBoolLast, "instructionVectorBoolLast"), +-- (instructionVectorBoolNth, "instructionVectorBoolNth"), +-- (instructionVectorBoolRest, "instructionVectorBoolRest"), +-- (instructionVectorBoolButLast, "instructionVectorBoolButLast"), +-- (instructionVectorBoolLength, "instructionVectorBoolLength"), +-- (instructionVectorBoolReverse, "instructionVectorBoolReverse"), +-- (instructionVectorBoolPushAll, "instructionVectorBoolPushAll"), +-- (instructionVectorBoolMakeEmpty, "instructionVectorBoolMakeEmpty"), +-- (instructionVectorBoolIsEmpty, "instructionVectorBoolIsEmpty"), +-- (instructionVectorBoolIndexOf, "instructionVectorBoolIndexOf"), +-- (instructionVectorBoolOccurrencesOf, "instructionVectorBoolOccurrencesOf"), +-- (instructionVectorBoolSetNth, "instructionVectorBoolSetNth"), +-- (instructionVectorBoolReplace, "instructionVectorBoolReplace"), +-- (instructionVectorBoolReplaceFirst, "instructionVectorBoolReplaceFirst"), +-- (instructionVectorBoolRemove, "instructionVectorBoolRemove"), +-- (instructionVectorBoolIterate, "instructionVectorBoolIterate"), +-- (instructionVectorBoolPop, "instructionVectorBoolPop"), +-- (instructionVectorBoolDup, "instructionVectorBoolDup"), +-- (instructionVectorBoolDupN, "instructionVectorBoolDupN"), +-- (instructionVectorBoolSwap, "instructionVectorBoolSwap"), +-- (instructionVectorBoolRot, "instructionVectorBoolRot"), +-- (instructionVectorBoolFlush, "instructionVectorBoolFlush"), +-- (instructionVectorBoolEq, "instructionVectorBoolEq"), +-- (instructionVectorBoolStackDepth, "instructionVectorBoolStackDepth"), +-- (instructionVectorBoolYank, "instructionVectorBoolYank"), +-- (instructionVectorBoolYankDup, "instructionVectorBoolYankDup"), +-- (instructionVectorBoolShove, "instructionVectorBoolShove"), +-- (instructionVectorBoolShoveDup, "instructionVectorBoolShoveDup"), +-- (instructionVectorBoolIsStackEmpty, "instructionVectorBoolIsStackEmpty"), +-- (instructionVectorBoolSort, "instructionVectorBoolSort"), +-- (instructionVectorBoolSortReverse, "instructionVectorBoolSortReverse"), +-- (instructionVectorBoolDupItems, "instructionVectorBoolDupItems") +-- ] -allInstructions :: [Gene] -allInstructions = - allIntInstructions <> - allFloatInstructions <> - allBoolInstructions <> - allCharInstructions <> - allCodeInstructions <> - allExecInstructions <> - allStringInstructions <> - allVectorIntInstructions <> - allVectorFloatInstructions <> - allVectorCharInstructions <> - allVectorStringInstructions <> - allVectorBoolInstructions +-- allInstructions :: [Gene] +-- allInstructions = +-- allIntInstructions <> +-- allFloatInstructions <> +-- allBoolInstructions <> +-- allCharInstructions <> +-- allCodeInstructions <> +-- allExecInstructions <> +-- allStringInstructions <> +-- allVectorIntInstructions <> +-- allVectorFloatInstructions <> +-- allVectorCharInstructions <> +-- allVectorStringInstructions <> +-- allVectorBoolInstructions diff --git a/src/HushGP/Instructions/BoolInstructions.hs b/src/HushGP/Instructions/BoolInstructions.hs index d6a96ae..c55f929 100644 --- a/src/HushGP/Instructions/BoolInstructions.hs +++ b/src/HushGP/Instructions/BoolInstructions.hs @@ -2,6 +2,7 @@ module HushGP.Instructions.BoolInstructions where import HushGP.State import HushGP.Instructions.GenericInstructions +import HushGP.Instructions.Utility -- |If top of int stack /= 0 pushes True to bool stack, else false. instructionBoolFromInt :: State -> State @@ -12,12 +13,6 @@ instructionBoolFromInt state = state instructionBoolFromFloat :: State -> State instructionBoolFromFloat state@(State {_float = f1 : fs, _bool = bs}) = state {_float = fs, _bool = (f1 /= 0) : bs} instructionBoolFromFloat state = state - --- |A template function to make bool comparisons concise. -boolTemplate :: (Bool -> Bool -> Bool) -> State -> State -boolTemplate func state@(State {_bool = b1 : b2 : bs}) = state {_bool = func b1 b2 : bs} -boolTemplate _ state = state - -- |Takes the top two bools and Ands them. instructionBoolAnd :: State -> State instructionBoolAnd = boolTemplate (&&) @@ -36,13 +31,6 @@ instructionBoolInvertSecondThenAnd state = state instructionBoolOr :: State -> State instructionBoolOr = boolTemplate (||) --- |Utility function. Haskell doesn't have its own xor operation. -xor :: Bool -> Bool -> Bool -xor b1 b2 - | b1 && not b2 = True - | not b1 && b2 = True - | otherwise = False - -- |Takes the xor of the top two bools. instructionBoolXor :: State -> State instructionBoolXor = boolTemplate xor diff --git a/src/HushGP/Instructions/CharInstructions.hs b/src/HushGP/Instructions/CharInstructions.hs index 902dc46..9c0d540 100644 --- a/src/HushGP/Instructions/CharInstructions.hs +++ b/src/HushGP/Instructions/CharInstructions.hs @@ -2,12 +2,8 @@ module HushGP.Instructions.CharInstructions where import Data.Char import HushGP.State -import HushGP.Instructions.StringInstructions (wschars) import HushGP.Instructions.GenericInstructions - --- |Converts a whole number `mod` 128 to a char. -intToAscii :: Integral a => a -> Char -intToAscii val = chr (abs (fromIntegral val) `mod` 128) +import HushGP.Instructions.Utility -- |Combines the top two chars into a string and pushes the result to the string stack. instructionCharConcat :: State -> State diff --git a/src/HushGP/Instructions/CodeInstructions.hs b/src/HushGP/Instructions/CodeInstructions.hs index 599619a..afe2d91 100644 --- a/src/HushGP/Instructions/CodeInstructions.hs +++ b/src/HushGP/Instructions/CodeInstructions.hs @@ -4,105 +4,9 @@ import Data.List (elemIndex) import HushGP.State import HushGP.Instructions.GenericInstructions import HushGP.Instructions.IntInstructions +import HushGP.Instructions.Utility -- import Debug.Trace --- |Utility function: Checks to see if a gene is a code block. --- If it is a block, returns true, else returns false -isBlock :: Gene -> Bool -isBlock (Block _) = True -isBlock _ = False - --- |Utility function: Returns the length of the passed block. --- If the gene isn't a block, returns 1 -blockLength :: Gene -> Int -blockLength (Block bxs) = length bxs -blockLength _ = 1 - --- |Utility function: Returns true if the passed block is empty, false is not. --- If the passed gene is not a block, returns false -blockIsNull :: Gene -> Bool -blockIsNull (Block bxs) = null bxs -blockIsNull _ = False - --- |Utility Function: A helper function for instructionCodeContainer. The full description is there. --- https://faculty.hampshire.edu/lspector/push3-description.html#Type --- CODE.CONTAINER -findContainer :: Gene -> Gene -> Gene -findContainer (Block fullA) gene - | length fullA <= blockLength gene = Block [] - | gene `elem` fullA = Block [] -- Not allowed to be top level - | any isBlock fullA = findContainer' (filter isBlock fullA) gene - | otherwise = Block [] - where - findContainer' :: [Gene] -> Gene -> Gene - findContainer' [] _ = Block [] - findContainer' ((Block bx1) : bxs) g = if g `elem` bx1 then Block bx1 else findContainer' bxs g - findContainer' _ _ = Block [] -- This should never happen -findContainer _ _ = Block [] - --- |Utility Function: A helper function for instructionCodeDiscrepancy. The full description is there. -countDiscrepancy :: Gene -> Gene -> Int -countDiscrepancy (Block xs) (Block ys) = sum [if uncurry (==) tup then 0 else 1 | tup <- zip xs ys] + abs (length xs - length ys) -countDiscrepancy xgene ygene = if xgene == ygene then 1 else 0 - --- |Utility Function: Extracts the first gene from a block. Returns itself if not a block -extractFirstFromBlock :: Gene -> Gene -extractFirstFromBlock (Block (bx1 : _)) = bx1 -extractFirstFromBlock gene = gene - --- |Utility Function: Returns the last gene from a block, [] if the block is empty, and itself if not a block -extractLastFromBlock :: Gene -> Gene -extractLastFromBlock (Block []) = Block [] -extractLastFromBlock (Block bxs) = last bxs -extractLastFromBlock gene = gene - --- |Utility Function: Calls init on a block. If the block is empty, returns []. If gene isn't a block, returns itself -extractInitFromBlock :: Gene -> Gene -extractInitFromBlock (Block bxs) = Block (safeInit bxs) -extractInitFromBlock gene = gene - --- |Utility Function: Calls `drop 1` on a block. If gene isn't a block, returns itself -extractTailFromBlock :: Gene -> Gene -extractTailFromBlock (Block bxs) = Block (drop 1 bxs) -extractTailFromBlock _ = Block [] - --- |Utility Function: Extracts the code at a point in the genome. Recurses into a nested Block if found. The --- point is based on an int. -codeAtPoint :: [Gene] -> Int -> Gene -codeAtPoint (gene : _) 0 = gene -codeAtPoint [] _ = Block [] -- Should only happen if an empty block is last Gene in the list of Genes -codeAtPoint ((Block nestedGenes) : genes) index = codeAtPoint (nestedGenes <> genes) (index - 1) -codeAtPoint (_ : genes) index = codeAtPoint genes (index - 1) - --- |Utility Function: Inserts code at a point in the genome. Recurses into a block if found. The point is based --- on an integer -codeInsertAtPoint :: [Gene] -> Gene -> Int -> [Gene] -codeInsertAtPoint oldGenes gene 0 = gene : oldGenes -codeInsertAtPoint [] gene _ = [gene] -- This shouldn't happen (lol) -codeInsertAtPoint ((Block genes) : oldGenes) gene index = Block (codeInsertAtPoint genes gene (index - 1)) : oldGenes -codeInsertAtPoint (oldGene : oldGenes) gene index = oldGene : codeInsertAtPoint oldGenes gene (index - 1) - --- |Utility Function: Combines two genes together into a block. -codeCombine :: Gene -> Gene -> Gene -codeCombine (Block bxs) (Block bys) = Block (bxs <> bys) -codeCombine (Block bxs) ygene = Block (ygene : bxs) -codeCombine xgene (Block bys) = Block (xgene : bys) -codeCombine xgene ygene = Block [xgene, ygene] - --- |Utility Function: Determines if the second gene is a member of the first gene. --- If the first gene is a Block and the second gene is also a Block, does a sublist search for the second block in the first block. --- if the first gene is a Block and the second gene is not, the block is searched for the second gene. --- If neither of the genes are blocks, returns False. -codeMember :: Gene -> Gene -> Bool -codeMember (Block bxs) (Block bys) = findSubA bxs bys /= (-1) -codeMember (Block bxs) ygene = ygene `elem` bxs -codeMember _ _ = False - --- |Utility Function: Calculates the size of a Block including counting the nested Blocks recursively -codeRecursiveSize :: Gene -> Int -codeRecursiveSize (Block bxs) = sum [codeRecursiveSize x + if isBlock x then 1 else 0 | x <- bxs] -codeRecursiveSize _ = 1 - -- |Pops the top of the code stack instructionCodePop :: State -> State instructionCodePop = instructionPop code diff --git a/src/HushGP/Instructions/FloatInstructions.hs b/src/HushGP/Instructions/FloatInstructions.hs index c7566a4..4f542fe 100644 --- a/src/HushGP/Instructions/FloatInstructions.hs +++ b/src/HushGP/Instructions/FloatInstructions.hs @@ -2,6 +2,7 @@ module HushGP.Instructions.FloatInstructions where import Data.Fixed (mod') import HushGP.Instructions.GenericInstructions +import HushGP.Instructions.Utility import HushGP.State import Data.Char diff --git a/src/HushGP/Instructions/GenericInstructions.hs b/src/HushGP/Instructions/GenericInstructions.hs index 31b8b7e..c754252 100644 --- a/src/HushGP/Instructions/GenericInstructions.hs +++ b/src/HushGP/Instructions/GenericInstructions.hs @@ -2,111 +2,13 @@ module HushGP.Instructions.GenericInstructions where import Control.Lens import HushGP.State +import HushGP.Instructions.Utility import Data.List (sort, sortBy) import Data.Ord import Data.List.Split -- import Debug.Trace --- |Utility Function: Deletes an item from a list at a specified index. -deleteAt :: Int -> [a] -> [a] -deleteAt idx xs = take idx xs <> drop 1 (drop idx xs) - --- |Utility Function: Combines two tuples containing lists with a value placed between them. -combineTuple :: a -> ([a], [a]) -> [a] -combineTuple val = combineTupleList [val] - --- |Utility Function: Combines two tuples containing lists with a list placed between them. -combineTupleList :: [a] -> ([a], [a]) -> [a] -combineTupleList val tup = fst tup <> val <> snd tup - --- |Utility Function: Inserts a value based on an int at a specified index. -insertAt :: Int -> a -> [a] -> [a] -insertAt idx val xs = combineTuple val (splitAt idx xs) - --- |Utility Function: Replaces a value based on an int at a specified index. -replaceAt :: Int -> a -> [a] -> [a] -replaceAt idx val xs = deleteAt (idx + 1) (insertAt idx val xs) - --- |Utility Function: Takes two ints as indicies. Sorts them low to high, sets the start to --- 0 if the lowest start is less than 0 and the end to the length of the list - 1 if the end --- if larger than the list. Grabs the sub list of adjusted indicies. -subList :: Int -> Int -> [a] -> [a] -subList idx0 idx1 xs = - let - (start, end) = if idx0 <= idx1 then (idx0, idx1) else (idx1, idx0) - adjStart = max 0 start - adjEnd = min end (length xs) - in - take adjEnd (drop adjStart xs) - --- |Utility Function: Finds the index of the second list inside of the first index. --- If the sublist passed is larger than the full list, returns -1 --- If the lists are of equal length, and then contents are equal, returns 0. If not equal, returns -1 --- Recursively shortens the full list until the sub list is found. -findSubA :: forall a. Eq a => [a] -> [a] -> Int -findSubA fullA subA - | length fullA < length subA = -1 - | length fullA == length subA = if fullA == subA then 0 else -1 - | otherwise = findSubA' fullA subA 0 - where - findSubA' :: [a] -> [a] -> Int -> Int - findSubA' fA sA subIndex - | null fA = -1 - | length sA > length fA = -1 - | sA == take (length sA) fA = subIndex - | otherwise = findSubA' (drop 1 fA) sA (subIndex + 1) - --- |Utility Function: Replaces a number of instances of old with new in a list. --- The Maybe Int is the amount of olds to replace with new. Nothing means replace all. --- Just chain findSubA calls. --- May not be the most efficient method with the findSubA calls. -replace :: Eq a => [a] -> [a] -> [a] -> Maybe Int -> [a] -replace fullA old new (Just amt) = - if findSubA fullA old /= -1 && amt > 0 - then replace (take (findSubA fullA old) fullA <> new <> drop (findSubA fullA old + length old) fullA) old new (Just $ amt - 1) - else fullA -replace fullA old new Nothing = - if findSubA fullA old /= -1 - then replace (take (findSubA fullA old) fullA <> new <> drop (findSubA fullA old + length old) fullA) old new Nothing - else fullA - --- |Utility Function: Counts the amount of occurrences of a sub list inside --- of a larger list. -amtOccurences :: forall a. Eq a => [a] -> [a] -> Int -amtOccurences fullA subA = amtOccurences' fullA subA 0 - where - amtOccurences' :: [a] -> [a] -> Int -> Int - amtOccurences' fA sA count = - if findSubA fA sA /= -1 - then amtOccurences' (replace fA sA mempty (Just 1)) sA (count + 1) - else count - --- |Utility Function: Takes the last N elements of a list. -takeR :: Int -> [a] -> [a] -takeR amt fullA = drop (length fullA - amt) fullA - --- |Utility Function: Drops the last N elements of a list. -dropR :: Int -> [a] -> [a] -dropR amt fullA = take (length fullA - amt) fullA - --- |Utility Function: A safe version of init. If the list is empty, returns the empty list. --- If the list has items, takes the init of the list. -safeInit :: [a] -> [a] -safeInit [] = [] -safeInit xs = init xs - --- |Utility Function: An indexing strategy used in parts of Hush. Takes the absolute value --- of the passed number `mod` the length of the passed list. -absNum :: Integral a => a -> [b] -> Int -absNum rawNum lst = abs (fromIntegral rawNum) `mod` length lst - --- |Utility Function: Checks to see if a list is empty. --- If the list is empty, returns False. --- If the list is not empty, returns True. -notEmptyStack :: Lens' State [a] -> State -> Bool -notEmptyStack accessor state = not . null $ view accessor state - -- |Duplicates the top of a stack based on a lens. instructionDup :: Lens' State [a] -> State -> State instructionDup accessor state = diff --git a/src/HushGP/Instructions/StringInstructions.hs b/src/HushGP/Instructions/StringInstructions.hs index 5f22327..cab2a5a 100644 --- a/src/HushGP/Instructions/StringInstructions.hs +++ b/src/HushGP/Instructions/StringInstructions.hs @@ -2,30 +2,9 @@ module HushGP.Instructions.StringInstructions where import HushGP.State import HushGP.Instructions.GenericInstructions +import HushGP.Instructions.Utility import Control.Lens --- |Utility String: Whitespack characters. --- shamelessly stolen from https://hackage.haskell.org/package/MissingH-1.6.0.1/docs/src/Data.String.Utils.html#strip -wschars :: String -wschars = " \t\r\n" - --- |Utility Function: Strips a string of its whitespace on both sides. -strip :: String -> String -strip = lstrip . rstrip - --- |Utility Function: Strips a string of its whitespace on the left side. -lstrip :: String -> String -lstrip s = case s of - [] -> [] - (x:xs) -> if x `elem` wschars - then lstrip xs - else s - --- |Utility Function: Strips a string of its whitespace on the right side. --- this is a tad inefficient -rstrip :: String -> String -rstrip = reverse . lstrip . reverse - -- |Concats the top two strings on the string stack and pushes the result. instructionStringConcat :: State -> State instructionStringConcat = instructionVectorConcat string diff --git a/src/HushGP/Instructions/Utility.hs b/src/HushGP/Instructions/Utility.hs new file mode 100644 index 0000000..31b0cb5 --- /dev/null +++ b/src/HushGP/Instructions/Utility.hs @@ -0,0 +1,249 @@ +module HushGP.Instructions.Utility where + +import Control.Lens hiding (index) +import HushGP.State +import Data.Char + +-- generic utility + +-- |Utility Function: Deletes an item from a list at a specified index. +deleteAt :: Int -> [a] -> [a] +deleteAt idx xs = take idx xs <> drop 1 (drop idx xs) + +-- |Utility Function: Combines two tuples containing lists with a value placed between them. +combineTuple :: a -> ([a], [a]) -> [a] +combineTuple val = combineTupleList [val] + +-- |Utility Function: Combines two tuples containing lists with a list placed between them. +combineTupleList :: [a] -> ([a], [a]) -> [a] +combineTupleList val tup = fst tup <> val <> snd tup + +-- |Utility Function: Inserts a value based on an int at a specified index. +insertAt :: Int -> a -> [a] -> [a] +insertAt idx val xs = combineTuple val (splitAt idx xs) + +-- |Utility Function: Replaces a value based on an int at a specified index. +replaceAt :: Int -> a -> [a] -> [a] +replaceAt idx val xs = deleteAt (idx + 1) (insertAt idx val xs) + +-- |Utility Function: Takes two ints as indicies. Sorts them low to high, sets the start to +-- 0 if the lowest start is less than 0 and the end to the length of the list - 1 if the end +-- if larger than the list. Grabs the sub list of adjusted indicies. +subList :: Int -> Int -> [a] -> [a] +subList idx0 idx1 xs = + let + (start, end) = if idx0 <= idx1 then (idx0, idx1) else (idx1, idx0) + adjStart = max 0 start + adjEnd = min end (length xs) + in + take adjEnd (drop adjStart xs) + +-- |Utility Function: Finds the index of the second list inside of the first index. +-- If the sublist passed is larger than the full list, returns -1 +-- If the lists are of equal length, and then contents are equal, returns 0. If not equal, returns -1 +-- Recursively shortens the full list until the sub list is found. +findSubA :: forall a. Eq a => [a] -> [a] -> Int +findSubA fullA subA + | length fullA < length subA = -1 + | length fullA == length subA = if fullA == subA then 0 else -1 + | otherwise = findSubA' fullA subA 0 + where + findSubA' :: [a] -> [a] -> Int -> Int + findSubA' fA sA subIndex + | null fA = -1 + | length sA > length fA = -1 + | sA == take (length sA) fA = subIndex + | otherwise = findSubA' (drop 1 fA) sA (subIndex + 1) + +-- |Utility Function: Replaces a number of instances of old with new in a list. +-- The Maybe Int is the amount of olds to replace with new. Nothing means replace all. +-- Just chain findSubA calls. +-- May not be the most efficient method with the findSubA calls. +replace :: Eq a => [a] -> [a] -> [a] -> Maybe Int -> [a] +replace fullA old new (Just amt) = + if findSubA fullA old /= -1 && amt > 0 + then replace (take (findSubA fullA old) fullA <> new <> drop (findSubA fullA old + length old) fullA) old new (Just $ amt - 1) + else fullA +replace fullA old new Nothing = + if findSubA fullA old /= -1 + then replace (take (findSubA fullA old) fullA <> new <> drop (findSubA fullA old + length old) fullA) old new Nothing + else fullA + +-- |Utility Function: Counts the amount of occurrences of a sub list inside +-- of a larger list. +amtOccurences :: forall a. Eq a => [a] -> [a] -> Int +amtOccurences fullA subA = amtOccurences' fullA subA 0 + where + amtOccurences' :: [a] -> [a] -> Int -> Int + amtOccurences' fA sA count = + if findSubA fA sA /= -1 + then amtOccurences' (replace fA sA mempty (Just 1)) sA (count + 1) + else count + +-- |Utility Function: Takes the last N elements of a list. +takeR :: Int -> [a] -> [a] +takeR amt fullA = drop (length fullA - amt) fullA + +-- |Utility Function: Drops the last N elements of a list. +dropR :: Int -> [a] -> [a] +dropR amt fullA = take (length fullA - amt) fullA + +-- |Utility Function: A safe version of init. If the list is empty, returns the empty list. +-- If the list has items, takes the init of the list. +safeInit :: [a] -> [a] +safeInit [] = [] +safeInit xs = init xs + +-- |Utility Function: An indexing strategy used in parts of Hush. Takes the absolute value +-- of the passed number `mod` the length of the passed list. +absNum :: Integral a => a -> [b] -> Int +absNum rawNum lst = abs (fromIntegral rawNum) `mod` length lst + +-- |Utility Function: Checks to see if a list is empty. +-- If the list is empty, returns False. +-- If the list is not empty, returns True. +notEmptyStack :: Lens' State [a] -> State -> Bool +notEmptyStack accessor state = not . null $ view accessor state + +-- bool utility + +-- |A template function to make bool comparisons concise. +boolTemplate :: (Bool -> Bool -> Bool) -> State -> State +boolTemplate func state@(State {_bool = b1 : b2 : bs}) = state {_bool = func b1 b2 : bs} +boolTemplate _ state = state + +-- |Utility function. Haskell doesn't have its own xor operation. +xor :: Bool -> Bool -> Bool +xor b1 b2 + | b1 && not b2 = True + | not b1 && b2 = True + | otherwise = False + +-- char utility + +-- |Utility: Converts a whole number `mod` 128 to a char. +intToAscii :: Integral a => a -> Char +intToAscii val = chr (abs (fromIntegral val) `mod` 128) + +-- code utility + +-- |Utility function: Checks to see if a gene is a code block. +-- If it is a block, returns true, else returns false +isBlock :: Gene -> Bool +isBlock (Block _) = True +isBlock _ = False + +-- |Utility function: Returns the length of the passed block. +-- If the gene isn't a block, returns 1 +blockLength :: Gene -> Int +blockLength (Block bxs) = length bxs +blockLength _ = 1 + +-- |Utility function: Returns true if the passed block is empty, false is not. +-- If the passed gene is not a block, returns false +blockIsNull :: Gene -> Bool +blockIsNull (Block bxs) = null bxs +blockIsNull _ = False + +-- |Utility Function: A helper function for instructionCodeContainer. The full description is there. +-- https://faculty.hampshire.edu/lspector/push3-description.html#Type +-- CODE.CONTAINER +findContainer :: Gene -> Gene -> Gene +findContainer (Block fullA) gene + | length fullA <= blockLength gene = Block [] + | gene `elem` fullA = Block [] -- Not allowed to be top level + | any isBlock fullA = findContainer' (filter isBlock fullA) gene + | otherwise = Block [] + where + findContainer' :: [Gene] -> Gene -> Gene + findContainer' [] _ = Block [] + findContainer' ((Block bx1) : bxs) g = if g `elem` bx1 then Block bx1 else findContainer' bxs g + findContainer' _ _ = Block [] -- This should never happen +findContainer _ _ = Block [] + +-- |Utility Function: A helper function for instructionCodeDiscrepancy. The full description is there. +countDiscrepancy :: Gene -> Gene -> Int +countDiscrepancy (Block xs) (Block ys) = sum [if uncurry (==) tup then 0 else 1 | tup <- zip xs ys] + abs (length xs - length ys) +countDiscrepancy xgene ygene = if xgene == ygene then 1 else 0 + +-- |Utility Function: Extracts the first gene from a block. Returns itself if not a block +extractFirstFromBlock :: Gene -> Gene +extractFirstFromBlock (Block (bx1 : _)) = bx1 +extractFirstFromBlock gene = gene + +-- |Utility Function: Returns the last gene from a block, [] if the block is empty, and itself if not a block +extractLastFromBlock :: Gene -> Gene +extractLastFromBlock (Block []) = Block [] +extractLastFromBlock (Block bxs) = last bxs +extractLastFromBlock gene = gene + +-- |Utility Function: Calls init on a block. If the block is empty, returns []. If gene isn't a block, returns itself +extractInitFromBlock :: Gene -> Gene +extractInitFromBlock (Block bxs) = Block (safeInit bxs) +extractInitFromBlock gene = gene + +-- |Utility Function: Calls `drop 1` on a block. If gene isn't a block, returns itself +extractTailFromBlock :: Gene -> Gene +extractTailFromBlock (Block bxs) = Block (drop 1 bxs) +extractTailFromBlock _ = Block [] + +-- |Utility Function: Extracts the code at a point in the genome. Recurses into a nested Block if found. The +-- point is based on an int. +codeAtPoint :: [Gene] -> Int -> Gene +codeAtPoint (gene : _) 0 = gene +codeAtPoint [] _ = Block [] -- Should only happen if an empty block is last Gene in the list of Genes +codeAtPoint ((Block nestedGenes) : genes) index = codeAtPoint (nestedGenes <> genes) (index - 1) +codeAtPoint (_ : genes) index = codeAtPoint genes (index - 1) + +-- |Utility Function: Inserts code at a point in the genome. Recurses into a block if found. The point is based +-- on an integer +codeInsertAtPoint :: [Gene] -> Gene -> Int -> [Gene] +codeInsertAtPoint oldGenes gene 0 = gene : oldGenes +codeInsertAtPoint [] gene _ = [gene] -- This shouldn't happen (lol) +codeInsertAtPoint ((Block genes) : oldGenes) gene index = Block (codeInsertAtPoint genes gene (index - 1)) : oldGenes +codeInsertAtPoint (oldGene : oldGenes) gene index = oldGene : codeInsertAtPoint oldGenes gene (index - 1) + +-- |Utility Function: Combines two genes together into a block. +codeCombine :: Gene -> Gene -> Gene +codeCombine (Block bxs) (Block bys) = Block (bxs <> bys) +codeCombine (Block bxs) ygene = Block (ygene : bxs) +codeCombine xgene (Block bys) = Block (xgene : bys) +codeCombine xgene ygene = Block [xgene, ygene] + +-- |Utility Function: Determines if the second gene is a member of the first gene. +-- If the first gene is a Block and the second gene is also a Block, does a sublist search for the second block in the first block. +-- if the first gene is a Block and the second gene is not, the block is searched for the second gene. +-- If neither of the genes are blocks, returns False. +codeMember :: Gene -> Gene -> Bool +codeMember (Block bxs) (Block bys) = findSubA bxs bys /= (-1) +codeMember (Block bxs) ygene = ygene `elem` bxs +codeMember _ _ = False + +-- |Utility Function: Calculates the size of a Block including counting the nested Blocks recursively +codeRecursiveSize :: Gene -> Int +codeRecursiveSize (Block bxs) = sum [codeRecursiveSize x + if isBlock x then 1 else 0 | x <- bxs] +codeRecursiveSize _ = 1 + +-- string utility + +-- |Utility String: Whitespack characters. +-- shamelessly stolen from https://hackage.haskell.org/package/MissingH-1.6.0.1/docs/src/Data.String.Utils.html#strip +wschars :: String +wschars = " \t\r\n" + +-- |Utility Function: Strips a string of its whitespace on both sides. +strip :: String -> String +strip = lstrip . rstrip + +-- |Utility Function: Strips a string of its whitespace on the left side. +lstrip :: String -> String +lstrip s = case s of + [] -> [] + (x:xs) -> if x `elem` wschars + then lstrip xs + else s + +-- |Utility Function: Strips a string of its whitespace on the right side. +-- this is a tad inefficient +rstrip :: String -> String +rstrip = reverse . lstrip . reverse diff --git a/src/HushGP/PushTests/UtilTests.hs b/src/HushGP/PushTests/UtilTests.hs index ea2ce60..4422001 100644 --- a/src/HushGP/PushTests/UtilTests.hs +++ b/src/HushGP/PushTests/UtilTests.hs @@ -1,6 +1,6 @@ module HushGP.PushTests.UtilTests where -import HushGP.Instructions.GenericInstructions +import HushGP.Instructions.Utility import Test.QuickCheck prop_DeleteAtTest :: Int -> [Int] -> Property From 76df52c55448f9ca348cda7956e7b3066785aad2 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Tue, 11 Feb 2025 00:13:34 -0600 Subject: [PATCH 144/171] Start to play around with TH :) --- HushGP.cabal | 1 + TODO.md | 2 +- src/HushGP/TH.hs | 12 ++++++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 src/HushGP/TH.hs diff --git a/HushGP.cabal b/HushGP.cabal index 49c8ce5..5d303c4 100644 --- a/HushGP.cabal +++ b/HushGP.cabal @@ -57,6 +57,7 @@ library , HushGP.PushTests.IntTests , HushGP.PushTests.GenericTests , HushGP.PushTests.UtilTests + , HushGP.TH -- Modules included in this library but not exported. -- other-modules: diff --git a/TODO.md b/TODO.md index becfb29..b9fd1da 100644 --- a/TODO.md +++ b/TODO.md @@ -15,7 +15,7 @@ - [X] Standardize the pattern matching parameter names, such as c1 : cs - [ ] Write unit/quickcheck tests for all of the instructions - [ ] Use template haskell to generate function lists -- [ ] Move utility functions to their own file +- [X] Move utility functions to their own file ## PushGP TODO - [ ] Implement a Plushy genome translator diff --git a/src/HushGP/TH.hs b/src/HushGP/TH.hs new file mode 100644 index 0000000..52b3cac --- /dev/null +++ b/src/HushGP/TH.hs @@ -0,0 +1,12 @@ +{-# LANGUAGE TemplateHaskell #-} +module HushGP.TH where + +import System.IO + +thTest :: IO () +thTest = do + handle <- openFile "src/HushGP/Instructions/IntInstructions.hs" ReadMode + let list = hGetContents handle + toPrint <- list + print toPrint + hClose handle From 68e1ebf268e50a68576e2270c79b09a9724b2d95 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Tue, 11 Feb 2025 13:01:42 -0600 Subject: [PATCH 145/171] more todo --- TODO.md | 1 + 1 file changed, 1 insertion(+) diff --git a/TODO.md b/TODO.md index b9fd1da..a6c0d68 100644 --- a/TODO.md +++ b/TODO.md @@ -16,6 +16,7 @@ - [ ] Write unit/quickcheck tests for all of the instructions - [ ] Use template haskell to generate function lists - [X] Move utility functions to their own file +- [ ] Make add/sub/mult/div/mod instructions generic ## PushGP TODO - [ ] Implement a Plushy genome translator From cff71ac4caff812cac47f914028b82a9b5a869ec Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Tue, 11 Feb 2025 13:02:24 -0600 Subject: [PATCH 146/171] make todo a bit more concise --- TODO.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TODO.md b/TODO.md index a6c0d68..219500b 100644 --- a/TODO.md +++ b/TODO.md @@ -23,4 +23,4 @@ - [ ] Need to make this reproducable too (Check pysh json files) - [ ] Add Memory - [ ] Add history stack(s), like a call stack -- [ ] Implement interpreter options (could probably just place this all into a map or something) +- [ ] Implement interpreter options (could probably just place this into a map) From 2f2b19e3d02c4c1929433008ace815c701caa50a Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Tue, 11 Feb 2025 16:30:15 -0600 Subject: [PATCH 147/171] gonna abandon TH --- src/HushGP/Instructions/CodeInstructions.hs | 14 +++-------- src/HushGP/Instructions/ExecInstructions.hs | 10 +++----- src/HushGP/TH.hs | 26 ++++++++++++++++++++- 3 files changed, 31 insertions(+), 19 deletions(-) diff --git a/src/HushGP/Instructions/CodeInstructions.hs b/src/HushGP/Instructions/CodeInstructions.hs index afe2d91..1588465 100644 --- a/src/HushGP/Instructions/CodeInstructions.hs +++ b/src/HushGP/Instructions/CodeInstructions.hs @@ -96,19 +96,11 @@ instructionCodeDoThenPop :: State -> State instructionCodeDoThenPop state@(State {_code = c1 : _, _exec = es}) = state {_exec = c1 : StateFunc (instructionCodePop, "instructionCodePop") : es} instructionCodeDoThenPop state = state --- |Utility: A shorthand for instrucitonCodeFromExec to make code instructions less bloated -codeFromExec :: Gene -codeFromExec = StateFunc (instructionCodeFromExec, "instructionCodeFromExec") - --- |Utility: A shorthand for instructionCodoDoRange to make code instructions less bloated -codeDoRange :: Gene -codeDoRange = StateFunc (instructionCodeDoRange, "instructionCodeDoRange") - -- |Evaluates the top item on the code stack for each step along the range i to j. Both i and j are taken from the int stack. instructionCodeDoRange :: State -> State instructionCodeDoRange state@(State {_code = c1 : cs, _int = i0 : i1 : is, _exec = es}) = if increment i0 i1 /= 0 - then state {_exec = c1 : Block [GeneInt (i1 + increment i0 i1), GeneInt i0, codeFromExec, c1, codeDoRange] : es, _int = i1 : is, _code = cs} + then state {_exec = c1 : Block [GeneInt (i1 + increment i0 i1), GeneInt i0, StateFunc (instructionCodeFromExec, "instructionCodeFromExec"), c1, StateFunc (instructionCodeDoRange, "instructionCodeDoRange")] : es, _int = i1 : is, _code = cs} else state {_exec = c1: es, _int = i1 : is, _code = cs} where increment :: Int -> Int -> Int @@ -123,7 +115,7 @@ instructionCodeDoCount :: State -> State instructionCodeDoCount state@(State {_code = c : cs, _int = i1 : is, _exec = es}) = if i1 < 1 then state - else state {_code = cs, _int = is, _exec = Block [GeneInt 0, GeneInt $ i1 - 1, codeFromExec, c, codeDoRange] : es} + else state {_code = cs, _int = is, _exec = Block [GeneInt 0, GeneInt $ i1 - 1, StateFunc (instructionCodeFromExec, "instructionCodeFromExec"), c, StateFunc (instructionCodeDoRange, "instructionCodeDoRange")] : es} instructionCodeDoCount state = state -- |Evaluates the top item on the code stack n times, where n comes from the n comes from the top of the int stack. @@ -131,7 +123,7 @@ instructionCodeDoTimes :: State -> State instructionCodeDoTimes state@(State {_code = c : cs, _int = i1 : is, _exec = es}) = if i1 < 1 then state - else state {_code = cs, _int = is, _exec = Block [GeneInt 0, GeneInt $ i1 - 1, codeFromExec, Block [StateFunc (instructionIntPop, "instructionIntPop"), c], codeDoRange] : es} + else state {_code = cs, _int = is, _exec = Block [GeneInt 0, GeneInt $ i1 - 1, StateFunc (instructionCodeFromExec, "instructionCodeFromExec"), Block [StateFunc (instructionIntPop, "instructionIntPop"), c], StateFunc (instructionCodeDoRange, "instructionCodeDoRange")] : es} instructionCodeDoTimes state = state -- |If the top boolean is true, execute the top element of the code stack and skip the second. Otherwise, skip the top element of the code stack and execute the second. diff --git a/src/HushGP/Instructions/ExecInstructions.hs b/src/HushGP/Instructions/ExecInstructions.hs index 8e5f53e..48f9005 100644 --- a/src/HushGP/Instructions/ExecInstructions.hs +++ b/src/HushGP/Instructions/ExecInstructions.hs @@ -70,16 +70,12 @@ instructionExecShoveDup = instructionShoveDup exec instructionExecIsStackEmpty :: State -> State instructionExecIsStackEmpty = instructionIsStackEmpty exec --- |Utility: Shorthand for instructionExecDoRange -execDoRange :: Gene -execDoRange = StateFunc (instructionExecDoRange, "instructionExecDoRange") - -- |Evaluates the top item on the exec stack for each step along the range i to j. Both i and j are -- taken from the int stack. Differs from code_do_range only in the source of the code and the recursive call. instructionExecDoRange :: State -> State instructionExecDoRange state@(State {_exec = e1 : es, _int = i0 : i1 : is}) = if increment i0 i1 /= 0 - then state {_exec = e1 : Block [GeneInt (i1 + increment i0 i1), GeneInt i0, execDoRange, e1] : es, _int = i1 : is} + then state {_exec = e1 : Block [GeneInt (i1 + increment i0 i1), GeneInt i0, StateFunc (instructionExecDoRange, "instructionExecDoRange"), e1] : es, _int = i1 : is} else state {_exec = e1 : es, _int = i1 : is} where increment :: Int -> Int -> Int @@ -95,7 +91,7 @@ instructionExecDoCount :: State -> State instructionExecDoCount state@(State {_exec = e1 : es, _int = i1 : is}) = if i1 < 1 then state - else state {_exec = Block [GeneInt 0, GeneInt $ i1 - 1, execDoRange, e1] : es, _int = is} + else state {_exec = Block [GeneInt 0, GeneInt $ i1 - 1, StateFunc (instructionExecDoRange, "instructionExecDoRange"), e1] : es, _int = is} instructionExecDoCount state = state -- |Evaluates the top item on the code stack n times, where n comes from the n comes from the top of the int stack. @@ -103,7 +99,7 @@ instructionExecDoTimes :: State -> State instructionExecDoTimes state@(State {_exec = e1 : es, _int = i1 : is}) = if i1 < 1 then state - else state {_exec = Block [GeneInt 0, GeneInt $ i1 - 1, execDoRange, Block [StateFunc (instructionIntPop, "instructionIntPop"), e1]] : es, _int = is} + else state {_exec = Block [GeneInt 0, GeneInt $ i1 - 1, StateFunc (instructionExecDoRange, "instructionExecDoRange"), Block [StateFunc (instructionIntPop, "instructionIntPop"), e1]] : es, _int = is} instructionExecDoTimes state = state -- |Utility: A shorthand for instructionExecWhile diff --git a/src/HushGP/TH.hs b/src/HushGP/TH.hs index 52b3cac..426f25a 100644 --- a/src/HushGP/TH.hs +++ b/src/HushGP/TH.hs @@ -2,11 +2,35 @@ module HushGP.TH where import System.IO +import Text.Regex.TDFA +import Data.List +import HushGP.State +import Language.Haskell.TH +import Language.Haskell.TH.Syntax + +-- listFields :: Name -> Q [Dec] +-- listFields name = do + -- TyConI (DataD _ _ _ [RecC _ fields] _ ) <- reify name + +strHead :: [String] -> String +strHead strxs = + case uncons strxs of + Just (str, _) -> str + _ -> [] + +instructionRegex :: String +instructionRegex = "instruction[a-zA-Z0-9]* ::" + +testRegex :: String -> Bool +testRegex str = str =~ instructionRegex :: Bool thTest :: IO () thTest = do handle <- openFile "src/HushGP/Instructions/IntInstructions.hs" ReadMode let list = hGetContents handle toPrint <- list - print toPrint + let funcs = map (strHead . words) (filter testRegex (lines toPrint)) + let names = map (newName :: (String -> IO Name)) funcs hClose handle + print "hello" + -- let instruction = "instructionIntAdd" From 0dcc8b6b85e0477c145abef85b6ebefea5e77361 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Tue, 11 Feb 2025 16:30:55 -0600 Subject: [PATCH 148/171] remove template haskell file --- src/HushGP/TH.hs | 36 ------------------------------------ 1 file changed, 36 deletions(-) delete mode 100644 src/HushGP/TH.hs diff --git a/src/HushGP/TH.hs b/src/HushGP/TH.hs deleted file mode 100644 index 426f25a..0000000 --- a/src/HushGP/TH.hs +++ /dev/null @@ -1,36 +0,0 @@ -{-# LANGUAGE TemplateHaskell #-} -module HushGP.TH where - -import System.IO -import Text.Regex.TDFA -import Data.List -import HushGP.State -import Language.Haskell.TH -import Language.Haskell.TH.Syntax - --- listFields :: Name -> Q [Dec] --- listFields name = do - -- TyConI (DataD _ _ _ [RecC _ fields] _ ) <- reify name - -strHead :: [String] -> String -strHead strxs = - case uncons strxs of - Just (str, _) -> str - _ -> [] - -instructionRegex :: String -instructionRegex = "instruction[a-zA-Z0-9]* ::" - -testRegex :: String -> Bool -testRegex str = str =~ instructionRegex :: Bool - -thTest :: IO () -thTest = do - handle <- openFile "src/HushGP/Instructions/IntInstructions.hs" ReadMode - let list = hGetContents handle - toPrint <- list - let funcs = map (strHead . words) (filter testRegex (lines toPrint)) - let names = map (newName :: (String -> IO Name)) funcs - hClose handle - print "hello" - -- let instruction = "instructionIntAdd" From 14f00420da15b501c0a3bb76b9d89ccec5a2e6e4 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Tue, 11 Feb 2025 16:31:31 -0600 Subject: [PATCH 149/171] oops --- HushGP.cabal | 1 - 1 file changed, 1 deletion(-) diff --git a/HushGP.cabal b/HushGP.cabal index 5d303c4..49c8ce5 100644 --- a/HushGP.cabal +++ b/HushGP.cabal @@ -57,7 +57,6 @@ library , HushGP.PushTests.IntTests , HushGP.PushTests.GenericTests , HushGP.PushTests.UtilTests - , HushGP.TH -- Modules included in this library but not exported. -- other-modules: From 899aaa93a7b1733f750593b8a9caff970fd8913f Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Tue, 11 Feb 2025 16:37:31 -0600 Subject: [PATCH 150/171] strikethrough template haskell --- TODO.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TODO.md b/TODO.md index 219500b..b9a46a6 100644 --- a/TODO.md +++ b/TODO.md @@ -14,7 +14,7 @@ - [X] Refactor all functions to take state as the final parameter - [X] Standardize the pattern matching parameter names, such as c1 : cs - [ ] Write unit/quickcheck tests for all of the instructions -- [ ] Use template haskell to generate function lists +~~[ ] Use template haskell to generate function lists~~ - [X] Move utility functions to their own file - [ ] Make add/sub/mult/div/mod instructions generic From 84e5c7b1dfa8ada51dfb02f49cc73d4cfe4fa054 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Tue, 11 Feb 2025 17:05:16 -0600 Subject: [PATCH 151/171] update instructions list/formatting --- Makefile | 2 +- TODO.md | 2 +- src/HushGP/Instructions.hs | 1155 ++++++++++------- src/HushGP/Instructions/StringInstructions.hs | 9 - src/HushGP/Instructions/Utility.hs | 10 + src/HushGP/Push.hs | 26 +- src/HushGP/State.hs | 10 +- 7 files changed, 685 insertions(+), 529 deletions(-) diff --git a/Makefile b/Makefile index bd281ee..79a3b29 100644 --- a/Makefile +++ b/Makefile @@ -15,7 +15,7 @@ test: # Runs unit tests. runghc -i./src/ test/Main.hs format: # Formats code using ormolu. - ormolu --mode inplace app/*.hs src/*.hs test/*.hs + ormolu --mode inplace app/*.hs src/HushGP/*.hs test/*.hs hlint: # HLint for lint suggestions. hlint src/*.hs diff --git a/TODO.md b/TODO.md index b9a46a6..b7d6c54 100644 --- a/TODO.md +++ b/TODO.md @@ -13,7 +13,7 @@ - [X] Write haddock documentation for each function - [X] Refactor all functions to take state as the final parameter - [X] Standardize the pattern matching parameter names, such as c1 : cs -- [ ] Write unit/quickcheck tests for all of the instructions +- [ ] Write unit/quickcheck tests for the generic functions ~~[ ] Use template haskell to generate function lists~~ - [X] Move utility functions to their own file - [ ] Make add/sub/mult/div/mod instructions generic diff --git a/src/HushGP/Instructions.hs b/src/HushGP/Instructions.hs index acfa87c..fcc9be3 100644 --- a/src/HushGP/Instructions.hs +++ b/src/HushGP/Instructions.hs @@ -12,530 +12,685 @@ module HushGP.Instructions module HushGP.Instructions.VectorStringInstructions, module HushGP.Instructions.VectorBoolInstructions, module HushGP.Instructions.VectorCharInstructions, - -- allIntInstructions, - -- allFloatInstructions, - -- allBoolInstructions, - -- allCharInstructions, - -- allCodeInstructions, - -- allExecInstructions, - -- allStringInstructions, - -- allVectorIntInstructions, - -- allVectorFloatInstructions, - -- allVectorCharInstructions, - -- allVectorStringInstructions, - -- allVectorBoolInstructions, - -- allInstructions + allIntInstructions, + allFloatInstructions, + allBoolInstructions, + allCharInstructions, + allCodeInstructions, + allExecInstructions, + allStringInstructions, + allVectorIntInstructions, + allVectorFloatInstructions, + allVectorCharInstructions, + allVectorStringInstructions, + allVectorBoolInstructions, + allInstructions, ) where +import HushGP.Instructions.BoolInstructions import HushGP.Instructions.CharInstructions import HushGP.Instructions.CodeInstructions import HushGP.Instructions.ExecInstructions import HushGP.Instructions.FloatInstructions import HushGP.Instructions.GenericInstructions import HushGP.Instructions.IntInstructions -import HushGP.Instructions.BoolInstructions import HushGP.Instructions.StringInstructions +import HushGP.Instructions.VectorBoolInstructions import HushGP.Instructions.VectorCharInstructions import HushGP.Instructions.VectorFloatInstructions import HushGP.Instructions.VectorIntInstructions -import HushGP.Instructions.VectorBoolInstructions import HushGP.Instructions.VectorStringInstructions --- import HushGP.State +import HushGP.State --- allIntInstructions :: [Gene] --- allIntInstructions = map StateFunc [ --- (instructionIntFromFloat, "instructionIntFromFloat"), --- (instructionIntFromBool, "instructionIntFromBool"), --- (instructionIntAdd, "instructionIntAdd"), --- (instructionIntSub, "instructionIntSub"), --- (instructionIntMul, "instructionIntMul"), --- (instructionIntDiv, "instructionIntDiv"), --- (instructionIntMod, "instructionIntMod"), --- (instructionIntMin, "instructionIntMin"), --- (instructionIntMax, "instructionIntMax"), --- (instructionIntInc, "instructionIntInc"), --- (instructionIntDec, "instructionIntDec"), --- (instructionIntLT, "instructionIntLT"), --- (instructionIntGT, "instructionIntGT"), --- (instructionIntLTE, "instructionIntLTE"), --- (instructionIntGTE, "instructionIntGTE"), --- (instructionIntDup, "instructionIntDup"), --- (instructionIntPop, "instructionIntPop"), --- (instructionIntDupN, "instructionIntDupN"), --- (instructionIntSwap, "instructionIntSwap"), --- (instructionIntRot, "instructionIntRot"), --- (instructionIntFlush, "instructionIntFlush"), --- (instructionIntEq, "instructionIntEq"), --- (instructionIntYank, "instructionIntYank"), --- (instructionIntYankDup, "instructionIntYankDup"), --- (instructionIntShove, "instructionIntShove"), --- (instructionIntIsStackEmpty, "instructionIntIsStackEmpty"), --- (instructionIntFromChar, "instructionIntFromChar"), --- (instructionIntFromString, "instructionIntFromString"), --- (instructionIntDupItems, "instructionIntDupItems") --- ] +allIntInstructions :: [Gene] +allIntInstructions = + map + StateFunc + [ (instructionIntFromFloat, "instructionIntFromFloat"), + (instructionIntFromBool, "instructionIntFromBool"), + (instructionIntAdd, "instructionIntAdd"), + (instructionIntSub, "instructionIntSub"), + (instructionIntMul, "instructionIntMul"), + (instructionIntDiv, "instructionIntDiv"), + (instructionIntMod, "instructionIntMod"), + (instructionIntMin, "instructionIntMin"), + (instructionIntMax, "instructionIntMax"), + (instructionIntInc, "instructionIntInc"), + (instructionIntDec, "instructionIntDec"), + (instructionIntLT, "instructionIntLT"), + (instructionIntGT, "instructionIntGT"), + (instructionIntLTE, "instructionIntLTE"), + (instructionIntGTE, "instructionIntGTE"), + (instructionIntDup, "instructionIntDup"), + (instructionIntPop, "instructionIntPop"), + (instructionIntDupN, "instructionIntDupN"), + (instructionIntSwap, "instructionIntSwap"), + (instructionIntRot, "instructionIntRot"), + (instructionIntFlush, "instructionIntFlush"), + (instructionIntEq, "instructionIntEq"), + (instructionIntYank, "instructionIntYank"), + (instructionIntYankDup, "instructionIntYankDup"), + (instructionIntShove, "instructionIntShove"), + (instructionIntIsStackEmpty, "instructionIntIsStackEmpty"), + (instructionIntFromChar, "instructionIntFromChar"), + (instructionIntFromString, "instructionIntFromString"), + (instructionIntDupItems, "instructionIntDupItems") + ] --- allFloatInstructions :: [Gene] --- allFloatInstructions = map StateFunc [ --- (instructionFloatFromInt, "instructionFloatFromInt"), --- (instructionFloatFromBool, "instructionFloatFromBool"), --- (instructionFloatAdd, "instructionFloatAdd"), --- (instructionFloatSub, "instructionFloatSub"), --- (instructionFloatMul, "instructionFloatMul"), --- (instructionFloatDiv, "instructionFloatDiv"), --- (instructionFloatMod, "instructionFloatMod"), --- (instructionFloatMin, "instructionFloatMin"), --- (instructionFloatMax, "instructionFloatMax"), --- (instructionFloatInc, "instructionFloatInc"), --- (instructionFloatDec, "instructionFloatDec"), --- (instructionFloatLT, "instructionFloatLT"), --- (instructionFloatGT, "instructionFloatGT"), --- (instructionFloatLTE, "instructionFloatLTE"), --- (instructionFloatGTE, "instructionFloatGTE"), --- (instructionFloatDup, "instructionFloatDup"), --- (instructionFloatPop, "instructionFloatPop"), --- (instructionFloatDupN, "instructionFloatDupN"), --- (instructionFloatSwap, "instructionFloatSwap"), --- (instructionFloatRot, "instructionFloatRot"), --- (instructionFloatFlush, "instructionFloatFlush"), --- (instructionFloatEq, "instructionFloatEq"), --- (instructionFloatYank, "instructionFloatYank"), --- (instructionFloatYankDup, "instructionFloatYankDup"), --- (instructionFloatShove, "instructionFloatShove"), --- (instructionFloatIsStackEmpty, "instructionFloatIsStackEmpty"), --- (instructionFloatFromChar, "instructionFloatFromChar"), --- (instructionFloatFromString, "instructionFloatFromString"), --- (instructionFloatDupItems, "instructionFloatDupItems") --- ] +allFloatInstructions :: [Gene] +allFloatInstructions = + map + StateFunc + [ (instructionFloatFromInt, "instructionFloatFromInt"), + (instructionFloatFromBool, "instructionFloatFromBool"), + (instructionFloatAdd, "instructionFloatAdd"), + (instructionFloatSub, "instructionFloatSub"), + (instructionFloatMul, "instructionFloatMul"), + (instructionFloatDiv, "instructionFloatDiv"), + (instructionFloatMod, "instructionFloatMod"), + (instructionFloatMin, "instructionFloatMin"), + (instructionFloatMax, "instructionFloatMax"), + (instructionFloatInc, "instructionFloatInc"), + (instructionFloatDec, "instructionFloatDec"), + (instructionFloatLT, "instructionFloatLT"), + (instructionFloatGT, "instructionFloatGT"), + (instructionFloatLTE, "instructionFloatLTE"), + (instructionFloatGTE, "instructionFloatGTE"), + (instructionFloatDup, "instructionFloatDup"), + (instructionFloatPop, "instructionFloatPop"), + (instructionFloatDupN, "instructionFloatDupN"), + (instructionFloatSwap, "instructionFloatSwap"), + (instructionFloatRot, "instructionFloatRot"), + (instructionFloatFlush, "instructionFloatFlush"), + (instructionFloatEq, "instructionFloatEq"), + (instructionFloatYank, "instructionFloatYank"), + (instructionFloatYankDup, "instructionFloatYankDup"), + (instructionFloatShove, "instructionFloatShove"), + (instructionFloatIsStackEmpty, "instructionFloatIsStackEmpty"), + (instructionFloatFromChar, "instructionFloatFromChar"), + (instructionFloatFromString, "instructionFloatFromString"), + (instructionFloatDupItems, "instructionFloatDupItems") + ] --- allBoolInstructions :: [Gene] --- allBoolInstructions = map StateFunc [ --- (instructionBoolFromInt, "instructionBoolFromInt"), --- (instructionBoolFromFloat, "instructionBoolFromFloat"), --- (instructionBoolAnd, "instructionBoolAnd"), --- (instructionBoolInvertFirstThenAnd, "instructionBoolInvertFirstThenAnd"), --- (instructionBoolInvertSecondThenAnd, "instructionBoolInvertSecondThenAnd"), --- (instructionBoolOr, "instructionBoolOr"), --- (instructionBoolXor, "instructionBoolXor"), --- (instructionBoolPop, "instructionBoolPop"), --- (instructionBoolDup, "instructionBoolDup"), --- (instructionBoolDupN, "instructionBoolDupN"), --- (instructionBoolSwap, "instructionBoolSwap"), --- (instructionBoolRot, "instructionBoolRot"), --- (instructionBoolFlush, "instructionBoolFlush"), --- (instructionBoolEq, "instructionBoolEq"), --- (instructionBoolStackDepth, "instructionBoolStackDepth"), --- (instructionBoolYank, "instructionBoolYank"), --- (instructionBoolYankDup, "instructionBoolYankDup"), --- (instructionBoolShove, "instructionBoolShove"), --- (instructionBoolShoveDup, "instructionBoolShoveDup"), --- (instructionBoolIsStackEmpty, "instructionBoolIsStackEmpty"), --- (instructionBoolDupItems, "instructionBoolDupItems") --- ] +allBoolInstructions :: [Gene] +allBoolInstructions = + map + StateFunc + [ (instructionBoolFromInt, "instructionBoolFromInt"), + (instructionBoolFromFloat, "instructionBoolFromFloat"), + (instructionBoolAnd, "instructionBoolAnd"), + (instructionBoolInvertFirstThenAnd, "instructionBoolInvertFirstThenAnd"), + (instructionBoolInvertSecondThenAnd, "instructionBoolInvertSecondThenAnd"), + (instructionBoolOr, "instructionBoolOr"), + (instructionBoolXor, "instructionBoolXor"), + (instructionBoolPop, "instructionBoolPop"), + (instructionBoolDup, "instructionBoolDup"), + (instructionBoolDupN, "instructionBoolDupN"), + (instructionBoolSwap, "instructionBoolSwap"), + (instructionBoolRot, "instructionBoolRot"), + (instructionBoolFlush, "instructionBoolFlush"), + (instructionBoolEq, "instructionBoolEq"), + (instructionBoolStackDepth, "instructionBoolStackDepth"), + (instructionBoolYank, "instructionBoolYank"), + (instructionBoolYankDup, "instructionBoolYankDup"), + (instructionBoolShove, "instructionBoolShove"), + (instructionBoolShoveDup, "instructionBoolShoveDup"), + (instructionBoolIsStackEmpty, "instructionBoolIsStackEmpty"), + (instructionBoolDupItems, "instructionBoolDupItems") + ] --- allCharInstructions :: [Gene] --- allCharInstructions = map StateFunc [ --- (instructionCharConcat, "instructionCharConcat"), --- (instructionCharFromFirstChar, "instructionCharFromFirstChar"), --- (instructionCharFromLastChar, "instructionCharFromLastChar"), --- (instructionCharFromNthChar, "instructionCharFromNthChar"), --- (instructionCharIsWhitespace, "instructionCharIsWhitespace"), --- (instructionCharIsLetter, "instructionCharIsLetter"), --- (instructionCharIsDigit, "instructionCharIsDigit"), --- (instructionCharFromBool, "instructionCharFromBool"), --- (instructionCharFromAsciiInt, "instructionCharFromAsciiInt"), --- (instructionCharFromAsciiFloat, "instructionCharFromAsciiFloat"), --- (instructionCharsFromString, "instructionCharsFromString"), --- (instructionCharPop, "instructionCharPop"), --- (instructionCharDup, "instructionCharDup"), --- (instructionCharDupN, "instructionCharDupN"), --- (instructionCharSwap, "instructionCharSwap"), --- (instructionCharRot, "instructionCharRot"), --- (instructionCharFlush, "instructionCharFlush"), --- (instructionCharEq, "instructionCharEq"), --- (instructionCharStackDepth, "instructionCharStackDepth"), --- (instructionCharYank, "instructionCharYank"), --- (instructionCharYankDup, "instructionCharYankDup"), --- (instructionCharShove, "instructionCharShove"), --- (instructionCharShoveDup, "instructionCharShoveDup"), --- (instructionCharIsStackEmpty, "instructionCharIsStackEmpty"), --- (instructionCharDupItems, "instructionCharDupItems") --- ] +allCharInstructions :: [Gene] +allCharInstructions = + map + StateFunc + [ (instructionCharConcat, "instructionCharConcat"), + (instructionCharFromFirstChar, "instructionCharFromFirstChar"), + (instructionCharFromLastChar, "instructionCharFromLastChar"), + (instructionCharFromNthChar, "instructionCharFromNthChar"), + (instructionCharIsWhitespace, "instructionCharIsWhitespace"), + (instructionCharIsLetter, "instructionCharIsLetter"), + (instructionCharIsDigit, "instructionCharIsDigit"), + (instructionCharFromBool, "instructionCharFromBool"), + (instructionCharFromAsciiInt, "instructionCharFromAsciiInt"), + (instructionCharFromAsciiFloat, "instructionCharFromAsciiFloat"), + (instructionCharsFromString, "instructionCharsFromString"), + (instructionCharPop, "instructionCharPop"), + (instructionCharDup, "instructionCharDup"), + (instructionCharDupN, "instructionCharDupN"), + (instructionCharSwap, "instructionCharSwap"), + (instructionCharRot, "instructionCharRot"), + (instructionCharFlush, "instructionCharFlush"), + (instructionCharEq, "instructionCharEq"), + (instructionCharStackDepth, "instructionCharStackDepth"), + (instructionCharYank, "instructionCharYank"), + (instructionCharYankDup, "instructionCharYankDup"), + (instructionCharShove, "instructionCharShove"), + (instructionCharShoveDup, "instructionCharShoveDup"), + (instructionCharIsStackEmpty, "instructionCharIsStackEmpty"), + (instructionCharDupItems, "instructionCharDupItems") + ] --- allCodeInstructions :: [Gene] --- allCodeInstructions = map StateFunc [ --- (instructionCodePop, "instructionCodePop"), --- (instructionCodeIsCodeBlock, "instructionCodeIsCodeBlock"), --- (instructionCodeIsSingular, "instructionCodeIsSingular"), --- (instructionCodeLength, "instructionCodeLength"), --- (instructionCodeFirst, "instructionCodeFirst"), --- (instructionCodeLast, "instructionCodeLast"), --- (instructionCodeTail, "instructionCodeTail"), --- (instructionCodeInit, "instructionCodeInit"), --- (instructionCodeWrap, "instructionCodeWrap"), --- (instructionCodeList, "instructionCodeList"), --- (instructionCodeCombine, "instructionCodeCombine"), --- (instructionCodeDo, "instructionCodeDo"), --- (instructionCodeDoDup, "instructionCodeDoDup"), --- (instructionCodeDoThenPop, "instructionCodeDoThenPop"), --- (instructionCodeDoRange, "instructionCodeDoRange"), --- (instructionCodeDoCount, "instructionCodeDoCount"), --- (instructionCodeDoTimes, "instructionCodeDoTimes"), --- (instructionCodeIf, "instructionCodeIf"), --- (instructionCodeWhen, "instructionCodeWhen"), --- (instructionCodeMember, "instructionCodeMember"), --- (instructionCodeN, "instructionCodeN"), --- (instructionMakeEmptyCodeBlock, "instructionMakeEmptyCodeBlock"), --- (instructionIsEmptyCodeBlock, "instructionIsEmptyCodeBlock"), --- (instructionCodeSize, "instructionCodeSize"), --- (instructionCodeExtract, "instructionCodeExtract"), --- (instructionCodeInsert, "instructionCodeInsert"), --- (instructionCodeFirstPosition, "instructionCodeFirstPosition"), --- (instructionCodeReverse, "instructionCodeReverse"), --- (instructionCodeDup, "instructionCodeDup"), --- (instructionCodeDupN, "instructionCodeDupN"), --- (instructionCodeDup, "instructionCodeDup"), --- (instructionCodeDupN, "instructionCodeDupN"), --- (instructionCodeSwap, "instructionCodeSwap"), --- (instructionCodeRot, "instructionCodeRot"), --- (instructionCodeFlush, "instructionCodeFlush"), --- (instructionCodeEq, "instructionCodeEq"), --- (instructionCodeStackDepth, "instructionCodeStackDepth"), --- (instructionCodeYank, "instructionCodeYank"), --- (instructionCodeYankDup, "instructionCodeYankDup"), --- (instructionCodeShove, "instructionCodeShove"), --- (instructionCodeShoveDup, "instructionCodeShoveDup"), --- (instructionCodeIsStackEmpty, "instructionCodeIsStackEmpty"), --- (instructionCodeFromBool, "instructionCodeFromBool"), --- (instructionCodeFromInt, "instructionCodeFromInt"), --- (instructionCodeFromChar, "instructionCodeFromChar"), --- (instructionCodeFromFloat, "instructionCodeFromFloat"), --- (instructionCodeFromString, "instructionCodeFromString"), --- (instructionCodeFromVectorInt, "instructionCodeFromVectorInt"), --- (instructionCodeFromVectorFloat, "instructionCodeFromVectorFloat"), --- (instructionCodeFromVectorString, "instructionCodeFromVectorString"), --- (instructionCodeFromVectorBool, "instructionCodeFromVectorBool"), --- (instructionCodeFromVectorChar, "instructionCodeFromVectorChar"), --- (instructionCodeFromExec, "instructionCodeFromExec"), --- (instructionCodeContainer, "instructionCodeContainer"), --- (instructionCodeDiscrepancy, "instructionCodeDiscrepancy"), --- (instructionCodeNoOp, "instructionCodeNoOp"), --- (instructionCodeTailN, "instructionCodeTailN"), --- (instructionCodeDupItems, "instructionCodeDupItems") --- ] +allCodeInstructions :: [Gene] +allCodeInstructions = + map + StateFunc + [ (instructionCodePop, "instructionCodePop"), + (instructionCodeIsCodeBlock, "instructionCodeIsCodeBlock"), + (instructionCodeIsSingular, "instructionCodeIsSingular"), + (instructionCodeLength, "instructionCodeLength"), + (instructionCodeFirst, "instructionCodeFirst"), + (instructionCodeLast, "instructionCodeLast"), + (instructionCodeTail, "instructionCodeTail"), + (instructionCodeInit, "instructionCodeInit"), + (instructionCodeWrap, "instructionCodeWrap"), + (instructionCodeList, "instructionCodeList"), + (instructionCodeCombine, "instructionCodeCombine"), + (instructionCodeDo, "instructionCodeDo"), + (instructionCodeDoDup, "instructionCodeDoDup"), + (instructionCodeDoThenPop, "instructionCodeDoThenPop"), + (instructionCodeDoRange, "instructionCodeDoRange"), + (instructionCodeDoCount, "instructionCodeDoCount"), + (instructionCodeDoTimes, "instructionCodeDoTimes"), + (instructionCodeIf, "instructionCodeIf"), + (instructionCodeWhen, "instructionCodeWhen"), + (instructionCodeMember, "instructionCodeMember"), + (instructionCodeN, "instructionCodeN"), + (instructionMakeEmptyCodeBlock, "instructionMakeEmptyCodeBlock"), + (instructionIsEmptyCodeBlock, "instructionIsEmptyCodeBlock"), + (instructionCodeSize, "instructionCodeSize"), + (instructionCodeExtract, "instructionCodeExtract"), + (instructionCodeInsert, "instructionCodeInsert"), + (instructionCodeFirstPosition, "instructionCodeFirstPosition"), + (instructionCodeReverse, "instructionCodeReverse"), + (instructionCodeDup, "instructionCodeDup"), + (instructionCodeDupN, "instructionCodeDupN"), + (instructionCodeDup, "instructionCodeDup"), + (instructionCodeDupN, "instructionCodeDupN"), + (instructionCodeSwap, "instructionCodeSwap"), + (instructionCodeRot, "instructionCodeRot"), + (instructionCodeFlush, "instructionCodeFlush"), + (instructionCodeEq, "instructionCodeEq"), + (instructionCodeStackDepth, "instructionCodeStackDepth"), + (instructionCodeYank, "instructionCodeYank"), + (instructionCodeYankDup, "instructionCodeYankDup"), + (instructionCodeShove, "instructionCodeShove"), + (instructionCodeShoveDup, "instructionCodeShoveDup"), + (instructionCodeIsStackEmpty, "instructionCodeIsStackEmpty"), + (instructionCodeFromBool, "instructionCodeFromBool"), + (instructionCodeFromInt, "instructionCodeFromInt"), + (instructionCodeFromChar, "instructionCodeFromChar"), + (instructionCodeFromFloat, "instructionCodeFromFloat"), + (instructionCodeFromString, "instructionCodeFromString"), + (instructionCodeFromVectorInt, "instructionCodeFromVectorInt"), + (instructionCodeFromVectorFloat, "instructionCodeFromVectorFloat"), + (instructionCodeFromVectorString, "instructionCodeFromVectorString"), + (instructionCodeFromVectorBool, "instructionCodeFromVectorBool"), + (instructionCodeFromVectorChar, "instructionCodeFromVectorChar"), + (instructionCodeFromExec, "instructionCodeFromExec"), + (instructionCodeContainer, "instructionCodeContainer"), + (instructionCodeDiscrepancy, "instructionCodeDiscrepancy"), + (instructionCodeNoOp, "instructionCodeNoOp"), + (instructionCodeTailN, "instructionCodeTailN"), + (instructionCodeDupItems, "instructionCodeDupItems") + ] --- allExecInstructions :: [Gene] --- allExecInstructions = map StateFunc [ --- (instructionExecIf, "instructionExecIf"), --- (instructionExecDup, "instructionExecDup"), --- (instructionExecDupN, "instructionExecDupN"), --- (instructionExecPop, "instructionExecPop"), --- (instructionExecSwap, "instructionExecSwap"), --- (instructionExecRot, "instructionExecRot"), --- (instructionExecFlush, "instructionExecFlush"), --- (instructionExecEq, "instructionExecEq"), --- (instructionExecStackDepth, "instructionExecStackDepth"), --- (instructionExecYank, "instructionExecYank"), --- (instructionExecYankDup, "instructionExecYankDup"), --- (instructionExecShove, "instructionExecShove"), --- (instructionExecShoveDup, "instructionExecShoveDup"), --- (instructionExecIsStackEmpty, "instructionExecIsStackEmpty"), --- (instructionExecDoRange, "instructionExecDoRange"), --- (instructionExecDoCount, "instructionExecDoCount"), --- (instructionExecDoTimes, "instructionExecDoTimes"), --- (instructionExecWhile, "instructionExecWhile"), --- (instructionExecDoWhile, "instructionExecDoWhile"), --- (instructionExecWhen, "instructionExecWhen"), --- (instructionExecK, "instructionExecK"), --- (instructionExecS, "instructionExecS"), --- (instructionExecY, "instrucitonExecY"), --- (instructionExecDupItems, "instructionExecDupItems") --- ] +allExecInstructions :: [Gene] +allExecInstructions = + map + StateFunc + [ (instructionExecIf, "instructionExecIf"), + (instructionExecDup, "instructionExecDup"), + (instructionExecDupN, "instructionExecDupN"), + (instructionExecPop, "instructionExecPop"), + (instructionExecSwap, "instructionExecSwap"), + (instructionExecRot, "instructionExecRot"), + (instructionExecFlush, "instructionExecFlush"), + (instructionExecEq, "instructionExecEq"), + (instructionExecStackDepth, "instructionExecStackDepth"), + (instructionExecYank, "instructionExecYank"), + (instructionExecYankDup, "instructionExecYankDup"), + (instructionExecShove, "instructionExecShove"), + (instructionExecShoveDup, "instructionExecShoveDup"), + (instructionExecIsStackEmpty, "instructionExecIsStackEmpty"), + (instructionExecDoRange, "instructionExecDoRange"), + (instructionExecDoCount, "instructionExecDoCount"), + (instructionExecDoTimes, "instructionExecDoTimes"), + (instructionExecWhile, "instructionExecWhile"), + (instructionExecDoWhile, "instructionExecDoWhile"), + (instructionExecWhen, "instructionExecWhen"), + (instructionExecK, "instructionExecK"), + (instructionExecS, "instructionExecS"), + (instructionExecY, "instrucitonExecY"), + (instructionExecDupItems, "instructionExecDupItems") + ] --- allStringInstructions :: [Gene] --- allStringInstructions = map StateFunc [ --- (instructionStringConcat, "instructionStringConcat"), --- (instructionStringSwap, "instructionStringSwap"), --- (instructionStringInsertString, "instructionStringInsertString"), --- (instructionStringFromFirstChar, "instructionStringFromFirstChar"), --- (instructionStringFromLastChar, "instructionStringFromLastChar"), --- (instructionStringFromNthChar, "instructionStringFromNthChar"), --- (instructionStringIndexOfString, "instructionStringIndexOfString"), --- (instructionStringContainsString, "instructionStringContainsString"), --- (instructionStringSplitOnString, "instructionStringSplitOnString"), --- (instructionStringReplaceFirstString, "instructionStringReplaceFirstString"), --- (instructionStringReplaceNString, "instructionStringReplaceNString"), --- (instructionStringReplaceAllString, "instructionStringReplaceAllString"), --- (instructionStringRemoveFirstString, "instructionStringRemoveFirstString"), --- (instructionStringRemoveNString, "instructionStringRemoveNString"), --- (instructionStringRemoveAllString, "instructionStringRemoveAllString"), --- (instructionStringOccurrencesOfString, "instructionStringOccurrencesOfString"), --- (instructionStringInsertChar, "instructionStringInsertChar"), --- (instructionStringContainsChar, "instructionStringContainsChar"), --- (instructionStringIndexOfChar, "instructionStringIndexOfChar"), --- (instructionStringSplitOnChar, "instructionStringSplitOnChar"), --- (instructionStringReplaceFirstChar, "instructionStringReplaceFirstChar"), --- (instructionStringReplaceNChar, "instructionStringReplaceNChar"), --- (instructionStringReplaceAllChar, "instructionStringReplaceAllChar"), --- (instructionStringRemoveFirstChar, "instructionStringRemoveFirstChar"), --- (instructionStringRemoveNChar, "instructionStringRemoveNChar"), --- (instructionStringRemoveAllChar, "instructionStringRemoveAllChar"), --- (instructionStringOccurrencesOfChar, "instructionStringOccurrencesOfChar"), --- (instructionStringReverse, "instructionStringReverse"), --- (instructionStringHead, "instructionStringHead"), --- (instructionStringTail, "instructionStringTail"), --- (instructionStringAppendChar, "instructionStringAppendChar"), --- (instructionStringRest, "instructionStringRest"), --- (instructionStringButLast, "instructionStringButLast"), --- (instructionStringDrop, "instructionStringDrop"), --- (instructionStringButLastN, "instructionStringButLastN"), --- (instructionStringLength, "instructionStringLength"), --- (instructionStringMakeEmpty, "instructionStringMakeEmpty"), --- (instructionStringIsEmptyString, "instructionStringIsEmptyString"), --- (instructionStringRemoveNth, "instructionStringRemoveNth"), --- (instructionStringSetNth, "instructionStringSetNth"), --- (instructionStringStripWhitespace, "instructionStringStripWhitespace"), --- (instructionStringFromBool, "instructionStringFromBool"), --- (instructionStringFromInt, "instructionStringFromInt"), --- (instructionStringFromFloat, "instructionStringFromFloat"), --- (instructionStringFromChar, "instructionStringFromChar"), --- (instructionStringPop, "instructionStringPop"), --- (instructionStringDup, "instructionStringDup"), --- (instructionStringDupN, "instructionStringDupN"), --- (instructionStringSwap, "instructionStringSwap"), --- (instructionStringRot, "instructionStringRot"), --- (instructionStringFlush, "instructionStringFlush"), --- (instructionStringEq, "instructionStringEq"), --- (instructionStringStackDepth, "instructionStringStackDepth"), --- (instructionStringYank, "instructionStringYank"), --- (instructionStringYankDup, "instructionStringYankDup"), --- (instructionStringShove, "instructionStringShove"), --- (instructionStringShoveDup, "instructionStringShoveDup"), --- (instructionStringIsStackEmpty, "instructionStringIsStackEmpty"), --- (instructionStringSort, "instructionStringSort"), --- (instructionStringSortReverse, "instructionStringSortReverse"), --- (instructionStringDupItems, "instructionStringDupItems"), --- (instructionStringParseToChar, "instructionStringParseToChar"), --- (instructionStringSubString, "instructionStringSubString") --- ] +allStringInstructions :: [Gene] +allStringInstructions = + map + StateFunc + [ (instructionStringConcat, "instructionStringConcat"), + (instructionStringSwap, "instructionStringSwap"), + (instructionStringInsertString, "instructionStringInsertString"), + (instructionStringFromFirstChar, "instructionStringFromFirstChar"), + (instructionStringFromLastChar, "instructionStringFromLastChar"), + (instructionStringFromNthChar, "instructionStringFromNthChar"), + (instructionStringIndexOfString, "instructionStringIndexOfString"), + (instructionStringContainsString, "instructionStringContainsString"), + (instructionStringSplitOnString, "instructionStringSplitOnString"), + (instructionStringReplaceFirstString, "instructionStringReplaceFirstString"), + (instructionStringReplaceNString, "instructionStringReplaceNString"), + (instructionStringReplaceAllString, "instructionStringReplaceAllString"), + (instructionStringRemoveFirstString, "instructionStringRemoveFirstString"), + (instructionStringRemoveNString, "instructionStringRemoveNString"), + (instructionStringRemoveAllString, "instructionStringRemoveAllString"), + (instructionStringOccurrencesOfString, "instructionStringOccurrencesOfString"), + (instructionStringInsertChar, "instructionStringInsertChar"), + (instructionStringContainsChar, "instructionStringContainsChar"), + (instructionStringIndexOfChar, "instructionStringIndexOfChar"), + (instructionStringSplitOnChar, "instructionStringSplitOnChar"), + (instructionStringReplaceFirstChar, "instructionStringReplaceFirstChar"), + (instructionStringReplaceNChar, "instructionStringReplaceNChar"), + (instructionStringReplaceAllChar, "instructionStringReplaceAllChar"), + (instructionStringRemoveFirstChar, "instructionStringRemoveFirstChar"), + (instructionStringRemoveNChar, "instructionStringRemoveNChar"), + (instructionStringRemoveAllChar, "instructionStringRemoveAllChar"), + (instructionStringOccurrencesOfChar, "instructionStringOccurrencesOfChar"), + (instructionStringReverse, "instructionStringReverse"), + (instructionStringHead, "instructionStringHead"), + (instructionStringTail, "instructionStringTail"), + (instructionStringPrependChar, "instructionStringPrependChar"), + (instructionStringAppendChar, "instructionStringAppendChar"), + (instructionStringRest, "instructionStringRest"), + (instructionStringButLast, "instructionStringButLast"), + (instructionStringDrop, "instructionStringDrop"), + (instructionStringButLastN, "instructionStringButLastN"), + (instructionStringLength, "instructionStringLength"), + (instructionStringMakeEmpty, "instructionStringMakeEmpty"), + (instructionStringIsEmptyString, "instructionStringIsEmptyString"), + (instructionStringRemoveNth, "instructionStringRemoveNth"), + (instructionStringSetNth, "instructionStringSetNth"), + (instructionStringStripWhitespace, "instructionStringStripWhitespace"), + (instructionStringFromBool, "instructionStringFromBool"), + (instructionStringFromInt, "instructionStringFromInt"), + (instructionStringFromFloat, "instructionStringFromFloat"), + (instructionStringFromChar, "instructionStringFromChar"), + (instructionStringPop, "instructionStringPop"), + (instructionStringDup, "instructionStringDup"), + (instructionStringDupN, "instructionStringDupN"), + (instructionStringSwap, "instructionStringSwap"), + (instructionStringRot, "instructionStringRot"), + (instructionStringFlush, "instructionStringFlush"), + (instructionStringEq, "instructionStringEq"), + (instructionStringStackDepth, "instructionStringStackDepth"), + (instructionStringYank, "instructionStringYank"), + (instructionStringYankDup, "instructionStringYankDup"), + (instructionStringShove, "instructionStringShove"), + (instructionStringShoveDup, "instructionStringShoveDup"), + (instructionStringIsStackEmpty, "instructionStringIsStackEmpty"), + (instructionStringSort, "instructionStringSort"), + (instructionStringSortReverse, "instructionStringSortReverse"), + (instructionStringDupItems, "instructionStringDupItems"), + (instructionStringParseToChar, "instructionStringParseToChar"), + (instructionStringSubString, "instructionStringSubString") + ] --- allVectorIntInstructions :: [Gene] --- allVectorIntInstructions = map StateFunc [ --- (instructionVectorIntConcat, "instructionVectorIntConcat"), --- (instructionVectorIntConj, "instructionVectorIntConj"), --- (instructionVectorIntTakeN, "instructionVectorIntTakeN"), --- (instructionVectorIntSubVector, "instructionVectorIntSubVector"), --- (instructionVectorIntFirst, "instructionVectorIntFirst"), --- (instructionVectorIntLast, "instructionVectorIntLast"), --- (instructionVectorIntNth, "instructionVectorIntNth"), --- (instructionVectorIntRest, "instructionVectorIntRest"), --- (instructionVectorIntButLast, "instructionVectorIntButLast"), --- (instructionVectorIntLength, "instructionVectorIntLength"), --- (instructionVectorIntReverse, "instructionVectorIntReverse"), --- (instructionVectorIntPushAll, "instructionVectorIntPushAll"), --- (instructionVectorIntMakeEmpty, "instructionVectorIntMakeEmpty"), --- (instructionVectorIntIsEmpty, "instructionVectorIntIsEmpty"), --- (instructionVectorIntIndexOf, "instructionVectorIntIndexOf"), --- (instructionVectorIntOccurrencesOf, "instructionVectorIntOccurrencesOf"), --- (instructionVectorIntSetNth, "instructionVectorIntSetNth"), --- (instructionVectorIntReplace, "instructionVectorIntReplace"), --- (instructionVectorIntReplaceFirst, "instructionVectorIntReplaceFirst"), --- (instructionVectorIntRemove, "instructionVectorIntRemove"), --- (instructionVectorIntIterate, "instructionVectorIntIterate"), --- (instructionVectorIntPop, "instructionVectorIntPop"), --- (instructionVectorIntDup, "instructionVectorIntDup"), --- (instructionVectorIntDupN, "instructionVectorIntDupN"), --- (instructionVectorIntSwap, "instructionVectorIntSwap"), --- (instructionVectorIntRot, "instructionVectorIntRot"), --- (instructionVectorIntFlush, "instructionVectorIntFlush"), --- (instructionVectorIntEq, "instructionVectorIntEq"), --- (instructionVectorIntStackDepth, "instructionVectorIntStackDepth"), --- (instructionVectorIntYank, "instructionVectorIntYank"), --- (instructionVectorIntYankDup, "instructionVectorIntYankDup"), --- (instructionVectorIntShove, "instructionVectorIntShove"), --- (instructionVectorIntShoveDup, "instructionVectorIntShoveDup"), --- (instructionVectorIntIsStackEmpty, "instructionVectorIntIsStackEmpty"), --- (instructionVectorIntSort, "instructionVectorIntSort"), --- (instructionVectorIntSortReverse, "instructionVectorIntSortReverse"), --- (instructionVectorIntDupItems, "instructionVectorIntDupItems") --- ] +allVectorIntInstructions :: [Gene] +allVectorIntInstructions = + map + StateFunc + [ (instructionVectorIntConcat, "instructionVectorIntConcat"), + (instructionVectorIntConj, "instructionVectorIntConj"), + (instructionVectorIntConjEnd, "instructionVectorIntConjEnd"), + (instructionVectorIntTakeN, "instructionVectorIntTakeN"), + (instructionVectorIntSubVector, "instructionVectorIntSubVector"), + (instructionVectorIntFirst, "instructionVectorIntFirst"), + (instructionVectorIntFromFirstPrim, "instructionVectorFirstFromPrim"), + (instructionVectorIntFromPrim, "instructionVectorFromPrim"), + (instructionVectorIntLast, "instructionVectorIntLast"), + (instructionVectorIntFromLastPrim, "instructionVectorIntFromLastPrim"), + (instructionVectorIntNth, "instructionVectorIntNth"), + (instructionVectorIntFromNthPrim, "instructionVectorIntFromNthPrim"), + (instructionVectorIntRest, "instructionVectorIntRest"), + (instructionVectorIntButLast, "instructionVectorIntButLast"), + (instructionVectorIntDrop, "instructionVectorIntDrop"), + (instructionVectorIntDropR, "instructionVectorIntDropR"), + (instructionVectorIntLength, "instructionVectorIntLength"), + (instructionVectorIntReverse, "instructionVectorIntReverse"), + (instructionVectorIntPushAll, "instructionVectorIntPushAll"), + (instructionVectorIntMakeEmpty, "instructionVectorIntMakeEmpty"), + (instructionVectorIntIsEmpty, "instructionVectorIntIsEmpty"), + (instructionVectorIntContains, "instructionVectorIntContains"), + (instructionVectorIntContainsVectorInt, "instructionVectorIntContainsVectorInt"), + (instructionVectorIntIndexOf, "instructionVectorIntIndexOf"), + (instructionVectorIntIndexOfVectorInt, "instructionVectorIntIndexOfVectorInt"), + (instructionVectorIntOccurrencesOf, "instructionVectorIntOccurrencesOf"), + (instructionVectorIntOccurrencesOfVectorInt, "instructionVectorIntOccurrencesOfVectorInt"), + (instructionVectorIntParseToInt, "instructionVectorIntParseToInt"), + (instructionVectorIntSetNth, "instructionVectorIntSetNth"), + (instructionVectorIntSplitOn, "instructionVectorIntSplitOn"), + (instructionVectorIntSplitOnVectorInt, "instructionVectorIntSplitOnVectorInt"), + (instructionVectorIntReplaceFirst, "instructionVectorIntReplaceFirst"), + (instructionVectorIntReplaceN, "instructionVectorIntReplaceN"), + (instructionVectorIntReplaceAll, "instructionVectorIntReplaceAll"), + (instructionVectorIntReplaceFirstVectorInt, "instructionVectorIntReplaceFirstVectorInt"), + (instructionVectorIntReplaceVectorIntN, "instructionVectorIntReplaceVectorIntN"), + (instructionVectorIntReplaceAllVectorInt, "instructionVectorIntReplaceAllVectorInt"), + (instructionVectorIntRemoveFirst, "instructionVectorIntRemoveFirst"), + (instructionVectorIntRemoveN, "instructionVectorIntRemoveN"), + (instructionVectorIntRemoveAll, "instructionVectorIntRemoveAll"), + (instructionVectorIntRemoveFirstVectorInt, "instructionVectorIntRemoveFirstVectorInt"), + (instructionVectorIntRemoveNVectorInt, "instructionVectorIntRemoveNVectorInt"), + (instructionVectorIntRemoveAllVectorInt, "instructionVectorIntRemoveAllVectorInt"), + (instructionVectorIntIterate, "instructionVectorIntIterate"), + (instructionVectorIntInsert, "instructionVectorIntInsert"), + (instructionVectorIntInsertVectorInt, "instructionVectorIntInsertVectorInt"), + (instructionVectorIntPop, "instructionVectorIntPop"), + (instructionVectorIntDup, "instructionVectorIntDup"), + (instructionVectorIntDupN, "instructionVectorIntDupN"), + (instructionVectorIntSwap, "instructionVectorIntSwap"), + (instructionVectorIntRot, "instructionVectorIntRot"), + (instructionVectorIntFlush, "instructionVectorIntFlush"), + (instructionVectorIntEq, "instructionVectorIntEq"), + (instructionVectorIntStackDepth, "instructionVectorIntStackDepth"), + (instructionVectorIntYank, "instructionVectorIntYank"), + (instructionVectorIntYankDup, "instructionVectorIntYankDup"), + (instructionVectorIntShove, "instructionVectorIntShove"), + (instructionVectorIntShoveDup, "instructionVectorIntShoveDup"), + (instructionVectorIntDupItems, "intsructionVectorIntDupItems"), + (instructionVectorIntIsStackEmpty, "instructionVectorIntIsStackEmpty"), + (instructionVectorIntSort, "instructionVectorIntSort"), + (instructionVectorIntSortReverse, "instructionVectorIntSortReverse"), + (instructionVectorIntDupItems, "instructionVectorIntDupItems") + ] --- allVectorFloatInstructions :: [Gene] --- allVectorFloatInstructions = map StateFunc [ --- (instructionVectorFloatConcat, "instructionVectorFloatConcat"), --- (instructionVectorFloatConj, "instructionVectorFloatConj"), --- (instructionVectorFloatTakeN, "instructionVectorFloatTakeN"), --- (instructionVectorFloatSubVector, "instructionVectorFloatSubVector"), --- (instructionVectorFloatFirst, "instructionVectorFloatFirst"), --- (instructionVectorFloatLast, "instructionVectorFloatLast"), --- (instructionVectorFloatNth, "instructionVectorFloatNth"), --- (instructionVectorFloatRest, "instructionVectorFloatRest"), --- (instructionVectorFloatButLast, "instructionVectorFloatButLast"), --- (instructionVectorFloatLength, "instructionVectorFloatLength"), --- (instructionVectorFloatReverse, "instructionVectorFloatReverse"), --- (instructionVectorFloatPushAll, "instructionVectorFloatPushAll"), --- (instructionVectorFloatMakeEmpty, "instructionVectorFloatMakeEmpty"), --- (instructionVectorFloatIsEmpty, "instructionVectorFloatIsEmpty"), --- (instructionVectorFloatIndexOf, "instructionVectorFloatIndexOf"), --- (instructionVectorFloatOccurrencesOf, "instructionVectorFloatOccurrencesOf"), --- (instructionVectorFloatSetNth, "instructionVectorFloatSetNth"), --- (instructionVectorFloatReplace, "instructionVectorFloatReplace"), --- (instructionVectorFloatReplaceFirst, "instructionVectorFloatReplaceFirst"), --- (instructionVectorFloatRemove, "instructionVectorFloatRemove"), --- (instructionVectorFloatIterate, "instructionVectorFloatIterate"), --- (instructionVectorFloatPop, "instructionVectorFloatPop"), --- (instructionVectorFloatDup, "instructionVectorFloatDup"), --- (instructionVectorFloatDupN, "instructionVectorFloatDupN"), --- (instructionVectorFloatSwap, "instructionVectorFloatSwap"), --- (instructionVectorFloatRot, "instructionVectorFloatRot"), --- (instructionVectorFloatFlush, "instructionVectorFloatFlush"), --- (instructionVectorFloatEq, "instructionVectorFloatEq"), --- (instructionVectorFloatStackDepth, "instructionVectorFloatStackDepth"), --- (instructionVectorFloatYank, "instructionVectorFloatYank"), --- (instructionVectorFloatYankDup, "instructionVectorFloatYankDup"), --- (instructionVectorFloatShove, "instructionVectorFloatShove"), --- (instructionVectorFloatShoveDup, "instructionVectorFloatShoveDup"), --- (instructionVectorFloatIsStackEmpty, "instructionVectorFloatIsStackEmpty"), --- (instructionVectorFloatSort, "instructionVectorFloatSort"), --- (instructionVectorFloatSortReverse, "instructionVectorFloatSortReverse"), --- (instructionVectorFloatDupItems, "instructionVectorFloatDupItems") --- ] +allVectorFloatInstructions :: [Gene] +allVectorFloatInstructions = + map + StateFunc + [ (instructionVectorFloatConcat, "instructionVectorFloatConcat"), + (instructionVectorFloatConj, "instructionVectorFloatConj"), + (instructionVectorFloatConjEnd, "instructionVectorFloatConjEnd"), + (instructionVectorFloatTakeN, "instructionVectorFloatTakeN"), + (instructionVectorFloatSubVector, "instructionVectorFloatSubVector"), + (instructionVectorFloatFirst, "instructionVectorFloatFirst"), + (instructionVectorFloatFromFirstPrim, "instructionVectorFirstFromPrim"), + (instructionVectorFloatFromPrim, "instructionVectorFromPrim"), + (instructionVectorFloatLast, "instructionVectorFloatLast"), + (instructionVectorFloatFromLastPrim, "instructionVectorFloatFromLastPrim"), + (instructionVectorFloatNth, "instructionVectorFloatNth"), + (instructionVectorFloatFromNthPrim, "instructionVectorFloatFromNthPrim"), + (instructionVectorFloatRest, "instructionVectorFloatRest"), + (instructionVectorFloatButLast, "instructionVectorFloatButLast"), + (instructionVectorFloatDrop, "instructionVectorFloatDrop"), + (instructionVectorFloatDropR, "instructionVectorFloatDropR"), + (instructionVectorFloatLength, "instructionVectorFloatLength"), + (instructionVectorFloatReverse, "instructionVectorFloatReverse"), + (instructionVectorFloatPushAll, "instructionVectorFloatPushAll"), + (instructionVectorFloatMakeEmpty, "instructionVectorFloatMakeEmpty"), + (instructionVectorFloatIsEmpty, "instructionVectorFloatIsEmpty"), + (instructionVectorFloatContains, "instructionVectorFloatContains"), + (instructionVectorFloatContainsVectorFloat, "instructionVectorFloatContainsVectorFloat"), + (instructionVectorFloatIndexOf, "instructionVectorFloatIndexOf"), + (instructionVectorFloatIndexOfVectorFloat, "instructionVectorFloatIndexOfVectorFloat"), + (instructionVectorFloatOccurrencesOf, "instructionVectorFloatOccurrencesOf"), + (instructionVectorFloatOccurrencesOfVectorFloat, "instructionVectorFloatOccurrencesOfVectorFloat"), + (instructionVectorFloatParseToFloat, "instructionVectorFloatParseToFloat"), + (instructionVectorFloatSetNth, "instructionVectorFloatSetNth"), + (instructionVectorFloatSplitOn, "instructionVectorFloatSplitOn"), + (instructionVectorFloatSplitOnVectorFloat, "instructionVectorFloatSplitOnVectorFloat"), + (instructionVectorFloatReplaceFirst, "instructionVectorFloatReplaceFirst"), + (instructionVectorFloatReplaceN, "instructionVectorFloatReplaceN"), + (instructionVectorFloatReplaceAll, "instructionVectorFloatReplaceAll"), + (instructionVectorFloatReplaceFirstVectorFloat, "instructionVectorFloatReplaceFirstVectorFloat"), + (instructionVectorFloatReplaceVectorFloatN, "instructionVectorFloatReplaceVectorFloatN"), + (instructionVectorFloatReplaceAllVectorFloat, "instructionVectorFloatReplaceAllVectorFloat"), + (instructionVectorFloatRemoveFirst, "instructionVectorFloatRemoveFirst"), + (instructionVectorFloatRemoveN, "instructionVectorFloatRemoveN"), + (instructionVectorFloatRemoveAll, "instructionVectorFloatRemoveAll"), + (instructionVectorFloatRemoveFirstVectorFloat, "instructionVectorFloatRemoveFirstVectorFloat"), + (instructionVectorFloatRemoveNVectorFloat, "instructionVectorFloatRemoveNVectorFloat"), + (instructionVectorFloatRemoveAllVectorFloat, "instructionVectorFloatRemoveAllVectorFloat"), + (instructionVectorFloatIterate, "instructionVectorFloatIterate"), + (instructionVectorFloatInsert, "instructionVectorFloatInsert"), + (instructionVectorFloatInsertVectorFloat, "instructionVectorFloatInsertVectorFloat"), + (instructionVectorFloatPop, "instructionVectorFloatPop"), + (instructionVectorFloatDup, "instructionVectorFloatDup"), + (instructionVectorFloatDupN, "instructionVectorFloatDupN"), + (instructionVectorFloatSwap, "instructionVectorFloatSwap"), + (instructionVectorFloatRot, "instructionVectorFloatRot"), + (instructionVectorFloatFlush, "instructionVectorFloatFlush"), + (instructionVectorFloatEq, "instructionVectorFloatEq"), + (instructionVectorFloatStackDepth, "instructionVectorFloatStackDepth"), + (instructionVectorFloatYank, "instructionVectorFloatYank"), + (instructionVectorFloatYankDup, "instructionVectorFloatYankDup"), + (instructionVectorFloatShove, "instructionVectorFloatShove"), + (instructionVectorFloatShoveDup, "instructionVectorFloatShoveDup"), + (instructionVectorFloatDupItems, "intsructionVectorFloatDupItems"), + (instructionVectorFloatIsStackEmpty, "instructionVectorFloatIsStackEmpty"), + (instructionVectorFloatSort, "instructionVectorFloatSort"), + (instructionVectorFloatSortReverse, "instructionVectorFloatSortReverse"), + (instructionVectorFloatDupItems, "instructionVectorFloatDupItems") + ] --- allVectorCharInstructions :: [Gene] --- allVectorCharInstructions = map StateFunc [ --- (instructionVectorCharConcat, "instructionVectorCharConcat"), --- (instructionVectorCharConj, "instructionVectorCharConj"), --- (instructionVectorCharTakeN, "instructionVectorCharTakeN"), --- (instructionVectorCharSubVector, "instructionVectorCharSubVector"), --- (instructionVectorCharFirst, "instructionVectorCharFirst"), --- (instructionVectorCharLast, "instructionVectorCharLast"), --- (instructionVectorCharNth, "instructionVectorCharNth"), --- (instructionVectorCharRest, "instructionVectorCharRest"), --- (instructionVectorCharButLast, "instructionVectorCharButLast"), --- (instructionVectorCharLength, "instructionVectorCharLength"), --- (instructionVectorCharReverse, "instructionVectorCharReverse"), --- (instructionVectorCharPushAll, "instructionVectorCharPushAll"), --- (instructionVectorCharMakeEmpty, "instructionVectorCharMakeEmpty"), --- (instructionVectorCharIsEmpty, "instructionVectorCharIsEmpty"), --- (instructionVectorCharIndexOf, "instructionVectorCharIndexOf"), --- (instructionVectorCharOccurrencesOf, "instructionVectorCharOccurrencesOf"), --- (instructionVectorCharSetNth, "instructionVectorCharSetNth"), --- (instructionVectorCharReplace, "instructionVectorCharReplace"), --- (instructionVectorCharReplaceFirst, "instructionVectorCharReplaceFirst"), --- (instructionVectorCharRemove, "instructionVectorCharRemove"), --- (instructionVectorCharIterate, "instructionVectorCharIterate"), --- (instructionVectorCharPop, "instructionVectorCharPop"), --- (instructionVectorCharDup, "instructionVectorCharDup"), --- (instructionVectorCharDupN, "instructionVectorCharDupN"), --- (instructionVectorCharSwap, "instructionVectorCharSwap"), --- (instructionVectorCharRot, "instructionVectorCharRot"), --- (instructionVectorCharFlush, "instructionVectorCharFlush"), --- (instructionVectorCharEq, "instructionVectorCharEq"), --- (instructionVectorCharStackDepth, "instructionVectorCharStackDepth"), --- (instructionVectorCharYank, "instructionVectorCharYank"), --- (instructionVectorCharYankDup, "instructionVectorCharYankDup"), --- (instructionVectorCharShove, "instructionVectorCharShove"), --- (instructionVectorCharShoveDup, "instructionVectorCharShoveDup"), --- (instructionVectorCharIsStackEmpty, "instructionVectorCharIsStackEmpty"), --- (instructionVectorCharSort, "instructionVectorCharSort"), --- (instructionVectorCharSortReverse, "instructionVectorCharSortReverse"), --- (instructionVectorCharDupItems, "instructionVectorCharDupItems") --- ] +allVectorCharInstructions :: [Gene] +allVectorCharInstructions = + map + StateFunc + [ (instructionVectorCharConcat, "instructionVectorCharConcat"), + (instructionVectorCharConj, "instructionVectorCharConj"), + (instructionVectorCharConjEnd, "instructionVectorCharConjEnd"), + (instructionVectorCharTakeN, "instructionVectorCharTakeN"), + (instructionVectorCharSubVector, "instructionVectorCharSubVector"), + (instructionVectorCharFirst, "instructionVectorCharFirst"), + (instructionVectorCharFromFirstPrim, "instructionVectorFirstFromPrim"), + (instructionVectorCharFromPrim, "instructionVectorFromPrim"), + (instructionVectorCharLast, "instructionVectorCharLast"), + (instructionVectorCharFromLastPrim, "instructionVectorCharFromLastPrim"), + (instructionVectorCharNth, "instructionVectorCharNth"), + (instructionVectorCharFromNthPrim, "instructionVectorCharFromNthPrim"), + (instructionVectorCharRest, "instructionVectorCharRest"), + (instructionVectorCharButLast, "instructionVectorCharButLast"), + (instructionVectorCharDrop, "instructionVectorCharDrop"), + (instructionVectorCharDropR, "instructionVectorCharDropR"), + (instructionVectorCharLength, "instructionVectorCharLength"), + (instructionVectorCharReverse, "instructionVectorCharReverse"), + (instructionVectorCharPushAll, "instructionVectorCharPushAll"), + (instructionVectorCharMakeEmpty, "instructionVectorCharMakeEmpty"), + (instructionVectorCharIsEmpty, "instructionVectorCharIsEmpty"), + (instructionVectorCharContains, "instructionVectorCharContains"), + (instructionVectorCharContainsVectorChar, "instructionVectorCharContainsVectorChar"), + (instructionVectorCharIndexOf, "instructionVectorCharIndexOf"), + (instructionVectorCharIndexOfVectorChar, "instructionVectorCharIndexOfVectorChar"), + (instructionVectorCharOccurrencesOf, "instructionVectorCharOccurrencesOf"), + (instructionVectorCharOccurrencesOfVectorChar, "instructionVectorCharOccurrencesOfVectorChar"), + (instructionVectorCharParseToChar, "instructionVectorCharParseToChar"), + (instructionVectorCharSetNth, "instructionVectorCharSetNth"), + (instructionVectorCharSplitOn, "instructionVectorCharSplitOn"), + (instructionVectorCharSplitOnVectorChar, "instructionVectorCharSplitOnVectorChar"), + (instructionVectorCharReplaceFirst, "instructionVectorCharReplaceFirst"), + (instructionVectorCharReplaceN, "instructionVectorCharReplaceN"), + (instructionVectorCharReplaceAll, "instructionVectorCharReplaceAll"), + (instructionVectorCharReplaceFirstVectorChar, "instructionVectorCharReplaceFirstVectorChar"), + (instructionVectorCharReplaceVectorCharN, "instructionVectorCharReplaceVectorCharN"), + (instructionVectorCharReplaceAllVectorChar, "instructionVectorCharReplaceAllVectorChar"), + (instructionVectorCharRemoveFirst, "instructionVectorCharRemoveFirst"), + (instructionVectorCharRemoveN, "instructionVectorCharRemoveN"), + (instructionVectorCharRemoveAll, "instructionVectorCharRemoveAll"), + (instructionVectorCharRemoveFirstVectorChar, "instructionVectorCharRemoveFirstVectorChar"), + (instructionVectorCharRemoveNVectorChar, "instructionVectorCharRemoveNVectorChar"), + (instructionVectorCharRemoveAllVectorChar, "instructionVectorCharRemoveAllVectorChar"), + (instructionVectorCharIterate, "instructionVectorCharIterate"), + (instructionVectorCharInsert, "instructionVectorCharInsert"), + (instructionVectorCharInsertVectorChar, "instructionVectorCharInsertVectorChar"), + (instructionVectorCharPop, "instructionVectorCharPop"), + (instructionVectorCharDup, "instructionVectorCharDup"), + (instructionVectorCharDupN, "instructionVectorCharDupN"), + (instructionVectorCharSwap, "instructionVectorCharSwap"), + (instructionVectorCharRot, "instructionVectorCharRot"), + (instructionVectorCharFlush, "instructionVectorCharFlush"), + (instructionVectorCharEq, "instructionVectorCharEq"), + (instructionVectorCharStackDepth, "instructionVectorCharStackDepth"), + (instructionVectorCharYank, "instructionVectorCharYank"), + (instructionVectorCharYankDup, "instructionVectorCharYankDup"), + (instructionVectorCharShove, "instructionVectorCharShove"), + (instructionVectorCharShoveDup, "instructionVectorCharShoveDup"), + (instructionVectorCharDupItems, "intsructionVectorCharDupItems"), + (instructionVectorCharIsStackEmpty, "instructionVectorCharIsStackEmpty"), + (instructionVectorCharSort, "instructionVectorCharSort"), + (instructionVectorCharSortReverse, "instructionVectorCharSortReverse"), + (instructionVectorCharDupItems, "instructionVectorCharDupItems") + ] --- allVectorStringInstructions :: [Gene] --- allVectorStringInstructions = map StateFunc [ --- (instructionVectorStringConcat, "instructionVectorStringConcat"), --- (instructionVectorStringConj, "instructionVectorStringConj"), --- (instructionVectorStringTakeN, "instructionVectorStringTakeN"), --- (instructionVectorStringSubVector, "instructionVectorStringSubVector"), --- (instructionVectorStringFirst, "instructionVectorStringFirst"), --- (instructionVectorStringLast, "instructionVectorStringLast"), --- (instructionVectorStringNth, "instructionVectorStringNth"), --- (instructionVectorStringRest, "instructionVectorStringRest"), --- (instructionVectorStringButLast, "instructionVectorStringButLast"), --- (instructionVectorStringLength, "instructionVectorStringLength"), --- (instructionVectorStringReverse, "instructionVectorStringReverse"), --- (instructionVectorStringPushAll, "instructionVectorStringPushAll"), --- (instructionVectorStringMakeEmpty, "instructionVectorStringMakeEmpty"), --- (instructionVectorStringIsEmpty, "instructionVectorStringIsEmpty"), --- (instructionVectorStringIndexOf, "instructionVectorStringIndexOf"), --- (instructionVectorStringOccurrencesOf, "instructionVectorStringOccurrencesOf"), --- (instructionVectorStringSetNth, "instructionVectorStringSetNth"), --- (instructionVectorStringReplace, "instructionVectorStringReplace"), --- (instructionVectorStringReplaceFirst, "instructionVectorStringReplaceFirst"), --- (instructionVectorStringRemove, "instructionVectorStringRemove"), --- (instructionVectorStringIterate, "instructionVectorStringIterate"), --- (instructionVectorStringPop, "instructionVectorStringPop"), --- (instructionVectorStringDup, "instructionVectorStringDup"), --- (instructionVectorStringDupN, "instructionVectorStringDupN"), --- (instructionVectorStringSwap, "instructionVectorStringSwap"), --- (instructionVectorStringRot, "instructionVectorStringRot"), --- (instructionVectorStringFlush, "instructionVectorStringFlush"), --- (instructionVectorStringEq, "instructionVectorStringEq"), --- (instructionVectorStringStackDepth, "instructionVectorStringStackDepth"), --- (instructionVectorStringYank, "instructionVectorStringYank"), --- (instructionVectorStringYankDup, "instructionVectorStringYankDup"), --- (instructionVectorStringShove, "instructionVectorStringShove"), --- (instructionVectorStringShoveDup, "instructionVectorStringShoveDup"), --- (instructionVectorStringIsStackEmpty, "instructionVectorStringIsStackEmpty"), --- (instructionVectorStringSort, "instructionVectorStringSort"), --- (instructionVectorStringSortReverse, "instructionVectorStringSortReverse"), --- (instructionVectorStringDupItems, "instructionVectorStringDupItems") --- ] +allVectorStringInstructions :: [Gene] +allVectorStringInstructions = + map + StateFunc + [ (instructionVectorStringConcat, "instructionVectorStringConcat"), + (instructionVectorStringConj, "instructionVectorStringConj"), + (instructionVectorStringConjEnd, "instructionVectorStringConjEnd"), + (instructionVectorStringTakeN, "instructionVectorStringTakeN"), + (instructionVectorStringSubVector, "instructionVectorStringSubVector"), + (instructionVectorStringFirst, "instructionVectorStringFirst"), + (instructionVectorStringFromFirstPrim, "instructionVectorFirstFromPrim"), + (instructionVectorStringFromPrim, "instructionVectorFromPrim"), + (instructionVectorStringLast, "instructionVectorStringLast"), + (instructionVectorStringFromLastPrim, "instructionVectorStringFromLastPrim"), + (instructionVectorStringNth, "instructionVectorStringNth"), + (instructionVectorStringFromNthPrim, "instructionVectorStringFromNthPrim"), + (instructionVectorStringRest, "instructionVectorStringRest"), + (instructionVectorStringButLast, "instructionVectorStringButLast"), + (instructionVectorStringDrop, "instructionVectorStringDrop"), + (instructionVectorStringDropR, "instructionVectorStringDropR"), + (instructionVectorStringLength, "instructionVectorStringLength"), + (instructionVectorStringReverse, "instructionVectorStringReverse"), + (instructionVectorStringPushAll, "instructionVectorStringPushAll"), + (instructionVectorStringMakeEmpty, "instructionVectorStringMakeEmpty"), + (instructionVectorStringIsEmpty, "instructionVectorStringIsEmpty"), + (instructionVectorStringContains, "instructionVectorStringContains"), + (instructionVectorStringContainsVectorString, "instructionVectorStringContainsVectorString"), + (instructionVectorStringIndexOf, "instructionVectorStringIndexOf"), + (instructionVectorStringIndexOfVectorString, "instructionVectorStringIndexOfVectorString"), + (instructionVectorStringOccurrencesOf, "instructionVectorStringOccurrencesOf"), + (instructionVectorStringOccurrencesOfVectorString, "instructionVectorStringOccurrencesOfVectorString"), + (instructionVectorStringParseToString, "instructionVectorStringParseToString"), + (instructionVectorStringSetNth, "instructionVectorStringSetNth"), + (instructionVectorStringSplitOn, "instructionVectorStringSplitOn"), + (instructionVectorStringSplitOnVectorString, "instructionVectorStringSplitOnVectorString"), + (instructionVectorStringReplaceFirst, "instructionVectorStringReplaceFirst"), + (instructionVectorStringReplaceN, "instructionVectorStringReplaceN"), + (instructionVectorStringReplaceAll, "instructionVectorStringReplaceAll"), + (instructionVectorStringReplaceFirstVectorString, "instructionVectorStringReplaceFirstVectorString"), + (instructionVectorStringReplaceVectorStringN, "instructionVectorStringReplaceVectorStringN"), + (instructionVectorStringReplaceAllVectorString, "instructionVectorStringReplaceAllVectorString"), + (instructionVectorStringRemoveFirst, "instructionVectorStringRemoveFirst"), + (instructionVectorStringRemoveN, "instructionVectorStringRemoveN"), + (instructionVectorStringRemoveAll, "instructionVectorStringRemoveAll"), + (instructionVectorStringRemoveFirstVectorString, "instructionVectorStringRemoveFirstVectorString"), + (instructionVectorStringRemoveNVectorString, "instructionVectorStringRemoveNVectorString"), + (instructionVectorStringRemoveAllVectorString, "instructionVectorStringRemoveAllVectorString"), + (instructionVectorStringIterate, "instructionVectorStringIterate"), + (instructionVectorStringInsert, "instructionVectorStringInsert"), + (instructionVectorStringInsertVectorString, "instructionVectorStringInsertVectorString"), + (instructionVectorStringPop, "instructionVectorStringPop"), + (instructionVectorStringDup, "instructionVectorStringDup"), + (instructionVectorStringDupN, "instructionVectorStringDupN"), + (instructionVectorStringSwap, "instructionVectorStringSwap"), + (instructionVectorStringRot, "instructionVectorStringRot"), + (instructionVectorStringFlush, "instructionVectorStringFlush"), + (instructionVectorStringEq, "instructionVectorStringEq"), + (instructionVectorStringStackDepth, "instructionVectorStringStackDepth"), + (instructionVectorStringYank, "instructionVectorStringYank"), + (instructionVectorStringYankDup, "instructionVectorStringYankDup"), + (instructionVectorStringShove, "instructionVectorStringShove"), + (instructionVectorStringShoveDup, "instructionVectorStringShoveDup"), + (instructionVectorStringDupItems, "intsructionVectorStringDupItems"), + (instructionVectorStringIsStackEmpty, "instructionVectorStringIsStackEmpty"), + (instructionVectorStringSort, "instructionVectorStringSort"), + (instructionVectorStringSortReverse, "instructionVectorStringSortReverse"), + (instructionVectorStringDupItems, "instructionVectorStringDupItems") + ] --- allVectorBoolInstructions :: [Gene] --- allVectorBoolInstructions = map StateFunc [ --- (instructionVectorBoolConcat, "instructionVectorBoolConcat"), --- (instructionVectorBoolConj, "instructionVectorBoolConj"), --- (instructionVectorBoolTakeN, "instructionVectorBoolTakeN"), --- (instructionVectorBoolSubVector, "instructionVectorBoolSubVector"), --- (instructionVectorBoolFirst, "instructionVectorBoolFirst"), --- (instructionVectorBoolLast, "instructionVectorBoolLast"), --- (instructionVectorBoolNth, "instructionVectorBoolNth"), --- (instructionVectorBoolRest, "instructionVectorBoolRest"), --- (instructionVectorBoolButLast, "instructionVectorBoolButLast"), --- (instructionVectorBoolLength, "instructionVectorBoolLength"), --- (instructionVectorBoolReverse, "instructionVectorBoolReverse"), --- (instructionVectorBoolPushAll, "instructionVectorBoolPushAll"), --- (instructionVectorBoolMakeEmpty, "instructionVectorBoolMakeEmpty"), --- (instructionVectorBoolIsEmpty, "instructionVectorBoolIsEmpty"), --- (instructionVectorBoolIndexOf, "instructionVectorBoolIndexOf"), --- (instructionVectorBoolOccurrencesOf, "instructionVectorBoolOccurrencesOf"), --- (instructionVectorBoolSetNth, "instructionVectorBoolSetNth"), --- (instructionVectorBoolReplace, "instructionVectorBoolReplace"), --- (instructionVectorBoolReplaceFirst, "instructionVectorBoolReplaceFirst"), --- (instructionVectorBoolRemove, "instructionVectorBoolRemove"), --- (instructionVectorBoolIterate, "instructionVectorBoolIterate"), --- (instructionVectorBoolPop, "instructionVectorBoolPop"), --- (instructionVectorBoolDup, "instructionVectorBoolDup"), --- (instructionVectorBoolDupN, "instructionVectorBoolDupN"), --- (instructionVectorBoolSwap, "instructionVectorBoolSwap"), --- (instructionVectorBoolRot, "instructionVectorBoolRot"), --- (instructionVectorBoolFlush, "instructionVectorBoolFlush"), --- (instructionVectorBoolEq, "instructionVectorBoolEq"), --- (instructionVectorBoolStackDepth, "instructionVectorBoolStackDepth"), --- (instructionVectorBoolYank, "instructionVectorBoolYank"), --- (instructionVectorBoolYankDup, "instructionVectorBoolYankDup"), --- (instructionVectorBoolShove, "instructionVectorBoolShove"), --- (instructionVectorBoolShoveDup, "instructionVectorBoolShoveDup"), --- (instructionVectorBoolIsStackEmpty, "instructionVectorBoolIsStackEmpty"), --- (instructionVectorBoolSort, "instructionVectorBoolSort"), --- (instructionVectorBoolSortReverse, "instructionVectorBoolSortReverse"), --- (instructionVectorBoolDupItems, "instructionVectorBoolDupItems") --- ] +allVectorBoolInstructions :: [Gene] +allVectorBoolInstructions = + map + StateFunc + [ (instructionVectorBoolConcat, "instructionVectorBoolConcat"), + (instructionVectorBoolConj, "instructionVectorBoolConj"), + (instructionVectorBoolConjEnd, "instructionVectorBoolConjEnd"), + (instructionVectorBoolTakeN, "instructionVectorBoolTakeN"), + (instructionVectorBoolSubVector, "instructionVectorBoolSubVector"), + (instructionVectorBoolFirst, "instructionVectorBoolFirst"), + (instructionVectorBoolFromFirstPrim, "instructionVectorFirstFromPrim"), + (instructionVectorBoolFromPrim, "instructionVectorFromPrim"), + (instructionVectorBoolLast, "instructionVectorBoolLast"), + (instructionVectorBoolFromLastPrim, "instructionVectorBoolFromLastPrim"), + (instructionVectorBoolNth, "instructionVectorBoolNth"), + (instructionVectorBoolFromNthPrim, "instructionVectorBoolFromNthPrim"), + (instructionVectorBoolRest, "instructionVectorBoolRest"), + (instructionVectorBoolButLast, "instructionVectorBoolButLast"), + (instructionVectorBoolDrop, "instructionVectorBoolDrop"), + (instructionVectorBoolDropR, "instructionVectorBoolDropR"), + (instructionVectorBoolLength, "instructionVectorBoolLength"), + (instructionVectorBoolReverse, "instructionVectorBoolReverse"), + (instructionVectorBoolPushAll, "instructionVectorBoolPushAll"), + (instructionVectorBoolMakeEmpty, "instructionVectorBoolMakeEmpty"), + (instructionVectorBoolIsEmpty, "instructionVectorBoolIsEmpty"), + (instructionVectorBoolContains, "instructionVectorBoolContains"), + (instructionVectorBoolContainsVectorBool, "instructionVectorBoolContainsVectorBool"), + (instructionVectorBoolIndexOf, "instructionVectorBoolIndexOf"), + (instructionVectorBoolIndexOfVectorBool, "instructionVectorBoolIndexOfVectorBool"), + (instructionVectorBoolOccurrencesOf, "instructionVectorBoolOccurrencesOf"), + (instructionVectorBoolOccurrencesOfVectorBool, "instructionVectorBoolOccurrencesOfVectorBool"), + (instructionVectorBoolParseToBool, "instructionVectorBoolParseToBool"), + (instructionVectorBoolSetNth, "instructionVectorBoolSetNth"), + (instructionVectorBoolSplitOn, "instructionVectorBoolSplitOn"), + (instructionVectorBoolSplitOnVectorBool, "instructionVectorBoolSplitOnVectorBool"), + (instructionVectorBoolReplaceFirst, "instructionVectorBoolReplaceFirst"), + (instructionVectorBoolReplaceN, "instructionVectorBoolReplaceN"), + (instructionVectorBoolReplaceAll, "instructionVectorBoolReplaceAll"), + (instructionVectorBoolReplaceFirstVectorBool, "instructionVectorBoolReplaceFirstVectorBool"), + (instructionVectorBoolReplaceVectorBoolN, "instructionVectorBoolReplaceVectorBoolN"), + (instructionVectorBoolReplaceAllVectorBool, "instructionVectorBoolReplaceAllVectorBool"), + (instructionVectorBoolRemoveFirst, "instructionVectorBoolRemoveFirst"), + (instructionVectorBoolRemoveN, "instructionVectorBoolRemoveN"), + (instructionVectorBoolRemoveAll, "instructionVectorBoolRemoveAll"), + (instructionVectorBoolRemoveFirstVectorBool, "instructionVectorBoolRemoveFirstVectorBool"), + (instructionVectorBoolRemoveNVectorBool, "instructionVectorBoolRemoveNVectorBool"), + (instructionVectorBoolRemoveAllVectorBool, "instructionVectorBoolRemoveAllVectorBool"), + (instructionVectorBoolIterate, "instructionVectorBoolIterate"), + (instructionVectorBoolInsert, "instructionVectorBoolInsert"), + (instructionVectorBoolInsertVectorBool, "instructionVectorBoolInsertVectorBool"), + (instructionVectorBoolPop, "instructionVectorBoolPop"), + (instructionVectorBoolDup, "instructionVectorBoolDup"), + (instructionVectorBoolDupN, "instructionVectorBoolDupN"), + (instructionVectorBoolSwap, "instructionVectorBoolSwap"), + (instructionVectorBoolRot, "instructionVectorBoolRot"), + (instructionVectorBoolFlush, "instructionVectorBoolFlush"), + (instructionVectorBoolEq, "instructionVectorBoolEq"), + (instructionVectorBoolStackDepth, "instructionVectorBoolStackDepth"), + (instructionVectorBoolYank, "instructionVectorBoolYank"), + (instructionVectorBoolYankDup, "instructionVectorBoolYankDup"), + (instructionVectorBoolShove, "instructionVectorBoolShove"), + (instructionVectorBoolShoveDup, "instructionVectorBoolShoveDup"), + (instructionVectorBoolDupItems, "intsructionVectorBoolDupItems"), + (instructionVectorBoolIsStackEmpty, "instructionVectorBoolIsStackEmpty"), + (instructionVectorBoolSort, "instructionVectorBoolSort"), + (instructionVectorBoolSortReverse, "instructionVectorBoolSortReverse"), + (instructionVectorBoolDupItems, "instructionVectorBoolDupItems") + ] --- allInstructions :: [Gene] --- allInstructions = --- allIntInstructions <> --- allFloatInstructions <> --- allBoolInstructions <> --- allCharInstructions <> --- allCodeInstructions <> --- allExecInstructions <> --- allStringInstructions <> --- allVectorIntInstructions <> --- allVectorFloatInstructions <> --- allVectorCharInstructions <> --- allVectorStringInstructions <> --- allVectorBoolInstructions +allInstructions :: [Gene] +allInstructions = + allIntInstructions + <> allFloatInstructions + <> allBoolInstructions + <> allCharInstructions + <> allCodeInstructions + <> allExecInstructions + <> allStringInstructions + <> allVectorIntInstructions + <> allVectorFloatInstructions + <> allVectorCharInstructions + <> allVectorStringInstructions + <> allVectorBoolInstructions diff --git a/src/HushGP/Instructions/StringInstructions.hs b/src/HushGP/Instructions/StringInstructions.hs index cab2a5a..04dcab5 100644 --- a/src/HushGP/Instructions/StringInstructions.hs +++ b/src/HushGP/Instructions/StringInstructions.hs @@ -3,7 +3,6 @@ module HushGP.Instructions.StringInstructions where import HushGP.State import HushGP.Instructions.GenericInstructions import HushGP.Instructions.Utility -import Control.Lens -- |Concats the top two strings on the string stack and pushes the result. instructionStringConcat :: State -> State @@ -221,14 +220,6 @@ instructionStringStripWhitespace :: State -> State instructionStringStripWhitespace state@(State {_string = s1 : ss}) = state{_string = strip s1 : ss} instructionStringStripWhitespace state = state --- |Utility Function: Casts a type based on a lens to a string. Pushes the result --- to the string stack. -instructionStringFromLens :: Show a => Lens' State [a] -> State -> State -instructionStringFromLens accessor state@(State {_string = ss}) = - case uncons (view accessor state) of - Nothing -> state - Just (x1,_) -> state{_string = show x1 : ss} - -- |Converts the top bool from the bool stack to a string. Pushes the result to -- the string stack. instructionStringFromBool :: State -> State diff --git a/src/HushGP/Instructions/Utility.hs b/src/HushGP/Instructions/Utility.hs index 31b0cb5..b58ebb7 100644 --- a/src/HushGP/Instructions/Utility.hs +++ b/src/HushGP/Instructions/Utility.hs @@ -247,3 +247,13 @@ lstrip s = case s of -- this is a tad inefficient rstrip :: String -> String rstrip = reverse . lstrip . reverse + +-- string utility + +-- |Utility Function: Casts a type based on a lens to a string. Pushes the result +-- to the string stack. +instructionStringFromLens :: Show a => Lens' State [a] -> State -> State +instructionStringFromLens accessor state@(State {_string = ss}) = + case uncons (view accessor state) of + Nothing -> state + Just (x1,_) -> state{_string = show x1 : ss} diff --git a/src/HushGP/Push.hs b/src/HushGP/Push.hs index f243be5..342968e 100644 --- a/src/HushGP/Push.hs +++ b/src/HushGP/Push.hs @@ -12,8 +12,8 @@ import HushGP.State -- Everntually, this can be part of the apply func to state helpers, -- which should take the number and type of parameter they have. --- |This is one of the push genome functions itself, not infrastructure. --- Optionally, split this off into independent functions +-- | This is one of the push genome functions itself, not infrastructure. +-- Optionally, split this off into independent functions instructionParameterLoad :: State -> State instructionParameterLoad state@(State {_parameter = (p : _)}) = case p of (GeneInt val) -> state & int .~ val : view int state @@ -32,20 +32,20 @@ instructionParameterLoad state@(State {_parameter = (p : _)}) = case p of (Block xs) -> state & exec .~ xs <> view exec state instructionParameterLoad state = state --- |Loads a genome into the exec stack +-- | Loads a genome into the exec stack loadProgram :: [Gene] -> State -> State loadProgram newstack state = state & exec .~ newstack --- |Takes a Push state, and generates the next push state via: --- If the first item on the EXEC stack is a single instruction --- then pop it and execute it. --- Else if the first item on the EXEC stack is a literal --- then pop it and push it onto the appropriate stack. --- Else (the first item must be a list) pop it and push all of the --- items that it contains back onto the EXEC stack individually, --- in reverse order (so that the item that was first in the list --- ends up on top). --- The empty-stack safety of interpretExec on empty stacks depends on the functions it calls. +-- | Takes a Push state, and generates the next push state via: +-- If the first item on the EXEC stack is a single instruction +-- then pop it and execute it. +-- Else if the first item on the EXEC stack is a literal +-- then pop it and push it onto the appropriate stack. +-- Else (the first item must be a list) pop it and push all of the +-- items that it contains back onto the EXEC stack individually, +-- in reverse order (so that the item that was first in the list +-- ends up on top). +-- The empty-stack safety of interpretExec on empty stacks depends on the functions it calls. interpretExec :: State -> State interpretExec state@(State {_exec = e : es}) = case e of diff --git a/src/HushGP/State.hs b/src/HushGP/State.hs index 62afec4..97f73ce 100644 --- a/src/HushGP/State.hs +++ b/src/HushGP/State.hs @@ -8,10 +8,10 @@ import Data.Map qualified as Map import GHC.Generics import Test.QuickCheck --- |The exec stack must store heterogenous types, --- and we must be able to detect that type at runtime. --- One solution is for the exec stack to be a list of [Gene]. --- The parameter stack could be singular [Gene] or multiple [atomic] types. +-- | The exec stack must store heterogenous types, +-- and we must be able to detect that type at runtime. +-- One solution is for the exec stack to be a list of [Gene]. +-- The parameter stack could be singular [Gene] or multiple [atomic] types. data Gene = GeneInt Int | GeneFloat Float @@ -83,7 +83,7 @@ instance Arbitrary Gene where return Close ] --- |The structure that holds all of the values. +-- | The structure that holds all of the values. data State = State { _exec :: [Gene], _code :: [Gene], From 437c99c9460a166314e386d23188858fa994a975 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Thu, 13 Feb 2025 00:06:07 -0600 Subject: [PATCH 152/171] TEMPLATE HASKELL git statusgit statusgit statusgit status --- HushGP.cabal | 5 +- TODO.md | 2 +- src/HushGP/Instructions.hs | 650 +----------------- src/HushGP/Instructions/BoolInstructions.hs | 5 + src/HushGP/Instructions/CharInstructions.hs | 5 + src/HushGP/Instructions/CodeInstructions.hs | 5 + src/HushGP/Instructions/ExecInstructions.hs | 5 + src/HushGP/Instructions/FloatInstructions.hs | 5 + src/HushGP/Instructions/IntInstructions.hs | 6 +- src/HushGP/Instructions/StringInstructions.hs | 5 + .../Instructions/VectorBoolInstructions.hs | 5 + .../Instructions/VectorCharInstructions.hs | 5 + .../Instructions/VectorFloatInstructions.hs | 5 + .../Instructions/VectorIntInstructions.hs | 5 + .../Instructions/VectorStringInstructions.hs | 5 + src/HushGP/PushTests/IntTests.hs | 4 +- src/HushGP/TH.hs | 38 + 17 files changed, 105 insertions(+), 655 deletions(-) create mode 100644 src/HushGP/TH.hs diff --git a/HushGP.cabal b/HushGP.cabal index 49c8ce5..1d128fd 100644 --- a/HushGP.cabal +++ b/HushGP.cabal @@ -28,7 +28,7 @@ category: Data build-type: Simple common warnings - ghc-options: -Wall + ghc-options: -Wall -XTemplateHaskell library -- Import common warning flags. @@ -57,6 +57,7 @@ library , HushGP.PushTests.IntTests , HushGP.PushTests.GenericTests , HushGP.PushTests.UtilTests + , HushGP.TH -- Modules included in this library but not exported. -- other-modules: @@ -66,7 +67,7 @@ library -- Other library packages from which modules are imported. build-depends: - base, containers, lens, split, QuickCheck + base, containers, lens, split, QuickCheck, regex-tdfa, template-haskell -- Directories containing source files. hs-source-dirs: src diff --git a/TODO.md b/TODO.md index b7d6c54..2561fd5 100644 --- a/TODO.md +++ b/TODO.md @@ -14,7 +14,7 @@ - [X] Refactor all functions to take state as the final parameter - [X] Standardize the pattern matching parameter names, such as c1 : cs - [ ] Write unit/quickcheck tests for the generic functions -~~[ ] Use template haskell to generate function lists~~ +- [X] Use template haskell to generate function lists - [X] Move utility functions to their own file - [ ] Make add/sub/mult/div/mod instructions generic diff --git a/src/HushGP/Instructions.hs b/src/HushGP/Instructions.hs index fcc9be3..6a8859d 100644 --- a/src/HushGP/Instructions.hs +++ b/src/HushGP/Instructions.hs @@ -12,18 +12,6 @@ module HushGP.Instructions module HushGP.Instructions.VectorStringInstructions, module HushGP.Instructions.VectorBoolInstructions, module HushGP.Instructions.VectorCharInstructions, - allIntInstructions, - allFloatInstructions, - allBoolInstructions, - allCharInstructions, - allCodeInstructions, - allExecInstructions, - allStringInstructions, - allVectorIntInstructions, - allVectorFloatInstructions, - allVectorCharInstructions, - allVectorStringInstructions, - allVectorBoolInstructions, allInstructions, ) where @@ -43,643 +31,7 @@ import HushGP.Instructions.VectorIntInstructions import HushGP.Instructions.VectorStringInstructions import HushGP.State -allIntInstructions :: [Gene] -allIntInstructions = - map - StateFunc - [ (instructionIntFromFloat, "instructionIntFromFloat"), - (instructionIntFromBool, "instructionIntFromBool"), - (instructionIntAdd, "instructionIntAdd"), - (instructionIntSub, "instructionIntSub"), - (instructionIntMul, "instructionIntMul"), - (instructionIntDiv, "instructionIntDiv"), - (instructionIntMod, "instructionIntMod"), - (instructionIntMin, "instructionIntMin"), - (instructionIntMax, "instructionIntMax"), - (instructionIntInc, "instructionIntInc"), - (instructionIntDec, "instructionIntDec"), - (instructionIntLT, "instructionIntLT"), - (instructionIntGT, "instructionIntGT"), - (instructionIntLTE, "instructionIntLTE"), - (instructionIntGTE, "instructionIntGTE"), - (instructionIntDup, "instructionIntDup"), - (instructionIntPop, "instructionIntPop"), - (instructionIntDupN, "instructionIntDupN"), - (instructionIntSwap, "instructionIntSwap"), - (instructionIntRot, "instructionIntRot"), - (instructionIntFlush, "instructionIntFlush"), - (instructionIntEq, "instructionIntEq"), - (instructionIntYank, "instructionIntYank"), - (instructionIntYankDup, "instructionIntYankDup"), - (instructionIntShove, "instructionIntShove"), - (instructionIntIsStackEmpty, "instructionIntIsStackEmpty"), - (instructionIntFromChar, "instructionIntFromChar"), - (instructionIntFromString, "instructionIntFromString"), - (instructionIntDupItems, "instructionIntDupItems") - ] - -allFloatInstructions :: [Gene] -allFloatInstructions = - map - StateFunc - [ (instructionFloatFromInt, "instructionFloatFromInt"), - (instructionFloatFromBool, "instructionFloatFromBool"), - (instructionFloatAdd, "instructionFloatAdd"), - (instructionFloatSub, "instructionFloatSub"), - (instructionFloatMul, "instructionFloatMul"), - (instructionFloatDiv, "instructionFloatDiv"), - (instructionFloatMod, "instructionFloatMod"), - (instructionFloatMin, "instructionFloatMin"), - (instructionFloatMax, "instructionFloatMax"), - (instructionFloatInc, "instructionFloatInc"), - (instructionFloatDec, "instructionFloatDec"), - (instructionFloatLT, "instructionFloatLT"), - (instructionFloatGT, "instructionFloatGT"), - (instructionFloatLTE, "instructionFloatLTE"), - (instructionFloatGTE, "instructionFloatGTE"), - (instructionFloatDup, "instructionFloatDup"), - (instructionFloatPop, "instructionFloatPop"), - (instructionFloatDupN, "instructionFloatDupN"), - (instructionFloatSwap, "instructionFloatSwap"), - (instructionFloatRot, "instructionFloatRot"), - (instructionFloatFlush, "instructionFloatFlush"), - (instructionFloatEq, "instructionFloatEq"), - (instructionFloatYank, "instructionFloatYank"), - (instructionFloatYankDup, "instructionFloatYankDup"), - (instructionFloatShove, "instructionFloatShove"), - (instructionFloatIsStackEmpty, "instructionFloatIsStackEmpty"), - (instructionFloatFromChar, "instructionFloatFromChar"), - (instructionFloatFromString, "instructionFloatFromString"), - (instructionFloatDupItems, "instructionFloatDupItems") - ] - -allBoolInstructions :: [Gene] -allBoolInstructions = - map - StateFunc - [ (instructionBoolFromInt, "instructionBoolFromInt"), - (instructionBoolFromFloat, "instructionBoolFromFloat"), - (instructionBoolAnd, "instructionBoolAnd"), - (instructionBoolInvertFirstThenAnd, "instructionBoolInvertFirstThenAnd"), - (instructionBoolInvertSecondThenAnd, "instructionBoolInvertSecondThenAnd"), - (instructionBoolOr, "instructionBoolOr"), - (instructionBoolXor, "instructionBoolXor"), - (instructionBoolPop, "instructionBoolPop"), - (instructionBoolDup, "instructionBoolDup"), - (instructionBoolDupN, "instructionBoolDupN"), - (instructionBoolSwap, "instructionBoolSwap"), - (instructionBoolRot, "instructionBoolRot"), - (instructionBoolFlush, "instructionBoolFlush"), - (instructionBoolEq, "instructionBoolEq"), - (instructionBoolStackDepth, "instructionBoolStackDepth"), - (instructionBoolYank, "instructionBoolYank"), - (instructionBoolYankDup, "instructionBoolYankDup"), - (instructionBoolShove, "instructionBoolShove"), - (instructionBoolShoveDup, "instructionBoolShoveDup"), - (instructionBoolIsStackEmpty, "instructionBoolIsStackEmpty"), - (instructionBoolDupItems, "instructionBoolDupItems") - ] - -allCharInstructions :: [Gene] -allCharInstructions = - map - StateFunc - [ (instructionCharConcat, "instructionCharConcat"), - (instructionCharFromFirstChar, "instructionCharFromFirstChar"), - (instructionCharFromLastChar, "instructionCharFromLastChar"), - (instructionCharFromNthChar, "instructionCharFromNthChar"), - (instructionCharIsWhitespace, "instructionCharIsWhitespace"), - (instructionCharIsLetter, "instructionCharIsLetter"), - (instructionCharIsDigit, "instructionCharIsDigit"), - (instructionCharFromBool, "instructionCharFromBool"), - (instructionCharFromAsciiInt, "instructionCharFromAsciiInt"), - (instructionCharFromAsciiFloat, "instructionCharFromAsciiFloat"), - (instructionCharsFromString, "instructionCharsFromString"), - (instructionCharPop, "instructionCharPop"), - (instructionCharDup, "instructionCharDup"), - (instructionCharDupN, "instructionCharDupN"), - (instructionCharSwap, "instructionCharSwap"), - (instructionCharRot, "instructionCharRot"), - (instructionCharFlush, "instructionCharFlush"), - (instructionCharEq, "instructionCharEq"), - (instructionCharStackDepth, "instructionCharStackDepth"), - (instructionCharYank, "instructionCharYank"), - (instructionCharYankDup, "instructionCharYankDup"), - (instructionCharShove, "instructionCharShove"), - (instructionCharShoveDup, "instructionCharShoveDup"), - (instructionCharIsStackEmpty, "instructionCharIsStackEmpty"), - (instructionCharDupItems, "instructionCharDupItems") - ] - -allCodeInstructions :: [Gene] -allCodeInstructions = - map - StateFunc - [ (instructionCodePop, "instructionCodePop"), - (instructionCodeIsCodeBlock, "instructionCodeIsCodeBlock"), - (instructionCodeIsSingular, "instructionCodeIsSingular"), - (instructionCodeLength, "instructionCodeLength"), - (instructionCodeFirst, "instructionCodeFirst"), - (instructionCodeLast, "instructionCodeLast"), - (instructionCodeTail, "instructionCodeTail"), - (instructionCodeInit, "instructionCodeInit"), - (instructionCodeWrap, "instructionCodeWrap"), - (instructionCodeList, "instructionCodeList"), - (instructionCodeCombine, "instructionCodeCombine"), - (instructionCodeDo, "instructionCodeDo"), - (instructionCodeDoDup, "instructionCodeDoDup"), - (instructionCodeDoThenPop, "instructionCodeDoThenPop"), - (instructionCodeDoRange, "instructionCodeDoRange"), - (instructionCodeDoCount, "instructionCodeDoCount"), - (instructionCodeDoTimes, "instructionCodeDoTimes"), - (instructionCodeIf, "instructionCodeIf"), - (instructionCodeWhen, "instructionCodeWhen"), - (instructionCodeMember, "instructionCodeMember"), - (instructionCodeN, "instructionCodeN"), - (instructionMakeEmptyCodeBlock, "instructionMakeEmptyCodeBlock"), - (instructionIsEmptyCodeBlock, "instructionIsEmptyCodeBlock"), - (instructionCodeSize, "instructionCodeSize"), - (instructionCodeExtract, "instructionCodeExtract"), - (instructionCodeInsert, "instructionCodeInsert"), - (instructionCodeFirstPosition, "instructionCodeFirstPosition"), - (instructionCodeReverse, "instructionCodeReverse"), - (instructionCodeDup, "instructionCodeDup"), - (instructionCodeDupN, "instructionCodeDupN"), - (instructionCodeDup, "instructionCodeDup"), - (instructionCodeDupN, "instructionCodeDupN"), - (instructionCodeSwap, "instructionCodeSwap"), - (instructionCodeRot, "instructionCodeRot"), - (instructionCodeFlush, "instructionCodeFlush"), - (instructionCodeEq, "instructionCodeEq"), - (instructionCodeStackDepth, "instructionCodeStackDepth"), - (instructionCodeYank, "instructionCodeYank"), - (instructionCodeYankDup, "instructionCodeYankDup"), - (instructionCodeShove, "instructionCodeShove"), - (instructionCodeShoveDup, "instructionCodeShoveDup"), - (instructionCodeIsStackEmpty, "instructionCodeIsStackEmpty"), - (instructionCodeFromBool, "instructionCodeFromBool"), - (instructionCodeFromInt, "instructionCodeFromInt"), - (instructionCodeFromChar, "instructionCodeFromChar"), - (instructionCodeFromFloat, "instructionCodeFromFloat"), - (instructionCodeFromString, "instructionCodeFromString"), - (instructionCodeFromVectorInt, "instructionCodeFromVectorInt"), - (instructionCodeFromVectorFloat, "instructionCodeFromVectorFloat"), - (instructionCodeFromVectorString, "instructionCodeFromVectorString"), - (instructionCodeFromVectorBool, "instructionCodeFromVectorBool"), - (instructionCodeFromVectorChar, "instructionCodeFromVectorChar"), - (instructionCodeFromExec, "instructionCodeFromExec"), - (instructionCodeContainer, "instructionCodeContainer"), - (instructionCodeDiscrepancy, "instructionCodeDiscrepancy"), - (instructionCodeNoOp, "instructionCodeNoOp"), - (instructionCodeTailN, "instructionCodeTailN"), - (instructionCodeDupItems, "instructionCodeDupItems") - ] - -allExecInstructions :: [Gene] -allExecInstructions = - map - StateFunc - [ (instructionExecIf, "instructionExecIf"), - (instructionExecDup, "instructionExecDup"), - (instructionExecDupN, "instructionExecDupN"), - (instructionExecPop, "instructionExecPop"), - (instructionExecSwap, "instructionExecSwap"), - (instructionExecRot, "instructionExecRot"), - (instructionExecFlush, "instructionExecFlush"), - (instructionExecEq, "instructionExecEq"), - (instructionExecStackDepth, "instructionExecStackDepth"), - (instructionExecYank, "instructionExecYank"), - (instructionExecYankDup, "instructionExecYankDup"), - (instructionExecShove, "instructionExecShove"), - (instructionExecShoveDup, "instructionExecShoveDup"), - (instructionExecIsStackEmpty, "instructionExecIsStackEmpty"), - (instructionExecDoRange, "instructionExecDoRange"), - (instructionExecDoCount, "instructionExecDoCount"), - (instructionExecDoTimes, "instructionExecDoTimes"), - (instructionExecWhile, "instructionExecWhile"), - (instructionExecDoWhile, "instructionExecDoWhile"), - (instructionExecWhen, "instructionExecWhen"), - (instructionExecK, "instructionExecK"), - (instructionExecS, "instructionExecS"), - (instructionExecY, "instrucitonExecY"), - (instructionExecDupItems, "instructionExecDupItems") - ] - -allStringInstructions :: [Gene] -allStringInstructions = - map - StateFunc - [ (instructionStringConcat, "instructionStringConcat"), - (instructionStringSwap, "instructionStringSwap"), - (instructionStringInsertString, "instructionStringInsertString"), - (instructionStringFromFirstChar, "instructionStringFromFirstChar"), - (instructionStringFromLastChar, "instructionStringFromLastChar"), - (instructionStringFromNthChar, "instructionStringFromNthChar"), - (instructionStringIndexOfString, "instructionStringIndexOfString"), - (instructionStringContainsString, "instructionStringContainsString"), - (instructionStringSplitOnString, "instructionStringSplitOnString"), - (instructionStringReplaceFirstString, "instructionStringReplaceFirstString"), - (instructionStringReplaceNString, "instructionStringReplaceNString"), - (instructionStringReplaceAllString, "instructionStringReplaceAllString"), - (instructionStringRemoveFirstString, "instructionStringRemoveFirstString"), - (instructionStringRemoveNString, "instructionStringRemoveNString"), - (instructionStringRemoveAllString, "instructionStringRemoveAllString"), - (instructionStringOccurrencesOfString, "instructionStringOccurrencesOfString"), - (instructionStringInsertChar, "instructionStringInsertChar"), - (instructionStringContainsChar, "instructionStringContainsChar"), - (instructionStringIndexOfChar, "instructionStringIndexOfChar"), - (instructionStringSplitOnChar, "instructionStringSplitOnChar"), - (instructionStringReplaceFirstChar, "instructionStringReplaceFirstChar"), - (instructionStringReplaceNChar, "instructionStringReplaceNChar"), - (instructionStringReplaceAllChar, "instructionStringReplaceAllChar"), - (instructionStringRemoveFirstChar, "instructionStringRemoveFirstChar"), - (instructionStringRemoveNChar, "instructionStringRemoveNChar"), - (instructionStringRemoveAllChar, "instructionStringRemoveAllChar"), - (instructionStringOccurrencesOfChar, "instructionStringOccurrencesOfChar"), - (instructionStringReverse, "instructionStringReverse"), - (instructionStringHead, "instructionStringHead"), - (instructionStringTail, "instructionStringTail"), - (instructionStringPrependChar, "instructionStringPrependChar"), - (instructionStringAppendChar, "instructionStringAppendChar"), - (instructionStringRest, "instructionStringRest"), - (instructionStringButLast, "instructionStringButLast"), - (instructionStringDrop, "instructionStringDrop"), - (instructionStringButLastN, "instructionStringButLastN"), - (instructionStringLength, "instructionStringLength"), - (instructionStringMakeEmpty, "instructionStringMakeEmpty"), - (instructionStringIsEmptyString, "instructionStringIsEmptyString"), - (instructionStringRemoveNth, "instructionStringRemoveNth"), - (instructionStringSetNth, "instructionStringSetNth"), - (instructionStringStripWhitespace, "instructionStringStripWhitespace"), - (instructionStringFromBool, "instructionStringFromBool"), - (instructionStringFromInt, "instructionStringFromInt"), - (instructionStringFromFloat, "instructionStringFromFloat"), - (instructionStringFromChar, "instructionStringFromChar"), - (instructionStringPop, "instructionStringPop"), - (instructionStringDup, "instructionStringDup"), - (instructionStringDupN, "instructionStringDupN"), - (instructionStringSwap, "instructionStringSwap"), - (instructionStringRot, "instructionStringRot"), - (instructionStringFlush, "instructionStringFlush"), - (instructionStringEq, "instructionStringEq"), - (instructionStringStackDepth, "instructionStringStackDepth"), - (instructionStringYank, "instructionStringYank"), - (instructionStringYankDup, "instructionStringYankDup"), - (instructionStringShove, "instructionStringShove"), - (instructionStringShoveDup, "instructionStringShoveDup"), - (instructionStringIsStackEmpty, "instructionStringIsStackEmpty"), - (instructionStringSort, "instructionStringSort"), - (instructionStringSortReverse, "instructionStringSortReverse"), - (instructionStringDupItems, "instructionStringDupItems"), - (instructionStringParseToChar, "instructionStringParseToChar"), - (instructionStringSubString, "instructionStringSubString") - ] - -allVectorIntInstructions :: [Gene] -allVectorIntInstructions = - map - StateFunc - [ (instructionVectorIntConcat, "instructionVectorIntConcat"), - (instructionVectorIntConj, "instructionVectorIntConj"), - (instructionVectorIntConjEnd, "instructionVectorIntConjEnd"), - (instructionVectorIntTakeN, "instructionVectorIntTakeN"), - (instructionVectorIntSubVector, "instructionVectorIntSubVector"), - (instructionVectorIntFirst, "instructionVectorIntFirst"), - (instructionVectorIntFromFirstPrim, "instructionVectorFirstFromPrim"), - (instructionVectorIntFromPrim, "instructionVectorFromPrim"), - (instructionVectorIntLast, "instructionVectorIntLast"), - (instructionVectorIntFromLastPrim, "instructionVectorIntFromLastPrim"), - (instructionVectorIntNth, "instructionVectorIntNth"), - (instructionVectorIntFromNthPrim, "instructionVectorIntFromNthPrim"), - (instructionVectorIntRest, "instructionVectorIntRest"), - (instructionVectorIntButLast, "instructionVectorIntButLast"), - (instructionVectorIntDrop, "instructionVectorIntDrop"), - (instructionVectorIntDropR, "instructionVectorIntDropR"), - (instructionVectorIntLength, "instructionVectorIntLength"), - (instructionVectorIntReverse, "instructionVectorIntReverse"), - (instructionVectorIntPushAll, "instructionVectorIntPushAll"), - (instructionVectorIntMakeEmpty, "instructionVectorIntMakeEmpty"), - (instructionVectorIntIsEmpty, "instructionVectorIntIsEmpty"), - (instructionVectorIntContains, "instructionVectorIntContains"), - (instructionVectorIntContainsVectorInt, "instructionVectorIntContainsVectorInt"), - (instructionVectorIntIndexOf, "instructionVectorIntIndexOf"), - (instructionVectorIntIndexOfVectorInt, "instructionVectorIntIndexOfVectorInt"), - (instructionVectorIntOccurrencesOf, "instructionVectorIntOccurrencesOf"), - (instructionVectorIntOccurrencesOfVectorInt, "instructionVectorIntOccurrencesOfVectorInt"), - (instructionVectorIntParseToInt, "instructionVectorIntParseToInt"), - (instructionVectorIntSetNth, "instructionVectorIntSetNth"), - (instructionVectorIntSplitOn, "instructionVectorIntSplitOn"), - (instructionVectorIntSplitOnVectorInt, "instructionVectorIntSplitOnVectorInt"), - (instructionVectorIntReplaceFirst, "instructionVectorIntReplaceFirst"), - (instructionVectorIntReplaceN, "instructionVectorIntReplaceN"), - (instructionVectorIntReplaceAll, "instructionVectorIntReplaceAll"), - (instructionVectorIntReplaceFirstVectorInt, "instructionVectorIntReplaceFirstVectorInt"), - (instructionVectorIntReplaceVectorIntN, "instructionVectorIntReplaceVectorIntN"), - (instructionVectorIntReplaceAllVectorInt, "instructionVectorIntReplaceAllVectorInt"), - (instructionVectorIntRemoveFirst, "instructionVectorIntRemoveFirst"), - (instructionVectorIntRemoveN, "instructionVectorIntRemoveN"), - (instructionVectorIntRemoveAll, "instructionVectorIntRemoveAll"), - (instructionVectorIntRemoveFirstVectorInt, "instructionVectorIntRemoveFirstVectorInt"), - (instructionVectorIntRemoveNVectorInt, "instructionVectorIntRemoveNVectorInt"), - (instructionVectorIntRemoveAllVectorInt, "instructionVectorIntRemoveAllVectorInt"), - (instructionVectorIntIterate, "instructionVectorIntIterate"), - (instructionVectorIntInsert, "instructionVectorIntInsert"), - (instructionVectorIntInsertVectorInt, "instructionVectorIntInsertVectorInt"), - (instructionVectorIntPop, "instructionVectorIntPop"), - (instructionVectorIntDup, "instructionVectorIntDup"), - (instructionVectorIntDupN, "instructionVectorIntDupN"), - (instructionVectorIntSwap, "instructionVectorIntSwap"), - (instructionVectorIntRot, "instructionVectorIntRot"), - (instructionVectorIntFlush, "instructionVectorIntFlush"), - (instructionVectorIntEq, "instructionVectorIntEq"), - (instructionVectorIntStackDepth, "instructionVectorIntStackDepth"), - (instructionVectorIntYank, "instructionVectorIntYank"), - (instructionVectorIntYankDup, "instructionVectorIntYankDup"), - (instructionVectorIntShove, "instructionVectorIntShove"), - (instructionVectorIntShoveDup, "instructionVectorIntShoveDup"), - (instructionVectorIntDupItems, "intsructionVectorIntDupItems"), - (instructionVectorIntIsStackEmpty, "instructionVectorIntIsStackEmpty"), - (instructionVectorIntSort, "instructionVectorIntSort"), - (instructionVectorIntSortReverse, "instructionVectorIntSortReverse"), - (instructionVectorIntDupItems, "instructionVectorIntDupItems") - ] - -allVectorFloatInstructions :: [Gene] -allVectorFloatInstructions = - map - StateFunc - [ (instructionVectorFloatConcat, "instructionVectorFloatConcat"), - (instructionVectorFloatConj, "instructionVectorFloatConj"), - (instructionVectorFloatConjEnd, "instructionVectorFloatConjEnd"), - (instructionVectorFloatTakeN, "instructionVectorFloatTakeN"), - (instructionVectorFloatSubVector, "instructionVectorFloatSubVector"), - (instructionVectorFloatFirst, "instructionVectorFloatFirst"), - (instructionVectorFloatFromFirstPrim, "instructionVectorFirstFromPrim"), - (instructionVectorFloatFromPrim, "instructionVectorFromPrim"), - (instructionVectorFloatLast, "instructionVectorFloatLast"), - (instructionVectorFloatFromLastPrim, "instructionVectorFloatFromLastPrim"), - (instructionVectorFloatNth, "instructionVectorFloatNth"), - (instructionVectorFloatFromNthPrim, "instructionVectorFloatFromNthPrim"), - (instructionVectorFloatRest, "instructionVectorFloatRest"), - (instructionVectorFloatButLast, "instructionVectorFloatButLast"), - (instructionVectorFloatDrop, "instructionVectorFloatDrop"), - (instructionVectorFloatDropR, "instructionVectorFloatDropR"), - (instructionVectorFloatLength, "instructionVectorFloatLength"), - (instructionVectorFloatReverse, "instructionVectorFloatReverse"), - (instructionVectorFloatPushAll, "instructionVectorFloatPushAll"), - (instructionVectorFloatMakeEmpty, "instructionVectorFloatMakeEmpty"), - (instructionVectorFloatIsEmpty, "instructionVectorFloatIsEmpty"), - (instructionVectorFloatContains, "instructionVectorFloatContains"), - (instructionVectorFloatContainsVectorFloat, "instructionVectorFloatContainsVectorFloat"), - (instructionVectorFloatIndexOf, "instructionVectorFloatIndexOf"), - (instructionVectorFloatIndexOfVectorFloat, "instructionVectorFloatIndexOfVectorFloat"), - (instructionVectorFloatOccurrencesOf, "instructionVectorFloatOccurrencesOf"), - (instructionVectorFloatOccurrencesOfVectorFloat, "instructionVectorFloatOccurrencesOfVectorFloat"), - (instructionVectorFloatParseToFloat, "instructionVectorFloatParseToFloat"), - (instructionVectorFloatSetNth, "instructionVectorFloatSetNth"), - (instructionVectorFloatSplitOn, "instructionVectorFloatSplitOn"), - (instructionVectorFloatSplitOnVectorFloat, "instructionVectorFloatSplitOnVectorFloat"), - (instructionVectorFloatReplaceFirst, "instructionVectorFloatReplaceFirst"), - (instructionVectorFloatReplaceN, "instructionVectorFloatReplaceN"), - (instructionVectorFloatReplaceAll, "instructionVectorFloatReplaceAll"), - (instructionVectorFloatReplaceFirstVectorFloat, "instructionVectorFloatReplaceFirstVectorFloat"), - (instructionVectorFloatReplaceVectorFloatN, "instructionVectorFloatReplaceVectorFloatN"), - (instructionVectorFloatReplaceAllVectorFloat, "instructionVectorFloatReplaceAllVectorFloat"), - (instructionVectorFloatRemoveFirst, "instructionVectorFloatRemoveFirst"), - (instructionVectorFloatRemoveN, "instructionVectorFloatRemoveN"), - (instructionVectorFloatRemoveAll, "instructionVectorFloatRemoveAll"), - (instructionVectorFloatRemoveFirstVectorFloat, "instructionVectorFloatRemoveFirstVectorFloat"), - (instructionVectorFloatRemoveNVectorFloat, "instructionVectorFloatRemoveNVectorFloat"), - (instructionVectorFloatRemoveAllVectorFloat, "instructionVectorFloatRemoveAllVectorFloat"), - (instructionVectorFloatIterate, "instructionVectorFloatIterate"), - (instructionVectorFloatInsert, "instructionVectorFloatInsert"), - (instructionVectorFloatInsertVectorFloat, "instructionVectorFloatInsertVectorFloat"), - (instructionVectorFloatPop, "instructionVectorFloatPop"), - (instructionVectorFloatDup, "instructionVectorFloatDup"), - (instructionVectorFloatDupN, "instructionVectorFloatDupN"), - (instructionVectorFloatSwap, "instructionVectorFloatSwap"), - (instructionVectorFloatRot, "instructionVectorFloatRot"), - (instructionVectorFloatFlush, "instructionVectorFloatFlush"), - (instructionVectorFloatEq, "instructionVectorFloatEq"), - (instructionVectorFloatStackDepth, "instructionVectorFloatStackDepth"), - (instructionVectorFloatYank, "instructionVectorFloatYank"), - (instructionVectorFloatYankDup, "instructionVectorFloatYankDup"), - (instructionVectorFloatShove, "instructionVectorFloatShove"), - (instructionVectorFloatShoveDup, "instructionVectorFloatShoveDup"), - (instructionVectorFloatDupItems, "intsructionVectorFloatDupItems"), - (instructionVectorFloatIsStackEmpty, "instructionVectorFloatIsStackEmpty"), - (instructionVectorFloatSort, "instructionVectorFloatSort"), - (instructionVectorFloatSortReverse, "instructionVectorFloatSortReverse"), - (instructionVectorFloatDupItems, "instructionVectorFloatDupItems") - ] - -allVectorCharInstructions :: [Gene] -allVectorCharInstructions = - map - StateFunc - [ (instructionVectorCharConcat, "instructionVectorCharConcat"), - (instructionVectorCharConj, "instructionVectorCharConj"), - (instructionVectorCharConjEnd, "instructionVectorCharConjEnd"), - (instructionVectorCharTakeN, "instructionVectorCharTakeN"), - (instructionVectorCharSubVector, "instructionVectorCharSubVector"), - (instructionVectorCharFirst, "instructionVectorCharFirst"), - (instructionVectorCharFromFirstPrim, "instructionVectorFirstFromPrim"), - (instructionVectorCharFromPrim, "instructionVectorFromPrim"), - (instructionVectorCharLast, "instructionVectorCharLast"), - (instructionVectorCharFromLastPrim, "instructionVectorCharFromLastPrim"), - (instructionVectorCharNth, "instructionVectorCharNth"), - (instructionVectorCharFromNthPrim, "instructionVectorCharFromNthPrim"), - (instructionVectorCharRest, "instructionVectorCharRest"), - (instructionVectorCharButLast, "instructionVectorCharButLast"), - (instructionVectorCharDrop, "instructionVectorCharDrop"), - (instructionVectorCharDropR, "instructionVectorCharDropR"), - (instructionVectorCharLength, "instructionVectorCharLength"), - (instructionVectorCharReverse, "instructionVectorCharReverse"), - (instructionVectorCharPushAll, "instructionVectorCharPushAll"), - (instructionVectorCharMakeEmpty, "instructionVectorCharMakeEmpty"), - (instructionVectorCharIsEmpty, "instructionVectorCharIsEmpty"), - (instructionVectorCharContains, "instructionVectorCharContains"), - (instructionVectorCharContainsVectorChar, "instructionVectorCharContainsVectorChar"), - (instructionVectorCharIndexOf, "instructionVectorCharIndexOf"), - (instructionVectorCharIndexOfVectorChar, "instructionVectorCharIndexOfVectorChar"), - (instructionVectorCharOccurrencesOf, "instructionVectorCharOccurrencesOf"), - (instructionVectorCharOccurrencesOfVectorChar, "instructionVectorCharOccurrencesOfVectorChar"), - (instructionVectorCharParseToChar, "instructionVectorCharParseToChar"), - (instructionVectorCharSetNth, "instructionVectorCharSetNth"), - (instructionVectorCharSplitOn, "instructionVectorCharSplitOn"), - (instructionVectorCharSplitOnVectorChar, "instructionVectorCharSplitOnVectorChar"), - (instructionVectorCharReplaceFirst, "instructionVectorCharReplaceFirst"), - (instructionVectorCharReplaceN, "instructionVectorCharReplaceN"), - (instructionVectorCharReplaceAll, "instructionVectorCharReplaceAll"), - (instructionVectorCharReplaceFirstVectorChar, "instructionVectorCharReplaceFirstVectorChar"), - (instructionVectorCharReplaceVectorCharN, "instructionVectorCharReplaceVectorCharN"), - (instructionVectorCharReplaceAllVectorChar, "instructionVectorCharReplaceAllVectorChar"), - (instructionVectorCharRemoveFirst, "instructionVectorCharRemoveFirst"), - (instructionVectorCharRemoveN, "instructionVectorCharRemoveN"), - (instructionVectorCharRemoveAll, "instructionVectorCharRemoveAll"), - (instructionVectorCharRemoveFirstVectorChar, "instructionVectorCharRemoveFirstVectorChar"), - (instructionVectorCharRemoveNVectorChar, "instructionVectorCharRemoveNVectorChar"), - (instructionVectorCharRemoveAllVectorChar, "instructionVectorCharRemoveAllVectorChar"), - (instructionVectorCharIterate, "instructionVectorCharIterate"), - (instructionVectorCharInsert, "instructionVectorCharInsert"), - (instructionVectorCharInsertVectorChar, "instructionVectorCharInsertVectorChar"), - (instructionVectorCharPop, "instructionVectorCharPop"), - (instructionVectorCharDup, "instructionVectorCharDup"), - (instructionVectorCharDupN, "instructionVectorCharDupN"), - (instructionVectorCharSwap, "instructionVectorCharSwap"), - (instructionVectorCharRot, "instructionVectorCharRot"), - (instructionVectorCharFlush, "instructionVectorCharFlush"), - (instructionVectorCharEq, "instructionVectorCharEq"), - (instructionVectorCharStackDepth, "instructionVectorCharStackDepth"), - (instructionVectorCharYank, "instructionVectorCharYank"), - (instructionVectorCharYankDup, "instructionVectorCharYankDup"), - (instructionVectorCharShove, "instructionVectorCharShove"), - (instructionVectorCharShoveDup, "instructionVectorCharShoveDup"), - (instructionVectorCharDupItems, "intsructionVectorCharDupItems"), - (instructionVectorCharIsStackEmpty, "instructionVectorCharIsStackEmpty"), - (instructionVectorCharSort, "instructionVectorCharSort"), - (instructionVectorCharSortReverse, "instructionVectorCharSortReverse"), - (instructionVectorCharDupItems, "instructionVectorCharDupItems") - ] - -allVectorStringInstructions :: [Gene] -allVectorStringInstructions = - map - StateFunc - [ (instructionVectorStringConcat, "instructionVectorStringConcat"), - (instructionVectorStringConj, "instructionVectorStringConj"), - (instructionVectorStringConjEnd, "instructionVectorStringConjEnd"), - (instructionVectorStringTakeN, "instructionVectorStringTakeN"), - (instructionVectorStringSubVector, "instructionVectorStringSubVector"), - (instructionVectorStringFirst, "instructionVectorStringFirst"), - (instructionVectorStringFromFirstPrim, "instructionVectorFirstFromPrim"), - (instructionVectorStringFromPrim, "instructionVectorFromPrim"), - (instructionVectorStringLast, "instructionVectorStringLast"), - (instructionVectorStringFromLastPrim, "instructionVectorStringFromLastPrim"), - (instructionVectorStringNth, "instructionVectorStringNth"), - (instructionVectorStringFromNthPrim, "instructionVectorStringFromNthPrim"), - (instructionVectorStringRest, "instructionVectorStringRest"), - (instructionVectorStringButLast, "instructionVectorStringButLast"), - (instructionVectorStringDrop, "instructionVectorStringDrop"), - (instructionVectorStringDropR, "instructionVectorStringDropR"), - (instructionVectorStringLength, "instructionVectorStringLength"), - (instructionVectorStringReverse, "instructionVectorStringReverse"), - (instructionVectorStringPushAll, "instructionVectorStringPushAll"), - (instructionVectorStringMakeEmpty, "instructionVectorStringMakeEmpty"), - (instructionVectorStringIsEmpty, "instructionVectorStringIsEmpty"), - (instructionVectorStringContains, "instructionVectorStringContains"), - (instructionVectorStringContainsVectorString, "instructionVectorStringContainsVectorString"), - (instructionVectorStringIndexOf, "instructionVectorStringIndexOf"), - (instructionVectorStringIndexOfVectorString, "instructionVectorStringIndexOfVectorString"), - (instructionVectorStringOccurrencesOf, "instructionVectorStringOccurrencesOf"), - (instructionVectorStringOccurrencesOfVectorString, "instructionVectorStringOccurrencesOfVectorString"), - (instructionVectorStringParseToString, "instructionVectorStringParseToString"), - (instructionVectorStringSetNth, "instructionVectorStringSetNth"), - (instructionVectorStringSplitOn, "instructionVectorStringSplitOn"), - (instructionVectorStringSplitOnVectorString, "instructionVectorStringSplitOnVectorString"), - (instructionVectorStringReplaceFirst, "instructionVectorStringReplaceFirst"), - (instructionVectorStringReplaceN, "instructionVectorStringReplaceN"), - (instructionVectorStringReplaceAll, "instructionVectorStringReplaceAll"), - (instructionVectorStringReplaceFirstVectorString, "instructionVectorStringReplaceFirstVectorString"), - (instructionVectorStringReplaceVectorStringN, "instructionVectorStringReplaceVectorStringN"), - (instructionVectorStringReplaceAllVectorString, "instructionVectorStringReplaceAllVectorString"), - (instructionVectorStringRemoveFirst, "instructionVectorStringRemoveFirst"), - (instructionVectorStringRemoveN, "instructionVectorStringRemoveN"), - (instructionVectorStringRemoveAll, "instructionVectorStringRemoveAll"), - (instructionVectorStringRemoveFirstVectorString, "instructionVectorStringRemoveFirstVectorString"), - (instructionVectorStringRemoveNVectorString, "instructionVectorStringRemoveNVectorString"), - (instructionVectorStringRemoveAllVectorString, "instructionVectorStringRemoveAllVectorString"), - (instructionVectorStringIterate, "instructionVectorStringIterate"), - (instructionVectorStringInsert, "instructionVectorStringInsert"), - (instructionVectorStringInsertVectorString, "instructionVectorStringInsertVectorString"), - (instructionVectorStringPop, "instructionVectorStringPop"), - (instructionVectorStringDup, "instructionVectorStringDup"), - (instructionVectorStringDupN, "instructionVectorStringDupN"), - (instructionVectorStringSwap, "instructionVectorStringSwap"), - (instructionVectorStringRot, "instructionVectorStringRot"), - (instructionVectorStringFlush, "instructionVectorStringFlush"), - (instructionVectorStringEq, "instructionVectorStringEq"), - (instructionVectorStringStackDepth, "instructionVectorStringStackDepth"), - (instructionVectorStringYank, "instructionVectorStringYank"), - (instructionVectorStringYankDup, "instructionVectorStringYankDup"), - (instructionVectorStringShove, "instructionVectorStringShove"), - (instructionVectorStringShoveDup, "instructionVectorStringShoveDup"), - (instructionVectorStringDupItems, "intsructionVectorStringDupItems"), - (instructionVectorStringIsStackEmpty, "instructionVectorStringIsStackEmpty"), - (instructionVectorStringSort, "instructionVectorStringSort"), - (instructionVectorStringSortReverse, "instructionVectorStringSortReverse"), - (instructionVectorStringDupItems, "instructionVectorStringDupItems") - ] - -allVectorBoolInstructions :: [Gene] -allVectorBoolInstructions = - map - StateFunc - [ (instructionVectorBoolConcat, "instructionVectorBoolConcat"), - (instructionVectorBoolConj, "instructionVectorBoolConj"), - (instructionVectorBoolConjEnd, "instructionVectorBoolConjEnd"), - (instructionVectorBoolTakeN, "instructionVectorBoolTakeN"), - (instructionVectorBoolSubVector, "instructionVectorBoolSubVector"), - (instructionVectorBoolFirst, "instructionVectorBoolFirst"), - (instructionVectorBoolFromFirstPrim, "instructionVectorFirstFromPrim"), - (instructionVectorBoolFromPrim, "instructionVectorFromPrim"), - (instructionVectorBoolLast, "instructionVectorBoolLast"), - (instructionVectorBoolFromLastPrim, "instructionVectorBoolFromLastPrim"), - (instructionVectorBoolNth, "instructionVectorBoolNth"), - (instructionVectorBoolFromNthPrim, "instructionVectorBoolFromNthPrim"), - (instructionVectorBoolRest, "instructionVectorBoolRest"), - (instructionVectorBoolButLast, "instructionVectorBoolButLast"), - (instructionVectorBoolDrop, "instructionVectorBoolDrop"), - (instructionVectorBoolDropR, "instructionVectorBoolDropR"), - (instructionVectorBoolLength, "instructionVectorBoolLength"), - (instructionVectorBoolReverse, "instructionVectorBoolReverse"), - (instructionVectorBoolPushAll, "instructionVectorBoolPushAll"), - (instructionVectorBoolMakeEmpty, "instructionVectorBoolMakeEmpty"), - (instructionVectorBoolIsEmpty, "instructionVectorBoolIsEmpty"), - (instructionVectorBoolContains, "instructionVectorBoolContains"), - (instructionVectorBoolContainsVectorBool, "instructionVectorBoolContainsVectorBool"), - (instructionVectorBoolIndexOf, "instructionVectorBoolIndexOf"), - (instructionVectorBoolIndexOfVectorBool, "instructionVectorBoolIndexOfVectorBool"), - (instructionVectorBoolOccurrencesOf, "instructionVectorBoolOccurrencesOf"), - (instructionVectorBoolOccurrencesOfVectorBool, "instructionVectorBoolOccurrencesOfVectorBool"), - (instructionVectorBoolParseToBool, "instructionVectorBoolParseToBool"), - (instructionVectorBoolSetNth, "instructionVectorBoolSetNth"), - (instructionVectorBoolSplitOn, "instructionVectorBoolSplitOn"), - (instructionVectorBoolSplitOnVectorBool, "instructionVectorBoolSplitOnVectorBool"), - (instructionVectorBoolReplaceFirst, "instructionVectorBoolReplaceFirst"), - (instructionVectorBoolReplaceN, "instructionVectorBoolReplaceN"), - (instructionVectorBoolReplaceAll, "instructionVectorBoolReplaceAll"), - (instructionVectorBoolReplaceFirstVectorBool, "instructionVectorBoolReplaceFirstVectorBool"), - (instructionVectorBoolReplaceVectorBoolN, "instructionVectorBoolReplaceVectorBoolN"), - (instructionVectorBoolReplaceAllVectorBool, "instructionVectorBoolReplaceAllVectorBool"), - (instructionVectorBoolRemoveFirst, "instructionVectorBoolRemoveFirst"), - (instructionVectorBoolRemoveN, "instructionVectorBoolRemoveN"), - (instructionVectorBoolRemoveAll, "instructionVectorBoolRemoveAll"), - (instructionVectorBoolRemoveFirstVectorBool, "instructionVectorBoolRemoveFirstVectorBool"), - (instructionVectorBoolRemoveNVectorBool, "instructionVectorBoolRemoveNVectorBool"), - (instructionVectorBoolRemoveAllVectorBool, "instructionVectorBoolRemoveAllVectorBool"), - (instructionVectorBoolIterate, "instructionVectorBoolIterate"), - (instructionVectorBoolInsert, "instructionVectorBoolInsert"), - (instructionVectorBoolInsertVectorBool, "instructionVectorBoolInsertVectorBool"), - (instructionVectorBoolPop, "instructionVectorBoolPop"), - (instructionVectorBoolDup, "instructionVectorBoolDup"), - (instructionVectorBoolDupN, "instructionVectorBoolDupN"), - (instructionVectorBoolSwap, "instructionVectorBoolSwap"), - (instructionVectorBoolRot, "instructionVectorBoolRot"), - (instructionVectorBoolFlush, "instructionVectorBoolFlush"), - (instructionVectorBoolEq, "instructionVectorBoolEq"), - (instructionVectorBoolStackDepth, "instructionVectorBoolStackDepth"), - (instructionVectorBoolYank, "instructionVectorBoolYank"), - (instructionVectorBoolYankDup, "instructionVectorBoolYankDup"), - (instructionVectorBoolShove, "instructionVectorBoolShove"), - (instructionVectorBoolShoveDup, "instructionVectorBoolShoveDup"), - (instructionVectorBoolDupItems, "intsructionVectorBoolDupItems"), - (instructionVectorBoolIsStackEmpty, "instructionVectorBoolIsStackEmpty"), - (instructionVectorBoolSort, "instructionVectorBoolSort"), - (instructionVectorBoolSortReverse, "instructionVectorBoolSortReverse"), - (instructionVectorBoolDupItems, "instructionVectorBoolDupItems") - ] - +-- | All of the instructions declared in all the instruction submodules allInstructions :: [Gene] allInstructions = allIntInstructions diff --git a/src/HushGP/Instructions/BoolInstructions.hs b/src/HushGP/Instructions/BoolInstructions.hs index c55f929..ece81d6 100644 --- a/src/HushGP/Instructions/BoolInstructions.hs +++ b/src/HushGP/Instructions/BoolInstructions.hs @@ -1,8 +1,10 @@ +{-# LANGUAGE TemplateHaskell #-} module HushGP.Instructions.BoolInstructions where import HushGP.State import HushGP.Instructions.GenericInstructions import HushGP.Instructions.Utility +import HushGP.TH -- |If top of int stack /= 0 pushes True to bool stack, else false. instructionBoolFromInt :: State -> State @@ -94,3 +96,6 @@ instructionBoolIsStackEmpty = instructionIsStackEmpty bool -- |Duplicate the top N items from the bool stack based on the top int from the int stack. instructionBoolDupItems :: State -> State instructionBoolDupItems = instructionDupItems bool + +allBoolInstructions :: [Gene] +allBoolInstructions = map StateFunc ($(functionExtractor "instruction")) diff --git a/src/HushGP/Instructions/CharInstructions.hs b/src/HushGP/Instructions/CharInstructions.hs index 9c0d540..578dafe 100644 --- a/src/HushGP/Instructions/CharInstructions.hs +++ b/src/HushGP/Instructions/CharInstructions.hs @@ -1,9 +1,11 @@ +{-# LANGUAGE TemplateHaskell #-} module HushGP.Instructions.CharInstructions where import Data.Char import HushGP.State import HushGP.Instructions.GenericInstructions import HushGP.Instructions.Utility +import HushGP.TH -- |Combines the top two chars into a string and pushes the result to the string stack. instructionCharConcat :: State -> State @@ -132,3 +134,6 @@ instructionCharDupItems = instructionDupItems char -- all chars in said string to the char stack. instructionCharFromAllString :: State -> State instructionCharFromAllString = instructionPushAll char string + +allCharInstructions :: [Gene] +allCharInstructions = map StateFunc ($(functionExtractor "instruction")) diff --git a/src/HushGP/Instructions/CodeInstructions.hs b/src/HushGP/Instructions/CodeInstructions.hs index 1588465..6454703 100644 --- a/src/HushGP/Instructions/CodeInstructions.hs +++ b/src/HushGP/Instructions/CodeInstructions.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE TemplateHaskell #-} module HushGP.Instructions.CodeInstructions where import Data.List (elemIndex) @@ -5,6 +6,7 @@ import HushGP.State import HushGP.Instructions.GenericInstructions import HushGP.Instructions.IntInstructions import HushGP.Instructions.Utility +import HushGP.TH -- import Debug.Trace -- |Pops the top of the code stack @@ -345,3 +347,6 @@ instructionCodeNoOp state = state -- |Duplicates the top N items of the code stack based on the top of the int stack. instructionCodeDupItems :: State -> State instructionCodeDupItems = instructionDupItems code + +allCodeInstructions :: [Gene] +allCodeInstructions = map StateFunc ($(functionExtractor "instruction")) diff --git a/src/HushGP/Instructions/ExecInstructions.hs b/src/HushGP/Instructions/ExecInstructions.hs index 48f9005..2c1d0a4 100644 --- a/src/HushGP/Instructions/ExecInstructions.hs +++ b/src/HushGP/Instructions/ExecInstructions.hs @@ -1,8 +1,10 @@ +{-# LANGUAGE TemplateHaskell #-} module HushGP.Instructions.ExecInstructions where import HushGP.State import HushGP.Instructions.IntInstructions import HushGP.Instructions.GenericInstructions +import HushGP.TH -- |Removes the second item from the exec stack if the top of the bool stack is True. -- Removes the first item from the exec stack if the top of the bool stack is False. @@ -153,3 +155,6 @@ instructionExecY state = state -- |Duplicates the top N items of the exec stack based on the top of the int stack. instructionExecDupItems :: State -> State instructionExecDupItems = instructionDupItems exec + +allExecInstructions :: [Gene] +allExecInstructions = map StateFunc ($(functionExtractor "instruction")) diff --git a/src/HushGP/Instructions/FloatInstructions.hs b/src/HushGP/Instructions/FloatInstructions.hs index 4f542fe..2495b31 100644 --- a/src/HushGP/Instructions/FloatInstructions.hs +++ b/src/HushGP/Instructions/FloatInstructions.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE TemplateHaskell #-} module HushGP.Instructions.FloatInstructions where import Data.Fixed (mod') @@ -5,6 +6,7 @@ import HushGP.Instructions.GenericInstructions import HushGP.Instructions.Utility import HushGP.State import Data.Char +import HushGP.TH -- |Converts the top int to a float and pushes the result to the float stack. instructionFloatFromInt :: State -> State @@ -169,3 +171,6 @@ instructionFloatTan state = state -- |Duplicate the top N items from the float stack based on the top int from the int stack. instructionFloatDupItems :: State -> State instructionFloatDupItems = instructionDupItems float + +allFloatInstructions :: [Gene] +allFloatInstructions = map StateFunc ($(functionExtractor "instruction")) diff --git a/src/HushGP/Instructions/IntInstructions.hs b/src/HushGP/Instructions/IntInstructions.hs index d1a2682..543f0a7 100644 --- a/src/HushGP/Instructions/IntInstructions.hs +++ b/src/HushGP/Instructions/IntInstructions.hs @@ -1,9 +1,10 @@ +{-# LANGUAGE TemplateHaskell #-} module HushGP.Instructions.IntInstructions where import HushGP.State import HushGP.Instructions.GenericInstructions import Data.Char --- import Debug.Trace +import HushGP.TH -- |Converts the top float to an int and pushes the result to the int stack. instructionIntFromFloat :: State -> State @@ -155,3 +156,6 @@ instructionIntIsStackEmpty = instructionIsStackEmpty int -- |Duplicate the top N items from the int stack based on the top int from the int stack. instructionIntDupItems :: State -> State instructionIntDupItems = instructionDupItems int + +allIntInstructions :: [Gene] +allIntInstructions = map StateFunc ($(functionExtractor "instruction")) diff --git a/src/HushGP/Instructions/StringInstructions.hs b/src/HushGP/Instructions/StringInstructions.hs index 04dcab5..f192b19 100644 --- a/src/HushGP/Instructions/StringInstructions.hs +++ b/src/HushGP/Instructions/StringInstructions.hs @@ -1,8 +1,10 @@ +{-# LANGUAGE TemplateHaskell #-} module HushGP.Instructions.StringInstructions where import HushGP.State import HushGP.Instructions.GenericInstructions import HushGP.Instructions.Utility +import HushGP.TH -- |Concats the top two strings on the string stack and pushes the result. instructionStringConcat :: State -> State @@ -323,3 +325,6 @@ instructionStringSubString = instructionSubVector string -- exec stack along the way. instructionStringIterate :: State -> State instructionStringIterate = instructionVectorIterate char string GeneString instructionStringIterate "instructionStringIterate" + +allStringInstructions :: [Gene] +allStringInstructions = map StateFunc ($(functionExtractor "instruction")) diff --git a/src/HushGP/Instructions/VectorBoolInstructions.hs b/src/HushGP/Instructions/VectorBoolInstructions.hs index 934495a..2f6987d 100644 --- a/src/HushGP/Instructions/VectorBoolInstructions.hs +++ b/src/HushGP/Instructions/VectorBoolInstructions.hs @@ -1,7 +1,9 @@ +{-# LANGUAGE TemplateHaskell #-} module HushGP.Instructions.VectorBoolInstructions where import HushGP.State import HushGP.Instructions.GenericInstructions +import HushGP.TH -- |Pops the top bool vector from the bool vector stack. instructionVectorBoolPop :: State -> State @@ -328,3 +330,6 @@ instructionVectorBoolInsert = instructionVectorInsert bool vectorBool -- pulled from the top of the int stack. instructionVectorBoolInsertVectorBool :: State -> State instructionVectorBoolInsertVectorBool = instructionVectorInsertVector vectorBool + +allVectorBoolInstructions :: [Gene] +allVectorBoolInstructions = map StateFunc ($(functionExtractor "instruction")) diff --git a/src/HushGP/Instructions/VectorCharInstructions.hs b/src/HushGP/Instructions/VectorCharInstructions.hs index 09084a3..caf52f1 100644 --- a/src/HushGP/Instructions/VectorCharInstructions.hs +++ b/src/HushGP/Instructions/VectorCharInstructions.hs @@ -1,7 +1,9 @@ +{-# LANGUAGE TemplateHaskell #-} module HushGP.Instructions.VectorCharInstructions where import HushGP.State import HushGP.Instructions.GenericInstructions +import HushGP.TH -- |Pops the top char vector from the char vector stack. instructionVectorCharPop :: State -> State @@ -328,3 +330,6 @@ instructionVectorCharInsert = instructionVectorInsert char vectorChar -- pulled from the top of the int stack. instructionVectorCharInsertVectorChar :: State -> State instructionVectorCharInsertVectorChar = instructionVectorInsertVector vectorChar + +allVectorCharInstructions :: [Gene] +allVectorCharInstructions = map StateFunc ($(functionExtractor "instruction")) diff --git a/src/HushGP/Instructions/VectorFloatInstructions.hs b/src/HushGP/Instructions/VectorFloatInstructions.hs index a2a8531..b1b115a 100644 --- a/src/HushGP/Instructions/VectorFloatInstructions.hs +++ b/src/HushGP/Instructions/VectorFloatInstructions.hs @@ -1,7 +1,9 @@ +{-# LANGUAGE TemplateHaskell #-} module HushGP.Instructions.VectorFloatInstructions where import HushGP.State import HushGP.Instructions.GenericInstructions +import HushGP.TH -- |Pops the top float vector from the float vector stack. instructionVectorFloatPop :: State -> State @@ -328,3 +330,6 @@ instructionVectorFloatInsert = instructionVectorInsert float vectorFloat -- pulled from the top of the int stack. instructionVectorFloatInsertVectorFloat :: State -> State instructionVectorFloatInsertVectorFloat = instructionVectorInsertVector vectorFloat + +allVectorFloatInstructions :: [Gene] +allVectorFloatInstructions = map StateFunc ($(functionExtractor "instruction")) diff --git a/src/HushGP/Instructions/VectorIntInstructions.hs b/src/HushGP/Instructions/VectorIntInstructions.hs index cddf728..f021ea0 100644 --- a/src/HushGP/Instructions/VectorIntInstructions.hs +++ b/src/HushGP/Instructions/VectorIntInstructions.hs @@ -1,7 +1,9 @@ +{-# LANGUAGE TemplateHaskell #-} module HushGP.Instructions.VectorIntInstructions where import HushGP.Instructions.GenericInstructions import HushGP.State +import HushGP.TH -- |Pops the top int vector from the int vector stack. instructionVectorIntPop :: State -> State @@ -328,3 +330,6 @@ instructionVectorIntInsert = instructionVectorInsert int vectorInt -- pulled from the top of the int stack. instructionVectorIntInsertVectorInt :: State -> State instructionVectorIntInsertVectorInt = instructionVectorInsertVector vectorInt + +allVectorIntInstructions :: [Gene] +allVectorIntInstructions = map StateFunc ($(functionExtractor "instruction")) diff --git a/src/HushGP/Instructions/VectorStringInstructions.hs b/src/HushGP/Instructions/VectorStringInstructions.hs index f4b904e..19f4600 100644 --- a/src/HushGP/Instructions/VectorStringInstructions.hs +++ b/src/HushGP/Instructions/VectorStringInstructions.hs @@ -1,7 +1,9 @@ +{-# LANGUAGE TemplateHaskell #-} module HushGP.Instructions.VectorStringInstructions where import HushGP.State import HushGP.Instructions.GenericInstructions +import HushGP.TH -- |Pops the top string vector from the string vector stack. instructionVectorStringPop :: State -> State @@ -328,3 +330,6 @@ instructionVectorStringInsert = instructionVectorInsert string vectorString -- pulled from the top of the int stack. instructionVectorStringInsertVectorString :: State -> State instructionVectorStringInsertVectorString = instructionVectorInsertVector vectorString + +allVectorStringInstructions :: [Gene] +allVectorStringInstructions = map StateFunc ($(functionExtractor "instruction")) diff --git a/src/HushGP/PushTests/IntTests.hs b/src/HushGP/PushTests/IntTests.hs index acd1cdd..caca9ee 100644 --- a/src/HushGP/PushTests/IntTests.hs +++ b/src/HushGP/PushTests/IntTests.hs @@ -26,8 +26,8 @@ prop_IntMod state = aaa1Test int instructionIntMod mod state prop_IntFromFloat :: State -> Property prop_IntFromFloat = ab1Test float int instructionIntFromFloat floor -prop_IntFromProperty :: State -> Property -prop_IntFromProperty = ab1Test bool int instructionIntFromBool (\x -> if x then 1 else 0) +prop_IntFromBool :: State -> Property +prop_IntFromBool = ab1Test bool int instructionIntFromBool (\x -> if x then 1 else 0) prop_IntMin :: State -> Property prop_IntMin = aaa1Test int instructionIntMin min diff --git a/src/HushGP/TH.hs b/src/HushGP/TH.hs new file mode 100644 index 0000000..a53b10d --- /dev/null +++ b/src/HushGP/TH.hs @@ -0,0 +1,38 @@ +module HushGP.TH where + +import Data.List +import Language.Haskell.TH +import Text.Regex.TDFA + +-- This old code made this all possible :) +-- https://github.com/finnsson/template-helper/blob/master/src/Language/Haskell/Extract.hs + +-- | A way to extract all functions from the file +-- `lines file` pulls all of the lines in one string from the file +-- `lex $ lines file` splits the function into a tuple +-- fst = the function nams, snd = the rest of the line +-- `concatMap lex $ lines file` maps lex onto all of the lines +-- and concats the result into a list +-- `filter (=~pattern) $ map fst $ concatMap lex $ lines file` filters +-- any line that doesn't have the passed pattern to it. "function" is a good pattern +-- for Hush. +-- `nub $ filter (=~pattern) $ map fst $ concatMap lex $ lines file` removes all +-- duplicates from the list. Or sets in this case :) +extractAllFunctions :: String -> Q [String] +extractAllFunctions pattern = do + loc <- location + -- file <- runIO $ readFile pattern + file <- runIO $ readFile $ loc_filename loc + return $ nub $ filter (=~ pattern) $ map fst $ concatMap lex $ lines file + +-- | Extracts all functions from a Q [String] (to be used with extractAllFunctions) +-- funcs has a list of all functions from extractAllFunctions +-- makePair makes a tuple of a passed function holding its name as a string and actual function value +-- in that order. StateFunc :) +-- `ListE $ map makePair funcs` makes a list of these function tuples holding all function +-- names and values. +functionExtractor :: String -> Q Exp +functionExtractor pattern = do + funcs <- extractAllFunctions pattern + let makePair n = TupE [Just $ VarE $ mkName n, Just $ LitE $ StringL n] + return $ ListE $ map makePair funcs From 108bc14d62908335a85df40ec5396734112906bc Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Thu, 13 Feb 2025 00:37:58 -0600 Subject: [PATCH 153/171] more todo --- TODO.md | 1 + 1 file changed, 1 insertion(+) diff --git a/TODO.md b/TODO.md index 2561fd5..e0cc3da 100644 --- a/TODO.md +++ b/TODO.md @@ -17,6 +17,7 @@ - [X] Use template haskell to generate function lists - [X] Move utility functions to their own file - [ ] Make add/sub/mult/div/mod instructions generic +- [ ] Use template haskell to (mostly) generate functions from generic ones ## PushGP TODO - [ ] Implement a Plushy genome translator From 24398989be82959a6c9de38203ef3d482027f435 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Thu, 13 Feb 2025 00:38:21 -0600 Subject: [PATCH 154/171] make some tests runnable --- test/Main.hs | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/test/Main.hs b/test/Main.hs index e923449..2c54d96 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -1,7 +1,7 @@ -import Instructions -import Push -import PushTests -import State +-- import HushGP.Instructions +-- import HushGP.Push +import HushGP.PushTests +-- import HushGP.State import Test.QuickCheck -- import Data.List @@ -19,3 +19,23 @@ qcw = quickCheckWith pushTestArgs vcw :: (Testable a) => a -> IO () vcw = verboseCheckWith pushTestArgs + +main :: IO () +main = do + qcw prop_IntAdd + qcw prop_IntSub + qcw prop_IntMul + qcw prop_IntDiv + qcw prop_IntMod + qcw prop_IntFromFloat + qcw prop_IntFromBool + qcw prop_IntMin + qcw prop_IntMax + qcw prop_IntInc + qcw prop_IntDec + qcw prop_IntLT + qcw prop_IntGT + qcw prop_IntLTE + qcw prop_IntGTE + qcw prop_IntDup + qcw prop_IntPop From 480f600ad3e9ff6e7ea1519bb80c54a826412d43 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Thu, 13 Feb 2025 00:39:52 -0600 Subject: [PATCH 155/171] add commutative opposites where it applies --- src/HushGP/Instructions/FloatInstructions.hs | 11 +++++++++++ src/HushGP/Instructions/IntInstructions.hs | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/HushGP/Instructions/FloatInstructions.hs b/src/HushGP/Instructions/FloatInstructions.hs index 2495b31..45a0c84 100644 --- a/src/HushGP/Instructions/FloatInstructions.hs +++ b/src/HushGP/Instructions/FloatInstructions.hs @@ -41,6 +41,11 @@ instructionFloatSub :: State -> State instructionFloatSub state@(State {_float = f1 : f2 : fs}) = state {_float = f2 - f1 : fs} instructionFloatSub state = state +-- |Subtracts the second float from the first float and pushes the result to the float stack. +instructionFloatSubOpp :: State -> State +instructionFloatSubOpp state@(State {_float = i1 : i2 : is}) = state {_float = i1 - i2 : is} +instructionFloatSubOpp state = state + -- |Multiplies the top two floats on the float stack. instructionFloatMul :: State -> State instructionFloatMul state@(State {_float = f1 : f2 : fs}) = state {_float = f2 * f1 : fs} @@ -51,6 +56,12 @@ instructionFloatDiv :: State -> State instructionFloatDiv state@(State {_float = f1 : f2 : fs}) = state {_float = if f1 /= 0 then f2 / f1 : fs else f1 : f2 : fs} instructionFloatDiv state = state +-- |Divides the second float from the first float and pushes the result to the float stack. +-- This does truncate. +instructionFloatDivOpp :: State -> State +instructionFloatDivOpp state@(State {_float = i1 : i2 : is}) = state {_float = if i2 /= 0 then (i1 / i2) : is else i1 : i2 : is} +instructionFloatDivOpp state = state + -- |Mods the first float from the second float on the float stack. instructionFloatMod :: State -> State instructionFloatMod state@(State {_float = f1 : f2 : fs}) = state {_float = if f1 /= 0 then f2 `mod'` f1 : fs else f1 : f2 : fs} diff --git a/src/HushGP/Instructions/IntInstructions.hs b/src/HushGP/Instructions/IntInstructions.hs index 543f0a7..43e8877 100644 --- a/src/HushGP/Instructions/IntInstructions.hs +++ b/src/HushGP/Instructions/IntInstructions.hs @@ -39,6 +39,11 @@ instructionIntSub :: State -> State instructionIntSub state@(State {_int = i1 : i2 : is}) = state {_int = i2 - i1 : is} instructionIntSub state = state +-- |Subtracts the second int from the first int and pushes the result to the int stack. +instructionIntSubOpp :: State -> State +instructionIntSubOpp state@(State {_int = i1 : i2 : is}) = state {_int = i1 - i2 : is} +instructionIntSubOpp state = state + -- |Multiplies the top two ints from the int stack and pushes the result to the int stack. instructionIntMul :: State -> State instructionIntMul state@(State {_int = i1 : i2 : is}) = state {_int = i2 * i1 : is} @@ -50,6 +55,12 @@ instructionIntDiv :: State -> State instructionIntDiv state@(State {_int = i1 : i2 : is}) = state {_int = if i1 /= 0 then (i2 `div` i1) : is else i1 : i2 : is} instructionIntDiv state = state +-- |Divides the second int from the first int and pushes the result to the int stack. +-- This does truncate. +instructionIntDivOpp :: State -> State +instructionIntDivOpp state@(State {_int = i1 : i2 : is}) = state {_int = if i2 /= 0 then (i1 `div` i2) : is else i1 : i2 : is} +instructionIntDivOpp state = state + -- |Mods the first float from the second float and pushes the result to the int stack. -- This does truncate. instructionIntMod :: State -> State From 714485e2e0117fd26808221efb0639e7cb598dc2 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Thu, 13 Feb 2025 02:40:40 -0600 Subject: [PATCH 156/171] add plushy todo --- TODO.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/TODO.md b/TODO.md index e0cc3da..359af1f 100644 --- a/TODO.md +++ b/TODO.md @@ -17,11 +17,12 @@ - [X] Use template haskell to generate function lists - [X] Move utility functions to their own file - [ ] Make add/sub/mult/div/mod instructions generic -- [ ] Use template haskell to (mostly) generate functions from generic ones +- [ ] Use template haskell to (mostly) generate functions from generic ones (Split files based on the arity of their functions) ## PushGP TODO - [ ] Implement a Plushy genome translator - [ ] Need to make this reproducable too (Check pysh json files) + - [ ] Implement silent markers as well - [ ] Add Memory - [ ] Add history stack(s), like a call stack - [ ] Implement interpreter options (could probably just place this into a map) From c8474bd4ef40e16bcfaf0facffee2743b31ef00f Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Thu, 13 Feb 2025 02:42:24 -0600 Subject: [PATCH 157/171] more plushy todo --- TODO.md | 1 + 1 file changed, 1 insertion(+) diff --git a/TODO.md b/TODO.md index 359af1f..05b7d26 100644 --- a/TODO.md +++ b/TODO.md @@ -23,6 +23,7 @@ - [ ] Implement a Plushy genome translator - [ ] Need to make this reproducable too (Check pysh json files) - [ ] Implement silent markers as well + - [ ] Have close amt of 1,2, and 3 - [ ] Add Memory - [ ] Add history stack(s), like a call stack - [ ] Implement interpreter options (could probably just place this into a map) From 56d546d1fd1a29183477bb5f140f2e4c60ed4b51 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Thu, 13 Feb 2025 02:44:32 -0600 Subject: [PATCH 158/171] supersize the plushy work --- TODO.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TODO.md b/TODO.md index 05b7d26..4b11d59 100644 --- a/TODO.md +++ b/TODO.md @@ -22,7 +22,7 @@ ## PushGP TODO - [ ] Implement a Plushy genome translator - [ ] Need to make this reproducable too (Check pysh json files) - - [ ] Implement silent markers as well + - [ ] Implement silent and skip markers as well - [ ] Have close amt of 1,2, and 3 - [ ] Add Memory - [ ] Add history stack(s), like a call stack From 5e08620a50b9f0a4fceb48eca1936009d018b787 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Thu, 13 Feb 2025 16:36:31 -0600 Subject: [PATCH 159/171] ercs on todo --- TODO.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/TODO.md b/TODO.md index 4b11d59..7d12fc9 100644 --- a/TODO.md +++ b/TODO.md @@ -24,6 +24,10 @@ - [ ] Need to make this reproducable too (Check pysh json files) - [ ] Implement silent and skip markers as well - [ ] Have close amt of 1,2, and 3 + - [ ] Need a random genome generator + - I'm only going to implement propeller's :specified version + - Is the best according to the papers - [ ] Add Memory - [ ] Add history stack(s), like a call stack - [ ] Implement interpreter options (could probably just place this into a map) +- [ ] Devise a good way to implement ERCs From 915ec947f566b0fa0ce3bacdf12b20703964dbe5 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Thu, 13 Feb 2025 23:18:04 -0600 Subject: [PATCH 160/171] a lot of changes, Int -> Integer, Float -> Double, ERCs, plushy testing, ... --- HushGP.cabal | 5 +- TODO.md | 6 +- src/HushGP/Genome.hs | 80 +++++++ src/HushGP/Instructions/CodeInstructions.hs | 18 +- src/HushGP/Instructions/ExecInstructions.hs | 4 +- src/HushGP/Instructions/FloatInstructions.hs | 6 +- .../Instructions/GenericInstructions.hs | 36 +-- src/HushGP/Instructions/IntInstructions.hs | 4 +- src/HushGP/Instructions/Opens.hs | 40 ++++ src/HushGP/Instructions/Utility.hs | 12 +- src/HushGP/Push.hs | 49 +++-- src/HushGP/PushTests.hs | 14 +- src/HushGP/PushTests/GenericTests.hs | 208 +++++++++--------- src/HushGP/PushTests/IntTests.hs | 114 +++++----- src/HushGP/PushTests/UtilTests.hs | 56 ++--- src/HushGP/State.hs | 156 ++++++++----- src/HushGP/Utility.hs | 15 ++ 17 files changed, 505 insertions(+), 318 deletions(-) create mode 100644 src/HushGP/Genome.hs create mode 100644 src/HushGP/Instructions/Opens.hs create mode 100644 src/HushGP/Utility.hs diff --git a/HushGP.cabal b/HushGP.cabal index 1d128fd..8e18df2 100644 --- a/HushGP.cabal +++ b/HushGP.cabal @@ -53,11 +53,14 @@ library , HushGP.Instructions.VectorBoolInstructions , HushGP.Instructions.VectorCharInstructions , HushGP.Instructions.Utility + , HushGP.Instructions.Opens , HushGP.PushTests , HushGP.PushTests.IntTests , HushGP.PushTests.GenericTests , HushGP.PushTests.UtilTests , HushGP.TH + , HushGP.Utility + , HushGP.Genome -- Modules included in this library but not exported. -- other-modules: @@ -67,7 +70,7 @@ library -- Other library packages from which modules are imported. build-depends: - base, containers, lens, split, QuickCheck, regex-tdfa, template-haskell + base, containers, lens, split, QuickCheck, regex-tdfa, template-haskell, random -- Directories containing source files. hs-source-dirs: src diff --git a/TODO.md b/TODO.md index 7d12fc9..52e81aa 100644 --- a/TODO.md +++ b/TODO.md @@ -24,10 +24,12 @@ - [ ] Need to make this reproducable too (Check pysh json files) - [ ] Implement silent and skip markers as well - [ ] Have close amt of 1,2, and 3 - - [ ] Need a random genome generator + - [X] Need a random genome generator - I'm only going to implement propeller's :specified version - Is the best according to the papers + - [ ] Need a NoOp that opens blocks - [ ] Add Memory - [ ] Add history stack(s), like a call stack - [ ] Implement interpreter options (could probably just place this into a map) -- [ ] Devise a good way to implement ERCs + - Should probably place this in a separate file +- [X] Devise a good way to implement ERCs diff --git a/src/HushGP/Genome.hs b/src/HushGP/Genome.hs new file mode 100644 index 0000000..e0442d6 --- /dev/null +++ b/src/HushGP/Genome.hs @@ -0,0 +1,80 @@ +module HushGP.Genome where + +import HushGP.State +import HushGP.Utility +import Data.Map qualified as Map +import Data.List +import Data.List.Split +import HushGP.Instructions.Opens + +-- |Makes a random plushy from variables in a passed argMap and +-- a passed list of instructions. +makeRandomPlushy :: Map.Map String String -> [Gene] -> IO [Gene] +makeRandomPlushy argMap = randomInstructions (read @Int (argMap Map.! "maxInitialPlushySize")) + +-- |A utility function to generate an amount based on an int rather than +-- from an argmap. +makeRandomPlushy' :: Int -> [Gene] -> IO [Gene] +makeRandomPlushy' = randomInstructions + +-- |Checks to see if a Gene is an (Open _) constructor. +isOpen :: Gene -> Bool +isOpen (Open _) = True +isOpen _ = False + +-- |Decrements the count of an (Open _) constructor. Acts as id +-- if the gene isn't an open. +decOpen :: Gene -> Gene +decOpen (Open n) = Open (n - 1) +decOpen gene = gene + +-- |Checks to see if the a list of genes with a single element is an opener. +isOpenerList :: [Gene] -> Bool +isOpenerList [instruction] = + case Map.lookup instruction instructionOpens of + Just _ -> True + _ -> False +isOpenerList _ = False + +-- |Gets the amount of blocks to open from a list of genes with a single element. +getOpenAmountList :: [Gene] -> Int +getOpenAmountList [instruction] = + case Map.lookup instruction instructionOpens of + Just amt -> amt + _ -> 0 +getOpenAmountList _ = 0 + +-- |Converts a plushy genome into a push genome. +plushyToPush :: [Gene] -> [Gene] +plushyToPush plushy = plushyToPush' (concatMap (\x -> if isOpenerList x then x <> [Open (getOpenAmountList x)] else x) (chunksOf 1 plushy)) [] + +-- |Internal function used to convert a plushy genome with opens in it into a push genome. +plushyToPush' :: [Gene] -> [Gene] -> [Gene] +plushyToPush' openPlushy push = + if null openPlushy + then + if any isOpen push + then plushyToPush' [Close] push + else push + else + if firstPlushy == Close + then + if any isOpen push + then plushyToPush' (drop 1 openPlushy) (if numOpen (push !! openIndex) == 1 then preOpen <> postOpen else preOpen <> postOpen <> [decOpen (Open (numOpen (push !! openIndex)))]) + else plushyToPush' (drop 1 openPlushy) push + else plushyToPush' (drop 1 openPlushy) (push <> [firstPlushy]) + where + firstPlushy :: Gene + firstPlushy = + case uncons openPlushy of + Just (g, _) -> g + _ -> error "This shouldn't happen" + postOpen :: [Gene] + postOpen = reverse (takeWhile (not . isOpen) (reverse push)) + openIndex :: Int + openIndex = length push - length postOpen - 1 + numOpen :: Gene -> Int + numOpen (Open n) = n + numOpen _ = 0 + preOpen :: [Gene] + preOpen = take openIndex push diff --git a/src/HushGP/Instructions/CodeInstructions.hs b/src/HushGP/Instructions/CodeInstructions.hs index 6454703..65b9b61 100644 --- a/src/HushGP/Instructions/CodeInstructions.hs +++ b/src/HushGP/Instructions/CodeInstructions.hs @@ -52,10 +52,10 @@ instructionCodeTail state = state -- https://faculty.hampshire.edu/lspector/push3-description.html#Type -- This is the CODE.NTHCDR command instructionCodeTailN :: State -> State -instructionCodeTailN state@(State {_code = Block bc : cs, _int = i : is}) = state {_code = Block (drop index bc) : cs, _int = is} +instructionCodeTailN state@(State {_code = Block bc : cs, _int = i1 : is}) = state {_code = Block (drop index bc) : cs, _int = is} where index :: Int - index = abs i `mod` length bc + index = fromIntegral (abs i1) `mod` length bc instructionCodeTailN state = state -- |If the top item on the code stack is a Block, takes the init of said Block and places the result on top of the code stack. @@ -101,8 +101,8 @@ instructionCodeDoThenPop state = state -- |Evaluates the top item on the code stack for each step along the range i to j. Both i and j are taken from the int stack. instructionCodeDoRange :: State -> State instructionCodeDoRange state@(State {_code = c1 : cs, _int = i0 : i1 : is, _exec = es}) = - if increment i0 i1 /= 0 - then state {_exec = c1 : Block [GeneInt (i1 + increment i0 i1), GeneInt i0, StateFunc (instructionCodeFromExec, "instructionCodeFromExec"), c1, StateFunc (instructionCodeDoRange, "instructionCodeDoRange")] : es, _int = i1 : is, _code = cs} + if increment (fromIntegral i0) (fromIntegral i1) /= 0 + then state {_exec = c1 : Block [GeneInt (i1 + toInteger (increment (fromIntegral i0) (fromIntegral i1))), GeneInt i0, StateFunc (instructionCodeFromExec, "instructionCodeFromExec"), c1, StateFunc (instructionCodeDoRange, "instructionCodeDoRange")] : es, _int = i1 : is, _code = cs} else state {_exec = c1: es, _int = i1 : is, _code = cs} where increment :: Int -> Int -> Int @@ -154,7 +154,7 @@ instructionCodeN state@(State {_code = (Block c1) : cs, _int = i1 : is}) = else state where index :: Int - index = abs i1 `mod` length c1 + index = fromIntegral (abs i1) `mod` length c1 instructionCodeN state@(State {_code = c1 : cs, _int = _ : is}) = state {_code = c1 : cs, _int = is} instructionCodeN state = state @@ -185,7 +185,7 @@ instructionCodeExtract state@(State {_code = block@(Block c1) : cs, _int = i1 : let index = abs i1 `mod` codeRecursiveSize block in - state{_code = codeAtPoint c1 index : cs, _int = is} + state{_code = codeAtPoint c1 (fromIntegral index) : cs, _int = is} instructionCodeExtract state@(State {_code = cs, _int = _ : is}) = state{_code = cs, _int = is} instructionCodeExtract state = state @@ -196,12 +196,12 @@ instructionCodeInsert state@(State {_code = block@(Block c1) : c2 : cs, _int = i let index = abs i1 `mod` codeRecursiveSize block in - state{_code = Block (codeInsertAtPoint c1 c2 index) : cs, _int = is} + state{_code = Block (codeInsertAtPoint c1 c2 (fromIntegral index)) : cs, _int = is} instructionCodeInsert state@(State {_code = c1 : c2 : cs, _int = i1 : is}) = let index = abs i1 `mod` codeRecursiveSize (Block [c1]) in - state{_code = Block (codeInsertAtPoint [c1] c2 index) : cs, _int = is} + state{_code = Block (codeInsertAtPoint [c1] c2 (fromIntegral index)) : cs, _int = is} instructionCodeInsert state = state -- |If the top code item is a Block that is empty, pushes 0 to the int stack if c2 is also an empty Block and -1 if not. @@ -209,7 +209,7 @@ instructionCodeInsert state = state -- If neither the top code item or second code item are Blocks, checks equality. If equal, pushes 1 to int stack, pushes 0 if not. instructionCodeFirstPosition :: State -> State instructionCodeFirstPosition state@(State {_code = (Block []) : c2 : cs, _int = is}) = state {_code = cs, _int = (if c2 == Block [] then 0 else -1) : is} -instructionCodeFirstPosition state@(State {_code = (Block c1) : c2 : cs, _int = is}) = state {_code = cs, _int = positionElem c1 c2 : is} +instructionCodeFirstPosition state@(State {_code = (Block c1) : c2 : cs, _int = is}) = state {_code = cs, _int = fromIntegral (positionElem c1 c2) : is} where positionElem :: [Gene] -> Gene -> Int positionElem genes gene = diff --git a/src/HushGP/Instructions/ExecInstructions.hs b/src/HushGP/Instructions/ExecInstructions.hs index 2c1d0a4..ef69261 100644 --- a/src/HushGP/Instructions/ExecInstructions.hs +++ b/src/HushGP/Instructions/ExecInstructions.hs @@ -76,8 +76,8 @@ instructionExecIsStackEmpty = instructionIsStackEmpty exec -- taken from the int stack. Differs from code_do_range only in the source of the code and the recursive call. instructionExecDoRange :: State -> State instructionExecDoRange state@(State {_exec = e1 : es, _int = i0 : i1 : is}) = - if increment i0 i1 /= 0 - then state {_exec = e1 : Block [GeneInt (i1 + increment i0 i1), GeneInt i0, StateFunc (instructionExecDoRange, "instructionExecDoRange"), e1] : es, _int = i1 : is} + if increment (fromIntegral i0) (fromIntegral i1) /= 0 + then state {_exec = e1 : Block [GeneInt (i1 + toInteger (increment (fromIntegral i0) (fromIntegral i1))), GeneInt i0, StateFunc (instructionExecDoRange, "instructionExecDoRange"), e1] : es, _int = i1 : is} else state {_exec = e1 : es, _int = i1 : is} where increment :: Int -> Int -> Int diff --git a/src/HushGP/Instructions/FloatInstructions.hs b/src/HushGP/Instructions/FloatInstructions.hs index 45a0c84..cbfd4b3 100644 --- a/src/HushGP/Instructions/FloatInstructions.hs +++ b/src/HushGP/Instructions/FloatInstructions.hs @@ -10,7 +10,7 @@ import HushGP.TH -- |Converts the top int to a float and pushes the result to the float stack. instructionFloatFromInt :: State -> State -instructionFloatFromInt state@(State {_float = fs, _int = i1 : is}) = state {_float = (fromIntegral i1 :: Float) : fs, _int = is} +instructionFloatFromInt state@(State {_float = fs, _int = i1 : is}) = state {_float = (fromIntegral i1 :: Double) : fs, _int = is} instructionFloatFromInt state = state -- |If the top bool True, pushes 1.0 to the float stack. Pushes 0.0 if False. @@ -20,14 +20,14 @@ instructionFloatFromBool state = state -- |Takes the top char and converts it to int representation. That int then gets casted to a float and pushed to the float stack. instructionFloatFromChar :: State -> State -instructionFloatFromChar state@(State {_char = c1 : cs, _float = fs}) = state {_char = cs, _float = (fromIntegral (ord c1) :: Float) : fs} +instructionFloatFromChar state@(State {_char = c1 : cs, _float = fs}) = state {_char = cs, _float = (fromIntegral (ord c1) :: Double) : fs} instructionFloatFromChar state = state -- |Reads the top string and converts it to a float if possible. If not, acts as a NoOp. instructionFloatFromString :: State -> State instructionFloatFromString state@(State {_string = s1 : ss, _float = fs}) = if all (\x -> isDigit x || x == '.') s1 && amtOccurences "." s1 <= 1 - then state{_string = ss, _float = read @Float s1 : fs} + then state{_string = ss, _float = read @Double s1 : fs} else state instructionFloatFromString state = state diff --git a/src/HushGP/Instructions/GenericInstructions.hs b/src/HushGP/Instructions/GenericInstructions.hs index c754252..5a557a5 100644 --- a/src/HushGP/Instructions/GenericInstructions.hs +++ b/src/HushGP/Instructions/GenericInstructions.hs @@ -35,7 +35,7 @@ instructionDupN accessor state = _ -> state _ -> state where - instructionDupNHelper :: Int -> a -> Lens' State [a] -> State -> State + instructionDupNHelper :: Integral b => b -> a -> Lens' State [a] -> State -> State instructionDupNHelper count instruction internalAccessor internalState = if count > 0 then instructionDupNHelper (count - 1) instruction internalAccessor (internalState & accessor .~ (instruction : view accessor internalState)) @@ -47,7 +47,7 @@ instructionDupItems :: Lens' State [a] -> State -> State instructionDupItems accessor state@(State {_int = i1 : is}) = if i1 <= 0 then state{_int = is} - else state{_int = is} & accessor .~ (take i1 (view accessor state{_int = is}) <> view accessor state{_int = is}) + else state{_int = is} & accessor .~ (take (fromIntegral i1) (view accessor state{_int = is}) <> view accessor state{_int = is}) instructionDupItems _ state = state -- |Swaps the top two instructions based on a lens @@ -88,14 +88,14 @@ instructionEq accessor state = -- |Calculates the stack depth based on a lens and pushes the result to the int stackk. instructionStackDepth :: Lens' State [a] -> State -> State -instructionStackDepth accessor state@(State {_int = is}) = state{_int = length (view accessor state) : is} +instructionStackDepth accessor state@(State {_int = is}) = state{_int = toInteger (length (view accessor state)) : is} -- |Copies an item from deep within a lens' stack to the top of the lens' stack based on -- the top int from the int stack. instructionYankDup :: Lens' State [a] -> State -> State instructionYankDup accessor state@(State {_int = i1 : is}) = if notEmptyStack accessor state - then state{_int = is} & accessor .~ (view accessor state{_int = is} !! max 0 (min i1 (length (view accessor state{_int = is}) - 1))) : view accessor state{_int = is} + then state{_int = is} & accessor .~ (view accessor state{_int = is} !! max 0 (min (fromIntegral i1) (length (view accessor state{_int = is}) - 1))) : view accessor state{_int = is} else state instructionYankDup _ state = state @@ -105,7 +105,7 @@ instructionYank :: forall a. Lens' State [a] -> State -> State instructionYank accessor state@(State {_int = i1 : is}) = let myIndex :: Int - myIndex = max 0 (min i1 (length (view accessor state{_int = is}) - 1)) + myIndex = max 0 (min (fromIntegral i1) (length (view accessor state{_int = is}) - 1)) item :: a item = view accessor state{_int = is} !! myIndex deletedState :: State @@ -122,7 +122,7 @@ instructionYank _ state = state instructionShoveDup :: Lens' State [a] -> State -> State instructionShoveDup accessor state@(State {_int = i1 : is}) = case uncons (view accessor state{_int = is}) of - Just (x,_) -> state{_int = is} & accessor .~ combineTuple x (splitAt (max 0 (min i1 (length (view accessor state{_int = is}) - 1))) (view accessor state{_int = is})) + Just (x,_) -> state{_int = is} & accessor .~ combineTuple x (splitAt (max 0 (min (fromIntegral i1) (length (view accessor state{_int = is}) - 1))) (view accessor state{_int = is})) _ -> state instructionShoveDup _ state = state @@ -182,7 +182,7 @@ instructionVectorTakeRN _ state = state instructionSubVector :: Lens' State [[a]] -> State -> State instructionSubVector accessor state@(State {_int = i1 : i2 : is}) = case uncons (view accessor state) of - Just (v1, vs) -> state{_int = is} & accessor .~ (subList i1 i2 v1 : vs) + Just (v1, vs) -> state{_int = is} & accessor .~ (subList (fromIntegral i1) (fromIntegral i2) v1 : vs) _ -> state instructionSubVector _ state = state @@ -301,7 +301,7 @@ instructionVectorDropR _ state = state instructionLength :: Lens' State [[a]] -> State -> State instructionLength accessor state@(State {_int = is}) = case uncons (view accessor state) of - Just (v1, vs) -> state{_int = length v1 : is} & accessor .~ vs + Just (v1, vs) -> state{_int = toInteger (length v1) : is} & accessor .~ vs _ -> state -- |Takes the top vector, reverses it, based on a lens. @@ -355,7 +355,7 @@ instructionVectorContainsVector accessor state@(State {_bool = bs}) = instructionVectorIndexOf :: Eq a => Lens' State [a] -> Lens' State [[a]] -> State -> State instructionVectorIndexOf primAccessor vectorAccessor state = case (uncons (view vectorAccessor state), uncons (view primAccessor state)) of - (Just (v1, vs), Just (p1, ps)) -> (state & vectorAccessor .~ vs & primAccessor .~ ps) & int .~ (findSubA v1 [p1] : view int (state & vectorAccessor .~ vs & primAccessor .~ ps)) + (Just (v1, vs), Just (p1, ps)) -> (state & vectorAccessor .~ vs & primAccessor .~ ps) & int .~ (toInteger (findSubA v1 [p1]) : view int (state & vectorAccessor .~ vs & primAccessor .~ ps)) _ -> state -- |Based on a vector lens and the two vectors on top of said stack. Searches and pushes the @@ -363,7 +363,7 @@ instructionVectorIndexOf primAccessor vectorAccessor state = instructionVectorIndexOfVector :: Eq a => Lens' State [[a]] -> State -> State instructionVectorIndexOfVector accessor state@(State {_int = is}) = case uncons (view accessor state) of - Just (v1, v2 : vs) -> state & accessor .~ vs & int .~ (findSubA v1 v2 : is) + Just (v1, v2 : vs) -> state & accessor .~ vs & int .~ (toInteger (findSubA v1 v2) : is) _ -> state -- |Based on two lenses, one of a primitive type and the next of a vector type, @@ -372,7 +372,7 @@ instructionVectorIndexOfVector accessor state@(State {_int = is}) = instructionVectorOccurrencesOf :: Eq a => Lens' State [a] -> Lens' State [[a]] -> State -> State instructionVectorOccurrencesOf primAccessor vectorAccessor state = case (uncons (view vectorAccessor state), uncons (view primAccessor state)) of - (Just (v1, vs), Just (p1, ps)) -> (state & vectorAccessor .~ vs & primAccessor .~ ps) & int .~ (amtOccurences v1 [p1] : view int (state & vectorAccessor .~ vs & primAccessor .~ ps)) + (Just (v1, vs), Just (p1, ps)) -> (state & vectorAccessor .~ vs & primAccessor .~ ps) & int .~ (toInteger (amtOccurences v1 [p1]) : view int (state & vectorAccessor .~ vs & primAccessor .~ ps)) _ -> state -- |Based on a vector lens and the top two vectors in said stack, @@ -381,7 +381,7 @@ instructionVectorOccurrencesOf primAccessor vectorAccessor state = instructionVectorOccurrencesOfVector :: Eq a => Lens' State [[a]] -> State -> State instructionVectorOccurrencesOfVector accessor state@(State {_int = is}) = case uncons (view accessor state) of - Just (v1, v2 : vs) -> state & accessor .~ vs & int .~ (amtOccurences v1 v2 : is) + Just (v1, v2 : vs) -> state & accessor .~ vs & int .~ (toInteger (amtOccurences v1 v2) : is) _ -> state -- |This function parses the primitives inside a vector type and pushes that vector split into @@ -435,7 +435,7 @@ instructionVectorReplace primAccessor vectorAccessor amt state = -- the primitive stack. The top of the primitive stack is the old value to be replaced. The second item -- in the primitive stack is the new value to replace the old one. N is pulled from the top of the int stack. instructionVectorReplaceN :: Eq a => Lens' State [a] -> Lens' State [[a]] -> State -> State -instructionVectorReplaceN primAccessor vectorAccessor state@(State {_int = i1 : is}) = instructionVectorReplace primAccessor vectorAccessor (Just i1) state{_int = is} +instructionVectorReplaceN primAccessor vectorAccessor state@(State {_int = i1 : is}) = instructionVectorReplace primAccessor vectorAccessor (Just (fromIntegral i1)) state{_int = is} instructionVectorReplaceN _ _ state = state -- |Based on a vector lens and the top three vectors on said stack. @@ -452,7 +452,7 @@ instructionVectorReplaceVector accessor amt state = -- Inside of the first vector, replaces the number of instances specified -- by the top of the int stack of the second vector with the third vector. instructionVectorReplaceVectorN :: Eq a => Lens' State [[a]] -> State -> State -instructionVectorReplaceVectorN accessor state@(State {_int = i1 : is}) = instructionVectorReplaceVector accessor (Just i1) state{_int = is} +instructionVectorReplaceVectorN accessor state@(State {_int = i1 : is}) = instructionVectorReplaceVector accessor (Just (fromIntegral i1)) state{_int = is} instructionVectorReplaceVectorN _ state = state -- |Based on two lenses, one of a primitive type and the next of a vector type, @@ -470,7 +470,7 @@ instructionVectorRemove primAccessor vectorAccessor amt state = -- item from the primitive stack equals a primitive inside of the vector stack. N is pulled -- from the top of the int stack. Not to be confused with instructionVectorRemoveNth. instructionVectorRemoveN :: Eq a => Lens' State [a] -> Lens' State [[a]] -> State -> State -instructionVectorRemoveN primAccessor vectorAccessor state@(State {_int = i1 : is}) = instructionVectorRemove primAccessor vectorAccessor (Just i1) state{_int = is} +instructionVectorRemoveN primAccessor vectorAccessor state@(State {_int = i1 : is}) = instructionVectorRemove primAccessor vectorAccessor (Just (fromIntegral i1)) state{_int = is} instructionVectorRemoveN _ _ state = state -- |Based on a vector lens. Removes the Nth index of the top vector of the passed @@ -496,7 +496,7 @@ instructionVectorRemoveVector accessor amt state = -- Inside of the first vector, removes the number of instances specified -- by the top of the int stack of the second vector. instructionVectorRemoveVectorN :: Eq a => Lens' State [[a]] -> State -> State -instructionVectorRemoveVectorN accessor state@(State {_int = i1 : is}) = instructionVectorRemoveVector accessor (Just i1) state{_int = is} +instructionVectorRemoveVectorN accessor state@(State {_int = i1 : is}) = instructionVectorRemoveVector accessor (Just (fromIntegral i1)) state{_int = is} instructionVectorRemoveVectorN _ state = state -- |Based on two lenses, one of a primitive type and the next of a vector type, @@ -542,7 +542,7 @@ instructionVectorSortReverse accessor state = instructionVectorInsert :: Lens' State [a] -> Lens' State [[a]] -> State -> State instructionVectorInsert primAccessor vectorAccessor state@(State {_int = i1 : is}) = case (uncons (view vectorAccessor state{_int = is}), uncons (view primAccessor state{_int = is})) of - (Just (v1, vs), Just (p1, ps)) -> state{_int = is} & primAccessor .~ ps & vectorAccessor .~ (combineTuple p1 (splitAt i1 v1) : vs) + (Just (v1, vs), Just (p1, ps)) -> state{_int = is} & primAccessor .~ ps & vectorAccessor .~ (combineTuple p1 (splitAt (fromIntegral i1) v1) : vs) _ -> state instructionVectorInsert _ _ state = state @@ -553,6 +553,6 @@ instructionVectorInsertVector :: Lens' State [[a]] -> State -> State instructionVectorInsertVector accessor state@(State {_int = i1 : is}) = case uncons (view accessor state) of Just (v1, v2 : vs) -> - state{_int = is} & accessor .~ (combineTupleList v2 (splitAt i1 v1) : vs) + state{_int = is} & accessor .~ (combineTupleList v2 (splitAt (fromIntegral i1) v1) : vs) _ -> state instructionVectorInsertVector _ state = state diff --git a/src/HushGP/Instructions/IntInstructions.hs b/src/HushGP/Instructions/IntInstructions.hs index 43e8877..5d49a6f 100644 --- a/src/HushGP/Instructions/IntInstructions.hs +++ b/src/HushGP/Instructions/IntInstructions.hs @@ -18,14 +18,14 @@ instructionIntFromBool state = state -- |Takes the top char and converts it to int representation. The result is pushed to the int stack. instructionIntFromChar :: State -> State -instructionIntFromChar state@(State {_char = c1 : cs, _int = is}) = state {_char = cs, _int = ord c1 : is} +instructionIntFromChar state@(State {_char = c1 : cs, _int = is}) = state {_char = cs, _int = fromIntegral (ord c1) : is} instructionIntFromChar state = state -- |Reads the top string and converts it to a int if possible. If not, acts as a NoOp. instructionIntFromString :: State -> State instructionIntFromString state@(State {_string = s1 : ss, _int = is}) = if all isDigit s1 - then state{_string = ss, _int = read @Int s1 : is} + then state{_string = ss, _int = read @Integer s1 : is} else state instructionIntFromString state = state diff --git a/src/HushGP/Instructions/Opens.hs b/src/HushGP/Instructions/Opens.hs new file mode 100644 index 0000000..d4334d5 --- /dev/null +++ b/src/HushGP/Instructions/Opens.hs @@ -0,0 +1,40 @@ +module HushGP.Instructions.Opens where + +import HushGP.State +import Data.Map qualified as Map +import HushGP.Instructions.ExecInstructions +import HushGP.Instructions.StringInstructions +import HushGP.Instructions.VectorIntInstructions +import HushGP.Instructions.VectorBoolInstructions +import HushGP.Instructions.VectorFloatInstructions +import HushGP.Instructions.VectorStringInstructions +import HushGP.Instructions.VectorCharInstructions + +-- |A Map that takes a Gene and returns how many Blocks it opens. +-- To be used in plushy conversion. +instructionOpens :: Map.Map Gene Int +instructionOpens = Map.fromList [ + (StateFunc (instructionExecIf, "instructionsExecIf"), 2), + (StateFunc (instructionExecDup, "instructionExecDup"), 1), + (StateFunc (instructionExecDupN, "instructionExecDupN"), 1), + (StateFunc (instructionExecPop, "instructionExecPop"), 1), + (StateFunc (instructionExecSwap, "instructionExecSwap"), 2), + (StateFunc (instructionExecRot, "instructionExecRot"), 3), + (StateFunc (instructionExecShove, "instructionExecShove"), 1), + (StateFunc (instructionExecShoveDup, "instructionExecShoveDup"), 1), + (StateFunc (instructionExecDoRange, "instructionExecDoRange"), 1), + (StateFunc (instructionExecDoCount, "instructionExecDoCount"), 1), + (StateFunc (instructionExecDoTimes, "instructionExecDoTimes"), 1), + (StateFunc (instructionExecWhile, "instructionExecWhile"), 1), + (StateFunc (instructionExecDoWhile, "instructionExecDoWhile"), 1), + (StateFunc (instructionExecWhen, "instructionExecWhen"), 1), + (StateFunc (instructionExecK, "instructionExecK"), 2), + (StateFunc (instructionExecS, "instructionExecS"), 3), + (StateFunc (instructionExecY, "instructionExecY"), 1), + (StateFunc (instructionStringIterate, "instructionStringIterate"), 1), + (StateFunc (instructionVectorIntIterate, "instructionVectorIntIterate"), 1), + (StateFunc (instructionVectorFloatIterate, "instructionVectorFloatIterate"), 1), + (StateFunc (instructionVectorStringIterate, "instructionVectorStringIterate"), 1), + (StateFunc (instructionVectorBoolIterate, "instructionVectorBoolIterate"), 1), + (StateFunc (instructionVectorCharIterate, "instructionVectorCharIterate"), 1) + ] diff --git a/src/HushGP/Instructions/Utility.hs b/src/HushGP/Instructions/Utility.hs index b58ebb7..bfbc8b6 100644 --- a/src/HushGP/Instructions/Utility.hs +++ b/src/HushGP/Instructions/Utility.hs @@ -135,8 +135,8 @@ isBlock _ = False -- |Utility function: Returns the length of the passed block. -- If the gene isn't a block, returns 1 -blockLength :: Gene -> Int -blockLength (Block bxs) = length bxs +blockLength :: Gene -> Integer +blockLength (Block bxs) = toInteger $ length bxs blockLength _ = 1 -- |Utility function: Returns true if the passed block is empty, false is not. @@ -150,7 +150,7 @@ blockIsNull _ = False -- CODE.CONTAINER findContainer :: Gene -> Gene -> Gene findContainer (Block fullA) gene - | length fullA <= blockLength gene = Block [] + | fromIntegral (length fullA) <= blockLength gene = Block [] | gene `elem` fullA = Block [] -- Not allowed to be top level | any isBlock fullA = findContainer' (filter isBlock fullA) gene | otherwise = Block [] @@ -162,8 +162,8 @@ findContainer (Block fullA) gene findContainer _ _ = Block [] -- |Utility Function: A helper function for instructionCodeDiscrepancy. The full description is there. -countDiscrepancy :: Gene -> Gene -> Int -countDiscrepancy (Block xs) (Block ys) = sum [if uncurry (==) tup then 0 else 1 | tup <- zip xs ys] + abs (length xs - length ys) +countDiscrepancy :: Gene -> Gene -> Integer +countDiscrepancy (Block xs) (Block ys) = sum [if uncurry (==) tup then 0 else 1 | tup <- zip xs ys] + abs (toInteger (length xs) - toInteger (length ys)) countDiscrepancy xgene ygene = if xgene == ygene then 1 else 0 -- |Utility Function: Extracts the first gene from a block. Returns itself if not a block @@ -220,7 +220,7 @@ codeMember (Block bxs) ygene = ygene `elem` bxs codeMember _ _ = False -- |Utility Function: Calculates the size of a Block including counting the nested Blocks recursively -codeRecursiveSize :: Gene -> Int +codeRecursiveSize :: Gene -> Integer codeRecursiveSize (Block bxs) = sum [codeRecursiveSize x + if isBlock x then 1 else 0 | x <- bxs] codeRecursiveSize _ = 1 diff --git a/src/HushGP/Push.hs b/src/HushGP/Push.hs index 342968e..a676ca0 100644 --- a/src/HushGP/Push.hs +++ b/src/HushGP/Push.hs @@ -12,25 +12,25 @@ import HushGP.State -- Everntually, this can be part of the apply func to state helpers, -- which should take the number and type of parameter they have. --- | This is one of the push genome functions itself, not infrastructure. +-- This is one of the push genome functions itself, not infrastructure. -- Optionally, split this off into independent functions -instructionParameterLoad :: State -> State -instructionParameterLoad state@(State {_parameter = (p : _)}) = case p of - (GeneInt val) -> state & int .~ val : view int state - (GeneFloat val) -> state & float .~ val : view float state - (GeneBool val) -> state & bool .~ val : view bool state - (GeneString val) -> state & string .~ val : view string state - (GeneChar val) -> state & char .~ val : view char state - (GeneVectorInt val) -> state & vectorInt .~ val : view vectorInt state - (GeneVectorFloat val) -> state & vectorFloat .~ val : view vectorFloat state - (GeneVectorBool val) -> state & vectorBool .~ val : view vectorBool state - (GeneVectorString val) -> state & vectorString .~ val : view vectorString state - (GeneVectorChar val) -> state & vectorChar .~ val : view vectorChar state - (StateFunc _) -> undefined - (PlaceInput _) -> undefined - Close -> undefined - (Block xs) -> state & exec .~ xs <> view exec state -instructionParameterLoad state = state +-- instructionParameterLoad :: State -> State +-- instructionParameterLoad state@(State {_parameter = (p : _)}) = case p of +-- (GeneInt val) -> state & int .~ val : view int state +-- (GeneFloat val) -> state & float .~ val : view float state +-- (GeneBool val) -> state & bool .~ val : view bool state +-- (GeneString val) -> state & string .~ val : view string state +-- (GeneChar val) -> state & char .~ val : view char state +-- (GeneVectorInt val) -> state & vectorInt .~ val : view vectorInt state +-- (GeneVectorFloat val) -> state & vectorFloat .~ val : view vectorFloat state +-- (GeneVectorBool val) -> state & vectorBool .~ val : view vectorBool state +-- (GeneVectorString val) -> state & vectorString .~ val : view vectorString state +-- (GeneVectorChar val) -> state & vectorChar .~ val : view vectorChar state +-- (StateFunc _) -> undefined +-- (PlaceInput _) -> undefined +-- Close -> undefined +-- (Block xs) -> state & exec .~ xs <> view exec state +-- instructionParameterLoad state = state -- | Loads a genome into the exec stack loadProgram :: [Gene] -> State -> State @@ -62,5 +62,16 @@ interpretExec state@(State {_exec = e : es}) = (StateFunc (func, _)) -> interpretExec $ func state {_exec = es} (Block block) -> interpretExec (state {_exec = block ++ es}) (PlaceInput val) -> interpretExec (state {_exec = (view input state Map.! val) : es}) - Close -> undefined -- This should be removed later. Will be converted to Blocks in the Plushy -> Exec stack process + (GeneIntERC (val, _)) -> interpretExec (state & exec .~ es & int .~ val : view int state) + (GeneFloatERC (val, _)) -> interpretExec (state & exec .~ es & float .~ val : view float state) + (GeneBoolERC (val, _)) -> interpretExec (state & exec .~ es & bool .~ val : view bool state) + (GeneStringERC (val, _)) -> interpretExec (state & exec .~ es & string .~ val : view string state) + (GeneCharERC (val, _)) -> interpretExec (state & exec .~ es & char .~ val : view char state) + (GeneVectorIntERC (val, _)) -> interpretExec (state & exec .~ es & vectorInt .~ val : view vectorInt state) + (GeneVectorFloatERC (val, _)) -> interpretExec (state & exec .~ es & vectorFloat .~ val : view vectorFloat state) + (GeneVectorBoolERC (val, _)) -> interpretExec (state & exec .~ es & vectorBool .~ val : view vectorBool state) + (GeneVectorStringERC (val, _)) -> interpretExec (state & exec .~ es & vectorString .~ val : view vectorString state) + (GeneVectorCharERC (val, _)) -> interpretExec (state & exec .~ es & vectorChar .~ val : view vectorChar state) + Close -> undefined -- This should never happen. Will be converted to Blocks in the Plushy -> Exec stack process + (Open _) -> undefined -- This should also never happen. Should be converted in Plushy -> Exec stack process interpretExec state = state diff --git a/src/HushGP/PushTests.hs b/src/HushGP/PushTests.hs index 24f356e..72344ab 100644 --- a/src/HushGP/PushTests.hs +++ b/src/HushGP/PushTests.hs @@ -1,10 +1,10 @@ module HushGP.PushTests - ( module HushGP.PushTests.GenericTests, - module HushGP.PushTests.IntTests, - module HushGP.PushTests.UtilTests, - ) + -- ( module HushGP.PushTests.GenericTests, + -- module HushGP.PushTests.IntTests, + -- module HushGP.PushTests.UtilTests, + -- ) where -import HushGP.PushTests.GenericTests -import HushGP.PushTests.IntTests -import HushGP.PushTests.UtilTests +-- import HushGP.PushTests.GenericTests +-- import HushGP.PushTests.IntTests +-- import HushGP.PushTests.UtilTests diff --git a/src/HushGP/PushTests/GenericTests.hs b/src/HushGP/PushTests/GenericTests.hs index 807af7b..b726e54 100644 --- a/src/HushGP/PushTests/GenericTests.hs +++ b/src/HushGP/PushTests/GenericTests.hs @@ -1,120 +1,103 @@ module HushGP.PushTests.GenericTests where -import HushGP.State -import Control.Lens --- import Debug.Trace -import Test.QuickCheck --- import HushGP.Instructions.GenericInstructions +-- import HushGP.State +-- import Control.Lens +-- -- import Debug.Trace +-- import Test.QuickCheck +-- -- import HushGP.Instructions.GenericInstructions --- The naming scheme: --- the letters at the beginning represent what kind of transformation (the word I'm using for a basic function) to the states is happening --- for example: the function aaa1Test relays this arg takes a transformation of two as and turns them into one a --- the numbers represent how many different stacks are used in the function. --- for example: the aaa1Test relays that it takes one stack as input. These stacks are passed in as Lens +-- -- The naming scheme: +-- -- the letters at the beginning represent what kind of transformation (the word I'm using for a basic function) to the states is happening +-- -- for example: the function aaa1Test relays this arg takes a transformation of two as and turns them into one a +-- -- the numbers represent how many different stacks are used in the function. +-- -- for example: the aaa1Test relays that it takes one stack as input. These stacks are passed in as Lens --- We may be able to get rid of Lens entirely and use haskell's integrated accessors of type State -> [a] --- You can see what I'm talking about if you go into ghci and type: `:info _int` for example +-- -- We may be able to get rid of Lens entirely and use haskell's integrated accessors of type State -> [a] +-- -- You can see what I'm talking about if you go into ghci and type: `:info _int` for example -aaa1Test :: (Show a, Eq a) => Lens' State [a] -> (State -> State) -> (a -> a -> a) -> State -> Property -aaa1Test accessor instruction transformation state = - case (uncons (view accessor state), uncons (view accessor $ instruction state)) of - (Just (origx1, origx2 : _), Just (modx1, _)) -> transformation origx2 origx1 === modx1 .&&. length (view accessor state) === length (view accessor $ instruction state) + 1 - _ -> state === instruction state +-- aaa1Test :: (Show a, Eq a) => Lens' State [a] -> (State -> State) -> (a -> a -> a) -> State -> Property +-- aaa1Test accessor instruction transformation state = +-- case (uncons (view accessor state), uncons (view accessor $ instruction state)) of +-- (Just (origx1, origx2 : _), Just (modx1, _)) -> transformation origx2 origx1 === modx1 .&&. length (view accessor state) === length (view accessor $ instruction state) + 1 +-- _ -> state === instruction state -aa1Test :: (Show a, Eq a) => Lens' State [a] -> (State -> State) -> (a -> a) -> State -> Property -aa1Test accessor instruction transformation state = - case (uncons (view accessor state), uncons (view accessor $ instruction state)) of - (Just (origx1, _), Just (modx1, _)) -> transformation origx1 === modx1 .&&. length (view accessor state) === length (view accessor $ instruction state) - _ -> state === instruction state +-- aa1Test :: (Show a, Eq a) => Lens' State [a] -> (State -> State) -> (a -> a) -> State -> Property +-- aa1Test accessor instruction transformation state = +-- case (uncons (view accessor state), uncons (view accessor $ instruction state)) of +-- (Just (origx1, _), Just (modx1, _)) -> transformation origx1 === modx1 .&&. length (view accessor state) === length (view accessor $ instruction state) +-- _ -> state === instruction state -ab1Test :: (Show b, Eq b) => Lens' State [a] -> Lens' State [b] -> (State -> State) -> (a -> b) -> State -> Property -ab1Test accessorFrom accessorTo instruction transformation state = - case (uncons (view accessorTo $ instruction state), uncons (view accessorFrom state)) of - (Just (t1, _), Just (f1, _)) -> - t1 === transformation f1 .&&. - length (view accessorTo $ instruction state) === length (view accessorTo state) + 1 .&&. - length (view accessorFrom $ instruction state) === length (view accessorFrom state) - 1 - _ -> state === instruction state +-- ab1Test :: (Show b, Eq b) => Lens' State [a] -> Lens' State [b] -> (State -> State) -> (a -> b) -> State -> Property +-- ab1Test accessorFrom accessorTo instruction transformation state = +-- case (uncons (view accessorTo $ instruction state), uncons (view accessorFrom state)) of +-- (Just (t1, _), Just (f1, _)) -> +-- t1 === transformation f1 .&&. +-- length (view accessorTo $ instruction state) === length (view accessorTo state) + 1 .&&. +-- length (view accessorFrom $ instruction state) === length (view accessorFrom state) - 1 +-- _ -> state === instruction state -aab2Test :: (Show b, Eq b) => Lens' State [a] -> Lens' State [b] -> (State -> State) -> (a -> a -> b) -> State -> Property -aab2Test accessorFrom accessorTo instruction transformation state = - case (uncons (view accessorTo $ instruction state), uncons (view accessorFrom state)) of - (Just (t1, _), Just (f1, f2 : _)) -> - t1 === transformation f1 f2 .&&. - length (view accessorTo $ instruction state) == length (view accessorTo state) + 1 .&&. - length (view accessorFrom $ instruction state) == length (view accessorFrom state) - 2 - _ -> state === instruction state +-- aab2Test :: (Show b, Eq b) => Lens' State [a] -> Lens' State [b] -> (State -> State) -> (a -> a -> b) -> State -> Property +-- aab2Test accessorFrom accessorTo instruction transformation state = +-- case (uncons (view accessorTo $ instruction state), uncons (view accessorFrom state)) of +-- (Just (t1, _), Just (f1, f2 : _)) -> +-- t1 === transformation f1 f2 .&&. +-- length (view accessorTo $ instruction state) == length (view accessorTo state) + 1 .&&. +-- length (view accessorFrom $ instruction state) == length (view accessorFrom state) - 2 +-- _ -> state === instruction state -popTest :: (Show a) => Lens' State [a] -> (State -> State) -> State -> Property -popTest accessor instruction state = - if null $ view accessor state - then state === instruction state - else length (view accessor $ instruction state) === length (view accessor state) - 1 +-- popTest :: (Show a) => Lens' State [a] -> (State -> State) -> State -> Property +-- popTest accessor instruction state = +-- if null $ view accessor state +-- then state === instruction state +-- else length (view accessor $ instruction state) === length (view accessor state) - 1 -dupTest :: (Eq a, Show a) => Lens' State [a] -> (State -> State) -> State -> Property -dupTest accessor instruction state = - case uncons (view accessor state) of - Just (origx1, _) -> - case uncons (view accessor $ instruction state) of - Just (modx1, modx2 : _) -> - origx1 === modx1 .&&. origx1 === modx2 .&&. length (view accessor $ instruction state) === length (view accessor state) + 1 - _ -> state === instruction state - _ -> state === instruction state +-- dupTest :: (Eq a, Show a) => Lens' State [a] -> (State -> State) -> State -> Property +-- dupTest accessor instruction state = +-- case uncons (view accessor state) of +-- Just (origx1, _) -> +-- case uncons (view accessor $ instruction state) of +-- Just (modx1, modx2 : _) -> +-- origx1 === modx1 .&&. origx1 === modx2 .&&. length (view accessor $ instruction state) === length (view accessor state) + 1 +-- _ -> state === instruction state +-- _ -> state === instruction state --- How to test the int stack in particular? -dupTestN :: (Eq a, Show a) => Lens' State [a] -> (State -> State) -> State -> Property -dupTestN accessor instruction state = - case uncons (view int state) of - Just (i1, is) -> - let amt = max i1 0 in - case uncons (view accessor state{_int = is}) of - Just (origx1, _) -> - conjoin (map (origx1 ===) (take amt (view accessor $ instruction state))) .&&. - length (view accessor $ instruction state) === (length (view accessor state{_int = is}) + amt - 1) - _ -> state === instruction state - _ -> state === instruction state +-- -- How to test the int stack in particular? +-- dupTestN :: (Eq a, Show a) => Lens' State [a] -> (State -> State) -> State -> Property +-- dupTestN accessor instruction state = +-- case uncons (view int state) of +-- Just (i1, is) -> +-- let amt = max i1 0 in +-- case uncons (view accessor state{_int = is}) of +-- Just (origx1, _) -> +-- conjoin (map (origx1 ===) (take amt (view accessor $ instruction state))) .&&. +-- length (view accessor $ instruction state) === (length (view accessor state{_int = is}) + amt - 1) +-- _ -> state === instruction state +-- _ -> state === instruction state -swapTest :: (Show a, Eq a) => Lens' State [a] -> (State -> State) -> State -> Property -swapTest accessor instruction state = - case (uncons (view accessor state), uncons (view accessor $ instruction state)) of - (Just (origx1, origx2 : _), Just (modx1, modx2 : _)) -> origx1 === modx2 .&&. origx2 === modx1 - _ -> state === instruction state +-- swapTest :: (Show a, Eq a) => Lens' State [a] -> (State -> State) -> State -> Property +-- swapTest accessor instruction state = +-- case (uncons (view accessor state), uncons (view accessor $ instruction state)) of +-- (Just (origx1, origx2 : _), Just (modx1, modx2 : _)) -> origx1 === modx2 .&&. origx2 === modx1 +-- _ -> state === instruction state -rotTest :: (Show a, Eq a) => Lens' State [a] -> (State -> State) -> State -> Property -rotTest accessor instruction state = - case (uncons (view accessor state), uncons (view accessor $ instruction state)) of - (Just (origx1, origx2 : origx3 : _), Just (modx1, modx2 : modx3 : _)) -> (origx1, origx2, origx3) === (modx2, modx3, modx1) - _ -> state === instruction state +-- rotTest :: (Show a, Eq a) => Lens' State [a] -> (State -> State) -> State -> Property +-- rotTest accessor instruction state = +-- case (uncons (view accessor state), uncons (view accessor $ instruction state)) of +-- (Just (origx1, origx2 : origx3 : _), Just (modx1, modx2 : modx3 : _)) -> (origx1, origx2, origx3) === (modx2, modx3, modx1) +-- _ -> state === instruction state -flushTest :: (Show a, Eq a) => Lens' State [a] -> (State -> State) -> State -> Property -flushTest accessor instruction state = - property $ null $ view accessor $ instruction state +-- flushTest :: (Show a, Eq a) => Lens' State [a] -> (State -> State) -> State -> Property +-- flushTest accessor instruction state = +-- property $ null $ view accessor $ instruction state -stackDepthTest :: (Show a) => Lens' State [a] -> (State -> State) -> State -> Property -stackDepthTest accessor instruction state = - case uncons (view int $ instruction state) of - Just (x1, _) -> x1 === length (view accessor state) - _ -> state === instruction state +-- stackDepthTest :: (Show a) => Lens' State [a] -> (State -> State) -> State -> Property +-- stackDepthTest accessor instruction state = +-- case uncons (view int $ instruction state) of +-- Just (x1, _) -> x1 === length (view accessor state) +-- _ -> state === instruction state -yankTest :: forall a. (Show a, Eq a) => Lens' State [a] -> (State -> State) -> State -> Property -yankTest accessor instruction state@(State {_int = i1 : is}) = - let - myIndex :: Int - myIndex = max 0 (min i1 (length (view accessor state{_int = is}) - 1)) - item :: a - item = view accessor state{_int = is} !! myIndex - in - case (uncons (view accessor $ instruction state), uncons is) of - (Just (x1, _), Just (_, _)) -> x1 === item - _ -> state === instruction state - -- .&&. -- unsure how to get this functional - -- length (view accessor state{_int = is}) === length (view accessor $ instruction state) -yankTest _ instruction state = state === instruction state - --- Might just make this a unit test --- Come back to this later --- yankDupTest :: forall a. (Show a, Eq a) => Lens' State [a] -> (State -> State) -> State -> Property --- yankDupTest accessor instruction state@(State {_int = i1 : is}) = +-- yankTest :: forall a. (Show a, Eq a) => Lens' State [a] -> (State -> State) -> State -> Property +-- yankTest accessor instruction state@(State {_int = i1 : is}) = -- let -- myIndex :: Int -- myIndex = max 0 (min i1 (length (view accessor state{_int = is}) - 1)) @@ -122,8 +105,25 @@ yankTest _ instruction state = state === instruction state -- item = view accessor state{_int = is} !! myIndex -- in -- case (uncons (view accessor $ instruction state), uncons is) of --- (Just (x1, xs), Just (_, _)) -> x1 === item .&&. (x1 : xs) !! === item +-- (Just (x1, _), Just (_, _)) -> x1 === item -- _ -> state === instruction state --- yankDupTest _ instruction state = state === instruction state +-- -- .&&. -- unsure how to get this functional +-- -- length (view accessor state{_int = is}) === length (view accessor $ instruction state) +-- yankTest _ instruction state = state === instruction state --- shoveTest +-- -- Might just make this a unit test +-- -- Come back to this later +-- -- yankDupTest :: forall a. (Show a, Eq a) => Lens' State [a] -> (State -> State) -> State -> Property +-- -- yankDupTest accessor instruction state@(State {_int = i1 : is}) = +-- -- let +-- -- myIndex :: Int +-- -- myIndex = max 0 (min i1 (length (view accessor state{_int = is}) - 1)) +-- -- item :: a +-- -- item = view accessor state{_int = is} !! myIndex +-- -- in +-- -- case (uncons (view accessor $ instruction state), uncons is) of +-- -- (Just (x1, xs), Just (_, _)) -> x1 === item .&&. (x1 : xs) !! === item +-- -- _ -> state === instruction state +-- -- yankDupTest _ instruction state = state === instruction state + +-- -- shoveTest diff --git a/src/HushGP/PushTests/IntTests.hs b/src/HushGP/PushTests/IntTests.hs index caca9ee..605911b 100644 --- a/src/HushGP/PushTests/IntTests.hs +++ b/src/HushGP/PushTests/IntTests.hs @@ -1,84 +1,84 @@ module HushGP.PushTests.IntTests where -import HushGP.State -import HushGP.Instructions.IntInstructions -import HushGP.PushTests.GenericTests --- import Control.Lens hiding (uncons) -import Test.QuickCheck +-- import HushGP.State +-- import HushGP.Instructions.IntInstructions +-- import HushGP.PushTests.GenericTests +-- -- import Control.Lens hiding (uncons) +-- import Test.QuickCheck -prop_IntAdd :: State -> Property -prop_IntAdd = aaa1Test int instructionIntAdd (+) +-- prop_IntAdd :: State -> Property +-- prop_IntAdd = aaa1Test int instructionIntAdd (+) -prop_IntSub :: State -> Property -prop_IntSub = aaa1Test int instructionIntSub (-) +-- prop_IntSub :: State -> Property +-- prop_IntSub = aaa1Test int instructionIntSub (-) -prop_IntMul :: State -> Property -prop_IntMul = aaa1Test int instructionIntMul (*) +-- prop_IntMul :: State -> Property +-- prop_IntMul = aaa1Test int instructionIntMul (*) -prop_IntDiv :: State -> Property -prop_IntDiv state@(State {_int = 0 : _}) = state === instructionIntDiv state -prop_IntDiv state = aaa1Test int instructionIntDiv div state +-- prop_IntDiv :: State -> Property +-- prop_IntDiv state@(State {_int = 0 : _}) = state === instructionIntDiv state +-- prop_IntDiv state = aaa1Test int instructionIntDiv div state -prop_IntMod :: State -> Property -prop_IntMod state@(State {_int = 0 : _}) = state === instructionIntMod state -prop_IntMod state = aaa1Test int instructionIntMod mod state +-- prop_IntMod :: State -> Property +-- prop_IntMod state@(State {_int = 0 : _}) = state === instructionIntMod state +-- prop_IntMod state = aaa1Test int instructionIntMod mod state -prop_IntFromFloat :: State -> Property -prop_IntFromFloat = ab1Test float int instructionIntFromFloat floor +-- prop_IntFromFloat :: State -> Property +-- prop_IntFromFloat = ab1Test float int instructionIntFromFloat floor -prop_IntFromBool :: State -> Property -prop_IntFromBool = ab1Test bool int instructionIntFromBool (\x -> if x then 1 else 0) +-- prop_IntFromBool :: State -> Property +-- prop_IntFromBool = ab1Test bool int instructionIntFromBool (\x -> if x then 1 else 0) -prop_IntMin :: State -> Property -prop_IntMin = aaa1Test int instructionIntMin min +-- prop_IntMin :: State -> Property +-- prop_IntMin = aaa1Test int instructionIntMin min -prop_IntMax :: State -> Property -prop_IntMax = aaa1Test int instructionIntMax max +-- prop_IntMax :: State -> Property +-- prop_IntMax = aaa1Test int instructionIntMax max -prop_IntInc :: State -> Property -prop_IntInc = aa1Test int instructionIntInc (+1) +-- prop_IntInc :: State -> Property +-- prop_IntInc = aa1Test int instructionIntInc (+1) -prop_IntDec :: State -> Property -prop_IntDec = aa1Test int instructionIntDec (\x -> x - 1) +-- prop_IntDec :: State -> Property +-- prop_IntDec = aa1Test int instructionIntDec (\x -> x - 1) -prop_IntLT :: State -> Property -prop_IntLT = aab2Test int bool instructionIntLT (<) +-- prop_IntLT :: State -> Property +-- prop_IntLT = aab2Test int bool instructionIntLT (<) -prop_IntGT :: State -> Property -prop_IntGT = aab2Test int bool instructionIntGT (>) +-- prop_IntGT :: State -> Property +-- prop_IntGT = aab2Test int bool instructionIntGT (>) -prop_IntLTE :: State -> Property -prop_IntLTE = aab2Test int bool instructionIntLTE (<=) +-- prop_IntLTE :: State -> Property +-- prop_IntLTE = aab2Test int bool instructionIntLTE (<=) -prop_IntGTE :: State -> Property -prop_IntGTE = aab2Test int bool instructionIntGTE (>=) +-- prop_IntGTE :: State -> Property +-- prop_IntGTE = aab2Test int bool instructionIntGTE (>=) -prop_IntDup :: State -> Property -prop_IntDup = dupTest int instructionIntDup +-- prop_IntDup :: State -> Property +-- prop_IntDup = dupTest int instructionIntDup -prop_IntPop :: State -> Property -prop_IntPop = popTest int instructionIntPop +-- prop_IntPop :: State -> Property +-- prop_IntPop = popTest int instructionIntPop -prop_IntDupN :: State -> Property -prop_IntDupN = dupTestN int instructionIntDupN +-- prop_IntDupN :: State -> Property +-- prop_IntDupN = dupTestN int instructionIntDupN -prop_IntSwap :: State -> Property -prop_IntSwap = swapTest int instructionIntSwap +-- prop_IntSwap :: State -> Property +-- prop_IntSwap = swapTest int instructionIntSwap -prop_IntRot :: State -> Property -prop_IntRot = rotTest int instructionIntRot +-- prop_IntRot :: State -> Property +-- prop_IntRot = rotTest int instructionIntRot -prop_IntFlush :: State -> Property -prop_IntFlush = flushTest int instructionIntFlush +-- prop_IntFlush :: State -> Property +-- prop_IntFlush = flushTest int instructionIntFlush -prop_IntEq :: State -> Property -prop_IntEq = aab2Test int bool instructionIntEq (==) +-- prop_IntEq :: State -> Property +-- prop_IntEq = aab2Test int bool instructionIntEq (==) -prop_IntStackDepth :: State -> Property -prop_IntStackDepth = stackDepthTest int instructionIntStackDepth +-- prop_IntStackDepth :: State -> Property +-- prop_IntStackDepth = stackDepthTest int instructionIntStackDepth -prop_IntYank :: State -> Property -prop_IntYank = yankTest int instructionIntYank +-- prop_IntYank :: State -> Property +-- prop_IntYank = yankTest int instructionIntYank --- prop_IntYankDup :: State -> Property --- prop_IntYankDup = yankDupTest int instructionIntYankDup +-- -- prop_IntYankDup :: State -> Property +-- -- prop_IntYankDup = yankDupTest int instructionIntYankDup diff --git a/src/HushGP/PushTests/UtilTests.hs b/src/HushGP/PushTests/UtilTests.hs index 4422001..e0ca4e9 100644 --- a/src/HushGP/PushTests/UtilTests.hs +++ b/src/HushGP/PushTests/UtilTests.hs @@ -1,36 +1,36 @@ module HushGP.PushTests.UtilTests where -import HushGP.Instructions.Utility -import Test.QuickCheck +-- import HushGP.Instructions.Utility +-- import Test.QuickCheck -prop_DeleteAtTest :: Int -> [Int] -> Property -prop_DeleteAtTest idx lst = - idx >= 0 && idx < length lst ==> - if null lst - then length lst === length (deleteAt idx lst) - else length lst === length (deleteAt idx lst) + 1 +-- prop_DeleteAtTest :: Int -> [Int] -> Property +-- prop_DeleteAtTest idx lst = +-- idx >= 0 && idx < length lst ==> +-- if null lst +-- then length lst === length (deleteAt idx lst) +-- else length lst === length (deleteAt idx lst) + 1 -prop_CombineTupleTest :: Int -> ([Int], [Int]) -> Property -prop_CombineTupleTest val tup = - length (fst tup) + length (snd tup) === length (combineTuple val tup) - 1 +-- prop_CombineTupleTest :: Int -> ([Int], [Int]) -> Property +-- prop_CombineTupleTest val tup = +-- length (fst tup) + length (snd tup) === length (combineTuple val tup) - 1 -prop_CombineTupleListTest :: [Int] -> ([Int], [Int]) -> Property -prop_CombineTupleListTest lst tup = - length (fst tup) + length (snd tup) === length (combineTupleList lst tup) - length lst +-- prop_CombineTupleListTest :: [Int] -> ([Int], [Int]) -> Property +-- prop_CombineTupleListTest lst tup = +-- length (fst tup) + length (snd tup) === length (combineTupleList lst tup) - length lst --- Could use forAll to only generate valid tests -prop_InsertAt :: Int -> Int -> [Int] -> Property -prop_InsertAt idx val lst = - idx >= 0 && idx < length lst ==> - length lst === length (insertAt idx val lst) - 1 .&&. - insertAt idx val lst !! idx === val +-- -- Could use forAll to only generate valid tests +-- prop_InsertAt :: Int -> Int -> [Int] -> Property +-- prop_InsertAt idx val lst = +-- idx >= 0 && idx < length lst ==> +-- length lst === length (insertAt idx val lst) - 1 .&&. +-- insertAt idx val lst !! idx === val -prop_ReplaceAt :: Int -> Int -> [Int] -> Property -prop_ReplaceAt idx val lst = - idx >= 0 && idx < length lst ==> - length lst === length (replaceAt idx val lst) .&&. - replaceAt idx val lst !! idx === val +-- prop_ReplaceAt :: Int -> Int -> [Int] -> Property +-- prop_ReplaceAt idx val lst = +-- idx >= 0 && idx < length lst ==> +-- length lst === length (replaceAt idx val lst) .&&. +-- replaceAt idx val lst !! idx === val --- prop_SubList :: Int -> Int -> [Int] -> Property --- prop_SubList idx0 idx1 lst = - -- idx +-- -- prop_SubList :: Int -> Int -> [Int] -> Property +-- -- prop_SubList idx0 idx1 lst = +-- -- idx diff --git a/src/HushGP/State.hs b/src/HushGP/State.hs index 97f73ce..80dd2fa 100644 --- a/src/HushGP/State.hs +++ b/src/HushGP/State.hs @@ -1,33 +1,42 @@ -{-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE TemplateHaskell #-} module HushGP.State where import Control.Lens hiding (elements) import Data.Map qualified as Map -import GHC.Generics -import Test.QuickCheck +import System.Random --- | The exec stack must store heterogenous types, +-- |The exec stack must store heterogenous types, -- and we must be able to detect that type at runtime. -- One solution is for the exec stack to be a list of [Gene]. -- The parameter stack could be singular [Gene] or multiple [atomic] types. data Gene - = GeneInt Int - | GeneFloat Float + = GeneInt Integer + | GeneFloat Double | GeneBool Bool | GeneString String | GeneChar Char - | GeneVectorInt [Int] - | GeneVectorFloat [Float] + | GeneVectorInt [Integer] + | GeneVectorFloat [Double] | GeneVectorBool [Bool] | GeneVectorString [String] | GeneVectorChar [Char] - | StateFunc (State -> State, String) -- The string stores the name of the function + -- |State -> State is the function itself. String stores the name of the function. + | StateFunc (State -> State, String) | PlaceInput String | Close + | Open Int | Block [Gene] - deriving (Generic) + | GeneIntERC (Integer, StdGen) + | GeneFloatERC (Double, StdGen) + | GeneBoolERC (Bool, StdGen) + | GeneStringERC (String, StdGen) + | GeneCharERC (Char, StdGen) + | GeneVectorIntERC ([Integer], StdGen) + | GeneVectorFloatERC ([Double], StdGen) + | GeneVectorBoolERC ([Bool], StdGen) + | GeneVectorStringERC ([String], StdGen) + | GeneVectorCharERC ([Char], StdGen) instance Eq Gene where GeneInt x == GeneInt y = x == y @@ -42,10 +51,69 @@ instance Eq Gene where GeneVectorString xs == GeneVectorString ys = xs == ys GeneVectorChar xs == GeneVectorChar ys = xs == ys Close == Close = True + Open x == Open y = x == y StateFunc (_, nameX) == StateFunc (_, nameY) = nameX == nameY Block x == Block y = x == y + GeneIntERC (x, _) == GeneIntERC (y, _) = x == y + GeneFloatERC (x, _) == GeneFloatERC (y, _) = x == y + GeneBoolERC (x, _) == GeneBoolERC (y, _) = x == y + GeneStringERC (x, _) == GeneStringERC (y, _) = x == y + GeneCharERC (x, _) == GeneCharERC (y, _) = x == y + GeneVectorIntERC (x, _) == GeneVectorIntERC (y, _) = x == y + GeneVectorFloatERC (x, _) == GeneVectorFloatERC (y, _) = x == y + GeneVectorBoolERC (x, _) == GeneVectorBoolERC (y, _) = x == y + GeneVectorStringERC (x, _) == GeneVectorStringERC (y, _) = x == y + GeneVectorCharERC (x, _) == GeneVectorCharERC (y, _) = x == y + GeneIntERC (x, _) == GeneInt y = x == y + GeneFloatERC (x, _) == GeneFloat y = x == y + GeneBoolERC (x, _) == GeneBool y = x == y + GeneStringERC (x, _) == GeneString y = x == y + GeneCharERC (x, _) == GeneChar y = x == y + GeneVectorIntERC (x, _) == GeneVectorInt y = x == y + GeneVectorFloatERC (x, _) == GeneVectorFloat y = x == y + GeneVectorBoolERC (x, _) == GeneVectorBool y = x == y + GeneVectorStringERC (x, _) == GeneVectorString y = x == y + GeneVectorCharERC (x, _) == GeneVectorChar y = x == y _ == _ = False +instance Ord Gene where + GeneInt x <= GeneInt y = x <= y + GeneFloat x <= GeneFloat y = x <= y + GeneBool x <= GeneBool y = x <= y + GeneString x <= GeneString y = x <= y + GeneChar x <= GeneChar y = x <= y + PlaceInput x <= PlaceInput y = x <= y + GeneVectorInt xs <= GeneVectorInt ys = xs <= ys + GeneVectorFloat xs <= GeneVectorFloat ys = xs <= ys + GeneVectorBool xs <= GeneVectorBool ys = xs <= ys + GeneVectorString xs <= GeneVectorString ys = xs <= ys + GeneVectorChar xs <= GeneVectorChar ys = xs <= ys + Close <= Close = True + Open x <= Open y = x <= y + StateFunc (_, nameX) <= StateFunc (_, nameY) = nameX <= nameY + Block x <= Block y = x <= y + GeneIntERC (x, _) <= GeneIntERC (y, _) = x <= y + GeneFloatERC (x, _) <= GeneFloatERC (y, _) = x <= y + GeneBoolERC (x, _) <= GeneBoolERC (y, _) = x <= y + GeneStringERC (x, _) <= GeneStringERC (y, _) = x <= y + GeneCharERC (x, _) <= GeneCharERC (y, _) = x <= y + GeneVectorIntERC (x, _) <= GeneVectorIntERC (y, _) = x <= y + GeneVectorFloatERC (x, _) <= GeneVectorFloatERC (y, _) = x <= y + GeneVectorBoolERC (x, _) <= GeneVectorBoolERC (y, _) = x <= y + GeneVectorStringERC (x, _) <= GeneVectorStringERC (y, _) = x <= y + GeneVectorCharERC (x, _) <= GeneVectorCharERC (y, _) = x <= y + GeneIntERC (x, _) <= GeneInt y = x <= y + GeneFloatERC (x, _) <= GeneFloat y = x <= y + GeneBoolERC (x, _) <= GeneBool y = x <= y + GeneStringERC (x, _) <= GeneString y = x <= y + GeneCharERC (x, _) <= GeneChar y = x <= y + GeneVectorIntERC (x, _) <= GeneVectorInt y = x <= y + GeneVectorFloatERC (x, _) <= GeneVectorFloat y = x <= y + GeneVectorBoolERC (x, _) <= GeneVectorBool y = x <= y + GeneVectorStringERC (x, _) <= GeneVectorString y = x <= y + GeneVectorCharERC (x, _) <= GeneVectorChar y = x <= y + _ <= _ = False + instance Show Gene where show (GeneInt x) = "Int: " <> show x show (GeneFloat x) = "Float: " <> show x @@ -60,69 +128,37 @@ instance Show Gene where show (GeneVectorString xs) = "String Vec: " <> show xs show (GeneVectorChar xs) = "Char Vec: " <> show xs show Close = "Close" + show (Open x) = "Open: " <> show x show (Block xs) = "Block: " <> show xs - -instance CoArbitrary Gene - -instance Arbitrary Gene where - arbitrary = - oneof - [ GeneInt <$> arbitrary, - GeneFloat <$> arbitrary, - GeneBool <$> arbitrary, - GeneString <$> arbitrary, - GeneChar <$> arbitrary, - StateFunc <$> arbitrary, - PlaceInput <$> arbitrary, - GeneVectorInt <$> arbitrary, - GeneVectorFloat <$> arbitrary, - GeneVectorBool <$> arbitrary, - GeneVectorString <$> arbitrary, - GeneVectorChar <$> arbitrary, - Block <$> arbitrary, - return Close - ] + show (GeneIntERC x) = "Int ERC: " <> show x + show (GeneFloatERC x) = "Float ERC: " <> show x + show (GeneBoolERC x) = "Bool ERC: " <> show x + show (GeneStringERC x) = "String ERC: " <> show x + show (GeneCharERC x) = "Char ERC: " <> show x + show (GeneVectorIntERC x) = "Int Vec ERC: " <> show x + show (GeneVectorFloatERC x) = "Float Vec ERC: " <> show x + show (GeneVectorBoolERC x) = "Bool Vec ERC: " <> show x + show (GeneVectorStringERC x) = "String Vec ERC: " <> show x + show (GeneVectorCharERC x) = "Char Vec ERC: " <> show x -- | The structure that holds all of the values. data State = State { _exec :: [Gene], _code :: [Gene], - _int :: [Int], - _float :: [Float], + _int :: [Integer], + _float :: [Double], _bool :: [Bool], _string :: [String], _char :: [Char], - _vectorInt :: [[Int]], - _vectorFloat :: [[Float]], + _vectorInt :: [[Integer]], + _vectorFloat :: [[Double]], _vectorBool :: [[Bool]], _vectorString :: [[String]], _vectorChar :: [[Char]], _parameter :: [Gene], _input :: Map.Map String Gene } - deriving (Show, Eq, Generic) - -instance Arbitrary State where - arbitrary = do - arbExec <- arbitrary - arbCode <- arbitrary - arbInt <- arbitrary - arbFloat <- arbitrary - arbBool <- arbitrary - arbString <- arbitrary - arbChar <- arbitrary - arbVectorInt <- arbitrary - arbVectorFloat <- arbitrary - arbVectorBool <- arbitrary - arbVectorString <- arbitrary - arbVectorChar <- arbitrary - arbParameter <- arbitrary - -- arbInput <- arbitrary - State arbExec arbCode arbInt arbFloat arbBool arbString arbChar arbVectorInt arbVectorFloat arbVectorBool arbVectorString arbVectorChar arbParameter <$> arbitrary - --- Thanks hlint lol - -instance CoArbitrary State + deriving (Show, Eq, Ord) emptyState :: State emptyState = @@ -148,8 +184,8 @@ exampleState = State { _exec = [], _code = [], - _int = [32, 56], - _float = [3.23, 9.235], + _int = [32, 56, 88, 91], + _float = [3.23, 9.235, 5.3211, 8.0], _bool = [True, False], _string = ["abc", "123"], _char = ['d', 'e', 'f'], diff --git a/src/HushGP/Utility.hs b/src/HushGP/Utility.hs new file mode 100644 index 0000000..3f2b525 --- /dev/null +++ b/src/HushGP/Utility.hs @@ -0,0 +1,15 @@ +module HushGP.Utility where + +import HushGP.State +import System.Random +import Control.Monad + +-- |Generates a single random instruction from a list of instructions. +randomInstruction :: [Gene] -> IO Gene +randomInstruction instructions = do + impureGen <- initStdGen + return $ instructions !! fst (uniformR (0, length instructions - 1) impureGen) + +-- |Generates a list of random instructions from a list of instructions passed in. +randomInstructions :: Int -> [Gene] -> IO [Gene] +randomInstructions amt instructions = replicateM amt (randomInstruction instructions) From ed960acef3918bb2c9700cca3255efb3532ab96c Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Thu, 13 Feb 2025 23:34:00 -0600 Subject: [PATCH 161/171] formatting --- src/HushGP/Genome.hs | 46 ++++++++++++++++++++--------------------- src/HushGP/PushTests.hs | 12 +++++------ src/HushGP/State.hs | 12 +++++------ src/HushGP/Utility.hs | 6 +++--- 4 files changed, 38 insertions(+), 38 deletions(-) diff --git a/src/HushGP/Genome.hs b/src/HushGP/Genome.hs index e0442d6..7fb90e8 100644 --- a/src/HushGP/Genome.hs +++ b/src/HushGP/Genome.hs @@ -1,34 +1,34 @@ module HushGP.Genome where -import HushGP.State -import HushGP.Utility -import Data.Map qualified as Map import Data.List import Data.List.Split +import Data.Map qualified as Map import HushGP.Instructions.Opens +import HushGP.State +import HushGP.Utility --- |Makes a random plushy from variables in a passed argMap and --- a passed list of instructions. +-- | Makes a random plushy from variables in a passed argMap and +-- a passed list of instructions. makeRandomPlushy :: Map.Map String String -> [Gene] -> IO [Gene] makeRandomPlushy argMap = randomInstructions (read @Int (argMap Map.! "maxInitialPlushySize")) --- |A utility function to generate an amount based on an int rather than --- from an argmap. +-- | A utility function to generate an amount based on an int rather than +-- from an argmap. makeRandomPlushy' :: Int -> [Gene] -> IO [Gene] makeRandomPlushy' = randomInstructions --- |Checks to see if a Gene is an (Open _) constructor. +-- | Checks to see if a Gene is an (Open _) constructor. isOpen :: Gene -> Bool isOpen (Open _) = True isOpen _ = False --- |Decrements the count of an (Open _) constructor. Acts as id --- if the gene isn't an open. +-- | Decrements the count of an (Open _) constructor. Acts as id +-- if the gene isn't an open. decOpen :: Gene -> Gene decOpen (Open n) = Open (n - 1) decOpen gene = gene --- |Checks to see if the a list of genes with a single element is an opener. +-- | Checks to see if the a list of genes with a single element is an opener. isOpenerList :: [Gene] -> Bool isOpenerList [instruction] = case Map.lookup instruction instructionOpens of @@ -36,7 +36,7 @@ isOpenerList [instruction] = _ -> False isOpenerList _ = False --- |Gets the amount of blocks to open from a list of genes with a single element. +-- | Gets the amount of blocks to open from a list of genes with a single element. getOpenAmountList :: [Gene] -> Int getOpenAmountList [instruction] = case Map.lookup instruction instructionOpens of @@ -44,25 +44,25 @@ getOpenAmountList [instruction] = _ -> 0 getOpenAmountList _ = 0 --- |Converts a plushy genome into a push genome. +-- | Converts a plushy genome into a push genome. plushyToPush :: [Gene] -> [Gene] plushyToPush plushy = plushyToPush' (concatMap (\x -> if isOpenerList x then x <> [Open (getOpenAmountList x)] else x) (chunksOf 1 plushy)) [] --- |Internal function used to convert a plushy genome with opens in it into a push genome. +-- | Internal function used to convert a plushy genome with opens in it into a push genome. plushyToPush' :: [Gene] -> [Gene] -> [Gene] plushyToPush' openPlushy push = if null openPlushy - then - if any isOpen push - then plushyToPush' [Close] push - else push - else - if firstPlushy == Close then if any isOpen push - then plushyToPush' (drop 1 openPlushy) (if numOpen (push !! openIndex) == 1 then preOpen <> postOpen else preOpen <> postOpen <> [decOpen (Open (numOpen (push !! openIndex)))]) - else plushyToPush' (drop 1 openPlushy) push - else plushyToPush' (drop 1 openPlushy) (push <> [firstPlushy]) + then plushyToPush' [Close] push + else push + else + if firstPlushy == Close + then + if any isOpen push + then plushyToPush' (drop 1 openPlushy) (if numOpen (push !! openIndex) == 1 then preOpen <> postOpen else preOpen <> postOpen <> [decOpen (Open (numOpen (push !! openIndex)))]) + else plushyToPush' (drop 1 openPlushy) push + else plushyToPush' (drop 1 openPlushy) (push <> [firstPlushy]) where firstPlushy :: Gene firstPlushy = diff --git a/src/HushGP/PushTests.hs b/src/HushGP/PushTests.hs index 72344ab..127f41c 100644 --- a/src/HushGP/PushTests.hs +++ b/src/HushGP/PushTests.hs @@ -1,9 +1,9 @@ -module HushGP.PushTests - -- ( module HushGP.PushTests.GenericTests, - -- module HushGP.PushTests.IntTests, - -- module HushGP.PushTests.UtilTests, - -- ) -where +module HushGP.PushTests where + +-- ( module HushGP.PushTests.GenericTests, +-- module HushGP.PushTests.IntTests, +-- module HushGP.PushTests.UtilTests, +-- ) -- import HushGP.PushTests.GenericTests -- import HushGP.PushTests.IntTests diff --git a/src/HushGP/State.hs b/src/HushGP/State.hs index 80dd2fa..7b9af01 100644 --- a/src/HushGP/State.hs +++ b/src/HushGP/State.hs @@ -6,10 +6,10 @@ import Control.Lens hiding (elements) import Data.Map qualified as Map import System.Random --- |The exec stack must store heterogenous types, --- and we must be able to detect that type at runtime. --- One solution is for the exec stack to be a list of [Gene]. --- The parameter stack could be singular [Gene] or multiple [atomic] types. +-- | The exec stack must store heterogenous types, +-- and we must be able to detect that type at runtime. +-- One solution is for the exec stack to be a list of [Gene]. +-- The parameter stack could be singular [Gene] or multiple [atomic] types. data Gene = GeneInt Integer | GeneFloat Double @@ -21,8 +21,8 @@ data Gene | GeneVectorBool [Bool] | GeneVectorString [String] | GeneVectorChar [Char] - -- |State -> State is the function itself. String stores the name of the function. - | StateFunc (State -> State, String) + | -- | State -> State is the function itself. String stores the name of the function. + StateFunc (State -> State, String) | PlaceInput String | Close | Open Int diff --git a/src/HushGP/Utility.hs b/src/HushGP/Utility.hs index 3f2b525..384710c 100644 --- a/src/HushGP/Utility.hs +++ b/src/HushGP/Utility.hs @@ -1,15 +1,15 @@ module HushGP.Utility where +import Control.Monad import HushGP.State import System.Random -import Control.Monad --- |Generates a single random instruction from a list of instructions. +-- | Generates a single random instruction from a list of instructions. randomInstruction :: [Gene] -> IO Gene randomInstruction instructions = do impureGen <- initStdGen return $ instructions !! fst (uniformR (0, length instructions - 1) impureGen) --- |Generates a list of random instructions from a list of instructions passed in. +-- | Generates a list of random instructions from a list of instructions passed in. randomInstructions :: Int -> [Gene] -> IO [Gene] randomInstructions amt instructions = replicateM amt (randomInstruction instructions) From 46fe4fac0f58f78fc704fd4fc867c3dcafc242b6 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Fri, 14 Feb 2025 00:46:48 -0600 Subject: [PATCH 162/171] plushy -> push done --- TODO.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/TODO.md b/TODO.md index 52e81aa..2e8b98d 100644 --- a/TODO.md +++ b/TODO.md @@ -20,14 +20,14 @@ - [ ] Use template haskell to (mostly) generate functions from generic ones (Split files based on the arity of their functions) ## PushGP TODO -- [ ] Implement a Plushy genome translator - - [ ] Need to make this reproducable too (Check pysh json files) +- [X] Implement a Plushy genome translator - [ ] Implement silent and skip markers as well - - [ ] Have close amt of 1,2, and 3 + ~~[ ] Have close amt of 1,2, and 3~~ - [X] Need a random genome generator - I'm only going to implement propeller's :specified version - Is the best according to the papers - [ ] Need a NoOp that opens blocks +- [ ] Need to make genomes serializable (Check pysh json files) - [ ] Add Memory - [ ] Add history stack(s), like a call stack - [ ] Implement interpreter options (could probably just place this into a map) From eab4932d540c49da1a8edad4bdb37e10e87f154c Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Fri, 14 Feb 2025 00:47:04 -0600 Subject: [PATCH 163/171] I love plushy genomes --- src/HushGP/Genome.hs | 58 +++++++++++-------- src/HushGP/Instructions.hs | 8 ++- .../Instructions/GenericInstructions.hs | 8 +++ src/HushGP/Instructions/Opens.hs | 4 +- 4 files changed, 52 insertions(+), 26 deletions(-) diff --git a/src/HushGP/Genome.hs b/src/HushGP/Genome.hs index 7fb90e8..f9a027a 100644 --- a/src/HushGP/Genome.hs +++ b/src/HushGP/Genome.hs @@ -6,6 +6,21 @@ import Data.Map qualified as Map import HushGP.Instructions.Opens import HushGP.State import HushGP.Utility +import HushGP.Instructions + +tempPlushy :: [Gene] +tempPlushy = [ + StateFunc (instructionIntDiv, "instructionIntDiv"), + StateFunc (instructionExecDup, "instructionExecDup"), + GeneInt 1, + GeneInt 0, + StateFunc (instructionIntDiv, "instructionIntDiv"), + GeneInt (-15), + StateFunc (instructionIntSub, "instructionIntSub"), + StateFunc (instructionNoOpBlock, "instructionNoOpBlock"), + Close, + Close + ] -- | Makes a random plushy from variables in a passed argMap and -- a passed list of instructions. @@ -50,31 +65,26 @@ plushyToPush plushy = plushyToPush' (concatMap (\x -> if isOpenerList x then x < -- | Internal function used to convert a plushy genome with opens in it into a push genome. plushyToPush' :: [Gene] -> [Gene] -> [Gene] -plushyToPush' openPlushy push = - if null openPlushy - then - if any isOpen push +plushyToPush' openPlushy push + | null openPlushy = if any isOpen push then plushyToPush' [Close] push else push - else - if firstPlushy == Close - then - if any isOpen push - then plushyToPush' (drop 1 openPlushy) (if numOpen (push !! openIndex) == 1 then preOpen <> postOpen else preOpen <> postOpen <> [decOpen (Open (numOpen (push !! openIndex)))]) + | firstPlushy == Close = if any isOpen push + then plushyToPush' (drop 1 openPlushy) (if numOpen (push !! openIndex) == 1 then preOpen <> [Block postOpen] else preOpen <> [Block (postOpen <> [decOpen (Open (numOpen (push !! openIndex)))])]) else plushyToPush' (drop 1 openPlushy) push - else plushyToPush' (drop 1 openPlushy) (push <> [firstPlushy]) + | otherwise = plushyToPush' (drop 1 openPlushy) (push <> [firstPlushy]) where - firstPlushy :: Gene - firstPlushy = - case uncons openPlushy of - Just (g, _) -> g - _ -> error "This shouldn't happen" - postOpen :: [Gene] - postOpen = reverse (takeWhile (not . isOpen) (reverse push)) - openIndex :: Int - openIndex = length push - length postOpen - 1 - numOpen :: Gene -> Int - numOpen (Open n) = n - numOpen _ = 0 - preOpen :: [Gene] - preOpen = take openIndex push + firstPlushy :: Gene + firstPlushy + = case uncons openPlushy of + Just (g, _) -> g + _ -> error "This shouldn't happen" + postOpen :: [Gene] + postOpen = reverse (takeWhile (not . isOpen) (reverse push)) + openIndex :: Int + openIndex = length push - length postOpen - 1 + numOpen :: Gene -> Int + numOpen (Open n) = n + numOpen _ = 0 + preOpen :: [Gene] + preOpen = take openIndex push diff --git a/src/HushGP/Instructions.hs b/src/HushGP/Instructions.hs index 6a8859d..ab701c7 100644 --- a/src/HushGP/Instructions.hs +++ b/src/HushGP/Instructions.hs @@ -31,10 +31,16 @@ import HushGP.Instructions.VectorIntInstructions import HushGP.Instructions.VectorStringInstructions import HushGP.State +noOpStateFunc :: Gene +noOpStateFunc = StateFunc (instructionNoOp, "instructionNoOp") + +noOpStateFuncBlock :: Gene +noOpStateFuncBlock = StateFunc (instructionNoOpBlock, "instructionNoOpBlock") + -- | All of the instructions declared in all the instruction submodules allInstructions :: [Gene] allInstructions = - allIntInstructions + noOpStateFunc : noOpStateFuncBlock : allIntInstructions <> allFloatInstructions <> allBoolInstructions <> allCharInstructions diff --git a/src/HushGP/Instructions/GenericInstructions.hs b/src/HushGP/Instructions/GenericInstructions.hs index 5a557a5..4d43fa9 100644 --- a/src/HushGP/Instructions/GenericInstructions.hs +++ b/src/HushGP/Instructions/GenericInstructions.hs @@ -9,6 +9,14 @@ import Data.List.Split -- import Debug.Trace +-- |Does No Operation. Useful for genome stuff :) +instructionNoOpBlock :: State -> State +instructionNoOpBlock state = state + +-- |Does No Operation. Just evolve fodder. +instructionNoOp :: State -> State +instructionNoOp state = state + -- |Duplicates the top of a stack based on a lens. instructionDup :: Lens' State [a] -> State -> State instructionDup accessor state = diff --git a/src/HushGP/Instructions/Opens.hs b/src/HushGP/Instructions/Opens.hs index d4334d5..ab8c837 100644 --- a/src/HushGP/Instructions/Opens.hs +++ b/src/HushGP/Instructions/Opens.hs @@ -2,6 +2,7 @@ module HushGP.Instructions.Opens where import HushGP.State import Data.Map qualified as Map +import HushGP.Instructions.GenericInstructions import HushGP.Instructions.ExecInstructions import HushGP.Instructions.StringInstructions import HushGP.Instructions.VectorIntInstructions @@ -36,5 +37,6 @@ instructionOpens = Map.fromList [ (StateFunc (instructionVectorFloatIterate, "instructionVectorFloatIterate"), 1), (StateFunc (instructionVectorStringIterate, "instructionVectorStringIterate"), 1), (StateFunc (instructionVectorBoolIterate, "instructionVectorBoolIterate"), 1), - (StateFunc (instructionVectorCharIterate, "instructionVectorCharIterate"), 1) + (StateFunc (instructionVectorCharIterate, "instructionVectorCharIterate"), 1), + (StateFunc (instructionNoOpBlock, "instructionNoOpBlock"), 1) ] From 2399b7660b82ca4b90f5839f709f2245caf45502 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Fri, 14 Feb 2025 01:11:37 -0600 Subject: [PATCH 164/171] this stuff is broken rn. It's bed time --- src/HushGP/Genome.hs | 9 ++++++++- src/HushGP/Instructions/Opens.hs | 2 +- src/HushGP/Push.hs | 1 + src/HushGP/State.hs | 2 ++ 4 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/HushGP/Genome.hs b/src/HushGP/Genome.hs index f9a027a..a791c6a 100644 --- a/src/HushGP/Genome.hs +++ b/src/HushGP/Genome.hs @@ -7,6 +7,7 @@ import HushGP.Instructions.Opens import HushGP.State import HushGP.Utility import HushGP.Instructions +import Debug.Trace tempPlushy :: [Gene] tempPlushy = [ @@ -15,9 +16,11 @@ tempPlushy = [ GeneInt 1, GeneInt 0, StateFunc (instructionIntDiv, "instructionIntDiv"), + Skip, GeneInt (-15), StateFunc (instructionIntSub, "instructionIntSub"), - StateFunc (instructionNoOpBlock, "instructionNoOpBlock"), + -- StateFunc (instructionNoOpBlock, "instructionNoOpBlock"), + StateFunc (instructionExecIf, "instructionExecIf"), Close, Close ] @@ -72,6 +75,10 @@ plushyToPush' openPlushy push | firstPlushy == Close = if any isOpen push then plushyToPush' (drop 1 openPlushy) (if numOpen (push !! openIndex) == 1 then preOpen <> [Block postOpen] else preOpen <> [Block (postOpen <> [decOpen (Open (numOpen (push !! openIndex)))])]) else plushyToPush' (drop 1 openPlushy) push + | firstPlushy == Skip = + case uncons openPlushy of + Just (_, _ : xs) -> plushyToPush' xs push + _ -> plushyToPush' (drop 1 openPlushy) push | otherwise = plushyToPush' (drop 1 openPlushy) (push <> [firstPlushy]) where firstPlushy :: Gene diff --git a/src/HushGP/Instructions/Opens.hs b/src/HushGP/Instructions/Opens.hs index ab8c837..7ac841d 100644 --- a/src/HushGP/Instructions/Opens.hs +++ b/src/HushGP/Instructions/Opens.hs @@ -15,7 +15,7 @@ import HushGP.Instructions.VectorCharInstructions -- To be used in plushy conversion. instructionOpens :: Map.Map Gene Int instructionOpens = Map.fromList [ - (StateFunc (instructionExecIf, "instructionsExecIf"), 2), + (StateFunc (instructionExecIf, "instructionExecIf"), 2), (StateFunc (instructionExecDup, "instructionExecDup"), 1), (StateFunc (instructionExecDupN, "instructionExecDupN"), 1), (StateFunc (instructionExecPop, "instructionExecPop"), 1), diff --git a/src/HushGP/Push.hs b/src/HushGP/Push.hs index a676ca0..d2a42a6 100644 --- a/src/HushGP/Push.hs +++ b/src/HushGP/Push.hs @@ -74,4 +74,5 @@ interpretExec state@(State {_exec = e : es}) = (GeneVectorCharERC (val, _)) -> interpretExec (state & exec .~ es & vectorChar .~ val : view vectorChar state) Close -> undefined -- This should never happen. Will be converted to Blocks in the Plushy -> Exec stack process (Open _) -> undefined -- This should also never happen. Should be converted in Plushy -> Exec stack process + Skip -> undefined -- This should double also never happen. interpretExec state = state diff --git a/src/HushGP/State.hs b/src/HushGP/State.hs index 7b9af01..39c6e9d 100644 --- a/src/HushGP/State.hs +++ b/src/HushGP/State.hs @@ -26,6 +26,7 @@ data Gene | PlaceInput String | Close | Open Int + | Skip | Block [Gene] | GeneIntERC (Integer, StdGen) | GeneFloatERC (Double, StdGen) @@ -129,6 +130,7 @@ instance Show Gene where show (GeneVectorChar xs) = "Char Vec: " <> show xs show Close = "Close" show (Open x) = "Open: " <> show x + show Skip = "Skip" show (Block xs) = "Block: " <> show xs show (GeneIntERC x) = "Int ERC: " <> show x show (GeneFloatERC x) = "Float ERC: " <> show x From b7926662e19cee7368d77ff97877d92aae987bad Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Fri, 14 Feb 2025 01:20:54 -0600 Subject: [PATCH 165/171] fix skip not working --- src/HushGP/Genome.hs | 8 ++++---- src/HushGP/State.hs | 2 ++ 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/HushGP/Genome.hs b/src/HushGP/Genome.hs index a791c6a..ff3dec5 100644 --- a/src/HushGP/Genome.hs +++ b/src/HushGP/Genome.hs @@ -69,17 +69,17 @@ plushyToPush plushy = plushyToPush' (concatMap (\x -> if isOpenerList x then x < -- | Internal function used to convert a plushy genome with opens in it into a push genome. plushyToPush' :: [Gene] -> [Gene] -> [Gene] plushyToPush' openPlushy push - | null openPlushy = if any isOpen push + | null openPlushy = trace "null" $ trace ("plushy: " <> show openPlushy) $ trace ("push: " <> show push) $ trace "--------------------" $ if any isOpen push then plushyToPush' [Close] push else push - | firstPlushy == Close = if any isOpen push + | firstPlushy == Close = trace "Close" $ trace ("plushy: " <> show openPlushy) $ trace ("push: " <> show push) $ trace "--------------------" $ if any isOpen push then plushyToPush' (drop 1 openPlushy) (if numOpen (push !! openIndex) == 1 then preOpen <> [Block postOpen] else preOpen <> [Block (postOpen <> [decOpen (Open (numOpen (push !! openIndex)))])]) else plushyToPush' (drop 1 openPlushy) push - | firstPlushy == Skip = + | firstPlushy == Skip = trace "Skip" $ trace ("plushy: " <> show openPlushy) $ trace ("push: " <> show push) $ trace "--------------------" $ case uncons openPlushy of Just (_, _ : xs) -> plushyToPush' xs push _ -> plushyToPush' (drop 1 openPlushy) push - | otherwise = plushyToPush' (drop 1 openPlushy) (push <> [firstPlushy]) + | otherwise = trace "otherwise" $ trace ("plushy: " <> show openPlushy) $ trace ("push: " <> show push) $ trace "--------------------" $ plushyToPush' (drop 1 openPlushy) (push <> [firstPlushy]) where firstPlushy :: Gene firstPlushy diff --git a/src/HushGP/State.hs b/src/HushGP/State.hs index 39c6e9d..5dad5a7 100644 --- a/src/HushGP/State.hs +++ b/src/HushGP/State.hs @@ -53,6 +53,7 @@ instance Eq Gene where GeneVectorChar xs == GeneVectorChar ys = xs == ys Close == Close = True Open x == Open y = x == y + Skip == Skip = True StateFunc (_, nameX) == StateFunc (_, nameY) = nameX == nameY Block x == Block y = x == y GeneIntERC (x, _) == GeneIntERC (y, _) = x == y @@ -91,6 +92,7 @@ instance Ord Gene where GeneVectorChar xs <= GeneVectorChar ys = xs <= ys Close <= Close = True Open x <= Open y = x <= y + Skip <= Skip = True StateFunc (_, nameX) <= StateFunc (_, nameY) = nameX <= nameY Block x <= Block y = x <= y GeneIntERC (x, _) <= GeneIntERC (y, _) = x <= y From 153f4264e25eaa2d576dfeb08549d14cb1807937 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Fri, 14 Feb 2025 16:32:23 -0600 Subject: [PATCH 166/171] fix block issue, gonna run a few more tests --- src/HushGP/Genome.hs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/HushGP/Genome.hs b/src/HushGP/Genome.hs index ff3dec5..20a0382 100644 --- a/src/HushGP/Genome.hs +++ b/src/HushGP/Genome.hs @@ -66,14 +66,14 @@ getOpenAmountList _ = 0 plushyToPush :: [Gene] -> [Gene] plushyToPush plushy = plushyToPush' (concatMap (\x -> if isOpenerList x then x <> [Open (getOpenAmountList x)] else x) (chunksOf 1 plushy)) [] --- | Internal function used to convert a plushy genome with opens in it into a push genome. +-- | Internal function used to convert a plushy genome with opens in it into its push phenotype. plushyToPush' :: [Gene] -> [Gene] -> [Gene] plushyToPush' openPlushy push | null openPlushy = trace "null" $ trace ("plushy: " <> show openPlushy) $ trace ("push: " <> show push) $ trace "--------------------" $ if any isOpen push then plushyToPush' [Close] push else push - | firstPlushy == Close = trace "Close" $ trace ("plushy: " <> show openPlushy) $ trace ("push: " <> show push) $ trace "--------------------" $ if any isOpen push - then plushyToPush' (drop 1 openPlushy) (if numOpen (push !! openIndex) == 1 then preOpen <> [Block postOpen] else preOpen <> [Block (postOpen <> [decOpen (Open (numOpen (push !! openIndex)))])]) + | firstPlushy == Close = trace "Close" $ trace ("plushy: " <> show openPlushy) $ trace ("push: " <> show push) $ trace ("openIndex: " <> show openIndex) $ trace ("preOpen: " <> show preOpen) $ trace ("postOpen: " <> show postOpen) $ trace "--------------------" $ if any isOpen push + then plushyToPush' (drop 1 openPlushy) (if numOpen (push !! openIndex) == 1 then preOpen <> [Block postOpen] else preOpen <> [Block postOpen] <> [decOpen (Open (numOpen (push !! openIndex)))]) else plushyToPush' (drop 1 openPlushy) push | firstPlushy == Skip = trace "Skip" $ trace ("plushy: " <> show openPlushy) $ trace ("push: " <> show push) $ trace "--------------------" $ case uncons openPlushy of From 054d321102d585be978b00ad6e3e4a92b96ce0bb Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Fri, 14 Feb 2025 17:00:13 -0600 Subject: [PATCH 167/171] comment out/remove debugging code --- src/HushGP/Genome.hs | 46 ++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/src/HushGP/Genome.hs b/src/HushGP/Genome.hs index 20a0382..7618840 100644 --- a/src/HushGP/Genome.hs +++ b/src/HushGP/Genome.hs @@ -6,24 +6,28 @@ import Data.Map qualified as Map import HushGP.Instructions.Opens import HushGP.State import HushGP.Utility -import HushGP.Instructions -import Debug.Trace +-- import HushGP.Instructions +-- import Debug.Trace -tempPlushy :: [Gene] -tempPlushy = [ - StateFunc (instructionIntDiv, "instructionIntDiv"), - StateFunc (instructionExecDup, "instructionExecDup"), - GeneInt 1, - GeneInt 0, - StateFunc (instructionIntDiv, "instructionIntDiv"), - Skip, - GeneInt (-15), - StateFunc (instructionIntSub, "instructionIntSub"), - -- StateFunc (instructionNoOpBlock, "instructionNoOpBlock"), - StateFunc (instructionExecIf, "instructionExecIf"), - Close, - Close - ] +-- tempPlushy :: [Gene] +-- tempPlushy = [ +-- StateFunc (instructionIntDiv, "instructionIntDiv"), +-- StateFunc (instructionExecDup, "instructionExecDup"), +-- GeneInt 1, +-- GeneInt 0, +-- StateFunc (instructionIntDiv, "instructionIntDiv"), +-- Skip, +-- GeneInt (-15), +-- StateFunc (instructionExecDup, "instructionExecDup"), +-- StateFunc (instructionIntSub, "instructionIntSub"), +-- StateFunc (instructionFloatMul, "instructionFloatMul"), +-- Skip, +-- Close, +-- -- StateFunc (instructionNoOpBlock, "instructionNoOpBlock"), +-- StateFunc (instructionExecIf, "instructionExecIf"), +-- Close, +-- Close +-- ] -- | Makes a random plushy from variables in a passed argMap and -- a passed list of instructions. @@ -69,17 +73,17 @@ plushyToPush plushy = plushyToPush' (concatMap (\x -> if isOpenerList x then x < -- | Internal function used to convert a plushy genome with opens in it into its push phenotype. plushyToPush' :: [Gene] -> [Gene] -> [Gene] plushyToPush' openPlushy push - | null openPlushy = trace "null" $ trace ("plushy: " <> show openPlushy) $ trace ("push: " <> show push) $ trace "--------------------" $ if any isOpen push + | null openPlushy = if any isOpen push then plushyToPush' [Close] push else push - | firstPlushy == Close = trace "Close" $ trace ("plushy: " <> show openPlushy) $ trace ("push: " <> show push) $ trace ("openIndex: " <> show openIndex) $ trace ("preOpen: " <> show preOpen) $ trace ("postOpen: " <> show postOpen) $ trace "--------------------" $ if any isOpen push + | firstPlushy == Close = if any isOpen push then plushyToPush' (drop 1 openPlushy) (if numOpen (push !! openIndex) == 1 then preOpen <> [Block postOpen] else preOpen <> [Block postOpen] <> [decOpen (Open (numOpen (push !! openIndex)))]) else plushyToPush' (drop 1 openPlushy) push - | firstPlushy == Skip = trace "Skip" $ trace ("plushy: " <> show openPlushy) $ trace ("push: " <> show push) $ trace "--------------------" $ + | firstPlushy == Skip = case uncons openPlushy of Just (_, _ : xs) -> plushyToPush' xs push _ -> plushyToPush' (drop 1 openPlushy) push - | otherwise = trace "otherwise" $ trace ("plushy: " <> show openPlushy) $ trace ("push: " <> show push) $ trace "--------------------" $ plushyToPush' (drop 1 openPlushy) (push <> [firstPlushy]) + | otherwise = plushyToPush' (drop 1 openPlushy) (push <> [firstPlushy]) where firstPlushy :: Gene firstPlushy From feddc3cbfeaa216472ee00a1c8b91ecd227ae35d Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Fri, 14 Feb 2025 20:02:38 -0600 Subject: [PATCH 168/171] add push arguments, more fun to come --- HushGP.cabal | 1 + TODO.md | 8 ++- src/HushGP/GP/PushArgs.hs | 131 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 138 insertions(+), 2 deletions(-) create mode 100644 src/HushGP/GP/PushArgs.hs diff --git a/HushGP.cabal b/HushGP.cabal index 8e18df2..8e34eab 100644 --- a/HushGP.cabal +++ b/HushGP.cabal @@ -58,6 +58,7 @@ library , HushGP.PushTests.IntTests , HushGP.PushTests.GenericTests , HushGP.PushTests.UtilTests + , HushGP.GP.PushArgs , HushGP.TH , HushGP.Utility , HushGP.Genome diff --git a/TODO.md b/TODO.md index 2e8b98d..fe3600d 100644 --- a/TODO.md +++ b/TODO.md @@ -21,15 +21,19 @@ ## PushGP TODO - [X] Implement a Plushy genome translator - - [ ] Implement silent and skip markers as well + - [X] Implement ~~silent and~~ skip marker(s) as well ~~[ ] Have close amt of 1,2, and 3~~ - [X] Need a random genome generator - I'm only going to implement propeller's :specified version - Is the best according to the papers - - [ ] Need a NoOp that opens blocks + - [X] Need a NoOp that opens blocks - [ ] Need to make genomes serializable (Check pysh json files) - [ ] Add Memory - [ ] Add history stack(s), like a call stack - [ ] Implement interpreter options (could probably just place this into a map) - Should probably place this in a separate file +- [ ] Implement different forms of downsampling +- [ ] Implement concurrent execution of creating random plushies and evaluating individuals - [X] Devise a good way to implement ERCs +- [ ] Implement random simplification of genomes + - [ ] Find a way to multi-thread this diff --git a/src/HushGP/GP/PushArgs.hs b/src/HushGP/GP/PushArgs.hs new file mode 100644 index 0000000..7388a6b --- /dev/null +++ b/src/HushGP/GP/PushArgs.hs @@ -0,0 +1,131 @@ +module HushGP.GP.PushArgs where + +import HushGP.State +import Data.Map qualified as Map +import HushGP.Instructions + +data PushArgs = PushArgs + { + -- | For alternation, std deviation fo index when alternating. + alignmentDeviation :: Int, + -- | For alternation, probability of switching parents at each location. + alternationRate :: Float, + -- | For bmx, rate genes are exchanged. + bmxExchangeRate :: Float, + -- | For bmx, max length of a gene. + bmxGeneLengthLimit :: Int, + -- | For bmx, mutation rate for gaps. + bmxGapChangeProbability :: Float, + -- | For bmx, whether mates selected using reverse case sequences of first parent + bmxIsComplementary :: Bool, + -- | For bmx, don't exchange distance if greater than this + bmxMaxDistance :: Int, + -- | For bmx, only allow exchanges between individual with same number of genes. + bmxSameGeneCount :: Bool, + -- | For bmx, swap segment with same sequence index, not by best match + ssxNotBmx :: Bool, + -- | Ways to construct a phenotype from a plushy genome, so far only "specified" is implemented. Unused (for now). + closes :: String, + -- | Custom report for each generation if provided. + customReport :: Maybe (PushArgs -> IO ()), + -- | If True, keeps running regardless of success. + dontEnd :: Bool, + -- | Whether of not to use downsampling. + enableDownsampling :: Bool, + -- | The downsample function to use. "caseRand", "caseMaxim", "caseMaximAuto". + downsampleFunction :: String, + -- | Proportion of data used in downsample. + downsampleRate :: Float, + -- | Proportion of parents used to evaluate case distances. + downsampleParentRate :: Float, + -- | Amount of generations between parent distance computation + downsampleParentsGens :: Int, + -- | Whether or not to add the best individual to the next generation. + elitism :: Bool, + -- User must provide their own error function. TODO: This + -- errorFunction :: (PushArgs -> ) + -- | Type of informed downsampling. "solved", "elite", "soft". + informedDownsamplingType :: String, + -- | List of instructions to use in the evolutionary run. + instructionList :: [Gene], + -- | For motely batch lexicase selection, max size of a batch of cases. + maxMotelyBatchSize :: Int, + -- | Max size of plushy genomes in a population. + maxInitialPlushySize :: Int, + -- | Maximum amount of generations allowed in an evolutionary run. + maxGenerations :: Int, + -- | Type of parent selection to use. Think "lexicase" and "tournament" for now. + parentSelectionAlgo :: String, + -- |Size of the population in the evolutionary run. + populationSize :: Int, + -- | For uniform replacement, rate of item replacement. + replacementRate :: Float, + -- | Whether or not to auto simplify solutions. + useSimplification :: Bool, + -- | When auto simplifying, max amt items deleted in a single step. + simplificationMaxAmt :: Int, + -- | When auto simplifying, number of simplification steps. + simplificationSteps :: Int, + -- | When auto simplifying, whether to print verbose information. + simplificationVerbose :: Bool, + -- | Whether to use mutli-threading. + useMultiThreading :: Bool, + -- | Max total error for solutions. + solutionErrorThreshold :: Int, + -- | Limit of push interpreter steps in push program evaluation. + stepLimit :: Int, + -- | For tournament selection, amount of individuals in each tournament. + tournamentSize :: Int, + -- Training data for the gp, must be provided. + -- trainingData :: something + -- Testing data for the gp, must be provided if there is any. + -- testingData :: something + -- | Addition rate for UMAD (deletion rate derived from this). + umadRate :: Float, + -- | Genetic operators and probabilities for their use, should sum to one + -- Takes a Map of String -> Float where the string is the genetic operator + variation :: Map.Map String Float + } + +defaultPushArgs :: PushArgs +defaultPushArgs = PushArgs { + alignmentDeviation = 2, + alternationRate = 0.1, + bmxExchangeRate = 0.5, + bmxGeneLengthLimit = 10, + bmxGapChangeProbability = 0.001, + bmxIsComplementary = False, + bmxMaxDistance = 1000000, + bmxSameGeneCount = False, + closes = "specified", + customReport = Nothing, + dontEnd = False, + enableDownsampling = True, + downsampleFunction = "caseMaxim", + downsampleRate = 0.05, + downsampleParentRate = 0.01, + downsampleParentsGens = 10, + elitism = False, + -- errorFunction = something, + informedDownsamplingType = "solved", + instructionList = allInstructions, + maxMotelyBatchSize = 10, + maxInitialPlushySize = 100, + maxGenerations = 1000, + parentSelectionAlgo = "lexicase", + populationSize = 1000, + replacementRate = 0.1, + useSimplification = True, + simplificationMaxAmt = 4, + simplificationSteps = 1000, + simplificationVerbose = False, + useMultiThreading = False, -- False for now, change to True later. + solutionErrorThreshold = 0, + ssxNotBmx = False, + stepLimit = 1000, + tournamentSize = 5, + -- testingData = [], + -- trainingData = [], + umadRate = 0.1, + variation = Map.fromList [("umad", 1.0)] + } From b88a4944f969d717b05ab17e107b9ff7ee6201ec Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sat, 15 Feb 2025 01:24:40 -0600 Subject: [PATCH 169/171] int regression start --- HushGP.cabal | 10 +++--- src/HushGP/GP/PushArgs.hs | 26 +++++++++------ src/HushGP/Problems/IntegerRegression.hs | 41 ++++++++++++++++++++++++ src/HushGP/State.hs | 8 ++--- 4 files changed, 68 insertions(+), 17 deletions(-) create mode 100644 src/HushGP/Problems/IntegerRegression.hs diff --git a/HushGP.cabal b/HushGP.cabal index 8e34eab..ab43c47 100644 --- a/HushGP.cabal +++ b/HushGP.cabal @@ -36,7 +36,9 @@ library -- Modules exported by the library. exposed-modules: HushGP.Push - , HushGP.GP + , HushGP.TH + , HushGP.Utility + , HushGP.Genome , HushGP.State , HushGP.Instructions , HushGP.Instructions.IntInstructions @@ -58,10 +60,10 @@ library , HushGP.PushTests.IntTests , HushGP.PushTests.GenericTests , HushGP.PushTests.UtilTests + , HushGP.GP , HushGP.GP.PushArgs - , HushGP.TH - , HushGP.Utility - , HushGP.Genome + , HushGP.Problems.IntegerRegression + -- Modules included in this library but not exported. -- other-modules: diff --git a/src/HushGP/GP/PushArgs.hs b/src/HushGP/GP/PushArgs.hs index 7388a6b..a8b8536 100644 --- a/src/HushGP/GP/PushArgs.hs +++ b/src/HushGP/GP/PushArgs.hs @@ -4,6 +4,8 @@ import HushGP.State import Data.Map qualified as Map import HushGP.Instructions +-- | The structure holding the arguments for the various aspects +-- of the evolutionary run in Hush. data PushArgs = PushArgs { -- | For alternation, std deviation fo index when alternating. @@ -42,8 +44,12 @@ data PushArgs = PushArgs downsampleParentsGens :: Int, -- | Whether or not to add the best individual to the next generation. elitism :: Bool, - -- User must provide their own error function. TODO: This - -- errorFunction :: (PushArgs -> ) + -- | User must provide their own error function. + -- Arg 1: PushArgs for the current set of arguments. + -- Arg 2: ([[Gene]], [Gene]) is the input data. Input is the first index and output is the second index. + -- Arg 3: [Gene] is the plushy representation of a program. + -- Returns the error list for a given set of inputs of type [Double]. + errorFunction :: PushArgs -> [[Gene]] -> [Gene] -> [Double], -- | Type of informed downsampling. "solved", "elite", "soft". informedDownsamplingType :: String, -- | List of instructions to use in the evolutionary run. @@ -76,10 +82,10 @@ data PushArgs = PushArgs stepLimit :: Int, -- | For tournament selection, amount of individuals in each tournament. tournamentSize :: Int, - -- Training data for the gp, must be provided. - -- trainingData :: something - -- Testing data for the gp, must be provided if there is any. - -- testingData :: something + -- | Training data for the gp, must be provided. + trainingData :: [[Gene]], + -- | Testing data for the gp, must be provided if there is any. + testingData :: [[Gene]], -- | Addition rate for UMAD (deletion rate derived from this). umadRate :: Float, -- | Genetic operators and probabilities for their use, should sum to one @@ -87,6 +93,8 @@ data PushArgs = PushArgs variation :: Map.Map String Float } +-- | The default values for which all runs of Hush derive +-- their args from. defaultPushArgs :: PushArgs defaultPushArgs = PushArgs { alignmentDeviation = 2, @@ -106,7 +114,7 @@ defaultPushArgs = PushArgs { downsampleParentRate = 0.01, downsampleParentsGens = 10, elitism = False, - -- errorFunction = something, + errorFunction = error "Must supply the error function yourself", informedDownsamplingType = "solved", instructionList = allInstructions, maxMotelyBatchSize = 10, @@ -124,8 +132,8 @@ defaultPushArgs = PushArgs { ssxNotBmx = False, stepLimit = 1000, tournamentSize = 5, - -- testingData = [], - -- trainingData = [], + testingData = [], + trainingData = [], umadRate = 0.1, variation = Map.fromList [("umad", 1.0)] } diff --git a/src/HushGP/Problems/IntegerRegression.hs b/src/HushGP/Problems/IntegerRegression.hs new file mode 100644 index 0000000..b85b7f0 --- /dev/null +++ b/src/HushGP/Problems/IntegerRegression.hs @@ -0,0 +1,41 @@ +module HushGP.Problems.IntegerRegression where + +import HushGP.State +import HushGP.Instructions +import Data.List.Split +import HushGP.GP.PushArgs +import HushGP.Genome +import HushGP.Push +import Data.Map qualified as Map + +-- | The target function for this run. The function the gp +-- is trying to evolve. +targetFunction :: Integer -> Integer +targetFunction x = (x * x * x) + (2 * x) + +-- | The training data for the model. +trainData :: ([[Gene]], [Gene]) +trainData = (chunksOf 1 $ map GeneInt [-10..10], map (GeneInt . targetFunction) [-10..11]) + +-- | The testing data for the model. +testData :: ([[Gene]], [Gene]) +testData = (chunksOf 1 $ map GeneInt $ [-20..(-11)] <> [11..21], map (GeneInt . targetFunction) ([-20..(-11)] <> [11..21])) + +-- | The instructions used to +runInstructions :: [Gene] +runInstructions = + [ + PlaceInput 0, + Close, + GeneInt 1, + GeneInt 0 + ] + <> allIntInstructions + +-- |The error function for a single set of inputs and outputs. +intErrorFunction :: PushArgs -> ([Gene], Gene) -> [Gene] -> [Double] +intErrorFunction args (inputData, outputData) plushy = + head $ _int $ interpretExec loadedState + where + loadedState :: State + loadedState = (loadProgram (plushyToPush plushy) emptyState){_input = Map.fromList (zip [0..] inputData)} diff --git a/src/HushGP/State.hs b/src/HushGP/State.hs index 5dad5a7..87a1d92 100644 --- a/src/HushGP/State.hs +++ b/src/HushGP/State.hs @@ -21,9 +21,9 @@ data Gene | GeneVectorBool [Bool] | GeneVectorString [String] | GeneVectorChar [Char] - | -- | State -> State is the function itself. String stores the name of the function. - StateFunc (State -> State, String) - | PlaceInput String + -- | State -> State is the function itself. String stores the name of the function. + | StateFunc (State -> State, String) + | PlaceInput Int | Close | Open Int | Skip @@ -160,7 +160,7 @@ data State = State _vectorString :: [[String]], _vectorChar :: [[Char]], _parameter :: [Gene], - _input :: Map.Map String Gene + _input :: Map.Map Int Gene } deriving (Show, Eq, Ord) From 090a402f0632ba3ae72707b5c1a6c41e1faa56cc Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sat, 15 Feb 2025 23:46:40 -0600 Subject: [PATCH 170/171] finish int regression framework, time for the rest of pieces --- src/HushGP/GP.hs | 11 +++++ src/HushGP/GP/PushArgs.hs | 6 +-- src/HushGP/Genome.hs | 5 +- src/HushGP/Instructions/Utility.hs | 8 +++ src/HushGP/Problems/IntegerRegression.hs | 62 ++++++++++++++++++++---- 5 files changed, 77 insertions(+), 15 deletions(-) diff --git a/src/HushGP/GP.hs b/src/HushGP/GP.hs index d2be570..56c76d2 100644 --- a/src/HushGP/GP.hs +++ b/src/HushGP/GP.hs @@ -1,3 +1,14 @@ module HushGP.GP where +import HushGP.State +import HushGP.Genome +import HushGP.GP.PushArgs -- import Debug.Trace (trace, traceStack) + +-- generatePopulation :: PushArgs -> [Gene] -> IO [[Gene]] +-- generatePopulation pushArgs instructions = do + -- randomPop <- makeRandomPlushy pushArgs + -- replicate (populationSize pushArgs) (makeRandomPlushy pushArgs) + +gpLoop :: PushArgs -> IO () +gpLoop = undefined diff --git a/src/HushGP/GP/PushArgs.hs b/src/HushGP/GP/PushArgs.hs index a8b8536..f604ff3 100644 --- a/src/HushGP/GP/PushArgs.hs +++ b/src/HushGP/GP/PushArgs.hs @@ -49,7 +49,7 @@ data PushArgs = PushArgs -- Arg 2: ([[Gene]], [Gene]) is the input data. Input is the first index and output is the second index. -- Arg 3: [Gene] is the plushy representation of a program. -- Returns the error list for a given set of inputs of type [Double]. - errorFunction :: PushArgs -> [[Gene]] -> [Gene] -> [Double], + errorFunction :: PushArgs -> ([[Gene]], [Gene]) -> [Gene] -> [Double], -- | Type of informed downsampling. "solved", "elite", "soft". informedDownsamplingType :: String, -- | List of instructions to use in the evolutionary run. @@ -83,9 +83,9 @@ data PushArgs = PushArgs -- | For tournament selection, amount of individuals in each tournament. tournamentSize :: Int, -- | Training data for the gp, must be provided. - trainingData :: [[Gene]], + trainingData :: ([[Gene]], [Gene]), -- | Testing data for the gp, must be provided if there is any. - testingData :: [[Gene]], + testingData :: ([[Gene]], [Gene]), -- | Addition rate for UMAD (deletion rate derived from this). umadRate :: Float, -- | Genetic operators and probabilities for their use, should sum to one diff --git a/src/HushGP/Genome.hs b/src/HushGP/Genome.hs index 7618840..cb98df0 100644 --- a/src/HushGP/Genome.hs +++ b/src/HushGP/Genome.hs @@ -6,6 +6,7 @@ import Data.Map qualified as Map import HushGP.Instructions.Opens import HushGP.State import HushGP.Utility +import HushGP.GP.PushArgs -- import HushGP.Instructions -- import Debug.Trace @@ -31,8 +32,8 @@ import HushGP.Utility -- | Makes a random plushy from variables in a passed argMap and -- a passed list of instructions. -makeRandomPlushy :: Map.Map String String -> [Gene] -> IO [Gene] -makeRandomPlushy argMap = randomInstructions (read @Int (argMap Map.! "maxInitialPlushySize")) +makeRandomPlushy :: PushArgs -> [Gene] -> IO [Gene] +makeRandomPlushy pushArgs = randomInstructions (maxInitialPlushySize pushArgs) -- | A utility function to generate an amount based on an int rather than -- from an argmap. diff --git a/src/HushGP/Instructions/Utility.hs b/src/HushGP/Instructions/Utility.hs index bfbc8b6..1ab6f61 100644 --- a/src/HushGP/Instructions/Utility.hs +++ b/src/HushGP/Instructions/Utility.hs @@ -105,6 +105,14 @@ absNum rawNum lst = abs (fromIntegral rawNum) `mod` length lst notEmptyStack :: Lens' State [a] -> State -> Bool notEmptyStack accessor state = not . null $ view accessor state +-- |Utility Function: Extracts an int from a GeneInt. +-- How to make this polymorphic???????? A general function for +-- this would be nice. Wrapped in a maybe too? +extractGeneInt :: Gene -> Integer +extractGeneInt (GeneInt x) = x +extractGeneInt _ = error "todo this later??" + + -- bool utility -- |A template function to make bool comparisons concise. diff --git a/src/HushGP/Problems/IntegerRegression.hs b/src/HushGP/Problems/IntegerRegression.hs index b85b7f0..028d182 100644 --- a/src/HushGP/Problems/IntegerRegression.hs +++ b/src/HushGP/Problems/IntegerRegression.hs @@ -1,12 +1,23 @@ module HushGP.Problems.IntegerRegression where +import Data.List.Split +import Data.List +import Data.Map qualified as Map import HushGP.State import HushGP.Instructions -import Data.List.Split import HushGP.GP.PushArgs import HushGP.Genome import HushGP.Push -import Data.Map qualified as Map +import HushGP.Instructions.Utility +import HushGP.GP + +testPlushy :: [Gene] +testPlushy = [ + PlaceInput 0, + GeneInt 0, + StateFunc (instructionIntAdd, "instructionIntAdd") + -- GeneFloat 3.2 + ] -- | The target function for this run. The function the gp -- is trying to evolve. @@ -21,7 +32,7 @@ trainData = (chunksOf 1 $ map GeneInt [-10..10], map (GeneInt . targetFunction) testData :: ([[Gene]], [Gene]) testData = (chunksOf 1 $ map GeneInt $ [-20..(-11)] <> [11..21], map (GeneInt . targetFunction) ([-20..(-11)] <> [11..21])) --- | The instructions used to +-- | The instructions used in the evolutionary run. runInstructions :: [Gene] runInstructions = [ @@ -32,10 +43,41 @@ runInstructions = ] <> allIntInstructions --- |The error function for a single set of inputs and outputs. -intErrorFunction :: PushArgs -> ([Gene], Gene) -> [Gene] -> [Double] -intErrorFunction args (inputData, outputData) plushy = - head $ _int $ interpretExec loadedState - where - loadedState :: State - loadedState = (loadProgram (plushyToPush plushy) emptyState){_input = Map.fromList (zip [0..] inputData)} +-- | Takes the head of the stack and returns it. If there is no head, returns an +-- error amount. +errorHead :: [Integer] -> Integer +errorHead xs = + case uncons xs of + Just (x, _) -> x + _ -> 100000000 -- Make this a variable for later? + +-- | Loads a plushy and a list of genes into the input state. +loadState :: [Gene] -> [Gene] -> State +loadState plushy vals = + (loadProgram (plushyToPush plushy) emptyState){_input = Map.fromList (zip [0..] vals)} + +-- | The error function for a single set of inputs and outputs. +intErrorFunction :: PushArgs -> ([[Gene]], [Gene]) -> [Gene] -> [Double] +intErrorFunction _args (inputData, outputData) plushy = + map abs $ zipWith (-) (map ((fromIntegral @Integer @Double . (errorHead . _int) . interpretExec) . loadState plushy) inputData) (map (fromIntegral @Integer @Double . extractGeneInt) outputData) + +intArgMap :: PushArgs +intArgMap = defaultPushArgs + { + instructionList = runInstructions, + errorFunction = intErrorFunction, + trainingData = trainData, + testingData = testData, + maxGenerations = 300, + populationSize = 1000, + maxInitialPlushySize = 100, + stepLimit = 200, + parentSelectionAlgo = "lexicase", + tournamentSize = 5, + umadRate = 0.1, + variation = Map.fromList [("umad", 1.0), ("crossover", 0.0)], + elitism = False + } + +main :: IO () +main = gpLoop intArgMap From 88b5b528134bc5631f9b736e137baa82c8c0085a Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Mon, 17 Feb 2025 22:54:41 -0600 Subject: [PATCH 171/171] parallelism/start the individual creation --- HushGP.cabal | 4 +-- src/HushGP/GP.hs | 19 ++++++++---- src/HushGP/GP/PushArgs.hs | 4 +-- src/HushGP/Genome.hs | 37 +++++++++--------------- src/HushGP/Problems/IntegerRegression.hs | 6 ++-- 5 files changed, 35 insertions(+), 35 deletions(-) diff --git a/HushGP.cabal b/HushGP.cabal index ab43c47..87df5dc 100644 --- a/HushGP.cabal +++ b/HushGP.cabal @@ -28,7 +28,7 @@ category: Data build-type: Simple common warnings - ghc-options: -Wall -XTemplateHaskell + ghc-options: -Wall -XTemplateHaskell -threaded library -- Import common warning flags. @@ -73,7 +73,7 @@ library -- Other library packages from which modules are imported. build-depends: - base, containers, lens, split, QuickCheck, regex-tdfa, template-haskell, random + base, containers, lens, split, QuickCheck, regex-tdfa, template-haskell, random, parallel -- Directories containing source files. hs-source-dirs: src diff --git a/src/HushGP/GP.hs b/src/HushGP/GP.hs index 56c76d2..c3a61c0 100644 --- a/src/HushGP/GP.hs +++ b/src/HushGP/GP.hs @@ -3,12 +3,21 @@ module HushGP.GP where import HushGP.State import HushGP.Genome import HushGP.GP.PushArgs +import Control.Monad +import Control.Parallel.Strategies -- import Debug.Trace (trace, traceStack) --- generatePopulation :: PushArgs -> [Gene] -> IO [[Gene]] --- generatePopulation pushArgs instructions = do - -- randomPop <- makeRandomPlushy pushArgs - -- replicate (populationSize pushArgs) (makeRandomPlushy pushArgs) +-- | Using a PushArgs object, generates a population of the specified size with the +-- specified instructions in parallel. +generatePopulation :: PushArgs -> [Individual] +generatePopulation pushArgs = + replicate (populationSize pushArgs) (makeRandomIndividual pushArgs) `using` rpar +evaluatePopulation :: PushArgs -> [Individual] -> IO [Individual] +evaluatePopulation pushArgs population = map (fmap (errorFunction pushArgs pushArgs (trainingData pushArgs)) . plushy) population + +-- | The start of the gp loop. TODO: Make this more accurate later. gpLoop :: PushArgs -> IO () -gpLoop = undefined +gpLoop pushArgs = do + let unEvaledPopulation = generatePopulation pushArgs + print "gamer" diff --git a/src/HushGP/GP/PushArgs.hs b/src/HushGP/GP/PushArgs.hs index f604ff3..3858e25 100644 --- a/src/HushGP/GP/PushArgs.hs +++ b/src/HushGP/GP/PushArgs.hs @@ -132,8 +132,8 @@ defaultPushArgs = PushArgs { ssxNotBmx = False, stepLimit = 1000, tournamentSize = 5, - testingData = [], - trainingData = [], + testingData = ([], []), + trainingData = ([], []), umadRate = 0.1, variation = Map.fromList [("umad", 1.0)] } diff --git a/src/HushGP/Genome.hs b/src/HushGP/Genome.hs index cb98df0..f57e7ab 100644 --- a/src/HushGP/Genome.hs +++ b/src/HushGP/Genome.hs @@ -10,30 +10,21 @@ import HushGP.GP.PushArgs -- import HushGP.Instructions -- import Debug.Trace --- tempPlushy :: [Gene] --- tempPlushy = [ --- StateFunc (instructionIntDiv, "instructionIntDiv"), --- StateFunc (instructionExecDup, "instructionExecDup"), --- GeneInt 1, --- GeneInt 0, --- StateFunc (instructionIntDiv, "instructionIntDiv"), --- Skip, --- GeneInt (-15), --- StateFunc (instructionExecDup, "instructionExecDup"), --- StateFunc (instructionIntSub, "instructionIntSub"), --- StateFunc (instructionFloatMul, "instructionFloatMul"), --- Skip, --- Close, --- -- StateFunc (instructionNoOpBlock, "instructionNoOpBlock"), --- StateFunc (instructionExecIf, "instructionExecIf"), --- Close, --- Close --- ] +-- | The structure for an individual containing the genome, the totalFitness, and +-- the individual fitness cases for lexicase. +data Individual = Individual { + plushy :: IO [Gene], + totalFitness :: Maybe Double, + fitnessCases :: Maybe [Double] +} --- | Makes a random plushy from variables in a passed argMap and --- a passed list of instructions. -makeRandomPlushy :: PushArgs -> [Gene] -> IO [Gene] -makeRandomPlushy pushArgs = randomInstructions (maxInitialPlushySize pushArgs) +-- | Makes a random individual based on the variables in a passed PushArgs. +makeRandomIndividual :: PushArgs -> Individual +makeRandomIndividual pushArgs = Individual {plushy = makeRandomPlushy pushArgs, totalFitness = Nothing, fitnessCases = Nothing} + +-- | Makes a random plushy from variables in a passed PushArgs. +makeRandomPlushy :: PushArgs -> IO [Gene] +makeRandomPlushy pushArgs = randomInstructions (maxInitialPlushySize pushArgs) (instructionList pushArgs) -- | A utility function to generate an amount based on an int rather than -- from an argmap. diff --git a/src/HushGP/Problems/IntegerRegression.hs b/src/HushGP/Problems/IntegerRegression.hs index 028d182..7abe7c2 100644 --- a/src/HushGP/Problems/IntegerRegression.hs +++ b/src/HushGP/Problems/IntegerRegression.hs @@ -61,8 +61,8 @@ intErrorFunction :: PushArgs -> ([[Gene]], [Gene]) -> [Gene] -> [Double] intErrorFunction _args (inputData, outputData) plushy = map abs $ zipWith (-) (map ((fromIntegral @Integer @Double . (errorHead . _int) . interpretExec) . loadState plushy) inputData) (map (fromIntegral @Integer @Double . extractGeneInt) outputData) -intArgMap :: PushArgs -intArgMap = defaultPushArgs +intPushArgs :: PushArgs +intPushArgs = defaultPushArgs { instructionList = runInstructions, errorFunction = intErrorFunction, @@ -80,4 +80,4 @@ intArgMap = defaultPushArgs } main :: IO () -main = gpLoop intArgMap +main = gpLoop intPushArgs