never executed always true always false
1 {-# LANGUAGE DeriveGeneric, DeriveDataTypeable, DeriveFunctor, DeriveTraversable, DeriveFoldable #-}
2
3 module Conjure.Language.Expression.Op.AtLeast 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 OpAtLeast x = OpAtLeast x -- X: variables
15 x -- o[i] is a lower bound for the nb of occurrences of v[i] in X, indexed by T
16 x -- v[i] values, indexed by T
17 deriving (Eq, Ord, Show, Data, Functor, Traversable, Foldable, Typeable, Generic)
18
19 instance Serialize x => Serialize (OpAtLeast x)
20 instance Hashable x => Hashable (OpAtLeast x)
21 instance ToJSON x => ToJSON (OpAtLeast x) where toJSON = genericToJSON jsonOptions
22 instance FromJSON x => FromJSON (OpAtLeast x) where parseJSON = genericParseJSON jsonOptions
23
24 instance (TypeOf x, Pretty x) => TypeOf (OpAtLeast x) where
25 typeOf p@(OpAtLeast vars occs vals) = do
26 tyVars <- typeOf vars
27 tyOccs <- typeOf occs
28 tyVals <- typeOf vals
29 let
30 tyError = raiseTypeError $ vcat [ pretty p
31 , pretty vars <+> "has type" <+> pretty tyVars
32 , pretty occs <+> "has type" <+> pretty tyOccs
33 , pretty vals <+> "has type" <+> pretty tyVals
34 ]
35 listLike (TypeList TypeInt{}) = return ()
36 listLike (TypeMatrix _ TypeInt{}) = return ()
37 listLike _ = tyError
38 listLike tyVars
39 listLike tyVals
40 listLike tyOccs
41 -- TODO: check the indices and the category of vals&occs
42 return TypeBool
43
44 instance SimplifyOp OpAtLeast x where
45 simplifyOp _ = na "simplifyOp{OpAtLeast}"
46
47 instance Pretty x => Pretty (OpAtLeast x) where
48 prettyPrec _ (OpAtLeast a b c) = "atleast" <> prettyList prParens "," [a, b, c]
49
50 instance (VarSymBreakingDescription x, ExpressionLike x) => VarSymBreakingDescription (OpAtLeast x) where
51 varSymBreakingDescription (OpAtLeast a b c) = JSON.Object $ KM.fromList
52 [ ("type", JSON.String "OpAtLeast")
53 , ("children", JSON.Array $ V.fromList [ varSymBreakingDescription a
54 , varSymBreakingDescription b
55 , varSymBreakingDescription c
56 ])
57 , ("symmetricChildren", JSON.Bool True)
58 ]