SlideShare a Scribd company logo
1 of 9
Download to read offline
F# code, code below
If i = 1 the branch jumps to line 2, so the program prints 20, 21, 20, 21, 20, 21, . . . and does not
terminate.
If i = 2 the branch jumps to line 12, so the program prints 20, 21.
The file:
// Abstract syntax
type branchId = int
type expr =
| Num of int // integer
| Var of string // variable
| Op of string * expr * expr // e1 op e2
type stmt =
| Print of expr // print e;
| Assign of string * expr // x = e;
| Block of stmt list // block { stmt1; ...; stmtN }
| Loop of stmt list // loop { stmt1; ...; stmtN }
| Br of branchId // br i;
| BrIf of expr * branchId // if (e) br i;
// Two more examples
// block {
// x = 0
// loop {
// print x;
// x = x + 1;
// if (x < n) br 0;
// }
// }
let countTo n =
Block [
Assign ("x", Num 0)
Loop [
Print (Var "x")
Assign ("x", Op ("+", Var "x", Num 1))
BrIf (Op ("<", Var "x", Num n), 0)
]
]
// block {
// x = 10
// y = 0
// block {
// loop {
// if (x < 2) br 1;
// x = x - 2;
// y = y + 1;
// br 0;
// }
// }
// print y;
// }
let div2 =
Block [
Assign ("x", Num 10)
Assign ("y", Num 0)
Block [
Loop [
BrIf (Op ("<", Var "x", Num 2), 1)
Assign ("x", Op ("-", Var "x", Num 2))
Assign ("y", Op ("+", Var "y", Num 1))
Br 0
]
]
Print (Var "y")
]
// Prettyprinter
let paren b s = if b then "(" + s + ")" else s
let rec prettyExpr (prec : int) (e : expr) : string =
match e with
| Var x -> x
| Num i -> sprintf "%d" i
| Op (op, e1, e2) ->
let e1 = prettyExpr 1 e1
let e2 = prettyExpr 1 e2
sprintf "%s %s %s" e1 op e2 |> paren (0 < prec)
let rec prettyStmtI (indent : string) (stmt : stmt) : string =
let extraIndent = indent + " "
match stmt with
| Print e ->
let e = prettyExpr 1 e
sprintf "%sprint %s;" indent e
| Assign (x, e) ->
let e = prettyExpr 0 e
sprintf "%s%s = %s;" indent x e
| Block stmts ->
let stmts = prettyStmtsI extraIndent stmts
sprintf "%sblock {n%s%s}" indent stmts indent
| Loop stmts ->
let stmts = prettyStmtsI extraIndent stmts
sprintf "%sloop {n%s%s}" indent stmts indent
| Br i -> sprintf "%sbr %i;" indent i
| BrIf (e, i) ->
let e = prettyExpr 0 e
sprintf "%sif (%s) br %i;" indent e i
and prettyStmtsI (indent : string) (stmts : stmt list) : string =
List.map (fun stmt -> prettyStmtI indent stmt + "n") stmts |> String.concat ""
let prettyStmt (stmt : stmt) : string =
prettyStmtI "" stmt
// Execution of statements
type naivestore = Map
let emptystore : Map = Map.empty
let getSto (store : naivestore) x = store.Item x
let setSto (store : naivestore) (k, v) = store.Add(k, v)
type result =
| Continue of naivestore
| Branch of branchId * naivestore
let rec eval e (store : naivestore) : int =
match e with
| Num i -> i
| Var x -> getSto store x
| Op (op, e1, e2) ->
let i1 = eval e1 store
let i2 = eval e2 store
match op with
| "*" -> i1 * i2
| "+" -> i1 + i2
| "-" -> i1 - i2
| "==" -> if i1 = i2 then 1 else 0
| "<" -> if i1 < i2 then 1 else 0
| _ -> failwith "unknown primitive"
let rec exec (stmt : stmt) (store : naivestore) : result =
match stmt with
| Print e ->
let v = eval e store
printfn "%d" v
Continue store
| Assign (x, e) ->
let v = eval e store
let store = setSto store (x, v)
Continue store
| Block stmts ->
match execStmts stmts store with
| Continue store -> Continue store
| Branch (i, store) ->
if i = 0
then Continue store
else Branch (i - 1, store)
| Loop stmts ->
failwith "Not implemented"
| Br i -> Branch (i, store)
| BrIf (e, i) ->
let v = eval e store
if v = 0 then Continue store else Branch (i, store)
and execStmts (stmts : stmt list) (store : naivestore) : result =
match stmts with
| [] -> Continue store
| stmt::stmts ->
match exec stmt store with
| Continue store -> execStmts stmts store
| Branch (i, store) -> Branch (i, store)
let run (stmt : stmt) : unit =
match exec stmt emptystore with
| Continue _ -> ()
| Branch _ -> failwith "Invalid branch"
// Make an if statement
let makeIf (e : expr) (stmt1 : stmt) (stmt2 : stmt) : stmt =
failwith "Not implemented"
// Abstract syntax of a simple imperative language with if statements,
// and a function that converts to the language above (assuming makeIf
// is implemented correctly)
type sstmt =
| SPrint of expr // print e
| SAssign of string * expr // x = e
| SBlock of sstmt list // { stmt1; stmt2; ...; stmtN }
| SIf of expr * sstmt * sstmt // if (e) stmt stmt
let rec convert (stmt : sstmt) : stmt =
match stmt with
| SPrint e -> Print e
| SAssign (x, e) -> Assign (x, e)
| SBlock stmts -> Block (List.map convert stmts)
| SIf (e, stmt1, stmt2) -> makeIf e (convert stmt1) (convert stmt2)
Here are tests for the question:
// block {
// x = n;
// y = 0;
// loop {
// z = x;
// loop {
// if (z < 1) br 2;
// y = y + 1;
// z = z - 1;
// if (0 < z) br 0;
// }
// x = x - 1;
// print y;
// br 0;
// }
// print z;
// }
let triangle n =
Block [
Assign ("x", Num n)
Assign ("y", Num 0)
Loop [
Assign ("z", Var "x")
Loop [
BrIf (Op ("<", Var "z", Num 1), 2)
Assign ("y", Op ("+", Var "y", Num 1))
Assign ("z", Op ("-", Var "z", Num 1))
BrIf (Op ("<", Num 0, Var "z"), 0)
]
Assign ("x", Op ("-", Var "x", Num 1))
Print (Var "y")
Br 0
]
Print (Var "z")
]
// > run (countTo 0);;
// 0
// val it: unit = ()
// > run (countTo 3);;
// 0
// 1
// 2
// val it: unit = ()
// > run (countTo 8);;
// 0
// 1
// 2
// 3
// 4
// 5
// 6
// 7
// val it: unit = ()
// > run div2;;
// 5
// val it: unit = ()
// > run (triangle 2);;
// 2
// 3
// val it: unit = ()
// > run (triangle 6);;
// 6
// 11
// 15
// 18
// 20
// 21
// val it: unit = ()
let s1 = SIf (Num 1, SPrint (Num 10), SPrint (Num 20))
let s2 = SIf (Num 0, SPrint (Num 10), SPrint (Num 20))
let s3 = SBlock [SAssign ("x", Num 10); SIf (Var "x", SPrint (Num 100), SBlock [])]
let s4 n =
SBlock [
SAssign ("x", Num n);
SIf (Op ("<", Num 0, Var "x"), SBlock [
SPrint (Num 0);
SIf (Op ("<", Num 5, Var "x"), SPrint (Num 5), SPrint (Var "x"))
],
SBlock [
SAssign ("x", Num 100)
SPrint (Var "x")
])
]
// The following test makeIf
// > run (convert s1);;
// 10
// val it: unit = ()
// > run (convert s2);;
// 20
// val it: unit = ()
// > run (convert s3);;
// 100
// val it: unit = ()
// > run (convert (s4 -1));;
// 100
// val it: unit = ()
// > run (convert (s4 1));;
// 0
// 1
// val it: unit = ()
// > run (convert (s4 10));;
// 0
// 5
// val it: unit = () Assignment4.fs contains a simple imperative language with WebAssembly-like
control flow involving blocks and branch statements. The statement br i means branch to the i th,
where blocks are counted from the innermost to the outermost. The 0th block is the block that
directly contains the branch statement, and the (i+1) th block is the block that directly contains
the i th block. There are two kinds of block: simple blocks (block in concrete syntax) and loops
(loop in concrete syntax). They differ in how they behave as branch targets: - If the target of a
branch is a simple block, then control jumps to the end of the block, so that the next statement
executed is (lexically) after the block. - If the target of a branch is a loop, then control jumps to
the beginning of the loop. At the end of a block of either kind, if there is no branch, then the next
statement executed is the next one that appears lexically. For an example, consider what the
branch in the following program will do when program is executed.
123456789101112block{loop{print20;block{block{print21;}bri;loop{print22;}}print23;}print24
;
(i) The function exec executes a statement, given a store. The result is Branch (i, sto) if execution
should branch to the ith block, and Continue sto otherwise. In both cases, sto is the new store.
Implement the Loop case of exec. You should use a recursive call to exec to handle a branch to
the beginning of the loop. ii) There are no if statements except for conditional branches, but
conditional branches are enough to implement if. Implement makeIf : expr stmt stmt stmt so
that makeIf e stmt1 stmt2 is a statement that behaves like stmt1 if e is non-zero and stmt2
otherwise. Your function should not inspect e, stmt1 or stmt2, and it should not use Loop.

