Skip to content

Instantly share code, notes, and snippets.

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

Embed
What would you like to do?
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
You can’t perform that action at this time.