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 | W8 Word8 | W16 Word16
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 W8Type = text "Word8"
290 pretty W16Type = text "Word16"
293 | RBind Name (Binder Raw) Raw
300instance Sized Raw where
302 size (RBind name bind right) = 1 + size bind + size right
303 size (RApp left right) = 1 + size left + size right
305 size (RForce raw) = 1 + size raw
306 size (RConstant const) = size const
308instance Pretty Raw where
312deriving instance Binary Raw
315data Binder b = Lam { binderTy :: b }
316 | Pi { binderTy :: b }
317 | Let { binderTy :: b,
319 | NLet { binderTy :: b,
321 | Hole { binderTy :: b}
322 | GHole { binderTy :: b}
323 | Guess { binderTy :: b,
325 | PVar { binderTy :: b }
326 | PVTy { binderTy :: b }
327 deriving (Show, Eq, Ord, Functor)
329deriving instance Binary Binder
332instance Sized a => Sized (Binder a) where
333 size (Lam ty) = 1 + size ty
334 size (Pi ty) = 1 + size ty
335 size (Let ty val) = 1 + size ty + size val
336 size (NLet ty val) = 1 + size ty + size val
337 size (Hole ty) = 1 + size ty
338 size (GHole ty) = 1 + size ty
339 size (Guess ty val) = 1 + size ty + size val
340 size (PVar ty) = 1 + size ty
341 size (PVTy ty) = 1 + size ty
343fmapMB :: Monad m => (a -> m b) -> Binder a -> m (Binder b)
344fmapMB f (Let t v) = liftM2 Let (f t) (f v)
345fmapMB f (NLet t v) = liftM2 NLet (f t) (f v)
346fmapMB f (Guess t v) = liftM2 Guess (f t) (f v)
347fmapMB f (Lam t) = liftM Lam (f t)
348fmapMB f (Pi t) = liftM Pi (f t)
349fmapMB f (Hole t) = liftM Hole (f t)
350fmapMB f (GHole t) = liftM GHole (f t)
351fmapMB f (PVar t) = liftM PVar (f t)
352fmapMB f (PVTy t) = liftM PVTy (f t)
354raw_apply :: Raw -> [Raw] -> Raw
356raw_apply f (a : as) = raw_apply (RApp f a) as
358raw_unapply :: Raw -> (Raw, [Raw])
359raw_unapply t = ua [] t where
360 ua args (RApp f a) = ua (a:args) f
361 ua args t = (t, args)
363data RawFun = RawFun { rtype :: Raw,
368data RawDatatype = RDatatype Name Raw [(Name, Raw)]
371data RDef = RFunction RawFun
376type RProgram = [(Name, RDef)]
378-- WELL TYPED TERMS ---------------------------------------------------------
380data UExp = UVar Int -- universe variable
381 | UVal Int -- explicit universe level
384instance Sized UExp where
387-- We assume that universe levels have been checked, so anything external
388-- can just have the same universe variable and we won't get any new
391instance Binary UExp where
393 get = return (UVar (-1))
395instance Show UExp where
396 show (UVar x) | x < 26 = [toEnum (x + fromEnum 'a')]
397 | otherwise = toEnum ((x `mod` 26) + fromEnum 'a') : show (x `div` 26)
398 show (UVal x) = show x
399-- show (UMax l r) = "max(" ++ show l ++ ", " ++ show r ++")"
401data UConstraint = ULT UExp UExp
405instance Show UConstraint where
406 show (ULT x y) = show x ++ " < " ++ show y
407 show (ULE x y) = show x ++ " <= " ++ show y
409type UCs = (Int, [UConstraint])
411data NameType = Bound | Ref | DCon Int Int | TCon Int Int
414deriving instance Binary NameType
417instance Sized NameType where
420instance Pretty NameType where
423instance Eq NameType where
424 Bound == Bound = True
426 DCon _ a == DCon _ b = (a == b) -- ignore tag
427 TCon _ a == TCon _ b = (a == b) -- ignore tag
430data TT n = P NameType n (TT n) -- embed type
432 | Bind n (Binder (TT n)) (TT n)
433 | App (TT n) (TT n) -- function, function type, arg
435 | Proj (TT n) Int -- argument projection; runtime only
437 | Impossible -- special case for totality checking
439 deriving (Ord, Functor)
441deriving instance Binary TT
444class TermSize a where
445 termsize :: Name -> a -> Int
447instance TermSize a => TermSize [a] where
449 termsize n (x : xs) = termsize n x + termsize n xs
451instance TermSize (TT Name) where
453 | x == n = 1000000 -- recursive => really big
456 termsize n (Bind n' (Let t v) sc)
457 = let rn = if n == n' then MN 0 "noname" else n in
458 termsize rn v + termsize rn sc
459 termsize n (App f a) = termsize n f + termsize n a
462instance Sized a => Sized (TT a) where
463 size (P name n trm) = 1 + size name + size n + size trm
465 size (Bind nm binder bdy) = 1 + size nm + size binder + size bdy
466 size (App l r) = 1 + size l + size r
467 size (Constant c) = size c
469 size (TType u) = 1 + size u
471instance Pretty a => Pretty (TT a) where
472 pretty _ = text "test"
474type EnvTT n = [(n, Binder (TT n))]
476data Datatype n = Data { d_typename :: n,
479 d_cons :: [(n, TT n)] }
480 deriving (Show, Functor, Eq)
482instance Eq n => Eq (TT n) where
483 (==) (P xt x _) (P yt y _) = xt == yt && x == y
484 (==) (V x) (V y) = x == y
485 (==) (Bind _ xb xs) (Bind _ yb ys) = xb == yb && xs == ys
486 (==) (App fx ax) (App fy ay) = fx == fy && ax == ay
487 (==) (TType _) (TType _) = True -- deal with constraints later
488 (==) (Constant x) (Constant y) = x == y
489 (==) (Proj x i) (Proj y j) = x == y && i == j
494-- A few handy operations on well typed terms:
496isInjective :: TT n -> Bool
497isInjective (P (DCon _ _) _ _) = True
498isInjective (P (TCon _ _) _ _) = True
499isInjective (Constant _) = True
500isInjective (TType x) = True
501isInjective (Bind _ (Pi _) sc) = True
502isInjective (App f a) = isInjective f
505-- Count the number of instances of a de Bruijn index in a term
506vinstances :: Int -> TT n -> Int
507vinstances i (V x) | i == x = 1
508vinstances i (App f a) = vinstances i f + vinstances i a
509vinstances i (Bind x b sc) = instancesB b + vinstances (i + 1) sc
510 where instancesB (Let t v) = vinstances i v
514instantiate :: TT n -> TT n -> TT n
515instantiate e = subst 0 where
516 subst i (V x) | i == x = e
517 subst i (Bind x b sc) = Bind x (fmap (subst i) b) (subst (i+1) sc)
518 subst i (App f a) = App (subst i f) (subst i a)
519 subst i (Proj x idx) = Proj (subst i x) idx
522explicitNames :: TT n -> TT n
523explicitNames (Bind x b sc) = let b' = fmap explicitNames b in
525 (explicitNames (instantiate
526 (P Bound x (binderTy b')) sc))
527explicitNames (App f a) = App (explicitNames f) (explicitNames a)
528explicitNames (Proj x idx) = Proj (explicitNames x) idx
531pToV :: Eq n => n -> TT n -> TT n
533pToV' n i (P _ x _) | n == x = V i
534pToV' n i (Bind x b sc)
535-- We can assume the inner scope has been pToVed already, so continue to
536-- resolve names from the *outer* scope which may happen to have the same id.
537-- | n == x = Bind x (fmap (pToV' n i) b) sc
538 | otherwise = Bind x (fmap (pToV' n i) b) (pToV' n (i+1) sc)
539pToV' n i (App f a) = App (pToV' n i f) (pToV' n i a)
540pToV' n i (Proj t idx) = Proj (pToV' n i t) idx
543-- Convert several names. First in the list comes out as V 0
544pToVs :: Eq n => [n] -> TT n -> TT n
545pToVs ns tm = pToVs' ns tm 0 where
547 pToVs' (n:ns) tm i = pToV' n i (pToVs' ns tm (i+1))
551 vToP' env (V i) = let (n, b) = (env !! i) in
552 P Bound n (binderTy b)
553 vToP' env (Bind n b sc) = let b' = fmap (vToP' env) b in
554 Bind n b' (vToP' ((n, b'):env) sc)
555 vToP' env (App f a) = App (vToP' env f) (vToP' env a)
558finalise :: Eq n => TT n -> TT n
559finalise (Bind x b sc) = Bind x (fmap finalise b) (pToV x (finalise sc))
560finalise (App f a) = App (finalise f) (finalise a)
563subst :: Eq n => n -> TT n -> TT n -> TT n
564subst n v tm = instantiate v (pToV n tm)
566substNames :: Eq n => [(n, TT n)] -> TT n -> TT n
568substNames ((n, tm) : xs) t = subst n tm (substNames xs t)
570substTerm :: Eq n => TT n -> TT n -> TT n -> TT n
571substTerm old new = st where
572 st t | t == old = new
573 st (App f a) = App (st f) (st a)
574 st (Bind x b sc) = Bind x (fmap st b) (st sc)
577-- Returns true if V 0 and bound name n do not occur in the term
579noOccurrence :: Eq n => n -> TT n -> Bool
580noOccurrence n t = no' 0 t
582 no' i (V x) = not (i == x)
583 no' i (P Bound x _) = not (n == x)
584 no' i (Bind n b sc) = noB' i b && no' (i+1) sc
585 where noB' i (Let t v) = no' i t && no' i v
586 noB' i (Guess t v) = no' i t && no' i v
587 noB' i b = no' i (binderTy b)
588 no' i (App f a) = no' i f && no' i a
589 no' i (Proj x _) = no' i x
592-- Returns all names used free in the term
594freeNames :: Eq n => TT n -> [n]
595freeNames (P _ n _) = [n]
596freeNames (Bind n (Let t v) sc) = nub $ freeNames v ++ (freeNames sc \\ [n])
598freeNames (Bind n b sc) = nub $ freeNames (binderTy b) ++ (freeNames sc \\ [n])
599freeNames (App f a) = nub $ freeNames f ++ freeNames a
600freeNames (Proj x i) = nub $ freeNames x
603-- Return the arity of a (normalised) type
606arity (Bind n (Pi t) sc) = 1 + arity sc
609-- deconstruct an application; returns the function and a list of arguments
611unApply :: TT n -> (TT n, [TT n])
612unApply t = ua [] t where
613 ua args (App f a) = ua (a:args) f
614 ua args t = (t, args)
616mkApp :: TT n -> [TT n] -> TT n
618mkApp f (a:as) = mkApp (App f a) as
620forget :: TT Name -> Raw
623 fe env (P _ n _) = Var n
624 fe env (V i) = Var (env !! i)
625 fe env (Bind n b sc) = RBind n (fmap (fe env) b)
627 fe env (App f a) = RApp (fe env f) (fe env a)
630 fe env (TType i) = RType
631 fe env Erased = RConstant Forgot
633bindAll :: [(n, Binder (TT n))] -> TT n -> TT n
635bindAll ((n, b) : bs) t = Bind n b (bindAll bs t)
637bindTyArgs :: (TT n -> Binder (TT n)) -> [(n, TT n)] -> TT n -> TT n
638bindTyArgs b xs = bindAll (map (\ (n, ty) -> (n, b ty)) xs)
640getArgTys :: TT n -> [(n, TT n)]
641getArgTys (Bind n (Pi t) sc) = (n, t) : getArgTys sc
644getRetTy :: TT n -> TT n
645getRetTy (Bind n (PVar _) sc) = getRetTy sc
646getRetTy (Bind n (PVTy _) sc) = getRetTy sc
647getRetTy (Bind n (Pi _) sc) = getRetTy sc
650uniqueName :: Name -> [Name] -> Name
651uniqueName n hs | n `elem` hs = uniqueName (nextName n) hs
654uniqueBinders :: [Name] -> TT Name -> TT Name
655uniqueBinders ns (Bind n b sc)
656 = let n' = uniqueName n ns in
657 Bind n' (fmap (uniqueBinders (n':ns)) b) (uniqueBinders ns sc)
658uniqueBinders ns (App f a) = App (uniqueBinders ns f) (uniqueBinders ns a)
659uniqueBinders ns t = t
662nextName (NS x s) = NS (nextName x) s
663nextName (MN i n) = MN (i+1) n
664nextName (UN x) = let (num', nm') = span isDigit (reverse x)
666 num = readN (reverse num') in
667 UN (nm ++ show (num+1))
677-- an environment with de Bruijn indices 'normalised' so that they all refer to
680newtype WkEnvTT n = Wk (EnvTT n)
681type WkEnv = WkEnvTT Name
683instance (Eq n, Show n) => Show (TT n) where
684 show t = showEnv [] t
686instance Show Const where
688 show (BI i) = show i ++ "L"
691 show (Str s) = show s
693 show (W16 x) = show x
695 show BIType = "Integer"
696 show FlType = "Float"
698 show StrType = "String"
700 show W8Type = "Word8"
701 show W16Type = "Word16"
702 show VoidType = "Void"
704showEnv env t = showEnv' env t False
705showEnvDbg env t = showEnv' env t True
707prettyEnv env t = prettyEnv' env t False
709 prettyEnv' env t dbg = prettySe 10 env t dbg
711 bracket outer inner p
712 | inner > outer = lparen <> p <> rparen
715 prettySe p env (P nt n t) debug =
718 lbrack <+> pretty nt <+> colon <+> prettySe 10 env t debug <+> rbrack
721 prettySe p env (V i) debug
724 text . show . fst $ env!!i
726 lbrack <+> text (show i) <+> rbrack
727 | otherwise = text "unbound" <+> text (show i) <+> text "!"
728 prettySe p env (Bind n b@(Pi t) sc) debug
729 | noOccurrence n sc && not debug =
730 bracket p 2 $ prettySb env n b debug <> prettySe 10 ((n, b):env) sc debug
731 prettySe p env (Bind n b sc) debug =
732 bracket p 2 $ prettySb env n b debug <> prettySe 10 ((n, b):env) sc debug
733 prettySe p env (App f a) debug =
734 bracket p 1 $ prettySe 1 env f debug <+> prettySe 0 env a debug
735 prettySe p env (Proj x i) debug =
736 prettySe 1 env x debug <+> text ("!" ++ show i)
737 prettySe p env (Constant c) debug = pretty c
738 prettySe p env Erased debug = text "[_]"
739 prettySe p env (TType i) debug = text "Type" <+> (text . show $ i)
741 prettySb env n (Lam t) = prettyB env "λ" "=>" n t
742 prettySb env n (Hole t) = prettyB env "?defer" "." n t
743 prettySb env n (Pi t) = prettyB env "(" ") ->" n t
744 prettySb env n (PVar t) = prettyB env "pat" "." n t
745 prettySb env n (PVTy t) = prettyB env "pty" "." n t
746 prettySb env n (Let t v) = prettyBv env "let" "in" n t v
747 prettySb env n (Guess t v) = prettyBv env "??" "in" n t v
749 prettyB env op sc n t debug =
750 text op <> pretty n <+> colon <+> prettySe 10 env t debug <> text sc
752 prettyBv env op sc n t v debug =
753 text op <> pretty n <+> colon <+> prettySe 10 env t debug <+> text "=" <+>
754 prettySe 10 env v debug <> text sc
757showEnv' env t dbg = se 10 env t where
758 se p env (P nt n t) = show n
759 ++ if dbg then "{" ++ show nt ++ " : " ++ se 10 env t ++ "}" else ""
760 se p env (V i) | i < length env = (show $ fst $ env!!i) ++
761 if dbg then "{" ++ show i ++ "}" else ""
762 | otherwise = "!!V " ++ show i ++ "!!"
763 se p env (Bind n b@(Pi t) sc)
764 | noOccurrence n sc && not dbg = bracket p 2 $ se 1 env t ++ " -> " ++ se 10 ((n,b):env) sc
765 se p env (Bind n b sc) = bracket p 2 $ sb env n b ++ se 10 ((n,b):env) sc
766 se p env (App f a) = bracket p 1 $ se 1 env f ++ " " ++ se 0 env a
767 se p env (Proj x i) = se 1 env x ++ "!" ++ show i
768 se p env (Constant c) = show c
769 se p env Erased = "[__]"
770 se p env Impossible = "[impossible]"
771 se p env (TType i) = "Type " ++ show i
773 sb env n (Lam t) = showb env "\\ " " => " n t
774 sb env n (Hole t) = showb env "? " ". " n t
775 sb env n (GHole t) = showb env "?defer " ". " n t
776 sb env n (Pi t) = showb env "(" ") -> " n t
777 sb env n (PVar t) = showb env "pat " ". " n t
778 sb env n (PVTy t) = showb env "pty " ". " n t
779 sb env n (Let t v) = showbv env "let " " in " n t v
780 sb env n (Guess t v) = showbv env "?? " " in " n t v
782 showb env op sc n t = op ++ show n ++ " : " ++ se 10 env t ++ sc
783 showbv env op sc n t v = op ++ show n ++ " : " ++ se 10 env t ++ " = " ++
786 bracket outer inner str | inner > outer = "(" ++ str ++ ")"
789-- Check whether a term has any holes in it - impure if so
791pureTerm :: TT n -> Bool
792pureTerm (App f a) = pureTerm f && pureTerm a
793pureTerm (Bind n b sc) = pureBinder b && pureTerm sc where
794 pureBinder (Hole _) = False
795 pureBinder (Guess _ _) = False
796 pureBinder (Let t v) = pureTerm t && pureTerm v
797 pureBinder t = pureTerm (binderTy t)
800-- weaken a term by adding i to each de Bruijn index (i.e. lift it over i bindings)
802weakenTm :: Int -> TT n -> TT n
803weakenTm i t = wk i 0 t
804 where wk i min (V x) | x >= min = V (i + x)
805 wk i m (App f a) = App (wk i m f) (wk i m a)
806 wk i m (Bind x b sc) = Bind x (wkb i m b) (wk i (m + 1) sc)
808 wkb i m t = fmap (wk i m) t
810-- weaken an environment so that all the de Bruijn indices are correct according
811-- to the latest bound variable
813weakenEnv :: EnvTT n -> EnvTT n
814weakenEnv env = wk (length env - 1) env
816 wk i ((n, b) : bs) = (n, weakenTmB i b) : wk (i - 1) bs
817 weakenTmB i (Let t v) = Let (weakenTm i t) (weakenTm i v)
818 weakenTmB i (Guess t v) = Guess (weakenTm i t) (weakenTm i v)
819 weakenTmB i t = t { binderTy = weakenTm i (binderTy t) }
821weakenTmEnv :: Int -> EnvTT n -> EnvTT n
822weakenTmEnv i = map (\ (n, b) -> (n, fmap (weakenTm i) b))
824orderPats :: Term -> Term
825orderPats tm = op [] tm
827 op ps (Bind n (PVar t) sc) = op ((n, PVar t) : ps) sc
828 op ps (Bind n (Hole t) sc) = op ((n, Hole t) : ps) sc
829 op ps sc = bindAll (map (\ (n, t) -> (n, t)) (sortP ps)) sc
831 sortP ps = pick [] (reverse ps)
833 namesIn (P _ n _) = [n]
834 namesIn (Bind n b t) = nub $ nb b ++ (namesIn t \\ [n])
835 where nb (Let t v) = nub (namesIn t) ++ nub (namesIn v)
836 nb (Guess t v) = nub (namesIn t) ++ nub (namesIn v)
837 nb t = namesIn (binderTy t)
838 namesIn (App f a) = nub (namesIn f ++ namesIn a)
841 pick acc [] = reverse acc
842 pick acc ((n, t) : ps) = pick (insert n t acc) ps
844 insert n t [] = [(n, t)]
845 insert n t ((n',t') : ps)
846 | n `elem` (namesIn (binderTy t') ++
847 concatMap namesIn (map (binderTy . snd) ps))
848 = (n', t') : insert n t ps
849 | 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