Handle input stack

This commit is contained in:
Taylor 2025-01-14 02:43:00 -06:00
parent 4e9dca38b8
commit a82958feda
2 changed files with 9 additions and 8 deletions

View File

@ -25,6 +25,7 @@ data Gene
| StringGene String | StringGene String
| StateFunc (State -> State -> State) | StateFunc (State -> State -> State)
| Close | Close
| Input Gene
-- | Group [Gene] -- | Group [Gene]
-- If we do plushy, -- If we do plushy,
@ -57,25 +58,25 @@ stackUpdate newstack@(IntGene _ : _) (State e _ f b s p) = State e newstack f b
stackUpdate newstack@(FloatGene _ : _) (State e i _ b s p) = State e i newstack b s p stackUpdate newstack@(FloatGene _ : _) (State e i _ b s p) = State e i newstack b s p
stackUpdate newstack@(BoolGene _ : _) (State e i f _ s p) = State e i f newstack s p stackUpdate newstack@(BoolGene _ : _) (State e i f _ s p) = State e i f newstack s p
stackUpdate newstack@(StringGene _ : _) (State e i f b _ p) = State e i f b newstack p stackUpdate newstack@(StringGene _ : _) (State e i f b _ p) = State e i f b newstack p
stackUpdate newstack@(Input _ : _) (State e i f b s _) = State e i f b s newstack
stackUpdate _ state = state stackUpdate _ state = state
-- stackUpdate stackFunc (Input? _ : xs) (State e i f b s _) = State e i f b s newstack
unpackIntGene :: Gene -> Int unpackIntGene :: Gene -> Int
unpackIntGene (IntGene item) = item unpackIntGene (IntGene item) = item
-- Start with monolithic add function this: -- Start with monolithic intAdd function:
intAdd :: State -> State intAdd :: State -> State
intAdd state = intAdd state =
let result = sum (map unpackIntGene (take 2 (int state))) let result = sum (map unpackIntGene (take 2 (int state)))
dropped = drop 2 (int state) dropped = drop 2 (int state)
in stackUpdate (IntGene result : dropped) state in stackUpdate (IntGene result : dropped) state
-- Later, generalize an applyFuncToState which applies simplifications of each simpler, modular atomic function: -- Later, generalize a function called applyFuncToState,
-- which takes each simpler atomic function, and the state,
-- and applies the function to the state, for example:
-- intAdd :: (Int, Int) -> Int -- intAdd :: (Int, Int) -> Int
-- applyFuncState :: AtomicFuncTypes -> State -> State
-- applyFuncState :: State -> AtomicFuncTypes -> State -- this would change Gene to something like GeneModular above.
interpretGenome :: State -> [(State -> State)] -> State interpretGenome :: State -> [(State -> State)] -> State
interpretGenome state = foldl (\acc f -> f acc) state interpretGenome state = foldl (\acc f -> f acc) state

View File

@ -12,7 +12,7 @@ main = do
float = [FloatGene 1.2, FloatGene 1.7], float = [FloatGene 1.2, FloatGene 1.7],
bool = [BoolGene True, BoolGene False], bool = [BoolGene True, BoolGene False],
string = [StringGene "Hello", StringGene "Push"], string = [StringGene "Hello", StringGene "Push"],
input = [IntGene 1, StringGene "Hi", BoolGene True, FloatGene 1.3] input = [Input $ IntGene 1, Input $ StringGene "Hi", Input $ BoolGene True, Input $ FloatGene 1.3]
} }
-- This is an example of applynig one function (head exampleGenome) to the exampleState: -- This is an example of applynig one function (head exampleGenome) to the exampleState:
assert ([3, 3] == map unpackIntGene (int (head exampleGenome exampleState))) pure () assert ([3, 3] == map unpackIntGene (int (head exampleGenome exampleState))) pure ()