X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;f=weblib%2FWhile%2Fsemantics.ma;h=ee4362b71ccd197ce70a660c9f8c9b95a27b0d39;hb=84b38ac86f1f92b91ae8913cd0dbcb5c3485dc3a;hp=ec11cf2ec002457254c6a2094ce13bbd0a1a5155;hpb=e0671ce496760cec78ec904747af76ed55fd03a2;p=helm.git diff --git a/weblib/While/semantics.ma b/weblib/While/semantics.ma index ec11cf2ec..ee4362b71 100644 --- a/weblib/While/semantics.ma +++ b/weblib/While/semantics.ma @@ -1,53 +1,52 @@ (* new script *) include "While/syntax.ma". -include "arithmetics/div_and_mod.ma. - +include "arithmetics/div_and_mod.ma". (* state *) -definition state ≝ a href="cic:/matita/While/syntax/Var.ind(1,0,0)"Var/a → a href="cic:/matita/arithmetics/nat/nat.ind(1,0,0)"nat/a. +definition state ≝ Var → nat. -(* Semantics of Arithmetic expressions *) +definition eqvar ≝ λv1,v2. + match v1 with + [Id n => match v2 with [Id m => eqb n m]]. -pre class="smallmargin" style="display: inline;"let rec eval_Aexp a s ≝ +definition update : state → Var → nat → state ≝ λs,v,a,v1. + if eqvar v1 v then a else s v1. + +(* Semantics of Arithmetic expressions *) +pre class="smallmargin" style="display: inline;"let rec evalA a s ≝ match a with - | Const n => n + [ Const n => n | Aid v => s v - | Add a b => a+b - | Sub a b => a-b - | Mul a b => a*b - | Div a b => div a b - | Mod a b => mod a b + | Add a b => evalA a s+evalA b s + | Sub a b => evalA a s-evalA b s + | Mul a b => evalA a s*evalA b s + | Div a b => div (evalA a s) (evalA b s) + | Mod a b => mod (evalA a s) (evalA b s) + ] . -interpretation "Aexp plus" 'plus x y = (Add x y). -interpretation "Aexp minus" 'plus x y = (Sub x y). -interpretation "Aexp times" 'times x y = (Mul x y). - -(* Boolean expressions *) -inductive Bexp : Type[0] ≝ - | BFalse : Bexp - | BTtrue : Bexp - | BNot : Bexp → Bexp - | BAnd : Bexp → Bexp → Bexp - | BOr : Bexp → Bexp → Bexp - | Eq : a href="cic:/matita/While/syntax/Aexp.ind(1,0,0)"Aexp/a → a href="cic:/matita/While/syntax/Aexp.ind(1,0,0)"Aexp/a → Bexp - | LE : a href="cic:/matita/While/syntax/Aexp.ind(1,0,0)"Aexp/a → a href="cic:/matita/While/syntax/Aexp.ind(1,0,0)"Aexp/a → Bexp -. - -interpretation "Bexp not" 'not x = (BNot x). -interpretation "Bexp and" 'and x y = (BAnd x y). -interpretation "Bexp or" 'or x y = (BOr x y). -interpretation "Bexp 'less or equal to'" 'leq x y = (LE x y). - -(* Commands *) -inductive Cmd : Type[0] ≝ - | Skip : Cmd - | Assign : Var → a href="cic:/matita/While/syntax/Aexp.ind(1,0,0)"Aexp/a → Cmd - | Seq : Cmd → Cmd → Cmd - | If : a href="cic:/matita/While/syntax/Bexp.ind(1,0,0)"Bexp/a → Cmd → Cmd → Cmd - | While : a href="cic:/matita/While/syntax/Bexp.ind(1,0,0)"Bexp/a → Cmd → Cmd -. - -/pre \ No newline at end of file +example exA1: evalA ((Const 2)+(Const 3)*(Aid (Id 2))) (λx.span style="text-decoration: underline;"1/span) = span style="text-decoration: underline;"5/span. +normalize // qed + +let rec evalB b s ≝ + match b with + [ BFalse => false + | BTtrue => true + | BNot b => ¬(evalB b s) + | BAnd b1 b2 => evalB b1 s ∧ evalB b2 s + | BOr b1 b2 => evalB b1 s ∨ evalB b2 s pre class="smallmargin" style="display: inline;"/pre + | Eq a1 a2 => eqb (evalA a1 s) (evalA a2 s) + | LE a1 a2 => leb (evalA a1 s) (evalA a2 s) + ]. + +inductive Cmd_sem: Cmd → state → statepre class="smallmargin" style="display: inline;"→ Prop/pre≝ + | Skip_sem : \forall s. Cmd_sem Skip s s + | Assign_sem : \forall s,v,a. Cmd_sem (Assign v a) s (update s v (evalA a s)) + | Seq_sem : \forall c1,c2,s1,s2,s3. Cmd_sem c1 s1 s2 \to Cmd_sem c2 s2 s3 \to Cmd_sem (Seq c1 c2) s1 s3 + | If_sem_true : \forall b,ct,cf,s,st. evalB b s = true \to Cmd_sem ct s st \to Cmd_sem (If b ct cf) s st + | If_sem_false : \forall b,ct,cf,s,sf. evalB b s = false \to Cmd_sem cf s sf \to Cmd_sem (If b ct cf) s sf + | While_true : \forall b,c,s,s1,s2. evalB b s = true \to Cmd_sem c s s1 \to Cmd_sem (While b c) s1 s2 \to Cmd_sem (While b c) s s2 + | While_false : \forall b,c,s. evalB b s = false \to Cmd_sem (While b c) s s +./pre \ No newline at end of file