module Agda.Syntax.Concrete.Attribute where
import Control.Arrow (second)
import Control.Monad (foldM)
import Data.List (foldl')
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Maybe
import Agda.Syntax.Common
import Agda.Syntax.Concrete (Expr(..))
import Agda.Syntax.Concrete.Pretty ()
import Agda.Syntax.Position
import Agda.Utils.Pretty (prettyShow)
data Attribute
= RelevanceAttribute Relevance
| QuantityAttribute Quantity
| TacticAttribute Expr
| CohesionAttribute Cohesion
deriving (Int -> Attribute -> ShowS
[Attribute] -> ShowS
Attribute -> String
(Int -> Attribute -> ShowS)
-> (Attribute -> String)
-> ([Attribute] -> ShowS)
-> Show Attribute
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Attribute] -> ShowS
$cshowList :: [Attribute] -> ShowS
show :: Attribute -> String
$cshow :: Attribute -> String
showsPrec :: Int -> Attribute -> ShowS
$cshowsPrec :: Int -> Attribute -> ShowS
Show)
instance HasRange Attribute where
getRange :: Attribute -> Range
getRange = \case
RelevanceAttribute r :: Relevance
r -> Relevance -> Range
forall t. HasRange t => t -> Range
getRange Relevance
r
QuantityAttribute q :: Quantity
q -> Quantity -> Range
forall t. HasRange t => t -> Range
getRange Quantity
q
CohesionAttribute c :: Cohesion
c -> Cohesion -> Range
forall t. HasRange t => t -> Range
getRange Cohesion
c
TacticAttribute e :: Expr
e -> Expr -> Range
forall t. HasRange t => t -> Range
getRange Expr
e
instance SetRange Attribute where
setRange :: Range -> Attribute -> Attribute
setRange r :: Range
r = \case
RelevanceAttribute a :: Relevance
a -> Relevance -> Attribute
RelevanceAttribute (Relevance -> Attribute) -> Relevance -> Attribute
forall a b. (a -> b) -> a -> b
$ Range -> Relevance -> Relevance
forall t. SetRange t => Range -> t -> t
setRange Range
r Relevance
a
QuantityAttribute q :: Quantity
q -> Quantity -> Attribute
QuantityAttribute (Quantity -> Attribute) -> Quantity -> Attribute
forall a b. (a -> b) -> a -> b
$ Range -> Quantity -> Quantity
forall t. SetRange t => Range -> t -> t
setRange Range
r Quantity
q
CohesionAttribute c :: Cohesion
c -> Cohesion -> Attribute
CohesionAttribute (Cohesion -> Attribute) -> Cohesion -> Attribute
forall a b. (a -> b) -> a -> b
$ Range -> Cohesion -> Cohesion
forall t. SetRange t => Range -> t -> t
setRange Range
r Cohesion
c
TacticAttribute e :: Expr
e -> Expr -> Attribute
TacticAttribute Expr
e
instance KillRange Attribute where
killRange :: Attribute -> Attribute
killRange = \case
RelevanceAttribute a :: Relevance
a -> Relevance -> Attribute
RelevanceAttribute (Relevance -> Attribute) -> Relevance -> Attribute
forall a b. (a -> b) -> a -> b
$ Relevance -> Relevance
forall a. KillRange a => KillRangeT a
killRange Relevance
a
QuantityAttribute q :: Quantity
q -> Quantity -> Attribute
QuantityAttribute (Quantity -> Attribute) -> Quantity -> Attribute
forall a b. (a -> b) -> a -> b
$ Quantity -> Quantity
forall a. KillRange a => KillRangeT a
killRange Quantity
q
CohesionAttribute c :: Cohesion
c -> Cohesion -> Attribute
CohesionAttribute (Cohesion -> Attribute) -> Cohesion -> Attribute
forall a b. (a -> b) -> a -> b
$ Cohesion -> Cohesion
forall a. KillRange a => KillRangeT a
killRange Cohesion
c
TacticAttribute e :: Expr
e -> Expr -> Attribute
TacticAttribute (Expr -> Attribute) -> Expr -> Attribute
forall a b. (a -> b) -> a -> b
$ KillRangeT Expr
forall a. KillRange a => KillRangeT a
killRange Expr
e
type LensAttribute a = (LensRelevance a, LensQuantity a, LensCohesion a)
relevanceAttributeTable :: [(String, Relevance)]
relevanceAttributeTable :: [(String, Relevance)]
relevanceAttributeTable = [[(String, Relevance)]] -> [(String, Relevance)]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
[ (String -> (String, Relevance))
-> [String] -> [(String, Relevance)]
forall a b. (a -> b) -> [a] -> [b]
map (, Relevance
Irrelevant) [ "irr", "irrelevant" ]
, (String -> (String, Relevance))
-> [String] -> [(String, Relevance)]
forall a b. (a -> b) -> [a] -> [b]
map (, Relevance
NonStrict) [ "shirr", "shape-irrelevant" ]
, (String -> (String, Relevance))
-> [String] -> [(String, Relevance)]
forall a b. (a -> b) -> [a] -> [b]
map (, Relevance
Relevant) [ "relevant" ]
]
quantityAttributeTable :: [(String, Quantity)]
quantityAttributeTable :: [(String, Quantity)]
quantityAttributeTable =
[ ("0" , Q0Origin -> Quantity
Quantity0 (Q0Origin -> Quantity) -> Q0Origin -> Quantity
forall a b. (a -> b) -> a -> b
$ Range -> Q0Origin
Q0 Range
forall a. Range' a
noRange)
, ("erased" , Q0Origin -> Quantity
Quantity0 (Q0Origin -> Quantity) -> Q0Origin -> Quantity
forall a b. (a -> b) -> a -> b
$ Range -> Q0Origin
Q0Erased Range
forall a. Range' a
noRange)
, ("ω" , QωOrigin -> Quantity
Quantityω (QωOrigin -> Quantity) -> QωOrigin -> Quantity
forall a b. (a -> b) -> a -> b
$ Range -> QωOrigin
Qω Range
forall a. Range' a
noRange)
, ("plenty" , QωOrigin -> Quantity
Quantityω (QωOrigin -> Quantity) -> QωOrigin -> Quantity
forall a b. (a -> b) -> a -> b
$ Range -> QωOrigin
QωPlenty Range
forall a. Range' a
noRange)
]
cohesionAttributeTable :: [(String, Cohesion)]
cohesionAttributeTable :: [(String, Cohesion)]
cohesionAttributeTable =
[ ("♭" , Cohesion
Flat)
, ("flat" , Cohesion
Flat)
]
attributesMap :: Map String Attribute
attributesMap :: Map String Attribute
attributesMap = [(String, Attribute)] -> Map String Attribute
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList ([(String, Attribute)] -> Map String Attribute)
-> [(String, Attribute)] -> Map String Attribute
forall a b. (a -> b) -> a -> b
$ [[(String, Attribute)]] -> [(String, Attribute)]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
[ ((String, Relevance) -> (String, Attribute))
-> [(String, Relevance)] -> [(String, Attribute)]
forall a b. (a -> b) -> [a] -> [b]
map ((Relevance -> Attribute)
-> (String, Relevance) -> (String, Attribute)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second Relevance -> Attribute
RelevanceAttribute) [(String, Relevance)]
relevanceAttributeTable
, ((String, Quantity) -> (String, Attribute))
-> [(String, Quantity)] -> [(String, Attribute)]
forall a b. (a -> b) -> [a] -> [b]
map ((Quantity -> Attribute)
-> (String, Quantity) -> (String, Attribute)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second Quantity -> Attribute
QuantityAttribute) [(String, Quantity)]
quantityAttributeTable
, ((String, Cohesion) -> (String, Attribute))
-> [(String, Cohesion)] -> [(String, Attribute)]
forall a b. (a -> b) -> [a] -> [b]
map ((Cohesion -> Attribute)
-> (String, Cohesion) -> (String, Attribute)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second Cohesion -> Attribute
CohesionAttribute) [(String, Cohesion)]
cohesionAttributeTable
]
stringToAttribute :: String -> Maybe Attribute
stringToAttribute :: String -> Maybe Attribute
stringToAttribute = (String -> Map String Attribute -> Maybe Attribute
forall k a. Ord k => k -> Map k a -> Maybe a
`Map.lookup` Map String Attribute
attributesMap)
exprToAttribute :: Expr -> Maybe Attribute
exprToAttribute :: Expr -> Maybe Attribute
exprToAttribute (Paren _ (RawApp _ [Tactic _ t :: Expr
t])) = Attribute -> Maybe Attribute
forall a. a -> Maybe a
Just (Attribute -> Maybe Attribute) -> Attribute -> Maybe Attribute
forall a b. (a -> b) -> a -> b
$ Expr -> Attribute
TacticAttribute Expr
t
exprToAttribute e :: Expr
e = Range -> Maybe Attribute -> Maybe Attribute
forall t. SetRange t => Range -> t -> t
setRange (Expr -> Range
forall t. HasRange t => t -> Range
getRange Expr
e) (Maybe Attribute -> Maybe Attribute)
-> Maybe Attribute -> Maybe Attribute
forall a b. (a -> b) -> a -> b
$ String -> Maybe Attribute
stringToAttribute (String -> Maybe Attribute) -> String -> Maybe Attribute
forall a b. (a -> b) -> a -> b
$ Expr -> String
forall a. Pretty a => a -> String
prettyShow Expr
e
setAttribute :: (LensAttribute a) => Attribute -> a -> a
setAttribute :: Attribute -> a -> a
setAttribute = \case
RelevanceAttribute r :: Relevance
r -> Relevance -> a -> a
forall a. LensRelevance a => Relevance -> a -> a
setRelevance Relevance
r
QuantityAttribute q :: Quantity
q -> Quantity -> a -> a
forall a. LensQuantity a => Quantity -> a -> a
setQuantity Quantity
q
CohesionAttribute c :: Cohesion
c -> Cohesion -> a -> a
forall a. LensCohesion a => Cohesion -> a -> a
setCohesion Cohesion
c
TacticAttribute t :: Expr
t -> a -> a
forall a. a -> a
id
setAttributes :: (LensAttribute a) => [Attribute] -> a -> a
setAttributes :: [Attribute] -> a -> a
setAttributes attrs :: [Attribute]
attrs arg :: a
arg = (a -> Attribute -> a) -> a -> [Attribute] -> a
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' ((Attribute -> a -> a) -> a -> Attribute -> a
forall a b c. (a -> b -> c) -> b -> a -> c
flip Attribute -> a -> a
forall a. LensAttribute a => Attribute -> a -> a
setAttribute) a
arg [Attribute]
attrs
setPristineRelevance :: (LensRelevance a) => Relevance -> a -> Maybe a
setPristineRelevance :: Relevance -> a -> Maybe a
setPristineRelevance r :: Relevance
r a :: a
a
| a -> Relevance
forall a. LensRelevance a => a -> Relevance
getRelevance a
a Relevance -> Relevance -> Bool
forall a. Eq a => a -> a -> Bool
== Relevance
defaultRelevance = a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> a -> Maybe a
forall a b. (a -> b) -> a -> b
$ Relevance -> a -> a
forall a. LensRelevance a => Relevance -> a -> a
setRelevance Relevance
r a
a
| Bool
otherwise = Maybe a
forall a. Maybe a
Nothing
setPristineQuantity :: (LensQuantity a) => Quantity -> a -> Maybe a
setPristineQuantity :: Quantity -> a -> Maybe a
setPristineQuantity q :: Quantity
q a :: a
a
| a -> Bool
forall a. LensQuantity a => a -> Bool
noUserQuantity a
a = a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> a -> Maybe a
forall a b. (a -> b) -> a -> b
$ Quantity -> a -> a
forall a. LensQuantity a => Quantity -> a -> a
setQuantity Quantity
q a
a
| Bool
otherwise = Maybe a
forall a. Maybe a
Nothing
setPristineCohesion :: (LensCohesion a) => Cohesion -> a -> Maybe a
setPristineCohesion :: Cohesion -> a -> Maybe a
setPristineCohesion c :: Cohesion
c a :: a
a
| a -> Cohesion
forall a. LensCohesion a => a -> Cohesion
getCohesion a
a Cohesion -> Cohesion -> Bool
forall a. Eq a => a -> a -> Bool
== Cohesion
defaultCohesion = a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> a -> Maybe a
forall a b. (a -> b) -> a -> b
$ Cohesion -> a -> a
forall a. LensCohesion a => Cohesion -> a -> a
setCohesion Cohesion
c a
a
| Bool
otherwise = Maybe a
forall a. Maybe a
Nothing
setPristineAttribute :: (LensAttribute a) => Attribute -> a -> Maybe a
setPristineAttribute :: Attribute -> a -> Maybe a
setPristineAttribute = \case
RelevanceAttribute r :: Relevance
r -> Relevance -> a -> Maybe a
forall a. LensRelevance a => Relevance -> a -> Maybe a
setPristineRelevance Relevance
r
QuantityAttribute q :: Quantity
q -> Quantity -> a -> Maybe a
forall a. LensQuantity a => Quantity -> a -> Maybe a
setPristineQuantity Quantity
q
CohesionAttribute c :: Cohesion
c -> Cohesion -> a -> Maybe a
forall a. LensCohesion a => Cohesion -> a -> Maybe a
setPristineCohesion Cohesion
c
TacticAttribute{} -> a -> Maybe a
forall a. a -> Maybe a
Just
setPristineAttributes :: (LensAttribute a) => [Attribute] -> a -> Maybe a
setPristineAttributes :: [Attribute] -> a -> Maybe a
setPristineAttributes attrs :: [Attribute]
attrs arg :: a
arg = (a -> Attribute -> Maybe a) -> a -> [Attribute] -> Maybe a
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM ((Attribute -> a -> Maybe a) -> a -> Attribute -> Maybe a
forall a b c. (a -> b -> c) -> b -> a -> c
flip Attribute -> a -> Maybe a
forall a. LensAttribute a => Attribute -> a -> Maybe a
setPristineAttribute) a
arg [Attribute]
attrs
isRelevanceAttribute :: Attribute -> Maybe Relevance
isRelevanceAttribute :: Attribute -> Maybe Relevance
isRelevanceAttribute = \case
RelevanceAttribute q :: Relevance
q -> Relevance -> Maybe Relevance
forall a. a -> Maybe a
Just Relevance
q
_ -> Maybe Relevance
forall a. Maybe a
Nothing
isQuantityAttribute :: Attribute -> Maybe Quantity
isQuantityAttribute :: Attribute -> Maybe Quantity
isQuantityAttribute = \case
QuantityAttribute q :: Quantity
q -> Quantity -> Maybe Quantity
forall a. a -> Maybe a
Just Quantity
q
_ -> Maybe Quantity
forall a. Maybe a
Nothing
isTacticAttribute :: Attribute -> Maybe Expr
isTacticAttribute :: Attribute -> Maybe Expr
isTacticAttribute (TacticAttribute t :: Expr
t) = Expr -> Maybe Expr
forall a. a -> Maybe a
Just Expr
t
isTacticAttribute _ = Maybe Expr
forall a. Maybe a
Nothing
relevanceAttributes :: [Attribute] -> [Attribute]
relevanceAttributes :: [Attribute] -> [Attribute]
relevanceAttributes = (Attribute -> Bool) -> [Attribute] -> [Attribute]
forall a. (a -> Bool) -> [a] -> [a]
filter ((Attribute -> Bool) -> [Attribute] -> [Attribute])
-> (Attribute -> Bool) -> [Attribute] -> [Attribute]
forall a b. (a -> b) -> a -> b
$ Maybe Relevance -> Bool
forall a. Maybe a -> Bool
isJust (Maybe Relevance -> Bool)
-> (Attribute -> Maybe Relevance) -> Attribute -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Attribute -> Maybe Relevance
isRelevanceAttribute
quantityAttributes :: [Attribute] -> [Attribute]
quantityAttributes :: [Attribute] -> [Attribute]
quantityAttributes = (Attribute -> Bool) -> [Attribute] -> [Attribute]
forall a. (a -> Bool) -> [a] -> [a]
filter ((Attribute -> Bool) -> [Attribute] -> [Attribute])
-> (Attribute -> Bool) -> [Attribute] -> [Attribute]
forall a b. (a -> b) -> a -> b
$ Maybe Quantity -> Bool
forall a. Maybe a -> Bool
isJust (Maybe Quantity -> Bool)
-> (Attribute -> Maybe Quantity) -> Attribute -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Attribute -> Maybe Quantity
isQuantityAttribute
tacticAttributes :: [Attribute] -> [Attribute]
tacticAttributes :: [Attribute] -> [Attribute]
tacticAttributes = (Attribute -> Bool) -> [Attribute] -> [Attribute]
forall a. (a -> Bool) -> [a] -> [a]
filter ((Attribute -> Bool) -> [Attribute] -> [Attribute])
-> (Attribute -> Bool) -> [Attribute] -> [Attribute]
forall a b. (a -> b) -> a -> b
$ Maybe Expr -> Bool
forall a. Maybe a -> Bool
isJust (Maybe Expr -> Bool)
-> (Attribute -> Maybe Expr) -> Attribute -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Attribute -> Maybe Expr
isTacticAttribute