Basics of interpretation with Haskell as the metalanguage

Similar documents
Type systems (An introduction)

Tree-based abstract syntax

Compilation a primer

Denotational semantics (An introduction)

The lambda calculus (An introduction)

Term rewriting a primer

Parsing a primer. Ralf Lämmel Software Languages Team University of Koblenz-Landau

The Notion of Software Language

Lab 6: More Steps in Haskell

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 4 MODULE, SPRING SEMESTER MATHEMATICAL FOUNDATIONS OF PROGRAMMING ANSWERS

GADTs. Wouter Swierstra. Advanced functional programming - Lecture 7. Faculty of Science Information and Computing Sciences

Logic - CM0845 Introduction to Haskell

(ii) Define a function ulh that takes a list xs, and pairs each element with all other elements in xs.

College Functors, Applicatives

CSci 450, Org. of Programming Languages. Chapter 12 Expression Language Compilation

Applicative, traversable, foldable

GADTs. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 7. [Faculty of Science Information and Computing Sciences]

CSc 520 Principles of Programming Languages

GADTs. Alejandro Serrano. AFP Summer School. [Faculty of Science Information and Computing Sciences]

M. Snyder, George Mason University LAMBDA CALCULUS. (untyped)

Applicative, traversable, foldable

Homework 3 COSE212, Fall 2018

Denotational Semantics. Domain Theory

Syntax and Grammars 1 / 21

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

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

CIS 194: Homework 3. Due Wednesday, February 11, Interpreters. Meet SImPL

Getting Started with Haskell (10 points)

Abstract Interpretation

It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

Lecture 5: Lazy Evaluation and Infinite Data Structures

Lambda Calculi With Polymorphism

Functional Programming Mid-term exam Tuesday 3/10/2017

CS 381: Programming Language Fundamentals

Functional Programming TDA 452, DIT 142

CA4003 Compiler Construction Assignment Language Definition

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

Relation Overriding. Syntax and Semantics. Simple Semantic Domains. Operational Semantics

Type Systems. Today. 1. Organizational Matters. 1. Organizational Matters. Lecture 1 Oct. 20th, 2004 Sebastian Maneth. 1. Organizational Matters

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

Oat v. 2 Language Specification

Introduction to Functional Programming in Haskell 1 / 56

A general introduction to Functional Programming using Haskell

(Not Quite) Minijava

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

Functional Programming. Pure Functional Programming

The Substitution Model

Lecture 4 Abstract syntax

Libraries (1A) Young Won Lim 6/5/17

CS 440: Programming Languages and Translators, Spring 2019 Mon

Homework #3: CMPT-379 Distributed on Oct 23; due on Nov 6 Anoop Sarkar

GADTs. GADTs in Haskell

Talen en Compilers. Johan Jeuring , period 2. December 15, Department of Information and Computing Sciences Utrecht University

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

CSE-321 Programming Languages 2010 Midterm

Type families and data kinds

10/21/2016. A Finite State Machine (FSM) Models a System. ECE 120: Introduction to Computing. An FSM Consists of Five Parts

Reasoning About Imperative Programs. COS 441 Slides 10

Graphical Untyped Lambda Calculus Interactive Interpreter

More Assigned Reading and Exercises on Syntax (for Exam 2)

CSCE 531 Spring 2009 Final Exam

MoreIntro.v. MoreIntro.v. Printed by Zach Tatlock. Oct 07, 16 18:11 Page 1/10. Oct 07, 16 18:11 Page 2/10. Monday October 10, 2016 lec02/moreintro.

Comp 311: Sample Midterm Examination

MASSACHUSETTS INSTITUTE OF TECHNOLOGY Fall Test I

Lesson 4 Typed Arithmetic Typed Lambda Calculus

UNIVERSITY OF TORONTO Faculty of Arts and Science. Midterm Sample Solutions CSC324H1 Duration: 50 minutes Instructor(s): David Liu.

Getting Started with Haskell (10 points)

CS 320: Concepts of Programming Languages

Lambda calculus. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 6

Test-data generation for Xtext

02157 Functional Programming Interpreters for two simple languages including exercises

COMP 382: Reasoning about algorithms

Lexical Considerations

CS 209 Functional Programming

Haskell Introduction Lists Other Structures Data Structures. Haskell Introduction. Mark Snyder

CIS 500 Software Foundations Midterm I Answer key October 8, 2003

Principles of Programming Languages

PROGRAM ANALYSIS & SYNTHESIS

CS2104 Prog. Lang. Concepts

Compiler Design: Lab 3 Fall 2009

