on 809c283 help sign in
Idris-dev/src/Idris/AbsSyntaxTree.hs
1039 lines 36 kB history raw
1{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, DeriveFunctor,
2 TypeSynonymInstances, PatternGuards #-}
4module Idris.AbsSyntaxTree where
6import Core.TT
7import Core.Evaluate
8import Core.Elaborate hiding (Tactic(..))
9import Core.Typecheck
10import IRTS.Lang
11import IRTS.CodegenCommon
12import Util.Pretty
14import Paths_idris
16import System.Console.Haskeline
18import Control.Monad.Trans.State.Strict
20import Data.List
21import Data.Char
22import Data.Either
24import Debug.Trace
26data IOption = IOption { opt_logLevel :: Int,
27 opt_typecase :: Bool,
28 opt_typeintype :: Bool,
29 opt_coverage :: Bool,
30 opt_showimp :: Bool,
31 opt_errContext :: Bool,
32 opt_repl :: Bool,
33 opt_verbose :: Bool,
34 opt_target :: Target,
35 opt_outputTy :: OutputType,
36 opt_ibcsubdir :: FilePath,
37 opt_importdirs :: [FilePath],
38 opt_cmdline :: [Opt] -- remember whole command line
39 }
40 deriving (Show, Eq)
42defaultOpts = IOption 0 False False True False False True True ViaC Executable "" [] []
44-- TODO: Add 'module data' to IState, which can be saved out and reloaded quickly (i.e
45-- without typechecking).
46-- This will include all the functions and data declarations, plus fixity declarations
47-- and syntax macros.
49data IState = IState {
50 tt_ctxt :: Context,
51 idris_constraints :: [(UConstraint, FC)],
52 idris_infixes :: [FixDecl],
53 idris_implicits :: Ctxt [PArg],
54 idris_statics :: Ctxt [Bool],
55 idris_classes :: Ctxt ClassInfo,
56 idris_dsls :: Ctxt DSL,
57 idris_optimisation :: Ctxt OptInfo,
58 idris_datatypes :: Ctxt TypeInfo,
59 idris_patdefs :: Ctxt [([Name], Term, Term)], -- not exported
60 idris_flags :: Ctxt [FnOpt],
61 idris_callgraph :: Ctxt CGInfo, -- name, args used in each pos
62 idris_calledgraph :: Ctxt [Name],
63 idris_docstrings :: Ctxt String,
64 idris_totcheck :: [(FC, Name)],
65 idris_log :: String,
66 idris_options :: IOption,
67 idris_name :: Int,
68 idris_metavars :: [Name],
69 syntax_rules :: [Syntax],
70 syntax_keywords :: [String],
71 imported :: [FilePath],
72 idris_scprims :: [(Name, (Int, PrimFn))],
73 idris_objs :: [FilePath],
74 idris_libs :: [String],
75 idris_hdrs :: [String],
76 proof_list :: [(Name, [String])],
77 errLine :: Maybe Int,
78 lastParse :: Maybe Name,
79 indent_stack :: [Int],
80 brace_stack :: [Maybe Int],
81 hide_list :: [(Name, Maybe Accessibility)],
82 default_access :: Accessibility,
83 default_total :: Bool,
84 ibc_write :: [IBCWrite],
85 compiled_so :: Maybe String
86 }
88data SizeChange = Smaller | Same | Bigger | Unknown
89 deriving (Show, Eq)
90{-!
91deriving instance Binary SizeChange
92!-}
94type SCGEntry = (Name, [Maybe (Int, SizeChange)])
96data CGInfo = CGInfo { argsdef :: [Name],
97 calls :: [(Name, [[Name]])],
98 scg :: [SCGEntry],
99 argsused :: [Name],
100 unusedpos :: [Int] }
101 deriving Show
102{-!
103deriving instance Binary CGInfo
104!-}
106primDefs = [UN "unsafePerformIO", UN "mkLazyForeign", UN "mkForeign", UN "FalseElim"]
108-- information that needs writing for the current module's .ibc file
109data IBCWrite = IBCFix FixDecl
110 | IBCImp Name
111 | IBCStatic Name
112 | IBCClass Name
113 | IBCInstance Bool Name Name
114 | IBCDSL Name
115 | IBCData Name
116 | IBCOpt Name
117 | IBCSyntax Syntax
118 | IBCKeyword String
119 | IBCImport FilePath
120 | IBCObj FilePath
121 | IBCLib String
122 | IBCHeader String
123 | IBCAccess Name Accessibility
124 | IBCTotal Name Totality
125 | IBCFlags Name [FnOpt]
126 | IBCCG Name
127 | IBCDoc Name
128 | IBCDef Name -- i.e. main context
129 deriving Show
131idrisInit = IState initContext [] [] emptyContext emptyContext emptyContext
132 emptyContext emptyContext emptyContext emptyContext
133 emptyContext emptyContext emptyContext emptyContext
134 [] "" defaultOpts 6 [] [] [] [] [] [] [] []
135 [] Nothing Nothing [] [] [] Hidden False [] Nothing
137-- The monad for the main REPL - reading and processing files and updating
138-- global state (hence the IO inner monad).
139type Idris = StateT IState (InputT IO)
141-- Commands in the REPL
143data Target = ViaC | ViaJava
144 deriving (Show, Eq)
146data Command = Quit
147 | Help
148 | Eval PTerm
149 | Check PTerm
150 | DocStr Name
151 | TotCheck Name
152 | Reload
153 | Load FilePath
154 | ModImport String
155 | Edit
156 | Compile Target String
157 | Execute
158 | ExecVal PTerm
159 | NewCompile String
160 | Metavars
161 | Prove Name
162 | AddProof (Maybe Name)
163 | RmProof Name
164 | ShowProof Name
165 | Proofs
166 | Universes
167 | TTShell
168 | LogLvl Int
169 | Spec PTerm
170 | HNF PTerm
171 | Defn Name
172 | Info Name
173 | Missing Name
174 | Pattelab PTerm
175 | DebugInfo Name
176 | Search PTerm
177 | SetOpt Opt
178 | UnsetOpt Opt
179 | NOP
181data Opt = Filename String
182 | Ver
183 | Usage
184 | ShowLibs
185 | ShowLibdir
186 | ShowIncs
187 | NoPrelude
188 | NoREPL
189 | OLogging Int
190 | Output String
191 | NewOutput String
192 | TypeCase
193 | TypeInType
194 | DefaultTotal
195 | DefaultPartial
196 | WarnPartial
197 | NoCoverage
198 | ErrContext
199 | ShowImpl
200 | Verbose
201 | IBCSubDir String
202 | ImportDir String
203 | PkgBuild String
204 | PkgInstall String
205 | PkgClean String
206 | WarnOnly
207 | Pkg String
208 | BCAsm String
209 | DumpDefun String
210 | DumpCases String
211 | FOVM String
212 | UseTarget Target
213 | OutputTy OutputType
214 deriving (Show, Eq)
216-- Parsed declarations
218data Fixity = Infixl { prec :: Int }
219 | Infixr { prec :: Int }
220 | InfixN { prec :: Int }
221 | PrefixN { prec :: Int }
222 deriving Eq
223{-!
224deriving instance Binary Fixity
225!-}
227instance Show Fixity where
228 show (Infixl i) = "infixl " ++ show i
229 show (Infixr i) = "infixr " ++ show i
230 show (InfixN i) = "infix " ++ show i
231 show (PrefixN i) = "prefix " ++ show i
233data FixDecl = Fix Fixity String
234 deriving Eq
236instance Show FixDecl where
237 show (Fix f s) = show f ++ " " ++ s
239{-!
240deriving instance Binary FixDecl
241!-}
243instance Ord FixDecl where
244 compare (Fix x _) (Fix y _) = compare (prec x) (prec y)
247data Static = Static | Dynamic
248 deriving (Show, Eq)
249{-!
250deriving instance Binary Static
251!-}
253-- Mark bindings with their explicitness, and laziness
254data Plicity = Imp { plazy :: Bool,
255 pstatic :: Static,
256 pdocstr :: String }
257 | Exp { plazy :: Bool,
258 pstatic :: Static,
259 pdocstr :: String }
260 | Constraint { plazy :: Bool,
261 pstatic :: Static,
262 pdocstr :: String }
263 | TacImp { plazy :: Bool,
264 pstatic :: Static,
265 pscript :: PTerm,
266 pdocstr :: String }
267 deriving (Show, Eq)
269{-!
270deriving instance Binary Plicity
271!-}
273impl = Imp False Dynamic ""
274expl = Exp False Dynamic ""
275constraint = Constraint False Dynamic ""
276tacimpl t = TacImp False Dynamic t ""
278data FnOpt = Inlinable | TotalFn | PartialFn
279 | Coinductive | AssertTotal | TCGen
280 | CExport String -- export, with a C name
281 | Specialise [Name] -- specialise it, freeze these names
282 deriving (Show, Eq)
283{-!
284deriving instance Binary FnOpt
285!-}
287type FnOpts = [FnOpt]
289inlinable :: FnOpts -> Bool
290inlinable = elem Inlinable
292data PDecl' t = PFix FC Fixity [String] -- fixity declaration
293 | PTy String SyntaxInfo FC FnOpts Name t -- type declaration
294 | PClauses FC FnOpts Name [PClause' t] -- pattern clause
295 | PCAF FC Name t -- top level constant
296 | PData String SyntaxInfo FC Bool -- codata
297 (PData' t) -- data declaration
298 | PParams FC [(Name, t)] [PDecl' t] -- params block
299 | PNamespace String [PDecl' t] -- new namespace
300 | PRecord String SyntaxInfo FC Name t String Name t -- record declaration
301 | PClass String SyntaxInfo FC
302 [t] -- constraints
303 Name
304 [(Name, t)] -- parameters
305 [PDecl' t] -- declarations
306 | PInstance SyntaxInfo FC [t] -- constraints
307 Name -- class
308 [t] -- parameters
309 t -- full instance type
310 (Maybe Name) -- explicit name
311 [PDecl' t]
312 | PDSL Name (DSL' t)
313 | PSyntax FC Syntax
314 | PMutual FC [PDecl' t]
315 | PDirective (Idris ())
316 deriving Functor
317{-!
318deriving instance Binary PDecl'
319!-}
321data PClause' t = PClause FC Name t [t] t [PDecl' t]
322 | PWith FC Name t [t] t [PDecl' t]
323 | PClauseR FC [t] t [PDecl' t]
324 | PWithR FC [t] t [PDecl' t]
325 deriving Functor
326{-!
327deriving instance Binary PClause'
328!-}
330data PData' t = PDatadecl { d_name :: Name,
331 d_tcon :: t,
332 d_cons :: [(String, Name, t, FC)] }
333 | PLaterdecl { d_name :: Name, d_tcon :: t }
334 deriving Functor
335{-!
336deriving instance Binary PData'
337!-}
339-- Handy to get a free function for applying PTerm -> PTerm functions
340-- across a program, by deriving Functor
342type PDecl = PDecl' PTerm
343type PData = PData' PTerm
344type PClause = PClause' PTerm
346-- get all the names declared in a decl
348declared :: PDecl -> [Name]
349declared (PFix _ _ _) = []
350declared (PTy _ _ _ _ n t) = [n]
351declared (PClauses _ _ n _) = [] -- not a declaration
352declared (PRecord _ _ _ n _ _ c _) = [n, c]
353declared (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
354 where fstt (_, a, _, _) = a
355declared (PParams _ _ ds) = concatMap declared ds
356declared (PMutual _ ds) = concatMap declared ds
357declared (PNamespace _ ds) = concatMap declared ds
358-- declared (PImport _) = []
360defined :: PDecl -> [Name]
361defined (PFix _ _ _) = []
362defined (PTy _ _ _ _ n t) = []
363defined (PClauses _ _ n _) = [n] -- not a declaration
364defined (PRecord _ _ _ n _ _ c _) = [n, c]
365defined (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
366 where fstt (_, a, _, _) = a
367defined (PParams _ _ ds) = concatMap defined ds
368defined (PMutual _ ds) = concatMap defined ds
369defined (PNamespace _ ds) = concatMap defined ds
370-- declared (PImport _) = []
372updateN :: [(Name, Name)] -> Name -> Name
373updateN ns n | Just n' <- lookup n ns = n'
374updateN _ n = n
376updateNs :: [(Name, Name)] -> PTerm -> PTerm
377updateNs [] t = t
378updateNs ns t = mapPT updateRef t
379 where updateRef (PRef fc f) = PRef fc (updateN ns f)
380 updateRef t = t
382-- updateDNs :: [(Name, Name)] -> PDecl -> PDecl
383-- updateDNs [] t = t
384-- updateDNs ns (PTy s f n t) | Just n' <- lookup n ns = PTy s f n' t
385-- updateDNs ns (PClauses f n c) | Just n' <- lookup n ns = PClauses f n' (map updateCNs c)
386-- where updateCNs ns (PClause n l ts r ds)
387-- = PClause (updateN ns n) (fmap (updateNs ns) l)
388-- (map (fmap (updateNs ns)) ts)
389-- (fmap (updateNs ns) r)
390-- (map (updateDNs ns) ds)
391-- updateDNs ns c = c
393-- High level language terms
395data PTerm = PQuote Raw
396 | PRef FC Name
397 | PPatvar FC Name
398 | PLam Name PTerm PTerm
399 | PPi Plicity Name PTerm PTerm
400 | PLet Name PTerm PTerm PTerm
401 | PTyped PTerm PTerm -- term with explicit type
402 | PApp FC PTerm [PArg]
403 | PCase FC PTerm [(PTerm, PTerm)]
404 | PTrue FC
405 | PFalse FC
406 | PRefl FC PTerm
407 | PResolveTC FC
408 | PEq FC PTerm PTerm
409 | PPair FC PTerm PTerm
410 | PDPair FC PTerm PTerm PTerm
411 | PAlternative Bool [PTerm] -- True if only one may work
412 | PHidden PTerm -- irrelevant or hidden pattern
413 | PSet
414 | PConstant Const
415 | Placeholder
416 | PDoBlock [PDo]
417 | PIdiom FC PTerm
418 | PReturn FC
419 | PMetavar Name
420 | PProof [PTactic]
421 | PTactics [PTactic] -- as PProof, but no auto solving
422 | PElabError Err -- error to report on elaboration
423 | PImpossible -- special case for declaring when an LHS can't typecheck
424 deriving Eq
425{-!
426deriving instance Binary PTerm
427!-}
429mapPT :: (PTerm -> PTerm) -> PTerm -> PTerm
430mapPT f t = f (mpt t) where
431 mpt (PLam n t s) = PLam n (mapPT f t) (mapPT f s)
432 mpt (PPi p n t s) = PPi p n (mapPT f t) (mapPT f s)
433 mpt (PLet n ty v s) = PLet n (mapPT f ty) (mapPT f v) (mapPT f s)
434 mpt (PApp fc t as) = PApp fc (mapPT f t) (map (fmap (mapPT f)) as)
435 mpt (PCase fc c os) = PCase fc (mapPT f c) (map (pmap (mapPT f)) os)
436 mpt (PEq fc l r) = PEq fc (mapPT f l) (mapPT f r)
437 mpt (PTyped l r) = PTyped (mapPT f l) (mapPT f r)
438 mpt (PPair fc l r) = PPair fc (mapPT f l) (mapPT f r)
439 mpt (PDPair fc l t r) = PDPair fc (mapPT f l) (mapPT f t) (mapPT f r)
440 mpt (PAlternative a as) = PAlternative a (map (mapPT f) as)
441 mpt (PHidden t) = PHidden (mapPT f t)
442 mpt (PDoBlock ds) = PDoBlock (map (fmap (mapPT f)) ds)
443 mpt (PProof ts) = PProof (map (fmap (mapPT f)) ts)
444 mpt (PTactics ts) = PTactics (map (fmap (mapPT f)) ts)
445 mpt x = x
448data PTactic' t = Intro [Name] | Intros | Focus Name
449 | Refine Name [Bool] | Rewrite t | LetTac Name t
450 | Exact t | Compute | Trivial
451 | Solve
452 | Attack
453 | ProofState | ProofTerm | Undo
454 | Try (PTactic' t) (PTactic' t)
455 | TSeq (PTactic' t) (PTactic' t)
456 | Qed | Abandon
457 deriving (Show, Eq, Functor)
458{-!
459deriving instance Binary PTactic'
460!-}
462instance Sized a => Sized (PTactic' a) where
463 size (Intro nms) = 1 + size nms
464 size Intros = 1
465 size (Focus nm) = 1 + size nm
466 size (Refine nm bs) = 1 + size nm + length bs
467 size (Rewrite t) = 1 + size t
468 size (LetTac nm t) = 1 + size nm + size t
469 size (Exact t) = 1 + size t
470 size Compute = 1
471 size Trivial = 1
472 size Solve = 1
473 size Attack = 1
474 size ProofState = 1
475 size ProofTerm = 1
476 size Undo = 1
477 size (Try l r) = 1 + size l + size r
478 size (TSeq l r) = 1 + size l + size r
479 size Qed = 1
480 size Abandon = 1
482type PTactic = PTactic' PTerm
484data PDo' t = DoExp FC t
485 | DoBind FC Name t
486 | DoBindP FC t t
487 | DoLet FC Name t t
488 | DoLetP FC t t
489 deriving (Eq, Functor)
490{-!
491deriving instance Binary PDo'
492!-}
494instance Sized a => Sized (PDo' a) where
495 size (DoExp fc t) = 1 + size fc + size t
496 size (DoBind fc nm t) = 1 + size fc + size nm + size t
497 size (DoBindP fc l r) = 1 + size fc + size l + size r
498 size (DoLet fc nm l r) = 1 + size fc + size nm + size l + size r
499 size (DoLetP fc l r) = 1 + size fc + size l + size r
501type PDo = PDo' PTerm
503-- The priority gives a hint as to elaboration order. Best to elaborate
504-- things early which will help give a more concrete type to other
505-- variables, e.g. a before (interpTy a).
507data PArg' t = PImp { priority :: Int,
508 lazyarg :: Bool, pname :: Name, getTm :: t,
509 pargdoc :: String }
510 | PExp { priority :: Int,
511 lazyarg :: Bool, getTm :: t,
512 pargdoc :: String }
513 | PConstraint { priority :: Int,
514 lazyarg :: Bool, getTm :: t,
515 pargdoc :: String }
516 | PTacImplicit { priority :: Int,
517 lazyarg :: Bool, pname :: Name,
518 getScript :: t,
519 getTm :: t,
520 pargdoc :: String }
521 deriving (Show, Eq, Functor)
523instance Sized a => Sized (PArg' a) where
524 size (PImp p l nm trm _) = 1 + size nm + size trm
525 size (PExp p l trm _) = 1 + size trm
526 size (PConstraint p l trm _) = 1 + size trm
527 size (PTacImplicit p l nm scr trm _) = 1 + size nm + size scr + size trm
529{-!
530deriving instance Binary PArg'
531!-}
533pimp n t = PImp 0 True n t ""
534pexp t = PExp 0 False t ""
535pconst t = PConstraint 0 False t ""
536ptacimp n s t = PTacImplicit 0 True n s t ""
538type PArg = PArg' PTerm
540-- Type class data
542data ClassInfo = CI { instanceName :: Name,
543 class_methods :: [(Name, (FnOpts, PTerm))],
544 class_defaults :: [(Name, (Name, PDecl))], -- method name -> default impl
545 class_params :: [Name],
546 class_instances :: [Name] }
547 deriving Show
548{-!
549deriving instance Binary ClassInfo
550!-}
552data OptInfo = Optimise { collapsible :: Bool,
553 forceable :: [Int], -- argument positions
554 recursive :: [Int] }
555 deriving Show
556{-!
557deriving instance Binary OptInfo
558!-}
561data TypeInfo = TI { con_names :: [Name], codata :: Bool }
562 deriving Show
563{-!
564deriving instance Binary TypeInfo
565!-}
567-- Syntactic sugar info
569data DSL' t = DSL { dsl_bind :: t,
570 dsl_return :: t,
571 dsl_apply :: t,
572 dsl_pure :: t,
573 dsl_var :: Maybe t,
574 index_first :: Maybe t,
575 index_next :: Maybe t,
576 dsl_lambda :: Maybe t,
577 dsl_let :: Maybe t
578 }
579 deriving (Show, Functor)
580{-!
581deriving instance Binary DSL'
582!-}
584type DSL = DSL' PTerm
586data SynContext = PatternSyntax | TermSyntax | AnySyntax
587 deriving Show
588{-!
589deriving instance Binary SynContext
590!-}
592data Syntax = Rule [SSymbol] PTerm SynContext
593 deriving Show
594{-!
595deriving instance Binary Syntax
596!-}
598data SSymbol = Keyword Name
599 | Symbol String
600 | Binding Name
601 | Expr Name
602 | SimpleExpr Name
603 deriving Show
604{-!
605deriving instance Binary SSymbol
606!-}
608initDSL = DSL (PRef f (UN ">>="))
609 (PRef f (UN "return"))
610 (PRef f (UN "<$>"))
611 (PRef f (UN "pure"))
612 Nothing
613 Nothing
614 Nothing
615 Nothing
616 Nothing
617 where f = FC "(builtin)" 0
619data SyntaxInfo = Syn { using :: [(Name, PTerm)],
620 syn_params :: [(Name, PTerm)],
621 syn_namespace :: [String],
622 no_imp :: [Name],
623 decoration :: Name -> Name,
624 inPattern :: Bool,
625 implicitAllowed :: Bool,
626 dsl_info :: DSL }
627 deriving Show
628{-!
629deriving instance Binary SyntaxInfo
630!-}
632defaultSyntax = Syn [] [] [] [] id False False initDSL
634expandNS :: SyntaxInfo -> Name -> Name
635expandNS syn n@(NS _ _) = n
636expandNS syn n = case syn_namespace syn of
637 [] -> n
638 xs -> NS n xs
641--- Pretty printing declarations and terms
643instance Show PTerm where
644 show tm = showImp False tm
646instance Pretty PTerm where
647 pretty = prettyImp False
649instance Show PDecl where
650 show d = showDeclImp False d
652instance Show PClause where
653 show c = showCImp True c
655instance Show PData where
656 show d = showDImp False d
658showDeclImp _ (PFix _ f ops) = show f ++ " " ++ showSep ", " ops
659showDeclImp t (PTy _ _ _ _ n ty) = show n ++ " : " ++ showImp t ty
660showDeclImp _ (PClauses _ _ n c) = showSep "\n" (map show c)
661showDeclImp _ (PData _ _ _ _ d) = show d
662showDeclImp _ (PParams f ns ps) = "parameters " ++ show ns ++ "\n" ++
663 showSep "\n" (map show ps)
666showCImp :: Bool -> PClause -> String
667showCImp impl (PClause _ n l ws r w)
668 = showImp impl l ++ showWs ws ++ " = " ++ showImp impl r
669 ++ " where " ++ show w
670 where
671 showWs [] = ""
672 showWs (x : xs) = " | " ++ showImp impl x ++ showWs xs
673showCImp impl (PWith _ n l ws r w)
674 = showImp impl l ++ showWs ws ++ " with " ++ showImp impl r
675 ++ " { " ++ show w ++ " } "
676 where
677 showWs [] = ""
678 showWs (x : xs) = " | " ++ showImp impl x ++ showWs xs
681showDImp :: Bool -> PData -> String
682showDImp impl (PDatadecl n ty cons)
683 = "data " ++ show n ++ " : " ++ showImp impl ty ++ " where\n\t"
684 ++ showSep "\n\t| "
685 (map (\ (_, n, t, _) -> show n ++ " : " ++ showImp impl t) cons)
687getImps :: [PArg] -> [(Name, PTerm)]
688getImps [] = []
689getImps (PImp _ _ n tm _ : xs) = (n, tm) : getImps xs
690getImps (_ : xs) = getImps xs
692getExps :: [PArg] -> [PTerm]
693getExps [] = []
694getExps (PExp _ _ tm _ : xs) = tm : getExps xs
695getExps (_ : xs) = getExps xs
697getConsts :: [PArg] -> [PTerm]
698getConsts [] = []
699getConsts (PConstraint _ _ tm _ : xs) = tm : getConsts xs
700getConsts (_ : xs) = getConsts xs
702getAll :: [PArg] -> [PTerm]
703getAll = map getTm
705prettyImp :: Bool -> PTerm -> Doc
706prettyImp impl = prettySe 10
707 where
708 prettySe p (PQuote r) =
709 if size r > breakingSize then
710 text "![" $$ pretty r <> text "]"
711 else
712 text "![" <> pretty r <> text "]"
713 prettySe p (PPatvar fc n) = pretty n
714 prettySe p (PRef fc n) =
715 if impl then
716 pretty n
717 else
718 prettyBasic n
719 where
720 prettyBasic n@(UN _) = pretty n
721 prettyBasic (MN _ s) = text s
722 prettyBasic (NS n s) = (foldr (<>) empty (intersperse (text ".") (map text $ reverse s))) <> prettyBasic n
723 prettySe p (PLam n ty sc) =
724 bracket p 2 $
725 if size sc > breakingSize then
726 text "λ" <> pretty n <+> text "=>" $+$ pretty sc
727 else
728 text "λ" <> pretty n <+> text "=>" <+> pretty sc
729 prettySe p (PLet n ty v sc) =
730 bracket p 2 $
731 if size sc > breakingSize then
732 text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" $+$
733 nest nestingSize (prettySe 10 sc)
734 else
735 text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" <+>
736 prettySe 10 sc
737 prettySe p (PPi (Exp l s _) n ty sc)
738 | n `elem` allNamesIn sc || impl =
739 let open = if l then text "|" <> lparen else lparen in
740 bracket p 2 $
741 if size sc > breakingSize then
742 open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
743 st <+> text "->" $+$ prettySe 10 sc
744 else
745 open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
746 st <+> text "->" <+> prettySe 10 sc
747 | otherwise =
748 bracket p 2 $
749 if size sc > breakingSize then
750 prettySe 0 ty <+> st <+> text "->" $+$ prettySe 10 sc
751 else
752 prettySe 0 ty <+> st <+> text "->" <+> prettySe 10 sc
753 where
754 st =
755 case s of
756 Static -> text "[static]"
757 _ -> empty
758 prettySe p (PPi (Imp l s _) n ty sc)
759 | impl =
760 let open = if l then text "|" <> lbrace else lbrace in
761 bracket p 2 $
762 if size sc > breakingSize then
763 open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
764 st <+> text "->" <+> prettySe 10 sc
765 else
766 open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
767 st <+> text "->" <+> prettySe 10 sc
768 | otherwise = prettySe 10 sc
769 where
770 st =
771 case s of
772 Static -> text $ "[static]"
773 _ -> empty
774 prettySe p (PPi (Constraint _ _ _) n ty sc) =
775 bracket p 2 $
776 if size sc > breakingSize then
777 prettySe 10 ty <+> text "=>" <+> prettySe 10 sc
778 else
779 prettySe 10 ty <+> text "=>" $+$ prettySe 10 sc
780 prettySe p (PPi (TacImp _ _ s _) n ty sc) =
781 bracket p 2 $
782 if size sc > breakingSize then
783 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
784 rbrace <+> text "->" $+$ prettySe 10 sc
785 else
786 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
787 rbrace <+> text "->" <+> prettySe 10 sc
788 prettySe p (PApp _ (PRef _ f) [])
789 | not impl = pretty f
790 prettySe p (PApp _ (PRef _ op@(UN (f:_))) args)
791 | length (getExps args) == 2 && (not impl) && (not $ isAlpha f) =
792 let [l, r] = getExps args in
793 bracket p 1 $
794 if size r > breakingSize then
795 prettySe 1 l <+> pretty op $+$ prettySe 0 r
796 else
797 prettySe 1 l <+> pretty op <+> prettySe 0 r
798 prettySe p (PApp _ f as) =
799 let args = getExps as in
800 bracket p 1 $
801 prettySe 1 f <+>
802 if impl then
803 foldl fS empty as
804 -- foldr (<+>) empty $ map prettyArgS as
805 else
806 foldl fSe empty args
807 -- foldr (<+>) empty $ map prettyArgSe args
808 where
809 fS l r =
810 if size r > breakingSize then
811 l $+$ nest nestingSize (prettyArgS r)
812 else
813 l <+> prettyArgS r
815 fSe l r =
816 if size r > breakingSize then
817 l $+$ nest nestingSize (prettyArgSe r)
818 else
819 l <+> prettyArgSe r
820 prettySe p (PCase _ scr opts) =
821 text "case" <+> prettySe 10 scr <+> text "of" $+$ nest nestingSize prettyBody
822 where
823 prettyBody = foldr ($$) empty $ intersperse (text "|") $ map sc opts
825 sc (l, r) = prettySe 10 l <+> text "=>" <+> prettySe 10 r
826 prettySe p (PHidden tm) = text "." <> prettySe 0 tm
827 prettySe p (PRefl _ _) = text "refl"
828 prettySe p (PResolveTC _) = text "resolvetc"
829 prettySe p (PTrue _) = text "()"
830 prettySe p (PFalse _) = text "_|_"
831 prettySe p (PEq _ l r) =
832 bracket p 2 $
833 if size r > breakingSize then
834 prettySe 10 l <+> text "=" $$ nest nestingSize (prettySe 10 r)
835 else
836 prettySe 10 l <+> text "=" <+> prettySe 10 r
837 prettySe p (PTyped l r) =
838 lparen <> prettySe 10 l <+> colon <+> prettySe 10 r <> rparen
839 prettySe p (PPair _ l r) =
840 if size r > breakingSize then
841 lparen <> prettySe 10 l <> text "," $+$
842 prettySe 10 r <> rparen
843 else
844 lparen <> prettySe 10 l <> text "," <+> prettySe 10 r <> rparen
845 prettySe p (PDPair _ l t r) =
846 if size r > breakingSize then
847 lparen <> prettySe 10 l <+> text "**" $+$
848 prettySe 10 r <> rparen
849 else
850 lparen <> prettySe 10 l <+> text "**" <+> prettySe 10 r <> rparen
851 prettySe p (PAlternative a as) =
852 lparen <> text "|" <> prettyAs <> text "|" <> rparen
853 where
854 prettyAs =
855 foldr (\l -> \r -> l <+> text "," <+> r) empty $ map (prettySe 10) as
856 prettySe p PSet = text "Set"
857 prettySe p (PConstant c) = pretty c
858 -- XXX: add pretty for tactics
859 prettySe p (PProof ts) =
860 text "proof" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
861 prettySe p (PTactics ts) =
862 text "tactics" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
863 prettySe p (PMetavar n) = text "?" <> pretty n
864 prettySe p (PReturn f) = text "return"
865 prettySe p PImpossible = text "impossible"
866 prettySe p Placeholder = text "_"
867 prettySe p (PDoBlock _) = text "do block pretty not implemented"
868 prettySe p (PElabError s) = pretty s
870 prettySe p _ = text "test"
872 prettyArgS (PImp _ _ n tm _) = prettyArgSi (n, tm)
873 prettyArgS (PExp _ _ tm _) = prettyArgSe tm
874 prettyArgS (PConstraint _ _ tm _) = prettyArgSc tm
875 prettyArgS (PTacImplicit _ _ n _ tm _) = prettyArgSti (n, tm)
877 prettyArgSe arg = prettySe 0 arg
878 prettyArgSi (n, val) = lbrace <> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
879 prettyArgSc val = lbrace <> lbrace <> prettySe 10 val <> rbrace <> rbrace
880 prettyArgSti (n, val) = lbrace <> text "auto" <+> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
882 bracket outer inner doc
883 | inner > outer = lparen <> doc <> rparen
884 | otherwise = doc
886showImp :: Bool -> PTerm -> String
887showImp impl tm = se 10 tm where
888 se p (PQuote r) = "![" ++ show r ++ "]"
889 se p (PPatvar fc n) = show n
890 se p (PRef fc n) = if impl then show n -- ++ "[" ++ show fc ++ "]"
891 else showbasic n
892 where showbasic n@(UN _) = show n
893 showbasic (MN _ s) = s
894 showbasic (NS n s) = showSep "." (reverse s) ++ "." ++ showbasic n
895 se p (PLam n ty sc) = bracket p 2 $ "\\ " ++ show n ++
896 (if impl then " : " ++ se 10 ty else "") ++ " => "
897 ++ se 10 sc
898 se p (PLet n ty v sc) = bracket p 2 $ "let " ++ show n ++ " = " ++ se 10 v ++
899 " in " ++ se 10 sc
900 se p (PPi (Exp l s _) n ty sc)
901 | n `elem` allNamesIn sc || impl
902 = bracket p 2 $
903 if l then "|(" else "(" ++
904 show n ++ " : " ++ se 10 ty ++
905 ") " ++ st ++
906 "-> " ++ se 10 sc
907 | otherwise = bracket p 2 $ se 0 ty ++ " " ++ st ++ "-> " ++ se 10 sc
908 where st = case s of
909 Static -> "[static] "
910 _ -> ""
911 se p (PPi (Imp l s _) n ty sc)
912 | impl = bracket p 2 $ if l then "|{" else "{" ++
913 show n ++ " : " ++ se 10 ty ++
914 "} " ++ st ++ "-> " ++ se 10 sc
915 | otherwise = se 10 sc
916 where st = case s of
917 Static -> "[static] "
918 _ -> ""
919 se p (PPi (Constraint _ _ _) n ty sc)
920 = bracket p 2 $ se 10 ty ++ " => " ++ se 10 sc
921 se p (PPi (TacImp _ _ s _) n ty sc)
922 = bracket p 2 $ "{tacimp " ++ show n ++ " : " ++ se 10 ty ++ "} -> " ++ se 10 sc
923 se p (PApp _ (PRef _ f) [])
924 | not impl = show f
925 se p (PApp _ (PRef _ op@(UN (f:_))) args)
926 | length (getExps args) == 2 && not impl && not (isAlpha f)
927 = let [l, r] = getExps args in
928 bracket p 1 $ se 1 l ++ " " ++ show op ++ " " ++ se 0 r
929 se p (PApp _ f as)
930 = let args = getExps as in
931 bracket p 1 $ se 1 f ++ if impl then concatMap sArg as
932 else concatMap seArg args
933 se p (PCase _ scr opts) = "case " ++ se 10 scr ++ " of " ++ showSep " | " (map sc opts)
934 where sc (l, r) = se 10 l ++ " => " ++ se 10 r
935 se p (PHidden tm) = "." ++ se 0 tm
936 se p (PRefl _ t)
937 | not impl = "refl"
938 | otherwise = "refl {" ++ se 10 t ++ "}"
939 se p (PResolveTC _) = "resolvetc"
940 se p (PTrue _) = "()"
941 se p (PFalse _) = "_|_"
942 se p (PEq _ l r) = bracket p 2 $ se 10 l ++ " = " ++ se 10 r
943 se p (PTyped l r) = "(" ++ se 10 l ++ " : " ++ se 10 r ++ ")"
944 se p (PPair _ l r) = "(" ++ se 10 l ++ ", " ++ se 10 r ++ ")"
945 se p (PDPair _ l t r) = "(" ++ se 10 l ++ " ** " ++ se 10 r ++ ")"
946 se p (PAlternative a as) = "(|" ++ showSep " , " (map (se 10) as) ++ "|)"
947 se p PSet = "Set"
948 se p (PConstant c) = show c
949 se p (PProof ts) = "proof { " ++ show ts ++ "}"
950 se p (PTactics ts) = "tactics { " ++ show ts ++ "}"
951 se p (PMetavar n) = "?" ++ show n
952 se p (PReturn f) = "return"
953 se p PImpossible = "impossible"
954 se p Placeholder = "_"
955 se p (PDoBlock _) = "do block show not implemented"
956 se p (PElabError s) = show s
957-- se p x = "Not implemented"
959 sArg (PImp _ _ n tm _) = siArg (n, tm)
960 sArg (PExp _ _ tm _) = seArg tm
961 sArg (PConstraint _ _ tm _) = scArg tm
962 sArg (PTacImplicit _ _ n _ tm _) = stiArg (n, tm)
964 seArg arg = " " ++ se 0 arg
965 siArg (n, val) = " {" ++ show n ++ " = " ++ se 10 val ++ "}"
966 scArg val = " {{" ++ se 10 val ++ "}}"
967 stiArg (n, val) = " {auto " ++ show n ++ " = " ++ se 10 val ++ "}"
969 bracket outer inner str | inner > outer = "(" ++ str ++ ")"
970 | otherwise = str
973instance Sized PTerm where
974 size (PQuote rawTerm) = size rawTerm
975 size (PRef fc name) = size name
976 size (PLam name ty bdy) = 1 + size ty + size bdy
977 size (PPi plicity name ty bdy) = 1 + size ty + size bdy
978 size (PLet name ty def bdy) = 1 + size ty + size def + size bdy
979 size (PTyped trm ty) = 1 + size trm + size ty
980 size (PApp fc name args) = 1 + size args
981 size (PCase fc trm bdy) = 1 + size trm + size bdy
982 size (PTrue fc) = 1
983 size (PFalse fc) = 1
984 size (PRefl fc _) = 1
985 size (PResolveTC fc) = 1
986 size (PEq fc left right) = 1 + size left + size right
987 size (PPair fc left right) = 1 + size left + size right
988 size (PDPair fs left ty right) = 1 + size left + size ty + size right
989 size (PAlternative a alts) = 1 + size alts
990 size (PHidden hidden) = size hidden
991 size PSet = 1
992 size (PConstant const) = 1 + size const
993 size Placeholder = 1
994 size (PDoBlock dos) = 1 + size dos
995 size (PIdiom fc term) = 1 + size term
996 size (PReturn fc) = 1
997 size (PMetavar name) = 1
998 size (PProof tactics) = size tactics
999 size (PElabError err) = size err
1000 size PImpossible = 1
1002allNamesIn :: PTerm -> [Name]
1003allNamesIn tm = nub $ ni [] tm
1004 where
1005 ni env (PRef _ n)
1006 | not (n `elem` env) = [n]
1007 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1008 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1009 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1010 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1011 ni env (PHidden tm) = ni env tm
1012 ni env (PEq _ l r) = ni env l ++ ni env r
1013 ni env (PTyped l r) = ni env l ++ ni env r
1014 ni env (PPair _ l r) = ni env l ++ ni env r
1015 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1016 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1017 ni env (PAlternative a ls) = concatMap (ni env) ls
1018 ni env _ = []
1020namesIn :: [(Name, PTerm)] -> IState -> PTerm -> [Name]
1021namesIn uvars ist tm = nub $ ni [] tm
1022 where
1023 ni env (PRef _ n)
1024 | not (n `elem` env)
1025 = case lookupTy Nothing n (tt_ctxt ist) of
1026 [] -> [n]
1027 _ -> if n `elem` (map fst uvars) then [n] else []
1028 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1029 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1030 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1031 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1032 ni env (PEq _ l r) = ni env l ++ ni env r
1033 ni env (PTyped l r) = ni env l ++ ni env r
1034 ni env (PPair _ l r) = ni env l ++ ni env r
1035 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1036 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1037 ni env (PAlternative a as) = concatMap (ni env) as
1038 ni env (PHidden tm) = ni env tm
1039 ni env _ = []