Skip to content

Commit

Permalink
allow with constraints to add a type equation to a datatype definition
Browse files Browse the repository at this point in the history
git-svn-id: http://caml.inria.fr/svn/ocaml/version/3.12@10669 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
  • Loading branch information
Jacques Garrigue committed Sep 6, 2010
1 parent eb93a24 commit 0247ae7
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 6 deletions.
14 changes: 14 additions & 0 deletions typing/ctype.ml
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,20 @@ let instance_parameterized_type_2 sch_args sch_lst sch =
cleanup_types ();
(ty_args, ty_lst, ty)

let instance_declaration decl =
let decl =
{decl with type_params = List.map copy decl.type_params;
type_manifest = may_map copy decl.type_manifest;
type_kind = match decl.type_kind with
| Type_abstract -> Type_abstract
| Type_variant cl ->
Type_variant (List.map (fun (s,tl) -> (s, List.map copy tl)) cl)
| Type_record (fl, rr) ->
Type_record (List.map (fun (s,m,ty) -> (s, m, copy ty)) fl, rr)}
in
cleanup_types ();
decl

let instance_class params cty =
let rec copy_class_type =
function
Expand Down
1 change: 1 addition & 0 deletions typing/ctype.mli
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ val instance_parameterized_type:
val instance_parameterized_type_2:
type_expr list -> type_expr list -> type_expr ->
type_expr list * type_expr list * type_expr
val instance_declaration: type_declaration -> type_declaration
val instance_class:
type_expr list -> class_type -> type_expr list * class_type
val instance_poly:
Expand Down
8 changes: 6 additions & 2 deletions typing/typedecl.ml
Original file line number Diff line number Diff line change
Expand Up @@ -771,14 +771,18 @@ let transl_value_decl env valdecl =

(* Translate a "with" constraint -- much simplified version of
transl_type_decl. *)
let transl_with_constraint env id row_path sdecl =
let transl_with_constraint env id row_path orig_decl sdecl =
reset_type_variables();
Ctype.begin_def();
let params =
try
List.map (enter_type_variable true sdecl.ptype_loc) sdecl.ptype_params
with Already_bound ->
raise(Error(sdecl.ptype_loc, Repeated_parameter)) in
let orig_decl = Ctype.instance_declaration orig_decl in
let arity_ok = List.length params = orig_decl.type_arity in
if arity_ok then
List.iter2 (Ctype.unify_var env) params orig_decl.type_params;
List.iter
(function (ty, ty', loc) ->
try
Expand All @@ -791,7 +795,7 @@ let transl_with_constraint env id row_path sdecl =
let decl =
{ type_params = params;
type_arity = List.length params;
type_kind = Type_abstract;
type_kind = if arity_ok then orig_decl.type_kind else Type_abstract;
type_private = sdecl.ptype_private;
type_manifest =
begin match sdecl.ptype_manifest with
Expand Down
2 changes: 1 addition & 1 deletion typing/typedecl.mli
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ val transl_value_decl:
Env.t -> Parsetree.value_description -> value_description

val transl_with_constraint:
Env.t -> Ident.t -> Path.t option ->
Env.t -> Ident.t -> Path.t option -> type_declaration ->
Parsetree.type_declaration -> type_declaration

val abstract_type_decl: int -> type_declaration
Expand Down
6 changes: 3 additions & 3 deletions typing/typemod.ml
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,15 @@ let merge_constraint initial_env loc sg lid constr =
and id_row = Ident.create (s^"#row") in
let initial_env = Env.add_type id_row decl_row initial_env in
let newdecl = Typedecl.transl_with_constraint
initial_env id (Some(Pident id_row)) sdecl in
initial_env id (Some(Pident id_row)) decl sdecl in
check_type_decl env id row_id newdecl decl rs rem;
let decl_row = {decl_row with type_params = newdecl.type_params} in
let rs' = if rs = Trec_first then Trec_not else rs in
Tsig_type(id_row, decl_row, rs') :: Tsig_type(id, newdecl, rs) :: rem
| (Tsig_type(id, decl, rs) :: rem, [s], Pwith_type sdecl)
when Ident.name id = s ->
let newdecl =
Typedecl.transl_with_constraint initial_env id None sdecl in
Typedecl.transl_with_constraint initial_env id None decl sdecl in
check_type_decl env id row_id newdecl decl rs rem;
Tsig_type(id, newdecl, rs) :: rem
| (Tsig_type(id, decl, rs) :: rem, [s], (Pwith_type _ | Pwith_typesubst _))
Expand All @@ -141,7 +141,7 @@ let merge_constraint initial_env loc sg lid constr =
when Ident.name id = s ->
(* Check as for a normal with constraint, but discard definition *)
let newdecl =
Typedecl.transl_with_constraint initial_env id None sdecl in
Typedecl.transl_with_constraint initial_env id None decl sdecl in
check_type_decl env id row_id newdecl decl rs rem;
real_id := Some id;
make_next_first rs rem
Expand Down

0 comments on commit 0247ae7

Please sign in to comment.