I'm familiarizing with the basic of MiniZinc. So, armed with the MiniZinc IDE, I write snippets like
solve satisfy;
string: s1 = "hello";
string: s2 = "world";
function list of int: cdr(list of int: v) =
[v[i] | i in 1..length(v)];
function list of string: cdr(list of string: v) =
[v[i] | i in 1..length(v)];
function string: concat(list of string: V) =
if length(V) == 0 then "" else V[0] ++ concat(cdr(V)) endif;
output [concat([s1," ",s2])++" "++show(cdr([1,2,3]))];
that displays
Compiling hello.mzn
Running hello.mzn
hello world [1, 2, 3]
----------
Finished in 49msec
now, the cdr of a list of ints seems to be wrong. I assume it's my bug, albeit I cannot spot it.
Could assertions help me here ? Since I'm going to use Gecode (and then I have Gist) to actually put my code in production, could I follow that route ?
Any hint appreciated...
edit this snippet
solve satisfy;
function list of string: cdr_s(list of string: v) =
[v[i] | i in 2..length(v)];
function string: vcat(list of string: V) =
if length(V) == 0 then "" else V[1] ++ vcat(cdr_s(V)) endif;
output [vcat(["hello"," ","world"])];
reports
MiniZinc: type error: no function or predicate with this signature found: `cdr_s(array[int] of string)'
/tmp/MiniZinc IDE-9nYiuF/hello.ozn:2
I'm a little confused of what you think is a bug and there are some other issues.
The output of the model seems fine with the "cdr([1,2,3])" giving "[1,2,3]". The name "cdr" suggests that you want the "but first" function, but MiniZinc is a default 1-based system (not 0-based) so your function should probably be
function list of int: cdr(list of int: v) =
[v[i] | i in 2..length(v)];
Since one can define the indices of an array (e.g. that start index is 0), a more general definition is this (which I'm not very happy with but you probably get my point):
function list of int: cdr3(list of int: v) =
[v[i] | i in index_set(v) diff {min(index_set(v))}];
So now you can write something like this:
% ...
array[int] of int: t = array1d(0..3, [1,2,3,4])
output [
show(cdr3(t))
];
Also, your "concat" function is not used at all, instead the built-in "concat" it used. (Try to rename your version to "concat1".) That is also why you don't get an error for the "V[0]" construct (which should give out of bounds error). I would have expected that trying to re-define a built-in would generate an error, but MiniZinc 2.0 is more permissive than version 1.6 in certain areas.
And I agree with Axel's general comment. As a general programming language MiniZinc is not very impressive (at least in my book). The real power come when you add constraints and decision variables to the model. Please note that MiniZinc's list/array handling with decision variables is not as dynamic as Prolog. In general you should always think in term of arrays as having fixed length.
It's great that you start to look into MiniZinc. I hope that these comments actually help you when learning MiniZinc.
/Hakan