Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Examples/contracts.ikr
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
(contract raw operative (any) any pure #t)

(printf "double={0}\n" (double 21))
(show (raw (+ 1 2)))
(print (show (raw (+ 1 2))))
2 changes: 1 addition & 1 deletion Examples/vau-dotnet.ikr
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
(vau (exp) env
(begin
(write " >> ")
(show exp)
(print (show exp))
(writeln "")
(eval exp env))))

Expand Down
4 changes: 2 additions & 2 deletions Examples/zipper.ikr
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
(define tree1 '(a (b) (c (d 1 2)) e))
(define tree2 '(z (u) (v (w 10 12)) y))

(depth-first (lambda (node) (begin (show node) (print "\n") #f)) tree1)
(depth-first (lambda (node) (begin (print (show node)) (print "\n") #f)) tree1)

(provide! (zipper zipper? z-curr-node z-k)

Expand Down Expand Up @@ -50,7 +50,7 @@
(if
(zipper? x)
(begin
(show (z-curr-node x))
(print (show (z-curr-node x)))
(print "\n")
(loop ((z-k x) #f)))
#f))))
Expand Down
13 changes: 7 additions & 6 deletions IronKernel.Runtime/Interop.fs
Original file line number Diff line number Diff line change
Expand Up @@ -328,11 +328,12 @@
bounceContinue env cont Inert
| _ -> signal cont (NumArgs(2,prms))

/// Render a value's printed representation as a string. Pure --
/// rendering grants no host authority, so `show` exists under every
/// profile; writing is `print`, `write`, and the ports' job. It used
/// to write to the console itself and return inert, which made the
/// natural `(print (show x))` an arity error on print.
let show env cont (prms : LispVal list) =
match prms with
| _ when not (has HostIO env) ->
signal cont (CapabilityDenied "host output requires HostIO")
| h :: _ ->
System.Console.Write(showVal h)
bounceContinue env cont Inert
| [] -> signal cont (NumArgs(1, []))
| [value] -> bounceContinue env cont (Obj(showVal value :> obj))
| _ -> signal cont (NumArgs(1, prms))
4 changes: 3 additions & 1 deletion IronKernel.Runtime/Runtime.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2754,6 +2754,8 @@
Some(certifiedVariadicApplicative name [NumberShape; NumberShape] 2 NumberShape)
| "<" | "<=" | ">" ->
Some(certifiedApplicative name [NumberShape; NumberShape] BooleanShape)
| "show" ->
Some(certifiedApplicative name [AnyShape] StringShape)
| _ -> None
let value =
match contract with
Expand Down Expand Up @@ -2785,7 +2787,7 @@
(name <> "load" || Set.contains SourceLoading capabilities)
&& (not (Set.contains name
(Set.ofList
[ "print"; "printf"; "show"; "read"; "write"
[ "print"; "printf"; "read"; "write"
"close-input-file"; "close-output-file"
"get-current-input-port"; "get-current-output-port"
"with-input-from-file"; "with-output-to-file"
Expand Down
16 changes: 16 additions & 0 deletions IronKernel.Tests/CliTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -669,3 +669,19 @@ let ``repl completion extends the symbol at the cursor`` () =
// Control: a bare tab must not dump the whole environment.
let _, none = completionCandidates env "(" 1
Assert.Empty none

[<Fact>]
let ``print of show renders and writes -- the print-show regression`` () =
match bootstrapEnv () with
| Choice1Of2 error -> failwith (showError error)
| Choice2Of2 env ->
let original = Console.Out
use writer = new StringWriter()
Console.SetOut writer
try
match runSource env "print-show.ikr" "(print (show (list 1 2)))\n(print (show (list 3 4)))\n" with
| Choice1Of2 error -> failwith (showError error)
| Choice2Of2 _ -> ()
finally
Console.SetOut original
Assert.Equal("(1 2)(3 4)", writer.ToString())
23 changes: 23 additions & 0 deletions IronKernel.Tests/InteropTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,26 @@ let ``CLR boolean results are Kernel booleans`` () =
"""(. System.Char IsDigit (. "x" get_Chars 0))""", Bool false
"""(.get System.Environment Is64BitProcess)""", Bool System.Environment.Is64BitProcess
]

[<Fact>]
let ``show renders a value to a string`` () =
evalSessionKernel [
"(show 5)", Obj "5"
"(show (list 1 2))", Obj "(1 2)"
// The idiom that used to fail: show wrote to the console itself and
// returned inert, so print then rejected #inert with an arity error.
"(print (show (list 1 2)))", Inert
]

[<Fact>]
let ``show requires no authority`` () =
IronKernel.RuntimeSourceServices.configure ()
IronKernel.Compiler.installBodyCompiler ()
let env = makePrimitiveBindingsForProfile Minimal
match evalIn env "(show 42)" with
| Obj value -> Assert.Equal("42", string value)
| other -> failwithf "unexpected show result: %s" (showVal other)
// Control: writing is still authority the minimal profile lacks.
match evalIn env "(print \"x\")" with
| Status message -> Assert.Contains("print", message)
| other -> failwithf "print unexpectedly available: %s" (showVal other)
2 changes: 1 addition & 1 deletion lib/IronKernel.Test/src/test.ikr
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
(define show-line
(lambda (label value)
(printf " {0} " label)
(show value)
(print (show value))
(printf "\n")))

; --- checks -------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion website/docs/guide.html
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ <h2 id="diy">14. Invent your own syntax</h2>
<pre><code>(define trace
(vau (exp) env
(begin
(show exp)
(print (show exp))
(. System.Console WriteLine "")
(eval exp env))))

Expand Down
2 changes: 1 addition & 1 deletion website/docs/operators.html
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ <h2 id="io">I/O &amp; display</h2>
<section class="op" id="op-print">
<div class="op-head"><h3 class="op-name">print / printf / show</h3><span class="op-kind">applicative</span></div>
<p class="op-sig">(print obj) (printf format arg…) (show value)</p>
<p><code>printf</code> uses .NET <code>String.Format</code> placeholders (<code>{0}</code>, <code>{1}</code>, …). <code>show</code> writes the Kernel printed representation.</p>
<p><code>printf</code> uses .NET <code>String.Format</code> placeholders (<code>{0}</code>, <code>{1}</code>, …). <code>show</code> renders the Kernel printed representation as a string — pure, available under every profile; write it with <code>(print (show value))</code>.</p>
</section>
<section class="op" id="op-ports">
<div class="op-head"><h3 class="op-name">open-*-file / read / write / …</h3><span class="op-kind">applicative</span></div>
Expand Down
Loading