25.10.2015 Views

Write You a Haskell Stephen Diehl

1kEcQTb

1kEcQTb

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Types<br />

e word “type” is quite often overload in the common programming lexicon. Other languages often<br />

refer to runtime tags present in the dynamics of the languages as “types”. Some examples:<br />

# Python<br />

>>> type(1)<br />

<br />

# Javascript<br />

> typeof(1)<br />

’number’<br />

# Ruby<br />

irb(main):001:0> 1.class<br />

=> Fixnum<br />

# Julia<br />

julia> typeof(1)<br />

Int64<br />

# Clojure<br />

user=> (type 1)<br />

java.lang.Long<br />

While this is a perfectly acceptable alternative definition, we are not going to go that route and instead<br />

restrict ourselves purely to the discussion of static types, in other words types which are known before<br />

runtime. Under this set of definitions many so-called dynamically typed languages often only have a<br />

single static type. For instance in Python all static types are subsumed by the PyObject and it is only<br />

at runtime that the tag PyTypeObject *ob_type is discriminated on to give rise to the Python notion<br />

of “types”. Again, this is not the kind of type we will discuss. e trade-offs that these languages make<br />

is that they often have trivial static semantics while the dynamics for the language are often exceedingly<br />

complicated. Languages like <strong>Haskell</strong> and OCaml are the opposite point in this design space.<br />

Types will usually be written as τ and can consist of many different constructions to the point where the<br />

type language may become as rich as the value level language. For now let’s only consider three simple<br />

types, two ground types (Nat and Bool) and an arrow type.<br />

τ ::= Bool<br />

Nat<br />

τ → τ<br />

e arrow type will be the type of function expressions, the left argument being the input type and the<br />

output type on the right. e arrow type will by convention associate to the right.<br />

τ 1 → τ 2 → τ 3 → τ 4 = τ 1 → (τ 2 → (τ 3 → τ 4 ))<br />

56

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!