File: lambda_example.ml

package info (click to toggle)
easy-format 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 388 kB
  • sloc: ml: 1,065; makefile: 110
file content (122 lines) | stat: -rw-r--r-- 2,534 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
type lambda =
    Lambda of string * lambda
  | Var of string
  | Apply of lambda * lambda


(* We make the page more narrow for illustration purposes *)
let _ = Format.set_margin 20



(* \n f x. n (\g h. h (g f)) (\u. x) (\u. u) *)
(* \n. \f. \x. n (\g. \h. h (g f)) (\u. x) (\u. u) *)

let sample_data =
  Lambda (
    "n",
    (Lambda (
       "f",
       (Lambda (
	  "x",
	  Apply (
	    Apply ( 
	      Apply (
		Var "n", 
		(Lambda (
		   "g", 
		   (Lambda (
		      "h", 
		      Apply (Var "h", Apply (Var "g", Var "f"))
		    )
		   )
		 )
		)
	      ),
	      (Lambda ("u", Var "x"))
	    ),
	    (Lambda ("u", Var "u"))
	  )
	)
       )
     )
    )
  )
    

(****************************************************************************)
(* Example from http://caml.inria.fr/resources/doc/guides/format.html
   using Format directly. *)

open Format

let ident = pp_print_string;;
let kwd = pp_print_string;;

let rec pr_exp0 ppf = function
  | Var s ->  ident ppf s
  | lam -> fprintf ppf "@[<1>(%a)@]" pr_lambda lam

and pr_app ppf = function
  | e ->  fprintf ppf "@[<2>%a@]" pr_other_applications e

and pr_other_applications ppf f =
  match f with
  | Apply (f, arg) -> fprintf ppf "%a@ %a" pr_app f pr_exp0 arg
  | f -> pr_exp0 ppf f

and pr_lambda ppf = function
 | Lambda (s, lam) ->
     fprintf ppf "@[<1>%a%a%a@ %a@]" kwd "\\" ident s kwd "." pr_lambda lam
 | e -> pr_app ppf e;;

let print_lambda x =
  pr_lambda std_formatter x; 
  pp_print_flush std_formatter ()


let _ = 
  print_endline 
    "Example from \
     http://caml.inria.fr/resources/doc/guides/format.html#example";
  print_lambda sample_data;
  print_newline ()



(***************************************************************************)
(* Same example, using Easy_format *)

open Printf
open Easy_format


let p1 = { label with indent_after_label = 1 }
let p2 = { label with indent_after_label = 2 }

let paren_style = 
  { list with
      space_after_opening = false;
      space_before_closing = false;
      align_closing = false
  }

let rec exp0_node = function
    Var s -> Atom (s, atom)
  | lam -> List (("(", "", ")", paren_style), [lambda_node lam])

and app_node = function
    Apply (f, arg) -> Label ((app_node f, p2), exp0_node arg)
  | f -> exp0_node f
      
and lambda_node = function
    Lambda (s, lam) ->
      Label ((Atom (sprintf "\\%s." s, atom), p1), lambda_node lam)
  | e -> app_node e


let _ =
  print_endline "Same, using Easy_format:";
  Pretty.to_stdout (lambda_node sample_data);
  print_newline ()