More Related Content

Similar to F# code, code below If i = 1 the branch jumps to line 2, so the pr.pdf

Start with the inclusion of libraries#include iostream .docx
 Start with the inclusion of libraries#include iostream .docx Start with the inclusion of libraries#include iostream .docx
Start with the inclusion of libraries#include iostream .docx
MARRY7
 
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdfIn Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
anjandavid
 
operating system ubuntu,linux,MacProgram will work only if you g.pdf
operating system ubuntu,linux,MacProgram will work only if you g.pdfoperating system ubuntu,linux,MacProgram will work only if you g.pdf
operating system ubuntu,linux,MacProgram will work only if you g.pdf
aptcomputerzone
 

Similar to F# code, code below If i = 1 the branch jumps to line 2, so the pr.pdf (20)

Python
PythonPython
Python
 
Go Programming Language (Golang)
Go Programming Language (Golang)Go Programming Language (Golang)
Go Programming Language (Golang)
 
Swift 5.1 Language Guide Notes.pdf
Swift 5.1 Language Guide Notes.pdfSwift 5.1 Language Guide Notes.pdf
Swift 5.1 Language Guide Notes.pdf
 
F# intro
F# introF# intro
F# intro
 
Golang and Eco-System Introduction / Overview
Golang and Eco-System Introduction / OverviewGolang and Eco-System Introduction / Overview
Golang and Eco-System Introduction / Overview
 
