@@ -1,4 +1,4 @@
1−{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, DeriveFunctor #-}
1+{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, DeriveFunctor, FlexibleInstances #-}
22
33module Core.TT where
44
@@ -36,6 +36,9 @@ data FC = FC { fc_fname :: String,
3636deriving instance Binary FC
3737!-}
3838
39+instance Sized FC where
40+ size (FC f l) = 1 + length f
41+
3942instance Show FC where
4043 show (FC f l) = f ++ ":" ++ show l
4144
@@ -50,6 +53,17 @@ data Err = Msg String
5053 | At FC Err
5154 deriving Eq
5255
56+instance Sized Err where
57+ size (Msg msg) = length msg
58+ size (CantUnify left right err score) = size left + size right + size err
59+ size (NoSuchVariable name) = size name
60+ size (NotInjective l c r) = size l + size c + size r
61+ size (CantResolve trm) = size trm
62+ size (IncompleteTerm trm) = size trm
63+ size UniverseError = 1
64+ size ProgramLineComment = 1
65+ size (At fc err) = size fc + size err
66+
5367score :: Err -> Int
5468score (CantUnify _ _ m s) = s + score m
5569score (CantResolve _) = 20
@@ -62,10 +76,30 @@ instance Show Err where
6276 ++ show e ++ " " ++ show i
6377 show _ = "Error"
6478
79+instance Pretty Err where
80+ pretty (Msg m) = text m
81+ pretty (CantUnify l r e i) =
82+ if size l + size r > breakingSize then
83+ text "Cannot unify" <+> colon $$
84+ nest nestingSize (pretty l <+> text "and" <+> pretty r) $$
85+ nest nestingSize (text "where" <+> pretty e <+> text "with" <+> (text . show $ i))
86+ else
87+ text "Cannot unify" <+> colon <+> pretty l <+> text "and" <+> pretty r $$
88+ nest nestingSize (text "where" <+> pretty e <+> text "with" <+> (text . show $ i))
89+ pretty _ = text "Error"
90+
6591data TC a = OK a
6692 | Error Err
6793 deriving (Eq, Functor)
6894
95+instance Pretty a => Pretty (TC a) where
96+ pretty (OK ok) = pretty ok
97+ pretty (Error err) =
98+ if size err > breakingSize then
99+ text "Error" <+> colon $$ (nest nestingSize $ pretty err)
100+ else
101+ text "Error" <+> colon <+> pretty err
102+
69103instance Show a => Show (TC a) where
70104 show (OK x) = show x
71105 show (Error str) = "Error: " ++ show str
@@ -120,6 +154,16 @@ data Name = UN String
120154deriving instance Binary Name
121155!-}
122156
157+instance Sized Name where
158+ size (UN n) = 1
159+ size (NS n els) = 1 + length els
160+ size (MN i n) = 1
161+
162+instance Pretty Name where
163+ pretty (UN n) = text n
164+ pretty (NS n s) = pretty n
165+ pretty (MN i s) = lbrace <+> text s <+> (text . show $ i) <+> rbrace
166+
123167instance Show Name where
124168 show (UN n) = n
125169 show (NS n s) = showSep "." (reverse s) ++ "." ++ show n
@@ -192,6 +236,23 @@ data Const = I Int | BI Integer | Fl Double | Ch Char | Str String
192236deriving instance Binary Const
193237!-}
194238
239+instance Sized Const where
240+ size _ = 1
241+
242+instance Pretty Const where
243+ pretty (I i) = text . show $ i
244+ pretty (BI i) = text . show $ i
245+ pretty (Fl f) = text . show $ f
246+ pretty (Ch c) = text . show $ c
247+ pretty (Str s) = text s
248+ pretty IType = text "Int"
249+ pretty BIType = text "BigInt"
250+ pretty FlType = text "Float"
251+ pretty ChType = text "Char"
252+ pretty StrType = text "String"
253+ pretty PtrType = text "Ptr"
254+ pretty Forgot = text "Forgot"
255+
195256data Raw = Var Name
196257 | RBind Name (Binder Raw) Raw
197258 | RApp Raw Raw
@@ -229,6 +290,17 @@ data Binder b = Lam { binderTy :: b }
229290deriving instance Binary Binder
230291!-}
231292
293+instance Sized a => Sized (Binder a) where
294+ size (Lam ty) = 1 + size ty
295+ size (Pi ty) = 1 + size ty
296+ size (Let ty val) = 1 + size ty + size val
297+ size (NLet ty val) = 1 + size ty + size val
298+ size (Hole ty) = 1 + size ty
299+ size (GHole ty) = 1 + size ty
300+ size (Guess ty val) = 1 + size ty + size val
301+ size (PVar ty) = 1 + size ty
302+ size (PVTy ty) = 1 + size ty
303+
232304fmapMB :: Monad m => (a -> m b) -> Binder a -> m (Binder b)
233305fmapMB f (Let t v) = liftM2 Let (f t) (f v)
234306fmapMB f (NLet t v) = liftM2 NLet (f t) (f v)
@@ -270,6 +342,9 @@ data UExp = UVar Int -- universe variable
270342 | UVal Int -- explicit universe level
271343 deriving (Eq, Ord)
272344
345+instance Sized UExp where
346+ size _ = 1
347+
273348-- We assume that universe levels have been checked, so anything external
274349-- can just have the same universe variable and we won't get any new
275350-- cycles.
@@ -300,6 +375,12 @@ data NameType = Bound | Ref | DCon Int Int | TCon Int Int
300375deriving instance Binary NameType
301376!-}
302377
378+instance Sized NameType where
379+ size _ = 1
380+
381+instance Pretty NameType where
382+ pretty = text . show
383+
303384instance Eq NameType where
304385 Bound == Bound = True
305386 Ref == Ref = True
@@ -319,6 +400,18 @@ data TT n = P NameType n (TT n) -- embed type
319400deriving instance Binary TT
320401!-}
321402
403+instance Sized a => Sized (TT a) where
404+ size (P name n trm) = 1 + size name + size n + size trm
405+ size (V v) = 1
406+ size (Bind nm binder bdy) = 1 + size nm + size binder + size bdy
407+ size (App l r) = 1 + size l + size r
408+ size (Constant c) = size c
409+ size Erased = 1
410+ size (Set u) = 1 + size u
411+
412+instance Pretty a => Pretty (TT a) where
413+ pretty
414+
322415type EnvTT n = [(n, Binder (TT n))]
323416
324417data Datatype n = Data { d_typename :: n,
@@ -507,6 +600,54 @@ instance Show Const where
507600showEnv env t = showEnv' env t False
508601showEnvDbg env t = showEnv' env t True
509602
603+prettyEnv env t = prettyEnv' env t False
604+ where
605+ prettyEnv' env t dbg = prettySe 10 env t dbg
606+
607+ bracket outer inner p
608+ | inner > outer = lparen <> p <> rparen
609+ | otherwise = p
610+
611+ prettySe p env (P nt n t) debug =
612+ pretty n <+>
613+ if debug then
614+ lbrack <+> pretty nt <+> colon <+> prettySe 10 env t debug <+> rbrack
615+ else
616+ empty
617+ prettySe p env (V i) debug
618+ | i < length env =
619+ if debug then
620+ text . show . fst $ env!!i
621+ else
622+ lbrack <+> text (show i) <+> rbrack
623+ | otherwise = text "unbound" <+> text (show i) <+> text "!"
624+ prettySe p env (Bind n b@(Pi t) sc) debug
625+ | noOccurrence n sc && not debug =
626+ bracket p 2 $ prettySb env n b debug <> prettySe 10 ((n, b):env) sc debug
627+ prettySe p env (Bind n b sc) debug =
628+ bracket p 2 $ prettySb env n b debug <> prettySe 10 ((n, b):env) sc debug
629+ prettySe p env (App f a) debug =
630+ bracket p 1 $ prettySe 1 env f debug <+> prettySe 0 env a debug
631+ prettySe p env (Constant c) debug = pretty c
632+ prettySe p env Erased debug = text "[_]"
633+ prettySe p env (Set i) debug = text "Set" <+> (text . show $ i)
634+
635+ prettySb env n (Lam t) = prettyB env "λ" "=>" n t
636+ prettySb env n (Hole t) = prettyB env "?defer" "." n t
637+ prettySb env n (Pi t) = prettyB env "(" ") ->" n t
638+ prettySb env n (PVar t) = prettyB env "pat" "." n t
639+ prettySb env n (PVTy t) = prettyB env "pty" "." n t
640+ prettySb env n (Let t v) = prettyBv env "let" "in" n t v
641+ prettySb env n (Guess t v) = prettyBv env "??" "in" n t v
642+
643+ prettyB env op sc n t debug =
644+ text op <> pretty n <+> colon <+> prettySe 10 env t debug <> text sc
645+
646+ prettyBv env op sc n t v debug =
647+ text op <> pretty n <+> colon <+> prettySe 10 env t debug <+> text "=" <+>
648+ prettySe 10 env v debug <> text sc
649+
650+
510651showEnv' env t dbg = se 10 env t where
511652 se p env (P nt n t) = show n
512653 ++ if dbg then "{" ++ show nt ++ " : " ++ se 10 env t ++ "}" else ""