Languages as Libraries

Similar documents
Languages as Libraries

Extensible Pattern Matching

The Typed Racket Guide

It s Turtles All the Way Up: Self-Extensible Programming Languages in PLT Scheme

Typed Racket: Racket with Static Types

Typed Scheme From Scripts to Programs. Sam Tobin-Hochstadt Northeastern University

Functions & First Class Function Values

The Typed Racket Guide

Racket: Macros. Advanced Functional Programming. Jean-Noël Monette. November 2013

Tracing Comes To Racket!

Creating Languages in Racket

The syntax and semantics of Beginning Student

The syntax and semantics of Beginning Student

The Typed Racket Reference

P2R. Implementation of Processing in Racket. António Menezes Leitão. Hugo Correia ABSTRACT

Racket: Modules, Contracts, Languages

Applying Random Testing to a Base Type Environment Experience Report. Vincent St-Amour Neil Toronto PLT

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

Refining Syntactic Sugar: Tools for Supporting Macro Development

Concepts of programming languages

CMSC 631 Program Analysis and Understanding. Dynamic Typing, Contracts, and Gradual Typing

CS 360 Programming Languages Interpreters

Syntax Warnings Language-Integrated Nitpicking

Syntax. A. Bellaachia Page: 1

Why Macros? Scheme-Style Macros: Patterns and Lexical Scope. Macros versus Arbitrary Program Generators. Macros versus Arbitrary Program Generators

Scheme-Style Macros: Patterns and Lexical Scope

FROM SCRIPTS TO PROGRAMS OR, TYPES FOR UNTYPED LANGUAGES. Matthias Felleisen Racketeer

Chapter 2 A Quick Tour

A Tour of Language Implementation

TYPES ARE LIKE THE WEATHER, TYPE SYSTEMS ARE LIKE WEATHERMEN MATTHIAS FELLEISEN, RACKETEER

CSE 341 Section 7. Eric Mullen Spring Adapted from slides by Nicholas Shahan, Dan Grossman, and Tam Dang

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

CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018

A simple syntax-directed

Functional Programming. Pure Functional Languages

Macros that Work Together

CPS 506 Comparative Programming Languages. Syntax Specification

Syntax: Meta-Programming Helpers

A Simple Syntax-Directed Translator

2D Syntax. Version October 30, 2017

Project 2: Scheme Interpreter

Essentials of Programming Languages Language

Essentials of Programming Languages Language

Module 10: Imperative Programming, Modularization, and The Future

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

What is a compiler? var a var b mov 3 a mov 4 r1 cmpi a r1 jge l_e mov 2 b jmp l_d l_e: mov 3 b l_d: ;done

Macro Debugger: Inspecting Macro Expansion

CSE 341 Section 7. Ethan Shea Autumn Adapted from slides by Nicholas Shahan, Dan Grossman, Tam Dang, and Eric Mullen

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

Welcome to CS 135 (Winter 2018)

A dissertation presented by. David Fisher. to the College of Computer and Information Science of Northeastern University Boston, Massachusetts

Functional Programming. Pure Functional Languages

GRADUAL TYPING FOR FIRST-CLASS MODULES

CS2500 Exam 2 Fall 2011

Big Bang. Designing a Statically Typed Scripting Language. Pottayil Harisanker Menon, Zachary Palmer, Scott F. Smith, Alexander Rozenshteyn

Interpreters. Prof. Clarkson Fall Today s music: Step by Step by New Kids on the Block

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler.

Introduction to Compiler Design

Feature-Specific Profiling

Programming Languages: Application and Interpretation

A Self-Hosting Evaluator using HOAS

Macros that Work Together

Syntax and Grammars 1 / 21

Functional Data Structures for Typed Racket. Hari Prashanth and Sam Tobin-Hochstadt Northeastern University

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Lexical analysis. Syntactical analysis. Semantical analysis. Intermediate code generation. Optimization. Code generation. Target specific optimization

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

