author | wenzelm |
Mon, 13 Aug 2007 00:23:43 +0200 | |
changeset 24237 | 4bf3e084d9b5 |
parent 24236 | d8b05073edc7 |
child 24246 | 3a915c75f7b6 |
permissions | -rw-r--r-- |
24236
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
1 |
(* Title: Pure/Syntax/simple_syntax.ML |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
2 |
ID: $Id$ |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
3 |
Author: Makarius |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
4 |
|
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
5 |
Simple syntax for types and terms --- for bootstrapping Pure. |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
6 |
*) |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
7 |
|
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
8 |
signature SIMPLE_SYNTAX = |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
9 |
sig |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
10 |
val read_typ: string -> typ |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
11 |
val read_term: string -> term |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
12 |
val read_prop: string -> term |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
13 |
end; |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
14 |
|
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
15 |
structure SimpleSyntax: SIMPLE_SYNTAX = |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
16 |
struct |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
17 |
|
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
18 |
(* scanning tokens *) |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
19 |
|
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
20 |
local |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
21 |
|
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
22 |
val eof = Lexicon.EndToken; |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
23 |
fun is_eof tk = tk = Lexicon.EndToken; |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
24 |
|
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
25 |
val stopper = (eof, is_eof); |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
26 |
val not_eof = not o is_eof; |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
27 |
|
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
28 |
val lexicon = Scan.make_lexicon |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
29 |
(map Symbol.explode ["!!", "%", "(", ")", ".", "::", "==", "==>", "=?=", "=>", "CONST"]); |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
30 |
|
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
31 |
in |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
32 |
|
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
33 |
fun read scan s = |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
34 |
(case |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
35 |
Symbol.explode s |> |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
36 |
Lexicon.tokenize lexicon false |> filter not_eof |> |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
37 |
Scan.read stopper scan of |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
38 |
SOME x => x |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
39 |
| NONE => error ("Bad input: " ^ quote s)); |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
40 |
|
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
41 |
end; |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
42 |
|
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
43 |
|
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
44 |
(* basic scanners *) |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
45 |
|
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
46 |
fun $$ s = Scan.some (fn Lexicon.Token s' => if s = s' then SOME s else NONE | _ => NONE); |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
47 |
fun enum1 s scan = scan -- Scan.repeat ($$ s |-- scan) >> op ::; |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
48 |
fun enum2 s scan = scan -- Scan.repeat1 ($$ s |-- scan) >> op ::; |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
49 |
|
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
50 |
val tfree = Scan.some (fn Lexicon.TFreeSy s => SOME s | _ => NONE); |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
51 |
val ident = Scan.some (fn Lexicon.IdentSy s => SOME s | _ => NONE); |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
52 |
val long_ident = Scan.some (fn Lexicon.LongIdentSy s => SOME s | _ => NONE); |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
53 |
val const = long_ident || ident; |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
54 |
|
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
55 |
|
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
56 |
(* types *) |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
57 |
|
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
58 |
(* |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
59 |
typ = typ1 => ... => typ1 |
24237 | 60 |
| typ1 |
24236
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
61 |
typ1 = typ2 const ... const |
24237 | 62 |
| typ2 |
63 |
typ2 = tfree |
|
64 |
| const |
|
65 |
| ( typ ) |
|
24236
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
66 |
*) |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
67 |
|
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
68 |
fun typ x = |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
69 |
(enum1 "=>" typ1 >> (op ---> o split_last)) x |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
70 |
and typ1 x = |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
71 |
(typ2 -- Scan.repeat const >> (fn (T, cs) => fold (fn c => fn U => Type (c, [U])) cs T)) x |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
72 |
and typ2 x = |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
73 |
(tfree >> (fn a => TFree (a, [])) || |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
74 |
const >> (fn c => Type (c, [])) || |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
75 |
$$ "(" |-- typ --| $$ ")") x; |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
76 |
|
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
77 |
val read_typ = read typ; |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
78 |
|
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
79 |
|
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
80 |
(* terms *) |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
81 |
|
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
82 |
(* |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
83 |
term = !!ident :: typ. term |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
84 |
| term1 |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
85 |
term1 = term2 ==> ... ==> term2 |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
86 |
| term2 |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
87 |
term2 = term3 == term2 |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
88 |
| term3 =?= term2 |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
89 |
| term3 |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
90 |
term3 = ident :: typ |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
91 |
| CONST const :: typ |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
92 |
| %ident :: typ. term3 |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
93 |
| term4 |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
94 |
term4 = term5 ... term5 |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
95 |
| term5 |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
96 |
term5 = ident |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
97 |
| CONST const |
24237 | 98 |
| ( term ) |
24236
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
99 |
*) |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
100 |
|
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
101 |
local |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
102 |
|
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
103 |
val constraint = $$ "::" |-- typ; |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
104 |
val var = ident -- constraint; |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
105 |
val bind = var --| $$ "."; |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
106 |
|
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
107 |
in |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
108 |
|
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
109 |
fun term env T x = |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
110 |
($$ "!!" |-- bind :|-- (fn v => |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
111 |
term (v :: env) propT >> (fn B => Term.all (#2 v) $ lambda (Free v) B)) || |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
112 |
term1 env T) x |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
113 |
and term1 env T x = |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
114 |
(enum2 "==>" (term2 env propT) >> foldr1 (fn (A, B) => Term.implies $ A $ B) || |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
115 |
term2 env T) x |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
116 |
and term2 env T x = |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
117 |
(equal env "==" || equal env "=?=" || term3 env T) x |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
118 |
and equal env eq x = |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
119 |
(term3 env dummyT -- ($$ eq |-- term2 env dummyT) >> (fn (t, u) => |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
120 |
Const (eq, Term.fastype_of t --> Term.fastype_of u --> propT) $ t $ u)) x |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
121 |
and term3 env T x = |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
122 |
(var >> Free || |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
123 |
$$ "CONST" |-- const -- constraint >> Const || |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
124 |
$$ "%" |-- bind :|-- (fn v => term3 (v :: env) dummyT >> lambda (Free v)) || |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
125 |
term4 env T) x |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
126 |
and term4 env T x = |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
127 |
(term5 env dummyT -- Scan.repeat1 (term5 env dummyT) >> Term.list_comb || |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
128 |
term5 env T) x |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
129 |
and term5 env T x = |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
130 |
(ident >> (fn a => Free (a, the_default T (AList.lookup (op =) env a))) || |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
131 |
$$ "CONST" |-- const >> (fn c => Const (c, T)) || |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
132 |
$$ "(" |-- term env T --| $$ ")") x; |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
133 |
|
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
134 |
val read_term = read (term [] dummyT); |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
135 |
val read_prop = read (term [] propT); |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
136 |
|
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
137 |
end; |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
138 |
|
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
139 |
end; |
d8b05073edc7
Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff
changeset
|
140 |