Typed Recursion {with {mk-rec : (((num -> num) -> (num -> num)) -> (num -> num)) {fun {body : ((num -> num) -> (num -> num))} {{fun {fx :

Structural polymorphism in Generic Haskell

Note that in this definition, n + m denotes the syntactic expression with three symbols n, +, and m, not to the number that is the sum of n and m.

SML-SYNTAX-LANGUAGE INTERPRETER IN JAVA. Jiahao Yuan Supervisor: Dr. Vijay Gehlot

Software Verification : Introduction

Haskell An Introduction

CSCE 314 Programming Languages

Lecture 15 CIS 341: COMPILERS

GHCi: Getting started (1A) Young Won Lim 6/3/17

Lexical Considerations

SECTION II: LANGUAGE BASICS

Haskell Scripts. Yan Huang

CIS 500 Software Foundations Midterm I

Compilers. Compiler Construction Tutorial The Front-end

Type Systems. Pierce Ch. 3, 8, 11, 15 CSE

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

Formal Semantics. Prof. Clarkson Fall Today s music: Down to Earth by Peter Gabriel from the WALL-E soundtrack

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

INTRODUCTION TO HASKELL

Operators & Expressions

Transcription:

Basics of interpretation with Haskell as the metalanguage Ralf Lämmel Software Language Engineering Team University of Koblenz-Landau http://www.softlang.org/

Interpretation for a simple expression language I evaluate (Pred (If (IsZero Zero) (Succ Zero) Zero)) Left 0 - - - - - - - - - - - - - - - - - - - - - - - - - - - - I evaluate (Pred TRUE) Left Exception:... Irrefutable pattern failed for pattern... The N.B.: first example evaluates the expression to Left 0 b An interpreter is a metaprogram which executes or evaluates object programs. The expression language at hand is also referred to as BTL Basic TAPL Language where TAPL is a reference to Pierce s textbook Types and programming languages.

Informal baseline for BTL interpretation I evaluate (Pred (If (IsZero Zero) (Succ Zero) Zero)) Left 0 - - - - - - - - - - - - - - - - - - - - - - - - - - - - I evaluate (Pred TRUE) Left Exception:... Irrefutable pattern failed for pattern... The first example evaluates the expression to Left 0 b TRUE: Evaluates to True. FALSE: Evaluates to False. Zero: Evaluates to 0. Succ e: Evaluates to n+1, if e evaluates to n. Pred e: Evaluates to n-1, if e evaluates to n and n>0. Evaluates to 0, if e evaluates to 0, IsZero e: Evaluates to a Boolean for a test for 0. If e0 e1 e2: Evaluates like a normal conditional.

The interpreter for BTL I evaluate (Pred (If (IsZero Zero) (Succ Zero) Zero)) Left 0 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - I evaluate (Pred TRUE) Left Results Exception: of evaluation... Irrefutable pattern failed for pattern... type Value = Either Int Bool The first example evaluates the expression to Left 0 because IsZero Zero evaluate :: Expr æ Value evaluate TRUE = Right True evaluate FALSE = Right False evaluate Zero = Left 0 evaluate (Succ e) = Left (n+1) where Left n = evaluate e evaluate (Pred e) = Left (n if n==0 then 0 else 1) where Left n = evaluate e evaluate (IsZero e) = Right (n==0) where Left n = evaluate e evaluate (If e0 e1 e2) = evaluate (if b then e1 else e2) where Right b = evaluate e0

Interpretation for BIPL Basic Imperative Programming Language // Compute quotient q and remainder r for dividing x by y q = 0; r = x; while (r >= y) { r = r y; q = q + 1; } euclideandiv :: Stmt euclideandiv = Seq (Assign "q" (IntConst 0)) (Seq (Assign "r" (Var "x")) (While (Binary Geq (Var "r") (Var"y")) (Seq (Assign "r" (Binary Sub (Var "r") (Var"y"))) (Assign "q" (Binary Add (Var "q") (IntConst 1)))))) I execute euclideandivision (fromlist [("x", Left 13), ("y", Left 4)]) fromlist [("q",left 3),("r",Left 2),("x",Left 14),("y",Left 4)] n N.B.: Interpretation involves stores.

The interpreter for BIPL Results of expression evaluation type Value = Either Int Bool Stores as maps from variable names to values type Store = Map String Value Execution of statements execute :: Stmt æ Store æ Store execute Skip m = m execute (Assign x e) m = insert x (evaluate e m) m execute (Seq s1 s2) m = execute s2 (execute s1 m) execute (If e s1 s2) m = execute (if b then s1 else s2) m where Right b = evaluate e m execute (While e s) m = execute (If e (Seq s (While e s)) Skip) m Evaluation of expressions evaluate :: Expr æ Store æ Value evaluate (IntConst i) _ = Left i evaluate (Var x) m = m!x evaluate (Unary o e) m = uop o (evaluate e m) evaluate (Binary o e1 e2) m = bop o (evaluate e1 m) (evaluate e2 m) Interpretation of unary operators uop :: UOp æ Value æ Value uop Negate (Left i) = Left (negate i) uop Not (Right b) = Right (not b)

Interpretation for BFPL Basic Functional Programming Language factorial :: Int > Int factorial x = if ((==) x 0) then 1 else (( ) x (factorial (( ) x 1))) main = print $ factorial 5 factorial :: Program factorial = ([( "factorial", (([IntType], IntType), (["x"], If (Binary Eq (Arg "x") (IntConst 0)) (IntConst 1) (Binary Mul (Arg "x") (Apply "factorial" [Binary Sub (Arg "x") (IntConst 1)])))))], (Apply "factorial" [IntConst 5])) I evaluate factorial Left 120 Thus, in the main N.B.: Interpretation function, we apply involves theenvironments. factorial function to 5, thereby

The interpreter for BFPL Results of expression evaluation type Value = Either Int Bool Environments as maps from argument names to values type Env = Map String Value Evaluation of a program s main expression evaluate :: Program æ Value evaluate (fs, e) = f e empty where Evaluation of expressions f :: Expr æ Env æ Value f (IntConst i) _ = Left i f (BoolConst b) _ = Right b f (Arg x) m = m!x f (If e0 e1 e2) m = f (if b then e1 else e2) m where Right b = f e0 m f (Unary o e) m = uop o (f e m) f (Binary o e1 e2) m = bop o (f e1 m) (f e2 m) f (Apply x es) m = f body m where Just (_, (xs, body)) = lookup x fs vs = map (flip f m) es m = fromlist (zip xs vs)

Interpretation for FSML Finite State Machine Language mute pass ticket/eject ticket/eject turnstilefsm :: Fsm exception release turnstilefsm = Fsm [ pass/alarm State True "locked" [ (Transition "ticket" (Just "collect") "unlocked"), (Transition "pass" (Just "alarm") "exception") ], State False "unlocked" [ (Transition "ticket" (Just "eject") "unlocked"), (Transition "pass" Nothing "locked") ], State False "exception" [ (Transition "ticket" (Just "eject") "exception"), (Transition "pass" Nothing "exception"), (Transition "mute" Nothing "exception"), (Transition "release" Nothing "locked") ] ] locked ticket/collect pass unlocked A turnstile FSM in a metro system n

Expected I/O behavior of FSM interpretation Sample input for sample FSM sampleinput :: Input sampleinput = [ "ticket", Regular insertion of a ticket in locked state "ticket", Irregular insertion of a ticket in unlocked state "pass", Regular passage of turnstile in unlocked state "pass", Irregular attempt to pass turnstile in locked state "ticket", Irregular insertion of a ticket in exceptional state "mute", Mute exceptional state alarm "release" Return from exceptional to locked state ] Expected output sampleoutput :: Output sampleoutput = ["collect", "eject", "alarm", "eject"] I simulate turnstilefsm sampleinput == sampleoutput True N.B.: Interpretation commences in a step-wise manner. Also, FSML is domain-specific modeling language. This form of interpretation is essentially batch-oriented in that we as-

The interpreter for FSML FSM simulation starting from initial state simulate :: Fsm æ Input æ Output simulate (Fsm ss) xs = snd (foldl maketransition (getinitial, []) xs) where Look up initial state getinitial :: StateId getinitial = ini where [State _ ini _] = [ s s@(state initial ) Ω ss, initial ] Process event; extent output maketransition :: (StateId, Output) æ Event æ (StateId, Output) maketransition (source, as) x = (target, as ++ maybetolist a) where (Transition _ a target) = gettransition source x Look up transition gettransition :: StateId æ Event æ Transition gettransition sid x = t where [t] = [ t t@(transition x )Ω ts, x == x ] [(State ts)] = [ s s@(state _ sid _) Ω ss, sid == sid ]

Online resources YAS (Yet Another SLR (Software Language Repository)) http://www.softlang.org/yas YAS GitHub repository contains all code. See languages BTL, BIPL, BFPL, and FSML. There are Haskell directories with, for example, interpreters. The Software Languages Book http://www.softlang.org/book The book discusses interpretation in detail. Other related subjects: operational and denotational semantics.