1
{-# LANGUAGE TupleSections, PatternGuards #-}
1
{-# LANGUAGE NoMonomorphismRestriction #-}
2
{-# LANGUAGE PatternGuards #-}
3
{-# LANGUAGE TupleSections #-}
3
5
module Agda.Syntax.Abstract.Views where
5
7
import Control.Applicative
6
8
import Control.Arrow (first)
7
9
import Control.Monad.Identity
11
import Data.Foldable (foldMap)
8
13
import Data.Traversable
10
15
import Agda.Syntax.Position
33
38
-- | Gather top-level 'AsP'atterns to expose underlying pattern.
34
39
asView :: A.Pattern -> ([Name], A.Pattern)
35
40
asView (A.AsP _ x p) = first (x :) $ asView p
38
43
-- | Check whether we are dealing with a universe.
39
44
isSet :: Expr -> Bool
56
61
-- | Apply an expression rewriting to every subexpression, inside-out.
57
62
-- See 'Agda.Syntax.Internal.Generic'
58
63
class ExprLike a where
64
foldExpr :: Monoid m => (Expr -> m) -> a -> m
59
65
traverseExpr :: (Monad m, Applicative m) => (Expr -> m Expr) -> a -> m a
60
66
mapExpr :: (Expr -> Expr) -> (a -> a)
61
67
mapExpr f e = runIdentity $ traverseExpr (Identity . f) e
63
69
instance ExprLike Expr where
80
App _ e e' -> m `mappend` fold e `mappend` fold e'
81
WithApp _ e es -> m `mappend` fold e `mappend` fold es
82
Lam _ b e -> m `mappend` fold b `mappend` fold e
84
ExtendedLam _ _ _ cs -> m `mappend` fold cs
85
Pi _ tel e -> m `mappend` fold tel `mappend` fold e
86
Fun _ e e' -> m `mappend` fold e `mappend` fold e'
89
Let _ bs e -> m `mappend` fold bs `mappend` fold e
90
ETel tel -> m `mappend` fold tel
91
Rec _ as -> m `mappend` fold as
92
RecUpdate _ e as -> m `mappend` fold e `mappend` fold as
93
ScopedExpr _ e -> m `mappend` fold e
94
QuoteGoal _ _ e -> m `mappend` fold e
95
QuoteContext _ _ e -> m `mappend` fold e
99
DontCare e -> m `mappend` fold e
64
104
traverseExpr f e = do
65
105
let trav e = traverseExpr f e
71
112
QuestionMark{} -> f e
95
136
-- | TODO: currently does not go into colors.
96
137
instance ExprLike a => ExprLike (Common.Arg c a) where
138
foldExpr = foldMap . foldExpr
97
139
traverseExpr = traverse . traverseExpr
99
141
instance ExprLike a => ExprLike (Named x a) where
142
foldExpr = foldMap . foldExpr
100
143
traverseExpr = traverse . traverseExpr
102
145
instance ExprLike a => ExprLike [a] where
146
foldExpr = foldMap . foldExpr
103
147
traverseExpr = traverse . traverseExpr
105
149
instance ExprLike a => ExprLike (x, a) where
150
foldExpr f (x, e) = foldExpr f e
106
151
traverseExpr f (x, e) = (x,) <$> traverseExpr f e
108
153
instance ExprLike LamBinding where
156
DomainFree{} -> mempty
157
DomainFull bs -> foldExpr f bs
109
158
traverseExpr f e =
111
160
DomainFree{} -> return e
112
161
DomainFull bs -> DomainFull <$> traverseExpr f bs
114
163
instance ExprLike TypedBindings where
164
foldExpr f (TypedBindings r b) = foldExpr f b
115
165
traverseExpr f (TypedBindings r b) = TypedBindings r <$> traverseExpr f b
117
167
instance ExprLike TypedBinding where
170
TBind _ _ e -> foldExpr f e
171
TLet _ ds -> foldExpr f ds
118
172
traverseExpr f e =
120
174
TBind r xs e -> TBind r xs <$> traverseExpr f e
121
175
TLet r ds -> TLet r <$> traverseExpr f ds
123
177
instance ExprLike LetBinding where
180
LetBind _ _ _ e e' -> fold e `mappend` fold e'
181
LetPatBind _ p e -> fold p `mappend` fold e
184
where fold e = foldExpr f e
124
186
traverseExpr f e = do
125
187
let trav e = traverseExpr f e
132
194
-- | TODO: currently does not go into patterns.
133
195
instance ExprLike (Pattern' a) where
196
foldExpr f _ = mempty
134
197
traverseExpr f e = return e
136
199
-- | TODO: currently does not go into clauses.
137
200
instance ExprLike (Clause' a) where
201
foldExpr f _ = mempty
138
202
traverseExpr f e = return e
205
instance ExprLike (Clause' a) where
206
foldExpr f (Clause _ rhs ds) = fold rhs `mappend` fold ds
207
where fold e = foldExpr f e
208
traverseExpr f (Clause lhs rhs ds) = Clause lhs <$> trav rhs <*> trav ds
209
where trav e = traverseExpr f e
211
instance ExprLike RHS where
215
AbsurdRHS{} -> mempty
216
WithRHS _ es cs -> fold es `mappend` fold cs
217
RewriteRHS _ es rhs ds -> fold es `mappend` fold rhs `mappend` fold ds
218
where fold e = foldExpr f e
222
RHS e -> RHS <$> trav e
223
AbsurdRHS{} -> pure rhs
224
WithRHS x es cs -> WithRHS x <$> trav es <*> trav cs
225
RewriteRHS xs es rhs ds -> RewriteRHS xs <$> trav es <*> trav rhs <*> trav ds
226
where trav e = traverseExpr f e
228
instance ExprLike Declaration where