1{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, DeriveFunctor #-}
5import Control.Monad.State
7import qualified Data.Map as Map
10import qualified Data.Binary as B
11import Data.Binary hiding (get, put)
13import Util.Pretty hiding (Str)
16 * Full dependent types
17 * A hierarchy of universes, with cumulativity: Type : Type1, Type1 : Type2, ...
18 * Pattern matching letrec binding
19 * (primitive types defined externally)
22 * Typechecker is kept as simple as possible
23 - no unification, just a checker for incomplete terms.
24 * We have a simple collection of tactics which we use to elaborate source
25 programs with implicit syntax into fully explicit terms.
28data Option = TTypeInTType
32data FC = FC { fc_fname :: String,
36deriving instance Binary FC
39instance Sized FC where
40 size (FC f l) = 1 + length f
43 show (FC f l) = f ++ ":" ++ show l
47 | CantUnify Bool Term Term Err [(Name, Type)] Int
48 -- Int is 'score' - how much we did unify
49 -- Bool indicates recoverability, True indicates more info may make
50 -- unification succeed
51 | InfiniteUnify Name Term [(Name, Type)]
52 | CantConvert Term Term [(Name, Type)]
55 | NotInjective Term Term Term
57 | CantResolveAlts [String]
62 | NonCollapsiblePostulate Name
67instance Sized Err where
68 size (Msg msg) = length msg
69 size (InternalMsg msg) = length msg
70 size (CantUnify _ left right err _ score) = size left + size right + size err
71 size (InfiniteUnify _ right _) = size right
72 size (CantConvert left right _) = size left + size right
73 size (NoSuchVariable name) = size name
74 size (NoTypeDecl name) = size name
75 size (NotInjective l c r) = size l + size c + size r
76 size (CantResolve trm) = size trm
77 size (CantResolveAlts _) = 1
78 size (IncompleteTerm trm) = size trm
79 size UniverseError = 1
80 size ProgramLineComment = 1
81 size (At fc err) = size fc + size err
85score (CantUnify _ _ _ m _ s) = s + score m
86score (CantResolve _) = 20
87score (NoSuchVariable _) = 1000
90instance Show Err where
92 show (InternalMsg s) = "Internal error: " ++ show s
93 show (CantUnify _ l r e sc i) = "CantUnify " ++ show l ++ " " ++ show r ++ " "
94 ++ show e ++ " in " ++ show sc ++ " " ++ show i
95 show (Inaccessible n) = show n ++ " is not an accessible pattern variable"
98instance Pretty Err where
99 pretty (Msg m) = text m
100 pretty (CantUnify _ l r e _ i) =
101 if size l + size r > breakingSize then
102 text "Cannot unify" <+> colon $$
103 nest nestingSize (pretty l <+> text "and" <+> pretty r) $$
104 nest nestingSize (text "where" <+> pretty e <+> text "with" <+> (text . show $ i))
106 text "Cannot unify" <+> colon <+> pretty l <+> text "and" <+> pretty r $$
107 nest nestingSize (text "where" <+> pretty e <+> text "with" <+> (text . show $ i))
108 pretty _ = text "Error"
112 deriving (Eq, Functor)
114instance Pretty a => Pretty (TC a) where
115 pretty (OK ok) = pretty ok
117 if size err > breakingSize then
118 text "Error" <+> colon $$ (nest nestingSize $ pretty err)
120 text "Error" <+> colon <+> pretty err
122instance Show a => Show (TC a) where
124 show (Error str) = "Error: " ++ show str
126-- at some point, this instance should also carry type checking options
129instance Monad TC where
134 fail e = Error (InternalMsg e)
139trun :: FC -> TC a -> TC a
141trun fc (Error e) = Error (At fc e)
143instance MonadPlus TC where
144 mzero = fail "Unknown error"
145 (OK x) `mplus` _ = OK x
146 _ `mplus` (OK y) = OK y
149discard :: Monad m => m a -> m ()
150discard f = f >> return ()
152showSep :: String -> [String] -> String
155showSep sep (x:xs) = x ++ sep ++ showSep sep xs
157pmap f (x, y) = (f x, f y)
159traceWhen True msg a = trace msg a
160traceWhen False _ a = a
162-- RAW TERMS ----------------------------------------------------------------
164-- Names are hierarchies of strings, describing scope (so no danger of
165-- duplicate names, but need to be careful on lookup).
166-- Also MN for machine chosen names
169 | NS Name [String] -- root, namespaces
171 | NErased -- name of somethng which is never used in scope
174deriving instance Binary Name
177instance Sized Name where
179 size (NS n els) = 1 + length els
183instance Pretty Name where
184 pretty (UN n) = text n
185 pretty (NS n s) = pretty n
186 pretty (MN i s) = lbrace <+> text s <+> (text . show $ i) <+> rbrace
188instance Show Name where
190 show (NS n s) = showSep "." (reverse s) ++ "." ++ show n
191 show (MN i s) = "{" ++ s ++ show i ++ "}"
194-- Contexts allow us to map names to things. A root name maps to a collection
195-- of things in different namespaces with that name.
197type Ctxt a = Map.Map Name (Map.Map Name a)
198emptyContext = Map.empty
200tcname (UN ('@':_)) = True
201tcname (NS n _) = tcname n
204implicitable (NS n _) = implicitable n
205implicitable (UN (x:xs)) = isLower x
206implicitable _ = False
211addDef :: Name -> a -> Ctxt a -> Ctxt a
212addDef n v ctxt = case Map.lookup (nsroot n) ctxt of
213 Nothing -> Map.insert (nsroot n)
214 (Map.insert n v Map.empty) ctxt
215 Just xs -> Map.insert (nsroot n)
216 (Map.insert n v xs) ctxt
218{- lookup a name in the context, given an optional namespace.
219 The name (n) may itself have a (partial) namespace given.
221 Rules for resolution:
222 - if an explicit namespace is given, return the names which match it. If none
223 match, return all names.
224 - if the name has has explicit namespace given, return the names which match it
225 and ignore the given namespace.
226 - otherwise, return all names.
230lookupCtxtName :: Maybe [String] -> Name -> Ctxt a -> [(Name, a)]
231lookupCtxtName nspace n ctxt = case Map.lookup (nsroot n) ctxt of
232 Just xs -> filterNS (Map.toList xs)
236 filterNS ((found, v) : xs)
237 | nsmatch n found = (found, v) : filterNS xs
238 | otherwise = filterNS xs
240 nsmatch (NS n ns) (NS p ps) = ns `isPrefixOf` ps
241 nsmatch (NS _ _) _ = False
242 nsmatch looking found = True
244lookupCtxt :: Maybe [String] -> Name -> Ctxt a -> [a]
245lookupCtxt ns n ctxt = map snd (lookupCtxtName ns n ctxt)
247updateDef :: Name -> (a -> a) -> Ctxt a -> Ctxt a
249 = let ds = lookupCtxtName Nothing n ctxt in
250 foldr (\ (n, t) c -> addDef n (f t) c) ctxt ds
252toAlist :: Ctxt a -> [(Name, a)]
253toAlist ctxt = let allns = map snd (Map.toList ctxt) in
254 concat (map (Map.toList) allns)
256addAlist :: Show a => [(Name, a)] -> Ctxt a -> Ctxt a
257addAlist [] ctxt = ctxt
258addAlist ((n, tm) : ds) ctxt = addDef n tm (addAlist ds ctxt)
260data Const = I Int | BI Integer | Fl Double | Ch Char | Str String
261 | IType | BIType | FlType | ChType | StrType
263 | B8 Word8 | B16 Word16 | B32 Word32 | B64 Word64
264 | B8Type | B16Type | B32Type | B64Type
266 | PtrType | VoidType | Forgot
269deriving instance Binary Const
272instance Sized Const where
275instance Pretty Const where
276 pretty (I i) = text . show $ i
277 pretty (BI i) = text . show $ i
278 pretty (Fl f) = text . show $ f
279 pretty (Ch c) = text . show $ c
280 pretty (Str s) = text s
281 pretty IType = text "Int"
282 pretty BIType = text "BigInt"
283 pretty FlType = text "Float"
284 pretty ChType = text "Char"
285 pretty StrType = text "String"
286 pretty PtrType = text "Ptr"
287 pretty VoidType = text "Void"
288 pretty Forgot = text "Forgot"
289 pretty B8Type = text "Bits8"
290 pretty B16Type = text "Bits16"
291 pretty B32Type = text "Bits32"
292 pretty B64Type = text "Bits64"
295 | RBind Name (Binder Raw) Raw
302instance Sized Raw where
304 size (RBind name bind right) = 1 + size bind + size right
305 size (RApp left right) = 1 + size left + size right
307 size (RForce raw) = 1 + size raw
308 size (RConstant const) = size const
310instance Pretty Raw where
314deriving instance Binary Raw
317data Binder b = Lam { binderTy :: b }
318 | Pi { binderTy :: b }
319 | Let { binderTy :: b,
321 | NLet { binderTy :: b,
323 | Hole { binderTy :: b}
324 | GHole { binderTy :: b}
325 | Guess { binderTy :: b,
327 | PVar { binderTy :: b }
328 | PVTy { binderTy :: b }
329 deriving (Show, Eq, Ord, Functor)
331deriving instance Binary Binder
334instance Sized a => Sized (Binder a) where
335 size (Lam ty) = 1 + size ty
336 size (Pi ty) = 1 + size ty
337 size (Let ty val) = 1 + size ty + size val
338 size (NLet ty val) = 1 + size ty + size val
339 size (Hole ty) = 1 + size ty
340 size (GHole ty) = 1 + size ty
341 size (Guess ty val) = 1 + size ty + size val
342 size (PVar ty) = 1 + size ty
343 size (PVTy ty) = 1 + size ty
345fmapMB :: Monad m => (a -> m b) -> Binder a -> m (Binder b)
346fmapMB f (Let t v) = liftM2 Let (f t) (f v)
347fmapMB f (NLet t v) = liftM2 NLet (f t) (f v)
348fmapMB f (Guess t v) = liftM2 Guess (f t) (f v)
349fmapMB f (Lam t) = liftM Lam (f t)
350fmapMB f (Pi t) = liftM Pi (f t)
351fmapMB f (Hole t) = liftM Hole (f t)
352fmapMB f (GHole t) = liftM GHole (f t)
353fmapMB f (PVar t) = liftM PVar (f t)
354fmapMB f (PVTy t) = liftM PVTy (f t)
356raw_apply :: Raw -> [Raw] -> Raw
358raw_apply f (a : as) = raw_apply (RApp f a) as
360raw_unapply :: Raw -> (Raw, [Raw])
361raw_unapply t = ua [] t where
362 ua args (RApp f a) = ua (a:args) f
363 ua args t = (t, args)
365data RawFun = RawFun { rtype :: Raw,
370data RawDatatype = RDatatype Name Raw [(Name, Raw)]
373data RDef = RFunction RawFun
378type RProgram = [(Name, RDef)]
380-- WELL TYPED TERMS ---------------------------------------------------------
382data UExp = UVar Int -- universe variable
383 | UVal Int -- explicit universe level
386instance Sized UExp where
389-- We assume that universe levels have been checked, so anything external
390-- can just have the same universe variable and we won't get any new
393instance Binary UExp where
395 get = return (UVar (-1))
397instance Show UExp where
398 show (UVar x) | x < 26 = [toEnum (x + fromEnum 'a')]
399 | otherwise = toEnum ((x `mod` 26) + fromEnum 'a') : show (x `div` 26)
400 show (UVal x) = show x
401-- show (UMax l r) = "max(" ++ show l ++ ", " ++ show r ++")"
403data UConstraint = ULT UExp UExp
407instance Show UConstraint where
408 show (ULT x y) = show x ++ " < " ++ show y
409 show (ULE x y) = show x ++ " <= " ++ show y
411type UCs = (Int, [UConstraint])
413data NameType = Bound | Ref | DCon Int Int | TCon Int Int
416deriving instance Binary NameType
419instance Sized NameType where
422instance Pretty NameType where
425instance Eq NameType where
426 Bound == Bound = True
428 DCon _ a == DCon _ b = (a == b) -- ignore tag
429 TCon _ a == TCon _ b = (a == b) -- ignore tag
432data TT n = P NameType n (TT n) -- embed type
434 | Bind n (Binder (TT n)) (TT n)
435 | App (TT n) (TT n) -- function, function type, arg
437 | Proj (TT n) Int -- argument projection; runtime only
439 | Impossible -- special case for totality checking
441 deriving (Ord, Functor)
443deriving instance Binary TT
446class TermSize a where
447 termsize :: Name -> a -> Int
449instance TermSize a => TermSize [a] where
451 termsize n (x : xs) = termsize n x + termsize n xs
453instance TermSize (TT Name) where
455 | x == n = 1000000 -- recursive => really big
458 termsize n (Bind n' (Let t v) sc)
459 = let rn = if n == n' then MN 0 "noname" else n in
460 termsize rn v + termsize rn sc
461 termsize n (App f a) = termsize n f + termsize n a
464instance Sized a => Sized (TT a) where
465 size (P name n trm) = 1 + size name + size n + size trm
467 size (Bind nm binder bdy) = 1 + size nm + size binder + size bdy
468 size (App l r) = 1 + size l + size r
469 size (Constant c) = size c
471 size (TType u) = 1 + size u
473instance Pretty a => Pretty (TT a) where
474 pretty _ = text "test"
476type EnvTT n = [(n, Binder (TT n))]
478data Datatype n = Data { d_typename :: n,
481 d_cons :: [(n, TT n)] }
482 deriving (Show, Functor, Eq)
484instance Eq n => Eq (TT n) where
485 (==) (P xt x _) (P yt y _) = xt == yt && x == y
486 (==) (V x) (V y) = x == y
487 (==) (Bind _ xb xs) (Bind _ yb ys) = xb == yb && xs == ys
488 (==) (App fx ax) (App fy ay) = fx == fy && ax == ay
489 (==) (TType _) (TType _) = True -- deal with constraints later
490 (==) (Constant x) (Constant y) = x == y
491 (==) (Proj x i) (Proj y j) = x == y && i == j
496-- A few handy operations on well typed terms:
498isInjective :: TT n -> Bool
499isInjective (P (DCon _ _) _ _) = True
500isInjective (P (TCon _ _) _ _) = True
501isInjective (Constant _) = True
502isInjective (TType x) = True
503isInjective (Bind _ (Pi _) sc) = True
504isInjective (App f a) = isInjective f
507-- Count the number of instances of a de Bruijn index in a term
508vinstances :: Int -> TT n -> Int
509vinstances i (V x) | i == x = 1
510vinstances i (App f a) = vinstances i f + vinstances i a
511vinstances i (Bind x b sc) = instancesB b + vinstances (i + 1) sc
512 where instancesB (Let t v) = vinstances i v
516instantiate :: TT n -> TT n -> TT n
517instantiate e = subst 0 where
518 subst i (V x) | i == x = e
519 subst i (Bind x b sc) = Bind x (fmap (subst i) b) (subst (i+1) sc)
520 subst i (App f a) = App (subst i f) (subst i a)
521 subst i (Proj x idx) = Proj (subst i x) idx
524explicitNames :: TT n -> TT n
525explicitNames (Bind x b sc) = let b' = fmap explicitNames b in
527 (explicitNames (instantiate
528 (P Bound x (binderTy b')) sc))
529explicitNames (App f a) = App (explicitNames f) (explicitNames a)
530explicitNames (Proj x idx) = Proj (explicitNames x) idx
533pToV :: Eq n => n -> TT n -> TT n
535pToV' n i (P _ x _) | n == x = V i
536pToV' n i (Bind x b sc)
537-- We can assume the inner scope has been pToVed already, so continue to
538-- resolve names from the *outer* scope which may happen to have the same id.
539-- | n == x = Bind x (fmap (pToV' n i) b) sc
540 | otherwise = Bind x (fmap (pToV' n i) b) (pToV' n (i+1) sc)
541pToV' n i (App f a) = App (pToV' n i f) (pToV' n i a)
542pToV' n i (Proj t idx) = Proj (pToV' n i t) idx
545-- Convert several names. First in the list comes out as V 0
546pToVs :: Eq n => [n] -> TT n -> TT n
547pToVs ns tm = pToVs' ns tm 0 where
549 pToVs' (n:ns) tm i = pToV' n i (pToVs' ns tm (i+1))
553 vToP' env (V i) = let (n, b) = (env !! i) in
554 P Bound n (binderTy b)
555 vToP' env (Bind n b sc) = let b' = fmap (vToP' env) b in
556 Bind n b' (vToP' ((n, b'):env) sc)
557 vToP' env (App f a) = App (vToP' env f) (vToP' env a)
560finalise :: Eq n => TT n -> TT n
561finalise (Bind x b sc) = Bind x (fmap finalise b) (pToV x (finalise sc))
562finalise (App f a) = App (finalise f) (finalise a)
565subst :: Eq n => n -> TT n -> TT n -> TT n
566subst n v tm = instantiate v (pToV n tm)
568substNames :: Eq n => [(n, TT n)] -> TT n -> TT n
570substNames ((n, tm) : xs) t = subst n tm (substNames xs t)
572substTerm :: Eq n => TT n -> TT n -> TT n -> TT n
573substTerm old new = st where
574 st t | t == old = new
575 st (App f a) = App (st f) (st a)
576 st (Bind x b sc) = Bind x (fmap st b) (st sc)
579-- Returns true if V 0 and bound name n do not occur in the term
581noOccurrence :: Eq n => n -> TT n -> Bool
582noOccurrence n t = no' 0 t
584 no' i (V x) = not (i == x)
585 no' i (P Bound x _) = not (n == x)
586 no' i (Bind n b sc) = noB' i b && no' (i+1) sc
587 where noB' i (Let t v) = no' i t && no' i v
588 noB' i (Guess t v) = no' i t && no' i v
589 noB' i b = no' i (binderTy b)
590 no' i (App f a) = no' i f && no' i a
591 no' i (Proj x _) = no' i x
594-- Returns all names used free in the term
596freeNames :: Eq n => TT n -> [n]
597freeNames (P _ n _) = [n]
598freeNames (Bind n (Let t v) sc) = nub $ freeNames v ++ (freeNames sc \\ [n])
600freeNames (Bind n b sc) = nub $ freeNames (binderTy b) ++ (freeNames sc \\ [n])
601freeNames (App f a) = nub $ freeNames f ++ freeNames a
602freeNames (Proj x i) = nub $ freeNames x
605-- Return the arity of a (normalised) type
608arity (Bind n (Pi t) sc) = 1 + arity sc
611-- deconstruct an application; returns the function and a list of arguments
613unApply :: TT n -> (TT n, [TT n])
614unApply t = ua [] t where
615 ua args (App f a) = ua (a:args) f
616 ua args t = (t, args)
618mkApp :: TT n -> [TT n] -> TT n
620mkApp f (a:as) = mkApp (App f a) as
622forget :: TT Name -> Raw
625 fe env (P _ n _) = Var n
626 fe env (V i) = Var (env !! i)
627 fe env (Bind n b sc) = RBind n (fmap (fe env) b)
629 fe env (App f a) = RApp (fe env f) (fe env a)
632 fe env (TType i) = RType
633 fe env Erased = RConstant Forgot
635bindAll :: [(n, Binder (TT n))] -> TT n -> TT n
637bindAll ((n, b) : bs) t = Bind n b (bindAll bs t)
639bindTyArgs :: (TT n -> Binder (TT n)) -> [(n, TT n)] -> TT n -> TT n
640bindTyArgs b xs = bindAll (map (\ (n, ty) -> (n, b ty)) xs)
642getArgTys :: TT n -> [(n, TT n)]
643getArgTys (Bind n (Pi t) sc) = (n, t) : getArgTys sc
646getRetTy :: TT n -> TT n
647getRetTy (Bind n (PVar _) sc) = getRetTy sc
648getRetTy (Bind n (PVTy _) sc) = getRetTy sc
649getRetTy (Bind n (Pi _) sc) = getRetTy sc
652uniqueName :: Name -> [Name] -> Name
653uniqueName n hs | n `elem` hs = uniqueName (nextName n) hs
656uniqueBinders :: [Name] -> TT Name -> TT Name
657uniqueBinders ns (Bind n b sc)
658 = let n' = uniqueName n ns in
659 Bind n' (fmap (uniqueBinders (n':ns)) b) (uniqueBinders ns sc)
660uniqueBinders ns (App f a) = App (uniqueBinders ns f) (uniqueBinders ns a)
661uniqueBinders ns t = t
664nextName (NS x s) = NS (nextName x) s
665nextName (MN i n) = MN (i+1) n
666nextName (UN x) = let (num', nm') = span isDigit (reverse x)
668 num = readN (reverse num') in
669 UN (nm ++ show (num+1))
679-- an environment with de Bruijn indices 'normalised' so that they all refer to
682newtype WkEnvTT n = Wk (EnvTT n)
683type WkEnv = WkEnvTT Name
685instance (Eq n, Show n) => Show (TT n) where
686 show t = showEnv [] t
688instance Show Const where
690 show (BI i) = show i ++ "L"
693 show (Str s) = show s
695 show (B16 x) = show x
696 show (B32 x) = show x
697 show (B64 x) = show x
699 show BIType = "Integer"
700 show FlType = "Float"
702 show StrType = "String"
704 show B8Type = "Bits8"
705 show B16Type = "Bits16"
706 show B32Type = "Bits32"
707 show B64Type = "Bits64"
708 show VoidType = "Void"
710showEnv env t = showEnv' env t False
711showEnvDbg env t = showEnv' env t True
713prettyEnv env t = prettyEnv' env t False
715 prettyEnv' env t dbg = prettySe 10 env t dbg
717 bracket outer inner p
718 | inner > outer = lparen <> p <> rparen
721 prettySe p env (P nt n t) debug =
724 lbrack <+> pretty nt <+> colon <+> prettySe 10 env t debug <+> rbrack
727 prettySe p env (V i) debug
730 text . show . fst $ env!!i
732 lbrack <+> text (show i) <+> rbrack
733 | otherwise = text "unbound" <+> text (show i) <+> text "!"
734 prettySe p env (Bind n b@(Pi t) sc) debug
735 | noOccurrence n sc && not debug =
736 bracket p 2 $ prettySb env n b debug <> prettySe 10 ((n, b):env) sc debug
737 prettySe p env (Bind n b sc) debug =
738 bracket p 2 $ prettySb env n b debug <> prettySe 10 ((n, b):env) sc debug
739 prettySe p env (App f a) debug =
740 bracket p 1 $ prettySe 1 env f debug <+> prettySe 0 env a debug
741 prettySe p env (Proj x i) debug =
742 prettySe 1 env x debug <+> text ("!" ++ show i)
743 prettySe p env (Constant c) debug = pretty c
744 prettySe p env Erased debug = text "[_]"
745 prettySe p env (TType i) debug = text "Type" <+> (text . show $ i)
747 prettySb env n (Lam t) = prettyB env "λ" "=>" n t
748 prettySb env n (Hole t) = prettyB env "?defer" "." n t
749 prettySb env n (Pi t) = prettyB env "(" ") ->" n t
750 prettySb env n (PVar t) = prettyB env "pat" "." n t
751 prettySb env n (PVTy t) = prettyB env "pty" "." n t
752 prettySb env n (Let t v) = prettyBv env "let" "in" n t v
753 prettySb env n (Guess t v) = prettyBv env "??" "in" n t v
755 prettyB env op sc n t debug =
756 text op <> pretty n <+> colon <+> prettySe 10 env t debug <> text sc
758 prettyBv env op sc n t v debug =
759 text op <> pretty n <+> colon <+> prettySe 10 env t debug <+> text "=" <+>
760 prettySe 10 env v debug <> text sc
763showEnv' env t dbg = se 10 env t where
764 se p env (P nt n t) = show n
765 ++ if dbg then "{" ++ show nt ++ " : " ++ se 10 env t ++ "}" else ""
766 se p env (V i) | i < length env = (show $ fst $ env!!i) ++
767 if dbg then "{" ++ show i ++ "}" else ""
768 | otherwise = "!!V " ++ show i ++ "!!"
769 se p env (Bind n b@(Pi t) sc)
770 | noOccurrence n sc && not dbg = bracket p 2 $ se 1 env t ++ " -> " ++ se 10 ((n,b):env) sc
771 se p env (Bind n b sc) = bracket p 2 $ sb env n b ++ se 10 ((n,b):env) sc
772 se p env (App f a) = bracket p 1 $ se 1 env f ++ " " ++ se 0 env a
773 se p env (Proj x i) = se 1 env x ++ "!" ++ show i
774 se p env (Constant c) = show c
775 se p env Erased = "[__]"
776 se p env Impossible = "[impossible]"
777 se p env (TType i) = "Type " ++ show i
779 sb env n (Lam t) = showb env "\\ " " => " n t
780 sb env n (Hole t) = showb env "? " ". " n t
781 sb env n (GHole t) = showb env "?defer " ". " n t
782 sb env n (Pi t) = showb env "(" ") -> " n t
783 sb env n (PVar t) = showb env "pat " ". " n t
784 sb env n (PVTy t) = showb env "pty " ". " n t
785 sb env n (Let t v) = showbv env "let " " in " n t v
786 sb env n (Guess t v) = showbv env "?? " " in " n t v
788 showb env op sc n t = op ++ show n ++ " : " ++ se 10 env t ++ sc
789 showbv env op sc n t v = op ++ show n ++ " : " ++ se 10 env t ++ " = " ++
792 bracket outer inner str | inner > outer = "(" ++ str ++ ")"
795-- Check whether a term has any holes in it - impure if so
797pureTerm :: TT n -> Bool
798pureTerm (App f a) = pureTerm f && pureTerm a
799pureTerm (Bind n b sc) = pureBinder b && pureTerm sc where
800 pureBinder (Hole _) = False
801 pureBinder (Guess _ _) = False
802 pureBinder (Let t v) = pureTerm t && pureTerm v
803 pureBinder t = pureTerm (binderTy t)
806-- weaken a term by adding i to each de Bruijn index (i.e. lift it over i bindings)
808weakenTm :: Int -> TT n -> TT n
809weakenTm i t = wk i 0 t
810 where wk i min (V x) | x >= min = V (i + x)
811 wk i m (App f a) = App (wk i m f) (wk i m a)
812 wk i m (Bind x b sc) = Bind x (wkb i m b) (wk i (m + 1) sc)
814 wkb i m t = fmap (wk i m) t
816-- weaken an environment so that all the de Bruijn indices are correct according
817-- to the latest bound variable
819weakenEnv :: EnvTT n -> EnvTT n
820weakenEnv env = wk (length env - 1) env
822 wk i ((n, b) : bs) = (n, weakenTmB i b) : wk (i - 1) bs
823 weakenTmB i (Let t v) = Let (weakenTm i t) (weakenTm i v)
824 weakenTmB i (Guess t v) = Guess (weakenTm i t) (weakenTm i v)
825 weakenTmB i t = t { binderTy = weakenTm i (binderTy t) }
827weakenTmEnv :: Int -> EnvTT n -> EnvTT n
828weakenTmEnv i = map (\ (n, b) -> (n, fmap (weakenTm i) b))
830orderPats :: Term -> Term
831orderPats tm = op [] tm
833 op ps (Bind n (PVar t) sc) = op ((n, PVar t) : ps) sc
834 op ps (Bind n (Hole t) sc) = op ((n, Hole t) : ps) sc
835 op ps sc = bindAll (map (\ (n, t) -> (n, t)) (sortP ps)) sc
837 sortP ps = pick [] (reverse ps)
839 namesIn (P _ n _) = [n]
840 namesIn (Bind n b t) = nub $ nb b ++ (namesIn t \\ [n])
841 where nb (Let t v) = nub (namesIn t) ++ nub (namesIn v)
842 nb (Guess t v) = nub (namesIn t) ++ nub (namesIn v)
843 nb t = namesIn (binderTy t)
844 namesIn (App f a) = nub (namesIn f ++ namesIn a)
847 pick acc [] = reverse acc
848 pick acc ((n, t) : ps) = pick (insert n t acc) ps
850 insert n t [] = [(n, t)]
851 insert n t ((n',t') : ps)
852 | n `elem` (namesIn (binderTy t') ++
853 concatMap namesIn (map (binderTy . snd) ps))
854 = (n', t') : insert n t ps
855 | 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