Caml

維基百科,自由的百科全書
Caml
編程範型多范型: 函數式, 指令式
語言家族ML
設計者Gérard Huet英語Gérard Huet, Guy Cousineau, Ascánder Suárez, Pierre Weis, Michel Mauny (Heavy Caml),
Xavier Leroy英語Xavier Leroy (Caml Light)
面市時間1985年
當前版本
  • 4.07 (2018年7月;穩定版本)[1]
編輯維基數據鏈接
型態系統類型推論, 靜態, 強類型
操作系統跨平台
網站caml.inria.fr
啟發語言
ML
影響語言
OCaml

Caml(英語:Categorical Abstract Machine Language:範疇抽象機語言),是一種函數式指令式的程式語言。最早由法國的INRIAENS聯合的Formel項目發展出來,是ML語言的兩種方言之一,現在主要由INRIA負責維護與發展。Caml是一種語言規範;它早期有過幾個實現,目前除了仍然活躍的OCaml,發布於2002年的Caml Light是Caml的另一個實現。

歷史[編輯]

在1981年,INRIAGérard Huet英語Gérard Huet,將最初的LCF英語Logic for Computable Functions ML適配到Multics系統的Maclisp下,並且增加了編譯器[2]。這個實現被描述於INRIA內部文檔「ML手冊」之中[3],它被開發者自稱為「Le_ML」[4]劍橋大學Lawrence Paulson英語Lawrence Paulson用它開發了Cambridge LCF,而劍橋大學Michael J. C. Gordon英語Michael J. C. Gordon用它開發了第一版的HOL英語HOL (proof assistant)[5]。這個ML系統由INRIA劍橋大學聯合維護和發行[3]

基於Thierry Coquand英語Thierry Coquand在1985年的關於構造演算的論文[6]INRIA的Formel項目開始致力於參與Coq的開發。在1987年,INRIA的Ascánder Suárez,基於巴黎第七大學Guy Cousineau法語Guy Cousineau的「範疇抽象機器英語Categorical abstract machine」(CAM)[7],利用Le Lisp的運行時間系統重新實現了Le_ML,並正式命名為「Caml」[2]

在1990年和1991年,INRIAXavier Leroy英語Xavier Leroy基於用C實現的字節碼解釋器[8],利用Damien Doligez英語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

引用[編輯]

  1. ^ http://caml.inria.fr/pub/distrib/ocaml-4.07/.
  2. ^ 2.0 2.1 Guy Cousineau, Gérard Huet英語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. ^ 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. 
  4. ^ 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.
     
  5. ^ Michael J. C. Gordon英語Michael J. C. Gordon. From LCF to HOL: a short history. 1996 [2007-10-11]. (原始內容存檔於2016-09-05). 
  6. ^ T. Coquand英語Thierry Coquand, Gérard Huet英語Gérard Huet. Constructions: a higher order proof system for mechanizing mathematics. RR-0401, INRIA. 1985. inria-00076155. [2021-09-09]. (原始內容存檔於2022-01-30). 
  7. ^ 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.
  8. ^ Xavier Leroy. The ZINC experiment : an economical implementation of the ML language. 1990 [2021-09-06]. (原始內容存檔於2022-04-06).  RT-0117, INRIA.
  9. ^ Xavier Leroy英語Xavier Leroy. The Caml Light system Release 0.74, documentation and user's guide. 1997 [2021-09-06]. (原始內容存檔於2022-03-08). 
  10. ^ Xavier Leroy. Manifest types, modules, and separate compilation (PDF). Principles of Programming Languages. 1994 [2021-09-07]. (原始內容 (PDF)存檔於2021-10-22). 
  11. ^ 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). 
  12. ^ Xavier Leroy. The Objective Caml system release 1.07, Documentation and user's manual. 1997 [2021-09-06]. (原始內容存檔於2022-01-23).