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