[m-rev.] diff: foreign export bool for java

Peter Wang novalazy at gmail.com
Wed Aug 12 12:02:53 AEST 2009


On 2009-08-11, Julien Fischer <juliensf at csse.unimelb.edu.au> wrote:
> 
> My main concern was that in modules that have both C and Java foreign
> code (e.g. many of the standard library modules), the slight difference
> between the two would be a source of errors.
> 
> Looking at it some more though, I think we should dispense with both the
> ML_ and MR_ prefixes in Java.  The sort of namespace problems they are intended
> to address can't arise in Java anyway.

Ok, I have done that on the documented symbols.  Even if they aren't necessary
I find prefixes to be useful to indicate exported predicates, but from
the user's point of view they probably don't make any sense.

> The diff looks fine - a further improvement would be to document
> pragma foreign_{export_enum,enum} for the Java backend.

Committed with these further changes.

diff --git a/doc/reference_manual.texi b/doc/reference_manual.texi
index c55041d..f59cbe9 100644
--- a/doc/reference_manual.texi
+++ b/doc/reference_manual.texi
@@ -6950,14 +6950,14 @@ and @code{string} are mapped to the Java types
 
 For the Mercury standard library type @samp{bool.bool}, there is a
 corresponding Java type, @code{bool.Bool_0}.  Java code can refer to the
-boolean data constructors @samp{yes} and @samp{no}, as @code{bool.MR_YES}
-and @code{bool.MR_NO} respectively.
+boolean data constructors @samp{yes} and @samp{no}, as @code{bool.YES}
+and @code{bool.NO} respectively.
 
 For the Mercury standard library type @samp{builtin.comparison_result}, there
 is a corresponding Java type, @code{builtin.Comparison_result_0}.  Java code
 can refer to the data constructors of this type, @samp{(<)}, @samp{(=)} and
- at samp{(>)}, as @code{builtin.MR_COMPARE_LESS}, @code{builtin.MR_COMPARE_EQUAL}
-and @code{builtin.MR_COMPARE_GREATER} respectively.
+ at samp{(>)}, as @code{builtin.COMPARE_LESS}, @code{builtin.COMPARE_EQUAL}
+and @code{builtin.COMPARE_GREATER} respectively.
 
 Mercury variables of a type for which there is a Java @samp{pragma
 foreign_type} declaration (@pxref{Using foreign types from Mercury}) will be
@@ -7025,11 +7025,11 @@ Mercury lists can be manipulated by Java code using the following methods,
 which are defined by the Mercury implementation.
 
 @example
-list.MR_is_empty(list)      /* test if a list is empty */
-list.MR_det_head(list)      /* get the head of a list */
-list.MR_det_tail(list)      /* get the tail of a list */
-list.MR_empty_list()        /* create an empty list */
-list.MR_cons(head, tail)    /* construct a list with the given head and tail */
+list.is_empty(list)     /* test if a list is empty */
+list.det_head(list)     /* get the head of a list */
+list.det_tail(list)     /* get the tail of a list */
+list.empty_list()       /* create an empty list */
+list.cons(head, tail)   /* construct a list with the given head and tail */
 @end example
 
 @node Erlang data passing conventions
@@ -8105,6 +8105,10 @@ standard library module @samp{type_desc}.
 
 @menu
 * Using pragma foreign_type for Java :: Declaring Java types in Mercury
+* Using pragma foreign_export_enum for Java :: Using Mercury enumerations in
+                                               Java
+* Using pragma foreign_enum for Java :: Assigning Mercury enumerations
+                                        values in Java
 * Using pragma foreign_proc for Java :: Calling Java code from Mercury
 * Using pragma foreign_export for Java :: Calling Mercury from Java code
 * Using pragma foreign_decl for Java :: Including Java declarations in Mercury
@@ -8131,6 +8135,24 @@ Furthermore, any Mercury procedure exported with @samp{pragma foreign_export}
 will use @var{JavaType} as the type for any
 parameters whose Mercury type is @var{MercuryTypeName}.
 
+ at node Using pragma foreign_export_enum for Java
+ at subsubsection Using pragma foreign_export_enum for Java
+
+For Java the symbolic names generated by a @samp{pragma foreign_export_enum}
+must form valid Java identifiers.
+These identifiers are used as the names of static class members
+which are assigned instances of the enumeration class.
+
+The default mapping used by @samp{pragma foreign_export_enum}
+declarations for Java is to use the Mercury constructor name as the
+base of the symbolic name.  For example, the symbolic name for
+the Mercury constructor @samp{foo} would be @code{foo}.
+
+ at node Using pragma foreign_enum for Java
+ at subsubsection Using pragma foreign_enum for Java
+
+ at samp{pragma foreign_enum} is currently not supported for Java.
+
 @node Using pragma foreign_proc for Java
 @subsubsection Using pragma foreign_proc for Java
 
