20 Sep 09:39 2007

compose (o) for n-ary functions

```Hi everyone,

I'm finally back to learn Alice, and the problem of the day is how to make
this nicer:

List.sort (fn (a,b)=>Int.compare ((look b), (look a))) aList

where look is a function that does some calculations on the arguments
(well, a Map lookup, actually).

Now, what I'd like to do is build the comparation function via function
composition instead of using the inline definition.

If compare was a single-valued function I'd do:
Int.compare o look

but since it takes 2 args I don't know how to handle it.

My solution would be
fun productCompose f g= fn (a,b)=>g(f a,f b);
val productCompose : ('a -> 'b) -> ('b * 'b -> 'c) -> 'a * 'a -> 'c = _fn

but it seem too specific, and I wonder if there is a general approach
(apart from function definition :)

Is it possible to define (or better, it is alread there) an n-compose
function that I could use in conjunction with Fn.curry ?

I am unable to find a type for it, but I have the feeling it is impossible
to define (I can't come up with a type for it) ?
```

20 Sep 14:09 2007

Re: compose (o) for n-ary functions

```gabriele renzi wrote:
>  List.sort (fn (a,b)=>Int.compare ((look b), (look a))) aList
>
> where look is a function that does some calculations on the arguments
> (well, a Map lookup, actually).
>
> Now, what I'd like to do is build the comparation function via function
> composition instead of using the inline definition.
>
> If compare was a single-valued function I'd do:
>  Int.compare o look
>
> but since it takes 2 args I don't know how to handle it.
>

Pair.mapBoth may be what you want, so you can write

List.sort (Int.compare o Pair.mapBoth look) aList

> Is it possible to define (or better, it is alread there) an n-compose
> function that I could use in conjunction with Fn.curry ?
>
> I am unable to find a type for it, but I have the feeling it is impossible
> to define (I can't come up with a type for it) ?
>

There is no direct way of expressing what you intend in a more general
way. You can do so using a significant amount of type trickery for
encoding so-called type-indexed values, though, e.g. look here:

```

20 Sep 14:38 2007

Re: compose (o) for n-ary functions

```On Thu, 20 Sep 2007 14:09:26 +0200, Andreas Rossberg wrote:

> gabriele renzi wrote:
<snip>

>> If compare was a single-valued function I'd do:
>>  Int.compare o look
>>
>> but since it takes 2 args I don't know how to handle it.
>>
>
> Pair.mapBoth may be what you want, so you can write
>
>   List.sort (Int.compare o Pair.mapBoth look) aList

ah, I should think of the Pair class more often, thank you.

>> Is it possible to define (or better, it is alread there) an n-compose
>> function that I could use in conjunction with Fn.curry ?
>>
>> I am unable to find a type for it, but I have the feeling it is impossible
>> to define (I can't come up with a type for it) ?
>>
>
> There is no direct way of expressing what you intend in a more general
> way. You can do so using a significant amount of type trickery for
> encoding so-called type-indexed values, though, e.g. look here:
<snip>

thanks for the reference, it will be hard to digest but it seems quite
```

21 Sep 01:27 2007

2 questions on constraints (x=a or b, FD.gcc arguments)

```Hi everyone,

please excuse my newbieness again, I'm trying 2 questions at once to save
time :)

1) is it possible to express a constraint so that an FD.intvar is bound to
be one of two choices which are themselves FD.intvar's ?
I believe this should be done with FD.gcc4 but I'm not sure if this is the
right thing.

2) about gcc*: what is the meaning of all the paramaters in the
various FD.gcc ?
[s,x,min,max,level] are clear from the description, but
[v,c,m,ulow,uup,all] are not so obvious, in my humble opinion.

As usual, thanks in advance.
```
21 Sep 06:26 2007

Save a pickle in a SQL table

```
I'm practically new to ML and to Alice.

Is there a way to save a package instead of in a file as a pickle as a, say,
string of bytes as a, say, blob, in a SQL table?

Many thanks for an answer in advance.

Hans Peter
```
21 Sep 10:43 2007

Re: 2 questions on constraints (x=a or b, FD.gcc arguments)

```gabriele renzi wrote:

> 1) is it possible to express a constraint so that an FD.intvar is
> bound to
> be one of two choices which are themselves FD.intvar's ?
> I believe this should be done with FD.gcc4 but I'm not sure if this
> is the
> right thing.

Do you mean a constraint like (x = y) or (x = z)?  That's not what
gcc is used for, but you can use reified constraints to achieve this:

val b1 = FD.boolvar space
val b2 = FD.boolvar space
val b3 = FD.intvar2boolvar(space, FD.intvar(space, #[(1,1)]))   (* b3
= true *)
val _ = FD.disj(space, b1, b2, b3)   (* b1 or b2 = b3 *)
val _ = FD.Reified.rel(space, x, FD.eq, y, b1)    (* x == y    <=>
b1 *)
val _ = FD.Reified.rel(space, x, FD.eq, z, b2)    (* x == z    <=>
b2 *)

> 2) about gcc*: what is the meaning of all the paramaters in the
> various FD.gcc ?
> [s,x,min,max,level] are clear from the description, but
> [v,c,m,ulow,uup,all] are not so obvious, in my humble opinion.

You're right, they are not obvious at all, and the documentation for
gcc practically doesn't exist.  In fact, we realized that this
interface doesn't work as expected for some cases, and -
```

