src/HOL/HOLCF/Tools/cont_consts.ML
author wenzelm
Wed, 25 Jun 2025 16:35:25 +0200
changeset 82768 8f866fd6fae1
parent 81590 e656c5edc352
permissions -rw-r--r--
merged
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42151
4da4fc77664b tuned headers;
wenzelm
parents: 40832
diff changeset
     1
(*  Title:      HOL/HOLCF/Tools/cont_consts.ML
81226
e8030f7b1386 misc tuning and clarification;
wenzelm
parents: 80897
diff changeset
     2
    Author:     Tobias Mayr
e8030f7b1386 misc tuning and clarification;
wenzelm
parents: 80897
diff changeset
     3
    Author:     David von Oheimb
e8030f7b1386 misc tuning and clarification;
wenzelm
parents: 80897
diff changeset
     4
    Author:     Makarius
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
     5
81226
e8030f7b1386 misc tuning and clarification;
wenzelm
parents: 80897
diff changeset
     6
HOLCF version of 'consts' command: handle continuous function types in mixfix
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
     7
syntax.
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
     8
*)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
     9
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    10
signature CONT_CONSTS =
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    11
sig
35257
3e5980f612d9 adapted to authentic syntax;
wenzelm
parents: 35130
diff changeset
    12
  val add_consts: (binding * typ * mixfix) list -> theory -> theory
3e5980f612d9 adapted to authentic syntax;
wenzelm
parents: 35130
diff changeset
    13
  val add_consts_cmd: (binding * string * mixfix) list -> theory -> theory
40832
4352ca878c41 remove gratuitous semicolons from ML code
huffman
parents: 40774
diff changeset
    14
end
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    15
35257
3e5980f612d9 adapted to authentic syntax;
wenzelm
parents: 35130
diff changeset
    16
structure Cont_Consts: CONT_CONSTS =
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    17
struct
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    18
81226
e8030f7b1386 misc tuning and clarification;
wenzelm
parents: 80897
diff changeset
    19
fun count_cfun \<^Type>\<open>cfun _ B\<close> = count_cfun B + 1
e8030f7b1386 misc tuning and clarification;
wenzelm
parents: 80897
diff changeset
    20
  | count_cfun _ = 0
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    21
81226
e8030f7b1386 misc tuning and clarification;
wenzelm
parents: 80897
diff changeset
    22
fun change_cfun 0 T = T
e8030f7b1386 misc tuning and clarification;
wenzelm
parents: 80897
diff changeset
    23
  | change_cfun n \<^Type>\<open>cfun A B\<close> = \<^Type>\<open>fun A \<open>change_cfun (n - 1) B\<close>\<close>
81228
ed326e93b835 misc tuning and clarification;
wenzelm
parents: 81227
diff changeset
    24
  | change_cfun _ T = raise TYPE ("cont_consts: change_cfun", [T], [])
35257
3e5980f612d9 adapted to authentic syntax;
wenzelm
parents: 35130
diff changeset
    25
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    26
fun gen_add_consts prep_typ raw_decls thy =
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    27
  let
81226
e8030f7b1386 misc tuning and clarification;
wenzelm
parents: 80897
diff changeset
    28
    val thy_ctxt = Proof_Context.init_global thy
e8030f7b1386 misc tuning and clarification;
wenzelm
parents: 80897
diff changeset
    29
e8030f7b1386 misc tuning and clarification;
wenzelm
parents: 80897
diff changeset
    30
    fun is_cont_const (_, _, NoSyn) = false
81228
ed326e93b835 misc tuning and clarification;
wenzelm
parents: 81227
diff changeset
    31
      | is_cont_const (_, _, Binder _) = false
81227
2f5c1761541d clarified concrete vs. abstract syntax: avoid translations on logical consts;
wenzelm
parents: 81226
diff changeset
    32
      | is_cont_const (b, T, mx) =
81226
e8030f7b1386 misc tuning and clarification;
wenzelm
parents: 80897
diff changeset
    33
          let
e8030f7b1386 misc tuning and clarification;
wenzelm
parents: 80897
diff changeset
    34
            val n = Mixfix.mixfix_args thy_ctxt mx handle ERROR msg =>
81227
2f5c1761541d clarified concrete vs. abstract syntax: avoid translations on logical consts;
wenzelm
parents: 81226
diff changeset
    35
              cat_error msg ("in mixfix annotation for " ^ Binding.print b)
81226
e8030f7b1386 misc tuning and clarification;
wenzelm
parents: 80897
diff changeset
    36
          in count_cfun T >= n end
e8030f7b1386 misc tuning and clarification;
wenzelm
parents: 80897
diff changeset
    37
