-
Notifications
You must be signed in to change notification settings - Fork 10
/
Concrete.hs
280 lines (230 loc) · 9.74 KB
/
Concrete.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
{-# LANGUAGE TupleSections, ParallelListComp #-}
-- | Convert the concrete syntax into the syntax of cubical TT.
module Concrete where
import Exp.Abs
import qualified CTT as C
import Pretty
import Control.Applicative
import Control.Arrow (second)
import Control.Monad.Trans
import Control.Monad.Trans.Reader
import Control.Monad.Trans.Error hiding (throwError)
import Control.Monad.Error (throwError)
import Control.Monad (when)
import Data.Functor.Identity
import Data.List (nub)
type Tele = [(AIdent,Exp)]
type Ter = C.Ter
-- | Useful auxiliary functions
-- Applicative cons
(<:>) :: Applicative f => f a -> f [a] -> f [a]
a <:> b = (:) <$> a <*> b
-- un-something functions
unAIdent :: AIdent -> C.Ident
unAIdent (AIdent (_,x)) = x
unVar :: Exp -> Maybe AIdent
unVar (Var x) = Just x
unVar _ = Nothing
unWhere :: ExpWhere -> Exp
unWhere (Where e ds) = Let ds e
unWhere (NoWhere e) = e
-- tail recursive form to transform a sequence of applications
-- App (App (App u v) ...) w into (u, [v, …, w])
-- (cleaner than the previous version of unApps)
unApps :: Exp -> [Exp] -> (Exp, [Exp])
unApps (App u v) ws = unApps u (v : ws)
unApps u ws = (u, ws)
vTele :: [VTDecl] -> Tele
vTele decls = [ (i, typ) | VTDecl id ids typ <- decls, i <- id:ids ]
-- turns an expression of the form App (... (App id1 id2) ... idn)
-- into a list of idents
pseudoIdents :: Exp -> Maybe [AIdent]
pseudoIdents = mapM unVar . uncurry (:) . flip unApps []
pseudoTele :: [PseudoTDecl] -> Maybe Tele
pseudoTele [] = return []
pseudoTele (PseudoTDecl exp typ : pd) = do
ids <- pseudoIdents exp
pt <- pseudoTele pd
return $ map (,typ) ids ++ pt
-------------------------------------------------------------------------------
-- | Resolver and environment
type Arity = Int
data SymKind = Variable | Constructor Arity
deriving (Eq,Show)
-- local environment for constructors
data Env = Env { envModule :: String,
variables :: [(C.Binder,SymKind)] }
deriving (Eq, Show)
type Resolver a = ReaderT Env (ErrorT String Identity) a
emptyEnv :: Env
emptyEnv = Env "" []
runResolver :: Resolver a -> Either String a
runResolver x = runIdentity $ runErrorT $ runReaderT x emptyEnv
updateModule :: String -> Env -> Env
updateModule mod e = e {envModule = mod}
insertBinder :: (C.Binder,SymKind) -> Env -> Env
insertBinder (x@(n,_),var) e
| n == "_" || n == "undefined" = e
| otherwise = e {variables = (x, var) : variables e}
insertBinders :: [(C.Binder,SymKind)] -> Env -> Env
insertBinders = flip $ foldr insertBinder
insertVar :: C.Binder -> Env -> Env
insertVar x = insertBinder (x,Variable)
insertVars :: [C.Binder] -> Env -> Env
insertVars = flip $ foldr insertVar
insertCon :: (C.Binder,Arity) -> Env -> Env
insertCon (x,a) = insertBinder (x,Constructor a)
insertCons :: [(C.Binder,Arity)] -> Env -> Env
insertCons = flip $ foldr insertCon
getModule :: Resolver String
getModule = envModule <$> ask
getVariables :: Resolver [(C.Binder,SymKind)]
getVariables = variables <$> ask
getLoc :: (Int,Int) -> Resolver C.Loc
getLoc l = C.Loc <$> getModule <*> pure l
resolveBinder :: AIdent -> Resolver C.Binder
resolveBinder (AIdent (l,x)) = (x,) <$> getLoc l
-- Eta expand constructors
expandConstr :: Arity -> String -> [Exp] -> Resolver Ter
expandConstr a x es = do
let r = a - length es
binders = map (('_' :) . show) [1..r]
args = map C.Var binders
ts <- mapM resolveExp es
return $ C.mkLams binders $ C.mkApps (C.Con x []) (ts ++ args)
resolveVar :: AIdent -> Resolver Ter
resolveVar (AIdent (l,x))
| (x == "_") || (x == "undefined") = C.PN <$> C.Undef <$> getLoc l
| otherwise = do
modName <- getModule
vars <- getVariables
case C.getIdent x vars of
Just Variable -> return $ C.Var x
Just (Constructor a) -> expandConstr a x []
_ -> throwError $
"Cannot resolve variable" <+> x <+> "at position" <+>
show l <+> "in module" <+> modName
lam :: AIdent -> Resolver Ter -> Resolver Ter
lam a e = do x <- resolveBinder a; C.Lam x <$> local (insertVar x) e
lams :: [AIdent] -> Resolver Ter -> Resolver Ter
lams = flip $ foldr lam
bind :: (Ter -> Ter -> Ter) -> (AIdent, Exp) -> Resolver Ter -> Resolver Ter
bind f (x,t) e = f <$> resolveExp t <*> lam x e
binds :: (Ter -> Ter -> Ter) -> Tele -> Resolver Ter -> Resolver Ter
binds f = flip $ foldr $ bind f
resolveExp :: Exp -> Resolver Ter
resolveExp U = return C.U
resolveExp (Var x) = resolveVar x
resolveExp (App t s) = case unApps t [s] of
(x@(Var (AIdent (_,n))),xs) -> do
-- Special treatment in the case of a constructor in order not to
-- eta expand too much
vars <- getVariables
case C.getIdent n vars of
Just (Constructor a) -> expandConstr a n xs
_ -> C.mkApps <$> resolveExp x <*> mapM resolveExp xs
(x,xs) -> C.mkApps <$> resolveExp x <*> mapM resolveExp xs
resolveExp (Sigma t b) = case pseudoTele t of
Just tele -> binds C.Sigma tele (resolveExp b)
Nothing -> throwError "Telescope malformed in Sigma"
resolveExp (Pi t b) = case pseudoTele t of
Just tele -> binds C.Pi tele (resolveExp b)
Nothing -> throwError "Telescope malformed in Pigma"
resolveExp (Fun a b) = bind C.Pi (AIdent ((0,0),"_"), a) (resolveExp b)
resolveExp (Lam x xs t) = lams (x:xs) (resolveExp t)
resolveExp (Fst t) = C.Fst <$> resolveExp t
resolveExp (Snd t) = C.Snd <$> resolveExp t
resolveExp (Pair t0 t1) = C.SPair <$> resolveExp t0 <*> resolveExp t1
resolveExp (Split brs) = do
brs' <- mapM resolveBranch brs
loc <- getLoc (case brs of Branch (AIdent (l,_)) _ _:_ -> l ; _ -> (0,0))
return $ C.Split loc brs'
resolveExp (Let decls e) = do
(rdecls,names) <- resolveDecls decls
C.mkWheres rdecls <$> local (insertBinders names) (resolveExp e)
resolveWhere :: ExpWhere -> Resolver Ter
resolveWhere = resolveExp . unWhere
resolveBranch :: Branch -> Resolver (C.Label,([C.Binder],C.Ter))
resolveBranch (Branch lbl args e) = do
binders <- mapM resolveBinder args
re <- local (insertVars binders) $ resolveWhere e
return (unAIdent lbl, (binders, re))
resolveTele :: [(AIdent,Exp)] -> Resolver C.Tele
resolveTele [] = return []
resolveTele ((i,d):t) = do
x <- resolveBinder i
((x,) <$> resolveExp d) <:> local (insertVar x) (resolveTele t)
resolveLabel :: Label -> Resolver (C.Binder, C.Tele)
resolveLabel (Label n vdecl) =
(,) <$> resolveBinder n <*> resolveTele (vTele vdecl)
declsLabels :: [Decl] -> Resolver [(C.Binder,Arity)]
declsLabels decls = do
let sums = concat [sum | DeclData _ _ sum <- decls]
sequence [ (,length args) <$> resolveBinder lbl | Label lbl args <- sums ]
-- Resolve Data or Def declaration
resolveDDecl :: Decl -> Resolver (C.Ident, C.Ter)
resolveDDecl (DeclDef (AIdent (_,n)) args body) =
(n,) <$> lams args (resolveWhere body)
resolveDDecl (DeclData x@(AIdent (l,n)) args sum) =
(n,) <$> lams args (C.Sum <$> resolveBinder x <*> mapM resolveLabel sum)
resolveDDecl d = throwError $ "Definition expected" <+> show d
-- Resolve mutual declarations (possibly one)
resolveMutuals :: [Decl] -> Resolver (C.Decls,[(C.Binder,SymKind)])
resolveMutuals decls = do
binders <- mapM resolveBinder idents
cs <- declsLabels decls
let cns = map (fst . fst) cs ++ names
when (nub cns /= cns) $
throwError $ "Duplicated constructor or ident:" <+> show cns
rddecls <-
mapM (local (insertVars binders . insertCons cs) . resolveDDecl) ddecls
when (names /= map fst rddecls) $
throwError $ "Mismatching names in" <+> show decls
rtdecls <- resolveTele tdecls
return ([ (x,t,d) | (x,t) <- rtdecls | (_,d) <- rddecls ],
map (second Constructor) cs ++ map (,Variable) binders)
where
idents = [ x | DeclType x _ <- decls ]
names = [ unAIdent x | x <- idents ]
tdecls = [ (x,t) | DeclType x t <- decls ]
ddecls = filter (not . isTDecl) decls
isTDecl d = case d of DeclType{} -> True; _ -> False
-- Resolve opaque/transparent decls
resolveOTDecl :: (C.Binder -> C.ODecls) -> AIdent -> [Decl] ->
Resolver ([C.ODecls],[(C.Binder,SymKind)])
resolveOTDecl c n ds = do
vars <- getVariables
(rest,names) <- resolveDecls ds
case C.getBinder (unAIdent n) vars of
Just x -> return (c x : rest, names)
Nothing -> throwError $ "Not in scope:" <+> show n
-- Resolve declarations
resolveDecls :: [Decl] -> Resolver ([C.ODecls],[(C.Binder,SymKind)])
resolveDecls [] = return ([],[])
resolveDecls (DeclOpaque n:ds) = resolveOTDecl C.Opaque n ds
resolveDecls (DeclTransp n:ds) = resolveOTDecl C.Transp n ds
resolveDecls (td@DeclType{}:d:ds) = do
(rtd,names) <- resolveMutuals [td,d]
(rds,names') <- local (insertBinders names) $ resolveDecls ds
return (C.ODecls rtd : rds, names' ++ names)
resolveDecls (DeclPrim x t:ds) = case C.mkPN (unAIdent x) of
Just pn -> do
b <- resolveBinder x
rt <- resolveExp t
(rds,names) <- local (insertVar b) $ resolveDecls ds
return (C.ODecls [(b, rt, C.PN pn)] : rds, names ++ [(b,Variable)])
Nothing -> throwError $ "Primitive notion not defined:" <+> unAIdent x
resolveDecls (DeclMutual defs : ds) = do
(rdefs,names) <- resolveMutuals defs
(rds, names') <- local (insertBinders names) $ resolveDecls ds
return (C.ODecls rdefs : rds, names' ++ names)
resolveDecls (decl:_) = throwError $ "Invalid declaration:" <+> show decl
resolveModule :: Module -> Resolver ([C.ODecls],[(C.Binder,SymKind)])
resolveModule (Module n imports decls) =
local (updateModule $ unAIdent n) $ resolveDecls decls
resolveModules :: [Module] -> Resolver ([C.ODecls],[(C.Binder,SymKind)])
resolveModules [] = return ([],[])
resolveModules (mod:mods) = do
(rmod, names) <- resolveModule mod
(rmods,names') <- local (insertBinders names) $ resolveModules mods
return (rmod ++ rmods, names' ++ names)