[m-rev.] for review: move same_type and find_functor to interface
Ondrej Bojar
bojar at csse.unimelb.edu.au
Tue Sep 25 13:45:32 AEST 2007
Incorporated Julien's further suggestions:
Estimated hours taken: 1
Branch: main
library/term.m:
library/type_desc.m:
library/construct.m:
Move same_type/2 and find_functor/5 from module term into the interfaces of
type_desc and construct, respectively. Both are useful in custom pickling
and unpickling predicates.
NEWS:
Announce the change.
Index: NEWS
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/NEWS,v
retrieving revision 1.478
diff -u -r1.478 NEWS
--- NEWS 21 Sep 2007 03:21:34 -0000 1.478
+++ NEWS 25 Sep 2007 03:29:38 -0000
@@ -197,6 +197,10 @@
* We have added the predicate `dir.current_directory'.
+* We have added two predicates that are useful for custom term construction:
+ construct.find_functor/5
+ type_desc.same_type/2
+
Changes to the Mercury compiler:
* Support for the reserve tag grades has been removed.
Index: library/construct.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/library/construct.m,v
retrieving revision 1.40
diff -u -r1.40 construct.m
--- library/construct.m 18 Sep 2007 07:56:35 -0000 1.40
+++ library/construct.m 25 Sep 2007 03:29:38 -0000
@@ -92,6 +92,15 @@
:- func get_functor_lex(type_desc, functor_number_ordinal) =
functor_number_lex is semidet.
+ % find_functor(Type, FunctorName, Arity, FunctorNumber, ArgTypes).
+ %
+ % Given a type descriptor, a functor name and arity, finds the functor
+ % number and the types of its arguments. It thus serves as the converse
+ % to get_functor/5.
+ %
+:- pred find_functor(type_desc::in, string::in, int::in,
+ functor_number_lex::out, list(type_desc)::out) is semidet.
+
% construct(Type, I, Args) = Term.
%
% Returns a term of the type specified by Type whose functor
@@ -115,6 +124,7 @@
:- implementation.
+:- import_module int.
:- import_module require.
% For use by the Erlang backends.
@@ -514,6 +524,24 @@
}
}").
+find_functor(Type, Functor, Arity, FunctorNumber, ArgTypes) :-
+ N = construct.num_functors(Type),
+ find_functor_2(Type, Functor, Arity, N, FunctorNumber, ArgTypes).
+
+:- pred find_functor_2(type_desc::in, string::in, int::in,
+ int::in, int::out, list(type_desc)::out) is semidet.
+
+find_functor_2(TypeInfo, Functor, Arity, Num0, FunctorNumber, ArgTypes) :-
+ Num0 >= 0,
+ Num = Num0 - 1,
+ ( get_functor(TypeInfo, Num, Functor, Arity, ArgPseudoTypes) ->
+ ArgTypes = list.map(ground_pseudo_type_desc_to_type_desc_det,
+ ArgPseudoTypes),
+ FunctorNumber = Num
+ ;
+ find_functor_2(TypeInfo, Functor, Arity, Num, FunctorNumber, ArgTypes)
+ ).
+
:- pragma no_inline(construct/3).
:- pragma foreign_proc("C",
construct(TypeDesc::in, FunctorNumber::in, ArgList::in) = (Term::out),
Index: library/term.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/library/term.m,v
retrieving revision 1.127
diff -u -r1.127 term.m
--- library/term.m 13 Feb 2007 01:58:54 -0000 1.127
+++ library/term.m 25 Sep 2007 03:29:38 -0000
@@ -647,27 +647,6 @@
Result = error(Error)
).
-:- pred find_functor(type_desc::in, string::in, int::in,
- int::out, list(type_desc.type_desc)::out) is semidet.
-
-find_functor(Type, Functor, Arity, FunctorNumber, ArgTypes) :-
- N = construct.num_functors(Type),
- find_functor_2(Type, Functor, Arity, N, FunctorNumber, ArgTypes).
-
-:- pred find_functor_2(type_desc::in, string::in, int::in,
- int::in, int::out, list(type_desc)::out) is semidet.
-
-find_functor_2(TypeInfo, Functor, Arity, Num, FunctorNumber, ArgTypes) :-
- Num >= 0,
- Num1 = Num - 1,
- ( get_functor(TypeInfo, Num1, Functor, Arity, ArgPseudoTypes) ->
- ArgTypes = list.map(ground_pseudo_type_desc_to_type_desc_det,
- ArgPseudoTypes),
- FunctorNumber = Num1
- ;
- find_functor_2(TypeInfo, Functor, Arity, Num1, FunctorNumber, ArgTypes)
- ).
-
det_term_to_type(Term, X) :-
( term_to_type(Term, X1) ->
X = X1
@@ -752,10 +731,6 @@
array.to_list(Array, List),
type_to_term(List, ArgsTerm).
-:- pred same_type(T::unused, T::unused) is det.
-
-same_type(_, _).
-
:- pred univ_list_to_term_list(list(univ)::in, list(term(T))::out) is det.
univ_list_to_term_list([], []).
Index: library/type_desc.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/library/type_desc.m,v
retrieving revision 1.54
diff -u -r1.54 type_desc.m
--- library/type_desc.m 7 Sep 2007 05:08:17 -0000 1.54
+++ library/type_desc.m 25 Sep 2007 03:29:38 -0000
@@ -89,6 +89,10 @@
%
:- some [T] pred has_type(T::unused, type_desc::in) is det.
+ % The predicate same_type/2 ensures type identity of the two arguments.
+ %
+:- pred same_type(T::unused, T::unused) is det.
+
% type_name(Type) returns the name of the specified type
% (e.g. type_name(type_of([2,3])) = "list.list(int)").
% Any equivalence types will be fully expanded.
@@ -464,6 +468,7 @@
TypeInfo_for_T = TypeInfo
").
+same_type(_, _).
% Export this function in order to use it in runtime/mercury_trace_external.c
:- pragma foreign_export("C", type_name(in) = out, "ML_type_name").
--
Ondrej Bojar (mailto:obo at cuni.cz / bojar at ufal.mff.cuni.cz)
http://www.cuni.cz/~obo
--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to: mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions: mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------
More information about the reviews
mailing list