From: Marjolein Zwerver Date: Tue, 27 May 2014 20:29:26 +0000 (+0200) Subject: implementatie functies in Clean X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=c71637c42ec1874342ace3e61bd7b3f39188b3d1;p=sec1415.git implementatie functies in Clean --- diff --git a/appendix.tex b/appendix.tex new file mode 100644 index 0000000..3bacb77 --- /dev/null +++ b/appendix.tex @@ -0,0 +1 @@ +\lstinputlisting[language=Clean, lastline=100]{functions.icl} \ No newline at end of file diff --git a/functions.icl b/functions.icl new file mode 100644 index 0000000..8250323 --- /dev/null +++ b/functions.icl @@ -0,0 +1,122 @@ +implementation module roll + +import StdInt, StdList + +d :: [Int] -> [Int] +d [] = [] +d [a:rest] = rest + +e :: [Int] Int -> [Int] +e [] a = [a] +e [b] a = [a] ++ [b] + +add :: [Int] -> [Int] +add [] = [] +add [a] = [a] +add [a:b:rest] = [b+a:rest] + +sub :: [Int] -> [Int] +sub [] = [] +sub [a] = [a] +sub [a:b:rest] = [b-a:rest] + +mul :: [Int] -> [Int] +mul [] = [] +mul [a] = [a] +mul [a:b:rest] = [b*a:rest] + +div :: [Int] -> [Int] +div [] = [] +div [a] = [a] +div [a:b:rest] = [b/a:rest] + +modc :: [Int] -> [Int] +modc[] = [] +modc[a] = [a] +modc[a:b:rest] = [modulo b a:rest] + +dup :: [Int] -> [Int] +dup[] = [] +dup[a:rest] = [a:a:rest] + +notc :: [Int] -> [Int] +notc[] = [] +notc[0:rest] = [1:rest] +notc[_:rest] = [0:rest] + +gre :: [Int] -> [Int] +gre[] = [] +gre[a] = [a] +gre[a:b:rest] +|a >= b = [0:rest] +|otherwise = [1:rest] + +roll :: [Int] -> [Int] +roll [] = [] +roll [a] = [a] +roll [a:b:rest] +|modulo a b == 0 = rest +|b <= 0 = rest +|b > length rest = rest +|a == 1 = roll1 [b:rest] +|a > 0 = roll ([a-1] ++ [b] ++ roll1 [b:rest]) +|a < 0 = roll ([modulo a b] ++ [b:rest]) + +pointer :: [Int] -> [Int] +pointer [] = [] +pointer [a:rest] = rest + +//outnum_s :: stack_output stack -> stack_output +outnum_s :: [Int] [Int] -> [Int] +outnum_s _ [] = [] +outnum_s _ [a:rest] = rest + +outnum_so :: [Int] [Int] -> [Int] +outnum_so so [] = so +outnum_so so [a:rest] = add_to_s a so + +//innum_s :: stack_input stack -> stack +innum_s :: [Int] [Int] -> [Int] +innum_s [] _ = [] +innum_s [a:rest] s = add_to_s a s + +innum_si :: [Int] [Int] -> [Int] +innum_si [] s = [] +innum_si [a:rest] s = rest + +//help functions +roll1 :: [Int] -> [Int] +roll1 [1:rest] = rest +roll1 [b:c:d:rest] = [d] ++ roll1[b-1:c:rest] + +modulo :: Int Int -> Int +modulo a b +|a<0 = modulo (a+b) b +|a [a] +add_to_s a [] = [a] +add_to_s a b = b ++ [a] + +x = 5 +y = 1 +stack = [] +so = [] +si = [] + +Start = start51 + +start21 = (notc(gre(sub(push 1(push 1(notc(sub(push 1(roll(push 1(push 3(dup(roll(push 1(push 2[2,1]))))))))))))))) +start22 = roll(push 1(push 2(pop(roll(push 2(push 3(sub(push 1(roll(push 1(push 3(dup(roll(push 1(push 2(pop(roll(push 1(push 2(mul(roll(push 1(push 4(dup(roll(push 2(push 3(dup[1,2])))))))))))))))))))))))))))) +start23 = pointer(notc(gre(sub(push 1(push 1(notc(sub(push 1(roll(push 1(push 3(dup(roll(push 1(push 2(push 1 (push 2 stack))))))))))))))))) + +start51 = (notc(gre(sub(push 1(push 1(notc(sub(push 1(roll(push 1(push 3(dup(roll(push 1(push 2[120,1]))))))))))))))) +start52 = roll(push 1(push 2(pop(roll(push 2(push 3(sub(push 1(roll(push 1(push 3(dup(roll(push 1(push 2(pop(roll(push 1(push 2(mul(roll(push 1(push 4(dup(roll(push 2(push 3(dup[60,2])))))))))))))))))))))))))))) +start53 = pointer(notc(gre(sub(push 1(push 1(notc(sub(push 1(roll(push 1(push 3(dup(roll(push 1(push 2[60,2]))))))))))))))) +start54 = roll(push 1(push 2(pop(roll(push 2(push 3(sub(push 1(roll(push 1(push 3(dup(roll(push 1(push 2(pop(roll(push 1(push 2(mul(roll(push 1(push 4(dup(roll(push 2(push 3(dup[20,3])))))))))))))))))))))))))))) +start55 = pointer(notc(gre(sub(push 1(push 1(notc(sub(push 1(roll(push 1(push 3(dup(roll(push 1(push 2[20,3]))))))))))))))) +start56 = roll(push 1(push 2(pop(roll(push 2(push 3(sub(push 1(roll(push 1(push 3(dup(roll(push 1(push 2(pop(roll(push 1(push 2(mul(roll(push 1(push 4(dup(roll(push 2(push 3(dup[5,4])))))))))))))))))))))))))))) +start57 = pointer(notc(gre(sub(push 1(push 1(notc(sub(push 1(roll(push 1(push 3(dup(roll(push 1(push 2[5,4]))))))))))))))) +start58 = roll(push 1(push 2(pop(roll(push 2(push 3(sub(push 1(roll(push 1(push 3(dup(roll(push 1(push 2(pop(roll(push 1(push 2(mul(roll(push 1(push 4(dup(roll(push 2(push 3(dup[1,5])))))))))))))))))))))))))))) +start59 = pointer(notc(gre(sub(push 1(push 1(notc(sub(push 1(roll(push 1(push 3(dup(roll(push 1(push 2(push y (push x stack))))))))))))))))) diff --git a/project.tex b/project.tex index 1b0c600..85110fd 100644 --- a/project.tex +++ b/project.tex @@ -71,4 +71,7 @@ \section{Planning} \input{planning.tex} +\section{Appendix} +\input{appendix.tex} + \end{document} diff --git a/semantics.tex b/semantics.tex index 5ab8dc4..dfa4fc7 100644 --- a/semantics.tex +++ b/semantics.tex @@ -18,111 +18,101 @@ op een niet terminerende transitie.\\ \subsection{Semantiekbeschrijving} +Om de semantiekregels te kunnen beschrijven maken we gebruik van hulpfuncties. Hieronder staat het type van de functies en een korte beschrijving. +De implementatie van deze functies staat in de appendix bij het aangegeven regelnummer.\\ + +delete functie (d, r. 5):\\ Om het bovenste element van een stack weg te kunnen gooien gebruiken we de volgende functie:\\ $$\mathcal{D} : Stack \rightarrow Stack$$ -$$\mathcal{D} [\:] = [\:]$$ -$$\mathcal{D} [a:rest] = [rest] $$ Deze functie haalt het bovenste element van de stack en gooit deze weg.\\ +extend functie (e, r. 9):\\ Om een stack uit te kunnen breiden met een element gebruiken we de volgende functie:\\ $$\mathcal{E} : Stack\rightarrow (\mathbb{Z} \rightarrow Stack)$$\\ Deze functie neemt een stack en een integer en voegt de integer toe bovenop de stack.\\ +add functie (add, r. 13):\\ Om de bovenste twee elementen van een stack op te tellen gebruiken we de volgende functie:\\ $$\mathcal{ADD} : Stack \rightarrow Stack$$ -$$\mathcal{ADD} [\:] = [\:]$$ -$$\mathcal{ADD} [a] = [a] $$ -$$\mathcal{ADD} [a:b:rest] = [b+a:rest] $$\\ Deze functie haalt de bovenste twee waarden van de stack, telt ze bij elkaar op, en stopt het resultaat bovenop de stack.\\ +subtract functie (sub, r. 18)\\ Om de bovenste twee elementen van een stack van elkaar af te trekken gebruiken we de volgende functie:\\ $$\mathcal{SUB} : Stack \rightarrow Stack$$ -$$\mathcal{SUB} [\:] = [\:]$$ -$$\mathcal{SUB} [a] = [a] $$ -$$\mathcal{SUB} [a:b:rest] = [b-a:rest] $$\\ Deze functie haalt de bovenste twee elementen van de stack, trekt de bovenste -waarde van de tweede bovenste waarde af, en stopt het resultaat bovenop de stack. +waarde van de tweede bovenste waarde af, en stopt het resultaat bovenop de stack.\\ +multiply functie (mul, r. 23):\\ Om de bovenste twee elementen van een stack te vermenigvuldigen gebruiken we de volgende functie:\\ $$\mathcal{MUL} : Stack \rightarrow Stack$$ -$$\mathcal{MUL} [\:] = [\:]$$ -$$\mathcal{MUL} [a] = [a] $$ -$$\mathcal{MUL} [a:b:rest] = [b*a:rest] $$\\ Deze functie haalt de bovenste twee elementen van de stack, vermenigvuldigt ze, en stopt het resultaat bovenop de stack.\\ +division functie (div, r. 28):\\ Om de geheeltallige deling van de bovenste twee elementen van de stack bovenop de stack te krijgen gebruiken we de volgende functie:\\ $$\mathcal{DIV} : Stack \rightarrow Stack$$ -$$\mathcal{DIV} [\:] = [\:]$$ -$$\mathcal{DIV} [a] = [a] $$ -$$\mathcal{DIV} [a:b:rest] = [b/a:rest] $$\\ Deze functie haalt de bovenste twee elementen van de stack, berekent de geheeltallige deling van de tweede bovenste waarde door de bovenste waarde, en stopt het resultaat bovenop de stack.\\ -Om gebruiken we de -volgende functie:\\ +modulo functie (modc, r. 33):\\ +Om de tweede bovenste waarde modulo de bovenste waarde te berekenen +gebruiken we de volgende functie:\\ $$\mathcal{MOD} : Stack \rightarrow Stack$$ -$$\mathcal{MOD} [\:] = [\:]$$ -$$\mathcal{MOD} [a] = [a] $$ -$$\mathcal{MOD} [a:b:rest] = [b \: mod \: a:rest] $$\\ -Deze functie haalt de bovenste twee elementen van de stack, berekent het resultaat van de tweede bovenste -waarde modulo de bovenste waarde, en stopt dit bovenop de stack.\\ +Deze functie haalt de bovenste twee elementen van de stack, berekent het +resultaat van de tweede bovenste waarde modulo de bovenste waarde, en +stopt dit bovenop de stack.\\ +duplicate functie (dup, r. 38):\\ Om het bovenste element van een stack te dupliceren gebruiken we de volgende functie:\\ $$\mathcal{DUP} : Stack \rightarrow Stack$$ -$$\mathcal{DUP} [\:] = [\:]$$ -$$\mathcal{DUP} [a:rest] = [a:a:rest] $$\\ -Deze functie haalt stopt een kopie van de bovenste waarde bovenop de stack.\\ +Deze functie haalt de bovenste waarde van de stack en stopt een kopie +en de bovenste waarde bovenop de stack.\\ -Om de gebruiken we de +not functie (notc, r. 42):\\ +Om de not van de bovenste waarde te berekenen gebruiken we de volgende functie:\\ -$$\mathcal{NOT} : Stack \rightarrow Stack$$ -$$\mathcal{NOT} [\:] = [\:]$$ -$$\mathcal{NOT} [0:rest] = [1:rest]$$ -$$\mathcal{NOT} [\_:rest] = [0:rest] $$\\ - +$$\mathcal{NOT} : Stack \rightarrow Stack$$\\ Deze functie neemt het bovenste element van de stack en stopt 0 bovenaan de stack als deze waarde niet gelijk is aan 0 en 1 als deze waarde gelijk is aan 0.\\ -Om gebruiken we de -volgende functie:\\ -$$\mathcal{GRE} : Stack \rightarrow Stack$$ -$$\mathcal{GRE} [\:] = [\:]$$ -$$\mathcal{GRE} [a] = [a] $$ -$$\mathcal{GRE} [a:b:rest]$$ -$$\qquad \qquad | \: a \geq b = [0:rest]$$ -$$\qquad \qquad | \: a < b = [1:rest]$$\\ - -$$\mathcal{ROLL} : Stack \rightarrow Stack$$ -$$\mathcal{ROLL} [\:] = [\:]$$ -$$\mathcal{ROLL} [a] = [a] $$ -$$\mathcal{ROLL} [a:b:rest]$$ -$$\qquad \qquad | modulo \: a \: b == 0 = rest$$ -$$\qquad \qquad | b <= 0 = rest$$ -$$\qquad \qquad | b > length rest = rest$$ -$$\qquad \qquad | a == 1 = roll1 [b:rest]$$ -$$\qquad \qquad | a > 0 = roll([a-1] ++ [b] ++ roll1 [b:rest]$$ -$$\qquad \qquad | a < 0 = roll(modulo \: a \: b] ++ [b:rest])$$ - -$$\mathcal{ROLL}1 : Stack \rightarrow Stack$$ -$$\mathcal{ROLL}1 [1:rest] = [rest]$$ -$$\mathcal{ROLL}1 [b:c:d:rest] = [d] ++ \mathcal{ROLL}1 [b-1:c:rest]$$ - - +greater functie (gre, r. 47):\\ +Om kijken of de tweede bovenste waarde groter is dan de bovenste waarde +gebruiken we de volgende functie:\\ +$$\mathcal{GRE} : Stack \rightarrow Stack$$\\ +Deze functie haalt de bovenste twee waarden van de stack en stopt een 1 bovenop +de stack als de tweede bovenste waarde groter is dan de bovenste waarde. Anders +word een 0 bovenop de stack gestopt.\\ + +roll functie (roll, r. 54):\\ +Om de stack te rollen gebruiken we de volgende functie:\\ +$$\mathcal{ROLL} : Stack \rightarrow Stack$$\\ Deze functie haalt de bovenste twee elementen van de stack en stopt 1 bovenop de stack als het tweede bovenste element een hogere waarde heeft als dat van het bovenste element. Anders word 1 bovenop de stack gestopt.\\ +pointer functie (pointer, r. 65):\\ + +out number functie (outnum, r. 74):\\ + +in number functie (innum, r. 83):\\ + + + + + +(switch, outchar, inchar)\\ + + Met behulp van deze gedefinieerde functies kunnen we nu de volgende semantiekregels opstellen:\\ $ @@ -153,22 +143,28 @@ $ {[greater_{sos}]} \qquad \langle greater, s_i, s_o, s\rangle \Rightarrow \langle s_i, s_o, \mathcal{GRE}(s)\rangle\\ +{[pointer_{sos}]} \qquad +\langle pointer, s_i, s_o, s\rangle \Rightarrow +\langle s_i, s_o, \mathcal{D}(s)\rangle\\ {[push_{sos}]} \qquad \langle push \: n, s_i, s_o, s\rangle \Rightarrow \langle s_i, s_o, \mathcal{E}(n,s)\rangle\\ {[roll_{sos}]} \qquad \langle roll, s_i, s_o, s\rangle \Rightarrow \langle s_i, s_o, \mathcal{ROLL}(s)\rangle\\ -{[inchar_{sos}]} \qquad -\langle inchar, s_i, s_o, s\rangle \Rightarrow -\langle s_i, s_o, s\rangle\\ {[innum_{sos}]} \qquad \langle innum, s_i, s_o, s\rangle \Rightarrow +\langle \mathcal{INN_{SI}}(s_i), s_o, \mathcal{INN_S}(s)\rangle\\ +{[outnum_{sos}]} \qquad +\langle outnum, s_i, s_o, s\rangle \Rightarrow +\langle s_i, \mathcal{OUTN_{SO}}(s_o),\mathcal{OUTN_S}(s)\rangle\\ +{[inchar_{sos}]} \qquad +\langle inchar, s_i, s_o, s\rangle \Rightarrow \langle s_i, s_o, s\rangle\\ {[outchar_{sos}]} \qquad \langle outchar, s_i, s_o, s\rangle \Rightarrow \langle s_i, s_o, s\rangle\\ -{[outnum_{sos}]} \qquad -\langle outnum, s_i, s_o, s\rangle \Rightarrow +{[switch_{sos}]} \qquad +\langle switch, s_i, s_o, s\rangle \Rightarrow \langle s_i, s_o, s\rangle\\ $