add full DSl example to readme

This commit is contained in:
NunoSempere 2024-06-10 11:06:31 -04:00
parent a4263d0765
commit 260d39e131
2 changed files with 53 additions and 40 deletions

View File

@ -13,19 +13,19 @@ Here is an example
``` ```
$ go run f.go $ go run f.go
5000000 12000000 5000000 12000000
=> 5000000.0 12000000.0 => 5.0M 12.0M
0.002 0.01 * beta 1 200
=> 13859.5 86583.4 1.9K 123.1K
30 180 * 30 180
=> 706832.8 9167656.0 122.9K 11.7M
/ 48 52 / 48 52
=> 14139.1 183614.7 2.5K 234.6K
/ 5 6 / 5 6
=> 2573.1 33632.0 448.8 43.0K
/ 6 8 / 6 8
=> 368.4 4893.5 64.5 6.2K
/ 60 / 60
=> 6.1 81.6 1.1 103.7
``` ```
Perhaps this example is more understandable with comments and better units: Perhaps this example is more understandable with comments and better units:
@ -33,25 +33,46 @@ Perhaps this example is more understandable with comments and better units:
``` ```
$ sed -u "s|#.*||" | sed -u 's|M|000000|g' | go run f.go $ sed -u "s|#.*||" | sed -u 's|M|000000|g' | go run f.go
5M 12M # number of people living in Chicago 5M 12M # number of people living in Chicago
=> 5000000.0 12000000.0 => 5.0M 12.0M
0.002 0.01 # fraction of people that have a piano * beta 1 200 # fraction of people that have a piano
=> 13859.5 86583.4 1.9K 123.1K
30 180 # minutes it takes to tune a piano, including travel time 30 180 # minutes it takes to tune a piano, including travel time
=> 706832.8 9167656.0 122.9K 11.7M
/ 48 52 # weeks a year that piano tuners work for / 48 52 # weeks a year pianotuners work for
=> 14139.1 183614.7 2.5K 234.6K
/ 5 6 # days a week in which piano tuners work / 6 8 # hours a day
=> 2573.1 33632.0 353.9 34.1K
/ 6 8 # hours a day in which piano tuners work
=> 368.4 4893.5
/ 60 # minutes to an hour / 60 # minutes to an hour
=> 6.1 81.6 5.9 568.3
# ^ piano tuners in Chicago =: piano_tuners_in_Chicago
piano_tuners_in_Chicago => 5.9 568.3
``` ```
You can see a recording in action here: Here is instead an example using beta distributions and variables:
[![asciicast](https://asciinema.org/a/fygBtg0XDc1iVajArdQn9b9CA.svg)](https://asciinema.org/a/fygBtg0XDc1iVajArdQn9b9CA) ```
1 2
=> 1.0 2.0
* 1_000_000_000
=> 1000.0M 2.0B
=: x # assign to variable
x => 1000.0M 2.0B
. # clear the stack, i.e., make it be 1
beta 1 2
=> beta 1.0 2.0
beta 12 300
=> beta 13.0 302.0
=. y # assign to variable and clear the stack (return it to 1)
y => beta 13.0 302.0
x
=> 1000.0M 2.0B
* y
=> samples 31.3M 98.2M
```
The difference between `=: x` and `=. y` is that `=.` clears the stack after the assignment.
## Installation ## Installation
@ -89,7 +110,9 @@ Note that these sed commands are just hacks, and won't parse e.g., `3.5K` correc
## Tips & tricks ## Tips & tricks
Conceptually clearer to have all the multiplications first and then all the divisions - It's conceptually clearer to have all the multiplications first and then all the divisions
- Sums and divisions now also supported
- For things between 0 and 1, consider using a beta distribution
## Roadmap ## Roadmap
@ -116,20 +139,9 @@ Conceptually clearer to have all the multiplications first and then all the divi
- Maps - Maps
- Joint types - Joint types
- Enums - Enums
- [ ] Fix correlation problem, by spinning up a new randomness thing every time some serial computation is done. - [x] Fix correlation problem, by spinning up a new randomness thing every time some serial computation is done.
- [ ] Dump samples to file
- [ ] Represent samples/statistics in some other way
- [ ] Perhaps use qsort rather than full sorting
Some possible syntax for a more expressive stack-based DSL Some possible syntax for a more expressive stack-based DSL (now implemented)
```
1B to 20B
* 1 to 100
/ beta 1 2 # or b 1 2
=: x # content of the stack at this point saved into x
1 to 10
10 to 100
=: y # content of the stack at this point saved into y
x # put x on the stack
- y # substract y from the content of the stack. Requires interpreting x and y as list of samples
```

1
f.go
View File

@ -301,6 +301,7 @@ func prettyPrintDist(dist Dist) {
low := tmp_xs[low_int] low := tmp_xs[low_int]
high_int := N_SAMPLES * 19 / 20 high_int := N_SAMPLES * 19 / 20
high := tmp_xs[high_int] high := tmp_xs[high_int]
fmt.Printf("=> samples ")
prettyPrint2Floats(low, high) prettyPrint2Floats(low, high)
case Beta: case Beta:
fmt.Printf("=> beta ") fmt.Printf("=> beta ")