219

ocaml manual

Embed Size (px)

Citation preview

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 1/218

T h e C a m l L i g h t s y s t e m

r e l e a s e 0 . 7 4

D o c u m e n t a t i o n a n d u s e r ' s m a n u a l

X a v i e r L e r o y

D e c e m b e r 2 , 1 9 9 7

C o p y r i g h t c

1 9 9 7 I n s t i t u t N a t i o n a l d e R e c h e r c h e e n I n f o r m a t i q u e e t A u t o m a t i q u e

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 2/218

2

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 3/218

C o n t e n t s

I G e t t i n g s t a r t e d 7

1 I n s t a l l a t i o n i n s t r u c t i o n s 9

1 . 1 T h e U n i x v e r s i o n : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 9

1 . 2 T h e M a c i n t o s h v e r s i o n : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 9

1 . 3 T h e M S - W i n d o w s v e r s i o n : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 0

I I T h e C a m l L i g h t l a n g u a g e r e f e r e n c e m a n u a l 1 1

2 T h e c o r e C a m l L i g h t l a n g u a g e 1 3

2 . 1 L e x i c a l c o n v e n t i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 4

2 . 2 G l o b a l n a m e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 6

2 . 3 V a l u e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 7

2 . 4 T y p e e x p r e s s i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 9

2 . 5 C o n s t a n t s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 0

2 . 6 P a t t e r n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 0

2 . 7 E x p r e s s i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 2

2 . 8 G l o b a l d e n i t i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 9

2 . 9 D i r e c t i v e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 3 1

2 . 1 0 M o d u l e i m p l e m e n t a t i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 3 1

2 . 1 1 M o d u l e i n t e r f a c e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 3 2

3 L a n g u a g e e x t e n s i o n s 3 5

3 . 1 S t r e a m s , p a r s e r s , a n d p r i n t e r s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 3 5

3 . 2 G u a r d s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 3 6

3 . 3 R a n g e p a t t e r n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 3 7

3 . 4 R e c u r s i v e d e n i t i o n s o f v a l u e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 3 7

3 . 5 L o c a l d e n i t i o n s u s i n g w h e r e : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 3 7

3 . 6 M u t a b l e v a r i a n t t y p e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 3 7

3 . 7 S t r i n g a c c e s s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 3 8

3 . 8 A l t e r n a t e s y n t a x : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 3 8

3 . 9 I n x s y m b o l s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 3 9

3 . 1 0 D i r e c t i v e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 4 0

1

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 4/218

2

I I I T h e C a m l L i g h t c o m m a n d s 4 3

4 B a t c h c o m p i l a t i o n ( c a m l c ) 4 5

4 . 1 O v e r v i e w o f t h e c o m p i l e r : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 4 5

4 . 2 O p t i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 4 6

4 . 3 M o d u l e s a n d t h e l e s y s t e m : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 4 9

4 . 4 C o m m o n e r r o r s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 4 9

5 T h e t o p l e v e l s y s t e m ( c a m l l i g h t ) 5 3

5 . 1 O p t i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 5 4

5 . 2 T o p l e v e l c o n t r o l f u n c t i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 5 5

5 . 3 T h e t o p l e v e l a n d t h e m o d u l e s y s t e m : : : : : : : : : : : : : : : : : : : : : : : : : : 5 8

5 . 4 C o m m o n e r r o r s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 5 9

5 . 5 B u i l d i n g c u s t o m t o p l e v e l s y s t e m s : c a m l m k t o p : : : : : : : : : : : : : : : : : : : : : 6 0

5 . 6 O p t i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 6 1

6 T h e r u n t i m e s y s t e m ( c a m l r u n ) 6 3

6 . 1 O v e r v i e w : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 6 3

6 . 2 O p t i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 6 4

6 . 3 C o m m o n e r r o r s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 6 4

7 T h e l i b r a r i a n ( c a m l l i b r ) 6 7

7 . 1 O v e r v i e w : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 6 7

7 . 2 O p t i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 6 8

7 . 3 T u r n i n g c o d e i n t o a l i b r a r y : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 6 8

8 L e x e r a n d p a r s e r g e n e r a t o r s ( c a m l l e x , c a m l y a c c ) 7 1

8 . 1 O v e r v i e w o f c a m l l e x : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 7 1

8 . 2 S y n t a x o f l e x e r d e n i t i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 7 2

8 . 3 O v e r v i e w o f c a m l y a c c : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 7 4

8 . 4 S y n t a x o f g r a m m a r d e n i t i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 7 4

8 . 5 O p t i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 7 6

8 . 6 A c o m p l e t e e x a m p l e : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 7 6

9 T h e d e b u g g e r ( c a m l d e b u g ) 7 9

9 . 1 C o m p i l i n g f o r d e b u g g i n g : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 7 9

9 . 2 I n v o c a t i o n : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 7 9

9 . 3 C o m m a n d s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 8 0

9 . 4 E x e c u t i n g a p r o g r a m : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 8 1

9 . 5 B r e a k p o i n t s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 8 4

9 . 6 T h e c a l l s t a c k : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 8 4

9 . 7 E x a m i n i n g v a r i a b l e v a l u e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 8 5

9 . 8 C o n t r o l l i n g t h e d e b u g g e r : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 8 6

9 . 9 M i s c e l l a n e o u s c o m m a n d s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 8 9

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 5/218

3

1 0 P r o l i n g ( c a m l p r o ) 9 1

1 0 . 1 C o m p i l i n g f o r p r o l i n g : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 9 1

1 0 . 2 P r o l i n g a n e x e c u t i o n : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 9 2

1 0 . 3 P r i n t i n g p r o l i n g i n f o r m a t i o n : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 9 2

1 0 . 4 K n o w n b u g s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 9 2

1 1 U s i n g C a m l L i g h t u n d e r E m a c s 9 5

1 1 . 1 U p d a t i n g y o u r . e m a c s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 9 5

1 1 . 2 T h e c a m l e d i t i n g m o d e : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 9 5

1 1 . 3 R u n n i n g t h e t o p l e v e l a s a n i n f e r i o r p r o c e s s : : : : : : : : : : : : : : : : : : : : : : : 9 6

1 1 . 4 R u n n i n g t h e d e b u g g e r a s a n i n f e r i o r p r o c e s s : : : : : : : : : : : : : : : : : : : : : : 9 7

1 2 I n t e r f a c i n g C w i t h C a m l L i g h t 9 9

1 2 . 1 O v e r v i e w a n d c o m p i l a t i o n i n f o r m a t i o n : : : : : : : : : : : : : : : : : : : : : : : : : 9 9

1 2 . 2 T h e v a l u e t y p e : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 0 1

1 2 . 3 R e p r e s e n t a t i o n o f C a m l L i g h t d a t a t y p e s : : : : : : : : : : : : : : : : : : : : : : : : 1 0 2

1 2 . 4 O p e r a t i o n s o n v a l u e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 0 3

1 2 . 5 L i v i n g i n h a r m o n y w i t h t h e g a r b a g e c o l l e c t o r : : : : : : : : : : : : : : : : : : : : : 1 0 5

1 2 . 6 A c o m p l e t e e x a m p l e : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 0 7

I V T h e C a m l L i g h t l i b r a r y 1 1 1

1 3 T h e c o r e l i b r a r y 1 1 3

1 3 . 1 b o o l : b o o l e a n o p e r a t i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 1 3

1 3 . 2 b u i l t i n : b a s e t y p e s a n d c o n s t r u c t o r s : : : : : : : : : : : : : : : : : : : : : : : : : 1 1 4

1 3 . 3 c h a r : c h a r a c t e r o p e r a t i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 1 5

1 3 . 4 e q : g e n e r i c c o m p a r i s o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 1 5

1 3 . 5 e x c : e x c e p t i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 1 6

1 3 . 6 f c h a r : c h a r a c t e r o p e r a t i o n s , w i t h o u t s a n i t y c h e c k s : : : : : : : : : : : : : : : : : : 1 1 7

1 3 . 7 f l o a t : o p e r a t i o n s o n o a t i n g - p o i n t n u m b e r s : : : : : : : : : : : : : : : : : : : : : 1 1 7

1 3 . 8 f s t r i n g : s t r i n g o p e r a t i o n s , w i t h o u t s a n i t y c h e c k s : : : : : : : : : : : : : : : : : : 1 1 9

1 3 . 9 f v e c t : o p e r a t i o n s o n v e c t o r s , w i t h o u t s a n i t y c h e c k s : : : : : : : : : : : : : : : : : 1 1 9

1 3 . 1 0 i n t : o p e r a t i o n s o n i n t e g e r s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 2 0

1 3 . 1 1 i o : b u e r e d i n p u t a n d o u t p u t : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 2 2

1 3 . 1 2 l i s t : o p e r a t i o n s o n l i s t s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 2 7

1 3 . 1 3 p a i r : o p e r a t i o n s o n p a i r s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 3 0

1 3 . 1 4 r e f : o p e r a t i o n s o n r e f e r e n c e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 3 0

1 3 . 1 5 s t r e a m : o p e r a t i o n s o n s t r e a m s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 3 1

1 3 . 1 6 s t r i n g : s t r i n g o p e r a t i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 3 2

1 3 . 1 7 v e c t : o p e r a t i o n s o n v e c t o r s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 3 4

1 4 T h e s t a n d a r d l i b r a r y 1 3 7

1 4 . 1 a r g : p a r s i n g o f c o m m a n d l i n e a r g u m e n t s : : : : : : : : : : : : : : : : : : : : : : : : 1 3 7

1 4 . 2 b a l t r e e : b a s i c b a l a n c e d b i n a r y t r e e s : : : : : : : : : : : : : : : : : : : : : : : : : : 1 3 8

1 4 . 3 f i l e n a m e : o p e r a t i o n s o n l e n a m e s : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 4 0

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 6/218

4

1 4 . 4 f o r m a t : p r e t t y p r i n t i n g : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 4 0

1 4 . 5 g c : m e m o r y m a n a g e m e n t c o n t r o l a n d s t a t i s t i c s : : : : : : : : : : : : : : : : : : : : 1 4 7

1 4 . 6 g e n l e x : a g e n e r i c l e x i c a l a n a l y z e r : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 4 9

1 4 . 7 h a s h t b l : h a s h t a b l e s a n d h a s h f u n c t i o n s : : : : : : : : : : : : : : : : : : : : : : : : 1 5 0

1 4 . 8 l e x i n g : t h e r u n - t i m e l i b r a r y f o r l e x e r s g e n e r a t e d b y c a m l l e x : : : : : : : : : : : : 1 5 1

1 4 . 9 m a p : a s s o c i a t i o n t a b l e s o v e r o r d e r e d t y p e s : : : : : : : : : : : : : : : : : : : : : : : 1 5 3

1 4 . 1 0 p a r s i n g : t h e r u n - t i m e l i b r a r y f o r p a r s e r s g e n e r a t e d b y c a m l y a c c : : : : : : : : : : 1 5 3

1 4 . 1 1 p r i n t e x c : a c a t c h - a l l e x c e p t i o n h a n d l e r : : : : : : : : : : : : : : : : : : : : : : : : 1 5 4

1 4 . 1 2 p r i n t f : f o r m a t t i n g p r i n t i n g f u n c t i o n s : : : : : : : : : : : : : : : : : : : : : : : : : 1 5 4

1 4 . 1 3 q u e u e : q u e u e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 5 6

1 4 . 1 4 r a n d o m : p s e u d o - r a n d o m n u m b e r g e n e r a t o r : : : : : : : : : : : : : : : : : : : : : : : 1 5 7

1 4 . 1 5 s e t : s e t s o v e r o r d e r e d t y p e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 5 7

1 4 . 1 6 s o r t : s o r t i n g a n d m e r g i n g l i s t s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 5 8

1 4 . 1 7 s t a c k : s t a c k s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 5 9

1 4 . 1 8 s y s : s y s t e m i n t e r f a c e : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 5 9

1 5 T h e g r a p h i c s l i b r a r y 1 6 3

1 5 . 1 g r a p h i c s : m a c h i n e - i n d e p e n d e n t g r a p h i c s p r i m i t i v e s : : : : : : : : : : : : : : : : : 1 6 4

1 6 T h e u n i x l i b r a r y : U n i x s y s t e m c a l l s 1 7 1

1 6 . 1 u n i x : i n t e r f a c e t o t h e U n i x s y s t e m : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 7 1

1 7 T h e n u m l i b r a r y : a r b i t r a r y - p r e c i s i o n r a t i o n a l a r i t h m e t i c 1 9 1

1 7 . 1 n u m : o p e r a t i o n s o n n u m b e r s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 9 1

1 7 . 2 a r i t h _ s t a t u s : a g s t h a t c o n t r o l r a t i o n a l a r i t h m e t i c : : : : : : : : : : : : : : : : : 1 9 4

1 8 T h e s t r l i b r a r y : r e g u l a r e x p r e s s i o n s a n d s t r i n g p r o c e s s i n g 1 9 7

1 8 . 1 s t r : r e g u l a r e x p r e s s i o n s a n d h i g h - l e v e l s t r i n g p r o c e s s i n g : : : : : : : : : : : : : : : 1 9 7

V A p p e n d i x 2 0 1

1 9 F u r t h e r r e a d i n g 2 0 3

1 9 . 1 P r o g r a m m i n g i n M L : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 0 3

1 9 . 2 D e s c r i p t i o n s o f M L d i a l e c t s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 0 4

1 9 . 3 I m p l e m e n t i n g f u n c t i o n a l p r o g r a m m i n g l a n g u a g e s : : : : : : : : : : : : : : : : : : : 2 0 5

1 9 . 4 A p p l i c a t i o n s o f M L : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 0 6

I n d e x t o t h e l i b r a r y 2 0 7

I n d e x o f k e y w o r d s 2 1 6

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 7/218

F o r e w o r d

T h i s m a n u a l d o c u m e n t s t h e r e l e a s e 0 . 7 4 o f t h e C a m l L i g h t s y s t e m . I t i s o r g a n i z e d a s f o l l o w s .

P a r t I , \ G e t t i n g s t a r t e d " , e x p l a i n s h o w t o i n s t a l l C a m l L i g h t o n y o u r m a c h i n e .

P a r t I I , \ T h e C a m l L i g h t l a n g u a g e r e f e r e n c e m a n u a l " , i s t h e r e f e r e n c e d e s c r i p t i o n o f t h e C a m l

L i g h t l a n g u a g e .

P a r t I I I , \ T h e C a m l L i g h t c o m m a n d s " , d o c u m e n t s t h e C a m l L i g h t c o m p i l e r , t o p l e v e l s y s t e m ,

a n d p r o g r a m m i n g u t i l i t i e s .

P a r t I V , \ T h e C a m l L i g h t l i b r a r y " , d e s c r i b e s t h e m o d u l e s p r o v i d e d i n t h e s t a n d a r d l i b r a r y .

P a r t V , \ A p p e n d i x " , c o n t a i n s a s h o r t b i b l i o g r a p h y , a n i n d e x o f a l l i d e n t i e r s d e n e d i n t h e

s t a n d a r d l i b r a r y , a n d a n i n d e x o f C a m l L i g h t k e y w o r d s .

C o n v e n t i o n s

T h e C a m l L i g h t s y s t e m c o m e s i n s e v e r a l v e r s i o n s : f o r U n i x m a c h i n e s , f o r M a c i n t o s h e s , a n d f o r

P C s . T h e p a r t s o f t h i s m a n u a l t h a t a r e s p e c i c t o o n e v e r s i o n a r e p r e s e n t e d a s s h o w n b e l o w :

U n i x : T h i s i s m a t e r i a l s p e c i c t o t h e U n i x v e r s i o n .

M a c : T h i s i s m a t e r i a l s p e c i c t o t h e M a c i n t o s h v e r s i o n .

P C : T h i s i s m a t e r i a l s p e c i c t o t h e P C v e r s i o n .

L i c e n s e

T h e C a m l L i g h t s y s t e m i s c o p y r i g h t

c

1 9 8 9 , 1 9 9 0 , 1 9 9 1 , 1 9 9 2 , 1 9 9 3 , 1 9 9 4 , 1 9 9 5 , 1 9 9 6 , 1 9 9 7 I n s t i t u t

N a t i o n a l d e R e c h e r c h e e n I n f o r m a t i q u e e t e n A u t o m a t i q u e ( I N R I A ) . I N R I A h o l d s a l l o w n e r s h i p

r i g h t s t o t h e C a m l L i g h t s y s t e m . S e e t h e l e C O P Y R I G H T i n t h e d i s t r i b u t i o n f o r t h e c o p y r i g h t n o t i c e .

T h e C a m l L i g h t s y s t e m c a n b e f r e e l y r e d i s t r i b u t e d . M o r e p r e c i s e l y , I N R I A g r a n t s a n y u s e r o f

t h e C a m l L i g h t s y s t e m t h e r i g h t t o r e p r o d u c e i t , p r o v i d e d t h a t t h e c o p i e s a r e d i s t r i b u t e d u n d e r t h e

c o n d i t i o n s g i v e n i n t h e C O P Y R I G H T l e . T h e p r e s e n t d o c u m e n t a t i o n i s d i s t r i b u t e d u n d e r t h e s a m e

c o n d i t i o n s .

5

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 8/218

6 F o r e w o r d

A v a i l a b i l i t y b y F T P

T h e c o m p l e t e C a m l L i g h t d i s t r i b u t i o n r e s i d e s o n t h e m a c h i n e f t p . i n r i a . f r . T h e d i s t r i b u t i o n l e s

c a n b e t r a n s f e r r e d b y a n o n y m o u s F T P :

H o s t : f t p . i n r i a . f r ( I n t e r n e t a d d r e s s 1 9 2 . 9 3 . 2 . 5 4 )

L o g i n n a m e : a n o n y m o u s

P a s s w o r d : y o u r e - m a i l a d d r e s s

D i r e c t o r y : l a n g / c a m l - l i g h t

F i l e s : s e e t h e i n d e x i n l e R E A D M E

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 9/218

P a r t I

G e t t i n g s t a r t e d

7

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 10/218

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 11/218

C h a p t e r 1

I n s t a l l a t i o n i n s t r u c t i o n s

T h i s c h a p t e r e x p l a i n s h o w t o i n s t a l l C a m l L i g h t o n y o u r m a c h i n e .

1 . 1 T h e U n i x v e r s i o n

R e q u i r e m e n t s . A n y m a c h i n e t h a t r u n s u n d e r o n e o f t h e v a r i o u s a v o r s o f t h e U n i x o p e r a t i n g

s y s t e m , a n d t h a t h a s a a t , n o n - s e g m e n t e d , 3 2 - b i t o r 6 4 - b i t a d d r e s s s p a c e . 4 M o f R A M , 2 M o f f r e e

d i s k s p a c e . T h e g r a p h i c s l i b r a r y r e q u i r e s X 1 1 r e l e a s e 4 o r l a t e r .

I n s t a l l a t i o n . T h e U n i x v e r s i o n i s d i s t r i b u t e d i n s o u r c e f o r m a t , a s a c o m p r e s s e d t a r l e n a m e d

c l 7 4 u n i x . t a r . g z . T o e x t r a c t , m o v e t o t h e d i r e c t o r y w h e r e y o u w a n t t h e s o u r c e l e s t o r e s i d e ,

t r a n s f e r c l 7 4 u n i x . t a r . g z t o t h a t d i r e c t o r y , a n d e x e c u t e

z c a t c l 7 4 u n i x . t a r . g z | t a r x B f -

T h i s e x t r a c t s t h e s o u r c e l e s i n t h e c u r r e n t d i r e c t o r y . T h e l e I N S T A L L c o n t a i n s c o m p l e t e i n s t r u c -

t i o n s o n h o w t o c o n g u r e , c o m p i l e a n d i n s t a l l C a m l L i g h t . R e a d i t a n d f o l l o w t h e i n s t r u c t i o n s .

T r o u b l e s h o o t i n g . S e e t h e l e I N S T A L L .

1 . 2 T h e M a c i n t o s h v e r s i o n

R e q u i r e m e n t s . A n y M a c i n t o s h w i t h a t l e a s t 1 M o f R A M ( 2 M i s r e c o m m e n d e d ) , r u n n i n g S y s t e m

6 o r 7 . A b o u t 8 5 0 K o f f r e e s p a c e o n t h e d i s k . T h e p a r t s o f t h e C a m l L i g h t s y s t e m t h a t s u p p o r t

b a t c h c o m p i l a t i o n c u r r e n t l y r e q u i r e t h e M a c i n t o s h P r o g r a m m e r ' s W o r k s h o p ( M P W ) v e r s i o n 3 . 2 .

M P W i s A p p l e ' s d e v e l o p m e n t e n v i r o n m e n t , a n d i t i s d i s t r i b u t e d b y A P D A , A p p l e ' s P r o g r a m m e r s

a n d D e v e l o p e r s A s s o c i a t i o n . S e e t h e l e R E A D M E i n t h e d i s t r i b u t i o n f o r A P D A ' s a d d r e s s .

I n s t a l l a t i o n . C r e a t e t h e f o l d e r w h e r e t h e C a m l L i g h t l e s w i l l r e s i d e . D o u b l e - c l i c k o n t h e l e

c l 7 4 m a c b i n . s e a f r o m t h e d i s t r i b u t i o n . T h i s d i s p l a y s a l e d i a l o g b o x . O p e n t h e f o l d e r w h e r e t h e

C a m l L i g h t l e s w i l l r e s i d e , a n d c l i c k o n t h e E x t r a c t b u t t o n . T h i s w i l l r e - c r e a t e a l l l e s f r o m t h e

d i s t r i b u t i o n i n t h e C a m l L i g h t f o l d e r .

T o t e s t t h e i n s t a l l a t i o n , d o u b l e - c l i c k o n t h e a p p l i c a t i o n C a m l L i g h t . T h e \ C a m l L i g h t o u t p u t "

w i n d o w s h o u l d d i s p l a y s o m e t h i n g l i k e

9

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 12/218

1 0

> C a m l L i g h t v e r s i o n 0 . 7 4

#

I n t h e \ C a m l L i g h t i n p u t " w i n d o w , e n t e r 1 + 2 ; ; a n d p r e s s t h e R e t u r n k e y . T h e \ C a m l L i g h t

o u t p u t " w i n d o w s h o u l d d i s p l a y :

> C a m l L i g h t v e r s i o n 0 . 7 4

# 1 + 2 ; ;

- : i n t = 3

#

S e l e c t \ Q u i t " f r o m t h e \ F i l e " m e n u t o r e t u r n t o t h e F i n d e r .

I f y o u h a v e M P W , y o u c a n i n s t a l l t h e b a t c h c o m p i l a t i o n t o o l s a s f o l l o w s . T h e t o o l s a n d s c r i p t s

f r o m t h e t o o l s f o l d e r m u s t r e s i d e i n a p l a c e w h e r e M P W w i l l n d t h e m a s c o m m a n d s . T h e r e

a r e t w o w a y s t o a c h i e v e t h i s r e s u l t : e i t h e r c o p y t h e l e s i n t h e t o o l s f o l d e r t o t h e T o o l s o r t h e

S c r i p t s f o l d e r i n y o u r M P W f o l d e r ; o r k e e p t h e l e s i n t h e t o o l s f o l d e r a n d a d d t h e f o l l o w i n g l i n e

t o y o u r U s e r S t a r t u p l e ( a s s u m i n g C a m l L i g h t r e s i d e s i n f o l d e r C a m l L i g h t o n t h e d i s k n a m e d

M y H D ) :

S e t C o m m a n d s " { C o m m a n d s } , M y H D : C a m l L i g h t : t o o l s : "

I n e i t h e r c a s e , y o u n o w h a v e t o e d i t t h e c a m l c s c r i p t , a n d r e p l a c e t h e s t r i n g

M a c i n t o s h H D : C a m l L i g h t : l i b :

( i n t h e r s t l i n e ) w i t h t h e a c t u a l p a t h n a m e o f t h e l i b f o l d e r . F o r e x a m p l e , i f y o u p u t C a m l L i g h t

i n f o l d e r C a m l L i g h t o n t h e d i s k n a m e d M y H D , t h e r s t l i n e o f c a m l c s h o u l d r e a d :

S e t s t d l i b " M y H D : C a m l L i g h t : l i b : "

T r o u b l e s h o o t i n g . H e r e i s o n e c o m m o n l y e n c o u n t e r e d p r o b l e m .

C a n n o t f i n d f i l e s t r e a m . z i

( D i s p l a y e d i n t h e \ C a m l L i g h t o u t p u t " w i n d o w , w i t h a n a l e r t b o x t e l l i n g y o u t h a t C a m l

L i g h t h a s t e r m i n a t e d a b n o r m a l l y . ) T h i s i s a n i n s t a l l a t i o n e r r o r . T h e f o l d e r n a m e d l i b i n t h e

d i s t r i b u t i o n m u s t a l w a y s b e i n t h e s a m e f o l d e r a s t h e C a m l L i g h t a p p l i c a t i o n . I t ' s O K t o

m o v e t h e a p p l i c a t i o n t o a n o t h e r f o l d e r ; b u t r e m e m b e r t o m o v e t h e l i b d i r e c t o r y t o t h e s a m e

f o l d e r . ( T o r e t u r n t o t h e F i n d e r , r s t s e l e c t \ Q u i t " f r o m t h e \ F i l e " m e n u . )

1 . 3 T h e M S - W i n d o w s v e r s i o n

R e q u i r e m e n t s . A P C e q u i p p e d w i t h a 8 0 3 8 6 , 8 0 4 8 6 o r P e n t i u m p r o c e s s o r , r u n n i n g M S W i n d o w s

3 . x , W i n d o w s 9 5 o r W i n d o w s N T . A b o u t 3 M o f f r e e s p a c e o n t h e d i s k . A t l e a s t 8 M o f R A M i s

r e c o m m e n d e d .

I n s t a l l a t i o n . T h e M S - W i n d o w s v e r s i o n i s d i s t r i b u t e d a s a s e l f - e x t r a c t i n g , s e l f - i n s t a l l i n g a r c h i v e

n a m e d c l 7 4 w i n . e x e . S i m p l y r u n i t a n d f o l l o w t h e s t e p s o f t h e i n s t a l l a t i o n p r o g r a m .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 13/218

P a r t I I

T h e C a m l L i g h t l a n g u a g e r e f e r e n c e

m a n u a l

1 1

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 14/218

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 15/218

C h a p t e r 2

T h e c o r e C a m l L i g h t l a n g u a g e

F o r e w o r d

T h i s d o c u m e n t i s i n t e n d e d a s a r e f e r e n c e m a n u a l f o r t h e C a m l L i g h t l a n g u a g e . I t l i s t s a l l l a n g u a g e

c o n s t r u c t s , a n d g i v e s t h e i r p r e c i s e s y n t a x a n d i n f o r m a l s e m a n t i c s . I t i s b y n o m e a n s a t u t o r i a l

i n t r o d u c t i o n t o t h e l a n g u a g e : t h e r e i s n o t a s i n g l e e x a m p l e . A g o o d w o r k i n g k n o w l e d g e o f t h e

l a n g u a g e , a s p r o v i d e d b y t h e c o m p a n i o n t u t o r i a l F u n c t i o n a l p r o g r a m m i n g u s i n g C a m l L i g h t , i s

a s s u m e d .

N o a t t e m p t h a s b e e n m a d e a t m a t h e m a t i c a l r i g o r : w o r d s a r e e m p l o y e d w i t h t h e i r i n t u i t i v e

m e a n i n g , w i t h o u t f u r t h e r d e n i t i o n . A s a c o n s e q u e n c e , t h e t y p i n g r u l e s h a v e b e e n l e f t o u t , b y l a c k

o f t h e m a t h e m a t i c a l f r a m e w o r k r e q u i r e d t o e x p r e s s t h e m , w h i l e t h e y a r e d e n i t e l y p a r t o f a f u l l

f o r m a l d e n i t i o n o f t h e l a n g u a g e . T h e r e a d e r i n t e r e s t e d i n t r u l y f o r m a l d e s c r i p t i o n s o f l a n g u a g e s

f r o m t h e M L f a m i l y i s r e f e r r e d t o T h e d e n i t i o n o f S t a n d a r d M L a n d C o m m e n t a r y o n S t a n d a r d

M L , b y M i l n e r , T o f t e a n d H a r p e r , M I T P r e s s .

W a r n i n g

S e v e r a l i m p l e m e n t a t i o n s o f t h e C a m l L i g h t l a n g u a g e a r e a v a i l a b l e , a n d t h e y e v o l v e a t e a c h r e -

l e a s e . C o n s e q u e n t l y , t h i s d o c u m e n t c a r e f u l l y d i s t i n g u i s h e s t h e l a n g u a g e a n d i t s i m p l e m e n t a t i o n s .

I m p l e m e n t a t i o n s c a n p r o v i d e e x t r a l a n g u a g e c o n s t r u c t s ; m o r e o v e r , a l l p o i n t s l e f t u n s p e c i e d i n

t h i s r e f e r e n c e m a n u a l c a n b e i n t e r p r e t e d d i e r e n t l y b y t h e i m p l e m e n t a t i o n s . T h e p u r p o s e o f t h i s

r e f e r e n c e m a n u a l i s t o s p e c i f y t h o s e f e a t u r e s t h a t a l l i m p l e m e n t a t i o n s m u s t p r o v i d e .

N o t a t i o n s

T h e s y n t a x o f t h e l a n g u a g e i s g i v e n i n B N F - l i k e n o t a t i o n . T e r m i n a l s y m b o l s a r e s e t i n t y p e w r i t e r

f o n t ( l i k e t h i s ) . N o n - t e r m i n a l s y m b o l s a r e s e t i n i t a l i c f o n t ( l i k e t h a t ) . S q u a r e b r a c k e t s : : : ]

d e n o t e o p t i o n a l c o m p o n e n t s . C u r l y b r a c k e t s f : : : g d e n o t e s z e r o , o n e o r s e v e r a l r e p e t i t i o n s o f t h e

e n c l o s e d c o m p o n e n t s . C u r l y b r a c k e t w i t h a t r a i l i n g p l u s s i g n f : : : g

+

d e n o t e o n e o r s e v e r a l r e p e t i t i o n s

o f t h e e n c l o s e d c o m p o n e n t s . P a r e n t h e s e s ( : : : ) d e n o t e g r o u p i n g .

1 3

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 16/218

1 4

2 . 1 L e x i c a l c o n v e n t i o n s

B l a n k s

T h e f o l l o w i n g c h a r a c t e r s a r e c o n s i d e r e d a s b l a n k s : s p a c e , n e w l i n e , h o r i z o n t a l t a b u l a t i o n , c a r r i a g e

r e t u r n , l i n e f e e d a n d f o r m f e e d . B l a n k s a r e i g n o r e d , b u t t h e y s e p a r a t e a d j a c e n t i d e n t i e r s , l i t e r a l s

a n d k e y w o r d s t h a t w o u l d o t h e r w i s e b e c o n f u s e d a s o n e s i n g l e i d e n t i e r , l i t e r a l o r k e y w o r d .

C o m m e n t s

C o m m e n t s a r e i n t r o d u c e d b y t h e t w o c h a r a c t e r s ( * , w i t h n o i n t e r v e n i n g b l a n k s , a n d t e r m i n a t e d

b y t h e c h a r a c t e r s * ) , w i t h n o i n t e r v e n i n g b l a n k s . C o m m e n t s a r e t r e a t e d a s b l a n k c h a r a c t e r s .

C o m m e n t s d o n o t o c c u r i n s i d e s t r i n g o r c h a r a c t e r l i t e r a l s . N e s t e d c o m m e n t s a r e c o r r e c t l y h a n d l e d .

I d e n t i e r s

i d e n t : : = l e t t e r f l e t t e r j 0 : : : 9 j _ g

l e t t e r : : = A : : : Z j a : : : z

I d e n t i e r s a r e s e q u e n c e s o f l e t t e r s , d i g i t s a n d _ ( t h e u n d e r s c o r e c h a r a c t e r ) , s t a r t i n g w i t h a l e t t e r .

L e t t e r s c o n t a i n a t l e a s t t h e 5 2 l o w e r c a s e a n d u p p e r c a s e l e t t e r s f r o m t h e A S C I I s e t . I m p l e m e n t a t i o n s

c a n r e c o g n i z e a s l e t t e r s o t h e r c h a r a c t e r s f r o m t h e e x t e n d e d A S C I I s e t . I d e n t i e r s c a n n o t c o n t a i n

t w o a d j a c e n t u n d e r s c o r e c h a r a c t e r s ( _ _ ) . I m p l e m e n t a t i o n m a y l i m i t t h e n u m b e r o f c h a r a c t e r s o f a n

i d e n t i e r , b u t t h i s l i m i t m u s t b e a b o v e 2 5 6 c h a r a c t e r s . A l l c h a r a c t e r s i n a n i d e n t i e r a r e m e a n i n g f u l .

I n t e g e r l i t e r a l s

i n t e g e r - l i t e r a l : : = - ] f 0 : : : 9 g

+

j - ] ( 0 x j 0 X ) f 0 : : : 9 j A : : : F j a : : : f g

+

j - ] ( 0 o j 0 O ) f 0 : : : 7 g

+

j - ] ( 0 b j 0 B ) f 0 : : : 1 g

+

A n i n t e g e r l i t e r a l i s a s e q u e n c e o f o n e o r m o r e d i g i t s , o p t i o n a l l y p r e c e d e d b y a m i n u s s i g n . B y

d e f a u l t , i n t e g e r l i t e r a l s a r e i n d e c i m a l ( r a d i x 1 0 ) . T h e f o l l o w i n g p r e x e s s e l e c t a d i e r e n t r a d i x :

P r e x R a d i x

0 x , 0 X h e x a d e c i m a l ( r a d i x 1 6 )

0 o , 0 O o c t a l ( r a d i x 8 )

0 b , 0 B b i n a r y ( r a d i x 2 )

( T h e i n i t i a l 0 i s t h e d i g i t z e r o ; t h e O f o r o c t a l i s t h e l e t t e r O . )

F l o a t i n g - p o i n t l i t e r a l s

o a t - l i t e r a l : : = - ] f 0 : : : 9 g

+

. f 0 : : : 9 g ] ( e j E ) + j - ] f 0 : : : 9 g

+

]

F l o a t i n g - p o i n t d e c i m a l s c o n s i s t i n a n i n t e g e r p a r t , a d e c i m a l p a r t a n d a n e x p o n e n t p a r t . T h e

i n t e g e r p a r t i s a s e q u e n c e o f o n e o r m o r e d i g i t s , o p t i o n a l l y p r e c e d e d b y a m i n u s s i g n . T h e d e c i m a l

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 17/218

C h a p t e r 2 . T h e c o r e C a m l L i g h t l a n g u a g e 1 5

p a r t i s a d e c i m a l p o i n t f o l l o w e d b y z e r o , o n e o r m o r e d i g i t s . T h e e x p o n e n t p a r t i s t h e c h a r a c t e r e

o r E f o l l o w e d b y a n o p t i o n a l + o r - s i g n , f o l l o w e d b y o n e o r m o r e d i g i t s . T h e d e c i m a l p a r t o r t h e

e x p o n e n t p a r t c a n b e o m i t t e d , b u t n o t b o t h t o a v o i d a m b i g u i t y w i t h i n t e g e r l i t e r a l s .

C h a r a c t e r l i t e r a l s

c h a r - l i t e r a l : : = ` r e g u l a r - c h a r `

j ` \ ( \ j ` j n j t j b j r ) `

j ` \ ( 0 : : : 9 ) ( 0 : : : 9 ) ( 0 : : : 9 ) `

C h a r a c t e r l i t e r a l s a r e d e l i m i t e d b y ` ( b a c k q u o t e ) c h a r a c t e r s . T h e t w o b a c k q u o t e s e n c l o s e e i t h e r

o n e c h a r a c t e r d i e r e n t f r o m ` a n d \ , o r o n e o f t h e e s c a p e s e q u e n c e s b e l o w :

S e q u e n c e C h a r a c t e r d e n o t e d

\ \ b a c k s l a s h ( \ )

\ ` b a c k q u o t e ( ` )

\ n n e w l i n e ( L F )

\ r r e t u r n ( C R )

\ t h o r i z o n t a l t a b u l a t i o n ( T A B )

\ b b a c k s p a c e ( B S )

\ d d d t h e c h a r a c t e r w i t h A S C I I c o d e d d d i n d e c i m a l

S t r i n g l i t e r a l s

s t r i n g - l i t e r a l : : = " f s t r i n g - c h a r a c t e r g "

s t r i n g - c h a r a c t e r : : = r e g u l a r - c h a r

j \ ( \ j " j n j t j b j r )

j \ ( 0 : : : 9 ) ( 0 : : : 9 ) ( 0 : : : 9 )

S t r i n g l i t e r a l s a r e d e l i m i t e d b y " ( d o u b l e q u o t e ) c h a r a c t e r s . T h e t w o d o u b l e q u o t e s e n c l o s e a

s e q u e n c e o f e i t h e r c h a r a c t e r s d i e r e n t f r o m " a n d \ , o r e s c a p e s e q u e n c e s f r o m t h e t a b l e b e l o w :

S e q u e n c e C h a r a c t e r d e n o t e d

\ \ b a c k s l a s h ( \ )

\ " d o u b l e q u o t e ( " )

\ n n e w l i n e ( L F )

\ r r e t u r n ( C R )

\ t h o r i z o n t a l t a b u l a t i o n ( T A B )

\ b b a c k s p a c e ( B S )

\ d d d t h e c h a r a c t e r w i t h A S C I I c o d e d d d i n d e c i m a l

I m p l e m e n t a t i o n s m u s t s u p p o r t s t r i n g l i t e r a l s u p t o 2

1 6

? 1 c h a r a c t e r s i n l e n g t h ( 6 5 5 3 5 c h a r a c -

t e r s ) .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 18/218

1 6

K e y w o r d s

T h e i d e n t i e r s b e l o w a r e r e s e r v e d a s k e y w o r d s , a n d c a n n o t b e e m p l o y e d o t h e r w i s e :

a n d a s b e g i n d o d o n e d o w n t o

e l s e e n d e x c e p t i o n f o r f u n f u n c t i o n

i f i n l e t m a t c h m u t a b l e n o t

o f o r p r e f i x r e c t h e n t o

t r y t y p e v a l u e w h e r e w h i l e w i t h

T h e f o l l o w i n g c h a r a c t e r s e q u e n c e s a r e a l s o k e y w o r d s :

# ! ! = & ( ) * * . + + .

, - - . - > . . ( / / . : : :

: = ; ; ; < < . < - < = < = . < > < > .

= = . = = > > . > = > = . @ |

] ^ _ _ _ { | | ] } '

A m b i g u i t i e s

L e x i c a l a m b i g u i t i e s a r e r e s o l v e d a c c o r d i n g t o t h e \ l o n g e s t m a t c h " r u l e : w h e n a c h a r a c t e r s e q u e n c e

c a n b e d e c o m p o s e d i n t o t w o t o k e n s i n s e v e r a l d i e r e n t w a y s , t h e d e c o m p o s i t i o n r e t a i n e d i s t h e o n e

w i t h t h e l o n g e s t r s t t o k e n .

2 . 2 G l o b a l n a m e s

G l o b a l n a m e s a r e u s e d t o d e n o t e v a l u e v a r i a b l e s , v a l u e c o n s t r u c t o r s ( c o n s t a n t o r n o n - c o n s t a n t ) ,

t y p e c o n s t r u c t o r s , a n d r e c o r d l a b e l s . I n t e r n a l l y , a g l o b a l n a m e c o n s i s t s o f t w o p a r t s : t h e n a m e o f

t h e d e n i n g m o d u l e ( t h e m o d u l e n a m e ) , a n d t h e n a m e o f t h e g l o b a l i n s i d e t h a t m o d u l e ( t h e l o c a l

n a m e ) . T h e t w o p a r t s o f t h e n a m e m u s t b e v a l i d i d e n t i e r s . E x t e r n a l l y , g l o b a l n a m e s h a v e t h e

f o l l o w i n g s y n t a x :

g l o b a l - n a m e : : = i d e n t

j i d e n t _ _ i d e n t

T h e f o r m i d e n t _ _ i d e n t i s c a l l e d a q u a l i e d n a m e . T h e r s t i d e n t i e r i s t h e m o d u l e n a m e , t h e

s e c o n d i d e n t i e r i s t h e l o c a l n a m e . T h e f o r m i d e n t i s c a l l e d a n u n q u a l i e d n a m e . T h e i d e n t i e r i s

t h e l o c a l n a m e ; t h e m o d u l e n a m e i s o m i t t e d . T h e c o m p i l e r i n f e r s t h i s m o d u l e n a m e f o l l o w i n g t h e

c o m p l e t i o n r u l e s g i v e n b e l o w , t h e r e f o r e t r a n s f o r m i n g t h e u n q u a l i e d n a m e i n t o a f u l l g l o b a l n a m e .

T o c o m p l e t e a n u n q u a l i e d i d e n t i e r , t h e c o m p i l e r c h e c k s a l i s t o f m o d u l e s , t h e o p e n e d m o d u l e s ,

t o s e e i f t h e y d e n e a g l o b a l w i t h t h e s a m e l o c a l n a m e a s t h e u n q u a l i e d i d e n t i e r . W h e n o n e i s

f o u n d , t h e i d e n t i e r i s c o m p l e t e d i n t o t h e f u l l n a m e o f t h a t g l o b a l . T h a t i s , t h e c o m p i l e r t a k e s a s

m o d u l e n a m e t h e n a m e o f a n o p e n e d m o d u l e t h a t d e n e s a g l o b a l w i t h t h e s a m e l o c a l n a m e a s t h e

u n q u a l i e d i d e n t i e r . I f s e v e r a l m o d u l e s s a t i s f y t h i s c o n d i t i o n , t h e o n e t h a t c o m e s r s t i n t h e l i s t

o f o p e n e d m o d u l e s i s s e l e c t e d .

T h e l i s t o f o p e n e d m o d u l e s a l w a y s i n c l u d e s t h e m o d u l e c u r r e n t l y b e i n g c o m p i l e d ( c h e c k e d r s t ) .

( I n t h e c a s e o f a t o p l e v e l - b a s e d i m p l e m e n t a t i o n , t h i s i s t h e m o d u l e w h e r e a l l t o p l e v e l d e n i t i o n s a r e

e n t e r e d . ) I t a l s o i n c l u d e s a n u m b e r o f s t a n d a r d l i b r a r y m o d u l e s t h a t p r o v i d e t h e i n i t i a l e n v i r o n m e n t

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 19/218

C h a p t e r 2 . T h e c o r e C a m l L i g h t l a n g u a g e 1 7

( c h e c k e d l a s t ) . I n a d d i t i o n , t h e # o p e n a n d # c l o s e d i r e c t i v e s c a n b e u s e d t o a d d o r r e m o v e m o d u l e s

f r o m t h a t l i s t . T h e m o d u l e s a d d e d w i t h # o p e n a r e c h e c k e d a f t e r t h e m o d u l e c u r r e n t l y b e i n g

c o m p i l e d , b u t b e f o r e t h e i n i t i a l s t a n d a r d l i b r a r y m o d u l e s .

v a r i a b l e : : = g l o b a l - n a m e

j p r e f i x o p e r a t o r - n a m e

o p e r a t o r - n a m e : : = + j - j * j / j m o d j + . j - . j * . j / .

j @ j ^ j ! j : = j = j < > j = = j ! = j !

j < j < = j > j < = j < . j < = . j > . j < = .

c c o n s t r : : = g l o b a l - n a m e

j ]

j ( )

n c c o n s t r : : = g l o b a l - n a m e

j p r e f i x : :

t y p e c o n s t r : : = g l o b a l - n a m e

l a b e l : : = g l o b a l - n a m e

D e p e n d i n g o n t h e c o n t e x t , g l o b a l n a m e s c a n s t a n d f o r g l o b a l v a r i a b l e s ( v a r i a b l e ) , c o n s t a n t v a l u e

c o n s t r u c t o r s ( c c o n s t r ) , n o n - c o n s t a n t v a l u e c o n s t r u c t o r s ( n c c o n s t ) , t y p e c o n s t r u c t o r s ( t y p e c o n s t r ) ,

o r r e c o r d l a b e l s ( l a b e l ) . F o r v a r i a b l e s a n d v a l u e c o n s t r u c t o r s , s p e c i a l n a m e s b u i l t w i t h p r e f i x a n d

a n o p e r a t o r n a m e a r e r e c o g n i z e d . T h e t o k e n s ] a n d ( ) a r e a l s o r e c o g n i z e d a s b u i l t - i n c o n s t a n t

c o n s t r u c t o r s ( t h e e m p t y l i s t a n d t h e u n i t v a l u e ) .

T h e s y n t a x o f t h e l a n g u a g e r e s t r i c t s l a b e l s a n d t y p e c o n s t r u c t o r s t o a p p e a r i n c e r t a i n p o s i t i o n s ,

w h e r e n o o t h e r k i n d o f g l o b a l n a m e s a r e a c c e p t e d . H e n c e l a b e l s a n d t y p e c o n s t r u c t o r s h a v e t h e i r

o w n n a m e s p a c e s . V a l u e c o n s t r u c t o r s a n d v a l u e v a r i a b l e s l i v e i n t h e s a m e n a m e s p a c e : a g l o b a l

n a m e i n v a l u e p o s i t i o n i s i n t e r p r e t e d a s a v a l u e c o n s t r u c t o r i f i t a p p e a r s i n t h e s c o p e o f a t y p e

d e c l a r a t i o n d e n i n g t h a t c o n s t r u c t o r ; o t h e r w i s e , t h e g l o b a l n a m e i s t a k e n t o b e a v a l u e v a r i a b l e .

F o r v a l u e c o n s t r u c t o r s , t h e t y p e d e c l a r a t i o n d e t e r m i n e s w h e t h e r a c o n s t r u c t o r i s c o n s t a n t o r n o t .

2 . 3 V a l u e s

T h i s s e c t i o n d e s c r i b e s t h e k i n d s o f v a l u e s t h a t a r e m a n i p u l a t e d b y C a m l L i g h t p r o g r a m s .

2 . 3 . 1 B a s e v a l u e s

I n t e g e r n u m b e r s

I n t e g e r v a l u e s a r e i n t e g e r n u m b e r s f r o m ? 2

3 0

t o 2

3 0

? 1 , t h a t i s ? 1 0 7 3 7 4 1 8 2 4 t o 1 0 7 3 7 4 1 8 2 3 .

I m p l e m e n t a t i o n s m a y s u p p o r t a w i d e r r a n g e o f i n t e g e r v a l u e s .

F l o a t i n g - p o i n t n u m b e r s

F l o a t i n g - p o i n t v a l u e s a r e n u m b e r s i n o a t i n g - p o i n t r e p r e s e n t a t i o n . E v e r y t h i n g a b o u t o a t i n g - p o i n t

v a l u e s i s i m p l e m e n t a t i o n - d e p e n d e n t , i n c l u d i n g t h e r a n g e o f r e p r e s e n t a b l e n u m b e r s , t h e n u m b e r o f

s i g n i c a n t d i g i t s , a n d t h e w a y o a t i n g - p o i n t r e s u l t s a r e r o u n d e d .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 20/218

1 8

C h a r a c t e r s

C h a r a c t e r v a l u e s a r e r e p r e s e n t e d a s 8 - b i t i n t e g e r s b e t w e e n 0 a n d 2 5 5 . C h a r a c t e r c o d e s b e t w e e n

0 a n d 1 2 7 a r e i n t e r p r e t e d f o l l o w i n g t h e A S C I I s t a n d a r d . T h e i n t e r p r e t a t i o n o f c h a r a c t e r c o d e s

b e t w e e n 1 2 8 a n d 2 5 5 i s i m p l e m e n t a t i o n - d e p e n d e n t .

C h a r a c t e r s t r i n g s

S t r i n g v a l u e s a r e n i t e s e q u e n c e s o f c h a r a c t e r s . I m p l e m e n t a t i o n s m u s t s u p p o r t s t r i n g s u p t o 2

1 6

? 1

c h a r a c t e r s i n l e n g t h ( 6 5 5 3 5 c h a r a c t e r s ) . I m p l e m e n t a t i o n s m a y s u p p o r t l o n g e r s t r i n g s .

2 . 3 . 2 T u p l e s

T u p l e s o f v a l u e s a r e w r i t t e n ( v

1

; : : : ; v

n

) , s t a n d i n g f o r t h e n - t u p l e o f v a l u e s v

1

t o v

n

. T u p l e s o f u p t o

2

1 4

? 1 e l e m e n t s ( 1 6 3 8 3 e l e m e n t s ) m u s t b e s u p p o r t e d , t h o u g h i m p l e m e n t a t i o n s m a y s u p p o r t t u p l e s

w i t h m o r e e l e m e n t s .

2 . 3 . 3 R e c o r d s

R e c o r d v a l u e s a r e l a b e l e d t u p l e s o f v a l u e s . T h e r e c o r d v a l u e w r i t t e n f l a b e l

1

= v

1

; : : : ; l a b e l

n

= v

n

g

a s s o c i a t e s t h e v a l u e v

i

t o t h e r e c o r d l a b e l l a b e l

i

, f o r i = 1 : : : n . R e c o r d s w i t h u p t o 2

1 4

? 1 e l d s

( 1 6 3 8 3 e l d s ) m u s t b e s u p p o r t e d , t h o u g h i m p l e m e n t a t i o n s m a y s u p p o r t r e c o r d s w i t h m o r e e l d s .

2 . 3 . 4 A r r a y s

A r r a y s a r e n i t e , v a r i a b l e - s i z e d s e q u e n c e s o f v a l u e s o f t h e s a m e t y p e . A r r a y s o f l e n g t h u p t o 2

1 4

? 1

( 1 6 3 8 3 e l e m e n t s ) m u s t b e s u p p o r t e d , t h o u g h i m p l e m e n t a t i o n s m a y s u p p o r t l a r g e r a r r a y s .

2 . 3 . 5 V a r i a n t v a l u e s

V a r i a n t v a l u e s a r e e i t h e r a c o n s t a n t c o n s t r u c t o r , o r a p a i r o f a n o n - c o n s t a n t c o n s t r u c t o r a n d a

v a l u e . T h e f o r m e r c a s e i s w r i t t e n c c o n s t r ; t h e l a t t e r c a s e i s w r i t t e n n c c o n s t r ( v ) , w h e r e v i s s a i d t o

b e t h e a r g u m e n t o f t h e n o n - c o n s t a n t c o n s t r u c t o r n c c o n s t r .

T h e f o l l o w i n g c o n s t a n t s a r e t r e a t e d l i k e b u i l t - i n c o n s t a n t c o n s t r u c t o r s :

C o n s t a n t C o n s t r u c t o r

f a l s e t h e b o o l e a n f a l s e

t r u e t h e b o o l e a n t r u e

( ) t h e \ u n i t " v a l u e

] t h e e m p t y l i s t

2 . 3 . 6 F u n c t i o n s

F u n c t i o n a l v a l u e s a r e m a p p i n g s f r o m v a l u e s t o v a l u e s .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 21/218

C h a p t e r 2 . T h e c o r e C a m l L i g h t l a n g u a g e 1 9

2 . 4 T y p e e x p r e s s i o n s

t y p e x p r : : = ' i d e n t

j ( t y p e x p r )

j t y p e x p r - > t y p e x p r

j t y p e x p r f * t y p e x p r g

+

j t y p e c o n s t r

j t y p e x p r t y p e c o n s t r

j ( t y p e x p r f , t y p e x p r g ) t y p e c o n s t r

T h e t a b l e b e l o w s h o w s t h e r e l a t i v e p r e c e d e n c e s a n d a s s o c i a t i v i t y o f o p e r a t o r s a n d n o n - c l o s e d

t y p e c o n s t r u c t i o n s . T h e c o n s t r u c t i o n s w i t h h i g h e r p r e c e d e n c e s c o m e r s t .

O p e r a t o r A s s o c i a t i v i t y

T y p e c o n s t r u c t o r a p p l i c a t i o n {

* {

- > r i g h t

T y p e e x p r e s s i o n s d e n o t e t y p e s i n d e n i t i o n s o f d a t a t y p e s a s w e l l a s i n t y p e c o n s t r a i n t s o v e r

p a t t e r n s a n d e x p r e s s i o n s .

T y p e v a r i a b l e s

T h e t y p e e x p r e s s i o n ' i d e n t s t a n d s f o r t h e t y p e v a r i a b l e n a m e d i d e n t . I n d a t a t y p e d e n i t i o n s , t y p e

v a r i a b l e s a r e n a m e s f o r t h e d a t a t y p e p a r a m e t e r s . I n t y p e c o n s t r a i n t s , t h e y r e p r e s e n t u n s p e c i e d

t y p e s t h a t c a n b e i n s t a n t i a t e d b y a n y t y p e t o s a t i s f y t h e t y p e c o n s t r a i n t .

P a r e n t h e s i z e d t y p e s

T h e t y p e e x p r e s s i o n ( t y p e x p r ) d e n o t e s t h e s a m e t y p e a s t y p e x p r .

F u n c t i o n t y p e s

T h e t y p e e x p r e s s i o n t y p e x p r

1

- > t y p e x p r

2

d e n o t e s t h e t y p e o f f u n c t i o n s m a p p i n g a r g u m e n t s o f

t y p e t y p e x p r

1

t o r e s u l t s o f t y p e t y p e x p r

2

.

T u p l e t y p e s

T h e t y p e e x p r e s s i o n t y p e x p r

1

* : : : * t y p e x p r

n

d e n o t e s t h e t y p e o f t u p l e s w h o s e e l e m e n t s b e l o n g t o

t y p e s t y p e x p r

1

; : : : t y p e x p r

n

r e s p e c t i v e l y .

C o n s t r u c t e d t y p e s

T y p e c o n s t r u c t o r s w i t h n o p a r a m e t e r , a s i n t y p e c o n s t r , a r e t y p e e x p r e s s i o n s .

T h e t y p e e x p r e s s i o n t y p e x p r t y p e c o n s t r , w h e r e t y p e c o n s t r i s a t y p e c o n s t r u c t o r w i t h o n e p a -

r a m e t e r , d e n o t e s t h e a p p l i c a t i o n o f t h e u n a r y t y p e c o n s t r u c t o r t y p e c o n s t r t o t h e t y p e t y p e x p r .

T h e t y p e e x p r e s s i o n ( t y p e x p r

1

; : : : ; t y p e x p r

n

) t y p e c o n s t r , w h e r e t y p e c o n s t r i s a t y p e c o n s t r u c -

t o r w i t h n p a r a m e t e r s , d e n o t e s t h e a p p l i c a t i o n o f t h e n - a r y t y p e c o n s t r u c t o r t y p e c o n s t r t o t h e

t y p e s t y p e x p r

1

t h r o u g h t y p e x p r

n

.

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 22/218

2 0

2 . 5 C o n s t a n t s

c o n s t a n t : : = i n t e g e r - l i t e r a l

j o a t - l i t e r a l

j c h a r - l i t e r a l

j s t r i n g - l i t e r a l

j c c o n s t r

T h e s y n t a c t i c c l a s s o f c o n s t a n t s c o m p r i s e s l i t e r a l s f r o m t h e f o u r b a s e t y p e s ( i n t e g e r s , o a t i n g -

p o i n t n u m b e r s , c h a r a c t e r s , c h a r a c t e r s t r i n g s ) , a n d c o n s t a n t c o n s t r u c t o r s .

2 . 6 P a t t e r n s

p a t t e r n : : = i d e n t

j _

j p a t t e r n a s i d e n t

j ( p a t t e r n )

j ( p a t t e r n : t y p e x p r )

j p a t t e r n | p a t t e r n

j c o n s t a n t

j n c c o n s t r p a t t e r n

j p a t t e r n , p a t t e r n f , p a t t e r n g

j { l a b e l = p a t t e r n f ; l a b e l = p a t t e r n g }

j ]

j p a t t e r n f ; p a t t e r n g ]

j p a t t e r n : : p a t t e r n

T h e t a b l e b e l o w s h o w s t h e r e l a t i v e p r e c e d e n c e s a n d a s s o c i a t i v i t y o f o p e r a t o r s a n d n o n - c l o s e d

p a t t e r n c o n s t r u c t i o n s . T h e c o n s t r u c t i o n s w i t h h i g h e r p r e c e d e n c e s c o m e r s t .

O p e r a t o r A s s o c i a t i v i t y

C o n s t r u c t o r a p p l i c a t i o n {

: : r i g h t

, {

| l e f t

a s {

P a t t e r n s a r e t e m p l a t e s t h a t a l l o w s e l e c t i n g d a t a s t r u c t u r e s o f a g i v e n s h a p e , a n d b i n d i n g i d e n -

t i e r s t o c o m p o n e n t s o f t h e d a t a s t r u c t u r e . T h i s s e l e c t i o n o p e r a t i o n i s c a l l e d p a t t e r n m a t c h i n g ;

i t s o u t c o m e i s e i t h e r \ t h i s v a l u e d o e s n o t m a t c h t h i s p a t t e r n " , o r \ t h i s v a l u e m a t c h e s t h i s p a t t e r n ,

r e s u l t i n g i n t h e f o l l o w i n g b i n d i n g s o f i d e n t i e r s t o v a l u e s " .

V a r i a b l e p a t t e r n s

A p a t t e r n t h a t c o n s i s t s i n a n i d e n t i e r m a t c h e s a n y v a l u e , b i n d i n g t h e i d e n t i e r t o t h e v a l u e . T h e

p a t t e r n _ a l s o m a t c h e s a n y v a l u e , b u t d o e s n o t b i n d a n y i d e n t i e r .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 23/218

C h a p t e r 2 . T h e c o r e C a m l L i g h t l a n g u a g e 2 1

A l i a s p a t t e r n s

T h e p a t t e r n p a t t e r n

1

a s i d e n t m a t c h e s t h e s a m e v a l u e s a s p a t t e r n

1

. I f t h e m a t c h i n g a g a i n s t

p a t t e r n

1

i s s u c c e s s f u l , t h e i d e n t i e r i d e n t i s b o u n d t o t h e m a t c h e d v a l u e , i n a d d i t i o n t o t h e b i n d i n g s

p e r f o r m e d b y t h e m a t c h i n g a g a i n s t p a t t e r n

1

.

P a r e n t h e s i z e d p a t t e r n s

T h e p a t t e r n ( p a t t e r n

1

) m a t c h e s t h e s a m e v a l u e s a s p a t t e r n

1

. A t y p e c o n s t r a i n t c a n a p p e a r i n a

p a r e n t h e s i z e d p a t t e r n s , a s i n ( p a t t e r n

1

: t y p e x p r ) . T h i s c o n s t r a i n t f o r c e s t h e t y p e o f p a t t e r n

1

t o

b e c o m p a t i b l e w i t h t y p e .

\ O r " p a t t e r n s

T h e p a t t e r n p a t t e r n

1

| p a t t e r n

2

r e p r e s e n t s t h e l o g i c a l \ o r " o f t h e t w o p a t t e r n s p a t t e r n

1

a n d

p a t t e r n

2

. A v a l u e m a t c h e s p a t t e r n

1

| p a t t e r n

2

e i t h e r i f i t m a t c h e s p a t t e r n

1

o r i f i t m a t c h e s p a t t e r n

2

.

T h e t w o s u b - p a t t e r n s p a t t e r n

1

a n d p a t t e r n

2

m u s t c o n t a i n n o i d e n t i e r s . H e n c e n o b i n d i n g s a r e

r e t u r n e d b y m a t c h i n g a g a i n s t a n \ o r " p a t t e r n .

C o n s t a n t p a t t e r n s

A p a t t e r n c o n s i s t i n g i n a c o n s t a n t m a t c h e s t h e v a l u e s t h a t a r e e q u a l t o t h i s c o n s t a n t .

V a r i a n t p a t t e r n s

T h e p a t t e r n n c c o n s t r p a t t e r n

1

m a t c h e s a l l v a r i a n t s w h o s e c o n s t r u c t o r i s e q u a l t o n c c o n s t r , a n d

w h o s e a r g u m e n t m a t c h e s p a t t e r n

1

.

T h e p a t t e r n p a t t e r n

1

: : p a t t e r n

2

m a t c h e s n o n - e m p t y l i s t s w h o s e h e a d s m a t c h p a t t e r n

1

, a n d

w h o s e t a i l s m a t c h p a t t e r n

2

. T h i s p a t t e r n b e h a v e s l i k e p r e f i x : : ( p a t t e r n

1

, p a t t e r n

2

) .

T h e p a t t e r n p a t t e r n

1

; : : : ; p a t t e r n

n

] m a t c h e s l i s t s o f l e n g t h n w h o s e e l e m e n t s m a t c h

p a t t e r n

1

: : : p a t t e r n

n

, r e s p e c t i v e l y . T h i s p a t t e r n b e h a v e s l i k e p a t t e r n

1

: : : : : : : p a t t e r n

n

: : ] .

T u p l e p a t t e r n s

T h e p a t t e r n p a t t e r n

1

, : : : , p a t t e r n

n

m a t c h e s n - t u p l e s w h o s e c o m p o n e n t s m a t c h t h e p a t t e r n s

p a t t e r n

1

t h r o u g h p a t t e r n

n

. T h a t i s , t h e p a t t e r n m a t c h e s t h e t u p l e v a l u e s ( v

1

; : : : ; v

n

) s u c h t h a t

p a t t e r n

i

m a t c h e s v

i

f o r i = 1 ; : : : ; n .

R e c o r d p a t t e r n s

T h e p a t t e r n { l a b e l

1

= p a t t e r n

1

; : : : ; l a b e l

n

= p a t t e r n

n

} m a t c h e s r e c o r d s t h a t d e n e a t l e a s t

t h e l a b e l s l a b e l

1

t h r o u g h l a b e l

n

, a n d s u c h t h a t t h e v a l u e a s s o c i a t e d t o l a b e l

i

m a t c h t h e p a t t e r n

p a t t e r n

i

, f o r i = 1 ; : : : ; n . T h e r e c o r d v a l u e c a n d e n e m o r e l a b e l s t h a n l a b e l

1

: : : l a b e l

n

; t h e v a l u e s

a s s o c i a t e d t o t h e s e e x t r a l a b e l s a r e n o t t a k e n i n t o a c c o u n t f o r m a t c h i n g .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 24/218

2 2

2 . 7 E x p r e s s i o n s

e x p r : : = i d e n t

j v a r i a b l e

j c o n s t a n t

j ( e x p r )

j b e g i n e x p r e n d

j ( e x p r : t y p e x p r )

j e x p r , e x p r f , e x p r g

j n c c o n s t r e x p r

j e x p r : : e x p r

j e x p r f ; e x p r g ]

j | e x p r f ; e x p r g | ]

j { l a b e l = e x p r f ; l a b e l = e x p r g }

j e x p r e x p r

j p r e x - o p e x p r

j e x p r i n x - o p e x p r

j e x p r . l a b e l

j e x p r . l a b e l < - e x p r

j e x p r . ( e x p r )

j e x p r . ( e x p r ) < - e x p r

j e x p r & e x p r

j e x p r o r e x p r

j i f e x p r t h e n e x p r e l s e e x p r ]

j w h i l e e x p r d o e x p r d o n e

j f o r i d e n t = e x p r ( t o j d o w n t o ) e x p r d o e x p r d o n e

j e x p r ; e x p r

j m a t c h e x p r w i t h s i m p l e - m a t c h i n g

j f u n m u l t i p l e - m a t c h i n g

j f u n c t i o n s i m p l e - m a t c h i n g

j t r y e x p r w i t h s i m p l e - m a t c h i n g

j l e t r e c ] l e t - b i n d i n g f a n d l e t - b i n d i n g g i n e x p r

s i m p l e - m a t c h i n g : : = p a t t e r n - > e x p r f | p a t t e r n - > e x p r g

m u l t i p l e - m a t c h i n g : : = p a t t e r n - l i s t - > e x p r f | p a t t e r n - l i s t - > e x p r g

p a t t e r n - l i s t : : = p a t t e r n f p a t t e r n g

l e t - b i n d i n g : : = p a t t e r n = e x p r

j v a r i a b l e p a t t e r n - l i s t = e x p r

p r e x - o p : : = - j - . j !

i n x - o p : : = + j - j * j / j m o d j + . j - . j * . j / . j * * j @ j ^ j ! j : =

j = j < > j = = j ! = j < j < = j > j > = j < . j < = . j > . j > = .

T h e t a b l e b e l o w s h o w s t h e r e l a t i v e p r e c e d e n c e s a n d a s s o c i a t i v i t y o f o p e r a t o r s a n d n o n - c l o s e d

c o n s t r u c t i o n s . T h e c o n s t r u c t i o n s w i t h h i g h e r p r e c e d e n c e c o m e r s t .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 25/218

C h a p t e r 2 . T h e c o r e C a m l L i g h t l a n g u a g e 2 3

C o n s t r u c t i o n o r o p e r a t o r A s s o c i a t i v i t y

! {

. . ( {

f u n c t i o n a p p l i c a t i o n l e f t

c o n s t r u c t o r a p p l i c a t i o n {

- - . ( p r e x ) {

* * r i g h t

m o d l e f t

* * . / / . l e f t

+ + . - - . l e f t

: : r i g h t

@ ^ r i g h t

c o m p a r i s o n s ( = = = < e t c . ) l e f t

n o t {

& l e f t

o r l e f t

, {

< - : = r i g h t

i f {

; r i g h t

l e t m a t c h f u n f u n c t i o n t r y {

2 . 7 . 1 S i m p l e e x p r e s s i o n s

C o n s t a n t s

E x p r e s s i o n s c o n s i s t i n g i n a c o n s t a n t e v a l u a t e t o t h i s c o n s t a n t .

V a r i a b l e s

E x p r e s s i o n s c o n s i s t i n g i n a v a r i a b l e e v a l u a t e t o t h e v a l u e b o u n d t o t h i s v a r i a b l e i n t h e c u r r e n t

e v a l u a t i o n e n v i r o n m e n t . T h e v a r i a b l e c a n b e e i t h e r a q u a l i e d i d e n t i e r o r a s i m p l e i d e n t i e r .

Q u a l i e d i d e n t i e r s a l w a y s d e n o t e g l o b a l v a r i a b l e s . S i m p l e i d e n t i e r s d e n o t e e i t h e r a l o c a l v a r i a b l e ,

i f t h e i d e n t i e r i s l o c a l l y b o u n d , o r a g l o b a l v a r i a b l e , w h o s e f u l l n a m e i s o b t a i n e d b y q u a l i f y i n g t h e

s i m p l e i d e n t i e r , a s d e s c r i b e d i n s e c t i o n 2 . 2 .

P a r e n t h e s i z e d e x p r e s s i o n s

T h e e x p r e s s i o n s ( e x p r ) a n d b e g i n e x p r e n d h a v e t h e s a m e v a l u e a s e x p r . B o t h c o n s t r u c t s a r e

s e m a n t i c a l l y e q u i v a l e n t , b u t i t i s g o o d s t y l e t o u s e b e g i n : : : e n d i n s i d e c o n t r o l s t r u c t u r e s :

i f : : : t h e n b e g i n : : : ; : : : e n d e l s e b e g i n : : : ; : : : e n d

a n d ( : : : ) f o r t h e o t h e r g r o u p i n g s i t u a t i o n s .

P a r e n t h e s i z e d e x p r e s s i o n s c a n c o n t a i n a t y p e c o n s t r a i n t , a s i n ( e x p r : t y p e ) . T h i s c o n s t r a i n t

f o r c e s t h e t y p e o f e x p r t o b e c o m p a t i b l e w i t h t y p e .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 26/218

2 4

F u n c t i o n a b s t r a c t i o n

T h e m o s t g e n e r a l f o r m o f f u n c t i o n a b s t r a c t i o n i s :

f u n p a t t e r n

1

1

: : : p a t t e r n

m

1

- > e x p r

1

| : : :

| p a t t e r n

1

n

: : : p a t t e r n

m

n

- > e x p r

n

T h i s e x p r e s s i o n e v a l u a t e s t o a f u n c t i o n a l v a l u e w i t h m c u r r i e d a r g u m e n t s . W h e n t h i s f u n c t i o n i s

a p p l i e d t o m v a l u e s v

1

: : : v

m

, t h e v a l u e s a r e m a t c h e d a g a i n s t e a c h p a t t e r n r o w p a t t e r n

1

i

: : : p a t t e r n

m

i

f o r i f r o m 1 t o n . I f o n e o f t h e s e m a t c h i n g s s u c c e e d s , t h a t i s i f t h e v a l u e v

j

m a t c h e s t h e p a t t e r n

p a t t e r n

j

i

f o r a l l j = 1 ; : : : ; m , t h e n t h e e x p r e s s i o n e x p r

i

a s s o c i a t e d t o t h e s e l e c t e d p a t t e r n r o w i s

e v a l u a t e d , a n d i t s v a l u e b e c o m e s t h e v a l u e o f t h e f u n c t i o n a p p l i c a t i o n . T h e e v a l u a t i o n o f e x p r

i

t a k e s p l a c e i n a n e n v i r o n m e n t e n r i c h e d b y t h e b i n d i n g s p e r f o r m e d d u r i n g t h e m a t c h i n g .

I f s e v e r a l p a t t e r n r o w s m a t c h t h e a r g u m e n t s , t h e o n e t h a t o c c u r s r s t i n t h e f u n c t i o n d e n i t i o n

i s s e l e c t e d . I f n o n e o f t h e p a t t e r n r o w s m a t c h e s t h e a r g u m e n t , t h e e x c e p t i o n M a t c h _ f a i l u r e i s

r a i s e d .

I f t h e f u n c t i o n a b o v e i s a p p l i e d t o l e s s t h a n m a r g u m e n t s , a f u n c t i o n a l v a l u e i s r e t u r n e d , t h a t

r e p r e s e n t s t h e p a r t i a l a p p l i c a t i o n o f t h e f u n c t i o n t o t h e a r g u m e n t s p r o v i d e d . T h i s p a r t i a l a p p l i c a t i o n

i s a f u n c t i o n t h a t , w h e n a p p l i e d t o t h e r e m a i n i n g a r g u m e n t s , m a t c h e s a l l a r g u m e n t s a g a i n s t t h e

p a t t e r n r o w s a s d e s c r i b e d a b o v e . M a t c h i n g d o e s n o t s t a r t u n t i l a l l m a r g u m e n t s h a v e b e e n p r o v i d e d

t o t h e f u n c t i o n ; h e n c e , p a r t i a l a p p l i c a t i o n s o f t h e f u n c t i o n t o l e s s t h a n m a r g u m e n t s n e v e r r a i s e

M a t c h _ f a i l u r e .

A l l p a t t e r n r o w s i n t h e f u n c t i o n b o d y m u s t c o n t a i n t h e s a m e n u m b e r o f p a t t e r n s . A v a r i a b l e

m u s t n o t b e b o u n d m o r e t h a n o n c e i n o n e p a t t e r n r o w .

F u n c t i o n s w i t h o n l y o n e a r g u m e n t c a n b e d e n e d w i t h t h e f u n c t i o n k e y w o r d i n s t e a d o f f u n :

f u n c t i o n p a t t e r n

1

- > e x p r

1

| : : :

| p a t t e r n

n

- > e x p r

n

T h e f u n c t i o n t h u s d e n e d b e h a v e s e x a c t l y a s d e s c r i b e d a b o v e . T h e o n l y d i e r e n c e b e t w e e n t h e t w o

f o r m s o f f u n c t i o n d e n i t i o n i s h o w a p a r s i n g a m b i g u i t y i s r e s o l v e d . T h e t w o f o r m s c c o n s t r p a t t e r n

( t w o p a t t e r n s i n a r o w ) a n d n c c o n s t r p a t t e r n ( o n e p a t t e r n ) c a n n o t b e d i s t i n g u i s h e d s y n t a c t i c a l l y .

F u n c t i o n d e n i t i o n s i n t r o d u c e d b y f u n r e s o l v e t h e a m b i g u i t y t o t h e f o r m e r f o r m ; f u n c t i o n d e n i t i o n s

i n t r o d u c e d b y f u n c t i o n r e s o l v e i t t o t h e l a t t e r f o r m ( t h e f o r m e r f o r m m a k e s n o s e n s e i n t h i s c a s e ) .

F u n c t i o n a p p l i c a t i o n

F u n c t i o n a p p l i c a t i o n i s d e n o t e d b y j u x t a p o s i t i o n o f e x p r e s s i o n s . T h e e x p r e s s i o n

e x p r

1

e x p r

2

: : : e x p r

n

e v a l u a t e s t h e e x p r e s s i o n s e x p r

1

t o e x p r

n

. T h e e x p r e s s i o n e x p r

1

m u s t e v a l u a t e t o a f u n c t i o n a l v a l u e , w h i c h i s t h e n a p p l i e d t o t h e v a l u e s o f e x p r

2

; : : : ; e x p r

n

. T h e

o r d e r i n w h i c h t h e e x p r e s s i o n s e x p r

1

; : : : ; e x p r

n

a r e e v a l u a t e d i s n o t s p e c i e d .

L o c a l d e n i t i o n s

T h e l e t a n d l e t r e c c o n s t r u c t s b i n d v a r i a b l e s l o c a l l y . T h e c o n s t r u c t

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 27/218

C h a p t e r 2 . T h e c o r e C a m l L i g h t l a n g u a g e 2 5

l e t p a t t e r n

1

= e x p r

1

a n d : : : a n d p a t t e r n

n

= e x p r

n

i n e x p r

e v a l u a t e s e x p r

1

: : : e x p r

n

i n s o m e u n s p e c i e d o r d e r , t h e n m a t c h e s t h e i r v a l u e s a g a i n s t t h e p a t t e r n s

p a t t e r n

1

: : : p a t t e r n

n

. I f t h e m a t c h i n g s s u c c e e d , e x p r i s e v a l u a t e d i n t h e e n v i r o n m e n t e n r i c h e d b y

t h e b i n d i n g s p e r f o r m e d d u r i n g m a t c h i n g , a n d t h e v a l u e o f e x p r i s r e t u r n e d a s t h e v a l u e o f t h e w h o l e

l e t e x p r e s s i o n . I f o n e o f t h e m a t c h i n g s f a i l s , t h e e x c e p t i o n M a t c h _ f a i l u r e i s r a i s e d .

A n a l t e r n a t e s y n t a x i s p r o v i d e d t o b i n d v a r i a b l e s t o f u n c t i o n a l v a l u e s : i n s t e a d o f w r i t i n g

i d e n t = f u n p a t t e r n

1

: : : p a t t e r n

m

- > e x p r

i n a l e t e x p r e s s i o n , o n e m a y i n s t e a d w r i t e

i d e n t p a t t e r n

1

: : : p a t t e r n

m

= e x p r

B o t h f o r m s b i n d i d e n t t o t h e c u r r i e d f u n c t i o n w i t h m a r g u m e n t s a n d o n l y o n e c a s e ,

p a t t e r n

1

: : : p a t t e r n

m

- > e x p r .

R e c u r s i v e d e n i t i o n s o f v a r i a b l e s a r e i n t r o d u c e d b y l e t r e c :

l e t r e c p a t t e r n

1

= e x p r

1

a n d : : : a n d p a t t e r n

n

= e x p r

n

i n e x p r

T h e o n l y d i e r e n c e w i t h t h e l e t c o n s t r u c t d e s c r i b e d a b o v e i s t h a t t h e b i n d i n g s o f v a r i a b l e s t o

v a l u e s p e r f o r m e d b y t h e p a t t e r n - m a t c h i n g a r e c o n s i d e r e d a l r e a d y p e r f o r m e d w h e n t h e e x p r e s s i o n s

e x p r

1

t o e x p r

n

a r e e v a l u a t e d . T h a t i s , t h e e x p r e s s i o n s e x p r

1

t o e x p r

n

c a n r e f e r e n c e i d e n t i e r s t h a t

a r e b o u n d b y o n e o f t h e p a t t e r n s p a t t e r n

1

; : : : ; p a t t e r n

n

, a n d e x p e c t t h e m t o h a v e t h e s a m e v a l u e

a s i n e x p r , t h e b o d y o f t h e l e t r e c c o n s t r u c t .

T h e r e c u r s i v e d e n i t i o n i s g u a r a n t e e d t o b e h a v e a s d e s c r i b e d a b o v e i f t h e e x p r e s s i o n s e x p r

1

t o e x p r

n

a r e f u n c t i o n d e n i t i o n s ( f u n : : : o r f u n c t i o n : : : ) , a n d t h e p a t t e r n s p a t t e r n

1

: : : p a t t e r n

n

c o n s i s t i n a s i n g l e v a r i a b l e , a s i n :

l e t r e c i d e n t

1

= f u n : : : a n d : : : a n d i d e n t

n

= f u n : : : i n e x p r

T h i s d e n e s i d e n t

1

: : : i d e n t

n

a s m u t u a l l y r e c u r s i v e f u n c t i o n s l o c a l t o e x p r . T h e b e h a v i o r o f o t h e r

f o r m s o f l e t r e c d e n i t i o n s i s i m p l e m e n t a t i o n - d e p e n d e n t .

2 . 7 . 2 C o n t r o l c o n s t r u c t s

S e q u e n c e

T h e e x p r e s s i o n e x p r

1

; e x p r

2

e v a l u a t e s e x p r

1

r s t , t h e n e x p r

2

, a n d r e t u r n s t h e v a l u e o f e x p r

2

.

C o n d i t i o n a l

T h e e x p r e s s i o n i f e x p r

1

t h e n e x p r

2

e l s e e x p r

3

e v a l u a t e s t o t h e v a l u e o f e x p r

2

i f e x p r

1

e v a l u a t e s

t o t h e b o o l e a n t r u e , a n d t o t h e v a l u e o f e x p r

3

i f e x p r

1

e v a l u a t e s t o t h e b o o l e a n f a l s e .

T h e e l s e e x p r

3

p a r t c a n b e o m i t t e d , i n w h i c h c a s e i t d e f a u l t s t o e l s e ( ) .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 28/218

2 6

C a s e e x p r e s s i o n

T h e e x p r e s s i o n

m a t c h e x p r

w i t h p a t t e r n

1

- > e x p r

1

| : : :

| p a t t e r n

n

- > e x p r

n

m a t c h e s t h e v a l u e o f e x p r a g a i n s t t h e p a t t e r n s p a t t e r n

1

t o p a t t e r n

n

. I f t h e m a t c h i n g a g a i n s t

p a t t e r n

i

s u c c e e d s , t h e a s s o c i a t e d e x p r e s s i o n e x p r

i

i s e v a l u a t e d , a n d i t s v a l u e b e c o m e s t h e v a l u e o f

t h e w h o l e m a t c h e x p r e s s i o n . T h e e v a l u a t i o n o f e x p r

i

t a k e s p l a c e i n a n e n v i r o n m e n t e n r i c h e d b y

t h e b i n d i n g s p e r f o r m e d d u r i n g m a t c h i n g . I f s e v e r a l p a t t e r n s m a t c h t h e v a l u e o f e x p r , t h e o n e t h a t

o c c u r s r s t i n t h e m a t c h e x p r e s s i o n i s s e l e c t e d . I f n o n e o f t h e p a t t e r n s m a t c h t h e v a l u e o f e x p r ,

t h e e x c e p t i o n M a t c h _ f a i l u r e i s r a i s e d .

B o o l e a n o p e r a t o r s

T h e e x p r e s s i o n e x p r

1

& e x p r

2

e v a l u a t e s t o t r u e i f b o t h e x p r

1

a n d e x p r

2

e v a l u a t e t o t r u e ; o t h e r w i s e ,

i t e v a l u a t e s t o f a l s e . T h e r s t c o m p o n e n t , e x p r

1

, i s e v a l u a t e d r s t . T h e s e c o n d c o m p o n e n t , e x p r

2

,

i s n o t e v a l u a t e d i f t h e r s t c o m p o n e n t e v a l u a t e s t o f a l s e . H e n c e , t h e e x p r e s s i o n e x p r

1

& e x p r

2

b e h a v e s e x a c t l y a s

i f e x p r

1

t h e n e x p r

2

e l s e f a l s e .

T h e e x p r e s s i o n e x p r

1

o r e x p r

2

e v a l u a t e s t o t r u e i f o n e o f e x p r

1

a n d e x p r

2

e v a l u a t e s t o t r u e ;

o t h e r w i s e , i t e v a l u a t e s t o f a l s e . T h e r s t c o m p o n e n t , e x p r

1

, i s e v a l u a t e d r s t . T h e s e c o n d

c o m p o n e n t , e x p r

2

, i s n o t e v a l u a t e d i f t h e r s t c o m p o n e n t e v a l u a t e s t o t r u e . H e n c e , t h e e x p r e s s i o n

e x p r

1

o r e x p r

2

b e h a v e s e x a c t l y a s

i f e x p r

1

t h e n t r u e e l s e e x p r

2

.

L o o p s

T h e e x p r e s s i o n w h i l e e x p r

1

d o e x p r

2

d o n e r e p e a t e d l y e v a l u a t e s e x p r

2

w h i l e e x p r

1

e v a l u a t e s t o

t r u e . T h e l o o p c o n d i t i o n e x p r

1

i s e v a l u a t e d a n d t e s t e d a t t h e b e g i n n i n g o f e a c h i t e r a t i o n . T h e

w h o l e w h i l e : : : d o n e e x p r e s s i o n e v a l u a t e s t o t h e u n i t v a l u e ( ) .

T h e e x p r e s s i o n f o r i d e n t = e x p r

1

t o e x p r

2

d o e x p r

3

d o n e r s t e v a l u a t e s t h e e x p r e s s i o n s e x p r

1

a n d e x p r

2

( t h e b o u n d a r i e s ) i n t o i n t e g e r v a l u e s n a n d p . T h e n , t h e l o o p b o d y e x p r

3

i s r e p e a t e d l y

e v a l u a t e d i n a n e n v i r o n m e n t w h e r e t h e l o c a l v a r i a b l e n a m e d i d e n t i s s u c c e s s i v e l y b o u n d t o t h e

v a l u e s n , n + 1 , : : : , p ? 1 , p . T h e l o o p b o d y i s n e v e r e v a l u a t e d i f n > p .

T h e e x p r e s s i o n f o r i d e n t = e x p r

1

d o w n t o e x p r

2

d o e x p r

3

d o n e r s t e v a l u a t e s t h e e x p r e s s i o n s

e x p r

1

a n d e x p r

2

( t h e b o u n d a r i e s ) i n t o i n t e g e r v a l u e s n a n d p . T h e n , t h e l o o p b o d y e x p r

3

i s

r e p e a t e d l y e v a l u a t e d i n a n e n v i r o n m e n t w h e r e t h e l o c a l v a r i a b l e n a m e d i d e n t i s s u c c e s s i v e l y b o u n d

t o t h e v a l u e s n , n ? 1 , : : : , p + 1 , p . T h e l o o p b o d y i s n e v e r e v a l u a t e d i f n < p .

I n b o t h c a s e s , t h e w h o l e f o r e x p r e s s i o n e v a l u a t e s t o t h e u n i t v a l u e ( ) .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 29/218

C h a p t e r 2 . T h e c o r e C a m l L i g h t l a n g u a g e 2 7

E x c e p t i o n h a n d l i n g

T h e e x p r e s s i o n

t r y e x p r

w i t h p a t t e r n

1

- > e x p r

1

| : : :

| p a t t e r n

n

- > e x p r

n

e v a l u a t e s t h e e x p r e s s i o n e x p r a n d r e t u r n s i t s v a l u e i f t h e e v a l u a t i o n o f e x p r d o e s n o t r a i s e a n y

e x c e p t i o n . I f t h e e v a l u a t i o n o f e x p r r a i s e s a n e x c e p t i o n , t h e e x c e p t i o n v a l u e i s m a t c h e d a g a i n s t t h e

p a t t e r n s p a t t e r n

1

t o p a t t e r n

n

. I f t h e m a t c h i n g a g a i n s t p a t t e r n

i

s u c c e e d s , t h e a s s o c i a t e d e x p r e s s i o n

e x p r

i

i s e v a l u a t e d , a n d i t s v a l u e b e c o m e s t h e v a l u e o f t h e w h o l e t r y e x p r e s s i o n . T h e e v a l u a t i o n o f

e x p r

i

t a k e s p l a c e i n a n e n v i r o n m e n t e n r i c h e d b y t h e b i n d i n g s p e r f o r m e d d u r i n g m a t c h i n g . I f s e v e r a l

p a t t e r n s m a t c h t h e v a l u e o f e x p r , t h e o n e t h a t o c c u r s r s t i n t h e t r y e x p r e s s i o n i s s e l e c t e d . I f n o n e

o f t h e p a t t e r n s m a t c h e s t h e v a l u e o f e x p r , t h e e x c e p t i o n v a l u e i s r a i s e d a g a i n , t h e r e b y t r a n s p a r e n t l y

\ p a s s i n g t h r o u g h " t h e t r y c o n s t r u c t .

2 . 7 . 3 O p e r a t i o n s o n d a t a s t r u c t u r e s

P r o d u c t s

T h e e x p r e s s i o n e x p r

1

, : : : , e x p r

n

e v a l u a t e s t o t h e n - t u p l e o f t h e v a l u e s o f e x p r e s s i o n s e x p r

1

t o

e x p r

n

. T h e e v a l u a t i o n o r d e r f o r t h e s u b e x p r e s s i o n s i s n o t s p e c i e d .

V a r i a n t s

T h e e x p r e s s i o n n c c o n s t r e x p r e v a l u a t e s t o t h e v a r i a n t v a l u e w h o s e c o n s t r u c t o r i s n c c o n s t r , a n d

w h o s e a r g u m e n t i s t h e v a l u e o f e x p r .

F o r l i s t s , s o m e s y n t a c t i c s u g a r i s p r o v i d e d . T h e e x p r e s s i o n e x p r

1

: : e x p r

2

s t a n d s f o r t h e

c o n s t r u c t o r p r e f i x : : a p p l i e d t o t h e a r g u m e n t ( e x p r

1

, e x p r

2

) , a n d t h e r e f o r e e v a l u a t e s t o

t h e l i s t w h o s e h e a d i s t h e v a l u e o f e x p r

1

a n d w h o s e t a i l i s t h e v a l u e o f e x p r

2

. T h e e x p r e s s i o n

e x p r

1

; : : : ; e x p r

n

] i s e q u i v a l e n t t o e x p r

1

: : : : : : : e x p r

n

: : ] , a n d t h e r e f o r e e v a l u a t e s t o t h e

l i s t w h o s e e l e m e n t s a r e t h e v a l u e s o f e x p r

1

t o e x p r

n

.

R e c o r d s

T h e e x p r e s s i o n { l a b e l

1

= e x p r

1

; : : : ; l a b e l

n

= e x p r

n

} e v a l u a t e s t o t h e r e c o r d v a l u e

{ l a b e l

1

= v

1

; : : : ; l a b e l

n

= v

n

} , w h e r e v

i

i s t h e v a l u e o f e x p r

i

f o r i = 1 ; : : : ; n . T h e l a b e l s l a b e l

1

t o l a b e l

n

m u s t a l l b e l o n g t o t h e s a m e r e c o r d t y p e s ; a l l l a b e l s b e l o n g i n g t o t h i s r e c o r d t y p e m u s t

a p p e a r e x a c t l y o n c e i n t h e r e c o r d e x p r e s s i o n , t h o u g h t h e y c a n a p p e a r i n a n y o r d e r . T h e o r d e r i n

w h i c h e x p r

1

t o e x p r

n

a r e e v a l u a t e d i s n o t s p e c i e d .

T h e e x p r e s s i o n e x p r

1

. l a b e l e v a l u a t e s e x p r

1

t o a r e c o r d v a l u e , a n d r e t u r n s t h e v a l u e a s s o c i a t e d

t o l a b e l i n t h i s r e c o r d v a l u e .

T h e e x p r e s s i o n e x p r

1

. l a b e l < - e x p r

2

e v a l u a t e s e x p r

1

t o a r e c o r d v a l u e , w h i c h i s t h e n m o d i e d

i n - p l a c e b y r e p l a c i n g t h e v a l u e a s s o c i a t e d t o l a b e l i n t h i s r e c o r d b y t h e v a l u e o f e x p r

2

. T h i s

o p e r a t i o n i s p e r m i t t e d o n l y i f l a b e l h a s b e e n d e c l a r e d m u t a b l e i n t h e d e n i t i o n o f t h e r e c o r d t y p e .

T h e w h o l e e x p r e s s i o n e x p r

1

. l a b e l < - e x p r

2

e v a l u a t e s t o t h e u n i t v a l u e ( ) .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 30/218

2 8

A r r a y s

T h e e x p r e s s i o n | e x p r

1

; : : : ; e x p r

n

| ] e v a l u a t e s t o a n - e l e m e n t a r r a y , w h o s e e l e m e n t s a r e i n i -

t i a l i z e d w i t h t h e v a l u e s o f e x p r

1

t o e x p r

n

r e s p e c t i v e l y . T h e o r d e r i n w h i c h t h e s e e x p r e s s i o n s a r e

e v a l u a t e d i s u n s p e c i e d .

T h e e x p r e s s i o n e x p r

1

. ( e x p r

2

) i s e q u i v a l e n t t o t h e a p p l i c a t i o n v e c t _ i t e m e x p r

1

e x p r

2

. I n t h e

i n i t i a l e n v i r o n m e n t , t h e i d e n t i e r v e c t _ i t e m r e s o l v e s t o a b u i l t - i n f u n c t i o n t h a t r e t u r n s t h e v a l u e

o f e l e m e n t n u m b e r e x p r

2

i n t h e a r r a y d e n o t e d b y e x p r

1

. T h e r s t e l e m e n t h a s n u m b e r 0 ; t h e l a s t

e l e m e n t h a s n u m b e r n ? 1 , w h e r e n i s t h e s i z e o f t h e a r r a y . T h e e x c e p t i o n I n v a l i d _ a r g u m e n t i s

r a i s e d i f t h e a c c e s s i s o u t o f b o u n d s .

T h e e x p r e s s i o n e x p r

1

. ( e x p r

2

) < - e x p r

3

i s e q u i v a l e n t t o v e c t _ a s s i g n e x p r

1

e x p r

2

e x p r

3

.

I n t h e i n i t i a l e n v i r o n m e n t , t h e i d e n t i e r v e c t _ a s s i g n r e s o l v e s t o a b u i l t - i n f u n c t i o n t h a t m o d i e s

i n - p l a c e t h e a r r a y d e n o t e d b y e x p r

1

, r e p l a c i n g e l e m e n t n u m b e r e x p r

2

b y t h e v a l u e o f e x p r

3

. T h e

e x c e p t i o n I n v a l i d _ a r g u m e n t i s r a i s e d i f t h e a c c e s s i s o u t o f b o u n d s . T h e b u i l t - i n f u n c t i o n r e t u r n s

( ) . H e n c e , t h e w h o l e e x p r e s s i o n e x p r

1

. ( e x p r

2

) < - e x p r

3

e v a l u a t e s t o t h e u n i t v a l u e ( ) .

T h i s b e h a v i o r o f t h e t w o c o n s t r u c t s e x p r

1

. ( e x p r

2

) a n d e x p r

1

. ( e x p r

2

) < - e x p r

3

m a y c h a n g e

i f t h e m e a n i n g o f t h e i d e n t i e r s v e c t _ i t e m a n d v e c t _ a s s i g n i s c h a n g e d , e i t h e r b y r e d e n i t i o n o r

b y m o d i c a t i o n o f t h e l i s t o f o p e n e d m o d u l e s . S e e t h e d i s c u s s i o n b e l o w o n o p e r a t o r s .

2 . 7 . 4 O p e r a t o r s

T h e o p e r a t o r s w r i t t e n i n f i x - o p i n t h e g r a m m a r a b o v e c a n a p p e a r i n i n x p o s i t i o n ( b e t w e e n t w o

e x p r e s s i o n s ) . T h e o p e r a t o r s w r i t t e n p r e f i x - o p i n t h e g r a m m a r a b o v e c a n a p p e a r i n p r e x p o s i t i o n

( i n f r o n t o f a n e x p r e s s i o n ) .

T h e e x p r e s s i o n p r e x - o p e x p r i s i n t e r p r e t e d a s t h e a p p l i c a t i o n i d e n t e x p r , w h e r e i d e n t i s

t h e i d e n t i e r a s s o c i a t e d t o t h e o p e r a t o r p r e x - o p i n t h e t a b l e b e l o w . S i m i l a r l y , t h e e x p r e s s i o n

e x p r

1

i n x - o p e x p r

2

i s i n t e r p r e t e d a s t h e a p p l i c a t i o n i d e n t e x p r

1

e x p r

2

, w h e r e i d e n t i s t h e i d e n -

t i e r a s s o c i a t e d t o t h e o p e r a t o r i n x - o p i n t h e t a b l e b e l o w . T h e i d e n t i e r s w r i t t e n i d e n t a b o v e

a r e t h e n e v a l u a t e d f o l l o w i n g t h e r u l e s i n s e c t i o n 2 . 7 . 1 . I n t h e i n i t i a l e n v i r o n m e n t , t h e y e v a l u a t e

t o b u i l t - i n f u n c t i o n s w h o s e b e h a v i o r i s d e s c r i b e d i n t h e t a b l e . T h e b e h a v i o r o f t h e c o n s t r u c t i o n s

p r e x - o p e x p r a n d e x p r

1

i n x - o p e x p r

2

m a y c h a n g e i f t h e m e a n i n g o f t h e i d e n t i e r s a s s o c i a t e d t o

p r e x - o p o r i n x - o p i s c h a n g e d , e i t h e r b y r e d e n i t i o n o f t h e i d e n t i e r s , o r b y m o d i c a t i o n o f t h e

l i s t o f o p e n e d m o d u l e s , t h r o u g h t h e # o p e n a n d # c l o s e d i r e c t i v e s .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 31/218

C h a p t e r 2 . T h e c o r e C a m l L i g h t l a n g u a g e 2 9

O p e r a t o r A s s o c i a t e d B e h a v i o r i n t h e d e f a u l t e n v i r o n m e n t

i d e n t i e r

+ p r e f i x + I n t e g e r a d d i t i o n .

- ( i n x ) p r e f i x - I n t e g e r s u b t r a c t i o n .

- ( p r e x ) m i n u s I n t e g e r n e g a t i o n .

* p r e f i x * I n t e g e r m u l t i p l i c a t i o n .

/ p r e f i x / I n t e g e r d i v i s i o n . R a i s e D i v i s i o n _ b y _ z e r o i f s e c o n d a r g u -

m e n t i s z e r o . T h e r e s u l t i s u n s p e c i e d i f e i t h e r a r g u m e n t i s

n e g a t i v e .

m o d p r e f i x m o d I n t e g e r m o d u l u s . R a i s e D i v i s i o n _ b y _ z e r o i f s e c o n d a r g u -

m e n t i s z e r o . T h e r e s u l t i s u n s p e c i e d i f e i t h e r a r g u m e n t i s

n e g a t i v e .

+ . p r e f i x + . F l o a t i n g - p o i n t a d d i t i o n .

- . ( i n x ) p r e f i x - . F l o a t i n g - p o i n t s u b t r a c t i o n .

- . ( p r e x ) m i n u s _ f l o a t F l o a t i n g - p o i n t n e g a t i o n .

* . p r e f i x * . F l o a t i n g - p o i n t m u l t i p l i c a t i o n .

/ . p r e f i x / . F l o a t i n g - p o i n t d i v i s i o n . R a i s e D i v i s i o n _ b y _ z e r o i f s e c o n d

a r g u m e n t i s z e r o .

* * p r e f i x * * F l o a t i n g - p o i n t e x p o n e n t i a t i o n .

@ p r e f i x @ L i s t c o n c a t e n a t i o n .

^ p r e f i x ^ S t r i n g c o n c a t e n a t i o n .

! p r e f i x ! D e r e f e r e n c i n g ( r e t u r n t h e c u r r e n t c o n t e n t s o f a r e f e r e n c e ) .

: = p r e f i x : = R e f e r e n c e a s s i g n m e n t ( u p d a t e t h e r e f e r e n c e g i v e n a s r s t

a r g u m e n t w i t h t h e v a l u e o f t h e s e c o n d a r g u m e n t ) .

= p r e f i x = S t r u c t u r a l e q u a l i t y t e s t .

< > p r e f i x < > S t r u c t u r a l i n e q u a l i t y t e s t .

= = p r e f i x = = P h y s i c a l e q u a l i t y t e s t .

! = p r e f i x ! = P h y s i c a l i n e q u a l i t y t e s t .

< p r e f i x < T e s t \ l e s s t h a n " o n i n t e g e r s .

< = p r e f i x < = T e s t \ l e s s t h a n o r e q u a l " o n i n t e g e r s .

> p r e f i x > T e s t \ g r e a t e r t h a n " o n i n t e g e r s .

> = p r e f i x > = T e s t \ g r e a t e r t h a n o r e q u a l " o n i n t e g e r s .

< . p r e f i x < . T e s t \ l e s s t h a n " o n o a t i n g - p o i n t n u m b e r s .

< = . p r e f i x < = . T e s t \ l e s s t h a n o r e q u a l " o n o a t i n g - p o i n t n u m b e r s .

> . p r e f i x > . T e s t \ g r e a t e r t h a n " o n o a t i n g - p o i n t n u m b e r s .

> = . p r e f i x > = . T e s t \ g r e a t e r t h a n o r e q u a l " o n o a t i n g - p o i n t n u m b e r s .

T h e b e h a v i o r o f t h e + , - , * , / , m o d , + . , - . , * . o r / . o p e r a t o r s i s u n s p e c i e d i f t h e r e s u l t f a l l s

o u t s i d e o f t h e r a n g e o f r e p r e s e n t a b l e i n t e g e r s o r o a t i n g - p o i n t n u m b e r s , r e s p e c t i v e l y . S e e c h a p t e r 1 3

f o r a m o r e p r e c i s e d e s c r i p t i o n o f t h e b e h a v i o r o f t h e o p e r a t o r s a b o v e .

2 . 8 G l o b a l d e n i t i o n s

T h i s s e c t i o n d e s c r i b e s t h e c o n s t r u c t s t h a t b i n d g l o b a l i d e n t i e r s ( v a l u e v a r i a b l e s , v a l u e c o n s t r u c t o r s ,

t y p e c o n s t r u c t o r s , r e c o r d l a b e l s ) .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 32/218

3 0

2 . 8 . 1 T y p e d e n i t i o n s

t y p e - d e n i t i o n : : = t y p e t y p e d e f f a n d t y p e d e f g

t y p e d e f : : = t y p e - p a r a m s i d e n t = c o n s t r - d e c l f | c o n s t r - d e c l g

j t y p e - p a r a m s i d e n t = { l a b e l - d e c l f ; l a b e l - d e c l g }

j t y p e - p a r a m s i d e n t = = t y p e x p r

j t y p e - p a r a m s i d e n t

t y p e - p a r a m s : : = n o t h i n g

j ' i d e n t

j ( ' i d e n t f , ' i d e n t g )

c o n s t r - d e c l : : = i d e n t

j i d e n t o f t y p e x p r

l a b e l - d e c l : : = i d e n t : t y p e x p r

j m u t a b l e i d e n t : t y p e x p r

T y p e d e n i t i o n s b i n d t y p e c o n s t r u c t o r s t o d a t a t y p e s : e i t h e r v a r i a n t t y p e s , r e c o r d t y p e s , t y p e

a b b r e v i a t i o n s , o r a b s t r a c t d a t a t y p e s .

T y p e d e n i t i o n s a r e i n t r o d u c e d b y t h e t y p e k e y w o r d , a n d c o n s i s t i n o n e o r s e v e r a l s i m p l e

d e n i t i o n s , p o s s i b l y m u t u a l l y r e c u r s i v e , s e p a r a t e d b y t h e a n d k e y w o r d . E a c h s i m p l e d e n i t i o n

d e n e s o n e t y p e c o n s t r u c t o r .

A s i m p l e d e n i t i o n c o n s i s t s i n a n i d e n t i e r , p o s s i b l y p r e c e d e d b y o n e o r s e v e r a l t y p e p a r a m e t e r s ,

a n d f o l l o w e d b y a d a t a t y p e d e s c r i p t i o n . T h e i d e n t i e r i s t h e l o c a l n a m e o f t h e t y p e c o n s t r u c t o r

b e i n g d e n e d . ( T h e m o d u l e n a m e f o r t h i s t y p e c o n s t r u c t o r i s t h e n a m e o f t h e m o d u l e b e i n g

c o m p i l e d . ) T h e o p t i o n a l t y p e p a r a m e t e r s a r e e i t h e r o n e t y p e v a r i a b l e ' i d e n t , f o r t y p e c o n s t r u c t o r s

w i t h o n e p a r a m e t e r , o r a l i s t o f t y p e v a r i a b l e s ( ' i d e n t

1

; : : : ; ' i d e n t

n

) , f o r t y p e c o n s t r u c t o r s w i t h

s e v e r a l p a r a m e t e r s . T h e s e t y p e p a r a m e t e r s c a n a p p e a r i n t h e t y p e e x p r e s s i o n s o f t h e r i g h t - h a n d

s i d e o f t h e d e n i t i o n .

V a r i a n t t y p e s

T h e t y p e d e n i t i o n t y p e p a r a m s i d e n t = c o n s t r - d e c l

1

| : : : | c o n s t r - d e c l

n

d e n e s a v a r i a n t t y p e . T h e

c o n s t r u c t o r d e c l a r a t i o n s c o n s t r - d e c l

1

; : : : ; c o n s t r - d e c l

n

d e s c r i b e t h e c o n s t r u c t o r s a s s o c i a t e d t o t h i s

v a r i a n t t y p e . T h e c o n s t r u c t o r d e c l a r a t i o n i d e n t o f t y p e x p r d e c l a r e s t h e l o c a l n a m e i d e n t ( i n t h e

m o d u l e b e i n g c o m p i l e d ) a s a n o n - c o n s t a n t c o n s t r u c t o r , w h o s e a r g u m e n t h a s t y p e t y p e x p r . T h e

c o n s t r u c t o r d e c l a r a t i o n i d e n t d e c l a r e s t h e l o c a l n a m e i d e n t ( i n t h e m o d u l e b e i n g c o m p i l e d ) a s a

c o n s t a n t c o n s t r u c t o r .

R e c o r d t y p e s

T h e t y p e d e n i t i o n t y p e p a r a m s i d e n t = { l a b e l - d e c l

1

; : : : ; l a b e l - d e c l

n

} d e n e s a r e c o r d t y p e . T h e

l a b e l d e c l a r a t i o n s l a b e l - d e c l

1

; : : : ; l a b e l - d e c l

n

d e s c r i b e t h e l a b e l s a s s o c i a t e d t o t h i s r e c o r d t y p e . T h e

l a b e l d e c l a r a t i o n i d e n t : t y p e x p r d e c l a r e s t h e l o c a l n a m e i d e n t i n t h e m o d u l e b e i n g c o m p i l e d a s a

l a b e l , w h o s e a r g u m e n t h a s t y p e t y p e x p r . T h e l a b e l d e c l a r a t i o n m u t a b l e i d e n t : t y p e x p r b e h a v e s

s i m i l a r l y ; i n a d d i t i o n , i t a l l o w s p h y s i c a l m o d i c a t i o n o v e r t h e a r g u m e n t t o t h i s l a b e l .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 33/218

C h a p t e r 2 . T h e c o r e C a m l L i g h t l a n g u a g e 3 1

T y p e a b b r e v i a t i o n s

T h e t y p e d e n i t i o n t y p e p a r a m s i d e n t = = t y p e x p r d e n e s t h e t y p e c o n s t r u c t o r i d e n t a s a n a b b r e v i -

a t i o n f o r t h e t y p e e x p r e s s i o n t y p e x p r .

A b s t r a c t t y p e s

T h e t y p e d e n i t i o n t y p e p a r a m s i d e n t d e n e s i d e n t a s a n a b s t r a c t t y p e . W h e n a p p e a r i n g i n a m o d -

u l e i n t e r f a c e , t h i s d e n i t i o n a l l o w s e x p o r t i n g a t y p e c o n s t r u c t o r w h i l e h i d i n g h o w i t i s r e p r e s e n t e d

i n t h e m o d u l e i m p l e m e n t a t i o n .

2 . 8 . 2 E x c e p t i o n d e n i t i o n s

e x c e p t i o n - d e n i t i o n : : = e x c e p t i o n c o n s t r - d e c l f a n d c o n s t r - d e c l g

E x c e p t i o n d e n i t i o n s a d d n e w c o n s t r u c t o r s t o t h e b u i l t - i n v a r i a n t t y p e e x n o f e x c e p t i o n v a l u e s .

T h e c o n s t r u c t o r s a r e d e c l a r e d a s f o r a d e n i t i o n o f a v a r i a n t t y p e .

2 . 9 D i r e c t i v e s

d i r e c t i v e : : = # o p e n s t r i n g

j # c l o s e s t r i n g

j # i d e n t s t r i n g

D i r e c t i v e s c o n t r o l t h e b e h a v i o r o f t h e c o m p i l e r . T h e y a p p l y t o t h e r e m a i n d e r o f t h e c u r r e n t

c o m p i l a t i o n u n i t .

T h e t w o d i r e c t i v e s # o p e n a n d # c l o s e m o d i f y t h e l i s t o f o p e n e d m o d u l e s , t h a t t h e c o m p i l e r u s e s

t o c o m p l e t e u n q u a l i e d i d e n t i e r s , a s d e s c r i b e d i n s e c t i o n 2 . 2 . T h e d i r e c t i v e # o p e n s t r i n g a d d s t h e

m o d u l e w h o s e n a m e i s g i v e n b y t h e s t r i n g c o n s t a n t s t r i n g t o t h e l i s t o f o p e n e d m o d u l e s , i n r s t

p o s i t i o n . T h e d i r e c t i v e # c l o s e s t r i n g r e m o v e s t h e r s t o c c u r r e n c e o f t h e m o d u l e w h o s e n a m e i s

g i v e n b y t h e s t r i n g c o n s t a n t s t r i n g f r o m t h e l i s t o f o p e n e d m o d u l e s .

I m p l e m e n t a t i o n s c a n p r o v i d e o t h e r d i r e c t i v e s , p r o v i d e d t h e y f o l l o w t h e s y n t a x # i d e n t s t r i n g ,

w h e r e i d e n t i s t h e n a m e o f t h e d i r e c t i v e , a n d t h e s t r i n g c o n s t a n t s t r i n g i s t h e a r g u m e n t t o t h e

d i r e c t i v e . T h e b e h a v i o r o f t h e s e a d d i t i o n a l d i r e c t i v e s i s i m p l e m e n t a t i o n - d e p e n d e n t .

2 . 1 0 M o d u l e i m p l e m e n t a t i o n s

i m p l e m e n t a t i o n : : = f i m p l - p h r a s e ; ; g

i m p l - p h r a s e : : = e x p r

j v a l u e - d e n i t i o n

j t y p e - d e n i t i o n

j e x c e p t i o n - d e n i t i o n

j d i r e c t i v e

v a l u e - d e n i t i o n : : = l e t r e c ] l e t - b i n d i n g f a n d l e t - b i n d i n g g

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 34/218

3 2

A m o d u l e i m p l e m e n t a t i o n c o n s i s t s i n a s e q u e n c e o f i m p l e m e n t a t i o n p h r a s e s , t e r m i n a t e d b y

d o u b l e s e m i c o l o n s . A n i m p l e m e n t a t i o n p h r a s e i s e i t h e r a n e x p r e s s i o n , a v a l u e d e n i t i o n , a t y p e o r

e x c e p t i o n d e n i t i o n , o r a d i r e c t i v e . A t r u n - t i m e , i m p l e m e n t a t i o n p h r a s e s a r e e v a l u a t e d s e q u e n t i a l l y ,

i n t h e o r d e r i n w h i c h t h e y a p p e a r i n t h e m o d u l e i m p l e m e n t a t i o n .

I m p l e m e n t a t i o n p h r a s e s c o n s i s t i n g i n a n e x p r e s s i o n a r e e v a l u a t e d f o r t h e i r s i d e - e e c t s .

V a l u e d e n i t i o n s b i n d g l o b a l v a l u e v a r i a b l e s i n t h e s a m e w a y a s a l e t : : : i n : : : e x p r e s s i o n b i n d s

l o c a l v a r i a b l e s . T h e e x p r e s s i o n s a r e e v a l u a t e d , a n d t h e i r v a l u e s a r e m a t c h e d a g a i n s t t h e l e f t - h a n d

s i d e s o f t h e = s i d e s , a s e x p l a i n e d i n s e c t i o n 2 . 7 . 1 . I f t h e m a t c h i n g s u c c e e d s , t h e b i n d i n g s o f i d e n t i e r s

t o v a l u e s p e r f o r m e d d u r i n g m a t c h i n g a r e i n t e r p r e t e d a s b i n d i n g s t o t h e g l o b a l v a l u e v a r i a b l e s w h o s e

l o c a l n a m e i s t h e i d e n t i e r , a n d w h o s e m o d u l e n a m e i s t h e n a m e o f t h e m o d u l e . I f t h e m a t c h i n g

f a i l s , t h e e x c e p t i o n M a t c h _ f a i l u r e i s r a i s e d . T h e s c o p e o f t h e s e b i n d i n g s i s t h e p h r a s e s t h a t f o l l o w

t h e v a l u e d e n i t i o n i n t h e m o d u l e i m p l e m e n t a t i o n .

T y p e a n d e x c e p t i o n d e n i t i o n s i n t r o d u c e t y p e c o n s t r u c t o r s , v a r i a n t c o n s t r u c t o r s a n d r e c o r d

l a b e l s a s d e s c r i b e d i n s e c t i o n s 2 . 8 . 1 a n d 2 . 8 . 2 . T h e s c o p e o f t h e s e d e n i t i o n s i s t h e p h r a s e s t h a t

f o l l o w t h e v a l u e d e n i t i o n i n t h e m o d u l e i m p l e m e n t a t i o n . T h e e v a l u a t i o n o f a n i m p l e m e n t a t i o n

p h r a s e c o n s i s t i n g i n a t y p e o r e x c e p t i o n d e n i t i o n p r o d u c e s n o e e c t a t r u n - t i m e .

D i r e c t i v e s m o d i f y t h e b e h a v i o r o f t h e c o m p i l e r o n t h e s u b s e q u e n t p h r a s e s o f t h e m o d u l e i m -

p l e m e n t a t i o n , a s d e s c r i b e d i n s e c t i o n 2 . 9 . T h e e v a l u a t i o n o f a n i m p l e m e n t a t i o n p h r a s e c o n s i s t i n g

i n a d i r e c t i v e p r o d u c e s n o e e c t a t r u n - t i m e . D i r e c t i v e s a p p l y o n l y t o t h e m o d u l e c u r r e n t l y b e i n g

c o m p i l e d ; i n p a r t i c u l a r , t h e y h a v e n o e e c t o n o t h e r m o d u l e s t h a t r e f e r t o g l o b a l s e x p o r t e d b y t h e

m o d u l e b e i n g c o m p i l e d .

2 . 1 1 M o d u l e i n t e r f a c e s

i n t e r f a c e : : = f i n t f - p h r a s e ; ; g

i n t f - p h r a s e : : = v a l u e - d e c l a r a t i o n

j t y p e - d e n i t i o n

j e x c e p t i o n - d e n i t i o n

j d i r e c t i v e

v a l u e - d e c l a r a t i o n : : = v a l u e i d e n t : t y p e x p r f a n d i d e n t : t y p e x p r g

M o d u l e i n t e r f a c e s d e c l a r e t h e g l o b a l o b j e c t s ( v a l u e v a r i a b l e s , t y p e c o n s t r u c t o r s , v a r i a n t c o n -

s t r u c t o r s , r e c o r d l a b e l s ) t h a t a m o d u l e e x p o r t s , t h a t i s , m a k e s a v a i l a b l e t o o t h e r m o d u l e s . O t h e r

m o d u l e s c a n r e f e r t o t h e s e g l o b a l s u s i n g q u a l i e d i d e n t i e r s o r t h e # o p e n d i r e c t i v e , a s e x p l a i n e d i n

s e c t i o n 2 . 2 .

A m o d u l e i n t e r f a c e c o n s i s t s i n a s e q u e n c e o f i n t e r f a c e p h r a s e s , t e r m i n a t e d b y d o u b l e s e m i c o l o n s .

A n i n t e r f a c e p h r a s e i s e i t h e r a v a l u e d e c l a r a t i o n , a t y p e d e n i t i o n , a n e x c e p t i o n d e n i t i o n , o r a

d i r e c t i v e .

V a l u e d e c l a r a t i o n s d e c l a r e g l o b a l v a l u e v a r i a b l e s t h a t a r e e x p o r t e d b y t h e m o d u l e i m p l e m e n t a -

t i o n , a n d t h e t y p e s w i t h w h i c h t h e y a r e e x p o r t e d . T h e m o d u l e i m p l e m e n t a t i o n m u s t d e n e t h e s e

v a r i a b l e s , w i t h t y p e s a t l e a s t a s g e n e r a l a s t h e t y p e s d e c l a r e d i n t h e i n t e r f a c e . T h e s c o p e o f t h e

b i n d i n g s f o r t h e s e g l o b a l v a r i a b l e s e x t e n d s f r o m t h e m o d u l e i m p l e m e n t a t i o n i t s e l f t o a l l m o d u l e s

t h a t r e f e r t o t h o s e v a r i a b l e s .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 35/218

C h a p t e r 2 . T h e c o r e C a m l L i g h t l a n g u a g e 3 3

T y p e o r e x c e p t i o n d e n i t i o n s i n t r o d u c e t y p e c o n s t r u c t o r s , v a r i a n t c o n s t r u c t o r s a n d r e c o r d l a -

b e l s a s d e s c r i b e d i n s e c t i o n s 2 . 8 . 1 a n d 2 . 8 . 2 . E x c e p t i o n d e n i t i o n s a n d t y p e d e n i t i o n s t h a t a r e n o t

a b s t r a c t t y p e d e c l a r a t i o n s a l s o t a k e e e c t i n t h e m o d u l e i m p l e m e n t a t i o n ; t h a t i s , t h e t y p e c o n s t r u c -

t o r s , v a r i a n t c o n s t r u c t o r s a n d r e c o r d l a b e l s t h e y d e n e a r e c o n s i d e r e d b o u n d o n e n t r a n c e t o t h e

m o d u l e i m p l e m e n t a t i o n , a n d c a n b e r e f e r r e d t o b y t h e i m p l e m e n t a t i o n p h r a s e s . T y p e d e n i t i o n s

t h a t a r e n o t a b s t r a c t t y p e d e c l a r a t i o n s m u s t n o t b e r e d e n e d i n t h e m o d u l e i m p l e m e n t a t i o n . I n

c o n t r a s t , t h e t y p e c o n s t r u c t o r s t h a t a r e d e c l a r e d a b s t r a c t i n a m o d u l e i n t e r f a c e m u s t b e d e n e d i n

t h e m o d u l e i m p l e m e n t a t i o n , w i t h t h e s a m e n a m e s .

D i r e c t i v e s m o d i f y t h e b e h a v i o r o f t h e c o m p i l e r o n t h e s u b s e q u e n t p h r a s e s o f t h e m o d u l e i n t e r -

f a c e , a s d e s c r i b e d i n s e c t i o n 2 . 9 . D i r e c t i v e s a p p l y o n l y t o t h e i n t e r f a c e c u r r e n t l y b e i n g c o m p i l e d ;

i n p a r t i c u l a r , t h e y h a v e n o e e c t o n o t h e r m o d u l e s t h a t r e f e r t o g l o b a l s e x p o r t e d b y t h e i n t e r f a c e

b e i n g c o m p i l e d .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 36/218

3 4

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 37/218

C h a p t e r 3

L a n g u a g e e x t e n s i o n s

T h i s c h a p t e r d e s c r i b e s t h e l a n g u a g e f e a t u r e s t h a t a r e i m p l e m e n t e d i n C a m l L i g h t , b u t n o t d e s c r i b e d

i n t h e C a m l L i g h t r e f e r e n c e m a n u a l . I n c o n t r a s t w i t h t h e f a i r l y s t a b l e k e r n e l l a n g u a g e t h a t i s

d e s c r i b e d i n t h e r e f e r e n c e m a n u a l , t h e e x t e n s i o n s p r e s e n t e d h e r e a r e s t i l l e x p e r i m e n t a l , a n d m a y

b e r e m o v e d o r c h a n g e d i n t h e f u t u r e .

3 . 1 S t r e a m s , p a r s e r s , a n d p r i n t e r s

C a m l L i g h t c o m p r i s e s a b u i l t - i n t y p e f o r s t r e a m s ( p o s s i b l y i n n i t e s e q u e n c e s o f e l e m e n t s , t h a t a r e

e v a l u a t e d o n d e m a n d ) , a n d a s s o c i a t e d s t r e a m e x p r e s s i o n s , t o b u i l d s t r e a m s , a n d s t r e a m p a t t e r n s ,

t o d e s t r u c t u r e s t r e a m s . S t r e a m s a n d s t r e a m p a t t e r n s p r o v i d e a n a t u r a l a p p r o a c h t o t h e w r i t i n g o f

r e c u r s i v e - d e s c e n t p a r s e r s .

S t r e a m s a r e p r e s e n t e d b y t h e f o l l o w i n g e x t e n s i o n s t o t h e s y n t a c t i c c l a s s e s o f e x p r e s s i o n s :

e x p r : : = : : :

j < > ]

j < s t r e a m - c o m p o n e n t f ; s t r e a m - c o m p o n e n t g > ]

j f u n c t i o n s t r e a m - m a t c h i n g

j m a t c h e x p r w i t h s t r e a m - m a t c h i n g

s t r e a m - c o m p o n e n t : : = ' e x p r

j e x p r

s t r e a m - m a t c h i n g : : = s t r e a m - p a t t e r n - > e x p r f | s t r e a m - p a t t e r n - > e x p r g

s t r e a m - p a t t e r n : : = < > ]

j < s t r e a m - c o m p - p a t f ; s t r e a m - c o m p - p a t g > ]

s t r e a m - c o m p - p a t : : = ' p a t t e r n

j e x p r p a t t e r n

j i d e n t

S t r e a m e x p r e s s i o n s a r e b r a c k e t e d b y < a n d > ] . T h e y r e p r e s e n t t h e c o n c a t e n a t i o n o f t h e i r

c o m p o n e n t s . T h e c o m p o n e n t ' e x p r r e p r e s e n t s t h e o n e - e l e m e n t s t r e a m w h o s e e l e m e n t i s t h e v a l u e

3 5

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 38/218

3 6

o f e x p r . T h e c o m p o n e n t e x p r r e p r e s e n t s a s u b - s t r e a m . F o r i n s t a n c e , i f b o t h s a n d t a r e s t r e a m s

o f i n t e g e r s , t h e n < ' 1 ; s ; t ; ' 2 > ] i s a s t r e a m o f i n t e g e r s c o n t a i n i n g t h e e l e m e n t 1 , t h e n t h e

e l e m e n t s o f s , t h e n t h o s e o f t , a n d n a l l y 2 . T h e e m p t y s t r e a m i s d e n o t e d b y < > ] .

U n l i k e a n y o t h e r k i n d o f e x p r e s s i o n s i n t h e l a n g u a g e , s t r e a m e x p r e s s i o n s a r e s u b m i t t e d t o l a z y

e v a l u a t i o n : t h e c o m p o n e n t s a r e n o t e v a l u a t e d w h e n t h e s t r e a m i s b u i l t , b u t o n l y w h e n t h e y a r e

a c c e s s e d d u r i n g s t r e a m m a t c h i n g . T h e c o m p o n e n t s a r e e v a l u a t e d o n c e , t h e r s t t i m e t h e y a r e

a c c e s s e d ; t h e f o l l o w i n g a c c e s s e s r e u s e t h e v a l u e c o m p u t e d t h e r s t t i m e .

S t r e a m p a t t e r n s , a l s o b r a c k e t e d b y < a n d > ] , d e s c r i b e i n i t i a l s e g m e n t s o f s t r e a m s . I n p a r t i c u l a r ,

t h e s t r e a m p a t t e r n < > ] m a t c h e s a l l s t r e a m s . S t r e a m p a t t e r n c o m p o n e n t s a r e m a t c h e d a g a i n s t t h e

c o r r e s p o n d i n g e l e m e n t s o f a s t r e a m . T h e c o m p o n e n t ' p a t t e r n m a t c h e s t h e c o r r e s p o n d i n g s t r e a m

e l e m e n t a g a i n s t t h e p a t t e r n . T h e c o m p o n e n t e x p r p a t t e r n a p p l i e s t h e f u n c t i o n d e n o t e d b y e x p r t o

t h e c u r r e n t s t r e a m , t h e n m a t c h e s t h e r e s u l t o f t h e f u n c t i o n a g a i n s t p a t t e r n . F i n a l l y , t h e c o m p o n e n t

i d e n t s i m p l y b i n d s t h e i d e n t i e r t o t h e s t r e a m b e i n g m a t c h e d . ( T h e c u r r e n t i m p l e m e n t a t i o n l i m i t s

i d e n t t o a p p e a r l a s t i n a s t r e a m p a t t e r n . )

S t r e a m m a t c h i n g p r o c e e d s d e s t r u c t i v e l y : o n c e a c o m p o n e n t h a s b e e n m a t c h e d , i t i s d i s c a r d e d

f r o m t h e s t r e a m ( b y i n - p l a c e m o d i c a t i o n ) .

S t r e a m m a t c h i n g p r o c e e d s i n t w o s t e p s : r s t , a p a t t e r n i s s e l e c t e d b y m a t c h i n g t h e s t r e a m

a g a i n s t t h e r s t c o m p o n e n t s o f t h e s t r e a m p a t t e r n s ; t h e n , t h e f o l l o w i n g c o m p o n e n t s o f t h e s e l e c t e d

p a t t e r n a r e c h e c k e d a g a i n s t t h e s t r e a m . I f t h e f o l l o w i n g c o m p o n e n t s d o n o t m a t c h , t h e e x c e p t i o n

P a r s e _ e r r o r i s r a i s e d . T h e r e i s n o b a c k t r a c k i n g h e r e : s t r e a m m a t c h i n g c o m m i t s t o t h e p a t t e r n

s e l e c t e d a c c o r d i n g t o t h e r s t e l e m e n t . I f n o n e o f t h e r s t c o m p o n e n t s o f t h e s t r e a m p a t t e r n s m a t c h ,

t h e e x c e p t i o n P a r s e _ f a i l u r e i s r a i s e d . T h e P a r s e _ f a i l u r e e x c e p t i o n c a u s e s t h e n e x t a l t e r n a t i v e

t o b e t r i e d , i f i t o c c u r s d u r i n g t h e m a t c h i n g o f t h e r s t e l e m e n t o f a s t r e a m , b e f o r e m a t c h i n g h a s

c o m m i t t e d t o o n e p a t t e r n .

S e e F u n c t i o n a l p r o g r a m m i n g u s i n g C a m l L i g h t f o r a m o r e g e n t l e i n t r o d u c t i o n s t o s t r e a m s , a n d

f o r s o m e e x a m p l e s o f t h e i r u s e i n w r i t i n g p a r s e r s . A m o r e f o r m a l p r e s e n t a t i o n o f s t r e a m s , a n d a

d i s c u s s i o n o f a l t e r n a t e s e m a n t i c s , c a n b e f o u n d i n P a r s e r s i n M L b y M i c h e l M a u n y a n d D a n i e l d e

R a u g l a u d r e , i n t h e p r o c e e d i n g s o f t h e 1 9 9 2 A C M c o n f e r e n c e o n L i s p a n d F u n c t i o n a l P r o g r a m m i n g .

3 . 2 G u a r d s

C a s e s o f a p a t t e r n m a t c h i n g c a n i n c l u d e g u a r d e x p r e s s i o n s , w h i c h a r e a r b i t r a r y b o o l e a n e x p r e s s i o n s

t h a t m u s t e v a l u a t e t o t r u e f o r t h e m a t c h c a s e t o b e s e l e c t e d . G u a r d s o c c u r j u s t b e f o r e t h e - > t o k e n

a n d a r e i n t r o d u c e d b y t h e w h e n k e y w o r d :

m a t c h e x p r

w i t h p a t t e r n

1

w h e n c o n d

1

] - > e x p r

1

| : : :

| p a t t e r n

n

w h e n c o n d

n

] - > e x p r

n

( S a m e s y n t a x f o r t h e f u n , f u n c t i o n , a n d t r y : : : w i t h c o n s t r u c t s . ) D u r i n g m a t c h i n g , i f t h e v a l u e o f

e x p r m a t c h e s s o m e p a t t e r n p a t t e r n

i

w h i c h h a s a g u a r d c o n d

i

, t h e n t h e e x p r e s s i o n c o n d

i

i s e v a l u a t e d

( i n a n e n v i r o n m e n t e n r i c h e d b y t h e b i n d i n g s p e r f o r m e d d u r i n g m a t c h i n g ) . I f c o n d

i

e v a l u a t e s t o

t r u e , t h e n e x p r

i

i s e v a l u a t e d a n d i t s v a l u e r e t u r n e d a s t h e r e s u l t o f t h e m a t c h i n g , a s u s u a l . B u t i f

c o n d

i

e v a l u a t e s t o f a l s e , t h e m a t c h i n g i s r e s u m e d a g a i n s t t h e p a t t e r n s f o l l o w i n g p a t t e r n

i

.

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 39/218

C h a p t e r 3 . L a n g u a g e e x t e n s i o n s 3 7

3 . 3 R a n g e p a t t e r n s

I n p a t t e r n s , C a m l L i g h t r e c o g n i z e s t h e f o r m ` c ` . . ` d ` ( t w o c h a r a c t e r c o n s t a n t s s e p a r a t e d

b y . . ) a s a s h o r t h a n d f o r t h e p a t t e r n

` c ` | ` c

1

` | ` c

2

` | : : : | ` c

n

` | ` d `

w h e r e c

1

; c

2

; : : : ; c

n

a r e t h e c h a r a c t e r s t h a t o c c u r b e t w e e n c a n d d i n t h e A S C I I c h a r a c t e r s e t . F o r

i n s t a n c e , t h e p a t t e r n ` 0 ` . . ` 9 ` m a t c h e s a l l c h a r a c t e r s t h a t a r e d i g i t s .

3 . 4 R e c u r s i v e d e n i t i o n s o f v a l u e s

B e s i d e s l e t r e c d e n i t i o n s o f f u n c t i o n a l v a l u e s , a s d e s c r i b e d i n t h e r e f e r e n c e m a n u a l , C a m l L i g h t

s u p p o r t s a c e r t a i n c l a s s o f r e c u r s i v e d e n i t i o n s o f n o n - f u n c t i o n a l v a l u e s . F o r i n s t a n c e , t h e f o l l o w i n g

d e n i t i o n i s a c c e p t e d :

l e t r e c x = 1 : : y a n d y = 2 : : x ; ;

a n d c o r r e c t l y b i n d s x t o t h e c y c l i c l i s t 1 : : 2 : : 1 : : 2 : : : : : , a n d y t o t h e c y c l i c l i s t

2 : : 1 : : 2 : : 1 : : : : : I n f o r m a l l y , t h e c l a s s o f a c c e p t e d d e n i t i o n s c o n s i s t s o f t h o s e d e n i t i o n s

w h e r e t h e d e n e d v a r i a b l e s o c c u r o n l y i n s i d e f u n c t i o n b o d i e s o r a s a e l d o f a d a t a s t r u c t u r e .

M o r e o v e r , t h e p a t t e r n s i n t h e l e f t - h a n d s i d e s m u s t b e i d e n t i e r s , n o t h i n g m o r e c o m p l e x .

3 . 5 L o c a l d e n i t i o n s u s i n g w h e r e

A p o s t x s y n t a x f o r l o c a l d e n i t i o n s i s p r o v i d e d :

e x p r : : = : : :

j e x p r w h e r e r e c ] l e t - b i n d i n g

T h e e x p r e s s i o n e x p r w h e r e l e t - b i n d i n g b e h a v e s e x a c t l y a s l e t l e t - b i n d i n g i n e x p r , a n d s i m i l a r l y

f o r w h e r e r e c a n d l e t r e c .

3 . 6 M u t a b l e v a r i a n t t y p e s

T h e a r g u m e n t o f a v a l u e c o n s t r u c t o r c a n b e d e c l a r e d \ m u t a b l e " w h e n t h e v a r i a n t t y p e i s d e n e d :

t y p e f o o = A o f m u t a b l e i n t

| B o f m u t a b l e i n t * i n t

| . . .

T h i s a l l o w s i n - p l a c e m o d i c a t i o n o f t h e a r g u m e n t p a r t o f a c o n s t r u c t e d v a l u e . M o d i c a t i o n i s

p e r f o r m e d b y a n e w k i n d o f e x p r e s s i o n s , w r i t t e n i d e n t < - e x p r , w h e r e i d e n t i s a n i d e n t i e r b o u n d

b y p a t t e r n - m a t c h i n g t o t h e a r g u m e n t o f a m u t a b l e c o n s t r u c t o r , a n d e x p r d e n o t e s t h e v a l u e t h a t

m u s t b e s t o r e d i n p l a c e o f t h a t a r g u m e n t . C o n t i n u i n g t h e e x a m p l e a b o v e :

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 40/218

3 8

l e t x = A 1 i n

b e g i n m a t c h x w i t h A y - > y < - 2 | _ - > ( ) e n d ;

x

r e t u r n s t h e v a l u e A 2 . T h e n o t a t i o n i d e n t < - e x p r w o r k s a l s o i f i d e n t i s a n i d e n t i e r b o u n d b y

p a t t e r n - m a t c h i n g t o t h e v a l u e o f a m u t a b l e e l d i n a r e c o r d . F o r i n s t a n c e ,

t y p e b a r = { m u t a b l e l b l : i n t } ; ;

l e t x = { l b l = 1 } i n

b e g i n m a t c h x w i t h { l b l = y } - > y < - 2 e n d ;

x

r e t u r n s t h e v a l u e { l b l = 2 } .

3 . 7 S t r i n g a c c e s s

E x t r a s y n t a c t i c c o n s t r u c t s a r e p r o v i d e d t o a c c e s s a n d m o d i f y c h a r a c t e r s i n s t r i n g s :

e x p r : : = : : :

j e x p r . e x p r ]

j e x p r . e x p r ] < - e x p r

T h e e x p r e s s i o n e x p r

1

. e x p r

2

] i s e q u i v a l e n t t o t h e a p p l i c a t i o n n t h _ c h a r e x p r

1

e x p r

2

. I n t h e

i n i t i a l e n v i r o n m e n t , t h e i d e n t i e r n t h _ c h a r r e s o l v e s t o a b u i l t - i n f u n c t i o n t h a t r e t u r n s t h e c h a r a c t e r

n u m b e r e x p r

2

i n t h e s t r i n g d e n o t e d b y e x p r

1

. T h e r s t e l e m e n t h a s n u m b e r 0 ; t h e l a s t e l e m e n t

h a s n u m b e r n ? 1 , w h e r e n i s t h e l e n g t h o f t h e s t r i n g . T h e e x c e p t i o n I n v a l i d _ a r g u m e n t i s r a i s e d

i f t h e a c c e s s i s o u t o f b o u n d s .

T h e e x p r e s s i o n e x p r

1

. e x p r

2

] < - e x p r

3

i s e q u i v a l e n t t o s e t _ n t h _ c h a r e x p r

1

e x p r

2

e x p r

3

. I n

t h e i n i t i a l e n v i r o n m e n t , t h e i d e n t i e r s e t _ n t h _ c h a r r e s o l v e s t o a b u i l t - i n f u n c t i o n t h a t m o d i e s

i n - p l a c e t h e s t r i n g d e n o t e d b y e x p r

1

, r e p l a c i n g c h a r a c t e r n u m b e r e x p r

2

b y t h e v a l u e o f e x p r

3

. T h e

e x c e p t i o n I n v a l i d _ a r g u m e n t i s r a i s e d i f t h e a c c e s s i s o u t o f b o u n d s . T h e b u i l t - i n f u n c t i o n r e t u r n s

( ) .

3 . 8 A l t e r n a t e s y n t a x

T h e s y n t a x o f s o m e c o n s t r u c t s h a s b e e n s l i g h t l y r e l a x e d :

A n o p t i o n a l ; m a y t e r m i n a t e a s e q u e n c e , l i s t e x p r e s s i o n , o r r e c o r d e x p r e s s i o n . F o r i n s t a n c e ,

b e g i n e

1

; e

2

; e n d i s s y n t a c t i c a l l y c o r r e c t a n d s y n o n y m o u s w i t h b e g i n e

1

; e

2

e n d .

S i m i l a r l y , a n o p t i o n a l | m a y b e g i n a p a t t e r n - m a t c h i n g e x p r e s s i o n . F o r i n s t a n c e ,

f u n c t i o n | p a t

1

- > e x p r

1

| : : : i s s y n t a c t i c a l l y c o r r e c t a n d s y n o n y m o u s w i t h

f u n c t i o n p a t

1

- > e x p r

1

| : : : .

T h e t o k e n s & & a n d | | a r e r e c o g n i z e d a s s y n o n y m o u s f o r & ( s e q u e n t i a l \ a n d " ) a n d o r ( s e q u e n -

t i a l \ o r " ) , r e s p e c t i v e l y .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 41/218

C h a p t e r 3 . L a n g u a g e e x t e n s i o n s 3 9

3 . 9 I n x s y m b o l s

S e q u e n c e s o f \ o p e r a t o r c h a r a c t e r s " , s u c h a s < = > o r ! ! , a r e r e a d a s a s i n g l e t o k e n f r o m t h e

i n x - s y m b o l o r p r e x - s y m b o l c l a s s :

i n x - s y m b o l : : = ( = j < j > j @ j ^ j | j & j ~ j + j - j * j / j $ j % ) f o p e r a t o r - c h a r g

p r e x - s y m b o l : : = ( ! j ? ) f o p e r a t o r - c h a r g

o p e r a t o r - c h a r : : = ! j $ j % j & j * j + j - j . j / j : j ; j < j = j > j ? j @ j ^ j | j ~

T o k e n s f r o m t h e s e t w o c l a s s e s g e n e r a l i z e t h e b u i l t - i n i n x a n d p r e x o p e r a t o r s d e s c r i b e d i n c h a p -

t e r 2 :

e x p r : : = : : :

j p r e x - s y m b o l e x p r

j e x p r i n x - s y m b o l e x p r

v a r i a b l e : : = : : :

j p r e f i x p r e x - s y m b o l

j p r e f i x i n x - s y m b o l

N o # i n f i x d i r e c t i v e ( s e c t i o n 3 . 1 0 ) i s n e e d e d t o g i v e i n x s y m b o l s t h e i r i n x s t a t u s . T h e p r e c e d e n c e s

a n d a s s o c i a t i v i t i e s o f i n x s y m b o l s i n e x p r e s s i o n s a r e d e t e r m i n e d b y t h e i r r s t c h a r a c t e r ( s ) : s y m b o l s

b e g i n n i n g w i t h * * h a v e h i g h e s t p r e c e d e n c e ( e x p o n e n t i a t i o n ) , f o l l o w e d b y s y m b o l s b e g i n n i n g w i t h

* , / o r % ( m u l t i p l i c a t i o n ) , t h e n + a n d - ( a d d i t i o n ) , t h e n @ a n d ^ ( c o n c a t e n a t i o n ) , t h e n a l l o t h e r s

s y m b o l s ( c o m p a r i s o n s ) . T h e u p d a t e d p r e c e d e n c e t a b l e f o r e x p r e s s i o n s i s s h o w n b e l o w . W e w r i t e

\ * : : : " t o m e a n \ a n y i n x s y m b o l s t a r t i n g w i t h * " .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 42/218

4 0

C o n s t r u c t i o n o r o p e r a t o r A s s o c i a t i v i t y

! : : : ? : : : {

. . ( . {

f u n c t i o n a p p l i c a t i o n l e f t

c o n s t r u c t o r a p p l i c a t i o n {

- - . ( p r e x ) {

* * : : : r i g h t

* : : : / : : : % : : : m o d l e f t

+ : : : - : : : l e f t

: : r i g h t

@ : : : ^ : : : r i g h t

c o m p a r i s o n s ( = = = < e t c . ) , a l l o t h e r i n x s y m b o l s l e f t

n o t {

& & & l e f t

o r | | l e f t

, {

< - : = r i g h t

i f {

; r i g h t

l e t m a t c h f u n f u n c t i o n t r y {

S o m e i n x a n d p r e x s y m b o l s a r e p r e d e n e d i n t h e d e f a u l t e n v i r o n m e n t ( s e e c h a p t e r s 2 a n d 1 3 f o r

a d e s c r i p t i o n o f t h e i r b e h a v i o r ) . T h e o t h e r s a r e i n i t i a l l y u n b o u n d a n d m u s t b e b o u n d b e f o r e u s e ,

w i t h a l e t p r e f i x i n x - s y m b o l = e x p r o r l e t p r e f i x p r e x - s y m b o l = e x p r b i n d i n g .

3 . 1 0 D i r e c t i v e s

I n a d d i t i o n t o t h e s t a n d a r d # o p e n a n d # c l o s e d i r e c t i v e s , C a m l L i g h t p r o v i d e s t h r e e a d d i t i o n a l

d i r e c t i v e s .

# i n f i x " i d "

C h a n g e t h e l e x i c a l s t a t u s o f t h e i d e n t i e r i d : i n t h e r e m a i n d e r o f t h e c o m p i l a t i o n u n i t , i d i s

r e c o g n i z e d a s a n i n x o p e r a t o r , j u s t l i k e + . T h e n o t a t i o n p r e f i x i d c a n b e u s e d t o r e f e r t o

t h e i d e n t i e r i d i t s e l f . E x p r e s s i o n s o f t h e f o r m e x p r

1

i d e x p r

2

a r e p a r s e d a s t h e a p p l i c a t i o n

p r e f i x i d e x p r

1

e x p r

2

. T h e a r g u m e n t t o t h e # i n f i x d i r e c t i v e m u s t b e a n i d e n t i e r , t h a t

i s , a s e q u e n c e o f l e t t e r s , d i g i t s a n d u n d e r s c o r e s s t a r t i n g w i t h a l e t t e r ; o t h e r w i s e , t h e # i n f i x

d e c l a r a t i o n h a s n o e e c t . E x a m p l e :

# i n f i x " u n i o n " ; ;

l e t p r e f i x u n i o n = f u n x y - > . . . ; ;

1 , 2 ] u n i o n 3 , 4 ] ; ;

# u n i n f i x " i d "

R e m o v e t h e i n x s t a t u s a t t a c h e d t o t h e i d e n t i e r i d b y a p r e v i o u s # i n f i x " i d " d i r e c t i v e .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 43/218

C h a p t e r 3 . L a n g u a g e e x t e n s i o n s 4 1

# d i r e c t o r y " d i r - n a m e "

A d d t h e n a m e d d i r e c t o r y t o t h e p a t h o f d i r e c t o r i e s s e a r c h e d f o r c o m p i l e d m o d u l e i n t e r f a c e

l e s . T h i s i s e q u i v a l e n t t o t h e - I c o m m a n d - l i n e o p t i o n t o t h e b a t c h c o m p i l e r a n d t h e t o p l e v e l

s y s t e m .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 44/218

4 2

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 45/218

P a r t I I I

T h e C a m l L i g h t c o m m a n d s

4 3

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 46/218

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 47/218

C h a p t e r 4

B a t c h c o m p i l a t i o n ( c a m l c )

T h i s c h a p t e r d e s c r i b e s h o w C a m l L i g h t p r o g r a m s c a n b e c o m p i l e d n o n - i n t e r a c t i v e l y , a n d t u r n e d

i n t o s t a n d a l o n e e x e c u t a b l e l e s . T h i s i s a c h i e v e d b y t h e c o m m a n d c a m l c , w h i c h c o m p i l e s a n d l i n k s

C a m l L i g h t s o u r c e l e s .

M a c : T h i s c o m m a n d i s n o t a s t a n d a l o n e M a c i n t o s h a p p l i c a t i o n . T o r u n c a m l c , y o u n e e d t h e

M a c i n t o s h P r o g r a m m e r ' s W o r k s h o p ( M P W ) p r o g r a m m i n g e n v i r o n m e n t . T h e p r o g r a m s

g e n e r a t e d b y c a m l c a r e a l s o M P W t o o l s , n o t s t a n d a l o n e M a c i n t o s h a p p l i c a t i o n s .

4 . 1 O v e r v i e w o f t h e c o m p i l e r

T h e c a m l c c o m m a n d h a s a c o m m a n d - l i n e i n t e r f a c e s i m i l a r t o t h e o n e o f m o s t C c o m p i l e r s . I t

a c c e p t s s e v e r a l t y p e s o f a r g u m e n t s : s o u r c e l e s f o r m o d u l e i m p l e m e n t a t i o n s ; s o u r c e l e s f o r m o d u l e

i n t e r f a c e s ; a n d c o m p i l e d m o d u l e i m p l e m e n t a t i o n s .

A r g u m e n t s e n d i n g i n . m l i a r e t a k e n t o b e s o u r c e l e s f o r m o d u l e i n t e r f a c e s . M o d u l e i n t e r f a c e s

d e c l a r e e x p o r t e d g l o b a l i d e n t i e r s , d e n e p u b l i c d a t a t y p e s , a n d s o o n . F r o m t h e l e x . m l i ,

t h e c a m l c c o m p i l e r p r o d u c e s a c o m p i l e d i n t e r f a c e i n t h e l e x . z i .

A r g u m e n t s e n d i n g i n . m l a r e t a k e n t o b e s o u r c e l e s f o r m o d u l e i m p l e m e n t a t i o n . M o d -

u l e i m p l e m e n t a t i o n s b i n d g l o b a l i d e n t i e r s t o v a l u e s , d e n e p r i v a t e d a t a t y p e s , a n d c o n t a i n

e x p r e s s i o n s t o b e e v a l u a t e d f o r t h e i r s i d e - e e c t s . F r o m t h e l e x . m l , t h e c a m l c c o m p i l e r

p r o d u c e s c o m p i l e d o b j e c t c o d e i n t h e l e x . z o . I f t h e i n t e r f a c e l e x . m l i e x i s t s , t h e m o d u l e

i m p l e m e n t a t i o n x . m l i s c h e c k e d a g a i n s t t h e c o r r e s p o n d i n g c o m p i l e d i n t e r f a c e x . z i , w h i c h

i s a s s u m e d t o e x i s t . I f n o i n t e r f a c e x . m l i i s p r o v i d e d , t h e c o m p i l a t i o n o f x . m l p r o d u c e s a

c o m p i l e d i n t e r f a c e l e x . z i i n a d d i t i o n t o t h e c o m p i l e d o b j e c t c o d e l e x . z o . T h e l e x . z i

p r o d u c e d c o r r e s p o n d s t o a n i n t e r f a c e t h a t e x p o r t s e v e r y t h i n g t h a t i s d e n e d i n t h e i m p l e -

m e n t a t i o n x . m l .

A r g u m e n t s e n d i n g i n . z o a r e t a k e n t o b e c o m p i l e d o b j e c t c o d e . T h e s e l e s a r e l i n k e d t o g e t h e r ,

a l o n g w i t h t h e o b j e c t c o d e l e s o b t a i n e d b y c o m p i l i n g . m l a r g u m e n t s ( i f a n y ) , a n d t h e C a m l

L i g h t s t a n d a r d l i b r a r y , t o p r o d u c e a s t a n d a l o n e e x e c u t a b l e p r o g r a m . T h e o r d e r i n w h i c h

. z o a n d . m l a r g u m e n t s a r e p r e s e n t e d o n t h e c o m m a n d l i n e i s r e l e v a n t : g l o b a l i d e n t i e r s a r e

i n i t i a l i z e d i n t h a t o r d e r a t r u n - t i m e , a n d i t i s a l i n k - t i m e e r r o r t o u s e a g l o b a l i d e n t i e r b e f o r e

4 5

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 48/218

4 6

h a v i n g i n i t i a l i z e d i t . H e n c e , a g i v e n x . z o l e m u s t c o m e b e f o r e a l l . z o l e s t h a t r e f e r t o

i d e n t i e r s d e n e d i n t h e l e x . z o .

T h e o u t p u t o f t h e l i n k i n g p h a s e i s a l e c o n t a i n i n g c o m p i l e d c o d e t h a t c a n b e e x e c u t e d b y t h e

C a m l L i g h t r u n t i m e s y s t e m : t h e c o m m a n d n a m e d c a m l r u n . I f c a m l . o u t i s t h e n a m e o f t h e l e

p r o d u c e d b y t h e l i n k i n g p h a s e , t h e c o m m a n d

c a m l r u n c a m l . o u t a r g

1

a r g

2

: : : a r g

n

e x e c u t e s t h e c o m p i l e d c o d e c o n t a i n e d i n c a m l . o u t , p a s s i n g i t a s a r g u m e n t s t h e c h a r a c t e r s t r i n g s

a r g

1

t o a r g

n

. ( S e e c h a p t e r 6 f o r m o r e d e t a i l s . )

U n i x : O n m o s t U n i x s y s t e m s , t h e l e p r o d u c e d b y t h e l i n k i n g p h a s e c a n b e r u n d i r e c t l y , a s i n :

. / c a m l . o u t a r g

1

a r g

2

: : : a r g

n

T h e p r o d u c e d l e h a s t h e e x e c u t a b l e b i t s e t , a n d i t m a n a g e s t o l a u n c h t h e b y t e c o d e i n t e r -

p r e t e r b y i t s e l f .

P C : T h e o u t p u t l e p r o d u c e d b y t h e l i n k i n g p h a s e i s d i r e c t l y e x e c u t a b l e , p r o v i d e d i t i s g i v e n

e x t e n s i o n . E X E . H e n c e , i f t h e o u t p u t l e i s n a m e d c a m l _ o u t . e x e , y o u c a n e x e c u t e i t w i t h

t h e c o m m a n d

c a m l _ o u t a r g

1

a r g

2

: : : a r g

n

A c t u a l l y , t h e p r o d u c e d l e c a m l _ o u t . e x e i s a t i n y e x e c u t a b l e l e p r e p e n d e d t o t h e b y t e c o d e

l e . T h e e x e c u t a b l e s i m p l y r u n s t h e c a m l r u n r u n t i m e s y s t e m o n t h e r e m a i n d e r o f t h e l e .

( A s a c o n s e q u e n c e , t h i s i s n o t a s t a n d a l o n e e x e c u t a b l e : i t s t i l l r e q u i r e s c a m l r u n . e x e t o

r e s i d e i n o n e o f t h e d i r e c t o r i e s i n t h e p a t h . )

4 . 2 O p t i o n s

T h e f o l l o w i n g c o m m a n d - l i n e o p t i o n s a r e r e c o g n i z e d b y c a m l c .

- c C o m p i l e o n l y . S u p p r e s s t h e l i n k i n g p h a s e o f t h e c o m p i l a t i o n . S o u r c e c o d e l e s a r e t u r n e d i n t o

c o m p i l e d l e s , b u t n o e x e c u t a b l e l e i s p r o d u c e d . T h i s o p t i o n i s u s e f u l t o c o m p i l e m o d u l e s

s e p a r a t e l y .

- c c o p t o p t i o n

P a s s t h e g i v e n o p t i o n t o t h e C c o m p i l e r a n d l i n k e r , w h e n l i n k i n g i n \ c u s t o m r u n t i m e " m o d e

( s e e t h e - c u s t o m o p t i o n ) . F o r i n s t a n c e , - c c o p t - L d i r c a u s e s t h e C l i n k e r t o s e a r c h f o r C

l i b r a r i e s i n d i r e c t o r y d i r .

- c u s t o m

L i n k i n \ c u s t o m r u n t i m e " m o d e . I n t h e d e f a u l t l i n k i n g m o d e , t h e l i n k e r p r o d u c e s b y t e c o d e

t h a t i s i n t e n d e d t o b e e x e c u t e d w i t h t h e s h a r e d r u n t i m e s y s t e m , c a m l r u n . I n t h e c u s t o m

r u n t i m e m o d e , t h e l i n k e r p r o d u c e s a n o u t p u t l e t h a t c o n t a i n s b o t h t h e r u n t i m e s y s t e m a n d

t h e b y t e c o d e f o r t h e p r o g r a m . T h e r e s u l t i n g l e i s c o n s i d e r a b l y l a r g e r , b u t i t c a n b e e x e c u t e d

d i r e c t l y , e v e n i f t h e c a m l r u n c o m m a n d i s n o t i n s t a l l e d . M o r e o v e r , t h e \ c u s t o m r u n t i m e " m o d e

e n a b l e s l i n k i n g C a m l L i g h t c o d e w i t h u s e r - d e n e d C f u n c t i o n s , a s d e s c r i b e d i n c h a p t e r 1 2 .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 49/218

C h a p t e r 4 . B a t c h c o m p i l a t i o n ( c a m l c ) 4 7

U n i x : N e v e r s t r i p a n e x e c u t a b l e p r o d u c e d w i t h t h e - c u s t o m o p t i o n .

P C : T h i s o p t i o n r e q u i r e s t h e D J G P P p o r t o f t h e G N U C c o m p i l e r t o b e i n s t a l l e d .

- g C a u s e t h e c o m p i l e r t o p r o d u c e a d d i t i o n a l d e b u g g i n g i n f o r m a t i o n . D u r i n g t h e l i n k i n g p h a s e ,

t h i s o p t i o n a d d i n f o r m a t i o n a t t h e e n d o f t h e e x e c u t a b l e b y t e c o d e l e p r o d u c e d . T h i s i n f o r -

m a t i o n i s r e q u i r e d b y t h e d e b u g g e r c a m l d e b u g a n d a l s o b y t h e c a t c h - a l l e x c e p t i o n h a n d l e r

f r o m t h e s t a n d a r d l i b r a r y m o d u l e p r i n t e x c .

D u r i n g t h e c o m p i l a t i o n o f a n i m p l e m e n t a t i o n l e ( . m l l e ) , w h e n t h e - g o p t i o n i s s e t , t h e

c o m p i l e r a d d s d e b u g g i n g i n f o r m a t i o n t o t h e . z o l e . I t a l s o w r i t e s a . z i x l e t h a t d e s c r i b e s

t h e f u l l i n t e r f a c e o f t h e . m l l e , t h a t i s , a l l t y p e s a n d v a l u e s d e n e d i n t h e . m l l e , i n c l u d i n g

t h o s e t h a t a r e l o c a l t o t h e . m l l e ( i . e . n o t d e c l a r e d i n t h e . m l i i n t e r f a c e l e ) . U s e d i n

c o n j u n c t i o n w i t h t h e - g o p t i o n t o t h e t o p l e v e l s y s t e m ( c h a p t e r 5 ) , t h e . z i x l e g i v e s a c c e s s

t o t h e l o c a l v a l u e s o f t h e m o d u l e , m a k i n g i t p o s s i b l e t o p r i n t o r \ t r a c e " t h e m . T h e . z i x l e

i s n o t p r o d u c e d i f t h e i m p l e m e n t a t i o n l e h a s n o e x p l i c i t i n t e r f a c e , s i n c e , i n t h i s c a s e , t h e

m o d u l e h a s n o l o c a l v a l u e s .

- i C a u s e t h e c o m p i l e r t o p r i n t t h e d e c l a r e d t y p e s , e x c e p t i o n s , a n d g l o b a l v a r i a b l e s ( w i t h t h e i r

i n f e r r e d t y p e s ) w h e n c o m p i l i n g a n i m p l e m e n t a t i o n ( . m l l e ) . T h i s c a n b e u s e f u l t o c h e c k

t h e t y p e s i n f e r r e d b y t h e c o m p i l e r . A l s o , s i n c e t h e o u t p u t f o l l o w s t h e s y n t a x o f m o d u l e

i n t e r f a c e s , i t c a n h e l p i n w r i t i n g a n e x p l i c i t i n t e r f a c e ( . m l i l e ) f o r a l e : j u s t r e d i r e c t t h e

s t a n d a r d o u t p u t o f t h e c o m p i l e r t o a . m l i l e , a n d e d i t t h a t l e t o r e m o v e a l l d e c l a r a t i o n s

o f u n e x p o r t e d g l o b a l s .

- I d i r e c t o r y

A d d t h e g i v e n d i r e c t o r y t o t h e l i s t o f d i r e c t o r i e s s e a r c h e d f o r c o m p i l e d i n t e r f a c e l e s ( . z i ) a n d

c o m p i l e d o b j e c t c o d e l e s ( . z o ) . B y d e f a u l t , t h e c u r r e n t d i r e c t o r y i s s e a r c h e d r s t , t h e n t h e

s t a n d a r d l i b r a r y d i r e c t o r y . D i r e c t o r i e s a d d e d w i t h - I a r e s e a r c h e d a f t e r t h e c u r r e n t d i r e c t o r y ,

b u t b e f o r e t h e s t a n d a r d l i b r a r y d i r e c t o r y . W h e n s e v e r a l d i r e c t o r i e s a r e a d d e d w i t h s e v e r a l - I

o p t i o n s o n t h e c o m m a n d l i n e , t h e s e d i r e c t o r i e s a r e s e a r c h e d f r o m r i g h t t o l e f t ( t h e r i g h t m o s t

d i r e c t o r y i s s e a r c h e d r s t , t h e l e f t m o s t i s s e a r c h e d l a s t ) . ( D i r e c t o r i e s c a n a l s o b e a d d e d t o

t h e s e a r c h p a t h f r o m i n s i d e t h e p r o g r a m s w i t h t h e # d i r e c t o r y d i r e c t i v e ; s e e c h a p t e r 3 . )

- l a n g l a n g u a g e - c o d e

T r a n s l a t e t h e c o m p i l e r m e s s a g e s t o t h e s p e c i e d l a n g u a g e . T h e l a n g u a g e - c o d e i s f r f o r F r e n c h ,

e s f o r S p a n i s h , d e f o r G e r m a n , : : : ( S e e t h e l e c a m l m s g s . t x t i n t h e C a m l L i g h t s t a n d a r d

l i b r a r y d i r e c t o r y f o r a l i s t o f a v a i l a b l e l a n g u a g e s . ) W h e n a n u n k n o w n l a n g u a g e i s s p e c i e d ,

o r n o t r a n s l a t i o n i s a v a i l a b l e f o r a m e s s a g e , A m e r i c a n E n g l i s h i s u s e d b y d e f a u l t .

- o e x e c - l e

S p e c i f y t h e n a m e o f t h e o u t p u t l e p r o d u c e d b y t h e l i n k e r .

U n i x : T h e d e f a u l t o u t p u t n a m e i s a . o u t , i n k e e p i n g w i t h t h e t r a d i t i o n .

P C : T h e d e f a u l t o u t p u t n a m e i s c a m l _ o u t . e x e .

M a c : T h e d e f a u l t o u t p u t n a m e i s C a m l . O u t .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 50/218

4 8

- O m o d u l e - s e t

S p e c i f y w h i c h s e t o f s t a n d a r d m o d u l e s i s t o b e i m p l i c i t l y \ o p e n e d " a t t h e b e g i n n i n g o f a

c o m p i l a t i o n . T h e r e a r e t h r e e m o d u l e s e t s c u r r e n t l y a v a i l a b l e :

c a u t i o u s

p r o v i d e s t h e s t a n d a r d o p e r a t i o n s o n i n t e g e r s , o a t i n g - p o i n t n u m b e r s , c h a r a c t e r s , s t r i n g s ,

a r r a y s , : : : , a s w e l l a s e x c e p t i o n h a n d l i n g , b a s i c i n p u t / o u t p u t , e t c . O p e r a t i o n s f r o m t h e

c a u t i o u s s e t p e r f o r m r a n g e a n d b o u n d c h e c k i n g o n s t r i n g a n d a r r a y o p e r a t i o n s , a s w e l l

a s v a r i o u s s a n i t y c h e c k s o n t h e i r a r g u m e n t s .

f a s t

p r o v i d e s t h e s a m e o p e r a t i o n s a s t h e c a u t i o u s s e t , b u t w i t h o u t s a n i t y c h e c k s o n t h e i r

a r g u m e n t s . P r o g r a m s c o m p i l e d w i t h - O f a s t a r e t h e r e f o r e s l i g h t l y f a s t e r , b u t u n s a f e .

n o n e

s u p p r e s s e s a l l a u t o m a t i c o p e n i n g o f m o d u l e s . C o m p i l a t i o n s t a r t s i n a n a l m o s t e m p t y

e n v i r o n m e n t . T h i s o p t i o n i s n o t o f g e n e r a l u s e , e x c e p t t o c o m p i l e t h e s t a n d a r d l i b r a r y

i t s e l f .

T h e d e f a u l t c o m p i l a t i o n m o d e i s - O c a u t i o u s . S e e c h a p t e r 1 3 f o r a c o m p l e t e l i s t i n g o f t h e

m o d u l e s i n t h e c a u t i o u s a n d f a s t s e t s .

- p C o m p i l e a n d l i n k i n p r o l i n g m o d e . S e e t h e d e s c r i p t i o n o f t h e p r o l e r c a m l p r o i n c h a p t e r 1 0 .

- v P r i n t t h e v e r s i o n n u m b e r o f t h e c o m p i l e r .

- W P r i n t e x t r a w a r n i n g m e s s a g e s f o r t h e f o l l o w i n g e v e n t s :

A # o p e n d i r e c t i v e i s u s e l e s s ( n o i d e n t i e r i n t h e o p e n e d m o d u l e i s e v e r r e f e r e n c e d ) .

A v a r i a b l e n a m e i n a p a t t e r n m a t c h i n g i s c a p i t a l i z e d ( o f t e n c o r r e s p o n d s t o a m i s s p e l l e d

c o n s t a n t c o n s t r u c t o r ) .

U n i x : T h e f o l l o w i n g e n v i r o n m e n t v a r i a b l e i s a l s o c o n s u l t e d :

L A N G

W h e n s e t , c o n t r o l w h i c h l a n g u a g e i s u s e d t o p r i n t t h e c o m p i l e r m e s s a g e s ( s e e t h e - l a n g

c o m m a n d - l i n e o p t i o n ) .

P C : T h e f o l l o w i n g o p t i o n i s a l s o s u p p o r t e d :

@ r e s p o n s e - l e

P r o c e s s t h e l e s w h o s e n a m e s a r e l i s t e d i n l e r e s p o n s e - l e , j u s t a s i f t h e s e n a m e s

a p p e a r e d o n t h e c o m m a n d l i n e . F i l e n a m e s i n r e s p o n s e - l e a r e s e p a r a t e d b y b l a n k s

( s p a c e s , t a b s , n e w l i n e s ) . T h i s o p t i o n a l l o w s t o o v e r c o m e s i l l y l i m i t a t i o n s o n t h e l e n g t h

o f t h e c o m m a n d l i n e .

T h e f o l l o w i n g e n v i r o n m e n t v a r i a b l e s a r e a l s o c o n s u l t e d :

C A M L L I B

C o n t a i n t h e p a t h t o t h e s t a n d a r d l i b r a r y d i r e c t o r y .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 51/218

C h a p t e r 4 . B a t c h c o m p i l a t i o n ( c a m l c ) 4 9

L A N G

W h e n s e t , c o n t r o l w h i c h l a n g u a g e i s u s e d t o p r i n t t h e c o m p i l e r m e s s a g e s ( s e e t h e - l a n g

c o m m a n d - l i n e o p t i o n ) .

4 . 3 M o d u l e s a n d t h e l e s y s t e m

T h i s s h o r t s e c t i o n i s i n t e n d e d t o c l a r i f y t h e r e l a t i o n s h i p b e t w e e n t h e n a m e s o f t h e m o d u l e s a n d t h e

n a m e s o f t h e l e s t h a t c o n t a i n t h e i r c o m p i l e d i n t e r f a c e a n d c o m p i l e d i m p l e m e n t a t i o n .

T h e c o m p i l e r a l w a y s d e r i v e s t h e n a m e o f t h e c o m p i l e d m o d u l e b y t a k i n g t h e b a s e n a m e o f t h e

s o u r c e l e ( . m l o r . m l i l e ) . T h a t i s , i t s t r i p s t h e l e a d i n g d i r e c t o r y n a m e , i f a n y , a s w e l l a s t h e . m l

o r . m l i s u x . T h e p r o d u c e d . z i a n d . z o l e s h a v e t h e s a m e b a s e n a m e a s t h e s o u r c e l e ; h e n c e ,

t h e c o m p i l e d l e s p r o d u c e d b y t h e c o m p i l e r a l w a y s h a v e t h e i r b a s e n a m e e q u a l t o t h e n a m e o f t h e

m o d u l e t h e y d e s c r i b e ( f o r . z i l e s ) o r i m p l e m e n t ( f o r . z o l e s ) .

F o r c o m p i l e d i n t e r f a c e l e s ( . z i l e s ) , t h i s i n v a r i a n t m u s t b e p r e s e r v e d a t a l l t i m e s , s i n c e t h e

c o m p i l e r r e l i e s o n i t t o l o a d t h e c o m p i l e d i n t e r f a c e l e f o r t h e m o d u l e s t h a t a r e u s e d f r o m t h e m o d u l e

b e i n g c o m p i l e d . H e n c e , i t i s r i s k y a n d g e n e r a l l y i n c o r r e c t t o r e n a m e . z i l e s . I t i s a d m i s s i b l e t o

m o v e t h e m t o a n o t h e r d i r e c t o r y , i f t h e i r b a s e n a m e i s p r e s e r v e d , a n d t h e c o r r e c t - I o p t i o n s a r e

g i v e n t o t h e c o m p i l e r .

C o m p i l e d b y t e c o d e l e s ( . z o l e s ) , o n t h e o t h e r h a n d , c a n b e f r e e l y r e n a m e d o n c e c r e a t e d .

T h a t ' s b e c a u s e 1 - . z o l e s c o n t a i n t h e t r u e n a m e o f t h e m o d u l e t h e y d e n e , s o t h e r e i s n o n e e d t o

d e r i v e t h a t n a m e f r o m t h e l e n a m e ; 2 - t h e l i n k e r n e v e r a t t e m p t s t o n d b y i t s e l f t h e . z o l e t h a t

i m p l e m e n t s a m o d u l e o f a g i v e n n a m e : i t r e l i e s o n t h e u s e r p r o v i d i n g t h e l i s t o f . z o l e s b y h a n d .

4 . 4 C o m m o n e r r o r s

T h i s s e c t i o n d e s c r i b e s a n d e x p l a i n s t h e m o s t f r e q u e n t l y e n c o u n t e r e d e r r o r m e s s a g e s .

C a n n o t n d l e l e n a m e

T h e n a m e d l e c o u l d n o t b e f o u n d i n t h e c u r r e n t d i r e c t o r y , n o r i n t h e d i r e c t o r i e s o f t h e s e a r c h

p a t h . T h e l e n a m e i s e i t h e r a c o m p i l e d i n t e r f a c e l e ( . z i l e ) , o r a c o m p i l e d b y t e c o d e l e

( . z o l e ) . I f l e n a m e h a s t h e f o r m a t m o d . z i , t h i s m e a n s y o u a r e t r y i n g t o c o m p i l e a l e t h a t

r e f e r e n c e s i d e n t i e r s f r o m m o d u l e m o d , b u t y o u h a v e n o t y e t c o m p i l e d a n i n t e r f a c e f o r m o d u l e

m o d . F i x : c o m p i l e m o d . m l i o r m o d . m l r s t , t o c r e a t e t h e c o m p i l e d i n t e r f a c e m o d . z i .

I f l e n a m e h a s t h e f o r m a t m o d . z o , t h i s m e a n s y o u a r e t r y i n g t o l i n k a b y t e c o d e o b j e c t l e

t h a t d o e s n o t e x i s t y e t . F i x : c o m p i l e m o d . m l r s t .

I f y o u r p r o g r a m s p a n s s e v e r a l d i r e c t o r i e s , t h i s e r r o r c a n a l s o a p p e a r b e c a u s e y o u h a v e n ' t

s p e c i e d t h e d i r e c t o r i e s t o l o o k i n t o . F i x : a d d t h e c o r r e c t - I o p t i o n s t o t h e c o m m a n d l i n e .

C o r r u p t e d c o m p i l e d i n t e r f a c e l e l e n a m e

T h e c o m p i l e r p r o d u c e s t h i s e r r o r w h e n i t t r i e s t o r e a d a c o m p i l e d i n t e r f a c e l e ( . z i l e ) t h a t

h a s t h e w r o n g s t r u c t u r e . T h i s m e a n s s o m e t h i n g w e n t w r o n g w h e n t h i s . z i l e w a s w r i t t e n :

t h e d i s k w a s f u l l , t h e c o m p i l e r w a s i n t e r r u p t e d i n t h e m i d d l e o f t h e l e c r e a t i o n , a n d s o o n .

T h i s e r r o r c a n a l s o a p p e a r i f a . z i l e i s m o d i e d a f t e r i t s c r e a t i o n b y t h e c o m p i l e r . F i x :

r e m o v e t h e c o r r u p t e d . z i l e , a n d r e b u i l d i t .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 52/218

5 0

T h i s e x p r e s s i o n h a s t y p e t

1

, b u t i s u s e d w i t h t y p e t

2

T h i s i s b y f a r t h e m o s t c o m m o n t y p e e r r o r i n p r o g r a m s . T y p e t

1

i s t h e t y p e i n f e r r e d f o r t h e

e x p r e s s i o n ( t h e p a r t o f t h e p r o g r a m t h a t i s d i s p l a y e d i n t h e e r r o r m e s s a g e ) , b y l o o k i n g a t t h e

e x p r e s s i o n i t s e l f . T y p e t

2

i s t h e t y p e e x p e c t e d b y t h e c o n t e x t o f t h e e x p r e s s i o n ; i t i s d e d u c e d

b y l o o k i n g a t h o w t h e v a l u e o f t h i s e x p r e s s i o n i s u s e d i n t h e r e s t o f t h e p r o g r a m . I f t h e t w o

t y p e s t

1

a n d t

2

a r e n o t c o m p a t i b l e , t h e n t h e e r r o r a b o v e i s p r o d u c e d .

I n s o m e c a s e s , i t i s h a r d t o u n d e r s t a n d w h y t h e t w o t y p e s t

1

a n d t

2

a r e i n c o m p a t i b l e . F o r

i n s t a n c e , t h e c o m p i l e r c a n r e p o r t t h a t \ e x p r e s s i o n o f t y p e f o o c a n n o t b e u s e d w i t h t y p e f o o " ,

a n d i t r e a l l y s e e m s t h a t t h e t w o t y p e s f o o a r e c o m p a t i b l e . T h i s i s n o t a l w a y s t r u e . T w o

t y p e c o n s t r u c t o r s c a n h a v e t h e s a m e n a m e , b u t a c t u a l l y r e p r e s e n t d i e r e n t t y p e s . T h i s c a n

h a p p e n i f a t y p e c o n s t r u c t o r i s r e d e n e d . E x a m p l e :

t y p e f o o = A | B ; ;

l e t f = f u n c t i o n A - > 0 | B - > 1 ; ;

t y p e f o o = C | D ; ;

f C ; ;

T h i s r e s u l t i n t h e e r r o r m e s s a g e \ e x p r e s s i o n C o f t y p e f o o c a n n o t b e u s e d w i t h t y p e f o o " .

I n c o m p a t i b l e t y p e s w i t h t h e s a m e n a m e s c a n a l s o a p p e a r w h e n a m o d u l e i s c h a n g e d a n d

r e c o m p i l e d , b u t s o m e o f i t s c l i e n t s a r e n o t r e c o m p i l e d . T h a t ' s b e c a u s e t y p e c o n s t r u c t o r s i n

. z i l e s a r e n o t r e p r e s e n t e d b y t h e i r n a m e ( t h a t w o u l d n o t s u c e t o i d e n t i f y t h e m , b e c a u s e

o f t y p e r e d e n i t i o n s ) , b u t b y u n i q u e s t a m p s t h a t a r e a s s i g n e d w h e n t h e t y p e d e c l a r a t i o n i s

c o m p i l e d . C o n s i d e r t h e t h r e e m o d u l e s :

m o d 1 . m l : t y p e t = A | B ; ;

l e t f = f u n c t i o n A - > 0 | B - > 1 ; ;

m o d 2 . m l : l e t g x = 1 + m o d 1 _ _ f ( x ) ; ;

m o d 3 . m l : m o d 2 _ _ g m o d 1 _ _ A ; ;

N o w , a s s u m e m o d 1 . m l i s c h a n g e d a n d r e c o m p i l e d , b u t m o d 2 . m l i s n o t r e c o m p i l e d . T h e r e c o m -

p i l a t i o n o f m o d 1 . m l c a n c h a n g e t h e s t a m p a s s i g n e d t o t y p e t . B u t t h e i n t e r f a c e m o d 2 . z i w i l l

s t i l l u s e t h e o l d s t a m p f o r m o d 1 _ _ t i n t h e t y p e o f m o d 2 _ _ g . H e n c e , w h e n c o m p i l i n g m o d 3 . m l ,

t h e s y s t e m c o m p l a i n s t h a t t h e a r g u m e n t t y p e o f m o d 2 _ _ g ( t h a t i s , m o d 1 _ _ t w i t h t h e o l d

s t a m p ) i s n o t c o m p a t i b l e w i t h t h e t y p e o f m o d 1 _ _ A ( t h a t i s , m o d 1 _ _ t w i t h t h e n e w s t a m p ) .

F i x : u s e m a k e o r a s i m i l a r t o o l t o e n s u r e t h a t a l l c l i e n t s o f a m o d u l e m o d a r e r e c o m p i l e d w h e n

t h e i n t e r f a c e m o d . z i c h a n g e s . T o c h e c k t h a t t h e M a k e f i l e c o n t a i n s t h e r i g h t d e p e n d e n c i e s ,

r e m o v e a l l . z i l e s a n d r e b u i l d t h e w h o l e p r o g r a m ; i f n o \ C a n n o t n d l e " e r r o r a p p e a r s ,

y o u ' r e a l l s e t .

T h e t y p e i n f e r r e d f o r n a m e , t h a t i s , t , c o n t a i n s n o n - g e n e r a l i z a b l e t y p e v a r i a b l e s

T y p e v a r i a b l e s ( ' a , ' b , : : : ) i n a t y p e t c a n b e i n e i t h e r o f t w o s t a t e s : g e n e r a l i z e d ( w h i c h

m e a n s t h a t t h e t y p e t i s v a l i d f o r a l l p o s s i b l e i n s t a n t i a t i o n s o f t h e v a r i a b l e s ) a n d n o t g e n e r -

a l i z e d ( w h i c h m e a n s t h a t t h e t y p e t i s v a l i d o n l y f o r o n e i n s t a n t i a t i o n o f t h e v a r i a b l e s ) . I n a

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 53/218

C h a p t e r 4 . B a t c h c o m p i l a t i o n ( c a m l c ) 5 1

l e t b i n d i n g l e t n a m e = e x p r , t h e t y p e - c h e c k e r n o r m a l l y g e n e r a l i z e s a s m a n y t y p e v a r i a b l e s

a s p o s s i b l e i n t h e t y p e o f e x p r . H o w e v e r , t h i s l e a d s t o u n s o u n d n e s s ( a w e l l - t y p e d p r o g r a m

c a n c r a s h ) i n c o n j u n c t i o n w i t h p o l y m o r p h i c m u t a b l e d a t a s t r u c t u r e s . T o a v o i d t h i s , g e n e r a l -

i z a t i o n i s p e r f o r m e d a t l e t b i n d i n g s o n l y i f t h e b o u n d e x p r e s s i o n e x p r b e l o n g s t o t h e c l a s s o f

\ s y n t a c t i c v a l u e s " , w h i c h i n c l u d e s c o n s t a n t s , i d e n t i e r s , f u n c t i o n s , t u p l e s o f s y n t a c t i c v a l u e s ,

e t c . I n a l l o t h e r c a s e s ( f o r i n s t a n c e , e x p r i s a f u n c t i o n a p p l i c a t i o n ) , a p o l y m o r p h i c m u t a b l e

c o u l d h a v e b e e n c r e a t e d a n d g e n e r a l i z a t i o n i s t h e r e f o r e t u r n e d o .

N o n - g e n e r a l i z e d t y p e v a r i a b l e s i n a t y p e c a u s e n o d i c u l t i e s i n s i d e a g i v e n c o m p i l a t i o n u n i t

( t h e c o n t e n t s o f a . m l l e , o r a n i n t e r a c t i v e s e s s i o n ) , b u t t h e y c a n n o t b e a l l o w e d i n t y p e s

w r i t t e n i n a . z i c o m p i l e d i n t e r f a c e l e , b e c a u s e t h e y c o u l d b e u s e d i n c o n s i s t e n t l y i n o t h e r

c o m p i l a t i o n u n i t s . T h e r e f o r e , t h e c o m p i l e r a g s a n e r r o r w h e n a . m l i m p l e m e n t a t i o n w i t h o u t

a . m l i i n t e r f a c e d e n e s a g l o b a l v a r i a b l e n a m e w h o s e t y p e c o n t a i n s n o n - g e n e r a l i z e d t y p e

v a r i a b l e s . T h e r e a r e t w o s o l u t i o n s t o t h i s p r o b l e m :

A d d a t y p e c o n s t r a i n t o r a . m l i i n t e r f a c e t o g i v e a m o n o m o r p h i c t y p e ( w i t h o u t t y p e

v a r i a b l e s ) t o n a m e . F o r i n s t a n c e , i n s t e a d o f w r i t i n g

l e t s o r t _ i n t _ l i s t = s o r t ( p r e f i x < ) ; ;

( * i n f e r r e d t y p e ' a l i s t - > ' a l i s t , w i t h ' a n o t g e n e r a l i z e d * )

w r i t e

l e t s o r t _ i n t _ l i s t = ( s o r t ( p r e f i x < ) : i n t l i s t - > i n t l i s t ) ; ;

I f y o u r e a l l y n e e d n a m e t o h a v e a p o l y m o r p h i c t y p e , t u r n i t s d e n i n g e x p r e s s i o n i n t o a

f u n c t i o n b y a d d i n g a n e x t r a p a r a m e t e r . F o r i n s t a n c e , i n s t e a d o f w r i t i n g

l e t m a p _ l e n g t h = m a p v e c t _ l e n g t h ; ;

( * i n f e r r e d t y p e ' a v e c t l i s t - > i n t l i s t , w i t h ' a n o t g e n e r a l i z e d * )

w r i t e

l e t m a p _ l e n g t h l v = m a p v e c t _ l e n g t h l v ; ;

m o d _ _ n a m e i s r e f e r e n c e d b e f o r e b e i n g d e n e d

T h i s e r r o r a p p e a r s w h e n t r y i n g t o l i n k a n i n c o m p l e t e o r i n c o r r e c t l y o r d e r e d s e t o f l e s . E i t h e r

y o u h a v e f o r g o t t e n t o p r o v i d e a n i m p l e m e n t a t i o n f o r t h e m o d u l e n a m e d m o d o n t h e c o m m a n d

l i n e ( t y p i c a l l y , t h e l e n a m e d m o d . z o , o r a l i b r a r y c o n t a i n i n g t h a t l e ) . F i x : a d d t h e m i s s i n g

. m l o r . z o l e t o t h e c o m m a n d l i n e . O r , y o u h a v e p r o v i d e d a n i m p l e m e n t a t i o n f o r t h e m o d u l e

n a m e d m o d , b u t i t c o m e s t o o l a t e o n t h e c o m m a n d l i n e : t h e i m p l e m e n t a t i o n o f m o d m u s t

c o m e b e f o r e a l l b y t e c o d e o b j e c t l e s t h a t r e f e r e n c e o n e o f t h e g l o b a l v a r i a b l e s d e n e d i n

m o d u l e m o d . F i x : c h a n g e t h e o r d e r o f . m l a n d . z o l e s o n t h e c o m m a n d l i n e .

O f c o u r s e , y o u w i l l a l w a y s e n c o u n t e r t h i s e r r o r i f y o u h a v e m u t u a l l y r e c u r s i v e f u n c t i o n s a c r o s s

m o d u l e s . T h a t i s , f u n c t i o n m o d 1 _ _ f c a l l s f u n c t i o n m o d 2 _ _ g , a n d f u n c t i o n m o d 2 _ _ g c a l l s

f u n c t i o n m o d 1 _ _ f . I n t h i s c a s e , n o m a t t e r w h a t p e r m u t a t i o n s y o u p e r f o r m o n t h e c o m m a n d

l i n e , t h e p r o g r a m w i l l b e r e j e c t e d a t l i n k - t i m e . F i x e s :

P u t f a n d g i n t h e s a m e m o d u l e .

P a r a m e t e r i z e o n e f u n c t i o n b y t h e o t h e r . T h a t i s , i n s t e a d o f h a v i n g

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 54/218

5 2

m o d 1 . m l : l e t f x = . . . m o d 2 _ _ g . . . ; ;

m o d 2 . m l : l e t g y = . . . m o d 1 _ _ f . . . ; ;

d e n e

m o d 1 . m l : l e t f g x = . . . g . . . ; ;

m o d 2 . m l : l e t r e c g y = . . . m o d 1 _ _ f g . . . ; ;

a n d l i n k m o d 1 b e f o r e m o d 2 .

U s e a r e f e r e n c e t o h o l d o n e o f t h e t w o f u n c t i o n s , a s i n :

m o d 1 . m l : l e t f o r w a r d _ g =

r e f ( ( f u n x - > f a i l w i t h " f o r w a r d _ g " ) : < t y p e > ) ; ;

l e t f x = . . . ! f o r w a r d _ g . . . ; ;

m o d 2 . m l : l e t g y = . . . m o d 1 _ _ f . . . ; ;

m o d 1 _ _ f o r w a r d _ g : = g ; ;

U n a v a i l a b l e C p r i m i t i v e f

T h i s e r r o r a p p e a r s w h e n t r y i n g t o l i n k c o d e t h a t c a l l s e x t e r n a l f u n c t i o n s w r i t t e n i n C i n

\ d e f a u l t r u n t i m e " m o d e . A s e x p l a i n e d i n c h a p t e r 1 2 , s u c h c o d e m u s t b e l i n k e d i n \ c u s t o m

r u n t i m e " m o d e . F i x : a d d t h e - c u s t o m o p t i o n , a s w e l l a s t h e ( n a t i v e c o d e ) l i b r a r i e s a n d

( n a t i v e c o d e ) o b j e c t l e s t h a t i m p l e m e n t t h e r e q u i r e d e x t e r n a l f u n c t i o n s .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 55/218

C h a p t e r 5

T h e t o p l e v e l s y s t e m ( c a m l l i g h t )

T h i s c h a p t e r d e s c r i b e s t h e t o p l e v e l s y s t e m f o r C a m l L i g h t , t h a t p e r m i t s i n t e r a c t i v e u s e o f t h e C a m l

L i g h t s y s t e m , t h r o u g h a r e a d - e v a l - p r i n t l o o p . I n t h i s m o d e , t h e s y s t e m r e p e a t e d l y r e a d s C a m l

L i g h t p h r a s e s f r o m t h e i n p u t , t h e n t y p e c h e c k s , c o m p i l e a n d e v a l u a t e t h e m , t h e n p r i n t s t h e i n f e r r e d

t y p e a n d r e s u l t v a l u e , i f a n y . T h e s y s t e m p r i n t s a # ( s h a r p ) p r o m p t b e f o r e r e a d i n g e a c h p h r a s e . A

p h r a s e c a n s p a n s e v e r a l l i n e s . P h r a s e s a r e d e l i m i t e d b y ; ; ( t h e n a l d o u b l e - s e m i c o l o n ) .

F r o m t h e s t a n d p o i n t o f t h e m o d u l e s y s t e m , a l l p h r a s e s e n t e r e d a t t o p l e v e l a r e t r e a t e d a s t h e

i m p l e m e n t a t i o n o f a m o d u l e n a m e d t o p . H e n c e , a l l t o p l e v e l d e n i t i o n s a r e e n t e r e d i n t h e m o d u l e

t o p .

U n i x : T h e t o p l e v e l s y s t e m i s s t a r t e d b y t h e c o m m a n d c a m l l i g h t . P h r a s e s a r e r e a d o n s t a n d a r d

i n p u t , r e s u l t s a r e p r i n t e d o n s t a n d a r d o u t p u t , e r r o r s o n s t a n d a r d e r r o r . E n d - o f - l e o n

s t a n d a r d i n p u t t e r m i n a t e s c a m l l i g h t ( s e e a l s o t h e q u i t s y s t e m f u n c t i o n b e l o w ) .

T h e t o p l e v e l s y s t e m d o e s n o t p e r f o r m l i n e e d i t i n g , b u t i t c a n e a s i l y b e u s e d i n c o n j u n c t i o n

w i t h a n e x t e r n a l l i n e e d i t o r s u c h a s f e p ; j u s t r u n f e p - e m a c s c a m l l i g h t o r f e p - v i

c a m l l i g h t . A n o t h e r o p t i o n i s t o u s e c a m l l i g h t u n d e r G n u E m a c s , w h i c h g i v e s t h e f u l l

e d i t i n g p o w e r o f E m a c s ( s e e t h e d i r e c t o r y c o n t r i b / c a m l m o d e i n t h e d i s t r i b u t i o n ) .

A t a n y p o i n t , t h e p a r s i n g , c o m p i l a t i o n o r e v a l u a t i o n o f t h e c u r r e n t p h r a s e c a n b e i n t e r -

r u p t e d b y p r e s s i n g c t r l - C ( o r , m o r e p r e c i s e l y , b y s e n d i n g t h e i n t r s i g n a l t o t h e c a m l l i g h t

p r o c e s s ) . T h i s g o e s b a c k t o t h e # p r o m p t .

M a c : T h e t o p l e v e l s y s t e m i s p r e s e n t e d a s t h e s t a n d a l o n e M a c i n t o s h a p p l i c a t i o n C a m l L i g h t . T h i s

a p p l i c a t i o n d o e s n o t r e q u i r e t h e M a c i n t o s h P r o g r a m m e r ' s W o r k s h o p t o r u n .

O n c e l a u n c h e d f r o m t h e F i n d e r , t h e a p p l i c a t i o n o p e n s t w o w i n d o w s , \ C a m l L i g h t I n p u t "

a n d \ C a m l L i g h t O u t p u t " . P h r a s e s a r e e n t e r e d i n t h e \ C a m l L i g h t I n p u t " w i n d o w . T h e

\ C a m l L i g h t O u t p u t " w i n d o w d i s p l a y s a c o p y o f t h e i n p u t p h r a s e s a s t h e y a r e p r o c e s s e d b y

t h e C a m l L i g h t t o p l e v e l , i n t e r s p e r s e d w i t h t h e t o p l e v e l r e s p o n s e s . T h e \ R e t u r n " k e y s e n d s

t h e c o n t e n t s o f t h e I n p u t w i n d o w t o t h e C a m l L i g h t t o p l e v e l . T h e \ E n t e r " k e y i n s e r t s a

n e w l i n e w i t h o u t s e n d i n g t h e c o n t e n t s o f t h e I n p u t w i n d o w . ( T h i s c a n b e c o n g u r e d w i t h

t h e \ P r e f e r e n c e s " m e n u i t e m . )

T h e c o n t e n t s o f t h e i n p u t w i n d o w c a n b e e d i t e d a t a l l t i m e s , w i t h t h e s t a n d a r d M a c i n t o s h

i n t e r f a c e . A n h i s t o r y o f p r e v i o u s l y e n t e r e d p h r a s e s i s m a i n t a i n e d , a n d c a n b e a c c e s s e d w i t h

t h e \ P r e v i o u s e n t r y " ( c o m m a n d - P ) a n d \ N e x t e n t r y " ( c o m m a n d - N ) m e n u i t e m s .

5 3

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 56/218

5 4

T o q u i t t h e C a m l L i g h t a p p l i c a t i o n , e i t h e r s e l e c t \ Q u i t " f r o m t h e \ F i l e s " m e n u , o r u s e t h e

q u i t f u n c t i o n d e s c r i b e d b e l o w .

A t a n y p o i n t , t h e p a r s i n g , c o m p i l a t i o n o r e v a l u a t i o n o f t h e c u r r e n t p h r a s e c a n b e i n t e r r u p t e d

b y p r e s s i n g \ c o m m a n d - p e r i o d " , o r b y s e l e c t i n g t h e i t e m \ I n t e r r u p t C a m l L i g h t " i n t h e

\ C a m l L i g h t " m e n u . T h i s g o e s b a c k t o t h e # p r o m p t .

P C : T h e t o p l e v e l s y s t e m i s p r e s e n t e d a s a W i n d o w s a p p l i c a t i o n n a m e d C a m l w i n . e x e . I t s h o u l d

b e l a u n c h e d f r o m t h e W i n d o w s l e m a n a g e r o r p r o g r a m m a n a g e r .

T h e \ T e r m i n a l " w i n d o w s i s s p l i t i n t w o p a n e s . P h r a s e s a r e e n t e r e d a n d e d i t e d i n t h e

b o t t o m p a n e . T h e t o p p a n e d i s p l a y s a c o p y o f t h e i n p u t p h r a s e s a s t h e y a r e p r o c e s s e d b y

t h e C a m l L i g h t t o p l e v e l , i n t e r s p e r s e d w i t h t h e t o p l e v e l r e s p o n s e s . T h e \ R e t u r n " k e y s e n d s

t h e c o n t e n t s o f t h e b o t t o m p a n e t o t h e C a m l L i g h t t o p l e v e l . T h e \ E n t e r " k e y i n s e r t s a

n e w l i n e w i t h o u t s e n d i n g t h e c o n t e n t s o f t h e I n p u t w i n d o w . ( T h i s c a n b e c o n g u r e d w i t h

t h e \ P r e f e r e n c e s " m e n u i t e m . )

T h e c o n t e n t s o f t h e i n p u t w i n d o w c a n b e e d i t e d a t a l l t i m e s , w i t h t h e s t a n d a r d W i n d o w s

i n t e r f a c e . A n h i s t o r y o f p r e v i o u s l y e n t e r e d p h r a s e s i s m a i n t a i n e d a n d d i s p l a y e d i n a s e p a r a t e

w i n d o w .

T o q u i t t h e C a m l w i n a p p l i c a t i o n , e i t h e r s e l e c t \ Q u i t " f r o m t h e \ F i l e " m e n u , o r u s e t h e q u i t

f u n c t i o n d e s c r i b e d b e l o w .

A t a n y p o i n t , t h e p a r s i n g , c o m p i l a t i o n o r e v a l u a t i o n o f t h e c u r r e n t p h r a s e c a n b e i n t e r r u p t e d

b y s e l e c t i n g t h e \ I n t e r r u p t C a m l L i g h t " m e n u i t e m . T h i s g o e s b a c k t o t h e # p r o m p t .

A t e x t - o n l y v e r s i o n o f t h e t o p l e v e l s y s t e m i s a v a i l a b l e u n d e r t h e n a m e c a m l . e x e . I t r u n s

u n d e r M S D O S a s w e l l a s u n d e r W i n d o w s i n a D O S w i n d o w . N o e d i t i n g f a c i l i t i e s a r e

p r o v i d e d .

5 . 1 O p t i o n s

T h e f o l l o w i n g c o m m a n d - l i n e o p t i o n s a r e r e c o g n i z e d b y t h e c a m l o r c a m l l i g h t c o m m a n d s .

- g S t a r t t h e t o p l e v e l s y s t e m i n d e b u g g i n g m o d e . T h i s m o d e g i v e s a c c e s s t o v a l u e s a n d t y p e s

t h a t a r e l o c a l t o a m o d u l e , t h a t i s , n o t e x p o r t e d b y t h e i n t e r f a c e o f t h e m o d u l e . W h e n

d e b u g g i n g m o d e i s o , t h e s e l o c a l o b j e c t s a r e n o t a c c e s s i b l e ( a t t e m p t s t o a c c e s s t h e m p r o d u c e

a n \ U n b o u n d i d e n t i e r " e r r o r ) . W h e n d e b u g g i n g m o d e i s o n , t h e s e o b j e c t s b e c o m e v i s i b l e ,

j u s t l i k e t h e o b j e c t s t h a t a r e e x p o r t e d i n t h e m o d u l e i n t e r f a c e . I n p a r t i c u l a r , v a l u e s o f a b s t r a c t

t y p e s a r e p r i n t e d u s i n g t h e i r c o n c r e t e r e p r e s e n t a t i o n s , a n d t h e f u n c t i o n s l o c a l t o a m o d u l e

c a n b e \ t r a c e d " ( s e e t h e t r a c e f u n c t i o n i n s e c t i o n 5 . 2 ) . T h i s a p p l i e s o n l y t o t h e m o d u l e s t h a t

h a v e b e e n c o m p i l e d i n d e b u g g i n g m o d e ( e i t h e r b y t h e b a t c h c o m p i l e r w i t h t h e - g o p t i o n , o r

b y t h e t o p l e v e l s y s t e m i n d e b u g g i n g m o d e ) , t h a t i s , t h o s e m o d u l e s t h a t h a v e a n a s s o c i a t e d

. z i x l e .

- I d i r e c t o r y

A d d t h e g i v e n d i r e c t o r y t o t h e l i s t o f d i r e c t o r i e s s e a r c h e d f o r c o m p i l e d i n t e r f a c e l e s ( . z i ) a n d

c o m p i l e d o b j e c t c o d e l e s ( . z o ) . B y d e f a u l t , t h e c u r r e n t d i r e c t o r y i s s e a r c h e d r s t , t h e n t h e

s t a n d a r d l i b r a r y d i r e c t o r y . D i r e c t o r i e s a d d e d w i t h - I a r e s e a r c h e d a f t e r t h e c u r r e n t d i r e c t o r y ,

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 57/218

C h a p t e r 5 . T h e t o p l e v e l s y s t e m ( c a m l l i g h t ) 5 5

b u t b e f o r e t h e s t a n d a r d l i b r a r y d i r e c t o r y . W h e n s e v e r a l d i r e c t o r i e s a r e a d d e d w i t h s e v e r a l - I

o p t i o n s o n t h e c o m m a n d l i n e , t h e s e d i r e c t o r i e s a r e s e a r c h e d f r o m r i g h t t o l e f t ( t h e r i g h t m o s t

d i r e c t o r y i s s e a r c h e d r s t , t h e l e f t m o s t i s s e a r c h e d l a s t ) . D i r e c t o r i e s c a n a l s o b e a d d e d t o t h e

s e a r c h p a t h o n c e t h e t o p l e v e l i s r u n n i n g w i t h t h e # d i r e c t o r y d i r e c t i v e ; s e e c h a p t e r 3 .

- l a n g l a n g u a g e - c o d e

T r a n s l a t e t h e t o p l e v e l m e s s a g e s t o t h e s p e c i e d l a n g u a g e . T h e l a n g u a g e - c o d e i s f r f o r F r e n c h ,

e s f o r S p a n i s h , d e f o r G e r m a n , : : : ( S e e t h e l e c a m l m s g s . t x t i n t h e C a m l L i g h t s t a n d a r d

l i b r a r y d i r e c t o r y f o r a l i s t o f a v a i l a b l e l a n g u a g e s . ) W h e n a n u n k n o w n l a n g u a g e i s s p e c i e d ,

o r n o t r a n s l a t i o n i s a v a i l a b l e f o r a m e s s a g e , A m e r i c a n E n g l i s h i s u s e d b y d e f a u l t .

- O m o d u l e - s e t

S p e c i f y w h i c h s e t o f s t a n d a r d m o d u l e s i s t o b e i m p l i c i t l y \ o p e n e d " w h e n t h e t o p l e v e l s t a r t s .

T h e r e a r e t h r e e m o d u l e s e t s c u r r e n t l y a v a i l a b l e :

c a u t i o u s

p r o v i d e s t h e s t a n d a r d o p e r a t i o n s o n i n t e g e r s , o a t i n g - p o i n t n u m b e r s , c h a r a c t e r s , s t r i n g s ,

a r r a y s , : : : , a s w e l l a s e x c e p t i o n h a n d l i n g , b a s i c i n p u t / o u t p u t , : : : O p e r a t i o n s f r o m t h e

c a u t i o u s s e t p e r f o r m r a n g e a n d b o u n d c h e c k i n g o n s t r i n g a n d v e c t o r o p e r a t i o n s , a s w e l l

a s v a r i o u s s a n i t y c h e c k s o n t h e i r a r g u m e n t s .

f a s t

p r o v i d e s t h e s a m e o p e r a t i o n s a s t h e c a u t i o u s s e t , b u t w i t h o u t s a n i t y c h e c k s o n t h e i r

a r g u m e n t s . P r o g r a m s c o m p i l e d w i t h - O f a s t a r e t h e r e f o r e s l i g h t l y f a s t e r , b u t u n s a f e .

n o n e

s u p p r e s s e s a l l a u t o m a t i c o p e n i n g o f m o d u l e s . C o m p i l a t i o n s t a r t s i n a n a l m o s t e m p t y

e n v i r o n m e n t . T h i s o p t i o n i s n o t o f g e n e r a l u s e .

T h e d e f a u l t c o m p i l a t i o n m o d e i s - O c a u t i o u s . S e e c h a p t e r 1 3 f o r a c o m p l e t e l i s t i n g o f t h e

m o d u l e s i n t h e c a u t i o u s a n d f a s t s e t s .

U n i x : T h e f o l l o w i n g e n v i r o n m e n t v a r i a b l e s a r e a l s o c o n s u l t e d :

L A N G

W h e n s e t , c o n t r o l w h i c h l a n g u a g e i s u s e d t o p r i n t t h e c o m p i l e r m e s s a g e s ( s e e t h e - l a n g

c o m m a n d - l i n e o p t i o n ) .

L C _ C T Y P E

I f s e t t o i s o _ 8 8 5 9 _ 1 , a c c e n t e d c h a r a c t e r s ( f r o m t h e I S O L a t i n - 1 c h a r a c t e r s e t ) i n s t r i n g

a n d c h a r a c t e r l i t e r a l s a r e p r i n t e d a s i s ; o t h e r w i s e , t h e y a r e p r i n t e d a s d e c i m a l e s c a p e

s e q u e n c e s ( \ d d d ) .

5 . 2 T o p l e v e l c o n t r o l f u n c t i o n s

T h e s t a n d a r d l i b r a r y m o d u l e t o p l e v e l , o p e n e d b y d e f a u l t w h e n t h e t o p l e v e l s y s t e m i s l a u n c h e d ,

p r o v i d e s a n u m b e r o f f u n c t i o n s t h a t c o n t r o l t h e t o p l e v e l b e h a v i o r , l o a d l e s i n m e m o r y , a n d t r a c e

p r o g r a m e x e c u t i o n .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 58/218

5 6

v a l u e q u i t : u n i t - > u n i t

E x i t t h e t o p l e v e l l o o p a n d t e r m i n a t e t h e c a m l l i g h t c o m m a n d .

v a l u e i n c l u d e : s t r i n g - > u n i t

R e a d , c o m p i l e a n d e x e c u t e s o u r c e p h r a s e s f r o m t h e g i v e n l e . T h e . m l e x t e n s i o n i s

a u t o m a t i c a l l y a d d e d t o t h e l e n a m e , i f n o t p r e s e n t . T h i s i s t e x t u a l i n c l u s i o n : p h r a s e s a r e

p r o c e s s e d j u s t a s i f t h e y w e r e t y p e d o n s t a n d a r d i n p u t . I n p a r t i c u l a r , g l o b a l i d e n t i e r s

d e n e d b y t h e s e p h r a s e s a r e e n t e r e d i n t h e m o d u l e n a m e d t o p , n o t i n a n e w m o d u l e .

v a l u e l o a d : s t r i n g - > u n i t

L o a d i n m e m o r y t h e s o u r c e c o d e f o r a m o d u l e i m p l e m e n t a t i o n . R e a d , c o m p i l e a n d e x e c u t e

s o u r c e p h r a s e s f r o m t h e g i v e n l e . T h e . m l e x t e n s i o n i s a u t o m a t i c a l l y a d d e d i f n o t p r e s e n t .

T h e l o a d f u n c t i o n b e h a v e s m u c h l i k e i n c l u d e , e x c e p t t h a t a n e w m o d u l e i s c r e a t e d , w i t h

n a m e t h e b a s e n a m e o f t h e s o u r c e l e n a m e . G l o b a l i d e n t i e r s d e n e d i n t h e s o u r c e l e a r e

e n t e r e d i n t h i s m o d u l e , i n s t e a d o f t h e t o p m o d u l e a s i n t h e c a s e o f i n c l u d e . F o r i n s t a n c e ,

a s s u m i n g l e f o o . m l c o n t a i n s t h e s i n g l e p h r a s e

l e t b a r = 1 ; ;

e x e c u t i n g l o a d " f o o " d e n e s t h e i d e n t i e r f o o _ _ b a r w i t h v a l u e 1 . C a v e a t : t h e l o a d e d

m o d u l e i s n o t a u t o m a t i c a l l y o p e n e d : t h e i d e n t i e r b a r d o e s n o t a u t o m a t i c a l l y c o m p l e t e t o

f o o _ _ b a r . T o a c h i e v e t h i s , y o u m u s t e x e c u t e t h e d i r e c t i v e # o p e n " f o o " a f t e r w a r d s .

v a l u e c o m p i l e : s t r i n g - > u n i t

C o m p i l e t h e s o u r c e c o d e f o r a m o d u l e i m p l e m e n t a t i o n o r i n t e r f a c e ( . m l o r . m l i l e ) .

C o m p i l a t i o n p r o c e e d s a s w i t h t h e b a t c h c o m p i l e r , a n d p r o d u c e s t h e s a m e r e s u l t s a s

c a m l c - c . I f t h e t o p l e v e l s y s t e m i s i n d e b u g g i n g m o d e ( o p t i o n - g o r f u n c t i o n d e b u g _ m o d e

b e l o w ) , t h e c o m p i l a t i o n i s a l s o p e r f o r m e d i n d e b u g g i n g m o d e , a s w h e n g i v i n g t h e - g o p t i o n

t o t h e b a t c h c o m p i l e r . T h e r e s u l t o f t h e c o m p i l a t i o n i s l e f t i n l e s ( . z o , . z i , . z i x ) . T h e

c o m p i l e d c o d e i s n o t l o a d e d i n m e m o r y . U s e l o a d _ o b j e c t t o l o a d a . z o l e p r o d u c e d b y

c o m p i l e .

v a l u e l o a d _ o b j e c t : s t r i n g - > u n i t

L o a d i n m e m o r y t h e c o m p i l e d b y t e c o d e c o n t a i n e d i n t h e g i v e n l e . T h e . z o e x t e n s i o n i s

a u t o m a t i c a l l y a d d e d t o t h e l e n a m e , i f n o t p r e s e n t . T h e b y t e c o d e l e h a s b e e n p r o d u c e d

e i t h e r b y t h e s t a n d a l o n e c o m p i l e r c a m l c o r b y t h e c o m p i l e f u n c t i o n . G l o b a l i d e n t i e r s

d e n e d i n t h e l e b e i n g l o a d e d a r e e n t e r e d i n t h e i r o w n m o d u l e , n o t i n t h e t o p m o d u l e , j u s t

a s w i t h t h e l o a d f u n c t i o n .

v a l u e t r a c e : s t r i n g - > u n i t

A f t e r t h e e x e c u t i o n o f t r a c e " f o o " , a l l c a l l s t o t h e g l o b a l f u n c t i o n n a m e d f o o w i l l b e

\ t r a c e d " . T h a t i s , t h e a r g u m e n t a n d t h e r e s u l t a r e d i s p l a y e d f o r e a c h c a l l , a s w e l l a s t h e

e x c e p t i o n s e s c a p i n g o u t o f f o o , e i t h e r r a i s e d b y f o o i t s e l f , o r r a i s e d b y o n e o f t h e f u n c t i o n s

c a l l e d f r o m f o o . I f f o o i s a c u r r i e d f u n c t i o n , e a c h a r g u m e n t i s p r i n t e d a s i t i s p a s s e d t o t h e

f u n c t i o n . O n l y f u n c t i o n s i m p l e m e n t e d i n M L c a n b e t r a c e d ; s y s t e m p r i m i t i v e s s u c h a s

s t r i n g _ l e n g t h o r u s e r - s u p p l i e d C f u n c t i o n s c a n n o t .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 59/218

C h a p t e r 5 . T h e t o p l e v e l s y s t e m ( c a m l l i g h t ) 5 7

v a l u e u n t r a c e : s t r i n g - > u n i t

E x e c u t i n g u n t r a c e " f o o " s t o p s a l l t r a c i n g o v e r t h e g l o b a l f u n c t i o n n a m e d f o o .

v a l u e v e r b o s e _ m o d e : b o o l - > u n i t

v e r b o s e _ m o d e t r u e c a u s e s t h e c o m p i l e f u n c t i o n t o p r i n t t h e i n f e r r e d t y p e s a n d o t h e r

i n f o r m a t i o n . v e r b o s e _ m o d e f a l s e r e v e r t s t o t h e d e f a u l t s i l e n t b e h a v i o r .

v a l u e i n s t a l l _ p r i n t e r : s t r i n g - > u n i t

i n s t a l l _ p r i n t e r " p r i n t e r n a m e " r e g i s t e r s t h e f u n c t i o n n a m e d p r i n t e r n a m e a s a p r i n t e r

f o r o b j e c t s w h o s e t y p e s m a t c h i t s a r g u m e n t t y p e . T h a t i s , t h e t o p l e v e l l o o p w i l l c a l l

p r i n t e r n a m e w h e n i t h a s s u c h a n o b j e c t t o p r i n t . T h e p r i n t i n g f u n c t i o n p r i n t e r n a m e m u s t

u s e t h e f o r m a t l i b r a r y m o d u l e t o p r o d u c e i t s o u t p u t , o t h e r w i s e t h e o u t p u t o f p r i n t e r n a m e

w i l l n o t b e c o r r e c t l y l o c a t e d i n t h e v a l u e s p r i n t e d b y t h e t o p l e v e l l o o p .

v a l u e r e m o v e _ p r i n t e r : s t r i n g - > u n i t

r e m o v e _ p r i n t e r " p r i n t e r n a m e " r e m o v e s t h e f u n c t i o n n a m e d p r i n t e r n a m e f r o m t h e t a b l e

o f t o p l e v e l p r i n t e r s .

v a l u e s e t _ p r i n t _ d e p t h : i n t - > u n i t

s e t _ p r i n t _ d e p t h n l i m i t s t h e p r i n t i n g o f v a l u e s t o a m a x i m a l d e p t h o f n . T h e p a r t s o f

v a l u e s w h o s e d e p t h e x c e e d s n a r e p r i n t e d a s . . . ( e l l i p s i s ) .

v a l u e s e t _ p r i n t _ l e n g t h : i n t - > u n i t

s e t _ p r i n t _ l e n g t h n l i m i t s t h e n u m b e r o f v a l u e n o d e s p r i n t e d t o a t m o s t n . R e m a i n i n g

p a r t s o f v a l u e s a r e p r i n t e d a s . . . ( e l l i p s i s ) .

v a l u e d e b u g _ m o d e : b o o l - > u n i t

S e t w h e t h e r e x t e n d e d m o d u l e i n t e r f a c e s m u s t b e u s e d d e b u g _ m o d e t r u e o r n o t

d e b u g _ m o d e f a l s e . E x t e n d e d m o d u l e i n t e r f a c e s a r e . z i x l e s t h a t d e s c r i b e t h e a c t u a l

i m p l e m e n t a t i o n o f a m o d u l e , i n c l u d i n g p r i v a t e t y p e s a n d v a r i a b l e s . T h e y a r e g e n e r a t e d

w h e n c o m p i l i n g w i t h c a m l c - g , o r w i t h t h e c o m p i l e f u n c t i o n a b o v e w h e n d e b u g _ m o d e i s

t r u e . W h e n d e b u g _ m o d e i s t r u e , t o p l e v e l p h r a s e s c a n r e f e r t o p r i v a t e t y p e s a n d v a r i a b l e s o f

m o d u l e s , a n d p r i v a t e f u n c t i o n s c a n b e t r a c e d w i t h t r a c e . S e t t i n g d e b u g _ m o d e t r u e i s

e q u i v a l e n t t o s t a r t i n g t h e t o p l e v e l w i t h t h e - g o p t i o n .

v a l u e c d : s t r i n g - > u n i t

C h a n g e t h e c u r r e n t w o r k i n g d i r e c t o r y .

v a l u e d i r e c t o r y : s t r i n g - > u n i t

A d d t h e g i v e n d i r e c t o r y t o t h e s e a r c h p a t h f o r l e s . S a m e b e h a v i o r a s t h e - I o p t i o n o r t h e

# d i r e c t o r y d i r e c t i v e .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 60/218

5 8

5 . 3 T h e t o p l e v e l a n d t h e m o d u l e s y s t e m

T o p l e v e l p h r a s e s c a n r e f e r t o i d e n t i e r s d e n e d i n m o d u l e s o t h e r t h a n t h e t o p m o d u l e w i t h

t h e s a m e m e c h a n i s m s a s f o r s e p a r a t e l y c o m p i l e d m o d u l e s : e i t h e r b y u s i n g q u a l i e d i d e n t i e r s

( m o d u l e n a m e _ _ l o c a l n a m e ) , o r b y u s i n g u n q u a l i e d i d e n t i e r s t h a t a r e a u t o m a t i c a l l y c o m p l e t e d

b y s e a r c h i n g t h e l i s t o f o p e n e d m o d u l e s . ( S e e s e c t i o n 2 . 2 . ) T h e m o d u l e s o p e n e d a t s t a r t u p a r e

g i v e n i n t h e d o c u m e n t a t i o n f o r t h e s t a n d a r d l i b r a r y . O t h e r m o d u l e s c a n b e o p e n e d w i t h t h e # o p e n

d i r e c t i v e .

H o w e v e r , b e f o r e r e f e r e n c i n g a g l o b a l v a r i a b l e f r o m a m o d u l e o t h e r t h a n t h e t o p m o d u l e , a

d e n i t i o n o f t h a t g l o b a l v a r i a b l e m u s t h a v e b e e n e n t e r e d i n m e m o r y . A t s t a r t - u p , t h e t o p l e v e l

s y s t e m c o n t a i n s t h e d e n i t i o n s f o r a l l t h e i d e n t i e r s i n t h e s t a n d a r d l i b r a r y . T h e d e n i t i o n s f o r

u s e r m o d u l e s c a n b e e n t e r e d w i t h t h e l o a d o r l o a d _ o b j e c t f u n c t i o n s d e s c r i b e d a b o v e . R e f e r e n c i n g

a g l o b a l v a r i a b l e f o r w h i c h n o d e n i t i o n h a s b e e n p r o v i d e d b y l o a d o r l o a d _ o b j e c t r e s u l t s i n

t h e e r r o r \ I d e n t i f i e r f o o _ _ b a r i s r e f e r e n c e d b e f o r e b e i n g d e f i n e d " . S i n c e t h i s i s a t r i c k y

p o i n t , l e t u s c o n s i d e r s o m e e x a m p l e s .

1 . T h e l i b r a r y f u n c t i o n s u b _ s t r i n g i s d e n e d i n m o d u l e s t r i n g . T h i s m o d u l e i s p a r t o f t h e

s t a n d a r d l i b r a r y , a n d i s o n e o f t h e m o d u l e s a u t o m a t i c a l l y o p e n e d a t s t a r t - u p . H e n c e , b o t h

p h r a s e s

s u b _ s t r i n g " q w e r t y " 1 3 ; ;

s t r i n g _ _ s u b _ s t r i n g " q w e r t y " 1 3 ; ;

a r e c o r r e c t , w i t h o u t h a v i n g t o u s e # o p e n , l o a d , o r l o a d _ o b j e c t .

2 . T h e l i b r a r y f u n c t i o n p r i n t f i s d e n e d i n m o d u l e p r i n t f . T h i s m o d u l e i s p a r t o f t h e s t a n d a r d

l i b r a r y , b u t i t i s n o t a u t o m a t i c a l l y o p e n e d a t s t a r t - u p . H e n c e , t h e p h r a s e

p r i n t f _ _ p r i n t f " % s % s " " h e l l o " " w o r l d " ; ;

i s c o r r e c t l y e x e c u t e d , w h i l e

p r i n t f " % s % s " " h e l l o " " w o r l d " ; ;

c a u s e s t h e e r r o r \ V a r i a b l e p r i n t f i s u n b o u n d " , s i n c e n o n e o f t h e c u r r e n t l y o p e n e d m o d -

u l e s d e n e a g l o b a l w i t h l o c a l n a m e p r i n t f . H o w e v e r ,

# o p e n " p r i n t f " ; ;

p r i n t f " % s % s " " h e l l o " " w o r l d " ; ;

e x e c u t e s c o r r e c t l y .

3 . A s s u m e t h e l e f o o . m l r e s i d e s i n t h e c u r r e n t d i r e c t o r y , a n d c o n t a i n s t h e s i n g l e p h r a s e

l e t x = 1 ; ;

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 61/218

C h a p t e r 5 . T h e t o p l e v e l s y s t e m ( c a m l l i g h t ) 5 9

W h e n t h e t o p l e v e l s t a r t s , r e f e r e n c e s t o x w i l l c a u s e t h e e r r o r \ V a r i a b l e x i s u n b o u n d " .

R e f e r e n c e s t o f o o _ _ x w i l l c a u s e t h e e r r o r \ C a n n o t f i n d f i l e f o o . z i " , s i n c e t h e t y p e c h e c k e r

i s a t t e m p t i n g t o l o a d t h e c o m p i l e d i n t e r f a c e f o r m o d u l e f o o i n o r d e r t o n d t h e t y p e o f x . T o

l o a d i n m e m o r y t h e m o d u l e f o o , j u s t d o :

l o a d " f o o " ; ;

T h e n , r e f e r e n c e s t o f o o _ _ x t y p e c h e c k a n d e v a l u a t e c o r r e c t l y . S i n c e l o a d d o e s n o t o p e n t h e

m o d u l e i t l o a d s , r e f e r e n c e s t o x w i l l s t i l l f a i l w i t h t h e e r r o r \ V a r i a b l e x i s u n b o u n d " . Y o u

w i l l h a v e t o d o

# o p e n " f o o " ; ;

e x p l i c i t l y , f o r x t o c o m p l e t e a u t o m a t i c a l l y i n t o f o o _ _ x .

4 . F i n a l l y , a s s u m e t h e l e f o o . m l a b o v e h a s b e e n p r e v i o u s l y c o m p i l e d w i t h t h e c a m l c - c c o m -

m a n d . T h e c u r r e n t d i r e c t o r y t h e r e f o r e c o n t a i n s a c o m p i l e d i n t e r f a c e f o o . z i , c l a i m i n g t h a t

f o o _ _ x i s a g l o b a l v a r i a b l e w i t h t y p e i n t , a n d a c o m p i l e d b y t e c o d e l e f o o . z o , d e n i n g

f o o _ _ x t o h a v e t h e v a l u e 1 . W h e n t h e t o p l e v e l s t a r t s , r e f e r e n c e s t o f o o _ _ x w i l l c a u s e t h e

e r r o r \ f o o _ _ x i s r e f e r e n c e d b e f o r e b e i n g d e f i n e d " . I n c o n t r a s t w i t h c a s e 3 a b o v e , t h e

t y p e c h e c k e r h a s s u c c e e d e d i n n d i n g t h e c o m p i l e d i n t e r f a c e f o r m o d u l e f o o . B u t e x e c u t i o n

c a n n o t p r o c e e d , b e c a u s e n o d e n i t i o n f o r f o o _ _ x h a s b e e n e n t e r e d i n m e m o r y . T o d o s o ,

e x e c u t e :

l o a d _ o b j e c t " f o o " ; ;

T h i s l o a d s t h e l e f o o . z o i n m e m o r y , t h e r e f o r e d e n i n g f o o _ _ x . T h e n , r e f e r e n c e s t o f o o _ _ x

e v a l u a t e c o r r e c t l y . A s i n c a s e 3 a b o v e , r e f e r e n c e s t o x s t i l l f a i l , b e c a u s e l o a d _ o b j e c t d o e s

n o t o p e n t h e m o d u l e i t l o a d s . A g a i n , y o u w i l l h a v e t o d o

# o p e n " f o o " ; ;

e x p l i c i t l y , f o r x t o c o m p l e t e a u t o m a t i c a l l y i n t o f o o _ _ x .

5 . 4 C o m m o n e r r o r s

T h i s s e c t i o n d e s c r i b e s a n d e x p l a i n s t h e m o s t f r e q u e n t l y e n c o u n t e r e d e r r o r m e s s a g e s .

C a n n o t n d l e l e n a m e

T h e n a m e d l e c o u l d n o t b e f o u n d i n t h e c u r r e n t d i r e c t o r y , n o r i n t h e d i r e c t o r i e s o f t h e s e a r c h

p a t h .

I f l e n a m e h a s t h e f o r m a t m o d . z i , t h i s m e a n s t h e c u r r e n t p h r a s e r e f e r e n c e s i d e n t i e r s f r o m

m o d u l e m o d , b u t y o u h a v e n o t y e t c o m p i l e d a n i n t e r f a c e f o r m o d u l e m o d . F i x : e i t h e r l o a d

t h e l e m o d . m l , w h i c h w i l l a l s o c r e a t e i n m e m o r y t h e c o m p i l e d i n t e r f a c e f o r m o d u l e m o d ; o r

u s e c a m l c t o c o m p i l e m o d . m l i o r m o d . m l , c r e a t i n g t h e c o m p i l e d i n t e r f a c e m o d . z i , b e f o r e

y o u s t a r t t h e t o p l e v e l .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 62/218

6 0

I f l e n a m e h a s t h e f o r m a t m o d . z o , t h i s m e a n s y o u a r e t r y i n g t o l o a d w i t h l o a d _ o b j e c t a

b y t e c o d e o b j e c t l e t h a t d o e s n o t e x i s t y e t . F i x : c o m p i l e m o d . m l w i t h c a m l c b e f o r e y o u

s t a r t t h e t o p l e v e l . O r , u s e l o a d i n s t e a d o f l o a d _ o b j e c t t o l o a d t h e s o u r c e c o d e i n s t e a d o f a

c o m p i l e d o b j e c t l e .

I f l e n a m e h a s t h e f o r m a t m o d . m l , t h i s m e a n s l o a d o r i n c l u d e c o u l d n o t n d t h e s p e c i e d

s o u r c e l e . F i x : c h e c k t h e s p e l l i n g o f t h e l e n a m e , o r w r i t e i t i f i t d o e s n o t e x i s t .

m o d _ _ n a m e i s r e f e r e n c e d b e f o r e b e i n g d e n e d

Y o u h a v e n e g l e c t e d t o l o a d i n m e m o r y a n i m p l e m e n t a t i o n f o r a m o d u l e , w i t h l o a d o r

l o a d _ o b j e c t . T h i s i s e x p l a i n e d i n f u l l d e t a i l i n s e c t i o n 5 . 3 a b o v e .

C o r r u p t e d c o m p i l e d i n t e r f a c e l e l e n a m e

S e e s e c t i o n 4 . 4 .

E x p r e s s i o n o f t y p e t

1

c a n n o t b e u s e d w i t h t y p e t

2

S e e s e c t i o n 4 . 4 .

T h e t y p e i n f e r r e d f o r t h e v a l u e n a m e , t h a t i s , t , c o n t a i n s t y p e v a r i a b l e s t h a t c a n n o t b e g e n e r a l i z

S e e s e c t i o n 4 . 4 .

5 . 5 B u i l d i n g c u s t o m t o p l e v e l s y s t e m s : c a m l m k t o p

T h e c a m l m k t o p c o m m a n d b u i l d s C a m l L i g h t t o p l e v e l s t h a t c o n t a i n u s e r c o d e p r e l o a d e d a t s t a r t - u p .

M a c : T h i s c o m m a n d i s n o t a v a i l a b l e i n t h e M a c i n t o s h v e r s i o n .

T h e c a m l m k t o p c o m m a n d t a k e s a s a r g u m e n t a s e t o f . z o l e s , a n d l i n k s t h e m w i t h t h e o b j e c t

l e s t h a t i m p l e m e n t t h e C a m l L i g h t t o p l e v e l . T h e t y p i c a l u s e i s :

c a m l m k t o p - o m y t o p l e v e l f o o . z o b a r . z o g e e . z o

T h i s c r e a t e s t h e b y t e c o d e l e m y t o p l e v e l , c o n t a i n i n g t h e C a m l L i g h t t o p l e v e l s y s t e m , p l u s t h e

c o d e f r o m t h e t h r e e . z o l e s . T o r u n t h i s t o p l e v e l , g i v e i t a s a r g u m e n t t o t h e c a m l l i g h t c o m m a n d :

c a m l l i g h t m y t o p l e v e l

T h i s s t a r t s a r e g u l a r t o p l e v e l l o o p , e x c e p t t h a t t h e c o d e f r o m f o o . z o , b a r . z o a n d g e e . z o i s a l r e a d y

l o a d e d i n m e m o r y , j u s t a s i f y o u h a d t y p e d :

l o a d _ o b j e c t " f o o " ; ;

l o a d _ o b j e c t " b a r " ; ;

l o a d _ o b j e c t " g e e " ; ;

o n e n t r a n c e t o t h e t o p l e v e l . T h e m o d u l e s f o o , b a r a n d g e e a r e n o t o p e n e d , t h o u g h ; y o u s t i l l h a v e

t o d o

# o p e n " f o o " ; ;

y o u r s e l f , i f t h i s i s w h a t y o u w i s h .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 63/218

C h a p t e r 5 . T h e t o p l e v e l s y s t e m ( c a m l l i g h t ) 6 1

5 . 6 O p t i o n s

T h e f o l l o w i n g c o m m a n d - l i n e o p t i o n s a r e r e c o g n i z e d b y c a m l m k t o p .

- c c o p t o p t i o n

P a s s t h e g i v e n o p t i o n t o t h e C c o m p i l e r a n d l i n k e r , w h e n l i n k i n g i n \ c u s t o m r u n t i m e " m o d e .

S e e t h e c o r r e s p o n d i n g o p t i o n f o r c a m l c , i n c h a p t e r 4 .

- c u s t o m

L i n k i n \ c u s t o m r u n t i m e " m o d e . S e e t h e c o r r e s p o n d i n g o p t i o n f o r c a m l c , i n c h a p t e r 4 .

- g A d d d e b u g g i n g i n f o r m a t i o n t o t h e t o p l e v e l l e p r o d u c e d , w h i c h c a n t h e n b e d e b u g g e d w i t h

c a m l d e b u g ( c h a p t e r 9 ) .

- I d i r e c t o r y

A d d t h e g i v e n d i r e c t o r y t o t h e l i s t o f d i r e c t o r i e s s e a r c h e d f o r c o m p i l e d o b j e c t c o d e l e s ( . z o ) .

- o e x e c - l e

S p e c i f y t h e n a m e o f t h e t o p l e v e l l e p r o d u c e d b y t h e l i n k e r .

U n i x : T h e d e f a u l t i s c a m l t o p . o u t .

P C : T h e d e f a u l t i s c a m l t o p . e x e . T h e n a m e m u s t h a v e . e x e e x t e n s i o n .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 64/218

6 2

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 65/218

C h a p t e r 6

T h e r u n t i m e s y s t e m ( c a m l r u n )

T h e c a m l r u n c o m m a n d e x e c u t e s b y t e c o d e l e s p r o d u c e d b y t h e l i n k i n g p h a s e o f t h e c a m l c c o m -

m a n d .

M a c : T h i s c o m m a n d i s a M P W t o o l , n o t a s t a n d a l o n e M a c i n t o s h a p p l i c a t i o n .

6 . 1 O v e r v i e w

T h e c a m l r u n c o m m a n d c o m p r i s e s t h r e e m a i n p a r t s : t h e b y t e c o d e i n t e r p r e t e r , t h a t a c t u a l l y e x e c u t e s

b y t e c o d e l e s ; t h e m e m o r y a l l o c a t o r a n d g a r b a g e c o l l e c t o r ; a n d a s e t o f C f u n c t i o n s t h a t i m p l e m e n t

p r i m i t i v e o p e r a t i o n s s u c h a s i n p u t / o u t p u t .

T h e u s a g e f o r c a m l r u n i s :

c a m l r u n o p t i o n s b y t e c o d e - e x e c u t a b l e a r g

1

. . . a r g

n

T h e r s t n o n - o p t i o n a r g u m e n t i s t a k e n t o b e t h e n a m e o f t h e l e c o n t a i n i n g t h e e x e c u t a b l e b y t e c o d e .

( T h a t l e i s s e a r c h e d i n t h e e x e c u t a b l e p a t h a s w e l l a s i n t h e c u r r e n t d i r e c t o r y . ) T h e r e m a i n i n g

a r g u m e n t s a r e p a s s e d t o t h e C a m l L i g h t p r o g r a m , i n t h e s t r i n g a r r a y s y s _ _ c o m m a n d _ l i n e . E l e m e n t

0 o f t h i s a r r a y i s t h e n a m e o f t h e b y t e c o d e e x e c u t a b l e l e ; e l e m e n t s 1 t o n a r e t h e r e m a i n i n g

a r g u m e n t s a r g

1

t o a r g

n

.

A s m e n t i o n e d i n c h a p t e r 4 , i n m o s t c a s e s , t h e b y t e c o d e e x e c u t a b l e l e s p r o d u c e d b y t h e c a m l c

c o m m a n d a r e s e l f - e x e c u t a b l e , a n d m a n a g e t o l a u n c h t h e c a m l r u n c o m m a n d o n t h e m s e l v e s a u t o -

m a t i c a l l y . T h a t i s , a s s u m i n g c a m l . o u t i s a b y t e c o d e e x e c u t a b l e l e ,

c a m l . o u t a r g

1

. . . a r g

n

w o r k s e x a c t l y a s

c a m l r u n c a m l . o u t a r g

1

. . . a r g

n

N o t i c e t h a t i t i s n o t p o s s i b l e t o p a s s o p t i o n s t o c a m l r u n w h e n i n v o k i n g c a m l . o u t d i r e c t l y .

6 3

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 66/218

6 4

6 . 2 O p t i o n s

T h e f o l l o w i n g c o m m a n d - l i n e o p t i o n i s r e c o g n i z e d b y c a m l r u n .

- V P r i n t o u t t h e c a m l r u n v e r s i o n n u m b e r . E x i t i m m e d i a t e l y w i t h o u t e x e c u t i n g a n y b y t e - c o d e

l e .

T h e f o l l o w i n g e n v i r o n m e n t v a r i a b l e a r e a l s o c o n s u l t e d :

C A M L R U N P A R A M

S e t t h e g a r b a g e c o l l e c t i o n p a r a m e t e r s . T h i s v a r i a b l e m u s t b e a s e q u e n c e o f p a r a m e t e r s p e c -

i c a t i o n s . A p a r a m e t e r s p e c i c a t i o n i s a n o p t i o n l e t t e r f o l l o w e d b y a n = s i g n a n d a d e c i m a l

n u m b e r . T h e r e a r e f o u r o p t i o n s , c o r r e s p o n d i n g t o t h e f o u r e l d s o f t h e c o n t r o l r e c o r d

d o c u m e n t e d i n s e c t i o n 1 4 . 5 :

s ( m i n o r _ h e a p _ s i z e ) S i z e o f t h e m i n o r h e a p .

i ( m a j o r _ h e a p _ i n c r e m e n t ) M i n i m u m s i z e i n c r e m e n t f o r t h e m a j o r h e a p .

o ( s p a c e _ o v e r h e a d ) T h e m a j o r G C s p e e d s e t t i n g .

v ( v e r b o s e ) W h e t h e r t o p r i n t G C m e s s a g e s o r n o t . 0 i s f a l s e ; 1 i s t r u e ; o t h e r v a l u e s m a y

g i v e u n e x p e c t e d r e s u l t s .

F o r e x a m p l e , u n d e r c s h t h e c o m m a n d

s e t e n v C A M L R U N P A R A M ' s = 2 5 0 0 0 0 v = 1 '

t e l l s a s u b s e q u e n t c a m l r u n t o s e t i t s i n i t i a l m i n o r h e a p s i z e t o a b o u t 1 m e g a b y t e ( o n a 3 2 - b i t

m a c h i n e ) a n d t o p r i n t i t s G C m e s s a g e s .

P A T H

L i s t o f d i r e c t o r i e s s e a r c h e d t o n d t h e b y t e c o d e e x e c u t a b l e l e .

6 . 3 C o m m o n e r r o r s

T h i s s e c t i o n d e s c r i b e s a n d e x p l a i n s t h e m o s t f r e q u e n t l y e n c o u n t e r e d e r r o r m e s s a g e s .

l e n a m e : n o s u c h f i l e o r d i r e c t o r y

I f l e n a m e i s t h e n a m e o f a s e l f - e x e c u t a b l e b y t e c o d e l e , t h i s m e a n s t h a t e i t h e r t h a t l e d o e s

n o t e x i s t , o r t h a t i t f a i l e d t o r u n t h e c a m l r u n b y t e c o d e i n t e r p r e t e r o n i t s e l f . T h e s e c o n d

p o s s i b i l i t y i n d i c a t e s t h a t C a m l L i g h t h a s n o t b e e n p r o p e r l y i n s t a l l e d o n y o u r s y s t e m .

C a n n o t e x e c c a m l r u n

( W h e n l a u n c h i n g a s e l f - e x e c u t a b l e b y t e c o d e l e . ) T h e c a m l r u n c o m m a n d c o u l d n o t b e f o u n d

i n t h e e x e c u t a b l e p a t h . C h e c k t h a t C a m l L i g h t h a s b e e n p r o p e r l y i n s t a l l e d o n y o u r s y s t e m .

C a n n o t f i n d t h e b y t e c o d e f i l e

T h e l e t h a t c a m l r u n i s t r y i n g t o e x e c u t e ( e . g . t h e l e g i v e n a s r s t n o n - o p t i o n a r g u m e n t t o

c a m l r u n ) e i t h e r d o e s n o t e x i s t , o r i s n o t a v a l i d e x e c u t a b l e b y t e c o d e l e .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 67/218

C h a p t e r 6 . T h e r u n t i m e s y s t e m ( c a m l r u n ) 6 5

T r u n c a t e d b y t e c o d e f i l e

T h e l e t h a t c a m l r u n i s t r y i n g t o e x e c u t e i s n o t a v a l i d e x e c u t a b l e b y t e c o d e l e . P r o b a b l y i t

h a s b e e n t r u n c a t e d o r m a n g l e d s i n c e c r e a t e d . E r a s e a n d r e b u i l d i t .

U n c a u g h t e x c e p t i o n

T h e p r o g r a m b e i n g e x e c u t e d c o n t a i n s a \ s t r a y " e x c e p t i o n . T h a t i s , i t r a i s e s a n e x c e p t i o n a t

s o m e p o i n t , a n d t h i s e x c e p t i o n i s n e v e r c a u g h t . T h i s c a u s e s i m m e d i a t e t e r m i n a t i o n o f t h e

p r o g r a m . I f y o u w i s h t o k n o w w h i c h e x c e p t i o n t h u s e s c a p e s , u s e t h e p r i n t e x c _ _ f f u n c t i o n

f r o m t h e s t a n d a r d l i b r a r y ( a n d d o n ' t f o r g e t t o l i n k y o u r p r o g r a m w i t h t h e - g o p t i o n ) .

O u t o f m e m o r y

T h e p r o g r a m b e i n g e x e c u t e d r e q u i r e s m o r e m e m o r y t h a n a v a i l a b l e . E i t h e r t h e p r o g r a m b u i l d s

t o o l a r g e d a t a s t r u c t u r e s ; o r t h e p r o g r a m c o n t a i n s t o o m a n y n e s t e d f u n c t i o n c a l l s , a n d t h e

s t a c k o v e r o w s . I n s o m e c a s e s , y o u r p r o g r a m i s p e r f e c t l y c o r r e c t , i t j u s t r e q u i r e s m o r e m e m o r y

t h a n y o u r m a c h i n e p r o v i d e s . ( T h i s h a p p e n s q u i t e f r e q u e n t l y o n s m a l l m i c r o c o m p u t e r s , b u t

i s u n l i k e l y o n U n i x m a c h i n e s . ) I n o t h e r c a s e s , t h e \ o u t o f m e m o r y " m e s s a g e r e v e a l s a n e r r o r

i n y o u r p r o g r a m : n o n - t e r m i n a t i n g r e c u r s i v e f u n c t i o n , a l l o c a t i o n o f a n e x c e s s i v e l y l a r g e a r r a y

o r s t r i n g , a t t e m p t s t o b u i l d a n i n n i t e l i s t o r o t h e r d a t a s t r u c t u r e , : : :

T o h e l p y o u d i a g n o s e t h i s e r r o r , r u n y o u r p r o g r a m w i t h t h e - v o p t i o n t o c a m l r u n . I f i t

d i s p l a y s l o t s o f \ G r o w i n g s t a c k : : : " m e s s a g e s , t h i s i s p r o b a b l y a l o o p i n g r e c u r s i v e f u n c t i o n .

I f i t d i s p l a y s l o t s o f \ G r o w i n g h e a p : : : " m e s s a g e s , w i t h t h e h e a p s i z e g r o w i n g s l o w l y , t h i s i s

p r o b a b l y a n a t t e m p t t o c o n s t r u c t a d a t a s t r u c t u r e w i t h t o o m a n y ( i n n i t e l y m a n y ? ) c e l l s .

I f i t d i s p l a y s f e w \ G r o w i n g h e a p : : : " m e s s a g e s , b u t w i t h a h u g e i n c r e m e n t i n t h e h e a p s i z e ,

t h i s i s p r o b a b l y a n a t t e m p t t o b u i l d a n e x c e s s i v e l y l a r g e a r r a y o r s t r i n g .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 68/218

6 6

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 69/218

C h a p t e r 7

T h e l i b r a r i a n ( c a m l l i b r )

M a c : T h i s c o m m a n d i s a M P W t o o l , n o t a s t a n d a l o n e M a c i n t o s h a p p l i c a t i o n .

7 . 1 O v e r v i e w

T h e c a m l l i b r p r o g r a m p a c k s i n o n e s i n g l e l e a s e t o f b y t e c o d e o b j e c t l e s ( . z o l e s ) . T h e r e s u l t i n g

l e i s a l s o a b y t e c o d e o b j e c t l e a n d a l s o h a s t h e . z o e x t e n s i o n . I t c a n b e p a s s e d t o t h e l i n k p h a s e

o f t h e c a m l c c o m p i l e r i n r e p l a c e m e n t o f t h e o r i g i n a l s e t o f b y t e c o d e o b j e c t l e s . T h a t i s , a f t e r

r u n n i n g

c a m l l i b r - o l i b r a r y . z o m o d 1 . z o m o d 2 . z o m o d 3 . z i m o d 4 . z o

a l l c a l l s t o t h e l i n k e r w i t h t h e f o r m

c a m l c . . . l i b r a r y . z o . . .

a r e e x a c t l y e q u i v a l e n t t o

c a m l c . . . m o d 1 . z o m o d 2 . z o m o d 3 . z i m o d 4 . z o . . .

T h e t y p i c a l u s e o f c a m l l i b r i s t o b u i l d a l i b r a r y c o m p o s e d o f s e v e r a l m o d u l e s : t h i s w a y , u s e r s

o f t h e l i b r a r y h a v e o n l y o n e . z o l e t o s p e c i f y o n t h e c o m m a n d l i n e t o c a m l c , i n s t e a d o f a b u n c h

o f . z o l e s , o n e p e r m o d u l e c o n t a i n e d i n t h e l i b r a r y .

T h e l i n k i n g p h a s e o f c a m l c i s c l e v e r e n o u g h t o d i s c a r d t h e c o d e c o r r e s p o n d i n g t o u s e l e s s p h r a s e s :

i n p a r t i c u l a r , d e n i t i o n s f o r g l o b a l v a r i a b l e s t h a t a r e n e v e r u s e d a f t e r t h e i r d e n i t i o n s . H e n c e , t h e r e

i s n o p r o b l e m w i t h p u t t i n g m a n y m o d u l e s , e v e n r a r e l y u s e d o n e s , i n t o o n e s i n g l e l i b r a r y : t h i s w i l l

n o t r e s u l t i n b i g g e r e x e c u t a b l e s .

T h e u s a g e f o r c a m l l i b r i s :

c a m l l i b r o p t i o n s l e

1

. z o . . . l e

n

. z o

w h e r e l e

1

. z o t h r o u g h l e

n

. z o a r e t h e o b j e c t l e s t o p a c k t o g e t h e r . T h e o r d e r i n w h i c h t h e s e l e

n a m e s a r e p r e s e n t e d o n t h e c o m m a n d l i n e i s r e l e v a n t : t h e c o m p i l e d p h r a s e s c o n t a i n e d i n t h e l i b r a r y

w i l l b e e x e c u t e d i n t h a t o r d e r . ( R e m e m b e r t h a t i t i s a l i n k - t i m e e r r o r t o r e f e r t o a g l o b a l v a r i a b l e

t h a t h a s n o t y e t b e e n d e n e d . )

6 7

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 70/218

6 8

7 . 2 O p t i o n s

T h e f o l l o w i n g c o m m a n d - l i n e o p t i o n s a r e r e c o g n i z e d b y c a m l l i b r .

- I d i r e c t o r y

A d d t h e g i v e n d i r e c t o r y t o t h e l i s t o f d i r e c t o r i e s s e a r c h e d f o r t h e i n p u t . z o l e s . B y d e f a u l t ,

t h e c u r r e n t d i r e c t o r y i s s e a r c h e d r s t , t h e n t h e s t a n d a r d l i b r a r y d i r e c t o r y . D i r e c t o r i e s a d d e d

w i t h - I a r e s e a r c h e d a f t e r t h e c u r r e n t d i r e c t o r y , b u t b e f o r e t h e s t a n d a r d l i b r a r y d i r e c t o r y .

W h e n s e v e r a l d i r e c t o r i e s a r e a d d e d w i t h s e v e r a l - I o p t i o n s o n t h e c o m m a n d l i n e , t h e s e d i r e c -

t o r i e s a r e s e a r c h e d f r o m r i g h t t o l e f t ( t h e r i g h t m o s t d i r e c t o r y i s s e a r c h e d r s t , t h e l e f t m o s t i s

s e a r c h e d l a s t ) .

- o l i b r a r y - n a m e

S p e c i f y t h e n a m e o f t h e o u t p u t l e . T h e d e f a u l t i s l i b r a r y . z o .

P C : T h e f o l l o w i n g o p t i o n i s a l s o s u p p o r t e d :

@ r e s p o n s e - l e

P r o c e s s t h e l e s w h o s e n a m e s a r e l i s t e d i n l e r e s p o n s e - l e , j u s t a s i f t h e s e n a m e s

a p p e a r e d o n t h e c o m m a n d l i n e . F i l e n a m e s i n r e s p o n s e - l e a r e s e p a r a t e d b y b l a n k s

( s p a c e s , t a b s , n e w l i n e s ) . T h i s o p t i o n a l l o w s t o o v e r c o m e s i l l y l i m i t a t i o n s o n t h e l e n g t h

o f t h e c o m m a n d l i n e .

7 . 3 T u r n i n g c o d e i n t o a l i b r a r y

T o d e v e l o p a l i b r a r y , i t i s u s u a l l y m o r e c o n v e n i e n t t o s p l i t i t i n t o s e v e r a l m o d u l e s , t h a t r e e c t t h e

i n t e r n a l s t r u c t u r e o f t h e l i b r a r y . F r o m t h e s t a n d p o i n t o f t h e l i b r a r y u s e r s , h o w e v e r , i t i s p r e f e r a b l e

t o v i e w t h e l i b r a r y a s a s i n g l e m o d u l e , w i t h o n l y o n e i n t e r f a c e l e ( . z i l e ) a n d o n e i m p l e m e n t a t i o n

l e ( . z o l e ) : l i n k i n g i s e a s i e r , a n d t h e r e i s n o n e e d t o p u t a b u n c h o f # o p e n d i r e c t i v e s , n o r t o h a v e

t o r e m e m b e r t h e i n t e r n a l s t r u c t u r e o f t h e l i b r a r y .

T h e c a m l l i b r c o m m a n d a l l o w s h a v i n g a s i n g l e . z o l e f o r t h e w h o l e l i b r a r y . H e r e i s h o w t h e

C a m l L i g h t m o d u l e s y s t e m c a n b e u s e d ( s o m e s a y \ a b u s e d " ) t o h a v e a s i n g l e . z i l e f o r t h e w h o l e

l i b r a r y . T o b e m o r e c o n c r e t e , a s s u m e t h a t t h e l i b r a r y c o m p r i s e s t h r e e m o d u l e s , w i n d o w s , i m a g e s

a n d b u t t o n s . T h e i d e a i s t o a d d a f o u r t h m o d u l e , m y l i b , t h a t r e - e x p o r t s t h e p u b l i c p a r t s o f

w i n d o w s , i m a g e s a n d b u t t o n s . T h e i n t e r f a c e m y l i b . m l i c o n t a i n s d e n i t i o n s f o r t h o s e t y p e s t h a t

a r e p u b l i c ( e x p o r t e d w i t h t h e i r d e n i t i o n s ) , d e c l a r a t i o n s f o r t h o s e t y p e s t h a t a r e a b s t r a c t ( e x p o r t e d

w i t h o u t t h e i r d e n i t i o n s ) , a n d d e c l a r a t i o n s f o r t h e f u n c t i o n s t h a t c a n b e c a l l e d f r o m t h e u s e r ' s c o d e :

( * F i l e m y l i b . m l i * )

t y p e ' a o p t i o n = N o n e | S o m e o f ' a ; ; ( * a p u b l i c t y p e * )

t y p e w i n d o w a n d i m a g e a n d b u t t o n ; ; ( * t h r e e a b s t r a c t t y p e s * )

v a l u e n e w _ w i n d o w : i n t - > i n t - > w i n d o w ( * t h e p u b l i c f u n c t i o n s * )

a n d d r a w _ i m a g e : i m a g e - > w i n d o w - > i n t - > i n t - > u n i t

a n d . . .

T h e i m p l e m e n t a t i o n o f t h e m y l i b m o d u l e s i m p l y e q u a t e s t h e a b s t r a c t t y p e s a n d t h e p u b l i c f u n c t i o n s

t o t h e c o r r e s p o n d i n g t y p e s a n d f u n c t i o n s i n t h e m o d u l e s w i n d o w s , i m a g e s a n d b u t t o n s :

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 71/218

C h a p t e r 7 . T h e l i b r a r i a n ( c a m l l i b r ) 6 9

( * F i l e m y l i b . m l * )

t y p e w i n d o w = = w i n d o w s _ _ w i n

a n d i m a g e = = i m a g e s _ _ p i x m a p

a n d b u t t o n = = b u t t o n s _ _ t ; ;

l e t n e w _ w i n d o w = w i n d o w s _ _ o p e n _ w i n d o w

a n d d r a w _ i m a g e = i m a g e s _ _ d r a w

a n d . . .

T h e l e s w i n d o w s . m l , i m a g e s . m l a n d b u t t o n s . m l c a n o p e n t h e m y l i b m o d u l e , t o a c c e s s t h e p u b l i c

t y p e s d e n e d i n t h e i n t e r f a c e m y l i b . m l i , s u c h a s t h e o p t i o n t y p e . O f c o u r s e , t h e s e m o d u l e s m u s t

n o t r e f e r e n c e t h e a b s t r a c t t y p e s n o r t h e p u b l i c f u n c t i o n s , t o a v o i d c i r c u l a r i t i e s .

T y p e s s u c h a s w i n d o w s _ _ w i n i n t h e e x a m p l e a b o v e c a n b e e x p o r t e d b y t h e w i n d o w s m o d u l e

e i t h e r a b s t r a c t l y o r c o n c r e t e l y ( w i t h t h e i r d e n i t i o n ) . O f t e n , i t i s n e c e s s a r y t o e x p o r t t h e m c o n -

c r e t e l y , b e c a u s e t h e o t h e r m o d u l e s i n t h e l i b r a r y ( i m a g e s , b u t t o n s ) n e e d t o b u i l d o r d e s t r u c t u r e

d i r e c t l y v a l u e s o f t h a t t y p e . E v e n i f w i n d o w s _ _ w i n i s e x p o r t e d c o n c r e t e l y b y t h e w i n d o w s m o d u l e ,

t h a t t y p e w i l l r e m a i n a b s t r a c t t o t h e l i b r a r y u s e r , s i n c e i t i s a b s t r a c t e d b y t h e p u b l i c i n t e r f a c e

m y l i b .

T h e a c t u a l b u i l d i n g o f t h e l i b r a r y m y l i b p r o c e e d s a s f o l l o w s :

c a m l c - c m y l i b . m l i # c r e a t e m y l i b . z i

c a m l c - c w i n d o w s . m l i w i n d o w s . m l i m a g e s . m l i i m a g e s . m l

c a m l c - c b u t t o n s . m l i b u t t o n s . m l

c a m l c - c m y l i b . m l # c r e a t e m y l i b . z o

m v m y l i b . z o t m p l i b . z o # r e n a m i n g t o a v o i d o v e r w r i t i n g m y l i b . z o

c a m l l i b r - o m y l i b . z o w i n d o w s . z o i m a g e s . z o b u t t o n s . z o t m p l i b . z o

T h e n , c o p y m y l i b . z i a n d m y l i b . z o t o a p l a c e a c c e s s i b l e t o t h e l i b r a r y u s e r s . T h e o t h e r . z i a n d

. z o l e s n e e d n o t b e c o p i e d .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 72/218

7 0

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 73/218

C h a p t e r 8

L e x e r a n d p a r s e r g e n e r a t o r s

( c a m l l e x , c a m l y a c c )

T h i s c h a p t e r d e s c r i b e s t w o p r o g r a m g e n e r a t o r s : c a m l l e x , t h a t p r o d u c e s a l e x i c a l a n a l y z e r f r o m a

s e t o f r e g u l a r e x p r e s s i o n s w i t h a s s o c i a t e d s e m a n t i c a c t i o n s , a n d c a m l y a c c , t h a t p r o d u c e s a p a r s e r

f r o m a g r a m m a r w i t h a s s o c i a t e d s e m a n t i c a c t i o n s .

T h e s e p r o g r a m g e n e r a t o r s a r e v e r y c l o s e t o t h e w e l l - k n o w n l e x a n d y a c c c o m m a n d s t h a t c a n

b e f o u n d i n m o s t C p r o g r a m m i n g e n v i r o n m e n t s . T h i s c h a p t e r a s s u m e s a w o r k i n g k n o w l e d g e o f l e x

a n d y a c c : w h i l e i t d e s c r i b e s t h e i n p u t s y n t a x f o r c a m l l e x a n d c a m l y a c c a n d t h e m a i n d i e r e n c e s

w i t h l e x a n d y a c c , i t d o e s n o t e x p l a i n t h e b a s i c s o f w r i t i n g a l e x e r o r p a r s e r d e s c r i p t i o n i n l e x a n d

y a c c . R e a d e r s u n f a m i l i a r w i t h l e x a n d y a c c a r e r e f e r r e d t o \ C o m p i l e r s : p r i n c i p l e s , t e c h n i q u e s ,

a n d t o o l s " b y A h o , S e t h i a n d U l l m a n ( A d d i s o n - W e s l e y , 1 9 8 6 ) , \ C o m p i l e r d e s i g n i n C " b y H o l u b

( P r e n t i c e - H a l l , 1 9 9 0 ) , o r \ L e x & Y a c c " , b y M a s o n a n d B r o w n ( O ' R e i l l y , 1 9 9 0 ) .

S t r e a m s a n d s t r e a m m a t c h i n g , a s d e s c r i b e d i n s e c t i o n 3 . 1 , p r o v i d e a n a l t e r n a t i v e w a y t o w r i t e

l e x e r s a n d p a r s e r s . T h e s t r e a m m a t c h i n g t e c h n i q u e i s m o r e p o w e r f u l t h a n t h e c o m b i n a t i o n o f

c a m l l e x a n d c a m l y a c c i n s o m e c a s e s ( h i g h e r - o r d e r p a r s e r s ) , b u t l e s s p o w e r f u l i n o t h e r c a s e s ( p r e c e -

d e n c e s ) . C h o o s e w h i c h e v e r a p p r o a c h i s m o r e a d a p t e d t o y o u r p a r s i n g p r o b l e m .

M a c : T h e s e c o m m a n d s a r e M P W t o o l , n o t s t a n d a l o n e M a c i n t o s h a p p l i c a t i o n s .

8 . 1 O v e r v i e w o f c a m l l e x

T h e c a m l l e x c o m m a n d p r o d u c e s a l e x i c a l a n a l y z e r f r o m a s e t o f r e g u l a r e x p r e s s i o n s w i t h a t t a c h e d

s e m a n t i c a c t i o n s , i n t h e s t y l e o f l e x . A s s u m i n g t h e i n p u t l e i s l e x e r : m l l , e x e c u t i n g

c a m l l e x l e x e r . m l l

p r o d u c e s C a m l L i g h t c o d e f o r a l e x i c a l a n a l y z e r i n l e l e x e r . m l . T h i s l e d e n e s o n e l e x i n g f u n c -

t i o n p e r e n t r y p o i n t i n t h e l e x e r d e n i t i o n . T h e s e f u n c t i o n s h a v e t h e s a m e n a m e s a s t h e e n t r y

p o i n t s . L e x i n g f u n c t i o n s t a k e a s a r g u m e n t a l e x e r b u e r , a n d r e t u r n t h e s e m a n t i c a t t r i b u t e o f t h e

c o r r e s p o n d i n g e n t r y p o i n t .

L e x e r b u e r s a r e a n a b s t r a c t d a t a t y p e i m p l e m e n t e d i n t h e s t a n d a r d l i b r a r y m o d u l e l e x i n g .

T h e f u n c t i o n s c r e a t e _ l e x e r _ c h a n n e l , c r e a t e _ l e x e r _ s t r i n g a n d c r e a t e _ l e x e r f r o m m o d u l e

7 1

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 74/218

7 2

l e x i n g c r e a t e l e x e r b u e r s t h a t r e a d f r o m a n i n p u t c h a n n e l , a c h a r a c t e r s t r i n g , o r a n y r e a d i n g

f u n c t i o n , r e s p e c t i v e l y . ( S e e t h e d e s c r i p t i o n o f m o d u l e l e x i n g i n c h a p t e r 1 3 . )

W h e n u s e d i n c o n j u n c t i o n w i t h a p a r s e r g e n e r a t e d b y c a m l y a c c , t h e s e m a n t i c a c t i o n s c o m p u t e

a v a l u e b e l o n g i n g t o t h e t y p e t o k e n d e n e d b y t h e g e n e r a t e d p a r s i n g m o d u l e . ( S e e t h e d e s c r i p t i o n

o f c a m l y a c c b e l o w . )

8 . 2 S y n t a x o f l e x e r d e n i t i o n s

T h e f o r m a t o f l e x e r d e n i t i o n s i s a s f o l l o w s :

f h e a d e r g

r u l e e n t r y p o i n t =

p a r s e r e g e x p f a c t i o n g

| : : :

| r e g e x p f a c t i o n g

a n d e n t r y p o i n t =

p a r s e : : :

a n d : : :

; ;

C o m m e n t s a r e d e l i m i t e d b y ( * a n d * ) , a s i n C a m l L i g h t .

8 . 2 . 1 H e a d e r

T h e h e a d e r s e c t i o n i s a r b i t r a r y C a m l L i g h t t e x t e n c l o s e d i n c u r l y b r a c e s . I t c a n b e o m i t t e d . I f i t i s

p r e s e n t , t h e e n c l o s e d t e x t i s c o p i e d a s i s a t t h e b e g i n n i n g o f t h e o u t p u t l e . T y p i c a l l y , t h e h e a d e r

s e c t i o n c o n t a i n s t h e # o p e n d i r e c t i v e s r e q u i r e d b y t h e a c t i o n s , a n d p o s s i b l y s o m e a u x i l i a r y f u n c t i o n s

u s e d i n t h e a c t i o n s .

8 . 2 . 2 E n t r y p o i n t s

T h e n a m e s o f t h e e n t r y p o i n t s m u s t b e v a l i d C a m l L i g h t i d e n t i e r s .

8 . 2 . 3 R e g u l a r e x p r e s s i o n s

T h e r e g u l a r e x p r e s s i o n s a r e i n t h e s t y l e o f l e x , w i t h a m o r e C a m l - l i k e s y n t a x .

` c h a r `

A c h a r a c t e r c o n s t a n t , w i t h t h e s a m e s y n t a x a s C a m l L i g h t c h a r a c t e r c o n s t a n t s . M a t c h t h e

d e n o t e d c h a r a c t e r .

_ M a t c h a n y c h a r a c t e r .

e o f M a t c h t h e e n d o f t h e l e x e r i n p u t .

" s t r i n g "

A s t r i n g c o n s t a n t , w i t h t h e s a m e s y n t a x a s C a m l L i g h t s t r i n g c o n s t a n t s . M a t c h t h e c o r r e -

s p o n d i n g s e q u e n c e o f c h a r a c t e r s .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 75/218

C h a p t e r 8 . L e x e r a n d p a r s e r g e n e r a t o r s ( c a m l l e x , c a m l y a c c ) 7 3

c h a r a c t e r - s e t ]

M a t c h a n y s i n g l e c h a r a c t e r b e l o n g i n g t o t h e g i v e n c h a r a c t e r s e t . V a l i d c h a r a c t e r s e t s a r e :

s i n g l e c h a r a c t e r c o n s t a n t s ` c ` ; r a n g e s o f c h a r a c t e r s ` c

1

` - ` c

2

` ( a l l c h a r a c t e r s b e t w e e n

c

1

a n d c

2

, i n c l u s i v e ) ; a n d t h e u n i o n o f t w o o r m o r e c h a r a c t e r s e t s , d e n o t e d b y c o n c a t e n a t i o n .

^ c h a r a c t e r - s e t ]

M a t c h a n y s i n g l e c h a r a c t e r n o t b e l o n g i n g t o t h e g i v e n c h a r a c t e r s e t .

r e g e x p *

( R e p e t i t i o n . ) M a t c h t h e c o n c a t e n a t i o n o f z e r o o r m o r e s t r i n g s t h a t m a t c h r e g e x p .

r e g e x p +

( S t r i c t r e p e t i t i o n . ) M a t c h t h e c o n c a t e n a t i o n o f o n e o r m o r e s t r i n g s t h a t m a t c h r e g e x p .

r e g e x p ?

( O p t i o n . ) M a t c h e i t h e r t h e e m p t y s t r i n g , o r a s t r i n g m a t c h i n g r e g e x p .

r e g e x p

1

| r e g e x p

2

( A l t e r n a t i v e . ) M a t c h a n y s t r i n g t h a t m a t c h e s e i t h e r r e g e x p

1

o r r e g e x p

2

r e g e x p

1

r e g e x p

2

( C o n c a t e n a t i o n . ) M a t c h t h e c o n c a t e n a t i o n o f t w o s t r i n g s , t h e r s t m a t c h i n g r e g e x p

1

, t h e

s e c o n d m a t c h i n g r e g e x p

2

.

( r e g e x p )

M a t c h t h e s a m e s t r i n g s a s r e g e x p .

C o n c e r n i n g t h e p r e c e d e n c e s o f o p e r a t o r s , * a n d + h a v e h i g h e s t p r e c e d e n c e , f o l l o w e d b y ? , t h e n

c o n c a t e n a t i o n , t h e n | ( a l t e r n a t i o n ) .

8 . 2 . 4 A c t i o n s

T h e a c t i o n s a r e a r b i t r a r y C a m l L i g h t e x p r e s s i o n s . T h e y a r e e v a l u a t e d i n a c o n t e x t w h e r e t h e

i d e n t i e r l e x b u f i s b o u n d t o t h e c u r r e n t l e x e r b u e r . S o m e t y p i c a l u s e s f o r l e x b u f , i n c o n j u n c t i o n

w i t h t h e o p e r a t i o n s o n l e x e r b u e r s p r o v i d e d b y t h e l e x i n g s t a n d a r d l i b r a r y m o d u l e , a r e l i s t e d

b e l o w .

l e x i n g _ _ g e t _ l e x e m e l e x b u f

R e t u r n t h e m a t c h e d s t r i n g .

l e x i n g _ _ g e t _ l e x e m e _ c h a r l e x b u f n

R e t u r n t h e n

t h

c h a r a c t e r i n t h e m a t c h e d s t r i n g . T h e r s t c h a r a c t e r c o r r e s p o n d s t o n = 0 .

l e x i n g _ _ g e t _ l e x e m e _ s t a r t l e x b u f

R e t u r n t h e a b s o l u t e p o s i t i o n i n t h e i n p u t t e x t o f t h e b e g i n n i n g o f t h e m a t c h e d s t r i n g . T h e

r s t c h a r a c t e r r e a d f r o m t h e i n p u t t e x t h a s p o s i t i o n 0 .

l e x i n g _ _ g e t _ l e x e m e _ e n d l e x b u f

R e t u r n t h e a b s o l u t e p o s i t i o n i n t h e i n p u t t e x t o f t h e e n d o f t h e m a t c h e d s t r i n g . T h e r s t

c h a r a c t e r r e a d f r o m t h e i n p u t t e x t h a s p o s i t i o n 0 .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 76/218

7 4

e n t r y p o i n t l e x b u f

( W h e r e e n t r y p o i n t i s t h e n a m e o f a n o t h e r e n t r y p o i n t i n t h e s a m e l e x e r d e n i t i o n . ) R e c u r s i v e l y

c a l l t h e l e x e r o n t h e g i v e n e n t r y p o i n t . U s e f u l f o r l e x i n g n e s t e d c o m m e n t s , f o r e x a m p l e .

8 . 3 O v e r v i e w o f c a m l y a c c

T h e c a m l y a c c c o m m a n d p r o d u c e s a p a r s e r f r o m a c o n t e x t - f r e e g r a m m a r s p e c i c a t i o n w i t h a t t a c h e d

s e m a n t i c a c t i o n s , i n t h e s t y l e o f y a c c . A s s u m i n g t h e i n p u t l e i s g r a m m a r : m l y , e x e c u t i n g

c a m l y a c c o p t i o n s g r a m m a r . m l y

p r o d u c e s C a m l L i g h t c o d e f o r a p a r s e r i n t h e l e g r a m m a r . m l , a n d i t s i n t e r f a c e i n l e g r a m m a r . m l i .

T h e g e n e r a t e d m o d u l e d e n e s o n e p a r s i n g f u n c t i o n p e r e n t r y p o i n t i n t h e g r a m m a r . T h e s e

f u n c t i o n s h a v e t h e s a m e n a m e s a s t h e e n t r y p o i n t s . P a r s i n g f u n c t i o n s t a k e a s a r g u m e n t s a l e x i c a l

a n a l y z e r ( a f u n c t i o n f r o m l e x e r b u e r s t o t o k e n s ) a n d a l e x e r b u e r , a n d r e t u r n t h e s e m a n t i c

a t t r i b u t e o f t h e c o r r e s p o n d i n g e n t r y p o i n t . L e x i c a l a n a l y z e r f u n c t i o n s a r e u s u a l l y g e n e r a t e d f r o m a

l e x e r s p e c i c a t i o n b y t h e c a m l l e x p r o g r a m . L e x e r b u e r s a r e a n a b s t r a c t d a t a t y p e i m p l e m e n t e d

i n t h e s t a n d a r d l i b r a r y m o d u l e l e x i n g . T o k e n s a r e v a l u e s f r o m t h e c o n c r e t e t y p e t o k e n , d e n e d

i n t h e i n t e r f a c e l e g r a m m a r . m l i p r o d u c e d b y c a m l y a c c .

8 . 4 S y n t a x o f g r a m m a r d e n i t i o n s

G r a m m a r d e n i t i o n s h a v e t h e f o l l o w i n g f o r m a t :

% f

h e a d e r

% g

d e c l a r a t i o n s

% %

r u l e s

% %

t r a i l e r

C o m m e n t s a r e e n c l o s e d b e t w e e n / * a n d * / ( a s i n C ) i n t h e \ d e c l a r a t i o n s " a n d \ r u l e s " s e c t i o n s ,

a n d b e t w e e n ( * a n d * ) ( a s i n C a m l ) i n t h e \ h e a d e r " a n d \ t r a i l e r " s e c t i o n s .

8 . 4 . 1 H e a d e r a n d t r a i l e r

T h e h e a d e r a n d t h e t r a i l e r s e c t i o n s a r e C a m l L i g h t c o d e t h a t i s c o p i e d a s i s i n t o l e g r a m m a r . m l .

B o t h s e c t i o n s a r e o p t i o n a l . T h e h e a d e r g o e s a t t h e b e g i n n i n g o f t h e o u t p u t l e ; i t u s u a l l y c o n t a i n s

# o p e n d i r e c t i v e s r e q u i r e d b y t h e s e m a n t i c a c t i o n s o f t h e r u l e s . T h e t r a i l e r g o e s a t t h e e n d o f t h e

o u t p u t l e .

8 . 4 . 2 D e c l a r a t i o n s

D e c l a r a t i o n s a r e g i v e n o n e p e r l i n e . T h e y a l l s t a r t w i t h a % s i g n .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 77/218

C h a p t e r 8 . L e x e r a n d p a r s e r g e n e r a t o r s ( c a m l l e x , c a m l y a c c ) 7 5

% t o k e n s y m b o l : : : s y m b o l

D e c l a r e t h e g i v e n s y m b o l s a s t o k e n s ( t e r m i n a l s y m b o l s ) . T h e s e s y m b o l s a r e a d d e d a s c o n s t a n t

c o n s t r u c t o r s f o r t h e t o k e n c o n c r e t e t y p e .

% t o k e n < t y p e > s y m b o l : : : s y m b o l

D e c l a r e t h e g i v e n s y m b o l s a s t o k e n s w i t h a n a t t a c h e d a t t r i b u t e o f t h e g i v e n t y p e . T h e s e s y m -

b o l s a r e a d d e d a s c o n s t r u c t o r s w i t h a r g u m e n t s o f t h e g i v e n t y p e f o r t h e t o k e n c o n c r e t e t y p e .

T h e t y p e p a r t i s a n a r b i t r a r y C a m l L i g h t t y p e e x p r e s s i o n , e x c e p t t h a t a l l t y p e c o n s t r u c t o r

n a m e s m u s t b e f u l l y q u a l i e d ( e . g . m o d n a m e _ _ t y p e n a m e ) f o r a l l t y p e s e x c e p t s t a n d a r d b u i l t - i n

t y p e s , e v e n i f t h e p r o p e r # o p e n d i r e c t i v e s ( e . g . # o p e n " m o d n a m e " ) w e r e g i v e n i n t h e h e a d e r

s e c t i o n . T h a t ' s b e c a u s e t h e h e a d e r i s c o p i e d o n l y t o t h e . m l o u t p u t l e , b u t n o t t o t h e . m l i

o u t p u t l e , w h i l e t h e t y p e p a r t o f a % t o k e n d e c l a r a t i o n i s c o p i e d t o b o t h .

% s t a r t s y m b o l : : : s y m b o l

D e c l a r e t h e g i v e n s y m b o l s a s e n t r y p o i n t s f o r t h e g r a m m a r . F o r e a c h e n t r y p o i n t , a p a r s i n g

f u n c t i o n w i t h t h e s a m e n a m e i s d e n e d i n t h e o u t p u t m o d u l e . N o n - t e r m i n a l s t h a t a r e n o t

d e c l a r e d a s e n t r y p o i n t s h a v e n o s u c h p a r s i n g f u n c t i o n . S t a r t s y m b o l s m u s t b e g i v e n a t y p e

w i t h t h e % t y p e d i r e c t i v e b e l o w .

% t y p e < t y p e > s y m b o l : : : s y m b o l

S p e c i f y t h e t y p e o f t h e s e m a n t i c a t t r i b u t e s f o r t h e g i v e n s y m b o l s . T h i s i s m a n d a t o r y f o r s t a r t

s y m b o l s o n l y . O t h e r n o n t e r m i n a l s y m b o l s n e e d n o t b e g i v e n t y p e s b y h a n d : t h e s e t y p e s w i l l

b e i n f e r r e d w h e n r u n n i n g t h e o u t p u t l e s t h r o u g h t h e C a m l L i g h t c o m p i l e r ( u n l e s s t h e - s

o p t i o n i s i n e e c t ) . T h e t y p e p a r t i s a n a r b i t r a r y C a m l L i g h t t y p e e x p r e s s i o n , e x c e p t t h a t

a l l t y p e c o n s t r u c t o r n a m e s m u s t b e f u l l y q u a l i e d ( e . g . m o d n a m e _ _ t y p e n a m e ) f o r a l l t y p e s

e x c e p t s t a n d a r d b u i l t - i n t y p e s , e v e n i f t h e p r o p e r # o p e n d i r e c t i v e s ( e . g . # o p e n " m o d n a m e " )

w e r e g i v e n i n t h e h e a d e r s e c t i o n . T h a t ' s b e c a u s e t h e h e a d e r i s c o p i e d o n l y t o t h e . m l o u t p u t

l e , b u t n o t t o t h e . m l i o u t p u t l e , w h i l e t h e t y p e p a r t o f a % t o k e n d e c l a r a t i o n i s c o p i e d t o

b o t h .

% l e f t s y m b o l : : : s y m b o l

% r i g h t s y m b o l : : : s y m b o l

% n o n a s s o c s y m b o l : : : s y m b o l

A s s o c i a t e p r e c e d e n c e s a n d a s s o c i a t i v i t i e s t o t h e g i v e n s y m b o l s . A l l s y m b o l s o n t h e s a m e l i n e

a r e g i v e n t h e s a m e p r e c e d e n c e . T h e y h a v e h i g h e r p r e c e d e n c e t h a n s y m b o l s d e c l a r e d b e f o r e

i n a % l e f t , % r i g h t o r % n o n a s s o c l i n e . T h e y h a v e l o w e r p r e c e d e n c e t h a n s y m b o l s d e c l a r e d

a f t e r i n a % l e f t , % r i g h t o r % n o n a s s o c l i n e . T h e s y m b o l s a r e d e c l a r e d t o a s s o c i a t e t o t h e

l e f t ( % l e f t ) , t o t h e r i g h t ( % r i g h t ) , o r t o b e n o n - a s s o c i a t i v e ( % n o n a s s o c ) . T h e s y m b o l s a r e

u s u a l l y t o k e n s . T h e y c a n a l s o b e d u m m y n o n t e r m i n a l s , f o r u s e w i t h t h e % p r e c d i r e c t i v e i n s i d e

t h e r u l e s .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 78/218

7 6

8 . 4 . 3 R u l e s

T h e s y n t a x f o r r u l e s i s a s u s u a l :

n o n t e r m i n a l :

s y m b o l : : : s y m b o l f s e m a n t i c - a c t i o n g

| : : :

| s y m b o l : : : s y m b o l f s e m a n t i c - a c t i o n g

;

R u l e s c a n a l s o c o n t a i n t h e % p r e c s y m b o l d i r e c t i v e i n t h e r i g h t - h a n d s i d e p a r t , t o o v e r r i d e t h e

d e f a u l t p r e c e d e n c e a n d a s s o c i a t i v i t y o f t h e r u l e w i t h t h e p r e c e d e n c e a n d a s s o c i a t i v i t y o f t h e g i v e n

s y m b o l .

S e m a n t i c a c t i o n s a r e a r b i t r a r y C a m l L i g h t e x p r e s s i o n s , t h a t a r e e v a l u a t e d t o p r o d u c e t h e s e -

m a n t i c a t t r i b u t e a t t a c h e d t o t h e d e n e d n o n t e r m i n a l . T h e s e m a n t i c a c t i o n s c a n a c c e s s t h e s e m a n t i c

a t t r i b u t e s o f t h e s y m b o l s i n t h e r i g h t - h a n d s i d e o f t h e r u l e w i t h t h e $ n o t a t i o n : $ 1 i s t h e a t t r i b u t e

f o r t h e r s t ( l e f t m o s t ) s y m b o l , $ 2 i s t h e a t t r i b u t e f o r t h e s e c o n d s y m b o l , e t c .

A c t i o n s o c c u r r i n g i n t h e m i d d l e o f r u l e s a r e n o t s u p p o r t e d . E r r o r r e c o v e r y i s n o t i m p l e m e n t e d .

8 . 5 O p t i o n s

T h e c a m l y a c c c o m m a n d r e c o g n i z e s t h e f o l l o w i n g o p t i o n s :

- v G e n e r a t e a d e s c r i p t i o n o f t h e p a r s i n g t a b l e s a n d a r e p o r t o n c o n i c t s r e s u l t i n g f r o m a m b i g u -

i t i e s i n t h e g r a m m a r . T h e d e s c r i p t i o n i s p u t i n l e g r a m m a r . o u t p u t .

- s G e n e r a t e a g r a m m a r . m l l e w i t h s m a l l e r p h r a s e s . S e m a n t i c a c t i o n s a r e p r e s e n t e d i n t h e

g r a m m a r . m l o u t p u t l e a s o n e l a r g e v e c t o r o f f u n c t i o n s . B y d e f a u l t , t h i s v e c t o r i s b u i l t b y

a s i n g l e p h r a s e . W h e n t h e g r a m m a r i s l a r g e , o r c o n t a i n s c o m p l i c a t e d s e m a n t i c a c t i o n s , t h e

r e s u l t i n g p h r a s e m a y r e q u i r e l a r g e a m o u n t s o f m e m o r y t o b e c o m p i l e d b y C a m l L i g h t . W i t h

t h e - s o p t i o n , t h e v e c t o r o f a c t i o n s i s c o n s t r u c t e d i n c r e m e n t a l l y , o n e p h r a s e p e r a c t i o n . T h i s

l o w e r s t h e m e m o r y r e q u i r e m e n t s f o r t h e c o m p i l e r , b u t i t i s n o l o n g e r p o s s i b l e t o i n f e r t h e

t y p e s o f n o n t e r m i n a l s y m b o l s : t y p e c h e c k i n g i s t u r n e d o o n s y m b o l s t h a t d o n o t h a v e a t y p e

s p e c i e d b y a % t y p e d i r e c t i v e .

- b p r e x

N a m e t h e o u t p u t l e s p r e x . m l , p r e x . m l i , p r e x . o u t p u t , i n s t e a d o f t h e d e f a u l t n a m i n g

c o n v e n t i o n .

8 . 6 A c o m p l e t e e x a m p l e

T h e a l l - t i m e f a v o r i t e : a d e s k c a l c u l a t o r . T h i s p r o g r a m r e a d s a r i t h m e t i c e x p r e s s i o n s o n s t a n d a r d

i n p u t , o n e p e r l i n e , a n d p r i n t s t h e i r v a l u e s . H e r e i s t h e g r a m m a r d e n i t i o n :

/ * F i l e p a r s e r . m l y * /

% t o k e n < i n t > I N T

% t o k e n P L U S M I N U S T I M E S D I V

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 79/218

C h a p t e r 8 . L e x e r a n d p a r s e r g e n e r a t o r s ( c a m l l e x , c a m l y a c c ) 7 7

% t o k e n L P A R E N R P A R E N

% t o k e n E O L

% l e f t P L U S M I N U S / * l o w e s t p r e c e d e n c e * /

% l e f t T I M E S D I V / * m e d i u m p r e c e d e n c e * /

% n o n a s s o c U M I N U S / * h i g h e s t p r e c e d e n c e * /

% s t a r t M a i n / * t h e e n t r y p o i n t * /

% t y p e < i n t > M a i n

% %

M a i n :

E x p r E O L { $ 1 }

;

E x p r :

I N T { $ 1 }

| L P A R E N E x p r R P A R E N { $ 2 }

| E x p r P L U S E x p r { $ 1 + $ 3 }

| E x p r M I N U S E x p r { $ 1 - $ 3 }

| E x p r T I M E S E x p r { $ 1 * $ 3 }

| E x p r D I V E x p r { $ 1 / $ 3 }

| M I N U S E x p r % p r e c U M I N U S { - $ 2 }

;

H e r e i s t h e d e n i t i o n f o r t h e c o r r e s p o n d i n g l e x e r :

( * F i l e l e x e r . m l l * )

{

# o p e n " p a r s e r " ; ; ( * T h e t y p e t o k e n i s d e f i n e d i n p a r s e r . m l i * )

e x c e p t i o n E o f ; ;

}

r u l e T o k e n = p a r s e

` ` ` \ t ` ] { T o k e n l e x b u f } ( * s k i p b l a n k s * )

| ` \ n ` ] { E O L }

| ` 0 ` - ` 9 ` ] + { I N T ( i n t _ o f _ s t r i n g ( g e t _ l e x e m e l e x b u f ) ) }

| ` + ` { P L U S }

| ` - ` { M I N U S }

| ` * ` { T I M E S }

| ` / ` { D I V }

| ` ( ` { L P A R E N }

| ` ) ` { R P A R E N }

| e o f { r a i s e E o f }

; ;

H e r e i s t h e m a i n p r o g r a m , t h a t c o m b i n e s t h e p a r s e r w i t h t h e l e x e r :

( * F i l e c a l c . m l * )

t r y

l e t l e x b u f = l e x i n g _ _ c r e a t e _ l e x e r _ c h a n n e l s t d _ i n i n

w h i l e t r u e d o

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 80/218

7 8

l e t r e s u l t = p a r s e r _ _ M a i n l e x e r _ _ T o k e n l e x b u f i n

p r i n t _ i n t r e s u l t ; p r i n t _ n e w l i n e ( ) ; f l u s h s t d _ o u t

d o n e

w i t h E o f - >

( )

; ;

T o c o m p i l e e v e r y t h i n g , e x e c u t e :

c a m l l e x l e x e r . m l l # g e n e r a t e s l e x e r . m l

c a m l y a c c p a r s e r . m l y # g e n e r a t e s p a r s e r . m l a n d p a r s e r . m l i

c a m l c - c p a r s e r . m l i

c a m l c - c l e x e r . m l

c a m l c - c p a r s e r . m l

c a m l c - c c a l c . m l

c a m l c - o c a l c l e x e r . z o p a r s e r . z o c a l c . z o

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 81/218

C h a p t e r 9

T h e d e b u g g e r ( c a m l d e b u g )

T h i s c h a p t e r d e s c r i b e s t h e C a m l L i g h t s o u r c e - l e v e l r e p l a y d e b u g g e r c a m l d e b u g .

U n i x : T h e d e b u g g e r r e s i d e s i n t h e d i r e c t o r y c o n t r i b / d e b u g g e r i n t h e d i s t r i b u t i o n . I t r e q u i r e s a

U n i x s y s t e m t h a t p r o v i d e s B S D s o c k e t s .

M a c : T h e d e b u g g e r i s n o t a v a i l a b l e .

P C : T h e d e b u g g e r i s n o t a v a i l a b l e .

9 . 1 C o m p i l i n g f o r d e b u g g i n g

B e f o r e t h e d e b u g g e r c a n b e u s e d , t h e p r o g r a m m u s t b e c o m p i l e d a n d l i n k e d w i t h t h e - g o p t i o n : a l l

. z o l e s t h a t a r e p a r t o f t h e p r o g r a m s h o u l d h a v e b e e n c r e a t e d w i t h c a m l c - g , a n d t h e y m u s t b e

l i n k e d t o g e t h e r w i t h c a m l c - g .

C o m p i l i n g w i t h - g e n t a i l s n o p e n a l t y o n t h e r u n n i n g t i m e o f p r o g r a m s : . z o l e s a n d b y t e c o d e

e x e c u t a b l e l e s a r e b i g g e r a n d t a k e s l i g h t l y l o n g e r t o p r o d u c e , b u t t h e e x e c u t a b l e l e s r u n a t e x a c t l y

t h e s a m e s p e e d a s i f t h e y h a d b e e n c o m p i l e d w i t h o u t - g . I t i s t h e r e f o r e p e r f e c t l y a c c e p t a b l e t o

c o m p i l e a l w a y s i n - g m o d e .

9 . 2 I n v o c a t i o n

9 . 2 . 1 S t a r t i n g t h e d e b u g g e r

T h e C a m l L i g h t d e b u g g e r i s i n v o k e d b y r u n n i n g t h e p r o g r a m c a m l d e b u g w i t h t h e n a m e o f t h e

b y t e c o d e e x e c u t a b l e l e a s a r g u m e n t :

c a m l d e b u g p r o g r a m

T h e f o l l o w i n g c o m m a n d - l i n e o p t i o n s a r e r e c o g n i z e d :

- s t d l i b d i r e c t o r y

L o o k f o r t h e s t a n d a r d l i b r a r y l e s i n d i r e c t o r y i n s t e a d o f i n t h e d e f a u l t d i r e c t o r y .

7 9

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 82/218

8 0

- s s o c k e t

U s e s o c k e t f o r c o m m u n i c a t i n g w i t h t h e d e b u g g e d p r o g r a m . S e e t h e d e s c r i p t i o n o f t h e c o m -

m a n d s e t s o c k e t ( s e c t i o n 9 . 8 . 7 ) f o r t h e f o r m a t o f s o c k e t .

- c c o u n t

S e t t h e m a x i m u m n u m b e r o f c h e c k p o i n t s t o c o u n t .

- c d d i r e c t o r y

R u n t h e d e b u g g e r p r o g r a m f r o m t h e w o r k i n g d i r e c t o r y d i r e c t o r y , i n s t e a d o f t h e c u r r e n t d i -

r e c t o r y .

- e m a c s

T e l l t h e d e b u g g e r i t i s e x e c u t i n g u n d e r E m a c s . ( S e e s e c t i o n 1 1 . 4 f o r i n f o r m a t i o n o n h o w t o

r u n t h e d e b u g g e r u n d e r E m a c s . )

9 . 2 . 2 Q u i t t i n g t h e d e b u g g e r

T h e c o m m a n d q u i t e x i t s t h e d e b u g g e r . Y o u c a n a l s o e x i t t h e d e b u g g e r b y t y p i n g a n e n d - o f - l e

c h a r a c t e r ( u s u a l l y c t r l - D ) .

T y p i n g a n i n t e r r u p t c h a r a c t e r ( u s u a l l y c t r l - C ) w i l l n o t e x i t t h e d e b u g g e r , b u t w i l l t e r m i n a t e

t h e a c t i o n o f a n y d e b u g g e r c o m m a n d t h a t i s i n p r o g r e s s a n d r e t u r n t o t h e d e b u g g e r c o m m a n d l e v e l .

9 . 3 C o m m a n d s

A d e b u g g e r c o m m a n d i s a s i n g l e l i n e o f i n p u t . I t s t a r t s w i t h a c o m m a n d n a m e , w h i c h i s f o l l o w e d

b y a r g u m e n t s d e p e n d i n g o n t h i s n a m e . E x a m p l e s :

r u n

g o t o 1 0 0 0

s e t a r g u m e n t s a r g 1 a r g 2

A c o m m a n d n a m e c a n b e t r u n c a t e d a s l o n g a s t h e r e i s n o a m b i g u i t y . F o r i n s t a n c e , g o 1 0 0 0

i s u n d e r s t o o d a s g o t o 1 0 0 0 , s i n c e t h e r e a r e n o o t h e r c o m m a n d s w h o s e n a m e s t a r t s w i t h g o . F o r

t h e m o s t f r e q u e n t l y u s e d c o m m a n d s , a m b i g u o u s a b b r e v i a t i o n s a r e a l l o w e d . F o r i n s t a n c e , r s t a n d s

f o r r u n e v e n t h o u g h t h e r e a r e o t h e r s c o m m a n d s s t a r t i n g w i t h r . Y o u c a n t e s t t h e v a l i d i t y o f a n

a b b r e v i a t i o n u s i n g t h e h e l p c o m m a n d .

I f t h e p r e v i o u s c o m m a n d h a s b e e n s u c c e s s f u l , a b l a n k l i n e ( t y p i n g j u s t R E T ) w i l l r e p e a t i t .

9 . 3 . 1 G e t t i n g h e l p

T h e C a m l L i g h t d e b u g g e r h a s a s i m p l e o n - l i n e h e l p s y s t e m , w h i c h g i v e s a b r i e f d e s c r i p t i o n o f e a c h

c o m m a n d a n d v a r i a b l e .

h e l p

P r i n t t h e l i s t o f c o m m a n d s .

h e l p c o m m a n d

G i v e h e l p a b o u t t h e c o m m a n d c o m m a n d .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 83/218

C h a p t e r 9 . T h e d e b u g g e r ( c a m l d e b u g ) 8 1

h e l p s e t v a r i a b l e , h e l p s h o w v a r i a b l e

G i v e h e l p a b o u t t h e v a r i a b l e v a r i a b l e . T h e l i s t o f a l l d e b u g g e r v a r i a b l e s c a n b e o b t a i n e d w i t h

h e l p s e t .

h e l p i n f o t o p i c

G i v e h e l p a b o u t t o p i c . U s e h e l p i n f o t o g e t a l i s t o f k n o w n t o p i c s .

9 . 3 . 2 A c c e s s i n g t h e d e b u g g e r s t a t e

s e t v a r i a b l e v a l u e

S e t t h e d e b u g g e r v a r i a b l e v a r i a b l e t o t h e v a l u e v a l u e .

s h o w v a r i a b l e

P r i n t t h e v a l u e o f t h e d e b u g g e r v a r i a b l e v a r i a b l e .

i n f o s u b j e c t

G i v e i n f o r m a t i o n a b o u t t h e g i v e n s u b j e c t . F o r i n s t a n c e , i n f o b r e a k p o i n t s w i l l p r i n t t h e l i s t

o f a l l b r e a k p o i n t s .

9 . 4 E x e c u t i n g a p r o g r a m

9 . 4 . 1 E v e n t s

E v e n t s a r e \ i n t e r e s t i n g " l o c a t i o n s i n t h e s o u r c e c o d e , c o r r e s p o n d i n g t o t h e b e g i n n i n g o r e n d o f

e v a l u a t i o n o f \ i n t e r e s t i n g " s u b - e x p r e s s i o n s . E v e n t s a r e t h e u n i t o f s i n g l e - s t e p p i n g ( s t e p p i n g g o e s t o

t h e n e x t o r p r e v i o u s e v e n t e n c o u n t e r e d i n t h e p r o g r a m e x e c u t i o n ) . A l s o , b r e a k p o i n t s c a n o n l y b e

s e t a t e v e n t s . T h u s , e v e n t s p l a y t h e r o l e o f l i n e n u m b e r s i n d e b u g g e r s f o r c o n v e n t i o n a l l a n g u a g e s .

D u r i n g p r o g r a m e x e c u t i o n , a c o u n t e r i s i n c r e m e n t e d a t e a c h e v e n t e n c o u n t e r e d . T h e v a l u e o f

t h i s c o u n t e r i s r e f e r r e d a s t h e c u r r e n t t i m e . T h a n k s t o r e v e r s e e x e c u t i o n , i t i s p o s s i b l e t o j u m p

b a c k a n d f o r t h t o a n y t i m e o f t h e e x e c u t i o n .

H e r e i s w h e r e t h e d e b u g g e r e v e n t s ( w r i t t e n . / ) a r e l o c a t e d i n t h e s o u r c e c o d e :

F o l l o w i n g a f u n c t i o n a p p l i c a t i o n :

( f a r g ) . /

A f t e r r e c e i v i n g a n a r g u m e n t t o a f u n c t i o n :

f u n x . / y . / z - > . / . . .

I f a c u r r i e d f u n c t i o n i s d e n e d b y p a t t e r n - m a t c h i n g w i t h s e v e r a l c a s e s , e v e n t s c o r r e s p o n d i n g

t o t h e p a s s i n g o f a r g u m e n t s a r e d i s p l a y e d o n t h e r s t c a s e o f t h e f u n c t i o n , b e c a u s e p a t t e r n -

m a t c h i n g h a s n o t y e t d e t e r m i n e d w h i c h c a s e t o s e l e c t :

f u n p a t 1 . / p a t 2 . / p a t 3 - > . / . . .

| . . .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 84/218

8 2

O n e a c h c a s e o f a p a t t e r n - m a t c h i n g d e n i t i o n ( f u n c t i o n , m a t c h : : : w i t h c o n s t r u c t , t r y : : : w i t h

c o n s t r u c t ) :

f u n c t i o n p a t 1 - > . / e x p r 1

| . . .

| p a t N - > . / e x p r N

B e t w e e n s u b e x p r e s s i o n s o f a s e q u e n c e :

e x p r 1 ; . / e x p r 2 ; . / . . . ; . / e x p r N

I n t h e t w o b r a n c h e s o f a c o n d i t i o n a l e x p r e s s i o n :

i f c o n d t h e n . / e x p r 1 e l s e . / e x p r 2

A t t h e b e g i n n i n g o f e a c h i t e r a t i o n o f a l o o p :

w h i l e c o n d d o . / b o d y d o n e

f o r i = a t o b d o . / b o d y d o n e

E x c e p t i o n s : A f u n c t i o n a p p l i c a t i o n f o l l o w e d b y a f u n c t i o n r e t u r n i s r e p l a c e d b y t h e c o m p i l e r b y

a j u m p ( t a i l - c a l l o p t i m i z a t i o n ) . I n t h i s c a s e , n o e v e n t i s p u t a f t e r t h e f u n c t i o n a p p l i c a t i o n . A l s o ,

n o e v e n t i s p u t a f t e r a f u n c t i o n a p p l i c a t i o n w h e n t h e f u n c t i o n i s a p r i m i t i v e f u n c t i o n ( w r i t t e n i n

C ) . F i n a l l y , s e v e r a l e v e n t s m a y c o r r e s p o n d t o t h e s a m e l o c a t i o n i n t h e c o m p i l e d p r o g r a m . T h e n ,

t h e d e b u g g e r c a n n o t d i s t i n g u i s h t h e m , a n d s e l e c t s o n e o f t h e e v e n t s t o a s s o c i a t e w i t h t h e g i v e n

c o d e l o c a t i o n . T h e e v e n t c h o s e n i s a \ f u n c t i o n a p p l i c a t i o n " e v e n t i f t h e r e i s o n e a t t h a t l o c a t i o n ,

o r o t h e r w i s e t h e e v e n t w h i c h a p p e a r s l a s t i n t h e s o u r c e . T h i s h e u r i s t i c g e n e r a l l y p i c k s t h e \ m o s t

i n t e r e s t i n g " e v e n t a s s o c i a t e d w i t h t h e c o d e l o c a t i o n .

9 . 4 . 2 S t a r t i n g t h e d e b u g g e d p r o g r a m

T h e d e b u g g e r s t a r t s e x e c u t i n g t h e d e b u g g e d p r o g r a m o n l y w h e n n e e d e d . T h i s a l l o w s s e t t i n g b r e a -

p o i n t s o r a s s i g n i n g d e b u g g e r v a r i a b l e s b e f o r e e x e c u t i o n s t a r t s . T h e r e a r e s e v e r a l w a y s t o s t a r t

e x e c u t i o n :

r u n R u n t h e p r o g r a m u n t i l a b r e a k p o i n t i s h i t , o r t h e p r o g r a m t e r m i n a t e s .

s t e p 0

L o a d t h e p r o g r a m a n d s t o p o n t h e r s t e v e n t .

g o t o t i m e

L o a d t h e p r o g r a m a n d e x e c u t e i t u n t i l t h e g i v e n t i m e . U s e f u l w h e n y o u a l r e a d y k n o w a p -

p r o x i m a t e l y a t w h a t t i m e t h e p r o b l e m a p p e a r s . A l s o u s e f u l t o s e t b r e a k p o i n t s o n f u n c t i o n

v a l u e s t h a t h a v e n o t b e e n c o m p u t e d a t t i m e 0 ( s e e s e c t i o n 9 . 5 ) .

T h e e x e c u t i o n o f a p r o g r a m i s a e c t e d b y c e r t a i n i n f o r m a t i o n i t r e c e i v e s w h e n t h e d e b u g g e r

s t a r t s i t , s u c h a s t h e c o m m a n d - l i n e a r g u m e n t s t o t h e p r o g r a m a n d i t s w o r k i n g d i r e c t o r y . T h e

d e b u g g e r p r o v i d e s c o m m a n d s t o s p e c i f y t h i s i n f o r m a t i o n ( s e t a r g u m e n t s a n d c d ) . T h e s e c o m -

m a n d s m u s t b e u s e d b e f o r e p r o g r a m e x e c u t i o n s t a r t s . I f y o u t r y t o c h a n g e t h e a r g u m e n t s o r t h e

w o r k i n g d i r e c t o r y a f t e r s t a r t i n g y o u r p r o g r a m , t h e d e b u g g e r w i l l k i l l t h e p r o g r a m ( a f t e r a s k i n g f o r

c o n r m a t i o n ) .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 85/218

C h a p t e r 9 . T h e d e b u g g e r ( c a m l d e b u g ) 8 3

9 . 4 . 3 R u n n i n g t h e p r o g r a m

T h e f o l l o w i n g c o m m a n d s e x e c u t e t h e p r o g r a m f o r w a r d o r b a c k w a r d , s t a r t i n g a t t h e c u r r e n t t i m e .

T h e e x e c u t i o n w i l l s t o p e i t h e r w h e n s p e c i e d b y t h e c o m m a n d o r w h e n a b r e a k p o i n t i s e n c o u n t e r e d .

r u n E x e c u t e t h e p r o g r a m f o r w a r d f r o m c u r r e n t t i m e . S t o p s a t n e x t b r e a k p o i n t o r w h e n t h e

p r o g r a m t e r m i n a t e s .

r e v e r s e

E x e c u t e t h e p r o g r a m b a c k w a r d f r o m c u r r e n t t i m e . M o s t l y u s e f u l t o g o t o t h e l a s t b r e a k p o i n t

e n c o u n t e r e d b e f o r e t h e c u r r e n t t i m e .

s t e p c o u n t ]

R u n t h e p r o g r a m a n d s t o p a t t h e n e x t e v e n t . W i t h a n a r g u m e n t , d o i t c o u n t t i m e s .

b a c k s t e p c o u n t ]

R u n t h e p r o g r a m b a c k w a r d a n d s t o p a t t h e p r e v i o u s e v e n t . W i t h a n a r g u m e n t , d o i t c o u n t

t i m e s .

n e x t c o u n t ]

R u n t h e p r o g r a m a n d s t o p a t t h e n e x t e v e n t , s k i p p i n g o v e r f u n c t i o n c a l l s . W i t h a n a r g u m e n t ,

d o i t c o u n t t i m e s .

f i n i s h

R u n t h e p r o g r a m u n t i l t h e c u r r e n t f u n c t i o n r e t u r n s .

9 . 4 . 4 T i m e t r a v e l

Y o u c a n j u m p d i r e c t l y t o a g i v e n t i m e , w i t h o u t s t o p p i n g o n b r e a k p o i n t s , u s i n g t h e g o t o c o m m a n d .

A s y o u m o v e t h r o u g h t h e p r o g r a m , t h e d e b u g g e r m a i n t a i n s a n h i s t o r y o f t h e s u c c e s s i v e t i m e s

y o u s t o p a t . T h e l a s t c o m m a n d c a n b e u s e d t o r e v i s i t t h e s e t i m e s : e a c h l a s t c o m m a n d m o v e s o n e

s t e p b a c k t h r o u g h t h e h i s t o r y . T h a t i s u s e f u l m a i n l y t o u n d o c o m m a n d s s u c h a s s t e p a n d n e x t .

g o t o t i m e

J u m p t o t h e g i v e n t i m e .

l a s t c o u n t ]

G o b a c k t o t h e l a t e s t t i m e r e c o r d e d i n t h e e x e c u t i o n h i s t o r y . W i t h a n a r g u m e n t , d o i t c o u n t

t i m e s .

s e t h i s t o r y s i z e

S e t t h e s i z e o f t h e e x e c u t i o n h i s t o r y .

9 . 4 . 5 K i l l i n g t h e p r o g r a m

k i l l

K i l l t h e p r o g r a m b e i n g e x e c u t e d . T h i s c o m m a n d i s m a i n l y u s e f u l i f y o u w i s h t o r e c o m p i l e

t h e p r o g r a m w i t h o u t l e a v i n g t h e d e b u g g e r .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 86/218

8 4

9 . 5 B r e a k p o i n t s

A b r e a k p o i n t c a u s e s t h e p r o g r a m t o s t o p w h e n e v e r a c e r t a i n p o i n t i n t h e p r o g r a m i s r e a c h e d . I t

c a n b e s e t i n s e v e r a l w a y s u s i n g t h e b r e a k c o m m a n d . B r e a k p o i n t s a r e a s s i g n e d n u m b e r s w h e n s e t ,

f o r f u r t h e r r e f e r e n c e .

b r e a k

S e t a b r e a k p o i n t a t t h e c u r r e n t p o s i t i o n i n t h e p r o g r a m e x e c u t i o n . T h e c u r r e n t p o s i t i o n m u s t

b e o n a n e v e n t ( i . e . , n e i t h e r a t t h e b e g i n n i n g , n o r a t t h e e n d o f t h e p r o g r a m ) .

b r e a k f u n c t i o n

S e t a b r e a k p o i n t a t t h e b e g i n n i n g o f f u n c t i o n . T h i s w o r k s o n l y w h e n t h e f u n c t i o n a l v a l u e o f

t h e i d e n t i e r f u n c t i o n h a s b e e n c o m p u t e d a n d a s s i g n e d t o t h e i d e n t i e r . H e n c e t h i s c o m m a n d

c a n n o t b e u s e d a t t h e v e r y b e g i n n i n g o f t h e p r o g r a m e x e c u t i o n , w h e n a l l i d e n t i e r s a r e s t i l l

u n d e n e d . M o r e o v e r , C f u n c t i o n s a r e n o t r e c o g n i z e d b y t h e d e b u g g e r .

b r e a k @ m o d u l e ] l i n e

S e t a b r e a k p o i n t i n m o d u l e m o d u l e ( o r i n t h e c u r r e n t m o d u l e i f m o d u l e i s n o t g i v e n ) , a t t h e

r s t e v e n t o f l i n e l i n e .

b r e a k @ m o d u l e ] l i n e c o l u m n

S e t a b r e a k p o i n t i n m o d u l e m o d u l e ( o r i n t h e c u r r e n t m o d u l e i f m o d u l e i s n o t g i v e n ) , a t t h e

e v e n t c l o s e s t t o l i n e l i n e , c o l u m n c o l u m n .

b r e a k @ m o d u l e ] # c h a r a c t e r

S e t a b r e a k p o i n t i n m o d u l e m o d u l e a t t h e e v e n t c l o s e s t t o c h a r a c t e r n u m b e r c h a r a c t e r .

b r e a k a d d r e s s

S e t a b r e a k p o i n t a t t h e c o d e a d d r e s s a d d r e s s .

d e l e t e b r e a k p o i n t - n u m b e r s ]

D e l e t e t h e s p e c i e d b r e a k p o i n t s . W i t h o u t a r g u m e n t , a l l b r e a k p o i n t s a r e d e l e t e d ( a f t e r a s k i n g

f o r c o n r m a t i o n ) .

i n f o b r e a k p o i n t s

P r i n t t h e l i s t o f a l l b r e a k p o i n t s .

9 . 6 T h e c a l l s t a c k

E a c h t i m e t h e p r o g r a m p e r f o r m s a f u n c t i o n a p p l i c a t i o n , i t s a v e s t h e l o c a t i o n o f t h e a p p l i c a t i o n ( t h e

r e t u r n a d d r e s s ) i n a b l o c k o f d a t a c a l l e d a s t a c k f r a m e . T h e f r a m e a l s o c o n t a i n s t h e l o c a l v a r i a b l e s

o f t h e c a l l e r f u n c t i o n . A l l t h e f r a m e s a r e a l l o c a t e d i n a r e g i o n o f m e m o r y c a l l e d t h e c a l l s t a c k . T h e

c o m m a n d b a c k t r a c e ( o r b t ) d i s p l a y s p a r t s o f t h e c a l l s t a c k .

A t a n y t i m e , o n e o f t h e s t a c k f r a m e s i s \ s e l e c t e d " b y t h e d e b u g g e r ; s e v e r a l d e b u g g e r c o m m a n d s

r e f e r i m p l i c i t l y t o t h e s e l e c t e d f r a m e . I n p a r t i c u l a r , w h e n e v e r y o u a s k t h e d e b u g g e r f o r t h e v a l u e

o f a l o c a l v a r i a b l e , t h e v a l u e i s f o u n d i n t h e s e l e c t e d f r a m e . T h e c o m m a n d s f r a m e , u p a n d d o w n

s e l e c t w h i c h e v e r f r a m e y o u a r e i n t e r e s t e d i n .

W h e n t h e p r o g r a m s t o p s , t h e d e b u g g e r a u t o m a t i c a l l y s e l e c t s t h e c u r r e n t l y e x e c u t i n g f r a m e a n d

d e s c r i b e s i t b r i e y a s t h e f r a m e c o m m a n d d o e s .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 87/218

C h a p t e r 9 . T h e d e b u g g e r ( c a m l d e b u g ) 8 5

f r a m e

D e s c r i b e t h e c u r r e n t l y s e l e c t e d s t a c k f r a m e .

f r a m e f r a m e - n u m b e r

S e l e c t a s t a c k f r a m e b y n u m b e r a n d d e s c r i b e i t . T h e f r a m e c u r r e n t l y e x e c u t i n g w h e n t h e

p r o g r a m s t o p p e d h a s n u m b e r 0 ; i t s c a l l e r h a s n u m b e r 1 ; a n d s o o n u p t h e c a l l s t a c k .

b a c k t r a c e c o u n t ] , b t c o u n t ]

P r i n t t h e c a l l s t a c k . T h i s i s u s e f u l t o s e e w h i c h s e q u e n c e o f f u n c t i o n c a l l s l e d t o t h e c u r r e n t l y

e x e c u t i n g f r a m e . W i t h a p o s i t i v e a r g u m e n t , p r i n t o n l y t h e i n n e r m o s t c o u n t f r a m e s . W i t h a

n e g a t i v e a r g u m e n t , p r i n t o n l y t h e o u t e r m o s t - c o u n t f r a m e s .

u p c o u n t ]

S e l e c t a n d d i s p l a y t h e s t a c k f r a m e j u s t \ a b o v e " t h e s e l e c t e d f r a m e , t h a t i s , t h e f r a m e t h a t

c a l l e d t h e s e l e c t e d f r a m e . A n a r g u m e n t s a y s h o w m a n y f r a m e s t o g o u p .

d o w n c o u n t ]

S e l e c t a n d d i s p l a y t h e s t a c k f r a m e j u s t \ b e l o w " t h e s e l e c t e d f r a m e , t h a t i s , t h e f r a m e t h a t

w a s c a l l e d b y t h e s e l e c t e d f r a m e . A n a r g u m e n t s a y s h o w m a n y f r a m e s t o g o d o w n .

9 . 7 E x a m i n i n g v a r i a b l e v a l u e s

T h e d e b u g g e r c a n p r i n t t h e c u r r e n t v a l u e o f a p r o g r a m v a r i a b l e ( e i t h e r a g l o b a l v a r i a b l e o r a l o c a l

v a r i a b l e r e l a t i v e t o t h e s e l e c t e d s t a c k f r a m e ) . I t c a n a l s o p r i n t s e l e c t e d p a r t s o f a v a l u e b y m a t c h i n g

i t a g a i n s t a p a t t e r n .

V a r i a b l e n a m e s c a n b e s p e c i e d e i t h e r f u l l y q u a l i e d ( m o d u l e - n a m e _ _ v a r - n a m e ) o r u n q u a l i e d

( v a r - n a m e ) . U n q u a l i e d n a m e s e i t h e r c o r r e s p o n d t o l o c a l v a r i a b l e s , o r a r e c o m p l e t e d i n t o f u l l y

q u a l i e d g l o b a l n a m e s b y l o o k i n g a t a l i s t o f \ o p e n e d " m o d u l e s t h a t d e n e t h e s a m e n a m e ( s e e

s e c t i o n 9 . 8 . 5 f o r h o w t o o p e n m o d u l e s i n t h e d e b u g g e r . ) T h e c o m p l e t i o n f o l l o w s t h e s a m e r u l e s a s

i n t h e C a m l L i g h t l a n g u a g e ( s e e s e c t i o n 2 . 2 ) .

p r i n t v a r i a b l e s

P r i n t t h e v a l u e s o f t h e g i v e n v a r i a b l e s .

m a t c h v a r i a b l e p a t t e r n

M a t c h t h e v a l u e o f t h e g i v e n v a r i a b l e a g a i n s t a p a t t e r n , a n d p r i n t t h e v a l u e s b o u n d t o t h e

i d e n t i e r s i n t h e p a t t e r n .

T h e s y n t a x o f p a t t e r n s f o r t h e m a t c h c o m m a n d e x t e n d s t h e o n e f o r C a m l L i g h t p a t t e r n s :

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 88/218

8 6

p a t t e r n : : = i d e n t

j _

j ( p a t t e r n )

j n c c o n s t r p a t t e r n

j p a t t e r n , p a t t e r n f , p a t t e r n g

j { l a b e l = p a t t e r n f ; l a b e l = p a t t e r n g }

j ]

j p a t t e r n f ; p a t t e r n g ]

j p a t t e r n : : p a t t e r n

j # i n t e g e r - l i t e r a l p a t t e r n

j > p a t t e r n

T h e p a t t e r n i d e n t , w h e r e i d e n t i s a n i d e n t i e r , m a t c h e s a n y v a l u e , a n d b i n d s t h e i d e n t i e r t o

t h i s v a l u e . T h e p a t t e r n # n p a t t e r n m a t c h e s a l i s t , a v e c t o r o r a t u p l e w h o s e n - t h e l e m e n t m a t c h e s

p a t t e r n . T h e p a t t e r n > p a t t e r n m a t c h e s a n y c o n s t r u c t e d v a l u e w h o s e a r g u m e n t m a t c h e s p a t t e r n ,

r e g a r d l e s s o f t h e c o n s t r u c t o r ; i t i s a s h o r t c u t f o r s k i p p i n g a c o n s t r u c t o r .

E x a m p l e : a s s u m i n g t h e v a l u e o f a i s C o n s t r { x = 1 ; 2 ; 3 ; 4 ] } , t h e c o m m a n d m a t c h a > { x =

# 2 k } p r i n t s k = 3 .

s e t p r i n t _ d e p t h d

L i m i t t h e p r i n t i n g o f v a l u e s t o a m a x i m a l d e p t h o f d .

s e t p r i n t _ l e n g t h l

L i m i t t h e p r i n t i n g o f v a l u e s t o a t m o s t l n o d e s p r i n t e d .

9 . 8 C o n t r o l l i n g t h e d e b u g g e r

9 . 8 . 1 S e t t i n g t h e p r o g r a m n a m e a n d a r g u m e n t s

s e t p r o g r a m l e

S e t t h e p r o g r a m n a m e t o l e .

s e t a r g u m e n t s a r g u m e n t s

G i v e a r g u m e n t s a s c o m m a n d - l i n e a r g u m e n t s f o r t h e p r o g r a m .

A s h e l l i s u s e d t o p a s s t h e a r g u m e n t s t o t h e d e b u g g e d p r o g r a m . Y o u c a n t h e r e f o r e u s e w i l d c a r d s ,

s h e l l v a r i a b l e s , a n d l e r e d i r e c t i o n s i n s i d e t h e a r g u m e n t s . T o d e b u g p r o g r a m s t h a t r e a d f r o m

s t a n d a r d i n p u t , i t i s r e c o m m e n d e d t o r e d i r e c t t h e i r i n p u t f r o m a l e ( u s i n g s e t a r g u m e n t s <

i n p u t - f i l e ) , o t h e r w i s e i n p u t t o t h e p r o g r a m a n d i n p u t t o t h e d e b u g g e r a r e n o t p r o p e r l y s e p a r a t e d .

9 . 8 . 2 H o w p r o g r a m s a r e l o a d e d

T h e l o a d i n g m o d e v a r i a b l e c o n t r o l s h o w t h e p r o g r a m i s e x e c u t e d .

s e t l o a d i n g m o d e d i r e c t

T h e p r o g r a m i s r u n d i r e c t l y b y t h e d e b u g g e r . T h i s i s t h e d e f a u l t m o d e .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 89/218

C h a p t e r 9 . T h e d e b u g g e r ( c a m l d e b u g ) 8 7

s e t l o a d i n g m o d e r u n t i m e

T h e d e b u g g e r e x e c u t e t h e C a m l L i g h t r u n t i m e c a m l r u n o n t h e p r o g r a m . R a r e l y u s e f u l ;

m o r e o v e r i t p r e v e n t s t h e d e b u g g i n g o f p r o g r a m s c o m p i l e d i n \ c u s t o m r u n t i m e " m o d e .

s e t l o a d i n g m o d e m a n u a l

T h e u s e r s t a r t s m a n u a l l y t h e p r o g r a m , w h e n a s k e d b y t h e d e b u g g e r . A l l o w s r e m o t e d e b u g g i n g

( s e e s e c t i o n 9 . 8 . 7 ) .

9 . 8 . 3 S e a r c h p a t h f o r l e s

T h e d e b u g g e r s e a r c h e s f o r s o u r c e l e s a n d c o m p i l e d i n t e r f a c e l e s i n a l i s t o f d i r e c t o r i e s , t h e s e a r c h

p a t h . T h e s e a r c h p a t h i n i t i a l l y c o n t a i n s t h e c u r r e n t d i r e c t o r y . a n d t h e s t a n d a r d l i b r a r y d i r e c t o r y .

T h e d i r e c t o r y c o m m a n d a d d s d i r e c t o r i e s t o t h e p a t h .

W h e n e v e r t h e s e a r c h p a t h i s m o d i e d , t h e d e b u g g e r w i l l c l e a r a n y i n f o r m a t i o n i t m a y h a v e

c a c h e d a b o u t t h e l e s .

d i r e c t o r y d i r e c t o r y n a m e s

A d d t h e g i v e n d i r e c t o r i e s t o t h e s e a r c h p a t h . T h e s e d i r e c t o r i e s a r e a d d e d a t t h e f r o n t , a n d

w i l l t h e r e f o r e b e s e a r c h e d r s t .

d i r e c t o r y

R e s e t t h e s e a r c h p a t h . T h i s r e q u i r e s c o n r m a t i o n .

9 . 8 . 4 W o r k i n g d i r e c t o r y

E a c h t i m e a p r o g r a m i s s t a r t e d i n t h e d e b u g g e r , i t i n h e r i t s i t s w o r k i n g d i r e c t o r y f r o m t h e c u r r e n t

w o r k i n g d i r e c t o r y o f t h e d e b u g g e r . T h i s w o r k i n g d i r e c t o r y i s i n i t i a l l y w h a t e v e r i t i n h e r i t e d f r o m i t s

p a r e n t p r o c e s s ( t y p i c a l l y t h e s h e l l ) , b u t y o u c a n s p e c i f y a n e w w o r k i n g d i r e c t o r y i n t h e d e b u g g e r

w i t h t h e c d c o m m a n d o r t h e - c d c o m m a n d - l i n e o p t i o n .

c d d i r e c t o r y

S e t t h e w o r k i n g d i r e c t o r y f o r c a m l d e b u g t o d i r e c t o r y .

p w d P r i n t t h e w o r k i n g d i r e c t o r y f o r c a m l d e b u g .

9 . 8 . 5 M o d u l e m a n a g e m e n t

L i k e t h e C a m l L i g h t c o m p i l e r , t h e d e b u g g e r m a i n t a i n s a l i s t o f o p e n e d m o d u l e s i n o r d e r t o r e s o l v e s

v a r i a b l e n a m e a m b i g u i t i e s . T h e o p e n e d m o d u l e s a l s o a e c t t h e p r i n t i n g o f v a l u e s : w h e t h e r f u l l y

q u a l i e d n a m e s o r s h o r t n a m e s a r e u s e d f o r c o n s t r u c t o r s a n d r e c o r d l a b e l s .

W h e n a p r o g r a m i s e x e c u t e d , t h e d e b u g g e r a u t o m a t i c a l l y o p e n s t h e m o d u l e s o f t h e s t a n d a r d

l i b r a r y i t u s e s .

o p e n m o d u l e s

O p e n t h e g i v e n m o d u l e s .

c l o s e m o d u l e s

C l o s e t h e g i v e n m o d u l e s .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 90/218

8 8

i n f o m o d u l e s

L i s t t h e m o d u l e s u s e d b y t h e p r o g r a m , a n d t h e o p e n m o d u l e s .

9 . 8 . 6 T u r n i n g r e v e r s e e x e c u t i o n o n a n d o

I n s o m e c a s e s , y o u m a y w a n t t o t u r n r e v e r s e e x e c u t i o n o . T h i s s p e e d s u p t h e p r o g r a m e x e c u t i o n ,

a n d i s a l s o s o m e t i m e s u s e f u l f o r i n t e r a c t i v e p r o g r a m s .

N o r m a l l y , t h e d e b u g g e r t a k e s c h e c k p o i n t s o f t h e p r o g r a m s t a t e f r o m t i m e t o t i m e . T h a t i s , i t

m a k e s a c o p y o f t h e c u r r e n t s t a t e o f t h e p r o g r a m ( u s i n g t h e U n i x s y s t e m c a l l f o r k ) . I f t h e v a r i a b l e

c h e c k p o i n t s i s s e t t o o f f , t h e d e b u g g e r w i l l n o t t a k e a n y c h e c k p o i n t s .

s e t c h e c k p o i n t s o n / o

S e l e c t w h e t h e r t h e d e b u g g e r m a k e s c h e c k p o i n t s o r n o t .

9 . 8 . 7 C o m m u n i c a t i o n b e t w e e n t h e d e b u g g e r a n d t h e p r o g r a m

T h e d e b u g g e r c o m m u n i c a t e w i t h t h e p r o g r a m b e i n g d e b u g g e d t h r o u g h a U n i x s o c k e t . Y o u m a y

n e e d t o c h a n g e t h e s o c k e t n a m e , f o r e x a m p l e i f y o u n e e d t o r u n t h e d e b u g g e r o n a m a c h i n e a n d

y o u r p r o g r a m o n a n o t h e r .

s e t s o c k e t s o c k e t

U s e s o c k e t f o r c o m m u n i c a t i o n w i t h t h e p r o g r a m . s o c k e t c a n b e e i t h e r a l e n a m e , o r a n

I n t e r n e t p o r t s p e c i c a t i o n h o s t : p o r t , w h e r e h o s t i s a h o s t n a m e o r a n I n t e r n e t a d d r e s s i n d o t

n o t a t i o n , a n d p o r t i s a p o r t n u m b e r o n t h e h o s t .

O n t h e d e b u g g e d p r o g r a m s i d e , t h e s o c k e t n a m e i s p a s s e d e i t h e r b y t h e - D c o m m a n d l i n e o p t i o n

t o c a m l r u n , o r t h r o u g h t h e C A M L _ D E B U G _ S O C K E T e n v i r o n m e n t v a r i a b l e .

9 . 8 . 8 F i n e - t u n i n g t h e d e b u g g e r

S e v e r a l v a r i a b l e s e n a b l e s t o n e - t u n e t h e d e b u g g e r . R e a s o n a b l e d e f a u l t s a r e p r o v i d e d , a n d y o u

s h o u l d n o r m a l l y n o t h a v e t o c h a n g e t h e m .

s e t p r o c e s s c o u n t c o u n t

S e t t h e m a x i m u m n u m b e r o f c h e c k p o i n t s t o c o u n t . M o r e c h e c k p o i n t s f a c i l i t a t e g o i n g f a r b a c k

i n t i m e , b u t u s e m o r e m e m o r y a n d c r e a t e m o r e U n i x p r o c e s s e s .

A s c h e c k p o i n t i n g i s q u i t e e x p e n s i v e , i t m u s t n o t b e d o n e t o o o f t e n . O n t h e o t h e r h a n d , b a c k w a r d

e x e c u t i o n i s f a s t e r w h e n c h e c k p o i n t s a r e t a k e n m o r e o f t e n . I n p a r t i c u l a r , b a c k w a r d s i n g l e - s t e p p i n g

i s m o r e r e s p o n s i v e w h e n m a n y c h e c k p o i n t s h a v e b e e n t a k e n j u s t b e f o r e t h e c u r r e n t t i m e . T o n e -

t u n e t h e c h e c k p o i n t i n g s t r a t e g y , t h e d e b u g g e r d o e s n o t t a k e c h e c k p o i n t s a t t h e s a m e f r e q u e n c y

f o r l o n g d i s p l a c e m e n t s ( e . g . r u n ) a n d s m a l l o n e s ( e . g . s t e p ) . T h e t w o v a r i a b l e s b i g s t e p a n d

s m a l l s t e p c o n t a i n t h e n u m b e r o f e v e n t s b e t w e e n t w o c h e c k p o i n t s i n e a c h c a s e .

s e t b i g s t e p c o u n t

S e t t h e n u m b e r o f e v e n t s b e t w e e n t w o c h e c k p o i n t s f o r l o n g d i s p l a c e m e n t s .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 91/218

C h a p t e r 9 . T h e d e b u g g e r ( c a m l d e b u g ) 8 9

s e t s m a l l s t e p c o u n t

S e t t h e n u m b e r o f e v e n t s b e t w e e n t w o c h e c k p o i n t s f o r s m a l l d i s p l a c e m e n t s .

T h e f o l l o w i n g c o m m a n d s d i s p l a y i n f o r m a t i o n o n c h e c k p o i n t s a n d e v e n t s :

i n f o c h e c k p o i n t s

P r i n t a l i s t o f c h e c k p o i n t s .

i n f o e v e n t s m o d u l e ]

P r i n t t h e l i s t o f e v e n t s i n t h e g i v e n m o d u l e ( t h e c u r r e n t m o d u l e , b y d e f a u l t ) .

9 . 9 M i s c e l l a n e o u s c o m m a n d s

l i s t m o d u l e ] b e g i n n i n g ] e n d ]

L i s t t h e s o u r c e o f m o d u l e m o d u l e , f r o m l i n e n u m b e r b e g i n n i n g t o l i n e n u m b e r e n d . B y d e f a u l t ,

2 0 l i n e s o f t h e c u r r e n t m o d u l e a r e d i s p l a y e d , s t a r t i n g 1 0 l i n e s b e f o r e t h e c u r r e n t p o s i t i o n .

s o u r c e l e n a m e

R e a d d e b u g g e r c o m m a n d s f r o m t h e s c r i p t l e n a m e .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 92/218

9 0

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 93/218

C h a p t e r 1 0

P r o l i n g ( c a m l p r o )

T h i s c h a p t e r d e s c r i b e s h o w t h e e x e c u t i o n o f C a m l L i g h t p r o g r a m s c a n b e p r o l e d , b y r e c o r d i n g

h o w m a n y t i m e s f u n c t i o n s a r e c a l l e d , b r a n c h e s o f c o n d i t i o n a l s a r e t a k e n , : : :

M a c : T h i s c o m m a n d i s n o t a v a i l a b l e .

P C : T h i s c o m m a n d i s n o t a v a i l a b l e .

1 0 . 1 C o m p i l i n g f o r p r o l i n g

B e f o r e p r o l i n g a n e x e c u t i o n , t h e p r o g r a m m u s t b e c o m p i l e d i n p r o l i n g m o d e , u s i n g t h e - p o p t i o n

t o t h e b a t c h c o m p i l e r c a m l c ( s e e c h a p t e r 4 ) . W h e n c o m p i l i n g m o d u l e s s e p a r a t e l y , t h e - p o p t i o n

m u s t b e g i v e n b o t h w h e n c o m p i l i n g t h e m o d u l e s ( p r o d u c t i o n o f . z o l e s ) a n d w h e n l i n k i n g t h e m

t o g e t h e r .

T h e a m o u n t o f p r o l i n g i n f o r m a t i o n c a n b e c o n t r o l l e d b y a d d i n g o n e o r s e v e r a l l e t t e r s a f t e r t h e

- p o p t i o n , i n d i c a t i n g w h i c h p a r t s o f t h e p r o g r a m s h o u l d b e p r o l e d :

a a l l o p t i o n s

f f u n c t i o n c a l l s : a c o u n t p o i n t i s s e t a t t h e b e g i n n i n g o f f u n c t i o n b o d i e s

i i f : : : t h e n : : : e l s e : : : : c o u n t p o i n t s a r e s e t i n b o t h t h e n b r a n c h a n d e l s e b r a n c h

l w h i l e , f o r l o o p s : a c o u n t p o i n t i s s e t a t t h e b e g i n n i n g o f t h e l o o p b o d y

m m a t c h b r a n c h e s : a c o u n t p o i n t i s s e t a t t h e b e g i n n i n g o f t h e b o d y o f e a c h b r a n c h

t t r y : : : w i t h : : : b r a n c h e s : a c o u n t p o i n t i s s e t a t t h e b e g i n n i n g o f t h e b o d y o f e a c h b r a n c h

F o r i n s t a n c e , c o m p i l i n g w i t h - p f i l m p r o l e s f u n c t i o n c a l l s , i f : : : t h e n : : : e l s e : : : , l o o p s a n d

p a t t e r n m a t c h i n g .

T h e - p o p t i o n w i t h o u t a d d i t i o n a l l e t t e r s d e f a u l t s t o - p f m , m e a n i n g t h a t o n l y f u n c t i o n c a l l s a n d

p a t t e r n m a t c h i n g a r e p r o l e d .

9 1

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 94/218

9 2

1 0 . 2 P r o l i n g a n e x e c u t i o n

R u n n i n g a b y t e c o d e e x e c u t a b l e l e t h a t h a s b e e n c o m p i l e d a n d l i n k e d w i t h - p r e c o r d s t h e e x e c u t i o n

c o u n t s f o r t h e s p e c i e d p a r t s o f t h e p r o g r a m a n d s a v e s t h e m i n a l e c a l l e d c a m l p r o . d u m p i n t h e

c u r r e n t d i r e c t o r y .

M o r e p r e c i s e l y , t h e d u m p l e c a m l p r o . d u m p i s w r i t t e n w h e n t h e i o _ _ e x i t f u n c t i o n i s c a l l e d .

T h e l i n k e r , c a l l e d w i t h t h e - p o p t i o n , a d d s i o _ _ e x i t 0 a s t h e l a s t p h r a s e o f t h e b y t e c o d e e x e c u t a b l e ,

i n c a s e t h e o r i g i n a l p r o g r a m n e v e r c a l l s i o _ _ e x i t . H o w e v e r , i f t h e p r o g r a m t e r m i n a t e s w i t h a n

u n c a u g h t e x c e p t i o n , t h e d u m p l e w i l l n o t b e p r o d u c e d .

I f a c o m p a t i b l e d u m p l e a l r e a d y e x i s t s i n t h e c u r r e n t d i r e c t o r y , t h e n t h e p r o l i n g i n f o r m a t i o n

i s a c c u m u l a t e d i n t h i s d u m p l e . T h i s a l l o w s , f o r i n s t a n c e , t h e p r o l i n g o f s e v e r a l e x e c u t i o n s o f a

p r o g r a m o n d i e r e n t i n p u t s .

1 0 . 3 P r i n t i n g p r o l i n g i n f o r m a t i o n

T h e c a m l p r o c o m m a n d p r o d u c e s a s o u r c e l i s t i n g o f t h e p r o g r a m m o d u l e s w h e r e e x e c u t i o n c o u n t s

h a v e b e e n i n s e r t e d a s c o m m e n t s . F o r i n s t a n c e ,

c a m l p r o f o o . m l

p r i n t s t h e s o u r c e c o d e f o r t h e f o o m o d u l e , w i t h c o m m e n t s i n d i c a t i n g h o w m a n y t i m e s t h e f u n c t i o n s

i n t h i s m o d u l e h a v e b e e n c a l l e d . N a t u r a l l y , t h i s i n f o r m a t i o n i s a c c u r a t e o n l y i f t h e s o u r c e l e h a s

n o t b e e n m o d i e d s i n c e t h e p r o l i n g e x e c u t i o n t o o k p l a c e .

T h e f o l l o w i n g o p t i o n s a r e r e c o g n i z e d b y c a m l p r o :

c o m p i l e r o p t i o n s - s t d l i b , - I , - i n c l u d e , - O , - o p e n , - i , - l a n g

S e e c h a p t e r 4 f o r t h e d e t a i l e d u s a g e .

- f d u m p l e

S p e c i e s a n a l t e r n a t e d u m p l e o f p r o l i n g i n f o r m a t i o n

- F s t r i n g

S p e c i e s a n a d d i t i o n a l s t r i n g t o b e o u t p u t w i t h p r o l i n g i n f o r m a t i o n . B y d e f a u l t , c a m l p r o

w i l l a n n o t a t e p r o g a m s w i t h c o m m e n t s o f t h e f o r m ( * n * ) w h e r e n i s t h e c o u n t e r v a l u e f o r

a p r o l i n g p o i n t . W i t h o p t i o n - F s , t h e a n n o t a t i o n w i l l b e ( * s n * ) .

A n a d d i t i o n a l a r g u m e n t s p e c i e s t h e o u t p u t l e . F o r i n s t a n c e

c a m l p r o - f . . / t e s t / c a m l p r o . d u m p f o o . m l f o o _ p r o f i l e d . m l

w i l l s a v e t h e a n n o t a t e d p r o g r a m i n l e f o o _ p r o f i l e d . m l . O t h e r w i s e , t h e a n n o t a t e d p r o g r a m i s

w r i t t e n o n t h e s t a n d a r d o u t p u t .

1 0 . 4 K n o w n b u g s

T h e f o l l o w i n g s i t u a t i o n ( l e x . m l )

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 95/218

C h a p t e r 1 0 . P r o l i n g ( c a m l p r o ) 9 3

l e t a = 1 ; ;

x _ _ a ; ;

w i l l b r e a k t h e p r o l e r . M o r e p r e c i s e l y , o n e s h o u l d a v o i d t o r e f e r t o s y m b o l s o f t h e c u r r e n t m o d u l e

w i t h t h e q u a l i e d s y m b o l s y n t a x .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 96/218

9 4

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 97/218

C h a p t e r 1 1

U s i n g C a m l L i g h t u n d e r E m a c s

T h i s c h a p t e r d e s c r i b e s h o w C a m l L i g h t c a n b e u s e d i n c o n j u n c t i o n w i t h G n u E m a c s v e r s i o n 1 9

( v e r s i o n 1 8 i s a l s o p a r t i a l l y s u p p o r t e d ) .

U n i x : T h e E m a c s L i s p l e s i m p l e m e n t i n g t h e C a m l / E m a c s i n t e r f a c e a r e i n c o n t r i b / c a m l m o d e i n

t h e d i s t r i b u t i o n .

M a c : T h e C a m l / E m a c s i n t e r f a c e i s n o t a v a i l a b l e .

P C : T h e C a m l / E m a c s i n t e r f a c e i s n o t a v a i l a b l e .

1 1 . 1 U p d a t i n g y o u r . e m a c s

T h e f o l l o w i n g i n i t i a l i z a t i o n s m u s t b e a d d e d t o y o u r . e m a c s l e :

( s e t q a u t o - m o d e - a l i s t ( c o n s ' ( " \ \ . m l i y l p ] ? " . c a m l - m o d e ) a u t o - m o d e - a l i s t ) )

( a u t o l o a d ' c a m l - m o d e " c a m l " " M a j o r m o d e f o r e d i t i n g C a m l c o d e . " t )

( a u t o l o a d ' r u n - c a m l " i n f - c a m l " " R u n a n i n f e r i o r C a m l p r o c e s s . " t )

( a u t o l o a d ' c a m l d e b u g " c a m l d e b u g " " R u n t h e C a m l d e b u g g e r . " t )

1 1 . 2 T h e c a m l e d i t i n g m o d e

T h e c a m l - m o d e f u n c t i o n i s a m a j o r e d i t i n g m o d e f o r C a m l s o u r c e l e s . I t p r o v i d e s t h e c o r r e c t

s y n t a x t a b l e s , c o m m e n t s y n t a x , : : : f o r t h e C a m l l a n g u a g e . A n e x t r e m e l y c r u d e i n d e n t a t i o n f a c i l i t y

i s p r o v i d e d , a s w e l l a s a s l i g h t l y e n h a n c e d n e x t - e r r o r c o m m a n d ( t o d i s p l a y t h e l o c a t i o n o f a

c o m p i l a t i o n e r r o r ) . T h e f o l l o w i n g k e y b i n d i n g s a r e p e r f o r m e d :

T A B ( f u n c t i o n c a m l - i n d e n t - c o m m a n d )

A t t h e b e g i n n i n g o f a l i n e , i n d e n t t h a t l i n e l i k e t h e l i n e a b o v e . S u c c e s s i v e T A B s i n c r e a s e

t h e i n d e n t a t i o n l e v e l b y 2 s p a c e s ( b y d e f a u l t ; c a n b e s e t w i t h t h e c a m l - m o d e - i n d e n t a t i o n

v a r i a b l e ) .

M - T A B ( f u n c t i o n c a m l - u n i n d e n t - c o m m a n d )

D e c r e a s e t h e i n d e n t a t i o n l e v e l o f t h e c u r r e n t p h r a s e .

9 5

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 98/218

9 6

C - x ` ( f u n c t i o n c a m l - n e x t - e r r o r )

D i s p l a y t h e n e x t c o m p i l a t i o n e r r o r , j u s t a s n e x t - e r r o r d o e s . I n a d d i t i o n , i t p u t s t h e p o i n t

a n d t h e m a r k a r o u n d t h e e x a c t l o c a t i o n o f t h e e r r o r ( t h e s u b e x p r e s s i o n t h a t c a u s e d t h e e r r o r ) .

U n d e r E m a c s 1 9 , t h a t s u b e x p r e s s i o n i s a l s o h i g h l i g h t e d .

M - C - h ( f u n c t i o n c a m l - m a r k - p h r a s e )

M a r k t h e C a m l p h r a s e t h a t c o n t a i n s t h e p o i n t : t h e p o i n t i s p u t a t t h e b e g i n n i n g o f t h e

p h r a s e a n d t h e m a r k a t t h e e n d . P h r a s e s a r e d e l i m i t e d b y ; ; ( t h e n a l d o u b l e - s e m i c o l o n ) .

T h i s f u n c t i o n d o e s n o t p r o p e r l y i g n o r e ; ; i n s i d e s t r i n g l i t e r a l s o r c o m m e n t s .

C - x S P C

W h e n t h e C a m l d e b u g g e r i s r u n n i n g a s a n i n f e r i o r p r o c e s s ( s e c t i o n 1 1 . 4 b e l o w ) , s e t a b r e a k -

p o i n t a t t h e c u r r e n t p o s i t i o n o f t h e p o i n t .

M - C - x o r C - c C - e ( f u n c t i o n c a m l - e v a l - p h r a s e )

W h e n a C a m l t o p l e v e l i s r u n n i n g a s a n i n f e r i o r p r o c e s s ( s e c t i o n 1 1 . 3 b e l o w ) , s e n d i t t h e

t h e C a m l p h r a s e t h a t c o n t a i n s t h e p o i n t . T h e p h r a s e w i l l t h e n b e e v a l u a t e d b y t h e i n f e r i o r

t o p l e v e l a s u s u a l . T h e p h r a s e i s d e l i m i t e d b y ; ; a s d e s c r i b e d f o r t h e c a m l - m a r k - p h r a s e

c o m m a n d .

C - c C - r ( f u n c t i o n c a m l - e v a l - r e g i o n )

S e n d t h e r e g i o n t o a C a m l t o p l e v e l r u n n i n g i n a n i n f e r i o r p r o c e s s .

1 1 . 3 R u n n i n g t h e t o p l e v e l a s a n i n f e r i o r p r o c e s s

M - x r u n - c a m l s t a r t s a C a m l t o p l e v e l w i t h i n p u t a n d o u t p u t i n a n E m a c s b u e r n a m e d

* i n f e r i o r - c a m l * . T h i s g i v e s y o u t h e f u l l p o w e r o f E m a c s t o e d i t t h e i n p u t t o t h e C a m l t o p l e v e l .

A n h i s t o r y o f i n p u t l i n e s i s m a i n t a i n e d , a s i n S h e l l m o d e . T h i s i n c l u d e s t h e f o l l o w i n g c o m m a n d s

( s e e t h e f u n c t i o n c o m i n t - m o d e f o r a c o m p l e t e d e s c r i p t i o n ) :

R E T S e n d t h e c u r r e n t l i n e t o t h e t o p l e v e l .

M - n a n d M - p

M o v e t o t h e n e x t o r p r e v i o u s l i n e i n t h e h i s t o r y .

M - r a n d M - s

R e g e x p s e a r c h i n t h e h i s t o r y .

C - c C - c

S e n d a b r e a k ( i n t e r r u p t s i g n a l ) t o t h e C a m l t o p l e v e l .

P h r a s e s c a n a l s o b e s e n t t o t h e C a m l t o p l e v e l f o r e v a l u a t i o n f r o m a n y b u e r i n C a m l m o d e ,

u s i n g M - C - x , C - c C - e o r C - c C - r .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 99/218

C h a p t e r 1 1 . U s i n g C a m l L i g h t u n d e r E m a c s 9 7

1 1 . 4 R u n n i n g t h e d e b u g g e r a s a n i n f e r i o r p r o c e s s

T h e C a m l d e b u g g e r i s s t a r t e d b y t h e c o m m a n d M - x c a m l d e b u g , w i t h a r g u m e n t t h e n a m e o f t h e

e x e c u t a b l e l e p r o g n a m e t o d e b u g . C o m m u n i c a t i o n w i t h t h e d e b u g g e r t a k e s p l a c e i n a n E m a c s

b u e r n a m e d * c a m l d e b u g - p r o g n a m e * . T h e e d i t i n g a n d h i s t o r y f a c i l i t i e s o f S h e l l m o d e a r e a v a i l a b l e

f o r i n t e r a c t i n g w i t h t h e d e b u g g e r .

I n a d d i t i o n , E m a c s d i s p l a y s t h e s o u r c e l e s c o n t a i n i n g t h e c u r r e n t e v e n t ( t h e c u r r e n t p o s i -

t i o n i n t h e p r o g r a m e x e c u t i o n ) a n d h i g h l i g h t s t h e l o c a t i o n o f t h e e v e n t . T h i s d i s p l a y i s u p d a t e d

s y n c h r o n o u s l y w i t h t h e d e b u g g e r a c t i o n .

T h e f o l l o w i n g b i n d i n g s f o r t h e m o s t c o m m o n d e b u g g e r c o m m a n d s a r e a v a i l a b l e i n t h e

* c a m l d e b u g - p r o g n a m e * b u e r ( s e e s e c t i o n 9 . 3 f o r a f u l l e x p l a n a t i o n o f t h e c o m m a n d s ) :

M - r r u n c o m m a n d : e x e c u t e t h e p r o g r a m f o r w a r d .

M - s s t e p c o m m a n d : e x e c u t e t h e p r o g r a m o n e s t e p f o r w a r d .

M - b b a c k c o m m a n d : e x e c u t e t h e p r o g r a m o n e s t e p b a c k w a r d .

M - l l a s t c o m m a n d : g o b a c k o n e s t e p i n t h e c o m m a n d h i s t o r y .

C - c >

d o w n c o m m a n d : s e l e c t t h e s t a c k f r a m e b e l o w t h e c u r r e n t f r a m e .

C - c <

u p c o m m a n d : s e l e c t t h e s t a c k f r a m e a b o v e t h e c u r r e n t f r a m e .

C - c C - f

f i n i s h c o m m a n d : r u n t i l l t h e c u r r e n t f u n c t i o n r e t u r n s .

I n a b u e r i n C a m l e d i t i n g m o d e , C - x S P C s e t s a b r e a k p o i n t a t t h e c u r r e n t p o s i t i o n o f t h e

p o i n t .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 100/218

9 8

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 101/218

C h a p t e r 1 2

I n t e r f a c i n g C w i t h C a m l L i g h t

T h i s c h a p t e r d e s c r i b e s h o w u s e r - d e n e d p r i m i t i v e s , w r i t t e n i n C , c a n b e a d d e d t o t h e C a m l L i g h t

r u n t i m e s y s t e m a n d c a l l e d f r o m C a m l L i g h t c o d e .

1 2 . 1 O v e r v i e w a n d c o m p i l a t i o n i n f o r m a t i o n

1 2 . 1 . 1 D e c l a r i n g p r i m i t i v e s

U s e r p r i m i t i v e s a r e d e c l a r e d i n a m o d u l e i n t e r f a c e ( a . m l i l e ) , i n t h e s a m e w a y a s a r e g u l a r

M L v a l u e , e x c e p t t h a t t h e d e c l a r a t i o n i s f o l l o w e d b y t h e = s i g n , t h e f u n c t i o n a r i t y ( n u m b e r o f

a r g u m e n t s ) , a n d t h e n a m e o f t h e c o r r e s p o n d i n g C f u n c t i o n . F o r i n s t a n c e , h e r e i s h o w t h e i n p u t

p r i m i t i v e i s d e c l a r e d i n t h e i n t e r f a c e f o r t h e s t a n d a r d l i b r a r y m o d u l e i o :

v a l u e i n p u t : i n _ c h a n n e l - > s t r i n g - > i n t - > i n t - > i n t

= 4 " i n p u t "

P r i m i t i v e s w i t h s e v e r a l a r g u m e n t s a r e a l w a y s c u r r i e d . T h e C f u n c t i o n d o e s n o t n e c e s s a r i l y h a v e

t h e s a m e n a m e a s t h e M L f u n c t i o n .

V a l u e s t h u s d e c l a r e d p r i m i t i v e i n a m o d u l e i n t e r f a c e m u s t n o t b e i m p l e m e n t e d i n t h e m o d u l e

i m p l e m e n t a t i o n ( t h e . m l l e ) . T h e y c a n b e u s e d i n s i d e t h e m o d u l e i m p l e m e n t a t i o n .

1 2 . 1 . 2 I m p l e m e n t i n g p r i m i t i v e s

U s e r p r i m i t i v e s w i t h a r i t y n 5 a r e i m p l e m e n t e d b y C f u n c t i o n s t h a t t a k e n a r g u m e n t s o f t y p e

v a l u e , a n d r e t u r n a r e s u l t o f t y p e v a l u e . T h e t y p e v a l u e i s t h e t y p e o f t h e r e p r e s e n t a t i o n s

f o r C a m l L i g h t v a l u e s . I t e n c o d e s o b j e c t s o f s e v e r a l b a s e t y p e s ( i n t e g e r s , o a t i n g - p o i n t n u m b e r s ,

s t r i n g s , : : : ) , a s w e l l a s C a m l L i g h t d a t a s t r u c t u r e s . T h e t y p e v a l u e a n d t h e a s s o c i a t e d c o n v e r s i o n

f u n c t i o n s a n d m a c r o s a r e d e s c r i b e d i n d e t a i l s b e l o w . F o r i n s t a n c e , h e r e i s t h e d e c l a r a t i o n f o r t h e C

f u n c t i o n i m p l e m e n t i n g t h e i n p u t p r i m i t i v e :

v a l u e i n p u t ( c h a n n e l , b u f f e r , o f f s e t , l e n g t h )

v a l u e c h a n n e l , b u f f e r , o f f s e t , l e n g t h ;

{

. . .

}

9 9

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 102/218

1 0 0

W h e n t h e p r i m i t i v e f u n c t i o n i s a p p l i e d i n a C a m l L i g h t p r o g r a m , t h e C f u n c t i o n i s c a l l e d w i t h

t h e v a l u e s o f t h e e x p r e s s i o n s t o w h i c h t h e p r i m i t i v e i s a p p l i e d a s a r g u m e n t s . T h e v a l u e r e t u r n e d

b y t h e f u n c t i o n i s p a s s e d b a c k t o t h e C a m l L i g h t p r o g r a m a s t h e r e s u l t o f t h e f u n c t i o n a p p l i c a t i o n .

U s e r p r i m i t i v e s w i t h a r i t y g r e a t e r t h a n 5 a r e i m p l e m e n t e d b y C f u n c t i o n s t h a t r e c e i v e t w o

a r g u m e n t s : a p o i n t e r t o a n a r r a y o f C a m l L i g h t v a l u e s ( t h e v a l u e s f o r t h e a r g u m e n t s ) , a n d a n

i n t e g e r w h i c h i s t h e n u m b e r o f a r g u m e n t s p r o v i d e d :

v a l u e p r i m _ w i t h _ l o t s _ o f _ a r g s ( a r g v , a r g n )

v a l u e * a r g v ;

i n t a r g n ;

{

. . . a r g v 0 ] . . . ; / * T h e f i r s t a r g u m e n t * /

. . . a r g v 6 ] . . . ; / * T h e s e v e n t h a r g u m e n t * /

}

I m p l e m e n t i n g a u s e r p r i m i t i v e i s a c t u a l l y t w o s e p a r a t e t a s k s : o n t h e o n e h a n d , d e c o d i n g t h e

a r g u m e n t s t o e x t r a c t C v a l u e s f r o m t h e g i v e n C a m l L i g h t v a l u e s , a n d e n c o d i n g t h e r e t u r n v a l u e a s

a C a m l L i g h t v a l u e ; o n t h e o t h e r h a n d , a c t u a l l y c o m p u t i n g t h e r e s u l t f r o m t h e a r g u m e n t s . E x c e p t

f o r v e r y s i m p l e p r i m i t i v e s , i t i s o f t e n p r e f e r a b l e t o h a v e t w o d i s t i n c t C f u n c t i o n s t o i m p l e m e n t

t h e s e t w o t a s k s . T h e r s t f u n c t i o n a c t u a l l y i m p l e m e n t s t h e p r i m i t i v e , t a k i n g n a t i v e C v a l u e s a s

a r g u m e n t s a n d r e t u r n i n g a n a t i v e C v a l u e . T h e s e c o n d f u n c t i o n , o f t e n c a l l e d t h e \ s t u b c o d e " , i s a

s i m p l e w r a p p e r a r o u n d t h e r s t f u n c t i o n t h a t c o n v e r t s i t s a r g u m e n t s f r o m C a m l L i g h t v a l u e s t o C

v a l u e s , c a l l t h e r s t f u n c t i o n , a n d c o n v e r t t h e r e t u r n e d C v a l u e t o C a m l L i g h t v a l u e . F o r i n s t a n c e ,

h e r e i s t h e s t u b c o d e f o r t h e i n p u t p r i m i t i v e :

v a l u e i n p u t ( c h a n n e l , b u f f e r , o f f s e t , l e n g t h )

v a l u e c h a n n e l , b u f f e r , o f f s e t , l e n g t h ;

{

r e t u r n V a l _ l o n g ( g e t b l o c k ( ( s t r u c t c h a n n e l * ) c h a n n e l ,

& B y t e ( b u f f e r , L o n g _ v a l ( o f f s e t ) ) ,

L o n g _ v a l ( l e n g t h ) ) ) ;

}

( H e r e , V a l _ l o n g , L o n g _ v a l a n d s o o n a r e c o n v e r s i o n m a c r o s f o r t h e t y p e v a l u e , t h a t w i l l b e

d e s c r i b e d l a t e r . ) T h e h a r d w o r k i s p e r f o r m e d b y t h e f u n c t i o n g e t b l o c k , w h i c h i s d e c l a r e d a s :

l o n g g e t b l o c k ( c h a n n e l , p , n )

s t r u c t c h a n n e l * c h a n n e l ;

c h a r * p ;

l o n g n ;

{

. . .

}

T o w r i t e C c o d e t h a t o p e r a t e s o n C a m l L i g h t v a l u e s , t h e f o l l o w i n g i n c l u d e l e s a r e p r o v i d e d :

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 103/218

C h a p t e r 1 2 . I n t e r f a c i n g C w i t h C a m l L i g h t 1 0 1

I n c l u d e l e P r o v i d e s

m l v a l u e s . h d e n i t i o n o f t h e v a l u e t y p e , a n d c o n v e r s i o n m a c r o s

a l l o c . h a l l o c a t i o n f u n c t i o n s ( t o c r e a t e s t r u c t u r e d C a m l L i g h t o b j e c t s )

m e m o r y . h m i s c e l l a n e o u s m e m o r y - r e l a t e d f u n c t i o n s ( f o r i n - p l a c e m o d i c a t i o n o f

s t r u c t u r e s , e t c ) .

T h e s e l e s r e s i d e i n t h e C a m l L i g h t s t a n d a r d l i b r a r y d i r e c t o r y ( u s u a l l y / u s r / l o c a l / l i b / c a m l - l i g h t ) .

1 2 . 1 . 3 L i n k i n g C c o d e w i t h C a m l L i g h t c o d e

T h e C a m l L i g h t r u n t i m e s y s t e m c o m p r i s e s t h r e e m a i n p a r t s : t h e b y t e c o d e i n t e r p r e t e r , t h e m e m -

o r y m a n a g e r , a n d a s e t o f C f u n c t i o n s t h a t i m p l e m e n t t h e p r i m i t i v e o p e r a t i o n s . S o m e b y t e c o d e

i n s t r u c t i o n s a r e p r o v i d e d t o c a l l t h e s e C f u n c t i o n s , d e s i g n a t e d b y t h e i r o s e t i n a t a b l e o f f u n c t i o n s

( t h e t a b l e o f p r i m i t i v e s ) .

I n t h e d e f a u l t m o d e , t h e C a m l L i g h t l i n k e r p r o d u c e s b y t e c o d e f o r t h e s t a n d a r d r u n t i m e s y s t e m ,

w i t h a s t a n d a r d s e t o f p r i m i t i v e s . R e f e r e n c e s t o p r i m i t i v e s t h a t a r e n o t i n t h i s s t a n d a r d s e t r e s u l t

i n t h e \ u n a v a i l a b l e C p r i m i t i v e " e r r o r .

I n t h e \ c u s t o m r u n t i m e " m o d e , t h e C a m l L i g h t l i n k e r s c a n s t h e b y t e c o d e o b j e c t l e s ( . z o l e s )

a n d d e t e r m i n e s t h e s e t o f r e q u i r e d p r i m i t i v e s . T h e n , i t b u i l d s a s u i t a b l e r u n t i m e s y s t e m , b y c a l l i n g

t h e n a t i v e c o d e l i n k e r w i t h :

t h e t a b l e o f t h e r e q u i r e d p r i m i t i v e s

a l i b r a r y t h a t p r o v i d e s t h e b y t e c o d e i n t e r p r e t e r , t h e m e m o r y m a n a g e r , a n d t h e s t a n d a r d

p r i m i t i v e s

l i b r a r i e s a n d o b j e c t c o d e l e s ( . o l e s ) m e n t i o n e d o n t h e c o m m a n d l i n e f o r t h e C a m l L i g h t

l i n k e r , t h a t p r o v i d e i m p l e m e n t a t i o n s f o r t h e u s e r ' s p r i m i t i v e s .

T h i s b u i l d s a r u n t i m e s y s t e m w i t h t h e r e q u i r e d p r i m i t i v e s . T h e C a m l L i g h t l i n k e r g e n e r a t e s b y t e -

c o d e f o r t h i s c u s t o m r u n t i m e s y s t e m . T h e b y t e c o d e i s a p p e n d e d t o t h e e n d o f t h e c u s t o m r u n t i m e

s y s t e m , s o t h a t i t w i l l b e a u t o m a t i c a l l y e x e c u t e d w h e n t h e o u t p u t l e ( c u s t o m r u n t i m e + b y t e c o d e )

i s l a u n c h e d .

T o l i n k i n \ c u s t o m r u n t i m e " m o d e , e x e c u t e t h e c a m l c c o m m a n d w i t h :

t h e - c u s t o m o p t i o n

t h e n a m e s o f t h e d e s i r e d C a m l L i g h t o b j e c t l e s ( . z o l e s )

t h e n a m e s o f t h e C o b j e c t l e s a n d l i b r a r i e s ( . o a n d . a l e s ) t h a t i m p l e m e n t t h e r e q u i r e d

p r i m i t i v e s . ( L i b r a r i e s c a n a l s o b e s p e c i e d w i t h t h e u s u a l - l s y n t a x . )

1 2 . 2 T h e v a l u e t y p e

A l l C a m l L i g h t o b j e c t s a r e r e p r e s e n t e d b y t h e C t y p e v a l u e , d e n e d i n t h e i n c l u d e l e m l v a l u e s . h ,

a l o n g w i t h m a c r o s t o m a n i p u l a t e v a l u e s o f t h a t t y p e . A n o b j e c t o f t y p e v a l u e i s e i t h e r :

a n u n b o x e d i n t e g e r

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 104/218

1 0 2

a p o i n t e r t o a b l o c k i n s i d e t h e h e a p ( s u c h a s t h e b l o c k s a l l o c a t e d t h r o u g h o n e o f t h e a l l o c _ *

f u n c t i o n s b e l o w )

a p o i n t e r t o a n o b j e c t o u t s i d e t h e h e a p ( e . g . , a p o i n t e r t o a b l o c k a l l o c a t e d b y m a l l o c , o r t o

a C v a r i a b l e ) .

1 2 . 2 . 1 I n t e g e r v a l u e s

I n t e g e r v a l u e s e n c o d e 3 1 - b i t s i g n e d i n t e g e r s . T h e y a r e u n b o x e d ( u n a l l o c a t e d ) .

1 2 . 2 . 2 B l o c k s

B l o c k s i n t h e h e a p a r e g a r b a g e - c o l l e c t e d , a n d t h e r e f o r e h a v e s t r i c t s t r u c t u r e c o n s t r a i n t s . E a c h

b l o c k i n c l u d e s a h e a d e r c o n t a i n i n g t h e s i z e o f t h e b l o c k ( i n w o r d s ) , a n d t h e t a g o f t h e b l o c k . T h e

t a g g o v e r n s h o w t h e c o n t e n t s o f t h e b l o c k s a r e s t r u c t u r e d . A t a g l o w e r t h a n N o _ s c a n _ t a g i n d i c a t e s

a s t r u c t u r e d b l o c k , c o n t a i n i n g w e l l - f o r m e d v a l u e s , w h i c h i s r e c u r s i v e l y t r a v e r s e d b y t h e g a r b a g e

c o l l e c t o r . A t a g g r e a t e r t h a n o r e q u a l t o N o _ s c a n _ t a g i n d i c a t e s a r a w b l o c k , w h o s e c o n t e n t s a r e

n o t s c a n n e d b y t h e g a r b a g e c o l l e c t o r . F o r t h e b e n e t s o f a d - h o c p o l y m o r p h i c p r i m i t i v e s s u c h a s

e q u a l i t y a n d s t r u c t u r e d i n p u t - o u t p u t , s t r u c t u r e d a n d r a w b l o c k s a r e f u r t h e r c l a s s i e d a c c o r d i n g t o

t h e i r t a g s a s f o l l o w s :

T a g C o n t e n t s o f t h e b l o c k

0 t o N o _ s c a n _ t a g ? 1 A s t r u c t u r e d b l o c k ( a n a r r a y o f C a m l L i g h t o b j e c t s ) . E a c h

e l d i s a v a l u e .

C l o s u r e _ t a g A c l o s u r e r e p r e s e n t i n g a f u n c t i o n a l v a l u e . T h e r s t w o r d i s

a p o i n t e r t o a p i e c e o f b y t e c o d e , t h e s e c o n d w o r d i s a v a l u e

c o n t a i n i n g t h e e n v i r o n m e n t .

S t r i n g _ t a g A c h a r a c t e r s t r i n g .

D o u b l e _ t a g A d o u b l e - p r e c i s i o n o a t i n g - p o i n t n u m b e r .

A b s t r a c t _ t a g A b l o c k r e p r e s e n t i n g a n a b s t r a c t d a t a t y p e .

F i n a l _ t a g A b l o c k r e p r e s e n t i n g a n a b s t r a c t d a t a t y p e w i t h a \ n a l i z a -

t i o n " f u n c t i o n , t o b e c a l l e d w h e n t h e b l o c k i s d e a l l o c a t e d .

1 2 . 2 . 3 P o i n t e r s t o o u t s i d e t h e h e a p

A n y p o i n t e r t o o u t s i d e t h e h e a p c a n b e s a f e l y c a s t t o a n d f r o m t h e t y p e v a l u e . T h i s i n c l u d e s

p o i n t e r s r e t u r n e d b y m a l l o c , a n d p o i n t e r s t o C v a r i a b l e s o b t a i n e d w i t h t h e & o p e r a t o r .

1 2 . 3 R e p r e s e n t a t i o n o f C a m l L i g h t d a t a t y p e s

T h i s s e c t i o n d e s c r i b e s h o w C a m l L i g h t d a t a t y p e s a r e e n c o d e d i n t h e v a l u e t y p e .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 105/218

C h a p t e r 1 2 . I n t e r f a c i n g C w i t h C a m l L i g h t 1 0 3

1 2 . 3 . 1 A t o m i c t y p e s

C a m l t y p e E n c o d i n g

i n t U n b o x e d i n t e g e r v a l u e s .

c h a r U n b o x e d i n t e g e r v a l u e s ( A S C I I c o d e ) .

f l o a t B l o c k s w i t h t a g D o u b l e _ t a g .

s t r i n g B l o c k s w i t h t a g S t r i n g _ t a g .

1 2 . 3 . 2 P r o d u c t t y p e s

T u p l e s a n d a r r a y s a r e r e p r e s e n t e d b y p o i n t e r s t o b l o c k s , w i t h t a g 0 .

R e c o r d s a r e a l s o r e p r e s e n t e d b y z e r o - t a g g e d b l o c k s . T h e o r d e r i n g o f l a b e l s i n t h e r e c o r d t y p e

d e c l a r a t i o n d e t e r m i n e s t h e l a y o u t o f t h e r e c o r d e l d s : t h e v a l u e a s s o c i a t e d t o t h e l a b e l d e c l a r e d

r s t i s s t o r e d i n e l d 0 o f t h e b l o c k , t h e v a l u e a s s o c i a t e d t o t h e l a b e l d e c l a r e d n e x t g o e s i n e l d 1 ,

a n d s o o n .

1 2 . 3 . 3 C o n c r e t e t y p e s

C o n s t r u c t e d t e r m s a r e r e p r e s e n t e d b y b l o c k s w h o s e t a g e n c o d e t h e c o n s t r u c t o r . T h e c o n s t r u c t o r s f o r

a g i v e n c o n c r e t e t y p e a r e n u m b e r e d f r o m 0 t o t h e n u m b e r o f c o n s t r u c t o r s m i n u s o n e , f o l l o w i n g t h e

o r d e r i n w h i c h t h e y a p p e a r i n t h e c o n c r e t e t y p e d e c l a r a t i o n . C o n s t a n t c o n s t r u c t o r s a r e r e p r e s e n t e d

b y z e r o - s i z e d b l o c k s ( a t o m s ) , t a g g e d w i t h t h e c o n s t r u c t o r n u m b e r . N o n - c o n s t a n t c o n s t r u c t o r s

d e c l a r e d w i t h a n - t u p l e a s a r g u m e n t a r e r e p r e s e n t e d b y a b l o c k o f s i z e n , t a g g e d w i t h t h e c o n s t r u c t o r

n u m b e r ; t h e n e l d s c o n t a i n t h e c o m p o n e n t s o f i t s t u p l e a r g u m e n t . O t h e r n o n - c o n s t a n t c o n s t r u c t o r s

a r e r e p r e s e n t e d b y a b l o c k o f s i z e 1 , t a g g e d w i t h t h e c o n s t r u c t o r n u m b e r ; t h e e l d 0 c o n t a i n s t h e

v a l u e o f t h e c o n s t r u c t o r a r g u m e n t . E x a m p l e :

C o n s t r u c t e d t e r m R e p r e s e n t a t i o n

( ) S i z e = 0 , t a g = 0

f a l s e S i z e = 0 , t a g = 0

t r u e S i z e = 0 , t a g = 1

] S i z e = 0 , t a g = 0

h : : t S i z e = 2 , t a g = 1 , r s t e l d = h , s e c o n d e l d = t

1 2 . 4 O p e r a t i o n s o n v a l u e s

1 2 . 4 . 1 K i n d t e s t s

I s _ i n t ( v ) i s t r u e i f v a l u e v i s a n i m m e d i a t e i n t e g e r , f a l s e o t h e r w i s e

I s _ b l o c k ( v ) i s t r u e i f v a l u e v i s a p o i n t e r t o a b l o c k , a n d f a l s e i f i t i s a n i m m e d i a t e i n t e g e r .

1 2 . 4 . 2 O p e r a t i o n s o n i n t e g e r s

V a l _ l o n g ( l ) r e t u r n s t h e v a l u e e n c o d i n g t h e l o n g i n t l

L o n g _ v a l ( v ) r e t u r n s t h e l o n g i n t e n c o d e d i n v a l u e v

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 106/218

1 0 4

V a l _ i n t ( i ) r e t u r n s t h e v a l u e e n c o d i n g t h e i n t i

I n t _ v a l ( v ) r e t u r n s t h e i n t e n c o d e d i n v a l u e v

1 2 . 4 . 3 A c c e s s i n g b l o c k s

W o s i z e _ v a l ( v ) r e t u r n s t h e s i z e o f v a l u e v , i n w o r d s , e x c l u d i n g t h e h e a d e r .

T a g _ v a l ( v ) r e t u r n s t h e t a g o f v a l u e v .

F i e l d ( v ; n ) r e t u r n s t h e v a l u e c o n t a i n e d i n t h e n

t h

e l d o f t h e s t r u c t u r e d b l o c k v . F i e l d s a r e

n u m b e r e d f r o m 0 t o W o s i z e _ v a l ( v ) ? 1 .

C o d e _ v a l ( v ) r e t u r n s t h e c o d e p a r t o f t h e c l o s u r e v .

E n v _ v a l ( v ) r e t u r n s t h e e n v i r o n m e n t p a r t o f t h e c l o s u r e v .

s t r i n g _ l e n g t h ( v ) r e t u r n s t h e l e n g t h ( n u m b e r o f c h a r a c t e r s ) o f t h e s t r i n g v .

B y t e ( v ; n ) r e t u r n s t h e n

t h

c h a r a c t e r o f t h e s t r i n g v , w i t h t y p e c h a r . C h a r a c t e r s a r e n u m b e r e d

f r o m 0 t o s t r i n g _ l e n g t h ( v ) ? 1 .

B y t e _ u ( v ; n ) r e t u r n s t h e n

t h

c h a r a c t e r o f t h e s t r i n g v , w i t h t y p e u n s i g n e d c h a r . C h a r a c t e r s

a r e n u m b e r e d f r o m 0 t o s t r i n g _ l e n g t h ( v ) ? 1 .

S t r i n g _ v a l ( v ) r e t u r n s a p o i n t e r t o t h e r s t b y t e o f t h e s t r i n g v , w i t h t y p e c h a r * . T h i s

p o i n t e r i s a v a l i d C s t r i n g : t h e r e i s a n u l l c h a r a c t e r a f t e r t h e l a s t c h a r a c t e r i n t h e s t r i n g .

H o w e v e r , C a m l L i g h t s t r i n g s c a n c o n t a i n e m b e d d e d n u l l c h a r a c t e r s , t h a t w i l l c o n f u s e t h e

u s u a l C f u n c t i o n s o v e r s t r i n g s .

D o u b l e _ v a l ( v ) r e t u r n s t h e o a t i n g - p o i n t n u m b e r c o n t a i n e d i n v a l u e v , w i t h t y p e d o u b l e .

T h e e x p r e s s i o n s F i e l d ( v ; n ) , C o d e _ v a l ( v ) , E n v _ v a l ( v ) , B y t e ( v ; n ) , B y t e _ u ( v ; n ) a n d

D o u b l e _ v a l ( v ) a r e v a l i d l - v a l u e s . H e n c e , t h e y c a n b e a s s i g n e d t o , r e s u l t i n g i n a n i n - p l a c e

m o d i c a t i o n o f v a l u e v . A s s i g n i n g d i r e c t l y t o F i e l d ( v ; n ) m u s t b e d o n e w i t h c a r e t o a v o i d

c o n f u s i n g t h e g a r b a g e c o l l e c t o r ( s e e b e l o w ) .

1 2 . 4 . 4 A l l o c a t i n g b l o c k s

F r o m t h e s t a n d p o i n t o f t h e a l l o c a t i o n f u n c t i o n s , b l o c k s a r e d i v i d e d a c c o r d i n g t o t h e i r s i z e a s z e r o -

s i z e d b l o c k s , s m a l l b l o c k s ( w i t h s i z e l e s s t h a n o r e q u a l t o M a x _ y o u n g _ w o s i z e ) , a n d l a r g e b l o c k s

( w i t h s i z e g r e a t e r t h a n t o M a x _ y o u n g _ w o s i z e ) . T h e c o n s t a n t M a x _ y o u n g _ w o s i z e i s d e c l a r e d i n t h e

i n c l u d e l e m l v a l u e s . h . I t i s g u a r a n t e e d t o b e a t l e a s t 6 4 ( w o r d s ) , s o t h a t a n y b l o c k w i t h c o n s t a n t

s i z e l e s s t h a n o r e q u a l t o 6 4 c a n b e a s s u m e d t o b e s m a l l . F o r b l o c k s w h o s e s i z e i s c o m p u t e d a t

r u n - t i m e , t h e s i z e m u s t b e c o m p a r e d a g a i n s t M a x _ y o u n g _ w o s i z e t o d e t e r m i n e t h e c o r r e c t a l l o c a t i o n

p r o c e d u r e .

A t o m ( t ) r e t u r n s a n \ a t o m " ( z e r o - s i z e d b l o c k ) w i t h t a g t . Z e r o - s i z e d b l o c k s a r e p r e a l l o c a t e d

o u t s i d e o f t h e h e a p . I t i s i n c o r r e c t t o t r y a n d a l l o c a t e a z e r o - s i z e d b l o c k u s i n g t h e f u n c t i o n s

b e l o w . F o r i n s t a n c e , A t o m ( 0 ) r e p r e s e n t s ( ) , f a l s e a n d ] ; A t o m ( 1 ) r e p r e s e n t s t r u e . ( A s a

c o n v e n i e n c e , m l v a l u e s . h d e n e s t h e m a c r o s V a l _ u n i t , V a l _ f a l s e a n d V a l _ t r u e . )

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 107/218

C h a p t e r 1 2 . I n t e r f a c i n g C w i t h C a m l L i g h t 1 0 5

a l l o c ( n ; t ) r e t u r n s a f r e s h s m a l l b l o c k o f s i z e n M a x _ y o u n g _ w o s i z e w o r d s , w i t h t a g t . I f

t h i s b l o c k i s a s t r u c t u r e d b l o c k ( i . e . i f t < N o _ s c a n _ t a g ) , t h e n t h e e l d s o f t h e b l o c k ( i n i t i a l l y

c o n t a i n i n g g a r b a g e ) m u s t b e i n i t i a l i z e d w i t h l e g a l v a l u e s ( u s i n g d i r e c t a s s i g n m e n t t o t h e e l d s

o f t h e b l o c k ) b e f o r e t h e n e x t a l l o c a t i o n .

a l l o c _ t u p l e ( n ) r e t u r n s a f r e s h s m a l l b l o c k o f s i z e n M a x _ y o u n g _ w o s i z e w o r d s , w i t h t a g

0 . T h e e l d s o f t h i s b l o c k m u s t b e l l e d w i t h l e g a l v a l u e s b e f o r e t h e n e x t a l l o c a t i o n o r

m o d i c a t i o n .

a l l o c _ s h r ( n ; t ) r e t u r n s a f r e s h b l o c k o f s i z e n , w i t h t a g t . T h e s i z e o f t h e b l o c k c a n b e g r e a t e r

t h a n M a x _ y o u n g _ w o s i z e . ( I t c a n a l s o b e s m a l l e r , b u t i n t h i s c a s e i t i s m o r e e c i e n t t o c a l l

a l l o c i n s t e a d o f a l l o c _ s h r . ) I f t h i s b l o c k i s a s t r u c t u r e d b l o c k ( i . e . i f t < N o _ s c a n _ t a g ) ,

t h e n t h e e l d s o f t h e b l o c k ( i n i t i a l l y c o n t a i n i n g g a r b a g e ) m u s t b e i n i t i a l i z e d w i t h l e g a l v a l u e s

( u s i n g t h e i n i t i a l i z e f u n c t i o n d e s c r i b e d b e l o w ) b e f o r e t h e n e x t a l l o c a t i o n .

a l l o c _ s t r i n g ( n ) r e t u r n s a s t r i n g v a l u e o f l e n g t h n c h a r a c t e r s . T h e s t r i n g i n i t i a l l y c o n t a i n s

g a r b a g e .

c o p y _ s t r i n g ( s ) r e t u r n s a s t r i n g v a l u e c o n t a i n i n g a c o p y o f t h e n u l l - t e r m i n a t e d C s t r i n g s ( a

c h a r * ) .

c o p y _ d o u b l e ( d ) r e t u r n s a o a t i n g - p o i n t v a l u e i n i t i a l i z e d w i t h t h e d o u b l e d .

a l l o c _ a r r a y ( f ; a ) a l l o c a t e s a n a r r a y o f v a l u e s , c a l l i n g f u n c t i o n f o v e r e a c h e l e m e n t o f t h e

i n p u t a r r a y a t o t r a n s f o r m i t i n t o a v a l u e . T h e a r r a y a i s a n a r r a y o f p o i n t e r s t e r m i n a t e d

b y t h e n u l l p o i n t e r . T h e f u n c t i o n f r e c e i v e s e a c h p o i n t e r a s a r g u m e n t , a n d r e t u r n s a v a l u e .

T h e z e r o - t a g g e d b l o c k r e t u r n e d b y a l l o c _ a r r a y ( f ; a ) i s l l e d w i t h t h e v a l u e s r e t u r n e d b y

t h e s u c c e s s i v e c a l l s t o f .

c o p y _ s t r i n g _ a r r a y ( p ) a l l o c a t e s a n a r r a y o f s t r i n g s , c o p i e d f r o m t h e p o i n t e r t o a s t r i n g a r r a y

p ( a c h a r * * ) .

1 2 . 4 . 5 R a i s i n g e x c e p t i o n s

C f u n c t i o n s c a n n o t r a i s e a r b i t r a r y e x c e p t i o n s . H o w e v e r , t w o f u n c t i o n s a r e p r o v i d e d t o r a i s e t w o

s t a n d a r d e x c e p t i o n s :

f a i l w i t h ( s ) , w h e r e s i s a n u l l - t e r m i n a t e d C s t r i n g ( w i t h t y p e c h a r * ) , r a i s e s e x c e p t i o n

F a i l u r e w i t h a r g u m e n t s .

i n v a l i d _ a r g u m e n t ( s ) , w h e r e s i s a n u l l - t e r m i n a t e d C s t r i n g ( w i t h t y p e c h a r * ) , r a i s e s e x -

c e p t i o n I n v a l i d _ a r g u m e n t w i t h a r g u m e n t s .

1 2 . 5 L i v i n g i n h a r m o n y w i t h t h e g a r b a g e c o l l e c t o r

U n u s e d b l o c k s i n t h e h e a p a r e a u t o m a t i c a l l y r e c l a i m e d b y t h e g a r b a g e c o l l e c t o r . T h i s r e q u i r e s s o m e

c o o p e r a t i o n f r o m C c o d e t h a t m a n i p u l a t e s h e a p - a l l o c a t e d b l o c k s .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 108/218

1 0 6

R u l e 1 A f t e r a s t r u c t u r e d b l o c k ( a b l o c k w i t h t a g l e s s t h a n N o _ s c a n _ t a g ) i s a l l o c a t e d , a l l e l d s o f

t h i s b l o c k m u s t b e l l e d w i t h w e l l - f o r m e d v a l u e s b e f o r e t h e n e x t a l l o c a t i o n o p e r a t i o n . I f t h e b l o c k h a s

b e e n a l l o c a t e d w i t h a l l o c o r a l l o c _ t u p l e , l l i n g i s p e r f o r m e d b y d i r e c t a s s i g n m e n t t o t h e e l d s o f

t h e b l o c k :

F i e l d ( v , n ) = v

n

;

I f t h e b l o c k h a s b e e n a l l o c a t e d w i t h a l l o c _ s h r , l l i n g i s p e r f o r m e d t h r o u g h t h e i n i t i a l i z e f u n c t i o n :

i n i t i a l i z e ( & F i e l d ( v , n ) , v

n

) ;

T h e n e x t a l l o c a t i o n c a n t r i g g e r a g a r b a g e c o l l e c t i o n . T h e g a r b a g e c o l l e c t o r a s s u m e s t h a t a l l

s t r u c t u r e d b l o c k s c o n t a i n w e l l - f o r m e d v a l u e s . N e w l y c r e a t e d b l o c k s c o n t a i n r a n d o m d a t a , w h i c h

g e n e r a l l y d o n o t r e p r e s e n t w e l l - f o r m e d v a l u e s .

I f y o u r e a l l y n e e d t o a l l o c a t e b e f o r e t h e e l d s c a n r e c e i v e t h e i r n a l v a l u e , r s t i n i t i a l i z e w i t h

a c o n s t a n t v a l u e ( e . g . V a l _ l o n g ( 0 ) ) , t h e n a l l o c a t e , t h e n m o d i f y t h e e l d s w i t h t h e c o r r e c t v a l u e

( s e e r u l e 3 ) .

R u l e 2 L o c a l v a r i a b l e s c o n t a i n i n g v a l u e s m u s t b e r e g i s t e r e d w i t h t h e g a r b a g e c o l l e c t o r ( u s i n g t h e

P u s h _ r o o t s a n d P o p _ r o o t s m a c r o s ) , i f t h e y a r e t o s u r v i v e a c a l l t o a n a l l o c a t i o n f u n c t i o n .

R e g i s t r a t i o n i s p e r f o r m e d w i t h t h e P u s h _ r o o t s a n d P o p _ r o o t s m a c r o s . P u s h _ r o o t s ( r , n )

d e c l a r e s a n a r r a y r o f n v a l u e s a n d r e g i s t e r s t h e m w i t h t h e g a r b a g e c o l l e c t o r . T h e v a l u e s c o n t a i n e d

i n r 0 ] t o r n - 1 ] a r e t r e a t e d l i k e r o o t s b y t h e g a r b a g e c o l l e c t o r . A r o o t v a l u e h a s t h e f o l l o w i n g

p r o p e r t i e s : i f i t p o i n t s t o a h e a p - a l l o c a t e d b l o c k , t h i s b l o c k ( a n d i t s c o n t e n t s ) w i l l n o t b e r e c l a i m e d ;

m o r e o v e r , i f t h i s b l o c k i s r e l o c a t e d b y t h e g a r b a g e c o l l e c t o r , t h e r o o t v a l u e i s u p d a t e d t o p o i n t t o t h e

n e w l o c a t i o n f o r t h e b l o c k . P u s h _ r o o t s ( r , n ) m u s t o c c u r i n a C b l o c k e x a c t l y b e t w e e n t h e l a s t l o c a l

v a r i a b l e d e c l a r a t i o n a n d t h e r s t s t a t e m e n t i n t h e b l o c k . T o u n - r e g i s t e r t h e r o o t s , P o p _ r o o t s ( )

m u s t b e c a l l e d b e f o r e t h e C b l o c k c o n t a i n i n g P u s h _ r o o t s ( r , n ) i s e x i t e d . ( R o o t s a r e a u t o m a t i c a l l y

u n - r e g i s t e r e d i f a C a m l e x c e p t i o n i s r a i s e d . )

R u l e 3 D i r e c t a s s i g n m e n t t o a e l d o f a b l o c k , a s i n

F i e l d ( v , n ) = w ;

i s s a f e o n l y i f v i s a b l o c k n e w l y a l l o c a t e d b y a l l o c o r a l l o c _ t u p l e ; t h a t i s , i f n o a l l o c a t i o n t o o k

p l a c e b e t w e e n t h e a l l o c a t i o n o f v a n d t h e a s s i g n m e n t t o t h e e l d . I n a l l o t h e r c a s e s , n e v e r a s s i g n

d i r e c t l y . I f t h e b l o c k h a s j u s t b e e n a l l o c a t e d b y a l l o c _ s h r , u s e i n i t i a l i z e t o a s s i g n a v a l u e t o a

e l d f o r t h e r s t t i m e :

i n i t i a l i z e ( & F i e l d ( v , n ) , w ) ;

O t h e r w i s e , y o u a r e u p d a t i n g a e l d t h a t p r e v i o u s l y c o n t a i n e d a w e l l - f o r m e d v a l u e ; t h e n , c a l l t h e

m o d i f y f u n c t i o n :

m o d i f y ( & F i e l d ( v , n ) , w ) ;

T o i l l u s t r a t e t h e r u l e s a b o v e , h e r e i s a C f u n c t i o n t h a t b u i l d s a n d r e t u r n s a l i s t c o n t a i n i n g t h e

t w o i n t e g e r s g i v e n a s p a r a m e t e r s :

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 109/218

C h a p t e r 1 2 . I n t e r f a c i n g C w i t h C a m l L i g h t 1 0 7

v a l u e a l l o c _ l i s t _ i n t ( i 1 , i 2 )

i n t i 1 , i 2 ;

{

v a l u e r e s u l t ;

P u s h _ r o o t s ( r , 1 ) ;

r 0 ] = a l l o c ( 2 , 1 ) ; / * A l l o c a t e a c o n s c e l l * /

F i e l d ( r 0 ] , 0 ) = V a l _ i n t ( i 2 ) ; / * c a r = t h e i n t e g e r i 2 * /

F i e l d ( r 0 ] , 1 ) = A t o m ( 0 ) ; / * c d r = t h e e m p t y l i s t ] * /

r e s u l t = a l l o c ( 2 , 1 ) ; / * A l l o c a t e t h e o t h e r c o n s c e l l * /

F i e l d ( r e s u l t , 0 ) = V a l _ i n t ( i 1 ) ; / * c a r = t h e i n t e g e r i 1 * /

F i e l d ( r e s u l t , 1 ) = r 0 ] ; / * c d r = t h e f i r s t c o n s c e l l * /

P o p _ r o o t s ( ) ;

r e t u r n r e s u l t ;

}

T h e \ c o n s " c e l l a l l o c a t e d r s t n e e d s t o s u r v i v e t h e a l l o c a t i o n o f t h e o t h e r c o n s c e l l ; h e n c e , t h e v a l u e

r e t u r n e d b y t h e r s t c a l l t o a l l o c m u s t b e s t o r e d i n a r e g i s t e r e d r o o t . T h e v a l u e r e t u r n e d b y t h e

s e c o n d c a l l t o a l l o c c a n r e s i d e i n t h e u n - r e g i s t e r e d l o c a l v a r i a b l e r e s u l t , s i n c e w e w o n ' t d o a n y

f u r t h e r a l l o c a t i o n i n t h i s f u n c t i o n .

I n t h e e x a m p l e a b o v e , t h e l i s t i s b u i l t b o t t o m - u p . H e r e i s a n a l t e r n a t e w a y , t h a t p r o c e e d s

t o p - d o w n . I t i s l e s s e c i e n t , b u t i l l u s t r a t e s t h e u s e o f m o d i f y .

v a l u e a l l o c _ l i s t _ i n t ( i 1 , i 2 )

i n t i 1 , i 2 ;

{

v a l u e t a i l ;

P u s h _ r o o t s ( r , 1 ) ;

r 0 ] = a l l o c ( 2 , 1 ) ; / * A l l o c a t e a c o n s c e l l * /

F i e l d ( r 0 ] , 0 ) = V a l _ i n t ( i 1 ) ; / * c a r = t h e i n t e g e r i 1 * /

F i e l d ( r 0 ] , 1 ) = V a l _ i n t ( 0 ) ; / * A d u m m y v a l u e

t a i l = a l l o c ( 2 , 1 ) ; / * A l l o c a t e t h e o t h e r c o n s c e l l * /

F i e l d ( t a i l , 0 ) = V a l _ i n t ( i 2 ) ; / * c a r = t h e i n t e g e r i 2 * /

F i e l d ( t a i l , 1 ) = A t o m ( 0 ) ; / * c d r = t h e e m p t y l i s t ] * /

m o d i f y ( & F i e l d ( r 0 ] , 1 ) , t a i l ) ; / * c d r o f t h e r e s u l t = t a i l * /

P o p _ r o o t s ( ) ;

r e t u r n r 0 ] ;

}

I t w o u l d b e i n c o r r e c t t o p e r f o r m F i e l d ( r 0 ] , 1 ) = t a i l d i r e c t l y , b e c a u s e t h e a l l o c a t i o n o f t a i l

h a s t a k e n p l a c e s i n c e r 0 ] w a s a l l o c a t e d .

1 2 . 6 A c o m p l e t e e x a m p l e

T h i s s e c t i o n o u t l i n e s h o w t h e f u n c t i o n s f r o m t h e U n i x c u r s e s l i b r a r y c a n b e m a d e a v a i l a b l e t o C a m l

L i g h t p r o g r a m s . F i r s t o f a l l , h e r e i s t h e i n t e r f a c e c u r s e s . m l i t h a t d e c l a r e s t h e c u r s e s p r i m i t i v e s

a n d d a t a t y p e s :

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 110/218

1 0 8

t y p e w i n d o w ; ; ( * T h e t y p e " w i n d o w " r e m a i n s a b s t r a c t * )

v a l u e i n i t s c r : u n i t - > w i n d o w = 1 " c u r s e s _ i n i t s c r "

a n d e n d w i n : u n i t - > u n i t = 1 " c u r s e s _ e n d w i n "

a n d r e f r e s h : u n i t - > u n i t = 1 " c u r s e s _ r e f r e s h "

a n d w r e f r e s h : w i n d o w - > u n i t = 1 " c u r s e s _ w r e f r e s h "

a n d n e w w i n : i n t - > i n t - > i n t - > i n t - > w i n d o w = 4 " c u r s e s _ n e w w i n "

a n d m v w i n : w i n d o w - > i n t - > i n t - > u n i t = 3 " c u r s e s _ m v w i n "

a n d a d d c h : c h a r - > u n i t = 1 " c u r s e s _ a d d c h "

a n d m v w a d d c h : w i n d o w - > i n t - > i n t - > c h a r - > u n i t = 4 " c u r s e s _ m v w a d d c h "

a n d a d d s t r : s t r i n g - > u n i t = 1 " c u r s e s _ a d d s t r "

a n d m v w a d d s t r : w i n d o w - > i n t - > i n t - > s t r i n g - > u n i t = 4 " c u r s e s _ m v w a d d s t r "

; ; ( * l o t s m o r e o m i t t e d * )

T o c o m p i l e t h i s i n t e r f a c e :

c a m l c - c c u r s e s . m l i

T o i m p l e m e n t t h e s e f u n c t i o n s , w e j u s t h a v e t o p r o v i d e t h e s t u b c o d e ; t h e c o r e f u n c t i o n s a r e

a l r e a d y i m p l e m e n t e d i n t h e c u r s e s l i b r a r y . T h e s t u b c o d e l e , c u r s e s . o , l o o k s l i k e :

# i n c l u d e < c u r s e s . h >

# i n c l u d e < m l v a l u e s . h >

v a l u e c u r s e s _ i n i t s c r ( u n i t )

v a l u e u n i t ;

{

r e t u r n ( v a l u e ) i n i t s c r ( ) ; / * O K t o c o e r c e d i r e c t l y f r o m W I N D O W * t o v a l u e

s i n c e t h a t ' s a b l o c k c r e a t e d b y m a l l o c ( ) * /

}

v a l u e c u r s e s _ w r e f r e s h ( w i n )

v a l u e w i n ;

{

w r e f r e s h ( ( W I N D O W * ) w i n ) ;

r e t u r n V a l _ u n i t ;

}

v a l u e c u r s e s _ n e w w i n ( n l i n e s , n c o l s , x 0 , y 0 )

v a l u e n l i n e s , n c o l s , x 0 , y 0 ;

{

r e t u r n ( v a l u e ) n e w w i n ( I n t _ v a l ( n l i n e s ) , I n t _ v a l ( n c o l s ) ,

I n t _ v a l ( x 0 ) , I n t _ v a l ( y 0 ) ) ;

}

v a l u e c u r s e s _ a d d c h ( c )

v a l u e c ;

{

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 111/218

C h a p t e r 1 2 . I n t e r f a c i n g C w i t h C a m l L i g h t 1 0 9

a d d c h ( I n t _ v a l ( c ) ) ; / * C h a r a c t e r s a r e e n c o d e d l i k e i n t e g e r s * /

r e t u r n V a l _ u n i t ;

}

v a l u e c u r s e s _ a d d s t r ( s )

v a l u e s ;

{

a d d s t r ( S t r i n g _ v a l ( s ) ) ;

r e t u r n V a l _ u n i t ;

}

/ * T h i s g o e s o n f o r p a g e s . * /

( A c t u a l l y , i t w o u l d b e b e t t e r t o c r e a t e a l i b r a r y f o r t h e s t u b c o d e , w i t h e a c h s t u b c o d e f u n c t i o n

i n a s e p a r a t e l e , s o t h a t l i n k i n g w o u l d p i c k o n l y t h o s e f u n c t i o n s f r o m t h e c u r s e s l i b r a r y t h a t a r e

a c t u a l l y u s e d . )

T h e l e c u r s e s . c c a n b e c o m p i l e d w i t h :

c c - c - I / u s r / l o c a l / l i b / c a m l - l i g h t c u r s e s . c

o r , e v e n s i m p l e r ,

c a m l c - c c u r s e s . c

( W h e n p a s s e d a . c l e , t h e c a m l c c o m m a n d s i m p l y c a l l s c c o n t h a t l e , w i t h t h e r i g h t - I o p t i o n . )

N o w , h e r e i s a s a m p l e C a m l L i g h t p r o g r a m t e s t . m l t h a t u s e s t h e c u r s e s m o d u l e :

# o p e n " c u r s e s " ; ;

l e t m a i n _ w i n d o w = i n i t s c r ( ) i n

l e t s m a l l _ w i n d o w = n e w w i n 1 0 5 2 0 1 0 i n

m v w a d d s t r m a i n _ w i n d o w 1 0 2 " H e l l o " ;

m v w a d d s t r s m a l l _ w i n d o w 4 3 " w o r l d " ;

r e f r e s h ( ) ;

f o r i = 1 t o 1 0 0 0 0 0 d o ( ) d o n e ;

e n d w i n ( )

; ;

T o c o m p i l e t h i s p r o g r a m , r u n :

c a m l c - c t e s t . m l

F i n a l l y , t o l i n k e v e r y t h i n g t o g e t h e r :

c a m l c - c u s t o m - o t e s t t e s t . z o c u r s e s . o - l c u r s e s

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 112/218

1 1 0

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 113/218

P a r t I V

T h e C a m l L i g h t l i b r a r y

1 1 1

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 114/218

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 115/218

C h a p t e r 1 3

T h e c o r e l i b r a r y

T h i s c h a p t e r d e s c r i b e s t h e f u n c t i o n s p r o v i d e d b y t h e C a m l L i g h t c o r e l i b r a r y . T h i s l i b r a r y i s s p e c i a l

i n t w o w a y s :

I t i s a u t o m a t i c a l l y l i n k e d w i t h t h e u s e r ' s o b j e c t c o d e l e s b y t h e c a m l c c o m m a n d ( c h a p t e r 4 ) .

H e n c e , t h e g l o b a l s d e n e d b y t h e s e l i b r a r i e s c a n b e u s e d i n s t a n d a l o n e p r o g r a m s w i t h o u t

h a v i n g t o a d d a n y . z o l e o n t h e c o m m a n d l i n e f o r t h e l i n k i n g p h a s e . S i m i l a r l y , i n i n t e r a c t i v e

u s e , t h e s e g l o b a l s c a n b e u s e d i n t o p l e v e l p h r a s e s w i t h o u t h a v i n g t o l o a d a n y . z o l e i n

m e m o r y .

T h e i n t e r f a c e s f o r t h e m o d u l e s b e l o w a r e a u t o m a t i c a l l y \ o p e n e d " w h e n a c o m p i l a t i o n s t a r t s ,

o r w h e n t h e t o p l e v e l s y s t e m i s l a u n c h e d . H e n c e , i t i s p o s s i b l e t o u s e u n q u a l i e d i d e n t i e r s t o

r e f e r t o t h e f u n c t i o n s p r o v i d e d b y t h e s e m o d u l e s , w i t h o u t a d d i n g # o p e n d i r e c t i v e s . A c t u a l l y ,

t h e l i s t o f a u t o m a t i c a l l y o p e n e d m o d u l e s d e p e n d o n t h e - O o p t i o n g i v e n t o t h e c o m p i l e r o r

t o t h e t o p l e v e l s y s t e m :

- O o p t i o n O p e n e d m o d u l e s ( r e v e r s e o p e n i n g o r d e r )

- O c a u t i o u s ( d e f a u l t ) i o , e q , i n t , f l o a t , r e f , p a i r , l i s t , v e c t , c h a r ,

s t r i n g , b o o l , e x c , s t r e a m , b u i l t i n

- O f a s t i o , e q , i n t , f l o a t , r e f , p a i r , l i s t , f v e c t ,

f c h a r , f s t r i n g , b o o l , e x c , s t r e a m , b u i l t i n

- O n o n e b u i l t i n

C o n v e n t i o n s

F o r e a s y r e f e r e n c e , t h e m o d u l e s a r e l i s t e d b e l o w i n a l p h a b e t i c a l o r d e r o f m o d u l e n a m e s . F o r e a c h

m o d u l e , t h e d e c l a r a t i o n s f r o m i t s i n t e r f a c e l e a r e p r i n t e d o n e b y o n e i n t y p e w r i t e r f o n t , f o l l o w e d

b y a s h o r t c o m m e n t . A l l m o d u l e s a n d t h e i d e n t i e r s t h e y e x p o r t a r e i n d e x e d a t t h e e n d o f t h i s

r e p o r t .

1 3 . 1 b o o l : b o o l e a n o p e r a t i o n s

v a l u e p r e f i x & : b o o l - > b o o l - > b o o l

1 1 3

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 116/218

1 1 4

v a l u e p r e f i x & & : b o o l - > b o o l - > b o o l

v a l u e p r e f i x o r : b o o l - > b o o l - > b o o l

v a l u e p r e f i x | | : b o o l - > b o o l - > b o o l

T h e b o o l e a n a n d i s w r i t t e n e 1 & e 2 o r e 1 & & e 2 . T h e b o o l e a n o r i s w r i t t e n e 1 o r e 2 o r

e 1 | | e 2 . B o t h c o n s t r u c t s a r e s e q u e n t i a l , l e f t - t o - r i g h t : e 2 i s e v a l u a t e d o n l y i f n e e d e d .

A c t u a l l y , e 1 & e 2 i s e q u i v a l e n t t o i f e 1 t h e n e 2 e l s e f a l s e , a n d e 1 o r e 2 i s e q u i v a l e n t

t o i f e 1 t h e n t r u e e l s e e 2 .

v a l u e p r e f i x n o t : b o o l - > b o o l

T h e b o o l e a n n e g a t i o n .

v a l u e s t r i n g _ o f _ b o o l : b o o l - > s t r i n g

R e t u r n a s t r i n g r e p r e s e n t i n g t h e g i v e n b o o l e a n .

1 3 . 2 b u i l t i n : b a s e t y p e s a n d c o n s t r u c t o r s

T h i s m o d u l e d e n e s s o m e t y p e s a n d e x c e p t i o n s f o r w h i c h t h e l a n g u a g e p r o v i d e s s p e c i a l

s y n t a x , a n d a r e t h e r e f o r e t r e a t e d s p e c i a l l y b y t h e c o m p i l e r .

t y p e i n t

t y p e f l o a t

t y p e s t r i n g

t y p e c h a r

T h e t y p e s o f i n t e g e r s , o a t i n g - p o i n t n u m b e r s , c h a r a c t e r s t r i n g s , a n d c h a r a c t e r s , r e s p e c t i v e l y .

t y p e e x n

T h e t y p e o f e x c e p t i o n v a l u e s .

t y p e b o o l = f a l s e | t r u e

T h e t y p e o f b o o l e a n v a l u e s .

t y p e ' a v e c t

T h e t y p e o f a r r a y s w h o s e e l e m e n t s h a v e t y p e ' a .

t y p e u n i t = ( )

T h e t y p e o f t h e u n i t v a l u e .

t y p e ' a l i s t = ] | p r e f i x : : o f ' a * ' a l i s t

T h e t y p e o f l i s t s .

t y p e ' a o p t i o n = N o n e | S o m e o f ' a

T h e t y p e o f o p t i o n a l v a l u e s .

e x c e p t i o n M a t c h _ f a i l u r e o f s t r i n g * i n t * i n t

T h e e x c e p t i o n r a i s e d w h e n a p a t t e r n - m a t c h i n g f a i l s . T h e a r g u m e n t i n d i c a t e s t h e p o s i t i o n i n

t h e s o u r c e c o d e o f t h e p a t t e r n - m a t c h i n g ( s o u r c e l e n a m e , p o s i t i o n o f t h e r s t c h a r a c t e r o f

t h e m a t c h i n g , p o s i t i o n o f t h e l a s t c h a r a c t e r .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 117/218

C h a p t e r 1 3 . T h e c o r e l i b r a r y 1 1 5

1 3 . 3 c h a r : c h a r a c t e r o p e r a t i o n s

v a l u e i n t _ o f _ c h a r : c h a r - > i n t

R e t u r n t h e A S C I I c o d e o f t h e a r g u m e n t .

v a l u e c h a r _ o f _ i n t : i n t - > c h a r

R e t u r n t h e c h a r a c t e r w i t h t h e g i v e n A S C I I c o d e . R a i s e I n v a l i d _ a r g u m e n t " c h a r _ o f _ i n t "

i f t h e a r g u m e n t i s o u t s i d e t h e r a n g e 0 { 2 5 5 .

v a l u e s t r i n g _ o f _ c h a r : c h a r - > s t r i n g

R e t u r n a s t r i n g r e p r e s e n t i n g t h e g i v e n c h a r a c t e r .

v a l u e c h a r _ f o r _ r e a d : c h a r - > s t r i n g

R e t u r n a s t r i n g r e p r e s e n t i n g t h e g i v e n c h a r a c t e r , w i t h s p e c i a l c h a r a c t e r s e s c a p e d f o l l o w i n g

t h e l e x i c a l c o n v e n t i o n s o f C a m l L i g h t .

1 3 . 4 e q : g e n e r i c c o m p a r i s o n s

v a l u e p r e f i x = : ' a - > ' a - > b o o l

e 1 = e 2 t e s t s f o r s t r u c t u r a l e q u a l i t y o f e 1 a n d e 2 . M u t a b l e s t r u c t u r e s ( e . g . r e f e r e n c e s a n d

a r r a y s ) a r e e q u a l i f a n d o n l y i f t h e i r c u r r e n t c o n t e n t s a r e s t r u c t u r a l l y e q u a l , e v e n i f t h e t w o

m u t a b l e o b j e c t s a r e n o t t h e s a m e p h y s i c a l o b j e c t . E q u a l i t y b e t w e e n f u n c t i o n a l v a l u e s r a i s e s

I n v a l i d _ a r g u m e n t . E q u a l i t y b e t w e e n c y c l i c d a t a s t r u c t u r e s m a y n o t t e r m i n a t e .

v a l u e p r e f i x < > : ' a - > ' a - > b o o l

N e g a t i o n o f p r e f i x = .

v a l u e p r e f i x < : ' a - > ' a - > b o o l

v a l u e p r e f i x < = : ' a - > ' a - > b o o l

v a l u e p r e f i x > : ' a - > ' a - > b o o l

v a l u e p r e f i x > = : ' a - > ' a - > b o o l

S t r u c t u r a l o r d e r i n g f u n c t i o n s . T h e s e f u n c t i o n s c o i n c i d e w i t h t h e u s u a l o r d e r i n g s o v e r

i n t e g e r , s t r i n g a n d o a t i n g - p o i n t n u m b e r s , a n d e x t e n d t h e m t o a t o t a l o r d e r i n g o v e r a l l

t y p e s . T h e o r d e r i n g i s c o m p a t i b l e w i t h p r e f i x = . A s i n t h e c a s e o f p r e f i x = , m u t a b l e

s t r u c t u r e s a r e c o m p a r e d b y c o n t e n t s . C o m p a r i s o n b e t w e e n f u n c t i o n a l v a l u e s r a i s e s

I n v a l i d _ a r g u m e n t . C o m p a r i s o n b e t w e e n c y c l i c s t r u c t u r e s m a y n o t t e r m i n a t e .

v a l u e c o m p a r e : ' a - > ' a - > i n t

c o m p a r e x y r e t u r n s 0 i f x = y , a n e g a t i v e i n t e g e r i f x < y , a n d a p o s i t i v e i n t e g e r i f x > y . T h e

s a m e r e s t r i c t i o n s a s f o r = a p p l y . c o m p a r e c a n b e u s e d a s t h e c o m p a r i s o n f u n c t i o n r e q u i r e d

b y t h e s e t a n d m a p m o d u l e s .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 118/218

1 1 6

v a l u e m i n : ' a - > ' a - > ' a

R e t u r n t h e s m a l l e r o f t h e t w o a r g u m e n t s .

v a l u e m a x : ' a - > ' a - > ' a

R e t u r n t h e g r e a t e r o f t h e t w o a r g u m e n t s .

v a l u e p r e f i x = = : ' a - > ' a - > b o o l

e 1 = = e 2 t e s t s f o r p h y s i c a l e q u a l i t y o f e 1 a n d e 2 . O n i n t e g e r s a n d c h a r a c t e r s , i t i s t h e s a m e

a s s t r u c t u r a l e q u a l i t y . O n m u t a b l e s t r u c t u r e s , e 1 = = e 2 i s t r u e i f a n d o n l y i f p h y s i c a l

m o d i c a t i o n o f e 1 a l s o a e c t s e 2 . O n n o n - m u t a b l e s t r u c t u r e s , t h e b e h a v i o r o f p r e f i x = = i s

i m p l e m e n t a t i o n - d e p e n d e n t , e x c e p t t h a t e 1 = = e 2 i m p l i e s e 1 = e 2 .

v a l u e p r e f i x ! = : ' a - > ' a - > b o o l

N e g a t i o n o f p r e f i x = = .

1 3 . 5 e x c : e x c e p t i o n s

v a l u e r a i s e : e x n - > ' a

R a i s e t h e g i v e n e x c e p t i o n v a l u e .

A f e w g e n e r a l - p u r p o s e p r e d e n e d e x c e p t i o n s .

e x c e p t i o n O u t _ o f _ m e m o r y

R a i s e d b y t h e g a r b a g e c o l l e c t o r , w h e n t h e r e i s i n s u c i e n t m e m o r y t o c o m p l e t e t h e

c o m p u t a t i o n .

e x c e p t i o n I n v a l i d _ a r g u m e n t o f s t r i n g

R a i s e d b y l i b r a r y f u n c t i o n s t o s i g n a l t h a t t h e g i v e n a r g u m e n t s d o n o t m a k e s e n s e .

e x c e p t i o n F a i l u r e o f s t r i n g

R a i s e d b y l i b r a r y f u n c t i o n s t o s i g n a l t h a t t h e y a r e u n d e n e d o n t h e g i v e n a r g u m e n t s .

e x c e p t i o n N o t _ f o u n d

R a i s e d b y s e a r c h f u n c t i o n s w h e n t h e d e s i r e d o b j e c t c o u l d n o t b e f o u n d .

e x c e p t i o n E x i t

T h i s e x c e p t i o n i s n o t r a i s e d b y a n y l i b r a r y f u n c t i o n . I t i s p r o v i d e d f o r u s e i n y o u r p r o g r a m s .

v a l u e f a i l w i t h : s t r i n g - > ' a

R a i s e e x c e p t i o n F a i l u r e w i t h t h e g i v e n s t r i n g .

v a l u e i n v a l i d _ a r g : s t r i n g - > ' a

R a i s e e x c e p t i o n I n v a l i d _ a r g u m e n t w i t h t h e g i v e n s t r i n g .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 119/218

C h a p t e r 1 3 . T h e c o r e l i b r a r y 1 1 7

1 3 . 6 f c h a r : c h a r a c t e r o p e r a t i o n s , w i t h o u t s a n i t y c h e c k s

T h i s m o d u l e i m p l e m e n t s t h e s a m e f u n c t i o n s a s t h e c h a r m o d u l e , b u t d o e s n o t p e r f o r m

b o u n d c h e c k s o n t h e a r g u m e n t s o f t h e f u n c t i o n s . T h e f u n c t i o n s a r e t h e r e f o r e f a s t e r t h a n

t h o s e i n t h e c h a r m o d u l e , b u t c a l l i n g t h e s e f u n c t i o n s w i t h i n c o r r e c t p a r a m e t e r s ( t h a t i s ,

p a r a m e t e r s t h a t w o u l d c a u s e t h e I n v a l i d _ a r g u m e n t e x c e p t i o n t o b e r a i s e d b y t h e

c o r r e s p o n d i n g f u n c t i o n s i n t h e c h a r m o d u l e ) c a n c r a s h t h e p r o g r a m .

1 3 . 7 f l o a t : o p e r a t i o n s o n o a t i n g - p o i n t n u m b e r s

v a l u e i n t _ o f _ f l o a t : f l o a t - > i n t

T r u n c a t e t h e g i v e n o a t t o a n i n t e g e r v a l u e . T h e r e s u l t i s u n s p e c i e d i f i t f a l l s o u t s i d e t h e

r a n g e o f r e p r e s e n t a b l e i n t e g e r s .

v a l u e f l o a t _ o f _ i n t : i n t - > f l o a t

C o n v e r t a n i n t e g e r t o o a t i n g - p o i n t .

v a l u e m i n u s : f l o a t - > f l o a t

v a l u e m i n u s _ f l o a t : f l o a t - > f l o a t

U n a r y n e g a t i o n .

v a l u e p r e f i x + : f l o a t - > f l o a t - > f l o a t

v a l u e p r e f i x + . : f l o a t - > f l o a t - > f l o a t

v a l u e a d d _ f l o a t : f l o a t - > f l o a t - > f l o a t

A d d i t i o n .

v a l u e p r e f i x - : f l o a t - > f l o a t - > f l o a t

v a l u e p r e f i x - . : f l o a t - > f l o a t - > f l o a t

v a l u e s u b _ f l o a t : f l o a t - > f l o a t - > f l o a t

S u b t r a c t i o n .

v a l u e p r e f i x * : f l o a t - > f l o a t - > f l o a t

v a l u e p r e f i x * . : f l o a t - > f l o a t - > f l o a t

v a l u e m u l t _ f l o a t : f l o a t - > f l o a t - > f l o a t

P r o d u c t .

v a l u e p r e f i x / : f l o a t - > f l o a t - > f l o a t

v a l u e p r e f i x / . : f l o a t - > f l o a t - > f l o a t

v a l u e d i v _ f l o a t : f l o a t - > f l o a t - > f l o a t

D i v i s i o n .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 120/218

1 1 8

v a l u e p r e f i x * * : f l o a t - > f l o a t - > f l o a t

v a l u e p r e f i x * * . : f l o a t - > f l o a t - > f l o a t

v a l u e p o w e r : f l o a t - > f l o a t - > f l o a t

E x p o n e n t i a t i o n .

v a l u e e q _ f l o a t : f l o a t - > f l o a t - > b o o l

v a l u e p r e f i x = . : f l o a t - > f l o a t - > b o o l

F l o a t i n g - p o i n t e q u a l i t y . E q u i v a l e n t t o g e n e r i c e q u a l i t y , j u s t f a s t e r .

v a l u e n e q _ f l o a t : f l o a t - > f l o a t - > b o o l

v a l u e p r e f i x < > . : f l o a t - > f l o a t - > b o o l

N e g a t i o n o f e q _ f l o a t .

v a l u e p r e f i x < . : f l o a t - > f l o a t - > b o o l

v a l u e l t _ f l o a t : f l o a t - > f l o a t - > b o o l

v a l u e p r e f i x > . : f l o a t - > f l o a t - > b o o l

v a l u e g t _ f l o a t : f l o a t - > f l o a t - > b o o l

v a l u e p r e f i x < = . : f l o a t - > f l o a t - > b o o l

v a l u e l e _ f l o a t : f l o a t - > f l o a t - > b o o l

v a l u e p r e f i x > = . : f l o a t - > f l o a t - > b o o l

v a l u e g e _ f l o a t : f l o a t - > f l o a t - > b o o l

U s u a l c o m p a r i s o n s b e t w e e n o a t i n g - p o i n t n u m b e r s .

v a l u e a c o s : f l o a t - > f l o a t

v a l u e a s i n : f l o a t - > f l o a t

v a l u e a t a n : f l o a t - > f l o a t

v a l u e a t a n 2 : f l o a t - > f l o a t - > f l o a t

v a l u e c o s : f l o a t - > f l o a t

v a l u e c o s h : f l o a t - > f l o a t

v a l u e e x p : f l o a t - > f l o a t

v a l u e l o g : f l o a t - > f l o a t

v a l u e l o g 1 0 : f l o a t - > f l o a t

v a l u e s i n : f l o a t - > f l o a t

v a l u e s i n h : f l o a t - > f l o a t

v a l u e s q r t : f l o a t - > f l o a t

v a l u e t a n : f l o a t - > f l o a t

v a l u e t a n h : f l o a t - > f l o a t

U s u a l t r a n s c e n d e n t a l f u n c t i o n s o n o a t i n g - p o i n t n u m b e r s .

v a l u e c e i l : f l o a t - > f l o a t

v a l u e f l o o r : f l o a t - > f l o a t

R o u n d t h e g i v e n o a t t o a n i n t e g e r v a l u e . f l o o r f r e t u r n s t h e g r e a t e s t i n t e g e r v a l u e l e s s

t h a n o r e q u a l t o f . c e i l f r e t u r n s t h e l e a s t i n t e g e r v a l u e g r e a t e r t h a n o r e q u a l t o f .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 121/218

C h a p t e r 1 3 . T h e c o r e l i b r a r y 1 1 9

v a l u e a b s _ f l o a t : f l o a t - > f l o a t

R e t u r n t h e a b s o l u t e v a l u e o f t h e a r g u m e n t .

v a l u e m o d _ f l o a t : f l o a t - > f l o a t - > f l o a t

f m o d a b r e t u r n s t h e r e m a i n d e r o f a w i t h r e s p e c t t o b .

v a l u e f r e x p : f l o a t - > f l o a t * i n t

f r e x p f r e t u r n s t h e p a i r o f t h e s i g n i c a n t a n d t h e e x p o n e n t o f f ( w h e n f i s z e r o , t h e

s i g n i c a n t x a n d t h e e x p o n e n t n o f f a r e e q u a l t o z e r o ; w h e n f i s n o n - z e r o , t h e y a r e d e n e d

b y f = x * . 2 * * n ) .

v a l u e l d e x p : f l o a t - > i n t - > f l o a t

l d e x p x n r e t u r n s x * . 2 * * n .

v a l u e m o d f : f l o a t - > f l o a t * f l o a t

m o d f f r e t u r n s t h e p a i r o f t h e f r a c t i o n a l a n d i n t e g r a l p a r t o f f .

v a l u e s t r i n g _ o f _ f l o a t : f l o a t - > s t r i n g

C o n v e r t t h e g i v e n o a t t o i t s d e c i m a l r e p r e s e n t a t i o n .

v a l u e f l o a t _ o f _ s t r i n g : s t r i n g - > f l o a t

C o n v e r t t h e g i v e n s t r i n g t o a o a t , i n d e c i m a l . T h e r e s u l t i s u n s p e c i e d i f t h e g i v e n s t r i n g i s

n o t a v a l i d r e p r e s e n t a t i o n o f a o a t .

1 3 . 8 f s t r i n g : s t r i n g o p e r a t i o n s , w i t h o u t s a n i t y c h e c k s

T h i s m o d u l e i m p l e m e n t s t h e s a m e f u n c t i o n s a s t h e s t r i n g m o d u l e , b u t d o e s n o t p e r f o r m

b o u n d c h e c k s o n t h e a r g u m e n t s o f t h e f u n c t i o n s . T h e f u n c t i o n s a r e t h e r e f o r e f a s t e r t h a n

t h o s e i n t h e s t r i n g m o d u l e , b u t c a l l i n g t h e s e f u n c t i o n s w i t h i n c o r r e c t p a r a m e t e r s ( t h a t i s ,

p a r a m e t e r s t h a t w o u l d c a u s e t h e I n v a l i d _ a r g u m e n t e x c e p t i o n t o b e r a i s e d b y t h e

c o r r e s p o n d i n g f u n c t i o n s i n t h e s t r i n g m o d u l e ) c a n c r a s h t h e p r o g r a m .

1 3 . 9 f v e c t : o p e r a t i o n s o n v e c t o r s , w i t h o u t s a n i t y c h e c k s

T h i s m o d u l e i m p l e m e n t s t h e s a m e f u n c t i o n s a s t h e v e c t m o d u l e , b u t d o e s n o t p e r f o r m

b o u n d c h e c k s o n t h e a r g u m e n t s o f t h e f u n c t i o n s . T h e f u n c t i o n s a r e t h e r e f o r e f a s t e r t h a n

t h o s e i n t h e v e c t m o d u l e , b u t c a l l i n g t h e s e f u n c t i o n s w i t h i n c o r r e c t p a r a m e t e r s ( t h a t i s ,

p a r a m e t e r s t h a t w o u l d c a u s e t h e I n v a l i d _ a r g u m e n t e x c e p t i o n t o b e r a i s e d b y t h e

c o r r e s p o n d i n g f u n c t i o n s i n t h e v e c t m o d u l e ) c a n c r a s h t h e p r o g r a m .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 122/218

1 2 0

1 3 . 1 0 i n t : o p e r a t i o n s o n i n t e g e r s

I n t e g e r s a r e 3 1 b i t s w i d e ( o r 6 3 b i t s o n 6 4 - b i t p r o c e s s o r s ) . A l l o p e r a t i o n s a r e t a k e n m o d u l o

2

3 1

( o r 2

6 3

) . T h e y d o n o t f a i l o n o v e r o w .

e x c e p t i o n D i v i s i o n _ b y _ z e r o

v a l u e m i n u s : i n t - > i n t

v a l u e m i n u s _ i n t : i n t - > i n t

U n a r y n e g a t i o n . Y o u c a n w r i t e - e i n s t e a d o f m i n u s e .

v a l u e s u c c : i n t - > i n t

s u c c x i s x + 1 .

v a l u e p r e d : i n t - > i n t

p r e d x i s x - 1 .

v a l u e p r e f i x + : i n t - > i n t - > i n t

v a l u e a d d _ i n t : i n t - > i n t - > i n t

A d d i t i o n .

v a l u e p r e f i x - : i n t - > i n t - > i n t

v a l u e s u b _ i n t : i n t - > i n t - > i n t

S u b t r a c t i o n .

v a l u e p r e f i x * : i n t - > i n t - > i n t

v a l u e m u l t _ i n t : i n t - > i n t - > i n t

M u l t i p l i c a t i o n .

v a l u e p r e f i x / : i n t - > i n t - > i n t

v a l u e d i v _ i n t : i n t - > i n t - > i n t

v a l u e p r e f i x q u o : i n t - > i n t - > i n t

I n t e g e r d i v i s i o n . R a i s e D i v i s i o n _ b y _ z e r o i f t h e s e c o n d a r g u m e n t i s 0 . G i v e u n p r e d i c t a b l e

r e s u l t s i f e i t h e r a r g u m e n t i s n e g a t i v e .

v a l u e p r e f i x m o d : i n t - > i n t - > i n t

R e m a i n d e r . R a i s e D i v i s i o n _ b y _ z e r o i f t h e s e c o n d a r g u m e n t i s 0 . G i v e u n p r e d i c t a b l e

r e s u l t s i f e i t h e r a r g u m e n t i s n e g a t i v e .

v a l u e e q _ i n t : i n t - > i n t - > b o o l

I n t e g e r e q u a l i t y . E q u i v a l e n t t o g e n e r i c e q u a l i t y , j u s t f a s t e r .

v a l u e n e q _ i n t : i n t - > i n t - > b o o l

N e g a t i o n o f e q _ i n t .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 123/218

C h a p t e r 1 3 . T h e c o r e l i b r a r y 1 2 1

v a l u e l t _ i n t : i n t - > i n t - > b o o l

v a l u e g t _ i n t : i n t - > i n t - > b o o l

v a l u e l e _ i n t : i n t - > i n t - > b o o l

v a l u e g e _ i n t : i n t - > i n t - > b o o l

U s u a l c o m p a r i s o n s b e t w e e n i n t e g e r s .

v a l u e a b s : i n t - > i n t

R e t u r n t h e a b s o l u t e v a l u e o f t h e a r g u m e n t .

v a l u e m a x _ i n t : i n t

v a l u e m i n _ i n t : i n t

T h e g r e a t e s t a n d s m a l l e s t i n t e g e r v a l u e s .

B i t w i s e o p e r a t i o n s

v a l u e p r e f i x l a n d : i n t - > i n t - > i n t

B i t w i s e l o g i c a l a n d .

v a l u e p r e f i x l o r : i n t - > i n t - > i n t

B i t w i s e l o g i c a l o r .

v a l u e p r e f i x l x o r : i n t - > i n t - > i n t

B i t w i s e l o g i c a l e x c l u s i v e o r .

v a l u e l n o t : i n t - > i n t

B i t w i s e c o m p l e m e n t

v a l u e p r e f i x l s l : i n t - > i n t - > i n t

v a l u e l s h i f t _ l e f t : i n t - > i n t - > i n t

n l s l m , o r e q u i v a l e n t l y l s h i f t _ l e f t n m , s h i f t s n t o t h e l e f t b y m b i t s .

v a l u e p r e f i x l s r : i n t - > i n t - > i n t

n l s r m s h i f t s n t o t h e r i g h t b y m b i t s . T h i s i s a l o g i c a l s h i f t : z e r o e s a r e i n s e r t e d r e g a r d l e s s

o f s i g n .

v a l u e p r e f i x a s r : i n t - > i n t - > i n t

v a l u e l s h i f t _ r i g h t : i n t - > i n t - > i n t

n a s r m , o r e q u i v a l e n t l y l s h i f t _ r i g h t n m , s h i f t s n t o t h e r i g h t b y m b i t s . T h i s i s a n

a r i t h m e t i c s h i f t : t h e s i g n b i t i s r e p l i c a t e d .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 124/218

1 2 2

C o n v e r s i o n f u n c t i o n s

v a l u e s t r i n g _ o f _ i n t : i n t - > s t r i n g

C o n v e r t t h e g i v e n i n t e g e r t o i t s d e c i m a l r e p r e s e n t a t i o n .

v a l u e i n t _ o f _ s t r i n g : s t r i n g - > i n t

C o n v e r t t h e g i v e n s t r i n g t o a n i n t e g e r , i n d e c i m a l ( b y d e f a u l t ) o r i n h e x a d e c i m a l , o c t a l o r

b i n a r y i f t h e s t r i n g b e g i n s w i t h 0 x , 0 o o r 0 b . R a i s e F a i l u r e " i n t _ o f _ s t r i n g " i f t h e g i v e n

s t r i n g i s n o t a v a l i d r e p r e s e n t a t i o n o f a n i n t e g e r .

1 3 . 1 1 i o : b u e r e d i n p u t a n d o u t p u t

t y p e i n _ c h a n n e l

t y p e o u t _ c h a n n e l

T h e a b s t r a c t t y p e s o f i n p u t c h a n n e l s a n d o u t p u t c h a n n e l s .

e x c e p t i o n E n d _ o f _ f i l e

R a i s e d w h e n a n o p e r a t i o n c a n n o t c o m p l e t e , b e c a u s e t h e e n d o f t h e l e h a s b e e n r e a c h e d .

v a l u e s t d i n : i n _ c h a n n e l

v a l u e s t d _ i n : i n _ c h a n n e l

v a l u e s t d o u t : o u t _ c h a n n e l

v a l u e s t d _ o u t : o u t _ c h a n n e l

v a l u e s t d e r r : o u t _ c h a n n e l

v a l u e s t d _ e r r : o u t _ c h a n n e l

T h e s t a n d a r d i n p u t , s t a n d a r d o u t p u t , a n d s t a n d a r d e r r o r o u t p u t f o r t h e p r o c e s s . s t d _ i n ,

s t d _ o u t a n d s t d _ e r r a r e r e s p e c t i v e l y s y n o n y m o u s w i t h s t d i n , s t d o u t a n d s t d e r r .

v a l u e e x i t : i n t - > ' a

F l u s h a l l p e n d i n g w r i t e s o n s t d _ o u t a n d s t d _ e r r , a n d t e r m i n a t e t h e p r o c e s s , r e t u r n i n g t h e

g i v e n s t a t u s c o d e t o t h e o p e r a t i n g s y s t e m ( u s u a l l y 0 t o i n d i c a t e n o e r r o r s , a n d a s m a l l

p o s i t i v e i n t e g e r t o i n d i c a t e f a i l u r e . ) T h i s f u n c t i o n s h o u l d b e c a l l e d a t t h e e n d o f a l l

s t a n d a l o n e p r o g r a m s t h a t o u t p u t r e s u l t s o n s t d _ o u t o r s t d _ e r r ; o t h e r w i s e , t h e p r o g r a m

m a y a p p e a r t o p r o d u c e n o o u t p u t , o r i t s o u t p u t m a y b e t r u n c a t e d .

O u t p u t f u n c t i o n s o n s t a n d a r d o u t p u t

v a l u e p r i n t _ c h a r : c h a r - > u n i t

P r i n t t h e c h a r a c t e r o n s t a n d a r d o u t p u t .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 125/218

C h a p t e r 1 3 . T h e c o r e l i b r a r y 1 2 3

v a l u e p r i n t _ s t r i n g : s t r i n g - > u n i t

P r i n t t h e s t r i n g o n s t a n d a r d o u t p u t .

v a l u e p r i n t _ i n t : i n t - > u n i t

P r i n t t h e i n t e g e r , i n d e c i m a l , o n s t a n d a r d o u t p u t .

v a l u e p r i n t _ f l o a t : f l o a t - > u n i t

P r i n t t h e o a t i n g - p o i n t n u m b e r , i n d e c i m a l , o n s t a n d a r d o u t p u t .

v a l u e p r i n t _ e n d l i n e : s t r i n g - > u n i t

P r i n t t h e s t r i n g , f o l l o w e d b y a n e w l i n e c h a r a c t e r , o n s t a n d a r d o u t p u t .

v a l u e p r i n t _ n e w l i n e : u n i t - > u n i t

P r i n t a n e w l i n e c h a r a c t e r o n s t a n d a r d o u t p u t , a n d u s h s t a n d a r d o u t p u t . T h i s c a n b e u s e d

t o s i m u l a t e l i n e b u e r i n g o f s t a n d a r d o u t p u t .

O u t p u t f u n c t i o n s o n s t a n d a r d e r r o r

v a l u e p r e r r _ c h a r : c h a r - > u n i t

P r i n t t h e c h a r a c t e r o n s t a n d a r d e r r o r .

v a l u e p r e r r _ s t r i n g : s t r i n g - > u n i t

P r i n t t h e s t r i n g o n s t a n d a r d e r r o r .

v a l u e p r e r r _ i n t : i n t - > u n i t

P r i n t t h e i n t e g e r , i n d e c i m a l , o n s t a n d a r d e r r o r .

v a l u e p r e r r _ f l o a t : f l o a t - > u n i t

P r i n t t h e o a t i n g - p o i n t n u m b e r , i n d e c i m a l , o n s t a n d a r d e r r o r .

v a l u e p r e r r _ e n d l i n e : s t r i n g - > u n i t

P r i n t t h e s t r i n g , f o l l o w e d b y a n e w l i n e c h a r a c t e r o n s t a n d a r d e r r o r a n d u s h s t a n d a r d e r r o r .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 126/218

1 2 4

I n p u t f u n c t i o n s o n s t a n d a r d i n p u t

v a l u e r e a d _ l i n e : u n i t - > s t r i n g

F l u s h s t a n d a r d o u t p u t , t h e n r e a d c h a r a c t e r s f r o m s t a n d a r d i n p u t u n t i l a n e w l i n e c h a r a c t e r

i s e n c o u n t e r e d . R e t u r n t h e s t r i n g o f a l l c h a r a c t e r s r e a d , w i t h o u t t h e n e w l i n e c h a r a c t e r a t

t h e e n d .

v a l u e r e a d _ i n t : u n i t - > i n t

F l u s h s t a n d a r d o u t p u t , t h e n r e a d o n e l i n e f r o m s t a n d a r d i n p u t a n d c o n v e r t i t t o a n i n t e g e r .

R a i s e F a i l u r e " i n t _ o f _ s t r i n g " i f t h e l i n e r e a d i s n o t a v a l i d r e p r e s e n t a t i o n o f a n i n t e g e r .

v a l u e r e a d _ f l o a t : u n i t - > f l o a t

F l u s h s t a n d a r d o u t p u t , t h e n r e a d o n e l i n e f r o m s t a n d a r d i n p u t a n d c o n v e r t i t t o a

o a t i n g - p o i n t n u m b e r . T h e r e s u l t i s u n s p e c i e d i f t h e l i n e r e a d i s n o t a v a l i d r e p r e s e n t a t i o n

o f a o a t i n g - p o i n t n u m b e r .

G e n e r a l o u t p u t f u n c t i o n s

v a l u e o p e n _ o u t : s t r i n g - > o u t _ c h a n n e l

O p e n t h e n a m e d l e f o r w r i t i n g , a n d r e t u r n a n e w o u t p u t c h a n n e l o n t h a t l e , p o s i t i o n n e d

a t t h e b e g i n n i n g o f t h e l e . T h e l e i s t r u n c a t e d t o z e r o l e n g t h i f i t a l r e a d y e x i s t s . I t i s

c r e a t e d i f i t d o e s n o t a l r e a d y e x i s t s . R a i s e s y s _ _ S y s _ e r r o r i f t h e l e c o u l d n o t b e o p e n e d .

v a l u e o p e n _ o u t _ b i n : s t r i n g - > o u t _ c h a n n e l

S a m e a s o p e n _ o u t , b u t t h e l e i s o p e n e d i n b i n a r y m o d e , s o t h a t n o t r a n s l a t i o n t a k e s p l a c e

d u r i n g w r i t e s . O n o p e r a t i n g s y s t e m s t h a t d o n o t d i s t i n g u i s h b e t w e e n t e x t m o d e a n d b i n a r y

m o d e , t h i s f u n c t i o n b e h a v e s l i k e o p e n _ o u t .

v a l u e o p e n _ o u t _ g e n : s y s _ _ o p e n _ f l a g l i s t - > i n t - > s t r i n g - > o u t _ c h a n n e l

o p e n _ o u t _ g e n m o d e r i g h t s f i l e n a m e o p e n s t h e l e n a m e d f i l e n a m e f o r w r i t i n g , a s

a b o v e . T h e e x t r a a r g u m e n t m o d e s p e c i f y t h e o p e n i n g m o d e ( s e e s y s _ _ o p e n ) . T h e e x t r a

a r g u m e n t r i g h t s s p e c i e s t h e l e p e r m i s s i o n s , i n c a s e t h e l e m u s t b e c r e a t e d ( s e e

s y s _ _ o p e n ) . o p e n _ o u t a n d o p e n _ o u t _ b i n a r e s p e c i a l c a s e s o f t h i s f u n c t i o n .

v a l u e o p e n _ d e s c r i p t o r _ o u t : i n t - > o u t _ c h a n n e l

o p e n _ d e s c r i p t o r _ o u t f d r e t u r n s a b u e r e d o u t p u t c h a n n e l w r i t i n g t o t h e l e d e s c r i p t o r

f d . T h e l e d e s c r i p t o r f d m u s t h a v e b e e n p r e v i o u s l y o p e n e d f o r w r i t i n g , e l s e t h e b e h a v i o r i s

u n d e n e d .

v a l u e f l u s h : o u t _ c h a n n e l - > u n i t

F l u s h t h e b u e r a s s o c i a t e d w i t h t h e g i v e n o u t p u t c h a n n e l , p e r f o r m i n g a l l p e n d i n g w r i t e s o n

t h a t c h a n n e l . I n t e r a c t i v e p r o g r a m s m u s t b e c a r e f u l a b o u t u s h i n g s t d _ o u t a n d s t d _ e r r a t

t h e r i g h t t i m e .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 127/218

C h a p t e r 1 3 . T h e c o r e l i b r a r y 1 2 5

v a l u e o u t p u t _ c h a r : o u t _ c h a n n e l - > c h a r - > u n i t

W r i t e t h e c h a r a c t e r o n t h e g i v e n o u t p u t c h a n n e l .

v a l u e o u t p u t _ s t r i n g : o u t _ c h a n n e l - > s t r i n g - > u n i t

W r i t e t h e s t r i n g o n t h e g i v e n o u t p u t c h a n n e l .

v a l u e o u t p u t : o u t _ c h a n n e l - > s t r i n g - > i n t - > i n t - > u n i t

o u t p u t c h a n b u f f o f s l e n w r i t e s l e n c h a r a c t e r s f r o m s t r i n g b u f f , s t a r t i n g a t o s e t o f s ,

t o t h e o u t p u t c h a n n e l c h a n . R a i s e I n v a l i d _ a r g u m e n t " o u t p u t " i f o f s a n d l e n d o n o t

d e s i g n a t e a v a l i d s u b s t r i n g o f b u f f .

v a l u e o u t p u t _ b y t e : o u t _ c h a n n e l - > i n t - > u n i t

W r i t e o n e 8 - b i t i n t e g e r ( a s t h e s i n g l e c h a r a c t e r w i t h t h a t c o d e ) o n t h e g i v e n o u t p u t c h a n n e l .

T h e g i v e n i n t e g e r i s t a k e n m o d u l o 2 5 6 .

v a l u e o u t p u t _ b i n a r y _ i n t : o u t _ c h a n n e l - > i n t - > u n i t

W r i t e o n e i n t e g e r i n b i n a r y f o r m a t o n t h e g i v e n o u t p u t c h a n n e l . T h e o n l y r e l i a b l e w a y t o

r e a d i t b a c k i s t h r o u g h t h e i n p u t _ b i n a r y _ i n t f u n c t i o n . T h e f o r m a t i s c o m p a t i b l e a c r o s s a l l

m a c h i n e s f o r a g i v e n v e r s i o n o f C a m l L i g h t .

v a l u e o u t p u t _ v a l u e : o u t _ c h a n n e l - > ' a - > u n i t

W r i t e t h e r e p r e s e n t a t i o n o f a s t r u c t u r e d v a l u e o f a n y t y p e t o a c h a n n e l . C i r c u l a r i t i e s a n d

s h a r i n g i n s i d e t h e v a l u e a r e d e t e c t e d a n d p r e s e r v e d . T h e o b j e c t c a n b e r e a d b a c k , b y t h e

f u n c t i o n i n p u t _ v a l u e . T h e f o r m a t i s c o m p a t i b l e a c r o s s a l l m a c h i n e s f o r a g i v e n v e r s i o n o f

C a m l L i g h t .

v a l u e o u t p u t _ c o m p a c t _ v a l u e : o u t _ c h a n n e l - > ' a - > u n i t

S a m e a s o u t p u t _ v a l u e , b u t u s e s a d i e r e n t f o r m a t , w h i c h o c c u p i e s l e s s s p a c e o n t h e l e ,

b u t t a k e s m o r e t i m e t o g e n e r a t e a n d r e a d b a c k .

v a l u e s e e k _ o u t : o u t _ c h a n n e l - > i n t - > u n i t

s e e k _ o u t c h a n p o s s e t s t h e c u r r e n t w r i t i n g p o s i t i o n t o p o s f o r c h a n n e l c h a n . T h i s w o r k s

o n l y f o r r e g u l a r l e s . O n l e s o f o t h e r k i n d s ( s u c h a s t e r m i n a l s , p i p e s a n d s o c k e t s ) , t h e

b e h a v i o r i s u n s p e c i e d .

v a l u e p o s _ o u t : o u t _ c h a n n e l - > i n t

R e t u r n t h e c u r r e n t w r i t i n g p o s i t i o n f o r t h e g i v e n c h a n n e l .

v a l u e o u t _ c h a n n e l _ l e n g t h : o u t _ c h a n n e l - > i n t

R e t u r n t h e t o t a l l e n g t h ( n u m b e r o f c h a r a c t e r s ) o f t h e g i v e n c h a n n e l . T h i s w o r k s o n l y f o r

r e g u l a r l e s . O n l e s o f o t h e r k i n d s , t h e r e s u l t i s m e a n i n g l e s s .

v a l u e c l o s e _ o u t : o u t _ c h a n n e l - > u n i t

C l o s e t h e g i v e n c h a n n e l , u s h i n g a l l b u e r e d w r i t e o p e r a t i o n s . T h e b e h a v i o r i s u n s p e c i e d i f

a n y o f t h e f u n c t i o n s a b o v e i s c a l l e d o n a c l o s e d c h a n n e l .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 128/218

1 2 6

G e n e r a l i n p u t f u n c t i o n s

v a l u e o p e n _ i n : s t r i n g - > i n _ c h a n n e l

O p e n t h e n a m e d l e f o r r e a d i n g , a n d r e t u r n a n e w i n p u t c h a n n e l o n t h a t l e , p o s i t i o n n e d a t

t h e b e g i n n i n g o f t h e l e . R a i s e s y s _ _ S y s _ e r r o r i f t h e l e c o u l d n o t b e o p e n e d .

v a l u e o p e n _ i n _ b i n : s t r i n g - > i n _ c h a n n e l

S a m e a s o p e n _ i n , b u t t h e l e i s o p e n e d i n b i n a r y m o d e , s o t h a t n o t r a n s l a t i o n t a k e s p l a c e

d u r i n g r e a d s . O n o p e r a t i n g s y s t e m s t h a t d o n o t d i s t i n g u i s h b e t w e e n t e x t m o d e a n d b i n a r y

m o d e , t h i s f u n c t i o n b e h a v e s l i k e o p e n _ i n .

v a l u e o p e n _ i n _ g e n : s y s _ _ o p e n _ f l a g l i s t - > i n t - > s t r i n g - > i n _ c h a n n e l

o p e n _ i n _ g e n m o d e r i g h t s f i l e n a m e o p e n s t h e l e n a m e d f i l e n a m e f o r r e a d i n g , a s a b o v e .

T h e e x t r a a r g u m e n t s m o d e a n d r i g h t s s p e c i f y t h e o p e n i n g m o d e a n d l e p e r m i s s i o n s ( s e e

s y s _ _ o p e n ) . o p e n _ i n a n d o p e n _ i n _ b i n a r e s p e c i a l c a s e s o f t h i s f u n c t i o n .

v a l u e o p e n _ d e s c r i p t o r _ i n : i n t - > i n _ c h a n n e l

o p e n _ d e s c r i p t o r _ i n f d r e t u r n s a b u e r e d i n p u t c h a n n e l r e a d i n g f r o m t h e l e d e s c r i p t o r

f d . T h e l e d e s c r i p t o r f d m u s t h a v e b e e n p r e v i o u s l y o p e n e d f o r r e a d i n g , e l s e t h e b e h a v i o r i s

u n d e n e d .

v a l u e i n p u t _ c h a r : i n _ c h a n n e l - > c h a r

R e a d o n e c h a r a c t e r f r o m t h e g i v e n i n p u t c h a n n e l . R a i s e E n d _ o f _ f i l e i f t h e r e a r e n o m o r e

c h a r a c t e r s t o r e a d .

v a l u e i n p u t _ l i n e : i n _ c h a n n e l - > s t r i n g

R e a d c h a r a c t e r s f r o m t h e g i v e n i n p u t c h a n n e l , u n t i l a n e w l i n e c h a r a c t e r i s e n c o u n t e r e d .

R e t u r n t h e s t r i n g o f a l l c h a r a c t e r s r e a d , w i t h o u t t h e n e w l i n e c h a r a c t e r a t t h e e n d . R a i s e

E n d _ o f _ f i l e i f t h e e n d o f t h e l e i s r e a c h e d a t t h e b e g i n n i n g o f l i n e .

v a l u e i n p u t : i n _ c h a n n e l - > s t r i n g - > i n t - > i n t - > i n t

i n p u t c h a n b u f f o f s l e n a t t e m p t s t o r e a d l e n c h a r a c t e r s f r o m c h a n n e l c h a n , s t o r i n g

t h e m i n s t r i n g b u f f , s t a r t i n g a t c h a r a c t e r n u m b e r o f s . I t r e t u r n s t h e a c t u a l n u m b e r o f

c h a r a c t e r s r e a d , b e t w e e n 0 a n d l e n ( i n c l u s i v e ) . A r e t u r n v a l u e o f 0 m e a n s t h a t t h e e n d o f

l e w a s r e a c h e d . A r e t u r n v a l u e b e t w e e n 0 a n d l e n e x c l u s i v e m e a n s t h a t n o m o r e c h a r a c t e r s

w e r e a v a i l a b l e a t t h a t t i m e ; i n p u t m u s t b e c a l l e d a g a i n t o r e a d t h e r e m a i n i n g c h a r a c t e r s , i f

d e s i r e d . E x c e p t i o n I n v a l i d _ a r g u m e n t " i n p u t " i s r a i s e d i f o f s a n d l e n d o n o t d e s i g n a t e a

v a l i d s u b s t r i n g o f b u f f .

v a l u e r e a l l y _ i n p u t : i n _ c h a n n e l - > s t r i n g - > i n t - > i n t - > u n i t

r e a l l y _ i n p u t c h a n b u f f o f s l e n r e a d s l e n c h a r a c t e r s f r o m c h a n n e l c h a n , s t o r i n g t h e m

i n s t r i n g b u f f , s t a r t i n g a t c h a r a c t e r n u m b e r o f s . R a i s e E n d _ o f _ f i l e i f t h e e n d o f l e i s

r e a c h e d b e f o r e l e n c h a r a c t e r s h a v e b e e n r e a d . R a i s e I n v a l i d _ a r g u m e n t " r e a l l y _ i n p u t " i f

o f s a n d l e n d o n o t d e s i g n a t e a v a l i d s u b s t r i n g o f b u f f .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 129/218

C h a p t e r 1 3 . T h e c o r e l i b r a r y 1 2 7

v a l u e i n p u t _ b y t e : i n _ c h a n n e l - > i n t

S a m e a s i n p u t _ c h a r , b u t r e t u r n t h e 8 - b i t i n t e g e r r e p r e s e n t i n g t h e c h a r a c t e r . R a i s e

E n d _ o f _ f i l e i f a n e n d o f l e w a s r e a c h e d .

v a l u e i n p u t _ b i n a r y _ i n t : i n _ c h a n n e l - > i n t

R e a d a n i n t e g e r e n c o d e d i n b i n a r y f o r m a t f r o m t h e g i v e n i n p u t c h a n n e l . S e e

o u t p u t _ b i n a r y _ i n t . R a i s e E n d _ o f _ f i l e i f a n e n d o f l e w a s r e a c h e d w h i l e r e a d i n g t h e

i n t e g e r .

v a l u e i n p u t _ v a l u e : i n _ c h a n n e l - > ' a

R e a d t h e r e p r e s e n t a t i o n o f a s t r u c t u r e d v a l u e , a s p r o d u c e d b y o u t p u t _ v a l u e o r

o u t p u t _ c o m p a c t _ v a l u e , a n d r e t u r n t h e c o r r e s p o n d i n g v a l u e . T h i s i s n o t t y p e - s a f e . T h e

t y p e o f t h e r e t u r n e d o b j e c t i s n o t ' a p r o p e r l y s p e a k i n g : t h e r e t u r n e d o b j e c t h a s o n e u n i q u e

t y p e , w h i c h c a n n o t b e d e t e r m i n e d a t c o m p i l e - t i m e . T h e p r o g r a m m e r s h o u l d e x p l i c i t l y g i v e

t h e e x p e c t e d t y p e o f t h e r e t u r n e d v a l u e , u s i n g t h e f o l l o w i n g s y n t a x :

( i n p u t _ v a l u e c h a n : t y p e ) . T h e b e h a v i o r i s u n s p e c i e d i f t h e o b j e c t i n t h e l e d o e s n o t

b e l o n g t o t h e g i v e n t y p e .

v a l u e s e e k _ i n : i n _ c h a n n e l - > i n t - > u n i t

s e e k _ i n c h a n p o s s e t s t h e c u r r e n t r e a d i n g p o s i t i o n t o p o s f o r c h a n n e l c h a n . T h i s w o r k s

o n l y f o r r e g u l a r l e s . O n l e s o f o t h e r k i n d s , t h e b e h a v i o r i s u n s p e c i e d .

v a l u e p o s _ i n : i n _ c h a n n e l - > i n t

R e t u r n t h e c u r r e n t r e a d i n g p o s i t i o n f o r t h e g i v e n c h a n n e l .

v a l u e i n _ c h a n n e l _ l e n g t h : i n _ c h a n n e l - > i n t

R e t u r n t h e t o t a l l e n g t h ( n u m b e r o f c h a r a c t e r s ) o f t h e g i v e n c h a n n e l . T h i s w o r k s o n l y f o r

r e g u l a r l e s . O n l e s o f o t h e r k i n d s , t h e r e s u l t i s m e a n i n g l e s s .

v a l u e c l o s e _ i n : i n _ c h a n n e l - > u n i t

C l o s e t h e g i v e n c h a n n e l . A n y t h i n g c a n h a p p e n i f a n y o f t h e f u n c t i o n s a b o v e i s c a l l e d o n a

c l o s e d c h a n n e l .

1 3 . 1 2 l i s t : o p e r a t i o n s o n l i s t s

v a l u e l i s t _ l e n g t h : ' a l i s t - > i n t

R e t u r n t h e l e n g t h ( n u m b e r o f e l e m e n t s ) o f t h e g i v e n l i s t .

v a l u e p r e f i x @ : ' a l i s t - > ' a l i s t - > ' a l i s t

L i s t c o n c a t e n a t i o n .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 130/218

1 2 8

v a l u e h d : ' a l i s t - > ' a

R e t u r n t h e r s t e l e m e n t o f t h e g i v e n l i s t . R a i s e F a i l u r e " h d " i f t h e l i s t i s e m p t y .

v a l u e t l : ' a l i s t - > ' a l i s t

R e t u r n t h e g i v e n l i s t w i t h o u t i t s r s t e l e m e n t . R a i s e F a i l u r e " t l " i f t h e l i s t i s e m p t y .

v a l u e r e v : ' a l i s t - > ' a l i s t

L i s t r e v e r s a l .

v a l u e m a p : ( ' a - > ' b ) - > ' a l i s t - > ' b l i s t

m a p f a 1 ; . . . ; a n ] a p p l i e s f u n c t i o n f t o a 1 , . . . , a n , a n d b u i l d s t h e l i s t

f a 1 ; . . . ; f a n ] w i t h t h e r e s u l t s r e t u r n e d b y

f .

v a l u e d o _ l i s t : ( ' a - > u n i t ) - > ' a l i s t - > u n i t

d o _ l i s t f a 1 ; . . . ; a n ] a p p l i e s f u n c t i o n f i n t u r n t o a 1 ; . . . ; a n , d i s c a r d i n g a l l t h e

r e s u l t s . I t i s e q u i v a l e n t t o b e g i n f a 1 ; f a 2 ; . . . ; f a n ; ( ) e n d .

v a l u e i t _ l i s t : ( ' a - > ' b - > ' a ) - > ' a - > ' b l i s t - > ' a

i t _ l i s t f a b 1 ; . . . ; b n ] i s f ( . . . ( f ( f a b 1 ) b 2 ) . . . ) b n .

v a l u e l i s t _ i t : ( ' a - > ' b - > ' b ) - > ' a l i s t - > ' b - > ' b

l i s t _ i t f a 1 ; . . . ; a n ] b i s f a 1 ( f a 2 ( . . . ( f a n b ) . . . ) ) .

v a l u e m a p 2 : ( ' a - > ' b - > ' c ) - > ' a l i s t - > ' b l i s t - > ' c l i s t

m a p 2 f a 1 ; . . . ; a n ] b 1 ; . . . ; b n ] i s f a 1 b 1 ; . . . ; f a n b n ] . R a i s e

I n v a l i d _ a r g u m e n t " m a p 2 " i f t h e t w o l i s t s h a v e d i e r e n t l e n g t h s .

v a l u e d o _ l i s t 2 : ( ' a - > ' b - > u n i t ) - > ' a l i s t - > ' b l i s t - > u n i t

d o _ l i s t 2 f a 1 ; . . . ; a n ] b 1 ; . . . ; b n ] c a l l s i n t u r n f a 1 b 1 ; . . . ; f a n b n ,

d i s c a r d i n g t h e r e s u l t s . R a i s e I n v a l i d _ a r g u m e n t " d o _ l i s t 2 " i f t h e t w o l i s t s h a v e d i e r e n t

l e n g t h s .

v a l u e i t _ l i s t 2 : ( ' a - > ' b - > ' c - > ' a ) - > ' a - > ' b l i s t - > ' c l i s t - > ' a

i t _ l i s t 2 f a b 1 ; . . . ; b n ] c 1 ; . . . ; c n ] i s

f ( . . . ( f ( f a b 1 c 1 ) b 2 c 2 ) . . . ) b n c n . R a i s e I n v a l i d _ a r g u m e n t " i t _ l i s t 2 " i f

t h e t w o l i s t s h a v e d i e r e n t l e n g t h s .

v a l u e l i s t _ i t 2 : ( ' a - > ' b - > ' c - > ' c ) - > ' a l i s t - > ' b l i s t - > ' c - > ' c

l i s t _ i t 2 f a 1 ; . . . ; a n ] b 1 ; . . . ; b n ] c i s

f a 1 b 1 ( f a 2 b 2 ( . . . ( f a n b n c ) . . . ) ) . R a i s e I n v a l i d _ a r g u m e n t " l i s t _ i t 2 " i f

t h e t w o l i s t s h a v e d i e r e n t l e n g t h s .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 131/218

C h a p t e r 1 3 . T h e c o r e l i b r a r y 1 2 9

v a l u e f l a t _ m a p : ( ' a - > ' b l i s t ) - > ' a l i s t - > ' b l i s t

f l a t _ m a p f l 1 ; . . . ; l n ] i s ( f l 1 ) @ ( f l 2 ) @ . . . @ ( f l n ) .

v a l u e f o r _ a l l : ( ' a - > b o o l ) - > ' a l i s t - > b o o l

f o r _ a l l p a 1 ; . . . ; a n ] i s ( p a 1 ) & ( p a 2 ) & . . . & ( p a n ) .

v a l u e e x i s t s : ( ' a - > b o o l ) - > ' a l i s t - > b o o l

e x i s t s p a 1 ; . . . ; a n ] i s ( p a 1 ) o r ( p a 2 ) o r . . . o r ( p a n ) .

v a l u e m e m : ' a - > ' a l i s t - > b o o l

m e m a l i s t r u e i f a n d o n l y i f a i s s t r u c t u r a l l y e q u a l ( s e e m o d u l e e q ) t o a n e l e m e n t o f l .

v a l u e m e m q : ' a - > ' a l i s t - > b o o l

m e m q a l i s t r u e i f a n d o n l y i f a i s p h y s i c a l l y e q u a l ( s e e m o d u l e e q ) t o a n e l e m e n t o f l .

v a l u e e x c e p t : ' a - > ' a l i s t - > ' a l i s t

e x c e p t a l r e t u r n s t h e l i s t l w h e r e t h e r s t e l e m e n t s t r u c t u r a l l y e q u a l t o a h a s b e e n

r e m o v e d . T h e l i s t l i s r e t u r n e d u n c h a n g e d i f i t d o e s n o t c o n t a i n a .

v a l u e e x c e p t q : ' a - > ' a l i s t - > ' a l i s t

S a m e a s e x c e p t , w i t h p h y s i c a l e q u a l i t y i n s t e a d o f s t r u c t u r a l e q u a l i t y .

v a l u e s u b t r a c t : ' a l i s t - > ' a l i s t - > ' a l i s t

s u b t r a c t l 1 l 2 r e t u r n s t h e l i s t l 1 w h e r e a l l e l e m e n t s s t r u c t u r a l l y e q u a l t o o n e o f t h e

e l e m e n t s o f l 2 h a v e b e e n r e m o v e d .

v a l u e u n i o n : ' a l i s t - > ' a l i s t - > ' a l i s t

u n i o n l 1 l 2 a p p e n d s b e f o r e l i s t l 2 a l l t h e e l e m e n t s o f l i s t l 1 t h a t a r e n o t s t r u c t u r a l l y

e q u a l t o a n e l e m e n t o f l 2 .

v a l u e i n t e r s e c t : ' a l i s t - > ' a l i s t - > ' a l i s t

i n t e r s e c t l 1 l 2 r e t u r n s t h e l i s t o f t h e e l e m e n t s o f l 1 t h a t a r e s t r u c t u r a l l y e q u a l t o a n

e l e m e n t o f l 2 .

v a l u e i n d e x : ' a - > ' a l i s t - > i n t

i n d e x a l r e t u r n s t h e p o s i t i o n o f t h e r s t e l e m e n t o f l i s t l t h a t i s s t r u c t u r a l l y e q u a l t o a .

T h e h e a d o f t h e l i s t h a s p o s i t i o n 0 . R a i s e N o t _ f o u n d i f a i s n o t p r e s e n t i n l .

v a l u e a s s o c : ' a - > ( ' a * ' b ) l i s t - > ' b

a s s o c a l r e t u r n s t h e v a l u e a s s o c i a t e d w i t h k e y a i n t h e l i s t o f p a i r s l . T h a t i s ,

a s s o c a . . . ; ( a , b ) ; . . . ] = b i f ( a , b ) i s t h e l e f t m o s t b i n d i n g o f a i n l i s t l . R a i s e

N o t _ f o u n d i f t h e r e i s n o v a l u e a s s o c i a t e d w i t h a i n t h e l i s t l .

v a l u e a s s q : ' a - > ( ' a * ' b ) l i s t - > ' b

S a m e a s a s s o c , b u t u s e p h y s i c a l e q u a l i t y i n s t e a d o f s t r u c t u r a l e q u a l i t y t o c o m p a r e k e y s .

v a l u e m e m _ a s s o c : ' a - > ( ' a * ' b ) l i s t - > b o o l

S a m e a s a s s o c , b u t s i m p l y r e t u r n t r u e i f a b i n d i n g e x i s t s , a n d f a l s e i f n o b i n d i n g s e x i s t f o r

t h e g i v e n k e y .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 132/218

1 3 0

1 3 . 1 3 p a i r : o p e r a t i o n s o n p a i r s

v a l u e f s t : ' a * ' b - > ' a

R e t u r n t h e r s t c o m p o n e n t o f a p a i r .

v a l u e s n d : ' a * ' b - > ' b

R e t u r n t h e s e c o n d c o m p o n e n t o f a p a i r .

v a l u e s p l i t : ( ' a * ' b ) l i s t - > ' a l i s t * ' b l i s t

T r a n s f o r m a l i s t o f p a i r s i n t o a p a i r o f l i s t s : s p l i t ( a 1 , b 1 ) ; . . . ; ( a n , b n ) ] i s

( a 1 ; . . . ; a n ] , b 1 ; . . . ; b n ] )

v a l u e c o m b i n e : ' a l i s t * ' b l i s t - > ( ' a * ' b ) l i s t

T r a n s f o r m a p a i r o f l i s t s i n t o a l i s t o f p a i r s : c o m b i n e ( a 1 ; . . . ; a n ] , b 1 ; . . . ; b n ] ) i s

( a 1 , b 1 ) ; . . . ; ( a n , b n ) ] . R a i s e I n v a l i d _ a r g u m e n t " c o m b i n e " i f t h e t w o l i s t s h a v e

d i e r e n t l e n g t h s .

v a l u e m a p _ c o m b i n e : ( ' a * ' b - > ' c ) - > ' a l i s t * ' b l i s t - > ' c l i s t

m a p _ c o m b i n e f ( a 1 ; . . . ; a n ] , b 1 ; . . . ; b n ] ) i s f ( a 1 , b 1 ) ; . . . ; f ( a n , b n ) ] .

R a i s e i n v a l i d _ a r g u m e n t " m a p _ c o m b i n e " i f t h e t w o l i s t s h a v e d i e r e n t l e n g t h s .

v a l u e d o _ l i s t _ c o m b i n e : ( ' a * ' b - > u n i t ) - > ' a l i s t * ' b l i s t - > u n i t

d o _ l i s t _ c o m b i n e f ( a 1 ; . . . ; a n ] , b 1 ; . . . ; b n ] ) c a l l s i n t u r n

f ( a 1 , b 1 ) ; . . . ; f ( a n , b n ) , d i s c a r d i n g t h e r e s u l t s . R a i s e

I n v a l i d _ a r g u m e n t " d o _ l i s t _ c o m b i n e " i f t h e t w o l i s t s h a v e d i e r e n t l e n g t h s .

1 3 . 1 4 r e f : o p e r a t i o n s o n r e f e r e n c e s

t y p e ' a r e f = r e f o f m u t a b l e ' a

T h e t y p e o f r e f e r e n c e s ( m u t a b l e i n d i r e c t i o n c e l l s ) c o n t a i n i n g a v a l u e o f t y p e ' a .

v a l u e p r e f i x ! : ' a r e f - > ' a

! r r e t u r n s t h e c u r r e n t c o n t e n t s o f r e f e r e n c e r . C o u l d b e d e n e d a s f u n ( r e f x ) - > x .

v a l u e p r e f i x : = : ' a r e f - > ' a - > u n i t

r : = a s t o r e s t h e v a l u e o f a i n r e f e r e n c e r .

v a l u e i n c r : i n t r e f - > u n i t

I n c r e m e n t t h e i n t e g e r c o n t a i n e d i n t h e g i v e n r e f e r e n c e . C o u l d b e d e n e d a s

f u n r - > r : = s u c c ! r .

v a l u e d e c r : i n t r e f - > u n i t

D e c r e m e n t t h e i n t e g e r c o n t a i n e d i n t h e g i v e n r e f e r e n c e . C o u l d b e d e n e d a s

f u n r - > r : = p r e d ! r .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 133/218

C h a p t e r 1 3 . T h e c o r e l i b r a r y 1 3 1

1 3 . 1 5 s t r e a m : o p e r a t i o n s o n s t r e a m s

t y p e ' a s t r e a m

T h e t y p e o f s t r e a m s c o n t a i n i n g v a l u e s o f t y p e ' a .

e x c e p t i o n P a r s e _ f a i l u r e

R a i s e d b y p a r s e r s w h e n n o n e o f t h e r s t c o m p o n e n t o f t h e s t r e a m p a t t e r n s i s a c c e p t e d

e x c e p t i o n P a r s e _ e r r o r

R a i s e d b y p a r s e r s w h e n t h e r s t c o m p o n e n t o f a s t r e a m p a t t e r n i s a c c e p t e d , b u t o n e o f t h e

f o l l o w i n g c o m p o n e n t s i s r e j e c t e d

v a l u e s t r e a m _ n e x t : ' a s t r e a m - > ' a

s t r e a m _ n e x t s r e t u r n s t h e r s t e l e m e n t o f s t r e a m s , a n d r e m o v e s i t f r o m t h e s t r e a m . R a i s e

P a r s e _ f a i l u r e i f t h e s t r e a m i s e m p t y .

v a l u e s t r e a m _ f r o m : ( u n i t - > ' a ) - > ' a s t r e a m

s t r e a m _ f r o m f r e t u r n s t h e s t r e a m w h i c h f e t c h e s i t s t e r m i n a l s u s i n g t h e f u n c t i o n f . T h i s

f u n c t i o n c o u l d b e d e n e d a s :

l e t r e c s t r e a m _ f r o m f = < ' f ( ) ; s t r e a m _ f r o m f > ]

b u t i s i m p l e m e n t e d m o r e e c i e n t l y .

v a l u e s t r e a m _ o f _ s t r i n g : s t r i n g - > c h a r s t r e a m

s t r e a m _ o f _ s t r i n g s r e t u r n s t h e s t r e a m o f t h e c h a r a c t e r s i n s t r i n g s .

v a l u e s t r e a m _ o f _ c h a n n e l : i n _ c h a n n e l - > c h a r s t r e a m

s t r e a m _ o f _ c h a n n e l i c r e t u r n s t h e s t r e a m o f c h a r a c t e r s r e a d o n c h a n n e l i c .

v a l u e d o _ s t r e a m : ( ' a - > u n i t ) - > ' a s t r e a m - > u n i t

d o _ s t r e a m f s s c a n s t h e w h o l e s t r e a m s , a p p l y i n g t h e f u n c t i o n f i n t u r n t o e a c h t e r m i n a l

e n c o u n t e r e d

v a l u e s t r e a m _ c h e c k : ( ' a - > b o o l ) - > ' a s t r e a m - > ' a

s t r e a m _ c h e c k p r e t u r n s t h e p a r s e r w h i c h r e t u r n s t h e r s t t e r m i n a l o f t h e s t r e a m i f t h e

p r e d i c a t e p r e t u r n s t r u e o n t h i s t e r m i n a l , a n d r a i s e s P a r s e _ f a i l u r e o t h e r w i s e .

v a l u e e n d _ o f _ s t r e a m : ' a s t r e a m - > u n i t

R e t u r n ( ) i t h e s t r e a m i s e m p t y , a n d r a i s e P a r s e _ f a i l u r e o t h e r w i s e .

v a l u e s t r e a m _ g e t : ' a s t r e a m - > ' a * ' a s t r e a m

s t r e a m _ g e t s r e t u r n t h e r s t e l e m e n t o f t h e s t r e a m s , a n d a s t r e a m c o n t a i n i n g t h e

r e m a i n i n g e l e m e n t s o f s . R a i s e P a r s e _ f a i l u r e i f t h e s t r e a m i s e m p t y . T h e s t r e a m s i s n o t

m o d i e d . T h i s f u n c t i o n m a k e s i t p o s s i b l e t o a c c e s s a s t r e a m n o n - d e s t r u c t i v e l y .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 134/218

1 3 2

1 3 . 1 6 s t r i n g : s t r i n g o p e r a t i o n s

v a l u e s t r i n g _ l e n g t h : s t r i n g - > i n t

R e t u r n t h e l e n g t h ( n u m b e r o f c h a r a c t e r s ) o f t h e g i v e n s t r i n g .

v a l u e n t h _ c h a r : s t r i n g - > i n t - > c h a r

n t h _ c h a r s n r e t u r n s c h a r a c t e r n u m b e r n i n s t r i n g s . T h e r s t c h a r a c t e r i s c h a r a c t e r

n u m b e r 0 . T h e l a s t c h a r a c t e r i s c h a r a c t e r n u m b e r s t r i n g _ l e n g t h s - 1 . R a i s e

I n v a l i d _ a r g u m e n t " n t h _ c h a r " i f n i s o u s i d e t h e r a n g e 0 t o ( s t r i n g _ l e n g t h s - 1 ) . Y o u

c a n a l s o w r i t e s . n ] i n s t e a d o f n t h _ c h a r s n .

v a l u e s e t _ n t h _ c h a r : s t r i n g - > i n t - > c h a r - > u n i t

s e t _ n t h _ c h a r s n c m o d i e s s t r i n g s i n p l a c e , r e p l a c i n g t h e c h a r a c t e r n u m b e r n b y c .

R a i s e I n v a l i d _ a r g u m e n t " s e t _ n t h _ c h a r " i f n i s o u s i d e t h e r a n g e 0 t o

( s t r i n g _ l e n g t h s - 1 ) . Y o u c a n a l s o w r i t e s . n ] < - c i n s t e a d o f s e t _ n t h _ c h a r s n c .

v a l u e p r e f i x ^ : s t r i n g - > s t r i n g - > s t r i n g

s 1 ^ s 2 r e t u r n s a f r e s h s t r i n g c o n t a i n i n g t h e c o n c a t e n a t i o n o f t h e s t r i n g s s 1 a n d s 2 .

v a l u e c o n c a t : s t r i n g l i s t - > s t r i n g

R e t u r n a f r e s h s t r i n g c o n t a i n i n g t h e c o n c a t e n a t i o n o f a l l t h e s t r i n g s i n t h e a r g u m e n t l i s t .

v a l u e s u b _ s t r i n g : s t r i n g - > i n t - > i n t - > s t r i n g

s u b _ s t r i n g s s t a r t l e n r e t u r n s a f r e s h s t r i n g o f l e n g t h l e n , c o n t a i n i n g t h e c h a r a c t e r s

n u m b e r s t a r t t o s t a r t + l e n - 1 o f s t r i n g s . R a i s e I n v a l i d _ a r g u m e n t " s u b _ s t r i n g " i f

s t a r t a n d l e n d o n o t d e s i g n a t e a v a l i d s u b s t r i n g o f s ; t h a t i s , i f s t a r t < 0 , o r l e n < 0 , o r

s t a r t + l e n > s t r i n g _ l e n g t h s .

v a l u e c r e a t e _ s t r i n g : i n t - > s t r i n g

c r e a t e _ s t r i n g n r e t u r n s a f r e s h s t r i n g o f l e n g t h n . T h e s t r i n g i n i t i a l l y c o n t a i n s a r b i t r a r y

c h a r a c t e r s .

v a l u e m a k e _ s t r i n g : i n t - > c h a r - > s t r i n g

m a k e _ s t r i n g n c r e t u r n s a f r e s h s t r i n g o f l e n g t h n , l l e d w i t h t h e c h a r a c t e r c .

v a l u e f i l l _ s t r i n g : s t r i n g - > i n t - > i n t - > c h a r - > u n i t

f i l l _ s t r i n g s s t a r t l e n c m o d i e s s t r i n g s i n p l a c e , r e p l a c i n g t h e c h a r a c t e r s n u m b e r

s t a r t t o s t a r t + l e n - 1 b y c . R a i s e I n v a l i d _ a r g u m e n t " f i l l _ s t r i n g " i f s t a r t a n d

l e n d o n o t d e s i g n a t e a v a l i d s u b s t r i n g o f s .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 135/218

C h a p t e r 1 3 . T h e c o r e l i b r a r y 1 3 3

v a l u e b l i t _ s t r i n g : s t r i n g - > i n t - > s t r i n g - > i n t - > i n t - > u n i t

b l i t _ s t r i n g s 1 o 1 s 2 o 2 l e n c o p i e s l e n c h a r a c t e r s f r o m s t r i n g s 1 , s t a r t i n g a t c h a r a c t e r

n u m b e r o 1 , t o s t r i n g s 2 , s t a r t i n g a t c h a r a c t e r n u m b e r o 2 . I t w o r k s c o r r e c t l y e v e n i f s 1 a n d

s 2 a r e t h e s a m e s t r i n g , a n d t h e s o u r c e a n d d e s t i n a t i o n c h u n k s o v e r l a p . R a i s e

I n v a l i d _ a r g u m e n t " b l i t _ s t r i n g " i f o 1 a n d l e n d o n o t d e s i g n a t e a v a l i d s u b s t r i n g o f s 1 ,

o r i f o 2 a n d l e n d o n o t d e s i g n a t e a v a l i d s u b s t r i n g o f s 2 .

v a l u e r e p l a c e _ s t r i n g : s t r i n g - > s t r i n g - > i n t - > u n i t

r e p l a c e _ s t r i n g d e s t s r c s t a r t c o p i e s a l l c h a r a c t e r s f r o m t h e s t r i n g s r c i n t o t h e s t r i n g

d s t , s t a r t i n g a t c h a r a c t e r n u m b e r s t a r t i n d s t . R a i s e

I n v a l i d _ a r g u m e n t " r e p l a c e _ s t r i n g " i f c o p y i n g w o u l d o v e r o w s t r i n g d e s t .

v a l u e e q _ s t r i n g : s t r i n g - > s t r i n g - > b o o l

v a l u e n e q _ s t r i n g : s t r i n g - > s t r i n g - > b o o l

v a l u e l e _ s t r i n g : s t r i n g - > s t r i n g - > b o o l

v a l u e l t _ s t r i n g : s t r i n g - > s t r i n g - > b o o l

v a l u e g e _ s t r i n g : s t r i n g - > s t r i n g - > b o o l

v a l u e g t _ s t r i n g : s t r i n g - > s t r i n g - > b o o l

C o m p a r i s o n f u n c t i o n s ( l e x i c o g r a p h i c o r d e r i n g ) b e t w e e n s t r i n g s .

v a l u e c o m p a r e _ s t r i n g s : s t r i n g - > s t r i n g - > i n t

G e n e r a l c o m p a r i s o n b e t w e e n s t r i n g s . c o m p a r e _ s t r i n g s s 1 s 2 r e t u r n s 0 i f s 1 a n d s 2 a r e

e q u a l , o r e l s e - 2 i f s 1 i s a p r e x o f s 2 , o r 2 i f s 2 i s a p r e x o f s 1 , o r e l s e - 1 i f s 1 i s

l e x i c o g r a p h i c a l l y b e f o r e s 2 , o r 1 i f s 2 i s l e x i c o g r a p h i c a l l y b e f o r e s 1 .

v a l u e s t r i n g _ f o r _ r e a d : s t r i n g - > s t r i n g

R e t u r n a c o p y o f t h e a r g u m e n t , w i t h s p e c i a l c h a r a c t e r s r e p r e s e n t e d b y e s c a p e s e q u e n c e s ,

f o l l o w i n g t h e l e x i c a l c o n v e n t i o n s o f C a m l L i g h t .

v a l u e i n d e x _ c h a r : s t r i n g - > c h a r - > i n t

i n d e x _ c h a r s c r e t u r n s t h e p o s i t i o n o f t h e l e f t m o s t o c c u r r e n c e o f c h a r a c t e r c i n s t r i n g s .

R a i s e N o t _ f o u n d i f c d o e s n o t o c c u r i n s .

v a l u e r i n d e x _ c h a r : s t r i n g - > c h a r - > i n t

r i n d e x _ c h a r s c r e t u r n s t h e p o s i t i o n o f t h e r i g h t m o s t o c c u r r e n c e o f c h a r a c t e r c i n s t r i n g

s . R a i s e N o t _ f o u n d i f c d o e s n o t o c c u r i n s .

v a l u e i n d e x _ c h a r _ f r o m : s t r i n g - > i n t - > c h a r - > i n t

v a l u e r i n d e x _ c h a r _ f r o m : s t r i n g - > i n t - > c h a r - > i n t

S a m e a s i n d e x _ c h a r a n d r i n d e x _ c h a r , b u t s t a r t s e a r c h i n g a t t h e c h a r a c t e r p o s i t i o n g i v e n

a s s e c o n d a r g u m e n t . i n d e x _ c h a r s c i s e q u i v a l e n t t o i n d e x _ c h a r _ f r o m s 0 c , a n d

r i n d e x _ c h a r s c t o r i n d e x _ c h a r _ f r o m s ( s t r i n g _ l e n g t h s - 1 ) c .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 136/218

1 3 4

1 3 . 1 7 v e c t : o p e r a t i o n s o n v e c t o r s

v a l u e v e c t _ l e n g t h : ' a v e c t - > i n t

R e t u r n t h e l e n g t h ( n u m b e r o f e l e m e n t s ) o f t h e g i v e n v e c t o r .

v a l u e v e c t _ i t e m : ' a v e c t - > i n t - > ' a

v e c t _ i t e m v n r e t u r n s t h e e l e m e n t n u m b e r n o f v e c t o r v . T h e r s t e l e m e n t h a s n u m b e r 0 .

T h e l a s t e l e m e n t h a s n u m b e r v e c t _ l e n g t h v - 1 . R a i s e I n v a l i d _ a r g u m e n t " v e c t _ i t e m "

i f n i s o u t s i d e t h e r a n g e 0 t o ( v e c t _ l e n g t h v - 1 ) . Y o u c a n a l s o w r i t e v . ( n ) i n s t e a d o f

v e c t _ i t e m v n .

v a l u e v e c t _ a s s i g n : ' a v e c t - > i n t - > ' a - > u n i t

v e c t _ a s s i g n v n x m o d i e s v e c t o r v i n p l a c e , r e p l a c i n g e l e m e n t n u m b e r n w i t h x . R a i s e

I n v a l i d _ a r g u m e n t " v e c t _ a s s i g n " i f n i s o u t s i d e t h e r a n g e 0 t o v e c t _ l e n g t h v - 1 . Y o u

c a n a l s o w r i t e v . ( n ) < - x i n s t e a d o f v e c t _ a s s i g n v n x .

v a l u e m a k e _ v e c t : i n t - > ' a - > ' a v e c t

m a k e _ v e c t n x r e t u r n s a f r e s h v e c t o r o f l e n g t h n , i n i t i a l i z e d w i t h x . A l l t h e e l e m e n t s o f t h i s

n e w v e c t o r a r e i n i t i a l l y p h y s i c a l l y e q u a l t o x ( s e e m o d u l e e q ) . C o n s e q u e n t l y , i f x i s m u t a b l e ,

i t i s s h a r e d a m o n g a l l e l e m e n t s o f t h e v e c t o r , a n d m o d i f y i n g x t h r o u g h o n e o f t h e v e c t o r

e n t r i e s w i l l m o d i f y a l l o t h e r e n t r i e s a t t h e s a m e t i m e .

v a l u e m a k e _ m a t r i x : i n t - > i n t - > ' a - > ' a v e c t v e c t

m a k e _ m a t r i x d i m x d i m y e r e t u r n s a t w o - d i m e n s i o n a l a r r a y ( a v e c t o r o f v e c t o r s ) w i t h r s t

d i m e n s i o n d i m x a n d s e c o n d d i m e n s i o n d i m y . A l l t h e e l e m e n t s o f t h i s n e w m a t r i x a r e

i n i t i a l l y p h y s i c a l l y e q u a l t o e . T h e e l e m e n t ( x , y ) o f a m a t r i x m i s a c c e s s e d w i t h t h e n o t a t i o n

m . ( x ) . ( y ) .

v a l u e i n i t _ v e c t : i n t - > ( i n t - > ' a ) - > ' a v e c t

i n i t _ v e c t n f r e t u r n s a f r e s h a r r a y o f l e n g t h n , w i t h e l e m e n t n u m b e r i e q u a l t o f i .

v a l u e c o n c a t _ v e c t : ' a v e c t - > ' a v e c t - > ' a v e c t

c o n c a t _ v e c t v 1 v 2 r e t u r n s a f r e s h v e c t o r c o n t a i n i n g t h e c o n c a t e n a t i o n o f v e c t o r s v 1 a n d

v 2 .

v a l u e s u b _ v e c t : ' a v e c t - > i n t - > i n t - > ' a v e c t

s u b _ v e c t v s t a r t l e n r e t u r n s a f r e s h v e c t o r o f l e n g t h l e n , c o n t a i n i n g t h e e l e m e n t s

n u m b e r s t a r t t o s t a r t + l e n - 1 o f v e c t o r v . R a i s e I n v a l i d _ a r g u m e n t " s u b _ v e c t " i f

s t a r t a n d l e n d o n o t d e s i g n a t e a v a l i d s u b v e c t o r o f v ; t h a t i s , i f s t a r t < 0 , o r l e n < 0 , o r

s t a r t + l e n > v e c t _ l e n g t h v .

v a l u e c o p y _ v e c t : ' a v e c t - > ' a v e c t

c o p y _ v e c t v r e t u r n s a c o p y o f v , t h a t i s , a f r e s h v e c t o r c o n t a i n i n g t h e s a m e e l e m e n t s a s v .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 137/218

C h a p t e r 1 3 . T h e c o r e l i b r a r y 1 3 5

v a l u e f i l l _ v e c t : ' a v e c t - > i n t - > i n t - > ' a - > u n i t

f i l l _ v e c t v o f s l e n x m o d i e s t h e v e c t o r v i n p l a c e , s t o r i n g x i n e l e m e n t s n u m b e r o f s

t o o f s + l e n - 1 . R a i s e I n v a l i d _ a r g u m e n t " f i l l _ v e c t " i f o f s a n d l e n d o n o t d e s i g n a t e

a v a l i d s u b v e c t o r o f v .

v a l u e b l i t _ v e c t : ' a v e c t - > i n t - > ' a v e c t - > i n t - > i n t - > u n i t

b l i t _ v e c t v 1 o 1 v 2 o 2 l e n c o p i e s l e n e l e m e n t s f r o m v e c t o r v 1 , s t a r t i n g a t e l e m e n t

n u m b e r o 1 , t o v e c t o r v 2 , s t a r t i n g a t e l e m e n t n u m b e r o 2 . I t w o r k s c o r r e c t l y e v e n i f v 1 a n d

v 2 a r e t h e s a m e v e c t o r , a n d t h e s o u r c e a n d d e s t i n a t i o n c h u n k s o v e r l a p . R a i s e

I n v a l i d _ a r g u m e n t " b l i t _ v e c t " i f o 1 a n d l e n d o n o t d e s i g n a t e a v a l i d s u b v e c t o r o f v 1 , o r

i f o 2 a n d l e n d o n o t d e s i g n a t e a v a l i d s u b v e c t o r o f v 2 .

v a l u e l i s t _ o f _ v e c t : ' a v e c t - > ' a l i s t

l i s t _ o f _ v e c t v r e t u r n s t h e l i s t o f a l l t h e e l e m e n t s o f v , t h a t i s :

v . ( 0 ) ; v . ( 1 ) ; . . . ; v . ( v e c t _ l e n g t h v - 1 ) ] .

v a l u e v e c t _ o f _ l i s t : ' a l i s t - > ' a v e c t

v e c t _ o f _ l i s t l r e t u r n s a f r e s h v e c t o r c o n t a i n i n g t h e e l e m e n t s o f l .

v a l u e m a p _ v e c t : ( ' a - > ' b ) - > ' a v e c t - > ' b v e c t

m a p _ v e c t f v a p p l i e s f u n c t i o n f t o a l l t h e e l e m e n t s o f v , a n d b u i l d s a v e c t o r w i t h t h e

r e s u l t s r e t u r n e d b y f : | f v . ( 0 ) ; f v . ( 1 ) ; . . . ; f v . ( v e c t _ l e n g t h v - 1 ) | ] .

v a l u e m a p _ v e c t _ l i s t : ( ' a - > ' b ) - > ' a v e c t - > ' b l i s t

m a p _ v e c t _ l i s t f v a p p l i e s f u n c t i o n f t o a l l t h e e l e m e n t s o f v , a n d b u i l d s a l i s t w i t h t h e

r e s u l t s r e t u r n e d b y f : f v . ( 0 ) ; f v . ( 1 ) ; . . . ; f v . ( v e c t _ l e n g t h v - 1 ) ] .

v a l u e d o _ v e c t : ( ' a - > u n i t ) - > ' a v e c t - > u n i t

d o _ v e c t f v a p p l i e s f u n c t i o n f i n t u r n t o a l l t h e e l e m e n t s o f v , d i s c a r d i n g a l l t h e r e s u l t s :

f v . ( 0 ) ; f v . ( 1 ) ; . . . ; f v . ( v e c t _ l e n g t h v - 1 ) ; ( ) .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 138/218

1 3 6

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 139/218

C h a p t e r 1 4

T h e s t a n d a r d l i b r a r y

T h i s c h a p t e r d e s c r i b e s t h e f u n c t i o n s p r o v i d e d b y t h e C a m l L i g h t s t a n d a r d l i b r a r y . J u s t a s t h e

m o d u l e s f r o m t h e c o r e l i b r a r y , t h e m o d u l e s f r o m t h e s t a n d a r d l i b r a r y a r e a u t o m a t i c a l l y l i n k e d w i t h

t h e u s e r ' s o b j e c t c o d e l e s b y t h e c a m l c c o m m a n d . H e n c e , t h e g l o b a l s d e n e d b y t h e s e l i b r a r i e s

c a n b e u s e d i n s t a n d a l o n e p r o g r a m s w i t h o u t h a v i n g t o a d d a n y . z o l e o n t h e c o m m a n d l i n e f o r t h e

l i n k i n g p h a s e . S i m i l a r l y , i n i n t e r a c t i v e u s e , t h e s e g l o b a l s c a n b e u s e d i n t o p l e v e l p h r a s e s w i t h o u t

h a v i n g t o l o a d a n y . z o l e i n m e m o r y .

U n l i k e t h e m o d u l e s f r o m t h e c o r e l i b r a r y , t h e m o d u l e s f r o m t h e s t a n d a r d l i b r a r y a r e n o t a u t o -

m a t i c a l l y \ o p e n e d " w h e n a c o m p i l a t i o n s t a r t s , o r w h e n t h e t o p l e v e l s y s t e m i s l a u n c h e d . H e n c e i t

i s n e c e s s a r y t o u s e q u a l i e d i d e n t i e r s t o r e f e r t o t h e f u n c t i o n s p r o v i d e d b y t h e s e m o d u l e s , o r t o

a d d # o p e n d i r e c t i v e s .

C o n v e n t i o n s

F o r e a s y r e f e r e n c e , t h e m o d u l e s a r e l i s t e d b e l o w i n a l p h a b e t i c a l o r d e r o f m o d u l e n a m e s . F o r e a c h

m o d u l e , t h e d e c l a r a t i o n s f r o m i t s i n t e r f a c e l e a r e p r i n t e d o n e b y o n e i n t y p e w r i t e r f o n t , f o l l o w e d

b y a s h o r t c o m m e n t . A l l m o d u l e s a n d t h e i d e n t i e r s t h e y e x p o r t a r e i n d e x e d a t t h e e n d o f t h i s

r e p o r t .

1 4 . 1 a r g : p a r s i n g o f c o m m a n d l i n e a r g u m e n t s

T h i s m o d u l e p r o v i d e s a g e n e r a l m e c h a n i s m f o r e x t r a c t i n g o p t i o n s a n d a r g u m e n t s f r o m t h e

c o m m a n d l i n e t o t h e p r o g r a m .

S y n t a x o f c o m m a n d l i n e s : A k e y w o r d i s a c h a r a c t e r s t r i n g s t a r t i n g w i t h a - . A n o p t i o n i s a

k e y w o r d a l o n e o r f o l l o w e d b y a n a r g u m e n t . T h e r e a r e f o u r t y p e s o f k e y w o r d s : U n i t , S t r i n g ,

I n t , a n d F l o a t . U n i t k e y w o r d s d o n o t t a k e a n a r g u m e n t . S t r i n g , I n t , a n d F l o a t k e y w o r d s

t a k e t h e f o l l o w i n g w o r d o n t h e c o m m a n d l i n e a s a n a r g u m e n t . A r g u m e n t s n o t p r e c e d e d b y a

k e y w o r d a r e c a l l e d a n o n y m o u s a r g u m e n t s .

E x a m p l e s ( c m d i s a s s u m e d t o b e t h e c o m m a n d n a m e ) :

c m d - f l a g ( a u n i t o p t i o n )

1 3 7

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 140/218

1 3 8

c m d - i n t 1 ( a n i n t o p t i o n w i t h a r g u m e n t 1 )

c m d - s t r i n g f o o b a r ( a s t r i n g o p t i o n w i t h a r g u m e n t " f o o b a r " )

c m d - f l o a t 1 2 . 3 4 ( a o a t o p t i o n w i t h a r g u m e n t 1 2 . 3 4 )

c m d 1 2 3 ( t h r e e a n o n y m o u s a r g u m e n t s : " 1 " , " 2 " , a n d " 3 " )

c m d 1 2 - f l a g 3 - s t r i n g b a r 4

( f o u r a n o n y m o u s a r g u m e n t s , a u n i t o p t i o n , a n d

a s t r i n g o p t i o n w i t h a r g u m e n t " b a r " )

t y p e s p e c =

S t r i n g o f ( s t r i n g - > u n i t )

| I n t o f ( i n t - > u n i t )

| U n i t o f ( u n i t - > u n i t )

| F l o a t o f ( f l o a t - > u n i t )

T h e c o n c r e t e t y p e d e s c r i b i n g t h e b e h a v i o r a s s o c i a t e d w i t h a k e y w o r d .

v a l u e p a r s e : ( s t r i n g * s p e c ) l i s t - > ( s t r i n g - > u n i t ) - > u n i t

p a r s e s p e c l i s t a n o n f u n p a r s e s t h e c o m m a n d l i n e , c a l l i n g t h e f u n c t i o n s i n s p e c l i s t

w h e n e v e r a p p r o p r i a t e , a n d a n o n f u n o n a n o n y m o u s a r g u m e n t s . T h e f u n c t i o n s a r e c a l l e d i n

t h e s a m e o r d e r a s t h e y a p p e a r o n t h e c o m m a n d l i n e . T h e s t r i n g s i n t h e

( s t r i n g * s p e c ) l i s t a r e k e y w o r d s a n d m u s t s t a r t w i t h a - , e l s e t h e y a r e i g n o r e d . F o r

t h e u s e r t o b e a b l e t o s p e c i f y a n o n y m o u s a r g u m e n t s s t a r t i n g w i t h a - , i n c l u d e f o r e x a m p l e

( " - - " , S t r i n g a n o n f u n ) i n s p e c l i s t .

e x c e p t i o n B a d o f s t r i n g

F u n c t i o n s i n s p e c l i s t

o r a n o n f u n

c a n r a i s e B a d

w i t h a n e r r o r m e s s a g e t o r e j e c t i n v a l i d

a r g u m e n t s .

1 4 . 2 b a l t r e e : b a s i c b a l a n c e d b i n a r y t r e e s

T h i s m o d u l e i m p l e m e n t s b a l a n c e d o r d e r e d b i n a r y t r e e s . A l l o p e r a t i o n s o v e r b i n a r y t r e e s a r e

a p p l i c a t i v e ( n o s i d e - e e c t s ) . T h e s e t a n d m a p m o d u l e s a r e b a s e d o n t h i s m o d u l e . T h i s

m o d u l e s g i v e s a m o r e d i r e c t a c c e s s t o t h e i n t e r n a l s o f t h e b i n a r y t r e e i m p l e m e n t a t i o n t h a n

t h e s e t a n d m a p a b s t r a c t i o n s , b u t i s m o r e d e l i c a t e t o u s e a n d n o t a s s a f e . F o r a d v a n c e d

u s e r s o n l y .

t y p e ' a t = E m p t y | N o d e o f ' a t * ' a * ' a t * i n t

T h e t y p e o f t r e e s c o n t a i n i n g e l e m e n t s o f t y p e ' a . E m p t y i s t h e e m p t y t r e e ( c o n t a i n i n g n o

e l e m e n t s ) .

t y p e ' a c o n t e n t s = N o t h i n g | S o m e t h i n g o f ' a

U s e d w i t h t h e f u n c t i o n s m o d i f y a n d s p l i t , t o r e p r e s e n t t h e p r e s e n c e o r t h e a b s e n c e o f a n

e l e m e n t i n a t r e e .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 141/218

C h a p t e r 1 4 . T h e s t a n d a r d l i b r a r y 1 3 9

v a l u e a d d : ( ' a - > i n t ) - > ' a - > ' a t - > ' a t

a d d f x t i n s e r t s t h e e l e m e n t x i n t o t h e t r e e t . f i s a n o r d e r i n g f u n c t i o n : f y m u s t r e t u r n

0 i f x a n d y a r e e q u a l ( o r e q u i v a l e n t ) , a n e g a t i v e i n t e g e r i f x i s s m a l l e r t h a n y , a n d a p o s i t i v e

i n t e g e r i f x i s g r e a t e r t h a n y . T h e t r e e t i s r e t u r n e d u n c h a n g e d i f i t a l r e a d y c o n t a i n s a n

e l e m e n t e q u i v a l e n t t o x ( t h a t i s , a n e l e m e n t y s u c h t h a t f y i s 0 ) . T h e o r d e r i n g f m u s t b e

c o n s i s t e n t w i t h t h e o r d e r i n g s u s e d t o b u i l d t w i t h a d d , r e m o v e , m o d i f y o r s p l i t o p e r a t i o n s .

v a l u e c o n t a i n s : ( ' a - > i n t ) - > ' a t - > b o o l

c o n t a i n s f t c h e c k s w h e t h e r t c o n t a i n s a n e l e m e n t s a t i s f y i n g f , t h a t i s , a n e l e m e n t x s u c h

t h a t f x i s 0 . f i s a n o r d e r i n g f u n c t i o n w i t h t h e s a m e c o n s t r a i n t s a s f o r a d d . I t c a n b e

c o a r s e r ( i d e n t i f y m o r e e l e m e n t s ) t h a n t h e o r d e r i n g s u s e d t o b u i l d t , b u t m u s t b e c o n s i s t e n t

w i t h t h e m .

v a l u e f i n d : ( ' a - > i n t ) - > ' a t - > ' a

S a m e a s c o n t a i n s , e x c e p t t h a t f i n d f t r e t u r n s t h e e l e m e n t x s u c h t h a t f x i s 0 , o r r a i s e s

N o t _ f o u n d i f n o n e h a s b e e n f o u n d .

v a l u e r e m o v e : ( ' a - > i n t ) - > ' a t - > ' a t

r e m o v e f t r e m o v e s o n e e l e m e n t x o f t s u c h t h a t f x i s 0 . f i s a n o r d e r i n g f u n c t i o n w i t h

t h e s a m e c o n s t r a i n t s a s f o r a d d . t i s r e t u r n e d u n c h a n g e d i f i t d o e s n o t c o n t a i n a n y e l e m e n t

s a t i s f y i n g f . I f s e v e r a l e l e m e n t s o f t s a t i s f y f , o n l y o n e i s r e m o v e d .

v a l u e m o d i f y : ( ' a - > i n t ) - > ( ' a c o n t e n t s - > ' a c o n t e n t s ) - > ' a t - > ' a t

G e n e r a l i n s e r t i o n / m o d i c a t i o n / d e l e t i o n f u n c t i o n . m o d i f y f g t s e a r c h s t f o r a n e l e m e n t x

s a t i s f y i n g t h e o r d e r i n g f u n c t i o n f . I f o n e i s f o u n d , g i s a p p l i e d t o S o m e t h i n g x ; i f g r e t u r n s

N o t h i n g , t h e e l e m e n t x i s r e m o v e d ; i f g r e t u r n s S o m e t h i n g y , t h e e l e m e n t y r e p l a c e s x i n

t h e t r e e . ( I t i s a s s u m e d t h a t x a n d y a r e e q u i v a l e n t , i n p a r t i c u l a r , t h a t f y i s 0 . ) I f t h e t r e e

d o e s n o t c o n t a i n a n y x s a t i s f y i n g f , g i s a p p l i e d t o N o t h i n g ; i f i t r e t u r n s N o t h i n g , t h e t r e e

i s r e t u r n e d u n c h a n g e d ; i f i t r e t u r n s S o m e t h i n g x , t h e e l e m e n t x i s i n s e r t e d i n t h e t r e e . ( I t i s

a s s u m e d t h a t f x i s 0 . ) T h e f u n c t i o n s a d d a n d r e m o v e a r e s p e c i a l c a s e s o f m o d i f y , s l i g h t l y

m o r e e c i e n t .

v a l u e s p l i t : ( ' a - > i n t ) - > ' a t - > ' a t * ' a c o n t e n t s * ' a t

s p l i t f t r e t u r n s a t r i p l e ( l e s s , e l t , g r e a t e r ) w h e r e l e s s i s a t r e e c o n t a i n i n g a l l

e l e m e n t s x o f t s u c h t h a t f x i s n e g a t i v e , g r e a t e r i s a t r e e c o n t a i n i n g a l l e l e m e n t s x o f t

s u c h t h a t f x i s p o s i t i v e , a n d e l t i s S o m e t h i n g x i f t c o n t a i n s a n e l e m e n t x s u c h t h a t f x

i s 0 , a n d N o t h i n g o t h e r w i s e .

v a l u e c o m p a r e : ( ' a - > ' a - > i n t ) - > ' a t - > ' a t - > i n t

C o m p a r e t w o t r e e s . T h e r s t a r g u m e n t f i s a c o m p a r i s o n f u n c t i o n o v e r t h e t r e e e l e m e n t s :

f e 1 e 2 i s z e r o i f t h e e l e m e n t s e 1 a n d e 2 a r e e q u a l , n e g a t i v e i f e 1 i s s m a l l e r t h a n e 2 , a n d

p o s i t i v e i f e 1 i s g r e a t e r t h a n e 2 . c o m p a r e f t 1 t 2 c o m p a r e s t h e f r i n g e s o f t 1 a n d t 2 b y

l e x i c o g r a p h i c e x t e n s i o n o f f .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 142/218

1 4 0

1 4 . 3 f i l e n a m e : o p e r a t i o n s o n l e n a m e s

v a l u e c u r r e n t _ d i r _ n a m e : s t r i n g

T h e c o n v e n t i o n a l n a m e f o r t h e c u r r e n t d i r e c t o r y ( e . g . . i n U n i x ) .

v a l u e c o n c a t : s t r i n g - > s t r i n g - > s t r i n g

c o n c a t d i r f i l e r e t u r n s a l e n a m e t h a t d e s i g n a t e s l e f i l e i n d i r e c t o r y d i r .

v a l u e i s _ a b s o l u t e : s t r i n g - > b o o l

R e t u r n t r u e i f t h e l e n a m e i s a b s o l u t e o r s t a r t s w i t h a n e x p l i c i t r e f e r e n c e t o t h e c u r r e n t

d i r e c t o r y ( . / o r . . / i n U n i x ) , a n d f a l s e i f i t i s r e l a t i v e t o t h e c u r r e n t d i r e c t o r y .

v a l u e c h e c k _ s u f f i x : s t r i n g - > s t r i n g - > b o o l

c h e c k _ s u f f i x n a m e s u f f r e t u r n s t r u e i f t h e l e n a m e n a m e e n d s w i t h t h e s u x s u f f .

v a l u e c h o p _ s u f f i x : s t r i n g - > s t r i n g - > s t r i n g

c h o p _ s u f f i x n a m e s u f f r e m o v e s t h e s u x s u f f f r o m t h e l e n a m e n a m e . T h e b e h a v i o r i s

u n d e n e d i f n a m e d o e s n o t e n d w i t h t h e s u x s u f f .

v a l u e b a s e n a m e : s t r i n g - > s t r i n g

v a l u e d i r n a m e : s t r i n g - > s t r i n g

S p l i t a l e n a m e i n t o d i r e c t o r y n a m e / b a s e l e n a m e .

c o n c a t ( d i r n a m e n a m e ) ( b a s e n a m e n a m e ) r e t u r n s a l e n a m e w h i c h i s e q u i v a l e n t t o n a m e .

M o r e o v e r , a f t e r s e t t i n g t h e c u r r e n t d i r e c t o r y t o d i r n a m e n a m e ( w i t h s y s _ _ c h d i r ) ,

r e f e r e n c e s t o b a s e n a m e n a m e ( w h i c h i s a r e l a t i v e l e n a m e ) d e s i g n a t e t h e s a m e l e a s n a m e

b e f o r e t h e c a l l t o c h d i r .

1 4 . 4 f o r m a t : p r e t t y p r i n t i n g

T h i s m o d u l e i m p l e m e n t s a p r e t t y - p r i n t i n g f a c i l i t y t o f o r m a t t e x t w i t h i n \ p r e t t y - p r i n t i n g

b o x e s " . T h e p r e t t y - p r i n t e r b r e a k s l i n e s a t s p e c i e d b r e a k h i n t s , a n d i n d e n t s l i n e s a c c o r d i n g

t o t h e b o x s t r u c t u r e .

R u l e o f t h u m b f o r c a s u a l u s e r s :

u s e s i m p l e b o x e s ( a s o b t a i n e d b y o p e n _ b o x 0 ) ;

u s e s i m p l e b r e a k h i n t s ( a s o b t a i n e d b y p r i n t _ c u t ( ) t h a t o u t p u t s a s i m p l e b r e a k h i n t , o r

b y p r i n t _ s p a c e ( ) t h a t o u p u t s a s p a c e i n d i c a t i n g a b r e a k h i n t ) ;

o n c e a b o x i s o p e n e d , d i s p l a y i t s m a t e r i a l w i t h b a s i c p r i n t i n g f u n c t i o n s ( e . g . p r i n t _ i n t

a n d p r i n t _ s t r i n g ) ;

w h e n t h e m a t e r i a l f o r a b o x h a s b e e n p r i n t e d , c a l l c l o s e _ b o x ( ) t o c l o s e t h e b o x ;

a t t h e e n d o f y o u r r o u t i n e , e v a l u a t e p r i n t _ n e w l i n e ( ) t o c l o s e a l l r e m a i n i n g b o x e s a n d

u s h t h e p r e t t y - p r i n t e r .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 143/218

C h a p t e r 1 4 . T h e s t a n d a r d l i b r a r y 1 4 1

Y o u m a y a l t e r n a t i v e l y c o n s i d e r t h i s m o d u l e a s p r o v i d i n g a n e x t e n s i o n t o t h e p r i n t f f a c i l i t y :

y o u c a n s i m p l y a d d p r e t t y - p r i n t i n g a n n o t a t i o n s t o y o u r r e g u l a r p r i n t f f o r m a t s , a s e x p l a i n e d

b e l o w i n t h e d o c u m e n t a t i o n o f t h e f u n c t i o n f p r i n t f .

T h e b e h a v i o u r o f p r e t t y - p r i n t i n g c o m m a n d s i s u n s p e c i e d i f t h e r e i s n o o p e n e d

p r e t t y - p r i n t i n g b o x . E a c h b o x o p e n e d v i a o n e o f t h e o p e n _ f u n c t i o n s b e l o w m u s t b e c l o s e d

u s i n g c l o s e _ b o x f o r p r o p e r f o r m a t t i n g . O t h e r w i s e , s o m e o f t h e m a t e r i a l p r i n t e d i n t h e

b o x e s m a y n o t b e o u t p u t , o r m a y b e f o r m a t t e d i n c o r r e c t l y .

I n c a s e o f i n t e r a c t i v e u s e , t h e s y s t e m c l o s e s a l l o p e n e d b o x e s a n d u s h e s a l l p e n d i n g t e x t ( a s

w i t h t h e p r i n t _ n e w l i n e f u n c t i o n ) a f t e r e a c h p h r a s e . E a c h p h r a s e i s t h e r e f o r e e x e c u t e d i n

t h e i n i t i a l s t a t e o f t h e p r e t t y - p r i n t e r .

B o x e s

v a l u e o p e n _ b o x : i n t - > u n i t

o p e n _ b o x d o p e n s a n e w p r e t t y - p r i n t i n g b o x w i t h o s e t d . T h i s b o x i s t h e g e n e r a l p u r p o s e

p r e t t y - p r i n t i n g b o x . M a t e r i a l i n t h i s b o x i s d i s p l a y e d \ h o r i z o n t a l o r v e r t i c a l " : b r e a k h i n t s

i n s i d e t h e b o x m a y l e a d t o a n e w l i n e , i f t h e r e i s n o m o r e r o o m o n t h e l i n e t o p r i n t t h e

r e m a i n d e r o f t h e b o x , o r i f a n e w l i n e m a y l e a d t o a n e w i n d e n t a t i o n ( d e m o n s t r a t i n g t h e

i n d e n t a t i o n o f t h e b o x ) . W h e n a n e w l i n e i s p r i n t e d i n t h e b o x , d i s a d d e d t o t h e c u r r e n t

i n d e n t a t i o n .

v a l u e c l o s e _ b o x : u n i t - > u n i t

C l o s e t h e m o s t r e c e n t l y o p e n e d p r e t t y - p r i n t i n g b o x .

F o r m a t t i n g f u n c t i o n s

v a l u e p r i n t _ s t r i n g : s t r i n g - > u n i t

p r i n t _ s t r i n g s t r p r i n t s s t r i n t h e c u r r e n t b o x .

v a l u e p r i n t _ a s : i n t - > s t r i n g - > u n i t

p r i n t _ a s l e n s t r p r i n t s s t r i n t h e c u r r e n t b o x . T h e p r e t t y - p r i n t e r f o r m a t s s t r a s i f i t

w e r e o f l e n g t h l e n .

v a l u e p r i n t _ i n t : i n t - > u n i t

P r i n t a n i n t e g e r i n t h e c u r r e n t b o x .

v a l u e p r i n t _ f l o a t : f l o a t - > u n i t

P r i n t a o a t i n g p o i n t n u m b e r i n t h e c u r r e n t b o x .

v a l u e p r i n t _ c h a r : c h a r - > u n i t

P r i n t a c h a r a c t e r i n t h e c u r r e n t b o x .

v a l u e p r i n t _ b o o l : b o o l - > u n i t

P r i n t a n b o o l e a n i n t h e c u r r e n t b o x .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 144/218

1 4 2

B r e a k h i n t s

v a l u e p r i n t _ s p a c e : u n i t - > u n i t

p r i n t _ s p a c e ( ) i s u s e d t o s e p a r a t e i t e m s ( t y p i c a l l y t o p r i n t a s p a c e b e t w e e n t w o w o r d s ) . I t

i n d i c a t e s t h a t t h e l i n e m a y b e s p l i t a t t h i s p o i n t . I t e i t h e r p r i n t s o n e s p a c e o r s p l i t s t h e l i n e .

I t i s e q u i v a l e n t t o p r i n t _ b r e a k 1 0 .

v a l u e p r i n t _ c u t : u n i t - > u n i t

p r i n t _ c u t ( ) i s u s e d t o m a r k a g o o d b r e a k p o s i t i o n . I t i n d i c a t e s t h a t t h e l i n e m a y b e s p l i t

a t t h i s p o i n t . I t e i t h e r p r i n t s n o t h i n g o r s p l i t s t h e l i n e . T h i s a l l o w s l i n e s p l i t t i n g a t t h e

c u r r e n t p o i n t , w i t h o u t p r i n t i n g s p a c e s o r a d d i n g i n d e n t a t i o n . I t i s e q u i v a l e n t t o

p r i n t _ b r e a k 0 0 .

v a l u e p r i n t _ b r e a k : i n t - > i n t - > u n i t

I n s e r t a b r e a k h i n t i n a p r e t t y - p r i n t i n g b o x . p r i n t _ b r e a k n s p a c e s o f f s e t i n d i c a t e s t h a t

t h e l i n e m a y b e s p l i t ( a n e w l i n e c h a r a c t e r i s p r i n t e d ) a t t h i s p o i n t , i f t h e c o n t e n t s o f t h e

c u r r e n t b o x d o e s n o t t o n o n e l i n e . I f t h e l i n e i s s p l i t a t t h a t p o i n t , o f f s e t i s a d d e d t o t h e

c u r r e n t i n d e n t a t i o n . I f t h e l i n e i s n o t s p l i t , n s p a c e s s p a c e s a r e p r i n t e d .

v a l u e p r i n t _ f l u s h : u n i t - > u n i t

F l u s h t h e p r e t t y p r i n t e r : a l l o p e n e d b o x e s a r e c l o s e d , a n d a l l p e n d i n g t e x t i s d i s p l a y e d .

v a l u e p r i n t _ n e w l i n e : u n i t - > u n i t

E q u i v a l e n t t o p r i n t _ f l u s h f o l l o w e d b y a n e w l i n e .

v a l u e f o r c e _ n e w l i n e : u n i t - > u n i t

F o r c e a n e w l i n e i n t h e c u r r e n t b o x . N o t t h e n o r m a l w a y o f p r e t t y - p r i n t i n g , y o u s h o u l d

p r e f e r b r e a k h i n t s .

v a l u e p r i n t _ i f _ n e w l i n e : u n i t - > u n i t

E x e c u t e t h e n e x t f o r m a t t i n g c o m m a n d i f t h e p r e c e d i n g l i n e h a s j u s t b e e n s p l i t . O t h e r w i s e ,

i g n o r e t h e n e x t f o r m a t t i n g c o m m a n d .

M a r g i n

v a l u e s e t _ m a r g i n : i n t - > u n i t

s e t _ m a r g i n d s e t s t h e v a l u e o f t h e r i g h t m a r g i n t o d ( i n c h a r a c t e r s ) : t h i s v a l u e i s u s e d t o

d e t e c t l i n e o v e r o w s t h a t l e a d s t o s p l i t l i n e s . N o t h i n g h a p p e n s i f d i s s m a l l e r t h a n 2 o r

b i g g e r t h a n 9 9 9 9 9 9 9 9 9 .

v a l u e g e t _ m a r g i n : u n i t - > i n t

R e t u r n t h e p o s i t i o n o f t h e r i g h t m a r g i n .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 145/218

C h a p t e r 1 4 . T h e s t a n d a r d l i b r a r y 1 4 3

M a x i m u m i n d e n t a t i o n l i m i t

v a l u e s e t _ m a x _ i n d e n t : i n t - > u n i t

s e t _ m a x _ i n d e n t d s e t s t h e v a l u e o f t h e m a x i m u m i n d e n t a t i o n l i m i t t o d ( i n c h a r a c t e r s ) :

o n c e t h i s l i m i t i s r e a c h e d , b o x e s a r e r e j e c t e d t o t h e l e f t , i f t h e y d o n o t t o n t h e c u r r e n t l i n e .

N o t h i n g h a p p e n s i f d i s s m a l l e r t h a n 2 o r b i g g e r t h a n 9 9 9 9 9 9 9 9 9 .

v a l u e g e t _ m a x _ i n d e n t : u n i t - > i n t

R e t u r n t h e v a l u e o f t h e m a x i m u m i n d e n t a t i o n l i m i t ( i n c h a r a c t e r s ) .

F o r m a t t i n g d e p t h : m a x i m u m n u m b e r o f b o x e s a l l o w e d b e f o r e e l l i p s i s

v a l u e s e t _ m a x _ b o x e s : i n t - > u n i t

s e t _ m a x _ b o x e s m a x s e t s t h e m a x i m u m n u m b e r o f b o x e s s i m u l t a n e o u s l y o p e n e d . M a t e r i a l

i n s i d e b o x e s n e s t e d d e e p e r i s p r i n t e d a s a n e l l i p s i s ( m o r e p r e c i s e l y a s t h e t e x t r e t u r n e d b y

g e t _ e l l i p s i s _ t e x t ( ) ) . N o t h i n g h a p p e n s i f m a x i s n o t g r e a t e r t h a n 1 .

v a l u e g e t _ m a x _ b o x e s : u n i t - > i n t

R e t u r n t h e m a x i m u m n u m b e r o f b o x e s a l l o w e d b e f o r e e l l i p s i s .

v a l u e o v e r _ m a x _ b o x e s : u n i t - > b o o l

T e s t t h e m a x i m u m n u m b e r o f b o x e s a l l o w e d h a v e a l r e a d y b e e n o p e n e d .

A d v a n c e d f o r m a t t i n g

v a l u e o p e n _ h b o x : u n i t - > u n i t

o p e n _ h b o x ( ) o p e n s a n e w p r e t t y - p r i n t i n g b o x . T h i s b o x i s \ h o r i z o n t a l " : t h e l i n e i s n o t

s p l i t i n t h i s b o x ( n e w l i n e s m a y s t i l l o c c u r i n s i d e b o x e s n e s t e d d e e p e r ) .

v a l u e o p e n _ v b o x : i n t - > u n i t

o p e n _ v b o x d o p e n s a n e w p r e t t y - p r i n t i n g b o x w i t h o s e t d . T h i s b o x i s \ v e r t i c a l " : e v e r y

b r e a k h i n t i n s i d e t h i s b o x l e a d s t o a n e w l i n e . W h e n a n e w l i n e i s p r i n t e d i n t h e b o x , d i s

a d d e d t o t h e c u r r e n t i n d e n t a t i o n .

v a l u e o p e n _ h v b o x : i n t - > u n i t

o p e n _ h v b o x d o p e n s a n e w p r e t t y - p r i n t i n g b o x w i t h o s e t d . T h i s b o x i s

\ h o r i z o n t a l - v e r t i c a l " : i t b e h a v e s a s a n \ h o r i z o n t a l " b o x i f i t t s o n a s i n g l e l i n e , o t h e r w i s e i t

b e h a v e s a s a \ v e r t i c a l " b o x . W h e n a n e w l i n e i s p r i n t e d i n t h e b o x , d i s a d d e d t o t h e

c u r r e n t i n d e n t a t i o n .

v a l u e o p e n _ h o v b o x : i n t - > u n i t

o p e n _ h o v b o x d o p e n s a n e w p r e t t y - p r i n t i n g b o x w i t h o s e t d . T h i s b o x i s \ h o r i z o n t a l o r

v e r t i c a l " : b r e a k h i n t s i n s i d e t h i s b o x m a y l e a d t o a n e w l i n e , i f t h e r e i s n o m o r e r o o m o n t h e

l i n e t o p r i n t t h e r e m a i n d e r o f t h e b o x . W h e n a n e w l i n e i s p r i n t e d i n t h e b o x , d i s a d d e d t o

t h e c u r r e n t i n d e n t a t i o n .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 146/218

1 4 4

T a b u l a t i o n s

v a l u e o p e n _ t b o x : u n i t - > u n i t

O p e n a t a b u l a t i o n b o x .

v a l u e c l o s e _ t b o x : u n i t - > u n i t

C l o s e t h e m o s t r e c e n t l y o p e n e d t a b u l a t i o n b o x .

v a l u e p r i n t _ t b r e a k : i n t - > i n t - > u n i t

B r e a k h i n t i n a t a b u l a t i o n b o x . p r i n t _ t b r e a k s p a c e s o f f s e t m o v e s t h e i n s e r t i o n p o i n t t o

t h e n e x t t a b u l a t i o n ( s p a c e s b e i n g a d d e d t o t h i s p o s i t i o n ) . N o t h i n g o c c u r s i f i n s e r t i o n p o i n t

i s a l r e a d y o n a t a b u l a t i o n m a r k . I f t h e r e i s n o n e x t t a b u l a t i o n o n t h e l i n e , t h e n a n e w l i n e i s

p r i n t e d a n d t h e i n s e r t i o n p o i n t m o v e s t o t h e r s t t a b u l a t i o n o f t h e b o x . I f a n e w l i n e i s

p r i n t e d , o f f s e t i s a d d e d t o t h e c u r r e n t i n d e n t a t i o n .

v a l u e s e t _ t a b : u n i t - > u n i t

S e t a t a b u l a t i o n m a r k a t t h e c u r r e n t i n s e r t i o n p o i n t .

v a l u e p r i n t _ t a b : u n i t - > u n i t

p r i n t _ t a b ( ) i s e q u i v a l e n t t o p r i n t _ t b r e a k ( 0 , 0 ) .

E l l i p s i s

v a l u e s e t _ e l l i p s i s _ t e x t : s t r i n g - > u n i t

S e t t h e t e x t o f t h e e l l i p s i s p r i n t e d w h e n t o o m a n y b o x e s a r e o p e n e d ( a s i n g l e d o t , . , b y

d e f a u l t ) .

v a l u e g e t _ e l l i p s i s _ t e x t : u n i t - > s t r i n g

R e t u r n t h e t e x t o f t h e e l l i p s i s .

R e d i r e c t i n g f o r m a t t e r o u t p u t

v a l u e s e t _ f o r m a t t e r _ o u t _ c h a n n e l : o u t _ c h a n n e l - > u n i t

R e d i r e c t t h e p r e t t y - p r i n t e r o u t p u t t o t h e g i v e n c h a n n e l .

v a l u e s e t _ f o r m a t t e r _ o u t p u t _ f u n c t i o n s :

( s t r i n g - > i n t - > i n t - > u n i t ) - > ( u n i t - > u n i t ) - > u n i t

s e t _ f o r m a t t e r _ o u t p u t _ f u n c t i o n s o u t f l u s h r e d i r e c t s t h e p r e t t y - p r i n t e r o u t p u t t o t h e

f u n c t i o n s o u t a n d f l u s h . T h e o u t f u n c t i o n p e r f o r m s t h e p r e t t y - p r i n t e r o u t p u t . I t i s c a l l e d

w i t h a s t r i n g s , a s t a r t p o s i t i o n p , a n d a n u m b e r o f c h a r a c t e r s n ; i t i s s u p p o s e d t o o u t p u t

c h a r a c t e r s p t o p + n - 1 o f s . T h e f l u s h f u n c t i o n i s c a l l e d w h e n e v e r t h e p r e t t y - p r i n t e r i s

u s h e d u s i n g p r i n t _ f l u s h o r p r i n t _ n e w l i n e .

v a l u e g e t _ f o r m a t t e r _ o u t p u t _ f u n c t i o n s :

u n i t - > ( s t r i n g - > i n t - > i n t - > u n i t ) * ( u n i t - > u n i t )

R e t u r n t h e c u r r e n t o u t p u t f u n c t i o n s o f t h e p r e t t y - p r i n t e r .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 147/218

C h a p t e r 1 4 . T h e s t a n d a r d l i b r a r y 1 4 5

M u l t i p l e f o r m a t t e d o u t p u t

t y p e f o r m a t t e r

A b s t r a c t d a t a t y p e c o r r e s p o n d i n g t o a p r e t t y - p r i n t e r a n d a l l i t s m a c h i n e r y . D e n i n g n e w

p r e t t y - p r i n t e r s p e r m i t s t h e o u t p u t o f m a t e r i a l i n p a r a l l e l o n s e v e r a l c h a n n e l s . P a r a m e t e r s o f

t h e p r e t t y - p r i n t e r a r e l o c a l t o t h e p r e t t y - p r i n t e r : m a r g i n , m a x i m u m i n d e n t a t i o n l i m i t ,

m a x i m u m n u m b e r o f b o x e s s i m u l t a n e o u s l y o p e n e d , e l l i p s i s , a n d s o o n , a r e s p e c i c t o e a c h

p r e t t y - p r i n t e r a n d m a y b e x e d i n d e p e n d e n t l y . A n e w f o r m a t t e r i s o b t a i n e d b y c a l l i n g t h e

m a k e _ f o r m a t t e r f u n c t i o n .

v a l u e s t d _ f o r m a t t e r : f o r m a t t e r

T h e s t a n d a r d f o r m a t t e r u s e d b y t h e f o r m a t t i n g f u n c t i o n s a b o v e . I t i s d e n e d u s i n g

m a k e _ f o r m a t t e r w i t h o u t p u t f u n c t i o n o u t p u t s t d o u t a n d u s h i n g f u n c t i o n

f u n ( ) - > f l u s h s t d o u t .

v a l u e e r r _ f o r m a t t e r : f o r m a t t e r

A f o r m a t t e r t o u s e w i t h f o r m a t t i n g f u n c t i o n s b e l o w f o r o u t p u t t o s t a n d a r d e r r o r . I t i s

d e n e d u s i n g m a k e _ f o r m a t t e r w i t h o u t p u t f u n c t i o n o u t p u t s t d e r r a n d u s h i n g f u n c t i o n

f u n ( ) - > f l u s h s t d e r r .

v a l u e m a k e _ f o r m a t t e r :

( s t r i n g - > i n t - > i n t - > u n i t ) - > ( u n i t - > u n i t ) - > f o r m a t t e r

m a k e _ f o r m a t t e r o u t f l u s h r e t u r n s a n e w f o r m a t t e r t h a t w r i t e s a c c o r d i n g t o t h e o u t p u t

f u n c t i o n o u t , a n d u s h i n g f u n c t i o n f l u s h . H e n c e , a f o r m a t t e r t o o u t c h a n n e l o c i s r e t u r n e d

b y m a k e _ f o r m a t t e r ( o u t p u t o c ) ( f u n ( ) - > f l u s h o c ) .

v a l u e p p _ o p e n _ h b o x : f o r m a t t e r - > u n i t - > u n i t

v a l u e p p _ o p e n _ v b o x : f o r m a t t e r - > i n t - > u n i t

v a l u e p p _ o p e n _ h v b o x : f o r m a t t e r - > i n t - > u n i t

v a l u e p p _ o p e n _ h o v b o x : f o r m a t t e r - > i n t - > u n i t

v a l u e p p _ o p e n _ b o x : f o r m a t t e r - > i n t - > u n i t

v a l u e p p _ c l o s e _ b o x : f o r m a t t e r - > u n i t - > u n i t

v a l u e p p _ p r i n t _ s t r i n g : f o r m a t t e r - > s t r i n g - > u n i t

v a l u e p p _ p r i n t _ a s : f o r m a t t e r - > i n t - > s t r i n g - > u n i t

v a l u e p p _ p r i n t _ i n t : f o r m a t t e r - > i n t - > u n i t

v a l u e p p _ p r i n t _ f l o a t : f o r m a t t e r - > f l o a t - > u n i t

v a l u e p p _ p r i n t _ c h a r : f o r m a t t e r - > c h a r - > u n i t

v a l u e p p _ p r i n t _ b o o l : f o r m a t t e r - > b o o l - > u n i t

v a l u e p p _ p r i n t _ b r e a k : f o r m a t t e r - > i n t - > i n t - > u n i t

v a l u e p p _ p r i n t _ c u t : f o r m a t t e r - > u n i t - > u n i t

v a l u e p p _ p r i n t _ s p a c e : f o r m a t t e r - > u n i t - > u n i t

v a l u e p p _ f o r c e _ n e w l i n e : f o r m a t t e r - > u n i t - > u n i t

v a l u e p p _ p r i n t _ f l u s h : f o r m a t t e r - > u n i t - > u n i t

v a l u e p p _ p r i n t _ n e w l i n e : f o r m a t t e r - > u n i t - > u n i t

v a l u e p p _ p r i n t _ i f _ n e w l i n e : f o r m a t t e r - > u n i t - > u n i t

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 148/218

1 4 6

v a l u e p p _ o p e n _ t b o x : f o r m a t t e r - > u n i t - > u n i t

v a l u e p p _ c l o s e _ t b o x : f o r m a t t e r - > u n i t - > u n i t

v a l u e p p _ p r i n t _ t b r e a k : f o r m a t t e r - > i n t - > i n t - > u n i t

v a l u e p p _ s e t _ t a b : f o r m a t t e r - > u n i t - > u n i t

v a l u e p p _ p r i n t _ t a b : f o r m a t t e r - > u n i t - > u n i t

v a l u e p p _ s e t _ m a r g i n : f o r m a t t e r - > i n t - > u n i t

v a l u e p p _ g e t _ m a r g i n : f o r m a t t e r - > u n i t - > i n t

v a l u e p p _ s e t _ m a x _ i n d e n t : f o r m a t t e r - > i n t - > u n i t

v a l u e p p _ g e t _ m a x _ i n d e n t : f o r m a t t e r - > u n i t - > i n t

v a l u e p p _ s e t _ m a x _ b o x e s : f o r m a t t e r - > i n t - > u n i t

v a l u e p p _ g e t _ m a x _ b o x e s : f o r m a t t e r - > u n i t - > i n t

v a l u e p p _ o v e r _ m a x _ b o x e s : f o r m a t t e r - > u n i t - > b o o l

v a l u e p p _ s e t _ e l l i p s i s _ t e x t : f o r m a t t e r - > s t r i n g - > u n i t

v a l u e p p _ g e t _ e l l i p s i s _ t e x t : f o r m a t t e r - > u n i t - > s t r i n g

v a l u e p p _ s e t _ f o r m a t t e r _ o u t _ c h a n n e l : f o r m a t t e r - > o u t _ c h a n n e l - > u n i t

v a l u e p p _ s e t _ f o r m a t t e r _ o u t p u t _ f u n c t i o n s : f o r m a t t e r - >

( s t r i n g - > i n t - > i n t - > u n i t ) - > ( u n i t - > u n i t ) - > u n i t

v a l u e p p _ g e t _ f o r m a t t e r _ o u t p u t _ f u n c t i o n s :

f o r m a t t e r - > u n i t - > ( s t r i n g - > i n t - > i n t - > u n i t ) * ( u n i t - > u n i t )

T h e b a s i c f u n c t i o n s t o u s e w i t h f o r m a t t e r s . T h e s e f u n c t i o n s a r e t h e b a s i c o n e s : u s u a l

f u n c t i o n s o p e r a t i n g o n t h e s t a n d a r d f o r m a t t e r a r e d e n e d v i a p a r t i a l e v a l u a t i o n o f t h e s e

p r i m i t i v e s . F o r i n s t a n c e , p r i n t _ s t r i n g i s e q u a l t o p p _ p r i n t _ s t r i n g s t d _ f o r m a t t e r .

v a l u e f p r i n t f : f o r m a t t e r - > ( ' a , f o r m a t t e r , u n i t ) f o r m a t - > ' a

f p r i n t f f f f o r m a t a r g 1 . . . a r g N f o r m a t s t h e a r g u m e n t s a r g 1 t o a r g N a c c o r d i n g t o t h e

f o r m a t s t r i n g f o r m a t , a n d o u t p u t s t h e r e s u l t i n g s t r i n g o n t h e f o r m a t t e r f f . T h e f o r m a t i s a

c h a r a c t e r s t r i n g w h i c h c o n t a i n s t h r e e t y p e s o f o b j e c t s : p l a i n c h a r a c t e r s a n d c o n v e r s i o n

s p e c i c a t i o n s a s s p e c i e d i n t h e p r i n t f m o d u l e , a n d p r e t t y - p r i n t i n g i n d i c a t i o n s . T h e

p r e t t y - p r i n t i n g i n d i c a t i o n c h a r a c t e r s a r e i n t r o d u c e d b y a @ c h a r a c t e r , a n d t h e i r m e a n i n g s

a r e :

: o p e n a p r e t t y - p r i n t i n g b o x . T h e t y p e a n d o s e t o f t h e b o x m a y b e o p t i o n a l l y s p e c i e d

w i t h t h e f o l l o w i n g s y n t a x : t h e < c h a r a c t e r , f o l l o w e d b y a n o p t i o n a l b o x t y p e i n d i c a t i o n , t h e n

a n o p t i o n a l i n t e g e r o s e t , a n d t h e c l o s i n g > c h a r a c t e r . B o x t y p e i s o n e o f h , v , h v , o r h o v ,

w h i c h s t a n d r e s p e c t i v e l y f o r a n h o r i z o n t a l , v e r t i c a l , \ h o r i z o n t a l - v e r t i c a l " a n d \ h o r i z o n t a l o r

v e r t i c a l " b o x .

] : c l o s e t h e m o s t r e c e n t l y o p e n e d p r e t t y - p r i n t i n g b o x .

, : o u t p u t a g o o d b r e a k a s w i t h p r i n t _ c u t ( ) .

: o u t p u t a s p a c e , a s w i t h p r i n t _ s p a c e ( ) .

\ n : f o r c e a n e w l i n e , a s w i t h f o r c e _ n e w l i n e ( ) .

; : o u t p u t a g o o d b r e a k a s w i t h p r i n t _ b r e a k . T h e n s p a c e s a n d o f f s e t p a r a m e t e r s o f t h e

b r e a k m a y b e o p t i o n a l l y s p e c i e d w i t h t h e f o l l o w i n g s y n t a x : t h e < c h a r a c t e r , f o l l o w e d b y a n

i n t e g e r n s p a c e s v a l u e , t h e n a n i n t e g e r o s e t , a n d a c l o s i n g > c h a r a c t e r .

. : u s h t h e p r e t t y p r i n t e r a s w i t h p r i n t _ n e w l i n e ( ) .

@ : a p l a i n @ c h a r a c t e r .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 149/218

C h a p t e r 1 4 . T h e s t a n d a r d l i b r a r y 1 4 7

v a l u e p r i n t f : ( ' a , f o r m a t t e r , u n i t ) f o r m a t - > ' a

S a m e a s f p r i n t f , b u t o u t p u t o n s t d _ f o r m a t t e r .

v a l u e e p r i n t f : ( ' a , f o r m a t t e r , u n i t ) f o r m a t - > ' a

S a m e a s f p r i n t f , b u t o u t p u t o n e r r _ f o r m a t t e r .

1 4 . 5 g c : m e m o r y m a n a g e m e n t c o n t r o l a n d s t a t i s t i c s

t y p e s t a t = {

m i n o r _ w o r d s : i n t ;

p r o m o t e d _ w o r d s : i n t ;

m a j o r _ w o r d s : i n t ;

m i n o r _ c o l l e c t i o n s : i n t ;

m a j o r _ c o l l e c t i o n s : i n t ;

h e a p _ w o r d s : i n t ;

h e a p _ c h u n k s : i n t ;

l i v e _ w o r d s : i n t ;

l i v e _ b l o c k s : i n t ;

f r e e _ w o r d s : i n t ;

f r e e _ b l o c k s : i n t ;

l a r g e s t _ w o r d s : i n t ;

f r a g m e n t s : i n t

}

T h e m e m o r y m a n a g e m e n t c o u n t e r s a r e r e t u r n e d i n a s t a t r e c o r d . A l l t h e n u m b e r s a r e

c o m p u t e d s i n c e t h e s t a r t o f t h e p r o g r a m . T h e e l d s o f t h i s r e c o r d a r e :

m i n o r _ w o r d s N u m b e r o f w o r d s a l l o c a t e d i n t h e m i n o r h e a p .

p r o m o t e d _ w o r d s N u m b e r o f w o r d s a l l o c a t e d i n t h e m i n o r h e a p t h a t s u r v i v e d a m i n o r

c o l l e c t i o n a n d w e r e m o v e d t o t h e m a j o r h e a p .

m a j o r _ w o r d s N u m b e r o f w o r d s a l l o c a t e d i n t h e m a j o r h e a p , i n c l u d i n g t h e p r o m o t e d w o r d s .

m i n o r _ c o l l e c t i o n s N u m b e r o f m i n o r c o l l e c t i o n s .

m a j o r _ c o l l e c t i o n s N u m b e r o f m a j o r c o l l e c t i o n c y c l e s , n o t c o u n t i n g t h e c u r r e n t c y c l e .

h e a p _ w o r d s T o t a l s i z e o f t h e m a j o r h e a p , i n w o r d s .

h e a p _ c h u n k s N u m b e r o f t i m e s t h e m a j o r h e a p s i z e w a s i n c r e a s e d .

l i v e _ w o r d s N u m b e r o f w o r d s o f l i v e d a t a i n t h e m a j o r h e a p , i n c l u d i n g t h e h e a d e r w o r d s .

l i v e _ b l o c k s N u m b e r o f l i v e o b j e c t s i n t h e m a j o r h e a p .

f r e e _ w o r d s N u m b e r o f w o r d s i n t h e f r e e l i s t .

f r e e _ b l o c k s N u m b e r o f o b j e c t s i n t h e f r e e l i s t .

l a r g e s t _ w o r d s S i z e ( i n w o r d s ) o f t h e l a r g e s t o b j e c t i n t h e f r e e l i s t .

f r a g m e n t s N u m b e r o f w a s t e d w o r d s d u e t o f r a g m e n t a t i o n . T h e s e a r e 1 - w o r d s f r e e b l o c k s

p l a c e d b e t w e e n t w o l i v e o b j e c t s . T h e y c a n n o t b e i n s e r t e d i n t h e f r e e l i s t , t h u s t h e y a r e n o t

a v a i l a b l e f o r a l l o c a t i o n .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 150/218

1 4 8

T h e t o t a l a m o u n t o f m e m o r y a l l o c a t e d b y t h e p r o g r a m i s ( i n w o r d s )

m i n o r _ w o r d s + m a j o r _ w o r d s - p r o m o t e d _ w o r d s . M u l t i p l y b y t h e w o r d s i z e ( 4 o n a 3 2 - b i t

m a c h i n e , 8 o n a 6 4 - b i t m a c h i n e ) t o g e t t h e n u m b e r o f b y t e s .

t y p e c o n t r o l = {

m u t a b l e m i n o r _ h e a p _ s i z e : i n t ;

m u t a b l e m a j o r _ h e a p _ i n c r e m e n t : i n t ;

m u t a b l e s p a c e _ o v e r h e a d : i n t ;

m u t a b l e v e r b o s e : b o o l

}

T h e G C p a r a m e t e r s a r e g i v e n a s a c o n t r o l r e c o r d . T h e e l d s a r e :

m i n o r _ h e a p _ s i z e T h e s i z e ( i n w o r d s ) o f t h e m i n o r h e a p . C h a n g i n g t h i s p a r a m e t e r w i l l

t r i g g e r a m i n o r c o l l e c t i o n .

m a j o r _ h e a p _ i n c r e m e n t T h e m i n i m u m n u m b e r o f w o r d s t o a d d t o t h e m a j o r h e a p w h e n

i n c r e a s i n g i t .

s p a c e _ o v e r h e a d T h e m a j o r G C s p e e d i s c o m p u t e d f r o m t h i s p a r a m e t e r . T h i s i s t h e

p e r c e n t a g e o f h e a p s p a c e t h a t w i l l b e " w a s t e d " b e c a u s e t h e G C d o e s n o t i m m e d i a t l y c o l l e c t

u n r e a c h a b l e o b j e c t s . T h e G C w i l l w o r k m o r e ( u s e m o r e C P U t i m e a n d c o l l e c t o b j e c t s m o r e

e a g e r l y ) i f s p a c e _ o v e r h e a d i s s m a l l e r . T h e c o m p u t a t i o n o f t h e G C s p e e d a s s u m e s t h a t t h e

a m o u n t o f l i v e d a t a i s c o n s t a n t .

v e r b o s e T h i s a g c o n t r o l s t h e G C m e s s a g e s o n s t a n d a r d e r r o r o u t p u t .

v a l u e s t a t : u n i t - > s t a t

R e t u r n t h e c u r r e n t v a l u e s o f t h e m e m o r y m a n a g e m e n t c o u n t e r s i n a s t a t r e c o r d .

v a l u e p r i n t _ s t a t : i o _ _ o u t _ c h a n n e l - > u n i t

P r i n t t h e c u r r e n t v a l u e s o f t h e m e m o r y m a n a g e m e n t c o u n t e r s ( i n h u m a n - r e a d a b l e f o r m )

i n t o t h e c h a n n e l a r g u m e n t .

v a l u e g e t : u n i t - > c o n t r o l

R e t u r n t h e c u r r e n t v a l u e s o f t h e G C p a r a m e t e r s i n a c o n t r o l r e c o r d .

v a l u e s e t : c o n t r o l - > u n i t

s e t r c h a n g e s t h e G C p a r a m e t e r s a c c o r d i n g t o t h e c o n t r o l r e c o r d r . T h e n o r m a l u s a g e i s :

l e t r = g c _ _ g e t ( ) i n ( * G e t t h e c u r r e n t p a r a m e t e r s . * )

r . v e r b o s e < - t r u e ; ( * C h a n g e s o m e o f t h e m . * )

g c _ _ s e t r ( * S e t t h e n e w v a l u e s . * )

v a l u e m i n o r : u n i t - > u n i t

T r i g g e r a m i n o r c o l l e c t i o n .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 151/218

C h a p t e r 1 4 . T h e s t a n d a r d l i b r a r y 1 4 9

v a l u e m a j o r : u n i t - > u n i t

F i n i s h t h e c u r r e n t m a j o r c o l l e c t i o n c y c l e .

v a l u e f u l l _ m a j o r : u n i t - > u n i t

F i n i s h t h e c u r r e n t m a j o r c o l l e c t i o n c y c l e a n d p e r f o r m a c o m p l e t e n e w c y c l e . T h i s w i l l

c o l l e c t a l l c u r r e n t l y u n r e a c h a b l e o b j e c t s .

1 4 . 6 g e n l e x : a g e n e r i c l e x i c a l a n a l y z e r

T h i s m o d u l e i m p l e m e n t s a s i m p l e \ s t a n d a r d " l e x i c a l a n a l y z e r , p r e s e n t e d a s a f u n c t i o n f r o m

c h a r a c t e r s t r e a m s t o t o k e n s t r e a m s . I t i m p l e m e n t s r o u g h l y t h e l e x i c a l c o n v e n t i o n s o f C a m l ,

b u t i s p a r a m e t e r i z e d b y t h e s e t o f k e y w o r d s o f y o u r l a n g u a g e .

t y p e t o k e n =

K w d o f s t r i n g

| I d e n t o f s t r i n g

| I n t o f i n t

| F l o a t o f f l o a t

| S t r i n g o f s t r i n g

| C h a r o f c h a r

T h e t y p e o f t o k e n s . T h e l e x i c a l c l a s s e s a r e : I n t a n d F l o a t f o r i n t e g e r a n d o a t i n g - p o i n t

n u m b e r s ; S t r i n g f o r s t r i n g l i t e r a l s , e n c l o s e d i n d o u b l e q u o t e s ; C h a r f o r c h a r a c t e r l i t e r a l s ,

e n c l o s e d i n b a c k q u o t e s ; I d e n t f o r i d e n t i e r s ( e i t h e r s e q u e n c e s o f l e t t e r s , d i g i t s , u n d e r s c o r e s

a n d q u o t e s , o r s e q u e n c e s o f \ o p e r a t o r c h a r a c t e r s " s u c h a s + , * , e t c ) ; a n d K w d f o r k e y w o r d s

( e i t h e r i d e n t i e r s o r s i n g l e \ s p e c i a l c h a r a c t e r s " s u c h a s ( , } , e t c ) .

v a l u e m a k e _ l e x e r : s t r i n g l i s t - > ( c h a r s t r e a m - > t o k e n s t r e a m )

C o n s t r u c t t h e l e x e r f u n c t i o n . T h e r s t a r g u m e n t i s t h e l i s t o f k e y w o r d s . A n i d e n t i e r s i s

r e t u r n e d a s K w d s i f s b e l o n g s t o t h i s l i s t , a n d a s I d e n t s o t h e r w i s e . A s p e c i a l c h a r a c t e r s

i s r e t u r n e d a s K w d s i f s b e l o n g s t o t h i s l i s t , a n d c a u s e a l e x i c a l e r r o r ( e x c e p t i o n

P a r s e _ e r r o r ) o t h e r w i s e . B l a n k s a n d n e w l i n e s a r e s k i p p e d . C o m m e n t s d e l i m i t e d b y ( * a n d

* ) a r e s k i p p e d a s w e l l , a n d c a n b e n e s t e d .

E x a m p l e : a l e x e r s u i t a b l e f o r a d e s k c a l c u l a t o r i s o b t a i n e d b y

l e t l e x e r = m a k e _ l e x e r " + " ; " - " ; " * " ; " / " ; " l e t " ; " = " ; " ( " ; " ) " ]

T h e a s s o c i a t e d p a r s e r w o u l d b e a f u n c t i o n f r o m t o k e n s t r e a m t o , f o r i n s t a n c e , i n t , a n d

w o u l d h a v e r u l e s s u c h a s :

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 152/218

1 5 0

l e t p a r s e _ e x p r = f u n c t i o n

< ' I n t n > ] - > n

| < ' K w d " ( " ; p a r s e _ e x p r n ; ' K w d " ) " > ] - > n

| < p a r s e _ e x p r n 1 ; ( p a r s e _ r e m a i n d e r n 1 ) n 2 > ] - > n 2

a n d p a r s e _ r e m a i n d e r n 1 = f u n c t i o n

< ' K w d " + " ; p a r s e _ e x p r n 2 > ] - > n 1 + n 2

| . . .

1 4 . 7 h a s h t b l : h a s h t a b l e s a n d h a s h f u n c t i o n s

H a s h t a b l e s a r e h a s h e d a s s o c i a t i o n t a b l e s , w i t h i n - p l a c e m o d i c a t i o n .

t y p e ( ' a , ' b ) t

T h e t y p e o f h a s h t a b l e s f r o m t y p e ' a t o t y p e ' b .

v a l u e n e w : i n t - > ( ' a , ' b ) t

n e w n c r e a t e s a n e w , e m p t y h a s h t a b l e , w i t h i n i t i a l s i z e n . T h e t a b l e g r o w s a s n e e d e d , s o n

i s j u s t a n i n i t i a l g u e s s . B e t t e r r e s u l t s a r e s a i d t o b e a c h i e v e d w h e n n i s a p r i m e n u m b e r .

R a i s e I n v a l i d _ a r g u m e n t " h a s h t b l _ _ n e w " i f n i s l e s s t h a n 1 .

v a l u e c l e a r : ( ' a , ' b ) t - > u n i t

E m p t y a h a s h t a b l e .

v a l u e a d d : ( ' a , ' b ) t - > ' a - > ' b - > u n i t

a d d t b l x y a d d s a b i n d i n g o f x t o y i n t a b l e t b l . P r e v i o u s b i n d i n g s f o r x a r e n o t

r e m o v e d , b u t s i m p l y h i d d e n . T h a t i s , a f t e r p e r f o r m i n g r e m o v e t b l x , t h e p r e v i o u s b i n d i n g

f o r x , i f a n y , i s r e s t o r e d . ( T h i s i s t h e s e m a n t i c s o f a s s o c i a t i o n l i s t s . )

v a l u e f i n d : ( ' a , ' b ) t - > ' a - > ' b

f i n d t b l x r e t u r n s t h e c u r r e n t b i n d i n g o f x i n t b l , o r r a i s e s N o t _ f o u n d i f n o s u c h b i n d i n g

e x i s t s .

v a l u e f i n d _ a l l : ( ' a , ' b ) t - > ' a - > ' b l i s t

f i n d _ a l l t b l x r e t u r n s t h e l i s t o f a l l d a t a a s s o c i a t e d w i t h x i n t b l . T h e c u r r e n t b i n d i n g i s

r e t u r n e d r s t , t h e n t h e p r e v i o u s b i n d i n g s , i n r e v e r s e o r d e r o f i n t r o d u c t i o n i n t h e t a b l e .

v a l u e r e m o v e : ( ' a , ' b ) t - > ' a - > u n i t

r e m o v e t b l x r e m o v e s t h e c u r r e n t b i n d i n g o f x i n t b l , r e s t o r i n g t h e p r e v i o u s b i n d i n g i f i t

e x i s t s . I t d o e s n o t h i n g i f x i s n o t b o u n d i n t b l .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 153/218

C h a p t e r 1 4 . T h e s t a n d a r d l i b r a r y 1 5 1

v a l u e d o _ t a b l e : ( ' a - > ' b - > u n i t ) - > ( ' a , ' b ) t - > u n i t

d o _ t a b l e f t b l a p p l i e s f t o a l l b i n d i n g s i n t a b l e t b l , d i s c a r d i n g a l l t h e r e s u l t s . f r e c e i v e s

t h e k e y a s r s t a r g u m e n t , a n d t h e a s s o c i a t e d v a l u e a s s e c o n d a r g u m e n t . E a c h b i n d i n g i s

p r e s e n t e d e x a c t l y o n c e t o f . T h e o r d e r i n w h i c h t h e b i n d i n g s a r e p a s s e d t o f i s

u n p r e d i c t a b l e , e x c e p t t h a t s u c c e s s i v e b i n d i n g s f o r t h e s a m e k e y a r e p r e s e n t e d i n r e v e r s e

c h r o n o l o g i c a l o r d e r ( m o s t r e c e n t r s t ) .

v a l u e d o _ t a b l e _ r e v : ( ' a - > ' b - > u n i t ) - > ( ' a , ' b ) t - > u n i t

S a m e a s d o _ t a b l e , e x c e p t t h a t s u c c e s s i v e b i n d i n g s f o r t h e s a m e k e y a r e p r e s e n t e d i n

c h r o n o l o g i c a l o r d e r ( o l d e s t r s t ) .

T h e p o l y m o r p h i c h a s h p r i m i t i v e

v a l u e h a s h : ' a - > i n t

h a s h x a s s o c i a t e s a p o s i t i v e i n t e g e r t o a n y v a l u e o f a n y t y p e . I t i s g u a r a n t e e d t h a t i f x = y ,

t h e n h a s h x = h a s h y . M o r e o v e r , h a s h a l w a y s t e r m i n a t e s , e v e n o n c y c l i c s t r u c t u r e s .

v a l u e h a s h _ p a r a m : i n t - > i n t - > ' a - > i n t

h a s h _ p a r a m n m x c o m p u t e s a h a s h v a l u e f o r x , w i t h t h e s a m e p r o p e r t i e s a s f o r h a s h . T h e

t w o e x t r a p a r a m e t e r s n a n d m g i v e m o r e p r e c i s e c o n t r o l o v e r h a s h i n g . H a s h i n g p e r f o r m s a

d e p t h - r s t , r i g h t - t o - l e f t t r a v e r s a l o f t h e s t r u c t u r e x , s t o p p i n g a f t e r n m e a n i n g f u l n o d e s w e r e

e n c o u n t e r e d , o r m n o d e s , m e a n i n g f u l o r n o t , w e r e e n c o u n t e r e d . M e a n i n g f u l n o d e s a r e :

i n t e g e r s ; o a t i n g - p o i n t n u m b e r s ; s t r i n g s ; c h a r a c t e r s ; b o o l e a n s ; a n d c o n s t a n t c o n s t r u c t o r s .

L a r g e r v a l u e s o f m a n d n m e a n s t h a t m o r e n o d e s a r e t a k e n i n t o a c c o u n t t o c o m p u t e t h e n a l

h a s h v a l u e , a n d t h e r e f o r e c o l l i s i o n s a r e l e s s l i k e l y t o h a p p e n . H o w e v e r , h a s h i n g t a k e s l o n g e r .

T h e p a r a m e t e r s m a n d n g o v e r n t h e t r a d e o b e t w e e n a c c u r a c y a n d s p e e d .

1 4 . 8 l e x i n g : t h e r u n - t i m e l i b r a r y f o r l e x e r s g e n e r a t e d b y

c a m l l e x

L e x e r b u e r s

t y p e l e x b u f =

{ r e f i l l _ b u f f : l e x b u f - > u n i t ;

l e x _ b u f f e r : s t r i n g ;

m u t a b l e l e x _ a b s _ p o s : i n t ;

m u t a b l e l e x _ s t a r t _ p o s : i n t ;

m u t a b l e l e x _ c u r r _ p o s : i n t ;

m u t a b l e l e x _ l a s t _ p o s : i n t ;

m u t a b l e l e x _ l a s t _ a c t i o n : l e x b u f - > o b j }

T h e t y p e o f l e x e r b u e r s . A l e x e r b u e r i s t h e a r g u m e n t p a s s e d t o t h e s c a n n i n g f u n c t i o n s

d e n e d b y t h e g e n e r a t e d s c a n n e r s . T h e l e x e r b u e r h o l d s t h e c u r r e n t s t a t e o f t h e s c a n n e r ,

p l u s a f u n c t i o n t o r e l l t h e b u e r f r o m t h e i n p u t .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 154/218

1 5 2

v a l u e c r e a t e _ l e x e r _ c h a n n e l : i n _ c h a n n e l - > l e x b u f

C r e a t e a l e x e r b u e r o n t h e g i v e n i n p u t c h a n n e l . c r e a t e _ l e x e r _ c h a n n e l i n c h a n r e t u r n s a

l e x e r b u e r w h i c h r e a d s f r o m t h e i n p u t c h a n n e l i n c h a n , a t t h e c u r r e n t r e a d i n g p o s i t i o n .

v a l u e c r e a t e _ l e x e r _ s t r i n g : s t r i n g - > l e x b u f

C r e a t e a l e x e r b u e r w h i c h r e a d s f r o m t h e g i v e n s t r i n g . R e a d i n g s t a r t s f r o m t h e r s t

c h a r a c t e r i n t h e s t r i n g . A n e n d - o f - i n p u t c o n d i t i o n i s g e n e r a t e d w h e n t h e e n d o f t h e s t r i n g i s

r e a c h e d .

v a l u e c r e a t e _ l e x e r : ( s t r i n g - > i n t - > i n t ) - > l e x b u f

C r e a t e a l e x e r b u e r w i t h t h e g i v e n f u n c t i o n a s i t s r e a d i n g m e t h o d . W h e n t h e s c a n n e r n e e d s

m o r e c h a r a c t e r s , i t w i l l c a l l t h e g i v e n f u n c t i o n , g i v i n g i t a c h a r a c t e r s t r i n g s a n d a c h a r a c t e r

c o u n t n . T h e f u n c t i o n s h o u l d p u t n c h a r a c t e r s o r l e s s i n s , s t a r t i n g a t c h a r a c t e r n u m b e r 0 ,

a n d r e t u r n t h e n u m b e r o f c h a r a c t e r s p r o v i d e d . A r e t u r n v a l u e o f 0 m e a n s e n d o f i n p u t .

F u n c t i o n s f o r l e x e r s e m a n t i c a c t i o n s

T h e f o l l o w i n g f u n c t i o n s c a n b e c a l l e d f r o m t h e s e m a n t i c a c t i o n s o f l e x e r d e n i t i o n s ( t h e M L

c o d e e n c l o s e d i n b r a c e s t h a t c o m p u t e s t h e v a l u e r e t u r n e d b y l e x i n g f u n c t i o n s ) . T h e y g i v e

a c c e s s t o t h e c h a r a c t e r s t r i n g m a t c h e d b y t h e r e g u l a r e x p r e s s i o n a s s o c i a t e d w i t h t h e

s e m a n t i c a c t i o n . T h e s e f u n c t i o n s m u s t b e a p p l i e d t o t h e a r g u m e n t l e x b u f , w h i c h , i n t h e

c o d e g e n e r a t e d b y c a m l l e x , i s b o u n d t o t h e l e x e r b u e r p a s s e d t o t h e p a r s i n g f u n c t i o n .

v a l u e g e t _ l e x e m e : l e x b u f - > s t r i n g

g e t _ l e x e m e l e x b u f r e t u r n s t h e s t r i n g m a t c h e d b y t h e r e g u l a r e x p r e s s i o n .

v a l u e g e t _ l e x e m e _ c h a r : l e x b u f - > i n t - > c h a r

g e t _ l e x e m e _ c h a r l e x b u f i r e t u r n s c h a r a c t e r n u m b e r i i n t h e m a t c h e d s t r i n g .

v a l u e g e t _ l e x e m e _ s t a r t : l e x b u f - > i n t

g e t _ l e x e m e _ s t a r t l e x b u f r e t u r n s t h e p o s i t i o n i n t h e i n p u t s t r e a m o f t h e r s t c h a r a c t e r o f

t h e m a t c h e d s t r i n g . T h e r s t c h a r a c t e r o f t h e s t r e a m h a s p o s i t i o n 0 .

v a l u e g e t _ l e x e m e _ e n d : l e x b u f - > i n t

g e t _ l e x e m e _ e n d l e x b u f r e t u r n s t h e p o s i t i o n i n t h e i n p u t s t r e a m o f t h e c h a r a c t e r f o l l o w i n g

t h e l a s t c h a r a c t e r o f t h e m a t c h e d s t r i n g . T h e r s t c h a r a c t e r o f t h e s t r e a m h a s p o s i t i o n 0 .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 155/218

C h a p t e r 1 4 . T h e s t a n d a r d l i b r a r y 1 5 3

1 4 . 9 m a p : a s s o c i a t i o n t a b l e s o v e r o r d e r e d t y p e s

T h i s m o d u l e i m p l e m e n t s a p p l i c a t i v e a s s o c i a t i o n t a b l e s , a l s o k n o w n a s n i t e m a p s o r

d i c t i o n a r i e s , g i v e n a t o t a l o r d e r i n g f u n c t i o n o v e r t h e k e y s . A l l o p e r a t i o n s o v e r m a p s a r e

p u r e l y a p p l i c a t i v e ( n o s i d e - e e c t s ) . T h e i m p l e m e n t a t i o n u s e s b a l a n c e d b i n a r y t r e e s , a n d

t h e r e f o r e s e a r c h i n g a n d i n s e r t i o n t a k e t i m e l o g a r i t h m i c i n t h e s i z e o f t h e m a p .

t y p e ( ' a , ' b ) t

T h e t y p e o f m a p s f r o m t y p e ' a t o t y p e ' b .

v a l u e e m p t y : ( ' a - > ' a - > i n t ) - > ( ' a , ' b ) t

T h e e m p t y m a p . T h e a r g u m e n t i s a t o t a l o r d e r i n g f u n c t i o n o v e r t h e s e t e l e m e n t s . T h i s i s a

t w o - a r g u m e n t f u n c t i o n f s u c h t h a t f e 1 e 2 i s z e r o i f t h e e l e m e n t s e 1 a n d e 2 a r e e q u a l ,

f e 1 e 2 i s s t r i c t l y n e g a t i v e i f e 1 i s s m a l l e r t h a n e 2 , a n d f e 1 e 2 i s s t r i c t l y p o s i t i v e i f e 1 i s

g r e a t e r t h a n e 2 . E x a m p l e s : a s u i t a b l e o r d e r i n g f u n c t i o n f o r t y p e i n t i s p r e f i x - . Y o u c a n

a l s o u s e t h e g e n e r i c s t r u c t u r a l c o m p a r i s o n f u n c t i o n e q _ _ c o m p a r e .

v a l u e a d d : ' a - > ' b - > ( ' a , ' b ) t - > ( ' a , ' b ) t

a d d x y m r e t u r n s a m a p c o n t a i n i n g t h e s a m e b i n d i n g s a s m , p l u s a b i n d i n g o f x t o y .

P r e v i o u s b i n d i n g s f o r x i n m a r e n o t r e m o v e d , b u t s i m p l y h i d d e n : t h e y r e a p p e a r a f t e r

p e r f o r m i n g a r e m o v e o p e r a t i o n . ( T h i s i s t h e s e m a n t i c s o f a s s o c i a t i o n l i s t s . )

v a l u e f i n d : ' a - > ( ' a , ' b ) t - > ' b

f i n d x m r e t u r n s t h e c u r r e n t b i n d i n g o f x i n m , o r r a i s e s N o t _ f o u n d i f n o s u c h b i n d i n g e x i s t s .

v a l u e r e m o v e : ' a - > ( ' a , ' b ) t - > ( ' a , ' b ) t

r e m o v e x m r e t u r n s a m a p c o n t a i n i n g t h e s a m e b i n d i n g s a s m e x c e p t t h e c u r r e n t b i n d i n g f o r

x . T h e p r e v i o u s b i n d i n g f o r x i s r e s t o r e d i f i t e x i s t s . m i s r e t u r n e d u n c h a n g e d i f x i s n o t

b o u n d i n m .

v a l u e i t e r : ( ' a - > ' b - > u n i t ) - > ( ' a , ' b ) t - > u n i t

i t e r f m a p p l i e s f t o a l l b i n d i n g s i n m a p m , d i s c a r d i n g t h e r e s u l t s . f r e c e i v e s t h e k e y a s

r s t a r g u m e n t , a n d t h e a s s o c i a t e d v a l u e a s s e c o n d a r g u m e n t . T h e o r d e r i n w h i c h t h e

b i n d i n g s a r e p a s s e d t o f i s u n s p e c i e d . O n l y c u r r e n t b i n d i n g s a r e p r e s e n t e d t o f : b i n d i n g s

h i d d e n b y m o r e r e c e n t b i n d i n g s a r e n o t p a s s e d t o f .

1 4 . 1 0 p a r s i n g : t h e r u n - t i m e l i b r a r y f o r p a r s e r s g e n e r a t e d b y

c a m l y a c c

v a l u e s y m b o l _ s t a r t : u n i t - > i n t

v a l u e s y m b o l _ e n d : u n i t - > i n t

s y m b o l _ s t a r t a n d s y m b o l _ e n d a r e t o b e c a l l e d i n t h e a c t i o n p a r t o f a g r a m m a r r u l e o n l y .

T h e y r e t u r n t h e p o s i t i o n o f t h e s t r i n g t h a t m a t c h e s t h e l e f t - h a n d s i d e o f t h e r u l e :

s y m b o l _ s t a r t ( ) r e t u r n s t h e p o s i t i o n o f t h e r s t c h a r a c t e r ; s y m b o l _ e n d ( ) r e t u r n s t h e

p o s i t i o n o f t h e l a s t c h a r a c t e r , p l u s o n e . T h e r s t c h a r a c t e r i n a l e i s a t p o s i t i o n 0 .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 156/218

1 5 4

v a l u e r h s _ s t a r t : i n t - > i n t

v a l u e r h s _ e n d : i n t - > i n t

S a m e a s s y m b o l _ s t a r t a n d s y m b o l _ e n d a b o v e , b u t r e t u r n t h e p o s i t i o n o f t h e s t r i n g

m a t c h i n g t h e n t h i t e m o n t h e r i g h t - h a n d s i d e o f t h e r u l e , w h e r e n i s t h e i n t e g e r p a r a m e t e r

t o l h s _ s t a r t a n d l h s _ e n d . n i s 1 f o r t h e l e f t m o s t i t e m .

v a l u e c l e a r _ p a r s e r : u n i t - > u n i t

E m p t y t h e p a r s e r s t a c k . C a l l i t j u s t a f t e r a p a r s i n g f u n c t i o n h a s r e t u r n e d , t o r e m o v e a l l

p o i n t e r s f r o m t h e p a r s e r s t a c k t o s t r u c t u r e s t h a t w e r e b u i l t b y s e m a n t i c a c t i o n s d u r i n g

p a r s i n g . T h i s i s o p t i o n a l , b u t l o w e r s t h e m e m o r y r e q u i r e m e n t s o f t h e p r o g r a m s .

e x c e p t i o n P a r s e _ e r r o r

R a i s e d w h e n a p a r s e r e n c o u n t e r s a s y n t a x e r r o r .

1 4 . 1 1 p r i n t e x c : a c a t c h - a l l e x c e p t i o n h a n d l e r

v a l u e f : ( ' a - > ' b ) - > ' a - > ' b

p r i n t e x c _ _ f f n x a p p l i e s f n t o x a n d r e t u r n s t h e r e s u l t . I f t h e e v a l u a t i o n o f f n x r a i s e s

a n y e x c e p t i o n , t h e n a m e o f t h e e x c e p t i o n i s p r i n t e d o n s t a n d a r d e r r o r o u t p u t , a n d t h e

p r o g r a m s a b o r t s w i t h e x i t c o d e 2 . T y p i c a l u s e i s p r i n t e x c _ _ f m a i n ( ) , w h e r e m a i n , w i t h

t y p e u n i t - > u n i t , i s t h e e n t r y p o i n t o f a s t a n d a l o n e p r o g r a m , t o c a t c h a n d p r i n t s t r a y

e x c e p t i o n s . F o r p r i n t e x c _ _ f t o w o r k p r o p e r l y , t h e p r o g r a m m u s t h a v e b e e n l i n k e d w i t h t h e

- g o p t i o n .

1 4 . 1 2 p r i n t f : f o r m a t t i n g p r i n t i n g f u n c t i o n s

t y p e ( ' a , ' b , ' c ) f o r m a t

T h e t y p e o f f o r m a t s t r i n g s . ' a i s t h e t y p e o f t h e p a r a m e t e r s o f t h e s t r i n g , ' c i s t h e r e s u l t

t y p e f o r t h e p r i n t f - s t y l e f u n c t i o n , a n d ' b i s t h e t y p e o f t h e r s t a r g u m e n t g i v e n t o % a a n d

% t p r i n t i n g f u n c t i o n s .

v a l u e f p r i n t f : o u t _ c h a n n e l - > ( ' a , o u t _ c h a n n e l , u n i t ) f o r m a t - > ' a

f p r i n t f o u t c h a n f o r m a t a r g 1 . . . a r g N f o r m a t s t h e a r g u m e n t s a r g 1 t o a r g N a c c o r d i n g

t o t h e f o r m a t s t r i n g f o r m a t , a n d o u t p u t s t h e r e s u l t i n g s t r i n g o n t h e c h a n n e l o u t c h a n . T h e

f o r m a t i s a c h a r a c t e r s t r i n g w h i c h c o n t a i n s t w o t y p e s o f o b j e c t s : p l a i n c h a r a c t e r s , w h i c h a r e

s i m p l y c o p i e d t o t h e o u t p u t c h a n n e l , a n d c o n v e r s i o n s p e c i c a t i o n s , e a c h o f w h i c h c a u s e s

c o n v e r s i o n a n d p r i n t i n g o f o n e a r g u m e n t . C o n v e r s i o n s p e c i c a t i o n s c o n s i s t i n t h e %

c h a r a c t e r , f o l l o w e d b y o p t i o n a l a g s a n d e l d w i d t h s , f o l l o w e d b y o n e c o n v e r s i o n c h a r a c t e r .

T h e c o n v e r s i o n c h a r a c t e r s a n d t h e i r m e a n i n g s a r e :

d o r i : c o n v e r t a n i n t e g e r a r g u m e n t t o s i g n e d d e c i m a l

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 157/218

C h a p t e r 1 4 . T h e s t a n d a r d l i b r a r y 1 5 5

u : c o n v e r t a n i n t e g e r a r g u m e n t t o u n s i g n e d d e c i m a l

x : c o n v e r t a n i n t e g e r a r g u m e n t t o u n s i g n e d h e x a d e c i m a l , u s i n g l o w e r c a s e l e t t e r s .

X : c o n v e r t a n i n t e g e r a r g u m e n t t o u n s i g n e d h e x a d e c i m a l , u s i n g u p p e r c a s e l e t t e r s .

s : i n s e r t a s t r i n g a r g u m e n t

c : i n s e r t a c h a r a c t e r a r g u m e n t

f : c o n v e r t a o a t i n g - p o i n t a r g u m e n t t o d e c i m a l n o t a t i o n , i n t h e s t y l e d d d d . d d d

e o r E : c o n v e r t a o a t i n g - p o i n t a r g u m e n t t o d e c i m a l n o t a t i o n , i n t h e s t y l e d . d d d e + - d d

( m a n t i s s a a n d e x p o n e n t )

g o r G : c o n v e r t a o a t i n g - p o i n t a r g u m e n t t o d e c i m a l n o t a t i o n , i n s t y l e f o r e , E ( w h i c h e v e r

i s m o r e c o m p a c t )

b : c o n v e r t a b o o l e a n a r g u m e n t t o t h e s t r i n g t r u e o r f a l s e

a : u s e r - d e n e d p r i n t e r . T a k e s t w o a r g u m e n t s a n d a p p l y t h e r s t o n e t o o u t c h a n ( t h e

c u r r e n t o u t p u t c h a n n e l ) a n d t o t h e s e c o n d a r g u m e n t . T h e r s t a r g u m e n t m u s t t h e r e f o r e

h a v e t y p e o u t _ c h a n n e l - > ' b - > u n i t a n d t h e s e c o n d ' b . T h e o u t p u t p r o d u c e d b y t h e

f u n c t i o n i s t h e r e f o r e i n s e r t e d i n t h e o u t p u t o f f p r i n t f a t t h e c u r r e n t p o i n t .

t : s a m e a s % a , b u t t a k e s o n l y o n e a r g u m e n t ( w i t h t y p e o u t _ c h a n n e l - > u n i t ) a n d a p p l y i t

t o o u t c h a n .

R e f e r t o t h e C l i b r a r y p r i n t f f u n c t i o n f o r t h e m e a n i n g o f a g s a n d e l d w i d t h s p e c i e r s . I f

t o o f e w a r g u m e n t s a r e p r o v i d e d , p r i n t i n g s t o p s j u s t b e f o r e c o n v e r t i n g t h e r s t m i s s i n g

a r g u m e n t .

v a l u e p r i n t f : ( ' a , o u t _ c h a n n e l , u n i t ) f o r m a t - > ' a

S a m e a s f p r i n t f , b u t o u t p u t o n s t d _ o u t .

v a l u e e p r i n t f : ( ' a , o u t _ c h a n n e l , u n i t ) f o r m a t - > ' a

S a m e a s f p r i n t f , b u t o u t p u t o n s t d _ e r r .

v a l u e s p r i n t f : ( ' a , u n i t , s t r i n g ) f o r m a t - > ' a

S a m e a s f p r i n t f , e x c e p t t h a t t h e r e s u l t o f t h e f o r m a t t i n g i s r e t u r n e d a s a s t r i n g i n s t e a d o f

b e i n g w r i t t e n o n a c h a n n e l .

v a l u e f p r i n t : o u t _ c h a n n e l - > s t r i n g - > u n i t

P r i n t t h e g i v e n s t r i n g o n t h e g i v e n o u t p u t c h a n n e l , w i t h o u t a n y f o r m a t t i n g . T h i s i s t h e

s a m e f u n c t i o n a s o u t p u t _ s t r i n g o f m o d u l e i o .

v a l u e p r i n t : s t r i n g - > u n i t

P r i n t t h e g i v e n s t r i n g o n s t d _ o u t , w i t h o u t a n y f o r m a t t i n g . T h i s i s t h e s a m e f u n c t i o n a s

p r i n t _ s t r i n g o f m o d u l e i o .

v a l u e e p r i n t : s t r i n g - > u n i t

P r i n t t h e g i v e n s t r i n g o n s t d _ e r r , w i t h o u t a n y f o r m a t t i n g . T h i s i s t h e s a m e f u n c t i o n a s

p r e r r _ s t r i n g o f m o d u l e i o .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 158/218

1 5 6

1 4 . 1 3 q u e u e : q u e u e s

T h i s m o d u l e i m p l e m e n t s q u e u e s ( F I F O s ) , w i t h i n - p l a c e m o d i c a t i o n .

t y p e ' a t

T h e t y p e o f q u e u e s c o n t a i n i n g e l e m e n t s o f t y p e ' a .

e x c e p t i o n E m p t y

R a i s e d w h e n t a k e i s a p p l i e d t o a n e m p t y q u e u e .

v a l u e n e w : u n i t - > ' a t

R e t u r n a n e w q u e u e , i n i t i a l l y e m p t y .

v a l u e a d d : ' a - > ' a t - > u n i t

a d d x q a d d s t h e e l e m e n t x a t t h e e n d o f t h e q u e u e q .

v a l u e t a k e : ' a t - > ' a

t a k e q r e m o v e s a n d r e t u r n s t h e r s t e l e m e n t i n q u e u e q , o r r a i s e s E m p t y i f t h e q u e u e i s

e m p t y .

v a l u e p e e k : ' a t - > ' a

p e e k q r e t u r n s t h e r s t e l e m e n t i n q u e u e q , w i t h o u t r e m o v i n g i t f r o m t h e q u e u e , o r r a i s e s

E m p t y i f t h e q u e u e i s e m p t y .

v a l u e c l e a r : ' a t - > u n i t

D i s c a r d a l l e l e m e n t s f r o m a q u e u e .

v a l u e l e n g t h : ' a t - > i n t

R e t u r n t h e n u m b e r o f e l e m e n t s i n a q u e u e .

v a l u e i t e r : ( ' a - > u n i t ) - > ' a t - > u n i t

i t e r f q a p p l i e s f i n t u r n t o a l l e l e m e n t s o f q , f r o m t h e l e a s t r e c e n t l y e n t e r e d t o t h e m o s t

r e c e n t l y e n t e r e d . T h e q u e u e i t s e l f i s u n c h a n g e d .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 159/218

C h a p t e r 1 4 . T h e s t a n d a r d l i b r a r y 1 5 7

1 4 . 1 4 r a n d o m : p s e u d o - r a n d o m n u m b e r g e n e r a t o r

v a l u e i n i t : i n t - > u n i t

I n i t i a l i z e t h e g e n e r a t o r , u s i n g t h e a r g u m e n t a s a s e e d . T h e s a m e s e e d w i l l a l w a y s y i e l d t h e

s a m e s e q u e n c e o f n u m b e r s .

v a l u e f u l l _ i n i t : i n t v e c t - > u n i t

S a m e a s i n i t b u t t a k e s m o r e d a t a a s s e e d . I t i s n o t u s e f u l t o g i v e m o r e t h a n 5 5 i n t e g e r s .

v a l u e i n t : i n t - > i n t

r a n d o m _ _ i n t b o u n d r e t u r n s a r a n d o m n u m b e r b e t w e e n 0 ( i n c l u s i v e ) a n d b o u n d ( e x c l u s i v e ) .

b o u n d m u s t b e p o s i t i v e a n d s m a l l e r t h a n 2

3 0

.

v a l u e f l o a t : f l o a t - > f l o a t

r a n d o m _ _ f l o a t b o u n d r e t u r n s a r a n d o m n u m b e r b e t w e e n 0 ( i n c l u s i v e ) a n d b o u n d

( e x c l u s i v e ) .

1 4 . 1 5 s e t : s e t s o v e r o r d e r e d t y p e s

T h i s m o d u l e i m p l e m e n t s t h e s e t d a t a s t r u c t u r e , g i v e n a t o t a l o r d e r i n g f u n c t i o n o v e r t h e s e t

e l e m e n t s . A l l o p e r a t i o n s o v e r s e t s a r e p u r e l y a p p l i c a t i v e ( n o s i d e - e e c t s ) . T h e

i m p l e m e n t a t i o n u s e s b a l a n c e d b i n a r y t r e e s , a n d i s t h e r e f o r e r e a s o n a b l y e c i e n t : i n s e r t i o n

a n d m e m b e r s h i p t a k e t i m e l o g a r i t h m i c i n t h e s i z e o f t h e s e t , f o r i n s t a n c e .

t y p e ' a t

T h e t y p e o f s e t s c o n t a i n i n g e l e m e n t s o f t y p e ' a .

v a l u e e m p t y : ( ' a - > ' a - > i n t ) - > ' a t

T h e e m p t y s e t . T h e a r g u m e n t i s a t o t a l o r d e r i n g f u n c t i o n o v e r t h e s e t e l e m e n t s . T h i s i s a

t w o - a r g u m e n t f u n c t i o n f s u c h t h a t f e 1 e 2 i s z e r o i f t h e e l e m e n t s e 1 a n d e 2 a r e e q u a l ,

f e 1 e 2 i s s t r i c t l y n e g a t i v e i f e 1 i s s m a l l e r t h a n e 2 , a n d f e 1 e 2 i s s t r i c t l y p o s i t i v e i f e 1 i s

g r e a t e r t h a n e 2 . E x a m p l e s : a s u i t a b l e o r d e r i n g f u n c t i o n f o r t y p e i n t i s p r e f i x - . Y o u c a n

a l s o u s e t h e g e n e r i c s t r u c t u r a l c o m p a r i s o n f u n c t i o n e q _ _ c o m p a r e .

v a l u e i s _ e m p t y : ' a t - > b o o l

T e s t w h e t h e r a s e t i s e m p t y o r n o t .

v a l u e m e m : ' a - > ' a t - > b o o l

m e m x s t e s t s w h e t h e r x b e l o n g s t o t h e s e t s .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 160/218

1 5 8

v a l u e a d d : ' a - > ' a t - > ' a t

a d d x s r e t u r n s a s e t c o n t a i n i n g a l l e l e m e n t s o f s , p l u s x . I f x w a s a l r e a d y i n s , s i s

r e t u r n e d u n c h a n g e d .

v a l u e r e m o v e : ' a - > ' a t - > ' a t

r e m o v e x s r e t u r n s a s e t c o n t a i n i n g a l l e l e m e n t s o f s , e x c e p t x . I f x w a s n o t i n s , s i s

r e t u r n e d u n c h a n g e d .

v a l u e u n i o n : ' a t - > ' a t - > ' a t

v a l u e i n t e r : ' a t - > ' a t - > ' a t

v a l u e d i f f : ' a t - > ' a t - > ' a t

U n i o n , i n t e r s e c t i o n a n d s e t d i e r e n c e .

v a l u e e q u a l : ' a t - > ' a t - > b o o l

e q u a l s 1 s 2 t e s t s w h e t h e r t h e s e t s s 1 a n d s 2 a r e e q u a l , t h a t i s , c o n t a i n t h e s a m e e l e m e n t s .

v a l u e c o m p a r e : ' a t - > ' a t - > i n t

T o t a l o r d e r i n g b e t w e e n s e t s . C a n b e u s e d a s t h e o r d e r i n g f u n c t i o n f o r d o i n g s e t s o f s e t s .

v a l u e e l e m e n t s : ' a t - > ' a l i s t

R e t u r n t h e l i s t o f a l l e l e m e n t s o f t h e g i v e n s e t . T h e e l e m e n t s a p p e a r i n t h e l i s t i n s o m e

n o n - s p e c i e d o r d e r .

v a l u e i t e r : ( ' a - > u n i t ) - > ' a t - > u n i t

i t e r f s a p p l i e s f i n t u r n t o a l l e l e m e n t s o f s , a n d d i s c a r d s t h e r e s u l t s . T h e e l e m e n t s o f s

a r e p r e s e n t e d t o f i n a n o n - s p e c i e d o r d e r .

v a l u e f o l d : ( ' a - > ' b - > ' b ) - > ' a t - > ' b - > ' b

f o l d f s a c o m p u t e s ( f x N . . . ( f x 2 ( f x 1 a ) ) . . . ) , w h e r e x 1 . . . x N a r e t h e

e l e m e n t s o f s . T h e o r d e r i n w h i c h e l e m e n t s o f s a r e p r e s e n t e d t o f i s n o t s p e c i e d .

v a l u e c h o o s e : ' a t - > ' a

R e t u r n o n e e l e m e n t o f t h e g i v e n s e t , o r r a i s e N o t _ f o u n d i f t h e s e t i s e m p t y . W h i c h e l e m e n t

i s c h o s e n i s n o t s p e c i e d , b u t e q u a l e l e m e n t s w i l l b e c h o s e n f o r e q u a l s e t s .

1 4 . 1 6 s o r t : s o r t i n g a n d m e r g i n g l i s t s

v a l u e s o r t : ( ' a - > ' a - > b o o l ) - > ' a l i s t - > ' a l i s t

S o r t a l i s t i n i n c r e a s i n g o r d e r a c c o r d i n g t o a n o r d e r i n g p r e d i c a t e . T h e p r e d i c a t e s h o u l d

r e t u r n t r u e i f i t s r s t a r g u m e n t i s l e s s t h a n o r e q u a l t o i t s s e c o n d a r g u m e n t .

v a l u e m e r g e : ( ' a - > ' a - > b o o l ) - > ' a l i s t - > ' a l i s t - > ' a l i s t

M e r g e t w o l i s t s a c c o r d i n g t o t h e g i v e n p r e d i c a t e . A s s u m i n g t h e t w o a r g u m e n t l i s t s a r e

s o r t e d a c c o r d i n g t o t h e p r e d i c a t e , m e r g e r e t u r n s a s o r t e d l i s t c o n t a i n i n g t h e e l e m e n t s f r o m

t h e t w o l i s t s . T h e b e h a v i o r i s u n d e n e d i f t h e t w o a r g u m e n t l i s t s w e r e n o t s o r t e d .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 161/218

C h a p t e r 1 4 . T h e s t a n d a r d l i b r a r y 1 5 9

1 4 . 1 7 s t a c k : s t a c k s

T h i s m o d u l e i m p l e m e n t s s t a c k s ( L I F O s ) , w i t h i n - p l a c e m o d i c a t i o n .

t y p e ' a t

T h e t y p e o f s t a c k s c o n t a i n i n g e l e m e n t s o f t y p e ' a .

e x c e p t i o n E m p t y

R a i s e d w h e n p o p i s a p p l i e d t o a n e m p t y s t a c k .

v a l u e n e w : u n i t - > ' a t

R e t u r n a n e w s t a c k , i n i t i a l l y e m p t y .

v a l u e p u s h : ' a - > ' a t - > u n i t

p u s h x s a d d s t h e e l e m e n t x a t t h e t o p o f s t a c k s .

v a l u e p o p : ' a t - > ' a

p o p s r e m o v e s a n d r e t u r n s t h e t o p m o s t e l e m e n t i n s t a c k s , o r r a i s e s E m p t y i f t h e s t a c k i s

e m p t y .

v a l u e c l e a r : ' a t - > u n i t

D i s c a r d a l l e l e m e n t s f r o m a s t a c k .

v a l u e l e n g t h : ' a t - > i n t

R e t u r n t h e n u m b e r o f e l e m e n t s i n a s t a c k .

v a l u e i t e r : ( ' a - > u n i t ) - > ' a t - > u n i t

i t e r f s a p p l i e s f i n t u r n t o a l l e l e m e n t s o f s , f r o m t h e e l e m e n t a t t h e t o p o f t h e s t a c k t o

t h e e l e m e n t a t t h e b o t t o m o f t h e s t a c k . T h e s t a c k i t s e l f i s u n c h a n g e d .

1 4 . 1 8 s y s : s y s t e m i n t e r f a c e

T h i s m o d u l e p r o v i d e s a s i m p l e i n t e r f a c e t o t h e o p e r a t i n g s y s t e m .

e x c e p t i o n S y s _ e r r o r o f s t r i n g

R a i s e d b y s o m e f u n c t i o n s i n t h e s y s a n d i o m o d u l e s , w h e n t h e u n d e r l y i n g s y s t e m c a l l s f a i l .

T h e a r g u m e n t t o S y s _ e r r o r i s a s t r i n g d e s c r i b i n g t h e e r r o r . T h e t e x t s o f t h e e r r o r m e s s a g e s

a r e i m p l e m e n t a t i o n - d e p e n d e n t , a n d s h o u l d n o t b e r e l i e d u p o n t o c a t c h s p e c i c s y s t e m

e r r o r s .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 162/218

1 6 0

v a l u e c o m m a n d _ l i n e : s t r i n g v e c t

T h e c o m m a n d l i n e a r g u m e n t s g i v e n t o t h e p r o c e s s . T h e r s t e l e m e n t i s t h e c o m m a n d n a m e

u s e d t o i n v o k e t h e p r o g r a m .

v a l u e i n t e r a c t i v e : b o o l

T r u e i f w e ' r e r u n n i n g u n d e r t h e t o p l e v e l s y s t e m . F a l s e i f w e ' r e r u n n i n g a s a s t a n d a l o n e

p r o g r a m .

t y p e f i l e _ p e r m = = i n t

v a l u e s _ i r u s r : f i l e _ p e r m

v a l u e s _ i w u s r : f i l e _ p e r m

v a l u e s _ i x u s r : f i l e _ p e r m

v a l u e s _ i r g r p : f i l e _ p e r m

v a l u e s _ i w g r p : f i l e _ p e r m

v a l u e s _ i x g r p : f i l e _ p e r m

v a l u e s _ i r o t h : f i l e _ p e r m

v a l u e s _ i w o t h : f i l e _ p e r m

v a l u e s _ i x o t h : f i l e _ p e r m

v a l u e s _ i s u i d : f i l e _ p e r m

v a l u e s _ i s g i d : f i l e _ p e r m

v a l u e s _ i r a l l : f i l e _ p e r m

v a l u e s _ i w a l l : f i l e _ p e r m

v a l u e s _ i x a l l : f i l e _ p e r m

A c c e s s p e r m i s s i o n s f o r l e s . r i s r e a d i n g p e r m i s s i o n , w i s w r i t i n g p e r m i s s i o n , x i s e x e c u t i o n

p e r m i s s i o n . u s r m e a n s p e r m i s s i o n s f o r t h e u s e r o w n i n g t h e l e , g r p f o r t h e g r o u p o w n i n g

t h e l e , o t h f o r o t h e r s . i s u i d a n d i s g i d a r e f o r s e t - u s e r - i d a n d s e t - g r o u p - i d l e s ,

r e s p e c t i v e l y . T h e r e m a i n i n g a r e c o m b i n a t i o n s o f t h e p e r m i s s i o n s a b o v e .

t y p e o p e n _ f l a g =

O _ R D O N L Y ( * o p e n r e a d - o n l y * )

| O _ W R O N L Y ( * o p e n w r i t e - o n l y * )

| O _ R D W R ( * o p e n f o r r e a d i n g a n d w r i t i n g * )

| O _ A P P E N D ( * o p e n f o r a p p e n d i n g * )

| O _ C R E A T ( * c r e a t e t h e f i l e i f n o n e x i s t e n t * )

| O _ T R U N C ( * t r u n c a t e t h e f i l e t o 0 i f i t e x i s t s * )

| O _ E X C L ( * f a i l s i f t h e f i l e e x i s t s * )

| O _ B I N A R Y ( * o p e n i n b i n a r y m o d e * )

| O _ T E X T ( * o p e n i n t e x t m o d e * )

T h e c o m m a n d s f o r o p e n .

v a l u e e x i t : i n t - > ' a

T e r m i n a t e t h e p r o g r a m a n d r e t u r n t h e g i v e n s t a t u s c o d e t o t h e o p e r a t i n g s y s t e m . I n

c o n t r a s t w i t h t h e f u n c t i o n e x i t f r o m m o d u l e i o , t h i s e x i t f u n c t i o n d o e s n o t u s h t h e

s t a n d a r d o u t p u t a n d s t a n d a r d e r r o r c h a n n e l s .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 163/218

C h a p t e r 1 4 . T h e s t a n d a r d l i b r a r y 1 6 1

v a l u e o p e n : s t r i n g - > o p e n _ f l a g l i s t - > f i l e _ p e r m - > i n t

O p e n a l e . T h e s e c o n d a r g u m e n t i s t h e o p e n i n g m o d e . T h e t h i r d a r g u m e n t i s t h e

p e r m i s s i o n s t o u s e i f t h e l e m u s t b e c r e a t e d . T h e r e s u l t i s a l e d e s c r i p t o r o p e n e d o n t h e

l e .

v a l u e c l o s e : i n t - > u n i t

C l o s e a l e d e s c r i p t o r .

v a l u e r e m o v e : s t r i n g - > u n i t

R e m o v e t h e g i v e n l e n a m e f r o m t h e l e s y s t e m .

v a l u e r e n a m e : s t r i n g - > s t r i n g - > u n i t

R e n a m e a l e . T h e r s t a r g u m e n t i s t h e o l d n a m e a n d t h e s e c o n d i s t h e n e w n a m e .

v a l u e g e t e n v : s t r i n g - > s t r i n g

R e t u r n t h e v a l u e a s s o c i a t e d t o a v a r i a b l e i n t h e p r o c e s s e n v i r o n m e n t . R a i s e N o t _ f o u n d i f

t h e v a r i a b l e i s u n b o u n d .

v a l u e c h d i r : s t r i n g - > u n i t

C h a n g e t h e c u r r e n t w o r k i n g d i r e c t o r y o f t h e p r o c e s s . N o t e t h a t t h e r e i s n o e a s y w a y o f

g e t t i n g t h e c u r r e n t w o r k i n g d i r e c t o r y f r o m t h e o p e r a t i n g s y s t e m .

v a l u e s y s t e m _ c o m m a n d : s t r i n g - > i n t

E x e c u t e t h e g i v e n s h e l l c o m m a n d a n d r e t u r n i t s e x i t c o d e .

v a l u e t i m e : u n i t - > f l o a t

R e t u r n t h e p r o c e s s o r t i m e , i n s e c o n d s , u s e d b y t h e p r o g r a m s i n c e t h e b e g i n n i n g o f e x e c u t i o n .

e x c e p t i o n B r e a k

E x c e p t i o n B r e a k i s r a i s e d o n u s e r i n t e r r u p t i f c a t c h _ b r e a k i s o n .

v a l u e c a t c h _ b r e a k : b o o l - > u n i t

c a t c h _ b r e a k g o v e r n s w h e t h e r u s e r i n t e r r u p t t e r m i n a t e s t h e p r o g r a m o r r a i s e s t h e B r e a k

e x c e p t i o n . C a l l c a t c h _ b r e a k t r u e t o e n a b l e r a i s i n g B r e a k , a n d c a t c h _ b r e a k f a l s e t o l e t

t h e s y s t e m t e r m i n a t e t h e p r o g r a m o n u s e r i n t e r r u p t .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 164/218

1 6 2

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 165/218

C h a p t e r 1 5

T h e g r a p h i c s l i b r a r y

T h i s c h a p t e r d e s c r i b e s t h e p o r t a b l e g r a p h i c s p r i m i t i v e s t h a t c o m e s t a n d a r d i n t h e i m p l e m e n t a t i o n

o f C a m l L i g h t o n m i c r o - c o m p u t e r s .

U n i x : O n U n i x w o r k s t a t i o n s r u n n i n g t h e X 1 1 w i n d o w s s y s t e m , a n i m p l e m e n t a t i o n o f t h e g r a p h i c s

p r i m i t i v e s i s a v a i l a b l e i n t h e d i r e c t o r y c o n t r i b / l i b g r a p h i n t h e d i s t r i b u t i o n . S e e t h e l e

R E A D M E i n t h i s d i r e c t o r y f o r i n f o r m a t i o n o n b u i l d i n g a n d u s i n g c a m l g r a p h , a t o p l e v e l s y s t e m

t h a t i n c l u d e s t h e g r a p h i c s p r i m i t i v e s , a n d l i n k i n g s t a n d a l o n e p r o g r a m s w i t h t h e l i b r a r y .

D r a w i n g t a k e s p l a c e i n a s e p a r a t e w i n d o w t h a t i s c r e a t e d w h e n o p e n _ g r a p h i s c a l l e d .

M a c : T h e g r a p h i c s p r i m i t i v e a r e a v a i l a b l e f r o m t h e s t a n d a l o n e a p p l i c a t i o n t h a t r u n s t h e t o p l e v e l

s y s t e m . T h e y a r e n o t a v a i l a b l e f r o m p r o g r a m s c o m p i l e d b y c a m l c a n d r u n u n d e r t h e M P W

s h e l l . D r a w i n g t a k e s p l a c e i n a s e p a r a t e w i n d o w , t h a t c a n b e m a d e v i s i b l e w i t h t h e \ S h o w

g r a p h i c s w i n d o w " m e n u e n t r y .

P C : T h e g r a p h i c s p r i m i t i v e a r e a v a i l a b l e f r o m t h e W i n d o w s a p p l i c a t i o n t h a t r u n s t h e t o p l e v e l

s y s t e m . T h e y a r e n o t a v a i l a b l e f r o m p r o g r a m s c o m p i l e d b y c a m l c a n d r u n i n a D O S

c o m m a n d w i n d o w . D r a w i n g t a k e s p l a c e i n a s e p a r a t e w i n d o w .

T h e s c r e e n c o o r d i n a t e s a r e i n t e r p r e t e d a s s h o w n i n t h e g u r e b e l o w . N o t i c e t h a t t h e c o o r d i n a t e

s y s t e m u s e d i s t h e s a m e a s i n m a t h e m a t i c s : y i n c r e a s e s f r o m t h e b o t t o m o f t h e s c r e e n t o t h e t o p

o f t h e s c r e e n , a n d a n g l e s a r e m e a s u r e d c o u n t e r c l o c k w i s e ( i n d e g r e e s ) . D r a w i n g i s c l i p p e d t o t h e

s c r e e n .

-

x

6

y

S c r e e n

s i z e _ x ( )

s i z e _ y ( )

p i x e l a t ( x ; y )

x

y

1 6 3

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 166/218

1 6 4

H e r e a r e t h e g r a p h i c s m o d e s p e c i c a t i o n s s u p p o r t e d b y o p e n _ g r a p h o n t h e v a r i o u s i m p l e m e n t a t i o n s

o f t h i s l i b r a r y .

U n i x : T h e a r g u m e n t t o o p e n _ g r a p h h a s t h e f o r m a t " d i s p l a y - n a m e g e o m e t r y " , w h e r e d i s p l a y - n a m e

i s t h e n a m e o f t h e X - w i n d o w s d i s p l a y t o c o n n e c t t o , a n d g e o m e t r y i s a s t a n d a r d X - w i n d o w s

g e o m e t r y s p e c i c a t i o n . T h e t w o c o m p o n e n t s a r e s e p a r a t e d b y a s p a c e . E i t h e r c a n b e

o m i t t e d , o r b o t h . E x a m p l e s :

o p e n _ g r a p h " f o o : 0 "

c o n n e c t s t o t h e d i s p l a y f o o : 0 a n d c r e a t e s a w i n d o w w i t h t h e d e f a u l t g e o m e t r y

o p e n _ g r a p h " f o o : 0 3 0 0 x 1 0 0 + 5 0 - 0 "

c o n n e c t s t o t h e d i s p l a y f o o : 0 a n d c r e a t e s a w i n d o w 3 0 0 p i x e l s w i d e b y 1 0 0 p i x e l s t a l l ,

a t l o c a t i o n ( 5 0 ; 0 )

o p e n _ g r a p h " 3 0 0 x 1 0 0 + 5 0 - 0 "

c o n n e c t s t o t h e d e f a u l t d i s p l a y a n d c r e a t e s a w i n d o w 3 0 0 p i x e l s w i d e b y 1 0 0 p i x e l s t a l l ,

a t l o c a t i o n ( 5 0 ; 0 )

o p e n _ g r a p h " "

c o n n e c t s t o t h e d e f a u l t d i s p l a y a n d c r e a t e s a w i n d o w w i t h t h e d e f a u l t g e o m e t r y .

M a c : T h e a r g u m e n t t o o p e n _ g r a p h i s i g n o r e d .

P C : T h e a r g u m e n t t o o p e n _ g r a p h h a s t h e f o r m a t " w i d t h x h e i g h t " o r " w i d t h x h e i g h t + x + y " , w h e r e

w i d t h a n d h e i g h t a r e t h e i n i t i a l d i m e n s i o n s o f t h e g r a p h i c s w i n d o w s , a n d x a n d y a r e t h e

p o s i t i o n o f t h e u p p e r - l e f t c o r n e r o f t h e g r a p h i c s w i n d o w . I f o m i t t e d , ( w i d t h , h e i g h t ) d e f a u l t

t o ( 6 0 0 , 4 0 0 ) a n d ( x , y ) d e f a u l t t o ( 1 0 , 1 0 ) .

1 5 . 1 g r a p h i c s : m a c h i n e - i n d e p e n d e n t g r a p h i c s p r i m i t i v e s

e x c e p t i o n G r a p h i c _ f a i l u r e o f s t r i n g

R a i s e d b y t h e f u n c t i o n s b e l o w w h e n t h e y e n c o u n t e r a n e r r o r .

I n i t i a l i z a t i o n s

v a l u e o p e n _ g r a p h : s t r i n g - > u n i t

S h o w t h e g r a p h i c s w i n d o w o r s w i t c h t h e s c r e e n t o g r a p h i c m o d e . T h e g r a p h i c s w i n d o w i s

c l e a r e d . T h e s t r i n g a r g u m e n t i s u s e d t o p a s s o p t i o n a l i n f o r m a t i o n o n t h e d e s i r e d g r a p h i c s

m o d e , t h e g r a p h i c s w i n d o w s i z e , a n d s o o n . I t s i n t e r p r e t a t i o n i s i m p l e m e n t a t i o n - d e p e n d e n t .

I f t h e e m p t y s t r i n g i s g i v e n , a s e n s i b l e d e f a u l t i s s e l e c t e d .

v a l u e c l o s e _ g r a p h : u n i t - > u n i t

D e l e t e t h e g r a p h i c s w i n d o w o r s w i t c h t h e s c r e e n b a c k t o t e x t m o d e .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 167/218

C h a p t e r 1 5 . T h e g r a p h i c s l i b r a r y 1 6 5

v a l u e c l e a r _ g r a p h : u n i t - > u n i t

E r a s e t h e g r a p h i c s w i n d o w .

v a l u e s i z e _ x : u n i t - > i n t

v a l u e s i z e _ y : u n i t - > i n t

R e t u r n t h e s i z e o f t h e g r a p h i c s w i n d o w . C o o r d i n a t e s o f t h e s c r e e n p i x e l s r a n g e o v e r

0 . . s i z e _ x ( ) - 1 a n d 0 . . s i z e _ y ( ) - 1 . D r a w i n g s o u t s i d e o f t h i s r e c t a n g l e a r e c l i p p e d ,

w i t h o u t c a u s i n g a n e r r o r . T h e o r i g i n ( 0 , 0 ) i s a t t h e l o w e r l e f t c o r n e r .

C o l o r s

t y p e c o l o r = = i n t

A c o l o r i s s p e c i e d b y i t s R , G , B c o m p o n e n t s . E a c h c o m p o n e n t i s i n t h e r a n g e 0 . . 2 5 5 .

T h e t h r e e c o m p o n e n t s a r e p a c k e d i n a n i n t : 0 x R R G G B B , w h e r e R R a r e t h e t w o h e x a d e c i m a l

d i g i t s f o r t h e r e d c o m p o n e n t , G G f o r t h e g r e e n c o m p o n e n t , B B f o r t h e b l u e c o m p o n e n t .

v a l u e r g b : i n t - > i n t - > i n t - > c o l o r

r g b r g b r e t u r n s t h e i n t e g e r e n c o d i n g t h e c o l o r w i t h r e d c o m p o n e n t r , g r e e n c o m p o n e n t

g , a n d b l u e c o m p o n e n t b . r , g a n d b a r e i n t h e r a n g e 0 . . 2 5 5 .

v a l u e s e t _ c o l o r : c o l o r - > u n i t

S e t t h e c u r r e n t d r a w i n g c o l o r .

v a l u e b l a c k : c o l o r

v a l u e w h i t e : c o l o r

v a l u e r e d : c o l o r

v a l u e g r e e n : c o l o r

v a l u e b l u e : c o l o r

v a l u e y e l l o w : c o l o r

v a l u e c y a n : c o l o r

v a l u e m a g e n t a : c o l o r

S o m e p r e d e n e d c o l o r s .

v a l u e b a c k g r o u n d : c o l o r

v a l u e f o r e g r o u n d : c o l o r

D e f a u l t b a c k g r o u n d a n d f o r e g r o u n d c o l o r s ( u s u a l l y , e i t h e r b l a c k f o r e g r o u n d o n a w h i t e

b a c k g r o u n d o r w h i t e f o r e g r o u n d o n a b l a c k b a c k g r o u n d ) . c l e a r _ g r a p h l l s t h e s c r e e n w i t h

t h e b a c k g r o u n d c o l o r . T h e i n i t i a l d r a w i n g c o l o r i s f o r e g r o u n d .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 168/218

1 6 6

P o i n t a n d l i n e d r a w i n g

v a l u e p l o t : i n t - > i n t - > u n i t

P l o t t h e g i v e n p o i n t w i t h t h e c u r r e n t d r a w i n g c o l o r .

v a l u e p o i n t _ c o l o r : i n t - > i n t - > c o l o r

R e t u r n t h e c o l o r o f t h e g i v e n p o i n t .

v a l u e m o v e t o : i n t - > i n t - > u n i t

P o s i t i o n t h e c u r r e n t p o i n t .

v a l u e c u r r e n t _ p o i n t : u n i t - > i n t * i n t

R e t u r n t h e p o s i t i o n o f t h e c u r r e n t p o i n t .

v a l u e l i n e t o : i n t - > i n t - > u n i t

D r a w a l i n e w i t h e n d p o i n t s t h e c u r r e n t p o i n t a n d t h e g i v e n p o i n t , a n d m o v e t h e c u r r e n t

p o i n t t o t h e g i v e n p o i n t .

v a l u e d r a w _ a r c : i n t - > i n t - > i n t - > i n t - > i n t - > i n t - > u n i t

d r a w _ a r c x y r x r y a 1 a 2 d r a w s a n e l l i p t i c a l a r c w i t h c e n t e r x , y , h o r i z o n t a l r a d i u s r x ,

v e r t i c a l r a d i u s r y , f r o m a n g l e a 1 t o a n g l e a 2 ( i n d e g r e e s ) . T h e c u r r e n t p o i n t i s u n c h a n g e d .

v a l u e d r a w _ e l l i p s e : i n t - > i n t - > i n t - > i n t - > u n i t

d r a w _ e l l i p s e x y r x r y d r a w s a n e l l i p s e w i t h c e n t e r x , y , h o r i z o n t a l r a d i u s r x a n d

v e r t i c a l r a d i u s r y . T h e c u r r e n t p o i n t i s u n c h a n g e d .

v a l u e d r a w _ c i r c l e : i n t - > i n t - > i n t - > u n i t

d r a w _ c i r c l e x y r d r a w s a c i r c l e w i t h c e n t e r x , y a n d r a d i u s r . T h e c u r r e n t p o i n t i s

u n c h a n g e d .

v a l u e s e t _ l i n e _ w i d t h : i n t - > u n i t

S e t t h e w i d t h o f p o i n t s a n d l i n e s d r a w n w i t h t h e f u n c t i o n s a b o v e . U n d e r X W i n d o w s ,

s e t _ l i n e _ w i d t h 0 s e l e c t s a w i d t h o f 1 p i x e l a n d a f a s t e r , b u t l e s s p r e c i s e d r a w i n g

a l g o r i t h m t h a n t h e o n e u s e d w h e n s e t _ l i n e _ w i d t h 1 i s s p e c i e d .

T e x t d r a w i n g

v a l u e d r a w _ c h a r : c h a r - > u n i t

v a l u e d r a w _ s t r i n g : s t r i n g - > u n i t

D r a w a c h a r a c t e r o r a c h a r a c t e r s t r i n g w i t h l o w e r l e f t c o r n e r a t c u r r e n t p o s i t i o n . A f t e r

d r a w i n g , t h e c u r r e n t p o s i t i o n i s s e t t o t h e l o w e r r i g h t c o r n e r o f t h e t e x t d r a w n .

v a l u e s e t _ f o n t : s t r i n g - > u n i t

v a l u e s e t _ t e x t _ s i z e : i n t - > u n i t

S e t t h e f o n t a n d c h a r a c t e r s i z e u s e d f o r d r a w i n g t e x t . T h e i n t e r p r e t a t i o n o f t h e a r g u m e n t s

t o s e t _ f o n t a n d s e t _ t e x t _ s i z e i s i m p l e m e n t a t i o n - d e p e n d e n t .

v a l u e t e x t _ s i z e : s t r i n g - > i n t * i n t

R e t u r n t h e d i m e n s i o n s o f t h e g i v e n t e x t , i f i t w e r e d r a w n w i t h t h e c u r r e n t f o n t a n d s i z e .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 169/218

C h a p t e r 1 5 . T h e g r a p h i c s l i b r a r y 1 6 7

F i l l i n g

v a l u e f i l l _ r e c t : i n t - > i n t - > i n t - > i n t - > u n i t

f i l l _ r e c t x y w h l l s t h e r e c t a n g l e w i t h l o w e r l e f t c o r n e r a t x , y , w i d t h w a n d h e i g t h h ,

w i t h t h e c u r r e n t c o l o r .

v a l u e f i l l _ p o l y : ( i n t * i n t ) v e c t - > u n i t

F i l l t h e g i v e n p o l y g o n w i t h t h e c u r r e n t c o l o r . T h e a r r a y c o n t a i n s t h e c o o r d i n a t e s o f t h e

v e r t i c e s o f t h e p o l y g o n .

v a l u e f i l l _ a r c : i n t - > i n t - > i n t - > i n t - > i n t - > i n t - > u n i t

F i l l a n e l l i p t i c a l p i e s l i c e w i t h t h e c u r r e n t c o l o r . T h e p a r a m e t e r s a r e t h e s a m e a s f o r

d r a w _ a r c .

v a l u e f i l l _ e l l i p s e : i n t - > i n t - > i n t - > i n t - > u n i t

F i l l a n e l l i p s e w i t h t h e c u r r e n t c o l o r . T h e p a r a m e t e r s a r e t h e s a m e a s f o r d r a w _ e l l i p s e .

v a l u e f i l l _ c i r c l e : i n t - > i n t - > i n t - > u n i t

F i l l a c i r c l e w i t h t h e c u r r e n t c o l o r . T h e p a r a m e t e r s a r e t h e s a m e a s f o r d r a w _ c i r c l e .

I m a g e s

t y p e i m a g e

T h e a b s t r a c t t y p e f o r i m a g e s , i n i n t e r n a l r e p r e s e n t a t i o n . E x t e r n a l l y , i m a g e s a r e r e p r e s e n t e d

a s m a t r i c e s o f c o l o r s .

v a l u e t r a n s p : c o l o r

I n m a t r i c e s o f c o l o r s , t h i s c o l o r r e p r e s e n t a \ t r a n s p a r e n t " p o i n t : w h e n d r a w i n g t h e

c o r r e s p o n d i n g i m a g e , a l l p i x e l s o n t h e s c r e e n c o r r e s p o n d i n g t o a t r a n s p a r e n t p i x e l i n t h e

i m a g e w i l l n o t b e m o d i e d , w h i l e o t h e r p o i n t s w i l l b e s e t t o t h e c o l o r o f t h e c o r r e s p o n d i n g

p o i n t i n t h e i m a g e . T h i s a l l o w s s u p e r i m p o s i n g a n i m a g e o v e r a n e x i s t i n g b a c k g r o u n d .

v a l u e m a k e _ i m a g e : c o l o r v e c t v e c t - > i m a g e

C o n v e r t t h e g i v e n c o l o r m a t r i x t o a n i m a g e . E a c h s u b - a r r a y r e p r e s e n t s o n e h o r i z o n t a l l i n e .

A l l s u b - a r r a y s m u s t h a v e t h e s a m e l e n g t h ; o t h e r w i s e , e x c e p t i o n G r a p h i c _ f a i l u r e i s r a i s e d .

v a l u e d u m p _ i m a g e : i m a g e - > c o l o r v e c t v e c t

C o n v e r t a n i m a g e t o a c o l o r m a t r i x .

v a l u e d r a w _ i m a g e : i m a g e - > i n t - > i n t - > u n i t

D r a w t h e g i v e n i m a g e w i t h l o w e r l e f t c o r n e r a t t h e g i v e n p o i n t .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 170/218

1 6 8

v a l u e g e t _ i m a g e : i n t - > i n t - > i n t - > i n t - > i m a g e

C a p t u r e t h e c o n t e n t s o f a r e c t a n g l e o n t h e s c r e e n a s a n i m a g e . T h e p a r a m e t e r s a r e t h e s a m e

a s f o r f i l l _ r e c t .

v a l u e c r e a t e _ i m a g e : i n t - > i n t - > i m a g e

c r e a t e _ i m a g e w h r e t u r n s a n e w i m a g e w p i x e l s w i d e a n d h p i x e l s t a l l , t o b e u s e d i n

c o n j u n c t i o n w i t h b l i t _ i m a g e . T h e i n i t i a l i m a g e c o n t e n t s a r e r a n d o m .

v a l u e b l i t _ i m a g e : i m a g e - > i n t - > i n t - > u n i t

b l i t _ i m a g e i m g x y c o p i e s s c r e e n p i x e l s i n t o t h e i m a g e i m g , m o d i f y i n g i m g i n - p l a c e . T h e

p i x e l s c o p i e d a r e t h o s e i n s i d e t h e r e c t a n g l e w i t h l o w e r l e f t c o r n e r a t x , y , a n d w i d t h a n d

h e i g h t e q u a l t o t h o s e o f t h e i m a g e .

M o u s e a n d k e y b o a r d e v e n t s

t y p e s t a t u s =

{ m o u s e _ x : i n t ; ( * X c o o r d i n a t e o f t h e m o u s e * )

m o u s e _ y : i n t ; ( * Y c o o r d i n a t e o f t h e m o u s e * )

b u t t o n : b o o l ; ( * t r u e i f a m o u s e b u t t o n i s p r e s s e d * )

k e y p r e s s e d : b o o l ; ( * t r u e i f a k e y h a s b e e n p r e s s e d * )

k e y : c h a r } ( * t h e c h a r a c t e r f o r t h e k e y p r e s s e d * )

T o r e p o r t e v e n t s .

t y p e e v e n t =

B u t t o n _ d o w n ( * A m o u s e b u t t o n i s p r e s s e d * )

| B u t t o n _ u p ( * A m o u s e b u t t o n i s r e l e a s e d * )

| K e y _ p r e s s e d ( * A k e y i s p r e s s e d * )

| M o u s e _ m o t i o n ( * T h e m o u s e i s m o v e d * )

| P o l l ( * D o n ' t w a i t ; r e t u r n i m m e d i a t e l y * )

T o s p e c i f y e v e n t s t o w a i t f o r .

v a l u e w a i t _ n e x t _ e v e n t : e v e n t l i s t - > s t a t u s

W a i t u n t i l o n e o f t h e e v e n t s s p e c i e d i n t h e g i v e n e v e n t l i s t o c c u r s , a n d r e t u r n t h e s t a t u s o f

t h e m o u s e a n d k e y b o a r d a t t h a t t i m e . I f P o l l i s g i v e n i n t h e e v e n t l i s t , r e t u r n i m m e d i a t e l y

w i t h t h e c u r r e n t s t a t u s . I f t h e m o u s e c u r s o r i s o u t s i d e o f t h e g r a p h i c s w i n d o w , t h e m o u s e _ x

a n d m o u s e _ y e l d s o f t h e e v e n t a r e o u t s i d e t h e r a n g e 0 . . s i z e _ x ( ) - 1 , 0 . . s i z e _ y ( ) - 1 .

K e y p r e s s e s a r e q u e u e d , a n d d e q u e u e d o n e b y o n e w h e n t h e K e y _ p r e s s e d e v e n t i s s p e c i e d .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 171/218

C h a p t e r 1 5 . T h e g r a p h i c s l i b r a r y 1 6 9

M o u s e a n d k e y b o a r d p o l l i n g

v a l u e m o u s e _ p o s : u n i t - > i n t * i n t

R e t u r n t h e p o s i t i o n o f t h e m o u s e c u r s o r , r e l a t i v e t o t h e g r a p h i c s w i n d o w . I f t h e m o u s e

c u r s o r i s o u t s i d e o f t h e g r a p h i c s w i n d o w , m o u s e _ p o s ( ) r e t u r n s a p o i n t o u t s i d e o f t h e r a n g e

0 . . s i z e _ x ( ) - 1 , 0 . . s i z e _ y ( ) - 1 .

v a l u e b u t t o n _ d o w n : u n i t - > b o o l

R e t u r n t r u e i f t h e m o u s e b u t t o n i s p r e s s e d , f a l s e o t h e r w i s e .

v a l u e r e a d _ k e y : u n i t - > c h a r

W a i t f o r a k e y t o b e p r e s s e d , a n d r e t u r n t h e c o r r e s p o n d i n g c h a r a c t e r . K e y p r e s s e s a r e

q u e u e d .

v a l u e k e y _ p r e s s e d : u n i t - > b o o l

R e t u r n t r u e i f a k e y p r e s s i s a v a i l a b l e ; t h a t i s , i f r e a d _ k e y w o u l d n o t b l o c k .

S o u n d

v a l u e s o u n d : i n t - > i n t - > u n i t

s o u n d f r e q d u r p l a y s a s o u n d a t f r e q u e n c y f r e q ( i n h e r t z ) f o r a d u r a t i o n d u r ( i n

m i l l i s e c o n d s ) . O n t h e M a c i n t o s h , t h e f r e q u e n c y i s r o u n d e d t o t h e n e a r e s t n o t e i n t h e

e q u a l - t e m p e r e d s c a l e .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 172/218

1 7 0

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 173/218

C h a p t e r 1 6

T h e u n i x l i b r a r y : U n i x s y s t e m c a l l s

T h e u n i x l i b r a r y ( d i s t r i b u t e d i n c o n t r i b / l i b u n i x ) m a k e s m a n y U n i x s y s t e m c a l l s a n d s y s t e m -

r e l a t e d l i b r a r y f u n c t i o n s a v a i l a b l e t o C a m l L i g h t p r o g r a m s . T h i s c h a p t e r d e s c r i b e s b r i e y t h e

f u n c t i o n s p r o v i d e d . R e f e r t o s e c t i o n s 2 a n d 3 o f t h e U n i x m a n u a l f o r m o r e d e t a i l s o n t h e b e h a v i o r

o f t h e s e f u n c t i o n s .

N o t a l l f u n c t i o n s a r e p r o v i d e d b y a l l U n i x v a r i a n t s . I f s o m e f u n c t i o n s a r e n o t a v a i l a b l e , t h e y

w i l l r a i s e I n v a l i d _ a r g w h e n c a l l e d .

P r o g r a m s t h a t u s e t h e u n i x l i b r a r y m u s t b e l i n k e d i n \ c u s t o m r u n t i m e " m o d e , a s f o l l o w s :

c a m l c - c u s t o m o t h e r o p t i o n s u n i x . z o o t h e r l e s - l u n i x

F o r i n t e r a c t i v e u s e o f t h e u n i x l i b r a r y , r u n c a m l l i g h t c a m l u n i x .

M a c : T h i s l i b r a r y i s n o t a v a i l a b l e .

P C : T h i s l i b r a r y i s n o t a v a i l a b l e .

1 6 . 1 u n i x : i n t e r f a c e t o t h e U n i x s y s t e m

E r r o r r e p o r t

t y p e e r r o r =

E N O E R R

| E P E R M ( * N o t o w n e r * )

| E N O E N T ( * N o s u c h f i l e o r d i r e c t o r y * )

| E S R C H ( * N o s u c h p r o c e s s * )

| E I N T R ( * I n t e r r u p t e d s y s t e m c a l l * )

| E I O ( * I / O e r r o r * )

| E N X I O ( * N o s u c h d e v i c e o r a d d r e s s * )

| E 2 B I G ( * A r g l i s t t o o l o n g * )

| E N O E X E C ( * E x e c f o r m a t e r r o r * )

| E B A D F ( * B a d f i l e n u m b e r * )

| E C H I L D ( * N o c h i l d r e n * )

| E A G A I N ( * N o m o r e p r o c e s s e s * )

1 7 1

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 174/218

1 7 2

| E N O M E M ( * N o t e n o u g h c o r e * )

| E A C C E S ( * P e r m i s s i o n d e n i e d * )

| E F A U L T ( * B a d a d d r e s s * )

| E N O T B L K ( * B l o c k d e v i c e r e q u i r e d * )

| E B U S Y ( * M o u n t d e v i c e b u s y * )

| E E X I S T ( * F i l e e x i s t s * )

| E X D E V ( * C r o s s - d e v i c e l i n k * )

| E N O D E V ( * N o s u c h d e v i c e * )

| E N O T D I R ( * N o t a d i r e c t o r y * )

| E I S D I R ( * I s a d i r e c t o r y * )

| E I N V A L ( * I n v a l i d a r g u m e n t * )

| E N F I L E ( * F i l e t a b l e o v e r f l o w * )

| E M F I L E ( * T o o m a n y o p e n f i l e s * )

| E N O T T Y ( * N o t a t y p e w r i t e r * )

| E T X T B S Y ( * T e x t f i l e b u s y * )

| E F B I G ( * F i l e t o o l a r g e * )

| E N O S P C ( * N o s p a c e l e f t o n d e v i c e * )

| E S P I P E ( * I l l e g a l s e e k * )

| E R O F S ( * R e a d - o n l y f i l e s y s t e m * )

| E M L I N K ( * T o o m a n y l i n k s * )

| E P I P E ( * B r o k e n p i p e * )

| E D O M ( * A r g u m e n t t o o l a r g e * )

| E R A N G E ( * R e s u l t t o o l a r g e * )

| E W O U L D B L O C K ( * O p e r a t i o n w o u l d b l o c k * )

| E I N P R O G R E S S ( * O p e r a t i o n n o w i n p r o g r e s s * )

| E A L R E A D Y ( * O p e r a t i o n a l r e a d y i n p r o g r e s s * )

| E N O T S O C K ( * S o c k e t o p e r a t i o n o n n o n - s o c k e t * )

| E D E S T A D D R R E Q ( * D e s t i n a t i o n a d d r e s s r e q u i r e d * )

| E M S G S I Z E ( * M e s s a g e t o o l o n g * )

| E P R O T O T Y P E ( * P r o t o c o l w r o n g t y p e f o r s o c k e t * )

| E N O P R O T O O P T ( * P r o t o c o l n o t a v a i l a b l e * )

| E P R O T O N O S U P P O R T ( * P r o t o c o l n o t s u p p o r t e d * )

| E S O C K T N O S U P P O R T ( * S o c k e t t y p e n o t s u p p o r t e d * )

| E O P N O T S U P P ( * O p e r a t i o n n o t s u p p o r t e d o n s o c k e t * )

| E P F N O S U P P O R T ( * P r o t o c o l f a m i l y n o t s u p p o r t e d * )

| E A F N O S U P P O R T ( * A d d r e s s f a m i l y n o t s u p p o r t e d b y p r o t o c o l f a m i l y * )

| E A D D R I N U S E ( * A d d r e s s a l r e a d y i n u s e * )

| E A D D R N O T A V A I L ( * C a n ' t a s s i g n r e q u e s t e d a d d r e s s * )

| E N E T D O W N ( * N e t w o r k i s d o w n * )

| E N E T U N R E A C H ( * N e t w o r k i s u n r e a c h a b l e * )

| E N E T R E S E T ( * N e t w o r k d r o p p e d c o n n e c t i o n o n r e s e t * )

| E C O N N A B O R T E D ( * S o f t w a r e c a u s e d c o n n e c t i o n a b o r t * )

| E C O N N R E S E T ( * C o n n e c t i o n r e s e t b y p e e r * )

| E N O B U F S ( * N o b u f f e r s p a c e a v a i l a b l e * )

| E I S C O N N ( * S o c k e t i s a l r e a d y c o n n e c t e d * )

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 175/218

C h a p t e r 1 6 . T h e u n i x l i b r a r y : U n i x s y s t e m c a l l s 1 7 3

| E N O T C O N N ( * S o c k e t i s n o t c o n n e c t e d * )

| E S H U T D O W N ( * C a n ' t s e n d a f t e r s o c k e t s h u t d o w n * )

| E T O O M A N Y R E F S ( * T o o m a n y r e f e r e n c e s : c a n ' t s p l i c e * )

| E T I M E D O U T ( * C o n n e c t i o n t i m e d o u t * )

| E C O N N R E F U S E D ( * C o n n e c t i o n r e f u s e d * )

| E L O O P ( * T o o m a n y l e v e l s o f s y m b o l i c l i n k s * )

| E N A M E T O O L O N G ( * F i l e n a m e t o o l o n g * )

| E H O S T D O W N ( * H o s t i s d o w n * )

| E H O S T U N R E A C H ( * N o r o u t e t o h o s t * )

| E N O T E M P T Y ( * D i r e c t o r y n o t e m p t y * )

| E P R O C L I M ( * T o o m a n y p r o c e s s e s * )

| E U S E R S ( * T o o m a n y u s e r s * )

| E D Q U O T ( * D i s c q u o t a e x c e e d e d * )

| E S T A L E ( * S t a l e N F S f i l e h a n d l e * )

| E R E M O T E ( * T o o m a n y l e v e l s o f r e m o t e i n p a t h * )

| E I D R M ( * I d e n t i f i e r r e m o v e d * )

| E D E A D L K ( * D e a d l o c k c o n d i t i o n . * )

| E N O L C K ( * N o r e c o r d l o c k s a v a i l a b l e . * )

| E N O S Y S ( * F u n c t i o n n o t i m p l e m e n t e d * )

| E U N K N O W N E R R

T h e t y p e o f e r r o r c o d e s .

e x c e p t i o n U n i x _ e r r o r o f e r r o r * s t r i n g * s t r i n g

R a i s e d b y t h e s y s t e m c a l l s b e l o w w h e n a n e r r o r i s e n c o u n t e r e d . T h e r s t c o m p o n e n t i s t h e

e r r o r c o d e ; t h e s e c o n d c o m p o n e n t i s t h e f u n c t i o n n a m e ; t h e t h i r d c o m p o n e n t i s t h e s t r i n g

p a r a m e t e r t o t h e f u n c t i o n , i f i t h a s o n e , o r t h e e m p t y s t r i n g o t h e r w i s e .

v a l u e e r r o r _ m e s s a g e : e r r o r - > s t r i n g

R e t u r n a s t r i n g d e s c r i b i n g t h e g i v e n e r r o r c o d e .

v a l u e h a n d l e _ u n i x _ e r r o r : ( ' a - > ' b ) - > ' a - > ' b

h a n d l e _ u n i x _ e r r o r f x a p p l i e s f t o x a n d r e t u r n s t h e r e s u l t . I f t h e e x c e p t i o n U n i x _ e r r o r

i s r a i s e d , i t p r i n t s a m e s s a g e d e s c r i b i n g t h e e r r o r a n d e x i t s w i t h c o d e 2 .

I n t e r f a c e w i t h t h e p a r e n t p r o c e s s

v a l u e e n v i r o n m e n t : u n i t - > s t r i n g v e c t

R e t u r n t h e p r o c e s s e n v i r o n m e n t , a s a n a r r a y o f s t r i n g s w i t h t h e f o r m a t \ v a r i a b l e = v a l u e " .

S e e a l s o s y s _ _ g e t e n v .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 176/218

1 7 4

P r o c e s s h a n d l i n g

t y p e p r o c e s s _ s t a t u s =

W E X I T E D o f i n t

| W S I G N A L E D o f i n t * b o o l

| W S T O P P E D o f i n t

T h e t e r m i n a t i o n s t a t u s o f a p r o c e s s . W E X I T E D m e a n s t h a t t h e p r o c e s s t e r m i n a t e d n o r m a l l y

b y e x i t ; t h e a r g u m e n t i s t h e r e t u r n c o d e . W S I G N A L E D m e a n s t h a t t h e p r o c e s s w a s k i l l e d b y

a s i g n a l ; t h e r s t a r g u m e n t i s t h e s i g n a l n u m b e r , t h e s e c o n d a r g u m e n t i n d i c a t e s w h e t h e r a

\ c o r e d u m p " w a s p e r f o r m e d . W S T O P P E D m e a n s t h a t t h e p r o c e s s w a s s t o p p e d b y a s i g n a l ; t h e

a r g u m e n t i s t h e s i g n a l n u m b e r .

t y p e w a i t _ f l a g =

W N O H A N G

| W U N T R A C E D

F l a g s f o r w a i t o p t a n d w a i t p i d . W N O H A N G m e a n s d o n o t b l o c k i f n o c h i l d h a s d i e d y e t , b u t

i m m e d i a t e l y r e t u r n w i t h a p i d e q u a l t o 0 . W U N T R A C E D m e a n s r e p o r t a l s o t h e c h i l d r e n t h a t

r e c e i v e s t o p s i g n a l s .

v a l u e e x e c v : s t r i n g - > s t r i n g v e c t - > u n i t

e x e c v p r o g a r g s e x e c u t e t h e p r o g r a m i n l e p r o g , w i t h t h e a r g u m e n t s a r g s , a n d t h e

c u r r e n t p r o c e s s e n v i r o n m e n t .

v a l u e e x e c v e : s t r i n g - > s t r i n g v e c t - > s t r i n g v e c t - > u n i t

S a m e a s e x e c v , e x c e p t t h a t t h e t h i r d a r g u m e n t p r o v i d e s t h e e n v i r o n m e n t t o t h e p r o g r a m

e x e c u t e d .

v a l u e e x e c v p : s t r i n g - > s t r i n g v e c t - > u n i t

S a m e a s e x e c v , e x c e p t t h a t t h e p r o g r a m i s s e a r c h e d i n t h e p a t h .

v a l u e f o r k : u n i t - > i n t

F o r k a n e w p r o c e s s . T h e r e t u r n e d i n t e g e r i s 0 f o r t h e c h i l d p r o c e s s , t h e p i d o f t h e c h i l d

p r o c e s s f o r t h e p a r e n t p r o c e s s .

v a l u e w a i t : u n i t - > i n t * p r o c e s s _ s t a t u s

W a i t u n t i l o n e o f t h e c h i l d r e n p r o c e s s e s d i e , a n d r e t u r n i t s p i d a n d t e r m i n a t i o n s t a t u s .

v a l u e w a i t o p t : w a i t _ f l a g l i s t - > i n t * p r o c e s s _ s t a t u s

S a m e a s w a i t , b u t t a k e s a l i s t o f o p t i o n s t o a v o i d b l o c k i n g , o r a l s o r e p o r t s t o p p e d c h i l d r e n .

T h e p i d r e t u r n e d i s 0 i f n o c h i l d h a s c h a n g e d s t a t u s .

v a l u e w a i t p i d : w a i t _ f l a g l i s t - > i n t - > i n t * p r o c e s s _ s t a t u s

S a m e a s w a i t o p t , b u t w a i t s f o r t h e p r o c e s s w h o s e p i d i s g i v e n . N e g a t i v e p i d a r g u m e n t s

r e p r e s e n t p r o c e s s g r o u p s .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 177/218

C h a p t e r 1 6 . T h e u n i x l i b r a r y : U n i x s y s t e m c a l l s 1 7 5

v a l u e s y s t e m : s t r i n g - > p r o c e s s _ s t a t u s

E x e c u t e t h e g i v e n c o m m a n d , w a i t u n t i l i t t e r m i n a t e s , a n d r e t u r n i t s t e r m i n a t i o n s t a t u s .

T h e s t r i n g i s i n t e r p r e t e d b y t h e s h e l l / b i n / s h a n d t h e r e f o r e c a n c o n t a i n r e d i r e c t i o n s ,

q u o t e s , v a r i a b l e s , e t c . T h e r e s u l t W E X I T E D 1 2 7 i n d i c a t e s t h a t t h e s h e l l c o u l d n ' t b e e x e c u t e d .

v a l u e g e t p i d : u n i t - > i n t

R e t u r n t h e p i d o f t h e p r o c e s s .

v a l u e g e t p p i d : u n i t - > i n t

R e t u r n t h e p i d o f t h e p a r e n t p r o c e s s .

v a l u e n i c e : i n t - > i n t

C h a n g e t h e p r o c e s s p r i o r i t y . T h e i n t e g e r a r g u m e n t i s a d d e d t o t h e \ n i c e " v a l u e . ( H i g h e r

v a l u e s o f t h e \ n i c e " v a l u e m e a n l o w e r p r i o r i t i e s . ) R e t u r n t h e n e w n i c e v a l u e .

B a s i c l e i n p u t / o u t p u t

t y p e f i l e _ d e s c r

T h e a b s t r a c t t y p e o f l e d e s c r i p t o r s .

v a l u e s t d i n : f i l e _ d e s c r

v a l u e s t d o u t : f i l e _ d e s c r

v a l u e s t d e r r : f i l e _ d e s c r

F i l e d e s c r i p t o r s f o r s t a n d a r d i n p u t , s t a n d a r d o u t p u t a n d s t a n d a r d e r r o r .

t y p e o p e n _ f l a g =

O _ R D O N L Y ( * O p e n f o r r e a d i n g * )

| O _ W R O N L Y ( * O p e n f o r w r i t i n g * )

| O _ R D W R ( * O p e n f o r r e a d i n g a n d w r i t i n g * )

| O _ N D E L A Y ( * O p e n i n n o n - b l o c k i n g m o d e * )

| O _ A P P E N D ( * O p e n f o r a p p e n d * )

| O _ C R E A T ( * C r e a t e i f n o n e x i s t e n t * )

| O _ T R U N C ( * T r u n c a t e t o 0 l e n g t h i f e x i s t i n g * )

| O _ E X C L ( * F a i l i f e x i s t i n g * )

T h e a g s t o o p e n .

t y p e f i l e _ p e r m = = i n t

T h e t y p e o f l e a c c e s s r i g h t s .

v a l u e o p e n : s t r i n g - > o p e n _ f l a g l i s t - > f i l e _ p e r m - > f i l e _ d e s c r

O p e n t h e n a m e d l e w i t h t h e g i v e n a g s . T h i r d a r g u m e n t i s t h e p e r m i s s i o n s t o g i v e t o t h e

l e i f i t i s c r e a t e d . R e t u r n a l e d e s c r i p t o r o n t h e n a m e d l e .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 178/218

1 7 6

v a l u e c l o s e : f i l e _ d e s c r - > u n i t

C l o s e a l e d e s c r i p t o r .

v a l u e r e a d : f i l e _ d e s c r - > s t r i n g - > i n t - > i n t - > i n t

r e a d f d b u f f s t a r t l e n r e a d s l e n c h a r a c t e r s f r o m d e s c r i p t o r f d , s t o r i n g t h e m i n s t r i n g

b u f f , s t a r t i n g a t p o s i t i o n o f s i n s t r i n g b u f f . R e t u r n t h e n u m b e r o f c h a r a c t e r s a c t u a l l y r e a d .

v a l u e w r i t e : f i l e _ d e s c r - > s t r i n g - > i n t - > i n t - > i n t

w r i t e f d b u f f s t a r t l e n w r i t e s l e n c h a r a c t e r s t o d e s c r i p t o r f d , t a k i n g t h e m f r o m s t r i n g

b u f f , s t a r t i n g a t p o s i t i o n o f s i n s t r i n g b u f f . R e t u r n t h e n u m b e r o f c h a r a c t e r s a c t u a l l y

w r i t t e n .

I n t e r f a c i n g w i t h t h e s t a n d a r d i n p u t / o u t p u t l i b r a r y ( m o d u l e i o ) .

v a l u e i n _ c h a n n e l _ o f _ d e s c r : f i l e _ d e s c r - > i n _ c h a n n e l

C r e a t e a n i n p u t c h a n n e l r e a d i n g f r o m t h e g i v e n d e s c r i p t o r .

v a l u e o u t _ c h a n n e l _ o f _ d e s c r : f i l e _ d e s c r - > o u t _ c h a n n e l

C r e a t e a n o u t p u t c h a n n e l w r i t i n g o n t h e g i v e n d e s c r i p t o r .

v a l u e d e s c r _ o f _ i n _ c h a n n e l : i n _ c h a n n e l - > f i l e _ d e s c r

R e t u r n t h e d e s c r i p t o r c o r r e s p o n d i n g t o a n i n p u t c h a n n e l .

v a l u e d e s c r _ o f _ o u t _ c h a n n e l : o u t _ c h a n n e l - > f i l e _ d e s c r

R e t u r n t h e d e s c r i p t o r c o r r e s p o n d i n g t o a n o u t p u t c h a n n e l .

S e e k i n g a n d t r u n c a t i n g

t y p e s e e k _ c o m m a n d =

S E E K _ S E T

| S E E K _ C U R

| S E E K _ E N D

P o s i t i o n i n g m o d e s f o r l s e e k . S E E K _ S E T i n d i c a t e s p o s i t i o n s r e l a t i v e t o t h e b e g i n n i n g o f t h e

l e , S E E K _ C U R r e l a t i v e t o t h e c u r r e n t p o s i t i o n , S E E K _ E N D r e l a t i v e t o t h e e n d o f t h e l e .

v a l u e l s e e k : f i l e _ d e s c r - > i n t - > s e e k _ c o m m a n d - > i n t

S e t t h e c u r r e n t p o s i t i o n f o r a l e d e s c r i p t o r

v a l u e t r u n c a t e : s t r i n g - > i n t - > u n i t

T r u n c a t e s t h e n a m e d l e t o t h e g i v e n s i z e .

v a l u e f t r u n c a t e : f i l e _ d e s c r - > i n t - > u n i t

T r u n c a t e s t h e l e c o r r e s p o n d i n g t o t h e g i v e n d e s c r i p t o r t o t h e g i v e n s i z e .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 179/218

C h a p t e r 1 6 . T h e u n i x l i b r a r y : U n i x s y s t e m c a l l s 1 7 7

F i l e s t a t i s t i c s

t y p e f i l e _ k i n d =

S _ R E G ( * R e g u l a r f i l e * )

| S _ D I R ( * D i r e c t o r y * )

| S _ C H R ( * C h a r a c t e r d e v i c e * )

| S _ B L K ( * B l o c k d e v i c e * )

| S _ L N K ( * S y m b o l i c l i n k * )

| S _ F I F O ( * N a m e d p i p e * )

| S _ S O C K ( * S o c k e t * )

t y p e s t a t s =

{ s t _ d e v : i n t ; ( * D e v i c e n u m b e r * )

s t _ i n o : i n t ; ( * I n o d e n u m b e r * )

s t _ k i n d : f i l e _ k i n d ; ( * K i n d o f t h e f i l e * )

s t _ p e r m : f i l e _ p e r m ; ( * A c c e s s r i g h t s * )

s t _ n l i n k : i n t ; ( * N u m b e r o f l i n k s * )

s t _ u i d : i n t ; ( * U s e r i d o f t h e o w n e r * )

s t _ g i d : i n t ; ( * G r o u p i d o f t h e o w n e r * )

s t _ r d e v : i n t ; ( * D e v i c e m i n o r n u m b e r * )

s t _ s i z e : i n t ; ( * S i z e i n b y t e s * )

s t _ a t i m e : i n t ; ( * L a s t a c c e s s t i m e * )

s t _ m t i m e : i n t ; ( * L a s t m o d i f i c a t i o n t i m e * )

s t _ c t i m e : i n t } ( * L a s t s t a t u s c h a n g e t i m e * )

T h e i n f o r m a t i o n s r e t u r n e d b y t h e s t a t c a l l s .

v a l u e s t a t : s t r i n g - > s t a t s

R e t u r n t h e i n f o r m a t i o n f o r t h e n a m e d l e .

v a l u e l s t a t : s t r i n g - > s t a t s

S a m e a s s t a t , b u t i n c a s e t h e l e i s a s y m b o l i c l i n k , r e t u r n t h e i n f o r m a t i o n f o r t h e l i n k i t s e l f .

v a l u e f s t a t : f i l e _ d e s c r - > s t a t s

R e t u r n t h e i n f o r m a t i o n f o r t h e l e a s s o c i a t e d w i t h t h e g i v e n d e s c r i p t o r .

O p e r a t i o n s o n l e n a m e s

v a l u e u n l i n k : s t r i n g - > u n i t

R e m o v e s t h e n a m e d l e

v a l u e r e n a m e : s t r i n g - > s t r i n g - > u n i t

r e n a m e o l d n e w c h a n g e s t h e n a m e o f a l e f r o m o l d t o n e w .

v a l u e l i n k : s t r i n g - > s t r i n g - > u n i t

l i n k s o u r c e d e s t c r e a t e s a h a r d l i n k n a m e d d e s t t o t h e l e n a m e d n e w .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 180/218

1 7 8

F i l e p e r m i s s i o n s a n d o w n e r s h i p

t y p e a c c e s s _ p e r m i s s i o n =

R _ O K ( * R e a d p e r m i s s i o n * )

| W _ O K ( * W r i t e p e r m i s s i o n * )

| X _ O K ( * E x e c u t i o n p e r m i s s i o n * )

| F _ O K ( * F i l e e x i s t s * )

F l a g s f o r t h e a c c e s s c a l l .

v a l u e c h m o d : s t r i n g - > f i l e _ p e r m - > u n i t

C h a n g e t h e p e r m i s s i o n s o f t h e n a m e d l e .

v a l u e f c h m o d : f i l e _ d e s c r - > f i l e _ p e r m - > u n i t

C h a n g e t h e p e r m i s s i o n s o f a n o p e n e d l e .

v a l u e c h o w n : s t r i n g - > i n t - > i n t - > u n i t

C h a n g e t h e o w n e r u i d a n d o w n e r g i d o f t h e n a m e d l e .

v a l u e f c h o w n : f i l e _ d e s c r - > i n t - > i n t - > u n i t

C h a n g e t h e o w n e r u i d a n d o w n e r g i d o f a n o p e n e d l e .

v a l u e u m a s k : i n t - > i n t

S e t t h e p r o c e s s c r e a t i o n m a s k , a n d r e t u r n t h e p r e v i o u s m a s k .

v a l u e a c c e s s : s t r i n g - > a c c e s s _ p e r m i s s i o n l i s t - > u n i t

C h e c k t h a t t h e p r o c e s s h a s t h e g i v e n p e r m i s s i o n s o v e r t h e n a m e d l e . R a i s e U n i x _ e r r o r

o t h e r w i s e .

F i l e d e s c r i p t o r h a c k i n g

v a l u e f c n t l _ i n t : f i l e _ d e s c r - > i n t - > i n t - > i n t

I n t e r f a c e t o f c n t l i n t h e c a s e w h e r e t h e a r g u m e n t i s a n i n t e g e r . T h e r s t i n t e g e r a r g u m e n t

i s t h e c o m m a n d c o d e ; t h e s e c o n d i s t h e i n t e g e r p a r a m e t e r .

v a l u e f c n t l _ p t r : f i l e _ d e s c r - > i n t - > s t r i n g - > i n t

I n t e r f a c e t o f c n t l i n t h e c a s e w h e r e t h e a r g u m e n t i s a p o i n t e r . T h e i n t e g e r a r g u m e n t i s t h e

c o m m a n d c o d e . A p o i n t e r t o t h e s t r i n g a r g u m e n t i s p a s s e d a s a r g u m e n t t o t h e c o m m a n d .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 181/218

C h a p t e r 1 6 . T h e u n i x l i b r a r y : U n i x s y s t e m c a l l s 1 7 9

D i r e c t o r i e s

v a l u e m k d i r : s t r i n g - > f i l e _ p e r m - > u n i t

C r e a t e a d i r e c t o r y w i t h t h e g i v e n p e r m i s s i o n s .

v a l u e r m d i r : s t r i n g - > u n i t

R e m o v e a n e m p t y d i r e c t o r y .

v a l u e c h d i r : s t r i n g - > u n i t

C h a n g e t h e p r o c e s s w o r k i n g d i r e c t o r y .

v a l u e g e t c w d : u n i t - > s t r i n g

R e t u r n t h e n a m e o f t h e c u r r e n t w o r k i n g d i r e c t o r y .

t y p e d i r _ h a n d l e

T h e t y p e o f d e s c r i p t o r s o v e r o p e n e d d i r e c t o r i e s .

v a l u e o p e n d i r : s t r i n g - > d i r _ h a n d l e

O p e n a d e s c r i p t o r o n a d i r e c t o r y

v a l u e r e a d d i r : d i r _ h a n d l e - > s t r i n g

R e t u r n t h e n e x t e n t r y i n a d i r e c t o r y . R a i s e E n d _ o f _ f i l e w h e n t h e e n d o f t h e d i r e c t o r y h a s

b e e n r e a c h e d .

v a l u e r e w i n d d i r : d i r _ h a n d l e - > u n i t

R e p o s i t i o n t h e d e s c r i p t o r t o t h e b e g i n n i n g o f t h e d i r e c t o r y

v a l u e c l o s e d i r : d i r _ h a n d l e - > u n i t

C l o s e a d i r e c t o r y d e s c r i p t o r .

P i p e s a n d r e d i r e c t i o n s

v a l u e p i p e : u n i t - > f i l e _ d e s c r * f i l e _ d e s c r

C r e a t e a p i p e . T h e r s t c o m p o n e n t o f t h e r e s u l t i s o p e n e d f o r r e a d i n g , t h a t ' s t h e e x i t t o t h e

p i p e . T h e s e c o n d c o m p o n e n t i s o p e n e d f o r w r i t i n g , t h a t ' s t h e e n t r a c e t o t h e p i p e .

v a l u e d u p : f i l e _ d e s c r - > f i l e _ d e s c r

D u p l i c a t e a d e s c r i p t o r .

v a l u e d u p 2 : f i l e _ d e s c r - > f i l e _ d e s c r - > u n i t

d u p 2 f d 1 f d 2 d u p l i c a t e s f d 1 t o f d 2 , c l o s i n g f d 2 i f a l r e a d y o p e n e d .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 182/218

1 8 0

v a l u e o p e n _ p r o c e s s _ i n : s t r i n g - > i n _ c h a n n e l

v a l u e o p e n _ p r o c e s s _ o u t : s t r i n g - > o u t _ c h a n n e l

v a l u e o p e n _ p r o c e s s : s t r i n g - > i n _ c h a n n e l * o u t _ c h a n n e l

H i g h - l e v e l p i p e a n d p r o c e s s m a n a g e m e n t . T h e s e f u n c t i o n s r u n t h e g i v e n c o m m a n d i n

p a r a l l e l w i t h t h e p r o g r a m , a n d r e t u r n c h a n n e l s c o n n e c t e d t o t h e s t a n d a r d i n p u t a n d / o r t h e

s t a n d a r d o u t p u t o f t h e c o m m a n d . T h e c o m m a n d i s i n t e r p r e t e d b y t h e s h e l l / b i n / s h ( c f .

s y s t e m ) . W a r n i n g : w r i t e s o n c h a n n e l s a r e b u e r e d , h e n c e b e c a r e f u l t o c a l l f l u s h a t t h e

r i g h t t i m e s t o e n s u r e c o r r e c t s y n c h r o n i z a t i o n .

v a l u e c l o s e _ p r o c e s s _ i n : i n _ c h a n n e l - > p r o c e s s _ s t a t u s

v a l u e c l o s e _ p r o c e s s _ o u t : o u t _ c h a n n e l - > p r o c e s s _ s t a t u s

v a l u e c l o s e _ p r o c e s s : i n _ c h a n n e l * o u t _ c h a n n e l - > p r o c e s s _ s t a t u s

C l o s e c h a n n e l s o p e n e d b y o p e n _ p r o c e s s _ i n , o p e n _ p r o c e s s _ o u t a n d o p e n _ p r o c e s s ,

r e s p e c t i v e l y , w a i t f o r t h e a s s o c i a t e d c o m m a n d t o t e r m i n a t e , a n d r e t u r n i t s t e r m i n a t i o n

s t a t u s .

S y m b o l i c l i n k s

v a l u e s y m l i n k : s t r i n g - > s t r i n g - > u n i t

s y m l i n k s o u r c e d e s t c r e a t e s t h e l e d e s t a s a s y m b o l i c l i n k t o t h e l e s o u r c e .

v a l u e r e a d l i n k : s t r i n g - > s t r i n g

R e a d t h e c o n t e n t s o f a l i n k .

N a m e d p i p e s

v a l u e m k f i f o : s t r i n g - > f i l e _ p e r m - > u n i t

C r e a t e a n a m e d p i p e w i t h t h e g i v e n p e r m i s s i o n s .

S p e c i a l l e s

v a l u e i o c t l _ i n t : f i l e _ d e s c r - > i n t - > i n t - > i n t

I n t e r f a c e t o i o c t l i n t h e c a s e w h e r e t h e a r g u m e n t i s a n i n t e g e r . T h e r s t i n t e g e r a r g u m e n t

i s t h e c o m m a n d c o d e ; t h e s e c o n d i s t h e i n t e g e r p a r a m e t e r .

v a l u e i o c t l _ p t r : f i l e _ d e s c r - > i n t - > s t r i n g - > i n t

I n t e r f a c e t o i o c t l i n t h e c a s e w h e r e t h e a r g u m e n t i s a p o i n t e r . T h e i n t e g e r a r g u m e n t i s t h e

c o m m a n d c o d e . A p o i n t e r t o t h e s t r i n g a r g u m e n t i s p a s s e d a s a r g u m e n t t o t h e c o m m a n d .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 183/218

C h a p t e r 1 6 . T h e u n i x l i b r a r y : U n i x s y s t e m c a l l s 1 8 1

P o l l i n g

v a l u e s e l e c t :

f i l e _ d e s c r l i s t - > f i l e _ d e s c r l i s t - > f i l e _ d e s c r l i s t - > f l o a t - >

f i l e _ d e s c r l i s t * f i l e _ d e s c r l i s t * f i l e _ d e s c r l i s t

W a i t u n t i l s o m e i n p u t / o u t p u t o p e r a t i o n s b e c o m e p o s s i b l e o n s o m e c h a n n e l s . T h e t h r e e l i s t

a r g u m e n t s a r e , r e s p e c t i v e l y , a s e t o f d e s c r i p t o r s t o c h e c k f o r r e a d i n g ( r s t a r g u m e n t ) , f o r

w r i t i n g ( s e c o n d a r g u m e n t ) , o r f o r e x c e p t i o n a l c o n d i t i o n s ( t h i r d a r g u m e n t ) . T h e f o u r t h

a r g u m e n t i s t h e m a x i m a l t i m e o u t , i n s e c o n d s ; a n e g a t i v e f o u r t h a r g u m e n t m e a n s n o t i m e o u t

( u n b o u n d e d w a i t ) . T h e r e s u l t i s c o m p o s e d o f t h r e e s e t s o f d e s c r i p t o r s : t h o s e r e a d y f o r

r e a d i n g ( r s t c o m p o n e n t ) , r e a d y f o r w r i t i n g ( s e c o n d c o m p o n e n t ) , a n d o v e r w h i c h a n

e x c e p t i o n a l c o n d i t i o n i s p e n d i n g ( t h i r d c o m p o n e n t ) .

L o c k i n g

t y p e l o c k _ c o m m a n d =

F _ U L O C K ( * U n l o c k a r e g i o n * )

| F _ L O C K ( * L o c k a r e g i o n , a n d b l o c k i f a l r e a d y l o c k e d * )

| F _ T L O C K ( * L o c k a r e g i o n , o r f a i l i f a l r e a d y l o c k e d * )

| F _ T E S T ( * T e s t a r e g i o n f o r o t h e r p r o c e s s ' l o c k s * )

C o m m a n d s f o r l o c k f .

v a l u e l o c k f : f i l e _ d e s c r - > l o c k _ c o m m a n d - > i n t - > u n i t

l o c k f f d c m d s i z e p u t s a l o c k o n a r e g i o n o f t h e l e o p e n e d a s f d . T h e r e g i o n s t a r t s a t t h e

c u r r e n t r e a d / w r i t e p o s i t i o n f o r f d ( a s s e t b y l s e e k ) , a n d e x t e n d s s i z e b y t e s f o r w a r d i f s i z e

i s p o s i t i v e , s i z e b y t e s b a c k w a r d s i f s i z e i s n e g a t i v e , o r t o t h e e n d o f t h e l e i f s i z e i s z e r o .

S i g n a l s

t y p e s i g n a l =

S I G H U P ( * h a n g u p * )

| S I G I N T ( * i n t e r r u p t * )

| S I G Q U I T ( * q u i t * )

| S I G I L L ( * i l l e g a l i n s t r u c t i o n ( n o t r e s e t w h e n c a u g h t ) * )

| S I G T R A P ( * t r a c e t r a p ( n o t r e s e t w h e n c a u g h t ) * )

| S I G A B R T ( * u s e d b y a b o r t * )

| S I G E M T ( * E M T i n s t r u c t i o n * )

| S I G F P E ( * f l o a t i n g p o i n t e x c e p t i o n * )

| S I G K I L L ( * k i l l ( c a n n o t b e c a u g h t o r i g n o r e d ) * )

| S I G B U S ( * b u s e r r o r * )

| S I G S E G V ( * s e g m e n t a t i o n v i o l a t i o n * )

| S I G S Y S ( * b a d a r g u m e n t t o s y s t e m c a l l * )

| S I G P I P E ( * w r i t e o n a p i p e w i t h n o o n e t o r e a d i t * )

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 184/218

1 8 2

| S I G A L R M ( * a l a r m c l o c k * )

| S I G T E R M ( * s o f t w a r e t e r m i n a t i o n s i g n a l f r o m k i l l * )

| S I G U R G ( * u r g e n t c o n d i t i o n o n I O c h a n n e l * )

| S I G S T O P ( * s e n d a b l e s t o p s i g n a l n o t f r o m t t y * )

| S I G T S T P ( * s t o p s i g n a l f r o m t t y * )

| S I G C O N T ( * c o n t i n u e a s t o p p e d p r o c e s s * )

| S I G C H L D ( * t o p a r e n t o n c h i l d s t o p o r e x i t * )

| S I G I O ( * i n p u t / o u t p u t p o s s i b l e s i g n a l * )

| S I G X C P U ( * e x c e e d e d C P U t i m e l i m i t * )

| S I G X F S Z ( * e x c e e d e d f i l e s i z e l i m i t * )

| S I G V T A L R M ( * v i r t u a l t i m e a l a r m * )

| S I G P R O F ( * p r o f i l i n g t i m e a l a r m * )

| S I G W I N C H ( * w i n d o w c h a n g e d * )

| S I G L O S T ( * r e s o u r c e l o s t ( e g , r e c o r d - l o c k l o s t ) * )

| S I G U S R 1 ( * u s e r d e f i n e d s i g n a l 1 * )

| S I G U S R 2 ( * u s e r d e f i n e d s i g n a l 2 * )

T h e t y p e o f s i g n a l s .

t y p e s i g n a l _ h a n d l e r =

S i g n a l _ d e f a u l t ( * D e f a u l t b e h a v i o r f o r t h e s i g n a l * )

| S i g n a l _ i g n o r e ( * I g n o r e t h e s i g n a l * )

| S i g n a l _ h a n d l e o f ( u n i t - > u n i t ) ( * C a l l t h e g i v e n f u n c t i o n

w h e n t h e s i g n a l o c c u r s . * )

T h e b e h a v i o r o n r e c e i p t o f a s i g n a l

v a l u e k i l l : i n t - > s i g n a l - > u n i t

S e n d a s i g n a l t o t h e p r o c e s s w i t h t h e g i v e n p r o c e s s i d .

v a l u e s i g n a l : s i g n a l - > s i g n a l _ h a n d l e r - > u n i t

S e t t h e b e h a v i o r t o b e t a k e n o n r e c e i p t o f t h e g i v e n s i g n a l .

v a l u e p a u s e : u n i t - > u n i t

W a i t u n t i l a n o n - i g n o r e d s i g n a l i s d e l i v e r e d .

T i m e f u n c t i o n s

t y p e p r o c e s s _ t i m e s =

{ t m s _ u t i m e : f l o a t ; ( * U s e r t i m e f o r t h e p r o c e s s * )

t m s _ s t i m e : f l o a t ; ( * S y s t e m t i m e f o r t h e p r o c e s s * )

t m s _ c u t i m e : f l o a t ; ( * U s e r t i m e f o r t h e c h i l d r e n p r o c e s s e s * )

t m s _ c s t i m e : f l o a t } ( * S y s t e m t i m e f o r t h e c h i l d r e n p r o c e s s e s * )

T h e e x e c u t i o n t i m e s ( C P U t i m e s ) o f a p r o c e s s .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 185/218

C h a p t e r 1 6 . T h e u n i x l i b r a r y : U n i x s y s t e m c a l l s 1 8 3

t y p e t m =

{ t m _ s e c : i n t ; ( * S e c o n d s 0 . . 5 9 * )

t m _ m i n : i n t ; ( * M i n u t e s 0 . . 5 9 * )

t m _ h o u r : i n t ; ( * H o u r s 0 . . 2 3 * )

t m _ m d a y : i n t ; ( * D a y o f m o n t h 1 . . 3 1 * )

t m _ m o n : i n t ; ( * M o n t h o f y e a r 0 . . 1 1 * )

t m _ y e a r : i n t ; ( * Y e a r - 1 9 0 0 * )

t m _ w d a y : i n t ; ( * D a y o f w e e k ( S u n d a y i s 0 ) * )

t m _ y d a y : i n t ; ( * D a y o f y e a r 0 . . 3 6 5 * )

t m _ i s d s t : b o o l } ( * D a y l i g h t t i m e s a v i n g s i n e f f e c t * )

T h e t y p e r e p r e s e n t i n g w a l l c l o c k t i m e a n d c a l e n d a r d a t e .

v a l u e t i m e : u n i t - > i n t

R e t u r n t h e c u r r e n t t i m e s i n c e 0 0 : 0 0 : 0 0 G M T , J a n . 1 , 1 9 7 0 , i n s e c o n d s .

v a l u e g e t t i m e o f d a y : u n i t - > f l o a t

S a m e a s t i m e , b u t w i t h r e s o l u t i o n b e t t e r t h a n 1 s e c o n d .

v a l u e g m t i m e : i n t - > t m

C o n v e r t a t i m e i n s e c o n d s , a s r e t u r n e d b y t i m e , i n t o a d a t e a n d a t i m e . A s s u m e s G r e e n w i c h

m e r i d i a n t i m e z o n e .

v a l u e l o c a l t i m e : i n t - > t m

C o n v e r t a t i m e i n s e c o n d s , a s r e t u r n e d b y t i m e , i n t o a d a t e a n d a t i m e . A s s u m e s t h e l o c a l

t i m e z o n e .

v a l u e a l a r m : i n t - > i n t

S c h e d u l e a S I G A L R M s i g n a l s a f t e r t h e g i v e n n u m b e r o f s e c o n d s .

v a l u e s l e e p : i n t - > u n i t

S t o p e x e c u t i o n f o r t h e g i v e n n u m b e r o f s e c o n d s .

v a l u e t i m e s : u n i t - > p r o c e s s _ t i m e s

R e t u r n t h e e x e c u t i o n t i m e s o f t h e p r o c e s s .

v a l u e u t i m e s : s t r i n g - > i n t - > i n t - > u n i t

S e t t h e l a s t a c c e s s t i m e ( s e c o n d a r g ) a n d l a s t m o d i c a t i o n t i m e ( t h i r d a r g ) f o r a l e . T i m e s

a r e e x p r e s s e d i n s e c o n d s f r o m 0 0 : 0 0 : 0 0 G M T , J a n . 1 , 1 9 7 0 .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 186/218

1 8 4

U s e r i d , g r o u p i d

v a l u e g e t u i d : u n i t - > i n t

R e t u r n t h e u s e r i d o f t h e u s e r e x e c u t i n g t h e p r o c e s s .

v a l u e g e t e u i d : u n i t - > i n t

R e t u r n t h e e e c t i v e u s e r i d u n d e r w h i c h t h e p r o c e s s r u n s .

v a l u e s e t u i d : i n t - > u n i t

S e t t h e r e a l u s e r i d a n d e e c t i v e u s e r i d f o r t h e p r o c e s s .

v a l u e g e t g i d : u n i t - > i n t

R e t u r n t h e g r o u p i d o f t h e u s e r e x e c u t i n g t h e p r o c e s s .

v a l u e g e t e g i d : u n i t - > i n t

R e t u r n t h e e e c t i v e g r o u p i d u n d e r w h i c h t h e p r o c e s s r u n s .

v a l u e s e t g i d : i n t - > u n i t

S e t t h e r e a l g r o u p i d a n d e e c t i v e g r o u p i d f o r t h e p r o c e s s .

v a l u e g e t g r o u p s : u n i t - > i n t v e c t

R e t u r n t h e l i s t o f g r o u p s t o w h i c h t h e u s e r e x e c u t i n g t h e p r o c e s s b e l o n g s .

t y p e p a s s w d _ e n t r y =

{ p w _ n a m e : s t r i n g ;

p w _ p a s s w d : s t r i n g ;

p w _ u i d : i n t ;

p w _ g i d : i n t ;

p w _ g e c o s : s t r i n g ;

p w _ d i r : s t r i n g ;

p w _ s h e l l : s t r i n g }

S t r u c t u r e o f e n t r i e s i n t h e p a s s w d d a t a b a s e .

t y p e g r o u p _ e n t r y =

{ g r _ n a m e : s t r i n g ;

g r _ p a s s w d : s t r i n g ;

g r _ g i d : i n t ;

g r _ m e m : s t r i n g v e c t }

S t r u c t u r e o f e n t r i e s i n t h e g r o u p s d a t a b a s e .

v a l u e g e t l o g i n : u n i t - > s t r i n g

R e t u r n t h e l o g i n n a m e o f t h e u s e r e x e c u t i n g t h e p r o c e s s .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 187/218

C h a p t e r 1 6 . T h e u n i x l i b r a r y : U n i x s y s t e m c a l l s 1 8 5

v a l u e g e t p w n a m : s t r i n g - > p a s s w d _ e n t r y

F i n d a n e n t r y i n p a s s w d w i t h t h e g i v e n n a m e , o r r a i s e N o t _ f o u n d .

v a l u e g e t g r n a m : s t r i n g - > g r o u p _ e n t r y

F i n d a n e n t r y i n g r o u p w i t h t h e g i v e n n a m e , o r r a i s e N o t _ f o u n d .

v a l u e g e t p w u i d : i n t - > p a s s w d _ e n t r y

F i n d a n e n t r y i n p a s s w d w i t h t h e g i v e n u s e r i d , o r r a i s e N o t _ f o u n d .

v a l u e g e t g r g i d : i n t - > g r o u p _ e n t r y

F i n d a n e n t r y i n g r o u p w i t h t h e g i v e n g r o u p i d , o r r a i s e N o t _ f o u n d .

I n t e r n e t a d d r e s s e s

t y p e i n e t _ a d d r

T h e a b s t r a c t t y p e o f I n t e r n e t a d d r e s s e s .

v a l u e i n e t _ a d d r _ o f _ s t r i n g : s t r i n g - > i n e t _ a d d r

v a l u e s t r i n g _ o f _ i n e t _ a d d r : i n e t _ a d d r - > s t r i n g

C o n v e r s i o n s b e t w e e n s t r i n g w i t h t h e f o r m a t X X X . Y Y Y . Z Z Z . T T T a n d I n t e r n e t a d d r e s s e s .

i n e t _ a d d r _ o f _ s t r i n g r a i s e s F a i l u r e w h e n g i v e n a s t r i n g t h a t d o e s n o t m a t c h t h i s f o r m a t .

S o c k e t s

t y p e s o c k e t _ d o m a i n =

P F _ U N I X ( * U n i x d o m a i n * )

| P F _ I N E T ( * I n t e r n e t d o m a i n * )

T h e t y p e o f s o c k e t d o m a i n s .

t y p e s o c k e t _ t y p e =

S O C K _ S T R E A M ( * S t r e a m s o c k e t * )

| S O C K _ D G R A M ( * D a t a g r a m s o c k e t * )

| S O C K _ R A W ( * R a w s o c k e t * )

| S O C K _ S E Q P A C K E T ( * S e q u e n c e d p a c k e t s s o c k e t * )

T h e t y p e o f s o c k e t k i n d s , s p e c i f y i n g t h e s e m a n t i c s o f c o m m u n i c a t i o n s .

t y p e s o c k a d d r =

A D D R _ U N I X o f s t r i n g

| A D D R _ I N E T o f i n e t _ a d d r * i n t

T h e t y p e o f s o c k e t a d d r e s s e s . A D D R _ U N I X n a m e i s a s o c k e t a d d r e s s i n t h e U n i x d o m a i n ; n a m e

i s a l e n a m e i n t h e l e s y s t e m . A D D R _ I N E T ( a d d r , p o r t ) i s a s o c k e t a d d r e s s i n t h e I n t e r n e t

d o m a i n ; a d d r i s t h e I n t e r n e t a d d r e s s o f t h e m a c h i n e , a n d p o r t i s t h e p o r t n u m b e r .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 188/218

1 8 6

t y p e s h u t d o w n _ c o m m a n d =

S H U T D O W N _ R E C E I V E ( * C l o s e f o r r e c e i v i n g * )

| S H U T D O W N _ S E N D ( * C l o s e f o r s e n d i n g * )

| S H U T D O W N _ A L L ( * C l o s e b o t h * )

T h e t y p e o f c o m m a n d s f o r s h u t d o w n .

t y p e m s g _ f l a g =

M S G _ O O B

| M S G _ D O N T R O U T E

| M S G _ P E E K

T h e a g s f o r r e c v , r e c v f r o m , s e n d a n d s e n d t o .

v a l u e s o c k e t : s o c k e t _ d o m a i n - > s o c k e t _ t y p e - > i n t - > f i l e _ d e s c r

C r e a t e a n e w s o c k e t i n t h e g i v e n d o m a i n , a n d w i t h t h e g i v e n k i n d . T h e t h i r d a r g u m e n t i s

t h e p r o t o c o l t y p e ; 0 s e l e c t s t h e d e f a u l t p r o t o c o l f o r t h a t k i n d o f s o c k e t s .

v a l u e s o c k e t p a i r :

s o c k e t _ d o m a i n - > s o c k e t _ t y p e - > i n t - > f i l e _ d e s c r * f i l e _ d e s c r

C r e a t e a p a i r o f u n n a m e d s o c k e t s , c o n n e c t e d t o g e t h e r .

v a l u e a c c e p t : f i l e _ d e s c r - > f i l e _ d e s c r * s o c k a d d r

A c c e p t c o n n e c t i o n s o n t h e g i v e n s o c k e t . T h e r e t u r n e d d e s c r i p t o r i s a s o c k e t c o n n e c t e d t o

t h e c l i e n t ; t h e r e t u r n e d a d d r e s s i s t h e a d d r e s s o f t h e c o n n e c t i n g c l i e n t .

v a l u e b i n d : f i l e _ d e s c r - > s o c k a d d r - > u n i t

B i n d a s o c k e t t o a n a d d r e s s .

v a l u e c o n n e c t : f i l e _ d e s c r - > s o c k a d d r - > u n i t

C o n n e c t a s o c k e t t o a n a d d r e s s .

v a l u e l i s t e n : f i l e _ d e s c r - > i n t - > u n i t

S e t u p a s o c k e t f o r r e c e i v i n g c o n n e c t i o n r e q u e s t s . T h e i n t e g e r a r g u m e n t i s t h e m a x i m a l

n u m b e r o f p e n d i n g r e q u e s t s .

v a l u e s h u t d o w n : f i l e _ d e s c r - > s h u t d o w n _ c o m m a n d - > u n i t

S h u t d o w n a s o c k e t c o n n e c t i o n . S H U T D O W N _ S E N D a s s e c o n d a r g u m e n t c a u s e s r e a d s o n t h e

o t h e r e n d o f t h e c o n n e c t i o n t o r e t u r n a n e n d - o f - l e c o n d i t i o n . S H U T D O W N _ R E C E I V E c a u s e s

w r i t e s o n t h e o t h e r e n d o f t h e c o n n e c t i o n t o r e t u r n a c l o s e d p i p e c o n d i t i o n ( S I G P I P E s i g n a l ) .

v a l u e g e t s o c k n a m e : f i l e _ d e s c r - > s o c k a d d r

R e t u r n t h e a d d r e s s o f t h e g i v e n s o c k e t .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 189/218

C h a p t e r 1 6 . T h e u n i x l i b r a r y : U n i x s y s t e m c a l l s 1 8 7

v a l u e g e t p e e r n a m e : f i l e _ d e s c r - > s o c k a d d r

R e t u r n t h e a d d r e s s o f t h e h o s t c o n n e c t e d t o t h e g i v e n s o c k e t .

v a l u e r e c v : f i l e _ d e s c r - > s t r i n g - > i n t - > i n t - > m s g _ f l a g l i s t - > i n t

v a l u e r e c v f r o m :

f i l e _ d e s c r - > s t r i n g - > i n t - > i n t - > m s g _ f l a g l i s t - > i n t * s o c k a d d r

R e c e i v e d a t a f r o m a n u n c o n n e c t e d s o c k e t .

v a l u e s e n d : f i l e _ d e s c r - > s t r i n g - > i n t - > i n t - > m s g _ f l a g l i s t - > i n t

v a l u e s e n d t o :

f i l e _ d e s c r - > s t r i n g - > i n t - > i n t - > m s g _ f l a g l i s t - > s o c k a d d r - > i n t

S e n d d a t a o v e r a n u n c o n n e c t e d s o c k e t .

H i g h - l e v e l n e t w o r k c o n n e c t i o n f u n c t i o n s

v a l u e o p e n _ c o n n e c t i o n : s o c k a d d r - > i n _ c h a n n e l * o u t _ c h a n n e l

C o n n e c t t o a s e r v e r a t t h e g i v e n a d d r e s s . R e t u r n a p a i r o f b u e r e d c h a n n e l s c o n n e c t e d t o

t h e s e r v e r . R e m e m b e r t o c a l l f l u s h o n t h e o u t p u t c h a n n e l a t t h e r i g h t t i m e s t o e n s u r e

c o r r e c t s y n c h r o n i z a t i o n .

v a l u e s h u t d o w n _ c o n n e c t i o n : i n _ c h a n n e l - > u n i t

\ S h u t d o w n " a c o n n e c t i o n e s t a b l i s h e d w i t h o p e n _ c o n n e c t i o n ; t h a t i s , t r a n s m i t a n

e n d - o f - l e c o n d i t i o n t o t h e s e r v e r r e a d i n g o n t h e o t h e r s i d e o f t h e c o n n e c t i o n .

v a l u e e s t a b l i s h _ s e r v e r : ( i n _ c h a n n e l - > o u t _ c h a n n e l - > u n i t ) - > s o c k a d d r - > u n i t

E s t a b l i s h a s e r v e r o n t h e g i v e n a d d r e s s . T h e f u n c t i o n g i v e n a s r s t a r g u m e n t i s c a l l e d f o r

e a c h c o n n e c t i o n w i t h t w o b u e r e d c h a n n e l s c o n n e c t e d t o t h e c l i e n t . A n e w p r o c e s s i s

c r e a t e d f o r e a c h c o n n e c t i o n . T h e f u n c t i o n e s t a b l i s h _ s e r v e r n e v e r r e t u r n s n o r m a l l y .

H o s t a n d p r o t o c o l d a t a b a s e s

t y p e h o s t _ e n t r y =

{ h _ n a m e : s t r i n g ;

h _ a l i a s e s : s t r i n g v e c t ;

h _ a d d r t y p e : s o c k e t _ d o m a i n ;

h _ a d d r _ l i s t : i n e t _ a d d r v e c t }

S t r u c t u r e o f e n t r i e s i n t h e h o s t s d a t a b a s e .

t y p e p r o t o c o l _ e n t r y =

{ p _ n a m e : s t r i n g ;

p _ a l i a s e s : s t r i n g v e c t ;

p _ p r o t o : i n t }

S t r u c t u r e o f e n t r i e s i n t h e p r o t o c o l s d a t a b a s e .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 190/218

1 8 8

t y p e s e r v i c e _ e n t r y =

{ s _ n a m e : s t r i n g ;

s _ a l i a s e s : s t r i n g v e c t ;

s _ p o r t : i n t ;

s _ p r o t o : s t r i n g }

S t r u c t u r e o f e n t r i e s i n t h e s e r v i c e s d a t a b a s e .

v a l u e g e t h o s t n a m e : u n i t - > s t r i n g

R e t u r n t h e n a m e o f t h e l o c a l h o s t .

v a l u e g e t h o s t b y n a m e : s t r i n g - > h o s t _ e n t r y

F i n d a n e n t r y i n h o s t s w i t h t h e g i v e n n a m e , o r r a i s e N o t _ f o u n d .

v a l u e g e t h o s t b y a d d r : i n e t _ a d d r - > h o s t _ e n t r y

F i n d a n e n t r y i n h o s t s w i t h t h e g i v e n a d d r e s s , o r r a i s e N o t _ f o u n d .

v a l u e g e t p r o t o b y n a m e : s t r i n g - > p r o t o c o l _ e n t r y

F i n d a n e n t r y i n p r o t o c o l s w i t h t h e g i v e n n a m e , o r r a i s e N o t _ f o u n d .

v a l u e g e t p r o t o b y n u m b e r : i n t - > p r o t o c o l _ e n t r y

F i n d a n e n t r y i n p r o t o c o l s w i t h t h e g i v e n p r o t o c o l n u m b e r , o r r a i s e N o t _ f o u n d .

v a l u e g e t s e r v b y n a m e : s t r i n g - > s t r i n g - > s e r v i c e _ e n t r y

F i n d a n e n t r y i n s e r v i c e s w i t h t h e g i v e n n a m e , o r r a i s e N o t _ f o u n d .

v a l u e g e t s e r v b y p o r t : i n t - > s t r i n g - > s e r v i c e _ e n t r y

F i n d a n e n t r y i n s e r v i c e s w i t h t h e g i v e n s e r v i c e n u m b e r , o r r a i s e N o t _ f o u n d .

T e r m i n a l i n t e r f a c e

T h e f o l l o w i n g f u n c t i o n s i m p l e m e n t t h e P O S I X s t a n d a r d t e r m i n a l i n t e r f a c e . T h e y p r o v i d e

c o n t r o l o v e r a s y n c h r o n o u s c o m m u n i c a t i o n p o r t s a n d p s e u d o - t e r m i n a l s . R e f e r t o t h e

t e r m i o s m a n p a g e f o r a c o m p l e t e d e s c r i p t i o n .

t y p e t e r m i n a l _ i o = {

I n p u t m o d e s :

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 191/218

C h a p t e r 1 6 . T h e u n i x l i b r a r y : U n i x s y s t e m c a l l s 1 8 9

m u t a b l e c _ i g n b r k : b o o l ; ( * I g n o r e t h e b r e a k c o n d i t i o n . * )

m u t a b l e c _ b r k i n t : b o o l ; ( * S i g n a l i n t e r r u p t o n b r e a k c o n d i t i o n . * )

m u t a b l e c _ i g n p a r : b o o l ; ( * I g n o r e c h a r a c t e r s w i t h p a r i t y e r r o r s . * )

m u t a b l e c _ p a r m r k : b o o l ; ( * M a r k p a r i t y e r r o r s . * )

m u t a b l e c _ i n p c k : b o o l ; ( * E n a b l e p a r i t y c h e c k o n i n p u t . * )

m u t a b l e c _ i s t r i p : b o o l ; ( * S t r i p 8 t h b i t o n i n p u t c h a r a c t e r s . * )

m u t a b l e c _ i n l c r : b o o l ; ( * M a p N L t o C R o n i n p u t . * )

m u t a b l e c _ i g n c r : b o o l ; ( * I g n o r e C R o n i n p u t . * )

m u t a b l e c _ i c r n l : b o o l ; ( * M a p C R t o N L o n i n p u t . * )

m u t a b l e c _ i x o n : b o o l ; ( * R e c o g n i z e X O N / X O F F c h a r a c t e r s o n i n p u t . * )

m u t a b l e c _ i x o f f : b o o l ; ( * E m i t X O N / X O F F c h a r s t o c o n t r o l i n p u t f l o w . * )

O u t p u t m o d e s :

m u t a b l e c _ o p o s t : b o o l ; ( * E n a b l e o u t p u t p r o c e s s i n g . * )

C o n t r o l m o d e s :

m u t a b l e c _ o b a u d : i n t ; ( * O u t p u t b a u d r a t e ( 0 m e a n s c l o s e c o n n e c t i o n ) . * )

m u t a b l e c _ i b a u d : i n t ; ( * I n p u t b a u d r a t e . * )

m u t a b l e c _ c s i z e : i n t ; ( * N u m b e r o f b i t s p e r c h a r a c t e r ( 5 - 8 ) . * )

m u t a b l e c _ c s t o p b : i n t ; ( * N u m b e r o f s t o p b i t s ( 1 - 2 ) . * )

m u t a b l e c _ c r e a d : b o o l ; ( * R e c e p t i o n i s e n a b l e d . * )

m u t a b l e c _ p a r e n b : b o o l ; ( * E n a b l e p a r i t y g e n e r a t i o n a n d d e t e c t i o n . * )

m u t a b l e c _ p a r o d d : b o o l ; ( * S p e c i f y o d d p a r i t y i n s t e a d o f e v e n . * )

m u t a b l e c _ h u p c l : b o o l ; ( * H a n g u p o n l a s t c l o s e . * )

m u t a b l e c _ c l o c a l : b o o l ; ( * I g n o r e m o d e m s t a t u s l i n e s . * )

L o c a l m o d e s :

m u t a b l e c _ i s i g : b o o l ; ( * G e n e r a t e s i g n a l o n I N T R , Q U I T , S U S P . * )

m u t a b l e c _ i c a n o n : b o o l ; ( * E n a b l e c a n o n i c a l p r o c e s s i n g

( l i n e b u f f e r i n g a n d e d i t i n g ) * )

m u t a b l e c _ n o f l s h : b o o l ; ( * D i s a b l e f l u s h a f t e r I N T R , Q U I T , S U S P . * )

m u t a b l e c _ e c h o : b o o l ; ( * E c h o i n p u t c h a r a c t e r s . * )

m u t a b l e c _ e c h o e : b o o l ; ( * E c h o E R A S E ( t o e r a s e p r e v i o u s c h a r a c t e r ) . * )

m u t a b l e c _ e c h o k : b o o l ; ( * E c h o K I L L ( t o e r a s e t h e c u r r e n t l i n e ) . * )

m u t a b l e c _ e c h o n l : b o o l ; ( * E c h o N L e v e n i f c _ e c h o i s n o t s e t . * )

C o n t r o l c h a r a c t e r s :

m u t a b l e c _ v i n t r : c h a r ; ( * I n t e r r u p t c h a r a c t e r ( u s u a l l y c t r l - C ) . * )

m u t a b l e c _ v q u i t : c h a r ; ( * Q u i t c h a r a c t e r ( u s u a l l y c t r l - \ ) . * )

m u t a b l e c _ v e r a s e : c h a r ; ( * E r a s e c h a r a c t e r ( u s u a l l y D E L o r c t r l - H ) . * )

m u t a b l e c _ v k i l l : c h a r ; ( * K i l l l i n e c h a r a c t e r ( u s u a l l y c t r l - U ) . * )

m u t a b l e c _ v e o f : c h a r ; ( * E n d - o f - f i l e c h a r a c t e r ( u s u a l l y c t r l - D ) . * )

m u t a b l e c _ v e o l : c h a r ; ( * A l t e r n a t e e n d - o f - l i n e c h a r . ( u s u a l l y n o n e ) . * )

m u t a b l e c _ v m i n : i n t ; ( * M i n i m u m n u m b e r o f c h a r a c t e r s t o r e a d

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 192/218

1 9 0

b e f o r e t h e r e a d r e q u e s t i s s a t i s f i e d . * )

m u t a b l e c _ v t i m e : i n t ; ( * M a x i m u m r e a d w a i t ( i n 0 . 1 s u n i t s ) . * )

m u t a b l e c _ v s t a r t : c h a r ; ( * S t a r t c h a r a c t e r ( u s u a l l y c t r l - Q ) . * )

m u t a b l e c _ v s t o p : c h a r ( * S t o p c h a r a c t e r ( u s u a l l y c t r l - S ) . * )

}

v a l u e t c g e t a t t r : f i l e _ d e s c r - > t e r m i n a l _ i o

R e t u r n t h e s t a t u s o f t h e t e r m i n a l r e f e r r e d t o b y t h e g i v e n l e d e s c r i p t o r .

t y p e s e t a t t r _ w h e n = T C S A N O W | T C S A D R A I N | T C S A F L U S H

v a l u e t c s e t a t t r : f i l e _ d e s c r - > s e t a t t r _ w h e n - > t e r m i n a l _ i o - > u n i t

S e t t h e s t a t u s o f t h e t e r m i n a l r e f e r r e d t o b y t h e g i v e n l e d e s c r i p t o r . T h e s e c o n d a r g u m e n t

i n d i c a t e s w h e n t h e s t a t u s c h a n g e t a k e s p l a c e : i m m e d i a t e l y ( T C S A N O W ) , w h e n a l l p e n d i n g

o u t p u t h a s b e e n t r a n s m i t t e d ( T C S A D R A I N ) , o r a f t e r u s h i n g a l l i n p u t t h a t h a s b e e n r e c e i v e d

b u t n o t r e a d ( T C S A F L U S H ) . T C S A D R A I N i s r e c o m m e n d e d w h e n c h a n g i n g t h e o u t p u t

p a r a m e t e r s ; T C S A F L U S H , w h e n c h a n g i n g t h e i n p u t p a r a m e t e r s .

v a l u e t c s e n d b r e a k : f i l e _ d e s c r - > i n t - > u n i t

S e n d a b r e a k c o n d i t i o n o n t h e g i v e n l e d e s c r i p t o r . T h e s e c o n d a r g u m e n t i s t h e d u r a t i o n o f

t h e b r e a k , i n 0 . 1 s u n i t s ; 0 m e a n s s t a n d a r d d u r a t i o n ( 0 . 2 5 s ) .

v a l u e t c d r a i n : f i l e _ d e s c r - > u n i t

W a i t s u n t i l a l l o u t p u t w r i t t e n o n t h e g i v e n l e d e s c r i p t o r h a s b e e n t r a n s m i t t e d .

t y p e f l u s h _ q u e u e = T C I F L U S H | T C O F L U S H | T C I O F L U S H

v a l u e t c f l u s h : f i l e _ d e s c r - > f l u s h _ q u e u e - > u n i t

D i s c a r d d a t a w r i t t e n o n t h e g i v e n l e d e s c r i p t o r b u t n o t y e t t r a n s m i t t e d , o r d a t a r e c e i v e d

b u t n o t y e t r e a d , d e p e n d i n g o n t h e s e c o n d a r g u m e n t : T C I F L U S H u s h e s d a t a r e c e i v e d b u t

n o t r e a d , T C O F L U S H u s h e s d a t a w r i t t e n b u t n o t t r a n s m i t t e d , a n d T C I O F L U S H u s h e s b o t h .

t y p e f l o w _ a c t i o n = T C O O F F | T C O O N | T C I O F F | T C I O N

v a l u e t c f l o w : f i l e _ d e s c r - > f l o w _ a c t i o n - > u n i t

S u s p e n d o r r e s t a r t r e c e p t i o n o r t r a n s m i s s i o n o f d a t a o n t h e g i v e n l e d e s c r i p t o r , d e p e n d i n g

o n t h e s e c o n d a r g u m e n t : T C O O F F s u s p e n d s o u t p u t , T C O O N r e s t a r t s o u t p u t , T C I O F F t r a n s m i t s

a S T O P c h a r a c t e r t o s u s p e n d i n p u t , a n d T C I O N t r a n s m i t s a S T A R T c h a r a c t e r t o r e s t a r t

i n p u t .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 193/218

C h a p t e r 1 7

T h e n u m l i b r a r y : a r b i t r a r y - p r e c i s i o n

r a t i o n a l a r i t h m e t i c

T h e n u m l i b r a r y ( d i s t r i b u t e d i n c o n t r i b / l i b n u m ) i m p l e m e n t s e x a c t - p r e c i s i o n r a t i o n a l a r i t h m e t i c .

I t i s b u i l t u p o n t h e s t a t e - o f - t h e - a r t B i g N u m a r b i t r a r y - p r e c i s i o n i n t e g e r a r i t h m e t i c p a c k a g e , a n d

t h e r e f o r e a c h i e v e s v e r y h i g h p e r f o r m a n c e .

T h e f u n c t i o n s p r o v i d e d i n t h i s l i b r a r y a r e f u l l y d o c u m e n t e d i n T h e C A M L N u m b e r s R e f e r -

e n c e M a n u a l b y V a l e r i e M e n i s s i e r - M o r a i n , t e c h n i c a l r e p o r t 1 4 1 , I N R I A , j u l y 1 9 9 2 ( a v a i l a b l e b y

a n o n y m o u s F T P f r o m f t p . i n r i a . f r , d i r e c t o r y I N R I A / p u b l i c a t i o n s / R T , l e R T - 0 1 4 1 . p s . Z ) . A

s u m m a r y o f t h e f u n c t i o n s i s g i v e n b e l o w .

P r o g r a m s t h a t u s e t h e n u m l i b r a r y m u s t b e l i n k e d i n \ c u s t o m r u n t i m e " m o d e , a s f o l l o w s :

c a m l c - c u s t o m o t h e r o p t i o n s n u m s . z o o t h e r l e s - l n u m s

F o r i n t e r a c t i v e u s e o f t h e n u m l i b r a r y , r u n c a m l l i g h t c a m l n u m .

M a c : T h i s l i b r a r y i s n o t a v a i l a b l e .

P C : T h i s l i b r a r y i s a v a i l a b l e b y d e f a u l t i n t h e s t a n d a r d r u n t i m e s y s t e m a n d i n t h e t o p l e v e l

s y s t e m . P r o g r a m s t h a t u s e t h i s l i b r a r y c a n b e l i n k e d n o r m a l l y , w i t h o u t t h e - c u s t o m o p t i o n .

1 7 . 1 n u m : o p e r a t i o n s o n n u m b e r s

N u m b e r s ( t y p e n u m ) a r e a r b i t r a r y - p r e c i s i o n r a t i o n a l n u m b e r s , p l u s t h e s p e c i a l e l e m e n t s 1 / 0

( i n n i t y ) a n d 0 / 0 ( u n d e n e d ) .

t y p e n u m = I n t o f i n t | B i g _ i n t o f b i g _ i n t | R a t i o o f r a t i o

T h e t y p e o f n u m b e r s .

v a l u e n o r m a l i z e _ n u m : n u m - > n u m

v a l u e n u m e r a t o r _ n u m : n u m - > n u m

v a l u e d e n o m i n a t o r _ n u m : n u m - > n u m

1 9 1

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 194/218

1 9 2

A r i t h m e t i c o p e r a t i o n s

v a l u e p r e f i x + / : n u m - > n u m - > n u m

v a l u e a d d _ n u m : n u m - > n u m - > n u m

A d d i t i o n

v a l u e m i n u s _ n u m : n u m - > n u m

U n a r y n e g a t i o n .

v a l u e p r e f i x - / : n u m - > n u m - > n u m

v a l u e s u b _ n u m : n u m - > n u m - > n u m

S u b t r a c t i o n

v a l u e p r e f i x * / : n u m - > n u m - > n u m

v a l u e m u l t _ n u m : n u m - > n u m - > n u m

M u l t i p l i c a t i o n

v a l u e s q u a r e _ n u m : n u m - > n u m

S q u a r i n g

v a l u e p r e f i x / / : n u m - > n u m - > n u m

v a l u e d i v _ n u m : n u m - > n u m - > n u m

D i v i s i o n

v a l u e q u o _ n u m : n u m - > n u m - > n u m

v a l u e m o d _ n u m : n u m - > n u m - > n u m

E u c l i d e a n d i v i s i o n : q u o t i e n t a n d r e m a i n d e r

v a l u e p r e f i x * * / : n u m - > n u m - > n u m

v a l u e p o w e r _ n u m : n u m - > n u m - > n u m

E x p o n e n t i a t i o n

v a l u e i s _ i n t e g e r _ n u m : n u m - > b o o l

T e s t i f a n u m b e r i s a n i n t e g e r

v a l u e i n t e g e r _ n u m : n u m - > n u m

v a l u e f l o o r _ n u m : n u m - > n u m

v a l u e r o u n d _ n u m : n u m - > n u m

v a l u e c e i l i n g _ n u m : n u m - > n u m

A p p r o x i m a t e a n u m b e r b y a n i n t e g e r . f l o o r _ n u m n r e t u r n s t h e l a r g e s t i n t e g e r s m a l l e r o r

e q u a l t o n . c e i l i n g _ n u m n r e t u r n s t h e s m a l l e s t i n t e g e r b i g g e r o r e q u a l t o n .

i n t e g e r _ n u m n r e t u r n s t h e i n t e g e r c l o s e s t t o n . I n c a s e o f t i e s , r o u n d s t o w a r d s z e r o .

r o u n d _ n u m n r e t u r n s t h e i n t e g e r c l o s e s t t o n . I n c a s e o f t i e s , r o u n d s o z e r o .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 195/218

C h a p t e r 1 7 . T h e n u m l i b r a r y : a r b i t r a r y - p r e c i s i o n r a t i o n a l a r i t h m e t i c 1 9 3

v a l u e s i g n _ n u m : n u m - > i n t

R e t u r n - 1 , 0 o r 1 a c c o r d i n g t o t h e s i g n o f t h e a r g u m e n t .

v a l u e p r e f i x = / : n u m - > n u m - > b o o l

v a l u e p r e f i x < / : n u m - > n u m - > b o o l

v a l u e p r e f i x > / : n u m - > n u m - > b o o l

v a l u e p r e f i x < = / : n u m - > n u m - > b o o l

v a l u e p r e f i x > = / : n u m - > n u m - > b o o l

v a l u e p r e f i x < > / : n u m - > n u m - > b o o l

v a l u e e q _ n u m : n u m - > n u m - > b o o l

v a l u e l t _ n u m : n u m - > n u m - > b o o l

v a l u e l e _ n u m : n u m - > n u m - > b o o l

v a l u e g t _ n u m : n u m - > n u m - > b o o l

v a l u e g e _ n u m : n u m - > n u m - > b o o l

U s u a l c o m p a r i s o n s b e t w e e n n u m b e r s

v a l u e c o m p a r e _ n u m : n u m - > n u m - > i n t

R e t u r n - 1 , 0 o r 1 i f t h e r s t a r g u m e n t i s l e s s t h a n , e q u a l t o , o r g r e a t e r t h a n t h e s e c o n d

a r g u m e n t .

v a l u e m a x _ n u m : n u m - > n u m - > n u m

v a l u e m i n _ n u m : n u m - > n u m - > n u m

R e t u r n t h e g r e a t e r ( r e s p . t h e s m a l l e r ) o f t h e t w o a r g u m e n t s .

v a l u e a b s _ n u m : n u m - > n u m

A b s o l u t e v a l u e .

v a l u e s u c c _ n u m : n u m - > n u m

s u c c n i s n + 1

v a l u e p r e d _ n u m : n u m - > n u m

p r e d n i s n - 1

v a l u e i n c r _ n u m : n u m r e f - > u n i t

i n c r r i s r : = ! r + 1 , w h e r e r i s a r e f e r e n c e t o a n u m b e r .

v a l u e d e c r _ n u m : n u m r e f - > u n i t

d e c r r i s r : = ! r - 1 , w h e r e r i s a r e f e r e n c e t o a n u m b e r .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 196/218

1 9 4

C o e r c i o n s w i t h s t r i n g s

v a l u e s t r i n g _ o f _ n u m : n u m - > s t r i n g

C o n v e r t a n u m b e r t o a s t r i n g , u s i n g f r a c t i o n a l n o t a t i o n .

v a l u e a p p r o x _ n u m _ f i x : i n t - > n u m - > s t r i n g

v a l u e a p p r o x _ n u m _ e x p : i n t - > n u m - > s t r i n g

A p p r o x i m a t e a n u m b e r b y a d e c i m a l . T h e r s t a r g u m e n t i s t h e r e q u i r e d p r e c i s i o n . T h e

s e c o n d a r g u m e n t i s t h e n u m b e r t o a p p r o x i m a t e . a p p r o x _ f i x u s e s d e c i m a l n o t a t i o n ; t h e r s t

a r g u m e n t i s t h e n u m b e r o f d i g i t s a f t e r t h e d e c i m a l p o i n t . a p p r o x _ e x p u s e s s c i e n t i c

( e x p o n e n t i a l ) n o t a t i o n ; t h e r s t a r g u m e n t i s t h e n u m b e r o f d i g i t s i n t h e m a n t i s s a .

v a l u e n u m _ o f _ s t r i n g : s t r i n g - > n u m

C o n v e r t a s t r i n g t o a n u m b e r .

C o e r c i o n s b e t w e e n n u m e r i c a l t y p e s

v a l u e i n t _ o f _ n u m : n u m - > i n t

v a l u e n u m _ o f _ i n t : i n t - > n u m

v a l u e n a t _ o f _ n u m : n u m - > n a t _ _ n a t

v a l u e n u m _ o f _ n a t : n a t _ _ n a t - > n u m

v a l u e n u m _ o f _ b i g _ i n t : b i g _ i n t - > n u m

v a l u e b i g _ i n t _ o f _ n u m : n u m - > b i g _ i n t

v a l u e r a t i o _ o f _ n u m : n u m - > r a t i o

v a l u e n u m _ o f _ r a t i o : r a t i o - > n u m

v a l u e f l o a t _ o f _ n u m : n u m - > f l o a t

v a l u e n u m _ o f _ f l o a t : f l o a t - > n u m

v a l u e s y s _ p r i n t _ n u m : i n t - > s t r i n g - > n u m - > s t r i n g - > u n i t

v a l u e p r i n t _ n u m : n u m - > u n i t

1 7 . 2 a r i t h _ s t a t u s : a g s t h a t c o n t r o l r a t i o n a l a r i t h m e t i c

v a l u e a r i t h _ s t a t u s : u n i t - > u n i t

P r i n t t h e c u r r e n t s t a t u s o f t h e a r i t h m e t i c a g s .

v a l u e g e t _ e r r o r _ w h e n _ n u l l _ d e n o m i n a t o r : u n i t - > b o o l

v a l u e s e t _ e r r o r _ w h e n _ n u l l _ d e n o m i n a t o r : b o o l - > u n i t

G e t o r s e t t h e a g n u l l _ d e n o m i n a t o r . W h e n o n , a t t e m p t i n g t o c r e a t e a r a t i o n a l w i t h a

n u l l d e n o m i n a t o r r a i s e s a n e x c e p t i o n . W h e n o , r a t i o n a l s w i t h n u l l d e n o m i n a t o r s a r e

a c c e p t e d . I n i t i a l l y : o n .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 197/218

C h a p t e r 1 7 . T h e n u m l i b r a r y : a r b i t r a r y - p r e c i s i o n r a t i o n a l a r i t h m e t i c 1 9 5

v a l u e g e t _ n o r m a l i z e _ r a t i o : u n i t - > b o o l

v a l u e s e t _ n o r m a l i z e _ r a t i o : b o o l - > u n i t

G e t o r s e t t h e a g n o r m a l i z e _ r a t i o . W h e n o n , r a t i o n a l n u m b e r s a r e n o r m a l i z e d a f t e r e a c h

o p e r a t i o n . W h e n o , r a t i o n a l n u m b e r s a r e n o t n o r m a l i z e d u n t i l p r i n t e d . I n i t i a l l y : o .

v a l u e g e t _ n o r m a l i z e _ r a t i o _ w h e n _ p r i n t i n g : u n i t - > b o o l

v a l u e s e t _ n o r m a l i z e _ r a t i o _ w h e n _ p r i n t i n g : b o o l - > u n i t

G e t o r s e t t h e a g n o r m a l i z e _ r a t i o _ w h e n _ p r i n t i n g . W h e n o n , r a t i o n a l n u m b e r s a r e

n o r m a l i z e d b e f o r e b e i n g p r i n t e d . W h e n o , r a t i o n a l n u m b e r s a r e p r i n t e d a s i s , w i t h o u t

n o r m a l i z a t i o n . I n i t i a l l y : o n .

v a l u e g e t _ a p p r o x _ p r i n t i n g : u n i t - > b o o l

v a l u e s e t _ a p p r o x _ p r i n t i n g : b o o l - > u n i t

G e t o r s e t t h e a g a p p r o x _ p r i n t i n g . W h e n o n , r a t i o n a l n u m b e r s a r e p r i n t e d a s a d e c i m a l

a p p r o x i m a t i o n . W h e n o , r a t i o n a l n u m b e r s a r e p r i n t e d a s a f r a c t i o n . I n i t i a l l y : o .

v a l u e g e t _ f l o a t i n g _ p r e c i s i o n : u n i t - > i n t

v a l u e s e t _ f l o a t i n g _ p r e c i s i o n : i n t - > u n i t

G e t o r s e t t h e p a r a m e t e r f l o a t i n g _ p r e c i s i o n . T h i s p a r a m e t e r i s t h e n u m b e r o f d i g i t s

d i s p l a y e d w h e n a p p r o x _ p r i n t i n g i s o n . I n i t i a l l y : 1 2 .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 198/218

1 9 6

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 199/218

C h a p t e r 1 8

T h e s t r l i b r a r y : r e g u l a r e x p r e s s i o n s

a n d s t r i n g p r o c e s s i n g

T h e s t r l i b r a r y ( d i s t r i b u t e d i n c o n t r i b / l i b s t r ) p r o v i d e s h i g h - l e v e l s t r i n g p r o c e s s i n g f u n c t i o n s ,

s o m e b a s e d o n r e g u l a r e x p r e s s i o n s . I t i s i n t e n d e d t o s u p p o r t t h e k i n d o f l e p r o c e s s i n g t h a t i s

u s u a l l y p e r f o r m e d w i t h s c r i p t i n g l a n g u a g e s s u c h a s a w k , p e r l o r s e d .

P r o g r a m s t h a t u s e t h e s t r l i b r a r y m u s t b e l i n k e d i n \ c u s t o m r u n t i m e " m o d e , a s f o l l o w s :

c a m l c - c u s t o m o t h e r o p t i o n s s t r . z o o t h e r l e s - l s t r

F o r i n t e r a c t i v e u s e o f t h e s t r l i b r a r y , r u n c a m l l i g h t c a m l s t r .

M a c : T h i s l i b r a r y i s n o t a v a i l a b l e .

P C : T h i s l i b r a r y i s n o t a v a i l a b l e .

1 8 . 1 s t r : r e g u l a r e x p r e s s i o n s a n d h i g h - l e v e l s t r i n g p r o c e s s i n g

R e g u l a r e x p r e s s i o n s

t y p e r e g e x p

T h e t y p e o f c o m p i l e d r e g u l a r e x p r e s s i o n s .

v a l u e r e g e x p : s t r i n g - > r e g e x p

C o m p i l e a r e g u l a r e x p r e s s i o n . T h e s y n t a x f o r r e g u l a r e x p r e s s i o n s i s t h e s a m e a s i n G n u

E m a c s . T h e s p e c i a l c h a r a c t e r s a r e \ $ ^ . * + ? ] . T h e f o l l o w i n g c o n s t r u c t s a r e r e c o g n i z e d :

. m a t c h e s a n y c h a r a c t e r e x c e p t n e w l i n e

* ( p o s t x ) m a t c h e s t h e p r e v i o u s e x p r e s s i o n z e r o , o n e o r s e v e r a l t i m e s

+ ( p o s t x ) m a t c h e s t h e p r e v i o u s e x p r e s s i o n o n e o r s e v e r a l t i m e s

? ( p o s t x ) m a t c h e s t h e p r e v i o u s e x p r e s s i o n o n c e o r n o t a t a l l

. . ] c h a r a c t e r s e t ; r a n g e s a r e d e n o t e d w i t h - , a s i n a - z ; a n i n i t i a l ^ , a s i n ^ 0 - 9 ,

c o m p l e m e n t s t h e s e t

1 9 7

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 200/218

1 9 8

^ m a t c h e s a t b e g i n n i n g o f l i n e

$ m a t c h e s a t e n d o f l i n e

\ | ( i n x ) a l t e r n a t i v e b e t w e e n t w o e x p r e s s i o n s

\ ( . . \ ) g r o u p i n g a n d n a m i n g o f t h e e n c l o s e d e x p r e s s i o n

\ 1 t h e t e x t m a t c h e d b y t h e r s t \ ( . . . \ ) e x p r e s s i o n ( \ 2 f o r t h e s e c o n d e x p r e s s i o n , e t c )

\ b m a t c h e s w o r d b o u n d a r i e s

\ q u o t e s s p e c i a l c h a r a c t e r s .

v a l u e r e g e x p _ c a s e _ f o l d : s t r i n g - > r e g e x p

S a m e a s r e g e x p , b u t t h e c o m p i l e d e x p r e s s i o n w i l l m a t c h t e x t i n a c a s e - i n s e n s i t i v e w a y :

u p p e r c a s e a n d l o w e r c a s e l e t t e r s w i l l b e c o n s i d e r e d e q u i v a l e n t .

S t r i n g m a t c h i n g a n d s e a r c h i n g

v a l u e s t r i n g _ m a t c h : r e g e x p - > s t r i n g - > i n t - > b o o l

s t r i n g _ m a t c h r s s t a r t t e s t s w h e t h e r t h e c h a r a c t e r s i n s s t a r t i n g a t p o s i t i o n s t a r t

m a t c h t h e r e g u l a r e x p r e s s i o n r . T h e r s t c h a r a c t e r o f a s t r i n g h a s p o s i t i o n 0 , a s u s u a l .

v a l u e s e a r c h _ f o r w a r d : r e g e x p - > s t r i n g - > i n t - > i n t

s e a r c h _ f o r w a r d r s s t a r t s e a r c h s t h e s t r i n g s f o r a s u b s t r i n g m a t c h i n g t h e r e g u l a r

e x p r e s s i o n r . T h e s e a r c h s t a r t s a t p o s i t i o n s t a r t a n d p r o c e e d s t o w a r d s t h e e n d o f t h e

s t r i n g . R e t u r n t h e p o s i t i o n o f t h e r s t c h a r a c t e r o f t h e m a t c h e d s u b s t r i n g , o r r a i s e

N o t _ f o u n d i f n o s u b s t r i n g m a t c h e s .

v a l u e s e a r c h _ b a c k w a r d : r e g e x p - > s t r i n g - > i n t - > i n t

S a m e a s s e a r c h _ f o r w a r d , b u t t h e s e a r c h p r o c e e d s t o w a r d s t h e b e g i n n i n g o f t h e s t r i n g .

v a l u e m a t c h e d _ s t r i n g : s t r i n g - > s t r i n g

m a t c h e d _ s t r i n g s r e t u r n s t h e s u b s t r i n g o f s t h a t w a s m a t c h e d b y t h e l a t e s t

s t r i n g _ m a t c h , s e a r c h _ f o r w a r d o r s e a r c h _ b a c k w a r d . T h e u s e r m u s t m a k e s u r e t h a t t h e

p a r a m e t e r s i s t h e s a m e s t r i n g t h a t w a s p a s s e d t o t h e m a t c h i n g o r s e a r c h i n g f u n c t i o n .

v a l u e m a t c h _ b e g i n n i n g : u n i t - > i n t

v a l u e m a t c h _ e n d : u n i t - > i n t

m a t c h _ b e g i n n i n g ( ) r e t u r n s t h e p o s i t i o n o f t h e r s t c h a r a c t e r o f t h e s u b s t r i n g t h a t w a s

m a t c h e d b y s t r i n g _ m a t c h , s e a r c h _ f o r w a r d o r s e a r c h _ b a c k w a r d . m a t c h _ e n d ( ) r e t u r n s

t h e p o s i t i o n o f t h e c h a r a c t e r f o l l o w i n g t h e l a s t c h a r a c t e r o f t h e m a t c h e d s u b s t r i n g .

v a l u e m a t c h e d _ g r o u p : i n t - > s t r i n g - > s t r i n g

m a t c h e d _ g r o u p n s r e t u r n s t h e s u b s t r i n g o f s t h a t w a s m a t c h e d b y t h e n t h g r o u p \ ( . . . \ )

o f t h e r e g u l a r e x p r e s s i o n d u r i n g t h e l a t e s t s t r i n g _ m a t c h , s e a r c h _ f o r w a r d o r

s e a r c h _ b a c k w a r d . T h e u s e r m u s t m a k e s u r e t h a t t h e p a r a m e t e r s i s t h e s a m e s t r i n g t h a t

w a s p a s s e d t o t h e m a t c h i n g o r s e a r c h i n g f u n c t i o n .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 201/218

C h a p t e r 1 8 . T h e s t r l i b r a r y : r e g u l a r e x p r e s s i o n s a n d s t r i n g p r o c e s s i n g 1 9 9

v a l u e g r o u p _ b e g i n n i n g : i n t - > i n t

v a l u e g r o u p _ e n d : i n t - > i n t

g r o u p _ b e g i n n i n g n r e t u r n s t h e p o s i t i o n o f t h e r s t c h a r a c t e r o f t h e s u b s t r i n g t h a t w a s

m a t c h e d b y t h e n t h g r o u p o f t h e r e g u l a r e x p r e s s i o n . g r o u p _ e n d n r e t u r n s t h e p o s i t i o n o f

t h e c h a r a c t e r f o l l o w i n g t h e l a s t c h a r a c t e r o f t h e m a t c h e d s u b s t r i n g .

R e p l a c e m e n t

v a l u e g l o b a l _ r e p l a c e : r e g e x p - > s t r i n g - > s t r i n g - > s t r i n g

g l o b a l _ r e p l a c e r e g e x p r e p l s r e t u r n s a s t r i n g i d e n t i c a l t o s , e x c e p t t h a t a l l s u b s t r i n g s

o f s t h a t m a t c h r e g e x p h a v e b e e n r e p l a c e d b y r e p l . T h e r e p l a c e m e n t t e x t r e p l c a n c o n t a i n

\ 1 , \ 2 , e t c ; t h e s e s e q u e n c e s w i l l b e r e p l a c e d b y t h e t e x t m a t c h e d b y t h e c o r r e s p o n d i n g g r o u p

i n t h e r e g u l a r e x p r e s s i o n . \ 0 s t a n d s f o r t h e t e x t m a t c h e d b y t h e w h o l e r e g u l a r e x p r e s s i o n .

v a l u e r e p l a c e _ f i r s t : r e g e x p - > s t r i n g - > s t r i n g - > s t r i n g

S a m e a s g l o b a l _ r e p l a c e , e x c e p t t h a t o n l y t h e r s t s u b s t r i n g m a t c h i n g t h e r e g u l a r

e x p r e s s i o n i s r e p l a c e d .

v a l u e g l o b a l _ s u b s t i t u t e : r e g e x p - > ( s t r i n g - > s t r i n g ) - > s t r i n g - > s t r i n g

g l o b a l _ s u b s t i t u t e r e g e x p s u b s t s r e t u r n s a s t r i n g i d e n t i c a l t o s , e x c e p t t h a t a l l

s u b s t r i n g s o f s t h a t m a t c h r e g e x p h a v e b e e n r e p l a c e d b y t h e r e s u l t o f f u n c t i o n s u b s t . T h e

f u n c t i o n s u b s t i s c a l l e d o n c e f o r e a c h m a t c h i n g s u b s t r i n g , a n d r e c e i v e s s ( t h e w h o l e t e x t ) a s

a r g u m e n t .

v a l u e s u b s t i t u t e _ f i r s t : r e g e x p - > ( s t r i n g - > s t r i n g ) - > s t r i n g - > s t r i n g

S a m e a s g l o b a l _ s u b s t i t u t e , e x c e p t t h a t o n l y t h e r s t s u b s t r i n g m a t c h i n g t h e r e g u l a r

e x p r e s s i o n i s r e p l a c e d .

S p l i t t i n g

v a l u e s p l i t : r e g e x p - > s t r i n g - > s t r i n g l i s t

s p l i t r s s p l i t s s i n t o s u b s t r i n g s , t a k i n g a s d e l i m i t e r s t h e s u b s t r i n g s t h a t m a t c h r , a n d

r e t u r n s t h e l i s t o f s u b s t r i n g s . F o r i n s t a n c e , s p l i t ( r e g e x p " \ t ] + " ) s s p l i t s s i n t o

b l a n k - s e p a r a t e d w o r d s .

v a l u e b o u n d e d _ s p l i t : r e g e x p - > s t r i n g - > i n t - > s t r i n g l i s t

S a m e a s s p l i t , b u t s p l i t s i n t o a t m o s t n s u b s t r i n g s , w h e r e n i s t h e e x t r a i n t e g e r p a r a m e t e r .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 202/218

2 0 0

J o i n i n g

v a l u e c o n c a t : s t r i n g l i s t - > s t r i n g

S a m e a s s t r i n g _ _ c o n c a t : c a t e n a t e a l i s t o f s t r i n g .

v a l u e j o i n : s t r i n g - > s t r i n g l i s t - > s t r i n g

C a t e n a t e a l i s t o f s t r i n g . T h e r s t a r g u m e n t i s a s e p a r a t o r , w h i c h i s i n s e r t e d b e t w e e n t h e

s t r i n g s .

E x t r a c t i n g s u b s t r i n g s

v a l u e s t r i n g _ b e f o r e : s t r i n g - > i n t - > s t r i n g

s t r i n g _ b e f o r e s n r e t u r n s t h e s u b s t r i n g o f a l l c h a r a c t e r s o f s t h a t p r e c e d e p o s i t i o n n

( e x c l u d i n g t h e c h a r a c t e r a t p o s i t i o n n ) .

v a l u e s t r i n g _ a f t e r : s t r i n g - > i n t - > s t r i n g

s t r i n g _ a f t e r s n r e t u r n s t h e s u b s t r i n g o f a l l c h a r a c t e r s o f s t h a t f o l l o w p o s i t i o n n

( i n c l u d i n g t h e c h a r a c t e r a t p o s i t i o n n ) .

v a l u e f i r s t _ c h a r s : s t r i n g - > i n t - > s t r i n g

f i r s t _ c h a r s s n r e t u r n s t h e r s t n c h a r a c t e r s o f s . T h i s i s t h e s a m e f u n c t i o n a s

s t r i n g _ b e f o r e .

v a l u e l a s t _ c h a r s : s t r i n g - > i n t - > s t r i n g

l a s t _ c h a r s s n r e t u r n s t h e l a s t n c h a r a c t e r s o f s .

F o r m a t t i n g

v a l u e f o r m a t : ( ' a , u n i t , s t r i n g ) p r i n t f _ _ f o r m a t - > ' a

S a m e a s p r i n t f _ _ s p r i n t f .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 203/218

P a r t V

A p p e n d i x

2 0 1

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 204/218

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 205/218

C h a p t e r 1 9

F u r t h e r r e a d i n g

F o r t h e i n t e r e s t e d r e a d e r , w e l i s t b e l o w s o m e r e f e r e n c e s t o b o o k s a n d r e p o r t s r e l a t e d ( s o m e t i m e s

l o o s e l y ) t o C a m l L i g h t .

1 9 . 1 P r o g r a m m i n g i n M L

T h e b o o k s b e l o w a r e p r o g r a m m i n g c o u r s e s t a u g h t i n M L . T h e i r m a i n g o a l i s t o t e a c h p r o g r a m m i n g ,

n o t t o d e s c r i b e M L i n f u l l d e t a i l s | t h o u g h m o s t c o n t a i n f a i r l y g o o d i n t r o d u c t i o n s t o t h e M L

l a n g u a g e . S o m e o f t h o s e b o o k s u s e t h e S t a n d a r d M L d i a l e c t i n s t e a d o f t h e C a m l d i a l e c t , s o y o u

w i l l h a v e t o k e e p i n m i n d t h e d i e r e n c e s i n s y n t a x a n d i n s e m a n t i c s .

P i e r r e W e i s a n d X a v i e r L e r o y . L e l a n g a g e C a m l . I n t e r

E d i t i o n s , 1 9 9 3 .

T h e n a t u r a l c o m p a n i o n t o t h i s m a n u a l , p r o v i d e d y o u r e a d F r e n c h . T h i s b o o k i s a s t e p - b y -

s t e p i n t r o d u c t i o n t o p r o g r a m m i n g i n C a m l , a n d p r e s e n t s m a n y r e a l i s t i c e x a m p l e s o f C a m l

p r o g r a m s .

G u y C o u s i n e a u a n d M i c h e l M a u n y . A p p r o c h e f o n c t i o n n e l l e d e l a p r o g r a m m a t i o n . E d i s c i e n c e ,

1 9 9 5 .

A n o t h e r C a m l p r o g r a m m i n g c o u r s e w r i t t e n i n F r e n c h , w i t h m a n y o r i g i n a l e x a m p l e s .

L a w r e n c e C . P a u l s o n . M L f o r t h e w o r k i n g p r o g r a m m e r . C a m b r i d g e U n i v e r s i t y P r e s s , 1 9 9 1 .

A g o o d i n t r o d u c t i o n t o p r o g r a m m i n g i n S t a n d a r d M L . D e v e l o p s a t h e o r e m p r o v e r a s a c o m -

p l e t e e x a m p l e . C o n t a i n s a p r e s e n t a t i o n o f t h e m o d u l e s y s t e m o f S t a n d a r d M L .

J e r e y D . U l l m a n . E l e m e n t s o f M L p r o g r a m m i n g . P r e n t i c e H a l l , 1 9 9 3 .

A n o t h e r g o o d i n t r o d u c t i o n t o p r o g r a m m i n g i n S t a n d a r d M L . N o r e a l i s t i c e x a m p l e s , b u t a

v e r y d e t a i l e d p r e s e n t a t i o n o f t h e l a n g u a g e c o n s t r u c t s .

R y a n S t a n s i f e r . M L p r i m e r . P r e n t i c e - H a l l , 1 9 9 2 .

A s h o r t , b u t n i c e i n t r o d u c t i o n t o p r o g r a m m i n g i n S t a n d a r d M L .

T h e r e s e A c c a r t H a r d i n a n d V e r o n i q u e D o n z e a u - G o u g e V i g u i e . C o n c e p t s e t o u t i l s d e l a p r o -

g r a m m a t i o n . D u f o n c t i o n n e l a l ' i m p e r a t i f a v e c C a m l e t A d a . I n t e r

E d i t i o n s , 1 9 9 2 .

2 0 3

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 206/218

2 0 4

A r s t c o u r s e i n p r o g r a m m i n g , t h a t r s t i n t r o d u c e s t h e m a i n p r o g r a m m i n g n o t i o n s i n C a m l ,

t h e n s h o w s t h e m u n d e r l y i n g A d a . I n t e n d e d f o r b e g i n n e r s ; s l o w - p a c e d f o r t h e o t h e r s .

R a c h e l H a r r i s o n . A b s t r a c t D a t a T y p e s i n S t a n d a r d M L . J o h n W i l e y & S o n s , 1 9 9 3 .

A p r e s e n t a t i o n o f S t a n d a r d M L f r o m t h e s t a n d p o i n t o f a b s t r a c t d a t a t y p e s . U s e s i n t e n s i v e l y

t h e S t a n d a r d M L m o d u l e s y s t e m .

H a r o l d A b e l s o n a n d G e r a l d J a y S u s s m a n . S t r u c t u r e a n d I n t e r p r e t a t i o n o f C o m p u t e r P r o -

g r a m s . T h e M I T p r e s s , 1 9 8 5 . ( F r e n c h t r a n s l a t i o n : S t r u c t u r e e t i n t e r p r e t a t i o n d e s p r o g r a m m e s

i n f o r m a t i q u e s , I n t e r

E d i t i o n s , 1 9 8 9 . )

A n o u t s t a n d i n g c o u r s e o n p r o g r a m m i n g , t a u g h t i n S c h e m e , t h e m o d e r n d i a l e c t o f L i s p . W e l l

w o r t h r e a d i n g , e v e n i f y o u a r e m o r e i n t e r e s t e d i n M L t h a n i n L i s p .

1 9 . 2 D e s c r i p t i o n s o f M L d i a l e c t s

T h e b o o k s a n d r e p o r t s b e l o w a r e d e s c r i p t i o n s o f v a r i o u s p r o g r a m m i n g l a n g u a g e s f r o m t h e M L

f a m i l y . T h e y a s s u m e s o m e f a m i l i a r i t y w i t h M L .

X a v i e r L e r o y a n d P i e r r e W e i s . M a n u e l d e r e f e r e n c e d u l a n g a g e C a m l . I n t e r

E d i t i o n s , 1 9 9 3 .

T h e F r e n c h e d i t i o n o f t h e p r e s e n t r e f e r e n c e m a n u a l a n d u s e r ' s m a n u a l .

R o b e r t H a r p e r . I n t r o d u c t i o n t o S t a n d a r d M L . T e c h n i c a l r e p o r t E C S - L F C S - 8 6 - 1 4 , U n i v e r s i t y

o f E d i n b u r g h , 1 9 8 6 .

A n o v e r v i e w o f S t a n d a r d M L , i n c l u d i n g t h e m o d u l e s y s t e m . T e r s e , b u t s t i l l r e a d a b l e .

R o b i n M i l n e r , M a d s T o f t e a n d R o b e r t H a r p e r . T h e d e n i t i o n o f S t a n d a r d M L . T h e M I T

p r e s s , 1 9 9 0 .

A c o m p l e t e f o r m a l d e n i t i o n o f S t a n d a r d M L , i n t h e f r a m e w o r k o f s t r u c t u r e d o p e r a t i o n a l s e -

m a n t i c s . T h i s b o o k i s p r o b a b l y t h e m o s t m a t h e m a t i c a l l y p r e c i s e d e n i t i o n o f a p r o g r a m m i n g

l a n g u a g e e v e r w r i t t e n . I t i s h e a v y o n f o r m a l i s m a n d e x t r e m e l y t e r s e , s o e v e n r e a d e r s w h o a r e

t h o r o u g h l y f a m i l i a r w i t h M L w i l l h a v e m a j o r d i c u l t i e s w i t h i t .

R o b i n M i l n e r a n d M a d s T o f t e . C o m m e n t a r y o n S t a n d a r d M L . T h e M I T P r e s s , 1 9 9 1 .

A c o m m e n t a r y o n t h e b o o k a b o v e , t h a t a t t e m p t s t o e x p l a i n t h e m o s t d e l i c a t e p a r t s a n d

m o t i v a t e t h e d e s i g n c h o i c e s . E a s i e r t o r e a d t h a n t h e D e n i t i o n , b u t s t i l l r a t h e r i n v o l v i n g .

G u y C o u s i n e a u a n d G e r a r d H u e t . T h e C A M L p r i m e r . T e c h n i c a l r e p o r t 1 2 2 , I N R I A , 1 9 9 0 .

A s h o r t d e s c r i p t i o n o f t h e o r i g i n a l C a m l s y s t e m , f r o m w h i c h C a m l L i g h t h a s e v o l v e d . S o m e

f a m i l i a r i t y w i t h L i s p i s a s s u m e d .

P i e r r e W e i s e t a l . T h e C A M L r e f e r e n c e m a n u a l , v e r s i o n 2 . 6 . 1 . T e c h n i c a l r e p o r t 1 2 1 , I N R I A ,

1 9 9 0 .

T h e m a n u a l f o r t h e o r i g i n a l C a m l s y s t e m , f r o m w h i c h C a m l L i g h t h a s e v o l v e d .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 207/218

C h a p t e r 1 9 . F u r t h e r r e a d i n g 2 0 5

M i c h a e l J . G o r d o n , A r t h u r J . M i l n e r a n d C h r i s t o p h e r P . W a d s w o r t h . E d i n b u r g h L C F . L e c t u r e

N o t e s i n C o m p u t e r S c i e n c e v o l u m e 7 8 , S p r i n g e r - V e r l a g , 1 9 7 9 .

T h i s i s t h e r s t p u b l i s h e d d e s c r i p t i o n o f t h e M L l a n g u a g e , a t t h e t i m e w h e n i t w a s n o t h i n g

m o r e t h a n t h e c o n t r o l l a n g u a g e f o r t h e L C F s y s t e m , a t h e o r e m p r o v e r . T h i s b o o k i s n o w

o b s o l e t e , s i n c e t h e M L l a n g u a g e h a s m u c h e v o l v e d s i n c e t h e n ; b u t i t i s s t i l l o f h i s t o r i c a l

i n t e r e s t .

P a u l H u d a k , S i m o n P e y t o n - J o n e s a n d P h i l i p W a d l e r . R e p o r t o n t h e p r o g r a m m i n g l a n g u a g e

H a s k e l l , v e r s i o n 1 . 1 . T e c h n i c a l r e p o r t , Y a l e U n i v e r s i t y , 1 9 9 1 .

H a s k e l l i s a p u r e l y f u n c t i o n a l l a n g u a g e w i t h l a z y s e m a n t i c s t h a t s h a r e s m a n y i m p o r t a n t

p o i n t s w i t h M L ( f u l l f u n c t i o n a l i t y , p o l y m o r p h i c t y p i n g ) , b u t h a s i n t e r e s t i n g f e a t u r e s o f i t s

o w n ( d y n a m i c o v e r l o a d i n g , a l s o c a l l e d t y p e c l a s s e s ) .

1 9 . 3 I m p l e m e n t i n g f u n c t i o n a l p r o g r a m m i n g l a n g u a g e s

T h e r e f e r e n c e s b e l o w a r e i n t e n d e d f o r t h o s e w h o a r e c u r i o u s t o l e a r n h o w a l a n g u a g e l i k e C a m l

L i g h t i s c o m p i l e d a n d i m p l e m e n t e d .

X a v i e r L e r o y . T h e Z I N C e x p e r i m e n t : a n e c o n o m i c a l i m p l e m e n t a t i o n o f t h e M L l a n g u a g e .

T e c h n i c a l r e p o r t 1 1 7 , I N R I A , 1 9 9 0 . ( A v a i l a b l e b y a n o n y m o u s F T P o n f t p . i n r i a . f r . )

A d e s c r i p t i o n o f t h e Z I N C i m p l e m e n t a t i o n , t h e p r o t o t y p e M L i m p l e m e n t a t i o n t h a t h a s

e v o l v e d i n t o C a m l L i g h t . L a r g e p a r t s o f t h i s r e p o r t s t i l l a p p l y t o t h e c u r r e n t C a m l L i g h t

s y s t e m , i n p a r t i c u l a r t h e d e s c r i p t i o n o f t h e e x e c u t i o n m o d e l a n d a b s t r a c t m a c h i n e . O t h e r

p a r t s a r e n o w o b s o l e t e . Y e t t h i s r e p o r t s t i l l g i v e s a c o m p l e t e o v e r v i e w o f t h e i m p l e m e n t a t i o n

t e c h n i q u e s u s e d i n C a m l L i g h t .

S i m o n P e y t o n - J o n e s . T h e i m p l e m e n t a t i o n o f f u n c t i o n a l p r o g r a m m i n g l a n g u a g e s . P r e n t i c e -

H a l l , 1 9 8 7 . ( F r e n c h t r a n s l a t i o n : M i s e e n u v r e d e s l a n g a g e s f o n c t i o n n e l s d e p r o g r a m m a t i o n ,

M a s s o n , 1 9 9 0 . )

A n e x c e l l e n t d e s c r i p t i o n o f t h e i m p l e m e n t a t i o n o f p u r e l y f u n c t i o n a l l a n g u a g e s w i t h l a z y s e -

m a n t i c s , u s i n g t h e t e c h n i q u e k n o w n a s g r a p h r e d u c t i o n . T h e p a r t o f t h e b o o k t h a t d e a l s w i t h

t h e t r a n s f o r m a t i o n f r o m M L t o e n r i c h e d l a m b d a - c a l c u l u s d i r e c t l y a p p l i e s t o C a m l L i g h t . Y o u

w i l l n d a g o o d d e s c r i p t i o n o f h o w p a t t e r n - m a t c h i n g i s c o m p i l e d a n d h o w t y p e s a r e i n f e r r e d .

T h e r e m a i n d e r o f t h e b o o k d o e s n o t a p p l y d i r e c t l y t o C a m l L i g h t , s i n c e C a m l L i g h t i s n o t

p u r e l y f u n c t i o n a l ( i t h a s s i d e - e e c t s ) , h a s s t r i c t s e m a n t i c s , a n d d o e s n o t u s e g r a p h r e d u c t i o n

a t a l l .

A n d r e w W . A p p e l . C o m p i l i n g w i t h c o n t i n u a t i o n s . C a m b r i d g e U n i v e r s i t y P r e s s , 1 9 9 2 .

A c o m p l e t e d e s c r i p t i o n o f a n o p t i m i z i n g c o m p i l e r f o r S t a n d a r d M L , b a s e d o n a n i n t e r m e d i a t e

r e p r e s e n t a t i o n c a l l e d c o n t i n u a t i o n - p a s s i n g s t y l e . S h o w s h o w m a n y a d v a n c e d p r o g r a m o p t i -

m i z a t i o n s c a n b e a p p l i e d t o M L . N o t d i r e c t l y r e l e v a n t t o t h e C a m l L i g h t s y s t e m , s i n c e C a m l

L i g h t d o e s n o t u s e c o n t i n u a t i o n - p a s s i n g s t y l e a t a l l , a n d m a k e s l i t t l e a t t e m p t s a t o p t i m i z i n g

p r o g r a m s .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 208/218

2 0 6

1 9 . 4 A p p l i c a t i o n s o f M L

T h e f o l l o w i n g r e p o r t s s h o w M L a t w o r k i n v a r i o u s , s o m e t i m e s u n e x p e c t e d , a r e a s .

E m m a n u e l C h a i l l o u x a n d G u y C o u s i n e a u . T h e M L g r a p h p r i m e r . T e c h n i c a l r e p o r t 9 2 - 1 5 ,

E c o l e N o r m a l e S u p e r i e u r e , 1 9 9 2 . ( A v a i l a b l e b y a n o n y m o u s F T P o n f t p . e n s . f r . )

D e s c r i b e s a C a m l L i g h t l i b r a r y t h a t p r o d u c e s P o s t s c r i p t p i c t u r e s t h r o u g h h i g h - l e v e l d r a w i n g

f u n c t i o n s .

X a v i e r L e r o y . P r o g r a m m a t i o n d u s y s t e m e U n i x e n C a m l L i g h t . T e c h n i c a l r e p o r t 1 4 7 , I N R I A ,

1 9 9 2 . ( A v a i l a b l e b y a n o n y m o u s F T P o n f t p . i n r i a . f r . )

A U n i x s y s t e m s p r o g r a m m i n g c o u r s e , d e m o n s t r a t i n g t h e u s e o f t h e C a m l L i g h t l i b r a r y t h a t

g i v e s a c c e s s t o U n i x s y s t e m c a l l s .

J o h n H . R e p p y . C o n c u r r e n t p r o g r a m m i n g w i t h e v e n t s | T h e c o n c u r r e n t M L m a n u a l . C o r n e l l

U n i v e r s i t y , 1 9 9 0 . ( A v a i l a b l e b y a n o n y m o u s F T P o n r e s e a r c h . a t t . c o m . )

C o n c u r r e n t M L e x t e n d s S t a n d a r d M L o f N e w J e r s e y w i t h c o n c u r r e n t p r o c e s s e s t h a t c o m m u -

n i c a t e t h r o u g h c h a n n e l s a n d e v e n t s .

J e a n n e t t e M . W i n g , M a n u e l F a e h n d r i c h , J . G r e g o r y M o r r i s e t t a n d S c o t t t N e t t l e s . E x t e n s i o n s

t o S t a n d a r d M L t o s u p p o r t t r a n s a c t i o n s . T e c h n i c a l r e p o r t C M U - C S - 9 2 - 1 3 2 , C a r n e g i e - M e l l o n

U n i v e r s i t y , 1 9 9 2 . ( A v a i l a b l e b y a n o n y m o u s F T P o n r e p o r t s . a d m . c s . c m u . e d u . )

H o w t o i n t e g r a t e t h e b a s i c d a t a b a s e o p e r a t i o n s t o S t a n d a r d M L .

E m d e n R . G a n s n e r a n d J o h n H . R e p p y . e X e n e . B e l l L a b s , 1 9 9 1 . ( A v a i l a b l e b y a n o n y m o u s

F T P o n r e s e a r c h . a t t . c o m . )

A n i n t e r f a c e b e t w e e n S t a n d a r d M L o f N e w J e r s e y a n d t h e X W i n d o w s w i n d o w i n g s y s t e m .

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 209/218

I n d e x t o t h e l i b r a r y

! ( i n x ) , 1 3 0

! = ( i n x ) , 1 1 6

& ( i n x ) , 1 1 4

& & ( i n x ) , 1 1 4

* ( i n x ) , 1 1 7 , 1 2 0

* * ( i n x ) , 1 1 8

* * . ( i n x ) , 1 1 8

* * / ( i n x ) , 1 9 2

* . ( i n x ) , 1 1 7

* / ( i n x ) , 1 9 2

+ ( i n x ) , 1 1 7 , 1 2 0

+ . ( i n x ) , 1 1 7

+ / ( i n x ) , 1 9 2

- ( i n x ) , 1 1 7 , 1 2 0

- . ( i n x ) , 1 1 7

- / ( i n x ) , 1 9 2

/ ( i n x ) , 1 1 7 , 1 2 0

/ . ( i n x ) , 1 1 7

/ / ( i n x ) , 1 9 2

< ( i n x ) , 1 1 5

< . ( i n x ) , 1 1 8

< / ( i n x ) , 1 9 3

< = ( i n x ) , 1 1 5

< = . ( i n x ) , 1 1 8

< = / ( i n x ) , 1 9 3

< > ( i n x ) , 1 1 5

< > . ( i n x ) , 1 1 8

< > / ( i n x ) , 1 9 3

= ( i n x ) , 1 1 5

= . ( i n x ) , 1 1 8

= / ( i n x ) , 1 9 3

= = ( i n x ) , 1 1 6

> ( i n x ) , 1 1 5

> . ( i n x ) , 1 1 8

> / ( i n x ) , 1 9 3

> = ( i n x ) , 1 1 5

> = . ( i n x ) , 1 1 8

> = / ( i n x ) , 1 9 3

@ ( i n x ) , 1 2 7

^ ( i n x ) , 1 3 2

| | ( i n x ) , 1 1 4

a b s , 1 2 1

a b s _ f l o a t , 1 1 9

a b s _ n u m , 1 9 3

a c c e p t , 1 8 6

a c c e s s , 1 7 8

a c o s , 1 1 8

a d d , 1 3 9 , 1 5 0 , 1 5 3 , 1 5 6 , 1 5 8

a d d _ f l o a t , 1 1 7

a d d _ i n t , 1 2 0

a d d _ n u m , 1 9 2

a l a r m , 1 8 3

a p p r o x _ n u m _ e x p , 1 9 4

a p p r o x _ n u m _ f i x , 1 9 4

a r g ( m o d u l e ) , 1 3 7

a r i t h _ s t a t u s , 1 9 4

a r i t h _ s t a t u s ( m o d u l e ) , 1 9 4

a s i n , 1 1 8

a s r ( i n x ) , 1 2 1

a s s o c , 1 2 9

a s s q , 1 2 9

a t a n , 1 1 8

a t a n 2 , 1 1 8

b a c k g r o u n d , 1 6 5

B a d ( e x c e p t i o n ) , 1 3 8

b a l t r e e ( m o d u l e ) , 1 3 8

b a s e n a m e , 1 4 0

b i g _ i n t _ o f _ n u m , 1 9 4

b i n d , 1 8 6

b l a c k , 1 6 5

b l i t _ i m a g e , 1 6 8

b l i t _ s t r i n g , 1 3 3

2 0 7

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 210/218

2 0 8

b l i t _ v e c t , 1 3 5

b l u e , 1 6 5

b o o l ( m o d u l e ) , 1 1 3

b o u n d e d _ s p l i t , 1 9 9

B r e a k ( e x c e p t i o n ) , 1 6 1

b u i l t i n ( m o d u l e ) , 1 1 4

b u t t o n _ d o w n , 1 6 9

c a t c h _ b r e a k , 1 6 1

c d , 5 7

c e i l , 1 1 8

c e i l i n g _ n u m , 1 9 2

c h a r ( m o d u l e ) , 1 1 5

c h a r _ f o r _ r e a d , 1 1 5

c h a r _ o f _ i n t , 1 1 5

c h d i r , 1 6 1 , 1 7 9

c h e c k _ s u f f i x , 1 4 0

c h m o d , 1 7 8

c h o o s e , 1 5 8

c h o p _ s u f f i x , 1 4 0

c h o w n , 1 7 8

c l e a r , 1 5 0 , 1 5 6 , 1 5 9

c l e a r _ g r a p h , 1 6 5

c l e a r _ p a r s e r , 1 5 4

c l o s e , 1 6 1 , 1 7 6

c l o s e _ b o x , 1 4 1

c l o s e _ g r a p h , 1 6 4

c l o s e _ i n , 1 2 7

c l o s e _ o u t , 1 2 5

c l o s e _ p r o c e s s , 1 8 0

c l o s e _ p r o c e s s _ i n , 1 8 0

c l o s e _ p r o c e s s _ o u t , 1 8 0

c l o s e _ t b o x , 1 4 4

c l o s e d i r , 1 7 9

c o m b i n e , 1 3 0

c o m m a n d _ l i n e , 1 6 0

c o m p a r e , 1 1 5 , 1 3 9 , 1 5 8

c o m p a r e _ n u m , 1 9 3

c o m p a r e _ s t r i n g s , 1 3 3

c o m p i l e , 5 6

c o n c a t , 1 3 2 , 1 4 0 , 2 0 0

c o n c a t _ v e c t , 1 3 4

c o n n e c t , 1 8 6

c o n t a i n s , 1 3 9

c o p y _ v e c t , 1 3 4

c o s , 1 1 8

c o s h , 1 1 8

c r e a t e _ i m a g e , 1 6 8

c r e a t e _ l e x e r , 1 5 2

c r e a t e _ l e x e r _ c h a n n e l , 1 5 2

c r e a t e _ l e x e r _ s t r i n g , 1 5 2

c r e a t e _ s t r i n g , 1 3 2

c u r r e n t _ d i r _ n a m e , 1 4 0

c u r r e n t _ p o i n t , 1 6 6

c y a n , 1 6 5

d e b u g _ m o d e , 5 7

d e c r , 1 3 0

d e c r _ n u m , 1 9 3

d e n o m i n a t o r _ n u m , 1 9 1

d e s c r _ o f _ i n _ c h a n n e l , 1 7 6

d e s c r _ o f _ o u t _ c h a n n e l , 1 7 6

d i f f , 1 5 8

d i r e c t o r y , 5 7

d i r n a m e , 1 4 0

d i v _ f l o a t , 1 1 7

d i v _ i n t , 1 2 0

d i v _ n u m , 1 9 2

D i v i s i o n _ b y _ z e r o ( e x c e p t i o n ) , 1 2 0

d o _ l i s t , 1 2 8

d o _ l i s t 2 , 1 2 8

d o _ l i s t _ c o m b i n e , 1 3 0

d o _ s t r e a m , 1 3 1

d o _ t a b l e , 1 5 1

d o _ t a b l e _ r e v , 1 5 1

d o _ v e c t , 1 3 5

d r a w _ a r c , 1 6 6

d r a w _ c h a r , 1 6 6

d r a w _ c i r c l e , 1 6 6

d r a w _ e l l i p s e , 1 6 6

d r a w _ i m a g e , 1 6 7

d r a w _ s t r i n g , 1 6 6

d u m p _ i m a g e , 1 6 7

d u p , 1 7 9

d u p 2 , 1 7 9

e l e m e n t s , 1 5 8

e m p t y , 1 5 3 , 1 5 7

E m p t y ( e x c e p t i o n ) , 1 5 6 , 1 5 9

E n d _ o f _ f i l e ( e x c e p t i o n ) , 1 2 2

e n d _ o f _ s t r e a m , 1 3 1

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 211/218

I n d e x t o t h e l i b r a r y 2 0 9

e n v i r o n m e n t , 1 7 3

e p r i n t , 1 5 5

e p r i n t f , 1 4 7 , 1 5 5

e q ( m o d u l e ) , 1 1 5

e q _ f l o a t , 1 1 8

e q _ i n t , 1 2 0

e q _ n u m , 1 9 3

e q _ s t r i n g , 1 3 3

e q u a l , 1 5 8

e r r _ f o r m a t t e r , 1 4 5

e r r o r _ m e s s a g e , 1 7 3

e s t a b l i s h _ s e r v e r , 1 8 7

e x c ( m o d u l e ) , 1 1 6

e x c e p t , 1 2 9

e x c e p t q , 1 2 9

e x e c v , 1 7 4

e x e c v e , 1 7 4

e x e c v p , 1 7 4

e x i s t s , 1 2 9

e x i t , 1 2 2 , 1 6 0

E x i t ( e x c e p t i o n ) , 1 1 6

e x p , 1 1 8

F a i l u r e ( e x c e p t i o n ) , 1 1 6

f a i l w i t h , 1 1 6

f c h a r ( m o d u l e ) , 1 1 7

f c h m o d , 1 7 8

f c h o w n , 1 7 8

f c n t l _ i n t , 1 7 8

f c n t l _ p t r , 1 7 8

f i l e n a m e ( m o d u l e ) , 1 4 0

f i l l _ a r c , 1 6 7

f i l l _ c i r c l e , 1 6 7

f i l l _ e l l i p s e , 1 6 7

f i l l _ p o l y , 1 6 7

f i l l _ r e c t , 1 6 7

f i l l _ s t r i n g , 1 3 2

f i l l _ v e c t , 1 3 5

f i n d , 1 3 9 , 1 5 0 , 1 5 3

f i n d _ a l l , 1 5 0

f i r s t _ c h a r s , 2 0 0

f l a t _ m a p , 1 2 9

f l o a t , 1 5 7

f l o a t ( m o d u l e ) , 1 1 7

f l o a t _ o f _ i n t , 1 1 7

f l o a t _ o f _ n u m , 1 9 4

f l o a t _ o f _ s t r i n g , 1 1 9

f l o o r , 1 1 8

f l o o r _ n u m , 1 9 2

f l u s h , 1 2 4

f o l d , 1 5 8

f o r _ a l l , 1 2 9

f o r c e _ n e w l i n e , 1 4 2

f o r e g r o u n d , 1 6 5

f o r k , 1 7 4

f o r m a t , 2 0 0

f o r m a t ( m o d u l e ) , 1 4 0

f p r i n t , 1 5 5

f p r i n t f , 1 4 6 , 1 5 4

f r e x p , 1 1 9

f s t , 1 3 0

f s t a t , 1 7 7

f s t r i n g ( m o d u l e ) , 1 1 9

f t r u n c a t e , 1 7 6

f u l l _ i n i t , 1 5 7

f u l l _ m a j o r , 1 4 9

f v e c t ( m o d u l e ) , 1 1 9

g c ( m o d u l e ) , 1 4 7

g e _ f l o a t , 1 1 8

g e _ i n t , 1 2 1

g e _ n u m , 1 9 3

g e _ s t r i n g , 1 3 3

g e n l e x ( m o d u l e ) , 1 4 9

g e t , 1 4 8

g e t _ a p p r o x _ p r i n t i n g , 1 9 5

g e t _ e l l i p s i s _ t e x t , 1 4 4

g e t _ e r r o r _ w h e n _ n u l l _ d e n o m i n a t o r , 1 9 4

g e t _ f l o a t i n g _ p r e c i s i o n , 1 9 5

g e t _ f o r m a t t e r _ o u t p u t _ f u n c t i o n s , 1 4 4

g e t _ i m a g e , 1 6 8

g e t _ l e x e m e , 1 5 2

g e t _ l e x e m e _ c h a r , 1 5 2

g e t _ l e x e m e _ e n d , 1 5 2

g e t _ l e x e m e _ s t a r t , 1 5 2

g e t _ m a r g i n , 1 4 2

g e t _ m a x _ b o x e s , 1 4 3

g e t _ m a x _ i n d e n t , 1 4 3

g e t _ n o r m a l i z e _ r a t i o , 1 9 5

g e t _ n o r m a l i z e _ r a t i o _ w h e n _ p r i n t i n g , 1 9 5

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 212/218

2 1 0

g e t c w d , 1 7 9

g e t e g i d , 1 8 4

g e t e n v , 1 6 1

g e t e u i d , 1 8 4

g e t g i d , 1 8 4

g e t g r g i d , 1 8 5

g e t g r n a m , 1 8 5

g e t g r o u p s , 1 8 4

g e t h o s t b y a d d r , 1 8 8

g e t h o s t b y n a m e , 1 8 8

g e t h o s t n a m e , 1 8 8

g e t l o g i n , 1 8 4

g e t p e e r n a m e , 1 8 7

g e t p i d , 1 7 5

g e t p p i d , 1 7 5

g e t p r o t o b y n a m e , 1 8 8

g e t p r o t o b y n u m b e r , 1 8 8

g e t p w n a m , 1 8 5

g e t p w u i d , 1 8 5

g e t s e r v b y n a m e , 1 8 8

g e t s e r v b y p o r t , 1 8 8

g e t s o c k n a m e , 1 8 6

g e t t i m e o f d a y , 1 8 3

g e t u i d , 1 8 4

g l o b a l _ r e p l a c e , 1 9 9

g l o b a l _ s u b s t i t u t e , 1 9 9

g m t i m e , 1 8 3

G r a p h i c _ f a i l u r e ( e x c e p t i o n ) , 1 6 4

g r a p h i c s ( m o d u l e ) , 1 6 4

g r e e n , 1 6 5

g r o u p _ b e g i n n i n g , 1 9 9

g r o u p _ e n d , 1 9 9

g t _ f l o a t , 1 1 8

g t _ i n t , 1 2 1

g t _ n u m , 1 9 3

g t _ s t r i n g , 1 3 3

h a n d l e _ u n i x _ e r r o r , 1 7 3

h a s h , 1 5 1

h a s h _ p a r a m , 1 5 1

h a s h t b l ( m o d u l e ) , 1 5 0

h d , 1 2 8

i n _ c h a n n e l _ l e n g t h , 1 2 7

i n _ c h a n n e l _ o f _ d e s c r , 1 7 6

i n c l u d e , 5 6

i n c r , 1 3 0

i n c r _ n u m , 1 9 3

i n d e x , 1 2 9

i n d e x _ c h a r , 1 3 3

i n d e x _ c h a r _ f r o m , 1 3 3

i n e t _ a d d r _ o f _ s t r i n g , 1 8 5

i n i t , 1 5 7

i n i t _ v e c t , 1 3 4

i n p u t , 1 2 6

i n p u t _ b i n a r y _ i n t , 1 2 7

i n p u t _ b y t e , 1 2 7

i n p u t _ c h a r , 1 2 6

i n p u t _ l i n e , 1 2 6

i n p u t _ v a l u e , 1 2 7

i n s t a l l _ p r i n t e r , 5 7

i n t , 1 5 7

i n t ( m o d u l e ) , 1 2 0

i n t _ o f _ c h a r , 1 1 5

i n t _ o f _ f l o a t , 1 1 7

i n t _ o f _ n u m , 1 9 4

i n t _ o f _ s t r i n g , 1 2 2

i n t e g e r _ n u m , 1 9 2

i n t e r , 1 5 8

i n t e r a c t i v e , 1 6 0

i n t e r s e c t , 1 2 9

i n v a l i d _ a r g , 1 1 6

I n v a l i d _ a r g u m e n t ( e x c e p t i o n ) , 1 1 6

i o ( m o d u l e ) , 1 2 2

i o c t l _ i n t , 1 8 0

i o c t l _ p t r , 1 8 0

i s _ a b s o l u t e , 1 4 0

i s _ e m p t y , 1 5 7

i s _ i n t e g e r _ n u m , 1 9 2

i t _ l i s t , 1 2 8

i t _ l i s t 2 , 1 2 8

i t e r , 1 5 3 , 1 5 6 , 1 5 8 , 1 5 9

j o i n , 2 0 0

k e y _ p r e s s e d , 1 6 9

k i l l , 1 8 2

l a n d ( i n x ) , 1 2 1

l a s t _ c h a r s , 2 0 0

l d e x p , 1 1 9

l e _ f l o a t , 1 1 8

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 213/218

I n d e x t o t h e l i b r a r y 2 1 1

l e _ i n t , 1 2 1

l e _ n u m , 1 9 3

l e _ s t r i n g , 1 3 3

l e n g t h , 1 5 6 , 1 5 9

l e x i n g ( m o d u l e ) , 1 5 1

l i n e t o , 1 6 6

l i n k , 1 7 7

l i s t ( m o d u l e ) , 1 2 7

l i s t _ i t , 1 2 8

l i s t _ i t 2 , 1 2 8

l i s t _ l e n g t h , 1 2 7

l i s t _ o f _ v e c t , 1 3 5

l i s t e n , 1 8 6

l n o t , 1 2 1

l o a d , 5 6

l o a d _ o b j e c t , 5 6

l o c a l t i m e , 1 8 3

l o c k f , 1 8 1

l o g , 1 1 8

l o g 1 0 , 1 1 8

l o r ( i n x ) , 1 2 1

l s e e k , 1 7 6

l s h i f t _ l e f t , 1 2 1

l s h i f t _ r i g h t , 1 2 1

l s l ( i n x ) , 1 2 1

l s r ( i n x ) , 1 2 1

l s t a t , 1 7 7

l t _ f l o a t , 1 1 8

l t _ i n t , 1 2 1

l t _ n u m , 1 9 3

l t _ s t r i n g , 1 3 3

l x o r ( i n x ) , 1 2 1

m a g e n t a , 1 6 5

m a j o r , 1 4 9

m a k e _ f o r m a t t e r , 1 4 5

m a k e _ i m a g e , 1 6 7

m a k e _ l e x e r , 1 4 9

m a k e _ m a t r i x , 1 3 4

m a k e _ s t r i n g , 1 3 2

m a k e _ v e c t , 1 3 4

m a p , 1 2 8

m a p ( m o d u l e ) , 1 5 3

m a p 2 , 1 2 8

m a p _ c o m b i n e , 1 3 0

m a p _ v e c t , 1 3 5

m a p _ v e c t _ l i s t , 1 3 5

m a t c h _ b e g i n n i n g , 1 9 8

m a t c h _ e n d , 1 9 8

M a t c h _ f a i l u r e ( e x c e p t i o n ) , 2 4 { 2 6 , 1 1 4

m a t c h e d _ g r o u p , 1 9 8

m a t c h e d _ s t r i n g , 1 9 8

m a x , 1 1 6

m a x _ i n t , 1 2 1

m a x _ n u m , 1 9 3

m e m , 1 2 9 , 1 5 7

m e m _ a s s o c , 1 2 9

m e m q , 1 2 9

m e r g e , 1 5 8

m i n , 1 1 6

m i n _ i n t , 1 2 1

m i n _ n u m , 1 9 3

m i n o r , 1 4 8

m i n u s , 1 1 7 , 1 2 0

m i n u s _ f l o a t , 1 1 7

m i n u s _ i n t , 1 2 0

m i n u s _ n u m , 1 9 2

m k d i r , 1 7 9

m k f i f o , 1 8 0

m o d ( i n x ) , 1 2 0

m o d _ f l o a t , 1 1 9

m o d _ n u m , 1 9 2

m o d f , 1 1 9

m o d i f y , 1 3 9

m o u s e _ p o s , 1 6 9

m o v e t o , 1 6 6

m u l t _ f l o a t , 1 1 7

m u l t _ i n t , 1 2 0

m u l t _ n u m , 1 9 2

n a t _ o f _ n u m , 1 9 4

n e q _ f l o a t , 1 1 8

n e q _ i n t , 1 2 0

n e q _ s t r i n g , 1 3 3

n e w , 1 5 0 , 1 5 6 , 1 5 9

n i c e , 1 7 5

n o r m a l i z e _ n u m , 1 9 1

n o t ( i n x ) , 1 1 4

N o t _ f o u n d ( e x c e p t i o n ) , 1 1 6

n t h _ c h a r , 1 3 2

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 214/218

2 1 2

n u m ( m o d u l e ) , 1 9 1

n u m _ o f _ b i g _ i n t , 1 9 4

n u m _ o f _ f l o a t , 1 9 4

n u m _ o f _ i n t , 1 9 4

n u m _ o f _ n a t , 1 9 4

n u m _ o f _ r a t i o , 1 9 4

n u m _ o f _ s t r i n g , 1 9 4

n u m e r a t o r _ n u m , 1 9 1

o p e n , 1 6 1 , 1 7 5

o p e n _ b o x , 1 4 1

o p e n _ c o n n e c t i o n , 1 8 7

o p e n _ d e s c r i p t o r _ i n , 1 2 6

o p e n _ d e s c r i p t o r _ o u t , 1 2 4

o p e n _ g r a p h , 1 6 4

o p e n _ h b o x , 1 4 3

o p e n _ h o v b o x , 1 4 3

o p e n _ h v b o x , 1 4 3

o p e n _ i n , 1 2 6

o p e n _ i n _ b i n , 1 2 6

o p e n _ i n _ g e n , 1 2 6

o p e n _ o u t , 1 2 4

o p e n _ o u t _ b i n , 1 2 4

o p e n _ o u t _ g e n , 1 2 4

o p e n _ p r o c e s s , 1 8 0

o p e n _ p r o c e s s _ i n , 1 8 0

o p e n _ p r o c e s s _ o u t , 1 8 0

o p e n _ t b o x , 1 4 4

o p e n _ v b o x , 1 4 3

o p e n d i r , 1 7 9

o r ( i n x ) , 1 1 4

o u t _ c h a n n e l _ l e n g t h , 1 2 5

o u t _ c h a n n e l _ o f _ d e s c r , 1 7 6

O u t _ o f _ m e m o r y ( e x c e p t i o n ) , 1 1 6

o u t p u t , 1 2 5

o u t p u t _ b i n a r y _ i n t , 1 2 5

o u t p u t _ b y t e , 1 2 5

o u t p u t _ c h a r , 1 2 5

o u t p u t _ c o m p a c t _ v a l u e , 1 2 5

o u t p u t _ s t r i n g , 1 2 5

o u t p u t _ v a l u e , 1 2 5

o v e r _ m a x _ b o x e s , 1 4 3

p a i r ( m o d u l e ) , 1 3 0

p a r s e , 1 3 8

P a r s e _ e r r o r ( e x c e p t i o n ) , 1 3 1 , 1 5 4

P a r s e _ f a i l u r e ( e x c e p t i o n ) , 1 3 1

p a r s i n g ( m o d u l e ) , 1 5 3

p a u s e , 1 8 2

p e e k , 1 5 6

p i p e , 1 7 9

p l o t , 1 6 6

p o i n t _ c o l o r , 1 6 6

p o p , 1 5 9

p o s _ i n , 1 2 7

p o s _ o u t , 1 2 5

p o w e r , 1 1 8

p o w e r _ n u m , 1 9 2

p p _ c l o s e _ b o x , 1 4 6

p p _ c l o s e _ t b o x , 1 4 6

p p _ f o r c e _ n e w l i n e , 1 4 6

p p _ g e t _ e l l i p s i s _ t e x t , 1 4 6

p p _ g e t _ f o r m a t t e r _ o u t p u t _ f u n c t i o n s , 1 4 6

p p _ g e t _ m a r g i n , 1 4 6

p p _ g e t _ m a x _ b o x e s , 1 4 6

p p _ g e t _ m a x _ i n d e n t , 1 4 6

p p _ o p e n _ b o x , 1 4 6

p p _ o p e n _ h b o x , 1 4 6

p p _ o p e n _ h o v b o x , 1 4 6

p p _ o p e n _ h v b o x , 1 4 6

p p _ o p e n _ t b o x , 1 4 6

p p _ o p e n _ v b o x , 1 4 6

p p _ o v e r _ m a x _ b o x e s , 1 4 6

p p _ p r i n t _ a s , 1 4 6

p p _ p r i n t _ b o o l , 1 4 6

p p _ p r i n t _ b r e a k , 1 4 6

p p _ p r i n t _ c h a r , 1 4 6

p p _ p r i n t _ c u t , 1 4 6

p p _ p r i n t _ f l o a t , 1 4 6

p p _ p r i n t _ f l u s h , 1 4 6

p p _ p r i n t _ i f _ n e w l i n e , 1 4 6

p p _ p r i n t _ i n t , 1 4 6

p p _ p r i n t _ n e w l i n e , 1 4 6

p p _ p r i n t _ s p a c e , 1 4 6

p p _ p r i n t _ s t r i n g , 1 4 6

p p _ p r i n t _ t a b , 1 4 6

p p _ p r i n t _ t b r e a k , 1 4 6

p p _ s e t _ e l l i p s i s _ t e x t , 1 4 6

p p _ s e t _ f o r m a t t e r _ o u t _ c h a n n e l , 1 4 6

p p _ s e t _ f o r m a t t e r _ o u t p u t _ f u n c t i o n s , 1 4 6

p p _ s e t _ m a r g i n , 1 4 6

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 215/218

I n d e x t o t h e l i b r a r y 2 1 3

p p _ s e t _ m a x _ b o x e s , 1 4 6

p p _ s e t _ m a x _ i n d e n t , 1 4 6

p p _ s e t _ t a b , 1 4 6

p r e d , 1 2 0

p r e d _ n u m , 1 9 3

p r e r r _ c h a r , 1 2 3

p r e r r _ e n d l i n e , 1 2 3

p r e r r _ f l o a t , 1 2 3

p r e r r _ i n t , 1 2 3

p r e r r _ s t r i n g , 1 2 3

p r i n t , 1 5 5

p r i n t _ a s , 1 4 1

p r i n t _ b o o l , 1 4 1

p r i n t _ b r e a k , 1 4 2

p r i n t _ c h a r , 1 2 2 , 1 4 1

p r i n t _ c u t , 1 4 2

p r i n t _ e n d l i n e , 1 2 3

p r i n t _ f l o a t , 1 2 3 , 1 4 1

p r i n t _ f l u s h , 1 4 2

p r i n t _ i f _ n e w l i n e , 1 4 2

p r i n t _ i n t , 1 2 3 , 1 4 1

p r i n t _ n e w l i n e , 1 2 3 , 1 4 2

p r i n t _ n u m , 1 9 4

p r i n t _ s p a c e , 1 4 2

p r i n t _ s t a t , 1 4 8

p r i n t _ s t r i n g , 1 2 3 , 1 4 1

p r i n t _ t a b , 1 4 4

p r i n t _ t b r e a k , 1 4 4

p r i n t e x c ( m o d u l e ) , 1 5 4

p r i n t f , 1 4 7 , 1 5 5

p r i n t f ( m o d u l e ) , 1 5 4

p u s h , 1 5 9

q u e u e ( m o d u l e ) , 1 5 6

q u i t , 5 6

q u o ( i n x ) , 1 2 0

q u o _ n u m , 1 9 2

r a i s e , 1 1 6

r a n d o m ( m o d u l e ) , 1 5 7

r a t i o _ o f _ n u m , 1 9 4

r e a d , 1 7 6

r e a d _ f l o a t , 1 2 4

r e a d _ i n t , 1 2 4

r e a d _ k e y , 1 6 9

r e a d _ l i n e , 1 2 4

r e a d d i r , 1 7 9

r e a d l i n k , 1 8 0

r e a l l y _ i n p u t , 1 2 6

r e c v , 1 8 7

r e c v f r o m , 1 8 7

r e d , 1 6 5

r e f ( m o d u l e ) , 1 3 0

r e g e x p , 1 9 7

r e g e x p _ c a s e _ f o l d , 1 9 8

r e m o v e , 1 3 9 , 1 5 0 , 1 5 3 , 1 5 8 , 1 6 1

r e m o v e _ p r i n t e r , 5 7

r e n a m e , 1 6 1 , 1 7 7

r e p l a c e _ f i r s t , 1 9 9

r e p l a c e _ s t r i n g , 1 3 3

r e v , 1 2 8

r e w i n d d i r , 1 7 9

r g b , 1 6 5

r h s _ e n d , 1 5 4

r h s _ s t a r t , 1 5 4

r i n d e x _ c h a r , 1 3 3

r i n d e x _ c h a r _ f r o m , 1 3 3

r m d i r , 1 7 9

r o u n d _ n u m , 1 9 2

s _ i r a l l , 1 6 0

s _ i r g r p , 1 6 0

s _ i r o t h , 1 6 0

s _ i r u s r , 1 6 0

s _ i s g i d , 1 6 0

s _ i s u i d , 1 6 0

s _ i w a l l , 1 6 0

s _ i w g r p , 1 6 0

s _ i w o t h , 1 6 0

s _ i w u s r , 1 6 0

s _ i x a l l , 1 6 0

s _ i x g r p , 1 6 0

s _ i x o t h , 1 6 0

s _ i x u s r , 1 6 0

s e a r c h _ b a c k w a r d , 1 9 8

s e a r c h _ f o r w a r d , 1 9 8

s e e k _ i n , 1 2 7

s e e k _ o u t , 1 2 5

s e l e c t , 1 8 1

s e n d , 1 8 7

s e n d t o , 1 8 7

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 216/218

2 1 4

s e t , 1 4 8

s e t ( m o d u l e ) , 1 5 7

s e t _ a p p r o x _ p r i n t i n g , 1 9 5

s e t _ c o l o r , 1 6 5

s e t _ e l l i p s i s _ t e x t , 1 4 4

s e t _ e r r o r _ w h e n _ n u l l _ d e n o m i n a t o r , 1 9 4

s e t _ f l o a t i n g _ p r e c i s i o n , 1 9 5

s e t _ f o n t , 1 6 6

s e t _ f o r m a t t e r _ o u t _ c h a n n e l , 1 4 4

s e t _ f o r m a t t e r _ o u t p u t _ f u n c t i o n s , 1 4 4

s e t _ l i n e _ w i d t h , 1 6 6

s e t _ m a r g i n , 1 4 2

s e t _ m a x _ b o x e s , 1 4 3

s e t _ m a x _ i n d e n t , 1 4 3

s e t _ n o r m a l i z e _ r a t i o , 1 9 5

s e t _ n o r m a l i z e _ r a t i o _ w h e n _ p r i n t i n g , 1 9 5

s e t _ n t h _ c h a r , 1 3 2

s e t _ p r i n t _ d e p t h , 5 7

s e t _ p r i n t _ l e n g t h , 5 7

s e t _ t a b , 1 4 4

s e t _ t e x t _ s i z e , 1 6 6

s e t g i d , 1 8 4

s e t u i d , 1 8 4

s h u t d o w n , 1 8 6

s h u t d o w n _ c o n n e c t i o n , 1 8 7

s i g n _ n u m , 1 9 3

s i g n a l , 1 8 2

s i n , 1 1 8

s i n h , 1 1 8

s i z e _ x , 1 6 5

s i z e _ y , 1 6 5

s l e e p , 1 8 3

s n d , 1 3 0

s o c k e t , 1 8 6

s o c k e t p a i r , 1 8 6

s o r t , 1 5 8

s o r t ( m o d u l e ) , 1 5 8

s o u n d , 1 6 9

s p l i t , 1 3 0 , 1 3 9 , 1 9 9

s p r i n t f , 1 5 5

s q r t , 1 1 8

s q u a r e _ n u m , 1 9 2

s t a c k ( m o d u l e ) , 1 5 9

s t a t , 1 4 8 , 1 7 7

s t d _ e r r , 1 2 2

s t d _ f o r m a t t e r , 1 4 5

s t d _ i n , 1 2 2

s t d _ o u t , 1 2 2

s t d e r r , 1 2 2 , 1 7 5

s t d i n , 1 2 2 , 1 7 5

s t d o u t , 1 2 2 , 1 7 5

s t r ( m o d u l e ) , 1 9 7

s t r e a m ( m o d u l e ) , 1 3 1

s t r e a m _ c h e c k , 1 3 1

s t r e a m _ f r o m , 1 3 1

s t r e a m _ g e t , 1 3 1

s t r e a m _ n e x t , 1 3 1

s t r e a m _ o f _ c h a n n e l , 1 3 1

s t r e a m _ o f _ s t r i n g , 1 3 1

s t r i n g ( m o d u l e ) , 1 3 2

s t r i n g _ a f t e r , 2 0 0

s t r i n g _ b e f o r e , 2 0 0

s t r i n g _ f o r _ r e a d , 1 3 3

s t r i n g _ l e n g t h , 1 3 2

s t r i n g _ m a t c h , 1 9 8

s t r i n g _ o f _ b o o l , 1 1 4

s t r i n g _ o f _ c h a r , 1 1 5

s t r i n g _ o f _ f l o a t , 1 1 9

s t r i n g _ o f _ i n e t _ a d d r , 1 8 5

s t r i n g _ o f _ i n t , 1 2 2

s t r i n g _ o f _ n u m , 1 9 4

s u b _ f l o a t , 1 1 7

s u b _ i n t , 1 2 0

s u b _ n u m , 1 9 2

s u b _ s t r i n g , 1 3 2

s u b _ v e c t , 1 3 4

s u b s t i t u t e _ f i r s t , 1 9 9

s u b t r a c t , 1 2 9

s u c c , 1 2 0

s u c c _ n u m , 1 9 3

s y m b o l _ e n d , 1 5 3

s y m b o l _ s t a r t , 1 5 3

s y m l i n k , 1 8 0

s y s ( m o d u l e ) , 1 5 9

S y s _ e r r o r ( e x c e p t i o n ) , 1 5 9

s y s _ p r i n t _ n u m , 1 9 4

s y s t e m , 1 7 5

s y s t e m _ c o m m a n d , 1 6 1

t a k e , 1 5 6

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 217/218

I n d e x t o t h e l i b r a r y 2 1 5

t a n , 1 1 8

t a n h , 1 1 8

t c d r a i n , 1 9 0

t c f l o w , 1 9 0

t c f l u s h , 1 9 0

t c g e t a t t r , 1 9 0

t c s e n d b r e a k , 1 9 0

t c s e t a t t r , 1 9 0

t e x t _ s i z e , 1 6 6

t i m e , 1 6 1 , 1 8 3

t i m e s , 1 8 3

t l , 1 2 8

t o p l e v e l ( m o d u l e ) , 5 5

t r a c e , 5 6

t r a n s p , 1 6 7

t r u n c a t e , 1 7 6

u m a s k , 1 7 8

u n i o n , 1 2 9 , 1 5 8

u n i x ( m o d u l e ) , 1 7 1

U n i x _ e r r o r ( e x c e p t i o n ) , 1 7 3

u n l i n k , 1 7 7

u n t r a c e , 5 7

u t i m e s , 1 8 3

v e c t ( m o d u l e ) , 1 3 4

v e c t _ a s s i g n , 1 3 4

v e c t _ i t e m , 1 3 4

v e c t _ l e n g t h , 1 3 4

v e c t _ o f _ l i s t , 1 3 5

v e r b o s e _ m o d e , 5 7

w a i t , 1 7 4

w a i t _ n e x t _ e v e n t , 1 6 8

w a i t o p t , 1 7 4

w a i t p i d , 1 7 4

w h i t e , 1 6 5

w r i t e , 1 7 6

y e l l o w , 1 6 5

8/7/2019 ocaml manual

http://slidepdf.com/reader/full/ocaml-manual 218/218

I n d e x o f k e y w o r d s

a n d , s e e l e t , t y p e , e x c e p t i o n , v a l u e , w h e r e

a s , 2 1

b e g i n , 2 2 , 2 3

w h e n , 3 6

w h e r e , 3 7

w h i l e , 2 6

w i t h , s e e m a t c h , t r y