@@ -5,15 +5,18 @@ module Idris.ElabTerm where
5import Idris.AbsSyntax
5import Idris.AbsSyntax
6import Idris.DSL
6import Idris.DSL
7import Idris.Delaborate
7import Idris.Delaborate
8import Idris.ProofSearch
9import Idris.ProofSearch
10import Idris.Core.Elaborate hiding (Tactic(..))
11import Idris.Core.Elaborate hiding (Tactic(..))
11import Idris.Core.TT
12import Idris.Core.TT
12import Idris.Core.Evaluate
13import Idris.Core.Evaluate
14+import Idris.Core.Typecheck (check)
14import Control.Monad
16import Control.Monad
15import Control.Monad.State
17import Control.Monad.State
16import Data.List
18import Data.List
19+import Data.Maybe (mapMaybe)
18import Debug.Trace
21import Debug.Trace
@@ -1352,9 +1355,36 @@ reflectEnv = foldr consToEnvList emptyEnvList
1353-- | Reflect an error into the internal datatype of Idris -- TODO
1356-- | Reflect an error into the internal datatype of Idris -- TODO
1354reflectErr :: Err -> Raw
1357reflectErr :: Err -> Raw
1355−reflectErr (Msg msg) = raw_apply (Var (NS (UN "Msg") ["Reflection", "Language"])) [reflectConstant (Str msg)]
1358+reflectErr (Msg msg) = raw_apply (Var (NS (UN "Msg") ["Errors", "Reflection", "Language"])) [RConstant (Str msg)]
1356−reflectErr (InternalMsg msg) = raw_apply (Var (NS (UN "InternalMsg") ["Reflection", "Language"])) [reflectConstant (Str msg)]
1359+reflectErr (InternalMsg msg) = raw_apply (Var (NS (UN "InternalMsg") ["Errors", "Reflection", "Language"])) [RConstant (Str msg)]
1357−reflectErr x = trace ("Couldn't reflect error " ++ show x) raw_apply (Var (NS (UN "Msg") ["Reflection", "Language"])) [reflectConstant (Str $ show x)]
1360+reflectErr x = raw_apply (Var (NS (UN "Msg") ["Errors", "Reflection", "Language"])) [RConstant . Str $ "Default reflection: " ++ show x]
1362+withErrorReflection :: Idris a -> Idris a
1363+withErrorReflection x = idrisCatch x (\ e -> handle e >>= ierror)
1364+ where handle :: Err -> Idris Err
1365+ handle e = do ist <- getIState
1366+ let handlers = idris_errorhandlers ist
1367+ let reports = map (\n -> RApp (Var n) (reflectErr e)) handlers
1369+ -- Typecheck error handlers - if this fails, then something else was wrong earlier!
1370+ handlers <- case mapM (check (tt_ctxt ist) []) reports of
1371+ Error e -> do formatted <- showErr e
1372+ ierror . InternalMsg $
1373+ "Type error during error reflection: " ++ formatted
1376+ -- Normalize error handler terms to produce the new messages
1378+ let results = map (normalise ctxt []) (map fst handlers)
1381+ errorparts <- mapM reifyReportPart (concat (mapMaybe unList (mapMaybe fromTTMaybe results)))
1382+ newMsgs <- mapM renderReportPart errorparts
1383+ return . Msg $ concat (intersperse " " newMsgs)
1385+ fromTTMaybe :: Term -> Maybe Term -- WARNING: Assumes the term has type Maybe a
1386+ fromTTMaybe (App (App (P (DCon _ _) (NS (UN "Just") _) _) ty) tm) = Just tm
1387+ fromTTMaybe x = Nothing
1359envTupleType :: Raw
1389envTupleType :: Raw
1360envTupleType
1390envTupleType