summary |
shortlog |
log |
commit | commitdiff |
tree
raw |
patch |
inline | side by side (from parent 1:
4d77188)
Signed-off-by: Jim Pryor <profjim@jimpryor.net>
match test with
| {height = h; weight = w; char_tester = test} ->
match test with
| {height = h; weight = w; char_tester = test} ->
- (* go on to use h, w, and test ... *)
+ (* go on to use h, w, and test ... *)
Anyway, using record types, we might define the tree zipper interface like so:
Anyway, using record types, we might define the tree zipper interface like so:
Using these fringe enumerators, we can write our `same_fringe` function like this:
let same_fringe (t1 : 'a tree) (t2 : 'a tree) : bool =
Using these fringe enumerators, we can write our `same_fringe` function like this:
let same_fringe (t1 : 'a tree) (t2 : 'a tree) : bool =
- let next1 = make_fringe_enumerator t1
- in let next2 = make_fringe_enumerator t2
- in let rec loop () : bool =
- match next1 (), next2 () with
- | Some a, Some b when a = b -> loop ()
- | None, None -> true
- | _ -> false
- in loop ()
- ;;
+ let next1 = make_fringe_enumerator t1
+ in let next2 = make_fringe_enumerator t2
+ in let rec loop () : bool =
+ match next1 (), next2 () with
+ | Some a, Some b when a = b -> loop ()
+ | None, None -> true
+ | _ -> false
+ in loop ()
+ ;;
The auxiliary `loop` function will keep calling itself recursively until a difference in the fringes has manifested itself---either because one fringe is exhausted before the other, or because the next leaves in the two fringes have different labels. If we get to the end of both fringes at the same time (`next1 (), next2 ()` matches the pattern `None, None`) then we've established that the trees do have the same fringe.
The auxiliary `loop` function will keep calling itself recursively until a difference in the fringes has manifested itself---either because one fringe is exhausted before the other, or because the next leaves in the two fringes have different labels. If we get to the end of both fringes at the same time (`next1 (), next2 ()` matches the pattern `None, None`) then we've established that the trees do have the same fringe.
With cooperative threads, one typically yields control to the thread, and then back again to the main program, multiple times. Here's the pattern in which that happens in our `same_fringe` function:
With cooperative threads, one typically yields control to the thread, and then back again to the main program, multiple times. Here's the pattern in which that happens in our `same_fringe` function:
- main program next1 thread next2 thread
- ------------ ------------ ------------
+ main program next1 thread next2 thread
+ ------------ ------------ ------------
- (paused) starting
- (paused) calculate first leaf
- (paused) <--- return it
- start next2 (paused) starting
- (paused) (paused) calculate first leaf
- (paused) (paused) <-- return it
- compare leaves (paused) (paused)
- call loop again (paused) (paused)
- call next1 again (paused) (paused)
- (paused) calculate next leaf (paused)
- (paused) <-- return it (paused)
+ (paused) starting
+ (paused) calculate first leaf
+ (paused) <--- return it
+ start next2 (paused) starting
+ (paused) (paused) calculate first leaf
+ (paused) (paused) <-- return it
+ compare leaves (paused) (paused)
+ call loop again (paused) (paused)
+ call next1 again (paused) (paused)
+ (paused) calculate next leaf (paused)
+ (paused) <-- return it (paused)
... and so on ...
If you want to read more about these kinds of threads, here are some links:
... and so on ...
If you want to read more about these kinds of threads, here are some links:
# let lst = [1; 2] in
"a" :: lst;;
Error: This expression has type int list
# let lst = [1; 2] in
"a" :: lst;;
Error: This expression has type int list
- but an expression was expected of type string list
+ but an expression was expected of type string list
But you may also have encountered other kinds of error, that arise while your program is running. For example:
But you may also have encountered other kinds of error, that arise while your program is running. For example:
let nth l n =
if n < 0 then invalid_arg "List.nth" else
let rec nth_aux l n =
let nth l n =
if n < 0 then invalid_arg "List.nth" else
let rec nth_aux l n =
- match l with
- | [] -> failwith "nth"
- | a::l -> if n = 0 then a else nth_aux l (n-1)
+ match l with
+ | [] -> failwith "nth"
+ | a::l -> if n = 0 then a else nth_aux l (n-1)
in nth_aux l n
Notice the two clauses `invalid_arg "List.nth"` and `failwith "nth"`. These are two helper functions which are shorthand for:
in nth_aux l n
Notice the two clauses `invalid_arg "List.nth"` and `failwith "nth"`. These are two helper functions which are shorthand for:
(if x = 1 then 10
else if x = 2 then raise (Failure "two")
else raise (Failure "three")
(if x = 1 then 10
else if x = 2 then raise (Failure "two")
else raise (Failure "three")
with Failure "two" -> 20
;;
val foo : int -> int = <fun>
with Failure "two" -> 20
;;
val foo : int -> int = <fun>
# try
try
(raise (Failure "blah")
# try
try
(raise (Failure "blah")
with Failure "fooey" -> 10
with Failure "blah" -> 20;;
- : int = 20
with Failure "fooey" -> 10
with Failure "blah" -> 20;;
- : int = 20
with Failure "blah" -> 20
in let bar x =
raise (Failure "blah")
with Failure "blah" -> 20
in let bar x =
raise (Failure "blah")
try begin
(if x = 1 then 10
else abort 20
try begin
(if x = 1 then 10
else abort 20
try begin
(if x = 1 then 10
else abort 20
try begin
(if x = 1 then 10
else abort 20
and we can think of the code starting with `let foo_result = ...` as a function, with the box being its parameter, like this:
fun box ->
and we can think of the code starting with `let foo_result = ...` as a function, with the box being its parameter, like this:
fun box ->
- let foo_result = box
- in (foo_result) + 1000
+ let foo_result = box
+ in (foo_result) + 1000
That function is our "snapshot". Normally what happens is that code *inside* the box delivers up a value, and that value gets supplied as an argument to the snapshot-function just described. That is, our code is essentially working like this:
let x = 2
in let snapshot = fun box ->
That function is our "snapshot". Normally what happens is that code *inside* the box delivers up a value, and that value gets supplied as an argument to the snapshot-function just described. That is, our code is essentially working like this:
let x = 2
in let snapshot = fun box ->
- let foo_result = box
- in (foo_result) + 1000
+ let foo_result = box
+ in (foo_result) + 1000
- (if x = 1 then 10
- else ... (* we'll come back to this part *)
- ) + 100
+ (if x = 1 then 10
+ else ... (* we'll come back to this part *)
+ ) + 100
in shapshot value;;
But now how should the `abort 20` part, that we ellided here, work? What should happen when we try to evaluate that?
in shapshot value;;
But now how should the `abort 20` part, that we ellided here, work? What should happen when we try to evaluate that?
let x = 2
in let snapshot = fun box ->
let x = 2
in let snapshot = fun box ->
- let foo_result = box
- in (foo_result) + 1000
+ let foo_result = box
+ in (foo_result) + 1000
- (if x = 1 then 10
- else snapshot 20
- ) + 100
+ (if x = 1 then 10
+ else snapshot 20
+ ) + 100
in shapshot value;;
Except that isn't quite right, yet---in this fragment, after the `snapshot 20` code is finished, we'd pick up again inside `let value = (...) + 100 in snapshot value`. That's not what we want. We don't want to pick up again there. We want instead to do this:
let x = 2
in let snapshot = fun box ->
in shapshot value;;
Except that isn't quite right, yet---in this fragment, after the `snapshot 20` code is finished, we'd pick up again inside `let value = (...) + 100 in snapshot value`. That's not what we want. We don't want to pick up again there. We want instead to do this:
let x = 2
in let snapshot = fun box ->
- let foo_result = box
- in (foo_result) + 1000
+ let foo_result = box
+ in (foo_result) + 1000
- (if x = 1 then 10
- else snapshot 20 THEN STOP
- ) + 100
+ (if x = 1 then 10
+ else snapshot 20 THEN STOP
+ ) + 100
in shapshot value;;
We can get that by some further rearranging of the code:
let x = 2
in let snapshot = fun box ->
in shapshot value;;
We can get that by some further rearranging of the code:
let x = 2
in let snapshot = fun box ->
- let foo_result = box
- in (foo_result) + 1000
+ let foo_result = box
+ in (foo_result) + 1000
in let continue_normally = fun from_value ->
in let continue_normally = fun from_value ->
- let value = from_value + 100
- in snapshot value
- in
- if x = 1 then continue_normally 10
- else snapshot 20;;
+ let value = from_value + 100
+ in snapshot value
+ in
+ if x = 1 then continue_normally 10
+ else snapshot 20;;
And this is indeed what is happening, at a fundamental level, when you use an expression like `abort 20`.
And this is indeed what is happening, at a fundamental level, when you use an expression like `abort 20`.
# open Delimcc;;
# let reset body = let p = new_prompt () in push_prompt p (body p);;
# let test_cps x =
# open Delimcc;;
# let reset body = let p = new_prompt () in push_prompt p (body p);;
# let test_cps x =
- let snapshot = fun box ->
- let foo_result = box
- in (foo_result) + 1000
- in let continue_normally = fun from_value ->
- let value = from_value + 100
- in snapshot value
- in if x = 1 then continue_normally 10
- else snapshot 20;;
+ let snapshot = fun box ->
+ let foo_result = box
+ in (foo_result) + 1000
+ in let continue_normally = fun from_value ->
+ let value = from_value + 100
+ in snapshot value
+ in if x = 1 then continue_normally 10
+ else snapshot 20;;
let foo x =
+===try begin================+
let foo x =
+===try begin================+
in (foo 2) + 1000;;
# let test_shift x =
in (foo 2) + 1000;;
# let test_shift x =
- let foo x = reset(fun p () ->
- (shift p (fun k ->
- if x = 1 then k 10 else 20)
- ) + 100)
- in foo z + 1000;;
+ let foo x = reset(fun p () ->
+ (shift p (fun k ->
+ if x = 1 then k 10 else 20)
+ ) + 100)
+ in foo z + 1000;;
# test_cps 1;;
- : int = 1110
# test_cps 1;;
- : int = 1110
try begin
(if x = 1 then 10
else abort 20
try begin
(if x = 1 then 10
else abort 20
let x = 2
in let snapshot = fun box ->
let x = 2
in let snapshot = fun box ->
- let foo_result = box
- in (foo_result) + 1000
+ let foo_result = box
+ in (foo_result) + 1000
in let continue_normally = fun from_value ->
in let continue_normally = fun from_value ->
- let value = from_value + 100
- in snapshot value
- in
- if x = 1 then continue_normally 10
- else snapshot 20;;
+ let value = from_value + 100
+ in snapshot value
+ in
+ if x = 1 then continue_normally 10
+ else snapshot 20;;
Code written in the latter form is said to be written in **explicit continuation-passing style** or CPS. Later we'll talk about algorithms that mechanically convert an entire program into CPS.
Code written in the latter form is said to be written in **explicit continuation-passing style** or CPS. Later we'll talk about algorithms that mechanically convert an entire program into CPS.