1{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, DeriveFunctor #-}
5import Control.Monad.State
7import qualified Data.Map as Map
12 * Full dependent types
13 * A hierarchy of universes, with cumulativity: Set : Set1, Set1 : Set2, ...
14 * Pattern matching letrec binding
15 * (primitive types defined externally)
18 * Typechecker is kept as simple as possible
19 - no unification, just a checker for incomplete terms.
20 * We have a simple collection of tactics which we use to elaborate source
21 programs with implicit syntax into fully explicit terms.
28data FC = FC { fc_fname :: String,
33 show (FC f l) = f ++ ":" ++ show l
36 | CantUnify Term Term Err
42instance Show Err where
47 deriving (Eq, Functor)
49instance Show a => Show (TC a) where
51 show (Error str) = "Error: " ++ show str
53-- at some point, this instance should also carry type checking options
56instance Monad TC where
61 fail e = Error (Msg e)
66trun :: FC -> TC a -> TC a
68trun fc (Error e) = Error (At fc e)
70instance MonadPlus TC where
71 mzero = fail "Unknown error"
72 (OK x) `mplus` _ = OK x
73 _ `mplus` (OK y) = OK y
76discard :: Monad m => m a -> m ()
77discard f = f >> return ()
79showSep :: String -> [String] -> String
82showSep sep (x:xs) = x ++ sep ++ showSep sep xs
84traceWhen True msg a = trace msg a
85traceWhen False _ a = a
87-- RAW TERMS ----------------------------------------------------------------
89-- Names are hierarchies of strings, describing scope (so no danger of
90-- duplicate names, but need to be careful on lookup).
91-- Also MN for machine chosen names
94 | NS Name [String] -- root, namespaces
98instance Show Name where
100 show (NS n s) = showSep "." (reverse s) ++ "." ++ show n
101 show (MN i s) = "{" ++ s ++ show i ++ "}"
104-- Contexts allow us to map names to things
105-- TODO: Namespaces and ambiguous names
107type Ctxt a = Map.Map Name (Map.Map Name a)
108emptyContext = Map.empty
113addDef :: Show a => Name -> a -> Ctxt a -> Ctxt a
114addDef n v ctxt = case Map.lookup (nsroot n) ctxt of
115 Nothing -> Map.insert (nsroot n)
116 (Map.insert n v Map.empty) ctxt
117 Just xs -> Map.insert (nsroot n)
118 (Map.insert n v xs) ctxt
120{- lookup a name in the context, given an optional namespace.
121 The name (n) may itself have a (partial) namespace given.
123 Rules for resolution:
124 - if an explicit namespace is given, return the names which match it. If none
125 match, return all names.
126 - if the name has has explicit namespace given, return the names which match it
127 and ignore the given namespace.
128 - otherwise, return all names.
132lookupCtxtName :: Maybe [String] -> Name -> Ctxt a -> [(Name, a)]
133lookupCtxtName nspace n ctxt = case Map.lookup (nsroot n) ctxt of
134 Just xs -> filterNS (Map.toList xs)
138 filterNS ((found, v) : xs)
139 | nsmatch n found = (found, v) : filterNS xs
140 | otherwise = filterNS xs
142 nsmatch (NS n ns) (NS p ps) = ns `isPrefixOf` ps
143 nsmatch (NS _ _) _ = False
144 nsmatch looking found = True
146lookupCtxt :: Maybe [String] -> Name -> Ctxt a -> [a]
147lookupCtxt ns n ctxt = map snd (lookupCtxtName ns n ctxt)
149toAlist :: Ctxt a -> [(Name, a)]
150toAlist ctxt = let allns = map snd (Map.toList ctxt) in
151 concat (map (Map.toList) allns)
153addAlist :: Show a => [(Name, a)] -> Ctxt a -> Ctxt a
154addAlist [] ctxt = ctxt
155addAlist ((n, tm) : ds) ctxt = addDef n tm (addAlist ds ctxt)
157data Const = I Int | BI Integer | Fl Double | Ch Char | Str String
158 | IType | BIType | FlType | ChType | StrType
163 | RBind Name (Binder Raw) Raw
169data Binder b = Lam { binderTy :: b }
170 | Pi { binderTy :: b }
171 | Let { binderTy :: b,
173 | NLet { binderTy :: b,
175 | Hole { binderTy :: b}
176 | GHole { binderTy :: b}
177 | Guess { binderTy :: b,
179 | PVar { binderTy :: b }
180 | PVTy { binderTy :: b }
181 deriving (Show, Eq, Functor)
183fmapMB :: Monad m => (a -> m b) -> Binder a -> m (Binder b)
184fmapMB f (Let t v) = liftM2 Let (f t) (f v)
185fmapMB f (NLet t v) = liftM2 NLet (f t) (f v)
186fmapMB f (Guess t v) = liftM2 Guess (f t) (f v)
187fmapMB f (Lam t) = liftM Lam (f t)
188fmapMB f (Pi t) = liftM Pi (f t)
189fmapMB f (Hole t) = liftM Hole (f t)
190fmapMB f (GHole t) = liftM GHole (f t)
191fmapMB f (PVar t) = liftM PVar (f t)
192fmapMB f (PVTy t) = liftM PVTy (f t)
194raw_apply :: Raw -> [Raw] -> Raw
196raw_apply f (a : as) = raw_apply (RApp f a) as
198data RawFun = RawFun { rtype :: Raw,
203data RawDatatype = RDatatype Name Raw [(Name, Raw)]
206data RDef = RFunction RawFun
211type RProgram = [(Name, RDef)]
213-- WELL TYPED TERMS ---------------------------------------------------------
215data UExp = UVar Int -- universe variable
216 | UVal Int -- explicit universe level
219instance Show UExp where
220 show (UVar x) | x < 26 = [toEnum (x + fromEnum 'a')]
221 | otherwise = toEnum ((x `mod` 26) + fromEnum 'a') : show (x `div` 26)
222 show (UVal x) = show x
223-- show (UMax l r) = "max(" ++ show l ++ ", " ++ show r ++")"
225data UConstraint = ULT UExp UExp
229instance Show UConstraint where
230 show (ULT x y) = show x ++ " < " ++ show y
231 show (ULE x y) = show x ++ " <= " ++ show y
233type UCs = (Int, [UConstraint])
235data NameType = Bound | Ref | DCon Int Int | TCon Int Int
238data TT n = P NameType n (TT n) -- embed type
240 | Bind n (Binder (TT n)) (TT n)
241 | App (TT n) (TT n) -- function, function type, arg
246type EnvTT n = [(n, Binder (TT n))]
248data Datatype n = Data { d_typename :: n,
251 d_cons :: [(n, TT n)] }
252 deriving (Show, Functor, Eq)
254instance Eq n => Eq (TT n) where
255 (==) (P xt x _) (P yt y _) = xt == yt && x == y
256 (==) (V x) (V y) = x == y
257 (==) (Bind _ xb xs) (Bind _ yb ys) = xb == yb && xs == ys
258 (==) (App fx ax) (App fy ay) = fx == fy && ax == ay
259 (==) (Set _) (Set _) = True -- deal with constraints later
260 (==) (Constant x) (Constant y) = x == y
263convEq :: Eq n => TT n -> TT n -> StateT UCs TC Bool
264convEq (P xt x _) (P yt y _) = return (xt == yt && x == y)
265convEq (V x) (V y) = return (x == y)
266convEq (Bind _ xb xs) (Bind _ yb ys)
267 = liftM2 (&&) (convEqB xb yb) (convEq xs ys)
268 where convEqB (Let v t) (Let v' t') = liftM2 (&&) (convEq v v') (convEq t t')
269 convEqB (Guess v t) (Guess v' t') = liftM2 (&&) (convEq v v') (convEq t t')
270 convEqB b b' = convEq (binderTy b) (binderTy b')
271convEq (App fx ax) (App fy ay) = liftM2 (&&) (convEq fx fy) (convEq ax ay)
272convEq (Constant x) (Constant y) = return (x == y)
273convEq (Set x) (Set y) = do (v, cs) <- get
274 put (v, ULE x y : cs)
276convEq _ _ = return False
278-- A few handy operations on well typed terms:
280instantiate :: TT n -> TT n -> TT n
281instantiate e = subst 0 where
282 subst i (V x) | i == x = e
283 subst i (Bind x b sc) = Bind x (fmap (subst i) b) (subst (i+1) sc)
284 subst i (App f a) = App (subst i f) (subst i a)
287pToV :: Eq n => n -> TT n -> TT n
289pToV' n i (P _ x _) | n == x = V i
290pToV' n i (Bind x b sc)
291 | n == x = Bind x (fmap (pToV' n i) b) sc
292 | otherwise = Bind x (fmap (pToV' n i) b) (pToV' n (i+1) sc)
293pToV' n i (App f a) = App (pToV' n i f) (pToV' n i a)
296-- Convert several names. First in the list comes out as V 0
297pToVs :: Eq n => [n] -> TT n -> TT n
298pToVs ns tm = pToVs' ns tm 0 where
300 pToVs' (n:ns) tm i = pToV' n i (pToVs' ns tm (i+1))
304 vToP' env (V i) = let (n, b) = (env !! i) in
305 P Bound n (binderTy b)
306 vToP' env (Bind n b sc) = let b' = fmap (vToP' env) b in
307 Bind n b' (vToP' ((n, b'):env) sc)
308 vToP' env (App f a) = App (vToP' env f) (vToP' env a)
311finalise :: Eq n => TT n -> TT n
312finalise (Bind x b sc) = Bind x (fmap finalise b) (pToV x (finalise sc))
313finalise (App f a) = App (finalise f) (finalise a)
316subst :: Eq n => n -> TT n -> TT n -> TT n
317subst n v tm = instantiate v (pToV n tm)
319substNames :: Eq n => [(n, TT n)] -> TT n -> TT n
321substNames ((n, tm) : xs) t = subst n tm (substNames xs t)
323-- Returns true if V 0 and bound name n do not occur in the term
325noOccurrence :: Eq n => n -> TT n -> Bool
326noOccurrence n t = no' 0 t
328 no' i (V x) = not (i == x)
329 no' i (P Bound x _) = not (n == x)
330 no' i (Bind n b sc) = noB' i b && no' (i+1) sc
331 where noB' i (Let t v) = no' i t && no' i v
332 noB' i (Guess t v) = no' i t && no' i v
333 noB' i b = no' i (binderTy b)
334 no' i (App f a) = no' i f && no' i a
337-- Return the arity of a (normalised) type
340arity (Bind n (Pi t) sc) = 1 + arity sc
343-- deconstruct an application; returns the function and a list of arguments
345unApply :: TT n -> (TT n, [TT n])
346unApply t = ua [] t where
347 ua args (App f a) = ua (a:args) f
348 ua args t = (t, args)
350mkApp :: TT n -> [TT n] -> TT n
352mkApp f (a:as) = mkApp (App f a) as
354forget :: TT Name -> Raw
357 fe env (P _ n _) = Var n
358 fe env (V i) = Var (env !! i)
359 fe env (Bind n b sc) = RBind n (fmap (fe env) b)
361 fe env (App f a) = RApp (fe env f) (fe env a)
364 fe env (Set i) = RSet
366bindAll :: [(n, Binder (TT n))] -> TT n -> TT n
368bindAll ((n, b) : bs) t = Bind n b (bindAll bs t)
370bindTyArgs :: (TT n -> Binder (TT n)) -> [(n, TT n)] -> TT n -> TT n
371bindTyArgs b xs = bindAll (map (\ (n, ty) -> (n, b ty)) xs)
373getArgTys :: TT n -> [(n, TT n)]
374getArgTys (Bind n (Pi t) sc) = (n, t) : getArgTys sc
377uniqueName :: Name -> [Name] -> Name
378uniqueName n hs | n `elem` hs = uniqueName (nextName n) hs
381nextName (MN i n) = MN (i+1) n
382nextName (UN x) = let (num', nm') = span isDigit (reverse x)
384 num = readN (reverse num') in
385 UN (nm ++ show (num+1))
395-- an environment with de Bruijn indices 'normalised' so that they all refer to
398newtype WkEnvTT n = Wk (EnvTT n)
399type WkEnv = WkEnvTT Name
401instance (Eq n, Show n) => Show (TT n) where
402 show t = showEnv [] t
404instance Show Const where
406 show (BI i) = show i ++ "L"
409 show (Str s) = show s
411 show BIType = "Integer"
412 show FlType = "Float"
414 show StrType = "String"
417showEnv env t = showEnv' env t False
418showEnvDbg env t = showEnv' env t True
420showEnv' env t dbg = se 10 env t where
421 se p env (P nt n t) = show n
422 -- ++ if dbg then "{" ++ show nt ++ " : " ++ se 10 env t ++ "}" else ""
423 se p env (V i) | i < length env = (show $ fst $ env!!i) ++
424 if dbg then "{" ++ show i ++ "}" else ""
425 | otherwise = "!!V " ++ show i ++ "!!"
426 se p env (Bind n b@(Pi t) sc)
427 | noOccurrence n sc && not dbg = bracket p 2 $ se 1 env t ++ " -> " ++ se 10 ((n,b):env) sc
428 se p env (Bind n b sc) = bracket p 2 $ sb env n b ++ se 10 ((n,b):env) sc
429 se p env (App f a) = bracket p 1 $ se 1 env f ++ " " ++ se 0 env a
430 se p env (Constant c) = show c
431 se p env (Set i) = "Set " ++ show i
433 sb env n (Lam t) = showb env "\\ " " => " n t
434 sb env n (Hole t) = showb env "? " ". " n t
435 sb env n (GHole t) = showb env "?defer " ". " n t
436 sb env n (Pi t) = showb env "(" ") -> " n t
437 sb env n (PVar t) = showb env "pat " ". " n t
438 sb env n (PVTy t) = showb env "pty " ". " n t
439 sb env n (Let t v) = showbv env "let " " in " n t v
440 sb env n (Guess t v) = showbv env "?? " " in " n t v
442 showb env op sc n t = op ++ show n ++ " : " ++ se 10 env t ++ sc
443 showbv env op sc n t v = op ++ show n ++ " : " ++ se 10 env t ++ " = " ++
446 bracket outer inner str | inner > outer = "(" ++ str ++ ")"
449-- Check whether a term has any holes in it - impure if so
451pureTerm :: TT n -> Bool
452pureTerm (App f a) = pureTerm f && pureTerm a
453pureTerm (Bind n b sc) = pureBinder b && pureTerm sc where
454 pureBinder (Hole _) = False
455 pureBinder (Guess _ _) = False
456 pureBinder (Let t v) = pureTerm t && pureTerm v
457 pureBinder t = pureTerm (binderTy t)
460-- weaken a term by adding i to each de Bruijn index (i.e. lift it over i bindings)
462weakenTm :: Int -> TT n -> TT n
463weakenTm i t = wk i 0 t
464 where wk i min (V x) | x >= min = V (i + x)
465 wk i m (App f a) = App (wk i m f) (wk i m a)
466 wk i m (Bind x b sc) = Bind x (wkb i m b) (wk i (m + 1) sc)
468 wkb i m t = fmap (wk i m) t
470-- weaken an environment so that all the de Bruijn indices are correct according
471-- to the latest bound variable
473weakenEnv :: EnvTT n -> EnvTT n
474weakenEnv env = wk (length env - 1) env
476 wk i ((n, b) : bs) = (n, weakenTmB i b) : wk (i - 1) bs
477 weakenTmB i (Let t v) = Let (weakenTm i t) (weakenTm i v)
478 weakenTmB i (Guess t v) = Guess (weakenTm i t) (weakenTm i v)
479 weakenTmB i t = t { binderTy = weakenTm i (binderTy t) }
481weakenTmEnv :: Int -> EnvTT n -> EnvTT n
482weakenTmEnv i = map (\ (n, b) -> (n, fmap (weakenTm i) b))
484orderPats :: Term -> Term
485orderPats tm = op [] tm
487 op ps (Bind n (PVar t) sc) = op ((n, t) : ps) sc
488 op ps sc = bindAll (map (\ (n, t) -> (n, PVar t)) (sortP ps)) sc
490 sortP ps = pick [] (reverse ps)
492 namesIn (P _ n _) = [n]
493 namesIn (Bind n b t) = nub $ nb b ++ (namesIn t \\ [n])
494 where nb (Let t v) = nub (namesIn t) ++ nub (namesIn v)
495 nb (Guess t v) = nub (namesIn t) ++ nub (namesIn v)
496 nb t = namesIn (binderTy t)
497 namesIn (App f a) = nub (namesIn f ++ namesIn a)
500 pick acc [] = reverse acc
501 pick acc ((n, t) : ps) = pick (insert n t acc) ps
503 insert n t [] = [(n, t)]
504 insert n t ((n',t') : ps)
505 | n `elem` (namesIn t' ++ concatMap namesIn (map snd ps))
506 = (n', t') : insert n t ps
507 | otherwise = (n,t):(n',t'):ps
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list