Quot/quotient_def.ML
author Christian Urban <urbanc@in.tum.de>
Mon, 15 Feb 2010 16:28:07 +0100
changeset 1150 689a18f9484c
parent 1149 64d896cc16f8
child 1151 2c84860c19d2
permissions -rw-r--r--
tuned the parsing and testing code in quotient_def.ML; cleaned out old stuff in AbsRepTest.thy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
952
9c3b3eaecaff use of equiv_relation_chk in quotient_term
Christian Urban <urbanc@in.tum.de>
parents: 884
diff changeset
     1
(*  Title:      quotient_def.thy
9c3b3eaecaff use of equiv_relation_chk in quotient_term
Christian Urban <urbanc@in.tum.de>
parents: 884
diff changeset
     2
    Author:     Cezary Kaliszyk and Christian Urban
9c3b3eaecaff use of equiv_relation_chk in quotient_term
Christian Urban <urbanc@in.tum.de>
parents: 884
diff changeset
     3
9c3b3eaecaff use of equiv_relation_chk in quotient_term
Christian Urban <urbanc@in.tum.de>
parents: 884
diff changeset
     4
    Definitions for constants on quotient types.
9c3b3eaecaff use of equiv_relation_chk in quotient_term
Christian Urban <urbanc@in.tum.de>
parents: 884
diff changeset
     5
9c3b3eaecaff use of equiv_relation_chk in quotient_term
Christian Urban <urbanc@in.tum.de>
parents: 884
diff changeset
     6
*)
277
37636f2b1c19 separated the quotient_def into a separate file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
37636f2b1c19 separated the quotient_def into a separate file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
signature QUOTIENT_DEF =
1128
17ca92ab4660 Main renaming + fixes for new Isabelle in IntEx2.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1114
diff changeset
     9
sig
1150
689a18f9484c tuned the parsing and testing code in quotient_def.ML; cleaned out old stuff in AbsRepTest.thy
Christian Urban <urbanc@in.tum.de>
parents: 1149
diff changeset
    10
  val quotient_def: (binding option * mixfix) * (Attrib.binding * (term * term)) ->
775
Christian Urban <urbanc@in.tum.de>
parents: 774
diff changeset
    11
    local_theory -> (term * thm) * local_theory
789
Christian Urban <urbanc@in.tum.de>
parents: 776
diff changeset
    12
1150
689a18f9484c tuned the parsing and testing code in quotient_def.ML; cleaned out old stuff in AbsRepTest.thy
Christian Urban <urbanc@in.tum.de>
parents: 1149
diff changeset
    13
  val quotdef_cmd: (binding option * mixfix) * (Attrib.binding * (string * string)) ->
1146
2e5303b7dde4 Synchronize the commands.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1145
diff changeset
    14
    local_theory -> (term * thm) * local_theory
277
37636f2b1c19 separated the quotient_def into a separate file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
end;
37636f2b1c19 separated the quotient_def into a separate file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
37636f2b1c19 separated the quotient_def into a separate file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
structure Quotient_Def: QUOTIENT_DEF =
37636f2b1c19 separated the quotient_def into a separate file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
struct
37636f2b1c19 separated the quotient_def into a separate file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
762
baac4639ecef avoided global "open"s - replaced by local "open"s
Christian Urban <urbanc@in.tum.de>
parents: 760
diff changeset
    20
open Quotient_Info;
774
b4ffb8826105 moved get_fun into quotient_term; this simplifies the overall including structure of the package
Christian Urban <urbanc@in.tum.de>
parents: 768
diff changeset
    21
open Quotient_Term;
762
baac4639ecef avoided global "open"s - replaced by local "open"s
Christian Urban <urbanc@in.tum.de>
parents: 760
diff changeset
    22
884
e49c6b6f37f4 tuned quotient_def.ML and cleaned somewhat LamEx.thy
Christian Urban <urbanc@in.tum.de>
parents: 869
diff changeset
    23
(** Interface and Syntax Setup **)
850
3c6f8a4074c4 minor comment editing
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 833
diff changeset
    24
1128
17ca92ab4660 Main renaming + fixes for new Isabelle in IntEx2.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1114
diff changeset
    25
