More type systems. Physical units A unit expression is defined by following grammar u, v := b | 1 | u * v | u -1 where u, v are unit expressions themselves.

Slides:



Advertisements
Similar presentations
Escape Sequences \n newline \t tab \b backspace \r carriage return
Advertisements

Trigonometric Functions
Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004.
Computer Programming w/ Eng. Applications
Cs776 (Prasad)L4Poly1 Polymorphic Type System. cs776 (Prasad)L4Poly2 Goals Allow expression of “for all types T” fun I x = x I : ’a -> ’a Allow expression.
More about functions Plus a few random things. 2 Tail recursion A function is said to be tail recursive if the recursive call is the very last thing it.
CS7100 (Prasad)L16-7AG1 Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification.
CSE 123 Symbolic Processing. Declaring Symbolic Variables and Constants To enable symbolic processing, the variables and constants involved must first.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Trigonometric Ratios Warm Up Lesson Presentation Lesson Quiz 11.4/5
Compiler Principle and Technology Prof. Dongming LU Mar. 28th, 2014.
12 April 2013Lecture 2: Intervals, Interval Arithmetic and Interval Functions1 Intervals, Interval Arithmetic and Interval Functions Jorge Cruz DI/FCT/UNL.
Chapter 2 A Simple Compiler
3 DERIVATIVES.
DIFFERENTIATION RULES 3. Before starting this section, you might need to review the trigonometric functions. DIFFERENTIATION RULES.
Definition of Trigonometric Functions With trigonometric ratios of acute angles in triangles, we are limited to angles between 0 and 90 degrees. We now.
Introduction to Radians (Definition, Converting Between Radians and Degrees, & When to use Degrees or Radians)
Introduction All circles are similar; thus, so are the arcs intercepting congruent angles in circles. A central angle is an angle with its vertex at the.
Using Cross Products Lesson 6-4. Cross Products When you have a proportion (two equal ratios), then you have equivalent cross products. Find the cross.
9.1 Multiplying and Dividing Rational Expressions
Sine, Cosine and Tangent Ratios Objective Students will be able to use sine, cosine, and tangent ratios to determine side lengths in triangles.
1.6 Trigonometric Functions. What you’ll learn about… Radian Measure Graphs of Trigonometric Functions Periodicity Even and Odd Trigonometric Functions.
Radian Measure. Many things can be measured using different units.
5.6 Angles and Radians (#1,2(a,c,e),5,7,15,17,21) 10/5/20151.
Simplifying Variable Expressions 1. What are the coefficients of 6 + 2s s + 8g?
4.4 Solving Exponential and Logarithmic Equations.
Pattern matching. The if expression The else part of an if expression is optional if ( condition ) expression1 else expression2 If the condition evaluates.
4.2 Day 1 Trigonometric Functions on the Unit Circle Pg. 472 # 6-10 evens, evens, 46, 54, 56, 60 For each question (except the 0 o, 90 o, 180 o,
ET-314 Week 9. Basic Geometry - Perimeters Rectangles: P = 2 (L + W) Example: For a rectangle with length = 4 cm and width = 7 cm P = 2 (4 cm + 7 cm)
CS 3240: Languages and Computation Context-Free Languages.
CS 363 Comparative Programming Languages Semantics.
Adding and subtracting fractions. Fractions with the same denominator Check the answers to these questions on your calculator: a.b. c. d.
Solving Proportions. 2 ways to solve proportions 1. Equivalent fractions (Old) Cross Products (New)
Chapter 14 Day 5 Trig Functions of Any Angle.  The of a circle is a portion of the of a circle. arc circumference.
The Unit circle. Def: If the terminal side of an angle is in standard position and intersects the unit circle at P(x,y) then x = cos Ɵ and y = sin Ɵ Trig.
Functions & Libraries. Introduction Functions – Modules of code Uses – Abstraction Build complex tasks with simpler ones Don't worry about details at.
Copyright © 2013, 2009, 2005 Pearson Education, Inc. 1 Inverse, Exponential, and Logarithmic Functions Copyright © 2013, 2009, 2005 Pearson Education,
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
Compiler (scalac, gcc) Compiler (scalac, gcc) I = 0 while (i < 10) { i = i + 1 } I = 0 while (i < 10) { i = i + 1 } source code i d 3 = 0 LF w i d 3 =
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
29 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Example 1 Multiplying Fractions a. 5 2 – 3 2 – Use rule for multiplying fractions. = 2 – () 2 – 5 3 Use rule for multiplying fractions. = – 5 Evaluate.
Does point P lie on the unit circle? If Point P is the point on the terminal arm of angle  that intersects the unit circle, in which quadrant does P lie?
Circles: Arcs, Angles, and Chords. Define the following terms Chord Circle Circumference Circumference Formula Central Angle Diameter Inscribed Angle.
Holt McDougal Geometry 8-2 Trigonometric Ratios Warm Up Write each fraction as a decimal rounded to the nearest hundredth Solve each equation. 3.
CS 115 Lecture 5 Math library; building a project Taken from notes by Dr. Neil Moore.
CS 461 – Sept. 23 Context-free grammars Derivations Ambiguity Proving correctness.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
Holt Geometry 8-2 Trigonometric Ratios 8-2 Trigonometric Ratios Holt Geometry Warm Up Warm Up Lesson Presentation Lesson Presentation Lesson Quiz Lesson.
Trigonometric Functions
Calculus, Section 1.4.
Additional Topics in math Lessons 5-7
Ratio and Proportion Unit IIA Day and 8.2.
Use an addition or subtraction formula to find the exact value of the expression: {image} Select the correct answer: {image}
Trigonometric Ratios Warm Up Lesson Presentation Lesson Quiz 8-3
3 DERIVATIVES.
Find sin 2x, cos 2x, and tan 2x from the given information: {image} Select the correct answer:
Multiplying and Dividing Rational Expressions
Representation, Syntax, Paradigms, Types
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
Example: If line AB is parallel to line CD and s is parallel to t, find the measure of all the angles when m< 1 = 100°. Justify your answers. t
Objectives Find the sine, cosine, and tangent of an acute angle.
Simplify the expression: {image} Select the correct answer.
Theory of Computation Lecture #
Syntax vs Semantics Backus-Naur Form Extended BNF Derivations
45 – Rewriting Trigonometric Expressions No Calculator
Unit III Trigonometric Ratios Holt Geometry.
Assignment Operators Topics Increment and Decrement Operators
Using Cross Products Chapter 3.
Presentation transcript:

More type systems

Physical units A unit expression is defined by following grammar u, v := b | 1 | u * v | u -1 where u, v are unit expressions themselves and b is a base unit: b := m | kg | s | A | K | cd | mol You may use B to denote the set of the unit types B = { m, kg, s, A, K, cd, mol } For readability, we use the syntactic sugar u^n = u * … * u if n > 0 1 if n = 0 u -1 * … * u -1 if n < 0 and u/v = u * v -1

Physical units a) Give the type rules for the arithmetic operations +, *, /, √, sin, abs. Assume that the trigonometric functions take as argument radians, which are dimensionless (since they are defined as the ratio of arc length to radius). You can denote that a variable is dimensionless by Γ e : 1 Γ a : U Γ b : U Γ a + b : U Γ a : U Γ b : V Γ a * b : U*V Γ a : U Γ b : V Γ a / b : U/V Γ a : U*U Γ √a : U Γ a : 1 Γ sin(a) : 1 Γ a : U Γ abs(a) : U

