Guest User

Untitled

a guest
May 27th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. {-# LANGUAGE DeriveFunctor, DeriveFoldable, DeriveTraversable #-}
  2. {-# LANGUAGE RankNTypes #-}
  3. module Bound.ScopeT where
  4.  
  5. import Bound
  6. import Bound.Name
  7. import Data.Functor.Classes
  8.  
  9. -- |
  10. -- @
  11. -- 'Scope' n f a ~ 'ScopeT' n 'Identity' f a
  12. -- 'ScopeT' n t f a ~ t ('Scope' n f a)
  13. -- @
  14. newtype ScopeT n t f a = ScopeT { unscopeT :: t f (Var n (f a)) }
  15. deriving (Functor, Foldable, Traversable)
  16.  
  17. infixl 1 >>>>=
  18. (>>>>=) :: (Monad f, Functor (t f)) => ScopeT n t f a -> (a -> f b) -> ScopeT n t f b
  19. ScopeT m >>>>= k = ScopeT $ fmap (fmap (>>= k)) m
  20.  
  21. -------------------------------------------------------------------------------
  22. -- Abstraction
  23. -------------------------------------------------------------------------------
  24.  
  25. abstractT :: (Functor (t f), Monad f) => (a -> Maybe n) -> t f a -> ScopeT n t f a
  26. abstractT f e = ScopeT (fmap k e) where
  27. k y = case f y of
  28. Just z -> B z
  29. Nothing -> F (return y)
  30. {-# INLINE abstractT #-}
  31.  
  32. -- | Abstract over a single variable.
  33. --
  34. -- >>> abstract1T 'x' (MaybeT (Nothing : map Just "xyz"))
  35. -- ScopeT (MaybeT [Nothing,Just (B ()),Just (F "y"),Just (F "z")])
  36. abstract1T :: (Functor (t f), Monad f, Eq a) => a -> t f a -> ScopeT () t f a
  37. abstract1T a = abstractT (\b -> if a == b then Just () else Nothing)
  38. {-# INLINE abstract1T #-}
  39.  
  40. -- TODO: abstractEither
  41.  
  42. -- | Abstraction, capturing named bound variables.
  43. abstractTName :: (Functor (t f), Monad f) => (a -> Maybe b) -> t f a -> ScopeT (Name a b) t f a
  44. abstractTName f t = ScopeT (fmap k t) where
  45. k a = case f a of
  46. Just b -> B (Name a b)
  47. Nothing -> F (return a)
  48. {-# INLINE abstractTName #-}
  49.  
  50. -- | Abstract over a single variable
  51. abstract1TName :: (Functor (t f), Monad f, Eq a) => a -> t f a -> ScopeT (Name a ()) t f a
  52. abstract1TName a = abstractTName (\b -> if a == b then Just () else Nothing)
  53. {-# INLINE abstract1TName #-}
  54.  
  55. -------------------------------------------------------------------------------
  56. -- Instantiation
  57. -------------------------------------------------------------------------------
  58.  
  59. instantiateT :: (Bound t, Monad f) => (n -> f a) -> ScopeT n t f a -> t f a
  60. instantiateT k (ScopeT e) = e >>>= \v -> case v of
  61. B b -> k b
  62. F a -> a
  63.  
  64. instantiate1T :: (Bound t, Monad f) => f a -> ScopeT n t f a -> t f a
  65. instantiate1T e = instantiateT (const e)
  66.  
  67. -- TODO: instantiateEitherT
  68.  
  69. -------------------------------------------------------------------------------
  70. -- Traditional de Bruijn
  71. -------------------------------------------------------------------------------
  72.  
  73. fromScopeT :: (Bound t, Monad f) => ScopeT n t f a -> t f (Var n a)
  74. fromScopeT (ScopeT s) = s >>>= \v -> case v of
  75. F e -> fmap F e
  76. B b -> return (B b)
  77.  
  78. toScopeT :: (Functor (t f), Monad f) => t f (Var n a) -> ScopeT n t f a
  79. toScopeT e = ScopeT (fmap (fmap return) e)
  80.  
  81. lowerScopeT
  82. :: Functor (t f)
  83. => (forall x. t f x -> g x)
  84. -> (forall x. f x -> g x)
  85. -> ScopeT n t f a -> Scope n g a
  86. lowerScopeT tf f (ScopeT x) = Scope (tf (fmap (fmap f) x))
  87.  
  88. {-
  89. wrapScope :: (forall x. f x -> t f x) -> Scope n f a -> ScopeT n t f a
  90. wrapScope f (Scope x) = ScopeT (f x)
  91. -}
  92.  
  93. -------------------------------------------------------------------------------
  94. -- Extras
  95. -------------------------------------------------------------------------------
  96.  
  97. -- | Return a list of occurences of the variables bound by this 'Scope'.
  98. bindingsT :: Foldable (t f) => ScopeT b t f a -> [b]
  99. bindingsT (ScopeT s) = foldr f [] s where
  100. f (B v) vs = v : vs
  101. f _ vs = vs
  102. {-# INLINE bindingsT #-}
  103.  
  104. -------------------------------------------------------------------------------
  105. -- Show
  106. -------------------------------------------------------------------------------
  107.  
  108. instance (Show n, Show1 (t f), Show1 f) => Show1 (ScopeT n t f) where
  109. liftShowsPrec sp sl d (ScopeT x) = showsUnaryWith
  110. (liftShowsPrec (liftShowsPrec2 showsPrec undefined (liftShowsPrec sp sl) undefined) undefined)
  111. "ScopeT" d x
  112.  
  113. instance (Show n, Show1 (t f), Show1 f, Show a) => Show (ScopeT n t f a) where
  114. showsPrec = showsPrec1
  115.  
  116. -- $setup
  117. -- >>> import Control.Monad.Trans.Maybe
Add Comment
Please, Sign In to add comment