|
| 1 | +DECLARE PLUGIN "dump_plugin" |
| 2 | + |
| 3 | +{ |
| 4 | + |
| 5 | +open Pp |
| 6 | +open Names |
| 7 | +open Printer |
| 8 | +open Globnames |
| 9 | +open Stdarg |
| 10 | +open Typeops |
| 11 | +open Constr |
| 12 | + |
| 13 | +exception Bad_list |
| 14 | +exception Bad_int |
| 15 | + |
| 16 | +let rec int_of_pos (p : Constr.t) : int = |
| 17 | + if isApp p then |
| 18 | + (let (constructorTag, args) = destApp p in |
| 19 | + let (r,_) = destRef constructorTag in |
| 20 | + match r with |
| 21 | + | GlobRef.ConstructRef (_,r) -> (* r is the number of the constructor, |
| 22 | + starting from 1. *) |
| 23 | + (if r = 1 then 2*int_of_pos args.(0)+1 else 2*int_of_pos args.(0)) |
| 24 | + | _ -> raise Bad_int) |
| 25 | + else 1 |
| 26 | + |
| 27 | +let int_of_z (z : Constr.t) : int = |
| 28 | + if isApp z then |
| 29 | + (let (constructorTag, args) = destApp z in |
| 30 | + let (r,_) = destRef constructorTag in |
| 31 | + match r with |
| 32 | + | GlobRef.ConstructRef (_,r) -> (* r is the number of the constructor, |
| 33 | + starting from 1. *) |
| 34 | + (if r = 2 then int_of_pos args.(0) else -int_of_pos args.(0)) |
| 35 | + | _ -> raise Bad_int) |
| 36 | + else 0 |
| 37 | + |
| 38 | +let pair_int_of_pair_z (zz : Constr.t) : int*int = |
| 39 | + if isApp zz then |
| 40 | + (let (constructorTag, args) = destApp zz in |
| 41 | + (int_of_z args.(2), int_of_z args.(3))) |
| 42 | + else raise Bad_int |
| 43 | + |
| 44 | +let rec process_constr_list (l : Constr.t) (f : Constr.t -> unit) : unit = |
| 45 | + if isApp l then |
| 46 | + (let (constructorTag, args) = destApp l in |
| 47 | + if Array.length args = 3 then |
| 48 | + (* args stores type, current element and tail *) |
| 49 | + (f args.(1) ; process_constr_list args.(2) f) |
| 50 | + else ()) (* empty list *) |
| 51 | + else raise Bad_list |
| 52 | + |
| 53 | +let dump_bool_list (l : Constr.t) (oc : out_channel) : unit = |
| 54 | + process_constr_list l |
| 55 | + (fun b -> |
| 56 | + (* b is either true of false, which are constructors of the type bool *) |
| 57 | + let (r,_) = destRef b in |
| 58 | + match r with |
| 59 | + | GlobRef.ConstructRef (_,r) -> (* r is the number of the constructor, |
| 60 | + starting from 1. *) |
| 61 | + output_string oc (if r = 1 then "1 " else "0 ") |
| 62 | + | _ -> raise Bad_list) ; |
| 63 | + output_string oc "\n" |
| 64 | + |
| 65 | +let dump_pgm (raster : Constr.t) (rastertype : Pp.t) = |
| 66 | + let bufType = Buffer.create 1000 in |
| 67 | + let fmtType = Format.formatter_of_buffer bufType in |
| 68 | + pp_with fmtType rastertype; |
| 69 | + Format.pp_print_flush fmtType (); |
| 70 | + let (strType : string) = Buffer.contents bufType in |
| 71 | + if Str.string_match (Str.regexp "(sparse_raster \\([0-9]+\\) \\([0-9]+\\))") |
| 72 | + strType 0 then |
| 73 | + (let line_count = int_of_string (Str.matched_group 2 strType) in |
| 74 | + let column_count = int_of_string (Str.matched_group 1 strType) in |
| 75 | + let oc = open_out "plot.pgm" in |
| 76 | + let pixels = Array.make_matrix line_count column_count false in |
| 77 | + let (constructorTag, args) = destApp raster in |
| 78 | + process_constr_list args.(2) |
| 79 | + (fun zz -> let (i,j) = pair_int_of_pair_z zz in |
| 80 | + pixels.(i).(j) <- true); |
| 81 | + (* P2 is the magic number for PGM ascii files. |
| 82 | + Then come the dimensions of the image and |
| 83 | + the number of levels of gray, ie 1. *) |
| 84 | + output_string oc "P2\n"; |
| 85 | + output_string oc (string_of_int column_count); |
| 86 | + output_string oc " "; |
| 87 | + output_string oc (string_of_int line_count); |
| 88 | + output_string oc "\n1\n"; |
| 89 | + (for i=0 to line_count-1 do |
| 90 | + for j=0 to column_count-1 do |
| 91 | + output_string oc (if pixels.(i).(j) then "1 " else "0 ") |
| 92 | + done; |
| 93 | + output_string oc "\n" |
| 94 | + done); |
| 95 | + close_out oc) |
| 96 | + else failwith "Bad raster" |
| 97 | + |
| 98 | +(* Write the global term t to an ascii PGM file (portable gray map), |
| 99 | + which is a format for grayscale matrices. |
| 100 | + t should be already reduced, for example by |
| 101 | + Definition t := Eval vm_compute in [some raster expression] *) |
| 102 | +let eval_and_dump_pgm (t : Libnames.qualid) = |
| 103 | + let env = Global.env () in |
| 104 | + prerr_endline "dumping to plot.pgm"; |
| 105 | + let (tg : GlobRef.t) = Constrintern.intern_reference t in |
| 106 | + let (tt : Constr.types) = fst (type_of_global_in_context env tg) in |
| 107 | + let (c : Constr.constr) = printable_constr_of_global tg in |
| 108 | + (* Delta-reduce c to unfold the name of the matrix of booleans |
| 109 | + and get its contents. *) |
| 110 | + let evalc = Reductionops.whd_delta env (Evd.from_env env) (EConstr.of_constr c) in |
| 111 | + let evalt = Reductionops.nf_all env (Evd.from_env env) (EConstr.of_constr tt) in |
| 112 | + dump_pgm (EConstr.to_constr (Evd.from_env env) evalc) |
| 113 | + (pr_econstr_env env (Evd.from_env env) evalt) |
| 114 | + |
| 115 | +} |
| 116 | + |
| 117 | +VERNAC COMMAND EXTEND DumpGrayMap CLASSIFIED AS QUERY |
| 118 | +| [ "DumpGrayMap" global(a) ] -> { eval_and_dump_pgm a } |
| 119 | +END |
| 120 | + |
0 commit comments