On Thu, May 15, 2008 at 9:12 PM, Gabriel Dos Reis wrote: > ... > Bill Page writes: > ... > | )abbrev domain BBB Bbb > | Bbb: with > | foo: Integer -> Integer > | == add > | foo(a: Integer): Integer == > | x:Union(Integer,Float) > | if a>0 then > | x := 1 > | else > | x := 2.3 > | x case Integer => (x*x)::Integer > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ > > Here, you are *manually inserting* the right coercion. This is > not the same thing you did in the interpreter case.
That is true however I was not trying to claim that the interpreter and the compiler are the same. I do not agree that I am "manually inserting" the right coercion. Based on your explain below, it is clear that I could/should have written: x case Integer => x*x x*x I do not think this is the same as manually inserting a coercion, but I admit that it is helping the compiler a little more than one might like. >This is precisely where I was commenting. So, ether you do the > same thing in the interpreter and the compiler or you compare > oranges to apples. Ok, forget about the interpreter. That was just an aside. > If you're going to manually insert the right coercions, you may just > as well declare the variable explicitly as being a Union. > I was suggesting exactly that: If you want x to be known outside the scope if the 'if' statement then you should be forced to explicitly declare it as a Union - just like the case you described with the 'for' loop: x:Integer for i in 1..10 repeat x:=i x I support the idea that 'if' should introduce a new scope. The only difference between 'for' and 'if' is that the type must be a Union. > You were wondering about `autoCoerce' in another message. They > are exactly for this case. Let me explain. > > When you have a union, usualy you have to do a `case' to take > a branch, such as above > > x case Integer => (x*x)::Integer > > this is equivalent to > > if x case Integer then return (x*x)::Integer > else ... > > Now, the compiler keeps track of control flow (see compIf), and when > it is in the success branch it knows that that `the Integer branch' > is taken -- the environment is augmented with a `condition' property > that records the `case expression -- and if you use `x' in that branch > where an Integer is used, it will call autoCoerce with Integer as > target -- see coerceExtraHard() -- which will insert the right coercion > by simple modemap selection. Very interesting! :-) > Notice that I extended this mechanism to user defined `case' > operator back then when I introduced the Syntax domain. > When the compiler is in the `else' branch, it compiles the > code in the `inverse environment' -- see compBoolean. The > inverse environment is computed by removing all `success' > tags from the success environment. In the specific case > where your union contains only two branches you have the > impression that you don't have to say anything in the else > branch, precisely because there remains only one > `autoCoerce' for use. If you have more branches you will > have to manually chose the `right coercions'. > Using more branches seems to work as shown on the wiki page revised according to your suggestion below. > > Try: > > )abbrev package BILLPAGE BillPageUnion > BillPageUnion(): Public == Private where > Public ==> with > foo: () -> Void > Private ==> add > makeU(): Union(Integer,Float) == > even? random()$Integer => 1 > 3.14 > > foo() == > x: Union(Integer,Float) := makeU() > y: Union(Integer,Float) := makeU() > x ** y > Unlike the interpreter, I would not expect the compiler to be able to handle this without some help . The library does not provide any functions with signature: **(Integer,Integer) -> **(Integer,Float) -> As above it is necessary to use the case construction in order to provide the right environment (as you describe above) and then also deal with the missing functions by providing one manual coercion. See: http://axiom-wiki.newsynthesis.org/SandBoxLexicalScope#bottom Regards, Bill Page. ------------------------------------------------------------------------- This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ _______________________________________________ open-axiom-devel mailing list open-axiom-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/open-axiom-devel