Path: news.daimi.aau.dk!news.uni-c.dk!sunic!sunic.sunet.se!trane.uninett.no!Norway.EU.net!EU.net!howland.reston.ans.net!math.ohio-state.edu!uwm.edu!lll-winken.llnl.gov!venus.sun.com!male.EBay.Sun.COM!engnews2.Eng.Sun.COM!olm From: olm@Eng.Sun.COM (Ole Lehrmann Madsen) Newsgroups: comp.object,comp.lang.beta Subject: Beta example (Was: Pointers to classes?) Date: 23 Jun 1995 08:51:34 GMT Organization: Sun Microsystems Inc., Mountain View, CA Lines: 244 Message-ID: <3sdvam$pfg@engnews2.Eng.Sun.COM> References: <3s80ek$rou@horus.infinet.com> <3s8k3t$n6e@engnews2.Eng.Sun.COM> NNTP-Posting-Host: det.eng.sun.com X-Newsreader: NN version 6.5.0 #21 (NOV) Xref: news.daimi.aau.dk comp.object:31909 comp.lang.beta:423 Scott Wheeler writes: >In Article <3s8k3t$n6e@engnews2.Eng.Sun.COM> Ole Lehrmann Madsen >writes: >>I hope this explanation makes sense, otherwise I will be happy to >>elaborate. You may also want to check the Beta home page. It has >>pointers to a book on Beta that explains pattern variables in greater >>detail. >I had a look at the Beta home page a couple of months back. There are >some interesting ideas in the language, but I couldn't see how one >would use the language in practice. In particular the idea that you can >only add to the definition of a method seemed very restrictive. Do you >happen to have a small complete program that you could post for >discussion? >Scott (This is a follow-up to a posting that originally appeared on comp.object) It is not easy explaining a programming language via the net. Usually I recommend reading an article or the Beta book. I can send a paper version of an article giving an overview of Beta if people e-mail me or Mjolner Informatics. Unfortunately it is not available via www. I can also recommend that you obtain a personal edition of the Mjolner Beta System from Mjolner Informatics. I think the current price is only US$ 60 plus shipping. Contact addresses are given below. Here is, however, a small complete Beta program that can be compiled an run. The example and its output is given after the following (long) explanation. The example does not include pattern variables as described in my first follow-up to the original posting. I understand that Scott is interested in a more basic example? The lines starting with ORIGIN, INCLUDE and --- are for referring to the Beta libraries and can be ignored here. It is part of the so-called fragment system that is used for modularization and information hiding in the Mjolner Beta System. All text within (* ... *) is comments. The basic construct in Beta is the pattern which is a unification of class, procedure, function, process, exception, etc. The following are all patterns: entity, display, person, student, scan, list, element, course, init Note that patterns can be nested. In general Beta has block structure in the sense that patterns can be nested arbitrarily. Patterns may be used as classes, procedures, etc. In this example the following patterns are used as class patterns: entity, person, student, list, element, course The following patterns are used as procedures display, scan, init Patterns may be non-virtual or virtual.The following patterns are virtual: display, element Display is similar to a virtual function in C++. Element is an example of a virtual class pattern. Virtual class patterns are used for parameterizing classes with other classes. Instance variables may be references to objects or part-objects. 'name' and 'title' are examples of an instance variables that may refer to text-objects. C1,C2,C3,S1,S2,S3 are examples of part-objects. Part-objects are generated together with the enclosing object. Objects can be class-less (called singular) in the sense that they need not be generated as instances of a class, but can be described directly. Courses is a singular object that inherits from the list-class. The main program is also an example of a singular object The virtual procedure pattern mechanism works as follows: Consider the execution of S1.display: 1. The entity::display in entity is executed 2. When inner is executed in entity::display, then person::display is executed 3. When inner is executed in person::display, then student::display is executed 4. Execution of inner in student::display is the empty action, since S1 is an instance of student. 5. Hereafter the inner calls returns and possible code after each inner gets executed. Only entity::display has code after its inner. As pointed out by Scott, virtual patterns can only be extended and this may seem restrictive compared to traditional object-oriented languages. It is true that it is more restricted and sometimes it is inconvenient. However, our experience in practice is that a large part of the virtual pattern redefinitions used in practice are specializations/extensions of the virtual function in the superpattern. And in this case the redefined function has explicitly to call the corresponding one in the superpattern. The Beta approach supports this directly since it is not possible to forget to execute the one in the superpattern. Control patterns (control structures) may often be easily defined using inner. An example of this is the scan-pattern of list used int the example. The following is a sketch of its implementation list: (# ... scan: (# current: ^element do loop: (if moreElements then nextElement[] -> current[]; inner; restart loop if) #); #) Another example is the Beta implementations of concurrency and synchronization where a superpattern may enclose an inner call with wait/signal operations on a semaphore. We find that the inner mechanism together with the rule that virtual patterns can only be extended is very useful in designing robust and easy to use libraries and frameworks. It may take time to get the libraries right, but I think this is the case with most languages.. I hope the explanation and example makes sense, otherwise I will be happy to supply further explanations. ----------------------------------------------------------- The example will produce the following output: Name: John Smith Enrolled in: Course: CS101 Course: CS202 Name: Lisa Jones Enrolled in: Course: CS303 Name: Lone Hansen Enrolled in: Course: CS101 Course: CS202 Course: CS303 ------------------------------------------------------------- The program: ORIGIN '~beta/basiclib/v1.4/betaenv'; (* include standard beta env *) INCLUDE '~beta/containers/v1.4/list' (* include list library *) ---lib:attributes--- (* define library patterns *) entity: (* entity is a class pattern *) (# display:< (# do INNER #); (* a virtual procedure pattern*) #); person: entity (* a subpattern of entity *) (# name: ^text; (* ref to instance of text *) display::< (* extension of display *) (# do 'Name: '->putText; name[] -> putText; INNER; newline #); #); student: person (* another subpattern of student*) (# courses: @ list(# element::< course #); (* instance of parameterized * class list; the elements of * the list are defined to be * course objects *) display::< (* another extension of display *) (# do '\nEnrolled in:\n' -> putText; courses.scan(#do current.display; newline #); INNER #); #); course: entity (* another subpattern of entity *) (# title: ^text; (* another extension of display *) display::< (# do 'Course: ' -> puttext; title[] -> puttext; INNER #); #) ---program:descriptor--- (* the main program *) (# C1,C2,C3: @ course; (* 3 static course objects *) S1,S2,S3: @ student; (* 3 static student objects *) init: (* procedure pattern *) (# do 'CS101' -> C1.title[]; 'CS202' -> C2.title[]; 'CS303' -> C3.title[]; 'John Smith' -> S1.name[]; C1[] -> S1.courses.append; C2[] -> S1.courses.append; 'Lisa Jones' -> S2.name[]; C3[] -> S2.courses.append; 'Lone Hansen' -> S3.name[]; C1[] -> S3.courses.append; C2[] -> S3.courses.append; C3[] -> S3.courses.append; #); do init; S1.display; S2.display; S3.display #) *************** BETA information Sources *********************************** WWW: http://www.mjolner.dk http://www.daimi.aau.dk/~beta/info News: comp.lang.beta FAQ: http://www.daimi.aau.dk/~beta/FAQ E-mail: info@mjolner.dk Address: Mjolner Informatics, Science Park Aarhus, Gustav Wieds Vej 10, DK-8000, Aarhus C, DENMARK Tel.: +45 86 20 20 00 Fax.: +45 86 20 12 22 *****************************************************************************