bf-optimize/src/Language/Brainfuck/Instruction.hs

37 lines
1.1 KiB
Haskell

{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE InstanceSigs #-}
module Language.Brainfuck.Instruction (Instruction(..)) where
import Data.Vector ( Vector )
import Test.QuickCheck.Gen (Gen)
import Test.QuickCheck.Arbitrary (Arbitrary (arbitrary))
import Test.QuickCheck.Instances.Vector ()
import qualified Test.QuickCheck.Gen as Gen
data Instruction
= Increment
| Decrement
| MoveLeft
| MoveRight
| ReadByte
| PutByte
| Loop (Vector Instruction)
deriving stock (Show, Eq)
instance Arbitrary Instruction where
arbitrary :: Gen Instruction
arbitrary = Gen.oneof $
[ pure Increment
, pure Decrement
, pure MoveLeft
, pure MoveRight
, pure ReadByte
, pure PutByte
, Loop <$> reduceSize 8 arbitrary
]
where
reduceSize d g = Gen.sized $ \ s -> Gen.resize (s `div` d) g
-- >>> Gen.generate (Gen.resize 30 arbitrary) :: IO [Instruction]
-- [MoveLeft,Decrement,PutByte,Decrement,Decrement,Increment,ReadByte,MoveLeft,Increment,Loop [],MoveRight,Increment,Increment,Loop [],MoveLeft,Loop [],MoveRight,ReadByte,ReadByte,MoveLeft,Decrement,MoveRight,MoveLeft,Loop [PutByte,Increment,Loop []],Decrement]