21 Sep 11:37 2007

Re: 2 questions on constraints (x=a or b, FD.gcc arguments)

```On Fri, 21 Sep 2007 10:43:15 +0200, Guido Tack wrote:

> Do you mean a constraint like (x = y) or (x = z)?  That's not what
> gcc is used for, but you can use reified constraints to achieve this:

yes. I thought I could express this via a cardinality constraint on the
triple [x,y,z] imposing that there are exactly two different values, and
that I could do this via gcc.

Actually I also gave some thought to the idea of using FD.count, but it
seems that there is a mismatch between the signatures:
countII : space * intvar vector *  int * relation * int -> unit
countVI : space * intvar vector *  intvar * relation * int -> unit
countIV : space * intvar vector *  int * relation * intvar -> unit
countVV : space * intvar vector *  intvar * relation * intvar -> unit

and the documentation

countII (s, v, rel1, n, rel2, m level)
countVI (s, v, rel1, x, rel2, m level)
countIV (s, v, rel1, n, rel2, y level)
countVV (s, v, rel1, x, rel2, y level)

(the rel1 in the latters doesn't seem to be there in the formers)
so I gave up on it.

<snip code>
I see this makes sense. And I didn't know I could express a domain
directly through its representation :)

```

21 Sep 13:17 2007

Re: Save a pickle in a SQL table

```Hans Peter Würmli wrote:
> Is there a way to save a package instead of in a file as a pickle as a, say,
> string of bytes as a, say, blob, in a SQL table?
>

Internally, functionality to pickle something to a byte string exists,
but it is not currently exposed in the library. But here is an
inofficial way to access it:

import "x-alice:/lib/system/UnsafeComponent"
val c = Component.fromPackage(pack (val it = 666) : (val it : int))
val s = UnsafeComponent.pack_ c
val c' = UnsafeComponent.unpack_ s
val p = ComponentManager.eval(Url.fromString "", c')
open unpack p : (val it : int)

I wouldn't necessarily recommend that, though.

We may add more official support in future versions.

Hope this helps,
- Andreas
```
21 Sep 23:14 2007

Re: 2 questions on constraints (x=a or b, FD.gcc arguments)

```On Fri, 21 Sep 2007 10:43:15 +0200, Guido Tack wrote:

> Do you mean a constraint like (x = y) or (x = z)?  That's not what
> gcc is used for, but you can use reified constraints to achieve this:
>
> val b1 = FD.boolvar space
> val b2 = FD.boolvar space
> val b3 = FD.intvar2boolvar(space, FD.intvar(space, #[(1,1)]))   (* b3
> = true *)
> val _ = FD.disj(space, b1, b2, b3)   (* b1 or b2 = b3 *)
> val _ = FD.Reified.rel(space, x, FD.eq, y, b1)    (* x == y    <=>
> b1 *)
> val _ = FD.Reified.rel(space, x, FD.eq, z, b2)    (* x == z    <=>
> b2 *)

one more thing:
I think that reifying with FD.Reified is equivalent to using
Modeling.HOLDS, is this true?
If true, am I right in thinking that the above is somewhat the
same as

Modeling.postTrue(space, Modeling.HOLDS(x`=y) `| Modeling.HOLDS(x`=z) )

where x,y and z are terms instead of intvars (types seem correct) ?
```
24 Sep 03:31 2007

Re: Save a pickle in a SQL table

```
Thank you a lot.

> import "x-alice:/lib/system/UnsafeComponent"
> val c = Component.fromPackage(pack (val it = 666) : (val it : int))
> val s = UnsafeComponent.pack_ c
> val c' = UnsafeComponent.unpack_ s
> val p = ComponentManager.eval(Url.fromString "", c')
> open unpack p : (val it : int)

The interface to sqlite though does not accept s as a string. For example I
tried on a table

tpackages (id int, seamstring text)

with above's s

val updateTable = "update tpackages set seamstring='"^
s ^
"' where id=1"
val ct : unit list   = await (SQLite.query(dbase,updateTable))

and received the error:

Uncaught exception
SQLError "unrecognized token: \"'seam\^E\""

I also tried with a blob column, but realised that I wouldn't really know how
to feed, say, a package to the insert-into-a-blob-column statement.

```