davidbejenariu2

LAB 9 FLP

Apr 13th, 2022
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.15 KB | None | 0 0
  1.  
  2. # Laboratorul 9 Prolog
  3. ## Implementarea IMP - semantica operationala
  4.  
  5. **Sarcini laborator**
  6. - completati toate sectiunile marcate cu [de lucrat];
  7. - trimiteti solutiile pe mail, pe *[email protected]* pana la finalul acestui laborator;
  8. - subiectul mailului va fi **Grupa, Nume, Prenume, Solutii FLP Lab9**
  9. - laboratorul este notat, si va face parte din punctajul bonus
  10.  
  11. In acest laborator vom implementa un limbaj care contine:
  12. - expresii *aritmetice* si *booleene*;
  13. + `x + 3`
  14. + `x >= 7`
  15. - instructiuni *de atribuire*, *conditionale* si *de ciclare*;
  16. + `x = 5`
  17. + `if(x >= 7, x = 5, x = 0)`
  18. + `while (x >= 7, x = x - 1)`
  19. - compunerea instructiunilor;
  20. + `x = 7; while(x >= 0; x = x - 1)`
  21. - blocuri de instructiuni.
  22. + `{x = 7; while(x >= 0, x = x - 1)}`
  23.  
  24. Un exemplu de program in IMP este
  25.  
  26. ```prolog
  27. {
  28. x = 10;
  29. sum = 0;
  30. while (0 =< x,
  31. {
  32. sum = sum + x;
  33. x = x - 1
  34. })
  35. },
  36. sum
  37. ```
  38.  
  39. ### 9.1. Definitia limbajului (BNF)
  40.  
  41. ```prolog
  42. E ::= n | x
  43. | E + E | E - E | E * E
  44.  
  45. B ::= true | false
  46. | E =< E | E >= E | E == E
  47. | not(B) | and(B, B) | or(B, B)
  48.  
  49. C ::= skip
  50. | x = E
  51. | if(B, C, C)
  52. | while(B, C)
  53. | { C }
  54. | C ; C
  55.  
  56. P ::= { C }, E
  57. ```
  58.  
  59. ### 9.2. Implementarea limbajului in Prolog
  60.  
  61. Avem urmatorii doi operatori:
  62. ```prolog
  63. :- op(100, xf, {}).
  64. :- op(1100, yf, ;).
  65. ```
  66.  
  67. Definim un predicat pentru fiecare neterminal din descrierea BNF de mai sus. Vom avea:
  68. - `aexp/1` pentru expresii aritmetice;
  69. - `bexp/1` pentru expresii booleene;
  70. - `stmt/1` pentru instructiunile propriu-zise;
  71. - `program/1` pentru structura programului.
  72.  
  73. #### 9.2.1. Implementarea `aexp/1`
  74.  
  75. ```prolog
  76. E ::= n | x | E + E | E - E | E * E
  77. ```
  78.  
  79. ```prolog
  80. aexp(I) :- integer(I). % intregii sunt cei din Prolog
  81. aexp(X) :- atom(X). % identificatorii din IMP sunt atomii
  82. aexp(A1 * A2) :- aexp(A1), aexp(A2).
  83. aexp(A1 + A2) :- aexp(A1), aexp(A2).
  84. aexp(A1 - A2) :- aexp(A1), aexp(A2).
  85. ```
  86.  
  87. #### 9.2.2. Implementarea `bexp/1`
  88.  
  89. ```prolog
  90. B ::= true | false
  91. | E =< E | E >= E | E == E
  92. | not(B) | and(B, B) | or(B, B)
  93. ```
  94. ```prolog
  95. bexp(true).
  96. bexp(false).
  97. bexp(A1 =< A2) :- aexp(A1), aexp(A2).
  98. bexp(A1 >= A2) :- aexp(A1), aexp(A2).
  99. bexp(A1 == A2) :- aexp(A1), aexp(A2).
  100. bexp(and(BE1,BE2)) :- bexp(BE1), bexp(BE2).
  101. bexp(or(BE1,BE2)) :- bexp(BE1), bexp(BE2).
  102. bexp(not(BE)) :- bexp(BE).
  103. ```
  104.  
  105. #### 9.2.3. Implementarea `stmt/1`
  106.  
  107. ```prolog
  108. C ::= skip
  109. | x = E
  110. | if(B, C, C)
  111. | while(B, C)
  112. | { C }
  113. | C ; C
  114. ```
  115.  
  116. ```prolog
  117. stmt(skip).
  118. stmt(X = AE) :- atom(X), aexp(AE).
  119. stmt(if(BE,St1,St2)) :- bexp(BE), stmt(St1), stmt(St2).
  120. stmt(while(BE,St)) :- bexp(BE), stmt(St).
  121. stmt(St1;St2) :- stmt(St1), stmt(St2).
  122. stmt({St}) :- stmt(St).
  123. ```
  124.  
  125. #### 9.2.4. Implementarea `program/1`
  126.  
  127. ```prolog
  128. P ::= { C }, E
  129. ```
  130.  
  131. ```prolog
  132. program(St,AE) :-
  133. stmt(St),
  134. aexp(AE).
  135. ```
  136.  
  137. Urmatorul predicat trebuie sa raspunda `true`:
  138.  
  139. ```prolog
  140. test0 :- program({x = 10; sum = 0; while(0 =< x, {sum = sum + x; x = x-1)}}, sum).
  141. ```
  142.  
  143. `?- test0.` => `true`
  144.  
  145. ### 9.3. Semantica operationala
  146.  
  147. - descrie cum se executa un program pe o masina abstracta;
  148. - semantica **small-step** descrie cum avanseaza o executie in functie de reduceri succesive. Avem mereu un cuplu (cod, starea memoriei), notat in general $<cod, \sigma>$, iar o tranzitie este $<cod, \sigma> \to <cod', \sigma'>$
  149.  
  150. Vom defini care sunt regulile structurale pentru toate neterminalele din programul nostru, dand astfel o semantica a programului.
  151.  
  152. Avem deja implementate predicatele auxiliare de mai jos:
  153.  
  154. ```prolog
  155. % get ne da informatii despre starea memoriei
  156. % vi/2 este o pereche variabila - valoare
  157. % de exemplu, putem avea S = [vi(X, 0), vi(Y, 1)] care spune
  158. % ca X |-> 0 si Y |-> 1 in stadiul curent
  159. % apelam get(S, X, -I) - dam starea memoriei, o variabila, si in I gasim valoarea
  160. get(S,X,I) :- member(vi(X,I),S).
  161. get(_,_,0).
  162.  
  163. % elimina X = I din instructiunile curente si adauga vi(X, I) in starea memoriei
  164. set(S,X,I,[vi(X,I)|S1]) :- del(S,X,S1).
  165. del([vi(X,_)|S],X,S).
  166. del([H|S],X,[H|S1]) :- del(S,X,S1) .
  167. del([],_,[]).
  168. ```
  169.  
  170. #### 9.3.1. Regula ID
  171.  
  172. $$\text{(ID)} <x, \sigma> \to <i, \sigma>$$
  173. daca $i = \sigma(x)$
  174.  
  175. ```prolog
  176. smallstepA(X, S, I, S) :-
  177. atom(X),
  178. get(S, X, I).
  179. ```
  180.  
  181. #### 9.3.2. Regula ADD
  182.  
  183. $$\text{(ADD1)} <i_1 + i_2, \sigma> \to <i, \sigma>$$ daca $i = i_1 + i_2$
  184.  
  185. ```prolog
  186. smallstepA(I1 + I2, S, I, S) :-
  187. integer(I1), integer(I2),
  188. I is I1 + I2.
  189. ```
  190.  
  191. $$\text{(ADD2)} \frac{<a_1, \sigma> \to <a_1', \sigma>}{<a_1 + a_2, \sigma> \to <a_1' + a_2, \sigma>}$$
  192.  
  193. ```prolog
  194. smallstepA(AE1 + I, S, AE2 + I, S) :-
  195. integer(I),
  196. smallstepA(AE1, S, AE2, S).
  197. ```
  198.  
  199. $$\text{(ADD3)} \frac{<a_2, \sigma> \to <a_2', \sigma>}{<a_1 + a_2, \sigma> \to <a_1 + a_2', \sigma>}$$
  200.  
  201. ```prolog
  202. smallstepA(I + AE1,S,I + AE2,S) :-
  203. integer(I),
  204. smallstepA(AE1, S, AE2, S).
  205. ```
  206.  
  207. #### 9.3.3. Regula DIFF [de lucrat]
  208.  
  209. $$\text{(DIFF1)} <i_1 - i_2, \sigma> \to <i, \sigma>$$ daca $i = i_1 - i_2$
  210. ```prolog
  211. smallstepA(I1 - I2, S, I, S) :-
  212. integer(I1), integer(I2),
  213. I is I1 - I2.
  214. ```
  215.  
  216. $$\text{(DIFF2)} \frac{<a_1, \sigma> \to <a_2, \sigma>}{<i - a_1, \sigma> \to <i - a_2, \sigma>}$$
  217. ```prolog
  218. smallstepA(I - AE1, S, I - AE2, S) :-
  219. integer(I),
  220. smallstepA(AE1, S, AE2, S).
  221. ```
  222.  
  223. $$\text{(DIFF3)} \frac{<a_1, \sigma> \to <a_2, \sigma>}{<a_1 - i, \sigma> \to <a_2 - i, \sigma>}$$
  224. ```prolog
  225. smallstepA(AE1 - I, S, AE2 - I, S) :-
  226. integer(I),
  227. smallstepA(AE1, S, AE2, S).
  228. ```
  229.  
  230. #### 9.3.4. Regula MUL [de lucrat]
  231.  
  232. $$\text{(MUL1)} <i_1 * i_2, \sigma> \to <i, \sigma>$$ daca $i = i_1 * i_2$
  233. ```prolog
  234. smallstepA(I1 * I2, S, I, S) :-
  235. integer(I1), integer(I2),
  236. I is I1 * I2.
  237. ```
  238.  
  239. $$\text{(MUL2)} \frac{<a_1, \sigma> \to <a_1', \sigma>}{<a_1 * a_2, \sigma> \to <a_1' * a_2, \sigma>}$$
  240. ```prolog
  241. smallstepA(AE1 * I, S, AE2 * I, S) :-
  242. integer(I),
  243. smallstepA(AE1, S, AE2, S).
  244. ```
  245.  
  246. $$\text{(MUL2)} \frac{<a_2, \sigma> \to <a_2', \sigma>}{<a_1 * a_2, \sigma> \to <a_1 * a_2', \sigma>}$$
  247. ```prolog
  248. smallstepA(I * AE1, S, I * AE2, S) :-
  249. integer(I),
  250. smallstepA(AE1, S, AE2, S).
  251. ```
  252.  
  253. #### 9.3.5. Reguli de comparatie LEQ
  254.  
  255. $$\text{(LEQ-FALSE)} <i_1 =< i_2, \sigma> \to<\text{false}, \sigma>$$ daca $i_1 > i_2$
  256.  
  257. ```prolog
  258. smallstepB(I1 =< I2, S, false, S) :-
  259. integer(I1), integer(I2),
  260. I1 > I2.
  261. ```
  262.  
  263. $$\text{(LEQ-TRUE)} <i_1 =< i_2, \sigma> \to<\text{true}, \sigma>$$ daca $i_1 \leq i_2$
  264.  
  265. ```prolog
  266. smallstepB(I1 =< I2, S, true, S):-
  267. integer(I1), integer(I2),
  268. I1 =< I2.
  269. ```
  270.  
  271. $$\text{(LEQ1)} \frac{<a_1, \sigma> \to <a_1', \sigma>}{<a_1 =< a_2, \sigma> \to <a_1' =< a_2, \sigma>}$$
  272.  
  273. ```prolog
  274. smallstepB(AE1 =< I, S, AE2 =< I, S) :-
  275. integer(I),
  276. smallstepA(AE1, S, AE2, S).
  277. ```
  278.  
  279. $$\text{(LEQ2)} \frac{<a_2, \sigma> \to <a_2', \sigma>}{<a_1 =< a_2, \sigma> \to <a_1 =< a_2', \sigma>}$$
  280.  
  281. ```prolog
  282. smallstepB(I =< AE1, S, I =< AE2, S) :-
  283. integer(I),
  284. smallstepA(AE1, S, AE2, S).
  285. ```
  286.  
  287. #### 9.3.6. Reguli de comparatie GEQ [de lucrat]
  288.  
  289. $$\text{(GEQ-FALSE)} <i_1 >= i_2, \sigma> \to<\text{false}, \sigma>$$ daca $i_1 < i_2$
  290.  
  291. ```prolog
  292. smallstepB(I1 >= I2, S, false, S) :-
  293. integer(I1), integer(I2),
  294. I1 < I2.
  295. ```
  296.  
  297. $$\text{(GEQ-TRUE)} <i_1 >= i_2, \sigma> \to<\text{true}, \sigma>$$ daca $i_1 \geq i_2$
  298.  
  299. ```prolog
  300. smallstepB(I1 >= I2, S, true, S):-
  301. integer(I1), integer(I2),
  302. I1 >= I2.
  303. ```
  304.  
  305. $$\text{(GEQ1)} \frac{<a_1, \sigma> \to <a_1', \sigma>}{<a_1 >= a_2, \sigma> \to <a_1' >= a_2, \sigma>}$$
  306.  
  307. ```prolog
  308. smallstepB(AE1 >= I, S, AE2 >= I, S) :-
  309. integer(I),
  310. smallstepA(AE1, S, AE2, S).
  311. ```
  312.  
  313. $$\text{(GEQ2)} \frac{<a_2, \sigma> \to <a_2', \sigma>}{<a_1 >= a_2, \sigma> \to <a_1 >= a_2', \sigma>}$$
  314.  
  315. ```prolog
  316. smallstepB(I >= AE1, S, I >= AE2, S) :-
  317. integer(I),
  318. smallstepA(AE1, S, AE2, S).
  319. ```
  320.  
  321. #### 9.3.7. Reguli de egalitate EQ [de lucrat]
  322.  
  323. $$\text{(EQ-FALSE)} <i_1 == i_2, \sigma> \to<\text{false}, \sigma>$$ daca $i_1 \not = i_2$
  324.  
  325. ```prolog
  326. smallstepB(I1 == I2, S, false, S) :-
  327. integer(I1), integer(I2),
  328. I1 \= I2.
  329. ```
  330.  
  331. $$\text{(EQ-TRUE)} <i_1 == i_2, \sigma> \to<\text{true}, \sigma>$$ daca $i_1 = i_2$
  332.  
  333. ```prolog
  334. smallstepB(I1 == I2, S, true, S):-
  335. integer(I1), integer(I2),
  336. I1 = I2.
  337. ```
  338.  
  339. $$\text{(EQ1)} \frac{<a_1, \sigma> \to <a_1', \sigma>}{<a_1 == a_2, \sigma> \to <a_1' == a_2, \sigma>}$$
  340.  
  341. ```prolog
  342. smallstepB(AE1 == I, S, AE2 == I, S) :-
  343. integer(I),
  344. smallstepA(AE1, S, AE2, S).
  345. ```
  346.  
  347. $$\text{(EQ2)} \frac{<a_2, \sigma> \to <a_2', \sigma>}{<a_1 == a_2, \sigma> \to <a_1 == a_2', \sigma>}$$
  348.  
  349. ```prolog
  350. smallstepB(I == AE1, S, I == AE2, S) :-
  351. integer(I),
  352. smallstepA(AE1, S, AE2, S).
  353. ```
  354.  
  355. #### 9.3.8. Reguli pentru conjunctie
  356.  
  357. $$\text{(AND1)} <and(\text{true}, BE_2), \sigma> \to <BE_2, \sigma>$$
  358.  
  359. ```prolog
  360. smallstepB(and(true, BE2), S, BE2, S).
  361. ```
  362.  
  363. $$\text{(AND2)} <and(\text{false}, \_), \sigma> \to <\text{false}, \sigma>$$
  364.  
  365. ```prolog
  366. smallstepB(and(false,_), S, false, S).
  367. ```
  368.  
  369. $$\text{(AND3)} \frac{<BE_1, \sigma> \to <BE_2, \sigma>}{<and(BE_1, BE), \sigma> \to <and(BE_2, BE), \sigma>}$$
  370.  
  371. ```prolog
  372. smallstepB(and(BE1, BE),S,and(BE2, BE),S) :-
  373. smallstepB(BE1, S, BE2, S).
  374. ```
  375.  
  376. #### 9.3.9. Reguli pentru disjunctie [de lucrat]
  377.  
  378. $$\text{(OR1)} <or(\text{true}, \_), \sigma> \to <true, \sigma>$$
  379.  
  380. ```prolog
  381. smallstepB(or(true, _), S, true, S).
  382. ```
  383.  
  384. $$\text{(OR2)} <or(\text{false}, BE_2), \sigma> \to <BE_2, \sigma>$$
  385.  
  386. ```prolog
  387. smallstepB(or(false, BE2), S, BE_2, S).
  388. ```
  389.  
  390. $$\text{(OR3)} \frac{<BE_1, \sigma> \to <BE_2, \sigma>}{<or(BE_1, BE), \sigma> \to <or(BE_2, BE), \sigma>}$$
  391.  
  392. ```prolog
  393. smallstepB(or(BE1, BE), S, or(BE2, BE), S) :-
  394. smallstepB(BE1, S, BE2, S).
  395. ```
  396.  
  397. #### 9.3.10. Regulile pentru negatie
  398.  
  399. $$\text{!-TRUE} <not(\text{true}), \sigma> \to<\text{false}, \sigma>$$
  400.  
  401. ```prolog
  402. smallstepB(not(true),S,false,S) .
  403. ```
  404.  
  405.  
  406. $$\text{!-FALSE} <not(\text{false}), \sigma> \to<\text{true}, \sigma>$$
  407.  
  408. ```prolog
  409. smallstepB(not(false),S,true,S) .
  410. ```
  411.  
  412. $$\text{(NEG)} \frac{<a, \sigma> \to <a', \sigma>}{<not(a), \sigma> \to <not(a'), \sigma>}$$
  413.  
  414. ```prolog
  415. smallstepB(not(BE1),S,not(BE2),S) :-
  416. smallstepB(BE1,S,BE2,S).
  417. ```
  418.  
  419. #### 9.3.11. Regula ASGN
  420.  
  421. $$\text{(ASGN1)} <x = i, \sigma> \to <\text{skip}, \sigma'>$$ daca $\sigma' = \sigma[i/x]$
  422.  
  423. ```prolog
  424. smallstepS(X = AE, S, skip, S1) :-
  425. integer(AE), set(S, X, AE, S1) .
  426. ```
  427.  
  428. $$\text{(ASGN2)} \frac{< e_1, \sigma > \to < e_2, \sigma > }{< x = e_1, \sigma> \to <x = e_2, \sigma>}$$
  429.  
  430. ```prolog
  431. smallstepS(X = AE1, S, X = AE2, S) :-
  432. smallstepA(AE1, S, AE2, S).
  433. ```
  434.  
  435. #### 9.3.12. Regula NEXT-STMT
  436.  
  437. $$\text{(NEXT-STMT1)} <\text{skip}; s_2 , \sigma> \to <s_2, \sigma>$$
  438.  
  439. ```prolog
  440. smallstepS((skip;St2), S, St2, S).
  441. ```
  442.  
  443. $$\text{(NEXT-STMT2)} \frac{<s_1, \sigma> \to <s_1', \sigma'>}{<s_1 ; s_2 , \sigma> \to <s_1' ; s_2, \sigma>}$$
  444.  
  445. ```prolog
  446. smallstepS((St1;St),S1,(St2;St),S2) :-
  447. smallstepS(St1,S1,St2,S2) .
  448. ```
  449.  
  450. #### 9.3.13. Regula pentru blocuri de cod
  451.  
  452. $$\text{(BLOCK)} <\{E\}, \sigma> \to <E, \sigma>$$
  453.  
  454. ```prolog
  455. smallstepS({E}, S, E, S).
  456. ```
  457.  
  458. #### 9.3.14. Reguli pentru IF
  459.  
  460. $$\text{(IF-TRUE)} <if(\text{true}, St_1, \_), \sigma> \to <St_1, \sigma>$$
  461.  
  462. ```prolog
  463. smallstepS(if(true, St1, _), S, St1, S).
  464. ```
  465.  
  466. $$\text{(IF-FALSE)} <if(\text{false}, \_, St_2), \sigma> \to <St_2, \sigma>$$
  467.  
  468. ```prolog
  469. smallstepS(if(false, _, St2), S, St2, S).
  470. ```
  471.  
  472. $$\text{(IF)} \frac{<BE_1, \sigma> \to <BE_2, \sigma>}{<if(BE_1, St_1, St_2), \sigma> \to <if(BE_2, St_1, St_2), \sigma>}$$
  473.  
  474. ```prolog
  475. smallstepS(if(BE1,St1,St2),S,if(BE2,St1,St2),S) :-
  476. smallstepB(BE1, S, BE2, S).
  477. ```
  478.  
  479. #### 9.3.15. Regula pentru WHILE
  480.  
  481. $$\text{(WHILE)} <while(BE, St), \sigma> \\ \to <if(BE, (St; while(BE, St)), \text{skip}), \sigma>$$
  482.  
  483. ```prolog
  484. smallstepS(while(BE, St), S, if(BE, (St; while(BE, St)), skip), S).
  485. ```
  486.  
  487. #### 9.3.16. Reguli pentru programe
  488.  
  489. ```prolog
  490. smallstepP(skip, AE1, S1, skip, AE2, S2) :-
  491. smallstepA(AE1, S1, AE2, S2).
  492.  
  493. smallstepP(St1, AE, S1, ST2, AE, S2) :-
  494. smallstepS(St1, S1, St2, S2).
  495. ```
  496.  
  497. ```prolog
  498. run(skip, I, _, I) :- integer(I).
  499. run(St1, AE1, S1, I) :-
  500. smallstepP(St1, AE1, S1, ST2, AE2, S2),
  501. run(St2, AE2, S2, I).
  502. ```
  503.  
  504. ```prolog
  505. run_program(Name) :- defpg(Name, {P}, E),
  506. run(P, E, [], I),
  507. write(I).
  508. ```
  509.  
  510. ```prolog
  511. defpg(pg1, {nr = 0; while(nr =< 10, nr=nr+1)}, nr).
  512. ```
  513.  
  514.  
Add Comment
Please, Sign In to add comment