src/HOL/TLA/Intensional.thy
author wenzelm
Fri Dec 17 17:43:54 2010 +0100 (2010-12-17)
changeset 41229 d797baa3d57c
parent 38786 e46e7a9cb622
child 42018 878f33040280
permissions -rw-r--r--
replaced command 'nonterminals' by slightly modernized version 'nonterminal';
     1 (*  Title:      HOL/TLA/Intensional.thy
     2     Author:     Stephan Merz
     3     Copyright:  1998 University of Munich
     4 *)
     5 
     6 header {* A framework for "intensional" (possible-world based) logics
     7   on top of HOL, with lifting of constants and functions *}
     8 
     9 theory Intensional
    10 imports Main
    11 begin
    12 
    13 classes world
    14 classrel world < type
    15 
    16 (** abstract syntax **)
    17 
    18 types
    19   ('w,'a) expr = "'w => 'a"               (* intention: 'w::world, 'a::type *)
    20   'w form = "('w, bool) expr"
    21 
    22 consts
    23   Valid    :: "('w::world) form => bool"
    24   const    :: "'a => ('w::world, 'a) expr"
    25   lift     :: "['a => 'b, ('w::world, 'a) expr] => ('w,'b) expr"
    26   lift2    :: "['a => 'b => 'c, ('w::world,'a) expr, ('w,'b) expr] => ('w,'c) expr"
    27   lift3    :: "['a => 'b => 'c => 'd, ('w::world,'a) expr, ('w,'b) expr, ('w,'c) expr] => ('w,'d) expr"
    28 
    29   (* "Rigid" quantification (logic level) *)
    30   RAll     :: "('a => ('w::world) form) => 'w form"       (binder "Rall " 10)
    31   REx      :: "('a => ('w::world) form) => 'w form"       (binder "Rex " 10)
    32   REx1     :: "('a => ('w::world) form) => 'w form"       (binder "Rex! " 10)
    33 
    34 (** concrete syntax **)
    35 
    36 nonterminal lift and liftargs
    37 
    38 syntax
    39   ""            :: "id => lift"                          ("_")
    40   ""            :: "longid => lift"                      ("_")
    41   ""            :: "var => lift"                         ("_")
    42   "_applC"      :: "[lift, cargs] => lift"               ("(1_/ _)" [1000, 1000] 999)
    43   ""            :: "lift => lift"                        ("'(_')")
    44   "_lambda"     :: "[idts, 'a] => lift"                  ("(3%_./ _)" [0, 3] 3)
    45   "_constrain"  :: "[lift, type] => lift"                ("(_::_)" [4, 0] 3)
    46   ""            :: "lift => liftargs"                    ("_")
    47   "_liftargs"   :: "[lift, liftargs] => liftargs"        ("_,/ _")
    48   "_Valid"      :: "lift => bool"                        ("(|- _)" 5)
    49   "_holdsAt"    :: "['a, lift] => bool"                  ("(_ |= _)" [100,10] 10)
    50 
    51   (* Syntax for lifted expressions outside the scope of |- or |= *)
    52   "_LIFT"       :: "lift => 'a"                          ("LIFT _")
    53 
    54   (* generic syntax for lifted constants and functions *)
    55   "_const"      :: "'a => lift"                          ("(#_)" [1000] 999)
    56   "_lift"       :: "['a, lift] => lift"                  ("(_<_>)" [1000] 999)
    57   "_lift2"      :: "['a, lift, lift] => lift"            ("(_<_,/ _>)" [1000] 999)
    58   "_lift3"      :: "['a, lift, lift, lift] => lift"      ("(_<_,/ _,/ _>)" [1000] 999)
    59 
    60   (* concrete syntax for common infix functions: reuse same symbol *)
    61   "_liftEqu"    :: "[lift, lift] => lift"                ("(_ =/ _)" [50,51] 50)
    62   "_liftNeq"    :: "[lift, lift] => lift"                ("(_ ~=/ _)" [50,51] 50)
    63   "_liftNot"    :: "lift => lift"                        ("(~ _)" [40] 40)
    64   "_liftAnd"    :: "[lift, lift] => lift"                ("(_ &/ _)" [36,35] 35)
    65   "_liftOr"     :: "[lift, lift] => lift"                ("(_ |/ _)" [31,30] 30)
    66   "_liftImp"    :: "[lift, lift] => lift"                ("(_ -->/ _)" [26,25] 25)
    67   "_liftIf"     :: "[lift, lift, lift] => lift"          ("(if (_)/ then (_)/ else (_))" 10)
    68   "_liftPlus"   :: "[lift, lift] => lift"                ("(_ +/ _)" [66,65] 65)
    69   "_liftMinus"  :: "[lift, lift] => lift"                ("(_ -/ _)" [66,65] 65)
    70   "_liftTimes"  :: "[lift, lift] => lift"                ("(_ */ _)" [71,70] 70)
    71   "_liftDiv"    :: "[lift, lift] => lift"                ("(_ div _)" [71,70] 70)
    72   "_liftMod"    :: "[lift, lift] => lift"                ("(_ mod _)" [71,70] 70)
    73   "_liftLess"   :: "[lift, lift] => lift"                ("(_/ < _)"  [50, 51] 50)
    74   "_liftLeq"    :: "[lift, lift] => lift"                ("(_/ <= _)" [50, 51] 50)
    75   "_liftMem"    :: "[lift, lift] => lift"                ("(_/ : _)" [50, 51] 50)
    76   "_liftNotMem" :: "[lift, lift] => lift"                ("(_/ ~: _)" [50, 51] 50)
    77   "_liftFinset" :: "liftargs => lift"                    ("{(_)}")
    78   (** TODO: syntax for lifted collection / comprehension **)
    79   "_liftPair"   :: "[lift,liftargs] => lift"                   ("(1'(_,/ _'))")
    80   (* infix syntax for list operations *)
    81   "_liftCons" :: "[lift, lift] => lift"                  ("(_ #/ _)" [65,66] 65)
    82   "_liftApp"  :: "[lift, lift] => lift"                  ("(_ @/ _)" [65,66] 65)
    83   "_liftList" :: "liftargs => lift"                      ("[(_)]")
    84 
    85   (* Rigid quantification (syntax level) *)
    86   "_ARAll"  :: "[idts, lift] => lift"                    ("(3! _./ _)" [0, 10] 10)
    87   "_AREx"   :: "[idts, lift] => lift"                    ("(3? _./ _)" [0, 10] 10)
    88   "_AREx1"  :: "[idts, lift] => lift"                    ("(3?! _./ _)" [0, 10] 10)
    89   "_RAll" :: "[idts, lift] => lift"                      ("(3ALL _./ _)" [0, 10] 10)
    90   "_REx"  :: "[idts, lift] => lift"                      ("(3EX _./ _)" [0, 10] 10)
    91   "_REx1" :: "[idts, lift] => lift"                      ("(3EX! _./ _)" [0, 10] 10)
    92 
    93 translations
    94   "_const"        == "CONST const"
    95   "_lift"         == "CONST lift"
    96   "_lift2"        == "CONST lift2"
    97   "_lift3"        == "CONST lift3"
    98   "_Valid"        == "CONST Valid"
    99   "_RAll x A"     == "Rall x. A"
   100   "_REx x  A"     == "Rex x. A"
   101   "_REx1 x  A"    == "Rex! x. A"
   102   "_ARAll"        => "_RAll"
   103   "_AREx"         => "_REx"
   104   "_AREx1"        => "_REx1"
   105 
   106   "w |= A"        => "A w"
   107   "LIFT A"        => "A::_=>_"
   108 
   109   "_liftEqu"      == "_lift2 (op =)"
   110   "_liftNeq u v"  == "_liftNot (_liftEqu u v)"
   111   "_liftNot"      == "_lift (CONST Not)"
   112   "_liftAnd"      == "_lift2 (op &)"
   113   "_liftOr"       == "_lift2 (op | )"
   114   "_liftImp"      == "_lift2 (op -->)"
   115   "_liftIf"       == "_lift3 (CONST If)"
   116   "_liftPlus"     == "_lift2 (op +)"
   117   "_liftMinus"    == "_lift2 (op -)"
   118   "_liftTimes"    == "_lift2 (op *)"
   119   "_liftDiv"      == "_lift2 (op div)"
   120   "_liftMod"      == "_lift2 (op mod)"
   121   "_liftLess"     == "_lift2 (op <)"
   122   "_liftLeq"      == "_lift2 (op <=)"
   123   "_liftMem"      == "_lift2 (op :)"
   124   "_liftNotMem x xs"   == "_liftNot (_liftMem x xs)"
   125   "_liftFinset (_liftargs x xs)"  == "_lift2 (CONST insert) x (_liftFinset xs)"
   126   "_liftFinset x" == "_lift2 (CONST insert) x (_const {})"
   127   "_liftPair x (_liftargs y z)"       == "_liftPair x (_liftPair y z)"
   128   "_liftPair"     == "_lift2 (CONST Pair)"
   129   "_liftCons"     == "CONST lift2 (CONST Cons)"
   130   "_liftApp"      == "CONST lift2 (op @)"
   131   "_liftList (_liftargs x xs)"  == "_liftCons x (_liftList xs)"
   132   "_liftList x"   == "_liftCons x (_const [])"
   133 
   134 
   135 
   136   "w |= ~A"       <= "_liftNot A w"
   137   "w |= A & B"    <= "_liftAnd A B w"
   138   "w |= A | B"    <= "_liftOr A B w"
   139   "w |= A --> B"  <= "_liftImp A B w"
   140   "w |= u = v"    <= "_liftEqu u v w"
   141   "w |= ALL x. A"   <= "_RAll x A w"
   142   "w |= EX x. A"   <= "_REx x A w"
   143   "w |= EX! x. A"  <= "_REx1 x A w"
   144 
   145 syntax (xsymbols)
   146   "_Valid"      :: "lift => bool"                        ("(\<turnstile> _)" 5)
   147   "_holdsAt"    :: "['a, lift] => bool"                  ("(_ \<Turnstile> _)" [100,10] 10)
   148   "_liftNeq"    :: "[lift, lift] => lift"                (infixl "\<noteq>" 50)
   149   "_liftNot"    :: "lift => lift"                        ("\<not> _" [40] 40)
   150   "_liftAnd"    :: "[lift, lift] => lift"                (infixr "\<and>" 35)
   151   "_liftOr"     :: "[lift, lift] => lift"                (infixr "\<or>" 30)
   152   "_liftImp"    :: "[lift, lift] => lift"                (infixr "\<longrightarrow>" 25)
   153   "_RAll"       :: "[idts, lift] => lift"                ("(3\<forall>_./ _)" [0, 10] 10)
   154   "_REx"        :: "[idts, lift] => lift"                ("(3\<exists>_./ _)" [0, 10] 10)
   155   "_REx1"       :: "[idts, lift] => lift"                ("(3\<exists>!_./ _)" [0, 10] 10)
   156   "_liftLeq"    :: "[lift, lift] => lift"                ("(_/ \<le> _)" [50, 51] 50)
   157   "_liftMem"    :: "[lift, lift] => lift"                ("(_/ \<in> _)" [50, 51] 50)
   158   "_liftNotMem" :: "[lift, lift] => lift"                ("(_/ \<notin> _)" [50, 51] 50)
   159 
   160 syntax (HTML output)
   161   "_liftNeq"    :: "[lift, lift] => lift"                (infixl "\<noteq>" 50)
   162   "_liftNot"    :: "lift => lift"                        ("\<not> _" [40] 40)
   163   "_liftAnd"    :: "[lift, lift] => lift"                (infixr "\<and>" 35)
   164   "_liftOr"     :: "[lift, lift] => lift"                (infixr "\<or>" 30)
   165   "_RAll"       :: "[idts, lift] => lift"                ("(3\<forall>_./ _)" [0, 10] 10)
   166   "_REx"        :: "[idts, lift] => lift"                ("(3\<exists>_./ _)" [0, 10] 10)
   167   "_REx1"       :: "[idts, lift] => lift"                ("(3\<exists>!_./ _)" [0, 10] 10)
   168   "_liftLeq"    :: "[lift, lift] => lift"                ("(_/ \<le> _)" [50, 51] 50)
   169   "_liftMem"    :: "[lift, lift] => lift"                ("(_/ \<in> _)" [50, 51] 50)
   170   "_liftNotMem" :: "[lift, lift] => lift"                ("(_/ \<notin> _)" [50, 51] 50)
   171 
   172 defs
   173   Valid_def:   "|- A    ==  ALL w. w |= A"
   174 
   175   unl_con:     "LIFT #c w  ==  c"
   176   unl_lift:    "lift f x w == f (x w)"
   177   unl_lift2:   "LIFT f<x, y> w == f (x w) (y w)"
   178   unl_lift3:   "LIFT f<x, y, z> w == f (x w) (y w) (z w)"
   179 
   180   unl_Rall:    "w |= ALL x. A x  ==  ALL x. (w |= A x)"
   181   unl_Rex:     "w |= EX x. A x   ==  EX x. (w |= A x)"
   182   unl_Rex1:    "w |= EX! x. A x  ==  EX! x. (w |= A x)"
   183 
   184 
   185 subsection {* Lemmas and tactics for "intensional" logics. *}
   186 
   187 lemmas intensional_rews [simp] =
   188   unl_con unl_lift unl_lift2 unl_lift3 unl_Rall unl_Rex unl_Rex1
   189 
   190 lemma inteq_reflection: "|- x=y  ==>  (x==y)"
   191   apply (unfold Valid_def unl_lift2)
   192   apply (rule eq_reflection)
   193   apply (rule ext)
   194   apply (erule spec)
   195   done
   196 
   197 lemma intI [intro!]: "(!!w. w |= A) ==> |- A"
   198   apply (unfold Valid_def)
   199   apply (rule allI)
   200   apply (erule meta_spec)
   201   done
   202 
   203 lemma intD [dest]: "|- A ==> w |= A"
   204   apply (unfold Valid_def)
   205   apply (erule spec)
   206   done
   207 
   208 (** Lift usual HOL simplifications to "intensional" level. **)
   209 
   210 lemma int_simps:
   211   "|- (x=x) = #True"
   212   "|- (~#True) = #False"  "|- (~#False) = #True"  "|- (~~ P) = P"
   213   "|- ((~P) = P) = #False"  "|- (P = (~P)) = #False"
   214   "|- (P ~= Q) = (P = (~Q))"
   215   "|- (#True=P) = P"  "|- (P=#True) = P"
   216   "|- (#True --> P) = P"  "|- (#False --> P) = #True"
   217   "|- (P --> #True) = #True"  "|- (P --> P) = #True"
   218   "|- (P --> #False) = (~P)"  "|- (P --> ~P) = (~P)"
   219   "|- (P & #True) = P"  "|- (#True & P) = P"
   220   "|- (P & #False) = #False"  "|- (#False & P) = #False"
   221   "|- (P & P) = P"  "|- (P & ~P) = #False"  "|- (~P & P) = #False"
   222   "|- (P | #True) = #True"  "|- (#True | P) = #True"
   223   "|- (P | #False) = P"  "|- (#False | P) = P"
   224   "|- (P | P) = P"  "|- (P | ~P) = #True"  "|- (~P | P) = #True"
   225   "|- (! x. P) = P"  "|- (? x. P) = P"
   226   "|- (~Q --> ~P) = (P --> Q)"
   227   "|- (P|Q --> R) = ((P-->R)&(Q-->R))"
   228   apply (unfold Valid_def intensional_rews)
   229   apply blast+
   230   done
   231 
   232 declare int_simps [THEN inteq_reflection, simp]
   233 
   234 lemma TrueW [simp]: "|- #True"
   235   by (simp add: Valid_def unl_con)
   236 
   237 
   238 
   239 (* ======== Functions to "unlift" intensional implications into HOL rules ====== *)
   240 
   241 ML {*
   242 (* Basic unlifting introduces a parameter "w" and applies basic rewrites, e.g.
   243    |- F = G    becomes   F w = G w
   244    |- F --> G  becomes   F w --> G w
   245 *)
   246 
   247 fun int_unlift th =
   248   rewrite_rule @{thms intensional_rews} (th RS @{thm intD} handle THM _ => th);
   249 
   250 (* Turn  |- F = G  into meta-level rewrite rule  F == G *)
   251 fun int_rewrite th =
   252   zero_var_indexes (rewrite_rule @{thms intensional_rews} (th RS @{thm inteq_reflection}))
   253 
   254 (* flattening turns "-->" into "==>" and eliminates conjunctions in the
   255    antecedent. For example,
   256 
   257          P & Q --> (R | S --> T)    becomes   [| P; Q; R | S |] ==> T
   258 
   259    Flattening can be useful with "intensional" lemmas (after unlifting).
   260    Naive resolution with mp and conjI may run away because of higher-order
   261    unification, therefore the code is a little awkward.
   262 *)
   263 fun flatten t =
   264   let
   265     (* analogous to RS, but using matching instead of resolution *)
   266     fun matchres tha i thb =
   267       case Seq.chop 2 (Thm.biresolution true [(false,tha)] i thb) of
   268           ([th],_) => th
   269         | ([],_)   => raise THM("matchres: no match", i, [tha,thb])
   270         |      _   => raise THM("matchres: multiple unifiers", i, [tha,thb])
   271 
   272     (* match tha with some premise of thb *)
   273     fun matchsome tha thb =
   274       let fun hmatch 0 = raise THM("matchsome: no match", 0, [tha,thb])
   275             | hmatch n = matchres tha n thb handle THM _ => hmatch (n-1)
   276       in hmatch (nprems_of thb) end
   277 
   278     fun hflatten t =
   279         case (concl_of t) of
   280           Const _ $ (Const (@{const_name HOL.implies}, _) $ _ $ _) => hflatten (t RS mp)
   281         | _ => (hflatten (matchsome conjI t)) handle THM _ => zero_var_indexes t
   282   in
   283     hflatten t
   284   end
   285 
   286 fun int_use th =
   287     case (concl_of th) of
   288       Const _ $ (Const ("Intensional.Valid", _) $ _) =>
   289               (flatten (int_unlift th) handle THM _ => th)
   290     | _ => th
   291 *}
   292 
   293 attribute_setup int_unlift = {* Scan.succeed (Thm.rule_attribute (K int_unlift)) *} ""
   294 attribute_setup int_rewrite = {* Scan.succeed (Thm.rule_attribute (K int_rewrite)) *} ""
   295 attribute_setup flatten = {* Scan.succeed (Thm.rule_attribute (K flatten)) *} ""
   296 attribute_setup int_use = {* Scan.succeed (Thm.rule_attribute (K int_use)) *} ""
   297 
   298 lemma Not_Rall: "|- (~(! x. F x)) = (? x. ~F x)"
   299   by (simp add: Valid_def)
   300 
   301 lemma Not_Rex: "|- (~ (? x. F x)) = (! x. ~ F x)"
   302   by (simp add: Valid_def)
   303 
   304 end