Pythonlearn-03-Conditional.pptx
Pythonlearn-03-Conditional.pptxPythonlearn-03-Conditional.pptx
Pythonlearn-03-Conditional.pptx
 
Introduction to python programming
Introduction to python programmingIntroduction to python programming
Introduction to python programming
 
Csci101 lect04 advanced_selection
Csci101 lect04 advanced_selectionCsci101 lect04 advanced_selection
Csci101 lect04 advanced_selection
 
Asterisk: PVS-Studio Takes Up Telephony
Asterisk: PVS-Studio Takes Up TelephonyAsterisk: PVS-Studio Takes Up Telephony
Asterisk: PVS-Studio Takes Up Telephony
 
MP in Clojure
MP in ClojureMP in Clojure
MP in Clojure
 
Python quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung FuPython quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung Fu
 
Start with the inclusion of libraries#include iostream .docx
 Start with the inclusion of libraries#include iostream .docx Start with the inclusion of libraries#include iostream .docx
Start with the inclusion of libraries#include iostream .docx
 
Python 1 liners
Python 1 linersPython 1 liners
Python 1 liners
 
ECMAScript 6 and beyond
ECMAScript 6 and beyondECMAScript 6 and beyond
ECMAScript 6 and beyond
 
Gophercon 2016 Communicating Sequential Goroutines
Gophercon 2016 Communicating Sequential GoroutinesGophercon 2016 Communicating Sequential Goroutines
Gophercon 2016 Communicating Sequential Goroutines
 
