Caml(英語:Categorical Abstract Machine Language:範疇抽象機語言),是一種函數式、指令式的程式語言。最早由法國的INRIA和ENS聯合的Formel項目發展出來,是ML語言的兩種方言之一,現在主要由INRIA負責維護與發展。Caml是一種語言規範;它早期有過幾個實現,目前除了仍然活躍的OCaml,發布於2002年的Caml Light是Caml的另一個實現。
Quick Facts 編程範型, 語言家族 ...
Close
在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.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
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.