never executed always true always false
1 {-# LANGUAGE DeriveGeneric, DeriveDataTypeable, DeriveFunctor, DeriveTraversable, DeriveFoldable #-}
2
3 module Conjure.Language.Expression.Op.Inverse where
4
5 import Conjure.Prelude
6 import Conjure.Language.Expression.Op.Internal.Common
7
8 import qualified Data.Aeson as JSON -- aeson
9 import qualified Data.Aeson.KeyMap as KM
10
11 import qualified Data.Vector as V -- vector
12
13
14
15
16 data OpInverse x = OpInverse x x
17 deriving (Eq, Ord, Show, Data, Functor, Traversable, Foldable, Typeable, Generic)
18
19 instance Serialize x => Serialize (OpInverse x)
20 instance Hashable x => Hashable (OpInverse x)
21 instance ToJSON x => ToJSON (OpInverse x) where toJSON = genericToJSON jsonOptions
22 instance FromJSON x => FromJSON (OpInverse x) where parseJSON = genericParseJSON jsonOptions
23
24 instance (TypeOf x, Pretty x) => TypeOf (OpInverse x) where
25 typeOf p@(OpInverse f g) = do
26 ft <- typeOf f
27 case ft of
28 TypeFunction fFrom fTo -> do
29 TypeFunction gFrom gTo <- typeOf g
30 if typesUnify [fFrom, gTo] && typesUnify [fTo, gFrom]
31 then return TypeBool
32 else raiseTypeError p
33 TypePermutation fi -> do
34 TypePermutation gi <- typeOf g
35 if typesUnify [fi,gi]
36 then return TypeBool
37 else raiseTypeError p
38 _ -> raiseTypeError p
39
40 instance SimplifyOp OpInverse x where
41 simplifyOp _ = na "simplifyOp{OpInverse}"
42
43 instance Pretty x => Pretty (OpInverse x) where
44 prettyPrec _ (OpInverse a b) = "inverse" <> prettyList prParens "," [a,b]
45
46 instance VarSymBreakingDescription x => VarSymBreakingDescription (OpInverse x) where
47 varSymBreakingDescription (OpInverse a b) = JSON.Object $ KM.fromList
48 [ ("type", JSON.String "OpInverse")
49 , ("children", JSON.Array $ V.fromList
50 [ varSymBreakingDescription a
51 , varSymBreakingDescription b
52 ])
53 ]