Tomáš Petříček, 309 (3rd floor)
petricek@d3s.mff.cuni.cz
https://tomasp.net | @tomaspetricek
https://d3s.mff.cuni.cz/teaching/nprg077

Encoded using "drag-down"

Simple, but can do a lot...




(Jakubovic et al., 2023)
What matters about stateful interactive systems?



Drag-down for formulas makes abstraction easy
You only ever work with concrete values
Always see sample inputs & verify sample outputs






Exploratory - easy to fiddle with data
Live - you see results (almost) immediately


Unimate industrial robot (1961)
Program by moving
the robotic hand
Macro recording
but done right





Humans are good at working with space
Programs are not typically spatial...
Grid is limiting, but powerful concept



Demos by Bret Victor
Learnable Programming: Designing a programming system for understanding programs (online)



In what order to evaluate sheet?
Avoid evaluating
a cell repeatedly!
What to re-evaluate
when cells change?


Dependencies via cell
and range references
Cyclic dependencies
Excel does a fixed maximal number of iterations
Explicit or implicit in code
Graph data structure vs. event listeners





List comprehensions with the yield keyword
let worldInfo =
[ yield addr "A1", Const(String "Continent")
yield addr "B1", Const(String "Population (thousands)")
for i, (cont, pop) in Seq.indexed continents do
yield addr ("A"+string(i+2)), Const(String cont)
yield addr ("B"+string(i+2)), Const(Number pop) ]
yield adds another item to the listfor and other constructs to write generatorsSeq.indexed trick to get item index

// Decares event value
let evt = Event<int>()
// Trigger event
evt.Trigger(1)
evt.Trigger(2)
evt.Trigger(3)
// Object for listening
evt.Publish
// Listen and print
evt.Publish.Add(fun n ->
printfn "Got: %d" n)
Regular F# objects
Not special constructs
Corresond to IObservable in C#
Add and remove handlers using AddHandler and RemoveHandler


If you know C#, you can use other options too!
let demo () =
let f = Path.GetTempFileName() + ".html"
use wr = new StreamWriter(File.OpenWrite(f))
wr.Write("""<html><body><h1>Hello world!</h1></body></html>""")
wr.Close()
Process.Start(f)
GetTempFileName gives you a file in TEMP folderuse to make sure stream gets closed on errorProcess.Start can (sometimes) open files too

// In column, row format
// e.g. A1 becomes (1, 1)
type Address = int * int
// Note error is a value!
type Value =
| Number of int
| String of string
| Error of string
// Operators are functions
type Expr =
| Const of Value
| Reference of Address
| Function of string * Expr list
// Using immutable F# map
type Sheet = Map<Address, Expr>
Standard ML-like expression language
References (instead
of variables) are evaluated recursively
Sheet maps (filled) addresses to expressions

// Expression and value are
// mutable. Updated triggered
// when they change.
type CellNode =
{ mutable Value : Value
mutable Expr : Expr
Updated : Event<unit> }
// Immutable map
// of mutable cells
type LiveSheet =
Map<Address, CellNode>
Value evaluated
on creation which prevents circular refs
Expression stored
"drag down" expansion
Updated event
to notify of changes

type Value = // (...)
| Array of Value list
type Expr = // (...)
| Range of Address * Address
type Index = Fixed of int | Normal of int
type RawAddress = int * int
type Address = Index * Index


Simple expression evaluator
With grid references by cell address
"Drag down" formula expanding
Relocating relative references in formula
Reactive event-based structure
Refactoring code to use graph nodes
Reactive event-based computation
Adding update event handling
Rendering sheets as HTML pages
First step towards a user interface

Absolute and relative addresses
Alongside with improved "drag down"
Adding range selection and array values
Required for the SUM function
Adding change visualization
Tracking and showing what has changed
Full support for live editing
Updating dependencies in the dependency graph



A tiny incremental spreadsheet system
Tomáš Petříček, 309 (3rd floor)
petricek@d3s.mff.cuni.cz
https://tomasp.net | @tomaspetricek
https://d3s.mff.cuni.cz/teaching/nprg077

https://direct.mit.edu/books/book/3071/Spreadsheet-Implementation-TechnologyBasics-and (hard to get...)
https://www.theregister.com/2020/12/04/microsoft_excel_lambda/
https://www.felienne.com/archives/2974
https://arxiv.org/ftp/arxiv/papers/1807/1807.08578.pdf
https://theconversation.com/the-reinhart-rogoff-error-or-how-not-to-excel-at-economics-13646
https://genomebiology.biomedcentral.com/articles/10.1186/s13059-016-1044-7
https://advait.org/publications-web/sarkar-2018-spreadsheet-learning
