@@ -30,6 +30,8 @@ import Data.Word (Word)
3030
3131import Debug.Trace
3232
33+import Text.PrettyPrint.Annotated.Leijen
34+
3335data IOption = IOption { opt_logLevel :: Int,
3436 opt_typecase :: Bool,
3537 opt_typeintype :: Bool,
@@ -908,275 +910,440 @@ expandNS syn n = case syn_namespace syn of
908910 xs -> sNS n xs
909911
910912
911−--- Pretty printing declarations and terms
913+-- For inferring types of things
912914
913−instance Show PTerm where
914− show tm = showImp Nothing False False tm
915+bi = fileFC "builtin"
915916
916−instance Pretty PTerm where
917− pretty = prettyImp False
917+inferTy = sMN 0 "__Infer"
918+inferCon = sMN 0 "__infer"
919+inferDecl = PDatadecl inferTy
920+ PType
921+ [("", inferCon, PPi impl (sMN 0 "iType") PType (
922+ PPi expl (sMN 0 "ival") (PRef bi (sMN 0 "iType"))
923+ (PRef bi inferTy)), bi, [])]
924+inferOpts = []
918925
919−instance Show PDecl where
920− show d = showDeclImp False d
926+infTerm t = PApp bi (PRef bi inferCon) [pimp (sMN 0 "iType") Placeholder True, pexp t]
927+infP = P (TCon 6 0) inferTy (TType (UVal 0))
921928
922−instance Show PClause where
923− show c = showCImp True c
929+getInferTerm, getInferType :: Term -> Term
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)
924933
925−instance Show PData where
926− show d = showDImp False d
934+getInferType (Bind n b sc) = Bind n b $ getInferType sc
935+getInferType (App (App _ ty) _) = ty
927936
928−showDecls :: Bool -> [PDecl] -> String
929−showDecls _ [] = ""
930−showDecls i (d:ds) = showDeclImp i d ++ "\n" ++ showDecls i ds
931937
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
945938
939+-- Handy primitives: Unit, False, Pair, MkPair, =, mkForeign, Elim type class
946940
947−showCImp :: Bool -> PClause -> String
948−showCImp impl (PClause _ n l ws r w)
949− = showImp Nothing impl False l ++ showWs ws ++ " = " ++ showImp Nothing impl False r
950− ++ " where " ++ show w
951− where
952− showWs [] = ""
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 ++ " } "
957− where
958− showWs [] = ""
959− showWs (x : xs) = " | " ++ showImp Nothing impl False x ++ showWs xs
941+primNames = [unitTy, unitCon,
942+ falseTy, pairTy, pairCon,
943+ eqTy, eqCon, inferTy, inferCon]
960944
945+unitTy = sMN 0 "__Unit"
946+unitCon = sMN 0 "__II"
947+unitDecl = PDatadecl unitTy PType
948+ [("", unitCon, PRef bi unitTy, bi, [])]
949+unitOpts = [DefaultEliminator]
961950
962−showDImp :: Bool -> PData -> String
963−showDImp impl (PDatadecl n ty cons)
964− = "data " ++ show n ++ " : " ++ showImp Nothing impl False ty ++ " where\n\t"
965− ++ showSep "\n\t| "
966− (map (\ (_, n, t, _, _) -> show n ++ " : " ++ showImp Nothing impl False t) cons)
951+falseTy = sMN 0 "__False"
952+falseDecl = PDatadecl falseTy PType []
953+falseOpts = []
967954
968−getImps :: [PArg] -> [(Name, PTerm)]
969−getImps [] = []
970−getImps (PImp _ _ _ n tm _ : xs) = (n, tm) : getImps xs
971−getImps (_ : xs) = getImps xs
955+pairTy = sMN 0 "__Pair"
956+pairCon = sMN 0 "__MkPair"
957+pairDecl = PDatadecl pairTy (piBind [(n "A", PType), (n "B", PType)] PType)
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, [])]
964+ where n a = sMN 0 a
965+pairOpts = []
972966
973−getExps :: [PArg] -> [PTerm]
974−getExps [] = []
975−getExps (PExp _ _ tm _ : xs) = tm : getExps xs
976−getExps (_ : xs) = getExps xs
967+eqTy = sUN "="
968+eqCon = sUN "refl"
969+eqDecl = PDatadecl eqTy (piBind [(n "A", PType), (n "B", PType),
970+ (n "x", PRef bi (n "A")), (n "y", PRef bi (n "B"))]
971+ PType)
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+ where n a = sMN 0 a
979+eqOpts = []
977980
978−getConsts :: [PArg] -> [PTerm]
979−getConsts [] = []
980−getConsts (PConstraint _ _ tm _ : xs) = tm : getConsts xs
981−getConsts (_ : xs) = getConsts xs
981+elimName = sUN "__Elim"
982+elimMethElimTy = sUN "__elimTy"
983+elimMethElim = sUN "elim"
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)]
987+
988+-- Defined in builtins.idr
989+sigmaTy = sUN "Exists"
990+existsCon = sUN "Ex_intro"
991+
992+piBind :: [(Name, PTerm)] -> PTerm -> PTerm
993+piBind = piBindp expl
994+
995+piBindp :: Plicity -> [(Name, PTerm)] -> PTerm -> PTerm
996+piBindp p [] t = t
997+piBindp p ((n, ty):ns) t = PPi p n ty (piBindp p ns t)
982998
983−getAll :: [PArg] -> [PTerm]
984−getAll = map getTm
985999
986−-- | Pretty-print a high-level Idris term
1000+-- Pretty-printing declarations and terms
1001+
1002+instance Show PTerm where
1003+ showsPrec _ tm = (displayS . renderCompact . prettyImp False) tm
1004+
1005+instance Show PDecl where
1006+ showsPrec _ d = (displayS . renderCompact . showDeclImp False) d
1007+
1008+instance Show PClause where
1009+ showsPrec _ c = (displayS . renderCompact . showCImp True) c
1010+
1011+instance Show PData where
1012+ showsPrec _ d = (displayS . renderCompact . showDImp False) d
1013+
1014+instance Pretty PTerm OutputAnnotation where
1015+ pretty = prettyImp False
1016+
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
1019+-- terminal.
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
1030+ in case () of
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
1036+
1037+-- | Pretty-print a high-level closed Idris term
9871038prettyImp :: Bool -- ^^ whether to show implicits
9881039 -> PTerm -- ^^ the term to pretty-print
989− -> Doc
990−prettyImp impl = prettySe 10
1040+ -> Doc OutputAnnotation
1041+prettyImp impl = pprintPTerm impl []
1042+
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
9911049 where
992− prettySe p (PQuote r) =
1050+ prettySe :: Int -> [(Name, Bool)] -> PTerm -> Doc OutputAnnotation
1051+ prettySe p bnd (PQuote r) =
9931052 if size r > breakingSize then
994− text "![" $$ pretty r <> text "]"
1053+ text "![" <> line <> pretty r <> text "]"
9951054 else
9961055 text "![" <> pretty r <> text "]"
997− prettySe p (PPatvar fc n) = pretty n
998− prettySe p (PRef fc n) =
999− if impl then
1000− pretty n
1001− else
1002− prettyBasic n
1003− where
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) =
1056+ prettySe p bnd (PPatvar fc n) = pretty n
1057+ prettySe p bnd e
1058+ | Just str <- slist p bnd e = str
1059+ | Just n <- snat p e = annotate AnnConstData (text (show n))
1060+ prettySe p bnd (PRef fc n) = prettyName impl bnd n
1061+ prettySe p bnd (PLam n ty sc) =
10091062 bracket p 2 $
10101063 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
10121065 else
1013− text "λ" <> pretty n <+> text "=>" <+> pretty sc
1014− prettySe p (PLet n ty v sc) =
1066+ text "\\" <> bindingOf n False <+> text "=>" <+> prettySe 10 ((n, False):bnd) sc
1067+ prettySe p bnd (PLet n ty v sc) =
10151068 bracket p 2 $
10161069 if size sc > breakingSize then
1017− text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" $+$
1018− nest nestingSize (prettySe 10 sc)
1070+ text "let" <+> bindingOf n False <+> text "=" <+> prettySe 10 bnd v <+> text "in" <>
1071+ nest nestingSize (prettySe 10 ((n, False):bnd) sc)
10191072 else
1020− text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" <+>
1021− prettySe 10 sc
1022− prettySe p (PPi (Exp l s _ _) n ty sc)
1073+ text "let" <+> bindingOf n False <+> text "=" <+> prettySe 10 bnd v <+> text "in" <+>
1074+ prettySe 10 ((n, False):bnd) sc
1075+ prettySe p bnd (PPi (Exp l s _ _) n ty sc)
10231076 | n `elem` allNamesIn sc || impl =
10241077 let open = if Lazy `elem` l then text "|" <> lparen else lparen in
10251078 bracket p 2 $
10261079 if size sc > breakingSize then
1027− open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
1028− st <+> text "->" $+$ prettySe 10 sc
1080+ enclose open rparen (bindingOf n False <+> colon <+> prettySe 10 bnd ty) <+>
1081+ st <> text "->" <> line <> prettySe 10 ((n, False):bnd) sc
10291082 else
1030− open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
1031− st <+> text "->" <+> prettySe 10 sc
1083+ enclose open rparen (bindingOf n False <+> colon <+> prettySe 10 bnd ty) <+>
1084+ st <> text "->" <+> prettySe 10 ((n, False):bnd) sc
10321085 | otherwise =
10331086 bracket p 2 $
10341087 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
10361089 else
1037− prettySe 0 ty <+> st <+> text "->" <+> prettySe 10 sc
1090+ prettySe 0 bnd ty <+> st <> text "->" <+> prettySe 10 ((n, False):bnd) sc
10381091 where
10391092 st =
10401093 case s of
1041− Static -> text "[static]"
1094+ Static -> text "[static]" <> space
10421095 _ -> empty
1043− prettySe p (PPi (Imp l s _ _) n ty sc)
1096+ prettySe p bnd (PPi (Imp l s _ _) n ty sc)
10441097 | impl =
10451098 let open = if Lazy `elem` l then text "|" <> lbrace else lbrace in
10461099 bracket p 2 $
10471100 if size sc > breakingSize then
1048− open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
1049− st <+> text "->" <+> prettySe 10 sc
1101+ open <> bindingOf n True <+> colon <+> prettySe 10 bnd ty <> rbrace <+>
1102+ st <> text "->" <+> prettySe 10 ((n, True):bnd) sc
10501103 else
1051− open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
1052− st <+> text "->" <+> prettySe 10 sc
1053− | otherwise = prettySe 10 sc
1104+ open <> bindingOf n True <+> colon <+> prettySe 10 bnd ty <> rbrace <+>
1105+ st <> text "->" <+> prettySe 10 ((n, True):bnd) sc
1106+ | otherwise = prettySe 10 ((n, True):bnd) sc
10541107 where
10551108 st =
10561109 case s of
1057− Static -> text $ "[static]"
1110+ Static -> text "[static]" <> space
10581111 _ -> empty
1059− prettySe p (PPi (Constraint _ _ _) n ty sc) =
1112+ prettySe p bnd (PPi (Constraint _ _ _) n ty sc) =
10601113 bracket p 2 $
10611114 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
10631116 else
1064− prettySe 10 ty <+> text "=>" $+$ prettySe 10 sc
1065− prettySe p (PPi (TacImp _ _ s _) n ty sc) =
1117+ prettySe 10 bnd ty <+> text "=>" <> line <> prettySe 10 ((n, True):bnd) sc
1118+ prettySe p bnd (PPi (TacImp _ _ s _) n ty sc) =
10661119 bracket p 2 $
10671120 if size sc > breakingSize then
1068− lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
1069− rbrace <+> text "->" $+$ prettySe 10 sc
1121+ lbrace <> text "tacimp" <+> bindingOf n True <+> colon <+> prettySe 10 bnd ty <>
1122+ rbrace <+> text "->" <> line <> prettySe 10 ((n, True):bnd) sc
10701123 else
1071− lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
1072− rbrace <+> text "->" <+> prettySe 10 sc
1073− prettySe p (PApp _ (PRef _ f) [])
1074− | not impl = pretty f
1075− prettySe p (PAppBind _ (PRef _ f) [])
1076− | not impl = text "!" <+> pretty f
1077− prettySe p (PApp _ (PRef _ op@(UN nm)) args)
1078− | not (tnull nm) &&
1124+ lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 bnd ty <>
1125+ rbrace <+> text "->" <+> prettySe 10 ((n, True):bnd) sc
1126+ prettySe p bnd (PApp _ (PRef _ f) [])
1127+ | not impl = prettyName impl bnd f
1128+ prettySe p bnd (PAppBind _ (PRef _ f) [])
1129+ | not impl = text "!" <> prettyName impl bnd f
1130+ prettySe p bnd (PApp _ (PRef _ op) args)
1131+ | UN nm <- basename op
1132+ , not (tnull nm) &&
10791133 length (getExps args) == 2 && (not impl) && (not $ isAlpha (thead nm)) =
10801134 let [l, r] = getExps args in
10811135 bracket p 1 $
10821136 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
10841138 else
1085− prettySe 1 l <+> pretty op <+> prettySe 0 r
1086− prettySe p (PApp _ f as) =
1087− let args = getExps as in
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))
1143+ prettySe p bnd (PApp _ f as) =
1144+ let args = getExps as
1145+ fp = prettySe 1 bnd f
1146+ in
10881147 bracket p 1 $
1089− prettySe 1 f <+>
1090− if impl then
1091− foldl' fS empty as
1092− -- foldr (<+>) empty $ map prettyArgS as
1093− else
1094− foldl' fSe empty args
1095− -- foldr (<+>) empty $ map prettyArgSe args
1148+ hsep (if impl
1149+ then fp : map (prettyArgS bnd) as
1150+ else fp : map (prettyArgSe bnd) args)
1151+ prettySe p bnd (PCase _ scr opts) =
1152+ text "case" <+> prettySe 10 bnd scr <+> text "of" <> prettyBody
10961153 where
1097− fS l r =
1098− if size r > breakingSize then
1099− l $+$ nest nestingSize (prettyArgS r)
1100− else
1101− l <+> prettyArgS r
1102−
1103− fSe l r =
1104− if size r > breakingSize then
1105− l $+$ nest nestingSize (prettyArgSe r)
1106− else
1107− l <+> prettyArgSe r
1108− prettySe p (PCase _ scr opts) =
1109− text "case" <+> prettySe 10 scr <+> text "of" $+$ nest nestingSize prettyBody
1110− where
1111− prettyBody = foldr ($$) empty $ intersperse (text "|") $ map sc opts
1112−
1113− sc (l, r) = prettySe 10 l <+> text "=>" <+> prettySe 10 r
1114− prettySe p (PHidden tm) = text "." <> prettySe 0 tm
1115− prettySe p (PRefl _ _) = text "refl"
1116− prettySe p (PResolveTC _) = text "resolvetc"
1117− prettySe p (PTrue _) = text "()"
1118− prettySe p (PFalse _) = text "_|_"
1119− prettySe p (PEq _ l r) =
1154+ prettyBody = foldr (<>) empty $ intersperse (text "|") $ map sc opts
1155+
1156+ sc (l, r) = nest nestingSize $ prettySe 10 bnd l <+> text "=>" <+> prettySe 10 bnd r
1157+ prettySe p bnd (PHidden tm) = text "." <> prettySe 0 bnd tm
1158+ prettySe p bnd (PRefl _ _) = annotate (AnnName eqCon Nothing Nothing) $ text "refl"
1159+ prettySe p bnd (PResolveTC _) = text "resolvetc"
1160+ prettySe p bnd (PTrue _) = annotate (AnnName unitTy Nothing Nothing) $ text "()"
1161+ prettySe p bnd (PFalse _) = annotate (AnnName falseTy Nothing Nothing) $ text "_|_"
1162+ prettySe p bnd (PEq _ l r) =
11201163 bracket p 2 $
11211164 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)
11231166 else
1124− prettySe 10 l <+> text "=" <+> prettySe 10 r
1125− prettySe p (PRewrite _ l r _) =
1167+ prettySe 10 bnd l <+> eq <+> prettySe 10 bnd r
1168+ where eq = annotate (AnnName eqTy Nothing Nothing) (text "=")
1169+ prettySe p bnd (PRewrite _ l r _) =
11261170 bracket p 2 $
11271171 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)
11291173 else
1130− text "rewrite" <+> prettySe 10 l <+> text "in" <+> prettySe 10 r
1131− prettySe p (PTyped l r) =
1132− lparen <> prettySe 10 l <+> colon <+> prettySe 10 r <> rparen
1133− prettySe p (PPair _ l r) =
1174+ text "rewrite" <+> prettySe 10 bnd l <+> text "in" <+> prettySe 10 bnd r
1175+ prettySe p bnd (PTyped l r) =
1176+ lparen <> prettySe 10 bnd l <+> colon <+> prettySe 10 bnd r <> rparen
1177+ prettySe p bnd (PPair _ l r) =
11341178 if size r > breakingSize then
1135− lparen <> prettySe 10 l <> text "," $+$
1136− prettySe 10 r <> rparen
1179+ lparen <> prettySe 10 bnd l <> text "," <>
1180+ line <> prettySe 10 bnd r <> rparen
11371181 else
1138− lparen <> prettySe 10 l <> text "," <+> prettySe 10 r <> rparen
1139− prettySe p (PDPair _ l t r) =
1182+ lparen <> prettySe 10 bnd l <> text "," <+> prettySe 10 bnd r <> rparen
1183+ prettySe p bnd (PDPair _ l t r) =
11401184 if size r > breakingSize then
1141− lparen <> prettySe 10 l <+> text "**" $+$
1142− prettySe 10 r <> rparen
1185+ lparen <> prettySe 10 bnd l <+> text "**" <>
1186+ line <> prettySe 10 bnd r <> rparen
11431187 else
1144− lparen <> prettySe 10 l <+> text "**" <+> prettySe 10 r <> rparen
1145− prettySe p (PAlternative a as) =
1188+ lparen <> prettySe 10 bnd l <+> text "**" <+> prettySe 10 bnd r <> rparen
1189+ prettySe p bnd (PAlternative a as) =
11461190 lparen <> text "|" <> prettyAs <> text "|" <> rparen
11471191 where
11481192 prettyAs =
1149− foldr (\l -> \r -> l <+> text "," <+> r) empty $ map (prettySe 10) as
1150− prettySe p PType = text "Type"
1151− prettySe p (PConstant c) = pretty c
1193+ foldr (\l -> \r -> l <+> text "," <+> r) empty $ map (prettySe 10 bnd) as
1194+ prettySe p bnd PType = annotate AnnConstType $ text "Type"
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
11521201 -- XXX: add pretty for tactics
1153− prettySe p (PProof ts) =
1154− text "proof" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
1155− prettySe p (PTactics ts) =
1156− text "tactics" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
1157− prettySe p (PMetavar n) = text "?" <> pretty n
1158− prettySe p (PReturn f) = text "return"
1159− prettySe p PImpossible = text "impossible"
1160− prettySe p Placeholder = text "_"
1161− prettySe p (PDoBlock _) = text "do block pretty not implemented"
1162− prettySe p (PElabError s) = pretty s
1163−
1164− prettySe p _ = text "test"
1165−
1166− prettyArgS (PImp _ _ _ n tm _) = prettyArgSi (n, tm)
1167− prettyArgS (PExp _ _ tm _) = prettyArgSe tm
1168− prettyArgS (PConstraint _ _ tm _) = prettyArgSc tm
1169− prettyArgS (PTacImplicit _ _ n _ tm _) = prettyArgSti (n, tm)
1170−
1171− prettyArgSe arg = prettySe 0 arg
1172− prettyArgSi (n, val) = lbrace <> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
1173− prettyArgSc val = lbrace <> lbrace <> prettySe 10 val <> rbrace <> rbrace
1174− prettyArgSti (n, val) = lbrace <> text "auto" <+> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
1202+ prettySe p bnd (PProof ts) =
1203+ text "proof" <+> lbrace <> nest nestingSize (text . show $ ts) <> rbrace
1204+ prettySe p bnd (PTactics ts) =
1205+ text "tactics" <+> lbrace <> nest nestingSize (text . show $ ts) <> rbrace
1206+ prettySe p bnd (PMetavar n) = text "?" <> pretty n
1207+ prettySe p bnd (PReturn f) = text "return"
1208+ prettySe p bnd PImpossible = text "impossible"
1209+ prettySe p bnd Placeholder = text "_"
1210+ prettySe p bnd (PDoBlock _) = text "do block pretty not implemented"
1211+ prettySe p bnd (PElabError s) = pretty s
1212+
1213+ prettySe p bnd _ = text "test"
1214+
1215+ prettyArgS bnd (PImp _ _ _ n tm _) = prettyArgSi bnd (n, tm)
1216+ prettyArgS bnd (PExp _ _ tm _) = prettyArgSe bnd tm
1217+ prettyArgS bnd (PConstraint _ _ tm _) = prettyArgSc bnd tm
1218+ prettyArgS bnd (PTacImplicit _ _ n _ tm _) = prettyArgSti bnd (n, tm)
1219+
1220+ prettyArgSe bnd arg = prettySe 0 bnd arg
1221+ prettyArgSi bnd (n, val) = lbrace <> pretty n <+> text "=" <+> prettySe 10 bnd val <> rbrace
1222+ prettyArgSc bnd val = lbrace <> lbrace <> prettySe 10 bnd val <> rbrace <> rbrace
1223+ prettyArgSti bnd (n, val) = lbrace <> text "auto" <+> pretty n <+> text "=" <+> prettySe 10 bnd val <> rbrace
1224+
1225+ basename :: Name -> Name
1226+ basename (NS n _) = basename n
1227+ basename n = n
1228+
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,
1234+ all isImp imps,
1235+ Just tl' <- slist' p bnd tl
1236+ = Just (hd:tl')
1237+ where
1238+ isImp (PImp {}) = True
1239+ isImp _ = False
1240+ slist' _ _ _ = Nothing
1241+
1242+ slist p bnd e | Just es <- slist' p bnd e = Just $
1243+ case es of [] -> annotate AnnConstData $ text "[]"
1244+ [x] -> enclose left
1245+ right
1246+ (prettySe p bnd x)
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
1252+
1253+ natns = "Prelude.Nat."
1254+
1255+ snat p (PRef _ z)
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
1260+ = Just $ 1 + n'
1261+ snat _ _ = Nothing
11751262
11761263 bracket outer inner doc
11771264 | inner > outer = lparen <> doc <> rparen
11781265 | otherwise = doc
11791266
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))
1272+
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
1285+
1286+
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)
1291+ where
1292+ showWs [] = empty
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))
1297+ where
1298+ showWs [] = empty
1299+ showWs (x : xs) = text "|" <+> prettyImp impl x <+> showWs xs
1300+
1301+
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))
1306+
1307+showDecls :: Bool -> [PDecl] -> Doc OutputAnnotation
1308+showDecls i ds = vsep (map (showDeclImp i) ds)
1309+
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
1324+
1325+instance Show (Doc OutputAnnotation) where
1326+ show = flip (displayS . renderCompact) ""
1327+
1328+getImps :: [PArg] -> [(Name, PTerm)]
1329+getImps [] = []
1330+getImps (PImp _ _ _ n tm _ : xs) = (n, tm) : getImps xs
1331+getImps (_ : xs) = getImps xs
1332+
1333+getExps :: [PArg] -> [PTerm]
1334+getExps [] = []
1335+getExps (PExp _ _ tm _ : xs) = tm : getExps xs
1336+getExps (_ : xs) = getExps xs
1337+
1338+getConsts :: [PArg] -> [PTerm]
1339+getConsts [] = []
1340+getConsts (PConstraint _ _ tm _ : xs) = tm : getConsts xs
1341+getConsts (_ : xs) = getConsts xs
1342+
1343+getAll :: [PArg] -> [PTerm]
1344+getAll = map getTm
1345+
1346+
11801347-- | Show Idris name
11811348showName :: Maybe IState -- ^^ the Idris state, for information about names and colours
11821349 -> [(Name, Bool)] -- ^^ the bound variables and whether they're implicit
@@ -1206,171 +1373,14 @@ showName ist bnd impl colour n = case ist of
12061373 -- (like error messages). Thus, unknown vars are colourised as implicits.
12071374 | otherwise -> colouriseImplicit t name
12081375
1209−-- | Show Idris term
1210−showImp :: Maybe 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
1214− -> String
1215−showImp ist impl colour tm = se 10 [] tm where
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
1219− Nothing -> str
1220−
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 ++ "]"
1225− se p bnd e
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
1237− = bracket p 2 $
1238− (if Lazy `elem` l then "|(" else "(") ++
1239− perhapsColourise colouriseBound (show n) ++ " : " ++ se 10 bnd ty ++
1240− ") " ++
1241− (if (impl && param) then "P" else "") ++
1242− st ++
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] "
1247− _ -> ""
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] "
1255− _ -> ""
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)
1259− = bracket p 2 $
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"
1319−
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,
1325− all isImp imps,
1326− Just tl' <- slist' p bnd tl
1327− = Just (hd:tl')
1328− where
1329− isImp (PImp {}) = True
1330− isImp _ = False
1331− slist' _ _ _ = Nothing
1332−
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
1338−
1339− -- since Prelude is always imported, S & Z are unqualified iff they're the
1340− -- Nat ones.
1341− snat p (PRef _ o)
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
1346− = Just $ 1 + n'
1347− snat _ _ = Nothing
1348−
1349− natns = "Prelude.Nat."
1376+showTm :: IState -- ^^ the Idris state, for information about identifiers and colours
1377+ -> PTerm -- ^^ the term to show
1378+ -> String
1379+showTm ist = displayDecorated (consoleDecorate ist) . renderCompact . prettyImp (opt_showimp (idris_options ist))
13501380
1351− sArg bnd (PImp _ _ _ n tm _) = siArg bnd (n, tm)
1352− sArg bnd (PExp _ _ tm _) = seArg bnd tm
1353− sArg bnd (PConstraint _ _ tm _) = scArg bnd tm
1354− sArg bnd (PTacImplicit _ _ n _ tm _) = stiArg bnd (n, tm)
1355−
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 _ = ""
1361−
1362− seArg bnd arg = " " ++ se 0 bnd arg
1363− siArg bnd (n, val) =
1364− let n' = show n
1365− val' = se 10 bnd val in
1366− if (n' == val')
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 ++ "}"
1371−
1372− bracket outer inner str | inner > outer = "(" ++ str ++ ")"
1373− | otherwise = str
1381+-- | Show a term with implicits, no colours
1382+showTmImpls :: PTerm -> String
1383+showTmImpls = flip (displayS . renderCompact . prettyImp True) ""
13741384
13751385
13761386instance Sized PTerm where