LEX & YACC TOOL
LEX & YACC TOOLLEX & YACC TOOL
LEX & YACC TOOL
 
Rust LDN 24 7 19 Oxidising the Command Line
Rust LDN 24 7 19 Oxidising the Command LineRust LDN 24 7 19 Oxidising the Command Line
Rust LDN 24 7 19 Oxidising the Command Line
 
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdfIn Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
 
CSE240 Pointers
CSE240 PointersCSE240 Pointers
CSE240 Pointers
 
operating system ubuntu,linux,MacProgram will work only if you g.pdf
operating system ubuntu,linux,MacProgram will work only if you g.pdfoperating system ubuntu,linux,MacProgram will work only if you g.pdf
operating system ubuntu,linux,MacProgram will work only if you g.pdf
 

More from alphaagenciesindia

Requirements Use JavaDo Not Use RecursionDo Not Use Array Lis.pdf
Requirements Use JavaDo Not Use RecursionDo Not Use Array Lis.pdfRequirements Use JavaDo Not Use RecursionDo Not Use Array Lis.pdf
Requirements Use JavaDo Not Use RecursionDo Not Use Array Lis.pdf
alphaagenciesindia
 
Required The City of Mississauga Goes Digital The City of Mississ.pdf
Required The City of Mississauga Goes Digital The City of Mississ.pdfRequired The City of Mississauga Goes Digital The City of Mississ.pdf
Required The City of Mississauga Goes Digital The City of Mississ.pdf
alphaagenciesindia
 
Reflexiona sobre un cambio profesional importante que hayas experime.pdf
Reflexiona sobre un cambio profesional importante que hayas experime.pdfReflexiona sobre un cambio profesional importante que hayas experime.pdf
Reflexiona sobre un cambio profesional importante que hayas experime.pdf
alphaagenciesindia
 
Required Designate where each of the following events would be pre.pdf
Required  Designate where each of the following events would be pre.pdfRequired  Designate where each of the following events would be pre.pdf
Required Designate where each of the following events would be pre.pdf
alphaagenciesindia
 
Romance en la oficina buscando respuestas a tientasDescripci�n de.pdf
Romance en la oficina buscando respuestas a tientasDescripci�n de.pdfRomance en la oficina buscando respuestas a tientasDescripci�n de.pdf
Romance en la oficina buscando respuestas a tientasDescripci�n de.pdf
alphaagenciesindia
 
ROBOTICS Currently, roboticists and AI researchers are still strug.pdf
ROBOTICS Currently, roboticists and AI researchers are still strug.pdfROBOTICS Currently, roboticists and AI researchers are still strug.pdf
ROBOTICS Currently, roboticists and AI researchers are still strug.pdf
alphaagenciesindia
 
RKO-Stanley v. GrazianoEagen, J.On April 30, 1970, RKO-Stanley W.pdf
RKO-Stanley v. GrazianoEagen, J.On April 30, 1970, RKO-Stanley W.pdfRKO-Stanley v. GrazianoEagen, J.On April 30, 1970, RKO-Stanley W.pdf
RKO-Stanley v. GrazianoEagen, J.On April 30, 1970, RKO-Stanley W.pdf
alphaagenciesindia
 

