@@ -14,6 +14,7 @@ import qualified Data.Set as S
14
14
import Data.STRef
15
15
import qualified Data.Traversable as T
16
16
import qualified Data.Vector as V
17
+ import Prelude.Extras
17
18
18
19
import Syntax
19
20
import qualified Syntax.Abstract as Abstract
@@ -22,39 +23,39 @@ import qualified Syntax.Lambda as Lambda
22
23
import TCM
23
24
import Util
24
25
25
- type Exists s = STRef s (Either Level (AbstractM s ))
26
+ type Exists e s = STRef s (Either Level (e ( MetaVar e s ) ))
26
27
27
- data MetaVar s = MetaVar
28
- { metaId :: ! Int
29
- , metaType :: AbstractM s
30
- , metaHint :: ! NameHint
31
- , metaRef :: ! (Maybe (Exists s ))
28
+ data MetaVar e s = MetaVar
29
+ { metaId :: ! Int
30
+ , metaType :: e ( MetaVar e s )
31
+ , metaHint :: ! NameHint
32
+ , metaRef :: ! (Maybe (Exists e s ))
32
33
}
33
34
34
- type ConcreteM s = Concrete. Expr (MetaVar s )
35
- type AbstractM s = Abstract. Expr (MetaVar s )
36
- type LambdaM s = Lambda. Expr (MetaVar s )
37
- type ScopeM b f s = Scope b f (MetaVar s )
38
- type BranchesM c f s = Branches c f (MetaVar s )
35
+ type ConcreteM s = Concrete. Expr (MetaVar Abstract. Expr s )
36
+ type AbstractM s = Abstract. Expr (MetaVar Abstract. Expr s )
37
+ type LambdaM s = Lambda. Expr (MetaVar Abstract. Expr s )
38
+ type ScopeM b f s = Scope b f (MetaVar Abstract. Expr s )
39
+ type BranchesM c f s = Branches c f (MetaVar Abstract. Expr s )
39
40
40
- instance Eq (MetaVar s ) where
41
+ instance Eq (MetaVar e s ) where
41
42
(==) = (==) `on` metaId
42
43
43
- instance Ord (MetaVar s ) where
44
+ instance Ord (MetaVar e s ) where
44
45
compare = compare `on` metaId
45
46
46
- instance Hashable (MetaVar s ) where
47
+ instance Hashable (MetaVar e s ) where
47
48
hashWithSalt s = hashWithSalt s . metaId
48
49
49
- instance Show (MetaVar s ) where
50
+ instance Show1 e => Show (MetaVar e s ) where
50
51
showsPrec d (MetaVar i t h _) = showParen (d > 10 ) $
51
52
showString " Meta" . showChar ' ' . showsPrec 11 i .
52
- showChar ' ' . showsPrec 11 t . showChar ' ' . showsPrec 11 h .
53
+ showChar ' ' . showsPrec1 11 t . showChar ' ' . showsPrec 11 h .
53
54
showChar ' ' . showString " <Ref>"
54
55
55
56
showMeta
56
- :: (Functor f , Foldable f , Pretty (f String ))
57
- => f (MetaVar s )
57
+ :: (Functor e , Foldable e , Functor f , Foldable f , Pretty (f String ), Pretty ( e String ))
58
+ => f (MetaVar e s )
58
59
-> TCM s Doc
59
60
showMeta x = do
60
61
vs <- foldMapM S. singleton x
@@ -68,9 +69,9 @@ showMeta x = do
68
69
let solutions = [(sv v, pretty $ sv <$> metaType v, pretty $ fmap sv <$> msol) | (v, msol) <- zip vsl pvs]
69
70
return $ pretty (sv <$> x) <> text " , vars: " <> pretty solutions
70
71
71
- tr :: (Functor f , Foldable f , Pretty (f String ))
72
+ tr :: (Functor e , Foldable e , Functor f , Foldable f , Pretty (f String ), Pretty ( e String ))
72
73
=> String
73
- -> f (MetaVar s )
74
+ -> f (MetaVar e s )
74
75
-> TCM s ()
75
76
tr s x = do
76
77
i <- gets tcIndent
@@ -87,38 +88,38 @@ trs s x = do
87
88
i <- gets tcIndent
88
89
TCM. log $ mconcat (replicate i " | " ) ++ " --" ++ s ++ " : " ++ show x
89
90
90
- existsAtLevel :: NameHint -> AbstractM s -> Level -> TCM s (MetaVar s )
91
+ existsAtLevel :: NameHint -> e ( MetaVar e s ) -> Level -> TCM s (MetaVar e s )
91
92
existsAtLevel hint typ l = do
92
93
i <- fresh
93
94
ref <- liftST $ newSTRef $ Left l
94
95
TCM. log $ " exists: " ++ show i
95
96
return $ MetaVar i typ hint (Just ref)
96
97
97
- exists :: NameHint -> AbstractM s -> TCM s (MetaVar s )
98
+ exists :: NameHint -> e ( MetaVar e s ) -> TCM s (MetaVar e s )
98
99
exists hint typ = existsAtLevel hint typ =<< level
99
100
100
- existsVar :: Applicative g => NameHint -> AbstractM s -> TCM s (g (MetaVar s ))
101
+ existsVar :: Applicative g => NameHint -> e ( MetaVar e s ) -> TCM s (g (MetaVar e s ))
101
102
existsVar hint typ = pure <$> exists hint typ
102
103
103
- existsVarAtLevel :: Applicative g => NameHint -> AbstractM s -> Level -> TCM s (g (MetaVar s ))
104
+ existsVarAtLevel :: Applicative g => NameHint -> e ( MetaVar e s ) -> Level -> TCM s (g (MetaVar e s ))
104
105
existsVarAtLevel hint typ l = pure <$> existsAtLevel hint typ l
105
106
106
- forall_ :: NameHint -> AbstractM s -> TCM s (MetaVar s )
107
+ forall_ :: NameHint -> e ( MetaVar e s ) -> TCM s (MetaVar e s )
107
108
forall_ hint typ = do
108
109
i <- fresh
109
110
TCM. log $ " forall: " ++ show i
110
111
return $ MetaVar i typ hint Nothing
111
112
112
- forallVar :: Applicative g => NameHint -> AbstractM s -> TCM s (g (MetaVar s ))
113
+ forallVar :: Applicative g => NameHint -> e ( MetaVar e s ) -> TCM s (g (MetaVar e s ))
113
114
forallVar hint typ = pure <$> forall_ hint typ
114
115
115
- solution :: Exists s -> TCM s (Either Level (AbstractM s ))
116
+ solution :: Exists e s -> TCM s (Either Level (e ( MetaVar e s ) ))
116
117
solution = liftST . readSTRef
117
118
118
- solve :: Exists s -> AbstractM s -> TCM s ()
119
+ solve :: Exists e s -> e ( MetaVar e s ) -> TCM s ()
119
120
solve r x = liftST $ writeSTRef r $ Right x
120
121
121
- refineIfSolved :: Exists s -> AbstractM s -> (AbstractM s -> TCM s (AbstractM s )) -> TCM s (AbstractM s )
122
+ refineIfSolved :: Exists e s -> e ( MetaVar e s ) -> (e ( MetaVar e s ) -> TCM s (e ( MetaVar e s ))) -> TCM s (e ( MetaVar e s ) )
122
123
refineIfSolved r d f = do
123
124
sol <- solution r
124
125
case sol of
@@ -128,21 +129,21 @@ refineIfSolved r d f = do
128
129
solve r e'
129
130
return e'
130
131
131
- letMeta :: NameHint -> AbstractM s -> AbstractM s -> TCM s (MetaVar s )
132
+ letMeta :: NameHint -> e ( MetaVar e s ) -> e ( MetaVar e s ) -> TCM s (MetaVar e s )
132
133
letMeta hint expr typ = do
133
134
i <- fresh
134
135
ref <- liftST $ newSTRef $ Right expr
135
136
return $ MetaVar i typ hint (Just ref)
136
137
137
138
letVar :: Applicative g
138
- => NameHint -> AbstractM s -> AbstractM s -> TCM s (g (MetaVar s ))
139
+ => NameHint -> e ( MetaVar e s ) -> e ( MetaVar e s ) -> TCM s (g (MetaVar e s ))
139
140
letVar hint expr typ = pure <$> letMeta hint expr typ
140
141
141
- foldMapM :: (Foldable f , Monoid m )
142
- => (MetaVar s -> m ) -> f (MetaVar s ) -> TCM s m
142
+ foldMapM :: (Foldable e , Foldable f , Monoid m )
143
+ => (MetaVar e s -> m ) -> f (MetaVar e s ) -> TCM s m
143
144
foldMapM f = foldrM go mempty
144
145
where
145
- go v m = (<> m) . (<> f v) <$> do
146
+ go v m = (<> m) . (<> f v) <$>
146
147
case metaRef v of
147
148
Just r -> do
148
149
sol <- solution r
@@ -151,9 +152,11 @@ foldMapM f = foldrM go mempty
151
152
Right c -> foldMapM f c
152
153
Nothing -> return mempty
153
154
154
- abstractM :: (MetaVar s -> Maybe b )
155
- -> AbstractM s
156
- -> TCM s (ScopeM b Abstract. Expr s )
155
+ abstractM
156
+ :: (Monad e , Traversable e , Show1 e )
157
+ => (MetaVar e s -> Maybe b )
158
+ -> e (MetaVar e s )
159
+ -> TCM s (Scope b e (MetaVar e s ))
157
160
abstractM f e = do
158
161
e' <- freeze e
159
162
changed <- liftST $ newSTRef False
@@ -183,18 +186,18 @@ abstractM f e = do
183
186
go _ v' = free v'
184
187
free = pure . pure . pure . pure
185
188
186
- abstract1M :: MetaVar s
189
+ abstract1M :: MetaVar Abstract. Expr s
187
190
-> AbstractM s
188
191
-> TCM s (ScopeM () Abstract. Expr s )
189
192
abstract1M v e = do
190
193
TCM. log $ " abstracting " ++ show (metaId v)
191
194
abstractM (\ v' -> if v == v' then Just () else Nothing ) e
192
195
193
196
abstractDefM
194
- :: (MetaVar s -> Maybe b )
195
- -> Definition Abstract. Expr (MetaVar s )
197
+ :: (MetaVar Abstract. Expr s -> Maybe b )
198
+ -> Definition Abstract. Expr (MetaVar Abstract. Expr s )
196
199
-> AbstractM s
197
- -> TCM s ( Definition Abstract. Expr (Var b (MetaVar s ))
200
+ -> TCM s ( Definition Abstract. Expr (Var b (MetaVar Abstract. Expr s ))
198
201
, ScopeM b Abstract. Expr s
199
202
)
200
203
abstractDefM f (Definition e) t = do
@@ -207,10 +210,10 @@ abstractDefM f (DataDefinition e) t = do
207
210
return (DataDefinition e', t')
208
211
209
212
abstractDataDefM
210
- :: (MetaVar s -> Maybe b )
211
- -> DataDef Abstract. Expr (MetaVar s )
213
+ :: (MetaVar Abstract. Expr s -> Maybe b )
214
+ -> DataDef Abstract. Expr (MetaVar Abstract. Expr s )
212
215
-> AbstractM s
213
- -> TCM s (DataDef Abstract. Expr (Var b (MetaVar s )))
216
+ -> TCM s (DataDef Abstract. Expr (Var b (MetaVar Abstract. Expr s )))
214
217
abstractDataDefM f (DataDef cs) typ = mdo
215
218
let inst = instantiateTele $ pure <$> vs
216
219
vs = (\ (_, _, _, v) -> v) <$> ps'
@@ -229,23 +232,23 @@ abstractDataDefM f (DataDef cs) typ = mdo
229
232
etaLamM
230
233
:: NameHint
231
234
-> Annotation
232
- -> Abstract. Expr (MetaVar s )
233
- -> Scope1 Abstract. Expr (MetaVar s )
234
- -> TCM s (Abstract. Expr (MetaVar s ))
235
+ -> Abstract. Expr (MetaVar Abstract. Expr s )
236
+ -> Scope1 Abstract. Expr (MetaVar Abstract. Expr s )
237
+ -> TCM s (Abstract. Expr (MetaVar Abstract. Expr s ))
235
238
etaLamM n p t s = do
236
239
s' <- freezeBound s
237
240
return $ Abstract. etaLam n p t s'
238
241
239
- freeze :: AbstractM s -> TCM s (AbstractM s )
242
+ freeze :: ( Monad e , Traversable e ) => e ( MetaVar e s ) -> TCM s (e ( MetaVar e s ) )
240
243
freeze e = join <$> traverse go e
241
244
where
242
245
go v@ (metaRef -> Just r) = either (const $ do mt <- freeze (metaType v); return $ pure v {metaType = mt})
243
246
freeze =<< solution r
244
247
go v = return $ pure v
245
248
246
- freezeBound :: (Traversable (t Abstract. Expr ), Bound t )
247
- => t Abstract. Expr (MetaVar s )
248
- -> TCM s (t Abstract. Expr (MetaVar s ))
249
+ freezeBound :: (Monad e , Traversable e , Traversable (t e ), Bound t )
250
+ => t e (MetaVar e s )
251
+ -> TCM s (t e (MetaVar e s ))
249
252
freezeBound e = (>>>= id ) <$> traverse go e
250
253
where
251
254
go v@ (metaRef -> Just r) = either (const $ do mt <- freeze (metaType v); return $ pure v {metaType = mt})
0 commit comments