caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* functions' recursive construction
@ 2007-05-22 23:17 Damien Lefortier
  2007-05-22 23:21 ` [Caml-list] " Jon Harrop
  2008-07-07 18:01 ` Fabrice Marchant
  0 siblings, 2 replies; 14+ messages in thread
From: Damien Lefortier @ 2007-05-22 23:17 UTC (permalink / raw)
  To: caml-list

Hi,

I try to do a function f which takes one integer argument and returns
a function g which returns its nth arguments.

For example f 3 gives g with let g = fun x -> fun y -> fun z -> z ;;

I tried to do that kind of f function.

let rec f = function
    1 -> fun x -> x
  | n -> fun _ -> f (n-1)
;;

But it does not work, any idea ?

-- 
Damien `Dam' Lefortier
The most important thing in the programming
language is the name      -- D. Knuth


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [Caml-list] functions' recursive construction
  2007-05-22 23:17 functions' recursive construction Damien Lefortier
@ 2007-05-22 23:21 ` Jon Harrop
  2007-05-22 23:31   ` Lukasz Stafiniak
  2008-07-07 18:01 ` Fabrice Marchant
  1 sibling, 1 reply; 14+ messages in thread
From: Jon Harrop @ 2007-05-22 23:21 UTC (permalink / raw)
  To: caml-list

On Wednesday 23 May 2007 00:17:08 Damien Lefortier wrote:
> But it does not work, any idea ?

What would the type of "f" be?

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
The F#.NET Journal
http://www.ffconsultancy.com/products/fsharp_journal/?e


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [Caml-list] functions' recursive construction
  2007-05-22 23:21 ` [Caml-list] " Jon Harrop
@ 2007-05-22 23:31   ` Lukasz Stafiniak
  2007-05-22 23:32     ` Lukasz Stafiniak
  2007-05-23  6:25     ` Jean-Christophe Filliatre
  0 siblings, 2 replies; 14+ messages in thread
From: Lukasz Stafiniak @ 2007-05-22 23:31 UTC (permalink / raw)
  To: Jon Harrop; +Cc: caml-list

On 5/23/07, Jon Harrop <jon@ffconsultancy.com> wrote:
> On Wednesday 23 May 2007 00:17:08 Damien Lefortier wrote:
> > But it does not work, any idea ?
>
> What would the type of "f" be?
>
In OCaml,

 'a -> 'a as 'a

;-)

Would this function (or some approximation) be possible in Coq?


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [Caml-list] functions' recursive construction
  2007-05-22 23:31   ` Lukasz Stafiniak
@ 2007-05-22 23:32     ` Lukasz Stafiniak
  2007-05-22 23:41       ` Jon Harrop
  2007-05-23  6:25     ` Jean-Christophe Filliatre
  1 sibling, 1 reply; 14+ messages in thread
From: Lukasz Stafiniak @ 2007-05-22 23:32 UTC (permalink / raw)
  To: Jon Harrop; +Cc: caml-list

On 5/23/07, Lukasz Stafiniak <lukstafi@gmail.com> wrote:
> On 5/23/07, Jon Harrop <jon@ffconsultancy.com> wrote:
> > What would the type of "f" be?
> >
>  'a -> 'a as 'a
>
I'm sorry, I meant:
#     val f : int -> ('a -> 'a as 'a) = <fun>


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [Caml-list] functions' recursive construction
  2007-05-22 23:32     ` Lukasz Stafiniak
@ 2007-05-22 23:41       ` Jon Harrop
  0 siblings, 0 replies; 14+ messages in thread
From: Jon Harrop @ 2007-05-22 23:41 UTC (permalink / raw)
  To: caml-list

On Wednesday 23 May 2007 00:32:58 you wrote:
> On 5/23/07, Lukasz Stafiniak <lukstafi@gmail.com> wrote:
> > On 5/23/07, Jon Harrop <jon@ffconsultancy.com> wrote:
> > > What would the type of "f" be?
> >
> >  'a -> 'a as 'a
>
> I'm sorry, I meant:
> #     val f : int -> ('a -> 'a as 'a) = <fun>

Not this then:

