on 89ffe9b help sign in
Idris-dev/src/Core.hs
239 lines 7.6 kB history raw
1{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, DeriveFunctor #-}
3module Core where
5import Control.Monad.State
6import Debug.Trace
8{- The language has:
9 * Full dependent types
10 * A hierarchy of universes, with cumulativity: Set : Set1, Set1 : Set2, ...
11 * Pattern matching letrec binding
12 * (primitive types defined externally)
14 Some technical stuff:
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.
19-}
21data Option = SetInSet
22 | CheckConv
23 deriving Eq
25data TC a = OK a
26 | Error String -- TMP! Make this an informative data structure
27 deriving (Eq, Functor)
29instance Show a => Show (TC a) where
30 show (OK x) = show x
31 show (Error str) = "Error: " ++ str
33-- at some point, this instance should also carry type checking options
34-- (e.g. Set:Set)
36instance Monad TC where
37 return = OK
38 x >>= k = case x of
39 OK v -> k v
40 Error e -> Error e
41 fail = Error
43instance MonadPlus TC where
44 mzero = Error "Unknown error"
45 (OK x) `mplus` _ = OK x
46 _ `mplus` (OK y) = OK y
47 err `mplus` _ = err
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]
56 | MN Int String
57 deriving Eq
59instance Show Name where
60 show (UN [n]) = n
61 show (UN (n:ns)) = show (UN [n]) ++ "." ++ show (UN ns)
62 show (MN i s) = "{" ++ s ++ show i ++ "}"
64data Raw = Var Name
65 | RBind Name (Binder Raw) Raw
66 | RApp Raw Raw
67 | RSet Int
68 deriving Show
70data Binder b = Lam { binderTy :: b }
71 | Pi { binderTy :: b }
72 | Let { binderTy :: b,
73 binderVal :: b }
74 | NLet { binderTy :: b,
75 binderVal :: b }
76 | Hole { binderTy :: b}
77 | Guess { binderTy :: b,
78 binderVal :: b }
79 | PVar { binderTy :: b }
80 deriving (Show, Eq, Functor)
82raw_apply :: Raw -> [Raw] -> Raw
83raw_apply f [] = f
84raw_apply f (a : as) = raw_apply (RApp f a) as
86data RawFun = RawFun { rtype :: Raw,
87 rval :: Raw
88 }
89 deriving Show
91data RawDatatype = RDatatype Name Raw [(Name, Raw)]
92 deriving Show
94data RDef = RFunction RawFun
95 | RConst Raw
96 | RData RawDatatype
97 deriving Show
99type RProgram = [(Name, RDef)]
101-- WELL TYPED TERMS ---------------------------------------------------------
103data NameType = Bound | Ref | DCon Int Int | TCon Int
104 deriving (Show, Eq)
106-- FIXME: Consider changing this to birectional, with type annotations.
108data TT n = P NameType n (TT n) -- embed type
109 | V Int
110 | Bind n (Binder (TT n)) (TT n)
111 | App (TT n) (TT n) -- function, function type, arg
112 | Set Int
113 deriving (Functor, Eq)
115type EnvTT n = [(n, Binder (TT n))]
117data Datatype n = Data { d_typename :: n,
118 d_type :: (TT 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
128 where
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
136 no' i _ = True
138-- Return the arity of a (normalised) type
140arity :: TT n -> Int
141arity (Bind n (Pi t) sc) = 1 + arity sc
142arity _ = 0
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
152forget tm = fe [] tm
153 where
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)
157 (fe (n:env) sc)
158 fe env (App f a) = RApp (fe env f) (fe env a)
159 fe env (Set i) = RSet i
161type Term = TT Name
162type Type = Term
164type Env = EnvTT Name
166-- an environment with de Bruijn indices 'normalised' so that they all refer to
167-- this environment
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 ++ " = " ++
199 se 10 env v ++ sc
201 bracket outer inner str | inner > outer = "(" ++ str ++ ")"
202 | otherwise = 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)
213pureTerm _ = True
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)
222 wk i m t = t
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
230 where wk i [] = []
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))