Skip to content

Commit

Permalink
add runcomp
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeplus64 committed Dec 15, 2012
1 parent fb11a30 commit b726a7c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
2 changes: 1 addition & 1 deletion record.cabal
@@ -1,5 +1,5 @@
name: record name: record
version: 0.1.0.52 version: 1.0.1
synopsis: Efficient, type safe records implemented using GADTs and type level strings. synopsis: Efficient, type safe records implemented using GADTs and type level strings.
homepage: http://quasimal.com/projects/records homepage: http://quasimal.com/projects/records
license: BSD3 license: BSD3
Expand Down
41 changes: 19 additions & 22 deletions src/Data/Record.hs
Expand Up @@ -13,6 +13,7 @@
, OverlappingInstances , OverlappingInstances
, UndecidableInstances , UndecidableInstances
, TemplateHaskell , TemplateHaskell
, ScopedTypeVariables
, ExplicitNamespaces #-} , ExplicitNamespaces #-}


module Data.Record ( key module Data.Record ( key
Expand All @@ -31,9 +32,7 @@ module Data.Record ( key
, alter , alter
, append , append
, Record , Record
, compose , runcomp
, decompose
, (:.)
, P , P
, (:=) , (:=)
, type (++) , type (++)
Expand All @@ -43,6 +42,7 @@ import Language.Haskell.TH.Syntax
import Language.Haskell.TH.Quote import Language.Haskell.TH.Quote
import Language.Haskell.TH.Lib import Language.Haskell.TH.Lib
import Control.Category ((.)) import Control.Category ((.))
import Control.Monad
import Prelude hiding ((.)) import Prelude hiding ((.))


-- | A key of a record. This does not exist at runtime, and as a tradeoff, -- | A key of a record. This does not exist at runtime, and as a tradeoff,
Expand Down Expand Up @@ -79,12 +79,6 @@ type family Wrap (w :: a) x
type instance Wrap (w :: * -> *) x = w x type instance Wrap (w :: * -> *) x = w x
type instance Wrap P x = x type instance Wrap P x = x


-- | Gross
newtype (w :. m) x = Wmx { decompose :: w (m x) }

compose :: (a -> w (m x)) -> a -> (w :. m) x
compose f x = Wmx (f x)

data Record w r where data Record w r where
C :: Wrap w e -> Record w r -> Record w (k := e ': r) C :: Wrap w e -> Record w r -> Record w (k := e ': r)
E :: Record w '[] E :: Record w '[]
Expand Down Expand Up @@ -136,9 +130,11 @@ class Transform r where
transform :: (forall a. (i :: * -> *) a -> (o :: * -> *) a) -> Record i r -> Record o r transform :: (forall a. (i :: * -> *) a -> (o :: * -> *) a) -> Record i r -> Record o r


instance Transform '[] where instance Transform '[] where
{-# INLINE transform #-}
transform _ _ = end transform _ _ = end


instance Transform xs => Transform (x ': xs) where instance Transform xs => Transform (x ': xs) where
{-# INLINE transform #-}
transform f (C x xs) = f x & transform f xs transform f (C x xs) = f x & transform f xs


class Run r where class Run r where
Expand All @@ -150,11 +146,8 @@ class Run r where
instance Run '[] where instance Run '[] where
run _ = return end run _ = return end


instance Run xs => Run (x ': xs) where instance (Run xs) => Run (x ': xs) where
run (C x xs) = do run (C x xs) = liftM2 C x (run xs)
y <- x
ys <- run xs
return (y & ys)


class Runtrans r where class Runtrans r where
-- | A more efficient implementation of @ run . transform f @. -- | A more efficient implementation of @ run . transform f @.
Expand All @@ -168,14 +161,7 @@ instance Runtrans '[] where


instance Runtrans xs => Runtrans (x ': xs) where instance Runtrans xs => Runtrans (x ': xs) where
{-# INLINE runtrans #-} {-# INLINE runtrans #-}
runtrans f (C x xs) = do runtrans f (C x xs) = liftM2 C (f x) (runtrans f xs)
y <- f x
ys <- runtrans f xs
return (y & ys)

{-# RULES "Record/runtrans"
forall (f :: forall a. i a -> o a) (r :: Runtrans r => Record i r).
run (transform f r) = runtrans f r #-}


class Access r k a | r k -> a where class Access r k a | r k -> a where
access :: Key k -> Record w r -> Wrap w a access :: Key k -> Record w r -> Wrap w a
Expand Down Expand Up @@ -228,3 +214,14 @@ instance Append xs ys => Append (x ': xs) ys where
{-# INLINE append #-} {-# INLINE append #-}
append (C x xs) ys = C x (append xs ys) append (C x xs) ys = C x (append xs ys)


class RunComp r where
runcomp :: (Functor m, Monad m) => (forall a. a -> m (w a)) -> Record P r -> m (Record w r)

instance RunComp '[] where
{-# INLINE runcomp #-}
runcomp _ _ = return end

instance RunComp xs => RunComp (x ': xs) where
{-# INLINE runcomp #-}
runcomp f (C x xs) = liftM2 C (f x) (runcomp f xs)

5 changes: 4 additions & 1 deletion src/Example.hs
Expand Up @@ -11,9 +11,12 @@ type Point
op :: Record P Point op :: Record P Point
op = 0 & 0 & 0 & (0,0,0) & end op = 0 & 0 & 0 & (0,0,0) & end


p :: Record (IO :. IORef) Point
p = box (compose newIORef) op

main :: IO () main :: IO ()
main = do main = do
let p = box (compose newIORef) op
print "yes" print "yes"




0 comments on commit b726a7c

Please sign in to comment.