[Faculty of Science Information and Computing Sciences] Concepts of programming languages PureScript Christian Stuart, Douwe van Gijn, Martijn Fleuren and Nick Begg 1
[Faculty of Science Information and Computing Sciences] Presentation overview ▶ Part 1: Introduction and Practical Matters ▶ Part 2: More Language Detail ▶ Part 3: The Foreign Function Interface ▶ Part 4: Handling side efgects 2
[Faculty of Science Information and Computing Sciences] Part 1 - ▶ Where Purescript fjts in the world ▶ A quick HOWTO ▶ Language introduction 3
[Faculty of Science Information and Computing Sciences] Where Purescript fjts in the world ▶ What is it? The big text on the website (purescript.org): PureScript is a small strongly typed programming language that compiles to JavaScript. It is a statically typed, compiled, functional language. Someone with Haskell experience should feel at home. 4
[Faculty of Science Information and Computing Sciences] Where would you use it? ▶ Where you want a functional environment for Javascript ▶ Given its JavaScript target, the common use case is for web development - both back and front end. ▶ It happily exists inside a command line environment ▶ Has a foreign function interface, so could be used anywhere JavaScript is used. ▶ There also exist non-Javascript backends, to varying degrees of completion, so other bindings are possible. 5
[Faculty of Science Information and Computing Sciences] The language and Implementation ▶ A language and an implementation are difgerent - in theory. ▶ Right now, there is one implementation. Along with this, there is a lot of common best practice . ▶ (or at least, practice ). 6
[Faculty of Science Information and Computing Sciences] Compiling ▶ The language compiles outside its runtime environment - ala C++, Java. ▶ The output of the compiler is Javascript. ▶ From a purely mechanical standpoint, compilation is a string -> string conversion. ▶ That is, there is no “magic” - no private hooks into JavaScript or Web browsers. ▶ The compiler is implemented in Haskell, but this has no practical efgect when using it. 7
[Faculty of Science Information and Computing Sciences] ▶ Where it fjts in the world ▶ A quick HOWTO 8
[Faculty of Science Information and Computing Sciences] Standard Tools - pulp pulp is the general front-end tool for Purescript development. ▶ Generates a project environment - directory structure and boilerplate ▶ Invokes the compiler as required (ala make ) ▶ Launches your program on the cmdline ▶ Automates running test suites 9
[Faculty of Science Information and Computing Sciences] Standard Tools - pulp - continued ▶ Has a built in http server for running a web app - automates starting the front- and back-end No self-respecting language these days comes without a package repository - eg CTAN / CPAN / CRAN / PyPI. Thus - ▶ Can upload your package to persuit - the Purescript package archive. ▶ Don’t confuse it with at least one other pulp project (package management) 10
[Faculty of Science Information and Computing Sciences] Standard Tools - psc, bower ▶ psc is the purescript compiler. In general, one will use pulp to invoke this. ▶ bower is a standard package manager from the javascript world. ▶ Module dependencies are brought into the project using bower (bower.json) 11
[Faculty of Science Information and Computing Sciences] Modules ▶ Module handling not as transparent as it fjrst appears - unlike R, Python et al ▶ Modules must be both imported into source code, and into the project (bower.json) 12
[Faculty of Science Information and Computing Sciences] $ pulp init [verbose output removed] module Main where import Prelude import Control.Monad.Eff.Console (CONSOLE, log) main :: forall e. Eff (console :: CONSOLE | e) Unit main = do log "Hello sailor!" Hello, world! Firstly, create a project environment with pulp - Now, contained in src/Main.purs: import Control.Monad.Eff (Eff) 13
[Faculty of Science Information and Computing Sciences] $ pulp run * Building project in/Users/nick/ps-test3 Compiling Data.Show Compiling Data.Boolean Compiling Control.Semigroupoid * Build successful. Hello sailor! Hello, world! - Running on the commandline [output trimmed] 14
[Faculty of Science Information and Computing Sciences] // Generated by psc version 0.10.3 "use strict"; var Prelude = require("../Prelude"); var Control_Monad_Eff = require("../Control.Monad.Eff"); var Control_Monad_Eff_Console = require("../Control.Monad.Eff.Console"); var main = Control_Monad_Eff_Console.log("Hello sailor!"); module.exports = { main: main }; Hello, world! - compiled as JavaScript output/Main/index.js: 15
[Faculty of Science Information and Computing Sciences] $ pulp server * Server listening on http://localhost:1337/ * Building project in /Users/nick/ps-test2 * Build successful. * Bundling JavaScript... * Bundled. Hello, world! - Running in a browser Starting the backend - 16
[Faculty of Science Information and Computing Sciences] Hello, world! - Running in a browser Figure 1: Where did our text go? 17
[Faculty of Science Information and Computing Sciences] Hello, world! - Running in a browser Where did our text go? ▶ The problem is that we printed to “stdout”; ▶ As this is a gui environment we’re not going to see anything until we create some gui objects. 18
[Faculty of Science Information and Computing Sciences] Hello, world! - Running in a browser Figure 2: There it is 19
[Faculty of Science Type :? for help > 7+5 unit hello, nautical professional! > log "hello, nautical professional!" > import Control.Monad.Eff.Console > import Prelude PSCi, version 0.10.3 Information and Computing $ pulp psci Sciences] 12 Interactive console mode Fire up an interactive session ala python, ghci (with limitations!) 20
[Faculty of Science Information and Computing Sciences] ▶ Where it fjts in the world ▶ A quick HOWTO ▶ Language introduction 21
[Faculty of Science Information and Computing Sciences] Language Overview - Basic Types ▶ JavaScript Basic types: Number, String, Boolean ▶ Additional Native types: integers, characters, arrays, records, and functions 22
[Faculty of Science Information and Computing Sciences] > :type 1 Int > :type 1.7 Number Basic Types - Numbers 23
[Faculty of Science Information and Computing Sciences] > :type true Boolean > :type false Boolean Basic Types - Boolean 24
[Faculty of Science Information and Computing Sciences] > :type "boat" String :type 'b' Char Basic Types - Strings 25
[Faculty of Science Information and Computing Sciences] > true :: Boolean true Basic Types - Strictness ▶ :: gives a type signature 26
[Faculty of Science at while checking that type Int is at least as general as type Boolean Boolean with type Int Could not match type in module $PSCI Information and Computing Error found: > 7 :: Boolean Sciences] while checking that expression 7has type Boolean in value declaration it Basic Types - Strictness ▶ Predictably - line 1, column 1 - line 1, column 3 27
[Faculty of Science Information and Computing Sciences] > true :: Int Error found: in module $PSCI at Could not match type Boolean with type Int while checking that type Boolean is at least as general as type Int while checking that expression true has type Int in value declaration it Basic Types - Strictness line 1, column 1 - line 1, column 5 28
[Faculty of Science Information and Computing Sciences] > [1,1,2,3,5,8] :: Array Int [1,1,2,3,5,8] > [1,1,2,3,5,8] [1,1,2,3,5,8] > :type [1,1,2,3,5,8] Array Int Language Overview - Arrays 29
[Faculty of Science Information and Computing Sciences] > [1, 2, 2.5] Error found: in module $PSCI at Could not match type Int with type Number Language Overview - Arrays Arrays must be homogeneous line 1, column 1 - line 1, column 11 30
[Faculty of Science Information and Computing Sciences] module Main where import Prelude fibonacci 1 = 1 fibonacci n = fibonacci (n - 2) + fibonacci (n - 1) logShow (fibonacci 7) logShow (fibonacci 15) Language Overview - Function import Control.Monad.Eff.Console (log, logShow) fibonacci :: Int -> Int fibonacci 0 = 0 main = do 31
[Faculty of Science }; (Data_Show.showInt)(fibonacci(15))(); return Control_Monad_Eff_Console.logShow (Data_Show.showInt)(fibonacci(7))(); Control_Monad_Eff_Console.logShow var main = function __do() { }; return fibonacci(v - 2) + fibonacci(v - 1) | 0; return 1; Information and Computing if (v === 1) { }; return 0; if (v === 0) { var fibonacci = function (v) { Sciences] }; Function - compiled 32
[Faculty of Science Information and Computing Sciences] $ pulp run * Building project in /Users/nick/ps/interactive Compiling Main [much output removed] * Build successful. 13 610 Function - running 33
[Faculty of Science Information and Computing Sciences] Features ▶ It has no runtime overhead ▶ 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. 34
Recommend
More recommend