This function:
fun divide (x: real, y: real) = if y = 0.0 then raise DivByZero 5 else x / y;
is taken from: "Beginner's Guide to Standard ML"
It's part of a "calculator" program included in the document.
I'm using: $ poly --use ./calc.sml Poly/ML 5.9.2 Release
and it chokes and quits at line 4 on the above function. I made no typo! So! What now? Why doesn't Poly/ML not like that code? TIA ...
Standard ML does not allow equality comparison of real numbers, as they are approximations. You have instead to check e.g. if x is sufficiently small, e.g. y < 1E~20. You also have to declare the exception DivByZero, but perhaps that was in code you did not show?
When you ask for assistance, please be specific about the error. In this case it was obvious, but generally it is difficult to help if you don't give the complete error message. Also, you showed two lines of code, so what would "line 4" be?
Lars-Henrik Eriksson, PhD, Senior Lecturer, Distinguished University Teacher Programme Coordinator for the Master Programme in Computer Science Computing Science, Dept. of Information Technology, Uppsala University, Sweden
20 jan. 2026 kl. 22:43 skrev Duke Normandin sidney.reilley.ii@gmail.com:
This function:
fun divide (x: real, y: real) = if y = 0.0 then raise DivByZero 5 else x / y;
is taken from: "Beginner's Guide to Standard ML"
It's part of a "calculator" program included in the document.
I'm using: $ poly --use ./calc.sml Poly/ML 5.9.2 Release
and it chokes and quits at line 4 on the above function. I made no typo! So! What now? Why doesn't Poly/ML not like that code? TIA ... -- Duke _______________________________________________ Poly/ML mailing list -- polyml@lists.polyml.org To unsubscribe send an email to polyml-leave@lists.polyml.org
När du har kontakt med oss på Uppsala universitet med e-post så innebär det att vi behandlar dina personuppgifter. För att läsa mer om hur vi gör det kan du läsa här: http://www.uu.se/om-uu/dataskydd-personuppgifter/
E-mailing Uppsala University means that we will process your personal data. For more information on how this is performed, please read here: http://www.uu.se/en/about-uu/data-protection-policy
Sorry, should be abs(y) < 1E~20, of course.
20 jan. 2026 kl. 22:55 skrev Lars-Henrik Eriksson lhe@it.uu.se:
Standard ML does not allow equality comparison of real numbers, as they are approximations. You have instead to check e.g. if x is sufficiently small, e.g. y < 1E~20. You also have to declare the exception DivByZero, but perhaps that was in code you did not show?
When you ask for assistance, please be specific about the error. In this case it was obvious, but generally it is difficult to help if you don't give the complete error message. Also, you showed two lines of code, so what would "line 4" be?
Lars-Henrik Eriksson, PhD, Senior Lecturer, Distinguished University Teacher Programme Coordinator for the Master Programme in Computer Science Computing Science, Dept. of Information Technology, Uppsala University, Sweden
20 jan. 2026 kl. 22:43 skrev Duke Normandin sidney.reilley.ii@gmail.com:
This function:
fun divide (x: real, y: real) = if y = 0.0 then raise DivByZero 5 else x / y;
is taken from: "Beginner's Guide to Standard ML"
It's part of a "calculator" program included in the document.
I'm using: $ poly --use ./calc.sml Poly/ML 5.9.2 Release
and it chokes and quits at line 4 on the above function. I made no typo! So! What now? Why doesn't Poly/ML not like that code? TIA ... -- Duke
När du har kontakt med oss på Uppsala universitet med e-post så innebär det att vi behandlar dina personuppgifter. För att läsa mer om hur vi gör det kan du läsa här: http://www.uu.se/om-uu/dataskydd-personuppgifter/
E-mailing Uppsala University means that we will process your personal data. For more information on how this is performed, please read here: http://www.uu.se/en/about-uu/data-protection-policy
Unfortunately some versions of ML - or at least one, namely Moscow ML - does allow it. (This is not arguing about what the Standard says!)
For the reason given, you generally shouldn't use equality of reals.
Jeremy
On 21/1/26 08:55, Lars-Henrik Eriksson wrote:
Standard ML does not allow equality comparison of real numbers, as they are approximations. You have instead to check e.g. if x is sufficiently small, e.g. y < 1E~20. You also have to declare the exception DivByZero, but perhaps that was in code you did not show?
When you ask for assistance, please be specific about the error. In this case it was obvious, but generally it is difficult to help if you don't give the complete error message. Also, you showed two lines of code, so what would "line 4" be?
Lars-Henrik Eriksson, PhD, Senior Lecturer, Distinguished University Teacher Programme Coordinator for the Master Programme in Computer Science Computing Science, Dept. of Information Technology, Uppsala University, Sweden
20 jan. 2026 kl. 22:43 skrev Duke Normandin sidney.reilley.ii@gmail.com:
This function:
fun divide (x: real, y: real) = if y = 0.0 then raise DivByZero 5 else x / y;
is taken from: "Beginner's Guide to Standard ML"
It's part of a "calculator" program included in the document.
I'm using: $ poly --use ./calc.sml Poly/ML 5.9.2 Release
and it chokes and quits at line 4 on the above function. I made no typo! So! What now? Why doesn't Poly/ML not like that code? TIA ... -- Duke _______________________________________________ Poly/ML mailing list -- polyml@lists.polyml.org To unsubscribe send an email to polyml-leave@lists.polyml.org
När du har kontakt med oss på Uppsala universitet med e-post så innebär det att vi behandlar dina personuppgifter. För att läsa mer om hur vi gör det kan du läsa här: http://www.uu.se/om-uu/dataskydd-personuppgifter/
E-mailing Uppsala University means that we will process your personal data. For more information on how this is performed, please read here: http://www.uu.se/en/about-uu/data-protection-policy _______________________________________________ Poly/ML mailing list -- polyml@lists.polyml.org To unsubscribe send an email to polyml-leave@lists.polyml.org
Here's the entire code for the "calculator" program. I _did not_ author the code. The author is presumably another PhD in CS somewhere on this planet. https://programming.muthu.co/posts/beginners-guide-to-standard-ml/
[quote] fun add (x: real, y: real) = x + y; fun subtract (x: real, y: real) = x - y; fun multiply (x: real, y: real) = x * y; fun divide (x: real, y: real) = if y = 0.0 then raise DivByZero else x / y; exception DivByZero;
fun calculate (op: string, x: real, y: real) = case op of "+" => add(x, y) | "-" => subtract(x, y) | "*" => multiply(x, y) | "/" => divide(x, y) | _ => raise UnknownOp; exception UnknownOp;
(* Simple interaction loop (can be improved with more robust input handling) *) fun main() = let val input = TextIO.inputLine TextIO.stdIn; val [op, xStr, yStr] = String.tokens (fn c => c = ' ') input; val x = Real.fromString xStr; val y = Real.fromString yStr; in (case calculate(op, x, y) of SOME result => print ("Result: " ^ Real.toString result ^ "\n") | NONE => print ("Invalid operation\n")); main() end
handle DivByZero => print ("Division by zero error!\n") | UnknownOp => print ("Unknown operation!\n");
main();
[/quote]
the error returned is:
[quote] $ poly --use calc.sml Poly/ML 5.9.2 Release val add = fn: real * real -> real val subtract = fn: real * real -> real val multiply = fn: real * real -> real calc.sml:4: error: Type error in function application. Function: = : ''a * ''a -> bool Argument: (y, 0.0) : real * ''a Reason: Can't unify ''a to real (Requires equality type) Found near if y = 0.0 then raise DivByZero else x / y calc.sml:4: error: Value or constructor (DivByZero) has not been declared Found near raise DivByZero Error trying to use the file: 'calc.sml' [/quote]
Just trying to get the hang of the syntax - but the SML implementations seem to vary one from each other. As a result the code in some docs are applicable to one incantation but bombs out on others. A noob is walking a mine field trying to learn SML.
Back in the olden days - on USENET - "plonk" was always at hand, and this is what I'm about to do with SML - unless I can my shit together with a SML implementation and its accompanying resource that is not a PhD thesis, but a plebian how-to. I'm open to suggestions! TIA ...
On 21/01/2026 03:31, Duke Normandin wrote:
Here's the entire code for the "calculator" program. I _did not_ author the code. The author is presumably another PhD in CS somewhere on this planet. https://programming.muthu.co/posts/beginners-guide-to-standard-ml/
[quote] fun add (x: real, y: real) = x + y; fun subtract (x: real, y: real) = x - y; fun multiply (x: real, y: real) = x * y; fun divide (x: real, y: real) = if y = 0.0 then raise DivByZero else x / y; exception DivByZero;
It's not surprising you're having problems. The code on that website has at least two errors. Others have already pointed out that the "real" type is not an equality type so "y = 0.0" is not allowed. But there's a more glaring error in that the exceptions are used before they are declared. It's a general principle in ML that identifiers have to be declared before they are used so the declaration exception DivByZero; needs to be moved above the declaration fun divide ...
It doesn't look as though this code has ever been tested.
I'm sure there are better introductions to Standard ML available. Larry Paulson's book "ML for the Working Programmer" is still available but perhaps rather expensive. Can anyone make any suggestions?
David
Hello,
Thanks to Larry Paulson and Cambridge University Press, it is now available in pdf too, for personal use, at: https://www.cl.cam.ac.uk/~lp15/MLbook/index.html
Great resource ! Bernard.
On 21/01/2026 11:10, David Matthews wrote:
ML for the Working Programmer
On Wed, 21 Jan 2026 11:29:46 +0100 Bernard Berthomieu Bernard.Berthomieu@laas.fr wrote:
Hello,
Thanks to Larry Paulson and Cambridge University Press, it is now available in pdf too, for personal use, at: https://www.cl.cam.ac.uk/~lp15/MLbook/index.html
Thanks!! I have it on order from Amazon as well!!
On Wed, 21 Jan 2026 10:10:55 +0000 David Matthews David.Matthews@prolingua.co.uk wrote:
It's not surprising you're having problems. The code on that website has at least two errors. Others have already pointed out that the "real" type is not an equality type so "y = 0.0" is not allowed. But there's a more glaring error in that the exceptions are used before they are declared. It's a general principle in ML that identifiers have to be declared before they are used so the declaration exception DivByZero; needs to be moved above the declaration fun divide ...
It doesn't look as though this code has ever been tested.
My bad luck to stumble on a sub-standard resource! Thanks for pointing that out.
I'm sure there are better introductions to Standard ML available. Larry Paulson's book "ML for the Working Programmer" is still available but perhaps rather expensive.
I have it on order. Should be here soon - I hope. Thanks again ....