🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.functional
Article #886

Re: Help needed with behaviour of SML

#886
From: Thomas M. Breuel
Date: Sat, 27 Apr 1991 02:27
47 lines
2180 bytes
In article <9104262016.AA28063@enuxha.eas.asu.edu> surendar@ENUXHA.EAS.ASU.EDU (Surendar Chandra) writes:
	   This is my first posting on this group. I do not even know if this
   question is appropriate in this group. Anyway, I am currently implementing
   a subset of the STandard ML as part of my course project and find the
   behavior of SML quite confusing in the following case.

   > val a = 1;
   > a ;
   | 1 : int ;
   > fun b c = c * a ;
   > b 2 ;
   | 2 : int ;
   > val a = 3;
   > b 2 ;
   | 2 : int ;
   ^^^^^^^^^^ Why does it do it like this.. Why doesn't it look up at the current
   value of the variable 'a'. Why does it store the current value of 'a' in the
   function during compilation itself? WHat is the logic behind this? I could
   not lay my hands on a good reference for SML and most of the books don't seem
   to talk about this at all.

There are several reasons. Here are some simple ones:


One is that otherwise it would be difficult to preserve type
correctness:

> val a = 1;
> fun f x = x * a;
> val a = "abc";   (* ??? *)

Not allowing you to define the variable a to be of a different type
would make program development a hassle (you couldn't ever change your
mind about the type that you gave some global variable). But if you
do allow the user to change the type "destructively", none of the
original type checking is still valid.

Another reason is that allowing bindings to change would inhibit
lots of compiler optimizations.

If you want a variable that can be modified, use ref:

> val a = ref 1;
> fun f x = x * !a;
> a := 3;

					Thomas.
Technical Details
Message-ID: <TMB.91Apr26222738@volterra.ai.mit.edu>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!think.com!snorkelwacker.mit.edu!ai-lab!life!tmb
References: <9104262016.AA28063@enuxha.eas.asu.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!think.com!snorkelwacker.mit.edu!ai-lab!life!tmb
From: tmb@ai.mit.edu (Thomas M. Breuel)
Newsgroups: comp.lang.functional
Subject: Re: Help needed with behaviour of SML
Message-ID: <TMB.91Apr26222738@volterra.ai.mit.edu>
Date: 27 Apr 91 02:27:38 GMT
References: <9104262016.AA28063@enuxha.eas.asu.edu>
Sender: news@ai.mit.edu
Organization: MIT Artificial Intelligence Lab
Lines: 47
In-reply-to: surendar@ENUXHA.EAS.ASU.EDU's message of 26 Apr 91 20:16:38 GMT