on 4187c57 help sign in
Idris-dev/src/Idris/AbsSyntaxTree.hs
1105 lines 38 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 idris_coercions :: [Name],
70 syntax_rules :: [Syntax],
71 syntax_keywords :: [String],
72 imported :: [FilePath],
73 idris_scprims :: [(Name, (Int, PrimFn))],
74 idris_objs :: [FilePath],
75 idris_libs :: [String],
76 idris_hdrs :: [String],
77 proof_list :: [(Name, [String])],
78 errLine :: Maybe Int,
79 lastParse :: Maybe Name,
80 indent_stack :: [Int],
81 brace_stack :: [Maybe Int],
82 hide_list :: [(Name, Maybe Accessibility)],
83 default_access :: Accessibility,
84 default_total :: Bool,
85 ibc_write :: [IBCWrite],
86 compiled_so :: Maybe String
87 }
89data SizeChange = Smaller | Same | Bigger | Unknown
90 deriving (Show, Eq)
91{-!
92deriving instance Binary SizeChange
93!-}
95type SCGEntry = (Name, [Maybe (Int, SizeChange)])
97data CGInfo = CGInfo { argsdef :: [Name],
98 calls :: [(Name, [[Name]])],
99 scg :: [SCGEntry],
100 argsused :: [Name],
101 unusedpos :: [Int] }
102 deriving Show
103{-!
104deriving instance Binary CGInfo
105!-}
107primDefs = [UN "unsafePerformIO", UN "mkLazyForeign", UN "mkForeign", UN "FalseElim"]
109-- information that needs writing for the current module's .ibc file
110data IBCWrite = IBCFix FixDecl
111 | IBCImp Name
112 | IBCStatic Name
113 | IBCClass Name
114 | IBCInstance Bool Name Name
115 | IBCDSL Name
116 | IBCData Name
117 | IBCOpt Name
118 | IBCSyntax Syntax
119 | IBCKeyword String
120 | IBCImport FilePath
121 | IBCObj FilePath
122 | IBCLib String
123 | IBCHeader String
124 | IBCAccess Name Accessibility
125 | IBCTotal Name Totality
126 | IBCFlags Name [FnOpt]
127 | IBCCG Name
128 | IBCDoc Name
129 | IBCCoercion Name
130 | IBCDef Name -- i.e. main context
131 deriving Show
133idrisInit = IState initContext [] [] emptyContext emptyContext emptyContext
134 emptyContext emptyContext emptyContext emptyContext
135 emptyContext emptyContext emptyContext emptyContext
136 [] "" defaultOpts 6 [] [] [] [] [] [] [] [] []
137 [] Nothing Nothing [] [] [] Hidden False [] Nothing
139-- The monad for the main REPL - reading and processing files and updating
140-- global state (hence the IO inner monad).
141type Idris = StateT IState IO
143-- Commands in the REPL
145data Target = ViaC
146 | ViaJava
147 | ViaNode
148 | ViaJavaScript
149 | Bytecode
150 deriving (Show, Eq)
152data Command = Quit
153 | Help
154 | Eval PTerm
155 | Check PTerm
156 | DocStr Name
157 | TotCheck Name
158 | Reload
159 | Load FilePath
160 | ModImport String
161 | Edit
162 | Compile Target String
163 | Execute
164 | ExecVal PTerm
165 | NewCompile String
166 | Metavars
167 | Prove Name
168 | AddProof (Maybe Name)
169 | RmProof Name
170 | ShowProof Name
171 | Proofs
172 | Universes
173 | TTShell
174 | LogLvl Int
175 | Spec PTerm
176 | HNF PTerm
177 | Defn Name
178 | Info Name
179 | Missing Name
180 | Pattelab PTerm
181 | DebugInfo Name
182 | Search PTerm
183 | SetOpt Opt
184 | UnsetOpt Opt
185 | NOP
187data Opt = Filename String
188 | Ver
189 | Usage
190 | ShowLibs
191 | ShowLibdir
192 | ShowIncs
193 | NoPrelude
194 | NoREPL
195 | OLogging Int
196 | Output String
197 | NewOutput String
198 | TypeCase
199 | TypeInType
200 | DefaultTotal
201 | DefaultPartial
202 | WarnPartial
203 | NoCoverage
204 | ErrContext
205 | ShowImpl
206 | Verbose
207 | IBCSubDir String
208 | ImportDir String
209 | PkgBuild String
210 | PkgInstall String
211 | PkgClean String
212 | WarnOnly
213 | Pkg String
214 | BCAsm String
215 | DumpDefun String
216 | DumpCases String
217 | FOVM String
218 | UseTarget Target
219 | OutputTy OutputType
220 deriving (Show, Eq)
222-- Parsed declarations
224data Fixity = Infixl { prec :: Int }
225 | Infixr { prec :: Int }
226 | InfixN { prec :: Int }
227 | PrefixN { prec :: Int }
228 deriving Eq
229{-!
230deriving instance Binary Fixity
231!-}
233instance Show Fixity where
234 show (Infixl i) = "infixl " ++ show i
235 show (Infixr i) = "infixr " ++ show i
236 show (InfixN i) = "infix " ++ show i
237 show (PrefixN i) = "prefix " ++ show i
239data FixDecl = Fix Fixity String
240 deriving Eq
242instance Show FixDecl where
243 show (Fix f s) = show f ++ " " ++ s
245{-!
246deriving instance Binary FixDecl
247!-}
249instance Ord FixDecl where
250 compare (Fix x _) (Fix y _) = compare (prec x) (prec y)
253data Static = Static | Dynamic
254 deriving (Show, Eq)
255{-!
256deriving instance Binary Static
257!-}
259-- Mark bindings with their explicitness, and laziness
260data Plicity = Imp { plazy :: Bool,
261 pstatic :: Static,
262 pdocstr :: String }
263 | Exp { plazy :: Bool,
264 pstatic :: Static,
265 pdocstr :: String }
266 | Constraint { plazy :: Bool,
267 pstatic :: Static,
268 pdocstr :: String }
269 | TacImp { plazy :: Bool,
270 pstatic :: Static,
271 pscript :: PTerm,
272 pdocstr :: String }
273 deriving (Show, Eq)
275{-!
276deriving instance Binary Plicity
277!-}
279impl = Imp False Dynamic ""
280expl = Exp False Dynamic ""
281constraint = Constraint False Dynamic ""
282tacimpl t = TacImp False Dynamic t ""
284data FnOpt = Inlinable | TotalFn | PartialFn
285 | Coinductive | AssertTotal | TCGen
286 | Implicit -- implicit coercion
287 | CExport String -- export, with a C name
288 | Specialise [Name] -- specialise it, freeze these names
289 deriving (Show, Eq)
290{-!
291deriving instance Binary FnOpt
292!-}
294type FnOpts = [FnOpt]
296inlinable :: FnOpts -> Bool
297inlinable = elem Inlinable
299data PDecl' t
300 = PFix FC Fixity [String] -- fixity declaration
301 | PTy String SyntaxInfo FC FnOpts Name t -- type declaration
302 | PPostulate String SyntaxInfo FC FnOpts Name t
303 | PClauses FC FnOpts Name [PClause' t] -- pattern clause
304 | PCAF FC Name t -- top level constant
305 | PData String SyntaxInfo FC Bool -- codata
306 (PData' t) -- data declaration
307 | PParams FC [(Name, t)] [PDecl' t] -- params block
308 | PNamespace String [PDecl' t] -- new namespace
309 | PRecord String SyntaxInfo FC Name t String Name t -- record declaration
310 | PClass String SyntaxInfo FC
311 [t] -- constraints
312 Name
313 [(Name, t)] -- parameters
314 [PDecl' t] -- declarations
315 | PInstance SyntaxInfo FC [t] -- constraints
316 Name -- class
317 [t] -- parameters
318 t -- full instance type
319 (Maybe Name) -- explicit name
320 [PDecl' t]
321 | PDSL Name (DSL' t)
322 | PSyntax FC Syntax
323 | PMutual FC [PDecl' t]
324 | PDirective (Idris ())
325 deriving Functor
326{-!
327deriving instance Binary PDecl'
328!-}
330data PClause' t = PClause FC Name t [t] t [PDecl' t]
331 | PWith FC Name t [t] t [PDecl' t]
332 | PClauseR FC [t] t [PDecl' t]
333 | PWithR FC [t] t [PDecl' t]
334 deriving Functor
335{-!
336deriving instance Binary PClause'
337!-}
339data PData' t = PDatadecl { d_name :: Name,
340 d_tcon :: t,
341 d_cons :: [(String, Name, t, FC)] }
342 | PLaterdecl { d_name :: Name, d_tcon :: t }
343 deriving Functor
344{-!
345deriving instance Binary PData'
346!-}
348-- Handy to get a free function for applying PTerm -> PTerm functions
349-- across a program, by deriving Functor
351type PDecl = PDecl' PTerm
352type PData = PData' PTerm
353type PClause = PClause' PTerm
355-- get all the names declared in a decl
357declared :: PDecl -> [Name]
358declared (PFix _ _ _) = []
359declared (PTy _ _ _ _ n t) = [n]
360declared (PPostulate _ _ _ _ n t) = [n]
361declared (PClauses _ _ n _) = [] -- not a declaration
362declared (PRecord _ _ _ n _ _ c _) = [n, c]
363declared (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
364 where fstt (_, a, _, _) = a
365declared (PParams _ _ ds) = concatMap declared ds
366declared (PMutual _ ds) = concatMap declared ds
367declared (PNamespace _ ds) = concatMap declared ds
369-- get the names declared, not counting nested parameter blocks
370tldeclared :: PDecl -> [Name]
371tldeclared (PFix _ _ _) = []
372tldeclared (PTy _ _ _ _ n t) = [n]
373tldeclared (PPostulate _ _ _ _ n t) = [n]
374tldeclared (PClauses _ _ n _) = [] -- not a declaration
375tldeclared (PRecord _ _ _ n _ _ c _) = [n, c]
376tldeclared (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
377 where fstt (_, a, _, _) = a
378tldeclared (PParams _ _ ds) = []
379tldeclared (PMutual _ ds) = concatMap tldeclared ds
380tldeclared (PNamespace _ ds) = concatMap tldeclared ds
381-- declared (PImport _) = []
383defined :: PDecl -> [Name]
384defined (PFix _ _ _) = []
385defined (PTy _ _ _ _ n t) = []
386defined (PPostulate _ _ _ _ n t) = []
387defined (PClauses _ _ n _) = [n] -- not a declaration
388defined (PRecord _ _ _ n _ _ c _) = [n, c]
389defined (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
390 where fstt (_, a, _, _) = a
391defined (PParams _ _ ds) = concatMap defined ds
392defined (PMutual _ ds) = concatMap defined ds
393defined (PNamespace _ ds) = concatMap defined ds
394-- declared (PImport _) = []
396updateN :: [(Name, Name)] -> Name -> Name
397updateN ns n | Just n' <- lookup n ns = n'
398updateN _ n = n
400updateNs :: [(Name, Name)] -> PTerm -> PTerm
401updateNs [] t = t
402updateNs ns t = mapPT updateRef t
403 where updateRef (PRef fc f) = PRef fc (updateN ns f)
404 updateRef t = t
406-- updateDNs :: [(Name, Name)] -> PDecl -> PDecl
407-- updateDNs [] t = t
408-- updateDNs ns (PTy s f n t) | Just n' <- lookup n ns = PTy s f n' t
409-- updateDNs ns (PClauses f n c) | Just n' <- lookup n ns = PClauses f n' (map updateCNs c)
410-- where updateCNs ns (PClause n l ts r ds)
411-- = PClause (updateN ns n) (fmap (updateNs ns) l)
412-- (map (fmap (updateNs ns)) ts)
413-- (fmap (updateNs ns) r)
414-- (map (updateDNs ns) ds)
415-- updateDNs ns c = c
417-- High level language terms
419data PTerm = PQuote Raw
420 | PRef FC Name
421 | PInferRef FC Name -- a name to be defined later
422 | PPatvar FC Name
423 | PLam Name PTerm PTerm
424 | PPi Plicity Name PTerm PTerm
425 | PLet Name PTerm PTerm PTerm
426 | PTyped PTerm PTerm -- term with explicit type
427 | PApp FC PTerm [PArg]
428 | PCase FC PTerm [(PTerm, PTerm)]
429 | PTrue FC
430 | PFalse FC
431 | PRefl FC PTerm
432 | PResolveTC FC
433 | PEq FC PTerm PTerm
434 | PPair FC PTerm PTerm
435 | PDPair FC PTerm PTerm PTerm
436 | PAlternative Bool [PTerm] -- True if only one may work
437 | PHidden PTerm -- irrelevant or hidden pattern
438 | PType
439 | PConstant Const
440 | Placeholder
441 | PDoBlock [PDo]
442 | PIdiom FC PTerm
443 | PReturn FC
444 | PMetavar Name
445 | PProof [PTactic]
446 | PTactics [PTactic] -- as PProof, but no auto solving
447 | PElabError Err -- error to report on elaboration
448 | PImpossible -- special case for declaring when an LHS can't typecheck
449 | PCoerced PTerm -- to mark a coerced argument, so as not to coerce twice
450 deriving Eq
451{-!
452deriving instance Binary PTerm
453!-}
455mapPT :: (PTerm -> PTerm) -> PTerm -> PTerm
456mapPT f t = f (mpt t) where
457 mpt (PLam n t s) = PLam n (mapPT f t) (mapPT f s)
458 mpt (PPi p n t s) = PPi p n (mapPT f t) (mapPT f s)
459 mpt (PLet n ty v s) = PLet n (mapPT f ty) (mapPT f v) (mapPT f s)
460 mpt (PApp fc t as) = PApp fc (mapPT f t) (map (fmap (mapPT f)) as)
461 mpt (PCase fc c os) = PCase fc (mapPT f c) (map (pmap (mapPT f)) os)
462 mpt (PEq fc l r) = PEq fc (mapPT f l) (mapPT f r)
463 mpt (PTyped l r) = PTyped (mapPT f l) (mapPT f r)
464 mpt (PPair fc l r) = PPair fc (mapPT f l) (mapPT f r)
465 mpt (PDPair fc l t r) = PDPair fc (mapPT f l) (mapPT f t) (mapPT f r)
466 mpt (PAlternative a as) = PAlternative a (map (mapPT f) as)
467 mpt (PHidden t) = PHidden (mapPT f t)
468 mpt (PDoBlock ds) = PDoBlock (map (fmap (mapPT f)) ds)
469 mpt (PProof ts) = PProof (map (fmap (mapPT f)) ts)
470 mpt (PTactics ts) = PTactics (map (fmap (mapPT f)) ts)
471 mpt x = x
474data PTactic' t = Intro [Name] | Intros | Focus Name
475 | Refine Name [Bool] | Rewrite t
476 | LetTac Name t | LetTacTy Name t t
477 | Exact t | Compute | Trivial
478 | Solve
479 | Attack
480 | ProofState | ProofTerm | Undo
481 | Try (PTactic' t) (PTactic' t)
482 | TSeq (PTactic' t) (PTactic' t)
483 | ReflectTac t -- see Language.Reflection module
484 | GoalType String (PTactic' t)
485 | Qed | Abandon
486 deriving (Show, Eq, Functor)
487{-!
488deriving instance Binary PTactic'
489!-}
491instance Sized a => Sized (PTactic' a) where
492 size (Intro nms) = 1 + size nms
493 size Intros = 1
494 size (Focus nm) = 1 + size nm
495 size (Refine nm bs) = 1 + size nm + length bs
496 size (Rewrite t) = 1 + size t
497 size (LetTac nm t) = 1 + size nm + size t
498 size (Exact t) = 1 + size t
499 size Compute = 1
500 size Trivial = 1
501 size Solve = 1
502 size Attack = 1
503 size ProofState = 1
504 size ProofTerm = 1
505 size Undo = 1
506 size (Try l r) = 1 + size l + size r
507 size (TSeq l r) = 1 + size l + size r
508 size Qed = 1
509 size Abandon = 1
511type PTactic = PTactic' PTerm
513data PDo' t = DoExp FC t
514 | DoBind FC Name t
515 | DoBindP FC t t
516 | DoLet FC Name t t
517 | DoLetP FC t t
518 deriving (Eq, Functor)
519{-!
520deriving instance Binary PDo'
521!-}
523instance Sized a => Sized (PDo' a) where
524 size (DoExp fc t) = 1 + size fc + size t
525 size (DoBind fc nm t) = 1 + size fc + size nm + size t
526 size (DoBindP fc l r) = 1 + size fc + size l + size r
527 size (DoLet fc nm l r) = 1 + size fc + size nm + size l + size r
528 size (DoLetP fc l r) = 1 + size fc + size l + size r
530type PDo = PDo' PTerm
532-- The priority gives a hint as to elaboration order. Best to elaborate
533-- things early which will help give a more concrete type to other
534-- variables, e.g. a before (interpTy a).
535-- TODO: priority no longer serves any purpose, drop it!
537data PArg' t = PImp { priority :: Int,
538 lazyarg :: Bool, pname :: Name, getTm :: t,
539 pargdoc :: String }
540 | PExp { priority :: Int,
541 lazyarg :: Bool, getTm :: t,
542 pargdoc :: String }
543 | PConstraint { priority :: Int,
544 lazyarg :: Bool, getTm :: t,
545 pargdoc :: String }
546 | PTacImplicit { priority :: Int,
547 lazyarg :: Bool, pname :: Name,
548 getScript :: t,
549 getTm :: t,
550 pargdoc :: String }
551 deriving (Show, Eq, Functor)
553instance Sized a => Sized (PArg' a) where
554 size (PImp p l nm trm _) = 1 + size nm + size trm
555 size (PExp p l trm _) = 1 + size trm
556 size (PConstraint p l trm _) = 1 + size trm
557 size (PTacImplicit p l nm scr trm _) = 1 + size nm + size scr + size trm
559{-!
560deriving instance Binary PArg'
561!-}
563pimp n t = PImp 0 True n t ""
564pexp t = PExp 0 False t ""
565pconst t = PConstraint 0 False t ""
566ptacimp n s t = PTacImplicit 0 True n s t ""
568type PArg = PArg' PTerm
570-- Type class data
572data ClassInfo = CI { instanceName :: Name,
573 class_methods :: [(Name, (FnOpts, PTerm))],
574 class_defaults :: [(Name, (Name, PDecl))], -- method name -> default impl
575 class_params :: [Name],
576 class_instances :: [Name] }
577 deriving Show
578{-!
579deriving instance Binary ClassInfo
580!-}
582data OptInfo = Optimise { collapsible :: Bool,
583 forceable :: [Int], -- argument positions
584 recursive :: [Int] }
585 deriving Show
586{-!
587deriving instance Binary OptInfo
588!-}
591data TypeInfo = TI { con_names :: [Name],
592 codata :: Bool,
593 param_pos :: [Int] }
594 deriving Show
595{-!
596deriving instance Binary TypeInfo
597!-}
599-- Syntactic sugar info
601data DSL' t = DSL { dsl_bind :: t,
602 dsl_return :: t,
603 dsl_apply :: t,
604 dsl_pure :: t,
605 dsl_var :: Maybe t,
606 index_first :: Maybe t,
607 index_next :: Maybe t,
608 dsl_lambda :: Maybe t,
609 dsl_let :: Maybe t
610 }
611 deriving (Show, Functor)
612{-!
613deriving instance Binary DSL'
614!-}
616type DSL = DSL' PTerm
618data SynContext = PatternSyntax | TermSyntax | AnySyntax
619 deriving Show
620{-!
621deriving instance Binary SynContext
622!-}
624data Syntax = Rule [SSymbol] PTerm SynContext
625 deriving Show
626{-!
627deriving instance Binary Syntax
628!-}
630data SSymbol = Keyword Name
631 | Symbol String
632 | Binding Name
633 | Expr Name
634 | SimpleExpr Name
635 deriving Show
636{-!
637deriving instance Binary SSymbol
638!-}
640initDSL = DSL (PRef f (UN ">>="))
641 (PRef f (UN "return"))
642 (PRef f (UN "<$>"))
643 (PRef f (UN "pure"))
644 Nothing
645 Nothing
646 Nothing
647 Nothing
648 Nothing
649 where f = FC "(builtin)" 0
651data SyntaxInfo = Syn { using :: [(Name, PTerm)],
652 syn_params :: [(Name, PTerm)],
653 syn_namespace :: [String],
654 no_imp :: [Name],
655 decoration :: Name -> Name,
656 inPattern :: Bool,
657 implicitAllowed :: Bool,
658 dsl_info :: DSL }
659 deriving Show
660{-!
661deriving instance Binary SyntaxInfo
662!-}
664defaultSyntax = Syn [] [] [] [] id False False initDSL
666expandNS :: SyntaxInfo -> Name -> Name
667expandNS syn n@(NS _ _) = n
668expandNS syn n = case syn_namespace syn of
669 [] -> n
670 xs -> NS n xs
673--- Pretty printing declarations and terms
675instance Show PTerm where
676 show tm = showImp False tm
678instance Pretty PTerm where
679 pretty = prettyImp False
681instance Show PDecl where
682 show d = showDeclImp False d
684instance Show PClause where
685 show c = showCImp True c
687instance Show PData where
688 show d = showDImp False d
690showDeclImp _ (PFix _ f ops) = show f ++ " " ++ showSep ", " ops
691showDeclImp t (PTy _ _ _ _ n ty) = show n ++ " : " ++ showImp t ty
692showDeclImp t (PPostulate _ _ _ _ n ty) = show n ++ " : " ++ showImp t ty
693showDeclImp _ (PClauses _ _ n c) = showSep "\n" (map show c)
694showDeclImp _ (PData _ _ _ _ d) = show d
695showDeclImp _ (PParams f ns ps) = "parameters " ++ show ns ++ "\n" ++
696 showSep "\n" (map show ps)
699showCImp :: Bool -> PClause -> String
700showCImp impl (PClause _ n l ws r w)
701 = showImp impl l ++ showWs ws ++ " = " ++ showImp impl r
702 ++ " where " ++ show w
703 where
704 showWs [] = ""
705 showWs (x : xs) = " | " ++ showImp impl x ++ showWs xs
706showCImp impl (PWith _ n l ws r w)
707 = showImp impl l ++ showWs ws ++ " with " ++ showImp impl r
708 ++ " { " ++ show w ++ " } "
709 where
710 showWs [] = ""
711 showWs (x : xs) = " | " ++ showImp impl x ++ showWs xs
714showDImp :: Bool -> PData -> String
715showDImp impl (PDatadecl n ty cons)
716 = "data " ++ show n ++ " : " ++ showImp impl ty ++ " where\n\t"
717 ++ showSep "\n\t| "
718 (map (\ (_, n, t, _) -> show n ++ " : " ++ showImp impl t) cons)
720getImps :: [PArg] -> [(Name, PTerm)]
721getImps [] = []
722getImps (PImp _ _ n tm _ : xs) = (n, tm) : getImps xs
723getImps (_ : xs) = getImps xs
725getExps :: [PArg] -> [PTerm]
726getExps [] = []
727getExps (PExp _ _ tm _ : xs) = tm : getExps xs
728getExps (_ : xs) = getExps xs
730getConsts :: [PArg] -> [PTerm]
731getConsts [] = []
732getConsts (PConstraint _ _ tm _ : xs) = tm : getConsts xs
733getConsts (_ : xs) = getConsts xs
735getAll :: [PArg] -> [PTerm]
736getAll = map getTm
738prettyImp :: Bool -> PTerm -> Doc
739prettyImp impl = prettySe 10
740 where
741 prettySe p (PQuote r) =
742 if size r > breakingSize then
743 text "![" $$ pretty r <> text "]"
744 else
745 text "![" <> pretty r <> text "]"
746 prettySe p (PPatvar fc n) = pretty n
747 prettySe p (PRef fc n) =
748 if impl then
749 pretty n
750 else
751 prettyBasic n
752 where
753 prettyBasic n@(UN _) = pretty n
754 prettyBasic (MN _ s) = text s
755 prettyBasic (NS n s) = (foldr (<>) empty (intersperse (text ".") (map text $ reverse s))) <> prettyBasic n
756 prettySe p (PLam n ty sc) =
757 bracket p 2 $
758 if size sc > breakingSize then
759 text "λ" <> pretty n <+> text "=>" $+$ pretty sc
760 else
761 text "λ" <> pretty n <+> text "=>" <+> pretty sc
762 prettySe p (PLet n ty v sc) =
763 bracket p 2 $
764 if size sc > breakingSize then
765 text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" $+$
766 nest nestingSize (prettySe 10 sc)
767 else
768 text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" <+>
769 prettySe 10 sc
770 prettySe p (PPi (Exp l s _) n ty sc)
771 | n `elem` allNamesIn sc || impl =
772 let open = if l then text "|" <> lparen else lparen in
773 bracket p 2 $
774 if size sc > breakingSize then
775 open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
776 st <+> text "->" $+$ prettySe 10 sc
777 else
778 open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
779 st <+> text "->" <+> prettySe 10 sc
780 | otherwise =
781 bracket p 2 $
782 if size sc > breakingSize then
783 prettySe 0 ty <+> st <+> text "->" $+$ prettySe 10 sc
784 else
785 prettySe 0 ty <+> st <+> text "->" <+> prettySe 10 sc
786 where
787 st =
788 case s of
789 Static -> text "[static]"
790 _ -> empty
791 prettySe p (PPi (Imp l s _) n ty sc)
792 | impl =
793 let open = if l then text "|" <> lbrace else lbrace in
794 bracket p 2 $
795 if size sc > breakingSize then
796 open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
797 st <+> text "->" <+> prettySe 10 sc
798 else
799 open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
800 st <+> text "->" <+> prettySe 10 sc
801 | otherwise = prettySe 10 sc
802 where
803 st =
804 case s of
805 Static -> text $ "[static]"
806 _ -> empty
807 prettySe p (PPi (Constraint _ _ _) n ty sc) =
808 bracket p 2 $
809 if size sc > breakingSize then
810 prettySe 10 ty <+> text "=>" <+> prettySe 10 sc
811 else
812 prettySe 10 ty <+> text "=>" $+$ prettySe 10 sc
813 prettySe p (PPi (TacImp _ _ s _) n ty sc) =
814 bracket p 2 $
815 if size sc > breakingSize then
816 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
817 rbrace <+> text "->" $+$ prettySe 10 sc
818 else
819 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
820 rbrace <+> text "->" <+> prettySe 10 sc
821 prettySe p (PApp _ (PRef _ f) [])
822 | not impl = pretty f
823 prettySe p (PApp _ (PRef _ op@(UN (f:_))) args)
824 | length (getExps args) == 2 && (not impl) && (not $ isAlpha f) =
825 let [l, r] = getExps args in
826 bracket p 1 $
827 if size r > breakingSize then
828 prettySe 1 l <+> pretty op $+$ prettySe 0 r
829 else
830 prettySe 1 l <+> pretty op <+> prettySe 0 r
831 prettySe p (PApp _ f as) =
832 let args = getExps as in
833 bracket p 1 $
834 prettySe 1 f <+>
835 if impl then
836 foldl fS empty as
837 -- foldr (<+>) empty $ map prettyArgS as
838 else
839 foldl fSe empty args
840 -- foldr (<+>) empty $ map prettyArgSe args
841 where
842 fS l r =
843 if size r > breakingSize then
844 l $+$ nest nestingSize (prettyArgS r)
845 else
846 l <+> prettyArgS r
848 fSe l r =
849 if size r > breakingSize then
850 l $+$ nest nestingSize (prettyArgSe r)
851 else
852 l <+> prettyArgSe r
853 prettySe p (PCase _ scr opts) =
854 text "case" <+> prettySe 10 scr <+> text "of" $+$ nest nestingSize prettyBody
855 where
856 prettyBody = foldr ($$) empty $ intersperse (text "|") $ map sc opts
858 sc (l, r) = prettySe 10 l <+> text "=>" <+> prettySe 10 r
859 prettySe p (PHidden tm) = text "." <> prettySe 0 tm
860 prettySe p (PRefl _ _) = text "refl"
861 prettySe p (PResolveTC _) = text "resolvetc"
862 prettySe p (PTrue _) = text "()"
863 prettySe p (PFalse _) = text "_|_"
864 prettySe p (PEq _ l r) =
865 bracket p 2 $
866 if size r > breakingSize then
867 prettySe 10 l <+> text "=" $$ nest nestingSize (prettySe 10 r)
868 else
869 prettySe 10 l <+> text "=" <+> prettySe 10 r
870 prettySe p (PTyped l r) =
871 lparen <> prettySe 10 l <+> colon <+> prettySe 10 r <> rparen
872 prettySe p (PPair _ l r) =
873 if size r > breakingSize then
874 lparen <> prettySe 10 l <> text "," $+$
875 prettySe 10 r <> rparen
876 else
877 lparen <> prettySe 10 l <> text "," <+> prettySe 10 r <> rparen
878 prettySe p (PDPair _ l t r) =
879 if size r > breakingSize then
880 lparen <> prettySe 10 l <+> text "**" $+$
881 prettySe 10 r <> rparen
882 else
883 lparen <> prettySe 10 l <+> text "**" <+> prettySe 10 r <> rparen
884 prettySe p (PAlternative a as) =
885 lparen <> text "|" <> prettyAs <> text "|" <> rparen
886 where
887 prettyAs =
888 foldr (\l -> \r -> l <+> text "," <+> r) empty $ map (prettySe 10) as
889 prettySe p PType = text "Type"
890 prettySe p (PConstant c) = pretty c
891 -- XXX: add pretty for tactics
892 prettySe p (PProof ts) =
893 text "proof" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
894 prettySe p (PTactics ts) =
895 text "tactics" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
896 prettySe p (PMetavar n) = text "?" <> pretty n
897 prettySe p (PReturn f) = text "return"
898 prettySe p PImpossible = text "impossible"
899 prettySe p Placeholder = text "_"
900 prettySe p (PDoBlock _) = text "do block pretty not implemented"
901 prettySe p (PElabError s) = pretty s
903 prettySe p _ = text "test"
905 prettyArgS (PImp _ _ n tm _) = prettyArgSi (n, tm)
906 prettyArgS (PExp _ _ tm _) = prettyArgSe tm
907 prettyArgS (PConstraint _ _ tm _) = prettyArgSc tm
908 prettyArgS (PTacImplicit _ _ n _ tm _) = prettyArgSti (n, tm)
910 prettyArgSe arg = prettySe 0 arg
911 prettyArgSi (n, val) = lbrace <> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
912 prettyArgSc val = lbrace <> lbrace <> prettySe 10 val <> rbrace <> rbrace
913 prettyArgSti (n, val) = lbrace <> text "auto" <+> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
915 bracket outer inner doc
916 | inner > outer = lparen <> doc <> rparen
917 | otherwise = doc
919showImp :: Bool -> PTerm -> String
920showImp impl tm = se 10 tm where
921 se p (PQuote r) = "![" ++ show r ++ "]"
922 se p (PPatvar fc n) = show n
923 se p (PInferRef fc n) = "!" ++ show n -- ++ "[" ++ show fc ++ "]"
924 se p (PRef fc n) = if impl then show n -- ++ "[" ++ show fc ++ "]"
925 else showbasic n
926 where showbasic n@(UN _) = show n
927 showbasic (MN _ s) = s
928 showbasic (NS n s) = showSep "." (reverse s) ++ "." ++ showbasic n
929 se p (PLam n ty sc) = bracket p 2 $ "\\ " ++ show n ++
930 (if impl then " : " ++ se 10 ty else "") ++ " => "
931 ++ se 10 sc
932 se p (PLet n ty v sc) = bracket p 2 $ "let " ++ show n ++ " = " ++ se 10 v ++
933 " in " ++ se 10 sc
934 se p (PPi (Exp l s _) n ty sc)
935 | n `elem` allNamesIn sc || impl
936 = bracket p 2 $
937 (if l then "|(" else "(") ++
938 show n ++ " : " ++ se 10 ty ++
939 ") " ++ st ++
940 "-> " ++ se 10 sc
941 | otherwise = bracket p 2 $ se 0 ty ++ " " ++ st ++ "-> " ++ se 10 sc
942 where st = case s of
943 Static -> "[static] "
944 _ -> ""
945 se p (PPi (Imp l s _) n ty sc)
946 | impl = bracket p 2 $ (if l then "|{" else "{") ++
947 show n ++ " : " ++ se 10 ty ++
948 "} " ++ st ++ "-> " ++ se 10 sc
949 | otherwise = se 10 sc
950 where st = case s of
951 Static -> "[static] "
952 _ -> ""
953 se p (PPi (Constraint _ _ _) n ty sc)
954 = bracket p 2 $ se 10 ty ++ " => " ++ se 10 sc
955 se p (PPi (TacImp _ _ s _) n ty sc)
956 = bracket p 2 $ "{tacimp " ++ show n ++ " : " ++ se 10 ty ++ "} -> " ++ se 10 sc
957 se p (PApp _ (PRef _ f) [])
958 | not impl = show f
959 se p (PApp _ (PRef _ op@(UN (f:_))) args)
960 | length (getExps args) == 2 && not impl && not (isAlpha f)
961 = let [l, r] = getExps args in
962 bracket p 1 $ se 1 l ++ " " ++ show op ++ " " ++ se 0 r
963 se p (PApp _ f as)
964 = let args = getExps as in
965 bracket p 1 $ se 1 f ++ if impl then concatMap sArg as
966 else concatMap seArg args
967 se p (PCase _ scr opts) = "case " ++ se 10 scr ++ " of " ++ showSep " | " (map sc opts)
968 where sc (l, r) = se 10 l ++ " => " ++ se 10 r
969 se p (PHidden tm) = "." ++ se 0 tm
970 se p (PRefl _ t)
971 | not impl = "refl"
972 | otherwise = "refl {" ++ se 10 t ++ "}"
973 se p (PResolveTC _) = "resolvetc"
974 se p (PTrue _) = "()"
975 se p (PFalse _) = "_|_"
976 se p (PEq _ l r) = bracket p 2 $ se 10 l ++ " = " ++ se 10 r
977 se p (PTyped l r) = "(" ++ se 10 l ++ " : " ++ se 10 r ++ ")"
978 se p (PPair _ l r) = "(" ++ se 10 l ++ ", " ++ se 10 r ++ ")"
979 se p (PDPair _ l t r) = "(" ++ se 10 l ++ " ** " ++ se 10 r ++ ")"
980 se p (PAlternative a as) = "(|" ++ showSep " , " (map (se 10) as) ++ "|)"
981 se p PType = "Type"
982 se p (PConstant c) = show c
983 se p (PProof ts) = "proof { " ++ show ts ++ "}"
984 se p (PTactics ts) = "tactics { " ++ show ts ++ "}"
985 se p (PMetavar n) = "?" ++ show n
986 se p (PReturn f) = "return"
987 se p PImpossible = "impossible"
988 se p Placeholder = "_"
989 se p (PDoBlock _) = "do block show not implemented"
990 se p (PElabError s) = show s
991 se p (PCoerced t) = se p t
992-- se p x = "Not implemented"
994 sArg (PImp _ _ n tm _) = siArg (n, tm)
995 sArg (PExp _ _ tm _) = seArg tm
996 sArg (PConstraint _ _ tm _) = scArg tm
997 sArg (PTacImplicit _ _ n _ tm _) = stiArg (n, tm)
999 seArg arg = " " ++ se 0 arg
1000 siArg (n, val) = " {" ++ show n ++ " = " ++ se 10 val ++ "}"
1001 scArg val = " {{" ++ se 10 val ++ "}}"
1002 stiArg (n, val) = " {auto " ++ show n ++ " = " ++ se 10 val ++ "}"
1004 bracket outer inner str | inner > outer = "(" ++ str ++ ")"
1005 | otherwise = str
1008instance Sized PTerm where
1009 size (PQuote rawTerm) = size rawTerm
1010 size (PRef fc name) = size name
1011 size (PLam name ty bdy) = 1 + size ty + size bdy
1012 size (PPi plicity name ty bdy) = 1 + size ty + size bdy
1013 size (PLet name ty def bdy) = 1 + size ty + size def + size bdy
1014 size (PTyped trm ty) = 1 + size trm + size ty
1015 size (PApp fc name args) = 1 + size args
1016 size (PCase fc trm bdy) = 1 + size trm + size bdy
1017 size (PTrue fc) = 1
1018 size (PFalse fc) = 1
1019 size (PRefl fc _) = 1
1020 size (PResolveTC fc) = 1
1021 size (PEq fc left right) = 1 + size left + size right
1022 size (PPair fc left right) = 1 + size left + size right
1023 size (PDPair fs left ty right) = 1 + size left + size ty + size right
1024 size (PAlternative a alts) = 1 + size alts
1025 size (PHidden hidden) = size hidden
1026 size PType = 1
1027 size (PConstant const) = 1 + size const
1028 size Placeholder = 1
1029 size (PDoBlock dos) = 1 + size dos
1030 size (PIdiom fc term) = 1 + size term
1031 size (PReturn fc) = 1
1032 size (PMetavar name) = 1
1033 size (PProof tactics) = size tactics
1034 size (PElabError err) = size err
1035 size PImpossible = 1
1037getPArity :: PTerm -> Int
1038getPArity (PPi _ _ _ sc) = 1 + getPArity sc
1039getPArity _ = 0
1041-- Return all names, free or globally bound, in the given term.
1043allNamesIn :: PTerm -> [Name]
1044allNamesIn tm = nub $ ni [] tm
1045 where
1046 ni env (PRef _ n)
1047 | not (n `elem` env) = [n]
1048 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1049 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1050 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1051 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1052 ni env (PHidden tm) = ni env tm
1053 ni env (PEq _ l r) = ni env l ++ ni env r
1054 ni env (PTyped l r) = ni env l ++ ni env r
1055 ni env (PPair _ l r) = ni env l ++ ni env r
1056 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1057 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1058 ni env (PAlternative a ls) = concatMap (ni env) ls
1059 ni env _ = []
1061-- Return names which are free in the given term.
1063namesIn :: [(Name, PTerm)] -> IState -> PTerm -> [Name]
1064namesIn uvars ist tm = nub $ ni [] tm
1065 where
1066 ni env (PRef _ n)
1067 | not (n `elem` env)
1068 = case lookupTy Nothing n (tt_ctxt ist) of
1069 [] -> [n]
1070 _ -> if n `elem` (map fst uvars) then [n] else []
1071 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1072 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1073 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1074 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1075 ni env (PEq _ l r) = ni env l ++ ni env r
1076 ni env (PTyped l r) = ni env l ++ ni env r
1077 ni env (PPair _ l r) = ni env l ++ ni env r
1078 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1079 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1080 ni env (PAlternative a as) = concatMap (ni env) as
1081 ni env (PHidden tm) = ni env tm
1082 ni env _ = []
1084-- Return which of the given names are used in the given term.
1086usedNamesIn :: [Name] -> IState -> PTerm -> [Name]
1087usedNamesIn vars ist tm = nub $ ni [] tm
1088 where
1089 ni env (PRef _ n)
1090 | n `elem` vars && not (n `elem` env)
1091 = case lookupTy Nothing n (tt_ctxt ist) of
1092 [] -> [n]
1093 _ -> []
1094 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1095 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1096 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1097 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1098 ni env (PEq _ l r) = ni env l ++ ni env r
1099 ni env (PTyped l r) = ni env l ++ ni env r
1100 ni env (PPair _ l r) = ni env l ++ ni env r
1101 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1102 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1103 ni env (PAlternative a as) = concatMap (ni env) as
1104 ni env (PHidden tm) = ni env tm
1105 ni env _ = []