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