Skip to content

Instantly share code, notes, and snippets.

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