on master help sign in
Add ASTUtils.
commitbb6873c4e82994bf4b38b4fe1f79e56ee579fb78 authorMatus Tejiscak <ziman@functor.sk> dateMon 14 Apr 2014, 15:33 (13 years ago) parentb283c0d
4 files changed+69−5
idris.cabal+1 src/Idris/ASTUtils.hs+62 src/Idris/Core/TT.hs+1 src/Idris/ElabDecls.hs+5−5
idris.cabal+1
@@ -456,6 +456,7 @@ Library
456456 , Idris.AbsSyntax
457457 , Idris.AbsSyntaxTree
458458 , Idris.Apropos
459+ , Idris.ASTUtils
459460 , Idris.CaseSplit
460461 , Idris.Chaser
461462 , Idris.Colours
src/Idris/ASTUtils.hsnew file+62
@@ -0,0 +1,62 @@
1+module Idris.ASTUtils where
2+
3+-- This implements just a few basic lens-like concepts to ease state updates.
4+-- Similar to fclabels in approach, just without the extra dependency.
5+--
6+-- We don't include an explicit export list
7+-- because everything here is meant to be exported.
8+
9+import Control.Category
10+import Control.Applicative
11+import Control.Monad.State.Class
12+import Data.Maybe
13+import Prelude hiding (id, (.))
14+
15+import Idris.Core.TT
16+import Idris.AbsSyntaxTree
17+
18+data Field rec fld = Field
19+ { fget :: rec -> fld
20+ , fset :: fld -> rec -> rec
21+ }
22+
23+fmodify :: Field rec fld -> (fld -> fld) -> rec -> rec
24+fmodify field f x = fset field (f $ fget field x) x
25+
26+instance Category Field where
27+ id = Field id const
28+ Field g2 s2 . Field g1 s1 = Field (g2 . g1) (\v2 x1 -> s1 (s2 v2 $ g1 x1) x1)
29+
30+fgetState :: MonadState s m => Field s a -> m a
31+fgetState field = gets $ fget field
32+
33+fputState :: MonadState s m => Field s a -> a -> m ()
34+fputState field x = fmodifyState field (const x)
35+
36+fmodifyState :: MonadState s m => Field s a -> (a -> a) -> m ()
37+fmodifyState field f = modify $ fmodify field f
38+
39+class InitialValue a where
40+ initialValue :: a
41+
42+ctxt_lookupExact :: InitialValue a => Name -> Field (Ctxt a) a
43+ctxt_lookupExact n = Field
44+ { fget = fromMaybe initialValue . lookupCtxtExact n
45+ , fset = addDef n
46+ }
47+
48+-----------------------------------
49+-- Individual records and fields --
50+-----------------------------------
51+
52+instance InitialValue OptInfo where
53+ initialValue = Optimise [] False
54+
55+ist_optimisation :: Field IState (Ctxt OptInfo)
56+ist_optimisation = Field idris_optimisation (\v ist -> ist{ idris_optimisation = v })
57+
58+opt_inaccessible :: Field OptInfo [(Int, Name)]
59+opt_inaccessible = Field inaccessible (\v opt -> opt{ inaccessible = v })
60+
61+opt_detaggable :: Field OptInfo Bool
62+opt_detaggable = Field detaggable (\v opt -> opt{ detaggable = v })
src/Idris/Core/TT.hs+1
@@ -383,6 +383,7 @@ implicitable _ = False
383383nsroot (NS n _) = n
384384nsroot n = n
385385
386+-- this will overwrite already existing definitions
386387addDef :: Name -> a -> Ctxt a -> Ctxt a
387388addDef n v ctxt = case Map.lookup (nsroot n) ctxt of
388389 Nothing -> Map.insert (nsroot n)
src/Idris/ElabDecls.hs+5−5
@@ -88,7 +88,7 @@ elabType' norm info syn doc argDocs fc opts n ty' = {- let ty' = piBind (params
8888 let ty = addImpl i ty'
8989 inacc = inaccessibleArgs 0 ty
9090 logLvl 3 $ show n ++ " type " ++ showTmImpls ty
91− logLvl 5 $ "Inaccessible arguments: " ++ show inacc
91+ logLvl 3 $ show n ++ ": inaccessible arguments: " ++ show inacc
9292
9393 ((tyT, defer, is), log) <-
9494 tclift $ elaborate ctxt n (TType (UVal 0)) []
@@ -182,12 +182,12 @@ elabType' norm info syn doc argDocs fc opts n ty' = {- let ty' = piBind (params
182182saveInaccArgs :: Name -> [(Int,Name)] -> Idris ()
183183saveInaccArgs n is = do
184184 ist <- getIState
185− case lookupCtxt n (idris_optimisation ist) of
186− [oi] -> do
185+ case lookupCtxtExact n (idris_optimisation ist) of
186+ Just oi -> do
187187 putIState ist{ idris_optimisation =
188188 addDef n oi{ inaccessible = is } (idris_optimisation ist) }
189189
190− _ -> do
190+ Nothing -> do
191191 putIState ist{ idris_optimisation =
192192 addDef n (Optimise is False) (idris_optimisation ist) }
193193 addIBC (IBCOpt n)
@@ -195,7 +195,7 @@ saveInaccArgs n is = do
195195-- Get the list of (index, name) of inaccessible arguments from the type.
196196inaccessibleArgs :: Int -> PTerm -> [(Int, Name)]
197197inaccessibleArgs i (PPi (Imp _ _ _) n Placeholder t)
198− = (i,n) : inaccessibleArgs (i+1) t -- unbound implicit
198+ = (i,n) : inaccessibleArgs (i+1) t -- unbound implicit
199199inaccessibleArgs i (PPi plicity n ty t)
200200 | InaccessibleArg `elem` pargopts plicity
201201 = (i,n) : inaccessibleArgs (i+1) t -- an .{erased : Implicit}