never executed always true always false
1 {-# LANGUAGE DeriveGeneric, DeriveDataTypeable, DeriveFunctor, DeriveTraversable, DeriveFoldable #-}
2
3 module Conjure.Language.Expression.Op.ToSet 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 OpToSet x = OpToSet
15 Bool -- True means we can assume there won't be any duplicates
16 x
17 deriving (Eq, Ord, Show, Data, Functor, Traversable, Foldable, Typeable, Generic)
18
19 instance Serialize x => Serialize (OpToSet x)
20 instance Hashable x => Hashable (OpToSet x)
21 instance ToJSON x => ToJSON (OpToSet x) where toJSON = genericToJSON jsonOptions
22 instance FromJSON x => FromJSON (OpToSet x) where parseJSON = genericParseJSON jsonOptions
23
24 instance (TypeOf x, Pretty x) => TypeOf (OpToSet x) where
25 typeOf p@(OpToSet _ x) = do
26 tx <- typeOf x
27 case tx of
28 TypeRelation is -> return (TypeSet (TypeTuple is))
29 TypeMSet i -> return (TypeSet i)
30 TypeFunction i j -> return (TypeSet (TypeTuple [i,j]))
31 TypeMatrix _ i -> return (TypeSet i)
32 TypeList i -> return (TypeSet i)
33 _ -> raiseTypeError $ vcat [ pretty p
34 , "The argument has type:" <+> pretty tx
35 ]
36
37 instance SimplifyOp OpToSet x where
38 simplifyOp _ = na "simplifyOp{OpToSet}"
39
40 instance Pretty x => Pretty (OpToSet x) where
41 prettyPrec _ (OpToSet _ a) = "toSet" <> prParens (pretty a)
42
43 instance VarSymBreakingDescription x => VarSymBreakingDescription (OpToSet x) where
44 varSymBreakingDescription (OpToSet b x) = JSON.Object $ KM.fromList
45 [ ("type", JSON.String "OpToSet")
46 , ("children", JSON.Array $ V.fromList
47 [ toJSON b
48 , varSymBreakingDescription x
49 ])
50 ]