@@ -30,6 +30,8 @@ import Data.Word (Word)
31import Debug.Trace
31import Debug.Trace
33+import Text.PrettyPrint.Annotated.Leijen
33data IOption = IOption { opt_logLevel :: Int,
35data IOption = IOption { opt_logLevel :: Int,
34 opt_typecase :: Bool,
36 opt_typecase :: Bool,
35 opt_typeintype :: Bool,
37 opt_typeintype :: Bool,
@@ -908,275 +910,440 @@ expandNS syn n = case syn_namespace syn of
908 xs -> sNS n xs
910 xs -> sNS n xs
911−--- Pretty printing declarations and terms
913+-- For inferring types of things
913−instance Show PTerm where
915+bi = fileFC "builtin"
914− show tm = showImp Nothing False False tm
916−instance Pretty PTerm where
917+inferTy = sMN 0 "__Infer"
917− pretty = prettyImp False
918+inferCon = sMN 0 "__infer"
919+inferDecl = PDatadecl inferTy
921+ [("", inferCon, PPi impl (sMN 0 "iType") PType (
922+ PPi expl (sMN 0 "ival") (PRef bi (sMN 0 "iType"))
923+ (PRef bi inferTy)), bi, [])]
919−instance Show PDecl where
926+infTerm t = PApp bi (PRef bi inferCon) [pimp (sMN 0 "iType") Placeholder True, pexp t]
920− show d = showDeclImp False d
927+infP = P (TCon 6 0) inferTy (TType (UVal 0))
922−instance Show PClause where
929+getInferTerm, getInferType :: Term -> Term
923− show c = showCImp True c
930+getInferTerm (Bind n b sc) = Bind n b $ getInferTerm sc
931+getInferTerm (App (App _ _) tm) = tm
932+getInferTerm tm = tm -- error ("getInferTerm " ++ show tm)
925−instance Show PData where
934+getInferType (Bind n b sc) = Bind n b $ getInferType sc
926− show d = showDImp False d
935+getInferType (App (App _ ty) _) = ty
928−showDecls :: Bool -> [PDecl] -> String
930−showDecls i (d:ds) = showDeclImp i d ++ "\n" ++ showDecls i ds
932−showDeclImp _ (PFix _ f ops) = show f ++ " " ++ showSep ", " ops
933−showDeclImp i (PTy _ _ _ _ n t) = "tydecl " ++ showCG n ++ " : " ++ showImp Nothing i False t
934−showDeclImp i (PClauses _ _ n cs) = "pat " ++ showCG n ++ "\t" ++ showSep "\n\t" (map (showCImp i) cs)
935−showDeclImp _ (PData _ _ _ _ d) = showDImp True d
936−showDeclImp i (PParams _ ns ps) = "params {" ++ show ns ++ "\n" ++ showDecls i ps ++ "}\n"
937−showDeclImp i (PNamespace n ps) = "namespace {" ++ n ++ "\n" ++ showDecls i ps ++ "}\n"
938−showDeclImp _ (PSyntax _ syn) = "syntax " ++ show syn
939−showDeclImp i (PClass _ _ _ cs n ps ds)
940− = "class " ++ show cs ++ " " ++ show n ++ " " ++ show ps ++ "\n" ++ showDecls i ds
941−showDeclImp i (PInstance _ _ cs n _ t _ ds)
942− = "instance " ++ show cs ++ " " ++ show n ++ " " ++ show t ++ "\n" ++ showDecls i ds
943−showDeclImp _ _ = "..."
944−-- showDeclImp (PImport i) = "import " ++ i
939+-- Handy primitives: Unit, False, Pair, MkPair, =, mkForeign, Elim type class
947−showCImp :: Bool -> PClause -> String
941+primNames = [unitTy, unitCon,
948−showCImp impl (PClause _ n l ws r w)
942+ falseTy, pairTy, pairCon,
949− = showImp Nothing impl False l ++ showWs ws ++ " = " ++ showImp Nothing impl False r
943+ eqTy, eqCon, inferTy, inferCon]
950− ++ " where " ++ show w
953− showWs (x : xs) = " | " ++ showImp Nothing impl False x ++ showWs xs
954−showCImp impl (PWith _ n l ws r w)
955− = showImp Nothing impl False l ++ showWs ws ++ " with " ++ showImp Nothing impl False r
956− ++ " { " ++ show w ++ " } "
959− showWs (x : xs) = " | " ++ showImp Nothing impl False x ++ showWs xs
945+unitTy = sMN 0 "__Unit"
946+unitCon = sMN 0 "__II"
947+unitDecl = PDatadecl unitTy PType
948+ [("", unitCon, PRef bi unitTy, bi, [])]
949+unitOpts = [DefaultEliminator]
962−showDImp :: Bool -> PData -> String
951+falseTy = sMN 0 "__False"
963−showDImp impl (PDatadecl n ty cons)
952+falseDecl = PDatadecl falseTy PType []
964− = "data " ++ show n ++ " : " ++ showImp Nothing impl False ty ++ " where\n\t"
953+falseOpts = []
966− (map (\ (_, n, t, _, _) -> show n ++ " : " ++ showImp Nothing impl False t) cons)
968−getImps :: [PArg] -> [(Name, PTerm)]
955+pairTy = sMN 0 "__Pair"
969−getImps [] = []
956+pairCon = sMN 0 "__MkPair"
970−getImps (PImp _ _ _ n tm _ : xs) = (n, tm) : getImps xs
957+pairDecl = PDatadecl pairTy (piBind [(n "A", PType), (n "B", PType)] PType)
971−getImps (_ : xs) = getImps xs
958+ [("", pairCon, PPi impl (n "A") PType (
959+ PPi impl (n "B") PType (
960+ PPi expl (n "a") (PRef bi (n "A")) (
961+ PPi expl (n "b") (PRef bi (n "B"))
962+ (PApp bi (PRef bi pairTy) [pexp (PRef bi (n "A")),
963+ pexp (PRef bi (n "B"))])))), bi, [])]
973−getExps :: [PArg] -> [PTerm]
967+eqTy = sUN "="
974−getExps [] = []
968+eqCon = sUN "refl"
975−getExps (PExp _ _ tm _ : xs) = tm : getExps xs
969+eqDecl = PDatadecl eqTy (piBind [(n "A", PType), (n "B", PType),
976−getExps (_ : xs) = getExps xs
970+ (n "x", PRef bi (n "A")), (n "y", PRef bi (n "B"))]
972+ [("", eqCon, PPi impl (n "A") PType (
973+ PPi impl (n "x") (PRef bi (n "A"))
974+ (PApp bi (PRef bi eqTy) [pimp (n "A") Placeholder False,
975+ pimp (n "B") Placeholder False,
976+ pexp (PRef bi (n "x")),
977+ pexp (PRef bi (n "x"))])), bi, [])]
978−getConsts :: [PArg] -> [PTerm]
981+elimName = sUN "__Elim"
979−getConsts [] = []
982+elimMethElimTy = sUN "__elimTy"
980−getConsts (PConstraint _ _ tm _ : xs) = tm : getConsts xs
983+elimMethElim = sUN "elim"
981−getConsts (_ : xs) = getConsts xs
984+elimDecl = PClass "Type class for eliminators" defaultSyntax bi [] elimName [(sUN "scrutineeType", PType)]
985+ [PTy "" defaultSyntax bi [TotalFn] elimMethElimTy PType,
986+ PTy "" defaultSyntax bi [TotalFn] elimMethElim (PRef bi elimMethElimTy)]
988+-- Defined in builtins.idr
989+sigmaTy = sUN "Exists"
990+existsCon = sUN "Ex_intro"
992+piBind :: [(Name, PTerm)] -> PTerm -> PTerm
993+piBind = piBindp expl
995+piBindp :: Plicity -> [(Name, PTerm)] -> PTerm -> PTerm
997+piBindp p ((n, ty):ns) t = PPi p n ty (piBindp p ns t)
983−getAll :: [PArg] -> [PTerm]
1000+-- Pretty-printing declarations and terms
1002+instance Show PTerm where
1003+ showsPrec _ tm = (displayS . renderCompact . prettyImp False) tm
1005+instance Show PDecl where
1006+ showsPrec _ d = (displayS . renderCompact . showDeclImp False) d
1008+instance Show PClause where
1009+ showsPrec _ c = (displayS . renderCompact . showCImp True) c
1011+instance Show PData where
1012+ showsPrec _ d = (displayS . renderCompact . showDImp False) d
1014+instance Pretty PTerm OutputAnnotation where
1015+ pretty = prettyImp False
1017+-- | Colourise annotations according to an Idris state. It ignores the names
1018+-- in the annotation, as there's no good way to show extended information on a
1020+consoleDecorate :: IState -> OutputAnnotation -> String -> String
1021+consoleDecorate ist _ | not (idris_colourRepl ist) = id
1022+consoleDecorate ist AnnConstData = let theme = idris_colourTheme ist
1023+ in colouriseData theme
1024+consoleDecorate ist AnnConstType = let theme = idris_colourTheme ist
1025+ in colouriseType theme
1026+consoleDecorate ist (AnnBoundName _ True) = colouriseImplicit (idris_colourTheme ist)
1027+consoleDecorate ist (AnnBoundName _ False) = colouriseBound (idris_colourTheme ist)
1028+consoleDecorate ist (AnnName n _ _) = let ctxt = tt_ctxt ist
1029+ theme = idris_colourTheme ist
1031+ _ | isDConName n ctxt -> colouriseData theme
1032+ _ | isFnName n ctxt -> colouriseFun theme
1033+ _ | isTConName n ctxt -> colouriseType theme
1034+ _ | otherwise -> id -- don't colourise unknown names
1035+consoleDecorate ist (AnnFC _) = id
986−-- | Pretty-print a high-level Idris term
1037+-- | Pretty-print a high-level closed Idris term
987prettyImp :: Bool -- ^^ whether to show implicits
1038prettyImp :: Bool -- ^^ whether to show implicits
988 -> PTerm -- ^^ the term to pretty-print
1039 -> PTerm -- ^^ the term to pretty-print
989− -> Doc
1040+ -> Doc OutputAnnotation
990−prettyImp impl = prettySe 10
1041+prettyImp impl = pprintPTerm impl []
1043+-- | Pretty-print a high-level Idris term in some bindings context
1044+pprintPTerm :: Bool -- ^^ whether to show implicits
1045+ -> [(Name, Bool)] -- ^^ the currently-bound names and whether they are implicit
1046+ -> PTerm -- ^^ the term to pretty-print
1047+ -> Doc OutputAnnotation
1048+pprintPTerm impl bnd = prettySe 10 bnd
1050+ prettySe :: Int -> [(Name, Bool)] -> PTerm -> Doc OutputAnnotation
992− prettySe p (PQuote r) =
1051+ prettySe p bnd (PQuote r) =
993 if size r > breakingSize then
1052 if size r > breakingSize then
994− text "![" $$ pretty r <> text "]"
1053+ text "![" <> line <> pretty r <> text "]"
996 text "![" <> pretty r <> text "]"
1055 text "![" <> pretty r <> text "]"
997− prettySe p (PPatvar fc n) = pretty n
1056+ prettySe p bnd (PPatvar fc n) = pretty n
998− prettySe p (PRef fc n) =
1057+ prettySe p bnd e
999− if impl then
1058+ | Just str <- slist p bnd e = str
1000− pretty n
1059+ | Just n <- snat p e = annotate AnnConstData (text (show n))
1001− else
1060+ prettySe p bnd (PRef fc n) = prettyName impl bnd n
1004− prettyBasic n@(UN _) = pretty n
1005− prettyBasic (MN _ s) = text (str s)
1006− prettyBasic (NS n s) = (foldr (<>) empty (intersperse (text ".") (map (text.str) $ reverse s))) <> prettyBasic n
1007− prettyBasic (SN sn) = text (show sn)
1008− prettySe p (PLam n ty sc) =
1061+ prettySe p bnd (PLam n ty sc) =
1009 bracket p 2 $
1062 bracket p 2 $
1010 if size sc > breakingSize then
1063 if size sc > breakingSize then
1011− text "λ" <> pretty n <+> text "=>" $+$ pretty sc
1064+ text "\\" <> bindingOf n False <+> text "=>" <> line <> prettySe 10 ((n, False):bnd) sc
1013− text "λ" <> pretty n <+> text "=>" <+> pretty sc
1066+ text "\\" <> bindingOf n False <+> text "=>" <+> prettySe 10 ((n, False):bnd) sc
1014− prettySe p (PLet n ty v sc) =
1067+ prettySe p bnd (PLet n ty v sc) =
1015 bracket p 2 $
1068 bracket p 2 $
1016 if size sc > breakingSize then
1069 if size sc > breakingSize then
1017− text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" $+$
1070+ text "let" <+> bindingOf n False <+> text "=" <+> prettySe 10 bnd v <+> text "in" <>
1018− nest nestingSize (prettySe 10 sc)
1071+ nest nestingSize (prettySe 10 ((n, False):bnd) sc)
1020− text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" <+>
1073+ text "let" <+> bindingOf n False <+> text "=" <+> prettySe 10 bnd v <+> text "in" <+>
1021− prettySe 10 sc
1074+ prettySe 10 ((n, False):bnd) sc
1022− prettySe p (PPi (Exp l s _ _) n ty sc)
1075+ prettySe p bnd (PPi (Exp l s _ _) n ty sc)
1023 | n `elem` allNamesIn sc || impl =
1076 | n `elem` allNamesIn sc || impl =
1024 let open = if Lazy `elem` l then text "|" <> lparen else lparen in
1077 let open = if Lazy `elem` l then text "|" <> lparen else lparen in
1025 bracket p 2 $
1078 bracket p 2 $
1026 if size sc > breakingSize then
1079 if size sc > breakingSize then
1027− open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
1080+ enclose open rparen (bindingOf n False <+> colon <+> prettySe 10 bnd ty) <+>
1028− st <+> text "->" $+$ prettySe 10 sc
1081+ st <> text "->" <> line <> prettySe 10 ((n, False):bnd) sc
1030− open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
1083+ enclose open rparen (bindingOf n False <+> colon <+> prettySe 10 bnd ty) <+>
1031− st <+> text "->" <+> prettySe 10 sc
1084+ st <> text "->" <+> prettySe 10 ((n, False):bnd) sc
1032 | otherwise =
1085 | otherwise =
1033 bracket p 2 $
1086 bracket p 2 $
1034 if size sc > breakingSize then
1087 if size sc > breakingSize then
1035− prettySe 0 ty <+> st <+> text "->" $+$ prettySe 10 sc
1088+ prettySe 0 bnd ty <+> st <> text "->" <> line <> prettySe 10 ((n, False):bnd) sc
1037− prettySe 0 ty <+> st <+> text "->" <+> prettySe 10 sc
1090+ prettySe 0 bnd ty <+> st <> text "->" <+> prettySe 10 ((n, False):bnd) sc
1040 case s of
1093 case s of
1041− Static -> text "[static]"
1094+ Static -> text "[static]" <> space
1042 _ -> empty
1095 _ -> empty
1043− prettySe p (PPi (Imp l s _ _) n ty sc)
1096+ prettySe p bnd (PPi (Imp l s _ _) n ty sc)
1044 | impl =
1097 | impl =
1045 let open = if Lazy `elem` l then text "|" <> lbrace else lbrace in
1098 let open = if Lazy `elem` l then text "|" <> lbrace else lbrace in
1046 bracket p 2 $
1099 bracket p 2 $
1047 if size sc > breakingSize then
1100 if size sc > breakingSize then
1048− open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
1101+ open <> bindingOf n True <+> colon <+> prettySe 10 bnd ty <> rbrace <+>
1049− st <+> text "->" <+> prettySe 10 sc
1102+ st <> text "->" <+> prettySe 10 ((n, True):bnd) sc
1051− open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
1104+ open <> bindingOf n True <+> colon <+> prettySe 10 bnd ty <> rbrace <+>
1052− st <+> text "->" <+> prettySe 10 sc
1105+ st <> text "->" <+> prettySe 10 ((n, True):bnd) sc
1053− | otherwise = prettySe 10 sc
1106+ | otherwise = prettySe 10 ((n, True):bnd) sc
1056 case s of
1109 case s of
1057− Static -> text $ "[static]"
1110+ Static -> text "[static]" <> space
1058 _ -> empty
1111 _ -> empty
1059− prettySe p (PPi (Constraint _ _ _) n ty sc) =
1112+ prettySe p bnd (PPi (Constraint _ _ _) n ty sc) =
1060 bracket p 2 $
1113 bracket p 2 $
1061 if size sc > breakingSize then
1114 if size sc > breakingSize then
1062− prettySe 10 ty <+> text "=>" <+> prettySe 10 sc
1115+ prettySe 10 bnd ty <+> text "=>" <+> prettySe 10 ((n, True):bnd) sc
1064− prettySe 10 ty <+> text "=>" $+$ prettySe 10 sc
1117+ prettySe 10 bnd ty <+> text "=>" <> line <> prettySe 10 ((n, True):bnd) sc
1065− prettySe p (PPi (TacImp _ _ s _) n ty sc) =
1118+ prettySe p bnd (PPi (TacImp _ _ s _) n ty sc) =
1066 bracket p 2 $
1119 bracket p 2 $
1067 if size sc > breakingSize then
1120 if size sc > breakingSize then
1068− lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
1121+ lbrace <> text "tacimp" <+> bindingOf n True <+> colon <+> prettySe 10 bnd ty <>
1069− rbrace <+> text "->" $+$ prettySe 10 sc
1122+ rbrace <+> text "->" <> line <> prettySe 10 ((n, True):bnd) sc
1071− lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
1124+ lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 bnd ty <>
1072− rbrace <+> text "->" <+> prettySe 10 sc
1125+ rbrace <+> text "->" <+> prettySe 10 ((n, True):bnd) sc
1073− prettySe p (PApp _ (PRef _ f) [])
1126+ prettySe p bnd (PApp _ (PRef _ f) [])
1074− | not impl = pretty f
1127+ | not impl = prettyName impl bnd f
1075− prettySe p (PAppBind _ (PRef _ f) [])
1128+ prettySe p bnd (PAppBind _ (PRef _ f) [])
1076− | not impl = text "!" <+> pretty f
1129+ | not impl = text "!" <> prettyName impl bnd f
1077− prettySe p (PApp _ (PRef _ op@(UN nm)) args)
1130+ prettySe p bnd (PApp _ (PRef _ op) args)
1131+ | UN nm <- basename op
1078− | not (tnull nm) &&
1132+ , not (tnull nm) &&
1079 length (getExps args) == 2 && (not impl) && (not $ isAlpha (thead nm)) =
1133 length (getExps args) == 2 && (not impl) && (not $ isAlpha (thead nm)) =
1080 let [l, r] = getExps args in
1134 let [l, r] = getExps args in
1081 bracket p 1 $
1135 bracket p 1 $
1082 if size r > breakingSize then
1136 if size r > breakingSize then
1083− prettySe 1 l <+> pretty op $+$ prettySe 0 r
1137+ prettySe 1 bnd l <+> prettyName impl bnd op <> line <> prettySe 0 bnd r
1085− prettySe 1 l <+> pretty op <+> prettySe 0 r
1139+ prettySe 1 bnd l <+> prettyName impl bnd op <+> prettySe 0 bnd r
1140+ prettySe p bnd (PApp _ hd@(PRef fc f) [tm])
1141+ | PConstant (Idris.Core.TT.Str str) <- getTm tm,
1142+ f == sUN "Symbol_" = char '\'' <> prettySe 10 bnd (PRef fc (sUN str))
1086− prettySe p (PApp _ f as) =
1143+ prettySe p bnd (PApp _ f as) =
1087− let args = getExps as in
1144+ let args = getExps as
1145+ fp = prettySe 1 bnd f
1088 bracket p 1 $
1147 bracket p 1 $
1090− if impl then
1148+ hsep (if impl
1092− -- foldr (<+>) empty $ map prettyArgS as
1149+ then fp : map (prettyArgS bnd) as
1094− foldl' fSe empty args
1095− -- foldr (<+>) empty $ map prettyArgSe args
1150+ else fp : map (prettyArgSe bnd) args)
1151+ prettySe p bnd (PCase _ scr opts) =
1152+ text "case" <+> prettySe 10 bnd scr <+> text "of" <> prettyBody
1098− if size r > breakingSize then
1099− l $+$ nest nestingSize (prettyArgS r)
1104− if size r > breakingSize then
1105− l $+$ nest nestingSize (prettyArgSe r)
1107− l <+> prettyArgSe r
1108− prettySe p (PCase _ scr opts) =
1109− text "case" <+> prettySe 10 scr <+> text "of" $+$ nest nestingSize prettyBody
1111− prettyBody = foldr ($$) empty $ intersperse (text "|") $ map sc opts
1154+ prettyBody = foldr (<>) empty $ intersperse (text "|") $ map sc opts
1113− sc (l, r) = prettySe 10 l <+> text "=>" <+> prettySe 10 r
1156+ sc (l, r) = nest nestingSize $ prettySe 10 bnd l <+> text "=>" <+> prettySe 10 bnd r
1114− prettySe p (PHidden tm) = text "." <> prettySe 0 tm
1157+ prettySe p bnd (PHidden tm) = text "." <> prettySe 0 bnd tm
1115− prettySe p (PRefl _ _) = text "refl"
1158+ prettySe p bnd (PRefl _ _) = annotate (AnnName eqCon Nothing Nothing) $ text "refl"
1116− prettySe p (PResolveTC _) = text "resolvetc"
1159+ prettySe p bnd (PResolveTC _) = text "resolvetc"
1117− prettySe p (PTrue _) = text "()"
1160+ prettySe p bnd (PTrue _) = annotate (AnnName unitTy Nothing Nothing) $ text "()"
1118− prettySe p (PFalse _) = text "_|_"
1161+ prettySe p bnd (PFalse _) = annotate (AnnName falseTy Nothing Nothing) $ text "_|_"
1119− prettySe p (PEq _ l r) =
1162+ prettySe p bnd (PEq _ l r) =
1120 bracket p 2 $
1163 bracket p 2 $
1121 if size r > breakingSize then
1164 if size r > breakingSize then
1122− prettySe 10 l <+> text "=" $$ nest nestingSize (prettySe 10 r)
1165+ prettySe 10 bnd l <+> eq <$> nest nestingSize (prettySe 10 bnd r)
1124− prettySe 10 l <+> text "=" <+> prettySe 10 r
1167+ prettySe 10 bnd l <+> eq <+> prettySe 10 bnd r
1168+ where eq = annotate (AnnName eqTy Nothing Nothing) (text "=")
1125− prettySe p (PRewrite _ l r _) =
1169+ prettySe p bnd (PRewrite _ l r _) =
1126 bracket p 2 $
1170 bracket p 2 $
1127 if size r > breakingSize then
1171 if size r > breakingSize then
1128− text "rewrite" <+> prettySe 10 l <+> text "in" $$ nest nestingSize (prettySe 10 r)
1172+ text "rewrite" <+> prettySe 10 bnd l <+> text "in" <> nest nestingSize (prettySe 10 bnd r)
1130− text "rewrite" <+> prettySe 10 l <+> text "in" <+> prettySe 10 r
1174+ text "rewrite" <+> prettySe 10 bnd l <+> text "in" <+> prettySe 10 bnd r
1131− prettySe p (PTyped l r) =
1175+ prettySe p bnd (PTyped l r) =
1132− lparen <> prettySe 10 l <+> colon <+> prettySe 10 r <> rparen
1176+ lparen <> prettySe 10 bnd l <+> colon <+> prettySe 10 bnd r <> rparen
1133− prettySe p (PPair _ l r) =
1177+ prettySe p bnd (PPair _ l r) =
1134 if size r > breakingSize then
1178 if size r > breakingSize then
1135− lparen <> prettySe 10 l <> text "," $+$
1179+ lparen <> prettySe 10 bnd l <> text "," <>
1136− prettySe 10 r <> rparen
1180+ line <> prettySe 10 bnd r <> rparen
1138− lparen <> prettySe 10 l <> text "," <+> prettySe 10 r <> rparen
1182+ lparen <> prettySe 10 bnd l <> text "," <+> prettySe 10 bnd r <> rparen
1139− prettySe p (PDPair _ l t r) =
1183+ prettySe p bnd (PDPair _ l t r) =
1140 if size r > breakingSize then
1184 if size r > breakingSize then
1141− lparen <> prettySe 10 l <+> text "**" $+$
1185+ lparen <> prettySe 10 bnd l <+> text "**" <>
1142− prettySe 10 r <> rparen
1186+ line <> prettySe 10 bnd r <> rparen
1144− lparen <> prettySe 10 l <+> text "**" <+> prettySe 10 r <> rparen
1188+ lparen <> prettySe 10 bnd l <+> text "**" <+> prettySe 10 bnd r <> rparen
1145− prettySe p (PAlternative a as) =
1189+ prettySe p bnd (PAlternative a as) =
1146 lparen <> text "|" <> prettyAs <> text "|" <> rparen
1190 lparen <> text "|" <> prettyAs <> text "|" <> rparen
1148 prettyAs =
1192 prettyAs =
1149− foldr (\l -> \r -> l <+> text "," <+> r) empty $ map (prettySe 10) as
1193+ foldr (\l -> \r -> l <+> text "," <+> r) empty $ map (prettySe 10 bnd) as
1150− prettySe p PType = text "Type"
1194+ prettySe p bnd PType = annotate AnnConstType $ text "Type"
1151− prettySe p (PConstant c) = pretty c
1195+ prettySe p bnd (PConstant c) = annotate (annot c) (text (show c))
1196+ where annot (AType _) = AnnConstType
1197+ annot StrType = AnnConstType
1198+ annot PtrType = AnnConstType
1199+ annot VoidType = AnnConstType
1200+ annot _ = AnnConstData
1152 -- XXX: add pretty for tactics
1201 -- XXX: add pretty for tactics
1153− prettySe p (PProof ts) =
1202+ prettySe p bnd (PProof ts) =
1154− text "proof" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
1203+ text "proof" <+> lbrace <> nest nestingSize (text . show $ ts) <> rbrace
1155− prettySe p (PTactics ts) =
1204+ prettySe p bnd (PTactics ts) =
1156− text "tactics" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
1205+ text "tactics" <+> lbrace <> nest nestingSize (text . show $ ts) <> rbrace
1157− prettySe p (PMetavar n) = text "?" <> pretty n
1206+ prettySe p bnd (PMetavar n) = text "?" <> pretty n
1158− prettySe p (PReturn f) = text "return"
1207+ prettySe p bnd (PReturn f) = text "return"
1159− prettySe p PImpossible = text "impossible"
1208+ prettySe p bnd PImpossible = text "impossible"
1160− prettySe p Placeholder = text "_"
1209+ prettySe p bnd Placeholder = text "_"
1161− prettySe p (PDoBlock _) = text "do block pretty not implemented"
1210+ prettySe p bnd (PDoBlock _) = text "do block pretty not implemented"
1162− prettySe p (PElabError s) = pretty s
1211+ prettySe p bnd (PElabError s) = pretty s
1164− prettySe p _ = text "test"
1213+ prettySe p bnd _ = text "test"
1166− prettyArgS (PImp _ _ _ n tm _) = prettyArgSi (n, tm)
1215+ prettyArgS bnd (PImp _ _ _ n tm _) = prettyArgSi bnd (n, tm)
1167− prettyArgS (PExp _ _ tm _) = prettyArgSe tm
1216+ prettyArgS bnd (PExp _ _ tm _) = prettyArgSe bnd tm
1168− prettyArgS (PConstraint _ _ tm _) = prettyArgSc tm
1217+ prettyArgS bnd (PConstraint _ _ tm _) = prettyArgSc bnd tm
1169− prettyArgS (PTacImplicit _ _ n _ tm _) = prettyArgSti (n, tm)
1218+ prettyArgS bnd (PTacImplicit _ _ n _ tm _) = prettyArgSti bnd (n, tm)
1171− prettyArgSe arg = prettySe 0 arg
1220+ prettyArgSe bnd arg = prettySe 0 bnd arg
1172− prettyArgSi (n, val) = lbrace <> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
1221+ prettyArgSi bnd (n, val) = lbrace <> pretty n <+> text "=" <+> prettySe 10 bnd val <> rbrace
1173− prettyArgSc val = lbrace <> lbrace <> prettySe 10 val <> rbrace <> rbrace
1222+ prettyArgSc bnd val = lbrace <> lbrace <> prettySe 10 bnd val <> rbrace <> rbrace
1174− prettyArgSti (n, val) = lbrace <> text "auto" <+> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
1223+ prettyArgSti bnd (n, val) = lbrace <> text "auto" <+> pretty n <+> text "=" <+> prettySe 10 bnd val <> rbrace
1225+ basename :: Name -> Name
1226+ basename (NS n _) = basename n
1229+ slist' p bnd (PApp _ (PRef _ nil) _)
1230+ | not impl && nsroot nil == sUN "Nil" = Just []
1231+ slist' p bnd (PApp _ (PRef _ cons) args)
1232+ | nsroot cons == sUN "::",
1233+ (PExp {getTm=tl}):(PExp {getTm=hd}):imps <- reverse args,
1235+ Just tl' <- slist' p bnd tl
1238+ isImp (PImp {}) = True
1240+ slist' _ _ _ = Nothing
1242+ slist p bnd e | Just es <- slist' p bnd e = Just $
1243+ case es of [] -> annotate AnnConstData $ text "[]"
1244+ [x] -> enclose left
1247+ xs -> (enclose left right . hsep . punctuate comma . map (prettySe p bnd)) xs
1248+ where left = (annotate AnnConstData (text "["))
1249+ right = (annotate AnnConstData (text "]"))
1250+ comma = (annotate AnnConstData (text ","))
1251+ slist _ _ _ = Nothing
1253+ natns = "Prelude.Nat."
1256+ | show z == (natns++"Z") || show z == "Z" = Just 0
1257+ snat p (PApp _ s [PExp {getTm=n}])
1258+ | show s == (natns++"S") || show s == "S",
1259+ Just n' <- snat p n
1176 bracket outer inner doc
1263 bracket outer inner doc
1177 | inner > outer = lparen <> doc <> rparen
1264 | inner > outer = lparen <> doc <> rparen
1178 | otherwise = doc
1265 | otherwise = doc
1267+-- | Pretty-printer helper for the binding site of a name
1268+bindingOf :: Name -- ^^ the bound name
1269+ -> Bool -- ^^ whether the name is implicit
1270+ -> Doc OutputAnnotation
1271+bindingOf n imp = annotate (AnnBoundName n imp) (text (show n))
1273+-- | Pretty-printer helper for names that attaches the correct annotations
1274+prettyName :: Bool -- ^^ whether to show namespaces
1275+ -> [(Name, Bool)] -- ^^ the current bound variables and whether they are implicit
1276+ -> Name -- ^^ the name to pprint
1277+ -> Doc OutputAnnotation
1278+prettyName showNS bnd n | Just imp <- lookup n bnd = annotate (AnnBoundName n imp) (text (strName n))
1279+ | otherwise = annotate (AnnName n Nothing Nothing) (text (strName n))
1280+ where strName (UN n) = T.unpack n
1281+ strName (NS n ns) | showNS = (concatMap (++ ".") . map T.unpack . reverse) ns ++ strName n
1282+ | otherwise = strName n
1283+ strName (MN i s) = T.unpack s
1284+ strName other = show other
1287+showCImp :: Bool -> PClause -> Doc OutputAnnotation
1288+showCImp impl (PClause _ n l ws r w)
1289+ = prettyImp impl l <+> showWs ws <+> text "=" <+> prettyImp impl r
1290+ <+> text "where" <+> text (show w)
1293+ showWs (x : xs) = text "|" <+> prettyImp impl x <+> showWs xs
1294+showCImp impl (PWith _ n l ws r w)
1295+ = prettyImp impl l <+> showWs ws <+> text "with" <+> prettyImp impl r
1296+ <+> braces (text (show w))
1299+ showWs (x : xs) = text "|" <+> prettyImp impl x <+> showWs xs
1302+showDImp :: Bool -> PData -> Doc OutputAnnotation
1303+showDImp impl (PDatadecl n ty cons)
1304+ = text "data" <+> text (show n) <+> colon <+> prettyImp impl ty <+> text "where" <$>
1305+ (indent 2 $ vsep (map (\ (_, n, t, _, _) -> pipe <+> prettyName False [] n <+> colon <+> prettyImp impl t) cons))
1307+showDecls :: Bool -> [PDecl] -> Doc OutputAnnotation
1308+showDecls i ds = vsep (map (showDeclImp i) ds)
1310+showDeclImp _ (PFix _ f ops) = text (show f) <+> cat (punctuate (text ",") (map text ops))
1311+showDeclImp i (PTy _ _ _ _ n t) = text "tydecl" <+> text (showCG n) <+> colon <+> prettyImp i t
1312+showDeclImp i (PClauses _ _ n cs) = text "pat" <+> text (showCG n) <+> text "\t" <+>
1313+ indent 2 (vsep (map (showCImp i) cs))
1314+showDeclImp _ (PData _ _ _ _ d) = showDImp True d
1315+showDeclImp i (PParams _ ns ps) = text "params" <+> braces (text (show ns) <> line <> showDecls i ps <> line)
1316+showDeclImp i (PNamespace n ps) = text "namespace" <+> text n <> braces (line <> showDecls i ps <> line)
1317+showDeclImp _ (PSyntax _ syn) = text "syntax" <+> text (show syn)
1318+showDeclImp i (PClass _ _ _ cs n ps ds)
1319+ = text "class" <+> text (show cs) <+> text (show n) <+> text (show ps) <> line <> showDecls i ds
1320+showDeclImp i (PInstance _ _ cs n _ t _ ds)
1321+ = text "instance" <+> text (show cs) <+> text (show n) <+> prettyImp i t <> line <> showDecls i ds
1322+showDeclImp _ _ = text "..."
1323+-- showDeclImp (PImport i) = "import " ++ i
1325+instance Show (Doc OutputAnnotation) where
1326+ show = flip (displayS . renderCompact) ""
1328+getImps :: [PArg] -> [(Name, PTerm)]
1330+getImps (PImp _ _ _ n tm _ : xs) = (n, tm) : getImps xs
1331+getImps (_ : xs) = getImps xs
1333+getExps :: [PArg] -> [PTerm]
1335+getExps (PExp _ _ tm _ : xs) = tm : getExps xs
1336+getExps (_ : xs) = getExps xs
1338+getConsts :: [PArg] -> [PTerm]
1340+getConsts (PConstraint _ _ tm _ : xs) = tm : getConsts xs
1341+getConsts (_ : xs) = getConsts xs
1343+getAll :: [PArg] -> [PTerm]
1180-- | Show Idris name
1347-- | Show Idris name
1181showName :: Maybe IState -- ^^ the Idris state, for information about names and colours
1348showName :: Maybe IState -- ^^ the Idris state, for information about names and colours
1182 -> [(Name, Bool)] -- ^^ the bound variables and whether they're implicit
1349 -> [(Name, Bool)] -- ^^ the bound variables and whether they're implicit
@@ -1206,171 +1373,14 @@ showName ist bnd impl colour n = case ist of
1206 -- (like error messages). Thus, unknown vars are colourised as implicits.
1373 -- (like error messages). Thus, unknown vars are colourised as implicits.
1207 | otherwise -> colouriseImplicit t name
1374 | otherwise -> colouriseImplicit t name
1209−-- | Show Idris term
1210−showImp :: Maybe IState -- ^^ the Idris state, for information about identifiers and colours
1376+showTm :: IState -- ^^ the Idris state, for information about identifiers and colours
1211− -> Bool -- ^^ whether to show implicits
1212− -> Bool -- ^^ whether to colourise
1213− -> PTerm -- ^^ the term to show
1377+ -> PTerm -- ^^ the term to show
1214− -> String
1378+ -> String
1215−showImp ist impl colour tm = se 10 [] tm where
1379+showTm ist = displayDecorated (consoleDecorate ist) . renderCompact . prettyImp (opt_showimp (idris_options ist))
1216− perhapsColourise :: (ColourTheme -> String -> String) -> String -> String
1217− perhapsColourise col str = case ist of
1218− Just i -> if colour then col (idris_colourTheme i) str else str
1221− se :: Int -> [(Name, Bool)] -> PTerm -> String
1222− se p bnd (PQuote r) = "![" ++ show r ++ "]"
1223− se p bnd (PPatvar fc n) = if impl then show n ++ "[p]" else show n
1224− se p bnd (PInferRef fc n) = "!" ++ show n -- ++ "[" ++ show fc ++ "]"
1226− | Just str <- slist p bnd e = str
1227− | Just num <- snat p e = perhapsColourise colouriseData (show num)
1228− se p bnd (PRef fc n) = showName ist bnd impl colour n
1229− se p bnd (PLam n ty sc) = bracket p 2 $ "\\ " ++ perhapsColourise colouriseBound (show n) ++
1230− (if impl then " : " ++ se 10 bnd ty else "") ++ " => "
1231− ++ se 10 ((n, False):bnd) sc
1232− se p bnd (PLet n ty v sc) = bracket p 2 $ "let " ++ perhapsColourise colouriseBound (show n) ++
1233− " = " ++ se 10 bnd v ++
1234− " in " ++ se 10 ((n, False):bnd) sc
1235− se p bnd (PPi (Exp l s _ param) n ty sc)
1236− | n `elem` allNamesIn sc || impl
1238− (if Lazy `elem` l then "|(" else "(") ++
1239− perhapsColourise colouriseBound (show n) ++ " : " ++ se 10 bnd ty ++
1241− (if (impl && param) then "P" else "") ++
1243− "-> " ++ se 10 ((n, False):bnd) sc
1244− | otherwise = bracket p 2 $ se 0 bnd ty ++ " " ++ st ++ "-> " ++ se 10 bnd sc
1245− where st = case s of
1246− Static -> "[static] "
1248− se p bnd (PPi (Imp l s _ _) n ty sc)
1249− | impl = bracket p 2 $ (if Lazy `elem` l then "|{" else "{") ++
1250− perhapsColourise colouriseBound (show n) ++ " : " ++ se 10 bnd ty ++
1251− "} " ++ st ++ "-> " ++ se 10 ((n, True):bnd) sc
1252− | otherwise = se 10 ((n, True):bnd) sc
1253− where st = case s of
1254− Static -> "[static] "
1256− se p bnd (PPi (Constraint _ _ _) n ty sc)
1257− = bracket p 2 $ se 10 bnd ty ++ " => " ++ se 10 bnd sc
1258− se p bnd (PPi (TacImp _ _ s _) n ty sc)
1260− "{tacimp " ++ (perhapsColourise colouriseBound (show n)) ++ " : " ++ se 10 bnd ty ++ "} -> " ++
1261− se 10 ((n, False):bnd) sc
1262− se p bnd (PMatchApp _ f) = "match " ++ show f
1263− se p bnd (PApp _ hd@(PRef fc f) [tm])
1264− | PConstant (Idris.Core.TT.Str str) <- getTm tm,
1265− f == sUN "Symbol_" = "'" ++ se 10 bnd (PRef fc (sUN str))
1266− se p bnd (PApp _ hd@(PRef _ f) [])
1267− | not impl = se p bnd hd
1268− se p bnd (PAppBind _ hd@(PRef _ f) [])
1269− | not impl = "!" ++ se p bnd hd
1270− se p bnd (PApp _ op@(PRef _ (UN nm)) args)
1271− | not (tnull nm) &&
1272− length (getExps args) == 2 && not impl && not (isAlpha (thead nm))
1273− = let [l, r] = getExps args in
1274− bracket p 1 $ se 1 bnd l ++ " " ++ se p bnd op ++ " " ++ se 0 bnd r
1275− se p bnd (PApp _ f as)
1276− = -- let args = getExps as in
1277− bracket p 1 $ se 1 bnd f ++
1278− if impl then concatMap (sArg bnd) as
1279− else concatMap (suiArg impl bnd) as
1280− se p bnd (PAppBind _ f as)
1281− = let args = getExps as in
1282− "!" ++ (bracket p 1 $ se 1 bnd f ++ if impl then concatMap (sArg bnd) as
1283− else concatMap (seArg bnd) args)
1284− se p bnd (PCase _ scr opts) = "case " ++ se 10 bnd scr ++ " of " ++ showSep " | " (map sc opts)
1285− where sc (l, r) = se 10 bnd l ++ " => " ++ se 10 bnd r
1286− se p bnd (PHidden tm) = "." ++ se 0 bnd tm
1287− se p bnd (PRefl _ t)
1288− | not impl = perhapsColourise colouriseData "refl"
1289− | otherwise = perhapsColourise colouriseData $ "refl {" ++ se 10 bnd t ++ "}"
1290− se p bnd (PResolveTC _) = "resolvetc"
1291− se p bnd (PTrue _) = perhapsColourise colouriseType "()"
1292− se p bnd (PFalse _) = perhapsColourise colouriseType "_|_"
1293− se p bnd (PEq _ l r) = bracket p 2 $ se 10 bnd l ++ perhapsColourise colouriseType " = " ++ se 10 bnd r
1294− se p bnd (PRewrite _ l r _) = bracket p 2 $ "rewrite " ++ se 10 bnd l ++ " in " ++ se 10 bnd r
1295− se p bnd (PTyped l r) = "(" ++ se 10 bnd l ++ " : " ++ se 10 bnd r ++ ")"
1296− se p bnd (PPair _ l r) = "(" ++ se 10 bnd l ++ ", " ++ se 10 bnd r ++ ")"
1297− se p bnd (PDPair _ l t r) = "(" ++ se 10 bnd l ++ " ** " ++ se 10 bnd r ++ ")"
1298− se p bnd (PAlternative a as) = "(|" ++ showSep " , " (map (se 10 bnd) as) ++ "|)"
1299− se p bnd PType = perhapsColourise colouriseType "Type"
1300− se p bnd (PConstant c) = perhapsColourise (cfun c) (show c)
1301− where cfun (AType _) = colouriseType
1302− cfun StrType = colouriseType
1303− cfun PtrType = colouriseType
1304− cfun VoidType = colouriseType
1305− cfun _ = colouriseData
1306− se p bnd (PProof ts) = "proof { " ++ show ts ++ "}"
1307− se p bnd (PTactics ts) = "tactics { " ++ show ts ++ "}"
1308− se p bnd (PMetavar n) = "?" ++ show n
1309− se p bnd (PReturn f) = "return"
1310− se p bnd PImpossible = "impossible"
1311− se p bnd Placeholder = "_"
1312− se p bnd (PDoBlock _) = "do block show not implemented"
1313− se p bnd (PElabError s) = show s
1314− se p bnd (PCoerced t) = se p bnd t
1315− se p bnd (PUnifyLog t) = "%unifyLog " ++ se p bnd t
1316− se p bnd (PDisamb ns t) = "%disamb " ++ show ns ++ se p bnd t
1317− se p bnd (PNoImplicits t) = "%noimplicit " ++ se p bnd t
1318−-- se p bnd x = "Not implemented"
1320− slist' p bnd (PApp _ (PRef _ nil) _)
1321− | not impl && nsroot nil == sUN "Nil" = Just []
1322− slist' p bnd (PApp _ (PRef _ cons) args)
1323− | nsroot cons == sUN "::",
1324− (PExp {getTm=tl}):(PExp {getTm=hd}):imps <- reverse args,
1326− Just tl' <- slist' p bnd tl
1329− isImp (PImp {}) = True
1331− slist' _ _ _ = Nothing
1333− slist p bnd e | Just es <- slist' p bnd e = Just $
1334− case es of [] -> "[]"
1335− [x] -> "[" ++ se p bnd x ++ "]"
1336− xs -> "[" ++ intercalate "," (map (se p bnd ) xs) ++ "]"
1337− slist _ _ _ = Nothing
1339− -- since Prelude is always imported, S & Z are unqualified iff they're the
1342− | show o == (natns++"Z") || show o == "Z" = Just 0
1343− snat p (PApp _ s [PExp {getTm=n}])
1344− | show s == (natns++"S") || show s == "S",
1345− Just n' <- snat p n
1349− natns = "Prelude.Nat."
1351− sArg bnd (PImp _ _ _ n tm _) = siArg bnd (n, tm)
1381+-- | Show a term with implicits, no colours
1352− sArg bnd (PExp _ _ tm _) = seArg bnd tm
1382+showTmImpls :: PTerm -> String
1353− sArg bnd (PConstraint _ _ tm _) = scArg bnd tm
1383+showTmImpls = flip (displayS . renderCompact . prettyImp True) ""
1354− sArg bnd (PTacImplicit _ _ n _ tm _) = stiArg bnd (n, tm)
1356− -- show argument, implicits given by the user also shown
1357− suiArg impl bnd (PImp _ mi _ n tm _)
1358− | impl || not mi = siArg bnd (n, tm)
1359− suiArg impl bnd (PExp _ _ tm _) = seArg bnd tm
1360− suiArg impl bnd _ = ""
1362− seArg bnd arg = " " ++ se 0 bnd arg
1363− siArg bnd (n, val) =
1365− val' = se 10 bnd val in
1367− then " {" ++ n' ++ "}"
1368− else " {" ++ n' ++ " = " ++ val' ++ "}"
1369− scArg bnd val = " {{" ++ se 10 bnd val ++ "}}"
1370− stiArg bnd (n, val) = " {auto " ++ show n ++ " = " ++ se 10 bnd val ++ "}"
1372− bracket outer inner str | inner > outer = "(" ++ str ++ ")"
1376instance Sized PTerm where
1386instance Sized PTerm where