src/Pure/Syntax/simple_syntax.ML
author wenzelm
Mon, 13 Aug 2007 00:17:57 +0200
changeset 24236 d8b05073edc7
child 24237 4bf3e084d9b5
permissions -rw-r--r--
Simple syntax for types and terms --- for bootstrapping Pure.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
    60
  typ1 = typ2 const ... const
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
    61
  typ2 = tfree | const | "(" typ ")"
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
    62
*)
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
    63
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
    64
fun typ x =
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
    65
 (enum1 "=>" typ1 >> (op ---> o split_last)) x
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
    66
and typ1 x =
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
    67
 (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
    68
and typ2 x =
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
    69
 (tfree >> (fn a => TFree (a, [])) ||
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
    70
  const >> (fn c => Type (c, [])) ||
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
    71
  $$ "(" |-- typ --| $$ ")") x;
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
    72
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
    73
val read_typ = read typ;
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
    74
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
    75
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
    76
(* terms *)
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
    77
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
  term  = !!ident :: typ. term
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
    80
        | term1
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
    81
  term1 = term2 ==> ... ==> term2
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
    82
        | term2
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
    83
  term2 = term3 == term2
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
    84
        | term3 =?= term2
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
    85
        | term3
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
    86
  term3 = ident :: typ
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
    87
        | CONST const :: typ
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
    88
        | %ident :: typ. term3
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
    89
        | term4
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
    90
  term4 = term5 ... term5
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
    91
        | term5
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
    92
  term5 = ident
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
    93
        | CONST const
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
    94
        | "(" term ")"
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
    95
*)
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
    96
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
    97
local
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
    98
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
    99
val constraint = $$ "::" |-- typ;
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
   100
val var = ident -- constraint;
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
   101
val bind = var --| $$ ".";
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
in
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
   104
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
   105
fun term env T x =
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
   106
 ($$ "!!" |-- bind :|-- (fn v =>
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
   107
   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
   108
  term1 env T) x
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
   109
and term1 env T x =
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
   110
 (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
   111
  term2 env T) x
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
   112
and term2 env T x =
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
   113
 (equal env "==" || equal env "=?=" || term3 env T) x
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
   114
and equal env eq x =
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
   115
 (term3 env dummyT -- ($$ eq |-- term2 env dummyT) >> (fn (t, u) =>
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
   116
   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
   117
and term3 env T x =
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
   118
 (var >> Free ||
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
   119
  $$ "CONST" |-- const -- constraint >> Const ||
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
   120
  $$ "%" |-- bind :|-- (fn v => term3 (v :: env) dummyT >> lambda (Free v)) ||
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
   121
  term4 env T) x
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
   122
and term4 env T x =
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
   123
 (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
   124
  term5 env T) x
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
   125
and term5 env T x =
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
   126
 (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
   127
  $$ "CONST" |-- const >> (fn c => Const (c, T)) ||
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
   128
  $$ "(" |-- term env T --| $$ ")") x;
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
   129
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
   130
val read_term = read (term [] dummyT);
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
   131
val read_prop = read (term [] propT);
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
   132
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
   133
end;
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
   134
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
   135
end;
d8b05073edc7 Simple syntax for types and terms --- for bootstrapping Pure.
wenzelm
parents:
diff changeset
   136