# let rec f n x = if n=0 then x else f (n-1) (x x);;
val f : int -> ('a -> 'a as 'a) -> 'a = <fun>

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
The F#.NET Journal
http://www.ffconsultancy.com/products/fsharp_journal/?e


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [Caml-list] functions' recursive construction
  2007-05-22 23:31   ` Lukasz Stafiniak
  2007-05-22 23:32     ` Lukasz Stafiniak
@ 2007-05-23  6:25     ` Jean-Christophe Filliatre
  1 sibling, 0 replies; 14+ messages in thread
From: Jean-Christophe Filliatre @ 2007-05-23  6:25 UTC (permalink / raw)
  To: Lukasz Stafiniak; +Cc: Jon Harrop, caml-list


Damien Lefortier wrote:
 > I try to do a function f which takes one integer argument and returns
 > a function g which returns its nth arguments.
 > 
 > For example f 3 gives g with let g = fun x -> fun y -> fun z -> z ;;

Lukasz Stafiniak wrote:
 > Would this function (or some approximation) be possible in Coq?

Yes, and here it is (0-based instead of 1-based as above):

======================================================================
Fixpoint t (n:nat) : Type := match n with
  | O => forall (A:Set), A -> A
  | S p => forall (A:Set), A -> t p
end.

Fixpoint f  (n:nat) : t n := 
  match n return t n with
  | O => (fun (A:Set) (x:A) => x)
  | S p => (fun (A:Set) (x:A) => f p)
end.

Eval compute in f 2.
= fun (A : Set) (_ : A) (A0 : Set) (_ : A0) (A1 : Set) (x1 : A1) => x1
     : t 2
======================================================================


-- 
Jean-Christophe


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [Caml-list] functions' recursive construction
  2007-05-22 23:17 functions' recursive construction Damien Lefortier
  2007-05-22 23:21 ` [Caml-list] " Jon Harrop
@ 2008-07-07 18:01 ` Fabrice Marchant
       [not found]   ` <a58674cc0807071304x1640d670sde3045a0920a5922@mail.gmail.com>
                     ` (2 more replies)
  1 sibling, 3 replies; 14+ messages in thread
From: Fabrice Marchant @ 2008-07-07 18:01 UTC (permalink / raw)
  To: caml-list

On Wed, 23 May 2007 01:17:08 +0200
"Damien Lefortier" <damien.lefortier@gmail.com> wrote:

> Hi,
> 
> I try to do a function f which takes one integer argument and returns
> a function g which returns its nth arguments.
> 
> For example f 3 gives g with let g = fun x -> fun y -> fun z -> z ;;
> 
> I tried to do that kind of f function.
> 
> let rec f = function
>     1 -> fun x -> x
>   | n -> fun _ -> f (n-1)
> ;;
> 
> But it does not work, any idea ?

  Hi List,

  Sometimes, trying to learn a bit more about OCaml, I dig into old List topics.
But here, outside Coq answer, I'm not sure to understand the explanations about the original OCaml question.
  Please, is it possible to write an OCaml function that would behave the way Damien Lefortier wish ?
 (I think the answer is 'No') Could you shed light on this ?

Thanks,

Fabrice


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [Caml-list] functions' recursive construction
       [not found]   ` <a58674cc0807071304x1640d670sde3045a0920a5922@mail.gmail.com>
@ 2008-07-07 18:28     ` Fabrice Marchant
  2008-07-07 20:40       ` Jeremy Yallop
  0 siblings, 1 reply; 14+ messages in thread
From: Fabrice Marchant @ 2008-07-07 18:28 UTC (permalink / raw)
  To: caml-list

On Mon, 7 Jul 2008 15:04:17 -0500
"William Neumann" <wneumann@gmail.com> wrote:

> For a quick hint as to why you cannot write such a function, ask
> yourself what the type of f would be.
> 
> Consider just the two cases of the type of (f 1) and (f 2).

Thanks for your swift answer,

So it's 'No', as I guessed, because the types of your two cases are different.

After Coq, maybe could we program this in Lisp ?

Regards,

Fabrice


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [Caml-list] functions' recursive construction
       [not found]   ` <C3DD7276-23D5-41BE-A272-7579586A5559@gmail.com>
@ 2008-07-07 18:36     ` Fabrice Marchant
  0 siblings, 0 replies; 14+ messages in thread
From: Fabrice Marchant @ 2008-07-07 18:36 UTC (permalink / raw)
  To: caml-list

On Mon, 7 Jul 2008 22:18:45 +0200
Christian Lindig <lindig@gmail.com> wrote:

> 
> On Jul 7, 2008, at 8:01 PM, Fabrice Marchant wrote:
> > (I think the answer is 'No') Could you shed light on this ?
> 
> Farbrice,
> 
> I think you are right I but can't give a formal argument, only some  
> intuition: you can't provide a static type for the function you want.  
> The type of the function would depend on the integer value you only  
> know at runtime. Such value-dependent types are not supported by  
> OCaml's type system.

  Thanks a lot Christian for these clear explanations. So they confirm William's answer.

Regards,

Fabrice


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [Caml-list] functions' recursive construction
  2008-07-07 20:25   ` Jeremy Yallop
@ 2008-07-07 19:11     ` Fabrice Marchant
  2008-07-07 22:48       ` asmadeus77
  2008-07-08  9:24       ` Daniel Bünzli
  0 siblings, 2 replies; 14+ messages in thread
From: Fabrice Marchant @ 2008-07-07 19:11 UTC (permalink / raw)
  To: caml-list

Jeremy Yallop <jeremy.yallop@ed.ac.uk> wrote:

> Here's a rather simple way to do it by encoding all the mechanics in the
> integer argument rather than in "f".  Like Jean-Christophe Filliatre in 
> the original thread, I'll use a zero-based rather than a one-based encoding.
> 
>     let z v = v
>     let s n _ = n
>     let f n = n
> 
> Now
> 
>      f z 0
>   => 0
> 
> and
> 
>      f (s (s (s z))) 0 1 2 3
>   => 3
> 
> and so on.

  Thanks Jeremy,

That is really nice ! I discover this original way of thinking.
's' nesting level remains however frozen at runtime.

A cool thing is that parameters types can be freely mixed :
f (s (s z)) '0' "1" (fun x -> 2*x) 3

=> 6

Regards,

Fabrice


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [Caml-list] functions' recursive construction
  2008-07-07 18:01 ` Fabrice Marchant
       [not found]   ` <a58674cc0807071304x1640d670sde3045a0920a5922@mail.gmail.com>
       [not found]   ` <C3DD7276-23D5-41BE-A272-7579586A5559@gmail.com>
@ 2008-07-07 20:25   ` Jeremy Yallop
  2008-07-07 19:11     ` Fabrice Marchant
  2 siblings, 1 reply; 14+ messages in thread
From: Jeremy Yallop @ 2008-07-07 20:25 UTC (permalink / raw)
  To: Fabrice Marchant; +Cc: caml-list

Fabrice Marchant wrote:
> On Wed, 23 May 2007 01:17:08 +0200 "Damien Lefortier"
> <damien.lefortier@gmail.com> wrote:
> 
>> I try to do a function f which takes one integer argument and
>> returns a function g which returns its nth arguments.
>> 
>> For example f 3 gives g with let g = fun x -> fun y -> fun z -> z
>> ;;
>> 
>> I tried to do that kind of f function.
>> 
>> let rec f = function 1 -> fun x -> x | n -> fun _ -> f (n-1) ;;
>> 
>> But it does not work, any idea ?
> 
> Sometimes, trying to learn a bit more about OCaml, I dig into old
> List topics. But here, outside Coq answer, I'm not sure to understand
> the explanations about the original OCaml question. Please, is it
> possible to write an OCaml function that would behave the way Damien
> Lefortier wish ? (I think the answer is 'No') Could you shed light on
> this ?

Here's a rather simple way to do it by encoding all the mechanics in the
integer argument rather than in "f".  Like Jean-Christophe Filliatre in 
the original thread, I'll use a zero-based rather than a one-based encoding.

    let z v = v
    let s n _ = n
    let f n = n

Now

     f z 0
  => 0

and

     f (s (s (s z))) 0 1 2 3
  => 3

and so on.

Jeremy.


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [Caml-list] functions' recursive construction
  2008-07-07 18:28     ` Fabrice Marchant
@ 2008-07-07 20:40       ` Jeremy Yallop
  0 siblings, 0 replies; 14+ messages in thread
From: Jeremy Yallop @ 2008-07-07 20:40 UTC (permalink / raw)
  To: Fabrice Marchant; +Cc: caml-list

Fabrice Marchant wrote:
> After Coq, maybe could we program this in Lisp ?

Yes, although this is rather off-topic.  Here are two solutions in 
(rather rusty) Scheme, both zero-based again.  A curried version:

   (define (f n)
     (cond
      ((zero? n) (lambda (x) x))
      (#t        (lambda (_) (f (- n 1))))))

Now

   > ((f 0) 1)
   1
   > (((((f 3) 1) 2) 3) 4)
   4

An uncurried version:

   (define (f n)
     (lambda args (list-ref args n)))

Now

   > ((f 0) 1)
   1
   > ((f 3) 1 2 3 4)
   4

Jeremy.


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [Caml-list] functions' recursive construction
  2008-07-07 19:11     ` Fabrice Marchant
@ 2008-07-07 22:48       ` asmadeus77
  2008-07-08  9:24       ` Daniel Bünzli
  1 sibling, 0 replies; 14+ messages in thread
From: asmadeus77 @ 2008-07-07 22:48 UTC (permalink / raw)
  To: Fabrice Marchant; +Cc: caml-list

Hello,

I am not sure I remember what was said exactly, and I'm quite too lazy
to check, but I remember that someone gave a possible implementation
with Obj.magic ?
This also is about the same as the Printf functions which can take a
variable amount of arguments and need a bit of tinkering, and I
remember a function 'eat", taking an integer n, which would ignore the
n first arguments given to the function. Now, if you have this,
getting the first argument should be possible after looking at the
printf source :) (although I could not find it searching in my mails,
sorry)