diff --git a/library/bitmap.m b/library/bitmap.m
index 4bd2a0a..f542851 100644
--- a/library/bitmap.m
+++ b/library/bitmap.m
@@ -1631,21 +1631,21 @@ bytes_equal(Index, MaxIndex, BM1, BM2) :-
         may_not_duplicate],
 "
     if (BM1.num_bits < BM2.num_bits) {
-        Result = builtin.MR_COMPARE_LESS;
+        Result = builtin.COMPARE_LESS;
     } else if (BM1.num_bits > BM2.num_bits) {
-        Result = builtin.MR_COMPARE_GREATER;
+        Result = builtin.COMPARE_GREATER;
     } else {
-        Result = builtin.MR_COMPARE_EQUAL;
+        Result = builtin.COMPARE_EQUAL;
         for (int i = 0; i < BM1.elements.length; i++) {
             // Mask off sign bits.
             int b1 = ((int) BM1.elements[i]) & 0xff;
             int b2 = ((int) BM2.elements[i]) & 0xff;
             if (b1 < b2) {
-                Result = builtin.MR_COMPARE_LESS;
+                Result = builtin.COMPARE_LESS;
                 break;
             }
             if (b1 > b2) {
-                Result = builtin.MR_COMPARE_GREATER;
+                Result = builtin.COMPARE_GREATER;
                 break;
             }
         }
diff --git a/library/bool.m b/library/bool.m
index 6322ab1..944b9ba 100644
--- a/library/bool.m
+++ b/library/bool.m
@@ -79,8 +79,8 @@
 
 :- pragma foreign_export_enum("Java", bool/0, [],
     [
-        no  - "MR_NO",
-        yes - "MR_YES"
+        no  - "NO",
+        yes - "YES"
     ]).
 
 :- instance enum(bool) where [
diff --git a/library/builtin.m b/library/builtin.m
index 9a6aedc..521056f 100644
--- a/library/builtin.m
+++ b/library/builtin.m
@@ -604,9 +604,9 @@ get_one_solution_io(Pred, X, !IO) :-
 
 :- pragma foreign_export_enum("Java", comparison_result/0, [],
     [
-        (=) - "MR_COMPARE_EQUAL",
-        (<) - "MR_COMPARE_LESS",
-        (>) - "MR_COMPARE_GREATER"
+        (=) - "COMPARE_EQUAL",
+        (<) - "COMPARE_LESS",
+        (>) - "COMPARE_GREATER"
     ]).
 
 ordering(X, Y) = R :-
diff --git a/library/list.m b/library/list.m
index 58aa296..5804677 100644
--- a/library/list.m
+++ b/library/list.m
@@ -2783,27 +2783,27 @@ list_to_doc_2([X | Xs]) = Doc :-
 ** the interfaces would expect type_info arguments.
 */
 
-public static List_1 MR_empty_list()
+public static List_1 empty_list()
 {
     return new List_1.F_nil_0();
 }
 
-public static List_1 MR_cons(Object H, List_1 T)
+public static List_1 cons(Object H, List_1 T)
 {
     return new List_1.F_cons_2(H, T);
 }
 
-public static boolean MR_is_empty(List_1 lst)
+public static boolean is_empty(List_1 lst)
 {
     return (lst instanceof List_1.F_nil_0);
 }
 
-public static Object MR_det_head(List_1 lst)
+public static Object det_head(List_1 lst)
 {
     return ((List_1.F_cons_2) lst).F1;
 }
 
-public static List_1 MR_det_tail(List_1 lst)
+public static List_1 det_tail(List_1 lst)
 {
     return ((List_1.F_cons_2) lst).F2;
 }
@@ -2813,12 +2813,12 @@ public static List_1 MR_det_tail(List_1 lst)
 ** You must use a new instance of this class for each loop!
 */
 
-public static class MR_ListIterator<E>
+public static class ListIterator<E>
     implements java.lang.Iterable, java.util.Iterator<E>
 {
     private List_1 lst;
 
-    public MR_ListIterator(List_1 lst)
+    public ListIterator(List_1 lst)
     {
         this.lst = lst;
     }
@@ -2830,14 +2830,14 @@ public static class MR_ListIterator<E>
 
     public boolean hasNext()
     {
-        return lst instanceof List_1.F_cons_2;
+        return !is_empty(lst);
     }
 
     public E next()
     {
-        if (lst instanceof List_1.F_cons_2) {
-            E head = (E) ((List_1.F_cons_2) lst).F1;
-            lst = ((List_1.F_cons_2) lst).F2;
+        if (is_empty(lst)) {
+            E head = (E) det_head(lst);
+            lst = det_tail(lst);
             return head;
         } else {
             throw new java.util.NoSuchElementException();
diff --git a/library/rtti_implementation.m b/library/rtti_implementation.m
index bb36992..d5e85c1 100644
--- a/library/rtti_implementation.m
+++ b/library/rtti_implementation.m
@@ -1347,12 +1347,12 @@ iterate(Start, Max, Func) = Results :-
         final Object[] args = new Object[arity];
 
         for (int i = 0; i < arity; i++) {
-            univ.Univ_0 head = (univ.Univ_0) list.MR_det_head(lst);
+            univ.Univ_0 head = (univ.Univ_0) list.det_head(lst);
             args[i] = univ.ML_unravel_univ(head)[1];
-            lst = list.MR_det_tail(lst);
+            lst = list.det_tail(lst);
         }
 
-        if (list.MR_is_empty(lst)) {
+        if (list.is_empty(lst)) {
             return args;
         } else {
             return null;
@@ -1434,8 +1434,8 @@ iterate(Start, Max, Func) = Results :-
         final Object[] array = new Object[arity];
 
         for (int i = 0; i < arity; i++) {
-            array[i] = list.MR_det_head(lst);
-            lst = list.MR_det_tail(lst);
+            array[i] = list.det_head(lst);
+            lst = list.det_tail(lst);
         }
 
         return array;
@@ -1466,9 +1466,9 @@ construct(_, _, _) = _ :-
     Object[] args_array = new Object[Arity];
 
     for (int i = 0; i < Arity; i++) {
-        univ.Univ_0 head = (univ.Univ_0) list.MR_det_head(args_list);
+        univ.Univ_0 head = (univ.Univ_0) list.det_head(args_list);
         args_array[i] = univ.ML_unravel_univ(head)[1];
-        args_list = list.MR_det_tail(args_list);
+        args_list = list.det_tail(args_list);
     }
 
     Object[] args = ML_list_to_array((list.List_1) ArgTypes, Arity);
diff --git a/library/string.m b/library/string.m
index 4ec4b36..6b3c11f 100644
--- a/library/string.m
+++ b/library/string.m
@@ -1339,10 +1339,10 @@ string.to_char_list(Str::uo, CharList::in) :-
     [will_not_call_mercury, promise_pure, thread_safe, will_not_modify_trail,
         does_not_affect_liveness, no_sharing],
 "
-    list.List_1 lst = list.MR_empty_list();
+    list.List_1 lst = list.empty_list();
     for (int i = Str.length() - 1; i >= 0; i--) {
         char c = Str.charAt(i);
-        lst = list.MR_cons(c, lst);
+        lst = list.cons(c, lst);
     }
     CharList = lst;
 ").
@@ -1438,7 +1438,7 @@ string.from_char_list(Chars::in, Str::uo) :-
         does_not_affect_liveness],
 "
     java.lang.StringBuilder sb = new StringBuilder();
-    Iterable<Character> iterable = new list.MR_ListIterator((list.List_1) CharList);
+    Iterable<Character> iterable = new list.ListIterator((list.List_1) CharList);
     for (char c : iterable) {
         sb.append(c);
     }
@@ -1666,7 +1666,7 @@ string.append_list(Lists, string.append_list(Lists)).
 "
     java.lang.StringBuilder sb = new java.lang.StringBuilder();
 
-    Iterable<String> iterable = new list.MR_ListIterator((list.List_1) Strs);
+    Iterable<String> iterable = new list.ListIterator((list.List_1) Strs);
     for (String s : iterable) {
         sb.append(s);
     }
@@ -1750,7 +1750,7 @@ string.append_list(Strs::in) = (Str::uo) :-
     java.lang.StringBuilder sb = new java.lang.StringBuilder();
     boolean add_sep = false;
 
-    Iterable<String> iterable = new list.MR_ListIterator((list.List_1) Strs);
+    Iterable<String> iterable = new list.ListIterator((list.List_1) Strs);
     for (String s : iterable) {
         if (add_sep) {
             sb.append(Sep);

--------------------------------------------------------------------------
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