[m-rev.] for review: move same_type and find_functor to interface

Ondrej Bojar bojar at csse.unimelb.edu.au
Mon Sep 24 21:20:24 AEST 2007


Incorporated Julien's suggestions:


Estimated hours taken: 0
Branch: main

library/term.m:
library/type_desc.m:
library/construct.m:
     Move same_type/2 and find_functor/5 from module term to interface 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	24 Sep 2007 11:09:53 -0000
@@ -197,6 +197,9 @@

  * We have added the predicate `dir.current_directory'.

+* We have added public interface to two predicates useful for custom term
+  construction: `construct.find_functor/5' and `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	24 Sep 2007 11:09:53 -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
@@ -116,6 +125,7 @@
  :- implementation.

  :- import_module require.
+:- import_module int.

  % For use by the Erlang backends.
  %
@@ -514,6 +524,28 @@
      }
  }").

+
+
+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)
+    ).
+
+
+
  :- 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	24 Sep 2007 11:09:53 -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	24 Sep 2007 11:09:53 -0000
@@ -89,6 +89,12 @@
      %
  :- 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.
@@ -465,6 +471,11 @@
  ").


+
+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").
  :- pragma foreign_export("IL", type_name(in) = out, "ML_type_name").
--------------------------------------------------------------------------
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