module Idris.AbsSyntaxTree where
import Idris.Core.TT
import Idris.Core.Evaluate
import Idris.Core.Elaborate hiding (Tactic(..))
import Idris.Core.Typecheck
import Idris.Docstrings
import IRTS.Lang
import IRTS.CodegenCommon
import Util.Pretty
import Util.DynamicLinker
import Idris.Colours
import System.Console.Haskeline
import System.IO
import Prelude hiding ((<$>))
import Control.Applicative ((<|>))
import Control.Monad.Trans.State.Strict
import Control.Monad.Trans.Except
import qualified Control.Monad.Trans.Class as Trans (lift)
import Data.Data (Data)
import Data.Function (on)
import Data.Generics.Uniplate.Data (universe, children)
import Data.List hiding (group)
import Data.Char
import qualified Data.Map.Strict as M
import qualified Data.Text as T
import Data.Either
import qualified Data.Set as S
import Data.Word (Word)
import Data.Maybe (fromMaybe, mapMaybe, maybeToList)
import Data.Traversable (Traversable)
import Data.Typeable
import Data.Foldable (Foldable)
import Debug.Trace
import Text.PrettyPrint.Annotated.Leijen
data ElabWhat = ETypes | EDefns | EAll
deriving (Show, Eq)
data ElabInfo = EInfo { params :: [(Name, PTerm)],
inblock :: Ctxt [Name],
liftname :: Name -> Name,
namespace :: Maybe [String],
elabFC :: Maybe FC,
rec_elabDecl :: ElabWhat -> ElabInfo -> PDecl ->
Idris () }
toplevel :: ElabInfo
toplevel = EInfo [] emptyContext id Nothing Nothing (\_ _ _ -> fail "Not implemented")
eInfoNames :: ElabInfo -> [Name]
eInfoNames info = map fst (params info) ++ M.keys (inblock info)
data IOption = IOption {
opt_logLevel :: Int
, opt_logcats :: [LogCat]
, opt_typecase :: Bool
, opt_typeintype :: Bool
, opt_coverage :: Bool
, opt_showimp :: Bool
, opt_errContext :: Bool
, opt_repl :: Bool
, opt_verbose :: Bool
, opt_nobanner :: Bool
, opt_quiet :: Bool
, opt_codegen :: Codegen
, opt_outputTy :: OutputType
, opt_ibcsubdir :: FilePath
, opt_importdirs :: [FilePath]
, opt_triple :: String
, opt_cpu :: String
, opt_cmdline :: [Opt]
, opt_origerr :: Bool
, opt_autoSolve :: Bool
, opt_autoImport :: [FilePath]
, opt_optimise :: [Optimisation]
, opt_printdepth :: Maybe Int
, opt_evaltypes :: Bool
, opt_desugarnats :: Bool
, opt_autoimpls :: Bool
} deriving (Show, Eq)
defaultOpts = IOption { opt_logLevel = 0
, opt_logcats = []
, opt_typecase = False
, opt_typeintype = False
, opt_coverage = True
, opt_showimp = False
, opt_errContext = False
, opt_repl = True
, opt_verbose = True
, opt_nobanner = False
, opt_quiet = False
, opt_codegen = Via "c"
, opt_outputTy = Executable
, opt_ibcsubdir = ""
, opt_importdirs = []
, opt_triple = ""
, opt_cpu = ""
, opt_cmdline = []
, opt_origerr = False
, opt_autoSolve = True
, opt_autoImport = []
, opt_optimise = defaultOptimise
, opt_printdepth = Just 5000
, opt_evaltypes = True
, opt_desugarnats = False
, opt_autoimpls = True
}
data PPOption = PPOption {
ppopt_impl :: Bool
, ppopt_desugarnats :: Bool
, ppopt_pinames :: Bool
, ppopt_depth :: Maybe Int
} deriving (Show)
data Optimisation = PETransform
deriving (Show, Eq)
defaultOptimise = [PETransform]
defaultPPOption :: PPOption
defaultPPOption = PPOption { ppopt_impl = False,
ppopt_desugarnats = False,
ppopt_pinames = False,
ppopt_depth = Just 200 }
verbosePPOption :: PPOption
verbosePPOption = PPOption { ppopt_impl = True,
ppopt_desugarnats = True,
ppopt_pinames = True,
ppopt_depth = Just 200 }
ppOption :: IOption -> PPOption
ppOption opt = PPOption {
ppopt_impl = opt_showimp opt,
ppopt_pinames = False,
ppopt_depth = opt_printdepth opt,
ppopt_desugarnats = opt_desugarnats opt
}
ppOptionIst :: IState -> PPOption
ppOptionIst = ppOption . idris_options
data LanguageExt = TypeProviders | ErrorReflection deriving (Show, Eq, Read, Ord)
data OutputMode = RawOutput Handle
| IdeMode Integer Handle
deriving Show
data ConsoleWidth = InfinitelyWide
| ColsWide Int
| AutomaticWidth
deriving (Show, Eq)
data IState = IState {
tt_ctxt :: Context,
idris_constraints :: S.Set ConstraintFC,
idris_infixes :: [FixDecl],
idris_implicits :: Ctxt [PArg],
idris_statics :: Ctxt [Bool],
idris_classes :: Ctxt ClassInfo,
idris_records :: Ctxt RecordInfo,
idris_dsls :: Ctxt DSL,
idris_optimisation :: Ctxt OptInfo,
idris_datatypes :: Ctxt TypeInfo,
idris_namehints :: Ctxt [Name],
idris_patdefs :: Ctxt ([([(Name, Term)], Term, Term)], [PTerm]),
idris_flags :: Ctxt [FnOpt],
idris_callgraph :: Ctxt CGInfo,
idris_docstrings :: Ctxt (Docstring DocTerm, [(Name, Docstring DocTerm)]),
idris_moduledocs :: Ctxt (Docstring DocTerm),
idris_tyinfodata :: Ctxt TIData,
idris_fninfo :: Ctxt FnInfo,
idris_transforms :: Ctxt [(Term, Term)],
idris_autohints :: Ctxt [Name],
idris_totcheck :: [(FC, Name)],
idris_defertotcheck :: [(FC, Name)],
idris_totcheckfail :: [(FC, String)],
idris_options :: IOption,
idris_name :: Int,
idris_lineapps :: [((FilePath, Int), PTerm)],
idris_metavars :: [(Name, (Maybe Name, Int, [Name], Bool, Bool))],
idris_coercions :: [Name],
idris_errRev :: [(Term, Term)],
syntax_rules :: SyntaxRules,
syntax_keywords :: [String],
imported :: [FilePath],
idris_scprims :: [(Name, (Int, PrimFn))],
idris_objs :: [(Codegen, FilePath)],
idris_libs :: [(Codegen, String)],
idris_cgflags :: [(Codegen, String)],
idris_hdrs :: [(Codegen, String)],
idris_imported :: [(FilePath, Bool)],
proof_list :: [(Name, (Bool, [String]))],
errSpan :: Maybe FC,
parserWarnings :: [(FC, Err)],
lastParse :: Maybe Name,
indent_stack :: [Int],
brace_stack :: [Maybe Int],
lastTokenSpan :: Maybe FC,
idris_parsedSpan :: Maybe FC,
hide_list :: Ctxt Accessibility,
default_access :: Accessibility,
default_total :: Bool,
ibc_write :: [IBCWrite],
compiled_so :: Maybe String,
idris_dynamic_libs :: [DynamicLib],
idris_language_extensions :: [LanguageExt],
idris_outputmode :: OutputMode,
idris_colourRepl :: Bool,
idris_colourTheme :: ColourTheme,
idris_errorhandlers :: [Name],
idris_nameIdx :: (Int, Ctxt (Int, Name)),
idris_function_errorhandlers :: Ctxt (M.Map Name (S.Set Name)),
module_aliases :: M.Map [T.Text] [T.Text],
idris_consolewidth :: ConsoleWidth,
idris_postulates :: S.Set Name,
idris_externs :: S.Set (Name, Int),
idris_erasureUsed :: [(Name, Int)],
idris_whocalls :: Maybe (M.Map Name [Name]),
idris_callswho :: Maybe (M.Map Name [Name]),
idris_repl_defs :: [Name],
elab_stack :: [(Name, Bool)],
idris_symbols :: M.Map Name Name,
idris_exports :: [Name],
idris_highlightedRegions :: [(FC, OutputAnnotation)],
idris_parserHighlights :: [(FC, OutputAnnotation)],
idris_deprecated :: Ctxt String,
idris_inmodule :: S.Set Name,
idris_ttstats :: M.Map Term (Int, Term)
}
instance Show IState where
show = const "{internal state}"
data SizeChange = Smaller | Same | Bigger | Unknown
deriving (Show, Eq)
type SCGEntry = (Name, [Maybe (Int, SizeChange)])
type UsageReason = (Name, Int)
data CGInfo = CGInfo { calls :: [Name],
scg :: [SCGEntry],
usedpos :: [(Int, [UsageReason])]
}
deriving Show
primDefs = [sUN "unsafePerformPrimIO",
sUN "mkLazyForeignPrim",
sUN "mkForeignPrim",
sUN "void"]
data IBCWrite = IBCFix FixDecl
| IBCImp Name
| IBCStatic Name
| IBCClass Name
| IBCRecord Name
| IBCInstance Bool Bool Name Name
| IBCDSL Name
| IBCData Name
| IBCOpt Name
| IBCMetavar Name
| IBCSyntax Syntax
| IBCKeyword String
| IBCImport (Bool, FilePath)
| IBCImportDir FilePath
| IBCObj Codegen FilePath
| IBCLib Codegen String
| IBCCGFlag Codegen String
| IBCDyLib String
| IBCHeader Codegen String
| IBCAccess Name Accessibility
| IBCMetaInformation Name MetaInformation
| IBCTotal Name Totality
| IBCFlags Name [FnOpt]
| IBCFnInfo Name FnInfo
| IBCTrans Name (Term, Term)
| IBCErrRev (Term, Term)
| IBCCG Name
| IBCDoc Name
| IBCCoercion Name
| IBCDef Name
| IBCNameHint (Name, Name)
| IBCLineApp FilePath Int PTerm
| IBCErrorHandler Name
| IBCFunctionErrorHandler Name Name Name
| IBCPostulate Name
| IBCExtern (Name, Int)
| IBCTotCheckErr FC String
| IBCParsedRegion FC
| IBCModDocs Name
| IBCUsage (Name, Int)
| IBCExport Name
| IBCAutoHint Name Name
| IBCDeprecate Name String
deriving Show
idrisInit :: IState
idrisInit = IState initContext S.empty []
emptyContext emptyContext emptyContext emptyContext
emptyContext emptyContext emptyContext emptyContext
emptyContext emptyContext emptyContext emptyContext
emptyContext emptyContext emptyContext emptyContext
emptyContext
[] [] [] defaultOpts 6 [] [] [] [] emptySyntaxRules [] [] [] [] [] [] []
[] [] Nothing [] Nothing [] [] Nothing Nothing emptyContext Private False [] Nothing [] []
(RawOutput stdout) True defaultTheme [] (0, emptyContext) emptyContext M.empty
AutomaticWidth S.empty S.empty [] Nothing Nothing [] [] M.empty [] [] []
emptyContext S.empty M.empty
type Idris = StateT IState (ExceptT Err IO)
catchError :: Idris a -> (Err -> Idris a) -> Idris a
catchError = liftCatch catchE
throwError :: Err -> Idris a
throwError = Trans.lift . throwE
data Codegen = Via String
| Bytecode
deriving (Show, Eq)
data HowMuchDocs = FullDocs | OverviewDocs
data Command = Quit
| Help
| Eval PTerm
| NewDefn [PDecl]
| Undefine [Name]
| Check PTerm
| Core PTerm
| DocStr (Either Name Const) HowMuchDocs
| TotCheck Name
| Reload
| Watch
| Load FilePath (Maybe Int)
| ChangeDirectory FilePath
| ModImport String
| Edit
| Compile Codegen String
| Execute PTerm
| ExecVal PTerm
| Metavars
| Prove Bool Name
| AddProof (Maybe Name)
| RmProof Name
| ShowProof Name
| Proofs
| Universes
| LogLvl Int
| LogCategory [LogCat]
| Spec PTerm
| WHNF PTerm
| TestInline PTerm
| Defn Name
| Missing Name
| DynamicLink FilePath
| ListDynamic
| Pattelab PTerm
| Search [String] PTerm
| CaseSplitAt Bool Int Name
| AddClauseFrom Bool Int Name
| AddProofClauseFrom Bool Int Name
| AddMissing Bool Int Name
| MakeWith Bool Int Name
| MakeCase Bool Int Name
| MakeLemma Bool Int Name
| DoProofSearch Bool
Bool
Int
Name
[Name]
| SetOpt Opt
| UnsetOpt Opt
| NOP
| SetColour ColourType IdrisColour
| ColourOn
| ColourOff
| ListErrorHandlers
| SetConsoleWidth ConsoleWidth
| SetPrinterDepth (Maybe Int)
| Apropos [String] String
| WhoCalls Name
| CallsWho Name
| Browse [String]
| MakeDoc String
| Warranty
| PrintDef Name
| PPrint OutputFmt Int PTerm
| TransformInfo Name
| DebugInfo Name
| DebugUnify PTerm PTerm
data OutputFmt = HTMLOutput | LaTeXOutput
data LogCat = IParse
| IElab
| ICodeGen
| IErasure
| ICoverage
| IIBC
deriving (Show, Eq, Ord)
strLogCat :: LogCat -> String
strLogCat IParse = "parser"
strLogCat IElab = "elab"
strLogCat ICodeGen = "codegen"
strLogCat IErasure = "erasure"
strLogCat ICoverage = "coverage"
strLogCat IIBC = "ibc"
codegenCats :: [LogCat]
codegenCats = [ICodeGen]
parserCats :: [LogCat]
parserCats = [IParse]
elabCats :: [LogCat]
elabCats = [IElab]
loggingCatsStr :: String
loggingCatsStr = unlines
[ (strLogCat IParse)
, (strLogCat IElab)
, (strLogCat ICodeGen)
, (strLogCat IErasure)
, (strLogCat ICoverage)
, (strLogCat IIBC)
]
data Opt = Filename String
| Quiet
| NoBanner
| ColourREPL Bool
| Idemode
| IdemodeSocket
| ShowLibs
| ShowLibdir
| ShowIncs
| ShowPkgs
| ShowLoggingCats
| NoBasePkgs
| NoPrelude
| NoBuiltins
| NoREPL
| OLogging Int
| OLogCats [LogCat]
| Output String
| Interface
| TypeCase
| TypeInType
| DefaultTotal
| DefaultPartial
| WarnPartial
| WarnReach
| EvalTypes
| NoCoverage
| ErrContext
| ShowImpl
| Verbose
| Port String
| IBCSubDir String
| ImportDir String
| PkgBuild String
| PkgInstall String
| PkgClean String
| PkgCheck String
| PkgREPL String
| PkgMkDoc String
| PkgTest String
| PkgIndex FilePath
| WarnOnly
| Pkg String
| BCAsm String
| DumpDefun String
| DumpCases String
| UseCodegen Codegen
| CodegenArgs String
| OutputTy OutputType
| Extension LanguageExt
| InterpretScript String
| EvalExpr String
| TargetTriple String
| TargetCPU String
| OptLevel Int
| AddOpt Optimisation
| RemoveOpt Optimisation
| Client String
| ShowOrigErr
| AutoWidth
| AutoSolve
| UseConsoleWidth ConsoleWidth
| DumpHighlights
| DesugarNats
| NoElimDeprecationWarnings
| NoOldTacticDeprecationWarnings
deriving (Show, Eq)
data ElabShellCmd = EQED | EAbandon | EUndo | EProofState | EProofTerm
| EEval PTerm | ECheck PTerm | ESearch PTerm
| EDocStr (Either Name Const)
deriving (Show, Eq)
data Fixity = Infixl { prec :: Int }
| Infixr { prec :: Int }
| InfixN { prec :: Int }
| PrefixN { prec :: Int }
deriving Eq
instance Show Fixity where
show (Infixl i) = "infixl " ++ show i
show (Infixr i) = "infixr " ++ show i
show (InfixN i) = "infix " ++ show i
show (PrefixN i) = "prefix " ++ show i
data FixDecl = Fix Fixity String
deriving Eq
instance Show FixDecl where
show (Fix f s) = show f ++ " " ++ s
instance Ord FixDecl where
compare (Fix x _) (Fix y _) = compare (prec x) (prec y)
data Static = Static | Dynamic
deriving (Show, Eq, Data, Typeable)
data Plicity = Imp { pargopts :: [ArgOpt],
pstatic :: Static,
pparam :: Bool,
pscoped :: Maybe ImplicitInfo
}
| Exp { pargopts :: [ArgOpt],
pstatic :: Static,
pparam :: Bool }
| Constraint { pargopts :: [ArgOpt],
pstatic :: Static }
| TacImp { pargopts :: [ArgOpt],
pstatic :: Static,
pscript :: PTerm }
deriving (Show, Eq, Data, Typeable)
is_scoped :: Plicity -> Maybe ImplicitInfo
is_scoped (Imp _ _ _ s) = s
is_scoped _ = Nothing
impl = Imp [] Dynamic False (Just (Impl False True))
forall_imp = Imp [] Dynamic False (Just (Impl False False))
forall_constraint = Imp [] Dynamic False (Just (Impl True False))
expl = Exp [] Dynamic False
expl_param = Exp [] Dynamic True
constraint = Constraint [] Static
tacimpl t = TacImp [] Dynamic t
data FnOpt = Inlinable
| TotalFn | PartialFn | CoveringFn
| Coinductive | AssertTotal
| Dictionary
| Implicit
| NoImplicit
| CExport String
| ErrorHandler
| ErrorReverse
| Reflection
| Specialise [(Name, Maybe Int)]
| Constructor
| AutoHint
| PEGenerated
deriving (Show, Eq)
type FnOpts = [FnOpt]
inlinable :: FnOpts -> Bool
inlinable = elem Inlinable
dictionary :: FnOpts -> Bool
dictionary = elem Dictionary
data ProvideWhat' t = ProvTerm t t
| ProvPostulate t
deriving (Show, Eq, Functor)
type ProvideWhat = ProvideWhat' PTerm
data PDecl' t
= PFix FC Fixity [String]
| PTy (Docstring (Either Err t)) [(Name, Docstring (Either Err t))] SyntaxInfo FC FnOpts Name FC t
| PPostulate Bool
(Docstring (Either Err t)) SyntaxInfo FC FC FnOpts Name t
| PClauses FC FnOpts Name [PClause' t]
| PCAF FC Name t
| PData (Docstring (Either Err t)) [(Name, Docstring (Either Err t))] SyntaxInfo FC DataOpts (PData' t)
| PParams FC [(Name, t)] [PDecl' t]
| PNamespace String FC [PDecl' t]
| PRecord (Docstring (Either Err t)) SyntaxInfo FC DataOpts
Name
FC
[(Name, FC, Plicity, t)]
[(Name, Docstring (Either Err t))]
[(Maybe (Name, FC), Plicity, t, Maybe (Docstring (Either Err t)))]
(Maybe (Name, FC))
(Docstring (Either Err t))
SyntaxInfo
| PClass (Docstring (Either Err t)) SyntaxInfo FC
[(Name, t)]
Name
FC
[(Name, FC, t)]
[(Name, Docstring (Either Err t))]
[(Name, FC)]
[PDecl' t]
(Maybe (Name, FC))
(Docstring (Either Err t))
| PInstance
(Docstring (Either Err t))
[(Name, Docstring (Either Err t))]
SyntaxInfo
FC [(Name, t)]
Accessibility
FnOpts
Name
FC
[t]
t
(Maybe Name)
[PDecl' t]
| PDSL Name (DSL' t)
| PSyntax FC Syntax
| PMutual FC [PDecl' t]
| PDirective Directive
| PProvider (Docstring (Either Err t)) SyntaxInfo FC FC (ProvideWhat' t) Name
| PTransform FC Bool t t
| PRunElabDecl FC t [String]
deriving Functor
data Directive = DLib Codegen String |
DLink Codegen String |
DFlag Codegen String |
DInclude Codegen String |
DHide Name |
DFreeze Name |
DAccess Accessibility |
DDefault Bool |
DLogging Integer |
DDynamicLibs [String] |
DNameHint Name FC [(Name, FC)] |
DErrorHandlers Name FC Name FC [(Name, FC)] |
DLanguage LanguageExt |
DDeprecate Name String |
DAutoImplicits Bool |
DUsed FC Name Name
data RDeclInstructions = RTyDeclInstrs Name FC [PArg] Type
| RClausesInstrs Name [([(Name, Term)], Term, Term)]
| RAddInstance Name Name
| RDatatypeDeclInstrs Name [PArg]
| RDatatypeDefnInstrs Name Type [(Name, [PArg], Type)]
data EState = EState {
case_decls :: [(Name, PDecl)],
delayed_elab :: [(Int, Elab' EState ())],
new_tyDecls :: [RDeclInstructions],
highlighting :: [(FC, OutputAnnotation)]
}
initEState :: EState
initEState = EState [] [] [] []
type ElabD a = Elab' EState a
highlightSource :: FC -> OutputAnnotation -> ElabD ()
highlightSource fc annot =
updateAux (\aux -> aux { highlighting = (fc, annot) : highlighting aux })
data PClause' t = PClause FC Name t [t] t [PDecl' t]
| PWith FC Name t [t] t (Maybe (Name, FC)) [PDecl' t]
| PClauseR FC [t] t [PDecl' t]
| PWithR FC [t] t (Maybe (Name, FC)) [PDecl' t]
deriving Functor
data PData' t = PDatadecl { d_name :: Name,
d_name_fc :: FC,
d_tcon :: t,
d_cons :: [(Docstring (Either Err PTerm), [(Name, Docstring (Either Err PTerm))], Name, FC, t, FC, [Name])]
}
| PLaterdecl { d_name :: Name, d_name_fc :: FC, d_tcon :: t }
deriving Functor
mapPDataFC :: (FC -> FC) -> (FC -> FC) -> PData -> PData
mapPDataFC f g (PDatadecl n nfc tycon ctors) =
PDatadecl n (g nfc) (mapPTermFC f g tycon) (map ctorFC ctors)
where ctorFC (doc, argDocs, n, nfc, ty, fc, ns) =
(doc, argDocs, n, g nfc, mapPTermFC f g ty, f fc, ns)
mapPDataFC f g (PLaterdecl n nfc tycon) =
PLaterdecl n (g nfc) (mapPTermFC f g tycon)
type PDecl = PDecl' PTerm
type PData = PData' PTerm
type PClause = PClause' PTerm
mapPDeclFC :: (FC -> FC) -> (FC -> FC) -> PDecl -> PDecl
mapPDeclFC f g (PFix fc fixity ops) =
PFix (f fc) fixity ops
mapPDeclFC f g (PTy doc argdocs syn fc opts n nfc ty) =
PTy doc argdocs syn (f fc) opts n (g nfc) (mapPTermFC f g ty)
mapPDeclFC f g (PPostulate ext doc syn fc nfc opts n ty) =
PPostulate ext doc syn (f fc) (g nfc) opts n (mapPTermFC f g ty)
mapPDeclFC f g (PClauses fc opts n clauses) =
PClauses (f fc) opts n (map (fmap (mapPTermFC f g)) clauses)
mapPDeclFC f g (PCAF fc n tm) = PCAF (f fc) n (mapPTermFC f g tm)
mapPDeclFC f g (PData doc argdocs syn fc opts dat) =
PData doc argdocs syn (f fc) opts (mapPDataFC f g dat)
mapPDeclFC f g (PParams fc params decls) =
PParams (f fc)
(map (\(n, ty) -> (n, mapPTermFC f g ty)) params)
(map (mapPDeclFC f g) decls)
mapPDeclFC f g (PNamespace ns fc decls) =
PNamespace ns (f fc) (map (mapPDeclFC f g) decls)
mapPDeclFC f g (PRecord doc syn fc opts n nfc params paramdocs fields ctor ctorDoc syn') =
PRecord doc syn (f fc) opts n (g nfc)
(map (\(pn, pnfc, plic, ty) -> (pn, g pnfc, plic, mapPTermFC f g ty)) params)
paramdocs
(map (\(fn, plic, ty, fdoc) -> (fmap (\(fn', fnfc) -> (fn', g fnfc)) fn,
plic, mapPTermFC f g ty, fdoc))
fields)
(fmap (\(ctorN, ctorNFC) -> (ctorN, g ctorNFC)) ctor)
ctorDoc
syn'
mapPDeclFC f g (PClass doc syn fc constrs n nfc params paramDocs det body ctor ctorDoc) =
PClass doc syn (f fc)
(map (\(constrn, constr) -> (constrn, mapPTermFC f g constr)) constrs)
n (g nfc) (map (\(n, nfc, pty) -> (n, g nfc, mapPTermFC f g pty)) params)
paramDocs (map (\(dn, dnfc) -> (dn, g dnfc)) det)
(map (mapPDeclFC f g) body)
(fmap (\(n, nfc) -> (n, g nfc)) ctor)
ctorDoc
mapPDeclFC f g (PInstance doc paramDocs syn fc constrs cn acc opts cnfc params instTy instN body) =
PInstance doc paramDocs syn (f fc)
(map (\(constrN, constrT) -> (constrN, mapPTermFC f g constrT)) constrs)
cn acc opts (g cnfc) (map (mapPTermFC f g) params)
(mapPTermFC f g instTy)
instN
(map (mapPDeclFC f g) body)
mapPDeclFC f g (PDSL n dsl) = PDSL n (fmap (mapPTermFC f g) dsl)
mapPDeclFC f g (PSyntax fc syn) = PSyntax (f fc) $
case syn of
Rule syms tm ctxt -> Rule syms (mapPTermFC f g tm) ctxt
DeclRule syms decls -> DeclRule syms (map (mapPDeclFC f g) decls)
mapPDeclFC f g (PMutual fc decls) =
PMutual (f fc) (map (mapPDeclFC f g) decls)
mapPDeclFC f g (PDirective d) =
PDirective $
case d of
DNameHint n nfc ns ->
DNameHint n (g nfc) (map (\(hn, hnfc) -> (hn, g hnfc)) ns)
DErrorHandlers n nfc n' nfc' ns ->
DErrorHandlers n (g nfc) n' (g nfc') $
map (\(an, anfc) -> (an, g anfc)) ns
mapPDeclFC f g (PProvider doc syn fc nfc what n) =
PProvider doc syn (f fc) (g nfc) (fmap (mapPTermFC f g) what) n
mapPDeclFC f g (PTransform fc safe l r) =
PTransform (f fc) safe (mapPTermFC f g l) (mapPTermFC f g r)
mapPDeclFC f g (PRunElabDecl fc script ns) =
PRunElabDecl (f fc) (mapPTermFC f g script) ns
declared :: PDecl -> [Name]
declared (PFix _ _ _) = []
declared (PTy _ _ _ _ _ n fc t) = [n]
declared (PPostulate _ _ _ _ _ _ n t) = [n]
declared (PClauses _ _ n _) = []
declared (PCAF _ n _) = [n]
declared (PData _ _ _ _ _ (PDatadecl n _ _ ts)) = n : map fstt ts
where fstt (_, _, a, _, _, _, _) = a
declared (PData _ _ _ _ _ (PLaterdecl n _ _)) = [n]
declared (PParams _ _ ds) = concatMap declared ds
declared (PNamespace _ _ ds) = concatMap declared ds
declared (PRecord _ _ _ _ n _ _ _ _ cn _ _) = n : map fst (maybeToList cn)
declared (PClass _ _ _ _ n _ _ _ _ ms cn cd) = n : (map fst (maybeToList cn) ++ concatMap declared ms)
declared (PInstance _ _ _ _ _ _ _ _ _ _ _ _ _) = []
declared (PDSL n _) = [n]
declared (PSyntax _ _) = []
declared (PMutual _ ds) = concatMap declared ds
declared (PDirective _) = []
declared _ = []
tldeclared :: PDecl -> [Name]
tldeclared (PFix _ _ _) = []
tldeclared (PTy _ _ _ _ _ n _ t) = [n]
tldeclared (PPostulate _ _ _ _ _ _ n t) = [n]
tldeclared (PClauses _ _ n _) = []
tldeclared (PRecord _ _ _ _ n _ _ _ _ cn _ _) = n : map fst (maybeToList cn)
tldeclared (PData _ _ _ _ _ (PDatadecl n _ _ ts)) = n : map fstt ts
where fstt (_, _, a, _, _, _, _) = a
tldeclared (PParams _ _ ds) = []
tldeclared (PMutual _ ds) = concatMap tldeclared ds
tldeclared (PNamespace _ _ ds) = concatMap tldeclared ds
tldeclared (PClass _ _ _ _ n _ _ _ _ ms cn _) = n : (map fst (maybeToList cn) ++ concatMap tldeclared ms)
tldeclared (PInstance _ _ _ _ _ _ _ _ _ _ _ _ _) = []
tldeclared _ = []
defined :: PDecl -> [Name]
defined (PFix _ _ _) = []
defined (PTy _ _ _ _ _ n _ t) = []
defined (PPostulate _ _ _ _ _ _ n t) = []
defined (PClauses _ _ n _) = [n]
defined (PCAF _ n _) = [n]
defined (PData _ _ _ _ _ (PDatadecl n _ _ ts)) = n : map fstt ts
where fstt (_, _, a, _, _, _, _) = a
defined (PData _ _ _ _ _ (PLaterdecl n _ _)) = []
defined (PParams _ _ ds) = concatMap defined ds
defined (PNamespace _ _ ds) = concatMap defined ds
defined (PRecord _ _ _ _ n _ _ _ _ cn _ _) = n : map fst (maybeToList cn)
defined (PClass _ _ _ _ n _ _ _ _ ms cn _) = n : (map fst (maybeToList cn) ++ concatMap defined ms)
defined (PInstance _ _ _ _ _ _ _ _ _ _ _ _ _) = []
defined (PDSL n _) = [n]
defined (PSyntax _ _) = []
defined (PMutual _ ds) = concatMap defined ds
defined (PDirective _) = []
defined _ = []
updateN :: [(Name, Name)] -> Name -> Name
updateN ns n | Just n' <- lookup n ns = n'
updateN _ n = n
updateNs :: [(Name, Name)] -> PTerm -> PTerm
updateNs [] t = t
updateNs ns t = mapPT updateRef t
where updateRef (PRef fc fcs f) = PRef fc fcs (updateN ns f)
updateRef t = t
data PunInfo = IsType | IsTerm | TypeOrTerm deriving (Eq, Show, Data, Typeable)
data PTerm = PQuote Raw
| PRef FC [FC] Name
| PInferRef FC [FC] Name
| PPatvar FC Name
| PLam FC Name FC PTerm PTerm
| PPi Plicity Name FC PTerm PTerm
| PLet FC Name FC PTerm PTerm PTerm
| PTyped PTerm PTerm
| PApp FC PTerm [PArg]
| PAppImpl PTerm [ImplicitInfo]
| PAppBind FC PTerm [PArg]
| PMatchApp FC Name
| PIfThenElse FC PTerm PTerm PTerm
| PCase FC PTerm [(PTerm, PTerm)]
| PTrue FC PunInfo
| PResolveTC FC
| PRewrite FC PTerm PTerm (Maybe PTerm)
| PPair FC [FC] PunInfo PTerm PTerm
| PDPair FC [FC] PunInfo PTerm PTerm PTerm
| PAs FC Name PTerm
| PAlternative [(Name, Name)] PAltType [PTerm]
| PHidden PTerm
| PType FC
| PUniverse Universe
| PGoal FC PTerm Name PTerm
| PConstant FC Const
| Placeholder
| PDoBlock [PDo]
| PIdiom FC PTerm
| PReturn FC
| PMetavar FC Name
| PProof [PTactic]
| PTactics [PTactic]
| PElabError Err
| PImpossible
| PCoerced PTerm
| PDisamb [[T.Text]] PTerm
| PUnifyLog PTerm
| PNoImplicits PTerm
| PQuasiquote PTerm (Maybe PTerm)
| PUnquote PTerm
| PQuoteName Name Bool FC
| PRunElab FC PTerm [String]
| PConstSugar FC PTerm
deriving (Eq, Data, Typeable)
data PAltType = ExactlyOne Bool
| FirstSuccess
| TryImplicit
deriving (Eq, Data, Typeable)
mapPTermFC :: (FC -> FC) -> (FC -> FC) -> PTerm -> PTerm
mapPTermFC f g (PQuote q) = PQuote q
mapPTermFC f g (PRef fc fcs n) = PRef (g fc) (map g fcs) n
mapPTermFC f g (PInferRef fc fcs n) = PInferRef (g fc) (map g fcs) n
mapPTermFC f g (PPatvar fc n) = PPatvar (g fc) n
mapPTermFC f g (PLam fc n fc' t1 t2) = PLam (f fc) n (g fc') (mapPTermFC f g t1) (mapPTermFC f g t2)
mapPTermFC f g (PPi plic n fc t1 t2) = PPi plic n (g fc) (mapPTermFC f g t1) (mapPTermFC f g t2)
mapPTermFC f g (PLet fc n fc' t1 t2 t3) = PLet (f fc) n (g fc') (mapPTermFC f g t1) (mapPTermFC f g t2) (mapPTermFC f g t3)
mapPTermFC f g (PTyped t1 t2) = PTyped (mapPTermFC f g t1) (mapPTermFC f g t2)
mapPTermFC f g (PApp fc t args) = PApp (f fc) (mapPTermFC f g t) (map (fmap (mapPTermFC f g)) args)
mapPTermFC f g (PAppImpl t1 impls) = PAppImpl (mapPTermFC f g t1) impls
mapPTermFC f g (PAppBind fc t args) = PAppBind (f fc) (mapPTermFC f g t) (map (fmap (mapPTermFC f g)) args)
mapPTermFC f g (PMatchApp fc n) = PMatchApp (f fc) n
mapPTermFC f g (PIfThenElse fc t1 t2 t3) = PIfThenElse (f fc) (mapPTermFC f g t1) (mapPTermFC f g t2) (mapPTermFC f g t3)
mapPTermFC f g (PCase fc t cases) = PCase (f fc) (mapPTermFC f g t) (map (\(l,r) -> (mapPTermFC f g l, mapPTermFC f g r)) cases)
mapPTermFC f g (PTrue fc info) = PTrue (f fc) info
mapPTermFC f g (PResolveTC fc) = PResolveTC (f fc)
mapPTermFC f g (PRewrite fc t1 t2 t3) = PRewrite (f fc) (mapPTermFC f g t1) (mapPTermFC f g t2) (fmap (mapPTermFC f g) t3)
mapPTermFC f g (PPair fc hls info t1 t2) = PPair (f fc) (map g hls) info (mapPTermFC f g t1) (mapPTermFC f g t2)
mapPTermFC f g (PDPair fc hls info t1 t2 t3) = PDPair (f fc) (map g hls) info (mapPTermFC f g t1) (mapPTermFC f g t2) (mapPTermFC f g t3)
mapPTermFC f g (PAs fc n t) = PAs (f fc) n (mapPTermFC f g t)
mapPTermFC f g (PAlternative ns ty ts) = PAlternative ns ty (map (mapPTermFC f g) ts)
mapPTermFC f g (PHidden t) = PHidden (mapPTermFC f g t)
mapPTermFC f g (PType fc) = PType (f fc)
mapPTermFC f g (PUniverse u) = PUniverse u
mapPTermFC f g (PGoal fc t1 n t2) = PGoal (f fc) (mapPTermFC f g t1) n (mapPTermFC f g t2)
mapPTermFC f g (PConstant fc c) = PConstant (f fc) c
mapPTermFC f g Placeholder = Placeholder
mapPTermFC f g (PDoBlock dos) = PDoBlock (map mapPDoFC dos)
where mapPDoFC (DoExp fc t) = DoExp (f fc) (mapPTermFC f g t)
mapPDoFC (DoBind fc n nfc t) = DoBind (f fc) n (g nfc) (mapPTermFC f g t)
mapPDoFC (DoBindP fc t1 t2 alts) =
DoBindP (f fc) (mapPTermFC f g t1) (mapPTermFC f g t2) (map (\(l,r)-> (mapPTermFC f g l, mapPTermFC f g r)) alts)
mapPDoFC (DoLet fc n nfc t1 t2) = DoLet (f fc) n (g nfc) (mapPTermFC f g t1) (mapPTermFC f g t2)
mapPDoFC (DoLetP fc t1 t2) = DoLetP (f fc) (mapPTermFC f g t1) (mapPTermFC f g t2)
mapPTermFC f g (PIdiom fc t) = PIdiom (f fc) (mapPTermFC f g t)
mapPTermFC f g (PReturn fc) = PReturn (f fc)
mapPTermFC f g (PMetavar fc n) = PMetavar (g fc) n
mapPTermFC f g (PProof tacs) = PProof (map (fmap (mapPTermFC f g)) tacs)
mapPTermFC f g (PTactics tacs) = PTactics (map (fmap (mapPTermFC f g)) tacs)
mapPTermFC f g (PElabError err) = PElabError err
mapPTermFC f g PImpossible = PImpossible
mapPTermFC f g (PCoerced t) = PCoerced (mapPTermFC f g t)
mapPTermFC f g (PDisamb msg t) = PDisamb msg (mapPTermFC f g t)
mapPTermFC f g (PUnifyLog t) = PUnifyLog (mapPTermFC f g t)
mapPTermFC f g (PNoImplicits t) = PNoImplicits (mapPTermFC f g t)
mapPTermFC f g (PQuasiquote t1 t2) = PQuasiquote (mapPTermFC f g t1) (fmap (mapPTermFC f g) t2)
mapPTermFC f g (PUnquote t) = PUnquote (mapPTermFC f g t)
mapPTermFC f g (PRunElab fc tm ns) = PRunElab (f fc) (mapPTermFC f g tm) ns
mapPTermFC f g (PConstSugar fc tm) = PConstSugar (g fc) (mapPTermFC f g tm)
mapPTermFC f g (PQuoteName n x fc) = PQuoteName n x (g fc)
mapPT :: (PTerm -> PTerm) -> PTerm -> PTerm
mapPT f t = f (mpt t) where
mpt (PLam fc n nfc t s) = PLam fc n nfc (mapPT f t) (mapPT f s)
mpt (PPi p n nfc t s) = PPi p n nfc (mapPT f t) (mapPT f s)
mpt (PLet fc n nfc ty v s) = PLet fc n nfc (mapPT f ty) (mapPT f v) (mapPT f s)
mpt (PRewrite fc t s g) = PRewrite fc (mapPT f t) (mapPT f s) (fmap (mapPT f) g)
mpt (PApp fc t as) = PApp fc (mapPT f t) (map (fmap (mapPT f)) as)
mpt (PAppBind fc t as) = PAppBind fc (mapPT f t) (map (fmap (mapPT f)) as)
mpt (PCase fc c os) = PCase fc (mapPT f c) (map (pmap (mapPT f)) os)
mpt (PIfThenElse fc c t e) = PIfThenElse fc (mapPT f c) (mapPT f t) (mapPT f e)
mpt (PTyped l r) = PTyped (mapPT f l) (mapPT f r)
mpt (PPair fc hls p l r) = PPair fc hls p (mapPT f l) (mapPT f r)
mpt (PDPair fc hls p l t r) = PDPair fc hls p (mapPT f l) (mapPT f t) (mapPT f r)
mpt (PAlternative ns a as) = PAlternative ns a (map (mapPT f) as)
mpt (PHidden t) = PHidden (mapPT f t)
mpt (PDoBlock ds) = PDoBlock (map (fmap (mapPT f)) ds)
mpt (PProof ts) = PProof (map (fmap (mapPT f)) ts)
mpt (PTactics ts) = PTactics (map (fmap (mapPT f)) ts)
mpt (PUnifyLog tm) = PUnifyLog (mapPT f tm)
mpt (PDisamb ns tm) = PDisamb ns (mapPT f tm)
mpt (PNoImplicits tm) = PNoImplicits (mapPT f tm)
mpt (PGoal fc r n sc) = PGoal fc (mapPT f r) n (mapPT f sc)
mpt x = x
data PTactic' t = Intro [Name] | Intros | Focus Name
| Refine Name [Bool] | Rewrite t | DoUnify
| Induction t
| CaseTac t
| Equiv t
| Claim Name t
| Unfocus
| MatchRefine Name
| LetTac Name t | LetTacTy Name t t
| Exact t | Compute | Trivial | TCInstance
| ProofSearch Bool Bool Int (Maybe Name)
[Name]
[Name]
| Solve
| Attack
| ProofState | ProofTerm | Undo
| Try (PTactic' t) (PTactic' t)
| TSeq (PTactic' t) (PTactic' t)
| ApplyTactic t
| ByReflection t
| Reflect t
| Fill t
| GoalType String (PTactic' t)
| TCheck t
| TEval t
| TDocStr (Either Name Const)
| TSearch t
| Skip
| TFail [ErrorReportPart]
| Qed | Abandon
| SourceFC
deriving (Show, Eq, Functor, Foldable, Traversable, Data, Typeable)
instance Sized a => Sized (PTactic' a) where
size (Intro nms) = 1 + size nms
size Intros = 1
size (Focus nm) = 1 + size nm
size (Refine nm bs) = 1 + size nm + length bs
size (Rewrite t) = 1 + size t
size (Induction t) = 1 + size t
size (LetTac nm t) = 1 + size nm + size t
size (Exact t) = 1 + size t
size Compute = 1
size Trivial = 1
size Solve = 1
size Attack = 1
size ProofState = 1
size ProofTerm = 1
size Undo = 1
size (Try l r) = 1 + size l + size r
size (TSeq l r) = 1 + size l + size r
size (ApplyTactic t) = 1 + size t
size (Reflect t) = 1 + size t
size (Fill t) = 1 + size t
size Qed = 1
size Abandon = 1
size Skip = 1
size (TFail ts) = 1 + size ts
size SourceFC = 1
size DoUnify = 1
size (CaseTac x) = 1 + size x
size (Equiv t) = 1 + size t
size (Claim x y) = 1 + size x + size y
size Unfocus = 1
size (MatchRefine x) = 1 + size x
size (LetTacTy x y z) = 1 + size x + size y + size z
size TCInstance = 1
type PTactic = PTactic' PTerm
data PDo' t = DoExp FC t
| DoBind FC Name FC t
| DoBindP FC t t [(t,t)]
| DoLet FC Name FC t t
| DoLetP FC t t
deriving (Eq, Functor, Data, Typeable)
instance Sized a => Sized (PDo' a) where
size (DoExp fc t) = 1 + size fc + size t
size (DoBind fc nm nfc t) = 1 + size fc + size nm + size nfc + size t
size (DoBindP fc l r alts) = 1 + size fc + size l + size r + size alts
size (DoLet fc nm nfc l r) = 1 + size fc + size nm + size l + size r
size (DoLetP fc l r) = 1 + size fc + size l + size r
type PDo = PDo' PTerm
data PArg' t = PImp { priority :: Int,
machine_inf :: Bool,
argopts :: [ArgOpt],
pname :: Name,
getTm :: t }
| PExp { priority :: Int,
argopts :: [ArgOpt],
pname :: Name,
getTm :: t }
| PConstraint { priority :: Int,
argopts :: [ArgOpt],
pname :: Name,
getTm :: t }
| PTacImplicit { priority :: Int,
argopts :: [ArgOpt],
pname :: Name,
getScript :: t,
getTm :: t }
deriving (Show, Eq, Functor, Data, Typeable)
data ArgOpt = AlwaysShow | HideDisplay | InaccessibleArg | UnknownImp
deriving (Show, Eq, Data, Typeable)
instance Sized a => Sized (PArg' a) where
size (PImp p _ l nm trm) = 1 + size nm + size trm
size (PExp p l nm trm) = 1 + size nm + size trm
size (PConstraint p l nm trm) = 1 + size nm +size nm + size trm
size (PTacImplicit p l nm scr trm) = 1 + size nm + size scr + size trm
pimp n t mach = PImp 1 mach [] n t
pexp t = PExp 1 [] (sMN 0 "arg") t
pconst t = PConstraint 1 [] (sMN 0 "carg") t
ptacimp n s t = PTacImplicit 2 [] n s t
type PArg = PArg' PTerm
highestFC :: PTerm -> Maybe FC
highestFC (PQuote _) = Nothing
highestFC (PRef fc _ _) = Just fc
highestFC (PInferRef fc _ _) = Just fc
highestFC (PPatvar fc _) = Just fc
highestFC (PLam fc _ _ _ _) = Just fc
highestFC (PPi _ _ _ _ _) = Nothing
highestFC (PLet fc _ _ _ _ _) = Just fc
highestFC (PTyped tm ty) = highestFC tm <|> highestFC ty
highestFC (PApp fc _ _) = Just fc
highestFC (PAppBind fc _ _) = Just fc
highestFC (PMatchApp fc _) = Just fc
highestFC (PCase fc _ _) = Just fc
highestFC (PIfThenElse fc _ _ _) = Just fc
highestFC (PTrue fc _) = Just fc
highestFC (PResolveTC fc) = Just fc
highestFC (PRewrite fc _ _ _) = Just fc
highestFC (PPair fc _ _ _ _) = Just fc
highestFC (PDPair fc _ _ _ _ _) = Just fc
highestFC (PAs fc _ _) = Just fc
highestFC (PAlternative _ _ args) =
case mapMaybe highestFC args of
[] -> Nothing
(fc:_) -> Just fc
highestFC (PHidden _) = Nothing
highestFC (PType fc) = Just fc
highestFC (PUniverse _) = Nothing
highestFC (PGoal fc _ _ _) = Just fc
highestFC (PConstant fc _) = Just fc
highestFC Placeholder = Nothing
highestFC (PDoBlock lines) =
case map getDoFC lines of
[] -> Nothing
(fc:_) -> Just fc
where
getDoFC (DoExp fc t) = fc
getDoFC (DoBind fc nm nfc t) = fc
getDoFC (DoBindP fc l r alts) = fc
getDoFC (DoLet fc nm nfc l r) = fc
getDoFC (DoLetP fc l r) = fc
highestFC (PIdiom fc _) = Just fc
highestFC (PReturn fc) = Just fc
highestFC (PMetavar fc _) = Just fc
highestFC (PProof _) = Nothing
highestFC (PTactics _) = Nothing
highestFC (PElabError _) = Nothing
highestFC PImpossible = Nothing
highestFC (PCoerced tm) = highestFC tm
highestFC (PDisamb _ opts) = highestFC opts
highestFC (PUnifyLog tm) = highestFC tm
highestFC (PNoImplicits tm) = highestFC tm
highestFC (PQuasiquote _ _) = Nothing
highestFC (PUnquote tm) = highestFC tm
highestFC (PQuoteName _ _ fc) = Just fc
highestFC (PRunElab fc _ _) = Just fc
highestFC (PConstSugar fc _) = Just fc
highestFC (PAppImpl t _) = highestFC t
data ClassInfo = CI { instanceCtorName :: Name,
class_methods :: [(Name, (FnOpts, PTerm))],
class_defaults :: [(Name, (Name, PDecl))],
class_default_superclasses :: [PDecl],
class_params :: [Name],
class_instances :: [(Name, Bool)],
class_determiners :: [Int] }
deriving Show
data RecordInfo = RI { record_parameters :: [(Name,PTerm)],
record_constructor :: Name,
record_projections :: [Name] }
deriving Show
data TIData = TIPartial
| TISolution [Term]
deriving Show
data FnInfo = FnInfo { fn_params :: [Int] }
deriving Show
data OptInfo = Optimise { inaccessible :: [(Int,Name)],
detaggable :: Bool }
deriving Show
data DSL' t = DSL { dsl_bind :: t,
dsl_return :: t,
dsl_apply :: t,
dsl_pure :: t,
dsl_var :: Maybe t,
index_first :: Maybe t,
index_next :: Maybe t,
dsl_lambda :: Maybe t,
dsl_let :: Maybe t,
dsl_pi :: Maybe t
}
deriving (Show, Functor)
type DSL = DSL' PTerm
data SynContext = PatternSyntax | TermSyntax | AnySyntax
deriving Show
data Syntax = Rule [SSymbol] PTerm SynContext
| DeclRule [SSymbol] [PDecl]
deriving Show
syntaxNames :: Syntax -> [Name]
syntaxNames (Rule syms _ _) = mapMaybe ename syms
where ename (Keyword n) = Just n
ename _ = Nothing
syntaxNames (DeclRule syms _) = mapMaybe ename syms
where ename (Keyword n) = Just n
ename _ = Nothing
syntaxSymbols :: Syntax -> [SSymbol]
syntaxSymbols (Rule ss _ _) = ss
syntaxSymbols (DeclRule ss _) = ss
data SSymbol = Keyword Name
| Symbol String
| Binding Name
| Expr Name
| SimpleExpr Name
deriving (Show, Eq)
newtype SyntaxRules = SyntaxRules { syntaxRulesList :: [Syntax] }
emptySyntaxRules :: SyntaxRules
emptySyntaxRules = SyntaxRules []
updateSyntaxRules :: [Syntax] -> SyntaxRules -> SyntaxRules
updateSyntaxRules rules (SyntaxRules sr) = SyntaxRules newRules
where
newRules = sortBy (ruleSort `on` syntaxSymbols) (rules ++ sr)
ruleSort [] [] = EQ
ruleSort [] _ = LT
ruleSort _ [] = GT
ruleSort (s1:ss1) (s2:ss2) =
case symCompare s1 s2 of
EQ -> ruleSort ss1 ss2
r -> r
symCompare (Keyword n1) (Keyword n2) = compare n1 n2
symCompare (Keyword _) _ = LT
symCompare (Symbol _) (Keyword _) = GT
symCompare (Symbol s1) (Symbol s2) = compare s1 s2
symCompare (Symbol _) _ = LT
symCompare (Binding _) (Keyword _) = GT
symCompare (Binding _) (Symbol _) = GT
symCompare (Binding b1) (Binding b2) = compare b1 b2
symCompare (Binding _) _ = LT
symCompare (Expr _) (Keyword _) = GT
symCompare (Expr _) (Symbol _) = GT
symCompare (Expr _) (Binding _) = GT
symCompare (Expr e1) (Expr e2) = compare e1 e2
symCompare (Expr _) _ = LT
symCompare (SimpleExpr _) (Keyword _) = GT
symCompare (SimpleExpr _) (Symbol _) = GT
symCompare (SimpleExpr _) (Binding _) = GT
symCompare (SimpleExpr _) (Expr _) = GT
symCompare (SimpleExpr e1) (SimpleExpr e2) = compare e1 e2
initDSL = DSL (PRef f [] (sUN ">>="))
(PRef f [] (sUN "return"))
(PRef f [] (sUN "<*>"))
(PRef f [] (sUN "pure"))
Nothing
Nothing
Nothing
Nothing
Nothing
Nothing
where f = fileFC "(builtin)"
data Using = UImplicit Name PTerm
| UConstraint Name [Name]
deriving (Show, Eq, Data, Typeable)
data SyntaxInfo = Syn { using :: [Using],
syn_params :: [(Name, PTerm)],
syn_namespace :: [String],
no_imp :: [Name],
imp_methods :: [Name],
decoration :: Name -> Name,
inPattern :: Bool,
implicitAllowed :: Bool,
maxline :: Maybe Int,
mut_nesting :: Int,
dsl_info :: DSL,
syn_in_quasiquote :: Int }
deriving Show
defaultSyntax = Syn [] [] [] [] [] id False False Nothing 0 initDSL 0
expandNS :: SyntaxInfo -> Name -> Name
expandNS syn n@(NS _ _) = n
expandNS syn n = case syn_namespace syn of
[] -> n
xs -> sNS n xs
bi = fileFC "builtin"
inferTy = sMN 0 "__Infer"
inferCon = sMN 0 "__infer"
inferDecl = PDatadecl inferTy NoFC
(PType bi)
[(emptyDocstring, [], inferCon, NoFC, PPi impl (sMN 0 "iType") NoFC (PType bi) (
PPi expl (sMN 0 "ival") NoFC (PRef bi [] (sMN 0 "iType"))
(PRef bi [] inferTy)), bi, [])]
inferOpts = []
infTerm t = PApp bi (PRef bi [] inferCon) [pimp (sMN 0 "iType") Placeholder True, pexp t]
infP = P (TCon 6 0) inferTy (TType (UVal 0))
getInferTerm, getInferType :: Term -> Term
getInferTerm (Bind n b sc) = Bind n b $ getInferTerm sc
getInferTerm (App _ (App _ _ _) tm) = tm
getInferTerm tm = tm
getInferType (Bind n b sc) = Bind n (toTy b) $ getInferType sc
where toTy (Lam t) = Pi Nothing t (TType (UVar 0))
toTy (PVar t) = PVTy t
toTy b = b
getInferType (App _ (App _ _ ty) _) = ty
primNames = [inferTy, inferCon]
unitTy = sUN "Unit"
unitCon = sUN "MkUnit"
falseDoc = fmap (const $ Msg "") . parseDocstring . T.pack $
"The empty type, also known as the trivially false proposition." ++
"\n\n" ++
"Use `void` or `absurd` to prove anything if you have a variable " ++
"of type `Void` in scope."
falseTy = sUN "Void"
pairTy = sNS (sUN "Pair") ["Builtins"]
pairCon = sNS (sUN "MkPair") ["Builtins"]
upairTy = sNS (sUN "UPair") ["Builtins"]
upairCon = sNS (sUN "MkUPair") ["Builtins"]
eqTy = sUN "="
eqCon = sUN "Refl"
eqDoc = fmap (const (Left $ Msg "")) . parseDocstring . T.pack $
"The propositional equality type. A proof that `x` = `y`." ++
"\n\n" ++
"To use such a proof, pattern-match on it, and the two equal things will " ++
"then need to be the _same_ pattern." ++
"\n\n" ++
"**Note**: Idris's equality type is potentially _heterogeneous_, which means that it " ++
"is possible to state equalities between values of potentially different " ++
"types. However, Idris will attempt the homogeneous case unless it fails to typecheck." ++
"\n\n" ++
"You may need to use `(~=~)` to explicitly request heterogeneous equality."
eqDecl = PDatadecl eqTy NoFC (piBindp impl [(n "A", PType bi), (n "B", PType bi)]
(piBind [(n "x", PRef bi [] (n "A")), (n "y", PRef bi [] (n "B"))]
(PType bi)))
[(reflDoc, reflParamDoc,
eqCon, NoFC, PPi impl (n "A") NoFC (PType bi) (
PPi impl (n "x") NoFC (PRef bi [] (n "A"))
(PApp bi (PRef bi [] eqTy) [pimp (n "A") Placeholder False,
pimp (n "B") Placeholder False,
pexp (PRef bi [] (n "x")),
pexp (PRef bi [] (n "x"))])), bi, [])]
where n a = sUN a
reflDoc = annotCode (const (Left $ Msg "")) . parseDocstring . T.pack $
"A proof that `x` in fact equals `x`. To construct this, you must have already " ++
"shown that both sides are in fact equal."
reflParamDoc = [(n "A", annotCode (const (Left $ Msg "")) . parseDocstring . T.pack $ "the type at which the equality is proven"),
(n "x", annotCode (const (Left $ Msg "")) . parseDocstring . T.pack $ "the element shown to be equal to itself.")]
eqParamDoc = [(n "A", annotCode (const (Left $ Msg "")) . parseDocstring . T.pack $ "the type of the left side of the equality"),
(n "B", annotCode (const (Left $ Msg "")) . parseDocstring . T.pack $ "the type of the right side of the equality")
]
where n a = sUN a
eqOpts = []
modDocName :: Name
modDocName = sMN 0 "ModuleDocs"
sigmaTy = sNS (sUN "DPair") ["Builtins"]
sigmaCon = sNS (sUN "MkDPair") ["Builtins"]
piBind :: [(Name, PTerm)] -> PTerm -> PTerm
piBind = piBindp expl
piBindp :: Plicity -> [(Name, PTerm)] -> PTerm -> PTerm
piBindp p [] t = t
piBindp p ((n, ty):ns) t = PPi p n NoFC ty (piBindp p ns t)
instance Show PTerm where
showsPrec _ tm = (displayS . renderPretty 1.0 10000000 . prettyImp defaultPPOption) tm
instance Show PDecl where
showsPrec _ d = (displayS . renderPretty 1.0 10000000 . showDeclImp verbosePPOption) d
instance Show PClause where
showsPrec _ c = (displayS . renderPretty 1.0 10000000 . showCImp verbosePPOption) c
instance Show PData where
showsPrec _ d = (displayS . renderPretty 1.0 10000000 . showDImp defaultPPOption) d
instance Pretty PTerm OutputAnnotation where
pretty = prettyImp defaultPPOption
annotationColour :: IState -> OutputAnnotation -> Maybe IdrisColour
annotationColour ist _ | not (idris_colourRepl ist) = Nothing
annotationColour ist (AnnConst c) =
let theme = idris_colourTheme ist
in Just $ if constIsType c
then typeColour theme
else dataColour theme
annotationColour ist (AnnData _ _) = Just $ dataColour (idris_colourTheme ist)
annotationColour ist (AnnType _ _) = Just $ typeColour (idris_colourTheme ist)
annotationColour ist (AnnBoundName _ True) = Just $ implicitColour (idris_colourTheme ist)
annotationColour ist (AnnBoundName _ False) = Just $ boundVarColour (idris_colourTheme ist)
annotationColour ist AnnKeyword = Just $ keywordColour (idris_colourTheme ist)
annotationColour ist (AnnName n _ _ _) =
let ctxt = tt_ctxt ist
theme = idris_colourTheme ist
in case () of
_ | isDConName n ctxt -> Just $ dataColour theme
_ | isFnName n ctxt -> Just $ functionColour theme
_ | isTConName n ctxt -> Just $ typeColour theme
_ | isPostulateName n ist -> Just $ postulateColour theme
_ | otherwise -> Nothing
annotationColour ist (AnnTextFmt fmt) = Just (colour fmt)
where colour BoldText = IdrisColour Nothing True False True False
colour UnderlineText = IdrisColour Nothing True True False False
colour ItalicText = IdrisColour Nothing True False False True
annotationColour ist _ = Nothing
consoleDecorate :: IState -> OutputAnnotation -> String -> String
consoleDecorate ist ann = maybe id colourise (annotationColour ist ann)
isPostulateName :: Name -> IState -> Bool
isPostulateName n ist = S.member n (idris_postulates ist)
prettyImp :: PPOption
-> PTerm
-> Doc OutputAnnotation
prettyImp impl = pprintPTerm impl [] [] []
prettyIst :: IState -> PTerm -> Doc OutputAnnotation
prettyIst ist = pprintPTerm (ppOptionIst ist) [] [] (idris_infixes ist)
pprintPTerm :: PPOption
-> [(Name, Bool)]
-> [Name]
-> [FixDecl]
-> PTerm
-> Doc OutputAnnotation
pprintPTerm ppo bnd docArgs infixes = prettySe (ppopt_depth ppo) startPrec bnd
where
startPrec = 0
funcAppPrec = 10
prettySe :: Maybe Int -> Int -> [(Name, Bool)] -> PTerm -> Doc OutputAnnotation
prettySe d p bnd (PQuote r) =
text "![" <> pretty r <> text "]"
prettySe d p bnd (PPatvar fc n) = pretty n
prettySe d p bnd e
| Just str <- slist d p bnd e = depth d $ str
| Just n <- snat ppo d p e = depth d $ annotate (AnnData "Nat" "") (text (show n))
prettySe d p bnd (PRef fc _ n) = prettyName True (ppopt_impl ppo) bnd n
prettySe d p bnd (PLam fc n nfc ty sc) =
depth d . bracket p startPrec . group . align . hang 2 $
text "\\" <> prettyBindingOf n False <+> text "=>" <$>
prettySe (decD d) startPrec ((n, False):bnd) sc
prettySe d p bnd (PLet fc n nfc ty v sc) =
depth d . bracket p startPrec . group . align $
kwd "let" <+> (group . align . hang 2 $ prettyBindingOf n False <+> text "=" <$> prettySe (decD d) startPrec bnd v) </>
kwd "in" <+> (group . align . hang 2 $ prettySe (decD d) startPrec ((n, False):bnd) sc)
prettySe d p bnd (PPi (Exp l s _) n _ ty sc)
| n `elem` allNamesIn sc || ppopt_impl ppo && uname n || n `elem` docArgs
|| ppopt_pinames ppo && uname n =
depth d . bracket p startPrec . group $
enclose lparen rparen (group . align $ prettyBindingOf n False <+> colon <+> prettySe (decD d) startPrec bnd ty) <+>
st <> text "->" <$> prettySe (decD d) startPrec ((n, False):bnd) sc
| otherwise =
depth d . bracket p startPrec . group $
group (prettySe (decD d) (startPrec + 1) bnd ty <+> st) <> text "->" <$>
group (prettySe (decD d) startPrec ((n, False):bnd) sc)
where
uname (UN n) = case str n of
('_':_) -> False
_ -> True
uname _ = False
st =
case s of
Static -> text "%static" <> space
_ -> empty
prettySe d p bnd (PPi (Imp l s _ fa) n _ ty sc)
| ppopt_impl ppo =
depth d . bracket p startPrec $
lbrace <> prettyBindingOf n True <+> colon <+> prettySe (decD d) startPrec bnd ty <> rbrace <+>
st <> text "->" </> prettySe (decD d) startPrec ((n, True):bnd) sc
| otherwise = depth d $ prettySe (decD d) startPrec ((n, True):bnd) sc
where
st =
case s of
Static -> text "%static" <> space
_ -> empty
prettySe d p bnd (PPi (Constraint _ _) n _ ty sc) =
depth d . bracket p startPrec $
prettySe (decD d) (startPrec + 1) bnd ty <+> text "=>" </> prettySe (decD d) startPrec ((n, True):bnd) sc
prettySe d p bnd (PPi (TacImp _ _ (PTactics [ProofSearch{}])) n _ ty sc) =
lbrace <> kwd "auto" <+> pretty n <+> colon <+> prettySe (decD d) startPrec bnd ty <>
rbrace <+> text "->" </> prettySe (decD d) startPrec ((n, True):bnd) sc
prettySe d p bnd (PPi (TacImp _ _ s) n _ ty sc) =
depth d . bracket p startPrec $
lbrace <> kwd "default" <+> prettySe (decD d) (funcAppPrec + 1) bnd s <+> pretty n <+> colon <+> prettySe (decD d) startPrec bnd ty <>
rbrace <+> text "->" </> prettySe (decD d) startPrec ((n, True):bnd) sc
prettySe d p bnd (PApp _ (PRef _ _ n) [lt, rt, l, r])
| n == eqTy, ppopt_impl ppo =
depth d . bracket p eqPrec $
enclose lparen rparen eq <+>
align (group (vsep (map (prettyArgS (decD d) bnd)
[lt, rt, l, r])))
| n == eqTy =
depth d . bracket p eqPrec . align . group $
prettyTerm (getTm l) <+> eq <$> group (prettyTerm (getTm r))
where eq = annName eqTy (text "=")
eqPrec = startPrec
prettyTerm = prettySe (decD d) (eqPrec + 1) bnd
prettySe d p bnd (PApp _ (PRef _ _ f) args)
| UN nm <- basename f
, not (ppopt_impl ppo) && null (getShowArgs args) =
prettyName True (ppopt_impl ppo) bnd f
prettySe d p bnd (PAppBind _ (PRef _ _ f) [])
| not (ppopt_impl ppo) = text "!" <> prettyName True (ppopt_impl ppo) bnd f
prettySe d p bnd (PApp _ (PRef _ _ op) args)
| UN nm <- basename op
, not (tnull nm) &&
(not (ppopt_impl ppo)) && (not $ isAlpha (thead nm)) =
case getShowArgs args of
[] -> opName True
[x] -> group (opName True <$> group (prettySe (decD d) startPrec bnd (getTm x)))
[l,r] -> let precedence = maybe (startPrec 1) prec f
in depth d . bracket p precedence $ inFix (getTm l) (getTm r)
(l@(PExp{}) : r@(PExp{}) : rest) ->
depth d . bracket p funcAppPrec $
enclose lparen rparen (inFix (getTm l) (getTm r)) <+>
align (group (vsep (map (prettyArgS d bnd) rest)))
as -> opName True <+> align (vsep (map (prettyArgS d bnd) as))
where opName isPrefix = prettyName isPrefix (ppopt_impl ppo) bnd op
f = getFixity (opStr op)
left = case f of
Nothing -> funcAppPrec + 1
Just (Infixl p') -> p'
Just f' -> prec f' + 1
right = case f of
Nothing -> funcAppPrec + 1
Just (Infixr p') -> p'
Just f' -> prec f' + 1
inFix l r = align . group $
(prettySe (decD d) left bnd l <+> opName False) <$>
group (prettySe (decD d) right bnd r)
prettySe d p bnd (PApp _ hd@(PRef fc _ f) [tm])
| PConstant NoFC (Idris.Core.TT.Str str) <- getTm tm,
f == sUN "Symbol_" = annotate (AnnType ("'" ++ str) ("The symbol " ++ str)) $
char '\'' <> prettySe (decD d) startPrec bnd (PRef fc [] (sUN str))
prettySe d p bnd (PApp _ f as) =
let args = getShowArgs as
fp = prettySe (decD d) (startPrec + 1) bnd f
shownArgs = if ppopt_impl ppo then as else args
in
depth d . bracket p funcAppPrec . group $
if null shownArgs
then fp
else fp <+> align (vsep (map (prettyArgS d bnd) shownArgs))
prettySe d p bnd (PCase _ scr cases) =
align $ kwd "case" <+> prettySe (decD d) startPrec bnd scr <+> kwd "of" <$>
depth d (indent 2 (vsep (map ppcase cases)))
where
ppcase (l, r) = let prettyCase = prettySe (decD d) startPrec
([(n, False) | n <- vars l] ++ bnd)
in nest nestingSize $
prettyCase l <+> text "=>" <+> prettyCase r
vars tm = filter noNS (allNamesIn tm)
noNS (NS _ _) = False
noNS _ = True
prettySe d p bnd (PIfThenElse _ c t f) =
depth d . bracket p funcAppPrec . group . align . hang 2 . vsep $
[ kwd "if" <+> prettySe (decD d) startPrec bnd c
, kwd "then" <+> prettySe (decD d) startPrec bnd t
, kwd "else" <+> prettySe (decD d) startPrec bnd f
]
prettySe d p bnd (PHidden tm) = text "." <> prettySe (decD d) funcAppPrec bnd tm
prettySe d p bnd (PResolveTC _) = kwd "%instance"
prettySe d p bnd (PTrue _ IsType) = annName unitTy $ text "()"
prettySe d p bnd (PTrue _ IsTerm) = annName unitCon $ text "()"
prettySe d p bnd (PTrue _ TypeOrTerm) = text "()"
prettySe d p bnd (PRewrite _ l r _) =
depth d . bracket p startPrec $
text "rewrite" <+> prettySe (decD d) (startPrec + 1) bnd l <+> text "in" <+> prettySe (decD d) startPrec bnd r
prettySe d p bnd (PTyped l r) =
lparen <> prettySe (decD d) startPrec bnd l <+> colon <+> prettySe (decD d) startPrec bnd r <> rparen
prettySe d p bnd pair@(PPair _ _ pun _ _)
| Just elts <- pairElts pair = depth d . enclose (ann lparen) (ann rparen) .
align . group . vsep . punctuate (ann comma) $
map (prettySe (decD d) startPrec bnd) elts
where ann = case pun of
TypeOrTerm -> id
IsType -> annName pairTy
IsTerm -> annName pairCon
prettySe d p bnd (PDPair _ _ pun l t r) =
depth d $
annotated lparen <>
left <+>
annotated (text "**") <+>
prettySe (decD d) startPrec (addBinding bnd) r <>
annotated rparen
where annotated = case pun of
IsType -> annName sigmaTy
IsTerm -> annName sigmaCon
TypeOrTerm -> id
(left, addBinding) = case (l, pun) of
(PRef _ _ n, IsType) -> (bindingOf n False <+> text ":" <+> prettySe (decD d) startPrec bnd t, ((n, False) :))
_ -> (prettySe (decD d) startPrec bnd l, id)
prettySe d p bnd (PAlternative ns a as) =
lparen <> text "|" <> prettyAs <> text "|" <> rparen
where
prettyAs =
foldr ((\ l r -> l <+> text "," <+> r) .
depth d . prettySe (decD d) startPrec bnd)
empty as
prettySe d p bnd (PType _) = annotate (AnnType "Type" "The type of types") $ text "Type"
prettySe d p bnd (PUniverse u) = annotate (AnnType (show u) "The type of unique types") $ text (show u)
prettySe d p bnd (PConstant _ c) = annotate (AnnConst c) (text (show c))
prettySe d p bnd (PProof ts) =
kwd "proof" <+> lbrace <> ellipsis <> rbrace
prettySe d p bnd (PTactics ts) =
kwd "tactics" <+> lbrace <> ellipsis <> rbrace
prettySe d p bnd (PMetavar _ n) = annotate (AnnName n (Just MetavarOutput) Nothing Nothing) $ text "?" <> pretty n
prettySe d p bnd (PReturn f) = kwd "return"
prettySe d p bnd PImpossible = kwd "impossible"
prettySe d p bnd Placeholder = text "_"
prettySe d p bnd (PDoBlock dos) =
bracket p startPrec $
kwd "do" <+> align (vsep (map (group . align . hang 2) (ppdo bnd dos)))
where ppdo bnd (DoExp _ tm:dos) = prettySe (decD d) startPrec bnd tm : ppdo bnd dos
ppdo bnd (DoBind _ bn _ tm : dos) =
(prettyBindingOf bn False <+> text "<-" <+>
group (align (hang 2 (prettySe (decD d) startPrec bnd tm)))) :
ppdo ((bn, False):bnd) dos
ppdo bnd (DoBindP _ _ _ _ : dos) =
text "no pretty printer for pattern-matching do binding" :
ppdo bnd dos
ppdo bnd (DoLet _ ln _ ty v : dos) =
(kwd "let" <+> prettyBindingOf ln False <+>
(if ty /= Placeholder
then colon <+> prettySe (decD d) startPrec bnd ty <+> text "="
else text "=") <+>
group (align (hang 2 (prettySe (decD d) startPrec bnd v)))) :
ppdo ((ln, False):bnd) dos
ppdo bnd (DoLetP _ _ _ : dos) =
text "no pretty printer for pattern-matching do binding" :
ppdo bnd dos
ppdo _ [] = []
prettySe d p bnd (PCoerced t) = prettySe d p bnd t
prettySe d p bnd (PElabError s) = pretty s
prettySe d p bnd (PQuasiquote t Nothing) = text "`(" <> prettySe (decD d) p [] t <> text ")"
prettySe d p bnd (PQuasiquote t (Just g)) = text "`(" <> prettySe (decD d) p [] t <+> colon <+> prettySe (decD d) p [] g <> text ")"
prettySe d p bnd (PUnquote t) = text "~" <> prettySe (decD d) p bnd t
prettySe d p bnd (PQuoteName n res _) = text start <> prettyName True (ppopt_impl ppo) bnd n <> text end
where start = if res then "`{" else "`{{"
end = if res then "}" else "}}"
prettySe d p bnd (PRunElab _ tm _) =
bracket p funcAppPrec . group . align . hang 2 $
text "%runElab" <$>
prettySe (decD d) funcAppPrec bnd tm
prettySe d p bnd (PConstSugar fc tm) = prettySe d p bnd tm
prettySe d p bnd _ = text "missing pretty-printer for term"
prettyBindingOf :: Name -> Bool -> Doc OutputAnnotation
prettyBindingOf n imp = annotate (AnnBoundName n imp) (text (display n))
where display (UN n) = T.unpack n
display (MN _ n) = T.unpack n
display (NS n ns) = (intercalate "." . map T.unpack . reverse) ns ++ "." ++ display n
display n = show n
prettyArgS d bnd (PImp _ _ _ n tm) = prettyArgSi d bnd (n, tm)
prettyArgS d bnd (PExp _ _ _ tm) = prettyArgSe d bnd tm
prettyArgS d bnd (PConstraint _ _ _ tm) = prettyArgSc d bnd tm
prettyArgS d bnd (PTacImplicit _ _ n _ tm) = prettyArgSti d bnd (n, tm)
prettyArgSe d bnd arg = prettySe d (funcAppPrec + 1) bnd arg
prettyArgSi d bnd (n, val) = lbrace <> pretty n <+> text "=" <+> prettySe (decD d) startPrec bnd val <> rbrace
prettyArgSc d bnd val = lbrace <> lbrace <> prettySe (decD d) startPrec bnd val <> rbrace <> rbrace
prettyArgSti d bnd (n, val) = lbrace <> kwd "auto" <+> pretty n <+> text "=" <+> prettySe (decD d) startPrec bnd val <> rbrace
annName :: Name -> Doc OutputAnnotation -> Doc OutputAnnotation
annName n = annotate (AnnName n Nothing Nothing Nothing)
opStr :: Name -> String
opStr (NS n _) = opStr n
opStr (UN n) = T.unpack n
slist' :: Maybe Int -> Int -> [(Name, Bool)] -> PTerm -> Maybe [Doc OutputAnnotation]
slist' (Just d) _ _ _ | d <= 0 = Nothing
slist' d _ _ e
| containsHole e = Nothing
slist' d p bnd (PApp _ (PRef _ _ nil) _)
| not (ppopt_impl ppo) && nsroot nil == sUN "Nil" = Just []
slist' d p bnd (PRef _ _ nil)
| not (ppopt_impl ppo) && nsroot nil == sUN "Nil" = Just []
slist' d p bnd (PApp _ (PRef _ _ cons) args)
| nsroot cons == sUN "::",
(PExp {getTm=tl}):(PExp {getTm=hd}):imps <- reverse args,
all isImp imps,
Just tl' <- slist' (decD d) p bnd tl
= Just (prettySe d startPrec bnd hd : tl')
where
isImp (PImp {}) = True
isImp _ = False
slist' _ _ _ tm = Nothing
slist d p bnd e | Just es <- slist' d p bnd e = Just $
case es of
[] -> annotate (AnnData "" "") $ text "[]"
[x] -> enclose left right . group $ x
xs -> enclose left right .
align . group . vsep .
punctuate comma $ xs
where left = (annotate (AnnData "" "") (text "["))
right = (annotate (AnnData "" "") (text "]"))
comma = (annotate (AnnData "" "") (text ","))
slist _ _ _ _ = Nothing
pairElts :: PTerm -> Maybe [PTerm]
pairElts (PPair _ _ _ x y) | Just elts <- pairElts y = Just (x:elts)
| otherwise = Just [x, y]
pairElts _ = Nothing
natns = "Prelude.Nat."
snat :: PPOption -> Maybe Int -> Int -> PTerm -> Maybe Integer
snat ppo d p e
| ppopt_desugarnats ppo = Nothing
| otherwise = snat' d p e
where
snat' :: Maybe Int -> Int -> PTerm -> Maybe Integer
snat' (Just x) _ _ | x <= 0 = Nothing
snat' d p (PRef _ _ z)
| show z == (natns++"Z") || show z == "Z" = Just 0
snat' d p (PApp _ s [PExp {getTm=n}])
| show s == (natns++"S") || show s == "S",
Just n' <- snat' (decD d) p n
= Just $ 1 + n'
snat' _ _ _ = Nothing
bracket outer inner doc
| outer > inner = lparen <> doc <> rparen
| otherwise = doc
ellipsis = text "..."
depth Nothing = id
depth (Just d) = if d <= 0 then const ellipsis else id
decD = fmap (\x -> x 1)
kwd = annotate AnnKeyword . text
fixities :: M.Map String Fixity
fixities = M.fromList [(s, f) | (Fix f s) <- infixes]
getFixity :: String -> Maybe Fixity
getFixity = flip M.lookup fixities
basename :: Name -> Name
basename (NS n _) = basename n
basename n = n
isHoleName :: Name -> Bool
isHoleName (UN n) = n == T.pack "[__]"
isHoleName _ = False
containsHole :: PTerm -> Bool
containsHole pterm = or [isHoleName n | PRef _ _ n <- take 1000 $ universe pterm]
prettyName
:: Bool
-> Bool
-> [(Name, Bool)]
-> Name
-> Doc OutputAnnotation
prettyName infixParen showNS bnd n
| (MN _ s) <- n, isPrefixOf "_" $ T.unpack s = text "_"
| (UN n') <- n, T.unpack n' == "_" = text "_"
| Just imp <- lookup n bnd = annotate (AnnBoundName n imp) fullName
| otherwise = annotate (AnnName n Nothing Nothing Nothing) fullName
where fullName = text nameSpace <> parenthesise (text (baseName n))
baseName (UN n) = T.unpack n
baseName (NS n ns) = baseName n
baseName (MN i s) = T.unpack s
baseName other = show other
nameSpace = case n of
(NS n' ns) -> if showNS then (concatMap (++ ".") . map T.unpack . reverse) ns else ""
_ -> ""
isInfix = case baseName n of
"" -> False
(c : _) -> not (isAlpha c)
parenthesise = if isInfix && infixParen then enclose lparen rparen else id
showCImp :: PPOption -> PClause -> Doc OutputAnnotation
showCImp ppo (PClause _ n l ws r w)
= prettyImp ppo l <+> showWs ws <+> text "=" <+> prettyImp ppo r
<+> text "where" <+> text (show w)
where
showWs [] = empty
showWs (x : xs) = text "|" <+> prettyImp ppo x <+> showWs xs
showCImp ppo (PWith _ n l ws r pn w)
= prettyImp ppo l <+> showWs ws <+> text "with" <+> prettyImp ppo r
<+> braces (text (show w))
where
showWs [] = empty
showWs (x : xs) = text "|" <+> prettyImp ppo x <+> showWs xs
showDImp :: PPOption -> PData -> Doc OutputAnnotation
showDImp ppo (PDatadecl n nfc ty cons)
= text "data" <+> text (show n) <+> colon <+> prettyImp ppo ty <+> text "where" <$>
(indent 2 $ vsep (map (\ (_, _, n, _, t, _, _) -> pipe <+> prettyName True False [] n <+> colon <+> prettyImp ppo t) cons))
showDecls :: PPOption -> [PDecl] -> Doc OutputAnnotation
showDecls o ds = vsep (map (showDeclImp o) ds)
showDeclImp _ (PFix _ f ops) = text (show f) <+> cat (punctuate (text ",") (map text ops))
showDeclImp o (PTy _ _ _ _ _ n _ t) = text "tydecl" <+> text (showCG n) <+> colon <+> prettyImp o t
showDeclImp o (PClauses _ _ n cs) = text "pat" <+> text (showCG n) <+> text "\t" <+>
indent 2 (vsep (map (showCImp o) cs))
showDeclImp o (PData _ _ _ _ _ d) = showDImp o { ppopt_impl = True } d
showDeclImp o (PParams _ ns ps) = text "params" <+> braces (text (show ns) <> line <> showDecls o ps <> line)
showDeclImp o (PNamespace n fc ps) = text "namespace" <+> text n <> braces (line <> showDecls o ps <> line)
showDeclImp _ (PSyntax _ syn) = text "syntax" <+> text (show syn)
showDeclImp o (PClass _ _ _ cs n _ ps _ _ ds _ _)
= text "interface" <+> text (show cs) <+> text (show n) <+> text (show ps) <> line <> showDecls o ds
showDeclImp o (PInstance _ _ _ _ cs acc _ n _ _ t _ ds)
= text "implementation" <+> text (show cs) <+> text (show n) <+> prettyImp o t <> line <> showDecls o ds
showDeclImp _ _ = text "..."
getImps :: [PArg] -> [(Name, PTerm)]
getImps [] = []
getImps (PImp _ _ _ n tm : xs) = (n, tm) : getImps xs
getImps (_ : xs) = getImps xs
getExps :: [PArg] -> [PTerm]
getExps [] = []
getExps (PExp _ _ _ tm : xs) = tm : getExps xs
getExps (_ : xs) = getExps xs
getShowArgs :: [PArg] -> [PArg]
getShowArgs [] = []
getShowArgs (e@(PExp _ _ _ tm) : xs) = e : getShowArgs xs
getShowArgs (e : xs) | AlwaysShow `elem` argopts e = e : getShowArgs xs
| PImp _ _ _ _ tm <- e
, containsHole tm = e : getShowArgs xs
getShowArgs (_ : xs) = getShowArgs xs
getConsts :: [PArg] -> [PTerm]
getConsts [] = []
getConsts (PConstraint _ _ _ tm : xs) = tm : getConsts xs
getConsts (_ : xs) = getConsts xs
getAll :: [PArg] -> [PTerm]
getAll = map getTm
showName :: Maybe IState
-> [(Name, Bool)]
-> PPOption
-> Bool
-> Name
-> String
showName ist bnd ppo colour n = case ist of
Just i -> if colour then colourise n (idris_colourTheme i) else showbasic n
Nothing -> showbasic n
where name = if ppopt_impl ppo then show n else showbasic n
showbasic n@(UN _) = showCG n
showbasic (MN i s) = str s
showbasic (NS n s) = showSep "." (map str (reverse s)) ++ "." ++ showbasic n
showbasic (SN s) = show s
fst3 (x, _, _) = x
colourise n t = let ctxt' = fmap tt_ctxt ist in
case ctxt' of
Nothing -> name
Just ctxt | Just impl <- lookup n bnd -> if impl then colouriseImplicit t name
else colouriseBound t name
| isDConName n ctxt -> colouriseData t name
| isFnName n ctxt -> colouriseFun t name
| isTConName n ctxt -> colouriseType t name
| otherwise -> colouriseImplicit t name
showTm :: IState
-> PTerm
-> String
showTm ist = displayDecorated (consoleDecorate ist) .
renderPretty 0.8 100000 .
prettyImp (ppOptionIst ist)
showTmImpls :: PTerm -> String
showTmImpls = flip (displayS . renderCompact . prettyImp verbosePPOption) ""
showTmOpts :: PPOption -> PTerm -> String
showTmOpts opt = flip (displayS . renderPretty 1.0 10000000 . prettyImp opt) ""
instance Sized PTerm where
size (PQuote rawTerm) = size rawTerm
size (PRef fc _ name) = size name
size (PLam fc name _ ty bdy) = 1 + size ty + size bdy
size (PPi plicity name fc ty bdy) = 1 + size ty + size fc + size bdy
size (PLet fc name nfc ty def bdy) = 1 + size ty + size def + size bdy
size (PTyped trm ty) = 1 + size trm + size ty
size (PApp fc name args) = 1 + size args
size (PAppBind fc name args) = 1 + size args
size (PCase fc trm bdy) = 1 + size trm + size bdy
size (PIfThenElse fc c t f) = 1 + sum (map size [c, t, f])
size (PTrue fc _) = 1
size (PResolveTC fc) = 1
size (PRewrite fc left right _) = 1 + size left + size right
size (PPair fc _ _ left right) = 1 + size left + size right
size (PDPair fs _ _ left ty right) = 1 + size left + size ty + size right
size (PAlternative _ a alts) = 1 + size alts
size (PHidden hidden) = size hidden
size (PUnifyLog tm) = size tm
size (PDisamb _ tm) = size tm
size (PNoImplicits tm) = size tm
size (PType _) = 1
size (PUniverse _) = 1
size (PConstant fc const) = 1 + size fc + size const
size Placeholder = 1
size (PDoBlock dos) = 1 + size dos
size (PIdiom fc term) = 1 + size term
size (PReturn fc) = 1
size (PMetavar _ name) = 1
size (PProof tactics) = size tactics
size (PElabError err) = size err
size PImpossible = 1
size _ = 0
getPArity :: PTerm -> Int
getPArity (PPi _ _ _ _ sc) = 1 + getPArity sc
getPArity _ = 0
allNamesIn :: PTerm -> [Name]
allNamesIn tm = nub $ ni 0 [] tm
where
ni 0 env (PRef _ _ n)
| not (n `elem` env) = [n]
ni 0 env (PPatvar _ n) = [n]
ni 0 env (PApp _ f as) = ni 0 env f ++ concatMap (ni 0 env . getTm) as
ni 0 env (PAppBind _ f as) = ni 0 env f ++ concatMap (ni 0 env . getTm) as
ni 0 env (PCase _ c os) = ni 0 env c ++ concatMap (ni 0 env . snd) os
ni 0 env (PIfThenElse _ c t f) = ni 0 env c ++ ni 0 env t ++ ni 0 env f
ni 0 env (PLam fc n _ ty sc) = ni 0 env ty ++ ni 0 (n:env) sc
ni 0 env (PPi p n _ ty sc) = niTacImp 0 env p ++ ni 0 env ty ++ ni 0 (n:env) sc
ni 0 env (PLet _ n _ ty val sc) = ni 0 env ty ++ ni 0 env val ++ ni 0 (n:env) sc
ni 0 env (PHidden tm) = ni 0 env tm
ni 0 env (PRewrite _ l r _) = ni 0 env l ++ ni 0 env r
ni 0 env (PTyped l r) = ni 0 env l ++ ni 0 env r
ni 0 env (PPair _ _ _ l r) = ni 0 env l ++ ni 0 env r
ni 0 env (PDPair _ _ _ (PRef _ _ n) Placeholder r) = n : ni 0 env r
ni 0 env (PDPair _ _ _ (PRef _ _ n) t r) = ni 0 env t ++ ni 0 (n:env) r
ni 0 env (PDPair _ _ _ l t r) = ni 0 env l ++ ni 0 env t ++ ni 0 env r
ni 0 env (PAlternative ns a ls) = concatMap (ni 0 env) ls
ni 0 env (PUnifyLog tm) = ni 0 env tm
ni 0 env (PDisamb _ tm) = ni 0 env tm
ni 0 env (PNoImplicits tm) = ni 0 env tm
ni i env (PQuasiquote tm ty) = ni (i+1) env tm ++ maybe [] (ni i env) ty
ni i env (PUnquote tm) = ni (i 1) env tm
ni i env tm = concatMap (ni i env) (children tm)
niTacImp i env (TacImp _ _ scr) = ni i env scr
niTacImp _ _ _ = []
boundNamesIn :: PTerm -> [Name]
boundNamesIn tm = S.toList (ni 0 S.empty tm)
where
ni :: Int -> S.Set Name -> PTerm -> S.Set Name
ni 0 set (PApp _ f as) = niTms 0 (ni 0 set f) (map getTm as)
ni 0 set (PAppBind _ f as) = niTms 0 (ni 0 set f) (map getTm as)
ni 0 set (PCase _ c os) = niTms 0 (ni 0 set c) (map snd os)
ni 0 set (PIfThenElse _ c t f) = niTms 0 set [c, t, f]
ni 0 set (PLam fc n _ ty sc) = S.insert n $ ni 0 (ni 0 set ty) sc
ni 0 set (PLet fc n nfc ty val sc) = S.insert n $ ni 0 (ni 0 (ni 0 set ty) val) sc
ni 0 set (PPi p n _ ty sc) = niTacImp 0 (S.insert n $ ni 0 (ni 0 set ty) sc) p
ni 0 set (PRewrite _ l r _) = ni 0 (ni 0 set l) r
ni 0 set (PTyped l r) = ni 0 (ni 0 set l) r
ni 0 set (PPair _ _ _ l r) = ni 0 (ni 0 set l) r
ni 0 set (PDPair _ _ _ (PRef _ _ n) t r) = ni 0 (ni 0 set t) r
ni 0 set (PDPair _ _ _ l t r) = ni 0 (ni 0 (ni 0 set l) t) r
ni 0 set (PAlternative ns a as) = niTms 0 set as
ni 0 set (PHidden tm) = ni 0 set tm
ni 0 set (PUnifyLog tm) = ni 0 set tm
ni 0 set (PDisamb _ tm) = ni 0 set tm
ni 0 set (PNoImplicits tm) = ni 0 set tm
ni i set (PQuasiquote tm ty) = ni (i + 1) set tm `S.union` maybe S.empty (ni i set) ty
ni i set (PUnquote tm) = ni (i 1) set tm
ni i set tm = foldr (S.union . ni i set) set (children tm)
niTms :: Int -> S.Set Name -> [PTerm] -> S.Set Name
niTms i set [] = set
niTms i set (x : xs) = niTms i (ni i set x) xs
niTacImp i set (TacImp _ _ scr) = ni i set scr
niTacImp i set _ = set
implicitNamesIn :: [Name] -> IState -> PTerm -> [Name]
implicitNamesIn uvars ist tm
= let (imps, fns) = execState (ni 0 [] tm) ([], []) in
nub imps \\ nub fns
where
addImp n = do (imps, fns) <- get
put (n : imps, fns)
addFn n = do (imps, fns) <- get
put (imps, n: fns)
notCAF [] = False
notCAF (PExp _ _ _ _ : _) = True
notCAF (_ : xs) = notCAF xs
notHidden (n, _) = case getAccessibility n of
Hidden -> False
Private -> False
_ -> True
getAccessibility n
= case lookupDefAccExact n False (tt_ctxt ist) of
Just (n,t) -> t
_ -> Public
ni 0 env (PRef _ _ n@(NS _ _))
| not (n `elem` env)
= addFn n
ni 0 env (PRef _ _ n)
| not (n `elem` env) && implicitable n || n `elem` uvars = addImp n
ni 0 env (PApp _ f@(PRef _ _ n) as)
| n `elem` uvars = do ni 0 env f
mapM_ (ni 0 env . getTm) as
| otherwise = do case lookupTy n (tt_ctxt ist) of
[] -> return ()
_ -> addFn n
mapM_ (ni 0 env . getTm) as
ni 0 env (PApp _ f as) = do ni 0 env f
mapM_ (ni 0 env . getTm) as
ni 0 env (PAppBind _ f as) = do ni 0 env f
mapM_ (ni 0 env . getTm) as
ni 0 env (PCase _ c os) = do ni 0 env c
mapM_ (ni 0 env . snd) os
(imps, fns) <- get
put ([] ,[])
mapM_ (ni 0 env . fst) os
(impsfst, _) <- get
put (nub imps \\ nub impsfst, fns)
ni 0 env (PIfThenElse _ c t f) = mapM_ (ni 0 env) [c, t, f]
ni 0 env (PLam fc n _ ty sc) = do ni 0 env ty; ni 0 (n:env) sc
ni 0 env (PPi p n _ ty sc) = do ni 0 env ty; ni 0 (n:env) sc
ni 0 env (PRewrite _ l r _) = do ni 0 env l; ni 0 env r
ni 0 env (PTyped l r) = do ni 0 env l; ni 0 env r
ni 0 env (PPair _ _ _ l r) = do ni 0 env l; ni 0 env r
ni 0 env (PDPair _ _ _ (PRef _ _ n) t r) = do ni 0 env t; ni 0 (n:env) r
ni 0 env (PDPair _ _ _ l t r) = do ni 0 env l
ni 0 env t
ni 0 env r
ni 0 env (PAlternative ns a as) = mapM_ (ni 0 env) as
ni 0 env (PHidden tm) = ni 0 env tm
ni 0 env (PUnifyLog tm) = ni 0 env tm
ni 0 env (PDisamb _ tm) = ni 0 env tm
ni 0 env (PNoImplicits tm) = ni 0 env tm
ni i env (PQuasiquote tm ty) = do ni (i + 1) env tm
maybe (return ()) (ni i env) ty
ni i env (PUnquote tm) = ni (i 1) env tm
ni i env tm = mapM_ (ni i env) (children tm)
namesIn :: [(Name, PTerm)] -> IState -> PTerm -> [Name]
namesIn uvars ist tm = nub $ ni 0 [] tm
where
ni 0 env (PRef _ _ n)
| not (n `elem` env)
= case lookupTy n (tt_ctxt ist) of
[] -> [n]
_ -> [n | n `elem` (map fst uvars)]
ni 0 env (PApp _ f as) = ni 0 env f ++ concatMap (ni 0 env . getTm) as
ni 0 env (PAppBind _ f as) = ni 0 env f ++ concatMap (ni 0 env . getTm) as
ni 0 env (PCase _ c os) = ni 0 env c ++
(nub (concatMap (ni 0 env . snd) os)
\\ nub (concatMap (ni 0 env . fst) os))
ni 0 env (PIfThenElse _ c t f) = concatMap (ni 0 env) [c, t, f]
ni 0 env (PLam fc n nfc ty sc) = ni 0 env ty ++ ni 0 (n:env) sc
ni 0 env (PPi p n _ ty sc) = niTacImp 0 env p ++ ni 0 env ty ++ ni 0 (n:env) sc
ni 0 env (PRewrite _ l r _) = ni 0 env l ++ ni 0 env r
ni 0 env (PTyped l r) = ni 0 env l ++ ni 0 env r
ni 0 env (PPair _ _ _ l r) = ni 0 env l ++ ni 0 env r
ni 0 env (PDPair _ _ _ (PRef _ _ n) t r) = ni 0 env t ++ ni 0 (n:env) r
ni 0 env (PDPair _ _ _ l t r) = ni 0 env l ++ ni 0 env t ++ ni 0 env r
ni 0 env (PAlternative ns a as) = concatMap (ni 0 env) as
ni 0 env (PHidden tm) = ni 0 env tm
ni 0 env (PUnifyLog tm) = ni 0 env tm
ni 0 env (PDisamb _ tm) = ni 0 env tm
ni 0 env (PNoImplicits tm) = ni 0 env tm
ni i env (PQuasiquote tm ty) = ni (i + 1) env tm ++ maybe [] (ni i env) ty
ni i env (PUnquote tm) = ni (i 1) env tm
ni i env tm = concatMap (ni i env) (children tm)
niTacImp i env (TacImp _ _ scr) = ni i env scr
niTacImp _ _ _ = []
usedNamesIn :: [Name] -> IState -> PTerm -> [Name]
usedNamesIn vars ist tm = nub $ ni 0 [] tm
where
ni 0 env (PRef _ _ n)
| n `elem` vars && not (n `elem` env)
= case lookupDefExact n (tt_ctxt ist) of
Nothing -> [n]
_ -> []
ni 0 env (PApp _ f as) = ni 0 env f ++ concatMap (ni 0 env . getTm) as
ni 0 env (PAppBind _ f as) = ni 0 env f ++ concatMap (ni 0 env . getTm) as
ni 0 env (PCase _ c os) = ni 0 env c ++ concatMap (ni 0 env . snd) os
ni 0 env (PIfThenElse _ c t f) = concatMap (ni 0 env) [c, t, f]
ni 0 env (PLam fc n _ ty sc) = ni 0 env ty ++ ni 0 (n:env) sc
ni 0 env (PPi p n _ ty sc) = niTacImp 0 env p ++ ni 0 env ty ++ ni 0 (n:env) sc
ni 0 env (PRewrite _ l r _) = ni 0 env l ++ ni 0 env r
ni 0 env (PTyped l r) = ni 0 env l ++ ni 0 env r
ni 0 env (PPair _ _ _ l r) = ni 0 env l ++ ni 0 env r
ni 0 env (PDPair _ _ _ (PRef _ _ n) t r) = ni 0 env t ++ ni 0 (n:env) r
ni 0 env (PDPair _ _ _ l t r) = ni 0 env l ++ ni 0 env t ++ ni 0 env r
ni 0 env (PAlternative ns a as) = concatMap (ni 0 env) as
ni 0 env (PHidden tm) = ni 0 env tm
ni 0 env (PUnifyLog tm) = ni 0 env tm
ni 0 env (PDisamb _ tm) = ni 0 env tm
ni 0 env (PNoImplicits tm) = ni 0 env tm
ni i env (PQuasiquote tm ty) = ni (i + 1) env tm ++ maybe [] (ni i env) ty
ni i env (PUnquote tm) = ni (i 1) env tm
ni i env tm = concatMap (ni i env) (children tm)
niTacImp i env (TacImp _ _ scr) = ni i env scr
niTacImp _ _ _ = []
getErasureInfo :: IState -> Name -> [Int]
getErasureInfo ist n =
case lookupCtxtExact n (idris_optimisation ist) of
Just (Optimise inacc detagg) -> map fst inacc
Nothing -> []