Clojure Macros. Homoiconicity All versions of Lisp, including Clojure, are homoiconic This means that there is no difference between the form of the data.

Slides:



Advertisements
Similar presentations
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
Advertisements

Macros and Function Generators CS 480/680 – Comparative Languages.
Environments and Evaluation
PRACTICAL COMMON LISP Peter Seibel 1.
Functional Programing Referencing material from Programming Language Pragmatics – Third Edition – by Michael L. Scott Andy Balaam (Youtube.com/user/ajbalaam)
Common Lisp Macros Read for Input Macros Macro lifetime Macro syntax
CS 355 – PROGRAMMING LANGUAGES Dr. X. Common LISP A combination of many of the features of the popular dialects of LISP around in the early 1980s A large.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
Lecture 1-2CS251: Intro to AI/Lisp II “And now for something completely different…”
Macros “How can you get anything done in [other languages], I think, without macros?” - Paul Graham, 2003.
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
1 COSC generating functions, templates, and macros Yves Lespérance Adapted from Peter Roosen-Runge.
Basic Introduction to Lisp
Macros and general code walkers in Lisp: how useful! or, how useful? Ernst van Waning
C language + The Preprocessor. + Introduction The preprocessor is a program that processes that source code before it passes through the compiler. It.
LISP LISt Processing. History & Overview b b One of the oldest high level programming languages. b b First developed in 1958 by John McCarthy. b b Later.
Macros Forms that the compiler expands into code ● Decide if the macro is really necessary ● Write down the syntax of the macro ● Figure out what the macro.
Feb 17, 2015 Clojure 4. Macros Code is data We have heard this before. It is what makes Lisp so amenable to the use of macros. Examples from Mastering.
Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester,
Chapter VII: Arrays.
Advanced Computer Systems
OOP - Object Oriented Programming
Programming what is C++
Assembler, Compiler, MIPS simulator
Tail Recursion.
Defining Macros in Lisp
Parsing & Context-Free Grammars
Context-Free Grammars: an overview
CS510 Compiler Lecture 4.
Introduction to Parsing (adapted from CS 164 at Berkeley)
Compiler Construction
LISP LISt Processing.
Syntax versus Semantics
Emily Leland (Not Nick) Spring 2017
Presentation by Julie Betlach 7/02/2009
Recursion 12-Nov-18.
Important Concepts from Clojure
Important Concepts from Clojure
Chapter 11 Introduction to Programming in C
Preparing for MUPL! Justin Harjanto
The Metacircular Evaluator
Recursion 2-Dec-18.
Recursion 2-Dec-18.
FP Foundations, Scheme In Text: Chapter 14.
Functions and Macros.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
slides created by Marty Stepp
CSE 341 Section 7 Winter 2018 Adapted from slides by Eric Mullen, Nicholas Shahan, Dan Grossman, and Tam Dang.
Recursion 29-Dec-18.
Adapted from slides by Nicholas Shahan and Dan Grossman
Adapted from slides by Nicholas Shahan, Dan Grossman, and Tam Dang
Defining Macros in Lisp
Teori Bahasa dan Automata Lecture 9: Contex-Free Grammars
Recursion Taken from notes by Dr. Neil Moore
Clojure Macros.
Nicholas Shahan Spring 2016
Important Concepts from Clojure
Peter Seibel Practical Common Lisp Peter Seibel
LISP LISt Processing.
Abstraction and Repetition
Recursion 23-Apr-19.
LISP LISt Processing.
topics interpreters meta-linguistic abstraction eval and apply
Common Lisp II.
Discrete Maths 13. Grammars Objectives
CISC101 Reminders Assignment 3 due today.
Lisp.
Defining Macros in Scheme
Presentation transcript:

Clojure Macros

Homoiconicity All versions of Lisp, including Clojure, are homoiconic This means that there is no difference between the form of the data and the form of the program Original Lisp used only lists and atoms “atom”: A simple value (number, string, nil) Clojure adds vectors, maps, and a few other things The result is still homoiconic, even if it’s less obviously true The compiler mostly compiles everything down into a common form anyway Homoiconicity greatly simplifies metaprogramming 2

Macros, defined Metaprogramming is writing code that produces code Metaprogramming is particularly easy in the Lisp family of languages, because of homoiconicity “All code is data, and all data is code” In Lisp languages, macros are the primary means of doing metaprogramming A macro definition is like a function definition, with three important differences Arguments to a macro are not evaluated Macro calls are evaluated at compile time The return value of a macro should be executable code 3

A trivial macro: triple-do (defmacro triple-do [form] (list 'do form form form) ) The do is quoted, so it is put as is into the result list Three copies of the value passed in to the form are put into the list (triple-do (println "Hello")) Result: The list (do (println "Hello") (println "Hello") (println "Hello")) If executed from the REPL, the result is Hello Hello Hello nil 4

Why macros? When you write any reasonably sized program, you are designing a language to solve a particular class of problems Your functions are the “verbs” in the language Your variables are the “nouns” in the language Your “grammar” is imposed by the language—if statements, while loops, and so on Metaprogramming allows you to define new “grammar” for your language 5

Abstract Syntax Trees For virtually every programming language, a compiler or interpreter parses programs into an abstract syntax tree In Lisp, the AST is represented directly! 6 (if (> x y) (reset! max x) (reset! max y))

Syntax In a sense, Lisp (and Clojure) are “syntax free” There is syntax, but it directly represents the abstract syntax tree You can define new functions, but you cannot define new special forms Macros give you a power equivalent to writing new special forms Because of homoiconicity, Lisp macros are far more powerful that “macros” in C 7

Quotes and unquotes The usual way of quoting something is to put a single quote mark in front of it: '(a b c) The backquote does the exact same thing: `(a b c) However, things within a backquote can be “unquoted” and evaluated, by putting a tilde, ~, in front of them Quotes and unquotes can be nested, to any level The following are equivalent: (defmacro triple-do [form] (list 'do form form form) ) (defmacro triple-do [form] `(do ~form ~form ~form) ) The second of these is called a “template” For complex macros, templates can be a lot easier to read, because they “look like” the code that is generated 8

Splicing unquotes println can take multiple arguments, which it prints in order Suppose you wanted to write a macro that prints its arguments in reverse order You might try (defmacro rev-println [args] `(println ~(reverse args)) ) Given a list of values, this will print them as a list (rev-println `(a b c)) would print (c b a), not c b a The splicing-unquote operator, will insert the list values individually, not as a list (defmacro rev-println [args] `(println args)) ) 9

Generating symbols Just as in a function, you can use let to define the names of local variables These names can be used in the generated code, where they will appear as written However, the names in the generated code may conflict with other names in use where the macro is called To avoid this issue: in any syntax-quoted form (forms using the back tick), add the # symbol to the end of any local names Clojure will replace these names with unique, generated names Example: A println macro that also returns the value printed (defmacro debug-println [expr] `(let [result# ~expr] (println (str “Value is: “ result#)) result# ) ) 10

Debugging To see what you get as the result of expanding a macro, use macroexpand Example: (macroexpand '(triple-do (println "Hello"))) gives (do (println "Hello") (println "Hello") (println "Hello")) 11

Why not macros? Macros are a higher level of abstraction that the usual features of a programming language This makes them more powerful, but also harder to reason about Don’t kill flies with sledgehammers! 12

When to use macros General rule: If you can do it with a function, do it with a function Macros are easy to describe but difficult to reason about The primary thing that macros do for you is allow you to modify the language by creating new control structures and formalizing recurring patterns 13

14 The End