Nominal/nominal_termination.ML
author Christian Urban <urbanc@in.tum.de>
Thu, 19 Apr 2018 13:57:17 +0100
changeset 3245 017e33849f4d
parent 3239 67370521c09c
permissions -rw-r--r--
updated to Isabelle 2016-1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2976
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
(*  Nominal Termination
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
    Author: Christian Urban
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
    heavily based on the code of Alexander Krauss
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
    (code forked on 18 July 2011)
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
Redefinition of the termination command
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
*)
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
signature NOMINAL_FUNCTION_TERMINATION =
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
sig
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
  include NOMINAL_FUNCTION_DATA
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
3055
1afcbaf4242b termination does not automatically prove equivariance for the defined function (label: no_eqvt)
Christian Urban <urbanc@in.tum.de>
parents: 3045
diff changeset
    14
  val termination : bool -> term option -> local_theory -> Proof.state
1afcbaf4242b termination does not automatically prove equivariance for the defined function (label: no_eqvt)
Christian Urban <urbanc@in.tum.de>
parents: 3045
diff changeset
    15
  val termination_cmd : bool -> string option -> local_theory -> Proof.state
2976
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
end
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
structure Nominal_Function_Termination : NOMINAL_FUNCTION_TERMINATION =
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
struct
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
open Function_Lib
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
open Function_Common
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
open Nominal_Function_Common
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
val simp_attribs = map (Attrib.internal o K)
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
  [Simplifier.simp_add,
3245
017e33849f4d updated to Isabelle 2016-1
Christian Urban <urbanc@in.tum.de>
parents: 3239
diff changeset
    28
   Code.add_default_eqn_attribute Code.Equation,
3239
67370521c09c updated for Isabelle 2015
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3236
diff changeset
    29
   Named_Theorems.add @{named_theorems nitpick_simp}]
2976
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
val eqvt_attrib =  Attrib.internal (K Nominal_ThmDecls.eqvt_add)
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
3055
1afcbaf4242b termination does not automatically prove equivariance for the defined function (label: no_eqvt)
Christian Urban <urbanc@in.tum.de>
parents: 3045
diff changeset
    33
fun prepare_termination_proof prep_term is_eqvt raw_term_opt lthy =
2976
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
  let
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
    val term_opt = Option.map (prep_term lthy) raw_term_opt
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
    val info = the (case term_opt of
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
                      SOME t => (import_function_data t lthy
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
                        handle Option.Option =>
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
                          error ("Not a function: " ^ quote (Syntax.string_of_term lthy t)))
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
                    | NONE => (import_last_function lthy handle Option.Option => error "Not a function"))
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
      val { termination, fs, R, add_simps, case_names, psimps,
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
        pinducts, defname, eqvts, ...} = info
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
      val domT = domain_type (fastype_of R)
2982
4a00077c008f completed the eqvt-proofs for functions; they are stored under the name function_name.eqvt and added to the eqvt-list
Christian Urban <urbanc@in.tum.de>
parents: 2981
diff changeset
    45
2976
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
      val goal = HOLogic.mk_Trueprop
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
                   (HOLogic.mk_all ("x", domT, mk_acc domT R $ Free ("x", domT)))
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
      fun afterqed [[totality]] lthy =
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
        let
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
          val totality = Thm.close_derivation totality
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
          val remove_domain_condition =
3218
89158f401b07 updated to simplifier changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3193
diff changeset
    52
            full_simplify (put_simpset HOL_basic_ss lthy
89158f401b07 updated to simplifier changes
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3193
diff changeset
    53
              addsimps [totality, @{thm True_implies_equals}])
2976
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
          val tsimps = map remove_domain_condition psimps
2982
4a00077c008f completed the eqvt-proofs for functions; they are stored under the name function_name.eqvt and added to the eqvt-list
Christian Urban <urbanc@in.tum.de>
parents: 2981
diff changeset
    55
          val tinducts = map remove_domain_condition pinducts
2976
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
          val teqvts = map remove_domain_condition eqvts
2981
c8acaded1777 temporary fix
Christian Urban <urbanc@in.tum.de>
parents: 2976
diff changeset
    57
2976
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
          fun qualify n = Binding.name n
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
            |> Binding.qualify true defname
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
        in
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
          lthy
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
          |> add_simps I "simps" I simp_attribs tsimps
3055
1afcbaf4242b termination does not automatically prove equivariance for the defined function (label: no_eqvt)
Christian Urban <urbanc@in.tum.de>
parents: 3045
diff changeset
    63
          ||>> Local_Theory.note ((qualify "eqvt", if is_eqvt then [eqvt_attrib] else []), teqvts)
