JVM ByteCode Interpreter

Similar documents
Refactoring to Functional. Hadi Hariri

Box-and-arrow Diagrams

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

Introduction to Functional Programming and Haskell. Aden Seaman

Background Type Classes (1B) Young Won Lim 6/28/18

Functional Programming in C++

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

Monad Background (3A) Young Won Lim 11/18/17

Functional Programming in Java. CSE 219 Department of Computer Science, Stony Brook University

INTRODUCTION TO HASKELL

Haskell: From Basic to Advanced. Part 2 Type Classes, Laziness, IO, Modules

Contents. A Source Code 32 A.1 ASCII versions of mathematical symbols A.2 Definitions of used library functions... 32

CSCC24 Functional Programming Scheme Part 2

CS 415 Midterm Exam Spring 2002

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

R13 SET Discuss how producer-consumer problem and Dining philosopher s problem are solved using concurrency in ADA.

Monad Background (3A) Young Won Lim 10/5/17

Concepts of Programming Languages

Monad Background (3A) Young Won Lim 11/20/17

Monad Background (3A) Young Won Lim 11/8/17

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Haskell Programming

Index. object lifetimes, and ownership, use after change by an alias errors, use after drop errors, BTreeMap, 309

Chapter 15. Functional Programming Languages

Functional Programming Languages (FPL)

Functional Programming

Streams and Evalutation Strategies

College Functors, Applicatives

Lecture 8: Summary of Haskell course + Type Level Programming

Advanced Functional Programming

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Programming language design and analysis

Part 0. A (Not So) Brief Introduction to Haskell

Declarative concurrency. March 3, 2014

Organization of Programming Languages CS3200/5200N. Lecture 11

Functional Programming. Big Picture. Design of Programming Languages

CITS3211 FUNCTIONAL PROGRAMMING. 14. Graph reduction

Programming Languages and Techniques (CIS120)

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

Background Operators (1E) Young Won Lim 7/7/18

Monad (1A) Young Won Lim 6/26/17

Denotational Semantics. Domain Theory

Types and Type Inference

Functional Programming Concepts for Data Processing

CPS 506 Comparative Programming Languages. Programming Language Paradigm

Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell

Monad (3A) Young Won Lim 8/9/17

Functional Programming. Pure Functional Programming

Index. Cambridge University Press Functional Programming Using F# Michael R. Hansen and Hans Rischel. Index.

Topic 6: Partial Application, Function Composition and Type Classes

Topic 6: Partial Application, Function Composition and Type Classes

Introduction to Programming Using Java (98-388)

Test 1 Summer 2014 Multiple Choice. Write your answer to the LEFT of each problem. 5 points each 1. Preprocessor macros are associated with: A. C B.

Maybe Monad (3B) Young Won Lim 12/21/17

15. Functional Programming

Background Type Classes (1B) Young Won Lim 6/14/18

What s different about Factor?

Functional programming in C#

Chapter 15. Functional Programming Languages

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

Imperative languages

Functional Programming

Haskell Monads CSC 131. Kim Bruce

Programming Languages (PL)

Programming with Math and Logic

CS152: Programming Languages. Lecture 7 Lambda Calculus. Dan Grossman Spring 2011

Programming Paradigms

CS 11 Haskell track: lecture 5. This week: State monads

Programming in Scala Second Edition

Absolute C++ Walter Savitch

Whereweare. CS-XXX: Graduate Programming Languages. Lecture 7 Lambda Calculus. Adding data structures. Data + Code. What about functions

Types and Type Inference

Processadors de Llenguatge II. Functional Paradigm. Pratt A.7 Robert Harper s SML tutorial (Sec II)

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

CS 415 Midterm Exam Spring SOLUTION

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Haskell Overview II (2A) Young Won Lim 8/9/16

Introduction to Functional Programming

UNIT 3

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

Chapter 11 :: Functional Languages

CSCE 314 Programming Languages

Index COPYRIGHTED MATERIAL

Functional Programming

Last week, David Terei lectured about the compilation pipeline which is responsible for producing the executable binaries of the Haskell code you

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

Defining Functions. CSc 372. Comparative Programming Languages. 5 : Haskell Function Definitions. Department of Computer Science University of Arizona

Programming Language Pragmatics

Functional programming with Common Lisp

These notes are intended exclusively for the personal usage of the students of CS352 at Cal Poly Pomona. Any other usage is prohibited without

(Refer Slide Time: 4:00)

CSE P 501 Compilers. Java Implementation JVMs, JITs &c Hal Perkins Winter /11/ Hal Perkins & UW CSE V-1

Haskell 98 in short! CPSC 449 Principles of Programming Languages

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

G Programming Languages - Fall 2012

Weeks 6&7: Procedures and Parameter Passing

St. MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

Advanced Programming Handout 7. Monads and Friends (SOE Chapter 18)

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

Functional Languages and Higher-Order Functions

CIS 194: Homework 6. Due Wednesday, 4 March. Fibonacci numbers. It s all about being lazy.

Transcription:

JVM ByteCode Interpreter written in Haskell (In under 1000 Lines of Code) By Louis Jenkins

Presentation Schedule ( 15 Minutes) Discuss and Run the Virtual Machine first <5 Minutes Syntax, Binding & Scope, Data Types, Control Flow, and Subprograms <10 Minutes +Code Snippets Future goals and plans 1 minute