More from alphaagenciesindia (20)

Researchers studying the STAR data report anecdotal evidence that sc.pdf
Researchers studying the STAR data report anecdotal evidence that sc.pdfResearchers studying the STAR data report anecdotal evidence that sc.pdf
Researchers studying the STAR data report anecdotal evidence that sc.pdf
 
Research results suggest a relationship between the TV viewing habit.pdf
Research results suggest a relationship between the TV viewing habit.pdfResearch results suggest a relationship between the TV viewing habit.pdf
Research results suggest a relationship between the TV viewing habit.pdf
 
Research done by doctors show that individuals with Kreuzfeld-Jacob .pdf
Research done by doctors show that individuals with Kreuzfeld-Jacob .pdfResearch done by doctors show that individuals with Kreuzfeld-Jacob .pdf
Research done by doctors show that individuals with Kreuzfeld-Jacob .pdf
 
Requirements Use JavaDo Not Use RecursionDo Not Use Array Lis.pdf
Requirements Use JavaDo Not Use RecursionDo Not Use Array Lis.pdfRequirements Use JavaDo Not Use RecursionDo Not Use Array Lis.pdf
Requirements Use JavaDo Not Use RecursionDo Not Use Array Lis.pdf
 
Research and describe TWO diseases - The cardiovascular disease .pdf
Research and describe TWO diseases - The cardiovascular disease .pdfResearch and describe TWO diseases - The cardiovascular disease .pdf
Research and describe TWO diseases - The cardiovascular disease .pdf
 
Required The City of Mississauga Goes Digital The City of Mississ.pdf
Required The City of Mississauga Goes Digital The City of Mississ.pdfRequired The City of Mississauga Goes Digital The City of Mississ.pdf
Required The City of Mississauga Goes Digital The City of Mississ.pdf
 
Required1-a. Determine Mahomess pension expense for 2024.1-b, .pdf
Required1-a. Determine Mahomess pension expense for 2024.1-b, .pdfRequired1-a. Determine Mahomess pension expense for 2024.1-b, .pdf
Required1-a. Determine Mahomess pension expense for 2024.1-b, .pdf
 
Relaciona cada �poca con sus caracter�sticas definitorias. Per�od.pdf
Relaciona cada �poca con sus caracter�sticas definitorias.  Per�od.pdfRelaciona cada �poca con sus caracter�sticas definitorias.  Per�od.pdf
Relaciona cada �poca con sus caracter�sticas definitorias. Per�od.pdf
 
Regarding the management of R&D teams after Vandeputte�s diversifica.pdf
Regarding the management of R&D teams after Vandeputte�s diversifica.pdfRegarding the management of R&D teams after Vandeputte�s diversifica.pdf
Regarding the management of R&D teams after Vandeputte�s diversifica.pdf
 
Reflect on your current workplace where you currently interact with .pdf
Reflect on your current workplace where you currently interact with .pdfReflect on your current workplace where you currently interact with .pdf
Reflect on your current workplace where you currently interact with .pdf
 
Reflexiona sobre un cambio profesional importante que hayas experime.pdf
Reflexiona sobre un cambio profesional importante que hayas experime.pdfReflexiona sobre un cambio profesional importante que hayas experime.pdf
Reflexiona sobre un cambio profesional importante que hayas experime.pdf
 
ReinforcementR-4.3 Viruses that perform no explicit malicious beha.pdf
ReinforcementR-4.3 Viruses that perform no explicit malicious beha.pdfReinforcementR-4.3 Viruses that perform no explicit malicious beha.pdf
ReinforcementR-4.3 Viruses that perform no explicit malicious beha.pdf
 
