Commit 97144313 authored by Pietro Abate's avatar Pietro Abate
Browse files

[r2003-08-21 11:26:19 by cvscast] Adding example on pm compilation

Original author: cvscast
Date: 2003-08-21 11:26:19+00:00
parent c574873c
......@@ -383,6 +383,41 @@ pop [];;
pop [];;
pop [];;
pop [];;
";"pm_compil","(* This example demonstrates the efficient compilation of pattern
matching. *)
type A = <a>[ Int* ]
type B = <b>[ Char* ]
(* Imagine we want to compile the following function:
fun ([A+|B+] -> Bool) [A+] -> 0 | [B+] -> 1
For an arbitrary value, it is expensive to check whether it has
type [A+] or not. But if we know statically that it has type [A+|B+],
we just have to check the tag of the first element !
This is demonstrated by the following internal debugging feature.
The syntax is:
debug compile T P1 ... Pn
where T is the input type (static information about the matched
value) and P1,...,Pn are the patterns to compile (simultaneously).
The \"debug compile\" instruction displays an human-readable
representation of the automaton corresponding to the pattern
matching. Note: in actual evaluation, this automaton is build
lazily (= on-the-fly, = JIT).
*)
debug compile [A+|B+] [A+] [B+]
(* You can see on the output that the pattern matching is actually
compiled as:
fun ([A+|B+] -> Int) [ <a>_ ; _ ] -> 0 | _ -> 1
*)
"; ]
let present = "<ul><li><a href=\"/cgi-bin/cduce?example=xml\">XML elements.</a>
XML elements.
......@@ -410,4 +445,7 @@ Syntactic sugar for projection.
How to perform XSLT-like transformations.
</li><li><a href=\"/cgi-bin/cduce?example=reference\">References.</a>
Mutable values.
</li><li><a href=\"/cgi-bin/cduce?example=pm_compil\">Compilation of pattern matching.</a>
This example demonstrates the efficient compilation of pattern
matching.
</li></ul>"
\ No newline at end of file
......@@ -243,6 +243,7 @@ EXTEND
[ op = [ LIDENT "flatten"
| LIDENT "load_xml"
| LIDENT "load_file" | LIDENT "load_file_utf8"
| LIDENT "getenv"
| LIDENT "load_html"
| LIDENT "print_xml" | LIDENT "print_xml_utf8"
| LIDENT "print"
......
......@@ -96,6 +96,9 @@ let exn_int_of =
Value.Atom (Atoms.mk_ascii "Invalid_argument"),
Value.string_latin1 "int_of"))
let exn_not_found =
Value.CDuceExn (Value.Atom (Atoms.mk_ascii "Not_found"))
let eval_load_file ~utf8 e =
Location.protect_op "load_file";
let ic = open_in (Value.get_string_latin1 e) in
......@@ -166,7 +169,16 @@ unary_op_cst "load_file_utf8"
unary_op_cst "load_file" string string_latin1
(eval_load_file ~utf8:false);;
unary_op_cst "getenv" string_latin1 string_latin1
(fun e ->
Location.protect_op "getenv";
let var = Value.get_string_latin1 e in
try Value.string_latin1 (Sys.getenv var)
with Not_found -> raise exn_not_found);;
Typer.register_unary_op "print_xml"
(fun tenv ->
......
......@@ -53,6 +53,11 @@ How to perform XSLT-like transformations.
Mutable values.
</example>
<example code="pm_compil" title="Compilation of pattern matching">
This example demonstrates the efficient compilation of pattern
matching.
</example>
</examples>
......
(* This example demonstrates the efficient compilation of pattern
matching. *)
type A = <a>[ Int* ]
type B = <b>[ Char* ]
(* Imagine we want to compile the following function:
fun ([A+|B+] -> Bool) [A+] -> 0 | [B+] -> 1
For an arbitrary value, it is expensive to check whether it has
type [A+] or not. But if we know statically that it has type [A+|B+],
we just have to check the tag of the first element !
This is demonstrated by the following internal debugging feature.
The syntax is:
debug compile T P1 ... Pn
where T is the input type (static information about the matched
value) and P1,...,Pn are the patterns to compile (simultaneously).
The "debug compile" instruction displays an human-readable
representation of the automaton corresponding to the pattern
matching. Note: in actual evaluation, this automaton is build
lazily (= on-the-fly, = JIT).
*)
debug compile [A+|B+] [A+] [B+]
(* You can see on the output that the pattern matching is actually
compiled as:
fun ([A+|B+] -> Int) [ <a>_ ; _ ] -> 0 | _ -> 1
*)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment