LFE - a lisp on the Erlang VM

Similar documents
Implementing languages on the Erlang VM

Luerl - an implementation of Lua on the Erlang VM

INF4820. Common Lisp: Closures and Macros

Common LISP-Introduction

Functional Programming - 2. Higher Order Functions

Robot Programming with Lisp

Symbolic Programming. Dr. Zoran Duric () Symbolic Programming 1/ 89 August 28, / 89

A Brief Introduction to Common Lisp

a little more on macros sort of like functions, but..

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

4. Functional Programming Language-Oriented Programming

Evaluating Scheme Expressions

Symbolic Computation and Common Lisp

User-defined Functions. Conditional Expressions in Scheme

CIS4/681 { Articial Intelligence 2 > (insert-sort '( )) ( ) 2 More Complicated Recursion So far everything we have dened requires

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. Common Lisp Fundamentals

Scripting with Luerl

Functional programming with Common Lisp

Functional Programming. Big Picture. Design of Programming Languages

The Typed Racket Guide

Introduction to ACL2. CS 680 Formal Methods for Computer Verification. Jeremy Johnson Drexel University

Lambda Calculus and Lambda notation in Lisp II. Based on Prof. Gotshalks notes on Lambda Calculus and Chapter 9 in Wilensky.

Common Lisp. Blake McBride

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

CSCC24 Functional Programming Scheme Part 2

Racket. CSE341: Programming Languages Lecture 14 Introduction to Racket. Getting started. Racket vs. Scheme. Example.

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

CSCI337 Organisation of Programming Languages LISP

A History of the Erlang VM

Functional Programming. Pure Functional Programming

The Go Programming Language. Frank Roberts

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Introduction to Functional Programming and basic Lisp

CS 314 Principles of Programming Languages

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

CS61A Midterm 2 Review (v1.1)

Metaprogramming and symbolic execution for detecting runtime errors in Erlang programs

CS 314 Principles of Programming Languages

A Genetic Algorithm Implementation

6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter

Meet the Macro. a quick introduction to Lisp macros. Patrick Stein / TC Lisp Users Group /

Documentation for LISP in BASIC

Common LISP Tutorial 1 (Basic)

CS 360 Programming Languages Interpreters

CPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia

The Typed Racket Reference

CS 11 Haskell track: lecture 1

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

Jatha. Common Lisp in Java. Ola Bini JRuby Core Developer ThoughtWorks Studios.

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. More Common Lisp

Functional Programming. Pure Functional Languages

COP4020 Programming Assignment 1 - Spring 2011

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

Functions, Conditionals & Predicates

UMBC CMSC 331 Final Exam

Functional Programming. Pure Functional Languages

More Scheme CS 331. Quiz. 4. What is the length of the list (()()()())? Which element does (car (cdr (x y z))) extract from the list?

Announcement. Overview. LISP: A Quick Overview. Outline of Writing and Running Lisp.

Scheme: Expressions & Procedures

11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT

An Introduction to Erlang. Richard Carlsson

Functional Programming

Typed Racket: Racket with Static Types

Seminar on Languages for Scientific Computing Aachen, 6 Feb Navid Abbaszadeh.

Introduction to Scheme

CS 480. Lisp J. Kosecka George Mason University. Lisp Slides

A little bit of Lisp

John McCarthy IBM 704

Erlang 101. Google Doc

Chapter 15. Functional Programming Languages

Chapter 15. Functional Programming Languages

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Streams and Lazy Evaluation in Lisp

Intro. Scheme Basics. scm> 5 5. scm>

CS390 Principles of Concurrency and Parallelism. Lecture Notes for Lecture #5 2/2/2012. Author: Jared Hall

Static rules of variable scoping in Erlang

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

A Small Interpreted Language

LECTURE 16. Functional Programming

The HALFWORD HEAP EMULATOR

CPS 506 Comparative Programming Languages. Programming Language Paradigm

Programming Languages

Core Erlang language specification

A Brief Introduction to Scheme (II)

CS 360: Programming Languages Lecture 10: Introduction to Haskell

Pierce Ch. 3, 8, 11, 15. Type Systems

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 5. LISP: MAKRA, DATOVÁ STRUKTURA ZÁZNAM

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

A Language for Specifying Type Contracts in Erlang and its Interaction with Success Typings

Advanced Functional Programming, 1DL Lecture 2, Cons T Åhs

Chapter 1. Fundamentals of Higher Order Programming

Functional Programming Languages (FPL)

COSC441. Lecture 8 Introduction to Erlang

Homework 1. Reading. Problems. Handout 3 CSCI 334: Spring, 2012

Midterm 2 Solutions Many acceptable answers; one was the following: (defparameter g1

CS558 Programming Languages

Transcription:

Robert Virding Principle Language Expert at Erlang Solutions Ltd. LFE - a lisp on the Erlang VM

What LFE isn t It isn t an implementation of Scheme It isn t an implementation of Common Lisp It isn t an implementation of Clojure Properties of the Erlang VM make these languages difficult to implement efficiently 2

What LFE is LFE is a proper lisp based on the features and limitations of the Erlang VM LFE coexists seamlessly with vanilla Erlang and OTP Runs on the standard Erlang VM 3

Overview A little history A bit of philosophy and a rationale The goal What is the BEAM? Properties of the BEAM/LFE Implementation 4

The problem Ericsson s best seller AXE telephone exchanges (switches) required large effort to develop and maintain software. The problem to solve was how to make programming these types of applications easier, but keeping the same characteristics. 5

Problem domain Lightweight, massive concurrency Fault-tolerance must be provided Timing constraints Continuous maintenance/evolution of the system Distributed systems 6

Properties of the Erlang system Lightweight, massive concurrency Asynchronous communication Process isolation Error handling Continuous evolution of the system Soft real-time These we seldom have to directly worry about in a language, except for receiving messages 7

Properties of the Erlang system Immutable data Predefined set of data types Pattern matching Functional language Modules/code No global data These are what we mainly see directly in our languages 8

Some reflections We were NOT trying to implement a functional language We were NOT trying to implement the actor model WE WERE TRYING TO SOLVE THE PROBLEM 9

Some reflections This made the development of the language/ system very focused We had a clear set of criteria for what should go into the language/system - Was it useful? - Did it or did it not help build systems? The language/system evolved to solve the problem 10

The LFE goal A proper lisp Efficient implementation on the BEAM Seamless interaction with Erlang/OTP and all libraries 11

12

New Skin for the Old Ceremony LFE libraries LFE OTP OTP Erlang ERLANG BEAM The thickness of the skin affects how efficiently the new language can be implemented and how seamlessly it can interact 13

What IS the BEAM? A virtual machine to run Erlang LFE - Lisp Flavoured Eralng 14

Properties of the BEAM Immutable data Predefined set of data types Pattern matching Functional language Modules/code No global data 15

Features of LFE Syntax Data types Modules/functions Lisp-1 vs. Lisp-2 Pattern matching Macros 16

Syntax [ ] an alternative to ( ) Symbol is any number which is not a number - a quoted symbol () [] {}. `,,@ #( #b( #m( separators #( ) tuple constant #b( ) binary constant abc < > (97 98 99) #\a or #\xab; characters 17

Data types LFE has a fixed set of data types - Numbers - Atoms (lisp symbols) - Lists - Tuples (lisp vectors) - Maps - Binaries - Opaque types 18

Atom/symbols Only has a name, no other properties ONE name space No CL packages - No name munging to fake it - foo in package bar => bar:foo Booleans are atoms, true and false 19

Binaries (binary 1 2 3) (binary (t little-endian (size 16)) (u (size 4)) (v (size 4)) (f float (size 32)) (b bitstring)) Byte/bit data with constructors Properties are type, size endianess, sign But must do ((foo a 35)) 1999-2014 Erlang Solutions Ltd. 20

Binaries (binary (ip-version (size 4)) (h-len (size 4)) (srvc-type (size 8)) (tot-len (size 16)) (id (size 16)) (flags (size 3)) (frag-off (size 13)) (ttl (size 8)) (proto (size 8)) (hrd-chksum (size 16)) (src-ip (size 32)) (dst-ip (size 32)) (rest bytes)) IP packet header 1999-2014 Erlang Solutions Ltd. 21

Modules and functions Modules are very basic - Only have name and exported functions - Only contains functions - Flat module space Modules are the unit of code handling - compilation, loading, deleting Functions only exist in modules - Except in the shell (REPL) NO interdependencies between modules 22

Modules and functions (defmodule arith (export (add 2) (add 3) (sub 2))) (defun add (a b) (+ a b)) (defun add (a b c) (+ a b c)) (defun sub (a b) (- a b)) Function definition resembles CL Functions CANNOT have a variable number of arguments Can have functions with the same name and different number of arguments (arity), they are different functions 1999-2014 Erlang Solutions Ltd. 23

Modules and functions LFE modules can consist of - Declarations - Function definitions - Macro definitions - Compile time function definitions Macros can be defined anywhere, but must be defined before being used 24

Lisp-1 vs. Lisp-2 How symbols are evaluated in the function position and argument position In Lisp-1 symbols only have value cells (foo 42 bar) value In Lisp-2 symbols have value and function cells (foo 42 bar) function value 25

Lisp-1 vs. Lisp-2 (defun foo (x y) ) (defun foo (x y z) ) (defun bar (a b c) (let ((baz (lambda (m) ))) (baz c) (foo a b) (foo 42 a b))) With Lisp-1 in LFE I can have multiple top-level functions with the same name, foo/2 and foo/3 But only one local function with a name, baz/1 THIS IS INCONSISTENT 1999-2014 Erlang Solutions Ltd. 26

Lisp-1 vs. Lisp-2 (defun foo (x y) ) (defun foo (x y z) ) (defun bar (a b c) (flet ((baz (m) ) (baz (m n) )) (foo a b) (foo 42 a b) (baz c) (baz a c))) With Lisp-2 in LFE I can have multiple top-level and local functions with the same name, foo/2, foo/3 and baz/1, baz/2 THIS IS CONSISTENT 1999-2014 Erlang Solutions Ltd. 27

Lisp-1 vs. Lisp-2 Erlang/LFE functions have both name and arity Lisp-2 fits Erlang VM better LFE is Lisp-2, or rather Lisp-2+ 28

Pattern matching Pattern matching is a BIG WIN The Erlang VM directly supports pattern matching We use pattern matching everywhere - Function clauses - let, case and receive - In macros cond, lc and bc 29

Pattern matching (let ((<pattern> <expression>) (<pattern> <expression>) ) (case <expression> (<pattern> <expression> ) (<pattern> <expression> ) ) (receive (<pattern> <expression> ) (<pattern> <expression> ) ) Variables are only bound through pattern matching 1999-2014 Erlang Solutions Ltd. 30

Pattern matching (defun name ([<pat1> <pat2> ] <expression> ) ([<pat1> <pat2> ] <expression> ) ) (cond (<test> ) ((?= <pattern> <expr>) ) ) Function clauses use pattern matching to select clause 1999-2014 Erlang Solutions Ltd. 31

Pattern matching (defun ackermann ([0 n] (+ n 1)) ([m 0] (ackermann (- m 1) 1)) ([m n] (ackermann (- m 1) (ackermann m (- n 1))))) (defun member (x es) (cond ((=:= es ()) false) ((=:= x (car es)) true) (else (member x (cdr es))))) (defun member ([x (cons e es)] (when (=:= x e)) true) ([x (cons e es)] (member x es)) ([x ()] false)) 1999-2014 Erlang Solutions Ltd. 32

Macros Macros are UNHYGIENIC No (gensym) - Cannot create unique atoms - Unsafe in long-lived systems Only compile-time at the moment - Except in the shell (REPL) Core forms can never be shadowed 33

Macros (defmacro add-them (a b) `(+,a,b)) (defmacro avg args ;(&rest args) in CL `(/ (+,@args),(length args))) (defmacro list* ((list e) e) ((cons e es) `(cons,e (list*.,es))) (() ())) Macros can have any other number of arguments - But only macro definition per name Macros can have multiple clauses like functions - The argument is then the list of arguments to the macro We have the backquote macro 1999-2014 Erlang Solutions Ltd. 34

Implementation: Erlang compiler Can work on files and Erlang abstract code Can generate.beam files or binaries Has Core, a nice intermediate language - Can be input to the compiler - simple and regular - easier to compile to 35

Implementation: Erlang compiler Internal languages Erlang Core Erlang Kernel Erlang Beam Assembler Internal modules sys_pre_expand v3_core v3_kernel v3_life v3_codegen Core optimisation passes LFE - Lisp Flavoured Eralng 36

Implementation: Core erlang Simple functional language "normal" lexical scoping Has just the basics - no records - no list comprehensions Supports pattern matching (yeah) Most optimisations done on core Dialyzer speaks Core - sort of :-( 37

Implementation: Core erlang (defun sum ([(cons h t)] (+ h (sum t))) ;`(,h.,t) ([()] 0)) 'sum'/1 = fun (_cor0) -> case _cor0 of <[H T]> when 'true' -> let <_cor1> = apply 'sum'/1(t) in call 'erlang':'+'(h, _cor1) <[]> when 'true' -> 0 ( <_cor2> when 'true' -> ( primop 'match_fail' ({'function_clause',_cor2}) - [{'function_name',{'sum',1}}] ) - ['compiler_generated'] ) end 38

Implementation: Core LFE (case expr clause...) (if test true false) (receive clause... (after timeout...)) (catch...) (try expr (case...) (catch...) (after...)) (lambda...) (match-lambda clause...) (let...) (let-function...), (letrec-function...) (cons h t), (list...) (tuple...) (binary...) (func arg...), (funcall var arg...) (call mod func arg...) (define-function name lambda match-lambda) (define-macro name lambda match-lambda) 39

WHY? WHY? WHY? I like Lisp I like Erlang I like to implement languages So doing LFE seemed natural 40

Robert Virding: rvirding@gmail.com @rvirding LFE http://lfe.io/ https://github.com/rvirding/lfe https://github.com/lfe http://groups.google.se/group/lisp-flavoured-erlang #erlang-lisp @ErlangLisp 41