|
| |||||
|
|
Start of topic | Skip to actions
COMP 617S07: (2007_02_09)
Question (Copy from Cherif's Homework)While trying to define the insert function on braun trees, Cherif encountered the following issue. Here is the definition for the braun tree:
type ('n:'(nat),'a) dbtree =
|DLeaf :('(0),'a) dbtree
|DParent1 of let 'n:'(nat) in 'a * ('(n),'a) dbtree *('(n),'a) dbtree : ('(S(n+n)),'a) dbtree
|DParent2 of let 'n:'(nat) in 'a * ('(S n),'a) dbtree *('(n),'a) dbtree: ('(S(S (n+n))),'a) dbtree;;
and Here is the definition for the insert function:
let rec insert_dbt .|n:'(nat)| (x:'a) (dbt:('(n),'a) dbtree): ('(S n),'a)
dbtree =
match dbt in ('(S n),'a) dbtree with
|DLeaf -> DParent1 .|'(0)| (x,DLeaf,DLeaf)
|DParent1 .|m:'(nat)| (r,dbt1,dbt2) -> DParent2 .|'(m)| (r, (insert_dbt .|'(m)| x dbt1), dbt2)
|DParent2 .|m:'(nat)| (r,dbt1,dbt2) -> DParent1 .|'(S m)| (r, dbt1, (insert_dbt .|'(m)| x dbt2));;
The following error message was then returned:
File "bt.ml", line 42, characters 41-98:
Type [(m : nat) (n := S (S (m + m)) : nat)] |-
('(S n), 'e1) dbtree was expected but
('(S (S m + S m)), 'e1) dbtree was inferred for the branch expression.
The solution to this would be to establish that (S m + S m) is the same as
S(S(m+m)). This can be done by adding a lemma. Another possible solution would be to try to write things in a way that avoid the need for that lemma.
DML Definition of Braun Trees
datatype 'a brauntree with nat =
L(0)
| {m:nat, n:nat | n <= m <= n+1}
B(m+n+1) of 'a * 'a brauntree(m) * 'a brauntree(n)
fun('a)
diff (k, L) = 0
| diff (k, B(_, l, r)) =
if k = 0 then 1
else if k % 2 = 1 then diff (k/2, l) else diff (k/2 - 1, r)
withtype {k:nat, n:nat | k <= n <= k+1} <n> => int(k) * 'a brauntree(n) -> int(n-k)
fun('a)
size (L) = 0
| size (B(_, l, r)) =
let val n = size r in 1 + n + n + diff (n, l) end
withtype {n:nat} <n> => 'a brauntree(n) -> int(n)
Function to Implement on Braun TreesIn addition to the insert and delete, we can will look at map and fold function. Both map and fold can be applied to any data structures. Erik Meijer has an interesting paper about the generalization of folds also known as catamorphisms to any data structure. Erik Meijer, Maarten M. Fokkinga, Ross Paterson: Functional Programming with Bananas, Lenses, Envelopes and Barbed Wire. FPCA 1991: 124-144Fold FunctionList Folda list = Nil | Cons(a,a list) map: (a --> b) --> a list --> b list fold: r --> (a --> r --> r) --> a list --> rwhere the first r is the type of the initial value i and (a --> r --> r) is the step function.
fold i step list =
match l with
| Nil -> i
| Cons(a,xs) -> step a (fold i step xs)
ExamplesExample 1: i = 0 step = lambda a. lambda r. a+r this fold is: add list Example 2: i = 1 step = lambda a. lambda r. a*r this fold is: multiply list Example 3: i = Nil step = lambda a. lambda r. Cons(a,r) this fold is: list id Example 4: i = Nil step = lambda a. lambda r. Cons(f a,r) this fold is: map f Example 5: i = lambda acc.acc step = lambda a. lambda r. lambda acc. Cons(a,(r acc)) (Option A) step = lambda a. lambda r. lambda acc. r (Cons(a,acc)) (Option B) this fold is: reverse listIn example 5 we wanted to reverse a list using fold. After type analysis, we note that we might have 2 options for the step part (Option A and B). By trying them out it turns out that option B is the correct one. If we have start with the list [1;2] applying the fold of example 5 (using option B) we get the following step 1 (step 2 lambda x.x) step 1 (lambda acc. lambda x.x (Cons(2,acc)) step 1 (lambda acc. (Cons(2,acc)) lambda acc. (lambda acc. (Cons(2,acc)) (Cons(1,acc))) lambda acc. (Cons(2,Cons(1,acc))) Homework
Topic Actions: Edit | Attach | Printable | Raw View | Backlinks: Web, All Webs | History: r3 < r2 < r1 | More topic actions
Webs: Main | TWiki | Africa | 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.