LECTURE 3. Compiler Phases

A Stepper for Scheme Macros

Kent Dybvig, Will Clinger, Matthew Flatt, Mike Sperber, and Anton van Straaten

Parsing. Zhenjiang Hu. May 31, June 7, June 14, All Right Reserved. National Institute of Informatics

CS115 - Module 1. Cameron Morland. Fall Cameron Morland CS115 - Module 1

Table-Driven Top-Down Parsers

Bindings & Substitution

COLLEGE OF ENGINEERING, NASHIK. LANGUAGE TRANSLATOR

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

COMPILER CONSTRUCTION LAB 2 THE SYMBOL TABLE. Tutorial 2 LABS. PHASES OF A COMPILER Source Program. Lab 2 Symbol table

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

2068 (I) Attempt all questions.

Basic Scheme February 8, Compound expressions Rules of evaluation Creating procedures by capturing common patterns

CSE341: Programming Languages Lecture 15 Macros. Zach Tatlock Winter 2018

What is a macro. CSE341: Programming Languages Lecture 15 Macros. Example uses. Using Racket Macros. Zach Tatlock Winter 2018

Programming Languages: Application and Interpretation

FrTime: A Language for Reactive Programs

Submodules in Racket

Scheme: Expressions & Procedures

Lecture 4 Abstract syntax

Fun in the Web: Web based programming environments for functional programs. Danny Yoo WPI

Vincent St-Amour. Université de Montréal, Montréal, Québec.

Introductory Example

Binding as Sets of Scopes

Chapter 1. Fundamentals of Higher Order Programming

Ruby: Introduction, Basics

Chapter 11 :: Functional Languages

CD Assignment I. 1. Explain the various phases of the compiler with a simple example.

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

Honu: A syntactically extensible language

Mid-Term 2 Grades

Transcription:

Languages as Libraries or, implementing the next 700 programming languages Sam Tobin-Hochstadt Vincent St-Amour Ryan Culpepper Matthew Flatt Matthias Felleisen PLT @ Northeastern & Utah June 6, 2011 PLDI

A domain specific language is the ultimate abstraction. Paul Hudak There will always be things we wish to say in our programs that in all known languages can only be said poorly. Alan Perlis

mzscheme racket racket/private racket/unit racket/private/base #%kernel racket/load racket/base racket/private/provider racket/signature slideshow racket/gui at-exp scheme/base at-exp racket/base scribble/doc scribble/manual scribble/base/reader scribble/lp deinprogramm/dmda htdp/asl htdp/isl+ htdp/bsl frtime/lang-utils frtime/frtime-lang-only frtime syntax/module-reader web-server/insta meta/web web-server srfi/provider typed/racket typed-scheme/minimal r6rs r5rs setup/infotab everything else Racket ships more than 40 documented languages

How can we build so many languages?

The Traditional Approach

The Traditional Approach Produces impressive results

