ℂDuce: Documentation: Tips and Tricks
Poor-man ML-like polymorphism
CDuce does not currently support ML-like parametric polymorphism. For instance, a fold_left function can be defined for arbitrary fixed types, but not in a polymorphic way. Here is one possible definition:
let fold_left (f : ((A,B) -> A), x : A, l : [B*]) : A = let accu = ref A x in (transform l with y -> accu := f (!accu,y)); !accu
Of course, it is possible to set A=B=Any, but then one looses all the type information.
A technique which was suggested by Serge Leblanc on the CDuce users mailing list is to rely on the include feature. Here is an example, which assumes that the above piece of code is in a file fold_left.cd:
type A = Int type B = Int include "fold_left.cd";; let fold_int_int = fold_left type A = Latin1 type B = Any include "fold_left.cd";; let fold_string_any = fold_left let s1 = fold_int_int ((fun (x : Int, y : Int) : Int = x + y), 0, [ 1 2 3 ]) let s2 = fold_string_any ((fun (x : Latin1, y : Any) : Latin1 = x @ (string_of y)), "",[ 1 `x 'a' ])
Of course, this is nothing but textual substitution, and this could also be handled through a general-purpose preprocessor such as cpp or m4.
ℂDuce: Documentation: Tips and Tricks