coq-club AT inria.fr
Subject: The Coq mailing list
List archive
Re: [Coq-Club] How can you build a coinductive memoization table for recursive functions over binary trees?
Chronological Thread
- From: Joachim Breitner <mail AT joachim-breitner.de>
- To: coq-club AT inria.fr
- Subject: Re: [Coq-Club] How can you build a coinductive memoization table for recursive functions over binary trees?
- Date: Thu, 26 Apr 2018 11:57:09 -0400
- Authentication-results: mail2-smtp-roc.national.inria.fr; spf=None smtp.pra=mail AT joachim-breitner.de; spf=Neutral smtp.mailfrom=mail AT joachim-breitner.de; spf=None smtp.helo=postmaster AT hermes.serverama.de
- Ironport-phdr: 9a23:muAqbRQsHXYTpsB2G/aobeq+7dpsv+yvbD5Q0YIujvd0So/mwa6yYheN2/xhgRfzUJnB7Loc0qyK6/umATRIyK3CmUhKSIZLWR4BhJdetC0bK+nBN3fGKuX3ZTcxBsVIWQwt1Xi6NU9IBJS2PAWK8TW94jEIBxrwKxd+KPjrFY7OlcS30P2594HObwlSizexfb1/IA+qoQnNq8IbnZZsJqEtxxXTv3BGYf5WxWRmJVKSmxbz+MK994N9/ipTpvws6ddOXb31cKokQ7NYCi8mM30u683wqRbDVwqP6WACXWgQjxFFHhLK7BD+Xpf2ryv6qu9w0zSUMMHqUbw5Xymp4qF2QxHqlSgHLSY0/mLKhMJwkqxVrhCupxJjzIDTb46YL/V+cr/HcN4AX2dNQsRcWipcCY28dYsPCO8BMP5coYn6vVQBsRu+BQipBOjy1zJInGH53awm0+QnDw7GxhErEtULsHvOrdX1MLwfUeKyzKbS0TrDb/JW2TLk5IfTaBAuv/CMXa52ccXP00kjDR7KgUuJpIHjIjib2OMNs22B4OphU+Kik2wnqwZrrTezxscsi4zJipsOxVDe6yp5wZo1JdumR05he9KrDYVfuzmBN4tqWMwtX2Jotzg1y7IauZ60Zi4KyJs5yBLFd/OHdI2I7grsVOaQPTd4hG9ld6mlixmu9kigz/XwVtO13VpQsiVKiMHAtncC1hDJ8MeIVuFx/lqi2TuJygvd6flELFgpmafZKpMt2Lo9moANvUjeECL6glj6gaGYe0gi5+Om8f7oYq/8qZ+ZL4J0ih/xMqApmsGnAeQ3LBIOX22F9uSnzrHj5lP2QK9UjvIoiKnWqpXaKt4BqqGkHQBZyocj6xChADe6yNkUgHsKIVNfdB+DlYTlJUzCLf7iAfuijVmhni9nx/XcMb3gBpXNIGLDkLDkfbtl7k5T0gszzdRE6pJWDbEOOu78Wk/wtNzdFxM2KBa0zPjmCNVh0IMRR3iPDrWEP6zMqVOI/P4gI/GQZI8JvzbwM+Qq5/n3jXMghVAdebSp0oAMZXCjHvVmJl2ZbmD2jtcAF2cKpAs+Q/bwhF2MSz4AL0q1Cqk7/3QwDJ+sJYbFXIGkxrKbjwmhGZgDRGdPAVCNFD/CbYiFR/EBcmrGJ8ZglhQGUrGqS44kkBu0uQ7mzbd9aObZrH5L/an/3cR4srWA3So58iZ5WpzEgjO9Clpsl2ZNfAcYmaV2oEhz0FCGi/EqgftRHtxS4rZDSAo7KZjR1ap2BoKoA16TTpKyUF+jB+6eL3QpVNtrmo0MZE92G9SnyxrZ0iu2BbIP0bCGVsRtr/DsmkPpLsM48E7okaksi158G5lPOHeji7U57AXJGYPTmkCQ0aqnJ/wR
Hi,
nothing magical. If hs-to-coq manages to handle the syntax, it would
output the same or similar Coq and run into the same problems.
Cheers,
Joachim
Am Donnerstag, den 26.04.2018, 11:48 -0400 schrieb Li-yao Xia:
> What happens if you write Russel's memoOpX at the end in Haskell and run
> hs-to-coq on it?
>
> Li-yao
>
> On 04/26/2018 11:43 AM, Joachim Breitner wrote:
> > …but then again, this is not at all addressing the question on how to
> > memoize recursive functions, so I guess my response is just noise …
> > sorry
> >
> > Joachim
> >
> > Am Donnerstag, den 26.04.2018, 11:41 -0400 schrieb Joachim Breitner:
> > > Hi,
> > >
> > > you might enjoy looking at
> > > https://github.com/antalsz/hs-to-coq/tree/master/examples/coinduction
> > > where I implement such a memo table in Haskell, convert it to Coq using
> > > hs-to-coq (the generated file is not in the git repo, so I attached it
> > > to this mail), and then in MemoProofs.v show that it preserves the
> > > semantics of the memoized functions.
> > >
> > > The Time commands at the end of this function show that memoization
> > > actually works in Coq!
> > >
> > > Time Eval vm_compute in slowFib 25. (* Finished transaction in 2.248
> > > secs *)
> > > Time Eval vm_compute in slowFib 25. (* Finished transaction in 2.233
> > > secs *)
> > > Time Eval vm_compute in cachedFib 25. (* Finished transaction in 2.203
> > > secs *)
> > > Time Eval vm_compute in cachedFib 25. (* Finished transaction in 0.
> > > secs *)
> > > Time Eval vm_compute in cachedFib 26. (* Finished transaction in 3.579
> > > secs *)
> > > Time Eval vm_compute in cachedFib 26. (* Finished transaction in 0.
> > > secs *)
> > >
> > > Cheers,
> > > Joachim
> > >
> > > Am Mittwoch, den 25.04.2018, 12:11 -0700 schrieb
> > > roconnor AT theorem.ca:
> > > > I've posted this question on the cstheory stack exchange at
> > > > https://cstheory.stackexchange.com/questions/40631/how-can-you-build-a-coinductive-memoization-table-for-recursive-functions-over-b
> > > > but I'm hoping to reach a wider audience by also asking here on the
> > > > Coq Club
> > > > mailing list. I copy my question below.
> > > >
> > > > ----
> > > >
> > > > The StreamMemo library for Coq illustrates how to memoize a function
> > > > f : nat ->
> > > > A over the natural numbers. In particular when f (S n) = g (f n), the
> > > > imemo_make shares the computation of recursive calls.
> > > >
> > > > Suppose instead of natural numbers, we want to memoize recursive
> > > > functions over
> > > > binary trees:
> > > >
> > > > Inductive binTree : Set :=
> > > > > Leaf : binTree
> > > > > Branch : binTree -> binTree -> binTree.
> > > >
> > > > Suppose we have a function f : binTree -> A that is structurally
> > > > recursive,
> > > > meaning that there is a function g : A -> A -> A such that f (Branch
> > > > x y) = g
> > > > (f x) (f y). How do we build a similar memo table for f in Coq such
> > > > that the
> > > > recursive computations are shared?
> > > >
> > > > In Haskell, it is not too hard to build such a memo table (see
> > > > MemoTrie for
> > > > example) and tie-the-knot. Clearly such memo tables are productive.
> > > > How can we
> > > > arrange things to convince a dependently typed language to accept
> > > > such knot
> > > > tying is productive?
> > > >
> > > > ----
> > > >
> > > > Even building a memotrie data type for binary trees is difficult in
> > > > Coq. The
> > > > natural solution calls for using a Bushy Datatype (see
> > > > https://www.cs.ox.ac.uk/richard.bird/online/BirdMeertens98Nested.pdf)
> > > > which
> > > > doesn't pass Coq's positivity check (but does seem to pass Agda's
> > > > positivity
> > > > check; maybe Christine Paulin could even further relax Coq's
> > > > positivity check.
> > > > :) ). At the risk of prejudicing the reader, I have a gist of how
> > > > I've worked
> > > > around this to define a memotrie data type for binary trees in Coq at
> > > > https://gist.github.com/roconnor/52375c3a81c927c5a8234be858f9830b
> > > >
> > > > My above solution to building a memotrie is moderately contrived and
> > > > adhoc, so
> > > > I encourage those intrested in this problem to try a solution
> > > > themselves first
> > > > before reading my gist. You may come up with a better solution than
> > > > what I
> > > > have.
> > > >
> > > > The memotrie I build in my gist doesn't memoize recursive calls of a
> > > > structurally recursive function over binary trees. At the end I write
> > > > out the
> > > > type of solution one would write in Haskell to build an example
> > > > memotrie that
> > > > memoizes recurive calls. However, this definition fails that
> > > > corecursive guard
> > > > check (and rightly so I think).
> > > >
> > > > I don't know how to build a memotrie to memoize recursive calls in a
> > > > structurally recursive function. I'm hoping someone one the mailing
> > > > list might
> > > > know a solution.
> > > >
> > > > --
> > > > Russell O'Connor <http://r6.ca/>
> > > >
>
>
--
Joachim Breitner
mail AT joachim-breitner.de
http://www.joachim-breitner.de/
Attachment:
signature.asc
Description: This is a digitally signed message part
- [Coq-Club] How can you build a coinductive memoization table for recursive functions over binary trees?, roconnor, 04/25/2018
- Re: [Coq-Club] How can you build a coinductive memoization table for recursive functions over binary trees?, Joachim Breitner, 04/26/2018
- Re: [Coq-Club] How can you build a coinductive memoization table for recursive functions over binary trees?, Joachim Breitner, 04/26/2018
- Re: [Coq-Club] How can you build a coinductive memoization table for recursive functions over binary trees?, Li-yao Xia, 04/26/2018
- Re: [Coq-Club] How can you build a coinductive memoization table for recursive functions over binary trees?, Joachim Breitner, 04/26/2018
- Re: [Coq-Club] How can you build a coinductive memoization table for recursive functions over binary trees?, Li-yao Xia, 04/26/2018
- Re: [Coq-Club] How can you build a coinductive memoization table for recursive functions over binary trees?, Joachim Breitner, 04/26/2018
- Re: [Coq-Club] How can you build a coinductive memoization table for recursive functions over binary trees?, Joachim Breitner, 04/26/2018
Archive powered by MHonArc 2.6.18.