Skip to content

Instantly share code, notes, and snippets.

@jtobin jtobin/free.hs
Created Dec 9, 2015

What would you like to do?
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
. decrement
$ sigkill
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.