Skip to content

Commit

Permalink
Add simple spreadsheet example
Browse files Browse the repository at this point in the history
  • Loading branch information
ollef committed Jul 1, 2019
1 parent 370d8c1 commit 06850da
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 148 deletions.
107 changes: 105 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,106 @@
# rock
# rock [![Hackage](https://img.shields.io/hackage/v/rock.svg)](https://hackage.haskell.org/package/rock)

A build system inspired by _Build systems à la carte_ and Haxl.
A build system inspired by [Build systems à la carte](https://www.microsoft.com/en-us/research/publication/build-systems-la-carte/) and [Haxl](http://hackage.haskell.org/package/haxl).

Used in [Sixten](https://github.com/ollef/sixten) and
[Sixty](https://github.com/ollef/sixty) to achieve incremental and query driven
compiler architectures.

# Example

```haskell
{-# language GADTs #-}
{-# language NoImplicitPrelude #-}
{-# language OverloadedStrings #-}
{-# language StandaloneDeriving #-}
{-# language TemplateHaskell #-}

import Protolude

import Data.GADT.Compare.TH (deriveGEq, deriveGCompare)
import qualified Rock

data Query a where
A :: Query Integer
B :: Query Integer
C :: Query Integer
D :: Query Integer

deriving instance Show (Query a)

deriveGEq ''Query
deriveGCompare ''Query

rules :: Rock.Rules Query
rules key = do
putText $ "Fetching " <> show key
case key of
A -> pure 10
B -> do
a <- Rock.fetch A
pure $ a + 20
C -> do
a <- Rock.fetch A
pure $ a + 30
D ->
(+) <$> Rock.fetch B <*> Rock.fetch C

main :: IO ()
main = do
do
putText "Running"
result <- Rock.runTask Rock.sequentially rules (Rock.fetch D)
print result
do
putText "Running with memoisation"
memoVar <- newMVar mempty
result <-
Rock.runTask
Rock.sequentially
(Rock.memoise memoVar rules)
(Rock.fetch D)
print result
do
putText "Running with memoisation using the parallel strategy"
memoVar <- newMVar mempty
result <-
Rock.runTask
Rock.inParallel
(Rock.memoise memoVar rules)
(Rock.fetch D)
print result
```

Prints

```
Running
Fetching D
Fetching B
Fetching A
Fetching C
Fetching A
70
Running with memoisation
Fetching D
Fetching B
Fetching A
Fetching C
70
Running with memoisation using the parallel strategy
Fetching D
Fetching C
Fetching B
Fetching A
70
```

# Related projects

* [Shake](http://hackage.haskell.org/package/shake)
* [Salsa](https://crates.io/crates/salsa)

# Contributions

... are very welcome, especially in the areas of documentation, examples,
testing, and benchmarking.
60 changes: 60 additions & 0 deletions examples/Spreadsheet.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{-# language GADTs #-}
{-# language NoImplicitPrelude #-}
{-# language OverloadedStrings #-}
{-# language StandaloneDeriving #-}
{-# language TemplateHaskell #-}

import Protolude

import Data.GADT.Compare.TH (deriveGEq, deriveGCompare)
import qualified Rock

data Query a where
A :: Query Integer
B :: Query Integer
C :: Query Integer
D :: Query Integer

deriving instance Show (Query a)

deriveGEq ''Query
deriveGCompare ''Query

rules :: Rock.Rules Query
rules key = do
putText $ "Fetching " <> show key
case key of
A -> pure 10
B -> do
a <- Rock.fetch A
pure $ a + 20
C -> do
a <- Rock.fetch A
pure $ a + 30
D ->
(+) <$> Rock.fetch B <*> Rock.fetch C

main :: IO ()
main = do
do
putText "Running"
result <- Rock.runTask Rock.sequentially rules (Rock.fetch D)
print result
do
putText "Running with memoisation"
memoVar <- newMVar mempty
result <-
Rock.runTask
Rock.sequentially
(Rock.memoise memoVar rules)
(Rock.fetch D)
print result
do
putText "Running with memoisation using the parallel strategy"
memoVar <- newMVar mempty
result <-
Rock.runTask
Rock.inParallel
(Rock.memoise memoVar rules)
(Rock.fetch D)
print result
16 changes: 15 additions & 1 deletion rock.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ library
exposed-modules:
Rock
Rock.Core
Rock.Examples
Rock.HashTag
Rock.Hashed
Rock.Traces
Expand All @@ -47,3 +46,18 @@ library
source-repository head
type: git
location: https://github.com/ollef/rock

flag examples
Description: "Build examples"
Default: False
Manual: True

executable rock-spreadsheet
if !flag(examples)
buildable: False
main-is: Spreadsheet.hs
ghc-options: -Wall
-threaded
hs-source-dirs: examples
default-language: Haskell2010
build-depends: base, rock, protolude, dependent-sum-template
145 changes: 0 additions & 145 deletions src/Rock/Examples.hs

This file was deleted.

0 comments on commit 06850da

Please sign in to comment.