[m-rev.] diff: fix a bug reported by Ondrej Bojar

Zoltan Somogyi zs at csse.unimelb.edu.au
Tue Dec 19 13:55:07 AEDT 2006


Fix a bug reported by Ondrej Bojar.

compiler/goal_util.m:
	When renaming variables, rename them in the list of variables
	quantified by a trace goal too. This is the bug fix.

compiler/hlds_out.m:
	Print the list of variables quantified by a trace goal more nicely.

tests/valid/mert.m:
	A cut down version of Ondrej's test case.

tests/valid/Mmakefile:
	Enable the new test case.

Zoltan.

cvs diff: Diffing .
cvs diff: Diffing analysis
cvs diff: Diffing bindist
cvs diff: Diffing boehm_gc
cvs diff: Diffing boehm_gc/Mac_files
cvs diff: Diffing boehm_gc/cord
cvs diff: Diffing boehm_gc/cord/private
cvs diff: Diffing boehm_gc/doc
cvs diff: Diffing boehm_gc/include
cvs diff: Diffing boehm_gc/include/private
cvs diff: Diffing boehm_gc/libatomic_ops-1.2
cvs diff: Diffing boehm_gc/libatomic_ops-1.2/doc
cvs diff: Diffing boehm_gc/libatomic_ops-1.2/src
cvs diff: Diffing boehm_gc/libatomic_ops-1.2/src/atomic_ops
cvs diff: Diffing boehm_gc/libatomic_ops-1.2/src/atomic_ops/sysdeps
cvs diff: Diffing boehm_gc/libatomic_ops-1.2/src/atomic_ops/sysdeps/gcc
cvs diff: Diffing boehm_gc/libatomic_ops-1.2/src/atomic_ops/sysdeps/hpc
cvs diff: Diffing boehm_gc/libatomic_ops-1.2/src/atomic_ops/sysdeps/ibmc
cvs diff: Diffing boehm_gc/libatomic_ops-1.2/src/atomic_ops/sysdeps/icc
cvs diff: Diffing boehm_gc/libatomic_ops-1.2/src/atomic_ops/sysdeps/msftc
cvs diff: Diffing boehm_gc/libatomic_ops-1.2/src/atomic_ops/sysdeps/sunc
cvs diff: Diffing boehm_gc/libatomic_ops-1.2/tests
cvs diff: Diffing boehm_gc/tests
cvs diff: Diffing boehm_gc/windows-untested
cvs diff: Diffing boehm_gc/windows-untested/vc60
cvs diff: Diffing boehm_gc/windows-untested/vc70
cvs diff: Diffing boehm_gc/windows-untested/vc71
cvs diff: Diffing browser
cvs diff: Diffing bytecode
cvs diff: Diffing compiler
Index: compiler/goal_util.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/goal_util.m,v
retrieving revision 1.143
diff -u -b -r1.143 goal_util.m
--- compiler/goal_util.m	1 Dec 2006 15:03:57 -0000	1.143
+++ compiler/goal_util.m	16 Dec 2006 02:31:46 -0000
@@ -583,8 +583,9 @@
         rename_var(Must, Subn, Var0, Var),
         Reason = from_ground_term(Var)
     ;
-        Reason0 = trace_goal(_Flag, _Grade, _Env, _Vars, _QuantVars),
-        Reason = Reason0
+        Reason0 = trace_goal(Flag, Grade, Env, Vars, QuantVars0),
+        rename_var_list(Must, Subn, QuantVars0, QuantVars),
+        Reason = trace_goal(Flag, Grade, Env, Vars, QuantVars)
     ),
     rename_vars_in_goal(Must, Subn, Goal0, Goal).
 