2976
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
          ||>> Local_Theory.note
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
             ((qualify "induct",
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
               [Attrib.internal (K (Rule_Cases.case_names case_names))]),
2982
4a00077c008f completed the eqvt-proofs for functions; they are stored under the name function_name.eqvt and added to the eqvt-list
Christian Urban <urbanc@in.tum.de>
parents: 2981
diff changeset
    67
              tinducts)
2976
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
          |-> (fn ((simps, (_, eqvts)), (_, inducts)) => fn lthy =>
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
            let val info' = { is_partial=false, defname=defname, add_simps=add_simps,
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
              case_names=case_names, fs=fs, R=R, psimps=psimps, pinducts=pinducts,
2981
c8acaded1777 temporary fix
Christian Urban <urbanc@in.tum.de>
parents: 2976
diff changeset
    71
              simps=SOME simps, inducts=SOME inducts, termination=termination, eqvts=teqvts }
2976
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
            in
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
              (info',
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
               lthy 
3045
d0ad264f8c4f updated to Isabelle 3 Nov; it includes a hack to work around a bug in the localised version of the quotient package
Christian Urban <urbanc@in.tum.de>
parents: 2982
diff changeset
    75
               |> Local_Theory.declaration {syntax = false, pervasive = false}
d0ad264f8c4f updated to Isabelle 3 Nov; it includes a hack to work around a bug in the localised version of the quotient package
Christian Urban <urbanc@in.tum.de>
parents: 2982
diff changeset
    76
                    (add_function_data o morph_function_data info')
2976
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
               |> Spec_Rules.add Spec_Rules.Equational (fs, tsimps))
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
            end)
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
        end
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
  in
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
    (goal, afterqed, termination)
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
  end
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
3055
1afcbaf4242b termination does not automatically prove equivariance for the defined function (label: no_eqvt)
Christian Urban <urbanc@in.tum.de>
parents: 3045
diff changeset
    84
fun gen_termination prep_term is_eqvt raw_term_opt lthy =
2976
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
  let
3055
1afcbaf4242b termination does not automatically prove equivariance for the defined function (label: no_eqvt)
Christian Urban <urbanc@in.tum.de>
parents: 3045
diff changeset
    86
    val (goal, afterqed, termination) = prepare_termination_proof prep_term is_eqvt raw_term_opt lthy
2976
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
  in
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
    lthy
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
    |> Proof_Context.note_thmss ""
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
       [((Binding.empty, [Context_Rules.rule_del]), [([allI], [])])] |> snd
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
    |> Proof_Context.note_thmss ""
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
       [((Binding.empty, [Context_Rules.intro_bang (SOME 1)]), [([allI], [])])] |> snd
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
    |> Proof_Context.note_thmss ""
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
       [((Binding.name "termination", [Context_Rules.intro_bang (SOME 0)]),
3227
35bb5b013f0e updated with current AFP version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3218
diff changeset
    95
         [([Goal.norm_result lthy termination], [])])] |> snd
2976
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
    |> Proof.theorem NONE (snd oo afterqed) [[(goal, [])]]
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
  end
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
val termination = gen_termination Syntax.check_term
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
val termination_cmd = gen_termination Syntax.read_term
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
(* outer syntax *)
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
val option_parser =
3135
92b9b8d2888d updated to new Isabelle (20 March)
Christian Urban <urbanc@in.tum.de>
parents: 3055
diff changeset
   105
  (Scan.optional (@{keyword "("} |-- Parse.!!! 
3236
e2da10806a34 changed nominal_primrec to nominal_function and termination to nominal_termination
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3227
diff changeset
   106
    ((Parse.reserved "eqvt" >> K true) ||
e2da10806a34 changed nominal_primrec to nominal_function and termination to nominal_termination
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3227
diff changeset
   107
     (Parse.reserved "no_eqvt" >> K false)) --| @{keyword ")"}) (false))
2976
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
val _ =
3239
67370521c09c updated for Isabelle 2015
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3236
diff changeset
   110
  Outer_Syntax.local_theory_to_proof @{command_keyword nominal_termination}
3236
e2da10806a34 changed nominal_primrec to nominal_function and termination to nominal_termination
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3227
diff changeset
   111
    "prove termination of a recursive nominal function"
3135
92b9b8d2888d updated to new Isabelle (20 March)
Christian Urban <urbanc@in.tum.de>
parents: 3055
diff changeset
   112
      (option_parser -- Scan.option Parse.term >> 
3236
e2da10806a34 changed nominal_primrec to nominal_function and termination to nominal_termination
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 3227
diff changeset
   113
        (fn (is_eqvt, opt_trm) => termination_cmd is_eqvt opt_trm))
2976
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
d5ecc2f7f299 added termination file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
end