1{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, DeriveFunctor #-}
5import Control.Monad.State
10 * A hierarchy of universes, with cumulativity: Set : Set1, Set1 : Set2, ...
11 * Pattern matching letrec binding
12 * (primitive types defined externally)
15 * Typechecker is kept as simple as possible
16 - no unification, just a checker for incomplete terms.
17 * We have a simple collection of tactics which we use to elaborate source
18 programs with implicit syntax into fully explicit terms.
26 | Error String -- TMP! Make this an informative data structure
27 deriving (Eq, Functor)
29instance Show a => Show (TC a) where
31 show (Error str) = "Error: " ++ str
33-- at some point, this instance should also carry type checking options
36instance Monad TC where
43instance MonadPlus TC where
44 mzero = Error "Unknown error"
45 (OK x) `mplus` _ = OK x
46 _ `mplus` (OK y) = OK y
49-- RAW TERMS ----------------------------------------------------------------
51-- Names are hierarchies of strings, describing scope (so no danger of
52-- duplicate names, but need to be careful on lookup).
53-- Also MN for machine chosen names
55data Name = UN [String]
59instance Show Name where
61 show (UN (n:ns)) = show (UN [n]) ++ "." ++ show (UN ns)
62 show (MN i s) = "{" ++ s ++ show i ++ "}"
65 | RBind Name (Binder Raw) Raw
70data Binder b = Lam { binderTy :: b }
71 | Pi { binderTy :: b }
72 | Let { binderTy :: b,
74 | NLet { binderTy :: b,
76 | Hole { binderTy :: b}
77 | Guess { binderTy :: b,
79 | PVar { binderTy :: b }
80 deriving (Show, Eq, Functor)
82raw_apply :: Raw -> [Raw] -> Raw
84raw_apply f (a : as) = raw_apply (RApp f a) as
86data RawFun = RawFun { rtype :: Raw,
91data RawDatatype = RDatatype Name Raw [(Name, Raw)]
94data RDef = RFunction RawFun
99type RProgram = [(Name, RDef)]
101-- WELL TYPED TERMS ---------------------------------------------------------
103data NameType = Bound | Ref | DCon Int Int | TCon Int
106-- FIXME: Consider changing this to birectional, with type annotations.
108data TT n = P NameType n (TT n) -- embed type
110 | Bind n (Binder (TT n)) (TT n)
111 | App (TT n) (TT n) -- function, function type, arg
113 deriving (Functor, Eq)
115type EnvTT n = [(n, Binder (TT n))]
117data Datatype n = Data { d_typename :: n,
119 d_cons :: [(n, TT n)] }
120 deriving (Show, Functor, Eq)
122-- A few handy operations on well typed terms:
124-- Returns true if V 0 and bound name n do not occur in the term
126noOccurrence :: Eq n => n -> TT n -> Bool
127noOccurrence n t = no' 0 t
129 no' i (V x) = not (i == x)
130 no' i (P Bound x _) = not (n == x)
131 no' i (Bind n b sc) = noB' i b && no' (i+1) sc
132 where noB' i (Let t v) = no' i t && no' i v
133 noB' i (Guess t v) = no' i t && no' i v
134 noB' i b = no' i (binderTy b)
135 no' i (App f a) = no' i f && no' i a
138-- Return the arity of a (normalised) type
141arity (Bind n (Pi t) sc) = 1 + arity sc
144-- deconstruct an application; returns the function and a list of arguments
146unApply :: TT n -> (TT n, [TT n])
147unApply t = ua [] t where
148 ua args (App f a) = ua (a:args) f
149 ua args t = (t, reverse args)
151forget :: TT Name -> Raw
154 fe env (P _ n _) = Var n
155 fe env (V i) = Var (env !! i)
156 fe env (Bind n b sc) = RBind n (fmap (fe env) b)
158 fe env (App f a) = RApp (fe env f) (fe env a)
159 fe env (Set i) = RSet i
166-- an environment with de Bruijn indices 'normalised' so that they all refer to
169newtype WkEnvTT n = Wk (EnvTT n)
170type WkEnv = WkEnvTT Name
172instance (Eq n, Show n) => Show (TT n) where
173 show t = showEnv [] t
175showEnv env t = showEnv' env t False
176showEnvDbg env t = showEnv' env t True
178showEnv' env t dbg = se 10 env t where
179 se p env (P nt n t) = show n
180-- ++ if dbg then "{" ++ show nt ++ " : " ++ se 10 env t ++ "}" else ""
181 se p env (V i) | i < length env = (show $ fst $ env!!i) ++
182 if dbg then "{" ++ show i ++ "}" else ""
183 | otherwise = "!!V " ++ show i ++ "!!"
184 se p env (Bind n b@(Pi t) sc)
185 | noOccurrence n sc = bracket p 2 $ se 10 env t ++ " -> " ++ se 10 ((n,b):env) sc
186 se p env (Bind n b sc) = bracket p 2 $ sb env n b ++ se 10 ((n,b):env) sc
187 se p env (App f a) = bracket p 1 $ se 1 env f ++ " " ++ se 0 env a
188 se p env (Set i) = "Set" ++ show i
190 sb env n (Lam t) = showb env "\\ " " => " n t
191 sb env n (Hole t) = showb env "? " ". " n t
192 sb env n (Pi t) = showb env "(" ") -> " n t
193 sb env n (PVar t) = showb env "! " ". " n t
194 sb env n (Let t v) = showbv env "let " " in " n t v
195 sb env n (Guess t v) = showbv env "?? " " in " n t v
197 showb env op sc n t = op ++ show n ++ " : " ++ se 10 env t ++ sc
198 showbv env op sc n t v = op ++ show n ++ " : " ++ se 10 env t ++ " = " ++
201 bracket outer inner str | inner > outer = "(" ++ str ++ ")"
204-- Check whether a term has any holes in it - impure if so
206pureTerm :: TT n -> Bool
207pureTerm (App f a) = pureTerm f && pureTerm a
208pureTerm (Bind n b sc) = pureBinder b && pureTerm sc where
209 pureBinder (Hole _) = False
210 pureBinder (Guess _ _) = False
211 pureBinder (Let t v) = pureTerm t && pureTerm v
212 pureBinder t = pureTerm (binderTy t)
215-- weaken a term by adding i to each de Bruijn index (i.e. lift it over i bindings)
217weakenTm :: Int -> TT n -> TT n
218weakenTm i t = wk i 0 t
219 where wk i min (V x) | x >= min = V (i + x)
220 wk i m (App f a) = App (wk i m f) (wk i m a)
221 wk i m (Bind x b sc) = Bind x (wkb i m b) (wk i (m + 1) sc)
223 wkb i m t = fmap (wk i m) t
225-- weaken an environment so that all the de Bruijn indices are correct according
226-- to the latest bound variable
228weakenEnv :: EnvTT n -> EnvTT n
229weakenEnv env = wk (length env - 1) env
231 wk i ((n, b) : bs) = (n, weakenTmB i b) : wk (i - 1) bs
232 weakenTmB i (Let t v) = Let (weakenTm i t) (weakenTm i v)
233 weakenTmB i (Guess t v) = Guess (weakenTm i t) (weakenTm i v)
234 weakenTmB i t = t { binderTy = weakenTm i (binderTy t) }
236weakenTmEnv :: Int -> EnvTT n -> EnvTT n
237weakenTmEnv i = map (\ (n, b) -> (n, fmap (weakenTm i) b))
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