Start of topic | Skip to actions

COMP 617S07: (2007_02_02)

Scribe: Angela Yun Zhu Date: 2007-02-02

Implementation of "at_size"

We are trying to write a "at_size" function which takes an unsized list and an integer n as input and returns a sized list with length n as an output. The n elements in the output should be the first n elements in the input list. If n greater than the length of the unsized list, we'd like to return a "None" as said in last class.

The type of "at_size" should be like:

type 'a option = 
     | None
      | Some of 'a ;;

at_size: \forall n:nat  (n snat) -> list  -> (n slist) option

it seems that we can do it, but it is not so easy to construct (n: snat) Here are some tricks we might be able to make:

  • We can use the function in this way (although it is not expected like this), as

 at_size .|S|
 at_size .|5| -> Succ .|4| (Succ .|3| (Succ .|2| (Succ ... (.|0| Zero))))  [1,2,3,4,5,6,7]
This way it should work, and should give us:
 Some [1,2,3,4,5]
  • We can write another function that will make a snat from an integer:

 mk_snat: int -> E n. (n snat)

And then take this function into context of

f(x) = let a = mk_snat x in match a in (nslist option)     
   with E .|n| b  -> at_size .|n| b l
This way it might not work, we are interested in what "error message" it will give us. (Walid: Emir, please send the code and result to Angela)

We expect it doesn't work because this function is not well typed. i.e. This function doesn't have a type.

The problem here is very tricky, since the "at_size" will requir a context. If we don't have a proper context for it, it cannot work.

  • One thing need attention is that in concoqtion, when doing pattern matching, you must use "match ... in ..." , otherwise it will do the OCaml match.

To get the function work, we can try:

f(x) = let a = mk_snat x in match a in (nslist option)     
   with E .|n| b  -> (E' .|n| at_size  .|n| b l)

and the type of this function should be

type: f: int -> list -> E n. (n slist)option

Notice that, here E and E' are different existential tag.

  • There might be sevaral way to implement "at_size". You can either first get a unsized list with length n, then "size_it"; or first get the list "sized", then take the first n elements from it.

Typed and UnTyped? Environments

Lists can exist in two worlds, typed one or untyped one. The latter one has and always carry less information about type, and you can do all kinds of manipulation on untyped list freely. Once they go over the boundary between the typed world and untyped world, things get more constrained. As far as we are not too restricted by the types, we can take advantage of it and use it to help us to preserve some desired properties, and guide us to do what we really want to do

 Can you write a function that has type: \forall n, m. m list -> n list ?

Forall and Exsit

Shorthand:
/\   denotes  \forall
-|   denotes  \exist

In the following type declaration:

type e = E of let n in n slist

The "E" has type:

E: /\ a. n slist -> e

And the type e equivalent to:

-| n. n slist

  • More explanation
To explain the type of "E" above, remember that if an optional type only has one case, the type of the whole expression is just the same as what after "E of". Look at one example, suppose:

 
type a = A of t
We can infer from it and get:
(s -> a)  == (s -> t)

Another example:

type a = A of int
gives:
(s -> a) == (s -> int)

Thus, it is now clear that: (I don't know how to print \alpha here, so I use "p" instead of "alpha" which is used in class, and "r" instead of "beta" .)

type a =A of let p in t(p): p
gives:
(s -> a)  == (s -> -| p . t(p))
Which is equivalent to:
(/\ q . (s-> a )) == s -> t(p))

Furthermore, if we have:

type (r) a = A of let p in t(p): (p) a       (*)
then:
A: /\ p. t(p) -> (p) a
and
(/\ p. (s -> (p) a)) == (s -> t(p))

  • Why in (*) on the left hand side of the equation it is "type (r) a" but not "type (p) a"?
  • This means that the type constructor can take any type (expression) to construct a new type.

-- YunZhu - 02 Feb 2007

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution 2.5 License. Please follow our citation guidelines.