Start of topic | Skip to actions

Updating OCaml for Concoqtion

Right now Concoqtion uses OCaml version 3.08, but we may want to update it to 3.09? 3.08 was originally chosen because 3.09 wasn't released back then, but now that it is, we want someone to work out the differences and update the OCaml underlying Concoqtion. Emir is going to be busy so he probably won't be able to do it. Anyone?

Typing copy2

Joseph brought up a point about typing copy2. The paper gives three ways to make trees of size n (all values same):

  • naive algorithm (just allocate the nodes n times) - O(n)
  • reuse subtree (For each branch, reuse one of the subtrees for the other subtree) - O((log (n)) ^ 2)
  • create two subtrees, reuse them (a la fibonacci). This is called copy2. - O(log(n))

copy2 works by maintaining a pair of trees, sizes m and m+1, and (roughly) doubling both trees on each step. This doubling is efficient because we can reuse the two trees as much as possible.

There's a problem with sizing copy2, because we need to do an induction where the induction step divides the natural number we're inducting on, instead of the usual method of subtracting 1.

One way to type this is to use a mod-2 representation of snat's:

type m X =
| Z : '(0) X
| S1 of let m:'(nat) in m X : (1 + 2*m) X
| S2 of let m:'(nat) in m X : (2 + 2*m) X

With this representation, n can be represented by a type that is lg(n) deep. There's also a bijection to the natural numbers (it is isomorphic in that sense), so any natural number can be represented by this type. This mirrors the type of copy2, split into cases according to the numerical parameter:

copy2 x 0
copy2 x (2 * m + 1)
copy2 x (2 * m + 2)

This is an example of the fact that whenever you have a well-founded recursion, you can type it to make it a structural recursion on the structure that represents the type.

That's the (type-) theoretical side of the story. Practically, we need to write an X_of_int. This is O(log n) so it's still efficient but involves much more scaffolding than the untyped version.

Algorithms like copy2 have indexed types implicit in them (when we casually analyze copy2, we naturally think of indexed types) so it's natural to type it that way and be sure of its correctness.

We can also use existentials to type copy2. This gives weaker typing, but is worth a try. We need an existential of pairs and not a pair of existentials, to encode a mutual constraint on the pair's elements. Thus

type 'a exists_tree =
| E of let m:'(nat) in '(m) tree
;;

wouldn't work because when we open up the existential in a match statement, we are forced to write like

match blah, blah with
 | E |i:'(nat)| _, E |j:'(nat)| _ -> ...

and we can't express the fact that i = j+1. Instead, we need

type 'a exists_subtrees =
| E of let m:'(nat) in ('(1+m) tree, '(m) tree)
;;

Misc

We should push through all the examples to see if we stumble on anything interesting.

HW for next time do delete (assume all values are unique).


End of topic
Skip to actions | Back to top
Creative Commons LicenseThis work is licensed under a Creative Commons Attribution 2.5 License. Please follow our citation guidelines.