mirror of
https://github.com/Mercury-Language/mercury.git
synced 2025-12-16 14:25:56 +00:00
Estimated hours taken: 6
(plus another 80 or so already recorded for
my commits on the existential_types branch)
Merge in the changes from the existential types branch,
and make some modifications to address dgj's code review comments.
These changes add support for existentially quantified type variables
and type class constraints on functions and predicates.
(Existential data types, however, are not supported -- see below.)
Existentially quantified type variables are introduced with
an explicit `some [T]', e.g. `:- some [T] pred foo(T)'.
Existentially quantified type class constraints are introduced
with `&' instead of `<=', e.g. `:- some [T] (pred foo(T) & ord(T))'.
There's still several limitations:
0. XXX It's not yet documented in the language reference manual.
1. XXX It doesn't do any mode checking or mode reordering.
If you write code that uses existentially typed procedures in the
wrong order, then you'll get an internal error in polymorphism.m
or in the code generator. (Cases where a type_info has no
producer at all are caught by the check for unbound type
variables in post_typecheck.m.)
To support this, we need to change things so that polymorphism.m
gets invoked before mode checking.
2. Using `in' modes on arguments of existential type won't work.
If you try, you will get a compile error.
It would be nice to extend things to allow this kind of
"implied mode" for type_infos, where an existential type
becomes a universal type if some value of that type is
input. Supporting this would require first fixing
limitation 1 (described above) and then
3. There's no support for `pragma c_code' for procedures
with existential type class constraints.
(In fact, there's not really any support for `pragma c_code'
for procedures with universal type class constraints either --
the C code has no way of getting access to the type class info.)
4. XXX Taking the address of something which is existentially typed
should be illegal, but we don't check this.
In addition, these changes in this batch make a start towards allowing
existentially typed data types. The compiler now accepts existential
quantifiers and type class constraints on type definitions, and type
checks them accordingly (assuming all functor occurrences are
deconstructors, not constructors -- see limitation 2 above). But
there's no special handling for them in polymorphism.m, so if you try
to use them, it will abort with an internal error.
The changes also includes fixes for a couple of bugs in typechecking
and polymorphism that I discovered while making the above changes,
and an improvement to the error reporting from typecheck.m in one case.
Those changes are listed separately below.
compiler/prog_data.m:
Add a new type `class_constraints', which holds two different
lists of constraints, namely the existentially quantified constraints
and the universally quantified ones.
Add a new field to the parse tree representation of pred and
func declarations to hold a list of the existentially quantified
type variables, and change the `list(class_constraint)' into
`class_constraints' so that we can store existential constraints too.
Add new fields to the `constructor' data type (formerly just a pair)
to hold the existentially quantified type variables and
type class constraints.
compiler/hlds_pred.m:
Add several new fields to the pred_info:
- a list of the existentially quantified type variables;
- a list of the "HeadTypeParams": type variables which
cannot be bound by this predicate (i.e. those whose type_infos
come from this pred's caller or are returned from
other preds called by this one);
- and a list of unsatisfied type class constraints.
Add a predicate pred_info_get_univ_quant_tvars to compute the
universally quantified type variables.
Change the pred constraints field from `list(class_constraint)'
to `class_constraints' so that it can hold existential constraints too.
compiler/hlds_data.m:
Add new fields to hlds_cons_defn to hold the existentially
quantified type variables and type class constraints.
compiler/*.m:
Minor changes to reflect the above-mentioned data structure
changes in prog_data.m, hlds_pred.m, and hlds_data.m.
compiler/prog_io.m:
Add code to parse the new constructs.
Also rewrite the code for parsing purity specifiers,
type quantifiers and type class constraints, using basically
the method suggested by Peter Schachte: treat these as
"declaration attributes", and have parse_decl strip off
all the declaration attributes into a seperate list and
then pass that list to process_decl, which for each different
kind of declaration processes the attributes which are
appropriate for that declaration and then calls check_no_attributes
to ensure that there were no inappropriate attributes.
The purpose of this rewrite was to allow it to handle the new
constructs properly, and to avoid unnecessary code duplication.
compiler/mercury_to_mercury.m:
Add code to pretty-print the new constructs.
compiler/make_hlds.m:
Copy the new fields in the parse tree into the
corresponding new fields in the pred_info.
Add code to check for various misuses of quantifiers.
compiler/hlds_out.m:
Print out the new fields in the pred_info (except the
unsatisfied type class constraints -- if these are non-empty,
post_typecheck.m will print them out in the error message).
When printing out types, pass the AppendVarNums parameter down,
so that HLDS dumps will distinguish between different type
variables that have the same name.
Delete hlds_out__write_constructor, since it was doing exactly
the same thing as mercury__output_ctor.
compiler/typecheck.m:
Lots of changes to handle existential types and existential
type class constraints.
compiler/post_typecheck.m:
When checking for unbound type variables,
use the value of HeadTypeParams from the pred_info.
compiler/type_util.m:
Delete `type_and_constraint_list_matches_exactly', since it was not
used. Add various `apply_variable_renaming_to_*' predicates for
renaming constraints.
compiler/polymorphism.m:
Lots of changes to handle existential types and existential
type class constraints.
Also some changes to make the code more maintainable:
compiler/prog_data.m:
compiler/hlds_goal.m:
compiler/mercury_to_mercury.m:
Put curly braces around the definitions of 'some'/2 and '&'/2 functors
in `:- type' definitions, to avoid them being misinterpreted as
existential type constraints.
compiler/goal_util.m:
compiler/polymorphism.m:
compiler/hlds_pred.m:
compiler/lambda.m:
Include type_infos for existentially quantified type variables
and type_class_infos for existential constraints
in the set of extra variables computed by
goal_util__extra_type_info_vars.
compiler/inlining.m:
Change inlining__do_goal to handle inlining of calls to
existentially typed predicates -- for them, instead of not
binding any type variables at all in the caller, it allows the
call to bind any type variables in the caller except for those
that are universally quantified.
compiler/inlining.m:
compiler/deforest.m:
Call pred_info_get_univ_quant_tvars and pass the
result to inlining__do_inline_goal.
tests/hard_coded/Mmakefile:
tests/hard_coded/existential_types_test.{m,exp}:
tests/hard_coded/typeclasses/Mmakefile:
tests/hard_coded/typeclasses/existential_type_classes.{m,exp}:
Test cases for the use of existential types and
existential type class constraints.
----------
Improve an error message.
compiler/typecheck.m:
Improve error reporting by checking type class constraints for
satisfiability as we go and thus reporting unsatisfiable constraints
as soon as possible, rather than only at the end of the clause.
Previously we already did that for the case of ground constraints,
but they are not the only unsatsfiable constraints: constraints
on head type params (type variables which cannot be bound) are
also unsatisfiable if they can't be eliminated straight away
by context reduction.
tests/invalid/Mmakefile:
tests/invalid/typeclass_test_7.{m,err_exp}:
Regression test for the above change.
----------
Avoid problems where type inference was reporting some
spurious errors for predicates using type classes,
because the check for unsatisfied type class constraints
was being done before the final pass of type inference
had finished.
compiler/hlds_pred.m:
Add new field to the pred_info containing the unproven
type class constraints.
compiler/typecheck.m:
When inferring type class constraints, make sure that before
we save the results back in the pred_info, we restrict the
constraints to the head type variables. Constraints
on other type variables should be treated as
unsatisfied constraints.
Don't check for unsatisfied type class constraints at the
end of each pass; instead, just save the unproven type class
constraints in the pred_info.
compiler/post_typecheck.m:
Check for unsatisfied type class constraints, using
the new field in the pred_info.
tests/hard_coded/typeclasses/Mmakefile:
tests/hard_coded/typeclasses/inference_test_2.{m,exp}:
tests/invalid/Mmakefile:
tests/invalid/typeclass_test_8.{m,err_exp}:
Add regression tests for this change.
----------
Fix a bug with the computation of the non-locals for
predicates with more than one constraint on the same type variable --
it was only including one of the type-class-infos, rather than all of them.
compiler/goal_util.m:
Change `goal_util__extra_nonlocal_typeinfos' so that it gets
passed the TypeClassInfoVarMap and uses this to include all
the appropriate typeclass infos in the extra nonlocals.
compiler/hlds_pred.m:
compiler/lambda.m:
compiler/polymorphism.m:
Pass the TypeClassInfoVarMap to `goal_util__extra_nonlocal_typeinfos'.
tests/hard_coded/typeclasses/Mmakefile:
tests/hard_coded/typeclasses/lambda_multi_constraint_same_tvar.{m,exp}:
Regression test for the above-mentioned bug.
818 lines
27 KiB
Mathematica
818 lines
27 KiB
Mathematica
%-----------------------------------------------------------------------------%
|
|
% Copyright (C) 1994-1998 The University of Melbourne.
|
|
% This file may only be copied under the terms of the GNU General
|
|
% Public License - see the file COPYING in the Mercury distribution.
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
% File: type_util.m.
|
|
% Main author: fjh.
|
|
|
|
% This file provides some utility predicates which operate on types.
|
|
% It is used by various stages of the compilation after type-checking,
|
|
% include the mode checker and the code generator.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- module type_util.
|
|
|
|
:- interface.
|
|
|
|
:- import_module hlds_module, hlds_pred, hlds_data, prog_data.
|
|
:- import_module list, term, map.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
% Succeed iff type is an "atomic" type - one which can be
|
|
% unified using a simple_test rather than a complicated_unify.
|
|
|
|
:- pred type_is_atomic(type, module_info).
|
|
:- mode type_is_atomic(in, in) is semidet.
|
|
|
|
% type_is_higher_order(Type, PredOrFunc, ArgTypes) succeeds iff
|
|
% Type is a higher-order predicate or function type with the specified
|
|
% argument types (for functions, the return type is appended to the
|
|
% end of the argument types).
|
|
|
|
:- pred type_is_higher_order(type, pred_or_func, list(type)).
|
|
:- mode type_is_higher_order(in, out, out) is semidet.
|
|
|
|
% type_id_is_higher_order(TypeId, PredOrFunc) succeeds iff
|
|
% TypeId is a higher-order predicate or function type.
|
|
|
|
:- pred type_id_is_higher_order(type_id, pred_or_func).
|
|
:- mode type_id_is_higher_order(in, out) is semidet.
|
|
|
|
% Given a type, determine what sort of type it is.
|
|
|
|
:- pred classify_type(type, module_info, builtin_type).
|
|
:- mode classify_type(in, in, out) is det.
|
|
|
|
:- type builtin_type ---> int_type
|
|
; char_type
|
|
; str_type
|
|
; float_type
|
|
; pred_type
|
|
; enum_type
|
|
; polymorphic_type
|
|
; user_type.
|
|
|
|
% Given a non-variable type, return its type-id and argument types.
|
|
|
|
:- pred type_to_type_id(type, type_id, list(type)).
|
|
:- mode type_to_type_id(in, out, out) is semidet.
|
|
|
|
% Given a variable type, return its type variable.
|
|
|
|
:- pred type_util__var(type, tvar).
|
|
:- mode type_util__var(in, out) is semidet.
|
|
:- mode type_util__var(out, in) is det.
|
|
|
|
% Given a type_id, a list of argument types and maybe a context,
|
|
% construct a type.
|
|
|
|
:- pred construct_type(type_id, list(type), (type)).
|
|
:- mode construct_type(in, in, out) is det.
|
|
|
|
:- pred construct_type(type_id, list(type), term__context, (type)).
|
|
:- mode construct_type(in, in, in, out) is det.
|
|
|
|
% Given a constant and an arity, return a type_id.
|
|
% Fails if the constant is not an atom.
|
|
|
|
:- pred make_type_id(const, int, type_id).
|
|
:- mode make_type_id(in, in, out) is semidet.
|
|
|
|
% Given a type_id, look up its module/name/arity
|
|
|
|
:- pred type_util__type_id_module(module_info, type_id, module_name).
|
|
:- mode type_util__type_id_module(in, in, out) is det.
|
|
|
|
:- pred type_util__type_id_name(module_info, type_id, string).
|
|
:- mode type_util__type_id_name(in, in, out) is det.
|
|
|
|
:- pred type_util__type_id_arity(module_info, type_id, arity).
|
|
:- mode type_util__type_id_arity(in, in, out) is det.
|
|
|
|
% If the type is a du type, return the list of its constructors.
|
|
|
|
:- pred type_constructors(type, module_info, list(constructor)).
|
|
:- mode type_constructors(in, in, out) is semidet.
|
|
|
|
% Work out the types of the arguments of a functor.
|
|
:- pred type_util__get_cons_id_arg_types(module_info::in, (type)::in,
|
|
cons_id::in, list(type)::out) is det.
|
|
|
|
% Given a list of constructors for a type,
|
|
% check whether that type is a no_tag type
|
|
% (i.e. one with only one constructor, and
|
|
% whose one constructor has only one argument,
|
|
% and which is not private_builtin:type_info/1),
|
|
% and if so, return its constructor symbol and argument type.
|
|
|
|
:- pred type_is_no_tag_type(list(constructor), sym_name, type).
|
|
:- mode type_is_no_tag_type(in, out, out) is semidet.
|
|
|
|
% Unify (with occurs check) two types with respect to a type
|
|
% substitution and update the type bindings.
|
|
% The third argument is a list of type variables which cannot
|
|
% be bound (i.e. head type variables).
|
|
|
|
:- pred type_unify(type, type, list(tvar), tsubst, tsubst).
|
|
:- mode type_unify(in, in, in, in, out) is semidet.
|
|
|
|
:- pred type_unify_list(list(type), list(type), list(tvar), tsubst, tsubst).
|
|
:- mode type_unify_list(in, in, in, in, out) is semidet.
|
|
|
|
% Return a list of the type variables of a type.
|
|
|
|
:- pred type_util__vars(type, list(tvar)).
|
|
:- mode type_util__vars(in, out) is det.
|
|
|
|
% type_list_subsumes(TypesA, TypesB, Subst) succeeds iff the list
|
|
% TypesA subsumes (is more general than) TypesB, producing a
|
|
% type substitution which when applied to TypesA will give TypesB.
|
|
|
|
:- pred type_list_subsumes(list(type), list(type), tsubst).
|
|
:- mode type_list_subsumes(in, in, out) is semidet.
|
|
|
|
% apply a type substitution (i.e. map from tvar -> type)
|
|
% to all the types in a variable typing (i.e. map from var -> type).
|
|
|
|
:- pred apply_substitution_to_type_map(map(var, type), tsubst, map(var, type)).
|
|
:- mode apply_substitution_to_type_map(in, in, out) is det.
|
|
|
|
% same thing as above, except for a recursive substitution
|
|
% (i.e. we keep applying the substitution recursively until
|
|
% there are no more changes).
|
|
|
|
:- pred apply_rec_substitution_to_type_map(map(var, type), tsubst,
|
|
map(var, type)).
|
|
:- mode apply_rec_substitution_to_type_map(in, in, out) is det.
|
|
|
|
% Update a map from tvar to type_info_locn, using the type substititon
|
|
% to rename tvars and a variable substition to rename vars.
|
|
%
|
|
% If tvar maps to a another type variable, we keep the new
|
|
% variable, if it maps to a type, we remove it from the map.
|
|
|
|
:- pred apply_substitutions_to_var_map(map(tvar, type_info_locn), tsubst,
|
|
map(var, var), map(tvar, type_info_locn)).
|
|
:- mode apply_substitutions_to_var_map(in, in, in, out) is det.
|
|
|
|
:- pred apply_rec_subst_to_constraints(substitution, class_constraints,
|
|
class_constraints).
|
|
:- mode apply_rec_subst_to_constraints(in, in, out) is det.
|
|
|
|
:- pred apply_rec_subst_to_constraint_list(substitution, list(class_constraint),
|
|
list(class_constraint)).
|
|
:- mode apply_rec_subst_to_constraint_list(in, in, out) is det.
|
|
|
|
:- pred apply_rec_subst_to_constraint(substitution, class_constraint,
|
|
class_constraint).
|
|
:- mode apply_rec_subst_to_constraint(in, in, out) is det.
|
|
|
|
:- pred apply_subst_to_constraints(substitution, class_constraints,
|
|
class_constraints).
|
|
:- mode apply_subst_to_constraints(in, in, out) is det.
|
|
|
|
:- pred apply_subst_to_constraint_list(substitution, list(class_constraint),
|
|
list(class_constraint)).
|
|
:- mode apply_subst_to_constraint_list(in, in, out) is det.
|
|
|
|
:- pred apply_subst_to_constraint(substitution, class_constraint,
|
|
class_constraint).
|
|
:- mode apply_subst_to_constraint(in, in, out) is det.
|
|
|
|
:- pred apply_variable_renaming_to_constraints(map(var, var),
|
|
class_constraints, class_constraints).
|
|
:- mode apply_variable_renaming_to_constraints(in, in, out) is det.
|
|
|
|
:- pred apply_variable_renaming_to_constraint_list(map(var, var),
|
|
list(class_constraint), list(class_constraint)).
|
|
:- mode apply_variable_renaming_to_constraint_list(in, in, out) is det.
|
|
|
|
:- pred apply_variable_renaming_to_constraint(map(var, var),
|
|
class_constraint, class_constraint).
|
|
:- mode apply_variable_renaming_to_constraint(in, in, out) is det.
|
|
|
|
% Apply a renaming (partial map) to a list.
|
|
% Useful for applying a variable renaming to a list of variables.
|
|
:- pred apply_partial_map_to_list(list(T), map(T, T), list(T)).
|
|
:- mode apply_partial_map_to_list(in, in, out) is det.
|
|
|
|
% strip out the term__context fields, replacing them with empty
|
|
% term__contexts (as obtained by term__context_init/1)
|
|
% in a type or list of types
|
|
:- pred strip_term_contexts(list(term)::in, list(term)::out) is det.
|
|
:- pred strip_term_context(term::in, term::out) is det.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- implementation.
|
|
:- import_module bool, require, std_util.
|
|
:- import_module prog_io, prog_io_goal, prog_util.
|
|
|
|
type_util__type_id_module(_ModuleInfo, TypeName - _Arity, ModuleName) :-
|
|
sym_name_get_module_name(TypeName, unqualified(""), ModuleName).
|
|
|
|
type_util__type_id_name(_ModuleInfo, Name0 - _Arity, Name) :-
|
|
unqualify_name(Name0, Name).
|
|
|
|
type_util__type_id_arity(_ModuleInfo, _Name - Arity, Arity).
|
|
|
|
type_is_atomic(Type, ModuleInfo) :-
|
|
classify_type(Type, ModuleInfo, BuiltinType),
|
|
BuiltinType \= polymorphic_type,
|
|
BuiltinType \= pred_type,
|
|
BuiltinType \= user_type.
|
|
|
|
type_util__var(term__variable(Var), Var).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
% Given a type, determine what sort of type it is.
|
|
|
|
classify_type(VarType, ModuleInfo, Type) :-
|
|
(
|
|
VarType = term__variable(_)
|
|
->
|
|
Type = polymorphic_type
|
|
;
|
|
VarType = term__functor(term__atom("character"), [], _)
|
|
->
|
|
Type = char_type
|
|
;
|
|
VarType = term__functor(term__atom("int"), [], _)
|
|
->
|
|
Type = int_type
|
|
;
|
|
VarType = term__functor(term__atom("float"), [], _)
|
|
->
|
|
Type = float_type
|
|
;
|
|
VarType = term__functor(term__atom("string"), [], _)
|
|
->
|
|
Type = str_type
|
|
;
|
|
type_is_higher_order(VarType, _, _)
|
|
->
|
|
Type = pred_type
|
|
;
|
|
type_is_enumeration(VarType, ModuleInfo)
|
|
->
|
|
Type = enum_type
|
|
;
|
|
Type = user_type
|
|
).
|
|
|
|
type_is_higher_order(Type, PredOrFunc, PredArgTypes) :-
|
|
(
|
|
Type = term__functor(term__atom("pred"),
|
|
PredArgTypes, _),
|
|
PredOrFunc = predicate
|
|
;
|
|
Type = term__functor(term__atom("="),
|
|
[term__functor(term__atom("func"),
|
|
FuncArgTypes, _),
|
|
FuncRetType], _),
|
|
list__append(FuncArgTypes, [FuncRetType], PredArgTypes),
|
|
PredOrFunc = function
|
|
).
|
|
|
|
type_id_is_higher_order(SymName - Arity, PredOrFunc) :-
|
|
unqualify_name(SymName, TypeName),
|
|
(
|
|
TypeName = "pred",
|
|
PredOrFunc = predicate
|
|
;
|
|
TypeName = "=",
|
|
Arity = 2,
|
|
PredOrFunc = function
|
|
).
|
|
|
|
:- pred type_is_enumeration(type, module_info).
|
|
:- mode type_is_enumeration(in, in) is semidet.
|
|
|
|
type_is_enumeration(Type, ModuleInfo) :-
|
|
type_to_type_id(Type, TypeId, _),
|
|
module_info_types(ModuleInfo, TypeDefnTable),
|
|
map__search(TypeDefnTable, TypeId, TypeDefn),
|
|
hlds_data__get_type_defn_body(TypeDefn, TypeBody),
|
|
TypeBody = du_type(_, _, IsEnum, _),
|
|
IsEnum = yes.
|
|
|
|
type_to_type_id(Type, SymName - Arity, Args) :-
|
|
sym_name_and_args(Type, SymName, Args1),
|
|
|
|
% higher order types may have representations where
|
|
% their arguments don't directly correspond to the
|
|
% arguments of the term.
|
|
(
|
|
type_is_higher_order(Type, _, PredArgTypes)
|
|
->
|
|
Args = PredArgTypes,
|
|
list__length(Args1, Arity) % functions have arity 2,
|
|
% (they are =/2)
|
|
;
|
|
Args = Args1,
|
|
list__length(Args, Arity)
|
|
).
|
|
|
|
construct_type(TypeId, Args, Type) :-
|
|
term__context_init(Context),
|
|
construct_type(TypeId, Args, Context, Type).
|
|
|
|
construct_type(TypeId, Args, Context, Type) :-
|
|
(
|
|
type_id_is_higher_order(TypeId, PredOrFunc)
|
|
->
|
|
(
|
|
PredOrFunc = predicate,
|
|
NewArgs = Args
|
|
;
|
|
PredOrFunc = function,
|
|
pred_args_to_func_args(Args, FuncArgTypes, FuncRetType),
|
|
NewArgs = [term__functor(term__atom("func"),
|
|
FuncArgTypes, Context),
|
|
FuncRetType]
|
|
)
|
|
;
|
|
NewArgs = Args
|
|
),
|
|
TypeId = SymName - _,
|
|
construct_qualified_term(SymName, NewArgs, Context, Type).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
% Given a constant and an arity, return a type_id.
|
|
% This really ought to take a name and an arity -
|
|
% use of integers/floats/strings as type names should
|
|
% be rejected by the parser in prog_io.m, not in module_qual.m.
|
|
|
|
make_type_id(term__atom(Name), Arity, unqualified(Name) - Arity).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
% If the type is a du type, return the list of its constructors.
|
|
|
|
type_constructors(Type, ModuleInfo, Constructors) :-
|
|
type_to_type_id(Type, TypeId, TypeArgs),
|
|
module_info_types(ModuleInfo, TypeTable),
|
|
map__search(TypeTable, TypeId, TypeDefn),
|
|
hlds_data__get_type_defn_tparams(TypeDefn, TypeParams),
|
|
hlds_data__get_type_defn_body(TypeDefn, TypeBody),
|
|
TypeBody = du_type(Constructors0, _, _, _),
|
|
substitute_type_args(TypeParams, TypeArgs, Constructors0,
|
|
Constructors).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
type_util__get_cons_id_arg_types(ModuleInfo, VarType, ConsId, ArgTypes) :-
|
|
(
|
|
type_to_type_id(VarType, TypeId, TypeArgs),
|
|
module_info_ctors(ModuleInfo, Ctors),
|
|
% will fail for builtin cons_ids.
|
|
map__search(Ctors, ConsId, ConsDefns),
|
|
CorrectCons = lambda([ConsDefn::in] is semidet, (
|
|
ConsDefn = hlds_cons_defn(_, _, _, TypeId, _)
|
|
)),
|
|
list__filter(CorrectCons, ConsDefns,
|
|
[hlds_cons_defn(_, _, ArgTypes0, _, _)]),
|
|
ArgTypes0 \= []
|
|
->
|
|
module_info_types(ModuleInfo, Types),
|
|
map__lookup(Types, TypeId, TypeDefn),
|
|
hlds_data__get_type_defn_tparams(TypeDefn, TypeDefnParams),
|
|
term__term_list_to_var_list(TypeDefnParams, TypeDefnVars),
|
|
term__substitute_corresponding_list(TypeDefnVars, TypeArgs,
|
|
ArgTypes0, ArgTypes)
|
|
;
|
|
ArgTypes = []
|
|
).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
% The checks for type_info and base_type_info
|
|
% are needed because those types lie about their
|
|
% arity; it might be cleaner to change that in
|
|
% private_builtin.m, but that would cause some
|
|
% bootstrapping difficulties.
|
|
% It might be slightly better to check for private_builtin:type_info
|
|
% etc. rather than just checking the unqualified type name,
|
|
% but I found it difficult to verify that the constructors
|
|
% would always be fully module-qualified at points where
|
|
% type_is_no_tag_type/3 is called.
|
|
|
|
type_is_no_tag_type(Ctors, Ctor, Type) :-
|
|
Ctors = [SingleCtor],
|
|
SingleCtor = ctor(ExistQVars, _Constraints, Ctor, [_FieldName - Type]),
|
|
ExistQVars = [],
|
|
unqualify_name(Ctor, Name),
|
|
Name \= "type_info",
|
|
Name \= "base_type_info",
|
|
Name \= "typeclass_info",
|
|
Name \= "base_typeclass_info".
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
% Substitute the actual values of the type parameters
|
|
% in list of constructors, for a particular instance of
|
|
% a polymorphic type.
|
|
|
|
:- pred substitute_type_args(list(type_param), list(type),
|
|
list(constructor), list(constructor)).
|
|
:- mode substitute_type_args(in, in, in, out) is det.
|
|
|
|
substitute_type_args(TypeParams0, TypeArgs, Constructors0, Constructors) :-
|
|
( TypeParams0 = [] ->
|
|
Constructors = Constructors0
|
|
;
|
|
term__term_list_to_var_list(TypeParams0, TypeParams),
|
|
map__from_corresponding_lists(TypeParams, TypeArgs, Subst),
|
|
substitute_type_args_2(Constructors0, Subst, Constructors)
|
|
).
|
|
|
|
:- pred substitute_type_args_2(list(constructor), substitution,
|
|
list(constructor)).
|
|
:- mode substitute_type_args_2(in, in, out) is det.
|
|
|
|
substitute_type_args_2([], _, []).
|
|
substitute_type_args_2([Ctor0| Ctors0], Subst, [Ctor | Ctors]) :-
|
|
% Note: prog_io.m ensures that the existentially quantified
|
|
% variables, if any, are distinct from the parameters,
|
|
% and that the (existential) constraints can only contain
|
|
% existentially quantified variables, so there's
|
|
% no need to worry about applying the substitution to
|
|
% ExistQVars or Constraints
|
|
Ctor0 = ctor(ExistQVars, Constraints, Name, Args0),
|
|
Ctor = ctor(ExistQVars, Constraints, Name, Args),
|
|
substitute_type_args_3(Args0, Subst, Args),
|
|
substitute_type_args_2(Ctors0, Subst, Ctors).
|
|
|
|
:- pred substitute_type_args_3(list(constructor_arg), substitution,
|
|
list(constructor_arg)).
|
|
:- mode substitute_type_args_3(in, in, out) is det.
|
|
|
|
substitute_type_args_3([], _, []).
|
|
substitute_type_args_3([Name - Arg0 | Args0], Subst, [Name - Arg | Args]) :-
|
|
term__apply_substitution(Arg0, Subst, Arg),
|
|
substitute_type_args_3(Args0, Subst, Args).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
% Check whether TypesA subsumes TypesB, and if so return
|
|
% a type substitution that will map from TypesA to TypesB.
|
|
|
|
type_list_subsumes(TypesA, TypesB, TypeSubst) :-
|
|
%
|
|
% TypesA subsumes TypesB iff TypesA can be unified with TypesB
|
|
% without binding any of the type variables in TypesB.
|
|
%
|
|
term__vars_list(TypesB, TypesBVars),
|
|
map__init(TypeSubst0),
|
|
type_unify_list(TypesA, TypesB, TypesBVars, TypeSubst0, TypeSubst).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
% Types are represented as terms, but we can't just use term__unify
|
|
% because we need to avoid binding any of the "head type params"
|
|
% (the type variables that occur in the head of the clause),
|
|
% and because one day we might want to handle equivalent types.
|
|
|
|
type_unify(term__variable(X), term__variable(Y), HeadTypeParams, Bindings0,
|
|
Bindings) :-
|
|
( list__member(Y, HeadTypeParams) ->
|
|
type_unify_head_type_param(X, Y, HeadTypeParams,
|
|
Bindings0, Bindings)
|
|
; list__member(X, HeadTypeParams) ->
|
|
type_unify_head_type_param(Y, X, HeadTypeParams,
|
|
Bindings0, Bindings)
|
|
; map__search(Bindings0, X, BindingOfX) ->
|
|
( map__search(Bindings0, Y, BindingOfY) ->
|
|
% both X and Y already have bindings - just
|
|
% unify the types they are bound to
|
|
type_unify(BindingOfX, BindingOfY, HeadTypeParams,
|
|
Bindings0, Bindings)
|
|
;
|
|
term__apply_rec_substitution(BindingOfX,
|
|
Bindings0, SubstBindingOfX),
|
|
% Y is a type variable which hasn't been bound yet
|
|
( SubstBindingOfX = term__variable(Y) ->
|
|
Bindings = Bindings0
|
|
;
|
|
\+ term__occurs(SubstBindingOfX, Y, Bindings0),
|
|
map__det_insert(Bindings0, Y, SubstBindingOfX,
|
|
Bindings)
|
|
)
|
|
)
|
|
;
|
|
( map__search(Bindings0, Y, BindingOfY) ->
|
|
term__apply_rec_substitution(BindingOfY,
|
|
Bindings0, SubstBindingOfY),
|
|
% X is a type variable which hasn't been bound yet
|
|
( SubstBindingOfY = term__variable(X) ->
|
|
Bindings = Bindings0
|
|
;
|
|
\+ term__occurs(SubstBindingOfY, X, Bindings0),
|
|
map__det_insert(Bindings0, X, SubstBindingOfY,
|
|
Bindings)
|
|
)
|
|
;
|
|
% both X and Y are unbound type variables -
|
|
% bind one to the other
|
|
( X = Y ->
|
|
Bindings = Bindings0
|
|
;
|
|
map__det_insert(Bindings0, X, term__variable(Y),
|
|
Bindings)
|
|
)
|
|
)
|
|
).
|
|
|
|
type_unify(term__variable(X), term__functor(F, As, C), HeadTypeParams,
|
|
Bindings0, Bindings) :-
|
|
(
|
|
map__search(Bindings0, X, BindingOfX)
|
|
->
|
|
type_unify(BindingOfX, term__functor(F, As, C), HeadTypeParams,
|
|
Bindings0, Bindings)
|
|
;
|
|
\+ term__occurs_list(As, X, Bindings0),
|
|
\+ list__member(X, HeadTypeParams),
|
|
map__det_insert(Bindings0, X, term__functor(F, As, C), Bindings)
|
|
).
|
|
|
|
type_unify(term__functor(F, As, C), term__variable(X), HeadTypeParams,
|
|
Bindings0, Bindings) :-
|
|
(
|
|
map__search(Bindings0, X, BindingOfX)
|
|
->
|
|
type_unify(term__functor(F, As, C), BindingOfX, HeadTypeParams,
|
|
Bindings0, Bindings)
|
|
;
|
|
\+ term__occurs_list(As, X, Bindings0),
|
|
\+ list__member(X, HeadTypeParams),
|
|
map__det_insert(Bindings0, X, term__functor(F, As, C), Bindings)
|
|
).
|
|
|
|
type_unify(term__functor(FX, AsX, _CX), term__functor(FY, AsY, _CY),
|
|
HeadTypeParams, Bindings0, Bindings) :-
|
|
list__length(AsX, ArityX),
|
|
list__length(AsY, ArityY),
|
|
(
|
|
FX = FY,
|
|
ArityX = ArityY
|
|
->
|
|
type_unify_list(AsX, AsY, HeadTypeParams, Bindings0, Bindings)
|
|
;
|
|
fail
|
|
).
|
|
|
|
% XXX Instead of just failing if the functors' name/arity is different,
|
|
% we should check here if these types have been defined
|
|
% to be equivalent using equivalence types. But this
|
|
% is difficult because (1) it causes typevarset synchronization
|
|
% problems, and (2) the relevant variables TypeInfo, TVarSet0, TVarSet
|
|
% haven't been passed in to here.
|
|
|
|
/*******
|
|
...
|
|
;
|
|
replace_eqv_type(FX, ArityX, AsX, EqvType)
|
|
->
|
|
type_unify(EqvType, term__functor(FY, AsY, CY), HeadTypeParams,
|
|
Bindings0, Bindings)
|
|
;
|
|
replace_eqv_type(FY, ArityY, AsY, EqvType)
|
|
->
|
|
type_unify(term__functor(FX, AsX, CX), EqvType, HeadTypeParams,
|
|
Bindings0, Bindings)
|
|
;
|
|
fail
|
|
).
|
|
|
|
:- pred replace_eqv_type(const, int, list(type), type).
|
|
:- mode replace_eqv_type(in, in, in, out) is semidet.
|
|
|
|
replace_eqv_type(Functor, Arity, Args, EqvType) :-
|
|
|
|
% XXX magically_obtain(TypeTable, TVarSet0, TVarSet)
|
|
|
|
make_type_id(Functor, Arity, TypeId),
|
|
map__search(TypeTable, TypeId, TypeDefn),
|
|
TypeDefn = hlds_type_defn(TypeVarSet, TypeParams0,
|
|
eqv_type(EqvType0), _Condition, Context, _Status),
|
|
varset__merge(TVarSet0, TypeVarSet, [EqvType0 | TypeParams0],
|
|
TVarSet, [EqvType1, TypeParams1]),
|
|
type_param_to_var_list(TypeParams1, TypeParams),
|
|
term__substitute_corresponding(EqvType1, TypeParams, AsX,
|
|
EqvType).
|
|
|
|
******/
|
|
|
|
type_unify_list([], [], _) --> [].
|
|
type_unify_list([X | Xs], [Y | Ys], HeadTypeParams) -->
|
|
type_unify(X, Y, HeadTypeParams),
|
|
type_unify_list(Xs, Ys, HeadTypeParams).
|
|
|
|
:- pred type_unify_head_type_param(tvar, tvar, list(tvar), tsubst, tsubst).
|
|
:- mode type_unify_head_type_param(in, in, in, in, out) is semidet.
|
|
|
|
type_unify_head_type_param(Var, HeadVar, HeadTypeParams, Bindings0,
|
|
Bindings) :-
|
|
( map__search(Bindings0, Var, BindingOfVar) ->
|
|
BindingOfVar = term__variable(Var2),
|
|
type_unify_head_type_param(Var2, HeadVar, HeadTypeParams,
|
|
Bindings0, Bindings)
|
|
;
|
|
( Var = HeadVar ->
|
|
Bindings = Bindings0
|
|
;
|
|
\+ list__member(Var, HeadTypeParams),
|
|
map__det_insert(Bindings0, Var, term__variable(HeadVar),
|
|
Bindings)
|
|
)
|
|
).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
type_util__vars(Type, Tvars) :-
|
|
term__vars(Type, Tvars).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
apply_substitution_to_type_map(VarTypes0, Subst, VarTypes) :-
|
|
% optimize the common case of an empty type substitution
|
|
( map__is_empty(Subst) ->
|
|
VarTypes = VarTypes0
|
|
;
|
|
map__keys(VarTypes0, Vars),
|
|
apply_substitution_to_type_map_2(Vars, VarTypes0, Subst,
|
|
VarTypes)
|
|
).
|
|
|
|
:- pred apply_substitution_to_type_map_2(list(var)::in, map(var, type)::in,
|
|
tsubst::in, map(var, type)::out) is det.
|
|
|
|
apply_substitution_to_type_map_2([], VarTypes, _Subst, VarTypes).
|
|
apply_substitution_to_type_map_2([Var | Vars], VarTypes0, Subst,
|
|
VarTypes) :-
|
|
map__lookup(VarTypes0, Var, VarType0),
|
|
term__apply_substitution(VarType0, Subst, VarType),
|
|
map__det_update(VarTypes0, Var, VarType, VarTypes1),
|
|
apply_substitution_to_type_map_2(Vars, VarTypes1, Subst, VarTypes).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
apply_rec_substitution_to_type_map(VarTypes0, Subst, VarTypes) :-
|
|
% optimize the common case of an empty type substitution
|
|
( map__is_empty(Subst) ->
|
|
VarTypes = VarTypes0
|
|
;
|
|
map__keys(VarTypes0, Vars),
|
|
apply_rec_substitution_to_type_map_2(Vars, VarTypes0, Subst,
|
|
VarTypes)
|
|
).
|
|
|
|
:- pred apply_rec_substitution_to_type_map_2(list(var)::in, map(var, type)::in,
|
|
tsubst::in, map(var, type)::out) is det.
|
|
|
|
apply_rec_substitution_to_type_map_2([], VarTypes, _Subst, VarTypes).
|
|
apply_rec_substitution_to_type_map_2([Var | Vars], VarTypes0, Subst,
|
|
VarTypes) :-
|
|
map__lookup(VarTypes0, Var, VarType0),
|
|
term__apply_rec_substitution(VarType0, Subst, VarType),
|
|
map__det_update(VarTypes0, Var, VarType, VarTypes1),
|
|
apply_rec_substitution_to_type_map_2(Vars, VarTypes1, Subst, VarTypes).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
apply_substitutions_to_var_map(VarMap0, TSubst, Subst, VarMap) :-
|
|
% optimize the common case of empty substitutions
|
|
( map__is_empty(Subst), map__is_empty(TSubst) ->
|
|
VarMap = VarMap0
|
|
;
|
|
map__keys(VarMap0, TVars),
|
|
map__init(NewVarMap),
|
|
apply_substitutions_to_var_map_2(TVars, VarMap0, TSubst,
|
|
Subst, NewVarMap, VarMap)
|
|
).
|
|
|
|
|
|
:- pred apply_substitutions_to_var_map_2(list(var)::in, map(tvar,
|
|
type_info_locn)::in, tsubst::in, map(var, var)::in,
|
|
map(tvar, type_info_locn)::in,
|
|
map(tvar, type_info_locn)::out) is det.
|
|
|
|
apply_substitutions_to_var_map_2([], _VarMap0, _, _, NewVarMap, NewVarMap).
|
|
apply_substitutions_to_var_map_2([TVar | TVars], VarMap0, TSubst, Subst,
|
|
NewVarMap0, NewVarMap) :-
|
|
map__lookup(VarMap0, TVar, Locn),
|
|
type_info_locn_var(Locn, Var),
|
|
|
|
% find the new tvar, if there is one, otherwise just
|
|
% create the old var as a type variable.
|
|
( map__search(TSubst, TVar, NewTerm0) ->
|
|
NewTerm = NewTerm0
|
|
;
|
|
type_util__var(NewTerm, TVar)
|
|
),
|
|
|
|
% find the new var, if there is one
|
|
( map__search(Subst, Var, NewVar0) ->
|
|
NewVar = NewVar0
|
|
;
|
|
NewVar = Var
|
|
),
|
|
type_info_locn_set_var(Locn, NewVar, NewLocn),
|
|
|
|
% if the tvar is still a variable, insert it into the
|
|
% map with the new var.
|
|
( type_util__var(NewTerm, NewTVar) ->
|
|
map__det_insert(NewVarMap0, NewTVar, NewLocn, NewVarMap1)
|
|
;
|
|
NewVarMap1 = NewVarMap0
|
|
),
|
|
apply_substitutions_to_var_map_2(TVars, VarMap0, TSubst, Subst,
|
|
NewVarMap1, NewVarMap).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
apply_rec_subst_to_constraints(Subst, Constraints0, Constraints) :-
|
|
Constraints0 = constraints(UnivCs0, ExistCs0),
|
|
apply_rec_subst_to_constraint_list(Subst, UnivCs0, UnivCs),
|
|
apply_rec_subst_to_constraint_list(Subst, ExistCs0, ExistCs),
|
|
Constraints = constraints(UnivCs, ExistCs).
|
|
|
|
apply_rec_subst_to_constraint_list(Subst, Constraints0, Constraints) :-
|
|
list__map(apply_rec_subst_to_constraint(Subst), Constraints0,
|
|
Constraints).
|
|
|
|
apply_rec_subst_to_constraint(Subst, Constraint0, Constraint) :-
|
|
Constraint0 = constraint(ClassName, Types0),
|
|
term__apply_rec_substitution_to_list(Types0, Subst, Types1),
|
|
% we need to maintain the invariant that types in class constraints
|
|
% do not have any information in their term__context fields
|
|
strip_term_contexts(Types1, Types),
|
|
Constraint = constraint(ClassName, Types).
|
|
|
|
apply_subst_to_constraints(Subst,
|
|
constraints(UniversalCs0, ExistentialCs0),
|
|
constraints(UniversalCs, ExistentialCs)) :-
|
|
apply_subst_to_constraint_list(Subst, UniversalCs0, UniversalCs),
|
|
apply_subst_to_constraint_list(Subst, ExistentialCs0, ExistentialCs).
|
|
|
|
apply_subst_to_constraint_list(Subst, Constraints0, Constraints) :-
|
|
list__map(apply_subst_to_constraint(Subst), Constraints0, Constraints).
|
|
|
|
apply_subst_to_constraint(Subst, Constraint0, Constraint) :-
|
|
Constraint0 = constraint(ClassName, Types0),
|
|
term__apply_substitution_to_list(Types0, Subst, Types),
|
|
Constraint = constraint(ClassName, Types).
|
|
|
|
apply_variable_renaming_to_constraints(Renaming,
|
|
constraints(UniversalCs0, ExistentialCs0),
|
|
constraints(UniversalCs, ExistentialCs)) :-
|
|
apply_variable_renaming_to_constraint_list(Renaming,
|
|
UniversalCs0, UniversalCs),
|
|
apply_variable_renaming_to_constraint_list(Renaming,
|
|
ExistentialCs0, ExistentialCs).
|
|
|
|
apply_variable_renaming_to_constraint_list(Renaming, Constraints0,
|
|
Constraints) :-
|
|
list__map(apply_variable_renaming_to_constraint(Renaming),
|
|
Constraints0, Constraints).
|
|
|
|
apply_variable_renaming_to_constraint(Renaming, Constraint0, Constraint) :-
|
|
Constraint0 = constraint(ClassName, ClassArgTypes0),
|
|
term__apply_variable_renaming_to_list(ClassArgTypes0,
|
|
Renaming, ClassArgTypes),
|
|
Constraint = constraint(ClassName, ClassArgTypes).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
apply_partial_map_to_list([], _PartialMap, []).
|
|
apply_partial_map_to_list([X|Xs], PartialMap, [Y|Ys]) :-
|
|
( map__search(PartialMap, X, Y0) ->
|
|
Y = Y0
|
|
;
|
|
Y = X
|
|
),
|
|
apply_partial_map_to_list(Xs, PartialMap, Ys).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
strip_term_contexts(Terms, StrippedTerms) :-
|
|
list__map(strip_term_context, Terms, StrippedTerms).
|
|
|
|
strip_term_context(term__variable(V), term__variable(V)).
|
|
strip_term_context(term__functor(F, As0, _C0), term__functor(F, As, C)) :-
|
|
term__context_init(C),
|
|
strip_term_contexts(As0, As).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
%-----------------------------------------------------------------------------%
|