@@ -5,15 +5,18 @@ module Idris.ElabTerm where
55import Idris.AbsSyntax
66import Idris.DSL
77import Idris.Delaborate
8+import Idris.Error
89import Idris.ProofSearch
910
1011import Idris.Core.Elaborate hiding (Tactic(..))
1112import Idris.Core.TT
1213import Idris.Core.Evaluate
14+import Idris.Core.Typecheck (check)
1315
1416import Control.Monad
1517import Control.Monad.State
1618import Data.List
19+import Data.Maybe (mapMaybe)
1720
1821import Debug.Trace
1922
@@ -1352,9 +1355,36 @@ reflectEnv = foldr consToEnvList emptyEnvList
13521355
13531356-- | Reflect an error into the internal datatype of Idris -- TODO
13541357reflectErr :: Err -> Raw
1355−reflectErr (Msg msg) = raw_apply (Var (NS (UN "Msg") ["Reflection", "Language"])) [reflectConstant (Str msg)]
1356−reflectErr (InternalMsg msg) = raw_apply (Var (NS (UN "InternalMsg") ["Reflection", "Language"])) [reflectConstant (Str msg)]
1357−reflectErr x = trace ("Couldn't reflect error " ++ show x) raw_apply (Var (NS (UN "Msg") ["Reflection", "Language"])) [reflectConstant (Str $ show x)]
1358+reflectErr (Msg msg) = raw_apply (Var (NS (UN "Msg") ["Errors", "Reflection", "Language"])) [RConstant (Str msg)]
1359+reflectErr (InternalMsg msg) = raw_apply (Var (NS (UN "InternalMsg") ["Errors", "Reflection", "Language"])) [RConstant (Str msg)]
1360+reflectErr x = raw_apply (Var (NS (UN "Msg") ["Errors", "Reflection", "Language"])) [RConstant . Str $ "Default reflection: " ++ show x]
1361+
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
1368+
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
1374+ OK hs -> return hs
1375+
1376+ -- Normalize error handler terms to produce the new messages
1377+ ctxt <- getContext
1378+ let results = map (normalise ctxt []) (map fst handlers)
1379+
1380+
1381+ errorparts <- mapM reifyReportPart (concat (mapMaybe unList (mapMaybe fromTTMaybe results)))
1382+ newMsgs <- mapM renderReportPart errorparts
1383+ return . Msg $ concat (intersperse " " newMsgs)
1384+
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
13581388
13591389envTupleType :: Raw
13601390envTupleType