81227
2f5c1761541d clarified concrete vs. abstract syntax: avoid translations on logical consts;
wenzelm
parents: 81226
diff changeset
    38
    fun transform (b, T, mx) =
81226
e8030f7b1386 misc tuning and clarification;
wenzelm
parents: 80897
diff changeset
    39
      let
81227
2f5c1761541d clarified concrete vs. abstract syntax: avoid translations on logical consts;
wenzelm
parents: 81226
diff changeset
    40
        val c = Sign.full_name thy b
2f5c1761541d clarified concrete vs. abstract syntax: avoid translations on logical consts;
wenzelm
parents: 81226
diff changeset
    41
        val c1 = Lexicon.mark_syntax c
2f5c1761541d clarified concrete vs. abstract syntax: avoid translations on logical consts;
wenzelm
parents: 81226
diff changeset
    42
        val c2 = Lexicon.mark_const c
81507
08574da77b4a clarified signature: shorten common cases;
wenzelm
parents: 81228
diff changeset
    43
        val xs = Name.invent_global "xa" (Mixfix.mixfix_args thy_ctxt mx)
81228
ed326e93b835 misc tuning and clarification;
wenzelm
parents: 81227
diff changeset
    44
        val trans_rules =
ed326e93b835 misc tuning and clarification;
wenzelm
parents: 81227
diff changeset
    45
          Syntax.Parse_Print_Rule
ed326e93b835 misc tuning and clarification;
wenzelm
parents: 81227
diff changeset
    46
            (Ast.mk_appl (Ast.Constant c1) (map Ast.Variable xs),
ed326e93b835 misc tuning and clarification;
wenzelm
parents: 81227
diff changeset
    47
              fold (fn x => fn t =>
ed326e93b835 misc tuning and clarification;
wenzelm
parents: 81227
diff changeset
    48
                  Ast.mk_appl (Ast.Constant \<^const_syntax>\<open>Rep_cfun\<close>) [t, Ast.Variable x])
ed326e93b835 misc tuning and clarification;
wenzelm
parents: 81227
diff changeset
    49
                xs (Ast.Constant c2)) ::
ed326e93b835 misc tuning and clarification;
wenzelm
parents: 81227
diff changeset
    50
          (if Mixfix.is_infix mx then [Syntax.Parse_Rule (Ast.Constant c1, Ast.Constant c2)] else [])
ed326e93b835 misc tuning and clarification;
wenzelm
parents: 81227
diff changeset
    51
      in ((b, T, NoSyn), (c1, change_cfun (length xs) T, mx), trans_rules) end
81226
e8030f7b1386 misc tuning and clarification;
wenzelm
parents: 80897
diff changeset
    52
81227
2f5c1761541d clarified concrete vs. abstract syntax: avoid translations on logical consts;
wenzelm
parents: 81226
diff changeset
    53
    val decls = map (fn (b, T, mx) => (b, prep_typ thy T, mx)) raw_decls
81226
e8030f7b1386 misc tuning and clarification;
wenzelm
parents: 80897
diff changeset
    54
    val (cont_decls, normal_decls) = List.partition is_cont_const decls
e8030f7b1386 misc tuning and clarification;
wenzelm
parents: 80897
diff changeset
    55
    val transformed_decls = map transform cont_decls
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    56
  in
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    57
    thy
81227
2f5c1761541d clarified concrete vs. abstract syntax: avoid translations on logical consts;
wenzelm
parents: 81226
diff changeset
    58
    |> Sign.add_consts (normal_decls @ map #1 transformed_decls)
2f5c1761541d clarified concrete vs. abstract syntax: avoid translations on logical consts;
wenzelm
parents: 81226
diff changeset
    59
    |> Sign.syntax_global true Syntax.mode_default (map #2 transformed_decls)
81590
e656c5edc352 clarified signature;
wenzelm
parents: 81507
diff changeset
    60
    |> Sign.translations_global true (maps #3 transformed_decls)
40832
4352ca878c41 remove gratuitous semicolons from ML code
huffman
parents: 40774
diff changeset
    61
  end
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    62
40832
4352ca878c41 remove gratuitous semicolons from ML code
huffman
parents: 40774
diff changeset
    63
val add_consts = gen_add_consts Sign.certify_typ
4352ca878c41 remove gratuitous semicolons from ML code
huffman
parents: 40774
diff changeset
    64
val add_consts_cmd = gen_add_consts Syntax.read_typ_global
35257
3e5980f612d9 adapted to authentic syntax;
wenzelm
parents: 35130
diff changeset
    65
40832
4352ca878c41 remove gratuitous semicolons from ML code
huffman
parents: 40774
diff changeset
    66
end