(* The ML-interface for a quotient definition takes
884
e49c6b6f37f4 tuned quotient_def.ML and cleaned somewhat LamEx.thy
Christian Urban <urbanc@in.tum.de>
parents: 869
diff changeset
    26
   as argument:
709
596467882518 Updated comments.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 705
diff changeset
    27
1150
689a18f9484c tuned the parsing and testing code in quotient_def.ML; cleaned out old stuff in AbsRepTest.thy
Christian Urban <urbanc@in.tum.de>
parents: 1149
diff changeset
    28
    - an optional binding and mixfix annotation
689a18f9484c tuned the parsing and testing code in quotient_def.ML; cleaned out old stuff in AbsRepTest.thy
Christian Urban <urbanc@in.tum.de>
parents: 1149
diff changeset
    29
    - attributes
884
e49c6b6f37f4 tuned quotient_def.ML and cleaned somewhat LamEx.thy
Christian Urban <urbanc@in.tum.de>
parents: 869
diff changeset
    30
    - the new constant as term
e49c6b6f37f4 tuned quotient_def.ML and cleaned somewhat LamEx.thy
Christian Urban <urbanc@in.tum.de>
parents: 869
diff changeset
    31
    - the rhs of the definition as term
850
3c6f8a4074c4 minor comment editing
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 833
diff changeset
    32
884
e49c6b6f37f4 tuned quotient_def.ML and cleaned somewhat LamEx.thy
Christian Urban <urbanc@in.tum.de>
parents: 869
diff changeset
    33
   It returns the defined constant and its definition
1128
17ca92ab4660 Main renaming + fixes for new Isabelle in IntEx2.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1114
diff changeset
    34
   theorem; stores the data in the qconsts data slot.
884
e49c6b6f37f4 tuned quotient_def.ML and cleaned somewhat LamEx.thy
Christian Urban <urbanc@in.tum.de>
parents: 869
diff changeset
    35
1150
689a18f9484c tuned the parsing and testing code in quotient_def.ML; cleaned out old stuff in AbsRepTest.thy
Christian Urban <urbanc@in.tum.de>
parents: 1149
diff changeset
    36
   Restriction: At the moment the right-hand side of the
689a18f9484c tuned the parsing and testing code in quotient_def.ML; cleaned out old stuff in AbsRepTest.thy
Christian Urban <urbanc@in.tum.de>
parents: 1149
diff changeset
    37
   definition must be a constant. Similarly the left-hand 
689a18f9484c tuned the parsing and testing code in quotient_def.ML; cleaned out old stuff in AbsRepTest.thy
Christian Urban <urbanc@in.tum.de>
parents: 1149
diff changeset
    38
   side must be a constant.
858
Christian Urban <urbanc@in.tum.de>
parents: 856
diff changeset
    39
*)
1150
689a18f9484c tuned the parsing and testing code in quotient_def.ML; cleaned out old stuff in AbsRepTest.thy
Christian Urban <urbanc@in.tum.de>
parents: 1149
diff changeset
    40
fun error_msg bind str = 
689a18f9484c tuned the parsing and testing code in quotient_def.ML; cleaned out old stuff in AbsRepTest.thy
Christian Urban <urbanc@in.tum.de>
parents: 1149
diff changeset
    41
  error ("Head of quotient_definition " ^ 
689a18f9484c tuned the parsing and testing code in quotient_def.ML; cleaned out old stuff in AbsRepTest.thy
Christian Urban <urbanc@in.tum.de>
parents: 1149
diff changeset
    42
    (quote str) ^ " differs from declaration " ^ (Binding.name_of bind) ^
689a18f9484c tuned the parsing and testing code in quotient_def.ML; cleaned out old stuff in AbsRepTest.thy
Christian Urban <urbanc@in.tum.de>
parents: 1149
diff changeset
    43
      Position.str_of (Binding.pos_of bind))
689a18f9484c tuned the parsing and testing code in quotient_def.ML; cleaned out old stuff in AbsRepTest.thy
Christian Urban <urbanc@in.tum.de>
parents: 1149
diff changeset
    44
689a18f9484c tuned the parsing and testing code in quotient_def.ML; cleaned out old stuff in AbsRepTest.thy
Christian Urban <urbanc@in.tum.de>
parents: 1149
diff changeset
    45
fun quotient_def ((optbind, mx), (attr, (lhs, rhs))) lthy =
293
653460d3e849 tuned the code in quotient and quotient_def
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
    46
let
884
e49c6b6f37f4 tuned quotient_def.ML and cleaned somewhat LamEx.thy
Christian Urban <urbanc@in.tum.de>
parents: 869
diff changeset
    47
  val (lhs_str, lhs_ty) = dest_Free lhs handle TERM _ => error "Constant already defined."
e49c6b6f37f4 tuned quotient_def.ML and cleaned somewhat LamEx.thy
Christian Urban <urbanc@in.tum.de>
parents: 869
diff changeset
    48
  val _ = if null (strip_abs_vars rhs) then () else error "The definiens cannot be an abstraction"
