Caml
編程範型 | 多范型: 函數式, 指令式 |
---|---|
語言家族 | ML |
設計者 | Gérard Huet, Guy Cousineau, Ascánder Suárez, Pierre Weis, Michel Mauny (Heavy Caml), Xavier Leroy (Caml Light) |
釋出時間 | 1985年 |
當前版本 |
|
型態系統 | 類型推論, 靜態, 強類型 |
作業系統 | 跨平台 |
網站 | caml |
啟發語言 | |
ML | |
影響語言 | |
OCaml |
Caml(英語:Categorical Abstract Machine Language:範疇抽象機語言),是一種函數式、指令式的程式語言。最早由法國的INRIA和ENS聯合的Formel項目發展出來,是ML語言的兩種方言之一,現在主要由INRIA負責維護與發展。Caml是一種語言規範;它早期有過幾個實現,目前除了仍然活躍的OCaml,發佈於2002年的Caml Light是Caml的另一個實現。
歷史
在1981年,INRIA的Gérard Huet,將最初的LCF ML適配到Multics系統的Maclisp下,並且增加了編譯器[2]。這個實現被描述於INRIA內部文檔「ML手冊」之中[3],它被開發者自稱為「Le_ML」[4]。劍橋大學的Lawrence Paulson用它開發了Cambridge LCF,而劍橋大學的Michael J. C. Gordon用它開發了第一版的HOL[5]。這個ML系統由INRIA和劍橋大學聯合維護和發行[3]。
基於Thierry Coquand在1985年的關於構造演算的論文[6],INRIA的Formel項目開始致力於參與Coq的開發。在1987年,INRIA的Ascánder Suárez,基於巴黎第七大學的Guy Cousineau的「範疇抽象機器」(CAM)[7],利用Le Lisp的運行時間系統重新實現了Le_ML,並正式命名為「Caml」[2]。
在1990年和1991年,INRIA的Xavier Leroy基於用C實現的字節碼解釋器[8],利用Damien Doligez提供的內存管理系統重新實現了Caml,並稱其為「Caml Light」[9]。在1995年,Xavier Leroy又增加了本機代碼編譯器和高層模塊系統[10],這個版本也稱為「Caml Special Light」。在1996年,INRIA的Didier Rémy和Jérôme Vouillon,向Caml Special Light增加了面向對象特徵[11],從而創建了OCaml[12]。
範例
Hello World
下面的程序hello.ml
:
print_endline "Hello World!"
階乘函數
很多數學函數,比如階乘,可以很自然的表示為純粹的函數形式:
let rec fact n =
if n=0 then 1 else n * fact(n - 1);;
這個函數可以使用模式匹配等價的寫為:
let rec fact = function
| 0 -> 1
| n -> n * fact(n - 1);;
後者形式是階乘作為遞推關係的數學定義。
編譯器將這個函數的類型推論為int -> int
,意味着這個函數將int
映射到int
。例如,12!
:
# fact 12;;
- : int = 479001600
引用
- ^ http://caml.inria.fr/pub/distrib/ocaml-4.07/.
- ^ 2.0 2.1 Guy Cousineau, Gérard Huet. The CAML primer Version 2.6.1. 1990 [2021-09-02]. (原始內容存檔於2022-05-04). RT-0122, INRIA. pp.78.
Pierre Weis, Maria Virginia Aponte, Alain Laville, Michel Mauny, Ascander Suarez. The CAML reference manual Version 2.6.1. 1990 [2021-09-02]. (原始內容存檔於2022-04-06). [Research Report] RT-0121, INRIA. pp.491. - ^ 3.0 3.1 G. Cousineau, M. Gordon, G. Huet, R. Milner, L. C. Paulson, C. Wadsworth. The ML Handbook, Version 6.2. Internal document. Project Formel, INRIA. July 1985.
Christoph Kreitz, Vincent Rahli. Introduction to Classic ML (PDF). 2011 [2021-09-09]. (原始內容 (PDF)存檔於2022-01-29).This handbook is a revised edition of Section 2 of 『Edinburgh LCF』, by M. Gordon, R. Milner, and C. Wadsworth, published in 1979 as Springer Verlag Lecture Notes in Computer Science no 78. ……The language is somewhere in between the original ML from LCF and standard ML, since Guy Cousineau added the constructors and call by patterns. This is a LISP based implementation, compatible for Maclisp on Multics, Franzlisp on VAX under Unix, Zetalisp on Symbolics 3600, and Le Lisp on 68000, VAX, Multics, Perkin-Elmer, etc... Video interfaces have been implemented by Philippe Le Chenadec on Multics, and by Maurice Migeon on Symbolics 3600. The ML system is maintained and distributed jointly by INRIA and the University of Cambridge.
- ^ A History of Caml. [2021-09-06]. (原始內容存檔於2022-04-13).
The Formel team became interested in the ML language in 1980-81. ……Gérard Huet decided to make the ML implementation compatible with various Lisp compilers (MacLisp, FranzLisp, LeLisp, ZetaLisp). This work involved Guy Cousineau and Larry Paulson. ……Guy Cousineau also added algebraic data types and pattern-matching, following ideas from Robin Milner ……. At some point, this implementation was called Le_ML, a name that did not survive. It was used by Larry Paulson to develop Cambridge LCF and by Mike Gordon for the first version of HOL ……. ……
Our main reason for developing Caml was to use it for sofware development inside Formel. Indeed, it was used for developing the Coq system ……. We were reluctant to adopt a standard that could later prevent us from adapting the language to our programming needs. ……We did incorporate into Caml most of the improvements brought by Standard ML over Edinburgh ML. ……The first implementation of Caml appeared in 1987 and was further developed until 1992. It was created mainly by Ascander Suarez. ……
In 1990 and 1991, Xavier Leroy designed a completely new implementation of Caml, based on a bytecode interpreter written in C. Damien Doligez provided an excellent memory management system. ……In 1995, Xavier Leroy released Caml Special Light, which improved over Caml Light in several ways. First, an optimizing native-code compiler was added to the bytecode compiler. ……Second, Caml Special Light offered a high-level module system, designed by Xavier Leroy and inspired by the module system of Standard ML. ……Didier Rémy, later joined by Jérôme Vouillon, designed an elegant and highly expressive type system for objects and classes. This design was integrated and implemented within Caml Special Light, leading to the Objective Caml language and implementation, first released in 1996 and renamed to OCaml in 2011. - ^ Michael J. C. Gordon. From LCF to HOL: a short history. 1996 [2007-10-11]. (原始內容存檔於2016-09-05).
- ^ T. Coquand, Gérard Huet. Constructions: a higher order proof system for mechanizing mathematics. RR-0401, INRIA. 1985. inria-00076155. [2021-09-09]. (原始內容存檔於2022-01-30).
- ^ G. Cousineau, P.-L. Curien, M. Mauny. The categorical abstract machine. 1985 [2021-09-05]. (原始內容存檔於2021-09-03). LNCS, 201, Functional programming languages computer architecture, pp.~50-64.
Michel Mauny, Ascánder Suárez. Implementing functional languages in the Categorical Abstract Machine (PDF). 1986 [2021-09-06]. (原始內容 (PDF)存檔於2022-01-28). LFP '86: Proceedings of the 1986 ACM conference on LISP and functional programming, Pages 266–278. - ^ Xavier Leroy. The ZINC experiment : an economical implementation of the ML language. 1990 [2021-09-06]. (原始內容存檔於2022-04-06). RT-0117, INRIA.
- ^ Xavier Leroy. The Caml Light system Release 0.74, documentation and user's guide. 1997 [2021-09-06]. (原始內容存檔於2022-03-08).
- ^ Xavier Leroy. Manifest types, modules, and separate compilation (PDF). Principles of Programming Languages. 1994 [2021-09-07]. (原始內容 (PDF)存檔於2021-10-22).
- ^ Didier Rémy. Type inference for records in a natural extension of ML. Research Report RR-1431, INRIA. 1991 [2021-09-10]. (原始內容存檔於2022-04-06).
Didier Rémy, Jérôme Vouillon. Objective ML: a simple object-oriented extension of ML (PDF). 1997 [2021-09-06]. (原始內容 (PDF)存檔於2022-01-21).
Didier Rémy, Jérôme Vouillon. Objective ML: An effective object-oriented extension to ML (PDF). 1998 [2021-09-06]. (原始內容 (PDF)存檔於2022-01-20). - ^ Xavier Leroy. The Objective Caml system release 1.07, Documentation and user's manual. 1997 [2021-09-06]. (原始內容存檔於2022-01-23).