Modularly Programmable Syntax

Similar documents
Safely-Composable Type-Specific Languages

Safely-Composable Type-Specific Languages (TSLs)

Composable and Hygienic Typed Syntax Macros

Syntax and Grammars 1 / 21

SMURF Language Reference Manual Serial MUsic Represented as Functions

Flang typechecker Due: February 27, 2015

Parser Tools: lex and yacc-style Parsing

Lecture 19: Signatures, Structures, and Type Abstraction

Lists. Michael P. Fourman. February 2, 2010

Many Holes in Hindley-Milner

Modules Matter Most. Robert Harper Carnegie Mellon University. MacQueen Fest Chicago May 2012

Parser Tools: lex and yacc-style Parsing

CSE3322 Programming Languages and Implementation

Racket: Modules, Contracts, Languages

exp ::= (match exp clause :::) j clause :::) j * clause :::) j (match-let ([pat exp] :::) body) j (match-let* ([pat exp] :::) body) j (match-letrec ([

Domain-Specific. Languages. Martin Fowler. AAddison-Wesley. Sydney Tokyo. With Rebecca Parsons

CSE-321: Assignment 8 (100 points)

xtc Robert Grimm Making C Safely Extensible New York University

SML Style Guide. Last Revised: 31st August 2011

Some Advanced ML Features

Metaprogramming assignment 3

Reasonably Programmable Syntax

Pattern Matching for Scheme

Homework 3 COSE212, Fall 2018

Ur/Web, a Domain-Specific Functional Programming Language for Modern Web Applications. Adam Chlipala

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

CS 312. Lecture April Lazy Evaluation, Thunks, and Streams. Evaluation

Principles of Functional Programming

Two approaches to writing interfaces

Last time: generic programming

A lexical analyzer generator for Standard ML. Version 1.6.0, October 1994

Programming in Haskell Aug Nov 2015

CS 11 Haskell track: lecture 1

Programming in Standard ML: Continued

Visitors Unchained. Using visitors to traverse abstract syntax with binding. François Pottier. Inria Paris May 22, 2017

Advanced Type System Features Tom Schrijvers. Leuven Haskell User Group

extensible Markup Language

CM-Lex and CM-Yacc User s Manual (Version 2.1)

Chapter 1: Getting Started. You will learn:

G Programming Languages - Fall 2012

CSCE 314 Programming Languages. Type System

GRADUAL TYPING FOR FIRST-CLASS MODULES

G Programming Languages Spring 2010 Lecture 8. Robert Grimm, New York University

CSC Web Technologies, Spring Web Data Exchange Formats

Active Code Completion

Functional Programming. Functional Programming

Programming Languages Third Edition. Chapter 7 Basic Semantics

Connecting Definition and Use? Tiger Semantic Analysis. Symbol Tables. Symbol Tables (cont d)

Teradata SQL Features Overview Version

CSE 341 Section 5. Winter 2018

Life in a Post-Functional World

CS Lectures 2-3. Introduction to OCaml. Polyvios Pratikakis

Functional programming Primer I

Programming in Standard ML

From Syntactic Sugar to the Syntactic Meth Lab:

Signature review: collect declarations

Compilation 2013 Semantic Analysis

Coq. LASER 2011 Summerschool Elba Island, Italy. Christine Paulin-Mohring

Exercises on ML. Programming Languages. Chanseok Oh

Compilation 2009 Domain-Specific Languages Syntax Extensions

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

The Design and Implementation of BER MetaOCaml

cs173: Programming Languages Final Exam

HOT Compilation: Elaboration

Types in Programming Languages Dynamic and Static Typing, Type Inference (CTM 2.8.3, EPL* 4) Abstract Data Types (CTM 3.7) Monads (GIH** 9)

Visitors Unchained. Using visitors to traverse abstract syntax with binding. François Pottier. WG 2.8, Edinburgh June 15, 2017

A retargetable control-flow construct for reactive, parallel and concurrent programming

EECS 700 Functional Programming

DSL developement through language embedding and extension

A modern programming language focused on integration

Concepts of programming languages

Programming Modeling Two Worlds? Programmierung Modellierung Zwei Welten? und. and. Markus Voelter Independent/itemis

Domain-Specific Languages Language Workbenches

Mini-ML. CS 502 Lecture 2 8/28/08

CS 457/557: Functional Languages

Pattern Matching and Abstract Data Types

Programming in Standard ML

XML: Introduction. !important Declaration... 9:11 #FIXED... 7:5 #IMPLIED... 7:5 #REQUIRED... Directive... 9:11

Bringing Functions into the Fold Tom Schrijvers. Leuven Haskell User Group

Functional Programming Languages (FPL)

Meta-programming in Nemerle

Using SQL with SQL Developer 18.2 Part I

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

Writing Interpreters Racket eval. CSE341 Section 7. ASTs, Interpreters, MUPL. Sunjay Cauligi. February 21 st, Sunjay Cauligi CSE341 Section 7

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

Language engineering and Domain Specific Languages

CSE 341 : Programming Languages Midterm, Spring 2015

Datatype declarations

Adam Chlipala University of California, Berkeley ICFP 2006

A Third Look At ML. Chapter Nine Modern Programming Languages, 2nd ed. 1

TIC. %goo~~oo eomputer-science. ELECTE f. No-te on Coridhitiom Cominliati"I~n Standard ML1

Hygienic Source-Code Generation Using Functors

Classical Themes of Computer Science

Modular Module Systems: a survey

COMP 105 Homework: Type Systems

An Algebra of Dependent Data Types

Functional Programming and the Web

Semantic Modularization Techniques in Practice: A TAPL case study

Programming Language Definition. Regular Expressions

CS153: Compilers Lecture 15: Local Optimization

Transcription:

Modularly Programmable Syntax Cyrus Omar Jonathan Aldrich Computer Science Department Carnegie Mellon University [CMU POP Retreat 2015]

List Syntax DERIVED FORM [1, 2, 3, 4, 5] EXPANSION Cons(1, Cons(2, Cons(3, Cons(4, Cons(5, Nil))))) 2

HTML Syntax DERIVED FORM <h1>welcome back, <%name></h1> EXPANSION H1Element(Empty, Seq( TextNode( Welcome back, ), TextNode(name))) 3

Regular Expression Syntax DERIVED FORM /A T G C/ EXPANSION Or(Str A, Or(Str T, Or(Str G, Str C ))) 4

Many Examples - Lists, sets, maps, multisets, vectors, arrays, - Monadic commands (ala Haskell do notation) - Regular expressions, SQL, other database query languages - HTML, CSS, SASS, LESS, JSON,... - Dates, times, URLs, paths, - Quasiquotation, object language syntax - Grammars - Mathematical and scientific notations (e.g. SMILE) 5

Large Languages UNSUSTAINABLE Library Language Derived Forms 6

A Better Approach: Programmable Syntax Library Language Derived Forms 8

import rx, html, json, kdb, list, xml let x = /A T G C/ fun greet(name : string) => <h1>hello, <%name></h1> 9

import rx, html, json, kdb, list, xml let x = /A T G C/ fun greet(name : string) => <h1>hello, <%name></h1> Composability: Can there be conflicts? With the base language? Between extensions? 10

import rx, html, json, kdb, list, xml let x = /A T G C/ fun greet(name : string) => <h1>hello, <%name></h1> let q = {(!R)@&{&/x!/:2_!x}'!R} Composability: Can there be conflicts? With the base language? Between extensions? Identifiability: Where did this syntax come from? 11

import rx, html, json, kdb, list, xml let x = /A T G C/ fun greet(name : string) => <h1>hello, <%name></h1> let q = {(!R)@&{&/x!/:2_!x}'!R} Composability: Can there be conflicts? With the base language? Between extensions? Identifiability: Where did this syntax come from? Type Discipline: What type does q have? 12

import rx, html, json, kdb, list, xml let x = /A T G C/ fun greet(name : string) => <h1>hello, <%name></h1> let q = {(!R)@&{&/x!/:2_!x}'!R} Composability: Can there be conflicts? With the base language? Between extensions? Identifiability: Where did this syntax come from? Type Discipline: What type does q have? Hygiene: Can I rename other variables safely? (or does the expansion capture specific variables?) 13

Our Solution: Typed Syntax Macros (TSMs) import rx, html, json, kdb, list, xml let x = rx.$regex /A T G C/ fun greet(name : string) => html.$html <h1>hello, <%name></h1> let q = kdb.$query {(!R)@&{&/x!/:2_!x}'!R} Composability: Can there be conflicts? With the base language? Between extensions? Identifiability: Where did this syntax come from? Type Discipline: What type does q have? Hygiene: Can I rename other variables safely? (or does the expansion capture specific variables?) 14

Our Solution: Typed Syntax Macros (TSMs) import rx, html, json, kdb, list, xml let x = rx.$regex /A T G C/ fun greet(name : string) => html.$html <h1>hello, <%name></h1> let q = kdb.$query {(!R)@&{&/x!/:2_!x}'!R} A fixed set of available outer delimiters prevent conflict. Composability: Can there be conflicts? With the base language? Between extensions? Identifiability: Where did this syntax come from? Type Discipline: What type does q have? Hygiene: Can I rename other variables safely? (or does the expansion capture specific variables?) 15

Our Solution: Typed Syntax Macros (TSMs) syntax $query at Query { static fn(body : Body) : Exp option => (* query parser here *) } import rx, html, json, kdb, list, xml let x = rx.$regex /A T G C/ fun greet(name : string) => html.$html <h1>hello, <%name></h1> let q = kdb.$query {(!R)@&{&/x!/:2_!x}'!R} Composability: Can there be conflicts? With the base language? Between extensions? Identifiability: Where did this syntax come from? Type Discipline: What type does q have? Hygiene: Can I rename other variables safely? (or does the expansion capture specific variables?) 16

Our Solution: Typed Syntax Macros (TSMs) syntax $query at Query { static fn(body : Body) : Exp option => (* query parser here *) } Type annotation requires that expansion be of this type. import rx, html, json, kdb, list, xml let x = rx.$regex /A T G C/ fun greet(name : string) => html.$html <h1>hello, <%name></h1> let q = kdb.$query {(!R)@&{&/x!/:2_!x}'!R} Composability: Can there be conflicts? With the base language? Between extensions? Identifiability: Where did this syntax come from? Type Discipline: What type does q have? Hygiene: Can I rename other variables safely? (or does the expansion capture specific variables?) 17

Our Solution: Typed Syntax Macros (TSMs) import rx, html, json, kdb, list, xml let x = rx.$regex /A T G C/ fun greet(name : string) => html.$html <h1>hello, <%name></h1> let q = kdb.$query {(!R)@&{&/x!/:2_!x}'!R} Only spliced subexpressions can refer to surrounding context. Composability: Can there be conflicts? With the base language? Between extensions? Identifiability: Where did this syntax come from? Type Discipline: What type does q have? Hygiene: Can I rename other variables safely? (or does the expansion capture specific variables?) 18

Our Solution: Typed Syntax Macros (TSMs) import rx, html, json, kdb, list, xml let x = rx.$regex /A T G C/ fun greet(name : string) => html.$html <h1>hello, <%name></h1> let q = kdb.$query {(!R)@&{&/x!/:2_!x}'!R} Composability: Can there be conflicts? With the base language? Between extensions? Identifiability: Where did this syntax come from? Type Discipline: What type does q have? Hygiene: Can I rename other variables safely? (or does the expansion capture specific variables?) 19

Our Solution: Typed Syntax Macros (TSMs) Expanded expressions Unexpanded expressions 20

Pattern TSMs pattern syntax $html at HTML { static fn(body : Body) : Pat option => (* HTML pattern parser *) } import rx, html, json, kdb, list, xml fun get_name(x : HTML) => match x with html.$html <h1>hello, <%name></h1> => name _ => raise Error 21

Pattern TSMs pattern syntax $html at HTML { static fn(body : Body) : Pat option => (* HTML pattern parser *) } import rx, html, json, kdb, list, xml fun get_name(x : HTML) => match x with html.$html <h1>hello, <%name></h1> => name _ => raise Error Only spliced subpatterns can bind variables. 22

The ML Module System Abstract Datatypes signature RX = sig type t val Empty : t val Str : string -> t val Seq : t * t -> t val Or : t * t -> t val Star : t -> t val case : (t -> a -> (string -> a) -> (t * t -> a) -> (t * t -> a) -> (t -> a) -> a) end 23

The ML Module System Abstract Datatypes signature RX = sig type t val Empty : t val Str : string -> t val Seq : t * t -> t val Or : t * t -> t val Star : t -> t val case : (t -> a -> (string -> a) -> (t * t -> a) -> (t * t -> a) -> (t -> a) -> a) end structure Rx1 :> RX = struct type t = string (* *) end structure Rx2 :> RX = struct type t = Empty Str of string Seq of t * t Or of t * t Star of t (* *) end... 24

The ML Module System Abstract Datatypes signature RX = sig type t val Empty : t val Str : string -> t val Seq : t * t -> t val Or : t * t -> t val Star : t -> t val case : (t -> a -> (string -> a) -> (t * t -> a) -> (t * t -> a) -> (t -> a) -> a) end structure Rx1 :> RX = struct type t = string (* *) end structure Rx2 :> RX = struct type t = Empty Str of string Seq of t * t Or of t * t Star of t (* *) end... let base = Rx1.Or(Rx1.Str( A ), Rx1.Or(Rx1.Str( T ), Rx1.Or(Rx1.Str( G ), Rx1.Str( C )))) 25

Our Solution: Parameterized TSMs Definition syntax $rx(r : RX) at R.t { static fn(body : Body) : Exp option => (* rx parser here *) } Usage let base = $rx Rx1 /A T G C/ let renzyme = $rx Rx1 /GC%{base}GC/ 26

Our Solution: Parameterized TSMs Definition syntax $rx(r : RX) at R.t { static fn(body : Body) : Exp option => (* rx parser here *) } Usage let base = $rx Rx1 /A T G C/ let renzyme = $rx Rx1 /GC%{base}GC/ 27

Our Solution: Type-Specific Languages (TSLs) Definition structure Rx1 :> RX = struct type t = string (* *) end with syntax $rx 28

Our Solution: Type-Specific Languages (TSLs) Definition structure Rx1 :> RX = struct type t = string (* *) end with syntax $rx The TSM $rx(rx1) is now associated with abstract type Rx1.t. 29

Our Solution: Type-Specific Languages (TSLs) Definition structure Rx1 :> RX = struct type t = string (* *) end with syntax $rx Usage The TSM $rx(rx1) is now associated with abstract type Rx1.t. let base : Rx1.t = /A T G C/ let renzyme : Rx1.t = /GC%{base}GC/ When a literal form appears by itself, the TSM associated with the type it is being checked against is applied. 30

Our Solution: Type-Specific Languages (TSLs) We associate TSMs with abstract types at abstraction boundaries. functor F(R : RX with syntax $rx) = struct val x : R.t = /A T G C/ end 31

Our Solution: Type-Specific Languages (TSLs) functor G() :> RX = struct type t = string (* *) end with syntax $rx structure G1 = G() (* G1.t has TSL $rx(g1) *) structure G2 = G() (* G2.t has TSL $rx(g2) *) We associate TSMs with abstract types at abstraction boundaries. 32

Conclusion Large Languages and Syntactic DSLs considered harmful. Typed syntax macros (TSMs) allow library providers to programmatically introduce new typed syntactic expansions in a safe, hygienic and modular manner. Ongoing/Future Work - Non-textual display forms in a structured editor. - Modularly programmable type structure. 33