Merge branch 'vectors_code' into learn_quickcheck

merge vectors_code into learn_quickcheck
This commit is contained in:
Rowan Torbitzky-Lane 2025-01-31 17:12:39 -06:00
commit 6c4c84e7dc
6 changed files with 33 additions and 51 deletions

12
TODO.md Normal file
View File

@ -0,0 +1,12 @@
# TODO
## Push Language TODO
- [ ] Make all vector functions applicable to string functions and vice versa
- [ ] 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
## PushGP TODO
- [ ] Implement a Plushy genome translator

View File

@ -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 then replace (take (findSubA fullA old) fullA <> new <> drop (findSubA fullA old + length old) fullA) old new Nothing
else fullA else fullA
-- a rather inefficient search
amtOccurences :: forall a. Eq a => [a] -> [a] -> Int amtOccurences :: forall a. Eq a => [a] -> [a] -> Int
amtOccurences fullA subA = amtOccurences' fullA subA 0 amtOccurences fullA subA = amtOccurences' fullA subA 0
where where
@ -149,39 +150,36 @@ instructionEq state accessor =
instructionStackDepth :: State -> Lens' State [a] -> State instructionStackDepth :: State -> Lens' State [a] -> State
instructionStackDepth state@(State {_int = is}) accessor = state{_int = length (view accessor state) : is} 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 -> Lens' State [a] -> State
instructionYankDup state@(State {_int = i : is}) accessor = instructionYankDup state@(State {_int = i : is}) accessor =
if notEmptyStack state 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 else state
instructionYankDup state _ = state instructionYankDup state _ = state
-- Is this optimal? Running instrucitonYankDup twice?????
-- int non generic too
instructionYank :: forall a. State -> Lens' State [a] -> State instructionYank :: forall a. State -> Lens' State [a] -> State
instructionYank state@(State {_int = rawIndex : _}) accessor = instructionYank state@(State {_int = i : is}) accessor =
let let
myIndex :: Int 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 :: a
item = view accessor state !! myIndex item = view accessor state{_int = is} !! myIndex
deletedState :: State deletedState :: State
deletedState = state & accessor .~ deleteAt myIndex (view accessor state) deletedState = state{_int = is} & accessor .~ deleteAt myIndex (view accessor state{_int = is})
in 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 instructionYank state _ = state
-- int non generic :( -- In pysh, instructionShoveDup and instructionShove behave differently when indexing in such a way that
-- Rewrite this eventually? -- 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 -> Lens' State [a] -> State
instructionShoveDup state@(State {_int = i : is}) accessor = instructionShoveDup state@(State {_int = i : is}) accessor =
case uncons (view accessor state) of case uncons (view accessor state{_int = is}) of
Just (x,_) -> (state & accessor .~ combineTuple x (splitAt (max 0 (min i (length (view accessor state) - 1))) (view accessor state))) {_int = is} 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 _ -> state
instructionShoveDup state@(State {_int = []}) _ = state instructionShoveDup state _ = state
-- also also not int generic
instructionShove :: State -> Lens' State [a] -> State instructionShove :: State -> Lens' State [a] -> State
instructionShove state accessor = instructionShoveDup state accessor & accessor .~ drop 1 (view accessor (instructionShoveDup state accessor)) instructionShove state accessor = instructionShoveDup state accessor & accessor .~ drop 1 (view accessor (instructionShoveDup state accessor))
@ -206,7 +204,7 @@ instructionConj state primAccessor vectorAccessor =
_ -> state _ -> state
-- v for vector, vs for vectorstack (also applicable to strings) -- 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 -> Lens' State [[a]] -> State
instructionTakeN state@(State {_int = i1 : is}) accessor = instructionTakeN state@(State {_int = i1 : is}) accessor =
case uncons (view accessor state) of case uncons (view accessor state) of

View File

@ -88,43 +88,17 @@ instructionIntEq state = instructionEq state int
instructionIntStackDepth :: State -> State instructionIntStackDepth :: State -> State
instructionIntStackDepth state = instructionStackDepth state int instructionIntStackDepth state = instructionStackDepth state int
-- int specific
instructionIntYank :: State -> State instructionIntYank :: State -> State
-- instructionIntYank state = instructionYank state int 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
instructionIntYankDup state@(State {_int = rawIndex : item : is}) = instructionIntYankDup state = instructionYankDup state int
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
instructionIntShove state@(State {_int = rawIndex : item : is}) = instructionIntShove state = instructionShove state int
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
instructionIntShoveDup state@(State {_int = rawIndex : item : is}) = instructionIntShoveDup state = instructionShoveDup state int
let
myIndex :: Int
myIndex = max 0 (min rawIndex (length is - 1))
in
state {_int = item : combineTuple item (splitAt myIndex is)}
instructionIntShoveDup state = state
instructionIntIsEmpty :: State -> State instructionIntIsEmpty :: State -> State
instructionIntIsEmpty state = instructionIsEmpty state int instructionIntIsEmpty state = instructionIsEmpty state int

View File

@ -94,8 +94,6 @@ instructionStringInsertChar state@(State {_string = s1 : ss, _char = c1 : cs, _i
instructionStringInsertChar state = state instructionStringInsertChar state = state
instructionStringContainsChar :: 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 instructionStringContainsChar state = instructionVectorContains state char string
instructionStringIndexOfChar :: State -> State instructionStringIndexOfChar :: State -> State

View File

@ -64,7 +64,7 @@ interpretExec state@(State {_exec = e : es}) =
(StateFunc func) -> interpretExec $ func state {_exec = es} (StateFunc func) -> interpretExec $ func state {_exec = es}
(Block block) -> interpretExec (state {_exec = block ++ es}) (Block block) -> interpretExec (state {_exec = block ++ es})
(PlaceInput val) -> interpretExec (state {_exec = (view input state Map.! val) : es}) (PlaceInput val) -> interpretExec (state {_exec = (view input state Map.! val) : es})
Close -> undefined -- remove Close constructor later? Close -> undefined -- This should be removed later. Will be converted to Blocks in the Plushy -> Exec stack process
interpretExec state = state interpretExec state = state
-- Need to make interpretExec strict, right? -- Need to make interpretExec strict, right?

View File

@ -68,8 +68,8 @@ main = do
intTestFunc "instructionIntStackDepth" [2, 51, 52] [GeneInt 52, GeneInt 51, StateFunc instructionIntStackDepth] emptyState 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 "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 "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 "instructionIntShove" [2, 3, 1, 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 "instructionIntShoveDup" [3, 2, 3, 1, 1] [GeneInt 1, GeneInt 1, GeneInt 2, GeneInt 3, GeneInt 2, StateFunc instructionIntShoveDup] emptyState
-- Exec tests -- Exec tests
intTestFunc "instructionExecIf" [6, 5] [GeneBool True, StateFunc instructionExecIf, Block [GeneInt 5, GeneInt 6], Block [GeneInt 7, GeneInt 8]] emptyState intTestFunc "instructionExecIf" [6, 5] [GeneBool True, StateFunc instructionExecIf, Block [GeneInt 5, GeneInt 6], Block [GeneInt 7, GeneInt 8]] emptyState