Skip to content

Instantly share code, notes, and snippets.

@jtobin
Created December 9, 2015 18:51
Show Gist options
  • Save jtobin/0a609ae3f4704fafc611 to your computer and use it in GitHub Desktop.
Save jtobin/0a609ae3f4704fafc611 to your computer and use it in GitHub Desktop.
A program defined using 'Free'
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE UndecidableInstances #-}
data Program f a =
Running (f (Program f a))
| Terminated a
deriving instance (Show a, Show (f (Program f a))) => Show (Program f a)
data Instruction r =
Increment r
| Decrement r
| Terminate
deriving (Functor, Show)
increment :: Program Instruction a -> Program Instruction a
increment = Running . Increment
decrement :: Program Instruction a -> Program Instruction a
decrement = Running . Decrement
terminate :: Program Instruction a
terminate = Running Terminate
sigkill :: Program f Int
sigkill = Terminated 1
program :: Program Instruction Int
program =
increment
. increment
. decrement
$ sigkill
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment