peval Documentation Release Bogdan Opanchuk

Similar documents
CIS192: Python Programming

CIS192 Python Programming

CIS192 Python Programming

CSE : Python Programming

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

Functions CHAPTER 5. FIGURE 1. Concrete syntax for the P 2 subset of Python. (In addition to that of P 1.)

Assignment 7: functions and closure conversion (part 1)

Functions CHAPTER 5. FIGURE 1. Concrete syntax for the P 2 subset of Python. (In addition to that of P 1.)

Decorators in Python

Friday, 11 April 14. Advanced methods for creating decorators Graham Dumpleton PyCon US - April 2014

Assignment 7: functions and closure conversion

TAIL RECURSION, SCOPE, AND PROJECT 4 11

INF 102 CONCEPTS OF PROG. LANGS FUNCTIONAL COMPOSITION. Instructors: James Jones Copyright Instructors.

Introduction to Concepts in Functional Programming. CS16: Introduction to Data Structures & Algorithms Spring 2017

A Class to Manage Large Ensembles and Batch Execution in Python

61A Lecture 2. Wednesday, September 4, 2013

PYTHON DATA SCIENCE TOOLBOX II. List comprehensions

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

MORE SCHEME. 1 What Would Scheme Print? COMPUTER SCIENCE MENTORS 61A. October 30 to November 3, Solution: Solutions begin on the following page.

Lessons on Python Functions

HotPy (2) Binary Compatible High Performance VM for Python. Mark Shannon

Mastering Python Decorators

Course Title: Python + Django for Web Application

django-subdomains Documentation

CS61A Notes Week 13: Interpreters

PYTHON CONTENT NOTE: Almost every task is explained with an example

GE PROBLEM SOVING AND PYTHON PROGRAMMING. Question Bank UNIT 1 - ALGORITHMIC PROBLEM SOLVING

funcsigs Documentation

Nuitka - The Python Compiler. Nuitka. Kay Hayen, 2015 Page 1 of 52

INTERPRETERS 8. 1 Calculator COMPUTER SCIENCE 61A. November 3, 2016

ENVIRONMENT DIAGRAMS AND RECURSION 2

Quick announcement. Midterm date is Wednesday Oct 24, 11-12pm.

Part 1 (80 points) Multiple Choice Questions (20 questions * 4 points per question = 80 points)

HIGHER ORDER FUNCTIONS 2

STREAMS AND REVIEW 12

2.Raspberry PI: Architecture & Hardware Specifications

INF 212 ANALYSIS OF PROG. LANGS FUNCTION COMPOSITION. Instructors: Crista Lopes Copyright Instructors.

COMP519 Web Programming Lecture 20: Python (Part 4) Handouts

LINKED LISTS AND MIDTERM REVIEW

About Python. Python Duration. Training Objectives. Training Pre - Requisites & Who Should Learn Python

Lecture content. Course goals. Course Introduction. TDDA69 Data and Program Structure Introduction

Programming in Python 3

Parsing Scheme (+ (* 2 3) 1) * 1

Python in the Cling World

CSC324 Functional Programming Efficiency Issues, Parameter Lists

61A Lecture 2. Friday, August 28, 2015

Structure and Interpretation of Computer Programs

Objects CHAPTER 6. FIGURE 1. Concrete syntax for the P 3 subset of Python. (In addition to that of P 2.)

ITERATORS AND STREAMS 9

Class 6: Efficiency in Scheme

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Lecture 16: Static Semantics Overview 1

Interpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions. 1 Calculator. calc> (+ 2 2) 4

(scheme-1) has lambda but NOT define

Chapter 4 Defining Classes I

Python Decorators. Chris Calloway

Module 10: Imperative Programming, Modularization, and The Future

CSCE 110: Programming I

INTERPRETERS AND TAIL CALLS 9

Defining Functions and Using Built-ins

61A LECTURE 15 MEMOIZATION, RECURSIVE DATA, SETS

1 Decorators. 2 Descriptors. 3 Static Variables. 4 Anonymous Classes. Sandeep Sadanandan (TU, Munich) Python For Fine Programmers July 13, / 19

Python Tips and Tricks

Extending Jython. with SIM, SPARQL and SQL

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003

