forked from github/semantic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFailure.hs
More file actions
54 lines (42 loc) · 1.53 KB
/
Copy pathFailure.hs
File metadata and controls
54 lines (42 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
{-# LANGUAGE DeriveFoldable, DeriveFunctor, DeriveGeneric, DeriveTraversable, ExistentialQuantification,
FlexibleContexts, KindSignatures, MultiParamTypeClasses, QuantifiedConstraints, RankNTypes,
StandaloneDeriving, TypeOperators #-}
module Language.Python.Failure
( Failure (..)
, unimplemented
, invariantViolated
, eliminateFailures
) where
import Prelude hiding (fail)
import Control.Algebra
import Control.Monad.Fail
import Data.Coerce
import Data.Kind
import GHC.Generics (Generic1)
import Syntax.Foldable
import Syntax.Module
import Syntax.Term
import Syntax.Traversable
data Failure (f :: Type -> Type) a
= Unimplemented String
| InvariantViolated String
deriving Generic1
instance Show (Failure f a) where
show (Unimplemented a) = "unimplemented: " <> a
show (InvariantViolated a) = "invariant violated: " <> a
deriving instance Functor (Failure f)
deriving instance Foldable (Failure f)
deriving instance Traversable (Failure f)
instance HFunctor Failure
instance HFoldable Failure
instance HTraversable Failure
instance RightModule Failure where
a >>=* _ = coerce a
unimplemented :: (Show ast, Has Failure sig m) => ast -> m a
unimplemented = send . Unimplemented . show
invariantViolated :: Has Failure sig m => String -> m a
invariantViolated = send . InvariantViolated
eliminateFailures :: (MonadFail m, HTraversable sig, RightModule sig)
=> Term (Failure :+: sig) a
-> m (Term sig a)
eliminateFailures = Syntax.Term.handle (pure . pure) (fail . show)