CLU
CLU是一門編程語言,由芭芭拉·利斯科夫和她的學生在1974年到1975年於麻省理工學院(MIT)創造。雖然它沒有被廣泛使用,但它引入了抽象數據類型[8],和許多現在廣泛使用的特性,而被視為面向對象編程發展的重要一步。
編程範型 | 多范型: 面向對象, 過程式 |
---|---|
設計者 | 芭芭拉·利斯科夫和她的學生 |
實作者 | 麻省理工學院 |
面市時間 | 1975年 |
型態系統 | 強類型 |
網站 | www |
主要實作產品 | |
Native CLU[1], Portable CLU[2], clu2c[3] | |
啟發語言 | |
ALGOL 60, Lisp[4], Simula[5] | |
影響語言 | |
Ada, Argus, C++, Lua, Python[6], Ruby, Sather, Swift[7] |
主要貢獻還包括:傳共享調用、迭代器、多值返回(並行賦值形式)、參數化類型和可變類型。值得注意的是它使用了具有構造器和方法的類,但沒有繼承。
聚類
編輯CLU的語法基於了ALGOL,這是當時多數新語言設計的起點。關鍵增補是「聚類」(cluster)概念,它是CLU的類型擴展系統和語言名字的根源(CLUster)[9]。聚類一般對應於面向對象語言中「類」(class)的概念。例如,下面是CLU用來實現複數的語法:
complex_number = cluster is add, subtract, multiply, ... rep = record [ real_part: real, imag_part: real ] add = proc ... end add; subtract = proc ... end subtract; multiply = proc ... end multiply; ... end complex_number;
聚類是一個模塊,它封裝了除了那些在is
子句中顯式命名的成員之外的所有成員。這些成員對應於現在面向對象語言中一個類的公開成員。聚類還定義了可以在聚類之外引用名字的一個類型(在這個案例中是complex_number
),但是它的表示類型(這裡的rep
)對於外部客戶是隱藏的。
聚類名字是全局的,不提供名字空間機制來組織聚類,也不允許它們在其他聚類內部被「局部」創建。
CLU不進行隱式類型轉換。在聚類中,顯式類型轉換up
和down
在抽象類型和表示之間進行變更。有一個全體類型any
,和一個過程force[]
來檢查一個對象是否是一個特定類型。對象可以是可變的或不可變的,後者是基礎類型,比如整數、布爾值、字符和字符串[9]。
其他特徵
編輯CLU類型系統的另一個關鍵特徵是迭代器,它一個接一個的按順序的從一個搜集返回對象[9]。迭代器提供了一致的應用編程接口(API),而不管所用於的是什麼數據。因此針對complex_number
搜集的迭代器,與針對integer
數組的迭代器,可以互換使用。CLU迭代器的顯著特徵是它們被實現為協程,每個值都是通過yield
語句提供給調用者的。像CLU中這樣的迭代器,現在是很多現代語言比如C#、Ruby和Python的常見特徵,然而它們近來經常被稱為生成器。下面是迭代器的例子:
% 产生从1到n的奇数 odds = iter(n:int) yields int i:int i = 1 while i < n do yield i i := i + 2 end end odds for i:int in odds(13) do print int$unparse(i) || "\n" end
CLU還包括了異常處理,它參考了在其他語言中的各種嘗試;異常使用signal
引發,並通過except
處理。不同於具有異常處理的多數其他語言,異常不會被隱式的沿着調用鏈重新發起。不同之處還有,在CLU中異常被當作是正常執行流程的一部份,並作為「正常」而有效的一種類型安全的方式,用來退出循環或從函數返回;這種方式下,「除非」其他條件適用,可以直接指定返回值。既未捕獲也未顯式的重新發起的異常,被立即轉換成特殊失敗異常,這典型的會終止程序。
CLU經常被引證為具有類型安全的可變類型的第一個語言,在這裡叫作oneof
,早於ML語言擁有的叫做代數數據類型的標籤聯合。
CLU中最後一個顯著特徵是並行賦值(多賦值),這裡多於一個變量可以出現在賦值算符的左側。例如,書寫x,y := y,x
將交換x
和y
的值。以相同的方式,函數可以返回多個值,比如x,y,z := f(t)
。並行賦值(但未包括多返回值),在CLU之前已經出現在CPL(1963年)之中,叫作「同時賦值」[10],然而確是CLU使之流行,並被引證為對後來語言中出現的並行賦值有直接的影響。
在CLU程序中所有對象都存活在堆中,而內存管理是自動化的。
CLU支持參數化類型的用戶定義數據抽象。它是提供類型安全限定的參數化類型的第一個語言,它使用where
子句結構,來表達在實際類型實際參數上的約束。
影響
編輯CLU的異常處理機制影響了後來的語言如C++和Java[12]。
Sather、Python和C#所包含的迭代器,最早出現在CLU中。
參考資料
編輯- ^ 1.0 1.1 Curtis, Dorothy. CLU home page. Programming Methodology Group, Computer Science and Artificial Intelligence Laboratory. Massachusetts Institute of Technology. 2009-11-06 [2016-05-26]. (原始內容存檔於2016-06-02).
- ^ 2.0 2.1 Curtis, Dorothy. Index of /pub/pclu. Programming Methodology Group, Computer Science and Artificial Intelligence Laboratory. Massachusetts Institute of Technology. 2009-11-06 [2016-05-26].
- ^ Ushijima, Tetsu. clu2c. clu2c. woodsheep.jp. [2016-05-26]. (原始內容存檔於2016-03-04).
- ^ Barbara Liskov. A history of CLU (PDF). 1992 [2022-04-27]. (原始內容 (PDF)存檔於2021-11-05).
CLU looks like an Algol-like language, but its semantics is like that of Lisp: CLU objects reside in an object universe (or heap), and a variable just identifies (or refers to) an object. We decided early on to have objects in the heap, although we had numerous discussions about the cost of garbage collection. This decision greatly simplified the data abstraction mechanism ……. A language that allocates objects only on the stack is not sufficiently expressive; the heap is needed for objects whose sizes must change and for objects whose lifetime exceeds that of the procedure that creates them. …… Therefore, the choice is: just heap, or both. ……
One unusual aspect of CLU is that our procedures have no free (global) variables ……. The view of procedures in CLU is similar to that in Lisp: CLU procedures are not nested (except that there can be local procedures within a cluster) but instead are defined at the "top" level, and can be called from any other module. In Lisp such procedures can have free variables that are scoped dynamically, a well-known source of confusion. - ^ Barbara Liskov. A history of CLU (PDF). 1992 [2022-04-27]. (原始內容 (PDF)存檔於2021-11-05).
Programming languages that existed when the concept of data abstraction arose did not support abstract data types, but some languages contained constructs that were precursors of this notion. …… The mechanism that matched the best was the class mechanism of Simula 67. A Simula class groups a set of procedures with some variables. A class can be instantiated to provide an object containing its own copies of the variables; the class contains code that initializes these variables at instantiation time. However, Simula classes did not enforce encapsulation ……, and Simula was lacking several other features needed to support data abstraction, …….
- ^ Lundh, Fredrik. Call By Object. effbot.org. [21 November 2017]. (原始內容存檔於2019-11-23).
replace "CLU" with "Python", "record" with "instance", and "procedure" with "function or method", and you get a pretty accurate description of Python's object model.
- ^ Lattner, Chris. Chris Lattner's Homepage. Chris Lattner. 2014-06-03 [2014-06-03]. (原始內容存檔於2018-12-25).
The Swift language is the product of tireless effort from a team of language experts, documentation gurus, compiler optimization ninjas, and an incredibly important internal dogfooding group who provided feedback to help refine and battle-test ideas. Of course, it also greatly benefited from the experiences hard-won by many other languages in the field, drawing ideas from Objective-C, Rust, Haskell, Ruby, Python, C#, CLU, and far too many others to list.
- ^ Liskov, Barbara; Zilles, Stephen. Programming with abstract data types. Proceedings of the ACM SIGPLAN symposium on Very high level languages. 1974: 50–59. doi:10.1145/800233.807045.
- ^ 9.0 9.1 9.2 Liskov, B.; Snyder, A.; Atkinson, R.; Schaffert, C. Abstraction mechanisms in CLU (PDF). Communications of the ACM. August 1977, 20 (8): 564–576 [2021-03-09]. doi:10.1145/359763.359789. (原始內容 (PDF)存檔於2021-08-09).
- ^ Barron, D. W.; Buxton, J. N.; Hartley, D. F.; Nixon, E.; Strachey, C. The main features of CPL. Computer Journal. 1963, 6 (2): 134–143 [2021-03-09]. doi:10.1093/comjnl/6.2.134. (原始內容存檔於2012-07-07).
- ^ Stroustrup, Bjarne. The C++ Programming Language (Third Edition and Special Edition). Bjarne Stroustrup's homepage. 2004-09-08 [2020-09-21]. (原始內容存檔於2019-05-14).
- ^ Bruce Eckel's MindView, Inc: Does Java need Checked Exceptions?. Mindview.net. [2011-12-15]. (原始內容存檔於2002-04-05).
- ^ Ierusalimschy, R.; De Figueiredo, L. H.; Celes, W. The evolution of Lua. Proceedings of the third ACM SIGPLAN conference on History of programming languages – HOPL III (PDF). 2007: 2–1–2–26 [2020-09-21]. ISBN 978-1-59593-766-7. doi:10.1145/1238844.1238846. (原始內容存檔 (PDF)於2020-08-17).
- ^ Ruby's Roots and Matz's Leadership. Appfolio Engineering. 2019-11-08 [2019-11-15]. (原始內容存檔於2019-11-14).
Matz feels that blocks are the greatest invention of Ruby (I agree.) He got the idea from a 1970s language called CLU from MIT, which called them 'iterators'...
- ^ Functional Programming HOWTO — Python 3.8.3 documentation. docs.python.org. [2020-05-25]. (原始內容存檔於2012-10-24).