Skip to content

Commit

Permalink
Merge pull request #837 from explorable-viz/linked-inputs
Browse files Browse the repository at this point in the history
Move `slicing/motivating-example.fld` to `linked-inputs/water.fld` and run using (in-progress) linked inputs test helper
  • Loading branch information
rolyp authored Nov 8, 2023
2 parents 39578a2 + 2c7b930 commit c522488
Show file tree
Hide file tree
Showing 16 changed files with 102 additions and 181 deletions.
3 changes: 0 additions & 3 deletions fluid/example/linked-inputs/bubble-chart.fld

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
let country_water countries_list cities c_name =
let city_water = sum [city.water | country <- countries_list
, c_name == country.name
, city <- cities
, elem city.name country.cities];
farm_water = sum [ country.farms * 3 | country <- countries_list, country.name == c_name ]
in city_water + farm_water;
let all_countries = [{name: "Germany", cities: ["Berlin", "Hamburg", "Munich"], farms: 100},
{name: "UK", cities: ["London", "Birmingham", "Manchester"], farms: 200}];
let all_cities = [
all_cities = [
{name: "Berlin", water: 130},
{name: "Munich", water: 80},
{name: "Hamburg", water: 60},
{name: "London", water: 200},
{name: "Birmingham", water: 50},
{name: "Manchester", water: 35}
];
let all_waters countries cities =
let country_water countries_list cities c_name =
let city_water = sum [city.water | country <- countries_list
, c_name == country.name
, city <- cities
, elem city.name country.cities];
farm_water = sum [ country.farms * 3 | country <- countries_list, country.name == c_name ]
in city_water + farm_water;
let all_waters countries cities =
let c_names = [country.name | country <- countries]
in map (country_water countries cities) c_names
in all_waters all_countries all_cities
4 changes: 2 additions & 2 deletions fluid/example/linked-outputs/water-bar-chart.fld
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
let totalFor country =
let totalFor country =
sum [row.cities + row.farms + row.industry + row.energy | row <- data, row.country == country];
let countryData = [ {x: country.country, y: totalFor country.country}
| country <- data]
in BarChart {
caption: "Total Water Consumption By Country",
data: countryData
}
}
2 changes: 1 addition & 1 deletion fluid/example/linked-outputs/water-consumption-data.fld
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[
{country: "Germany", cities: 900, farms: 150, industry:500 , energy: 450, popMil: 81},
{country: "UK", cities: 800, farms: 200, industry: 400, energy: 700, popMil: 67}
]
]
2 changes: 1 addition & 1 deletion fluid/example/linked-outputs/water-ratio-chart.fld
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ let countryData = [{x: country.cities, y: country.farms, z: country.popMil} | co
in BubbleChart {
caption: "Ratio of farmland consumption to population in millions",
data: countryData
}
}
16 changes: 0 additions & 16 deletions fluid/example/slicing/motivating-example.expect.fld

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"clean-bench": "rm -rf dist/benches && mkdir -p dist/benches && cp -r fluid dist/benches && cp web/index.html dist/benches && cp -r web/css dist/benches",
"clean-examples": "rm -rf dist/examples && mkdir -p dist/examples && cp -r fluid dist/examples && cp web/index.html dist/examples && cp -r web/css dist/examples",
"build-examples": "yarn clean-examples && spago build --purs-args '--strict --censor-codes=UserDefinedWarning' && purs-backend-es bundle-app --main Example.Example --to dist/examples/app.js",
"build-tests": "yarn clean-tests && spago build --purs-args '--strict --censor-codes=UserDefinedWarning' && purs-backend-es bundle-app --main Test.Main --to dist/tests/app.js",
"build-tests": "yarn clean-tests && spago build --purs-args '--strict --censor-codes=UserDefinedWarning' && purs-backend-es bundle-app --main Test.Test --to dist/tests/app.js",
"build-bench": "yarn clean-bench && spago build --purs-args '--strict --censor-codes=UserDefinedWarning' && purs-backend-es bundle-app --main Test.Benchmark --to dist/benches/app.js",
"tests": "karma start karma.conf.tests.js",
"bench": "npx http-serve dist/benches -a 127.0.0.1",
Expand Down
47 changes: 3 additions & 44 deletions src/App.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,10 @@ module App where

import Prelude hiding (absurd)

import App.CodeMirror (addEditorView)
import App.Fig (Fig, FigSpec, LinkedOutputsFig, LinkedOutputsFigSpec, drawCode, drawFig, drawLinkedOutputsFig, loadFig, loadLinkedOutputsFig)
import Data.Either (Either(..))
import Data.Newtype (unwrap)
import Data.Traversable (sequence, sequence_)
import App.Fig (FigSpec, LinkedOutputsFigSpec, drawFiles, drawFigs, drawLinkedOutputsFigs, loadFig, loadLinkedOutputsFig)
import Effect (Effect)
import Effect.Aff (Aff, runAff_)
import Effect.Console (log)
import Lattice (botOf)
import Module (File(..), Folder(..), loadFile)
import Util (type (×), (×), Endo)
import Module (File(..), Folder(..))
import Util ((×))