Required 1. prepare the Income Statement for the year ended Decembe.pdf
Required 1. prepare the Income Statement for the year ended Decembe.pdfRequired 1. prepare the Income Statement for the year ended Decembe.pdf
Required 1. prepare the Income Statement for the year ended Decembe.pdf
 
Required Designate where each of the following events would be pre.pdf
Required  Designate where each of the following events would be pre.pdfRequired  Designate where each of the following events would be pre.pdf
Required Designate where each of the following events would be pre.pdf
 
Routes of Administration The nurse is teaching clients with inflamma.pdf
Routes of Administration The nurse is teaching clients with inflamma.pdfRoutes of Administration The nurse is teaching clients with inflamma.pdf
Routes of Administration The nurse is teaching clients with inflamma.pdf
 
Renzo es un l�der entusiasta que cree en sus ideas. Renzo se detiene.pdf
Renzo es un l�der entusiasta que cree en sus ideas. Renzo se detiene.pdfRenzo es un l�der entusiasta que cree en sus ideas. Renzo se detiene.pdf
Renzo es un l�der entusiasta que cree en sus ideas. Renzo se detiene.pdf
 
Romance en la oficina buscando respuestas a tientasDescripci�n de.pdf
Romance en la oficina buscando respuestas a tientasDescripci�n de.pdfRomance en la oficina buscando respuestas a tientasDescripci�n de.pdf
Romance en la oficina buscando respuestas a tientasDescripci�n de.pdf
 
ROBOTICS Currently, roboticists and AI researchers are still strug.pdf
ROBOTICS Currently, roboticists and AI researchers are still strug.pdfROBOTICS Currently, roboticists and AI researchers are still strug.pdf
ROBOTICS Currently, roboticists and AI researchers are still strug.pdf
 
Roberts Rules of Order refer toA.procedural guidelines on the corre.pdf
Roberts Rules of Order refer toA.procedural guidelines on the corre.pdfRoberts Rules of Order refer toA.procedural guidelines on the corre.pdf
Roberts Rules of Order refer toA.procedural guidelines on the corre.pdf
 
RKO-Stanley v. GrazianoEagen, J.On April 30, 1970, RKO-Stanley W.pdf
RKO-Stanley v. GrazianoEagen, J.On April 30, 1970, RKO-Stanley W.pdfRKO-Stanley v. GrazianoEagen, J.On April 30, 1970, RKO-Stanley W.pdf
RKO-Stanley v. GrazianoEagen, J.On April 30, 1970, RKO-Stanley W.pdf
 

Recently uploaded

Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 

Recently uploaded (20)

Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 

