Commit 502d90fe authored by Andrew Gibiansky's avatar Andrew Gibiansky

Fixing repeated data declarations being ignored, closes #61

parent 6c404483
...@@ -12,90 +12,51 @@ ...@@ -12,90 +12,51 @@
"cell_type": "code", "cell_type": "code",
"collapsed": false, "collapsed": false,
"input": [ "input": [
"data X = X Int\n", "data X = Y Int"
" | Y String\n",
" | Z Float\n",
" deriving Show\n",
" \n",
"let values = [X 20,\n",
" Y \"Test\",\n",
" Z 0.5]\n",
"mapM_ print values"
], ],
"language": "python", "language": "python",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{ "prompt_number": 2
"metadata": {},
"output_type": "display_data",
"text": [
"X 20\n",
"Y \"Test\"\n",
"Z 0.5"
]
}
],
"prompt_number": 1
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "collapsed": false,
"input": [ "input": [
"import Control.Applicative\n", "data X = Y Int deriving Show"
"print 1\n",
"print $ (+) <$> Just 3 <*> Just 10"
], ],
"language": "python", "language": "python",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{ "prompt_number": 4
"metadata": {},
"output_type": "display_data",
"text": [
"1"
]
}, },
{ {
"metadata": {}, "cell_type": "code",
"output_type": "display_data", "collapsed": false,
"text": [ "input": [
"Just 13" "print (Y 3)"
]
}
], ],
"prompt_number": 2 "language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 5
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "collapsed": false,
"input": [ "input": [
"import Control.Monad\n", "data M = Z Int deriving Show"
"forM [1, 2, 3, 4] $ \\x -> do\n",
" print (x * x)\n",
" return (-x)"
], ],
"language": "python", "language": "python",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{ "prompt_number": 5
"metadata": {},
"output_type": "display_data",
"text": [
"1\n",
"4\n",
"9\n",
"16\n",
"[-1,-2,-3,-4]"
]
}
],
"prompt_number": 3
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "collapsed": false,
"input": [ "input": [
"1" "print (Z 3)"
], ],
"language": "python", "language": "python",
"metadata": {}, "metadata": {},
...@@ -104,22 +65,11 @@ ...@@ -104,22 +65,11 @@
"metadata": {}, "metadata": {},
"output_type": "display_data", "output_type": "display_data",
"text": [ "text": [
"1" "Z 3"
] ]
} }
], ],
"prompt_number": 4 "prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import Prelude"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 5
}, },
{ {
"cell_type": "code", "cell_type": "code",
...@@ -676,6 +626,58 @@ ...@@ -676,6 +626,58 @@
], ],
"prompt_number": 6 "prompt_number": 6
}, },
{
"cell_type": "code",
"collapsed": false,
"input": [
"data Thing = One Int\n",
"One 3"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<span style='color: red; font-style: italic;'>No instance for (Show Thing)<br/> arising from a use of `print'<br/>Possible fix:<br/> add an instance declaration for (Show Thing)</span>"
],
"metadata": {},
"output_type": "display_data",
"text": [
"No instance for (GHC.Show.Show :Interactive.Thing)\n",
" arising from a use of `System.IO.print'\n",
"Possible fix:\n",
" add an instance declaration for (GHC.Show.Show :Interactive.Thing)"
]
}
],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"data Thing = One Int deriving Show\n",
"One 3"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<span style='color: red; font-style: italic;'>No instance for (Show Thing)<br/> arising from a use of `print'<br/>Possible fix:<br/> add an instance declaration for (Show Thing)</span>"
],
"metadata": {},
"output_type": "display_data",
"text": [
"No instance for (GHC.Show.Show :Interactive.Thing)\n",
" arising from a use of `System.IO.print'\n",
"Possible fix:\n",
" add an instance declaration for (GHC.Show.Show :Interactive.Thing)"
]
}
],
"prompt_number": 2
},
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "collapsed": false,
......
...@@ -23,6 +23,7 @@ import System.Directory (removeFile, createDirectoryIfMissing, removeDirectoryRe ...@@ -23,6 +23,7 @@ import System.Directory (removeFile, createDirectoryIfMissing, removeDirectoryRe
import System.Posix.IO import System.Posix.IO
import System.IO (hGetChar, hFlush) import System.IO (hGetChar, hFlush)
import System.Random (getStdGen, randomRs) import System.Random (getStdGen, randomRs)
import Unsafe.Coerce
import NameSet import NameSet
import Name import Name
...@@ -347,9 +348,8 @@ evalCommand _ (Directive GetInfo str) = wrapExecution $ do ...@@ -347,9 +348,8 @@ evalCommand _ (Directive GetInfo str) = wrapExecution $ do
let strings = map (showSDocForUser flags unqual) outs let strings = map (showSDocForUser flags unqual) outs
return [plain $ intercalate "\n" strings] return [plain $ intercalate "\n" strings]
evalCommand output (Statement stmt) = do evalCommand output (Statement stmt) = wrapExecution $ do
write $ "Statement: " ++ stmt write $ "Statement: " ++ stmt
ghandle handler $ do
let outputter str = output False [plain str] let outputter str = output False [plain str]
(printed, result) <- capturedStatement outputter stmt (printed, result) <- capturedStatement outputter stmt
case result of case result of
...@@ -357,18 +357,9 @@ evalCommand output (Statement stmt) = do ...@@ -357,18 +357,9 @@ evalCommand output (Statement stmt) = do
dflags <- getSessionDynFlags dflags <- getSessionDynFlags
write $ "Names: " ++ show (map (showPpr dflags) names) write $ "Names: " ++ show (map (showPpr dflags) names)
let output = [plain printed | not . null $ strip printed] let output = [plain printed | not . null $ strip printed]
return (Success, output) return output
RunException exception -> do RunException exception -> throw exception
write $ "RunException: " ++ show exception RunBreak{} -> error "Should not break."
return (Failure, displayError $ show exception)
RunBreak{} ->
error "Should not break."
where
handler :: SomeException -> Interpreter (ErrorOccurred, [DisplayData])
handler exception = do
write $ concat ["BreakCom: ", show exception, "\nfrom statement:\n", stmt]
return (Failure, displayError $ show exception)
evalCommand output (Expression expr) = do evalCommand output (Expression expr) = do
-- Evaluate this expression as though it's just a statement. -- Evaluate this expression as though it's just a statement.
...@@ -381,7 +372,6 @@ evalCommand output (Expression expr) = do ...@@ -381,7 +372,6 @@ evalCommand output (Expression expr) = do
-- return False, and we just resort to plaintext. -- return False, and we just resort to plaintext.
let displayExpr = printf "(IHaskell.Display.display (%s))" expr let displayExpr = printf "(IHaskell.Display.display (%s))" expr
canRunDisplay <- attempt $ exprType displayExpr canRunDisplay <- attempt $ exprType displayExpr
write displayExpr
-- If evaluation failed, return the failure. If it was successful, we -- If evaluation failed, return the failure. If it was successful, we
-- may be able to use the IHaskellDisplay typeclass. -- may be able to use the IHaskellDisplay typeclass.
...@@ -430,7 +420,11 @@ evalCommand output (Expression expr) = do ...@@ -430,7 +420,11 @@ evalCommand output (Expression expr) = do
return displayData return displayData
evalCommand _ (Declaration decl) = wrapExecution $ runDecls decl >> return [] evalCommand _ (Declaration decl) = wrapExecution $ do
runDecls decl
-- Do not display any output
return []
evalCommand _ (ParseError loc err) = wrapExecution $ evalCommand _ (ParseError loc err) = wrapExecution $
return $ displayError $ formatParseError loc err return $ displayError $ formatParseError loc err
...@@ -477,6 +471,7 @@ capturedStatement output stmt = do ...@@ -477,6 +471,7 @@ capturedStatement output stmt = do
, voidpf "closeFd %s" writeVariable , voidpf "closeFd %s" writeVariable
, printf "let it = %s" itVariable , printf "let it = %s" itVariable
] ]
pipeExpr = printf "let %s = %s" (var "pipe_var_") readVariable
goStmt s = runStmt s RunToCompletion goStmt s = runStmt s RunToCompletion
...@@ -484,12 +479,21 @@ capturedStatement output stmt = do ...@@ -484,12 +479,21 @@ capturedStatement output stmt = do
forM_ initStmts goStmt forM_ initStmts goStmt
-- Get the pipe to read printed output from. -- Get the pipe to read printed output from.
dynPipe <- dynCompileExpr readVariable -- This is effectively the source code of dynCompileExpr from GHC API's
pipe <- case fromDynamic dynPipe of -- InteractiveEval. However, instead of using a `Dynamic` as an
Nothing -> error "Expecting lazy Bytestring" -- intermediary, it just directly reads the value. This is incredibly
Just fd -> liftIO $ fdToHandle fd -- unsafe! However, for some reason the `getContext` and `setContext`
-- required by dynCompileExpr (to import and clear Data.Dynamic) cause
-- Read from a file handle until we hit a delimieter or until we've read -- issues with data declarations being updated (e.g. it drops newer
-- versions of data declarations for older ones for unknown reasons).
-- First, compile down to an HValue.
Just (_, hValues, _) <- withSession $ liftIO . flip hscStmt pipeExpr
-- Then convert the HValue into an executable bit, and read the value.
pipe <- liftIO $ do
fd <- head <$> unsafeCoerce hValues
fdToHandle fd
-- Read from a file handle until we hit a delimiter or until we've read
-- as many characters as requested -- as many characters as requested
let let
readChars :: Handle -> String -> Int -> IO String readChars :: Handle -> String -> Int -> IO String
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment