X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?p=lambda.git;a=blobdiff_plain;f=translating_between_OCaml_Scheme_and_Haskell.mdwn;h=ea4079e954dbefbbcc1c74dffd2caed93d6708f2;hp=aaefb3645d215b3f249dc4e5248d2d97d933cdb3;hb=047287e7095a4f7001b9b7154a08da7198e45499;hpb=1b7446b685897f2463c3ffe680f3321fdb8900be;ds=sidebyside
diff --git a/translating_between_OCaml_Scheme_and_Haskell.mdwn b/translating_between_OCaml_Scheme_and_Haskell.mdwn
index aaefb364..ea4079e9 100644
--- a/translating_between_OCaml_Scheme_and_Haskell.mdwn
+++ b/translating_between_OCaml_Scheme_and_Haskell.mdwn
@@ -1,8 +1,8 @@
-##Translating between OCaml, SML, and Haskell##
+[[!toc]]
The functional programming literature tends to use one of four languages: Scheme, OCaml, Standard ML (SML), or Haskell. With experience, you'll grow comfortable switching between these. At the beginning, though, it can be confusing.
-The easiest translations are between OCaml and SML. These languages are both derived from a common ancestor, ML. For the most part, the differences between them are only superficial. [Here's a translatio nmanual](http://www.mpi-sws.org/~rossberg/sml-vs-ocaml.html).
+The easiest translations are between OCaml and SML. These languages are both derived from a common ancestor, ML. For the most part, the differences between them are only superficial. [Here's a translation manual](http://www.mpi-sws.org/~rossberg/sml-vs-ocaml.html).
In some respects these languages are closer to Scheme than to Haskell: Scheme, OCaml and SML all default to call-by-value evaluation order, and all three have native syntax for mutation and other imperative idioms (though that's not central to their design). Haskell is different in both respects: the default evaluation order is call-by-name (strictly speaking, it's "call-by-need", which is a more efficient cousin), and the only way to have mutation or the like is through the use of monads.
@@ -10,7 +10,7 @@ On both sides, however, the non-default evaluation order can also be had by usin
Additionally, the syntax of OCaml and SML is superficially much closer to Haskell's than to Scheme's.
-##Comments, Whitespace, and Brackets##
+#Comments, Whitespace, and Brackets#
-- this is a single line comment in Haskell
@@ -36,7 +36,7 @@ Additionally, the syntax of OCaml and SML is superficially much closer to Haskel
* In Haskell, a block of code can be bracketed with `{` and `}`, with different expressions separated by `;`. But usually one would use line-breaks and proper indentation instead. In OCaml, separating expressions with `;` has a different meaning, having to do with how side-effects are sequenced. Instead, one can bracket a block of code with `(` and `)` or with `begin` and `end`. In Scheme, of course, every parentheses is significant.
-##Scheme and OCaml##
+#Scheme and OCaml#
* You can [try Scheme in your web browser](http://tryscheme.sourceforge.net/). This is useful if you don't have Racket or another Scheme implementation installed---but don't expect it to have all the bells and whistles of a mature implementation!
@@ -129,7 +129,7 @@ Additionally, the syntax of OCaml and SML is superficially much closer to Haskel
We won't say any more about translating to and from Scheme.
-##Haskell and OCaml##
+#Haskell and OCaml#
We will however try to give some general advice about how to translate between OCaml and Haskell.
@@ -141,12 +141,11 @@ We will however try to give some general advice about how to translate between O
* [Learn You a Haskell for Great Good](http://learnyouahaskell.com/)
-#Type expressions#
+##Type expressions##
* In Haskell, you say a value has a certain type with: `value :: type`. You express the operation of prepending a new `int` to a list of `int`s with `1 : other_numbers`. In OCaml it's the reverse: you say `value : type` and `1 :: other_numbers`.
-* In Haskell, type names and constructors both begin with capital letters, and type variables always appear after their constructors, in Curried form. And the primary term for declaring a new type is `data` (short for "abstract datatype").
-So we have:
+* In Haskell, type names and constructors both begin with capital letters, and type variables always appear after their constructors, in Curried form. And the primary term for declaring a new type is `data` (short for [[!wikipedia algebraic data type]]). So we have:
data Either a b = Left a | Right b;
data FooType a b = Foo_constructor1 a b | Foo_constructor2 a b;
@@ -207,10 +206,10 @@ So we have:
class Eq a where
(==) :: a -> a -> Bool
- This declares the type-class `Eq`; in order to belong to this class, a type `a` will have to supply its own implementation of the function ==, with the type a -> a -> Bool. Here is how the `Integer` class signs up to join the type-class:
+ This declares the type-class `Eq`; in order to belong to this class, a type `a` will have to supply its own implementation of the function `==`, with the type `a -> a -> Bool`. Here is how the `Integer` class signs up to join this type-class:
instance Eq Integer where
- x == y = ...
+ x == y = ... some definition for the Integer-specific version of that function here ...
Type expressions can be conditional on some of their parameters belonging to certain type-classes. For example:
@@ -227,7 +226,7 @@ So we have:
says that if `a` belongs to the typeclass `Eq`, then so too does `Tree a`, and in such cases here is the implementation of `==` for `Tree a`...
-* OCaml doesn't have type-classes. You can do soemthing similar with OCaml modules that take are parameterized on other modules. Again, [see here](/code/tree_monadize.ml) for an example.
+* OCaml doesn't have type-classes. You can do something similar with OCaml modules that take are parameterized on other modules. Again, [see here](/code/tree_monadize.ml) for an example.
* Some specific differences in how certain types are expressed. This block in Haskell:
@@ -239,7 +238,7 @@ So we have:
Prelude> let x = () :: ()
Prelude> let x = (1, True) :: (Int, Bool)
-corresponds to this block in OCaml:
+ corresponds to this block in OCaml:
# type 'a option = None | Some of 'a;;
type 'a option = None | Some of 'a
@@ -278,7 +277,7 @@ corresponds to this block in OCaml:
* We'll discuss differences between Haskell's and OCaml's record types below.
-#Lists, Tuples, Unit, Booleans#
+##Lists, Tuples, Unit, Booleans##
* As noted above, Haskell describes the type of a list of `Int`s as `[Int]`. OCaml describes it as `int list`. Haskell describes the type of a pair of `Int`s as `(Int, Int)`. OCaml describes it as `int * int`. Finally, Haskell uses `()` to express both the unit type and a value of that type. In OCaml, one uses `()` for the value and `unit` for the type.
@@ -291,7 +290,7 @@ corresponds to this block in OCaml:
In OCaml, there is no predefined `null` or `isempty` function. One can still test whether a list is empty using the comparison `lst = []`.
-* In Haskell, the expression [1..5] is the same as [1,2,3,4,5], and the expression [0..] is a infinite lazily-evaluated stream of the natural numbers. In OCaml, there is no [1..5] shortcut, lists must be finite, and they are eagerly evaluated. It is possible to create lazy streams in OCaml, even infinite ones, but you have to use other techniques than the native list type.
+* In Haskell, the expression `[1..5]` is the same as `[1,2,3,4,5]`, and the expression `[0..]` is a infinite lazily-evaluated stream of the natural numbers. In OCaml, there is no `[1..5]` shortcut, lists must be finite, and they are eagerly evaluated. It is possible to create lazy streams in OCaml, even infinite ones, but you have to use other techniques than the native list type.
* Haskell has *list comprehensions*:
@@ -301,17 +300,20 @@ corresponds to this block in OCaml:
List.map (fun x -> x * x) (List.filter odd [1..10]);;
-* In Haskell, the expressions "abc" and ['a','b','c'] are equivalent. (Strings are just lists of chars. In OCaml, these expressions have two different types.
+* In Haskell, the expressions `"abc"` and `['a','b','c']` are equivalent. (Strings are just lists of `char`s.) In OCaml, these expressions have two different types.
Haskell uses the operator `++` for appending both strings and lists (since Haskell strings are just one kind of list). OCaml uses different operators:
- "string1" ^ "string2"
- ['s';'t'] @ ['r';'i';'n';'g']
- (* or equivalently *)
- List.append ['s';'t'] ['r';'i';'n';'g']
+ # "string1" ^ "string2";;
+ - : string = "string1string2"
+ # ['s';'t'] @ ['r';'i';'n';'g'];;
+ - : char list = ['s'; 't'; 'r'; 'i'; 'n'; 'g']
+ # (* or equivalently *)
+ List.append ['s';'t'] ['r';'i';'n';'g'];;
+ - : char list = ['s'; 't'; 'r'; 'i'; 'n'; 'g']
-#Let and Where#
+##Let and Where##
* Haskell permits both:
@@ -333,7 +335,7 @@ corresponds to this block in OCaml:
in let result2 = x + 1
in result1 + result2;;
-#Patterns#
+##Patterns##
* In OCaml:
@@ -364,34 +366,34 @@ corresponds to this block in OCaml:
[] -> result4
-#Records#
+##Records##
Haskell and OCaml both have `records`, which are essentially just tuples with a pretty interface. The syntax for declaring and using these is a little bit different in the two languages.
* In Haskell one says:
-- declare a record type
- data Color = C { red, green, blue :: Int }
+ data Color = Col { red, green, blue :: Int }
-- create a value of that type
- let c = C { red = 0, green = 127, blue = 255 }
+ let c = Col { red = 0, green = 127, blue = 255 }
In OCaml one says instead:
- type color = { red : int; green : int; blue : int};;
+ type color = { red : int; green : int; blue : int };;
let c = { red = 0; green = 127; blue = 255 }
- Notice that OCaml doesn't use any value constructor `C`. The record syntax `{ red = ...; green = ...; blue = ... }` is by itself the constructor. The record labels `red`, `green`, and `blue` cannot be re-used for any other record type.
+ Notice that OCaml doesn't use any value constructor `Col`. The record syntax `{ red = ...; green = ...; blue = ... }` is by itself the constructor. The record labels `red`, `green`, and `blue` cannot be re-used for any other record type.
* In Haskell, one may have multiple constructors for a single record type, and one may re-use record labels within that type, so long as the labels go with fields of the same type:
data FooType = Constructor1 {f :: Int, g :: Float} | Constructor2 {f :: Int, h :: Bool}
-* In Haskell, one can extract the field of a record like this:
+* In Haskell, one can extract a single field of a record like this:
- let c = C { red = 0, green = 127, blue = 255 }
+ let c = Col { red = 0, green = 127, blue = 255 }
in red c -- evaluates to 0
- In OCaml:
+ In OCaml one says:
let c = { red = 0; green = 127; blue = 255 }
in c.red (* evaluates to 0 *)
@@ -399,7 +401,7 @@ Haskell and OCaml both have `records`, which are essentially just tuples with a
* In both languages, there is a special syntax for creating a copy of an existing record, with some specified fields altered. In Haskell:
let c2 = c { green = 50, blue = 50 }
- -- evaluates to C { red = red c, green = 50, blue = 50 }
+ -- evaluates to Col { red = red c, green = 50, blue = 50 }
In OCaml:
@@ -408,7 +410,7 @@ Haskell and OCaml both have `records`, which are essentially just tuples with a
* One pattern matches on records in similar ways. In Haskell:
- let C { red = r, green = g } = c
+ let Col { red = r, green = g } = c
in r
In OCaml:
@@ -418,11 +420,11 @@ Haskell and OCaml both have `records`, which are essentially just tuples with a
In Haskell:
- makegray c@(C { red = r} ) = c { green = r, blue = r }
+ makegray c@(Col { red = r } ) = c { green = r, blue = r }
is equivalent to:
- makegray c = let C { red = r } = c
+ makegray c = let Col { red = r } = c
in { red = r, green = r, blue = r }
In OCaml it's:
@@ -433,7 +435,7 @@ Haskell and OCaml both have `records`, which are essentially just tuples with a
- : color = {red = 0; green = 0; blue = 0}
-#Functions#
+##Functions##
* In Haskell functions are assumed to be recursive, and their types and applications to values matching different patterns are each declared on different lines. So we have:
@@ -488,7 +490,7 @@ Haskell and OCaml both have `records`, which are essentially just tuples with a
| x' when x' = 0 -> 0
| _ -> -1
-* In Haskell the equality comparison operator is `==`, and the non-equality operator is `/=`. In OCaml, `==` expresses "physical identity", which has no analogue in Haskell because Haskell has no mutable types. See our discussion of "Four grades of mutable involvement" in the [[Week9]] notes. In OCaml the operator corresponding to Haskell's `==` is just `=`, and the corresponding non-equality operator is `<>`.
+* In Haskell the equality comparison operator is `==`, and the non-equality operator is `/=`. In OCaml, `==` expresses "physical identity", which has no analogue in Haskell because Haskell has no mutable types. See our discussion of "Four grades of mutation involvement" in the [[Week9]] notes. In OCaml the operator corresponding to Haskell's `==` is just `=`, and the corresponding non-equality operator is `<>`.
* In both Haskell and OCaml, one can use many infix operators as prefix functions by parenthesizing them. So for instance:
@@ -496,10 +498,10 @@ Haskell and OCaml both have `records`, which are essentially just tuples with a
will work in both languages. One notable exception is that in OCaml you can't do this with the list constructor `::`:
- # (::) 1 [1;2];;
- Error: Syntax error
- # (fun x xs -> x :: xs) 1 [1; 2];;
- - : int list = [1; 1; 2]
+ # (::) 1 [1;2];;
+ Error: Syntax error
+ # (fun x xs -> x :: xs) 1 [1; 2];;
+ - : int list = [1; 1; 2]
* Haskell also permits two further shortcuts here that OCaml has no analogue for. In Haskell, in addition to writing:
@@ -507,15 +509,15 @@ Haskell and OCaml both have `records`, which are essentially just tuples with a
you can also write either of:
- (1 >) 2
- (> 2) 1
+ (2 >) 1
+ (> 1) 2
In OCaml one has to write these out longhand:
- (fun y -> 1 > y) 2;;
- (fun x -> x > 2) 1;;
+ (fun y -> 2 > y) 1;;
+ (fun x -> x > 1) 2;;
- Also, in Haskell, there's a special syntax for using what are ordinarily prefix functions into infix operators:
+ Also, in Haskell, there's a special syntax for using what are ordinarily prefix functions as infix operators:
Prelude> elem 1 [1, 2]
True
@@ -567,14 +569,14 @@ Haskell and OCaml both have `records`, which are essentially just tuples with a
* Some functions are predefined in Haskell but not in OCaml. Here are OCaml definitions for some common ones:
- let id x = x;;
- let const x _ = x;;
- let flip f x y = f y x;;
- let curry (f : ('a, 'b) -> 'c) = fun x y -> f (x, y);;
- let uncurry (f : 'a -> 'b -> 'c) = fun (x, y) -> f x y;;
- let null lst = lst = [];;
+ let id x = x;;
+ let const x _ = x;;
+ let flip f x y = f y x;;
+ let curry (f : ('a, 'b) -> 'c) = fun x y -> f (x, y);;
+ let uncurry (f : 'a -> 'b -> 'c) = fun (x, y) -> f x y;;
+ let null lst = lst = [];;
- `fst` and `snd` (defined only on pairs) are provided in both languages. Haskell has `head` and `tail` for lists; these will raise an exception if applied to []. In OCaml the corresponding functions are `List.hd` and `List.tl`. Many other Haskell list functions like `length` are available in OCaml as `List.length`, but OCaml's standard libraries are leaner that Haskell's.
+ `fst` and `snd` (defined only on pairs) are provided in both languages. Haskell has `head` and `tail` for lists; these will raise an exception if applied to `[]`. In OCaml the corresponding functions are `List.hd` and `List.tl`. Many other Haskell list functions like `length` are available in OCaml as `List.length`, but OCaml's standard libraries are leaner that Haskell's.
* The `until` function in Haskell is used like this:
@@ -586,11 +588,11 @@ Haskell and OCaml both have `records`, which are essentially just tuples with a
This can be defined in OCaml as:
- let rec until test f z =
- if test z then z else until test f (f z)
+ let rec until test f z =
+ if test z then z else until test f (f z)
-#Lazy or Eager#
+##Lazy or Eager##
* As we've mentioned several times, Haskell's evaluation is by default *lazy* or "call-by-need" (that's an efficient version of "call-by-name" that avoids computing the same results again and again). In some places Haskell will force evaluation to be *eager* or "strict". This is done in several different ways; the symbols `!` and `seq` are signs that it's being used.
@@ -614,14 +616,14 @@ Haskell and OCaml both have `records`, which are essentially just tuples with a
# eval_later3;;
- : int lazy_t = lazy 1
- Notice in the last line the value is reported as being `lazy 1` instead of ``. Since the value has once been forced, it won't ever need to be recomputed. The thunks are less efficient in this respect. Even though OCaml will now remember that `eval_later3` should be forced to, `eval_later3` is still type distinct from a plain `int`.
+ Notice in the last line the value is reported as being `lazy 1` instead of ``. Since the value has once been forced, it won't ever need to be recomputed. The thunks are less efficient in this respect. Even though OCaml will now remember what `eval_later3` should be forced to, `eval_later3` is still type-distinct from a plain `int`.
-#Monads#
+##Monads##
Haskell has more built-in support for monads, but one can define the monads one needs in OCaml.
-* In our seminar, we've been calling one monadic operation `unit`, in Haskell the same operation is called `return`. We've been calling another monadic operation `bind`, used in prefix form, like this:
+* In our seminar, we've been calling one monadic operation `unit`; in Haskell the same operation is called `return`. We've been calling another monadic operation `bind`, used in prefix form, like this:
bind u f
@@ -629,13 +631,13 @@ Haskell has more built-in support for monads, but one can define the monads one
u >>= f
- If you like this Haskell convention, you can define (>>=) in OCaml like this:
+ If you like this Haskell convention, you can define `>>=` in OCaml like this:
let (>>=) = bind;;
* Haskell also uses the operator `>>`, where `u >> v` means the same as `u >>= \_ -> v`.
-* In Haskell, one can generally just use plain `return` and `>>=` and the compiler will infer what monad you must be talking about from the surrounding type constraints. In OCaml, you generally need to be specific about which monad you're using. So in these notes, when mutiple monads are on the table, we've defined operations as `reader_unit` and `reader_bind`.
+* In Haskell, one can generally just use plain `return` and `>>=` and the interpreter will infer what monad you must be talking about from the surrounding type constraints. In OCaml, you generally need to be specific about which monad you're using. So in these notes, when mutiple monads are on the table, we've defined operations as `reader_unit` and `reader_bind`, and so on.
* Haskell has a special syntax for working conveniently with monads. It looks like this. Assume `u` `v` and `w` are values of some monadic type `M a`. Then `x` `y` and `z` will be variables of type `a`:
@@ -652,7 +654,7 @@ Haskell has more built-in support for monads, but one can define the monads one
v >>= \ y ->
w >>= \ _ ->
let z = foo x y
- in unit z
+ in return z
which can be translated straightforwardly into OCaml.