Path: news.daimi.aau.dk!news.uni-c.dk!sunic!uunet!MathWorks.Com!europa.eng.gtefsd.com!library.ucla.edu!csulb.edu!nic-nac.CSU.net!charnel.ecst.csuchico.edu!olivea!koriel!news2me.EBay.Sun.COM!engnews2.Eng.Sun.COM!olm From: olm@Eng.Sun.COM (Ole Lehrmann Madsen) Newsgroups: comp.lang.beta Subject: Re: unsafe virtual patterns? Date: 21 Sep 1994 00:22:08 GMT Organization: Sun Microsystems Inc., Mountain View, CA Lines: 122 Message-ID: <35nubg$74o@engnews2.Eng.Sun.COM> References: <35nlra$t2@rosie.next.com> NNTP-Posting-Host: det.eng.sun.com X-Newsreader: NN version 6.5.0 #21 (NOV) krab@next.com (Kresten Krab Thorup) writes: >How can a Beta compiler know that the program below is incorrect, which I >assume it is. The problem I see is that it will try to execute the >pattern xx in an instance of Foo. It should be obvious that the compiler >should protect the innocent from that kind of thing, or what?As far as I >can see, the statement at {*} performs an implicit (unsafe) downcast from >Foo to Bar; does that generate a runtime error? Someone, please explain. >Kresten >(# > Foo : (# do INNER #); > A : (# > O :< Foo; > o: ^O > enter o[] > #); > Bar : Foo (# xx :< (# do 'xx' -> putText #) #); > B : A (# > O ::< Bar > do o.xx > #); > a : ^A > do &B[] -> a; > &Foo[] -> a {*} > #) > The above type of error is not catched by the compiler, but will be caught by a run-time check. The BETA virtual patterns (as shown in the above example) implies that BETA has covariance of parameters. If you consider the following properties of a language 1. subtype substitutability 2. covariance 3. static typing it is not possible to get all 3 at the same time. For BETA we have decided to give up complete static typing. I.e. in the cases of covariance, the type-checking is deferred to run-time. The run-time type checking for the covariance case is (in BETA) just another version of checking the type for reverse assignments: aVehicle[] -> aBus[] where vehicle: (# ... #); bus: vehicle(# ... #); aVehicle: ^ vehicle; aBus: ^ Bus Simula has the run-time check for reverse assignments. In Eiffel it corresponds to aBus?=aVehicle and for C++ it is the repsonsibility of the programmer (correct me if I am wrong!) For the covariance of parameters, Eiffel does some flow analysis in order to check for possible errors, but this may rule out some cases where there are no errors (again correct me if I am wrong) For BETA, data-flow analysis or type-inference could be applied to eliminate some of the run-time checks. In practice, higher-order patterns (classes) - i.e. patterns parameterized with virtual class patterns - type exact variables are often used. E.g. S: @ Set(# element::< text #) In such cases the compiler can check the types - this is, however, not implemented in the current version of the compiler, but we hope the next version will have it. The static/dynamic typing issues of BETA are discussed in: Madsen, Magnusson,Moller-Pedersen: Strong typing of object-oriented languages revisited, OOPSLA-90 In the current version of the Mjolner BETA System, a run-time occurs if the types are not compatible. In the next version, an exception will be raised. --olm PS. Here is a version of Kresten's example, that will actually compile (as you may know, BETA has case-sensitive identifiers:-) ORIGIN '~beta/basiclib/v1.4/betaenv' ---program:descriptor--- (# Foo : (# do INNER #); A: (# O :< Foo; oo: ^O enter oo[] do INNER #); Bar : Foo (# xx :< (# do 'xx' -> putText #) #); B: A (# O ::< Bar do oo.xx #); aa : ^A do &B[] -> aa[]; &Foo[] -> aa (* * *) #)