Index: compiler/hlds_out.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/hlds_out.m,v
retrieving revision 1.414
diff -u -b -r1.414 hlds_out.m
--- compiler/hlds_out.m	14 Dec 2006 04:35:45 -0000	1.414
+++ compiler/hlds_out.m	16 Dec 2006 02:27:00 -0000
@@ -1606,7 +1606,7 @@
         list.foldl(write_trace_mutable_var_hlds(Indent + 1), MutableVars, !IO),
         write_indent(Indent + 1, !IO),
         io.write_string("% quantified vars ", !IO),
-        list.foldl(mercury_output_var(VarSet, AppendVarNums), QuantVars, !IO),
+        mercury_output_vars(VarSet, AppendVarNums, QuantVars, !IO),
         io.nl(!IO)
     ),
     write_goal_a(Goal, ModuleInfo, VarSet, AppendVarNums, Indent + 1, "\n",
cvs diff: Diffing compiler/notes
cvs diff: Diffing debian
cvs diff: Diffing debian/patches
cvs diff: Diffing deep_profiler
cvs diff: Diffing deep_profiler/notes
cvs diff: Diffing doc
cvs diff: Diffing extras
cvs diff: Diffing extras/base64
cvs diff: Diffing extras/cgi
cvs diff: Diffing extras/complex_numbers
cvs diff: Diffing extras/complex_numbers/samples
cvs diff: Diffing extras/complex_numbers/tests
cvs diff: Diffing extras/concurrency
cvs diff: Diffing extras/curs
cvs diff: Diffing extras/curs/samples
cvs diff: Diffing extras/curses
cvs diff: Diffing extras/curses/sample
cvs diff: Diffing extras/dynamic_linking
cvs diff: Diffing extras/error
cvs diff: Diffing extras/fixed
cvs diff: Diffing extras/gator
cvs diff: Diffing extras/gator/generations
cvs diff: Diffing extras/gator/generations/1
cvs diff: Diffing extras/graphics
cvs diff: Diffing extras/graphics/easyx
cvs diff: Diffing extras/graphics/easyx/samples
cvs diff: Diffing extras/graphics/mercury_glut
cvs diff: Diffing extras/graphics/mercury_opengl
cvs diff: Diffing extras/graphics/mercury_tcltk
cvs diff: Diffing extras/graphics/samples
cvs diff: Diffing extras/graphics/samples/calc
cvs diff: Diffing extras/graphics/samples/gears
cvs diff: Diffing extras/graphics/samples/maze
cvs diff: Diffing extras/graphics/samples/pent
cvs diff: Diffing extras/lazy_evaluation
cvs diff: Diffing extras/lex
cvs diff: Diffing extras/lex/samples
cvs diff: Diffing extras/lex/tests
cvs diff: Diffing extras/log4m
cvs diff: Diffing extras/logged_output
cvs diff: Diffing extras/moose
cvs diff: Diffing extras/moose/samples
cvs diff: Diffing extras/moose/tests
cvs diff: Diffing extras/mopenssl
cvs diff: Diffing extras/morphine
cvs diff: Diffing extras/morphine/non-regression-tests
cvs diff: Diffing extras/morphine/scripts
cvs diff: Diffing extras/morphine/source
cvs diff: Diffing extras/net
cvs diff: Diffing extras/odbc
cvs diff: Diffing extras/posix
cvs diff: Diffing extras/quickcheck
cvs diff: Diffing extras/quickcheck/tutes
cvs diff: Diffing extras/references
cvs diff: Diffing extras/references/samples
cvs diff: Diffing extras/references/tests
cvs diff: Diffing extras/solver_types
cvs diff: Diffing extras/solver_types/library
cvs diff: Diffing extras/stream
cvs diff: Diffing extras/stream/tests
cvs diff: Diffing extras/trailed_update
cvs diff: Diffing extras/trailed_update/samples
cvs diff: Diffing extras/trailed_update/tests
cvs diff: Diffing extras/windows_installer_generator
cvs diff: Diffing extras/windows_installer_generator/sample
cvs diff: Diffing extras/windows_installer_generator/sample/images
cvs diff: Diffing extras/xml
cvs diff: Diffing extras/xml/samples
cvs diff: Diffing extras/xml_stylesheets
cvs diff: Diffing java
cvs diff: Diffing java/runtime
cvs diff: Diffing library
cvs diff: Diffing mdbcomp
cvs diff: Diffing profiler
cvs diff: Diffing robdd
cvs diff: Diffing runtime
cvs diff: Diffing runtime/GETOPT
cvs diff: Diffing runtime/machdeps
cvs diff: Diffing samples
cvs diff: Diffing samples/c_interface
cvs diff: Diffing samples/c_interface/c_calls_mercury
cvs diff: Diffing samples/c_interface/cplusplus_calls_mercury
cvs diff: Diffing samples/c_interface/mercury_calls_c
cvs diff: Diffing samples/c_interface/mercury_calls_cplusplus
cvs diff: Diffing samples/c_interface/mercury_calls_fortran
cvs diff: Diffing samples/c_interface/simpler_c_calls_mercury
cvs diff: Diffing samples/c_interface/simpler_cplusplus_calls_mercury
cvs diff: Diffing samples/diff
cvs diff: Diffing samples/muz
cvs diff: Diffing samples/rot13
cvs diff: Diffing samples/solutions
cvs diff: Diffing samples/tests
cvs diff: Diffing samples/tests/c_interface
cvs diff: Diffing samples/tests/c_interface/c_calls_mercury
cvs diff: Diffing samples/tests/c_interface/cplusplus_calls_mercury
cvs diff: Diffing samples/tests/c_interface/mercury_calls_c
cvs diff: Diffing samples/tests/c_interface/mercury_calls_cplusplus
cvs diff: Diffing samples/tests/c_interface/mercury_calls_fortran
cvs diff: Diffing samples/tests/c_interface/simpler_c_calls_mercury
cvs diff: Diffing samples/tests/c_interface/simpler_cplusplus_calls_mercury
cvs diff: Diffing samples/tests/diff
cvs diff: Diffing samples/tests/muz
cvs diff: Diffing samples/tests/rot13
cvs diff: Diffing samples/tests/solutions
cvs diff: Diffing samples/tests/toplevel
cvs diff: Diffing scripts
cvs diff: Diffing slice
cvs diff: Diffing tests
cvs diff: Diffing tests/benchmarks
cvs diff: Diffing tests/debugger
cvs diff: Diffing tests/debugger/declarative
cvs diff: Diffing tests/dppd
cvs diff: Diffing tests/general
cvs diff: Diffing tests/general/accumulator
cvs diff: Diffing tests/general/string_format
cvs diff: Diffing tests/general/structure_reuse
cvs diff: Diffing tests/grade_subdirs
cvs diff: Diffing tests/hard_coded
cvs diff: Diffing tests/hard_coded/exceptions
cvs diff: Diffing tests/hard_coded/purity
cvs diff: Diffing tests/hard_coded/sub-modules
cvs diff: Diffing tests/hard_coded/typeclasses
cvs diff: Diffing tests/invalid
cvs diff: Diffing tests/invalid/purity
cvs diff: Diffing tests/misc_tests
cvs diff: Diffing tests/mmc_make
cvs diff: Diffing tests/mmc_make/lib
cvs diff: Diffing tests/par_conj
cvs diff: Diffing tests/recompilation
cvs diff: Diffing tests/tabling
cvs diff: Diffing tests/term
cvs diff: Diffing tests/trailing
cvs diff: Diffing tests/valid
Index: tests/valid/Mmakefile
===================================================================
RCS file: /home/mercury/mercury1/repository/tests/valid/Mmakefile,v
retrieving revision 1.183
diff -u -b -r1.183 Mmakefile
--- tests/valid/Mmakefile	7 Nov 2006 01:59:04 -0000	1.183
+++ tests/valid/Mmakefile	16 Dec 2006 04:02:09 -0000
@@ -148,6 +148,7 @@
 	mc_hhf_nonlocals_bug \
 	mc_implied_modes \
 	merge_ground_any \
+	mert \
 	middle_rec_labels \
 	modes_bug \
 	mode_syntax \
Index: tests/valid/mert.m
===================================================================
RCS file: tests/valid/mert.m
diff -N tests/valid/mert.m
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/valid/mert.m	16 Dec 2006 04:22:22 -0000
@@ -0,0 +1,275 @@
+% This condensed version of part of a program by Ondrej Bojar is a
+% regression test.
+%
+% Versions of the compiler before 16 Dec 2006 aborted when generating code
+% for the optimize predicate. The problem was that inlining, when it inlined
+% nbestlist_to_data in optimize, did not adjust the variable numbers of the
+% variables quantified by the trace goal in nbestlist_to_data. As a result,
+% quantification thought that a variable (AllCandsC) computed before the
+% trace goal was in fact quantified by the trace goal, which meant that it
+% was not in the nonlocal set of the trace goal, which in turn meant that
+% it was not in the nonlocal set of the goal that generated it, which lead
+% to the deletion of that goal. After that, the presence of a consumer of
+% AllCandsC without a generator guaranteed trouble.
+
+:- module mert.
+% bleu-based minimum error rate training, using Philipp Koehn's code
+:- interface.
+
+:- import_module float, list.
+
+:- type weights == list(float).
+
+:- type randomize --->
+          no_randomize
+        ; randomize(
+            mins :: list(float),
+            maxs :: list(float),
+            iters :: int
+          ).
+
+:- func optimize(scorednbestlist, randomize, weights) = weights.
+% given an nbestlist, reference translations, starting weights and possibly
+% a randomization requirement, optimize weights to get highest bleu
+
+:- implementation.
+
+:- import_module string, int.
+
+:- pragma foreign_decl("C", "
+#include ""mert.koehn.h""
+").
+
+:- pragma foreign_code("C", "
+#include ""mert.koehn.c""
+  /* all the C functions */
+").
+
+%% Creating data for MERTing:
+
+:- type c_candidate. % C-implemented, represents one sentence
+:- pragma foreign_type("C", c_candidate, "candidate_t *",
+    [stable, can_pass_as_mercury_type]).
+
+:- type data. % C-implemented, represents the whole nbestlist
+:- pragma foreign_type("C", data, "data_t *",
+    [stable, can_pass_as_mercury_type]).
+
+:- type feats == list(float). % score breakdown
+
+:- func new_c_candidate(feats, bleucomps) = c_candidate.
+% create a new candidate
+
+new_c_candidate(F, C) = new_c_candidate(length(F), F, length(C), C).
+
+:- func new_c_candidate(int::in, feats::in, int::in, bleucomps::in) = (c_candidate::uo) is det.
+
+:- pragma foreign_proc("C",
+new_c_candidate(NFeats::in, Feats::in, NComps::in, Comps::in) = (C::uo),
+  [promise_pure, will_not_call_mercury, thread_safe], "
+  C = MR_GC_malloc(sizeof(candidate_t));
+  C->features = MR_GC_malloc(NFeats*sizeof(float));
+  C->comps = MR_GC_malloc(NComps*sizeof(int));
+  C->m = 0.0;
+  C->b = 0.0;
+  /* copy list to array */
+  float *p = C->features;
+  while (!MR_list_is_empty(Feats)) {
+    *p = MR_list_head(Feats);
+    p++;
+    Feats = MR_list_tail(Feats);
+  }
+  /* copy list to array */
+  int *q = C->comps;
+  while (!MR_list_is_empty(Comps)) {
+    *q = MR_list_head(Comps);
+    q++;
+    Comps = MR_list_tail(Feats);
+  }
+").
+
+:- func nbestlist_to_data(scorednbestlist) = data.
+  % construct data (using GC_malloc)
+nbestlist_to_data(NBL) = OutData :-
+  Lengths = list__map(length, NBL),
+    % how many candidates do we have per sentence
+  AllCands = list__condense(NBL),
+  AllCandsC = list__map(
+    func(scored_candidate(Scores, BLEUComps))
+      = new_c_candidate(Scores, BLEUComps),
+    AllCands),
+  trace[io(!IO)]debugstr("nbestlist_to_data: lengths, allcands: ", {0+length(Lengths), 0+length(AllCandsC)}, !IO),
+  OutData = new_c_data(length(Lengths), Lengths, length(AllCandsC), AllCandsC).
+
+:- func new_c_data(int, list(int), int, list(c_candidate)) = data.
+:- mode new_c_data(in, in, in, in) = uo is det.
+:- pragma foreign_proc("C",
+new_c_data(NSents::in, CandsPerSent::in, TotNCands::in, AllCands::in) = (D::uo),
+  [promise_pure, will_not_call_mercury, thread_safe], "
+  D = MR_GC_malloc(sizeof(data_t));
+  D->sents_max = NSents;
+  D->sents_n = NSents;
+  D->cands_n = MR_GC_malloc(sizeof(int)*D->sents_max);
+  /* copy list to array: CandsPerSent */
+  int *q = D->cands_n;
+  while (!MR_list_is_empty(CandsPerSent)) {
+    *q = MR_list_head(CandsPerSent);
+    q++;
+    CandsPerSent = MR_list_tail(CandsPerSent);
+  }
+
+  /* create master array for candidates and then set data->sents
+     to point into it */
+  candidate_t *MasterCands;
+  MasterCands = MR_GC_malloc(TotNCands * sizeof(candidate_t));
+  /* copy list to array: MasterCands */
+  candidate_t *p = MasterCands;
+  while (!MR_list_is_empty(AllCands)) {
+    candidate_t *thisC = (candidate_t*) MR_list_head(AllCands);
+    *p = *thisC; /* copy the contents */
+    p++;
+    AllCands = MR_list_tail(AllCands);
+  }
+
+  D->sents = MR_GC_malloc(D->sents_n * sizeof(candidate_t *));
+  int CandsSoFar = 0;
+  int sent_i;
+  for (sent_i=0; sent_i<D->sents_n; sent_i++) {
+    D->sents[sent_i] = MasterCands+CandsSoFar;
+    CandsSoFar += D->cands_n[sent_i];
+  }
+
+  /* GC will collect the masterarray 'MasterCands' */
+").
+
+:- type point. % C-implemented, represents the whole nbestlist
+:- pragma foreign_type("C", point, "point_t *",
+    [stable, can_pass_as_mercury_type]).
+
+optimize(NBL, Rand, InW) = OutW :-
+  Data = nbestlist_to_data(NBL),
+  (
+  Rand = no_randomize,
+    OutWPoint = optimize_koehn(Data, construct_point(InW))
+  ;
+  Rand = randomize(Mins, Maxs, Iters),
+    MinsPoint = construct_point(Mins),
+    MaxsPoint = construct_point(Maxs),
+    BestSoFar = optimize_koehn(Data, construct_point(InW)),
+      % one iteration from current weights
+    OutWPoint = optimize_random(Data, BestSoFar, MinsPoint, MaxsPoint, Iters),
+      % plus some random seeds
+    % now deconstruct mins and maxs
+    _ = deconstruct_point(MinsPoint),
+    _ = deconstruct_point(MaxsPoint)
+  ),
+  OutW = deconstruct_point(OutWPoint).
+
+:- func optimize_random(data, point, point, point, int) = point.
+   % warning, destructively modifies the BestSoFar!
+:- pragma foreign_proc("C",
+optimize_random(Data::in, BestSoFar::in, Min::in, Max::in, Iter::in) = (Out::out),
+  [promise_pure, will_not_call_mercury, thread_safe], "
+  int i;
+  point_t *newpoint;
+  for (i=0; i<Iter; i++) {
+    newpoint = random_point(Min, Max);
+    newpoint = optimize_koehn(Data, newpoint);
+    if (newpoint->score > BestSoFar->score)
+      point_copy_contents(newpoint, BestSoFar);
+    point_delete(newpoint);
+  }
+  Out = BestSoFar;
+").
+
+:- func optimize_koehn(data, point) = point.
+  % destructively replace contents of point doing one iteration of optimization
+
+:- pragma foreign_proc("C",
+optimize_koehn(Data::in, In::in) = (Out::out),
+  [promise_pure, will_not_call_mercury, thread_safe], "
+  Out = optimize_koehn(Data, In);
+").
+
+:- func construct_point(list(float)) = point.
+  % construct a new point, uses plain malloc!
+
+:- func deconstruct_point(point) = list(float).
+  % delete point representation and return the contents as a list
+
+:- pragma foreign_proc("C",
+construct_point(List::in) = (Point::out),
+  [promise_pure, will_not_call_mercury, thread_safe], "
+  int i;
+
+  Point = new_point();
+  for(i=0; i<dim; i++) {
+    if (MR_list_is_empty(List)) {
+      fprintf(stderr, ""Failed to construct point, expected %i dimensions!\\n"", dim);
+      exit(1);
+    }
+    Point->weights[i] = MR_list_head(List);
+    List = MR_list_tail(List);
+  }
+").
+
+:- pragma foreign_proc("C",
+deconstruct_point(Point::in) = (List::out),
+  [promise_pure, will_not_call_mercury, thread_safe], "
+  MR_Word Tail;
+  float v;
+  int i;
+  Tail = MR_list_empty();
+  for(i=dim; i<=0; i--) {
+    v = Point->weights[i];
+    Tail = MR_list_cons(v, Tail);
+  }
+  point_delete(Point);
+  List = Tail;
+").
+
+:- type bleucomps == list(int).
+
+:- type nbestlist == list(list(candidate)).
+:- type candidate ---> candidate(
+          words :: list(string), % the sentence itself
+          scores :: list(float)
+        ).
+
+:- type scorednbestlist == list(list(scored_candidate)).
+:- type scored_candidate ---> scored_candidate(
+          scores2 :: list(float),
+          bleucomps :: bleucomps
+        ).
+
+:- func score_nbestlist(refs, nbestlist) = scorednbestlist.
+% score nbestlist (do not complete BLEU scores, just collect the breakdowns)
+% also removes duplicates from nbestlist
+
+score_nbestlist(_, _) = [].
+
+:- type word == string.
+
+:- type refs ---> refs(
+          refcount :: int,
+          sentcount :: int,
+          lengths :: version_array(list(int)), % sent->reference->length
+          seen_ngrams :: version_array2d(approved_per_sent) % n->sent->approved
+        ).
+
+:- type ngram == list(word).
+:- type approved_per_sent == map(ngram, int).
+          % how many times was each ngram seen
+
+:- import_module map.
+:- import_module version_array.
+:- import_module version_array2d.
+:- import_module io.
+
+:- pred debugstr(string::in, T::in, io::di, io::uo) is det.
+
+debugstr(Msg, Data, !IO) :-
+	io__stderr_stream(E, !IO),
+	io__write_string(E, Msg, !IO),
+	io__write(E, Data, !IO),
+	io__nl(E, !IO).
cvs diff: Diffing tests/warnings
cvs diff: Diffing tools
cvs diff: Diffing trace
cvs diff: Diffing util
cvs diff: Diffing vim
cvs diff: Diffing vim/after
cvs diff: Diffing vim/ftplugin
cvs diff: Diffing vim/syntax
--------------------------------------------------------------------------
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