src/Tools/code/code_target.ML
changeset 24841 df8448bc7a8b
parent 24811 3bf788a0c49a
child 24867 e5b55d7be9bb
     1.1 --- a/src/Tools/code/code_target.ML	Thu Oct 04 19:41:54 2007 +0200
     1.2 +++ b/src/Tools/code/code_target.ML	Thu Oct 04 19:41:55 2007 +0200
     1.3 @@ -29,15 +29,17 @@
     1.4      -> string -> string -> theory -> theory;
     1.5    val add_pretty_imperative_monad_bind: string -> string -> theory -> theory;
     1.6  
     1.7 +  val allow_exception: string -> theory -> theory;
     1.8 +
     1.9    type serializer;
    1.10    val add_serializer: string * serializer -> theory -> theory;
    1.11    val get_serializer: theory -> string -> bool -> string option -> string option -> Args.T list
    1.12 -    -> (theory -> string -> string) -> (string -> bool) -> string list option
    1.13 +    -> (theory -> string -> string) -> string list option
    1.14      -> CodeThingol.code -> unit;
    1.15    val assert_serializer: theory -> string -> string;
    1.16  
    1.17    val eval_verbose: bool ref;
    1.18 -  val eval_invoke: theory -> (theory -> string -> string) -> (string -> bool)
    1.19 +  val eval_invoke: theory -> (theory -> string -> string)
    1.20      -> (string * (unit -> 'a) option ref) -> CodeThingol.code
    1.21      -> CodeThingol.iterm * CodeThingol.itype -> string list -> 'a;
    1.22    val code_width: int ref;
    1.23 @@ -302,10 +304,10 @@
    1.24          * ((class * (string * (string * dict list list))) list
    1.25        * (string * const) list));
    1.26  
    1.27 -fun pr_sml allows_exception tyco_syntax const_syntax labelled_name init_syms deresolv is_cons ml_def =
    1.28 +fun pr_sml tyco_syntax const_syntax labelled_name init_syms deresolv is_cons ml_def =
    1.29    let
    1.30      val pr_label_classrel = translate_string (fn "." => "__" | c => c) o NameSpace.qualifier;
    1.31 -    val pr_label_classop = NameSpace.base o NameSpace.qualifier;
    1.32 +    val pr_label_classparam = NameSpace.base o NameSpace.qualifier;
    1.33      fun pr_dicts fxy ds =
    1.34        let
    1.35          fun pr_dictvar (v, (_, 1)) = first_upper v ^ "_"
    1.36 @@ -422,47 +424,65 @@
    1.37            let
    1.38              val definer =
    1.39                let
    1.40 -                fun mk [] [] = "val"
    1.41 -                  | mk (_::_) _ = "fun"
    1.42 -                  | mk [] vs = if (null o filter_out (null o snd)) vs then "val" else "fun";
    1.43 -                fun chk (_, ((vs, _), (ts, _) :: _)) NONE = SOME (mk ts vs)
    1.44 -                  | chk (_, ((vs, _), (ts, _) :: _)) (SOME defi) =
    1.45 -                      if defi = mk ts vs then SOME defi
    1.46 +                fun no_args _ ((ts, _) :: _) = length ts
    1.47 +                  | no_args ty [] = (length o fst o CodeThingol.unfold_fun) ty;
    1.48 +                fun mk 0 [] = "val"
    1.49 +                  | mk 0 vs = if (null o filter_out (null o snd)) vs then "val" else "fun"
    1.50 +                  | mk k _ = "fun";
    1.51 +                fun chk (_, ((vs, ty), eqs)) NONE = SOME (mk (no_args ty eqs) vs)
    1.52 +                  | chk (_, ((vs, ty), eqs)) (SOME defi) =
    1.53 +                      if defi = mk (no_args ty eqs) vs then SOME defi
    1.54                        else error ("Mixing simultaneous vals and funs not implemented: "
    1.55                          ^ commas (map (labelled_name o fst) funns));
    1.56                in the (fold chk funns NONE) end;
    1.57 -            fun pr_funn definer (name, ((raw_vs, ty), eqs as eq :: eqs')) =
    1.58 -              let
    1.59 -                val vs = filter_out (null o snd) raw_vs;
    1.60 -                val shift = if null eqs' then I else
    1.61 -                  map (Pretty.block o single o Pretty.block o single);
    1.62 -                fun pr_eq definer (ts, t) =
    1.63 +            fun pr_funn definer (name, ((raw_vs, ty), [])) =
    1.64                    let
    1.65 -                    val consts = map_filter
    1.66 -                      (fn c => if (is_some o const_syntax) c
    1.67 -                        then NONE else (SOME o NameSpace.base o deresolv) c)
    1.68 -                        ((fold o CodeThingol.fold_constnames) (insert (op =)) (t :: ts) []);
    1.69 -                    val vars = init_syms
    1.70 -                      |> CodeName.intro_vars consts
    1.71 -                      |> CodeName.intro_vars ((fold o CodeThingol.fold_unbound_varnames)
    1.72 -                           (insert (op =)) ts []);
    1.73 +                    val vs = filter_out (null o snd) raw_vs;
    1.74 +                    val n = length vs + (length o fst o CodeThingol.unfold_fun) ty;
    1.75                    in
    1.76                      concat (
    1.77 -                      [str definer, (str o deresolv) name]
    1.78 -                      @ (if null ts andalso null vs
    1.79 -                         then [str ":", pr_typ NOBR ty]
    1.80 -                         else
    1.81 -                           pr_tyvars vs
    1.82 -                           @ map (pr_term true vars BR) ts)
    1.83 -                   @ [str "=", pr_term false vars NOBR t]
    1.84 +                      str definer
    1.85 +                      :: (str o deresolv) name
    1.86 +                      :: map str (replicate n "_")
    1.87 +                      @ str "="
    1.88 +                      :: str "raise"
    1.89 +                      :: str "(Fail"
    1.90 +                      :: (str o ML_Syntax.print_string o NameSpace.base o NameSpace.qualifier) name
    1.91 +                      @@ str ")"
    1.92                      )
    1.93                    end
    1.94 -              in
    1.95 -                (Pretty.block o Pretty.fbreaks o shift) (
    1.96 -                  pr_eq definer eq
    1.97 -                  :: map (pr_eq "|") eqs'
    1.98 -                )
    1.99 -              end;
   1.100 +              | pr_funn definer (name, ((raw_vs, ty), eqs as eq :: eqs')) =
   1.101 +                  let
   1.102 +                    val vs = filter_out (null o snd) raw_vs;
   1.103 +                    val shift = if null eqs' then I else
   1.104 +                      map (Pretty.block o single o Pretty.block o single);
   1.105 +                    fun pr_eq definer (ts, t) =
   1.106 +                      let
   1.107 +                        val consts = map_filter
   1.108 +                          (fn c => if (is_some o const_syntax) c
   1.109 +                            then NONE else (SOME o NameSpace.base o deresolv) c)
   1.110 +                            ((fold o CodeThingol.fold_constnames) (insert (op =)) (t :: ts) []);
   1.111 +                        val vars = init_syms
   1.112 +                          |> CodeName.intro_vars consts
   1.113 +                          |> CodeName.intro_vars ((fold o CodeThingol.fold_unbound_varnames)
   1.114 +                               (insert (op =)) ts []);
   1.115 +                      in
   1.116 +                        concat (
   1.117 +                          [str definer, (str o deresolv) name]
   1.118 +                          @ (if null ts andalso null vs
   1.119 +                             then [str ":", pr_typ NOBR ty]
   1.120 +                             else
   1.121 +                               pr_tyvars vs
   1.122 +                               @ map (pr_term true vars BR) ts)
   1.123 +                       @ [str "=", pr_term false vars NOBR t]
   1.124 +                        )
   1.125 +                      end
   1.126 +                  in
   1.127 +                    (Pretty.block o Pretty.fbreaks o shift) (
   1.128 +                      pr_eq definer eq
   1.129 +                      :: map (pr_eq "|") eqs'
   1.130 +                    )
   1.131 +                  end;
   1.132              val (ps, p) = split_last (pr_funn definer funn :: map (pr_funn "and") funns');
   1.133            in Pretty.chunks (ps @ [Pretty.block ([p, str ";"])]) end
   1.134       | pr_def (MLDatas (datas as (data :: datas'))) =
   1.135 @@ -491,24 +511,24 @@
   1.136                    );
   1.137              val (ps, p) = split_last (pr_data "datatype" data :: map (pr_data "and") datas');
   1.138            in Pretty.chunks (ps @ [Pretty.block ([p, str ";"])]) end
   1.139 -     | pr_def (MLClass (class, (v, (superclasses, classops)))) =
   1.140 +     | pr_def (MLClass (class, (v, (superclasses, classparams)))) =
   1.141            let
   1.142              val w = first_upper v ^ "_";
   1.143              fun pr_superclass_field (class, classrel) =
   1.144                (concat o map str) [
   1.145                  pr_label_classrel classrel, ":", "'" ^ v, deresolv class
   1.146                ];
   1.147 -            fun pr_classop_field (classop, ty) =
   1.148 +            fun pr_classparam_field (classparam, ty) =
   1.149                concat [
   1.150 -                (str o pr_label_classop) classop, str ":", pr_typ NOBR ty
   1.151 +                (str o pr_label_classparam) classparam, str ":", pr_typ NOBR ty
   1.152                ];
   1.153 -            fun pr_classop_proj (classop, _) =
   1.154 +            fun pr_classparam_proj (classparam, _) =
   1.155                semicolon [
   1.156                  str "fun",
   1.157 -                (str o deresolv) classop,
   1.158 +                (str o deresolv) classparam,
   1.159                  Pretty.enclose "(" ")" [str (w ^ ":'" ^ v ^ " " ^ deresolv class)],
   1.160                  str "=",
   1.161 -                str ("#" ^ pr_label_classop classop),
   1.162 +                str ("#" ^ pr_label_classparam classparam),
   1.163                  str w
   1.164                ];
   1.165              fun pr_superclass_proj (_, classrel) =
   1.166 @@ -527,14 +547,14 @@
   1.167                  (str o deresolv) class,
   1.168                  str "=",
   1.169                  Pretty.enum "," "{" "};" (
   1.170 -                  map pr_superclass_field superclasses @ map pr_classop_field classops
   1.171 +                  map pr_superclass_field superclasses @ map pr_classparam_field classparams
   1.172                  )
   1.173                ]
   1.174                :: map pr_superclass_proj superclasses
   1.175 -              @ map pr_classop_proj classops
   1.176 +              @ map pr_classparam_proj classparams
   1.177              )
   1.178            end
   1.179 -     | pr_def (MLClassinst (inst, ((class, (tyco, arity)), (superarities, classop_defs)))) =
   1.180 +     | pr_def (MLClassinst (inst, ((class, (tyco, arity)), (superarities, classparam_insts)))) =
   1.181            let
   1.182              fun pr_superclass (_, (classrel, dss)) =
   1.183                concat [
   1.184 @@ -542,9 +562,9 @@
   1.185                  str "=",
   1.186                  pr_dicts NOBR [DictConst dss]
   1.187                ];
   1.188 -            fun pr_classop (classop, c_inst) =
   1.189 +            fun pr_classparam (classparam, c_inst) =
   1.190                concat [
   1.191 -                (str o pr_label_classop) classop,
   1.192 +                (str o pr_label_classparam) classparam,
   1.193                  str "=",
   1.194                  pr_app false init_syms NOBR (c_inst, [])
   1.195                ];
   1.196 @@ -554,7 +574,7 @@
   1.197                (str o deresolv) inst ] @
   1.198                pr_tyvars arity @ [
   1.199                str "=",
   1.200 -              Pretty.enum "," "{" "}" (map pr_superclass superarities @ map pr_classop classop_defs),
   1.201 +              Pretty.enum "," "{" "}" (map pr_superclass superarities @ map pr_classparam classparam_insts),
   1.202                str ":",
   1.203                pr_tycoexpr NOBR (class, [tyco `%% map (ITyVar o fst) arity])
   1.204              ])
   1.205 @@ -571,7 +591,7 @@
   1.206      str ("end; (*struct " ^ name ^ "*)")
   1.207    ]);
   1.208  
   1.209 -fun pr_ocaml allows_exception tyco_syntax const_syntax labelled_name
   1.210 +fun pr_ocaml tyco_syntax const_syntax labelled_name
   1.211      init_syms deresolv is_cons ml_def =
   1.212    let
   1.213      fun pr_dicts fxy ds =
   1.214 @@ -702,7 +722,18 @@
   1.215                  str "->",
   1.216                  pr_term false vars NOBR t
   1.217                ] end;
   1.218 -            fun pr_eqs [(ts, t)] =
   1.219 +            fun pr_eqs name ty [] =
   1.220 +                  let
   1.221 +                    val n = (length o fst o CodeThingol.unfold_fun) ty;
   1.222 +                  in
   1.223 +                    concat (
   1.224 +                      map str (replicate n "_")
   1.225 +                      @ str "="
   1.226 +                      :: str "failwith"
   1.227 +                      @@ (str o ML_Syntax.print_string o NameSpace.base o NameSpace.qualifier) name
   1.228 +                    )
   1.229 +                  end
   1.230 +              | pr_eqs _ _ [(ts, t)] =
   1.231                    let
   1.232                      val consts = map_filter
   1.233                        (fn c => if (is_some o const_syntax) c
   1.234 @@ -719,7 +750,7 @@
   1.235                        @@ pr_term false vars NOBR t
   1.236                      )
   1.237                    end
   1.238 -              | pr_eqs (eqs as (eq as ([_], _)) :: eqs') =
   1.239 +              | pr_eqs _ _ (eqs as (eq as ([_], _)) :: eqs') =
   1.240                    Pretty.block (
   1.241                      str "="
   1.242                      :: Pretty.brk 1
   1.243 @@ -728,7 +759,7 @@
   1.244                      :: pr_eq eq
   1.245                      :: maps (append [Pretty.fbrk, str "|", Pretty.brk 1] o single o pr_eq) eqs'
   1.246                    )
   1.247 -              | pr_eqs (eqs as eq :: eqs') =
   1.248 +              | pr_eqs _ _ (eqs as eq :: eqs') =
   1.249                    let
   1.250                      val consts = map_filter
   1.251                        (fn c => if (is_some o const_syntax) c
   1.252 @@ -758,7 +789,7 @@
   1.253                  str definer
   1.254                  :: (str o deresolv) name
   1.255                  :: pr_tyvars (filter_out (null o snd) vs)
   1.256 -                @| pr_eqs eqs
   1.257 +                @| pr_eqs name ty eqs
   1.258                );
   1.259              val (ps, p) = split_last (pr_funn "let rec" funn :: map (pr_funn "and") funns');
   1.260            in Pretty.chunks (ps @ [Pretty.block ([p, str ";;"])]) end
   1.261 @@ -788,24 +819,24 @@
   1.262                    );
   1.263              val (ps, p) = split_last (pr_data "type" data :: map (pr_data "and") datas');
   1.264            in Pretty.chunks (ps @ [Pretty.block ([p, str ";;"])]) end
   1.265 -     | pr_def (MLClass (class, (v, (superclasses, classops)))) =
   1.266 +     | pr_def (MLClass (class, (v, (superclasses, classparams)))) =
   1.267            let
   1.268              val w = "_" ^ first_upper v;
   1.269              fun pr_superclass_field (class, classrel) =
   1.270                (concat o map str) [
   1.271                  deresolv classrel, ":", "'" ^ v, deresolv class
   1.272                ];
   1.273 -            fun pr_classop_field (classop, ty) =
   1.274 +            fun pr_classparam_field (classparam, ty) =
   1.275                concat [
   1.276 -                (str o deresolv) classop, str ":", pr_typ NOBR ty
   1.277 +                (str o deresolv) classparam, str ":", pr_typ NOBR ty
   1.278                ];
   1.279 -            fun pr_classop_proj (classop, _) =
   1.280 +            fun pr_classparam_proj (classparam, _) =
   1.281                concat [
   1.282                  str "let",
   1.283 -                (str o deresolv) classop,
   1.284 +                (str o deresolv) classparam,
   1.285                  str w,
   1.286                  str "=",
   1.287 -                str (w ^ "." ^ deresolv classop ^ ";;")
   1.288 +                str (w ^ "." ^ deresolv classparam ^ ";;")
   1.289                ];
   1.290            in Pretty.chunks (
   1.291              concat [
   1.292 @@ -813,12 +844,12 @@
   1.293                (str o deresolv) class,
   1.294                str "=",
   1.295                Pretty.enum ";" "{" "};;" (
   1.296 -                map pr_superclass_field superclasses @ map pr_classop_field classops
   1.297 +                map pr_superclass_field superclasses @ map pr_classparam_field classparams
   1.298                )
   1.299              ]
   1.300 -            :: map pr_classop_proj classops
   1.301 +            :: map pr_classparam_proj classparams
   1.302            ) end
   1.303 -     | pr_def (MLClassinst (inst, ((class, (tyco, arity)), (superarities, classop_defs)))) =
   1.304 +     | pr_def (MLClassinst (inst, ((class, (tyco, arity)), (superarities, classparam_insts)))) =
   1.305            let
   1.306              fun pr_superclass (_, (classrel, dss)) =
   1.307                concat [
   1.308 @@ -826,9 +857,9 @@
   1.309                  str "=",
   1.310                  pr_dicts NOBR [DictConst dss]
   1.311                ];
   1.312 -            fun pr_classop_def (classop, c_inst) =
   1.313 +            fun pr_classparam_inst (classparam, c_inst) =
   1.314                concat [
   1.315 -                (str o deresolv) classop,
   1.316 +                (str o deresolv) classparam,
   1.317                  str "=",
   1.318                  pr_app false init_syms NOBR (c_inst, [])
   1.319                ];
   1.320 @@ -839,7 +870,7 @@
   1.321                :: pr_tyvars arity
   1.322                @ str "="
   1.323                @@ (Pretty.enclose "(" ");;" o Pretty.breaks) [
   1.324 -                Pretty.enum ";" "{" "}" (map pr_superclass superarities @ map pr_classop_def classop_defs),
   1.325 +                Pretty.enum ";" "{" "}" (map pr_superclass superarities @ map pr_classparam_inst classparam_insts),
   1.326                  str ":",
   1.327                  pr_tycoexpr NOBR (class, [tyco `%% map (ITyVar o fst) arity])
   1.328                ]
   1.329 @@ -861,7 +892,7 @@
   1.330  fun code_output p = Pretty.setmp_margin (!code_width) Pretty.output p ^ "\n";
   1.331  
   1.332  fun seri_ml pr_def pr_modl module output labelled_name reserved_syms raw_module_alias module_prolog
   1.333 -  allows_exception (_ : string -> class_syntax option) tyco_syntax const_syntax code =
   1.334 +  (_ : string -> class_syntax option) tyco_syntax const_syntax code =
   1.335    let
   1.336      val module_alias = if is_some module then K module else raw_module_alias;
   1.337      val is_cons = CodeThingol.is_cons code;
   1.338 @@ -928,7 +959,7 @@
   1.339                map_nsp_typ (name_def false name) >> (fn base => (base, SOME (name, info)))
   1.340            | (name, CodeThingol.Classrel _) =>
   1.341                map_nsp_fun (name_def false name) >> (fn base => (base, NONE))
   1.342 -          | (name, CodeThingol.Classop _) =>
   1.343 +          | (name, CodeThingol.Classparam _) =>
   1.344                map_nsp_fun (name_def false name) >> (fn base => (base, NONE))
   1.345            | (name, def) => error ("Class block containing illegal definition: " ^ labelled_name name)
   1.346          ) defs
   1.347 @@ -990,7 +1021,7 @@
   1.348            add_group mk_class defs
   1.349        | group_defs ((defs as (_, CodeThingol.Classrel _)::_)) =
   1.350            add_group mk_class defs
   1.351 -      | group_defs ((defs as (_, CodeThingol.Classop _)::_)) =
   1.352 +      | group_defs ((defs as (_, CodeThingol.Classparam _)::_)) =
   1.353            add_group mk_class defs
   1.354        | group_defs ((defs as [(_, CodeThingol.Classinst _)])) =
   1.355            add_group mk_inst defs
   1.356 @@ -1020,7 +1051,7 @@
   1.357      fun pr_node prefix (Def (_, NONE)) =
   1.358            NONE
   1.359        | pr_node prefix (Def (_, SOME def)) =
   1.360 -          SOME (pr_def allows_exception tyco_syntax const_syntax labelled_name init_syms
   1.361 +          SOME (pr_def tyco_syntax const_syntax labelled_name init_syms
   1.362              (deresolver prefix) is_cons def)
   1.363        | pr_node prefix (Module (dmodlname, (_, nodes))) =
   1.364            SOME (pr_modl dmodlname (the_prolog (NameSpace.implode (prefix @ [dmodlname]))
   1.365 @@ -1070,17 +1101,17 @@
   1.366  
   1.367  in
   1.368  
   1.369 -fun pr_haskell allows_exception class_syntax tyco_syntax const_syntax labelled_name
   1.370 +fun pr_haskell class_syntax tyco_syntax const_syntax labelled_name
   1.371      init_syms deresolv_here deresolv is_cons deriving_show def =
   1.372    let
   1.373      fun class_name class = case class_syntax class
   1.374       of NONE => deresolv class
   1.375        | SOME (class, _) => class;
   1.376 -    fun classop_name class classop = case class_syntax class
   1.377 -     of NONE => deresolv_here classop
   1.378 -      | SOME (_, classop_syntax) => case classop_syntax classop
   1.379 -         of NONE => (snd o dest_name) classop
   1.380 -          | SOME classop => classop
   1.381 +    fun classparam_name class classparam = case class_syntax class
   1.382 +     of NONE => deresolv_here classparam
   1.383 +      | SOME (_, classparam_syntax) => case classparam_syntax classparam
   1.384 +         of NONE => (snd o dest_name) classparam
   1.385 +          | SOME classparam => classparam
   1.386      fun pr_typparms tyvars vs =
   1.387        case maps (fn (v, sort) => map (pair v) sort) vs
   1.388         of [] => str ""
   1.389 @@ -1163,7 +1194,28 @@
   1.390              ) (map pr bs)
   1.391            end
   1.392        | pr_case vars fxy ((_, []), _) = str "error \"empty case\"";
   1.393 -    fun pr_def (name, CodeThingol.Fun ((vs, ty), eqs)) =
   1.394 +    fun pr_def (name, CodeThingol.Fun ((vs, ty), [])) =
   1.395 +          let
   1.396 +            val tyvars = CodeName.intro_vars (map fst vs) init_syms;
   1.397 +            val n = (length o fst o CodeThingol.unfold_fun) ty;
   1.398 +          in
   1.399 +            Pretty.chunks [
   1.400 +              Pretty.block [
   1.401 +                (str o suffix " ::" o deresolv_here) name,
   1.402 +                Pretty.brk 1,
   1.403 +                pr_typscheme tyvars (vs, ty),
   1.404 +                str ";"
   1.405 +              ],
   1.406 +              concat (
   1.407 +                (str o deresolv_here) name
   1.408 +                :: map str (replicate n "_")
   1.409 +                @ str "="
   1.410 +                :: str "error"
   1.411 +                @@ (str o (fn s => s ^ ";") o ML_Syntax.print_string o NameSpace.base o NameSpace.qualifier) name
   1.412 +              )
   1.413 +            ]
   1.414 +          end
   1.415 +      | pr_def (name, CodeThingol.Fun ((vs, ty), eqs)) =
   1.416            let
   1.417              val tyvars = CodeName.intro_vars (map fst vs) init_syms;
   1.418              fun pr_eq ((ts, t), _) =
   1.419 @@ -1235,12 +1287,12 @@
   1.420                @ (if deriving_show name then [str "deriving (Read, Show)"] else [])
   1.421              )
   1.422            end
   1.423 -      | pr_def (name, CodeThingol.Class (v, (superclasses, classops))) =
   1.424 +      | pr_def (name, CodeThingol.Class (v, (superclasses, classparams))) =
   1.425            let
   1.426              val tyvars = CodeName.intro_vars [v] init_syms;
   1.427 -            fun pr_classop (classop, ty) =
   1.428 +            fun pr_classparam (classparam, ty) =
   1.429                semicolon [
   1.430 -                (str o classop_name name) classop,
   1.431 +                (str o classparam_name name) classparam,
   1.432                  str "::",
   1.433                  pr_typ tyvars NOBR ty
   1.434                ]
   1.435 @@ -1253,14 +1305,14 @@
   1.436                  str " where {"
   1.437                ],
   1.438                str "};"
   1.439 -            ) (map pr_classop classops)
   1.440 +            ) (map pr_classparam classparams)
   1.441            end
   1.442 -      | pr_def (_, CodeThingol.Classinst ((class, (tyco, vs)), (_, classop_defs))) =
   1.443 +      | pr_def (_, CodeThingol.Classinst ((class, (tyco, vs)), (_, classparam_insts))) =
   1.444            let
   1.445              val tyvars = CodeName.intro_vars (map fst vs) init_syms;
   1.446 -            fun pr_instdef ((classop, c_inst), _) =
   1.447 +            fun pr_instdef ((classparam, c_inst), _) =
   1.448                semicolon [
   1.449 -                (str o classop_name class) classop,
   1.450 +                (str o classparam_name class) classparam,
   1.451                  str "=",
   1.452                  pr_app false init_syms NOBR (c_inst, [])
   1.453                ];
   1.454 @@ -1274,7 +1326,7 @@
   1.455                  str " where {"
   1.456                ],
   1.457                str "};"
   1.458 -            ) (map pr_instdef classop_defs)
   1.459 +            ) (map pr_instdef classparam_insts)
   1.460            end;
   1.461    in pr_def def end;
   1.462  
   1.463 @@ -1301,7 +1353,7 @@
   1.464  
   1.465  fun seri_haskell module_prefix module destination string_classes labelled_name
   1.466      reserved_syms raw_module_alias module_prolog
   1.467 -    allows_exception class_syntax tyco_syntax const_syntax code =
   1.468 +    class_syntax tyco_syntax const_syntax code =
   1.469    let
   1.470      val _ = Option.map File.check destination;
   1.471      val is_cons = CodeThingol.is_cons code;
   1.472 @@ -1328,7 +1380,7 @@
   1.473              | CodeThingol.Datatypecons _ => add_fun true
   1.474              | CodeThingol.Class _ => add_typ
   1.475              | CodeThingol.Classrel _ => pair base
   1.476 -            | CodeThingol.Classop _ => add_fun false
   1.477 +            | CodeThingol.Classparam _ => add_fun false
   1.478              | CodeThingol.Classinst _ => pair base;
   1.479          val modlname' = name_modl modl;
   1.480          fun add_def base' =
   1.481 @@ -1337,7 +1389,7 @@
   1.482              | CodeThingol.Datatypecons _ =>
   1.483                  cons (name, ((NameSpace.append modlname' base', base'), NONE))
   1.484              | CodeThingol.Classrel _ => I
   1.485 -            | CodeThingol.Classop _ =>
   1.486 +            | CodeThingol.Classparam _ =>
   1.487                  cons (name, ((NameSpace.append modlname' base', base'), NONE))
   1.488              | _ => cons (name, ((NameSpace.append modlname' base', base'), SOME def));
   1.489        in
   1.490 @@ -1372,7 +1424,7 @@
   1.491                andalso forall (deriv' tycos) tys
   1.492            | deriv' _ (ITyVar _) = true
   1.493        in deriv [] tyco end;
   1.494 -    fun seri_def qualified = pr_haskell allows_exception class_syntax tyco_syntax
   1.495 +    fun seri_def qualified = pr_haskell class_syntax tyco_syntax
   1.496        const_syntax labelled_name init_syms
   1.497        deresolv_here (if qualified then deresolv else deresolv_here) is_cons
   1.498        (if string_classes then deriving_show else K false);
   1.499 @@ -1436,7 +1488,7 @@
   1.500  
   1.501  (** diagnosis serializer **)
   1.502  
   1.503 -fun seri_diagnosis labelled_name _ _ _ _ _ _ _ code =
   1.504 +fun seri_diagnosis labelled_name _ _ _ _ _ _ code =
   1.505    let
   1.506      val init_names = CodeName.make_vars [];
   1.507      fun pr_fun "fun" = SOME (2, fn pr_typ => fn fxy => fn [ty1, ty2] =>
   1.508 @@ -1446,7 +1498,7 @@
   1.509              pr_typ (INFX (1, R)) ty2
   1.510            ])
   1.511        | pr_fun _ = NONE
   1.512 -    val pr = pr_haskell (K true) (K NONE) pr_fun (K NONE) labelled_name init_names I I (K false) (K false);
   1.513 +    val pr = pr_haskell (K NONE) pr_fun (K NONE) labelled_name init_names I I (K false) (K false);
   1.514    in
   1.515      []
   1.516      |> Graph.fold (fn (name, (def, _)) => case try pr (name, def) of SOME p => cons p | NONE => I) code
   1.517 @@ -1503,7 +1555,6 @@
   1.518    -> string list
   1.519    -> (string -> string option)
   1.520    -> (string -> Pretty.T option)
   1.521 -  -> (string -> bool)
   1.522    -> (string -> class_syntax option)
   1.523    -> (string -> typ_syntax option)
   1.524    -> (string -> term_syntax option)
   1.525 @@ -1512,9 +1563,9 @@
   1.526  datatype target = Target of {
   1.527    serial: serial,
   1.528    serializer: serializer,
   1.529 +  reserved: string list,
   1.530    syntax_expr: syntax_expr,
   1.531 -  syntax_modl: syntax_modl,
   1.532 -  reserved: string list
   1.533 +  syntax_modl: syntax_modl
   1.534  };
   1.535  
   1.536  fun mk_target (serial, ((serializer, reserved), (syntax_expr, syntax_modl))) =
   1.537 @@ -1535,11 +1586,12 @@
   1.538  
   1.539  structure CodeTargetData = TheoryDataFun
   1.540  (
   1.541 -  type T = target Symtab.table;
   1.542 -  val empty = Symtab.empty;
   1.543 +  type T = target Symtab.table * string list;
   1.544 +  val empty = (Symtab.empty, []);
   1.545    val copy = I;
   1.546    val extend = I;
   1.547 -  fun merge _ = Symtab.join merge_target;
   1.548 +  fun merge _ ((target1, exc1), (target2, exc2)) =
   1.549 +    (Symtab.join merge_target (target1, target2), Library.merge (op =) (exc1, exc2));
   1.550  );
   1.551  
   1.552  fun the_serializer (Target { serializer, ... }) = serializer;
   1.553 @@ -1548,18 +1600,18 @@
   1.554  fun the_syntax_modl (Target { syntax_modl = SyntaxModl x, ... }) = x;
   1.555  
   1.556  fun assert_serializer thy target =
   1.557 -  case Symtab.lookup (CodeTargetData.get thy) target
   1.558 +  case Symtab.lookup (fst (CodeTargetData.get thy)) target
   1.559     of SOME data => target
   1.560      | NONE => error ("Unknown code target language: " ^ quote target);
   1.561  
   1.562  fun add_serializer (target, seri) thy =
   1.563    let
   1.564 -    val _ = case Symtab.lookup (CodeTargetData.get thy) target
   1.565 +    val _ = case Symtab.lookup (fst (CodeTargetData.get thy)) target
   1.566       of SOME _ => warning ("overwriting existing serializer " ^ quote target)
   1.567        | NONE => ();
   1.568    in
   1.569      thy
   1.570 -    |> (CodeTargetData.map oo Symtab.map_default)
   1.571 +    |> (CodeTargetData.map o apfst oo Symtab.map_default)
   1.572            (target, mk_target (serial (), ((seri, []),
   1.573              (mk_syntax_expr ((Symtab.empty, Symtab.empty), (Symtab.empty, Symtab.empty)),
   1.574                mk_syntax_modl (Symtab.empty, Symtab.empty)))))
   1.575 @@ -1571,7 +1623,7 @@
   1.576      val _ = assert_serializer thy target;
   1.577    in
   1.578      thy
   1.579 -    |> (CodeTargetData.map o Symtab.map_entry target o map_target) f
   1.580 +    |> (CodeTargetData.map o apfst o Symtab.map_entry target o map_target) f
   1.581    end;
   1.582  
   1.583  val target_SML = "SML";
   1.584 @@ -1580,9 +1632,10 @@
   1.585  val target_diag = "diag";
   1.586  
   1.587  fun get_serializer thy target permissive module file args
   1.588 -    labelled_name allows_exception = fn cs =>
   1.589 +    labelled_name = fn cs =>
   1.590    let
   1.591 -    val data = case Symtab.lookup (CodeTargetData.get thy) target
   1.592 +    val (seris, exc) = CodeTargetData.get thy;
   1.593 +    val data = case Symtab.lookup seris target
   1.594       of SOME data => data
   1.595        | NONE => error ("Unknown code target language: " ^ quote target);
   1.596      val seri = the_serializer data;
   1.597 @@ -1592,17 +1645,18 @@
   1.598      val project = if target = target_diag then I
   1.599        else CodeThingol.project_code permissive
   1.600          (Symtab.keys class @ Symtab.keys inst @ Symtab.keys tyco @ Symtab.keys const) cs;
   1.601 -    fun check_empty_funs code = case CodeThingol.empty_funs code
   1.602 +    fun check_empty_funs code = case (filter_out (member (op =) exc)
   1.603 +        (CodeThingol.empty_funs code))
   1.604       of [] => code
   1.605        | names => error ("No defining equations for " ^ commas (map (labelled_name thy) names));
   1.606    in
   1.607      project
   1.608      #> check_empty_funs
   1.609      #> seri module file args (labelled_name thy) reserved (Symtab.lookup alias) (Symtab.lookup prolog)
   1.610 -      allows_exception (Symtab.lookup class) (Symtab.lookup tyco) (Symtab.lookup const)
   1.611 +      (Symtab.lookup class) (Symtab.lookup tyco) (Symtab.lookup const)
   1.612    end;
   1.613  
   1.614 -fun eval_invoke thy labelled_name allows_exception (ref_name, reff) code (t, ty) args =
   1.615 +fun eval_invoke thy labelled_name (ref_name, reff) code (t, ty) args =
   1.616    let
   1.617      val _ = if CodeThingol.contains_dictvar t then
   1.618        error "Term to be evaluated constains free dictionaries" else ();
   1.619 @@ -1610,7 +1664,7 @@
   1.620      val val_name' = "Isabelle_Eval.EVAL";
   1.621      val val_name'_args = space_implode " " (val_name' :: map (enclose "(" ")") args);
   1.622      val seri = get_serializer thy "SML" false (SOME "Isabelle_Eval") NONE []
   1.623 -      labelled_name allows_exception;
   1.624 +      labelled_name;
   1.625      fun eval code = (
   1.626        reff := NONE;
   1.627        seri (SOME [val_name]) code;
   1.628 @@ -1783,23 +1837,23 @@
   1.629  
   1.630  fun gen_add_syntax_class prep_class prep_const target raw_class raw_syn thy =
   1.631    let
   1.632 -    val cls = prep_class thy raw_class;
   1.633 -    val class = CodeName.class thy cls;
   1.634 -    fun mk_classop c = case AxClass.class_of_param thy c
   1.635 -     of SOME class' => if cls = class' then CodeName.const thy c
   1.636 +    val class = prep_class thy raw_class;
   1.637 +    val class' = CodeName.class thy class;
   1.638 +    fun mk_classparam c = case AxClass.class_of_param thy c
   1.639 +     of SOME class'' => if class = class'' then CodeName.const thy c
   1.640            else error ("Not a class operation for class " ^ quote class ^ ": " ^ quote c)
   1.641        | NONE => error ("Not a class operation: " ^ quote c);
   1.642 -    fun mk_syntax_ops raw_ops = AList.lookup (op =)
   1.643 -      ((map o apfst) (mk_classop o prep_const thy) raw_ops);
   1.644 +    fun mk_syntax_params raw_params = AList.lookup (op =)
   1.645 +      ((map o apfst) (mk_classparam o prep_const thy) raw_params);
   1.646    in case raw_syn
   1.647 -   of SOME (syntax, raw_ops) =>
   1.648 +   of SOME (syntax, raw_params) =>
   1.649        thy
   1.650        |> (map_syntax_exprs target o apfst o apfst)
   1.651 -           (Symtab.update (class, (syntax, mk_syntax_ops raw_ops)))
   1.652 +           (Symtab.update (class', (syntax, mk_syntax_params raw_params)))
   1.653      | NONE =>
   1.654        thy
   1.655        |> (map_syntax_exprs target o apfst o apfst)
   1.656 -           (Symtab.delete_safe class)
   1.657 +           (Symtab.delete_safe class')
   1.658    end;
   1.659  
   1.660  fun gen_add_syntax_inst prep_class prep_tyco target (raw_tyco, raw_class) add_del thy =
   1.661 @@ -1910,6 +1964,12 @@
   1.662      (fn (modl, NONE) => Symtab.delete modl | (modl, SOME prolog) =>
   1.663        Symtab.update (modl, Pretty.str prolog));
   1.664  
   1.665 +fun gen_allow_exception prep_cs raw_c thy =
   1.666 +  let
   1.667 +    val c = prep_cs thy raw_c;
   1.668 +    val c' = CodeName.const thy c;
   1.669 +  in thy |> (CodeTargetData.map o apsnd) (insert (op =) c') end;
   1.670 +
   1.671  fun zip_list (x::xs) f g =
   1.672    f
   1.673    #-> (fn y =>
   1.674 @@ -1937,9 +1997,9 @@
   1.675        >> (fn (parse, s) => parse s)) xs;
   1.676  
   1.677  val (code_classK, code_instanceK, code_typeK, code_constK, code_monadK,
   1.678 -  code_reservedK, code_modulenameK, code_moduleprologK) =
   1.679 +  code_reservedK, code_modulenameK, code_moduleprologK, code_exceptionK) =
   1.680    ("code_class", "code_instance", "code_type", "code_const", "code_monad",
   1.681 -    "code_reserved", "code_modulename", "code_moduleprolog");
   1.682 +    "code_reserved", "code_modulename", "code_moduleprolog", "code_exception");
   1.683  
   1.684  in
   1.685  
   1.686 @@ -1949,11 +2009,13 @@
   1.687  val add_syntax_inst = gen_add_syntax_inst cert_class cert_tyco;
   1.688  val add_syntax_tyco = gen_add_syntax_tyco cert_tyco;
   1.689  val add_syntax_const = gen_add_syntax_const (K I);
   1.690 +val allow_exception = gen_allow_exception (K I);
   1.691  
   1.692  val add_syntax_class_cmd = gen_add_syntax_class read_class CodeUnit.read_const;
   1.693  val add_syntax_inst_cmd = gen_add_syntax_inst read_class read_tyco;
   1.694  val add_syntax_tyco_cmd = gen_add_syntax_tyco read_tyco;
   1.695  val add_syntax_const_cmd = gen_add_syntax_const CodeUnit.read_const;
   1.696 +val allow_exception_cmd = gen_allow_exception CodeUnit.read_const;
   1.697  
   1.698  fun add_syntax_tycoP target tyco = parse_syntax I >> add_syntax_tyco_cmd target tyco;
   1.699  fun add_syntax_constP target c = parse_syntax fst >> (add_syntax_const_cmd target c o no_bindings);
   1.700 @@ -2071,24 +2133,29 @@
   1.701    OuterSyntax.command code_reservedK "declare words as reserved for target language" K.thy_decl (
   1.702      P.name -- Scan.repeat1 P.name
   1.703      >> (fn (target, reserveds) => (Toplevel.theory o fold (add_reserved target)) reserveds)
   1.704 -  )
   1.705 +  );
   1.706  
   1.707  val code_modulenameP =
   1.708    OuterSyntax.command code_modulenameK "alias module to other name" K.thy_decl (
   1.709      P.name -- Scan.repeat1 (P.name -- P.name)
   1.710      >> (fn (target, modlnames) => (Toplevel.theory o fold (add_modl_alias target)) modlnames)
   1.711 -  )
   1.712 +  );
   1.713  
   1.714  val code_moduleprologP =
   1.715    OuterSyntax.command code_moduleprologK "add prolog to module" K.thy_decl (
   1.716      P.name -- Scan.repeat1 (P.name -- (P.text >> (fn "-" => NONE | s => SOME s)))
   1.717      >> (fn (target, prologs) => (Toplevel.theory o fold (add_modl_prolog target)) prologs)
   1.718 -  )
   1.719 +  );
   1.720 +
   1.721 +val code_exceptionP =
   1.722 +  OuterSyntax.command code_exceptionK "permit exceptions for constant" K.thy_decl (
   1.723 +    Scan.repeat1 P.term >> (Toplevel.theory o fold allow_exception_cmd)
   1.724 +  );
   1.725  
   1.726  val _ = OuterSyntax.add_keywords [infixK, infixlK, infixrK];
   1.727  
   1.728  val _ = OuterSyntax.add_parsers [code_classP, code_instanceP, code_typeP, code_constP,
   1.729 -  code_reservedP, code_modulenameP, code_moduleprologP, code_monadP];
   1.730 +  code_reservedP, code_modulenameP, code_moduleprologP, code_monadP, code_exceptionP];
   1.731  
   1.732  
   1.733  (*including serializer defaults*)
   1.734 @@ -2096,7 +2163,7 @@
   1.735    add_serializer (target_SML, isar_seri_sml)
   1.736    #> add_serializer (target_OCaml, isar_seri_ocaml)
   1.737    #> add_serializer (target_Haskell, isar_seri_haskell)
   1.738 -  #> add_serializer (target_diag, fn _ => fn _ => fn _ => seri_diagnosis)
   1.739 +  #> add_serializer (target_diag, fn _=> fn _ => fn _ => seri_diagnosis)
   1.740    #> add_syntax_tyco "SML" "fun" (SOME (2, fn pr_typ => fn fxy => fn [ty1, ty2] =>
   1.741        (gen_brackify (case fxy of NOBR => false | _ => eval_fxy (INFX (1, R)) fxy) o Pretty.breaks) [
   1.742          pr_typ (INFX (1, X)) ty1,