Professional Documents
Culture Documents
Purescript
Purescript
PureScript
Christian Stuart, Douwe van Gijn, Martijn Fleuren and
Nick Begg
[Faculty of Science
Information and Computing
Sciences]
1
Presentation overview
[Faculty of Science
Information and Computing
Sciences]
2
Part 1 -
[Faculty of Science
Information and Computing
Sciences]
3
Where Purescript fits in the world
▶ What is it?
[Faculty of Science
Information and Computing
Sciences]
4
Where would you use it?
[Faculty of Science
Information and Computing
Sciences]
5
The language and Implementation
[Faculty of Science
Information and Computing
Sciences]
6
Compiling
[Faculty of Science
Information and Computing
Sciences]
7
▶ Where it fits in the world
▶ A quick HOWTO
[Faculty of Science
Information and Computing
Sciences]
8
Standard Tools - pulp
[Faculty of Science
Information and Computing
Sciences]
9
Standard Tools - pulp - continued
[Faculty of Science
Information and Computing
Sciences]
10
Standard Tools - psc, bower
[Faculty of Science
Information and Computing
Sciences]
11
Modules
[Faculty of Science
Information and Computing
Sciences]
12
Hello, world!
Firstly, create a project environment with pulp -
$ pulp init
[verbose output removed]
import Prelude
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, log)
$ pulp run
* Building project in/Users/nick/ps-test3
Compiling Data.Show
Compiling Data.Boolean
Compiling Control.Semigroupoid
[output trimmed]
* Build successful.
Hello sailor!
[Faculty of Science
Information and Computing
Sciences]
14
Hello, world! - compiled as JavaScript
output/Main/index.js:
[Faculty of Science
Information and Computing
Sciences]
15
Hello, world! - Running in a browser
$ pulp server
* Server listening on http://localhost:1337/
* Building project in /Users/nick/ps-test2
* Build successful.
* Bundling JavaScript...
* Bundled.
[Faculty of Science
Information and Computing
Sciences]
16
Hello, world! - Running in a browser
[Faculty of Science
Information and Computing
Sciences]
18
Hello, world! - Running in a browser
Figure 2: There it is
[Faculty of Science
Information and Computing
Sciences]
19
Interactive console mode
$ pulp psci
PSCi, version 0.10.3
Type :? for help
> 7+5
12
[Faculty of Science
Information and Computing
Sciences]
20
▶ Where it fits in the world
▶ A quick HOWTO
▶ Language introduction
[Faculty of Science
Information and Computing
Sciences]
21
Language Overview - Basic Types
[Faculty of Science
Information and Computing
Sciences]
22
Basic Types - Numbers
> :type 1
Int
> :type 1.7
Number
[Faculty of Science
Information and Computing
Sciences]
23
Basic Types - Boolean
[Faculty of Science
Information and Computing
Sciences]
24
Basic Types - Strings
[Faculty of Science
Information and Computing
Sciences]
25
Basic Types - Strictness
[Faculty of Science
Information and Computing
Sciences]
26
Basic Types - Strictness
▶ Predictably -
> 7 :: Boolean
Error found:
in module $PSCI
at line 1, column 1 - line 1, column 3
[Faculty of Science
Information and Computing
Sciences]
28
Language Overview - Arrays
> [1,1,2,3,5,8]
[1,1,2,3,5,8]
[Faculty of Science
Information and Computing
Sciences]
29
Language Overview - Arrays
Int
with type
main = do
logShow (fibonacci 7)
logShow (fibonacci 15)
[Faculty of Science
Information and Computing
Sciences]
31
Function - compiled
$ pulp run
* Building project in/Users/nick/ps/interactive
Compiling Main
* Build successful.
13
610
[Faculty of Science
Information and Computing
Sciences]
33
Features
[Faculty of Science
Information and Computing
Sciences]
34
Some examples
▶ It has no runtime
▶ Type security using javascript features
▶ Type classes
▶ Extensible records
▶ Human readable and debuggable output
▶ Many more such as: ADT’s, pattern matching, type
inference, rank-N types, etc.
[Faculty of Science
Information and Computing
Sciences]
35
Type security
value type
"" string
[] object
{} object
null object
undefined undefined
1 number
true boolean
(function(){}) function
> [] + {}
'[object Object]'
> {} + []
0
[Faculty of Science
Information and Computing
Sciences]
37
Type security
> [] + {}
'[object Object]'
> {} + []
0
WAT
[Faculty of Science
Information and Computing
Sciences]
37
Type security
x :: Int
x = 5
will be
var x = 5 | 0;
[Faculty of Science
Information and Computing
Sciences]
38
Type classes
[Faculty of Science
Information and Computing
Sciences]
39
Type classes
[Faculty of Science
Information and Computing
Sciences]
40
Extensible records
[Faculty of Science
Information and Computing
Sciences]
41
Extensible records
So we can do this
origin :: Vector
origin = {x: 0, y: 0 } -- JSON syntax setters
or this
originX :: Number
originX = origin.x -- Object field getters
[Faculty of Science
Information and Computing
Sciences]
42
Extensible records
-- the same as
setX val (Vector {x: x, y: y}) = Vector {x: val, y: y}
-- if Vector would have a constructor
[Faculty of Science
Information and Computing
Sciences]
43
Differences with Haskell
[Faculty of Science
Information and Computing
Sciences]
44
Differences with Haskell
▶ Explicit forall
▶ No syntactic sugar for lists
▶ Type class instances are named
[Faculty of Science
Information and Computing
Sciences]
44
What does that compile to?
▶ Currying
▶ Algebraic data types
▶ Type class instance
[Faculty of Science
Information and Computing
Sciences]
45
Currying
Remember the input: add x y = x + y
[Faculty of Science
Information and Computing
Sciences]
46
Algebraic data types
Let’s look at ADT’s. First we look at the Nothing constructor,
remember the input:
};
Nothing.value = new Nothing();
return Nothing;
})();
[Faculty of Science
Information and Computing
Sciences]
47
Algebraic data types
Let’s look at ADT’s. First we look at the Nothing constructor,
remember the input:
};
Nothing.value = new Nothing();
return Nothing;
})();
[Faculty of Science
Information and Computing
Sciences]
48
Algebraic data types
input data Maybe a = Nothing | Just a
Let’s see if we can dissect this function with the knowledge
from the previous slide.
psci> Vector 2 3
[2; 3]
[Faculty of Science
Information and Computing
Sciences]
50
Type class instance
[Faculty of Science
Information and Computing
Sciences]
51
The Foreign Function Interface
[Faculty of Science
Information and Computing
Sciences]
52
Calling PureScript from JavaScript
Compiles to:
Result:
> mul(3)(2);
6 [Faculty of Science
Information and Computing
Sciences]
53
Calling JavaScript from PureScript
To use foreign functions in a PureScript module we need to:
[Faculty of Science
Information and Computing
Sciences]
54
Example: Math bindings
[Faculty of Science
Information and Computing
Sciences]
55
Math.purs
The PureScript module file contains the type declaration:
[Faculty of Science
Information and Computing
Sciences]
56
Math.js
The JavaScript file belongs to the PureScript module, and
contains the function definition:
[Faculty of Science
Information and Computing
Sciences]
57
Calling the function:
[Faculty of Science
Information and Computing
Sciences]
58
Type safety
Lets change our function:
[Faculty of Science
Information and Computing
Sciences]
59
Running the function:
[Faculty of Science
Information and Computing
Sciences]
60
Multiple arguments
[Faculty of Science
Information and Computing
Sciences]
61
Javascript:
> pow(3)
NaN
[Faculty of Science
Information and Computing
Sciences]
62
Type Safety
JavaScript functions are not type safe
PureScript provides the Foreign module
[Faculty of Science
Information and Computing
Sciences]
63
The module gives us:
[Faculty of Science
Information and Computing
Sciences]
64
type F a = Except (NonEmptyList ForeignError) a
[Faculty of Science
Information and Computing
Sciences]
65
Provided by PureScript:
[Faculty of Science
Information and Computing
Sciences]
66
> runExcept $ (readInt <<< halve) 4
(Right 2)
[Faculty of Science
Information and Computing
Sciences]
67
Currying and uncurrying
PureScript offers:
[Faculty of Science
Information and Computing
Sciences]
68
import Data.Function.Uncurried (Fn2, runFn2)
[Faculty of Science
Information and Computing
Sciences]
69
import Data.Function.Uncurried (Fn2, mkFn2)
[Faculty of Science
Information and Computing
Sciences]
70
What about side effects?
Functions with side effects need to be declared as Eff
monads, with an appropriate effect type.
For example, Console.log is declared as:
[Faculty of Science
Information and Computing
Sciences]
71
part 4: Handling side effects
[Faculty of Science
Information and Computing
Sciences]
72
Eff monad
[Faculty of Science
Information and Computing
Sciences]
73
side effects in Haskell
main :: IO ()
[Faculty of Science
Information and Computing
Sciences]
74
side effects in PureScript
[Faculty of Science
Information and Computing
Sciences]
75
granularity makes sense
JavaScript was designed for interactivity in the browser.
Typical PureScript program has more I/O than typical Haskell
program.
[Faculty of Science
Information and Computing
Sciences]
76
there are a lot of effects
Examples of general purpose effects:
▶ Console IO
▶ Random number generation
▶ Exceptions
▶ Reading/writing mutable state
▶ …
▶ DOM manipulation
▶ AJAX calls
▶ talking to a websocket
▶ reading/writing in local storage
▶ …
[Faculty of Science
Information and Computing
Sciences]
77
example: printing a random number
[Faculty of Science
Information and Computing
Sciences]
79
var main =
Prelude[">>="]
(Control_Monad_Eff.monadEff())
(Control_Monad_Eff_Random.random)
(function (n) {
return Control_Monad_Eff_Console.logShow(
Prelude.showNumber())(n);
});
[Faculty of Science
Information and Computing
Sciences]
80
var main = function __do() {
var n = Control_Monad_Eff_Random.random();
return Control_Monad_Eff_Console.logShow(
Prelude.showNumber())(n)();
};
[Faculty of Science
Information and Computing
Sciences]
81
Define your own effect
You can import JavaScript code as an effect.
Let’s implement a counter!
[Faculty of Science
Information and Computing
Sciences]
82
Example: counter
PureScript code:
[Faculty of Science
Information and Computing
Sciences]
83
JavaScript code:
exports.incrCounter = function() {
return ++globalCounter;
};
[Faculty of Science
Information and Computing
Sciences]
84
main :: Eff (console :: CONSOLE,
counter :: COUNTER) Unit
main = do
a <- incrCounter
b <- incrCounter
c <- incrCounter
logShow a
logShow b
logShow c
▶ returns 1 2 3
[Faculty of Science
Information and Computing
Sciences]
85
Conclusion
[Faculty of Science
Information and Computing
Sciences]
86
External Links
▶ Language Website
http://www.purescript.org
▶ Getting started
http://www.purescript.org/learn/getting-started
▶ Purescript by Example
https://leanpub.com/purescript/read
▶ Try Purescript
http://try.purescript.org
▶ Try Thermite
http://try.purescript.org/?backend=thermite
▶ Browserify
http://browserify.org
[Faculty of Science
Information and Computing
Sciences]
87