How to convert something to a string in SML?

I am trying to implement a test function to compare and display an error message if they are not equal:

exception AssertionErrorException of string

fun assert(testName, actual, expect) : bool =
    if actual = expect
    then true
    else raise (AssertionErrorException (testName ^ " failed. actual: " ^ actual 
                ^ ", expect: " ^ expect ));

Unfortunately, it does not work if I call it with non-string parameters:

assert("test1", SOME [], NONE);

It cannot be compiled and the error message is:

Error: operator and operand don't agree [tycon mismatch]
  operator domain: string * string * string
  operand:         string * 'Z list option * 'Y option
  in expression:
    assert ("test1",SOME nil,NONE)

How to fix it?

+4
source share
3 answers

Haskell Show show :: Show a => a -> String, show x, x. , Standard ML , show , .

SML ( , ML) makestring, , . . makestring 2 makestring 2.0 , makestring (0,0) . (: , makestring PolyML .)

, , - , . "union" C.

exception AssertionError of string
datatype assert = AssertInt of int
                | AssertReal of real
                | AssertBoolBool of bool * bool
                | ...

fun assertPP (AssertInt i) = Int.toString i
  | assertPP (AssertReal r) = Real.toString r
  | assertPP (AssertBoolBool (b1,b2)) =
    String.concat ["(", Bool.toString b1, ", ", Bool.toString b2, ")" ]
  | assertPP (...) = ...

fun assert (testName, actual: assert, expect: assert) =
    actual = expect  (* ML infers equality for constructors *)
    orelse raise AssertionError (String.concat
        [ testName, " failed. actual: ", assertPP actual,
          ", expect: ", assertPP expect, "." ])

.

+6

makestring ML, . Poly/ML PolyML.makestring, , .

fun assert(testName, actual, expect) =
if actual = expect
   then true
   else raise AssertionErrorException(testName ^ " failed. actual: " ^
                PolyML.makestring actual ^ ", expect: " ^
                PolyML.makestring expect);

,

 assert("test1", SOME [], NONE);

Exception-
AssertionErrorException "test1 failed. actual: SOME [], expect: NONE"
   raised

, , . , , PolyML.makestring , , , "?". , , .

fun assert(testName, actual, expect, toString) =
   if actual = expect
   then true
   else raise AssertionErrorException(testName ^ " failed. actual: " ^
                toString actual ^ ", expect: " ^ toString expect );

, . Poly/ML PolyML.makestring.

assert("test2", (1,2,3), (1,2,4), PolyML.makestring);

Exception-
   AssertionErrorException
  "test2 failed. actual: (1, 2, 3), expect: (1, 2, 4)" raised

SML, .

assert("test2", (1,2,3), (1,2,4),
     fn (a,b,c) =>
        String.concat["(", Int.toString a, ",", Int.toString b,
                      ",", Int.toString c, ")"]);

, .

+6
structure Printf =
   struct
      fun $ (_, f) = f (fn p => p ()) ignore
      fun fprintf out f = f (out, id)
      val printf = fn z => fprintf TextIO.stdOut z
      fun one ((out, f), make) g =
         g (out, fn r =>
            f (fn p =>
               make (fn s =>
                     r (fn () => (p (); TextIO.output (out, s))))))
      fun ` x s = one (x, fn f => f s)
      fun spec to x = one (x, fn f => f o to)
      val B = fn z => spec Bool.toString z
      val I = fn z => spec Int.toString z
      val R = fn z => spec Real.toString z
   end

.

val () = printf `"Int="I`"  Bool="B`"  Real="R`"\n" $ 1 false 2.0

.

Int=1  Bool=false  Real=2.0

+5

All Articles