Skip to content

Commit

Permalink
Add interpretation of statements.
Browse files Browse the repository at this point in the history
This allows us to bind values and functions,
in the familiar form `name <- stmt`.

Requires a version of `hint` that is not
available on hackage yet.
  • Loading branch information
HeinrichApfelmus committed Sep 6, 2017
1 parent 32a8e64 commit 83d4963
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 15 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

**v2.0.0** — Snapshot release.

* File format upgraded to version `0.2.0.0`. Conversion of old files happens *automatically* when loading them in the application.
* Upgrade file format to version `0.2.0.0`. Conversion of old files happens *automatically* when loading them in the application.
* Support different cell types:
1. 'code' cells -- for source code to be evaluated
2. 'text' cells -- plain text format
Expand All @@ -19,6 +19,10 @@

### `hyper`, `hyper-extra`, `hyper-haskell-server` packages

**v0.2.0.0**

* Add interpretation of statements. This allows us to bind values and functions to names, in the familiar form `name <- stmt` where `stmt` is an IO action with a result.

**v0.1.0.2** — Bump dependencies for compatibility with GHC 8.2

**v0.1.0.1** — Bump dependencies.
Expand Down
3 changes: 0 additions & 3 deletions docs/TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ Immediate

* Level β:

* Implement `runStmt` function to bind values while doing IO actions.
Cannot be implemented by evaluating expressions.

* Implement a way to evaluate cells concurrently.
We still want to be able to bind variables sequentially.
Using `forkIO` explicitely would do, but then we can't stop it again.
Expand Down
46 changes: 38 additions & 8 deletions haskell/hyper-haskell-server/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import Control.DeepSeq
import Control.Monad
import Control.Monad.Catch
import Control.Exception (AsyncException(UserInterrupt), evaluate)
import Data.List (groupBy)
import Data.Maybe (catMaybes)
import Data.Typeable
import Text.Read (readMaybe)
import System.Environment as System
Expand All @@ -23,8 +25,8 @@ import qualified Language.Haskell.Interpreter as Hint
import Data.Aeson (toJSON, (.=))
import qualified Data.Aeson as JSON
import qualified Data.ByteString.Char8 as B
import Data.Text (Text)
import Data.String (fromString)
import Data.Text as T (Text, concat)
import Data.String (fromString)
import Web.Scotty

-- Interpreter
Expand Down Expand Up @@ -99,12 +101,40 @@ setImports hint = run hint . Hint.setImports

loadFiles hint = run hint . Hint.loadModules . filter (not . null)

eval hint expr = run hint $ do
-- NOTE: We wrap results into an implicit call to Hyper.display
m <- Hint.interpret ("Hyper.displayIO " ++ Hint.parens expr) (as :: IO Graphic)
liftIO $ do
g <- m
evaluate (force g) -- See NOTE [EvaluateToNF]
-- | Evalute an input cell.
eval hint input = run hint $ do
mgs <- forM (parseStmts input) $ \line -> case line of
Expr expr -> do
-- NOTE: We wrap results into an implicit call to Hyper.display
m <- Hint.interpret ("Hyper.displayIO " ++ Hint.parens expr) (as :: IO Graphic)
liftIO $ do
g <- m
x <- evaluate (force g) -- See NOTE [EvaluateToNF]
return $ Just x
Bind var stmt -> do
Hint.runStmt (var ++ "<- " ++ Hint.parens stmt)
return Nothing
return . combineGraphics $ catMaybes mgs

combineGraphics :: [Graphic] -> Graphic
combineGraphics xs = Graphic { gHtml = T.concat $ map gHtml xs }

-- | Statements that we can evaluate.
type Var = String
data Stmt = Expr String | Bind Var String

-- | Parse an input cell into a list of statements to evaluate.
parseStmts :: String -> [Stmt]
parseStmts = map parseStmt . map unlines . groupByIndent . stripIndent . lines
where
indent xs = if null xs then 0 else length . takeWhile (== ' ') $ head xs
stripIndent xs = map (drop $ indent xs) xs
groupByIndent = groupBy (\x y -> indent [y] > 0)

parseStmt xs = case words xs of
(name:"<-":stmt) -> Bind name $ unwords stmt
_ -> Expr xs


{- NOTE [EvaluateToNF]
Expand Down
4 changes: 2 additions & 2 deletions haskell/hyper-haskell-server/hyper-haskell-server.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Name: hyper-haskell-server
Version: 0.1.0.2
Version: 0.2.0.0
Synopsis: Server back-end for the HyperHaskell graphical Haskell interpreter
Description:
This package is part of the /HyperHaskell/ project and provides
Expand All @@ -26,7 +26,7 @@ Executable hyper-haskell-server
, bytestring >= 0.9 && < 0.11
, deepseq >= 1.2 && < 1.5
, exceptions >= 0.6 && < 0.9
, hint >= 0.4 && < 0.8
, hint >= 0.7 && < 0.8
, hyper == 0.1.*
, text >= 0.11 && < 1.3
, transformers >= 0.3 && < 0.6
Expand Down
7 changes: 6 additions & 1 deletion haskell/stack.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
resolver: lts-5.8
extra-deps:
- hint-0.6.0
# we need a custom version of hint
# - hint-0.6.0
# diagrams and SVG
- diagrams-core-1.3.0.8
- diagrams-lib-1.3.1.3
Expand All @@ -20,5 +21,9 @@ packages:
- hyper
- hyper-extra
- hyper-haskell-server
- location:
git: https://github.com/mvdan/hint
commit: f10e357d8249bc4b8a71e2c211bebc202549c765
extra-dep: true
# - location: lib/hint-0.4.2.3-patched
# extra-dep: true
20 changes: 20 additions & 0 deletions worksheets/Prelude.hhs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,26 @@
{
"cell_type": "code",
"source": "let fibs = 1 : 1 : zipWith (+) fibs (tail fibs) in take 20 fibs :: [Int]"
},
{
"cell_type": "text",
"source": "It is also possible to bind new variables in the IO monad, via a statement.\n"
},
{
"cell_type": "code",
"source": "test <- return \"Hello\""
},
{
"cell_type": "code",
"source": "test"
},
{
"cell_type": "text",
"source": "Input cells can span multiple lines. Statements are executed in sequence.\n"
},
{
"cell_type": "code",
"source": "test <- return \"Multiple lines\"\ntest"
}
],
"importModules": "Prelude",
Expand Down

0 comments on commit 83d4963

Please sign in to comment.