linkedOutputsFig1 :: LinkedOutputsFigSpec
linkedOutputsFig1 =
Expand All @@ -37,40 +30,6 @@ fig2 =
, xs: [ "image", "filter" ]
}

codeMirrorDiv :: Endo String
codeMirrorDiv = ("codemirror-" <> _)

drawLinkedOutputsFigs :: Array (Aff LinkedOutputsFig) -> Effect Unit
drawLinkedOutputsFigs loadFigs =
flip runAff_ (sequence loadFigs)
case _ of
Left err -> log $ show err
Right figs -> do
sequence_ $ figs <#> \fig -> do
ed1 <- addEditorView $ codeMirrorDiv $ unwrap (fig.spec.file1)
ed2 <- addEditorView $ codeMirrorDiv $ unwrap (fig.spec.file2)
ed3 <- addEditorView $ codeMirrorDiv $ unwrap (fig.spec.dataFile)
drawLinkedOutputsFig fig ed1 ed2 ed3 (Left $ botOf)

drawFigs :: Array (Aff Fig) -> Effect Unit
drawFigs loadFigs =
flip runAff_ (sequence loadFigs)
case _ of
Left err -> log $ show err
Right figs -> sequence_ $ figs <#> \fig -> do
ed <- addEditorView $ codeMirrorDiv fig.spec.divId
drawFig fig ed botOf

drawFiles :: Array (Folder × File) -> Effect Unit
drawFiles files =
sequence_ $ files <#> \(folder × file) ->
flip runAff_ (loadFile folder file)
case _ of
Left err -> log $ show err
Right src -> do
ed <- addEditorView $ codeMirrorDiv $ unwrap file
drawCode ed src

