7e018aa588067bfd01f1693f7e55e59c022c8385
[lambda.git] / family_tree_of_functional_programming_languages.mdwn
1 There's no need for you to know this for our seminar. But in case you're interested...
2
3 Others (and ourselves) will often talk about "functional programming
4 languages." But it would be more appropriate to talk of functional *paradigms*
5 or *programming patterns*. Most programming languages are hybrids that allow
6 programmers to use any of several programming paradigms. The ones that get
7 called "functional languages" are just ones that give functional paradigms a
8 central place in their design, and make them very easy to use.
9
10 We can divide functional languages into two classes: those that are dynamically
11 typed and those that are statically typed.
12
13 The **dynamically typed** languages give types more of a background role in the
14 program. They include the Lisp family (which in turn includes all the variants
15 of [[!wikipedia Scheme]], and also [[!wikipedia Common Lisp]], and [[!wikipedia
16 Clojure]]). They also include [[!wikipedia Erlang]] and [[!wikipedia Joy]] and
17 [[!wikipedia Pure]], and others.
18
19 Although these languages are hospitable to functional programming, some of them
20 also permit you to write *imperatival* code (that is, code with *side-effects*)
21 too. In Scheme, by convention, imperatival functions are named ending with a
22 "!". So `(set-car! p 1)` is a Scheme expression that, when evaluated, *mutates*
23 the pair p so that its first member changes to 1. For our purposes though,
24 we'll mostly be working with the parts of Scheme that are purely functional.
25 We'll be discussing the difference between functional and imperatival
26 programming a lot during the seminar.
27
28 We're using Scheme in parallel with our discussion of *untyped* lambda calculi.
29 Scheme isn't really untyped. If you assign a string to variable x and then try
30 to add x to 1, Scheme will realize that strings aren't the right type of value
31 to add to integers, and will complain about it. However, Scheme will complain
32 about it *at runtime*: that is, at the point when that particular instruction
33 is about the be executed. This is what's meant by calling these languages
34 "dynamically typed."
35
36 In practice, dynamically typed languages allow the programmer to be more
37 relaxed about the types of the values they're manipulating. For instance, it's
38 trivial to create a list whose first member is a string, whose second member is
39 an integer, and so on. You just have to keep track somehow so that you don't
40 try doing anything with values of the wrong type, else you'll get an error at
41 runtime.
42
43
44 The other large class of languages are **statically typed**. This means that
45 typing information is checked at *compile time*: that is, when you're
46 converting your source code into a file that your computer knows how to
47 directly execute. If you make type mistakes---for instance, you try to add a
48 string to an integer---the compiler will choke on this so you never get to the
49 point of even trying to run the program. Once you finally do get the program to
50 compile, you can be more confident that errors of that sort have all been
51 eliminated. They can't sneak up to bite you unawares while the program is
52 running.
53
54 Formerly, static typing required the programmer to add lots of annotations in
55 her source code explicitly specifying what the type of each function argument
56 is, what the type of the function's return value was, and so on. This is
57 tedious, and partly for this reason dynamically typed languages have become
58 popular and are thought of as easier to work with. However, nowadays statically
59 typed languages tend to use "type inference": that is, you can let the computer
60 do most of the work of figuring out what the types of everything are. For
61 example, if you define a function like this:
62
63         let foo x y = (1 + x, sqrt(y) )
64
65 the compiler will see that its first argument x is added to an integer, so it
66 also needs to be an integer; and (supposing sqrt is known independently to take
67 floating point arguments), foo's second argument y needs to be a floating point
68 value. What's more, foo returns a pair whose first member is of type int and
69 second member is of type float. The compiler can figure this out all on its
70 own. The programmer doesn't have to explicitly spell it out, though she could,
71 like follows:
72
73         let foo (x:int) (y:float) : int*float = (1 + x, sqrt(y) )
74
75 This just says explicitly that foo takes an argument x of type int, an argument
76 y of type float, and returns a pair of type int\*float (that is, a pair whose
77 first member is if type int and whose second member is of type float).
78
79 Type inference allows programmers to enjoy the benefits of strict compile-time
80 type-checking, which as we said, helps eliminate a large class of errors at a
81 more convenient time---when the program is being developed, rather than after
82 it's been deployed and is running. While making life easier for the programmer,
83 so that they don't have to explicitly write out the types of everything.
84 (However, if a programmer doesn't keep track of types at least in her head,
85 she's going to get into trouble and will face compile-time errors until she
86 gets everything sorted out.)
87
88 Though as we said dynamically-typed languages have become popular, programmers
89 who get used to modern statically-typed languages find them productive.
90 Sometimes they become zealots for working this way instead; at any case, they
91 say that the popular dim opinion of static typing is based on out-of-date
92 experiences of older languages like C and Java.
93
94 Most functional programming languages these days are statically typed.
95
96 We can further divide these languages based on whether they use *lazy* or
97 *strict/eager* evaluation. We'll discuss the difference between these during
98 the seminar.
99
100 Most programming languages, functional or not, use strict/eager evaluation. For
101 instance, languages of the ML family are all statically-typed functional
102 languages with strict/eager evaluation. These include [[!wikipedia SML]] and
103 [[!wikipedia Caml]] and [[!wikipedia Nemerle]]. Other statically-typed
104 functional languages with strict/eager semantics are [[!wikipedia Scala]] and
105 [[!wikipedia Coq]]. Like Scheme, many of these languages permit *imperatival*
106 as well as functional coding; but they are regarded as functional programming
107 languages because they are so hospitable to functional programming, and give it
108 a central place in their design.
109
110 A few languages such as [[!wikipedia Miranda]] and [[!wikipedia Haskell]] are
111 statically-typed languages that instead mostly use lazy evaluation. However,
112 it'd be more strictly accurate to say Haskell is lazy *by default*. You can
113 also make Haskell evaluate some expressions strictly/eagerly; you just have to
114 be ask for that explicitly. (I don't know about Miranda.) Languages like OCaml
115 are the reverse: they're strict/eager by default, but you can get lazy
116 evaluation where it's needed, you just have to ask for it explicitly.
117
118 Unlike OCaml, Haskell is much more extreme about being non-imperatival. Though
119 it's possible to write imperative code in Haskell, too; one just has to
120 encapsulate it in a ST or IO *monad*. We'll be discussing in the seminar how
121 monads can be used to create purely functional representations of imperatival
122 algorithms. (You can do the same in languages like Scheme and OCaml, too.
123 What's different is that in Haskell monads are the *only* way to deal with
124 imperatival code.)
125
126 We'll talk much more about monads, lazy vs strict evaluation, and functional vs
127 imperatival code as we proceed.
128
129