1150
689a18f9484c tuned the parsing and testing code in quotient_def.ML; cleaned out old stuff in AbsRepTest.thy
Christian Urban <urbanc@in.tum.de>
parents: 1149
diff changeset
    49
  
689a18f9484c tuned the parsing and testing code in quotient_def.ML; cleaned out old stuff in AbsRepTest.thy
Christian Urban <urbanc@in.tum.de>
parents: 1149
diff changeset
    50
  fun sanity_test NONE _ = true
689a18f9484c tuned the parsing and testing code in quotient_def.ML; cleaned out old stuff in AbsRepTest.thy
Christian Urban <urbanc@in.tum.de>
parents: 1149
diff changeset
    51
    | sanity_test (SOME bind) str =
689a18f9484c tuned the parsing and testing code in quotient_def.ML; cleaned out old stuff in AbsRepTest.thy
Christian Urban <urbanc@in.tum.de>
parents: 1149
diff changeset
    52
        if Name.of_binding bind = str then true
689a18f9484c tuned the parsing and testing code in quotient_def.ML; cleaned out old stuff in AbsRepTest.thy
Christian Urban <urbanc@in.tum.de>
parents: 1149
diff changeset
    53
        else error_msg bind str
689a18f9484c tuned the parsing and testing code in quotient_def.ML; cleaned out old stuff in AbsRepTest.thy
Christian Urban <urbanc@in.tum.de>
parents: 1149
diff changeset
    54
689a18f9484c tuned the parsing and testing code in quotient_def.ML; cleaned out old stuff in AbsRepTest.thy
Christian Urban <urbanc@in.tum.de>
parents: 1149
diff changeset
    55
  val _ = sanity_test optbind lhs_str
689a18f9484c tuned the parsing and testing code in quotient_def.ML; cleaned out old stuff in AbsRepTest.thy
Christian Urban <urbanc@in.tum.de>
parents: 1149
diff changeset
    56
689a18f9484c tuned the parsing and testing code in quotient_def.ML; cleaned out old stuff in AbsRepTest.thy
Christian Urban <urbanc@in.tum.de>
parents: 1149
diff changeset
    57
  val qconst_bname = Binding.name lhs_str
1097
551eacf071d7 More indentation, names and todo cleaning in the quotient package
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 952
diff changeset
    58
  val absrep_trm = absrep_fun AbsF lthy (fastype_of rhs, lhs_ty) $ rhs
854
5961edda27d7 absrep_fun and equiv_relation do not produce anymore spurious maps; two problems arose in IntEx, which are marked with "INJECTION PROBLEM"
Christian Urban <urbanc@in.tum.de>
parents: 833
diff changeset
    59
  val prop = Logic.mk_equals (lhs, Syntax.check_term lthy absrep_trm)
