{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveLift #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-}
module Features.Attributes
( HasAttributes(..)
, Attributes(..)
, Role(..)
, Purpose(..)
, emptyAttributes
, basicAttributes
, emptyPurpose
, setAttributes
, setAttributesEmpty
, setManyAttributes
) where
import Data.Text (Text)
import GHC.Generics (Generic)
import GHC.TypeLits (KnownSymbol)
import Language.Haskell.TH hiding (Role)
import Language.Haskell.TH.Syntax hiding (Role)
data Role =
Outcome
| Censoring
| Covariate
| Exposure
| Competing
| Weight
| Intermediate
| Unspecified
deriving (Role -> Role -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Role -> Role -> Bool
$c/= :: Role -> Role -> Bool
== :: Role -> Role -> Bool
$c== :: Role -> Role -> Bool
Eq, Eq Role
Role -> Role -> Bool
Role -> Role -> Ordering
Role -> Role -> Role
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Role -> Role -> Role
$cmin :: Role -> Role -> Role
max :: Role -> Role -> Role
$cmax :: Role -> Role -> Role
>= :: Role -> Role -> Bool
$c>= :: Role -> Role -> Bool
> :: Role -> Role -> Bool
$c> :: Role -> Role -> Bool
<= :: Role -> Role -> Bool
$c<= :: Role -> Role -> Bool
< :: Role -> Role -> Bool
$c< :: Role -> Role -> Bool
compare :: Role -> Role -> Ordering
$ccompare :: Role -> Role -> Ordering
Ord, Int -> Role -> ShowS
[Role] -> ShowS
Role -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Role] -> ShowS
$cshowList :: [Role] -> ShowS
show :: Role -> String
$cshow :: Role -> String
showsPrec :: Int -> Role -> ShowS
$cshowsPrec :: Int -> Role -> ShowS
Show, forall x. Rep Role x -> Role
forall x. Role -> Rep Role x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Role x -> Role
$cfrom :: forall x. Role -> Rep Role x
Generic, forall t.
(forall (m :: * -> *). Quote m => t -> m Exp)
-> (forall (m :: * -> *). Quote m => t -> Code m t) -> Lift t
forall (m :: * -> *). Quote m => Role -> m Exp
forall (m :: * -> *). Quote m => Role -> Code m Role
liftTyped :: forall (m :: * -> *). Quote m => Role -> Code m Role
$cliftTyped :: forall (m :: * -> *). Quote m => Role -> Code m Role
lift :: forall (m :: * -> *). Quote m => Role -> m Exp
$clift :: forall (m :: * -> *). Quote m => Role -> m Exp
Lift)
data Purpose = MkPurpose
{ Purpose -> [Role]
getRole :: [Role]
, Purpose -> [Text]
getTags :: [Text]
}
deriving (Purpose -> Purpose -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Purpose -> Purpose -> Bool
$c/= :: Purpose -> Purpose -> Bool
== :: Purpose -> Purpose -> Bool
$c== :: Purpose -> Purpose -> Bool
Eq, Int -> Purpose -> ShowS
[Purpose] -> ShowS
Purpose -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Purpose] -> ShowS
$cshowList :: [Purpose] -> ShowS
show :: Purpose -> String
$cshow :: Purpose -> String
showsPrec :: Int -> Purpose -> ShowS
$cshowsPrec :: Int -> Purpose -> ShowS
Show, forall x. Rep Purpose x -> Purpose
forall x. Purpose -> Rep Purpose x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Purpose x -> Purpose
$cfrom :: forall x. Purpose -> Rep Purpose x
Generic, forall t.
(forall (m :: * -> *). Quote m => t -> m Exp)
-> (forall (m :: * -> *). Quote m => t -> Code m t) -> Lift t
forall (m :: * -> *). Quote m => Purpose -> m Exp
forall (m :: * -> *). Quote m => Purpose -> Code m Purpose
liftTyped :: forall (m :: * -> *). Quote m => Purpose -> Code m Purpose
$cliftTyped :: forall (m :: * -> *). Quote m => Purpose -> Code m Purpose
lift :: forall (m :: * -> *). Quote m => Purpose -> m Exp
$clift :: forall (m :: * -> *). Quote m => Purpose -> m Exp
Lift)
data Attributes = MkAttributes
{
Attributes -> Text
getShortLabel :: Text
, Attributes -> Text
getLongLabel :: Text
, Attributes -> Text
getDerivation :: Text
, Attributes -> Purpose
getPurpose :: Purpose
}
deriving (Attributes -> Attributes -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Attributes -> Attributes -> Bool
$c/= :: Attributes -> Attributes -> Bool
== :: Attributes -> Attributes -> Bool
$c== :: Attributes -> Attributes -> Bool
Eq, Int -> Attributes -> ShowS
[Attributes] -> ShowS
Attributes -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Attributes] -> ShowS
$cshowList :: [Attributes] -> ShowS
show :: Attributes -> String
$cshow :: Attributes -> String
showsPrec :: Int -> Attributes -> ShowS
$cshowsPrec :: Int -> Attributes -> ShowS
Show, forall x. Rep Attributes x -> Attributes
forall x. Attributes -> Rep Attributes x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Attributes x -> Attributes
$cfrom :: forall x. Attributes -> Rep Attributes x
Generic, forall t.
(forall (m :: * -> *). Quote m => t -> m Exp)
-> (forall (m :: * -> *). Quote m => t -> Code m t) -> Lift t
forall (m :: * -> *). Quote m => Attributes -> m Exp
forall (m :: * -> *). Quote m => Attributes -> Code m Attributes
liftTyped :: forall (m :: * -> *). Quote m => Attributes -> Code m Attributes
$cliftTyped :: forall (m :: * -> *). Quote m => Attributes -> Code m Attributes
lift :: forall (m :: * -> *). Quote m => Attributes -> m Exp
$clift :: forall (m :: * -> *). Quote m => Attributes -> m Exp
Lift)
emptyPurpose :: Purpose
emptyPurpose :: Purpose
emptyPurpose = [Role] -> [Text] -> Purpose
MkPurpose forall a. Monoid a => a
mempty forall a. Monoid a => a
mempty
emptyAttributes :: Attributes
emptyAttributes :: Attributes
emptyAttributes = Text -> Text -> Text -> Purpose -> Attributes
MkAttributes Text
"" Text
"" Text
"" Purpose
emptyPurpose
basicAttributes
:: Text
-> Text
-> [Role]
-> [Text]
-> Attributes
basicAttributes :: Text -> Text -> [Role] -> [Text] -> Attributes
basicAttributes Text
sl Text
ll [Role]
roles [Text]
tags = Text -> Text -> Text -> Purpose -> Attributes
MkAttributes Text
sl Text
ll Text
"" ([Role] -> [Text] -> Purpose
MkPurpose [Role]
roles [Text]
tags)
class (KnownSymbol name) => HasAttributes name d | name -> d where
getAttributes :: forall name . Attributes
getAttributes = Attributes
emptyAttributes
setAttributes
:: Attributes
-> String
-> Name
-> Q [Dec]
setAttributes :: Attributes -> String -> Name -> Q [Dec]
setAttributes Attributes
attrs String
name Name
ty = [d|
instance HasAttributes $a $b where
getAttributes = attrs
|]
where
a :: Q Type
a = forall (m :: * -> *). Quote m => m TyLit -> m Type
litT ( forall (m :: * -> *). Quote m => String -> m TyLit
strTyLit String
name )
b :: Q Type
b = forall (m :: * -> *). Quote m => Name -> m Type
conT Name
ty
setAttributesEmpty :: String -> Name -> Q [Dec]
setAttributesEmpty :: String -> Name -> Q [Dec]
setAttributesEmpty = Attributes -> String -> Name -> Q [Dec]
setAttributes Attributes
emptyAttributes
setManyAttributes :: [(String, Name, Attributes)] -> Q [Dec]
setManyAttributes :: [(String, Name, Attributes)] -> Q [Dec]
setManyAttributes [(String, Name, Attributes)]
x = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat (forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse (String, Name, Attributes) -> Q [Dec]
f [(String, Name, Attributes)]
x)
where f :: (String, Name, Attributes) -> Q [Dec]
f (String
name, Name
typ, Attributes
attrs) = Attributes -> String -> Name -> Q [Dec]
setAttributes Attributes
attrs String
name Name
typ