|
| |||||
|
|
Start of topic | Skip to actions
COMP 617S07: (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:
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]
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 lThis 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.
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)optionNotice that, here E and E' are different existential tag.
Typed and UnTyped? EnvironmentsLists 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 doCan you write a function that has type: \forall n, m. m list -> n list ? Forall and ExsitShorthand:/\ denotes \forall -| denotes \existIn the following type declaration: type e = E of let n in n slistThe "E" has type: E: /\ a. n slist -> eAnd the type e equivalent to: -| n. n slist
type a = A of tWe can infer from it and get: (s -> a) == (s -> t)Another example: type a = A of intgives: (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): pgives: (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) aand (/\ p. (s -> (p) a)) == (s -> t(p))
Topic Actions: Edit | Attach | Printable | Raw View | Backlinks: Web, All Webs | History: r3 < r2 < r1 | More topic actions
Webs: Main | TWiki | Africa | CPSX | EmbeddedSystems | Gpce | Houston | International | K12 | MetaOCaml | MulticoreOCR | ProgrammingLanguages | RAP | RIDL | Sandbox | SpeechClub | Teaching | Texbot | WG211 Web Actions: | ||||
This work is licensed under a Creative Commons Attribution 2.5 License. Please follow our citation guidelines.