src/HOL/IMP/ACom.thy
changeset 53015 a1119cf551e8
parent 52046 bc01725d7918
child 54941 6d99745afe34
equal deleted inserted replaced
53009:bb18eed53ed6 53015:a1119cf551e8
    17 
    17 
    18 text_raw{*\snip{stripdef}{1}{1}{% *}
    18 text_raw{*\snip{stripdef}{1}{1}{% *}
    19 fun strip :: "'a acom \<Rightarrow> com" where
    19 fun strip :: "'a acom \<Rightarrow> com" where
    20 "strip (SKIP {P}) = com.SKIP" |
    20 "strip (SKIP {P}) = com.SKIP" |
    21 "strip (x ::= e {P}) = x ::= e" |
    21 "strip (x ::= e {P}) = x ::= e" |
    22 "strip (C\<^isub>1;;C\<^isub>2) = strip C\<^isub>1;; strip C\<^isub>2" |
    22 "strip (C\<^sub>1;;C\<^sub>2) = strip C\<^sub>1;; strip C\<^sub>2" |
    23 "strip (IF b THEN {P\<^isub>1} C\<^isub>1 ELSE {P\<^isub>2} C\<^isub>2 {P}) =
    23 "strip (IF b THEN {P\<^sub>1} C\<^sub>1 ELSE {P\<^sub>2} C\<^sub>2 {P}) =
    24   IF b THEN strip C\<^isub>1 ELSE strip C\<^isub>2" |
    24   IF b THEN strip C\<^sub>1 ELSE strip C\<^sub>2" |
    25 "strip ({I} WHILE b DO {P} C {Q}) = WHILE b DO strip C"
    25 "strip ({I} WHILE b DO {P} C {Q}) = WHILE b DO strip C"
    26 text_raw{*}%endsnip*}
    26 text_raw{*}%endsnip*}
    27 
    27 
    28 text_raw{*\snip{asizedef}{1}{1}{% *}
    28 text_raw{*\snip{asizedef}{1}{1}{% *}
    29 fun asize :: "com \<Rightarrow> nat" where
    29 fun asize :: "com \<Rightarrow> nat" where
    30 "asize com.SKIP = 1" |
    30 "asize com.SKIP = 1" |
    31 "asize (x ::= e) = 1" |
    31 "asize (x ::= e) = 1" |
    32 "asize (C\<^isub>1;;C\<^isub>2) = asize C\<^isub>1 + asize C\<^isub>2" |
    32 "asize (C\<^sub>1;;C\<^sub>2) = asize C\<^sub>1 + asize C\<^sub>2" |
    33 "asize (IF b THEN C\<^isub>1 ELSE C\<^isub>2) = asize C\<^isub>1 + asize C\<^isub>2 + 3" |
    33 "asize (IF b THEN C\<^sub>1 ELSE C\<^sub>2) = asize C\<^sub>1 + asize C\<^sub>2 + 3" |
    34 "asize (WHILE b DO C) = asize C + 3"
    34 "asize (WHILE b DO C) = asize C + 3"
    35 text_raw{*}%endsnip*}
    35 text_raw{*}%endsnip*}
    36 
    36 
    37 text_raw{*\snip{annotatedef}{1}{1}{% *}
    37 text_raw{*\snip{annotatedef}{1}{1}{% *}
    38 definition shift :: "(nat \<Rightarrow> 'a) \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> 'a" where
    38 definition shift :: "(nat \<Rightarrow> 'a) \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> 'a" where
    39 "shift f n = (\<lambda>p. f(p+n))"
    39 "shift f n = (\<lambda>p. f(p+n))"
    40 
    40 
    41 fun annotate :: "(nat \<Rightarrow> 'a) \<Rightarrow> com \<Rightarrow> 'a acom" where
    41 fun annotate :: "(nat \<Rightarrow> 'a) \<Rightarrow> com \<Rightarrow> 'a acom" where
    42 "annotate f com.SKIP = SKIP {f 0}" |
    42 "annotate f com.SKIP = SKIP {f 0}" |
    43 "annotate f (x ::= e) = x ::= e {f 0}" |
    43 "annotate f (x ::= e) = x ::= e {f 0}" |
    44 "annotate f (c\<^isub>1;;c\<^isub>2) = annotate f c\<^isub>1;; annotate (shift f (asize c\<^isub>1)) c\<^isub>2" |
    44 "annotate f (c\<^sub>1;;c\<^sub>2) = annotate f c\<^sub>1;; annotate (shift f (asize c\<^sub>1)) c\<^sub>2" |
    45 "annotate f (IF b THEN c\<^isub>1 ELSE c\<^isub>2) =
    45 "annotate f (IF b THEN c\<^sub>1 ELSE c\<^sub>2) =
    46   IF b THEN {f 0} annotate (shift f 1) c\<^isub>1
    46   IF b THEN {f 0} annotate (shift f 1) c\<^sub>1
    47   ELSE {f(asize c\<^isub>1 + 1)} annotate (shift f (asize c\<^isub>1 + 2)) c\<^isub>2
    47   ELSE {f(asize c\<^sub>1 + 1)} annotate (shift f (asize c\<^sub>1 + 2)) c\<^sub>2
    48   {f(asize c\<^isub>1 + asize c\<^isub>2 + 2)}" |
    48   {f(asize c\<^sub>1 + asize c\<^sub>2 + 2)}" |
    49 "annotate f (WHILE b DO c) =
    49 "annotate f (WHILE b DO c) =
    50   {f 0} WHILE b DO {f 1} annotate (shift f 2) c {f(asize c + 2)}"
    50   {f 0} WHILE b DO {f 1} annotate (shift f 2) c {f(asize c + 2)}"
    51 text_raw{*}%endsnip*}
    51 text_raw{*}%endsnip*}
    52 
    52 
    53 text_raw{*\snip{annosdef}{1}{1}{% *}
    53 text_raw{*\snip{annosdef}{1}{1}{% *}
    54 fun annos :: "'a acom \<Rightarrow> 'a list" where
    54 fun annos :: "'a acom \<Rightarrow> 'a list" where
    55 "annos (SKIP {P}) = [P]" |
    55 "annos (SKIP {P}) = [P]" |
    56 "annos (x ::= e {P}) = [P]" |
    56 "annos (x ::= e {P}) = [P]" |
    57 "annos (C\<^isub>1;;C\<^isub>2) = annos C\<^isub>1 @ annos C\<^isub>2" |
    57 "annos (C\<^sub>1;;C\<^sub>2) = annos C\<^sub>1 @ annos C\<^sub>2" |
    58 "annos (IF b THEN {P\<^isub>1} C\<^isub>1 ELSE {P\<^isub>2} C\<^isub>2 {Q}) =
    58 "annos (IF b THEN {P\<^sub>1} C\<^sub>1 ELSE {P\<^sub>2} C\<^sub>2 {Q}) =
    59   P\<^isub>1 # annos C\<^isub>1 @  P\<^isub>2 # annos C\<^isub>2 @ [Q]" |
    59   P\<^sub>1 # annos C\<^sub>1 @  P\<^sub>2 # annos C\<^sub>2 @ [Q]" |
    60 "annos ({I} WHILE b DO {P} C {Q}) = I # P # annos C @ [Q]"
    60 "annos ({I} WHILE b DO {P} C {Q}) = I # P # annos C @ [Q]"
    61 text_raw{*}%endsnip*}
    61 text_raw{*}%endsnip*}
    62 
    62 
    63 definition anno :: "'a acom \<Rightarrow> nat \<Rightarrow> 'a" where
    63 definition anno :: "'a acom \<Rightarrow> nat \<Rightarrow> 'a" where
    64 "anno C p = annos C ! p"
    64 "anno C p = annos C ! p"
    68 
    68 
    69 text_raw{*\snip{mapacomdef}{1}{2}{% *}
    69 text_raw{*\snip{mapacomdef}{1}{2}{% *}
    70 fun map_acom :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a acom \<Rightarrow> 'b acom" where
    70 fun map_acom :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a acom \<Rightarrow> 'b acom" where
    71 "map_acom f (SKIP {P}) = SKIP {f P}" |
    71 "map_acom f (SKIP {P}) = SKIP {f P}" |
    72 "map_acom f (x ::= e {P}) = x ::= e {f P}" |
    72 "map_acom f (x ::= e {P}) = x ::= e {f P}" |
    73 "map_acom f (C\<^isub>1;;C\<^isub>2) = map_acom f C\<^isub>1;; map_acom f C\<^isub>2" |
    73 "map_acom f (C\<^sub>1;;C\<^sub>2) = map_acom f C\<^sub>1;; map_acom f C\<^sub>2" |
    74 "map_acom f (IF b THEN {P\<^isub>1} C\<^isub>1 ELSE {P\<^isub>2} C\<^isub>2 {Q}) =
    74 "map_acom f (IF b THEN {P\<^sub>1} C\<^sub>1 ELSE {P\<^sub>2} C\<^sub>2 {Q}) =
    75   IF b THEN {f P\<^isub>1} map_acom f C\<^isub>1 ELSE {f P\<^isub>2} map_acom f C\<^isub>2
    75   IF b THEN {f P\<^sub>1} map_acom f C\<^sub>1 ELSE {f P\<^sub>2} map_acom f C\<^sub>2
    76   {f Q}" |
    76   {f Q}" |
    77 "map_acom f ({I} WHILE b DO {P} C {Q}) =
    77 "map_acom f ({I} WHILE b DO {P} C {Q}) =
    78   {f I} WHILE b DO {f P} map_acom f C {f Q}"
    78   {f I} WHILE b DO {f P} map_acom f C {f Q}"
    79 text_raw{*}%endsnip*}
    79 text_raw{*}%endsnip*}
    80 
    80