Successfully reported this slideshow.
Upcoming SlideShare
×

# CAML考古学

488 views

Published on

presentation at 2018-03-31 ML Day #1

Published in: Engineering
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

### CAML考古学

1. 1. CAML @dico leque ML Day #1 2018-03-31
2. 2. CAML OCaml 1990 Edinburgh LCF Cambridge LCF CAML Caml Light Caml Special Light OCaml Categorical Abstract Machine Language Le-Lisp MacLisp Lisp Edinburgh LCF Stanford Lisp on PDP-10 Cambridge LCF MacLisp on Multics ML CAM code LLM3 code Le-Lisp 2 / 18
3. 3. The CAML Reference Manual[refman] The CAML Primer[primer] CAML 3 / 18
4. 4. (1) [primer p. 5] # l e t x = 7 + 4 ∗ 4 in x ∗ 3 ; ; 69 : num ̸= int OCaml Num 4 / 18
5. 5. (2) [refman pp. 42–43, 111–113] # #−1;; #−1 : i n t # #+1.1;; #1.1 : f l o a t # #−1.2e −1;; #0.12 : f l o a t int, float # int -32767 +32767 int add int, sub int, . . ., float add float, sub float, . . . 5 / 18
6. 6. [refman p. 17] # 1 ,2 ,3 (1 ,2 ,3) : (num ∗ num ∗ num) # f s t (1 ,2 ,3) 1 : num # snd (1 ,2 ,3) (2 , 3) : (num ∗ num) # f s t ( snd (1 ,2 ,3)) 2 : num 2 3 2 , 6 / 18
7. 7. (1) [primer p. 10] l e t imply = function ( true , x ) −> x | ( false , ) −> true l e t imply ( true , f a l s e ) = f a l s e | = true OCaml fun function SML fun 7 / 18
8. 8. (2) [refman p. 21] # #i n f i x power ; ; Ident power i s now parsed as an i n f i x D i r e c t i v e () : u n i t # l e t rec x power y = i f x = 0 then 0 i f y = 0 then 1 else x ∗ ( x power ( y − 1)) Value p r e f i x power = <fun> : (num ∗ num) −> num # 2 power 8 256 : num infix prefix SML op 8 / 18
9. 9. [primer p. 11] l e t rec Ack = function (0 , n) −> n + 1 | (m, 0) −> Ack (m − 1 , 1) | (m, n) −> Ack (m − 1 , Ack (m, n − 1))) l e t fact ’ = f a c t r e c 1 where rec f a c t r e c r e s = function 0 −> r e s | n −> f a c t r e c (n ∗ r s ) (n − 1) let rec where rec rec where let in 9 / 18
10. 10. (1) [primer pp. 25–26] type s u i t = ’ Heart | ’ Diamond | ’ Club | ’ Spade and card = ’ Ace of s u i t | ’ King of s u i t | ’ Queen of s u i t | ’ Jack of s u i t | ’ Plain of s u i t ∗ num l e t s u i t s = [ ’ Heart ; ’ Diamond ; ’ Club ; ’ Spade ] ’ 10 / 18
11. 11. (2) [primer p. 27] # map ’ King s u i t s ; ; l i n e 1: i l l e g a l use of function c o n s t u r c t o r ’ King a 1 e r r o r in typechecking Typecheck F a i l e d type card = King of suit | ... ’ let King = fun s -> ’King s 11 / 18
12. 12. [primer p. 31] type numpair == num ∗ num OCaml = == SML datatype type 12 / 18
13. 13. 1 [primer p. 33] # dynamic (1 , true ) ( dynamic ((1 , true ) : (num ∗ bool ) ) ) : dyn Lisp 13 / 18
14. 14. 2 [primer pp. 33-34] type exp = ’ Numconst of num | ’ Boolconst of bool | ’ A p p l i c a t i o n of o p e r a t o r ∗ exp ∗ exp and o p e r a t o r = ’Sum | ’ Equal | ’ Or l e t rec e v a l = f u n c t i o n ’ Numconst ( n ) −> dynamic n | ’ Boolconst ( b ) −> dynamic b | ’ A p p l i c a t i o n ( op , e1 , e2 ) −> apply ( op , e v a l e1 , e v a l e2 ) and apply = f u n c t i o n ( ’Sum, dynamic ( n1 : num) , dynamic ( n2 : num)) −> dynamic ( n1+n2 ) | ( ’ Equal , dynamic ( n1 : num) , dynamic ( n2 : num) ) −> dynamic ( n1=n2 ) | ( ’ Equal , dynamic ( b1 : bool ) , dynamic ( b2 : bool )) −> dynamic ( b1=b2 ) | ( ’ Or , dynamic ( b1 : bool ) , dynamic ( b2 : bool ) ) −> dynamic ( b1 or b2 ) dynamic 14 / 18
15. 15. [primer p. 34, 37] type ’ a f r o z e n = lazy ’ Frozen of ’ a type ’ a l a z y s t r e a m = { ∗ item : ’ a ; ∗ r e s t : ’ a l a z y s t r e a m } lazy mutable force lazy * 15 / 18
16. 16. Object Language [refman p. 259–] Yacc 16 / 18
17. 17. : LCF ML vs CAML [primer p. 69] LCF ML CAML % . . . % (* . . . *) ‘ . . . ‘ " . . . " letrec let rec whererec where rec t1 # t2 t1 * t2 x y. body fun x y -> body + fun p1. e1 function p1 -> e1 | . . . | . . . | pn. en | pn -> en LCF ML [lcfml] 17 / 18
18. 18. G. Cousineau and G´erard Huet The CAML primer. Technical Report RT-0122, INRIA, September 1990 https://hal.inria.fr/inria-00070045/ﬁle/RT-0122.pdf Michael Gordon and Robin Milner and Christopher Wadsworth. Edinburgh LCF, Vol. 78 of LNCS. 1979. http://www.springer.com/gp/book/9783540097242 Pierre Weis, Maria Virginia Aponte, Alain Laville, Michele Mauny, and Ascander Suarez. The CAML reference manual. Research Report RT-0121, INRIA, 1990. Project Formel https://hal.inria.fr/inria-00070046/ﬁle/RT-0121.pdf 18 / 18