The Macro Approach (define-syntax and (syntax-parser [(_ e1 e2) #'(if e1 e2 #f)]))

The Macro Approach (define-syntax and (syntax-parser [(_ e1 e2) #'(if e1 e2 #f)])) Supports linguistic reuse Scoping if... Functions Classes Modules

Our approach: Linguistic reuse of the macro approach Capabilities of the traditional approach

Our approach: Linguistic reuse of the macro approach Capabilities of the traditional approach By exposing compiler tools to library authors

Providing the tools

Lexing & Parsing Semantic Analysis Intermediate Language Code Generation Linking

Lexing & Parsing Semantic Analysis Intermediate Language Code Generation Language authors control each stage Linking

Lexing & Parsing Semantic Analysis [Flatt et al, 2009] Intermediate Language Code Generation Linking

Lexing & Parsing Semantic Analysis Intermediate Language Code Generation Linking In the paper

Lexing & Parsing Semantic Analysis Intermediate Language Code Generation Illustrated by Typed Racket Linking

Semantic Analysis

Static Checking #lang racket ack ; ack : Integer Integer -> Integer (define (ack m n) (cond [(<= m 0) (+ n 1)] [(<= n 0) (ack (- m 1) 1)] [else (ack (- m 1) (ack m (- n 1)))])) (ack 2 3)

Static Checking #lang typed/racket ack (: ack : Integer Integer -> Integer) (define (ack m n) (cond [(<= m 0) (+ n 1)] [(<= n 0) (ack (- m 1) 1)] [else (ack (- m 1) (ack m (- n 1)))])) (ack 2 3)

Static Checking #lang typed/racket ack (: ack : Integer Integer -> Integer) (define (ack m n) (cond [(<= m 0) (+ n 1)] [(<= n 0) (ack (- m 1) 1)] [else (ack (- m 1) (ack m (- n 1)))])) (ack 2 3) Type checking is a global process

module-begin #lang typed/racket ack (module-begin (: ack : Integer Integer -> Integer) (define (ack m n) (cond [(<= m 0) (+ n 1)] [(<= n 0) (ack (- m 1) 1)] [else (ack (- m 1) (ack m (- n 1)))])) (ack 2 3)) Languages control the whole module

Implementing a language #lang racket Module Semantics (define-syntax module-begin...) Core Syntax (define-syntax λ...) Standard Functions (define +...) typed/racket

Implementing a language #lang racket (define-syntax module-begin (syntax-parser [(_ forms...) (for ([form #'(forms...)]) (typecheck form)) #'(forms...)])) typed/racket

The Typechecker #lang racket (define (typecheck form) (syntax-parse form [v:identifier...] [(λ args body)...] [(define v body)...]... other syntactic forms...)) typechecker

Intermediate Language

Why Intermediate Languages? The compiler serves a broader set of programmers than it would if it only supported one source language Chris Lattner

Why Intermediate Languages? Most forms come from libraries (: ack : Integer Integer -> Integer) (define (ack m n) (cond [(<= m 0) (+ n 1)] [(<= n 0) (ack (- m 1) 1)] [else (ack (- m 1) (ack m (- n 1)))]))

Why Intermediate Languages? Most forms come from libraries (: ack : Integer Integer -> Integer) (define (ack m n) (cond [(<= m 0) (+ n 1)] [(<= n 0) (ack (- m 1) 1)] [else (ack (- m 1) (ack m (- n 1)))])) Also: pattern matching, keyword arguments, classes, loops, comprehensions, any many more Can t know static semantics ahead of time

Core Racket Racket defines a common subset that expansion targets expr ::= identifier (plain-lambda args expr) (app expr...+)... a dozen core expressions def ::= expr (define-values ids expr) (require spec)...

local-expand #lang racket typed/racket (define-syntax module-begin (syntax-parser [(_ forms...) (define expanded-forms (local-expand #'(forms...))) (for ([form expanded-forms]) (typecheck form)) expanded-forms]))

The Revised Typechecker #lang racket typechecker (define (typecheck form) (syntax-parse form [v:identifier...] [(plain-lambda args body)...] [(define-values vs body)...]... two dozen core forms...)) Communication between levels see paper

Code Generation

Code generation Problem: optimizing generic arithmetic (: norm : Float Float -> Float) (define (norm x y) (sqrt (+ (sqr x) (sqr y))))

Code generation Express guarantees as rewritings (: norm : Float Float -> Float) (define (norm x y) (unsafe-flsqrt (unsafe-fl+ (unsafe-fl* x x) (unsafe-fl* y y)))) Low-level operations expose code generation to libraries

Results

The take-away Languages are powerful abstractions Racket enables full-scale languages as libraries Key idea: expose compiler pipeline to language authors

The take-away Languages are powerful abstractions Racket enables full-scale languages as libraries Key idea: expose compiler pipeline to language authors Thank you racket-lang.org