663
0dd10a900cae Different syntax for definitions that allows overloading and retrieving of definitions by matching whole constants.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 598
diff changeset
    60
  val (_, prop') = LocalDefs.cert_def lthy prop
0dd10a900cae Different syntax for definitions that allows overloading and retrieving of definitions by matching whole constants.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 598
diff changeset
    61
  val (_, newrhs) = Primitive_Defs.abs_def prop'
310
fec6301a1989 added a container for quotient constants (does not work yet though)
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
    62
1142
b102e1444851 remove one-line wrapper.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1141
diff changeset
    63
  val ((trm, (_ , thm)), lthy') = Local_Theory.define ((qconst_bname, mx), (attr, newrhs)) lthy
310
fec6301a1989 added a container for quotient constants (does not work yet though)
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
    64
800
71225f4a4635 some slight tuning
Christian Urban <urbanc@in.tum.de>
parents: 799
diff changeset
    65
  (* data storage *)
799
0755f8fd56b3 renamed transfer to transform (Markus)
Christian Urban <urbanc@in.tum.de>
parents: 789
diff changeset
    66
  fun qcinfo phi = transform_qconsts phi {qconst = trm, rconst = rhs, def = thm}
869
ce5f78f0eac5 Finished organising an efficient datastructure for qconst_info.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 868
diff changeset
    67
  fun trans_name phi = (fst o dest_Const o #qconst) (qcinfo phi)
319
0ae9d9e66cb7 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
    68
  val lthy'' = Local_Theory.declaration true
869
ce5f78f0eac5 Finished organising an efficient datastructure for qconst_info.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 868
diff changeset
    69
                 (fn phi => qconsts_update_gen (trans_name phi) (qcinfo phi)) lthy'
293
653460d3e849 tuned the code in quotient and quotient_def
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
    70
in
310
fec6301a1989 added a container for quotient constants (does not work yet though)
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
    71
  ((trm, thm), lthy'')
293
653460d3e849 tuned the code in quotient and quotient_def
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
    72
end
653460d3e849 tuned the code in quotient and quotient_def
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
    73
1150
689a18f9484c tuned the parsing and testing code in quotient_def.ML; cleaned out old stuff in AbsRepTest.thy
Christian Urban <urbanc@in.tum.de>
parents: 1149
diff changeset
    74
fun quotdef_cmd (decl, (attr, (lhs_str, rhs_str))) lthy =
277
37636f2b1c19 separated the quotient_def into a separate file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
let
1141
3c8ad149a4d3 Undid the read_terms change; now compiles.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1138
diff changeset
    76
  val lhs = Syntax.read_term lthy lhs_str
3c8ad149a4d3 Undid the read_terms change; now compiles.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1138
diff changeset
    77
  val rhs = Syntax.read_term lthy rhs_str
1114
Christian Urban <urbanc@in.tum.de>
parents: 1097
diff changeset
    78
  val lthy' = Variable.declare_term lhs lthy
Christian Urban <urbanc@in.tum.de>
parents: 1097
diff changeset
    79
  val lthy'' = Variable.declare_term rhs lthy'
277
37636f2b1c19 separated the quotient_def into a separate file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
in
1150
689a18f9484c tuned the parsing and testing code in quotient_def.ML; cleaned out old stuff in AbsRepTest.thy
Christian Urban <urbanc@in.tum.de>
parents: 1149
diff changeset
    81
  quotient_def (decl, (attr, (lhs, rhs))) lthy''
277
37636f2b1c19 separated the quotient_def into a separate file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
end
37636f2b1c19 separated the quotient_def into a separate file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
1150
689a18f9484c tuned the parsing and testing code in quotient_def.ML; cleaned out old stuff in AbsRepTest.thy
Christian Urban <urbanc@in.tum.de>
parents: 1149
diff changeset
    84
local
689a18f9484c tuned the parsing and testing code in quotient_def.ML; cleaned out old stuff in AbsRepTest.thy
Christian Urban <urbanc@in.tum.de>
parents: 1149
diff changeset
    85
  structure P = OuterParse;
689a18f9484c tuned the parsing and testing code in quotient_def.ML; cleaned out old stuff in AbsRepTest.thy
Christian Urban <urbanc@in.tum.de>
parents: 1149
diff changeset
    86
in
689a18f9484c tuned the parsing and testing code in quotient_def.ML; cleaned out old stuff in AbsRepTest.thy
Christian Urban <urbanc@in.tum.de>
parents: 1149
diff changeset
    87
689a18f9484c tuned the parsing and testing code in quotient_def.ML; cleaned out old stuff in AbsRepTest.thy
Christian Urban <urbanc@in.tum.de>
parents: 1149
diff changeset
    88
val quotdef_decl = (P.binding >> SOME) -- P.opt_mixfix' --| P.$$$ "where"
689a18f9484c tuned the parsing and testing code in quotient_def.ML; cleaned out old stuff in AbsRepTest.thy
Christian Urban <urbanc@in.tum.de>
parents: 1149
diff changeset
    89
279
b2fd070c8833 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 277
diff changeset
    90
val quotdef_parser =
1150
689a18f9484c tuned the parsing and testing code in quotient_def.ML; cleaned out old stuff in AbsRepTest.thy
Christian Urban <urbanc@in.tum.de>
parents: 1149
diff changeset
    91
  Scan.optional quotdef_decl (NONE, NoSyn) -- 
689a18f9484c tuned the parsing and testing code in quotient_def.ML; cleaned out old stuff in AbsRepTest.thy
Christian Urban <urbanc@in.tum.de>
parents: 1149
diff changeset
    92
    P.!!! (SpecParse.opt_thm_name ":" -- (P.term --| P.$$$ "is" -- P.term))
689a18f9484c tuned the parsing and testing code in quotient_def.ML; cleaned out old stuff in AbsRepTest.thy
Christian Urban <urbanc@in.tum.de>
parents: 1149
diff changeset
    93
end
279
b2fd070c8833 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 277
diff changeset
    94
1144
538daee762e6 Added a binding to the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1142
diff changeset
    95
val _ =
538daee762e6 Added a binding to the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1142
diff changeset
    96
  OuterSyntax.local_theory "quotient_definition"
538daee762e6 Added a binding to the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1142
diff changeset
    97
    "definition for constants over the quotient type"
1146
2e5303b7dde4 Synchronize the commands.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1145
diff changeset
    98
      OuterKeyword.thy_decl (quotdef_parser >> (snd oo quotdef_cmd))
277
37636f2b1c19 separated the quotient_def into a separate file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
37636f2b1c19 separated the quotient_def into a separate file
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
end; (* structure *)