1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<!-- $Id: scheme.html,v 6.3 2012-01-09 14:22:20 deraugla Exp $ -->
<!-- Copyright (c) INRIA 2007-2012 -->
<title>Scheme</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<link rel="stylesheet" type="text/css" href="styles/base.css"
title="Normal" />
</head>
<body>
<div id="menu">
</div>
<div id="content">
<h1 class="top">Scheme and Lisp syntaxes</h1>
<p>It is possible to write OCaml programs with Scheme or Lisp
syntax. They are close to one another, both using parentheses
to identify and separate statements.</p>
<div id="tableofcontents">
</div>
<h2>Common</h2>
<p>The syntax extension kits are named "<tt>pa_scheme.cmo</tt>" and
"<tt>pa_lisp.cmo</tt>". The sources (same names ending with ".ml"
in the Camlp5 sources), are written in their own syntax. They are
boostrapped thanks to the versions being written in revised syntax and to
the Camlp5 pretty printing system.</p>
<p>In the OCaml toplevel, it is possible to use them by loading
"<tt>camlp5r.cma</tt>" first, then "<tt>pa_lisp.cmo</tt>" or
"<tt>pa_scheme.cmo</tt>" after:</p>
<pre>
ocaml -I +camlp5 camlp5r.cma pa_scheme.cmo
Objective Caml version ...
Camlp5 Parsing version ...
# (let ((x 3)) (* 3 x))
- : int = 9
# (values 3 4 5)
- : (int * int * int) = (3, 4, 5)
ocaml -I +camlp5 camlp5r.cma pa_lisp.cmo
Objective Caml version ...
Camlp5 Parsing version ...
# (let ((x 3)) (* 3 x))
- : int = 9
# (, 3 4 5)
- : (int * int * int) = (3, 4, 5)
</pre>
<p>The grammar of Scheme and Lisp are relatively simple, just reading
s-expressions. The syntax tree nodes are created in the semantic
actions. Because of this, these grammars are hardly extensible.</p>
<p>However, the syntax extension EXTEND ("<tt>pa_extend.cmo</tt>" in
<a href="grammars.html">extensible grammars</a>) works for them:
only the semantic actions need be written with the Scheme or Lisp
syntax. Stream parsers are also implemented.</p>
<p>Warning: these syntaxes are incomplete, but can be completed, if
Camlp5 users are insterested.</p>
<h2>Scheme syntax</h2>
<p>Some examples are given to show the principles:</p>
<table width="75%" class="centered" cellspacing="0" cellpadding="0">
<tr>
<th align="left" class="half">OCaml</th>
<th align="left" class="half">Scheme</th>
</tr>
<tr>
<td><tt>let x = 42;;</tt></td>
<td><tt>(define x 42)</tt></td>
</tr>
<tr>
<td><tt>let f x = 0;;</tt></td>
<td><tt>(define (f x) 0)</tt></td>
</tr>
<tr>
<td><tt>let rec f x y = 0;;</tt></td>
<td><tt>(definerec (f x y) 0)</tt></td>
</tr>
<tr>
<td><tt>let x = 42 and y = 27 in x + y;; </tt></td>
<td><tt>(let ((x 42) (y 27)) (+ x y))</tt></td>
</tr>
<tr>
<td><tt>let x = 42 in let y = 27 in x + y;; </tt></td>
<td><tt>(let* ((x 42) (y 27)) (+ x y))</tt></td>
</tr>
<tr>
<td><tt>module M = struct ... end;; </tt></td>
<td><tt>(module M (struct ...))</tt></td>
</tr>
<tr>
<td><tt>type 'a t = A of 'a * int | B</tt></td>
<td><tt>(type (t 'a) (sum (A 'a int) (B)))</tt></td>
</tr>
<tr>
<td><tt>fun x y -> x</tt></td>
<td><tt>(lambda (x y) x)</tt></td>
</tr>
<tr>
<td><tt>x; y; z</tt></td>
<td><tt>(begin x y z)</tt></td>
</tr>
<tr>
<td><tt>f x y</tt></td>
<td><tt>(f x y)</tt></td>
</tr>
<tr>
<td><tt>[1; 2; 3]</tt></td>
<td><tt>[1 2 3]</tt></td>
</tr>
<tr>
<td><tt>x :: y :: z :: t</tt></td>
<td><tt>[x y z :: t]</tt></td>
</tr>
<tr>
<td><tt>a, b, c</tt></td>
<td><tt>(values a b c)</tt></td>
</tr>
<tr>
<td><tt>match x with 'A'..'Z' -> "x"</tt></td>
<td><tt>(match x ((range 'A' 'Z') "x")))</tt></td>
</tr>
<tr>
<td><tt>{x = y; z = t}</tt></td>
<td><tt>{(x y) (z t)}</tt></td>
</tr>
</table>
<h2>Lisp syntax</h2>
<p>The same examples:</p>
<table width="75%" class="centered" cellspacing="0" cellpadding="0">
<tr>
<th align="left" class="half">OCaml</th>
<th align="left" class="half">Lisp</th>
</tr>
<tr>
<td><tt>let x = 42;;</tt></td>
<td><tt>(value x 42)</tt></td>
</tr>
<tr>
<td><tt>let f x = 0;;</tt></td>
<td><tt>(value f (lambda x 0))</tt></td>
</tr>
<tr>
<td><tt>let rec f x y = 0;;</tt></td>
<td><tt>(value rec f (lambda (x y) 0))</tt></td>
</tr>
<tr>
<td><tt>let x = 42 and y = 27 in x + y;; </tt></td>
<td><tt>(let ((x 42) (y 27)) (+ x y))</tt></td>
</tr>
<tr>
<td><tt>let x = 42 in let y = 27 in x + y;; </tt></td>
<td><tt>(let* ((x 42) (y 27)) (+ x y))</tt></td>
</tr>
<tr>
<td><tt>module M = struct ... end;; </tt></td>
<td><tt>(module M (struct ...))</tt></td>
</tr>
<tr>
<td><tt>type 'a t = A of 'a * int | B</tt></td>
<td><tt>(type (t 'a) (sum (A 'a int) (B)))</tt></td>
</tr>
<tr>
<td><tt>fun x y -> x</tt></td>
<td><tt>(lambda (x y) x)</tt></td>
</tr>
<tr>
<td><tt>x; y; z</tt></td>
<td><tt>(progn x y z)</tt></td>
</tr>
<tr>
<td><tt>f x y</tt></td>
<td><tt>(f x y)</tt></td>
</tr>
<tr>
<td><tt>[1; 2; 3]</tt></td>
<td><tt>(list 1 2 3)</tt></td>
</tr>
<tr>
<td><tt>x :: y :: z :: t</tt></td>
<td><tt>(list x y z :: t)</tt></td>
</tr>
<tr>
<td><tt>a, b, c</tt></td>
<td><tt>(, a b c)</tt></td>
</tr>
<tr>
<td><tt>match x with 'A'..'Z' -> "x"</tt></td>
<td><tt>(match x ((range 'A' 'Z') "x")))</tt></td>
</tr>
<tr>
<td><tt>{x = y; z = t}</tt></td>
<td><tt>({} (x y) (z t))</tt></td>
</tr>
</table>
<div class="trailer">
</div>
</div>
</body>
</html>
|