Files
mercury/compiler/prog_out.m
Mark Brown 3fc6b3f128 Change the representation of types in the compiler.
Estimated hours taken: 30
Branches: main

Change the representation of types in the compiler.

We also add some support for handling kinds, which will be used later when we
have a kind system.  There are a number of places where kinds are not yet
handled correctly -- we assume that all kinds will be `star'.  Each of these
locations is flagged with a comment that contains "XXX kind inference:".


compiler/prog_data.m:
	Implement the new version of type (type).

	Change the definition of type_param to be a variable instead of a
	term, since all parameters must be variables anyway.

	Implement versions of varset.merge_* which work with tvarsets and
	produce renamings instead of substitutions.  Renamings are more
	convenient than substitutions because we don't need to know the
	kinds of type variables in order to build the renaming, and in any
	case the substitutions shouldn't have anything other than variables
	in the range so renamings will be more efficient and safe.

	Define the type of kinds, and provide a couple of utility predicates
	to operate on them.

compiler/prog_io.m:
	Parse type definition heads as a sym_name and list of type_params,
	rather than a functor.  Handle this change in other predicates.

	Allow parse errors to be returned by get_with_type/3, and handle
	these errors.

	Remove parse_type/2.  This predicate didn't do any processing, it
	just forwarded handling to convert_type/2.

compiler/prog_io_typeclass.m:
	Change type_is_functor_and_vars to handle the new representation
	of types.  In doing so, we retain the old behaviour that pure
	predicates pass this test, but no other pred or func types.  This
	behaviour is arguably incorrect, but there is little point changing
	the behaviour at the moment.  Instead we should remove these kind of
	restrictions entirely, but that should be done later.

compiler/prog_io_util.m:
	Provide predicates to both parse and unparse types.  We need to
	unparse types before printing them out, since we do a lot of special
	case handling when printing out terms and we don't want to duplicate
	this functionality for types.

compiler/module_qual.m:
	Remove report_invalid_type.  We now report ill-formed types during
	parsing.

compiler/superhomogeneous.m:
	Handle errors from the parsing of type expressions.

compiler/prog_out.m:
	Provide a predicate to convert builtin_types to their string names,
	and vice-versa.

compiler/prog_type.m:
	Add a bunch of simple tests to use on types which may have kind
	annotations present.  In such cases, types do not have a canonical
	representation so the simple handling of these tests is not what we
	want.  (Note that these are only required in early phases.  The kind
	annotations -- when they are implemented -- will be removed before
	type checking.)

	Consistently handle the application of renamings, substitutions and
	recursive substitutions to various data structures.

compiler/mercury_to_mercury.m:
	Implement mercury_output_type, mercury_format_type and
	mercury_type_to_string.  These convert the type to a term before
	formatting -- the reason for this is so that appropriate parentheses
	are used when formatting operators.  This results in some slight
	changes to error messages, which are reflected in changes to the
	expected output files in the tests.

	Remove the old version of mercury_type_to_string.

	Change the argument ordering of mercury_format_var to be consistent
	with mercury_format_type.  (Other predicates in this module should
	probably be changed in a similar way, since this argument ordering
	is more amenable to higher-order programming.  But that can be left
	for another change.)

compiler/type_util.m:
	Implement type unification.  The behaviour is much the same as the
	previous behaviour, except that we now handle apply/N types properly,
	and we also allow for kind annotations.

	Implement an occurs check for types.

	Remove the example definition of replace_eqv_type.  It isn't used and
	would no longer work anyway even if it would have worked before.

	Add a tvar_kind_map field to ctor_defn.

	The functions type_info_type and type_ctor_info_type now return
	types with `void' as their argument, rather than the type that the
	type_info or type_ctor_info was for.

	Remove type_util.real_vars/2, since it no longer does anything
	different from prog_type.vars/2.

	Remove the commented out implementation of type_to_ctor_and_args/3.
	Its implementation is in prog_type.m, and has changed significantly
	in any case.

compiler/add_clause.m:
	Move parse_purity_annotation/3 to prog_io_util.m.

compiler/check_typeclass.m:
	Remove apply_substitution_to_var_list/3, since we now have predicates
	in prog_type.m to handle such things.

compiler/continuation_info.m:
compiler/trace.m:
	Use prog_type.vars/2 instead of type_util.real_vars/2.  The two
	predicates have the same meaning now since type_infos don't contain
	any type variables.

compiler/hlds_data.m:
	Add tvar_kind_map fields to hlds_type_defn and hlds_class_defn.