Virtual Machine Does Accept and parse.class files Can be generated by any JVM Language Examples shown are generated from Scala and Java Interpret a subset of ByteCode instructions Loads, Stores, Arithmetic Basic I/O support, Support for conditional expressions if else if else, for, while for only supported in Java Scala generates more complex bytecode Does Not Contain a garbage collected heap Variables exist on the stack only Support side-effects Only computations that operate purely on the operand stack and local variables work I.E: An object that is duplicated on the stack are two different objects, and not a pointer to the heap (yet) Support multi-threading Monitors are not implemented Have exception handling Although relatively trivial to implement Load the runtime Relies on stubbed pseudo-implementations I.E: println uses Haskell s built-in putstrln

Syntax Currying, Function Declaration and Definition, Pattern Guards, and Function Calling

Currying The translation of an uncurried function taking a tuple of arguments, into a sequence of functions taking only a single argument Example f x, y = z f: x y z The function f takes x as the input, and returns a function f x : y z. Note that the arrows are right associative, so x y z x y z

Function Syntax Functions take arguments as arrows Considered the Curried form of function application Declaration arrows represent the types, but the names are decided in the definition Pattern Guards Determine which function definition to call based on predicate Represented with the character Functions arguments are passed sequentially To disambiguate the function arguments, they can be wrapped in, or have the $ operator appended after the function.

Binding & Scope Rules Unlimited Extent, Lambdas, Lazy Evaluation, Thunks, and more

Binding & Scoping Rules Referential Transparency Variables defined are immutable With some exceptions Since they are immutable, their outputs are always deterministic Variables have Unlimited Extent They exist for as long as they are referenced Even variables of lambdas Lazy-Evaluation Computations are delayed inside of thunks Thunks contain lazy computations that are only evaluated when needed. Immutability All data is immutable, with some exception The IO Monad needs side-effects to interact with the RealWorld I.E: Printing to the console is a side-effect IORef, STRef, MVar, Tvar, etc., all can maintain references to immutable to data that can be changed to point something else Special Case: Software Transactional Memory Underlying data is still immutable

Control Flow Functors, Applicative Functors, Lazy Recursion and Evaluation, and Monads

A container for values that allow mapping of each of it s values from one category to another. Category: Collection of Objects I.E: Sets Example: Adding some constant to all elements in a list +1 < $ > 1.. 100 2.. 101 Functors - Simplified

Applicative Functors - Simplified A type of functor that allows partial applications Partial Applications of Functions discussed later Why? What if we want to add two functors together? + < $ > Just 2 Just 2 + Just Int Int fmap requires a b, not f a b as the mapping function Applicative does exactly that + < $ > Just 2 < > Just 2 = Just 4

Monads A type of functor that allows chaining operations. Chaining operations can be done using bind, represented as = Allows you to form pipelines of instructions Simulate side-effects Example: Processing User Input get = process = write Obtain the input String with get get IO String m = IO, a = String Process the input String with process process String IO String m = IO, a = String, b = String Write the processed String with write write String IO m = IO, a = String, b = How is this different from normal imperative programming? There are no side-effects. The String in each step is never mutated, but it appears as if it did!

Control Flow (Recursion) Recursion Any and all iteration is performed through recursion Why? Iteration requires mutation of some variable All variables are immutable Infinite recursion is actually safe Used to produce infinite data streams Recursive calls only called when needed Example: Obtain first n Fibonacci Numbers fibs = 0 1 zipwith + fibs tail fibs take n fibs Result of each call to fibs is stored as evaluated inside of a thunk. The function used zipwith a b c a b c applies the function to the head of both lists (I.E: The last two values evaluated). take will force it to evaluate only up to n times and collect the result.

Data Types Type-Classes and deriving/instantiating them

Data Types Type Classes Constructs that define methods Even arithmetic operators are methods Can sometimes be automatically derived Only if the objects they are composed of all are instances of it Can be used for type constraints of polymorphic functions Specify that the generic type must implement the listed types Have data constructors Remember: Same as a normal function Can have field selectors Can have a default values of undefined Defined as, or bottom Also used for non-terminating functions and runtime errors All types have this value in common Can be instantiated by data types Must implement required methods

Subprograms and Parameter Passing Partial Applications of Functions (in theory and practice)

Partial Application of Functions (in Theory) Applying an argument to a function taking more than one argument, resulting in a function taking one less argument Remember Currying f x, y = z f: x y z f: x y z Application: f x f x : y z Applying x to f will result in a function f x that takes the remaining arguments In Haskell, all function arguments are applied this way! Since all variables have unlimited extent, applied arguments are always safe to use! Example: The addition/plus binary operator + Int Int Int + 1 Int Int + 1 1 Int

Subprograms and Parameter Passing Partial Application of Functions (in Practice) Data Constructors for a type are just functions, and like such can be partially applied With a combination of the results from getnext, which returns a Parser Word, that result can be passed to the data constructor through application. Why is this important? Arguments can be passed from functors Arguments can also be passed by value Cuts out the amount of boilerplate Functions Composition The. operator denotes function composition. g. f x g f x = g f x Pronounced g after f of x. b c a b a c

Virtual Machine Plans and Goals Implement a Heap that takes advantage of Haskell s GC Implement all ByteCode Instructions Bootstrap Classloading Monitors Exception Handling Refactor, Refactor, Refactor Needs vast improvements!