module Idris.ASTUtils where
import Control.Category
import Control.Applicative
import Control.Monad.State.Class
import Data.Maybe
import Prelude hiding (id, (.))
import Idris.Core.TT
import Idris.Core.Evaluate
import Idris.AbsSyntaxTree
data Field rec fld = Field
{ fget :: rec -> fld
, fset :: fld -> rec -> rec
}
fmodify :: Field rec fld -> (fld -> fld) -> rec -> rec
fmodify field f x = fset field (f $ fget field x) x
instance Category Field where
id = Field id const
Field g2 s2 . Field g1 s1 = Field (g2 . g1) (\v2 x1 -> s1 (s2 v2 $ g1 x1) x1)
fgetState :: MonadState s m => Field s a -> m a
fgetState field = gets $ fget field
fputState :: MonadState s m => Field s a -> a -> m ()
fputState field x = fmodifyState field (const x)
fmodifyState :: MonadState s m => Field s a -> (a -> a) -> m ()
fmodifyState field f = modify $ fmodify field f
ctxt_lookup :: Name -> Field (Ctxt a) (Maybe a)
ctxt_lookup n = Field
{ fget = lookupCtxtExact n
, fset = \newVal -> case newVal of
Just x -> addDef n x
Nothing -> deleteDefExact n
}
maybe_default :: a -> Field (Maybe a) a
maybe_default dflt = Field (fromMaybe dflt) (const . Just)
ist_optimisation :: Name -> Field IState OptInfo
ist_optimisation n =
maybe_default Optimise
{ inaccessible = []
, detaggable = False
}
. ctxt_lookup n
. Field idris_optimisation (\v ist -> ist{ idris_optimisation = v })
opt_inaccessible :: Field OptInfo [(Int, Name)]
opt_inaccessible = Field inaccessible (\v opt -> opt{ inaccessible = v })
opt_detaggable :: Field OptInfo Bool
opt_detaggable = Field detaggable (\v opt -> opt{ detaggable = v })
ist_callgraph :: Name -> Field IState CGInfo
ist_callgraph n =
maybe_default CGInfo
{ argsdef = [], calls = [], scg = []
, argsused = [], usedpos = []
}
. ctxt_lookup n
. Field idris_callgraph (\v ist -> ist{ idris_callgraph = v })
cg_usedpos :: Field CGInfo [(Int, [UsageReason])]
cg_usedpos = Field usedpos (\v cg -> cg{ usedpos = v })
opts_idrisCmdline :: Field IState [Opt]
opts_idrisCmdline =
Field opt_cmdline (\v opts -> opts{ opt_cmdline = v })
. Field idris_options (\v ist -> ist{ idris_options = v })
known_terms :: Field IState (Ctxt (Def, Accessibility, Totality, MetaInformation))
known_terms = Field (definitions . tt_ctxt)
(\v state -> state {tt_ctxt = (tt_ctxt state) {definitions = v}})
known_classes :: Field IState (Ctxt ClassInfo)
known_classes = Field idris_classes (\v state -> state {idris_classes = idris_classes state})
repl_definitions :: Field IState [Name]
repl_definitions = Field idris_repl_defs (\v state -> state {idris_repl_defs = v})
idris_fixities :: Field IState [FixDecl]
idris_fixities = Field idris_infixes (\v state -> state {idris_infixes = v})