Contact/support | Changelog

indexed kleene store

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{-# LANGUAGE FlexibleInstances #-}
import Control.Applicative

data Kleene c d a
  = Done a
  | More (Kleene c d (d -> a)) c

instance Functor (Kleene c d) where
  fmap f (Done a) = Done (f a)
  fmap f (More v b) = More (fmap (f .) v)  b

instance Applicative (Kleene c d) where
  pure = Done
  Done f <*> m = fmap f m
  More z a <*> m = More (flip <$> z <*> m) a

-- extract from the indexed kleene store
unkleene :: Kleene c c a -> a
unkleene (Done a) = a
unkleene (More z a) = unkleene z a

dup :: Kleene c e a -> Kleene c d (Kleene d e a)
dup (Done b) = Done (Done b)
dup (More z a) = More (More <$> dup z) a