Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. {-# LANGUAGE RebindableSyntax #-}
  2. {-# LANGUAGE AllowAmbiguousTypes #-}
  3. {-# LANGUAGE UndecidableInstances #-}
  4.  
  5.  
  6. {-|
  7. Module : Control.Monad.Lang.Types
  8. Description : Datatypes and instances we use when implementing a LangT
  9. Copyright : (c) Rohit Ramesh, 2018
  10. License : GPL-2
  11. Maintainer : rkr@berkley.edu
  12. Stability : experimental
  13. Portability : POSIX
  14. -}
  15.  
  16. module Control.Monad.Analysis.Class where
  17.  
  18. import EDGPrelude
  19. import Data.Functor.Foldable
  20. import Control.Monad.Error.Class
  21. import Control.Unification
  22. import Data.Dynamic
  23.  
  24. class MonadAnalyse al k m where
  25. type AnalysisKey al k :: *
  26.  
  27. -- | Retrieve a closed analysis result for a term in keyspace `k`.
  28. analyseWith :: k -> AnalysisKey al k -> m (Maybe al)
  29.  
  30. -- | This lets you retrieve a partially bound term, with values that either
  31. -- refer to other elements in keyspace `k` or arbitrarily assigned integers
  32. -- that refer to free terms that aren't found in `k`.
  33. partiallyAnalyseWith :: k -> AnalysisKey al k -> m (UTerm (Base al) (Either k Int))
  34.  
  35. type KeyConstraints al k = (Show (AnalysisKey al k), Typeable al)
  36.  
  37. class (Show k) => AnalysisAccessErr k e where
  38. unexpectedAnalysisTermFound :: (KeyConstraints al k) => AnalysisKey al k -> e
  39. unexpectedAnalysisFound :: (KeyConstraints al k) => AnalysisKey al k -> e
  40. expectedAnalysisTermMissing :: (KeyConstraints al k) => AnalysisKey al k -> e
  41. expectedAnalysisMissing :: (KeyConstraints al k) => AnalysisKey al k -> e
  42. incorrectAnalysisType :: (KeyConstraints al k) => AnalysisKey al k -> Dynamic -> e
  43.  
  44. type ThrowConstraints e al k m = (KeyConstraints al k, AnalysisAccessErr k e, MonadError e m)
  45.  
  46. throwUnexpectedAnalysisTermFound :: forall e k al m a.
  47. (ThrowConstraints e al k m) => AnalysisKey al k -> m a
  48. throwUnexpectedAnalysisTermFound
  49. = throwError @e . unexpectedAnalysisTermFound @k @e @al
  50.  
  51. throwUnexpectedAnalysisFound :: forall e k al m a.
  52. (ThrowConstraints e al k m) => AnalysisKey al k -> m a
  53. throwUnexpectedAnalysisFound
  54. = throwError @e . unexpectedAnalysisFound @k @e @al
  55.  
  56. throwExpectedAnalysisTermMissing :: forall e k al m a.
  57. (ThrowConstraints e al k m) => AnalysisKey al k -> m a
  58. throwExpectedAnalysisTermMissing
  59. = throwError @e . expectedAnalysisTermMissing @k @e @al
  60.  
  61. throwExpectedAnalysisMissing :: forall e k al m a.
  62. (ThrowConstraints e al k m) => AnalysisKey al k -> m a
  63. throwExpectedAnalysisMissing
  64. = throwError @e . expectedAnalysisMissing @k @e @al
  65.  
  66. throwIncorrectAnalysisType :: forall e k al m a.
  67. (ThrowConstraints e al k m) => AnalysisKey al k -> Dynamic -> m a
  68. throwIncorrectAnalysisType k
  69. = throwError @e . incorrectAnalysisType @k @e @al k
  70.  
  71. instance (Show k) => AnalysisAccessErr k String where
  72. unexpectedAnalysisFound k = "Unexpected Analysis Found : " ++ show k
  73. unexpectedAnalysisTermFound k = "Unexpected Analysis Term Found : " ++ show k
  74. expectedAnalysisMissing k = "Expected Analysis Missing : " ++ show k
  75. expectedAnalysisTermMissing k = "Excepted Analysis Term Missing : " ++ show k
  76.  
  77. incorrectAnalysisType :: forall al. (KeyConstraints al k) => AnalysisKey al k -> Dynamic -> String
  78. incorrectAnalysisType (k :: AnalysisKey al k) d = "Analysis at " ++ show (k :: AnalysisKey al k)
  79. ++ " expected language of type "
  80. ++ (show . typeRep $ (Proxy :: Proxy al))
  81. ++ " instead found " ++ show d
  82.  
  83.  
  84. instance (Show k) => AnalysisAccessErr k Void where
  85.  
  86. unexpectedAnalysisTermFound :: (KeyConstraints al k) => AnalysisKey al k -> Void
  87. unexpectedAnalysisFound = error . unexpectedAnalysisFound
  88.  
  89. unexpectedAnalysisFound :: (KeyConstraints al k) => AnalysisKey al k -> Void
  90. unexpectedAnalysisTermFound = error . unexpectedAnalysisFound
  91.  
  92. expectedAnalysisMissing :: (KeyConstraints al k) => AnalysisKey al k -> Void
  93. expectedAnalysisMissing = error . expectedAnalysisMissing
  94.  
  95. expectedAnalysisTermMissing :: (KeyConstraints al k) => AnalysisKey al k -> Void
  96. expectedAnalysisTermMissing = error . expectedAnalysisTermMissing
  97.  
  98. incorrectAnalysisType :: forall al. (KeyConstraints al k) => AnalysisKey al k -> Dynamic -> Void
  99. incorrectAnalysisType k d = error $ incorrectAnalysisType @k @String @al k d
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement