| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
open Pp |
| 14 |
open CErrors |
| 15 |
open Util |
| 16 |
open Names |
| 17 |
open Miniml |
| 18 |
open Mlutil |
| 19 |
open Table |
| 20 |
open Common |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
let keywords = |
| 25 |
List.fold_right (fun s -> Id.Set.add (Id.of_string s)) |
| 26 |
[ "define"; "let"; "lambda"; "lambdas"; "match"; |
| 27 |
"apply"; "car"; "cdr"; |
| 28 |
"error"; "delay"; "force"; "_"; "__"] |
| 29 |
Id.Set.empty |
| 30 |
|
| 31 |
let pp_comment s = str ";; " ++ h s ++ fnl () |
| 32 |
|
| 33 |
let pp_header_comment = function |
| 34 |
| None -> mt () |
| 35 |
| Some com -> pp_comment com ++ fnl () ++ fnl () |
| 36 |
|
| 37 |
let preamble _ _ comment _ usf = |
| 38 |
pp_header_comment comment ++ |
| 39 |
str ";; This extracted scheme code relies on some additional macros\n" ++ |
| 40 |
str ";; available at http://www.pps.univ-paris-diderot.fr/~letouzey/scheme\n" ++ |
| 41 |
str "(load \"macros_extr.scm\")\n\n" ++ |
| 42 |
(if usf.mldummy then str "(define __ (lambda (_) __))\n\n" else mt ()) |
| 43 |
|
| 44 |
let pr_id id = |
| 45 |
str @@ String.map (fun c -> if c == '\'' then '~' else c) (Id.to_string id) |
| 46 |
|
| 47 |
let paren = pp_par true |
| 48 |
|
| 49 |
let pp_abst st = function |
| 50 |
| [] -> assert false |
| 51 |
| [id] -> paren (str "lambda " ++ paren (pr_id id) ++ spc () ++ st) |
| 52 |
| l -> paren |
| 53 |
(str "lambdas " ++ paren (prlist_with_sep spc pr_id l) ++ spc () ++ st) |
| 54 |
|
| 55 |
let pp_apply st _ = function |
| 56 |
| [] -> st |
| 57 |
| [a] -> hov 2 (paren (st ++ spc () ++ a)) |
| 58 |
| args -> hov 2 (paren (str "@ " ++ st ++ |
| 59 |
(prlist_strict (fun x -> spc () ++ x) args))) |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
let pp_global table k r = |
| 64 |
if is_inline_custom r then str (find_custom r) |
| 65 |
else str (Common.pp_global table k r) |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
let rec pp_expr table env args = |
| 70 |
let apply st = pp_apply st true args in |
| 71 |
function |
| 72 |
| MLrel n -> |
| 73 |
let id = get_db_name n env in apply (pr_id id) |
| 74 |
| MLapp (f,args') -> |
| 75 |
let stl = List.map (pp_expr table env []) args' in |
| 76 |
pp_expr table env (stl @ args) f |
| 77 |
| MLlam _ as a -> |
| 78 |
let fl,a' = collect_lams a in |
| 79 |
let fl,env' = push_vars (List.map id_of_mlid fl) env in |
| 80 |
apply (pp_abst (pp_expr table env' [] a') (List.rev fl)) |
| 81 |
| MLletin (id,a1,a2) -> |
| 82 |
let i,env' = push_vars [id_of_mlid id] env in |
| 83 |
apply |
| 84 |
(hv 0 |
| 85 |
(hov 2 |
| 86 |
(paren |
| 87 |
(str "let " ++ |
| 88 |
paren |
| 89 |
(paren |
| 90 |
(pr_id (List.hd i) ++ spc () ++ pp_expr table env [] a1)) |
| 91 |
++ spc () ++ hov 0 (pp_expr table env' [] a2))))) |
| 92 |
| MLglob r -> |
| 93 |
apply (pp_global table Term r) |
| 94 |
| MLcons (_,r,args') -> |
| 95 |
assert (List.is_empty args); |
| 96 |
let st = |
| 97 |
str "`" ++ |
| 98 |
paren (pp_global table Cons r ++ |
| 99 |
(if List.is_empty args' then mt () else spc ()) ++ |
| 100 |
prlist_with_sep spc (pp_cons_args table env) args') |
| 101 |
in |
| 102 |
if is_coinductive (State.get_table table) r then paren (str "delay " ++ st) else st |
| 103 |
| MLtuple _ -> user_err Pp.(str "Cannot handle tuples in Scheme yet.") |
| 104 |
| MLcase (_,_,pv) when not (is_regular_match pv) -> |
| 105 |
user_err Pp.(str "Cannot handle general patterns in Scheme yet.") |
| 106 |
| MLcase (_,t,pv) when is_custom_match pv -> |
| 107 |
let mkfun (ids,_,e) = |
| 108 |
if not (List.is_empty ids) then named_lams (List.rev ids) e |
| 109 |
else dummy_lams (ast_lift 1 e) 1 |
| 110 |
in |
| 111 |
apply |
| 112 |
(paren |
| 113 |
(hov 2 |
| 114 |
(str (find_custom_match pv) ++ fnl () ++ |
| 115 |
prvect (fun tr -> pp_expr table env [] (mkfun tr) ++ fnl ()) pv |
| 116 |
++ pp_expr table env [] t))) |
| 117 |
| MLcase (typ,t, pv) -> |
| 118 |
let e = |
| 119 |
if not (is_coinductive_type (State.get_table table) typ) then pp_expr table env [] t |
| 120 |
else paren (str "force" ++ spc () ++ pp_expr table env [] t) |
| 121 |
in |
| 122 |
apply (v 3 (paren (str "match " ++ e ++ fnl () ++ pp_pat table env pv))) |
| 123 |
| MLfix (i,ids,defs) -> |
| 124 |
let ids',env' = push_vars (List.rev (Array.to_list ids)) env in |
| 125 |
pp_fix table env' i (Array.of_list (List.rev ids'),defs) args |
| 126 |
| MLexn s -> |
| 127 |
|
| 128 |
paren (str "error" ++ spc () ++ qs s) |
| 129 |
| MLdummy _ -> |
| 130 |
str "__" |
| 131 |
| MLmagic a -> |
| 132 |
pp_expr table env args a |
| 133 |
| MLaxiom s -> paren (str "error \"AXIOM TO BE REALIZED (" ++ str s ++ str ")\"") |
| 134 |
| MLuint _ -> |
| 135 |
paren (str "Prelude.error \"EXTRACTION OF UINT NOT IMPLEMENTED\"") |
| 136 |
| MLfloat _ -> |
| 137 |
paren (str "Prelude.error \"EXTRACTION OF FLOAT NOT IMPLEMENTED\"") |
| 138 |
| MLstring _ -> |
| 139 |
paren (str "Prelude.error \"EXTRACTION OF STRING NOT IMPLEMENTED\"") |
| 140 |
| MLparray _ -> |
| 141 |
paren (str "Prelude.error \"EXTRACTION OF PARRAY NOT IMPLEMENTED\"") |
| 142 |
|
| 143 |
and pp_cons_args table env = function |
| 144 |
| MLcons (_,r,args) when is_coinductive (State.get_table table) r -> |
| 145 |
paren (pp_global table Cons r ++ |
| 146 |
(if List.is_empty args then mt () else spc ()) ++ |
| 147 |
prlist_with_sep spc (pp_cons_args table env) args) |
| 148 |
| e -> str "," ++ pp_expr table env [] e |
| 149 |
|
| 150 |
and pp_one_pat table env (ids,p,t) = |
| 151 |
let r = match p with |
| 152 |
| Pusual r -> r |
| 153 |
| Pcons (r,l) -> r |
| 154 |
| _ -> assert false |
| 155 |
in |
| 156 |
let ids,env' = push_vars (List.rev_map id_of_mlid ids) env in |
| 157 |
let args = |
| 158 |
if List.is_empty ids then mt () |
| 159 |
else (str " " ++ prlist_with_sep spc pr_id (List.rev ids)) |
| 160 |
in |
| 161 |
(pp_global table Cons r ++ args), (pp_expr table env' [] t) |
| 162 |
|
| 163 |
and pp_pat table env pv = |
| 164 |
prvect_with_sep fnl |
| 165 |
(fun x -> let s1,s2 = pp_one_pat table env x in |
| 166 |
hov 2 (str "((" ++ s1 ++ str ")" ++ spc () ++ s2 ++ str ")")) pv |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
and pp_fix table env j (ids,bl) args = |
| 172 |
paren |
| 173 |
(str "letrec " ++ |
| 174 |
(v 0 (paren |
| 175 |
(prvect_with_sep fnl |
| 176 |
(fun (fi,ti) -> |
| 177 |
paren ((pr_id fi) ++ spc () ++ (pp_expr table env [] ti))) |
| 178 |
(Array.map2 (fun id b -> (id,b)) ids bl)) ++ |
| 179 |
fnl () ++ |
| 180 |
hov 2 (pp_apply (pr_id (ids.(j))) true args)))) |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
let pp_decl table = function |
| 185 |
| Dind _ -> mt () |
| 186 |
| Dtype _ -> mt () |
| 187 |
| Dfix (rv, defs,_) -> |
| 188 |
let names = Array.map |
| 189 |
(fun r -> if is_inline_custom r then mt () else pp_global table Term r) rv |
| 190 |
in |
| 191 |
prvecti |
| 192 |
(fun i r -> |
| 193 |
let void = is_inline_custom r || |
| 194 |
(not (is_custom r) && |
| 195 |
match defs.(i) with MLexn "UNUSED" -> true | _ -> false) |
| 196 |
in |
| 197 |
if void then mt () |
| 198 |
else |
| 199 |
hov 2 |
| 200 |
(paren (str "define " ++ names.(i) ++ spc () ++ |
| 201 |
(if is_custom r then str (find_custom r) |
| 202 |
else pp_expr table (empty_env table ()) [] defs.(i))) |
| 203 |
++ fnl ()) ++ fnl ()) |
| 204 |
rv |
| 205 |
| Dterm (r, a, _) -> |
| 206 |
if is_inline_custom r then mt () |
| 207 |
else |
| 208 |
hov 2 (paren (str "define " ++ pp_global table Term r ++ spc () ++ |
| 209 |
(if is_custom r then str (find_custom r) |
| 210 |
else pp_expr table (empty_env table ()) [] a))) |
| 211 |
++ fnl2 () |
| 212 |
|
| 213 |
let rec pp_structure_elem table = function |
| 214 |
| (l,SEdecl d) -> pp_decl table d |
| 215 |
| (l,SEmodule m) -> pp_module_expr table m.ml_mod_expr |
| 216 |
| (l,SEmodtype m) -> mt () |
| 217 |
|
| 218 |
|
| 219 |
and pp_module_expr table = function |
| 220 |
| MEstruct (mp,sel) -> prlist_strict (fun e -> pp_structure_elem table e) sel |
| 221 |
| MEfunctor _ -> mt () |
| 222 |
|
| 223 |
| MEident _ | MEapply _ -> assert false |
| 224 |
|
| 225 |
|
| 226 |
let pp_struct table = |
| 227 |
let pp_sel (mp,sel) = State.with_visibility table mp [] begin fun table -> |
| 228 |
prlist_strict (fun e -> pp_structure_elem table e) sel |
| 229 |
end in |
| 230 |
prlist_strict pp_sel |
| 231 |
|
| 232 |
let file_naming state mp = file_of_modfile (State.get_table state) mp |
| 233 |
|
| 234 |
let scheme_descr = { |
| 235 |
keywords = keywords; |
| 236 |
file_suffix = ".scm"; |
| 237 |
file_naming = file_naming; |
| 238 |
preamble = preamble; |
| 239 |
pp_struct = pp_struct; |
| 240 |
sig_suffix = None; |
| 241 |
sig_preamble = (fun _ _ _ _ _ -> mt ()); |
| 242 |
pp_sig = (fun _ _ -> mt ()); |
| 243 |
pp_decl = pp_decl; |
| 244 |
} |