-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Advent of Code 2019 intcode interpreter -- -- Implementation of the Intcode virtual machine as defined by Advent of -- Code https://adventofcode.com/2019. -- -- This implementation provides an efficient, pure implementation of the -- interpreter and exposes multiple levels of abstraction to make it easy -- to use in a variety of situations. @package intcode @version 0.1.0.0 -- | This Intcode interpreter is defined across multiple Advent of Code -- days: -- -- -- -- This implementation works with the following passes: -- --
    --
  1. Parse input text file into a list of numbers
  2. --
  3. Execute op codes to single-step input/output effects.
  4. --
  5. Execute single-stop effects into big-step effects.
  6. --
  7. Optional: Evaluate the effect as a function from a list of inputs -- to list of outputs
  8. --
-- -- Common use modes: -- -- module Intcode -- | Run a given memory image as a list transducer. -- -- Use effectList when you want to provide a specific -- Effect. -- -- Throws: IntcodeFault when machine faults or too few inputs are -- provided. -- --
--   >>> intCodeToList [3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9] <$> [[0],[10]]
--   [[0],[1]]
--   
-- --
--   >>> intCodeToList [3,3,1105,-1,9,1101,0,0,12,4,12,99,1] <$> [[0],[10]]
--   [[0],[1]]
--   
-- --
--   >>> :{
--   
--   >>> intCodeToList
--   
--   >>> [3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,
--   
--   >>> 1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,
--   
--   >>> 999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99]
--   
--   >>> <$> [[7],[8],[9]]
--   
--   >>> :}
--   [[999],[1000],[1001]]
--   
intCodeToList :: [Int] -> [Int] -> [Int] -- | Run intcode program using stdio. -- --
--   >>> runIO (run (new [104,72,104,101,104,108,104,108,104,111,104,33,104,10,99]))
--   Hello!
--   
runIO :: Effect -> IO () -- | Machine state. data Machine Machine :: !Int -> !Int -> !IntMap Int -> {-# UNPACK #-} !PrimArray Int -> Machine -- | program counter [pc] :: Machine -> !Int -- | relative base pointer [relBase] :: Machine -> !Int -- | memory updates [memory] :: Machine -> !IntMap Int -- | initial memory [image] :: Machine -> {-# UNPACK #-} !PrimArray Int -- | Memory lookup from 0-based index. (!) :: Machine -> Int -> Int -- | Construct machine from a list of initial values starting at address 0. -- Program counter and relative base start at 0. new :: [Int] -> Machine -- | Update the value stored at a given location in memory. set :: Int -> Int -> Machine -> Machine -- | Generate a list representation of memory starting from zero. This can -- get big for sparsely filled memory using large addresses. Returned -- values start at position 0. -- --
--   >>> memoryList (set 8 10 (new [1,2,3]))
--   [1,2,3,0,0,0,0,0,10]
--   
memoryList :: Machine -> [Int] -- | Possible effects from running a machine data Effect -- | Output an integer Output :: !Int -> Effect -> Effect -- | Input an integer Input :: (Int -> Effect) -> Effect -- | Halt execution Halt :: Effect -- | Execution failure Fault :: Effect -- | Big-step semantics of virtual machine. -- --
--   >>> run (new [1102,34915192,34915192,7,4,7,99,0])
--   Output 1219070632396864 Halt
--   
-- --
--   >>> run (new [3,1,99])
--   Input <function>
--   
run :: Machine -> Effect -- | Compose two effects together. Outputs from first argument are used as -- inputs to the second effect. Composed effect halts when the second -- machine halts. -- --
--   >>> let mult n = Input (\i -> Output (i*n) Halt)
--   
--   >>> let add  n = Input (\i -> Output (i+n) Halt)
--   
--   >>> effectList (mult 3 >>> add 1) [4]
--   [13]
--   
(>>>) :: Effect -> Effect -> Effect infixl 9 >>> -- | Evaluate a program's effect as a function from a list of inputs to a -- list of outputs. -- -- Throws: IntcodeFault when machine faults or too few inputs are -- provided. effectList :: Effect -> [Int] -> [Int] -- | Run first effect until it halts, then run the second effect. -- --
--   >>> Output 1 Halt `followedBy` Output 2 Halt
--   Output 1 (Output 2 Halt)
--   
followedBy :: Effect -> Effect -> Effect -- | Provide an input to the first occurence of an input request in a -- program effect. It is considered a fault if a program terminates -- before using the input. -- --
--   >>> let mult n = Input (\i -> Output (i*n) Halt)
--   
--   >>> feedInput [6] (mult 5)
--   Output 30 Halt
--   
feedInput :: [Int] -> Effect -> Effect -- | Result of small-step semantics. data Step -- | no effect Step :: !Machine -> Step -- | output StepOut :: !Int -> !Machine -> Step -- | input StepIn :: (Int -> Machine) -> Step -- | halt StepHalt :: Step -- | bad instruction StepFault :: Step -- | Small-step semantics of virtual machine. step :: Machine -> Step -- | Parameter modes data Mode -- | absolute position Abs :: Mode -- | immediate Imm :: Mode -- | relative position Rel :: Mode -- | Opcodes parameterized over argument representations. data Opcode a -- | addition: c = a + b Add :: !a -> !a -> !a -> Opcode a -- | multiplication: c = a * b Mul :: !a -> !a -> !a -> Opcode a -- | input: a = input() Inp :: !a -> Opcode a -- | output: output(a) Out :: !a -> Opcode a -- | jump-if-true: if a then goto b Jnz :: !a -> !a -> Opcode a -- | jump-if-false: if !a then goto b Jz :: !a -> !a -> Opcode a -- | less-than: c = a < b Lt :: !a -> !a -> !a -> Opcode a -- | equals: c = a == b Eq :: !a -> !a -> !a -> Opcode a -- | adjust-rel-base: rel += a Arb :: !a -> Opcode a -- | halt Hlt :: Opcode a -- | Decode an instruction to determine the opcode and parameter modes. -- --
--   >>> decode 1002
--   Just (Mul Abs Imm Abs)
--   
decode :: Int -> Maybe (Opcode Mode) -- | Error when a machine fails to step. data IntcodeFault IntcodeFault :: IntcodeFault instance GHC.Read.Read Intcode.IntcodeFault instance GHC.Show.Show Intcode.IntcodeFault instance GHC.Classes.Ord Intcode.IntcodeFault instance GHC.Classes.Eq Intcode.IntcodeFault instance Data.Foldable.Foldable Intcode.Opcode instance GHC.Base.Functor Intcode.Opcode instance GHC.Show.Show a => GHC.Show.Show (Intcode.Opcode a) instance GHC.Read.Read a => GHC.Read.Read (Intcode.Opcode a) instance GHC.Classes.Ord a => GHC.Classes.Ord (Intcode.Opcode a) instance GHC.Classes.Eq a => GHC.Classes.Eq (Intcode.Opcode a) instance GHC.Show.Show Intcode.Mode instance GHC.Read.Read Intcode.Mode instance GHC.Classes.Ord Intcode.Mode instance GHC.Classes.Eq Intcode.Mode instance GHC.Show.Show Intcode.Step instance GHC.Show.Show Intcode.Effect instance GHC.Show.Show Intcode.Machine instance GHC.Classes.Ord Intcode.Machine instance GHC.Classes.Eq Intcode.Machine instance GHC.Exception.Type.Exception Intcode.IntcodeFault instance Data.Traversable.Traversable Intcode.Opcode