src/HOL/Spec_Check/gen_construction.ML
changeset 52253 afca6a99a361
parent 52248 2c893e0c1def
child 52254 994055f7db80
equal deleted inserted replaced
52252:81fcc11d8c65 52253:afca6a99a361
    66 and parse_fun s = (parse_type_arg -- $$ "->" -- parse_type >> (fn ((a, f), r) => Con (f, [a, r]))) s
    66 and parse_fun s = (parse_type_arg -- $$ "->" -- parse_type >> (fn ((a, f), r) => Con (f, [a, r]))) s
    67 
    67 
    68 and parse_tuple s = (parse_type_single -- Scan.repeat1 ($$ "*" |-- parse_type_single)
    68 and parse_tuple s = (parse_type_single -- Scan.repeat1 ($$ "*" |-- parse_type_single)
    69   >> (fn (t, tl) => Tuple (t :: tl))) s;
    69   >> (fn (t, tl) => Tuple (t :: tl))) s;
    70 
    70 
    71 (*Parses entire type + name*)
    71 (*Parse entire type + name*)
    72 fun parse_function s =
    72 fun parse_function s =
    73   let
    73   let
    74     val p = $$ "val" |-- scan_name --| ($$ "=" -- $$ "fn" -- $$ ":")
    74     val p = $$ "val" |-- scan_name --| ($$ "=" -- $$ "fn" -- $$ ":")
    75     val (name, ty) = p (split s)
    75     val (name, ty) = p (split s)
    76     val stop = Scan.stopper (fn _ => ";") (fn s => s = ";");
    76     val stop = Scan.stopper (fn _ => ";") (fn s => s = ";");
    77     val (typ, _) = Scan.finite stop parse_type ty
    77     val (typ, _) = Scan.finite stop parse_type ty
    78   in (name, typ) end;
    78   in (name, typ) end;
    79 
    79 
    80 (*Creates desired output*)
    80 (*Create desired output*)
    81 fun parse_pred s =
    81 fun parse_pred s =
    82   let
    82   let
    83     val (name, Con ("->", t :: _)) = parse_function s
    83     val (name, Con ("->", t :: _)) = parse_function s
    84   in (name, t) end;
    84   in (name, t) end;
    85 
    85 
   109   val empty = initial_content
   109   val empty = initial_content
   110   val extend = I
   110   val extend = I
   111   fun merge data : T = Symtab.merge (K true) data
   111   fun merge data : T = Symtab.merge (K true) data
   112 )
   112 )
   113 
   113 
   114 fun data_of thy tycon = case Symtab.lookup (Data.get thy) tycon of SOME data => data
   114 fun data_of thy tycon =
   115   | NONE => error ("No generator and printer defined for ML type constructor " ^ quote tycon)
   115   (case Symtab.lookup (Data.get thy) tycon of
       
   116     SOME data => data
       
   117   | NONE => error ("No generator and printer defined for ML type constructor " ^ quote tycon))
   116 
   118 
   117 val generator_of = fst oo data_of
   119 val generator_of = fst oo data_of
   118 val printer_of = snd oo data_of
   120 val printer_of = snd oo data_of
   119 
   121 
   120 fun register (ty, data) = Data.map (Symtab.update (ty, data))
   122 fun register (ty, data) = Data.map (Symtab.update (ty, data))
   147       fun tuple_body t = space_implode " ^ \", \" ^ "
   149       fun tuple_body t = space_implode " ^ \", \" ^ "
   148         (map (fn (ty, n) => "(" ^ compose_printer thy ty ^ ") x" ^ string_of_int n)
   150         (map (fn (ty, n) => "(" ^ compose_printer thy ty ^ ") x" ^ string_of_int n)
   149         (t ~~ (1 upto (length t))))
   151         (t ~~ (1 upto (length t))))
   150     in "fn (" ^ tuple_head (length t) ^ ") => \"(\" ^ " ^ tuple_body t ^ " ^ \")\"" end;
   152     in "fn (" ^ tuple_head (length t) ^ ") => \"(\" ^ " ^ tuple_body t ^ " ^ \")\"" end;
   151 
   153 
   152 (*produces compilable string*)
   154 (*produce compilable string*)
   153 fun build_check thy name (ty, spec) = "Spec_Check.checkGen (ML_Context.the_generic_context ()) ("
   155 fun build_check thy name (ty, spec) = "Spec_Check.checkGen (ML_Context.the_generic_context ()) ("
   154   ^ compose_generator thy ty ^ ", SOME (" ^ compose_printer thy ty ^ ")) (\""
   156   ^ compose_generator thy ty ^ ", SOME (" ^ compose_printer thy ty ^ ")) (\""
   155   ^ name ^ "\", Property.pred (" ^ spec ^ "));";
   157   ^ name ^ "\", Property.pred (" ^ spec ^ "));";
   156 
   158 
   157 (*produces compilable string - non-eqtype functions*)
   159 (*produce compilable string - non-eqtype functions*)
   158 (*
   160 (*
   159 fun safe_check name (ty, spec) =
   161 fun safe_check name (ty, spec) =
   160   let
   162   let
   161     val default = case AList.lookup (op =) (!gen_table) "->"
   163     val default = case AList.lookup (op =) (!gen_table) "->"
   162       of NONE => ("gen_function_rand", "fn (_, _) => fn _ => \"fn\"")
   164       of NONE => ("gen_function_rand", "fn (_, _) => fn _ => \"fn\"")