never executed always true always false
1 {-# LANGUAGE DeriveGeneric, DeriveDataTypeable, DeriveFunctor, DeriveTraversable, DeriveFoldable #-}
2
3 module Conjure.Language.Expression.Op.Image 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 data OpImage x = OpImage x x
15 deriving (Eq, Ord, Show, Data, Functor, Traversable, Foldable, Typeable, Generic)
16
17 instance Serialize x => Serialize (OpImage x)
18 instance Hashable x => Hashable (OpImage x)
19 instance ToJSON x => ToJSON (OpImage x) where toJSON = genericToJSON jsonOptions
20 instance FromJSON x => FromJSON (OpImage x) where parseJSON = genericParseJSON jsonOptions
21
22 instance (TypeOf x, Pretty x) => TypeOf (OpImage x) where
23 typeOf p@(OpImage f x) = do
24 tyF <- typeOf f
25 tyX <- typeOf x
26 (from, to) <- case tyF of
27 TypeFunction from to -> return (from, to)
28 TypeSequence to -> return (TypeInt TagInt, to)
29 TypePermutation _ -> return (tyX, tyX)
30 _ -> raiseTypeError $ "(function application)" <+> pretty p
31 if typesUnify [tyX, from]
32 then return to
33 else raiseTypeError $ vcat
34 [ pretty p
35 , "function :" <+> pretty f
36 , "function type:" <+> pretty (TypeFunction from to)
37 , "argument :" <+> pretty x
38 , "argument type:" <+> pretty tyX
39 ]
40
41 instance SimplifyOp OpImage x where
42 simplifyOp _ = na "simplifyOp{OpImage}"
43
44 instance Pretty x => Pretty (OpImage x) where
45 prettyPrec _ (OpImage a b) = "image" <> prettyList prParens "," [a,b]
46
47 instance VarSymBreakingDescription x => VarSymBreakingDescription (OpImage x) where
48 varSymBreakingDescription (OpImage a b) = JSON.Object $ KM.fromList
49 [ ("type", JSON.String "OpImage")
50 , ("children", JSON.Array $ V.fromList
51 [ varSymBreakingDescription a
52 , varSymBreakingDescription b
53 ])
54 ]