Actually, I've just glanced at the source and there _is_ a "get_arg"
function, taking a single integer and returning an Obj.magic'ed item
which would most likely be the nth argument of a function - however I
can't separate the function from the rest of the code properly in just
a minute, but it definitly is a place to look at if you need such a
function... Although it might be quite dangerous. But as it was said,
the type is impossible to represent and magic is probably necessary
without typing multiple calls to functions manually; but Jeremy's
solution could be enough :)

Good luck,
Dominique Martinet


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [Caml-list] functions' recursive construction
  2008-07-07 19:11     ` Fabrice Marchant
  2008-07-07 22:48       ` asmadeus77
@ 2008-07-08  9:24       ` Daniel Bünzli
  1 sibling, 0 replies; 14+ messages in thread
From: Daniel Bünzli @ 2008-07-08  9:24 UTC (permalink / raw)
  To: Fabrice Marchant; +Cc: caml-list


Le 7 juil. 08 à 21:11, Fabrice Marchant a écrit :

>  That is really nice ! I discover this original way of thinking.

A good reference for these kind of tricks can be found here :

http://mlton.org/Fold

Best,

Daniel


^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2008-07-08  9:25 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-05-22 23:17 functions' recursive construction Damien Lefortier
2007-05-22 23:21 ` [Caml-list] " Jon Harrop
2007-05-22 23:31   ` Lukasz Stafiniak
2007-05-22 23:32     ` Lukasz Stafiniak
2007-05-22 23:41       ` Jon Harrop
2007-05-23  6:25     ` Jean-Christophe Filliatre
2008-07-07 18:01 ` Fabrice Marchant
     [not found]   ` <a58674cc0807071304x1640d670sde3045a0920a5922@mail.gmail.com>
2008-07-07 18:28     ` Fabrice Marchant
2008-07-07 20:40       ` Jeremy Yallop
     [not found]   ` <C3DD7276-23D5-41BE-A272-7579586A5559@gmail.com>
2008-07-07 18:36     ` Fabrice Marchant
2008-07-07 20:25   ` Jeremy Yallop
2008-07-07 19:11     ` Fabrice Marchant
2008-07-07 22:48       ` asmadeus77
2008-07-08  9:24       ` Daniel Bünzli

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).