F# code, code below If i = 1 the branch jumps to line 2, so the pr.pdf

  • 1. F# code, code below If i = 1 the branch jumps to line 2, so the program prints 20, 21, 20, 21, 20, 21, . . . and does not terminate. If i = 2 the branch jumps to line 12, so the program prints 20, 21. The file: // Abstract syntax type branchId = int type expr = | Num of int // integer | Var of string // variable | Op of string * expr * expr // e1 op e2 type stmt = | Print of expr // print e; | Assign of string * expr // x = e; | Block of stmt list // block { stmt1; ...; stmtN } | Loop of stmt list // loop { stmt1; ...; stmtN } | Br of branchId // br i; | BrIf of expr * branchId // if (e) br i; // Two more examples // block { // x = 0 // loop { // print x; // x = x + 1; // if (x < n) br 0; // } // } let countTo n = Block [ Assign ("x", Num 0) Loop [ Print (Var "x") Assign ("x", Op ("+", Var "x", Num 1))
  • 2. BrIf (Op ("<", Var "x", Num n), 0) ] ] // block { // x = 10 // y = 0 // block { // loop { // if (x < 2) br 1; // x = x - 2; // y = y + 1; // br 0; // } // } // print y; // } let div2 = Block [ Assign ("x", Num 10) Assign ("y", Num 0) Block [ Loop [ BrIf (Op ("<", Var "x", Num 2), 1) Assign ("x", Op ("-", Var "x", Num 2)) Assign ("y", Op ("+", Var "y", Num 1)) Br 0 ] ] Print (Var "y") ] // Prettyprinter let paren b s = if b then "(" + s + ")" else s let rec prettyExpr (prec : int) (e : expr) : string = match e with
  • 3. | Var x -> x | Num i -> sprintf "%d" i | Op (op, e1, e2) -> let e1 = prettyExpr 1 e1 let e2 = prettyExpr 1 e2 sprintf "%s %s %s" e1 op e2 |> paren (0 < prec) let rec prettyStmtI (indent : string) (stmt : stmt) : string = let extraIndent = indent + " " match stmt with | Print e -> let e = prettyExpr 1 e sprintf "%sprint %s;" indent e | Assign (x, e) -> let e = prettyExpr 0 e sprintf "%s%s = %s;" indent x e | Block stmts -> let stmts = prettyStmtsI extraIndent stmts sprintf "%sblock {n%s%s}" indent stmts indent | Loop stmts -> let stmts = prettyStmtsI extraIndent stmts sprintf "%sloop {n%s%s}" indent stmts indent | Br i -> sprintf "%sbr %i;" indent i | BrIf (e, i) -> let e = prettyExpr 0 e sprintf "%sif (%s) br %i;" indent e i and prettyStmtsI (indent : string) (stmts : stmt list) : string = List.map (fun stmt -> prettyStmtI indent stmt + "n") stmts |> String.concat "" let prettyStmt (stmt : stmt) : string = prettyStmtI "" stmt // Execution of statements type naivestore = Map let emptystore : Map = Map.empty let getSto (store : naivestore) x = store.Item x let setSto (store : naivestore) (k, v) = store.Add(k, v)
  • 4. type result = | Continue of naivestore | Branch of branchId * naivestore let rec eval e (store : naivestore) : int = match e with | Num i -> i | Var x -> getSto store x | Op (op, e1, e2) -> let i1 = eval e1 store let i2 = eval e2 store match op with | "*" -> i1 * i2 | "+" -> i1 + i2 | "-" -> i1 - i2 | "==" -> if i1 = i2 then 1 else 0 | "<" -> if i1 < i2 then 1 else 0 | _ -> failwith "unknown primitive" let rec exec (stmt : stmt) (store : naivestore) : result = match stmt with | Print e -> let v = eval e store printfn "%d" v Continue store | Assign (x, e) -> let v = eval e store let store = setSto store (x, v) Continue store | Block stmts -> match execStmts stmts store with | Continue store -> Continue store | Branch (i, store) -> if i = 0 then Continue store
  • 5. else Branch (i - 1, store) | Loop stmts -> failwith "Not implemented" | Br i -> Branch (i, store) | BrIf (e, i) -> let v = eval e store if v = 0 then Continue store else Branch (i, store) and execStmts (stmts : stmt list) (store : naivestore) : result = match stmts with | [] -> Continue store | stmt::stmts -> match exec stmt store with | Continue store -> execStmts stmts store | Branch (i, store) -> Branch (i, store) let run (stmt : stmt) : unit = match exec stmt emptystore with | Continue _ -> () | Branch _ -> failwith "Invalid branch" // Make an if statement let makeIf (e : expr) (stmt1 : stmt) (stmt2 : stmt) : stmt = failwith "Not implemented" // Abstract syntax of a simple imperative language with if statements, // and a function that converts to the language above (assuming makeIf // is implemented correctly) type sstmt = | SPrint of expr // print e | SAssign of string * expr // x = e | SBlock of sstmt list // { stmt1; stmt2; ...; stmtN } | SIf of expr * sstmt * sstmt // if (e) stmt stmt let rec convert (stmt : sstmt) : stmt = match stmt with | SPrint e -> Print e
  • 6. | SAssign (x, e) -> Assign (x, e) | SBlock stmts -> Block (List.map convert stmts) | SIf (e, stmt1, stmt2) -> makeIf e (convert stmt1) (convert stmt2) Here are tests for the question: // block { // x = n; // y = 0; // loop { // z = x; // loop { // if (z < 1) br 2; // y = y + 1; // z = z - 1; // if (0 < z) br 0; // } // x = x - 1; // print y; // br 0; // } // print z; // } let triangle n = Block [ Assign ("x", Num n) Assign ("y", Num 0) Loop [ Assign ("z", Var "x") Loop [ BrIf (Op ("<", Var "z", Num 1), 2) Assign ("y", Op ("+", Var "y", Num 1)) Assign ("z", Op ("-", Var "z", Num 1)) BrIf (Op ("<", Num 0, Var "z"), 0) ] Assign ("x", Op ("-", Var "x", Num 1)) Print (Var "y")
  • 7. Br 0 ] Print (Var "z") ] // > run (countTo 0);; // 0 // val it: unit = () // > run (countTo 3);; // 0 // 1 // 2 // val it: unit = () // > run (countTo 8);; // 0 // 1 // 2 // 3 // 4 // 5 // 6 // 7 // val it: unit = () // > run div2;; // 5 // val it: unit = () // > run (triangle 2);; // 2 // 3 // val it: unit = () // > run (triangle 6);; // 6 // 11 // 15 // 18 // 20
  • 8. // 21 // val it: unit = () let s1 = SIf (Num 1, SPrint (Num 10), SPrint (Num 20)) let s2 = SIf (Num 0, SPrint (Num 10), SPrint (Num 20)) let s3 = SBlock [SAssign ("x", Num 10); SIf (Var "x", SPrint (Num 100), SBlock [])] let s4 n = SBlock [ SAssign ("x", Num n); SIf (Op ("<", Num 0, Var "x"), SBlock [ SPrint (Num 0); SIf (Op ("<", Num 5, Var "x"), SPrint (Num 5), SPrint (Var "x")) ], SBlock [ SAssign ("x", Num 100) SPrint (Var "x") ]) ] // The following test makeIf // > run (convert s1);; // 10 // val it: unit = () // > run (convert s2);; // 20 // val it: unit = () // > run (convert s3);; // 100 // val it: unit = () // > run (convert (s4 -1));; // 100 // val it: unit = () // > run (convert (s4 1));; // 0
  • 9. // 1 // val it: unit = () // > run (convert (s4 10));; // 0 // 5 // val it: unit = () Assignment4.fs contains a simple imperative language with WebAssembly-like control flow involving blocks and branch statements. The statement br i means branch to the i th, where blocks are counted from the innermost to the outermost. The 0th block is the block that directly contains the branch statement, and the (i+1) th block is the block that directly contains the i th block. There are two kinds of block: simple blocks (block in concrete syntax) and loops (loop in concrete syntax). They differ in how they behave as branch targets: - If the target of a branch is a simple block, then control jumps to the end of the block, so that the next statement executed is (lexically) after the block. - If the target of a branch is a loop, then control jumps to the beginning of the loop. At the end of a block of either kind, if there is no branch, then the next statement executed is the next one that appears lexically. For an example, consider what the branch in the following program will do when program is executed. 123456789101112block{loop{print20;block{block{print21;}bri;loop{print22;}}print23;}print24 ; (i) The function exec executes a statement, given a store. The result is Branch (i, sto) if execution should branch to the ith block, and Continue sto otherwise. In both cases, sto is the new store. Implement the Loop case of exec. You should use a recursive call to exec to handle a branch to the beginning of the loop. ii) There are no if statements except for conditional branches, but conditional branches are enough to implement if. Implement makeIf : expr stmt stmt stmt so that makeIf e stmt1 stmt2 is a statement that behaves like stmt1 if e is non-zero and stmt2 otherwise. Your function should not inspect e, stmt1 or stmt2, and it should not use Loop.