compiler/hlds_pred.m:
	Add a tvar_kind_map field to pred_info.

compiler/polymorphism.m:
	Add a tvar_kind_map field to poly_info.

	Remove unify_corresponding_types, which is no longer used.

compiler/hlds_out.m:
	Use mercury_output_type/5 instead of term_io__write_term/4 and
	mercury_output_term/5.

compiler/post_typecheck.m:
	Build the void substitution directly rather than building intermediate
	lists.

compiler/recompilation.version.m:
	Use term__list_subsumes instead of type_list_subsumes, which now
	operates only on types.  This follows up on what was suggested in
	an XXX comment.

compiler/typecheck_errors.m:
	Use unparse_type/2 to format error messages.

compiler/typecheck_info.m:
	Don't export write_type_with_bindings/5.  It is no longer used
	outside of this module.

compiler/*.m:
	Conform to the above changes.

library/rtti_implementation.m:
	Fix a syntax error that went undetected in our previous
	implementation, and amazingly enough was compiled correctly anyway.

library/term.m:
	Move the versions of term__unify, term__unify_list and
	term__list_subsumes that were implemented specifically for types
	to here.  The version of term_unify that takes a list of bound
	variables (i.e., variables that should not be bound any further)
	is used by the subsumption check, which in turn is used by
	recompilation.version.m.

tests/invalid/kind.err_exp:
tests/invalid/tc_err1.err_exp:
tests/invalid/tc_err2.err_exp:
tests/misc_tests/pretty_print_test.exp:
	Update the expected output of these tests to match what we now do.
2005-09-12 05:25:01 +00:00

476 lines
16 KiB
Mathematica

%-----------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sw=4 et
%-----------------------------------------------------------------------------%
% Copyright (C) 1993-2005 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.
%-----------------------------------------------------------------------------%
:- module parse_tree__prog_out.
% Main author: fjh.
% This module defines some predicates which output various parts
% of the parse tree created by prog_io.
% WARNING - this module is mostly junk at the moment!
% Only the first hundred lines or so are meaningful.
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
:- interface.
:- import_module mdbcomp__prim_data.
:- import_module parse_tree__prog_data.
:- import_module bool.
:- import_module io.
:- import_module list.
:- import_module std_util.
:- pred maybe_report_stats(bool::in, io::di, io::uo) is det.
:- pred maybe_write_string(bool::in, string::in, io::di, io::uo) is det.
:- pred maybe_flush_output(bool::in, io::di, io::uo) is det.
:- pred report_error(string::in, io::di, io::uo) is det.
:- pred report_error(io__output_stream::in, string::in, io::di, io::uo) is det.
% Write out the list of error/warning messages which is returned
% when a module is parsed.
%
:- pred write_messages(message_list::in, io::di, io::uo) is det.
% Write out the information in term context (at the moment, just
% the line number) in a form suitable for the beginning of an
% error message.
%
:- pred write_context(prog_context::in, io::di, io::uo) is det.
% Write to a string the information in term context (at the moment,
% just the line number) in a form suitable for the beginning of an
% error message.
%
:- pred context_to_string(prog_context::in, string::out) is det.
% Write out a symbol name, with special characters escaped,
% but without any quotes. This is suitable for use in
% error messages, where the caller should print out an
% enclosing forward/backward-quote pair (`...').
%
:- pred write_sym_name(sym_name::in, io::di, io::uo) is det.
:- func sym_name_to_escaped_string(sym_name) = string.
:- pred write_sym_name_and_arity(sym_name_and_arity::in, io::di, io::uo)
is det.
% Write out a symbol name, enclosed in single forward quotes ('...')
% if necessary, and with any special characters escaped.
% The output should be a syntactically valid Mercury term.
%
:- pred write_quoted_sym_name(sym_name::in, io::di, io::uo) is det.
% sym_name_and_arity_to_string(SymName, String):
%
% Convert a symbol name and arity to a "<Name>/<Arity>" string,
% with module qualifiers separated by the standard Mercury module
% qualifier operator.
%
:- pred sym_name_and_arity_to_string(sym_name_and_arity::in, string::out)
is det.
:- func sym_name_and_arity_to_string(sym_name_and_arity) = string.
:- pred write_simple_call_id(simple_call_id::in, io::di, io::uo) is det.
:- func simple_call_id_to_string(simple_call_id) = string.
:- pred write_simple_call_id(pred_or_func::in, sym_name_and_arity::in,
io::di, io::uo) is det.
:- func simple_call_id_to_string(pred_or_func, sym_name_and_arity) = string.
:- pred write_simple_call_id(pred_or_func::in, sym_name::in, arity::in,
io::di, io::uo) is det.
:- func simple_call_id_to_string(pred_or_func, sym_name, arity) = string.
:- pred simple_call_id_to_sym_name_and_arity(simple_call_id::in,
sym_name_and_arity::out) is det.
% Write out a module specifier.
%
:- pred write_module_spec(module_specifier::in, io::di, io::uo) is det.
:- func module_spec_to_escaped_string(module_specifier) = string.
:- pred write_module_list(list(module_name)::in, io::di, io::uo) is det.
:- pred write_list(list(T)::in, pred(T, io, io)::in(pred(in, di, uo) is det),
io::di, io::uo) is det.
:- pred write_string_list(list(string)::in, io::di, io::uo) is det.
:- pred write_promise_type(promise_type::in, io::di, io::uo) is det.
:- func promise_to_string(promise_type) = string.
:- mode promise_to_string(in) = out is det.
:- mode promise_to_string(out) = in is semidet.
:- mode promise_to_string(out) = out is multi.
:- pred builtin_type_to_string(builtin_type, string).
:- mode builtin_type_to_string(in, out) is det.
:- mode builtin_type_to_string(out, in) is semidet.
% Print "predicate" or "function" depending on the given value.
%
:- pred write_pred_or_func(pred_or_func::in, io::di, io::uo) is det.
% Return "predicate" or "function" depending on the given value.
%
:- func pred_or_func_to_full_str(pred_or_func) = string.
% Return "pred" or "func" depending on the given value.
%
:- func pred_or_func_to_str(pred_or_func) = string.
% Print out a purity name.
%
:- pred write_purity(purity::in, io::di, io::uo) is det.
% Get a purity name as a string.
%
:- pred purity_name(purity, string).
:- mode purity_name(in, out) is det.
:- mode purity_name(out, in) is semidet.
% Print out a purity prefix.
% This works under the assumptions that all purity names but `pure'
% are operators, and that we never need `pure' indicators/declarations.
%
:- pred write_purity_prefix(purity::in, io::di, io::uo) is det.
:- func purity_prefix_to_string(purity) = string.
% Convert an eval_method to a string giving the name of the pragma,
% and if the eval_method specifies tabling methods for individual
% arguments, a description of those argument tabling methods.
%
:- func eval_method_to_string(eval_method) = pair(string, maybe(string)).
% Convert an eval_method to a single string. This is suitable for use
% in error messages, but not for generating valid Mercury code.
%
:- func eval_method_to_one_string(eval_method) = string.
:- func maybe_arg_tabling_method_to_string(maybe(arg_tabling_method)) = string.
:- func arg_tabling_method_to_string(arg_tabling_method) = string.
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
:- implementation.
:- import_module mdbcomp__prim_data.
:- import_module parse_tree__prog_util.
:- import_module int.
:- import_module require.
:- import_module string.
:- import_module term.
:- import_module term_io.
:- import_module varset.
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
maybe_report_stats(yes, !IO) :-
io__report_stats(!IO).
maybe_report_stats(no, !IO).
maybe_write_string(yes, String, !IO) :-
io__write_string(String, !IO).
maybe_write_string(no, _, !IO).
maybe_flush_output(yes, !IO) :-
io__flush_output(!IO).
maybe_flush_output(no, !IO).
report_error(ErrorMessage, !IO) :-
io__write_string("Error: ", !IO),
io__write_string(ErrorMessage, !IO),
io__write_string("\n", !IO),
io__set_exit_status(1, !IO).
report_error(Stream, ErrorMessage, !IO) :-
io__set_output_stream(Stream, OldStream, !IO),
report_error(ErrorMessage, !IO),
io__set_output_stream(OldStream, _, !IO).
write_messages([], !IO).
write_messages([Message | Messages], !IO) :-
write_message(Message, !IO),
write_messages(Messages, !IO).
:- pred write_message(pair(string, term)::in,
io::di, io::uo) is det.
write_message(Msg - Term, !IO) :-
( Term = term__functor(_Functor, _Args, Context0) ->
Context0 = term__context(File, Line),
Context = term__context(File, Line),
write_context(Context, !IO)
;
true
),
io__write_string(Msg, !IO),
( Term = term__functor(term__atom(""), [], _Context2) ->
io__write_string(".\n", !IO)
;
io__write_string(": ", !IO),
varset__init(VarSet),
% XXX variable names in error messages
term_io__write_term_nl(VarSet, Term, !IO)
).
%-----------------------------------------------------------------------------%
write_context(Context, !IO) :-
context_to_string(Context, ContextMessage),
io__write_string(ContextMessage, !IO).
context_to_string(Context, ContextMessage) :-
term__context_file(Context, FileName),
term__context_line(Context, LineNumber),
( FileName = "" ->
ContextMessage = ""
;
string__format("%s:%03d: ", [s(FileName), i(LineNumber)],
ContextMessage)
).
%-----------------------------------------------------------------------------%
write_sym_name(qualified(ModuleSpec, Name), !IO) :-
write_module_spec(ModuleSpec, !IO),
io__write_string(".", !IO),
term_io__write_escaped_string(Name, !IO).
write_sym_name(unqualified(Name), !IO) :-
term_io__write_escaped_string(Name, !IO).
sym_name_to_escaped_string(qualified(ModuleSpec, Name)) =
module_spec_to_escaped_string(ModuleSpec)
++ "."
++ term_io__escaped_string(Name).
sym_name_to_escaped_string(unqualified(Name)) =
term_io__escaped_string(Name).
write_sym_name_and_arity(Name / Arity, !IO) :-
write_sym_name(Name, !IO),
io__write_string("/", !IO),
io__write_int(Arity, !IO).
write_quoted_sym_name(SymName, !IO) :-
io__write_string("'", !IO),
write_sym_name(SymName, !IO),
io__write_string("'", !IO).
sym_name_and_arity_to_string(SymName/Arity, String) :-
mdbcomp__prim_data__sym_name_to_string(SymName, SymNameString),
string__int_to_string(Arity, ArityString),
string__append_list([SymNameString, "/", ArityString], String).
sym_name_and_arity_to_string(SymName/Arity) = String :-
sym_name_and_arity_to_string(SymName/Arity, String).
write_simple_call_id(PredOrFunc - Name/Arity, !IO) :-
Str = simple_call_id_to_string(PredOrFunc, Name, Arity),
io__write_string(Str, !IO).
write_simple_call_id(PredOrFunc, Name/Arity, !IO) :-
Str = simple_call_id_to_string(PredOrFunc, Name, Arity),
io__write_string(Str, !IO).
write_simple_call_id(PredOrFunc, Name, Arity, !IO) :-
Str = simple_call_id_to_string(PredOrFunc, Name, Arity),
io__write_string(Str, !IO).
simple_call_id_to_string(PredOrFunc - Name/Arity) =
simple_call_id_to_string(PredOrFunc, Name, Arity).
simple_call_id_to_string(PredOrFunc, Name/Arity) =
simple_call_id_to_string(PredOrFunc, Name, Arity).
simple_call_id_to_string(PredOrFunc, Name, Arity) = Str :-
% XXX When printed, promises are differentiated from predicates or
% functions by module name, so the module names `promise',
% `promise_exclusive', etc. should be reserved, and their dummy
% predicates should have more unusual module names.
(
Name = unqualified(StrName)
;
Name = qualified(_, StrName)
),
% Is it really a promise?
( string__prefix(StrName, "promise__") ->
MaybePromise = yes(true)
; string__prefix(StrName, "promise_exclusive__") ->
MaybePromise = yes(exclusive)
; string__prefix(StrName, "promise_exhaustive__") ->
MaybePromise = yes(exhaustive)
; string__prefix(StrName, "promise_exclusive_exhaustive__") ->
MaybePromise = yes(exclusive_exhaustive)
;
MaybePromise = no % No, it is really a pred or func.
),
(
MaybePromise = yes(PromiseType),
PromiseStr = promise_to_string(PromiseType),
Str = "`" ++ PromiseStr ++ "' declaration"
;
MaybePromise = no,
PredOrFuncStr = pred_or_func_to_full_str(PredOrFunc),
simple_call_id_to_sym_name_and_arity(PredOrFunc - Name/Arity,
SymArity),
SymArityStr = sym_name_and_arity_to_string(SymArity),
Str = PredOrFuncStr ++ " `" ++ SymArityStr ++ "'"
).
simple_call_id_to_sym_name_and_arity(PredOrFunc - SymName/Arity,
SymName/OrigArity) :-
adjust_func_arity(PredOrFunc, OrigArity, Arity).
write_module_spec(ModuleSpec, !IO) :-
write_sym_name(ModuleSpec, !IO).
module_spec_to_escaped_string(ModuleSpec) =
sym_name_to_escaped_string(ModuleSpec).
%-----------------------------------------------------------------------------%
write_module_list(Modules, !IO) :-
write_list(Modules, write_module, !IO).
:- pred write_module(module_name::in, io::di, io::uo) is det.
write_module(Module, !IO) :-
io__write_string("`", !IO),
write_sym_name(Module, !IO),
io__write_string("'", !IO).
write_list([Import1, Import2, Import3 | Imports], Writer, !IO) :-
call(Writer, Import1, !IO),
io__write_string(", ", !IO),
write_list([Import2, Import3 | Imports], Writer, !IO).
write_list([Import1, Import2], Writer, !IO) :-
call(Writer, Import1, !IO),
io__write_string(" and ", !IO),
call(Writer, Import2, !IO).
write_list([Import], Writer, !IO) :-
call(Writer, Import, !IO).
write_list([], _, !IO) :-
error("write_module_list").
write_string_list([], !IO).
write_string_list([Name], !IO) :-
io__write_string(Name, !IO).
write_string_list([Name1, Name2 | Names], !IO) :-
io__write_string(Name1, !IO),
io__write_string(", ", !IO),
write_string_list([Name2 | Names], !IO).
promise_to_string(true) = "promise".
promise_to_string(exclusive) = "promise_exclusive".
promise_to_string(exhaustive) = "promise_exhaustive".
promise_to_string(exclusive_exhaustive) =
"promise_exclusive_exhaustive".
builtin_type_to_string(int, "int").
builtin_type_to_string(float, "float").
builtin_type_to_string(string, "string").
builtin_type_to_string(character, "character").
write_promise_type(PromiseType, !IO) :-
io__write_string(promise_to_string(PromiseType), !IO).
write_pred_or_func(PorF, !IO) :-
io__write_string(pred_or_func_to_full_str(PorF), !IO).
pred_or_func_to_full_str(predicate) = "predicate".
pred_or_func_to_full_str(function) = "function".
pred_or_func_to_str(predicate) = "pred".
pred_or_func_to_str(function) = "func".
write_purity_prefix(Purity, !IO) :-
( Purity = pure ->
true
;
write_purity(Purity, !IO),
io__write_string(" ", !IO)
).
purity_prefix_to_string(Purity) = String :-
( Purity = pure ->
String = ""
;
purity_name(Purity, PurityName),
String = string__append(PurityName, " ")
).
write_purity(Purity, !IO) :-
purity_name(Purity, String),
io__write_string(String, !IO).
purity_name(pure, "pure").
purity_name((semipure), "semipure").
purity_name((impure), "impure").
eval_method_to_one_string(EvalMethod) = Str :-
BaseStr - MaybeArgsStr = eval_method_to_string(EvalMethod),
(
MaybeArgsStr = yes(ArgsStr),
Str = BaseStr ++ "(" ++ ArgsStr ++ ")"
;
MaybeArgsStr = no,
Str = BaseStr
).
eval_method_to_string(eval_normal) = "normal" - no.
eval_method_to_string(eval_loop_check) = "loop_check" - no.
eval_method_to_string(eval_memo(all_strict)) = "memo" - no.
eval_method_to_string(eval_memo(all_fast_loose)) = "fast_loose_memo" - no.
eval_method_to_string(eval_memo(specified(Args))) = "memo" - yes(ArgsStr) :-
ArgStrs = list__map(maybe_arg_tabling_method_to_string, Args),
ArgsStr = "[" ++ string__join_list(", ", ArgStrs) ++ "]".
eval_method_to_string(eval_minimal(MinimalMethod)) = Str - no :-
(
MinimalMethod = own_stacks,
Str = "minimal_model_own_stacks"
;
MinimalMethod = stack_copy,
Str = "minimal_model_stack_copy"
).
eval_method_to_string(eval_table_io(IsDecl, IsUnitize)) = Str - no :-
(
IsDecl = table_io_decl,
DeclStr = "decl, "
;
IsDecl = table_io_proc,
DeclStr = "proc, "
),
(
IsUnitize = table_io_unitize,
UnitizeStr = "unitize"
;
IsUnitize = table_io_alone,
UnitizeStr = "alone"
),
Str = "table_io(" ++ DeclStr ++ UnitizeStr ++ ")".
maybe_arg_tabling_method_to_string(yes(ArgTablingMethod)) =
arg_tabling_method_to_string(ArgTablingMethod).
maybe_arg_tabling_method_to_string(no) = "output".
arg_tabling_method_to_string(arg_value) = "value".
arg_tabling_method_to_string(arg_addr) = "addr".
arg_tabling_method_to_string(arg_promise_implied) = "promise_implied".
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%