Physical units b) The unit expressions as defined above are strings, so that e.g. (s^4*m^2)/(s^2*m^3) ≠ s^2*m however physically these units match. Define a procedure on the unit expressions such that your type rules type check expressions, whenever they are correct according to physics. trait PUnit case class Times(a: PUnit, b: PUnit) extends PUnit case class Inverse(a: PUnit) extends PUnit case class SI(v: String) extends PUnit case class One extends PUnit

Physical units def numerator(t: PUnit): List[SI] = t match { case Times(a, b) => numerator(a) ++ numerator(a) case Inverse(a) => denominator(a) case SI(_) => List(t) case One => Nil } def denominator(t: PUnit): List[SI] = t match { case Times(a, b) => denominator(a) ++ denominator (a) case Inverse(a) => numerator(a) case SI(_) => List() case One => Nil }

Physical units def simplify(t: PUnit): PUnit = { val num = numerator(t) val den = denominator(t) val inter = num intersect den val num2 = (num -- inter).sortBy(_.v) val den2 = (den – inter).sortBy(_.v) val a = (One /: num2) { case (res, p) => Times(res, p)} val b = (One /: den2) { case (res, p) => Times(res, p)} Times(num2, Inverse(denum2)) } Γ a : U Γ a : simplify(U) Γ a : (U*U) -1 Γ √a : U -1

Physical units c)Determine the type of T in the following code fragment. The values in angle brackets give the unit type expressions of the variables and Pi is the usual constant π in the Scala math library. Give the full type derivation tree using your rules from a) and b), i.e. the tree that infers the types of the variables R, w, T. val x: = 800 val y: = 6378 val g: = 9.8 val R = x + y val w = sqrt(g/R) val T = (2 * Pi) / w

Physical units val x: = 800 val y: = 6378 val g: = 9.8 val R = x + y val w = sqrt(g/R) val T = (2 * Pi) / w Γ x : m Γ x + y : m Γ g : m/(s*s) Γ g / R : (m/(s*s)) / m Γ 2 : 1 Γ π : 1 Γ 2*π : 1*1 Γ g / R : (1/s)*(1/s) Γ √(g/R): 1/s Γ y : m Γ R : m Γ g / R : 1/(s*s) Γ 2*π : 1 Γ w: 1/s Γ (2*π / w): 1/(1/s) Γ (2*π / w): s

Physical units d) Consider the following function that computes the Coulomb force, and suppose for now that the compiler can parse the type expressions: def coulomb(k:, q1:, q2:, r: ): { return (k* q1 * q2)/(r*r) } The derived types are C = A*s and N = kg*m / s^2. Does the code type check? Justify your answer rigorously. No: Expected N, got N/m. Type tree for return expression.

Physical units Γ k*q1 : N*m/C Γ k*q1*q2 : m*N Γ r*r: m*m Γ k*q1*q2/(r*r) : m*N / (m*m) Γ k*q1*q2/(r*r) : N/m Γ q2 : C Γ k*q1*q2 : (N*m/C * C) Γ k*q1 : N*m/(C*C) * C Γ k : N*m/(C*C) Γ q1 : C Γ r: m