Import changes from the newtypein branch (svn merge -r 9361:9367 https://frisch@yquem.inria.fr/caml/svn/ocaml/branches/newtypein).

git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@9368 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
master
Alain Frisch 2009-10-06 12:51:42 +00:00
parent e5522b690d
commit db5a053fb4
9 changed files with 80 additions and 1 deletions

View File

@ -825,6 +825,8 @@ expr:
{ mkexp(Pexp_let($2, List.rev $3, $5)) }
| LET MODULE UIDENT module_binding IN seq_expr
{ mkexp(Pexp_letmodule($3, $4, $6)) }
| LET NEW TYPE LIDENT IN seq_expr
{ mkexp(Pexp_newtype($4, $6)) }
| FUNCTION opt_bar match_cases
{ mkexp(Pexp_function("", None, List.rev $3)) }
| FUN labeled_simple_pattern fun_def

View File

@ -112,6 +112,7 @@ and expression_desc =
| Pexp_lazy of expression
| Pexp_poly of expression * core_type option
| Pexp_object of class_structure
| Pexp_newtype of string * expression
(* Value descriptions *)

View File

@ -305,6 +305,9 @@ and expression i ppf x =
| Pexp_object s ->
line i ppf "Pexp_object";
class_structure i ppf s
| Pexp_newtype (s, e) ->
line i ppf "Pexp_newtype \"%s\"\n" s;
expression i ppf e
and value_description i ppf x =
line i ppf "value_description\n";

View File

@ -28,7 +28,8 @@ CODERUNPARAMS=OCAMLRUNPARAM='o=100'
BYTE_EXE=fib.byt takc.byt taku.byt sieve.byt quicksort.byt quicksort.fast.byt \
fft.byt fft.fast.byt soli.byt soli.fast.byt boyer.byt kb.byt \
nucleic.byt bdd.byt hamming.byt sorts.byt \
almabench.byt almabench.fast.byt weaktest.byt
almabench.byt almabench.fast.byt weaktest.byt \
newtype.byt
CODE_EXE=$(BYTE_EXE:.byt=.out)

31
test/newtype.ml Normal file
View File

@ -0,0 +1,31 @@
let property =
let new type t in
fun () ->
let module M = struct exception E of t end in
(fun x -> M.E x), (function M.E x -> Some x | _ -> None)
let () =
let (int_inj, int_proj) = property () in
let (string_inj, string_proj) = property () in
let i = int_inj 3 in
let s = string_inj "abc" in
Printf.printf "%b\n%!" (int_proj i = None);
Printf.printf "%b\n%!" (int_proj s = None);
Printf.printf "%b\n%!" (string_proj i = None);
Printf.printf "%b\n%!" (string_proj s = None)
let sort_uniq =
let new type s in
fun cmp l ->
let module S = Set.Make(struct type t = s let compare = cmp end) in
S.elements (List.fold_right S.add l S.empty)
let () =
print_endline (String.concat "," (sort_uniq compare [ "abc"; "xyz"; "abc" ]))

View File

@ -157,6 +157,7 @@ let rec add_expr bv exp =
| Pexp_poly (e, t) -> add_expr bv e; add_opt add_type bv t
| Pexp_object (pat, fieldl) ->
add_pattern bv pat; List.iter (add_class_field bv) fieldl
| Pexp_newtype (_, e) -> add_expr bv e
and add_pat_expr_list bv pel =
List.iter (fun (p, e) -> add_pattern bv p; add_expr bv e) pel

View File

@ -285,6 +285,9 @@ and rw_exp iflag sexp =
| Pexp_object (_, fieldl) ->
List.iter (rewrite_class_field iflag) fieldl
| Pexp_newtype (_, sexp) ->
rewrite_exp iflag sexp
and rewrite_ifbody iflag ghost sifbody =
if !instr_if && not ghost then
insert_profile rw_exp sifbody

View File

@ -1596,6 +1596,42 @@ let rec type_exp env sexp =
}
| Pexp_poly _ ->
assert false
| Pexp_newtype(name, sbody) ->
(* Create a fake abstract type declaration for name. *)
let decl = {
type_params = [];
type_arity = 0;
type_kind = Type_abstract;
type_private = Public;
type_manifest = None;
type_variance = [];
}
in
let ty = newvar () in
Ident.set_current_time ty.level;
let (id, new_env) = Env.enter_type name decl env in
Ctype.init_def(Ident.current_time());
let body = type_exp new_env sbody in
(* Replace every instance of this type constructor in the resulting type. *)
let seen = Hashtbl.create 8 in
let rec replace t =
if Hashtbl.mem seen t.id then ()
else begin
Hashtbl.add seen t.id ();
match t.desc with
| Tconstr (Path.Pident id', _, _) when id == id' -> link_type t ty
| _ -> Btype.iter_type_expr replace t
end
in
let ety = Subst.type_expr Subst.identity body.exp_type in
replace ety;
(* non-expansive if the body is non-expansive, so we don't introduce
any new extra node in the typed AST. *)
re { body with exp_loc = sexp.pexp_loc; exp_type = ety }
and type_argument env sarg ty_expected' =
(* ty_expected' may be generic *)

View File

@ -173,6 +173,7 @@ and expression ppf tbl e =
| Pexp_lazy e -> expression ppf tbl e;
| Pexp_poly (e, _) -> expression ppf tbl e;
| Pexp_object cs -> class_structure ppf tbl cs;
| Pexp_newtype (_, e) -> expression ppf tbl e
and expression_option ppf tbl eo =
match eo with