{-# LANGUAGE DeriveGeneric #-} import qualified Data.HashMap.Strict as H import qualified Data.HashSet as S

Σχετικά έγγραφα
Συναρτησιακός Προγραμματισμός 2008 Λύσεις στο Δεύτερο Φύλλο Ασκήσεων

Top Down Parsing LL(1) Narges S. Bathaeian

. visual basic. int sum(int a, int b){ return a+b;} : : :

Dynamic types, Lambda calculus machines Section and Practice Problems Apr 21 22, 2016

Fractional Colorings and Zykov Products of graphs

Μεταγλωττιστές. Δημήτρης Μιχαήλ. Ακ. Έτος Συντακτική Ανάλυση. Τμήμα Πληροφορικής και Τηλεματικής Χαροκόπειο Πανεπιστήμιο

The Simply Typed Lambda Calculus

Chap. 6 Pushdown Automata

CHAPTER 101 FOURIER SERIES FOR PERIODIC FUNCTIONS OF PERIOD

Syntax Analysis Part I

Instruction Execution Times

D Alembert s Solution to the Wave Equation

VBA ΣΤΟ WORD. 1. Συχνά, όταν ήθελα να δώσω ένα φυλλάδιο εργασίας με ασκήσεις στους μαθητές έκανα το εξής: Version ΗΜΙΤΕΛΗΣ!!!!

Chapter 4 (c) parsing

SCITECH Volume 13, Issue 2 RESEARCH ORGANISATION Published online: March 29, 2018

Κατανεμημένα Συστήματα. Javascript LCR example

Calculating the propagation delay of coaxial cable

Homework 3 Solutions

Jesse Maassen and Mark Lundstrom Purdue University November 25, 2013

6.1. Dirac Equation. Hamiltonian. Dirac Eq.

Srednicki Chapter 55

TMA4115 Matematikk 3

Πίνακας Περιεχομένων. 1. Locals Window & BreakPoints

Section 9.2 Polar Equations and Graphs

3.4 SUM AND DIFFERENCE FORMULAS. NOTE: cos(α+β) cos α + cos β cos(α-β) cos α -cos β

Solutions to the Schrodinger equation atomic orbitals. Ψ 1 s Ψ 2 s Ψ 2 px Ψ 2 py Ψ 2 pz

Overview. Transition Semantics. Configurations and the transition relation. Executions and computation

Εγκατάσταση λογισμικού και αναβάθμιση συσκευής Device software installation and software upgrade

MathCity.org Merging man and maths

2 Composition. Invertible Mappings

ΚΥΠΡΙΑΚΗ ΕΤΑΙΡΕΙΑ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ 19/5/2007

Areas and Lengths in Polar Coordinates

MATH423 String Theory Solutions 4. = 0 τ = f(s). (1) dτ ds = dxµ dτ f (s) (2) dτ 2 [f (s)] 2 + dxµ. dτ f (s) (3)

Finite Field Problems: Solutions

Διάλεξη 14: Δομές Δεδομένων ΙΙI (Λίστες και Παραδείγματα)

alpha Language age (3/5) alpha Language Φροντιστήριο Syntax Directed Translation and

ΠΑΝΕΠΙΣΤΗΜΙΟ ΠΑΤΡΩΝ ΠΟΛΥΤΕΧΝΙΚΗ ΣΧΟΛΗ ΤΜΗΜΑ ΜΗΧΑΝΙΚΩΝ Η/Υ ΚΑΙ ΠΛΗΡΟΦΟΡΙΚΗΣ ΠΜΣ ΕΠΙΣΤΗΜΗ ΚΑΙ ΤΕΧΝΟΛΟΓΙΑ Η/Υ

Special edition of the Technical Chamber of Greece on Video Conference Services on the Internet, 2000 NUTWBCAM

4.6 Autoregressive Moving Average Model ARMA(1,1)

Πρόβλημα 1: Αναζήτηση Ελάχιστης/Μέγιστης Τιμής

Ηχογράφηση στο Audacity

derivation of the Laplacian from rectangular to spherical coordinates

Η γλώσσα Fortress. Γιώργος Κορφιάτης. Ερευνητικά Θέματα Υλοποίησης Γλωσσών Προγραμματισμού, Εργαστήριο Λογισμικού, ΕΜΠ ...

About these lecture notes. Simply Typed λ-calculus. Types

Areas and Lengths in Polar Coordinates

How to register an account with the Hellenic Community of Sheffield.

Concrete Mathematics Exercises from 30 September 2016

1. A fully continuous 20-payment years, 30-year term life insurance of 2000 is issued to (35). You are given n A 1

Other Test Constructions: Likelihood Ratio & Bayes Tests

EE512: Error Control Coding

Chapter 6: Systems of Linear Differential. be continuous functions on the interval

CHAPTER 12: PERIMETER, AREA, CIRCUMFERENCE, AND 12.1 INTRODUCTION TO GEOMETRIC 12.2 PERIMETER: SQUARES, RECTANGLES,

To find the relationships between the coefficients in the original equation and the roots, we have to use a different technique.

ECON 381 SC ASSIGNMENT 2

Δημιουργία Λογαριασμού Διαχείρισης Business Telephony Create a Management Account for Business Telephony

Μεταγλωττιστές. μια φοιτητική προσέγγιση

Integrals in cylindrical, spherical coordinates (Sect. 15.7)

Test Data Management in Practice

Hancock. Ζωγραφάκης Ιωάννης Εξαρχάκος Νικόλαος. ΕΠΛ 428 Προγραμματισμός Συστημάτων

Υπολογιστικά Συστήματα

HY340, 2009 Α. Σαββίδης Slide 2 / 143. HY340, 2009 Α. Σαββίδης Slide 3 / 143. HY340, 2009 Α. Σαββίδης Slide 4 / 143

Section 8.3 Trigonometric Equations

A Lambda Model Characterizing Computational Behaviours of Terms

An Inventory of Continuous Distributions

2. THEORY OF EQUATIONS. PREVIOUS EAMCET Bits.

PARTIAL NOTES for 6.1 Trigonometric Identities

Section 7.6 Double and Half Angle Formulas

The λ-calculus. Lecturer: John Wickerson. Phil Wadler

ΚΥΠΡΙΑΚΗ ΕΤΑΙΡΕΙΑ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ 6/5/2006

SOLUTIONS TO MATH38181 EXTREME VALUES AND FINANCIAL RISK EXAM

Modbus basic setup notes for IO-Link AL1xxx Master Block

CE 530 Molecular Simulation

ST5224: Advanced Statistical Theory II

C.S. 430 Assignment 6, Sample Solutions

Fourier Series. MATH 211, Calculus II. J. Robert Buchanan. Spring Department of Mathematics

ΠΑΝΕΠΙΣΤΗΜΙΟ ΚΥΠΡΟΥ - ΤΜΗΜΑ ΠΛΗΡΟΦΟΡΙΚΗΣ ΕΠΛ 133: ΑΝΤΙΚΕΙΜΕΝΟΣΤΡΕΦΗΣ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΣ ΕΡΓΑΣΤΗΡΙΟ 3 Javadoc Tutorial

b. Use the parametrization from (a) to compute the area of S a as S a ds. Be sure to substitute for ds!

Development of a Tiltmeter with a XY Magnetic Detector (Part +)

P AND P. P : actual probability. P : risk neutral probability. Realtionship: mutual absolute continuity P P. For example:

ΚΥΠΡΙΑΚΟΣ ΣΥΝΔΕΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY 21 ος ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ Δεύτερος Γύρος - 30 Μαρτίου 2011

Lecture 2. Soundness and completeness of propositional logic

A Note on Intuitionistic Fuzzy. Equivalence Relation

HOMEWORK 4 = G. In order to plot the stress versus the stretch we define a normalized stretch:

ΚΥΠΡΙΑΚΗ ΕΤΑΙΡΕΙΑ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ 24/3/2007

Risk! " #$%&'() *!'+,'''## -. / # $

Galatia SIL Keyboard Information

Μηχανική Μάθηση Hypothesis Testing

Λογισμικά για Στατιστική Ανάλυση. Minitab, R (ελεύθερο λογισμικό), Sas, S-Plus, Stata, StatGraphics, Mathematica (εξειδικευμένο λογισμικό για

Tutorial problem set 6,

Assalamu `alaikum wr. wb.

Εισαγωγή στην Πληροφορική & τον Προγραμματισμό

w o = R 1 p. (1) R = p =. = 1

ω ω ω ω ω ω+2 ω ω+2 + ω ω ω ω+2 + ω ω+1 ω ω+2 2 ω ω ω ω ω ω ω ω+1 ω ω2 ω ω2 + ω ω ω2 + ω ω ω ω2 + ω ω+1 ω ω2 + ω ω+1 + ω ω ω ω2 + ω

CRASH COURSE IN PRECALCULUS

k A = [k, k]( )[a 1, a 2 ] = [ka 1,ka 2 ] 4For the division of two intervals of confidence in R +

Practice Exam 2. Conceptual Questions. 1. State a Basic identity and then verify it. (a) Identity: Solution: One identity is csc(θ) = 1

Μεταγλωττιστές. Γιώργος Δημητρίου. Μάθημα 4 ο. Πανεπιστήμιο Θεσσαλίας - Τμήμα Πληροφορικής

From the finite to the transfinite: Λµ-terms and streams

Runtime Checking (1/3) Runtime Checking (2/3) Runtime Checking (3/3) ΗΥ 340 Γλώσσες και Μεταφραστές Φροντιστήριο

Lecture 2: Dirac notation and a review of linear algebra Read Sakurai chapter 1, Baym chatper 3

Transcript:

G (N, T, P ) N T P ϵ {-# LANGUAGE DeriveGeneric #-} import qualified Data.HashMap.Strict as H import qualified Data.HashSet as S import GHC.Generics (Generic) import Data.Hashable hash stack hashable unordered-containers executable grmsets executable grmsets-exe hs-source-dirs: app main-is: Main.hs ghc-options: -threaded -rtsopts -with-rtsopts=-n build-depends: base, grmsets, hashable, unordered-containers default-language: Haskell2010 build-depends 1

data Symbol = Term String NTerm String Eps deriving (Show,Eq,Generic) instance Hashable Symbol data Prod = Prod Symbol [Symbol] deriving (Show,Eq) data Prod = Prod Symbol [Symbol] deriving (Show,Eq) type GSet = H.HashMap Symbol (S.HashSet Symbol) S A x B y z A 1 C B 2 B B 3 B C C 4 ϵ gr = [Prod (NTerm "S") [NTerm "A", Term "x"],prod (NTerm "S") [NTerm "B", Term "y"],prod (NTerm "S") [Term "z"],prod (NTerm "A") [Term "1", NTerm "C", NTerm "B"],Prod (NTerm "A") [Term "2", NTerm "B"],Prod (NTerm "B") [Term "3", NTerm "B"],Prod (NTerm "B") [NTerm "C"],Prod (NTerm "C") [Term "4"],Prod (NTerm "C") [Eps] ] 2

updatedefault :: (Eq k, Hashable k) => (v -> v) -> v -> k -> H.HashMap k v -> H.HashMap k v updatedefault f d k m = case H.lookup k m of Nothing -> H.insert k (f d) m Just v2 -> H.insert k (f v2) m lookupdefault Main> updatedefault (+1) 0 "x" H.empty fromlist [("x",1)] Main> updatedefault (+1) 0 "x" $ H.insert "x" 10 H.empty fromlist [("x",11)] α α α S x y z first(z) = {z} first(s) = {x, y} first(z S) = {z} α N ϵ N ϵ x first(x α) = {x} first(n α) = first(n) first(n α) = (first(n ) ϵ) first(α) first(ϵ) = {ϵ} first :: [Symbol] -> GSet -> S.HashSet Symbol first [] fset = S.singleton Eps first (Term t : _) fset = S.singleton (Term t) first (Eps : syms) fset = first syms fset first (NTerm t : syms) fset = let fs_t = H.lookupDefault (NTerm t) fset in if S.member Eps fs_t then S.union (S.delete Eps fs_t) (first syms fset) else fs_t N α first(n) first(n) first(α) updatefirst updatefirst :: GSet -> Prod -> GSet updatefirst fset (Prod nt syms) fset = updatedefault (S.union (first syms fset)) nt fset 3

gr f irst(s) = {z} first(a) = {1, 2} f irst(b) = {3} first(c) = {4, ϵ} propagatefirst propagatefirst :: [Prod] -> GSet -> GSet propagatefirst grm fset = Prelude.foldl updatefirst fset grm Main> propagatefirst gr H.empty (NTerm "S",fromList [Term "z"]), (NTerm "B",fromList [Term "3"])] B S Main> propagatefirst gr it (NTerm "S",fromList [Term "z",term "3",Term "2",Term "1"]), (NTerm "B",fromList [Term "4",Term "3",Eps])] propagatefirst Main> propagatefirst gr it (NTerm "S",fromList [Term "4",Term "z",term "3",Term "y",term "2",Term "1"]), (NTerm "B",fromList [Term "4",Term "3",Eps])] propagatefirst propagatefirst fix f x = if x == result then x else fix f result where result = f x firstset grm = fix (propagatefirst grm) H.empty Main> firstset gr (NTerm "S",fromList [Term "4",Term "z",term "3",Term "y",term "2",Term "1"]), (NTerm "B",fromList [Term "4",Term "3",Eps])] 4

N N N αmβ N M α β ϵ first(β) follow(m) first(β) ϵ β M ϵ ϵ first(β) follow(m) follow(n) M N S $ $ follow(s) A 1 C B follow(c) = first(b) follow(a) ϵ first(b) gr follow(s) = {$} follow(a) = {x} follow(b) = {x, y} follow(c) = {3, 4, x, y} updatefollow :: GSet -> GSet -> Prod -> GSet updatefollow fiset foset (Prod nt []) = foset updatefollow fiset foset (Prod nt (NTerm n : syms)) = let fs_n = first syms fiset = updatefollow fiset foset (Prod nt syms) in if S.member Eps fs_n then updatedefault (\fs -> fs `S.union` H.lookupDefault nt foset `S.union` (S.delete Eps fs_n)) else updatedefault (S.union (S.delete Eps fs_n)) updatefollow fiset foset (Prod nt (_ : syms)) = updatefollow fiset foset (Prod nt syms) ϵ ϵ followset 5

{-# LANGUAGE DeriveGeneric #-} module Main where import qualified Data.HashMap.Strict as H import qualified Data.HashSet as S import GHC.Generics (Generic) import Data.Hashable -- Add these next three lines if you are using stack import Lib main :: IO () main = somefunc -- The Types data Symbol = Term String NTerm String Eps deriving (Show,Eq,Generic) instance Hashable Symbol data Prod = Prod Symbol [Symbol] deriving (Show,Eq) type GSet = H.HashMap Symbol (S.HashSet Symbol) updatedefault :: (Eq k, Hashable k) => (v -> v) -> v -> k -> H.HashMap k v -> H.HashMap k v updatedefault f d k m = case H.lookup k m of Nothing -> H.insert k (f d) m Just v2 -> H.insert k (f v2) m gr = [Prod (NTerm "S") [NTerm "A", Term "x"],prod (NTerm "S") [NTerm "B", Term "y"],prod (NTerm "S") [Term "z"],prod (NTerm "A") [Term "1", NTerm "C", NTerm "B"],Prod (NTerm "A") [Term "2", NTerm "B"],Prod (NTerm "B") [Term "3", NTerm "B"],Prod (NTerm "B") [NTerm "C"],Prod (NTerm "C") [Term "4"],Prod (NTerm "C") [Eps] ] first :: [Symbol] -> GSet -> S.HashSet Symbol 6

first [] fset = S.singleton Eps first (Term t : _) fset = S.singleton $ Term t first (Eps : syms) fset = first syms fset first (NTerm t : syms) fset = let fs_t = H.lookupDefault (NTerm t) fset in if S.member Eps fs_t then S.union (S.delete Eps fs_t) $ first syms fset else fs_t updatefirst :: GSet -> Prod -> GSet updatefirst fset (Prod nt syms) = updatedefault (S.union (first syms fset)) nt fset propagatefirst grm fset = Prelude.foldl updatefirst fset grm fix f x = if x == result then x else fix f result where result = f x firstset grm = fix (propagatefirst grm) H.empty follow sym foset = H.lookupDefault sym foset updatefollow :: GSet -> GSet -> Prod -> GSet updatefollow fiset foset (Prod nt []) = foset updatefollow fiset foset (Prod nt (NTerm n : syms)) = let fs_n = first syms fiset = updatefollow fiset foset (Prod nt syms) in if S.member Eps fs_n then updatedefault (\fs -> fs `S.union` H.lookupDefault nt foset `S.union` (S.delete Eps fs_n)) else updatedefault (S.union (S.delete Eps fs_n)) updatefollow fiset foset (Prod nt (_ : syms)) = updatefollow fiset foset (Prod nt syms) propagatefollow fiset grm foset = Prelude.foldl (updatefollow fiset) foset grm followset grm = fix (propagatefollow (firstset grm) grm) H.empty 7