CIS192: Python Programming Data Types & Comprehensions Harry Smith University of Pennsylvania September 6, 2017 Harry Smith (University of Pennsylvani

TH IRD EDITION. Python Cookbook. David Beazley and Brian K. Jones. O'REILLY. Beijing Cambridge Farnham Köln Sebastopol Tokyo

;;; Determines if e is a primitive by looking it up in the primitive environment. ;;; Define indentation and output routines for the output for

Measuring Efficiency

Next: What s in a name? Programming Languages. Data model in functional PL. What s in a name? CSE 130 : Fall Lecture 13: What s in a Name?

ChAmElEoN Parse Tree

CS2304: Python for Java Programmers. CS2304: Advanced Function Topics

tolerance Documentation

Midterm Exam 1 Solutions

Fluent Python. Luciano Ramalho. Tokyo O'REILLY. Beijing. Sebastopol. Farnham. Boston

Lecture 27. Lecture 27: Regular Expressions and Python Identifiers

MetacircularScheme! (a variant of lisp)

CSE : Python Programming. Decorators. Announcements. The decorator pattern. The decorator pattern. The decorator pattern

Lecture 7. SchemeList, finish up; Universal Hashing introduction

6.001 Recitation 23: Register Machines and Stack Frames

IAP Python - Lecture 2

Execution order. main()

Python Basics. Lecture and Lab 5 Day Course. Python Basics

Announcements. My office hours are today in Gates 160 from 1PM-3PM. Programming Project 3 checkpoint due tomorrow night at 11:59PM.

Implementing Recursion

Code example: sfact SICP Explicit-control evaluator. Example register machine: instructions. Goal: a tail-recursive evaluator.

Nuitka - The statically optimizing Python Compiler

functional programming in Python, part 2

Environments

pygtrie Release Jul 03, 2017

Structure and Interpretation of Computer Programs

Flask-Cors Documentation

CS1 Recitation. Week 2

6.01: Introduction to EECS 1 Week 2 September 15, 2009

Tail Recursion. ;; a recursive program for factorial (define fact (lambda (m) ;; m is non-negative (if (= m 0) 1 (* m (fact (- m 1))))))

SCHEME AND CALCULATOR 5b

61A Lecture 3. Friday, September 5

CIS192 Python Programming

MEMOIZATION, RECURSIVE DATA, AND SETS

Transcription:

peval Documentation Release 0.1.0 Bogdan Opanchuk January 29, 2016

Contents 1 Introduction 1 2 Implementation details 3 3 Restrictions on functions 5 4 API reference 7 4.1 Core functions.............................................. 7 4.2 Helper functions............................................. 7 4.3 Tags.................................................... 7 Python Module Index 9 i

ii

CHAPTER 1 Introduction This library allows one to perform code specialization at run-time, turning this: @inline def power(x, n): if n == 0: return 1 elif n % 2 == 0: v = power(x, n // 2) return v * v else: return x * power(x, n - 1) with n set to, say, 27, into this: def power_27(x): peval_return_7 = (x * 1) peval_return_6 = ( peval_return_7 * peval_return_7) peval_return_5 = (x * peval_return_6) peval_return_4 = ( peval_return_5 * peval_return_5) peval_return_3 = ( peval_return_4 * peval_return_4) peval_return_1 = (x * peval_return_3) peval_return_2 = ( peval_return_1 * peval_return_1) return (x * peval_return_2) The resulting code runs 6 times faster under CPython 3.5.1. The API is identical to that of functools.partial(): import peval power_27 = peval.partial_apply(power, n=27) You must mark the functions that you want inlined (maybe recursively) with peval.inline(). If want some function to be evaluated during partial evaluation, mark it with peval.pure() (if it is not, in fact, pure, the results are unpredictable). 1

peval Documentation, Release 0.1.0 2 Chapter 1. Introduction

CHAPTER 2 Implementation details The partial evaluation is performed on an AST using a range of optimizations: constant propagation constant folding unreachable code elimination function inlining 3

peval Documentation, Release 0.1.0 4 Chapter 2. Implementation details

CHAPTER 3 Restrictions on functions Currently not all functions can be partially evaluated or inlined. Hopefully, these restrictions will be lifted in the future as the package improves. The inlined functions: cannot be async functions; cannot be generators; cannot have nested definitions (lambdas, functions or classes); cannot have closures; cannot have decorators and annotations (they will be ignored). The partially evaluated functions: cannot be async functions; cannot have nested definitions (lambdas, functions or classes); cannot have decorators and annotations (they will be ignored). Also note that the partial evaluated function loses the connection to the globals dictionary of the original function. Therefore, it cannot reassign global variables (the copy is shallow, so mutation of global objects is still possible). 5

peval Documentation, Release 0.1.0 6 Chapter 3. Restrictions on functions

CHAPTER 4 API reference 4.1 Core functions peval.partial_eval(func) Returns a partially evaluated version of func, using the values of associated global and closure variables. peval.partial_apply(func, *args, **kwds) Same as partial_eval(), but in addition uses the provided values of positional and keyword arguments in the partial evaluation. 4.2 Helper functions peval.getsource(func) Returns the source of a function func. Falls back to inspect.getsource() for regular functions, but can also return the source of a partially evaluated function. peval.specialize_on(names, maxsize=none) A decorator that wraps a function, partially evaluating it with the parameters defined by names (can be a string or an iterable of strings) being fixed. The partially evaluated versions are cached based on the values of these parameters using functools.lru_cache with the provided maxsize (consequently, these values should be hashable). 4.3 Tags peval.inline(func) Marks the function for inlining. peval.pure(func) Marks the function as pure (not having any side effects, except maybe argument mutation). 7

peval Documentation, Release 0.1.0 8 Chapter 4. API reference

Python Module Index p peval, 7 9

peval Documentation, Release 0.1.0 10 Python Module Index

Index G getsource() (in module peval), 7 I inline() (in module peval), 7 P partial_apply() (in module peval), 7 partial_eval() (in module peval), 7 peval (module), 7 pure() (in module peval), 7 S specialize_on() (in module peval), 7 11