make pattern matching parameters consistent
This commit is contained in:
parent
ff31a8fa35
commit
867f3ac440
2
TODO.md
2
TODO.md
@ -12,7 +12,7 @@
|
|||||||
- [X] Make int yank, shove, yankdup, and shovedup generic
|
- [X] Make int yank, shove, yankdup, and shovedup generic
|
||||||
- [ ] Write hackage documentation for each function
|
- [ ] Write hackage documentation for each function
|
||||||
- [X] 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
|
- [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 all of the instructions
|
||||||
|
|
||||||
## PushGP TODO
|
## PushGP TODO
|
||||||
|
@ -4,26 +4,26 @@ import HushGP.State
|
|||||||
import HushGP.Instructions.GenericInstructions
|
import HushGP.Instructions.GenericInstructions
|
||||||
|
|
||||||
instructionBoolFromInt :: State -> State
|
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
|
instructionBoolFromInt state = state
|
||||||
|
|
||||||
instructionBoolFromFloat :: 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
|
instructionBoolFromFloat state = state
|
||||||
|
|
||||||
boolTemplate :: (Bool -> Bool -> Bool) -> 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
|
boolTemplate _ state = state
|
||||||
|
|
||||||
instructionBoolAnd :: State -> State
|
instructionBoolAnd :: State -> State
|
||||||
instructionBoolAnd = boolTemplate (&&)
|
instructionBoolAnd = boolTemplate (&&)
|
||||||
|
|
||||||
instructionBoolInvertFirstThenAnd :: State -> State
|
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
|
instructionBoolInvertFirstThenAnd state = state
|
||||||
|
|
||||||
instructionBoolInvertSecondThenAnd :: 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
|
instructionBoolInvertSecondThenAnd state = state
|
||||||
|
|
||||||
instructionBoolOr :: State -> State
|
instructionBoolOr :: State -> State
|
||||||
|
@ -11,11 +11,11 @@ isBlock (Block _) = True
|
|||||||
isBlock _ = False
|
isBlock _ = False
|
||||||
|
|
||||||
blockLength :: Gene -> Int
|
blockLength :: Gene -> Int
|
||||||
blockLength (Block xs) = length xs
|
blockLength (Block bxs) = length bxs
|
||||||
blockLength _ = 1
|
blockLength _ = 1
|
||||||
|
|
||||||
blockIsNull :: Gene -> Bool
|
blockIsNull :: Gene -> Bool
|
||||||
blockIsNull (Block xs) = null xs
|
blockIsNull (Block bxs) = null bxs
|
||||||
blockIsNull _ = False
|
blockIsNull _ = False
|
||||||
|
|
||||||
-- https://faculty.hampshire.edu/lspector/push3-description.html#Type
|
-- https://faculty.hampshire.edu/lspector/push3-description.html#Type
|
||||||
@ -29,7 +29,7 @@ findContainer (Block fullA) gene
|
|||||||
where
|
where
|
||||||
findContainer' :: [Gene] -> Gene -> Gene
|
findContainer' :: [Gene] -> Gene -> Gene
|
||||||
findContainer' [] _ = Block []
|
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 [] -- This should never happen
|
||||||
findContainer _ _ = Block []
|
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
|
countDiscrepancy xgene ygene = if xgene == ygene then 1 else 0
|
||||||
|
|
||||||
extractFirstFromBlock :: Gene -> Gene
|
extractFirstFromBlock :: Gene -> Gene
|
||||||
extractFirstFromBlock (Block (x : _)) = x
|
extractFirstFromBlock (Block (bx1 : _)) = bx1
|
||||||
extractFirstFromBlock gene = gene
|
extractFirstFromBlock gene = gene
|
||||||
|
|
||||||
extractLastFromBlock :: Gene -> Gene
|
extractLastFromBlock :: Gene -> Gene
|
||||||
extractLastFromBlock (Block []) = Block []
|
extractLastFromBlock (Block []) = Block []
|
||||||
extractLastFromBlock (Block xs) = last xs
|
extractLastFromBlock (Block bxs) = last bxs
|
||||||
extractLastFromBlock gene = gene
|
extractLastFromBlock gene = gene
|
||||||
|
|
||||||
extractInitFromBlock :: Gene -> Gene
|
extractInitFromBlock :: Gene -> Gene
|
||||||
extractInitFromBlock (Block []) = Block []
|
extractInitFromBlock (Block []) = Block []
|
||||||
extractInitFromBlock (Block xs) = Block (init xs)
|
extractInitFromBlock (Block bxs) = Block (init bxs)
|
||||||
extractInitFromBlock gene = gene
|
extractInitFromBlock gene = gene
|
||||||
|
|
||||||
extractTailFromBlock :: Gene -> Gene
|
extractTailFromBlock :: Gene -> Gene
|
||||||
extractTailFromBlock (Block xs) = Block (drop 1 xs)
|
extractTailFromBlock (Block bxs) = Block (drop 1 bxs)
|
||||||
extractTailFromBlock _ = Block []
|
extractTailFromBlock _ = Block []
|
||||||
|
|
||||||
codeAtPoint :: [Gene] -> Int -> Gene
|
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)
|
codeInsertAtPoint (oldGene : oldGenes) gene index = oldGene : codeInsertAtPoint oldGenes gene (index - 1)
|
||||||
|
|
||||||
codeCombine :: Gene -> Gene -> Gene
|
codeCombine :: Gene -> Gene -> Gene
|
||||||
codeCombine (Block xs) (Block ys) = Block (xs <> ys)
|
codeCombine (Block bxs) (Block bys) = Block (bxs <> bys)
|
||||||
codeCombine (Block xs) ygene = Block (ygene : xs)
|
codeCombine (Block bxs) ygene = Block (ygene : bxs)
|
||||||
codeCombine xgene (Block ys) = Block (xgene : ys)
|
codeCombine xgene (Block bys) = Block (xgene : bys)
|
||||||
codeCombine xgene ygene = Block [xgene, ygene]
|
codeCombine xgene ygene = Block [xgene, ygene]
|
||||||
|
|
||||||
codeMember :: Gene -> Gene -> Bool
|
codeMember :: Gene -> Gene -> Bool
|
||||||
codeMember (Block _) (Block _) = False -- Can't compare two lists with `elem`
|
codeMember (Block bxs) (Block bys) = findSubA bxs bys /= (-1)
|
||||||
codeMember (Block xs) ygene = ygene `elem` xs
|
codeMember (Block bxs) ygene = ygene `elem` bxs
|
||||||
codeMember _ _ = False
|
codeMember _ _ = False
|
||||||
|
|
||||||
codeRecursiveSize :: Gene -> Int
|
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
|
codeRecursiveSize _ = 1
|
||||||
|
|
||||||
instructionCodePop :: State -> State
|
instructionCodePop :: State -> State
|
||||||
instructionCodePop = instructionPop code
|
instructionCodePop = instructionPop code
|
||||||
|
|
||||||
instructionCodeIsCodeBlock :: State -> State
|
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
|
instructionCodeIsCodeBlock state = state
|
||||||
|
|
||||||
instructionCodeIsSingular :: 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
|
instructionCodeIsSingular state = state
|
||||||
|
|
||||||
instructionCodeLength :: 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
|
instructionCodeLength state = state
|
||||||
|
|
||||||
-- CODE.CAR
|
-- CODE.CAR
|
||||||
instructionCodeFirst :: State -> State
|
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
|
instructionCodeFirst state = state
|
||||||
|
|
||||||
instructionCodeLast :: 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
|
instructionCodeLast state = state
|
||||||
|
|
||||||
-- CODE.CDR
|
-- CODE.CDR
|
||||||
-- https://erp12.github.io/pyshgp/html/core_instructions.html#code-rest
|
-- https://erp12.github.io/pyshgp/html/core_instructions.html#code-rest
|
||||||
instructionCodeTail :: State -> State
|
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
|
instructionCodeTail state = state
|
||||||
|
|
||||||
-- |Takes the tail of a block starting at an index determined by the int stack
|
-- |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
|
-- https://erp12.github.io/pyshgp/html/core_instructions.html#code-but-last
|
||||||
instructionCodeInit :: State -> State
|
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
|
instructionCodeInit state = state
|
||||||
|
|
||||||
instructionCodeWrap :: 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
|
instructionCodeWrap state = state
|
||||||
|
|
||||||
instructionCodeList :: 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
|
instructionCodeList state = state
|
||||||
|
|
||||||
instructionCodeCombine :: 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
|
instructionCodeCombine state = state
|
||||||
|
|
||||||
instructionCodeDo :: State -> State
|
instructionCodeDo :: State -> State
|
||||||
@ -144,7 +144,7 @@ instructionCodeDo state@(State {_code = c1 : cs, _exec = es}) = state {_code = c
|
|||||||
instructionCodeDo state = state
|
instructionCodeDo state = state
|
||||||
|
|
||||||
instructionCodeDoDup :: 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
|
instructionCodeDoDup state = state
|
||||||
|
|
||||||
-- https://erp12.github.io/pyshgp/html/core_instructions.html#code-do-then-pop
|
-- https://erp12.github.io/pyshgp/html/core_instructions.html#code-do-then-pop
|
||||||
@ -159,7 +159,7 @@ codeDoRange :: Gene
|
|||||||
codeDoRange = StateFunc (instructionCodeDoRange, "instructionCodeDoRange")
|
codeDoRange = StateFunc (instructionCodeDoRange, "instructionCodeDoRange")
|
||||||
|
|
||||||
instructionCodeDoRange :: State -> State
|
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
|
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, codeFromExec, c1, codeDoRange] : es, _int = i1 : is, _code = cs}
|
||||||
else state {_exec = c1: 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
|
instructionCodeDoRange state = state
|
||||||
|
|
||||||
instructionCodeDoCount :: State -> State
|
instructionCodeDoCount :: State -> State
|
||||||
instructionCodeDoCount state@(State {_code = (c : cs), _int = (i : is), _exec = es}) =
|
instructionCodeDoCount state@(State {_code = c : cs, _int = i1 : is, _exec = es}) =
|
||||||
if i < 1
|
if i1 < 1
|
||||||
then state
|
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
|
instructionCodeDoCount state = state
|
||||||
|
|
||||||
instructionCodeDoTimes :: State -> State
|
instructionCodeDoTimes :: State -> State
|
||||||
instructionCodeDoTimes state@(State {_code = (c : cs), _int = (i : is), _exec = es}) =
|
instructionCodeDoTimes state@(State {_code = c : cs, _int = i1 : is, _exec = es}) =
|
||||||
if i < 1
|
if i1 < 1
|
||||||
then state
|
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
|
instructionCodeDoTimes state = state
|
||||||
|
|
||||||
instructionCodeIf :: 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
|
instructionCodeIf state = state
|
||||||
|
|
||||||
instructionCodeWhen :: 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
|
instructionCodeWhen state = state
|
||||||
|
|
||||||
instructionCodeMember :: 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
|
instructionCodeMember state = state
|
||||||
|
|
||||||
-- This one doesn't count the recursive Blocks while instructionCodeExtract does
|
-- This one doesn't count the recursive Blocks while instructionCodeExtract does
|
||||||
-- https://erp12.github.io/pyshgp/html/core_instructions.html#code-nth
|
-- https://erp12.github.io/pyshgp/html/core_instructions.html#code-nth
|
||||||
instructionCodeN :: State -> State
|
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
|
if not $ null c1
|
||||||
then state {_code = c1 !! index : cs, _int = is}
|
then state {_code = c1 !! index : cs, _int = is}
|
||||||
else state
|
else state
|
||||||
where
|
where
|
||||||
index :: Int
|
index :: Int
|
||||||
index = abs i1 `mod` length c1
|
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
|
instructionCodeN state = state
|
||||||
|
|
||||||
instructionMakeEmptyCodeBlock :: 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
|
-- I designed this function differently so 0 returns the 0th element, and the last item
|
||||||
-- in the codeblock can be returned.
|
-- in the codeblock can be returned.
|
||||||
instructionCodeExtract :: State -> State
|
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
|
let
|
||||||
index = abs i1 `mod` codeRecursiveSize block
|
index = abs i1 `mod` codeRecursiveSize block
|
||||||
in
|
in
|
||||||
@ -235,7 +235,7 @@ instructionCodeExtract state@(State {_code = cs, _int = _ : is}) = state{_code =
|
|||||||
instructionCodeExtract state = state
|
instructionCodeExtract state = state
|
||||||
|
|
||||||
instructionCodeInsert :: 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
|
let
|
||||||
index = abs i1 `mod` codeRecursiveSize block
|
index = abs i1 `mod` codeRecursiveSize block
|
||||||
in
|
in
|
||||||
|
@ -5,8 +5,8 @@ import HushGP.Instructions.IntInstructions
|
|||||||
import HushGP.Instructions.GenericInstructions
|
import HushGP.Instructions.GenericInstructions
|
||||||
|
|
||||||
instructionExecIf :: State -> State
|
instructionExecIf :: State -> State
|
||||||
instructionExecIf state@(State {_exec = (e1 : e2 : es), _bool = (b : bs)}) =
|
instructionExecIf state@(State {_exec = e1 : e2 : es, _bool = b1 : bs}) =
|
||||||
if b
|
if b1
|
||||||
then state {_exec = e1 : es, _bool = bs}
|
then state {_exec = e1 : es, _bool = bs}
|
||||||
else state {_exec = e2 : es, _bool = bs}
|
else state {_exec = e2 : es, _bool = bs}
|
||||||
instructionExecIf state = state
|
instructionExecIf state = state
|
||||||
@ -54,7 +54,7 @@ execDoRange :: Gene
|
|||||||
execDoRange = StateFunc (instructionExecDoRange, "instructionExecDoRange")
|
execDoRange = StateFunc (instructionExecDoRange, "instructionExecDoRange")
|
||||||
|
|
||||||
instructionExecDoRange :: 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
|
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, execDoRange, e1] : es, _int = i1 : is}
|
||||||
else state {_exec = 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
|
instructionExecDoRange state = state
|
||||||
|
|
||||||
instructionExecDoCount :: State -> State
|
instructionExecDoCount :: State -> State
|
||||||
instructionExecDoCount state@(State {_exec = (e : es), _int = (i : is)}) =
|
instructionExecDoCount state@(State {_exec = e1 : es, _int = i1 : is}) =
|
||||||
if i < 1
|
if i1 < 1
|
||||||
then state
|
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
|
instructionExecDoCount state = state
|
||||||
|
|
||||||
instructionExecDoTimes :: State -> State
|
instructionExecDoTimes :: State -> State
|
||||||
instructionExecDoTimes state@(State {_exec = (e : es), _int = (i : is)}) =
|
instructionExecDoTimes state@(State {_exec = e1 : es, _int = i1 : is}) =
|
||||||
if i < 1
|
if i1 < 1
|
||||||
then state
|
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
|
instructionExecDoTimes state = state
|
||||||
|
|
||||||
execWhile :: Gene
|
execWhile :: Gene
|
||||||
execWhile = StateFunc (instructionExecWhile, "instructionExecWhile")
|
execWhile = StateFunc (instructionExecWhile, "instructionExecWhile")
|
||||||
|
|
||||||
instructionExecWhile :: State -> State
|
instructionExecWhile :: State -> State
|
||||||
instructionExecWhile state@(State {_exec = (_ : es), _bool = []}) =
|
instructionExecWhile state@(State {_exec = _ : es, _bool = []}) =
|
||||||
state {_exec = es}
|
state {_exec = es}
|
||||||
instructionExecWhile state@(State {_exec = alles@(e : es), _bool = (b : bs)}) =
|
instructionExecWhile state@(State {_exec = alles@(e1 : es), _bool = b1 : bs}) =
|
||||||
if b
|
if b1
|
||||||
then state {_exec = e : execWhile : alles, _bool = bs}
|
then state {_exec = e1 : execWhile : alles, _bool = bs}
|
||||||
else state {_exec = es}
|
else state {_exec = es}
|
||||||
instructionExecWhile state = state
|
instructionExecWhile state = state
|
||||||
|
|
||||||
instructionExecDoWhile :: State -> State
|
instructionExecDoWhile :: State -> State
|
||||||
instructionExecDoWhile state@(State {_exec = alles@(e : _)}) =
|
instructionExecDoWhile state@(State {_exec = alles@(e1 : _)}) =
|
||||||
state {_exec = e : execWhile : alles}
|
state {_exec = e1 : execWhile : alles}
|
||||||
instructionExecDoWhile state = state
|
instructionExecDoWhile state = state
|
||||||
|
|
||||||
-- Eats the _boolean no matter what
|
-- Eats the _boolean no matter what
|
||||||
instructionExecWhen :: State -> State
|
instructionExecWhen :: State -> State
|
||||||
instructionExecWhen state@(State {_exec = (_ : es), _bool = (b : bs)}) =
|
instructionExecWhen state@(State {_exec = _ : es, _bool = b1 : bs}) =
|
||||||
if not b
|
if not b1
|
||||||
then state {_exec = es, _bool = bs}
|
then state {_exec = es, _bool = bs}
|
||||||
else state {_bool = bs}
|
else state {_bool = bs}
|
||||||
instructionExecWhen state = state
|
instructionExecWhen state = state
|
||||||
|
@ -6,15 +6,15 @@ import HushGP.State
|
|||||||
import Data.Char
|
import Data.Char
|
||||||
|
|
||||||
instructionFloatFromInt :: State -> State
|
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
|
instructionFloatFromInt state = state
|
||||||
|
|
||||||
instructionFloatFromBool :: 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
|
instructionFloatFromBool state = state
|
||||||
|
|
||||||
instructionFloatFromChar :: 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
|
instructionFloatFromChar state = state
|
||||||
|
|
||||||
instructionFloatFromString :: State -> State
|
instructionFloatFromString :: State -> State
|
||||||
@ -25,39 +25,39 @@ instructionFloatFromString state@(State {_string = s1 : ss, _float = fs}) =
|
|||||||
instructionFloatFromString state = state
|
instructionFloatFromString state = state
|
||||||
|
|
||||||
instructionFloatAdd :: 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
|
instructionFloatAdd state = state
|
||||||
|
|
||||||
instructionFloatSub :: 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
|
instructionFloatSub state = state
|
||||||
|
|
||||||
instructionFloatMul :: 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
|
instructionFloatMul state = state
|
||||||
|
|
||||||
instructionFloatDiv :: 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
|
instructionFloatDiv state = state
|
||||||
|
|
||||||
instructionFloatMod :: 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
|
instructionFloatMod state = state
|
||||||
|
|
||||||
instructionFloatMin :: 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
|
instructionFloatMin state = state
|
||||||
|
|
||||||
instructionFloatMax :: 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
|
instructionFloatMax state = state
|
||||||
|
|
||||||
instructionFloatInc :: 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
|
instructionFloatInc state = state
|
||||||
|
|
||||||
instructionFloatDec :: 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
|
instructionFloatDec state = state
|
||||||
|
|
||||||
instructionFloatLT :: State -> State
|
instructionFloatLT :: State -> State
|
||||||
|
@ -91,7 +91,7 @@ instructionDup :: Lens' State [a] -> State -> State
|
|||||||
instructionDup accessor state =
|
instructionDup accessor state =
|
||||||
case uncons (view accessor state) of
|
case uncons (view accessor state) of
|
||||||
Nothing -> state
|
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 :: Lens' State [a] -> State -> State
|
||||||
instructionPop accessor state = state & accessor .~ drop 1 (view accessor 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))
|
then instructionDupNHelper (count - 1) instruction internalAccessor (internalState & accessor .~ (instruction : view accessor internalState))
|
||||||
else 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
|
-- TODO: Will need to implement a max stack items at some point
|
||||||
instructionDupItems :: Lens' State [a] -> State -> State
|
instructionDupItems :: Lens' State [a] -> State -> State
|
||||||
instructionDupItems accessor state@(State {_int = i1 : is}) =
|
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}
|
instructionStackDepth accessor state@(State {_int = is}) = state{_int = length (view accessor state) : is}
|
||||||
|
|
||||||
instructionYankDup :: Lens' State [a] -> State -> State
|
instructionYankDup :: Lens' State [a] -> State -> State
|
||||||
instructionYankDup accessor state@(State {_int = i : is}) =
|
instructionYankDup accessor state@(State {_int = i1 : is}) =
|
||||||
if notEmptyStack accessor state
|
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
|
else state
|
||||||
instructionYankDup _ state = state
|
instructionYankDup _ state = state
|
||||||
|
|
||||||
instructionYank :: forall a. Lens' State [a] -> 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
|
let
|
||||||
myIndex :: Int
|
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 :: a
|
||||||
item = view accessor state{_int = is} !! myIndex
|
item = view accessor state{_int = is} !! myIndex
|
||||||
deletedState :: State
|
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.
|
-- 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.
|
-- I'm not going to keep this behavior. Check out interpysh examples for how pysh handles it.
|
||||||
instructionShoveDup :: Lens' State [a] -> State -> State
|
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
|
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
|
_ -> state
|
||||||
instructionShoveDup _ state = state
|
instructionShoveDup _ state = state
|
||||||
|
|
||||||
|
@ -6,15 +6,15 @@ import Data.Char
|
|||||||
-- import Debug.Trace
|
-- import Debug.Trace
|
||||||
|
|
||||||
instructionIntFromFloat :: State -> State
|
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
|
instructionIntFromFloat state = state
|
||||||
|
|
||||||
instructionIntFromBool :: 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
|
instructionIntFromBool state = state
|
||||||
|
|
||||||
instructionIntFromChar :: 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
|
instructionIntFromChar state = state
|
||||||
|
|
||||||
instructionIntFromString :: State -> State
|
instructionIntFromString :: State -> State
|
||||||
@ -25,39 +25,39 @@ instructionIntFromString state@(State {_string = s1 : ss, _int = is}) =
|
|||||||
instructionIntFromString state = state
|
instructionIntFromString state = state
|
||||||
|
|
||||||
instructionIntAdd :: 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
|
instructionIntAdd state = state
|
||||||
|
|
||||||
instructionIntSub :: 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
|
instructionIntSub state = state
|
||||||
|
|
||||||
instructionIntMul :: 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
|
instructionIntMul state = state
|
||||||
|
|
||||||
instructionIntDiv :: 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
|
instructionIntDiv state = state
|
||||||
|
|
||||||
instructionIntMod :: 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
|
instructionIntMod state = state
|
||||||
|
|
||||||
instructionIntMin :: 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
|
instructionIntMin state = state
|
||||||
|
|
||||||
instructionIntMax :: 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
|
instructionIntMax state = state
|
||||||
|
|
||||||
instructionIntInc :: 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
|
instructionIntInc state = state
|
||||||
|
|
||||||
instructionIntDec :: 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
|
instructionIntDec state = state
|
||||||
|
|
||||||
instructionIntLT :: State -> State
|
instructionIntLT :: State -> State
|
||||||
|
@ -182,7 +182,7 @@ instructionStringFromLens :: Show a => State -> Lens' State [a] -> State
|
|||||||
instructionStringFromLens state@(State {_string = ss}) accessor =
|
instructionStringFromLens state@(State {_string = ss}) accessor =
|
||||||
case uncons (view accessor state) of
|
case uncons (view accessor state) of
|
||||||
Nothing -> state
|
Nothing -> state
|
||||||
Just (x,_) -> state{_string = show x : ss}
|
Just (x1,_) -> state{_string = show x1 : ss}
|
||||||
|
|
||||||
instructionStringFromBool :: State -> State
|
instructionStringFromBool :: State -> State
|
||||||
instructionStringFromBool state = instructionStringFromLens state bool
|
instructionStringFromBool state = instructionStringFromLens state bool
|
||||||
|
Loading…
x
Reference in New Issue
Block a user