Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
676 views
in Technique[技术] by (71.8m points)

sml - how to print in ML

I've searched and found several people asking this question, but I can't find an explicit answer. How can I print a non-string in sml?

For example if I have an instance of an ADT, i.e., of a type declared by datatype, and I would like to print the value for debugging. Am I responsible for writing a function which converts such an object to a string, and then print the string? Or is there some printer library I should use? Or is there some sort of printObject or toString function?

Also how can I print other non-string objects such as true and false?

It would appear that sml knows how to print such objects, because when I compile a file using C-l in emacs, I see output such as the following, showing that sml does know how to print the values.

[opening /Users/jimka/Repos/mciml/ex1.1.sml]
type key = string
datatype tree = LEAF | TREE of tree * string * tree
val empty = LEAF : tree
val insert = fn : key * tree -> tree
val member = fn : key * tree -> bool
val t1 = TREE (LEAF,"a",LEAF) : tree
val t2 = TREE (LEAF,"a",TREE (LEAF,"c",LEAF)) : tree
val t3 = TREE (LEAF,"a",TREE (TREE (LEAF,"b",LEAF),"c",LEAF)) : tree

val it = true : bool

val it = () : unit

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

How can I print a non-string in sml?

As I understand it, this is not possible (in a portable way). Depending on the implementation you're using it may expose a function that does this.

Also how can I print other non-string objects such as true and false?

Many types with corresponding basis library structures (e.g., int and Int) have a toString function, so you could print a bool b via print (Bool.toString b) and similarity with Int.toString for an int.

Some implementation specific thoughts:

For PolyML, you can use the function PolyML.print to print values of arbitrary types (though you may need to explicitly type annotate; the type of the argument should not have any type variables).

For SML/NJ, you might try taking a look at the approach discussed here https://sourceforge.net/p/smlnj/mailman/message/21897190/, though this seems like more trouble than it's worth.

For MLton, I'm not aware of anything like a polymorphic function, but they have a couple guides on implementing printf or similar.

It looks like Moscow ML supports a function Meta.printVal, but only in an interactive session. I'm not sure what support SML# has for this sort of thing.

Am I responsible for writing a function which converts such an object to a string, and then print the string?

Generally speaking, yes.

It would appear that sml knows how to print such objects

Depending on your SML implementation this is enabled because the REPL has access to more information than a program normally might. For instance, SML/NJ is able to do this because the REPL has access to type information not available elsewhere (for a source, see John Reppy's statements in the linked mailman thread).

You might also find MLton's TypeIndexedValues example page helpful for this sort of thing, though I haven't closely examined it for quality myself.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...