main :: Effect Unit
main = do
drawFiles [ Folder "fluid/lib" × File "convolution" ]
Expand Down
4 changes: 2 additions & 2 deletions src/App/BarChart.purs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ newtype BarChartRecord = BarChartRecord { x :: String × 𝔹, y :: Number ×

foreign import drawBarChart :: Renderer BarChart

instance Reflect (Dict (Val Boolean)) BarChartRecord where
instance Reflect (Dict (Val 𝔹)) BarChartRecord where
from r = BarChartRecord
{ x: string.unpack (get f_x r)
, y: get_intOrNumber f_y r
}

instance Reflect (Dict (Val Boolean)) BarChart where
instance Reflect (Dict (Val 𝔹)) BarChart where
from r = BarChart
{ caption: string.unpack (get f_caption r)
, data: record from <$> from (get f_data r)
Expand Down
2 changes: 1 addition & 1 deletion src/App/BubbleChart.purs
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ bubbleChartHandler ev = toggleDot $ unsafeDotIndex $ target ev
let
tgt = definitely' $ tgt_opt
in
(unsafeCoerce tgt).__data__ ! 0
(unsafeCoerce tgt).__data__ ! 0
37 changes: 24 additions & 13 deletions src/App/Fig.purs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ type FigSpec =
type Fig =
{ spec :: FigSpec
, γ0 :: Env 𝔹 -- ambient env
, γ :: Env 𝔹 -- local env (loaded dataset, if any, plus additional let bindings at beginning of ex)
, γ :: Env 𝔹 -- loaded dataset, if any, plus additional let bindings at beginning of ex
, s0 :: S.Expr 𝔹 -- program that was originally "split"
, s :: S.Expr 𝔹 -- body of example
, e :: Expr 𝔹 -- desugared s
Expand All @@ -117,7 +117,12 @@ type LinkedOutputsFigSpec =
, x :: Var
}

type LinkedInputsFigSpec = FigSpec
type LinkedInputsFigSpec =
{ divId :: HTMLId
, file :: File
, x1 :: Var -- variables to be considered "inputs"
, x2 :: Var
}

type LinkedOutputsFig =
{ spec :: LinkedOutputsFigSpec
Expand All @@ -137,7 +142,7 @@ type LinkedOutputsFig =
type LinkedInputsFig =
{ spec :: LinkedInputsFigSpec
, γ0 :: Env 𝔹 -- ambient env
, γ :: Env 𝔹 -- local env (additional let bindings at beginning of ex)
, γ :: Env 𝔹 -- additional let bindings at beginning of ex; must include vars defined in spec
, s0 :: S.Expr 𝔹 -- program that was originally "split"
, s :: S.Expr 𝔹 -- body of example
, e :: Expr 𝔹
Expand All @@ -146,13 +151,13 @@ type LinkedInputsFig =
}

type LinkedOutputsResult =
{ v' :: Val 𝔹 -- will represent either v1' or v2'
, v0' :: Val 𝔹
{ v' :: Val 𝔹 -- selection on other output
, v0' :: Val 𝔹 -- selection that arose on shared input
}

type LinkedInputsResult =
{ v' :: Val 𝔹 -- will represent either v1' or v2'
, v0' :: Val 𝔹
{ v' :: Val 𝔹 -- selection on other input
-- will also want selection that arose on shared output
}

drawLinkedOutputsFig :: LinkedOutputsFig -> EditorView -> EditorView -> EditorView -> Selector Val + Selector Val -> Effect Unit
Expand Down Expand Up @@ -186,10 +191,6 @@ drawLinkedOutputsFigs loadFigs =
ed3 <- addEditorView $ codeMirrorDiv $ unwrap (fig.spec.dataFile)
drawLinkedOutputsFig fig ed1 ed2 ed3 (Left $ botOf)

drawCode :: EditorView -> String -> Effect Unit
drawCode ed s =
dispatch ed =<< update ed.state [ { changes: { from: 0, to: getContentsLength ed, insert: s } } ]

drawFig :: Fig -> EditorView -> Selector Val -> Effect Unit
drawFig fig@{ spec: { divId }, s0 } ed δv = do
log $ "Redrawing " <> divId
Expand All @@ -208,6 +209,10 @@ drawFigs loadFigs =
ed <- addEditorView $ codeMirrorDiv fig.spec.divId
drawFig fig ed botOf

drawCode :: EditorView -> String -> Effect Unit
drawCode ed s =
dispatch ed =<< update ed.state [ { changes: { from: 0, to: getContentsLength ed, insert: s } } ]

drawFiles :: Array (Folder × File) -> Effect Unit
drawFiles files =
sequence_ $ files <#> \(folder × file) ->
Expand Down Expand Up @@ -239,11 +244,17 @@ linkedOutputsResult x γ0γ e1 e2 t1 _ v1 = do
γ0γ' × _ = evalBwd (erase <$> γ0γ) (erase e1) v1 t1
γ0' × γ' = append_inv (S.singleton x) γ0γ'
v0' <- lookup x γ' # orElse absurd
-- make γ0 and e2 fully available; γ0 was previously too big to operate on, so we use
-- (topOf γ0) combined with negation of the dataset environment slice
-- make γ0 and e2 fully available
_ × v2' <- eval (neg ((botOf <$> γ0') <+> γ')) (topOf e2) true
pure { v': neg v2', v0' }

linkedInputsResult :: forall m. MonadError Error m => Var -> Var -> Env 𝔹 -> Expr 𝔹 -> Trace -> Selector Val -> m LinkedInputsResult
linkedInputsResult x1 x2 γ _ _ _ = do
-- TODO: replace with environment selection; fwd De Morgan; bwd; retrieve x2 from env
_ <- lookup x1 γ # orElse absurd
v2 <- lookup x2 γ # orElse absurd
pure { v': v2 }

loadFig :: forall m. FigSpec -> AffError m Fig
loadFig spec@{ file } = do
{ γ: γ' } <- defaultImports >>= initialConfig
Expand Down
31 changes: 6 additions & 25 deletions src/PLDI2024.purs
Original file line number Diff line number Diff line change
@@ -1,38 +1,19 @@
module PLDI2024 where

import Prelude hiding (absurd)

import App.Fig (FigSpec, LinkedOutputsFigSpec, drawFiles, drawFigs, drawLinkedOutputsFigs, loadFig, loadLinkedOutputsFig)
import Prelude
import App.Fig (LinkedOutputsFigSpec, drawLinkedOutputsFigs, loadLinkedOutputsFig)
import Effect (Effect)
import Module (File(..), Folder(..))
import Util ((×))
import Module (File(..))

-- Will changes to PLDI figures but for now same as those on f.luid.org main page.
linkedOutputsFig1 :: LinkedOutputsFigSpec
linkedOutputsFig1 =
waterFig :: LinkedOutputsFigSpec
waterFig =
{ divId: "fig-1"
, file1: File "water-bar-chart"
, file2: File "water-ratio-chart"
, dataFile: File "water-consumption-data"
, x: "data"
}

fig1 :: FigSpec
fig1 =
{ divId: "fig-conv-1"
, file: File "slicing/convolution/emboss"
, xs: [ "image", "filter" ]
}

fig2 :: FigSpec
fig2 =
{ divId: "fig-conv-2"
, file: File "slicing/convolution/emboss-wrap"
, xs: [ "image", "filter" ]
}

main :: Effect Unit
main = do
drawFiles [ Folder "fluid/lib" × File "convolution" ]
drawFigs [ loadFig fig1, loadFig fig2 ]
drawLinkedOutputsFigs [ loadLinkedOutputsFig linkedOutputsFig1 ]
drawLinkedOutputsFigs [ loadLinkedOutputsFig waterFig ]
Loading

0 comments on commit c522488

Please sign in to comment.