Skip to content

Commit e4a3df1

Browse files
The Terminal keeps every cell's value in run, and back n counts from the end (#1427)
#601: history beyond the last value. F# Interactive binds `it` to the value of the last expression cell and nothing further back, and the Terminal's readme did not mention even that. Now the pre-run code declares `run`, a list of every value a cell has produced, oldest first, and `back n` for the n-th from the end; after every cell that displayed a value the library submits `run.Add (box it)` to the kernel, which fails harmlessly before `run` exists, during the warm-up cell. A cell with no value adds nothing; a question about the history is a cell with a value and is remembered too. Declared before the arithmetic operators are opened, since under them `run.Count - n` would build an expression rather than an index. Closes #601. Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent afe7966 commit e4a3df1

4 files changed

Lines changed: 59 additions & 1 deletion

File tree

‎Sources/Terminal/AngouriMath.Terminal.Lib/FSharpInteractive.fs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ let private objectDecode (s : string Option) =
3939
JsonSerializer.Deserialize<ExecutionResult> (latex.[EncodingLatexPrefix.Length..], options) |> nonNull
4040
| _ -> VoidSuccess
4141

42+
/// The cell that appends the last value to `run`, the history PreRunCode declares. `it` is
43+
/// what F# Interactive binds the value of an expression cell to, so this is sent after every
44+
/// cell that displayed a value, and it fails harmlessly -- and silently -- before `run` exists,
45+
/// which is only during the warm-up cell.
46+
/// https://github.com/asc-community/AngouriMath/issues/601
47+
let private rememberCode = "run.Add (box it)"
48+
4249
let execute (kernel : FSharpKernel) code =
4350
let submitCode = SubmitCode code
4451
let computed = (kernel.SendAsync submitCode).Result // Yes. It's Result.
@@ -56,6 +63,11 @@ let execute (kernel : FSharpKernel) code =
5663
nonVoidResponse <- (Seq.head display.FormattedValues).Value |> Some
5764
| _ -> ()
5865

66+
match res, nonVoidResponse with
67+
| Some (PlainTextSuccess _ | LatexSuccess _), Some _ when code <> rememberCode ->
68+
(kernel.SendAsync (SubmitCode rememberCode)).Result |> ignore
69+
| _ -> ()
70+
5971
match res with
6072
| None -> EndOfFile
6173
| Some res -> res

‎Sources/Terminal/AngouriMath.Terminal.Lib/PreRunCode.fs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ let eval (x : obj) =
1919
| :? Entity.Number.Complex as cx -> cx.RealPart.EDecimal.ToString() + "" + "" + cx.ImaginaryPart.EDecimal.ToString() + ""i""
2020
| other -> (evaled other).ToString()
2121
22+
/// Every value a cell has produced, oldest first: `run.[0]` is the first, `run.[run.Count - 1]`
23+
/// the last, and `back 1` the last, `back 2` the one before it. F# Interactive's own `it` is
24+
/// the last value only. Declared before the arithmetic operators are opened, since under them
25+
/// `run.Count - n` would build an expression rather than an index. https://github.com/asc-community/AngouriMath/issues/601
26+
let run = ResizeArray<obj> ()
27+
28+
let back (n : int) = run.[run.Count - n]
29+
2230
open AngouriMath.Interactive.ArithmeticOperators
2331
2432
let x = symbol ""x""

‎Sources/Terminal/readme.md‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,25 @@ To uninstall it, run
2727
dotnet tool uninstall --global AngouriMath.Terminal
2828
```
2929

30+
31+
## Earlier results
32+
33+
Every value a cell produces is kept: `it` is the last one, as in F# Interactive, `run` is all of
34+
them oldest first, and `back n` counts from the end.
35+
36+
```
37+
[...] 1 + 10
38+
11
39+
40+
[...] 2 * 10
41+
20
42+
43+
[...] back 1
44+
20
45+
46+
[...] run.[0]
47+
11
48+
```
49+
50+
A cell with no value — a `let` — adds nothing, and a question about the history is itself a cell
51+
with a value, so it is remembered too.

‎Sources/Tests/TerminalUnitTests/Tests.fs‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module AngouriMath.Terminal.Tests
1+
module AngouriMath.Terminal.Tests
22

33
open System
44
open Xunit
@@ -54,6 +54,22 @@ let executeNewWithAm code =
5454
execute kernel code
5555

5656

57+
// Every value a cell produces is kept in `run`, oldest first, and `back n` counts from the end;
58+
// a cell without a value (a `let`) adds nothing. https://github.com/asc-community/AngouriMath/issues/601
59+
[<Fact>]
60+
let ``The values of earlier cells are kept in run`` () =
61+
let kernel = ``Create kernel or fail`` ()
62+
enableAngouriMath kernel |> ignore
63+
execute kernel "1 + 10" |> ignore
64+
execute kernel "let unused = 4" |> ignore
65+
execute kernel "2 * 10" |> ignore
66+
Assert.Equal(PlainTextSuccess "2", execute kernel "run.Count")
67+
// Asking is itself a cell with a value, so each question is remembered too.
68+
Assert.Equal(LatexSuccess ("11", "11"), execute kernel "run.[0]")
69+
Assert.Equal(LatexSuccess ("20", "20"), execute kernel "run.[1]")
70+
Assert.Equal(LatexSuccess ("11", "11"), execute kernel "back 5")
71+
Assert.Equal(PlainTextSuccess "6", execute kernel "run.Count")
72+
5773
[<Fact>]
5874
let ``Test 1`` () =
5975
executeNewWithAm "1 + 1"

0 commit comments

Comments
 (0)