src/Pure/facts.ML
author wenzelm
Thu Mar 20 17:38:53 2008 +0100 (2008-03-20)
changeset 26366 5c089f4219c2
parent 26361 7946f459c6c8
child 26393 42febbed5460
permissions -rw-r--r--
added pos_of_ref;
     1 (*  Title:      Pure/facts.ML
     2     ID:         $Id$
     3     Author:     Makarius
     4 
     5 Environment of named facts, optionally indexed by proposition.
     6 *)
     7 
     8 signature FACTS =
     9 sig
    10   val the_single: string -> thm list -> thm
    11   datatype interval = FromTo of int * int | From of int | Single of int
    12   datatype ref =
    13     Named of (string * Position.T) * interval list option |
    14     Fact of string
    15   val named: string -> ref
    16   val string_of_ref: ref -> string
    17   val name_of_ref: ref -> string
    18   val pos_of_ref: ref -> Position.T
    19   val map_name_of_ref: (string -> string) -> ref -> ref
    20   val select: ref -> thm list -> thm list
    21   val selections: string * thm list -> (ref * thm) list
    22   type T
    23   val space_of: T -> NameSpace.T
    24   val lookup: T -> string -> thm list option
    25   val dest: T -> (string * thm list) list
    26   val extern: T -> (xstring * thm list) list
    27   val props: T -> thm list
    28   val could_unify: T -> term -> thm list
    29   val empty: T
    30   val merge: T * T -> T
    31   val add_global: NameSpace.naming -> string * thm list -> T -> T
    32   val add_local: bool -> NameSpace.naming -> string * thm list -> T -> T
    33   val del: string -> T -> T
    34 end;
    35 
    36 structure Facts: FACTS =
    37 struct
    38 
    39 (** fact references **)
    40 
    41 fun the_single _ [th] : thm = th
    42   | the_single name _ = error ("Single theorem expected " ^ quote name);
    43 
    44 
    45 (* datatype interval *)
    46 
    47 datatype interval =
    48   FromTo of int * int |
    49   From of int |
    50   Single of int;
    51 
    52 fun string_of_interval (FromTo (i, j)) = string_of_int i ^ "-" ^ string_of_int j
    53   | string_of_interval (From i) = string_of_int i ^ "-"
    54   | string_of_interval (Single i) = string_of_int i;
    55 
    56 fun interval n iv =
    57   let fun err () = raise Fail ("Bad interval specification " ^ string_of_interval iv) in
    58     (case iv of
    59       FromTo (i, j) => if i <= j then i upto j else err ()
    60     | From i => if i <= n then i upto n else err ()
    61     | Single i => [i])
    62   end;
    63 
    64 
    65 (* datatype ref *)
    66 
    67 datatype ref =
    68   Named of (string * Position.T) * interval list option |
    69   Fact of string;
    70 
    71 fun named name = Named ((name, Position.none), NONE);
    72 
    73 fun name_pos_of_ref (Named (name_pos, _)) = name_pos
    74   | name_pos_of_ref (Fact _) = error "Illegal literal fact";
    75 
    76 val name_of_ref = #1 o name_pos_of_ref;
    77 val pos_of_ref = #2 o name_pos_of_ref;
    78 
    79 fun map_name_of_ref f (Named ((name, pos), is)) = Named ((f name, pos), is)
    80   | map_name_of_ref _ r = r;
    81 
    82 fun string_of_ref (Named ((name, _), NONE)) = name
    83   | string_of_ref (Named ((name, _), SOME is)) =
    84       name ^ enclose "(" ")" (commas (map string_of_interval is))
    85   | string_of_ref (Fact _) = error "Illegal literal fact";
    86 
    87 
    88 (* select *)
    89 
    90 fun select (Fact _) ths = ths
    91   | select (Named (_, NONE)) ths = ths
    92   | select (Named ((name, pos), SOME ivs)) ths =
    93       let
    94         val n = length ths;
    95         fun err msg =
    96           error (msg ^ " for " ^ quote name ^ " (length " ^ string_of_int n ^ ")" ^
    97             Position.str_of pos);
    98         fun sel i =
    99           if i < 1 orelse i > n then err ("Bad subscript " ^ string_of_int i)
   100           else nth ths (i - 1);
   101         val is = maps (interval n) ivs handle Fail msg => err msg;
   102       in map sel is end;
   103 
   104 
   105 (* selections *)
   106 
   107 fun selections (name, [th]) = [(Named ((name, Position.none), NONE), th)]
   108   | selections (name, ths) = map2 (fn i => fn th =>
   109       (Named ((name, Position.none), SOME [Single i]), th)) (1 upto length ths) ths;
   110 
   111 
   112 
   113 (** fact environment **)
   114 
   115 (* datatype T *)
   116 
   117 datatype T = Facts of
   118  {facts: thm list NameSpace.table,
   119   props: thm Net.net};
   120 
   121 fun make_facts facts props = Facts {facts = facts, props = props};
   122 
   123 
   124 (* named facts *)
   125 
   126 fun facts_of (Facts {facts, ...}) = facts;
   127 
   128 val space_of = #1 o facts_of;
   129 val lookup = Symtab.lookup o #2 o facts_of;
   130 val dest = NameSpace.dest_table o facts_of;
   131 val extern = NameSpace.extern_table o facts_of;
   132 
   133 
   134 (* indexed props *)
   135 
   136 val prop_ord = Term.term_ord o pairself Thm.full_prop_of;
   137 
   138 fun props (Facts {props, ...}) = sort_distinct prop_ord (Net.content props);
   139 fun could_unify (Facts {props, ...}) = Net.unify_term props;
   140 
   141 
   142 (* build facts *)
   143 
   144 val empty = make_facts NameSpace.empty_table Net.empty;
   145 
   146 fun merge (Facts {facts = facts1, props = props1}, Facts {facts = facts2, props = props2}) =
   147   let
   148     val facts' = NameSpace.merge_tables (K true) (facts1, facts2);
   149     val props' = Net.merge (is_equal o prop_ord) (props1, props2);
   150   in make_facts facts' props' end;
   151 
   152 fun add permissive do_props naming (name, ths) (Facts {facts, props}) =
   153   let
   154     val facts' =
   155       if name = "" then facts
   156       else
   157         let
   158           val (space, tab) = facts;
   159           val space' = NameSpace.declare naming name space;
   160           val tab' = (if permissive then Symtab.update else Symtab.update_new) (name, ths) tab
   161             handle Symtab.DUP dup => (legacy_feature ("Duplicate fact " ^ quote dup ^
   162               Position.str_of (Position.thread_data ())); tab);
   163         in (space', tab') end;
   164     val props' =
   165       if do_props then fold (fn th => Net.insert_term (K false) (Thm.full_prop_of th, th)) ths props
   166       else props;
   167   in make_facts facts' props' end;
   168 
   169 val add_global = add false false;
   170 val add_local = add true;
   171 
   172 fun del name (Facts {facts = (space, tab), props}) =
   173   let
   174     val space' = NameSpace.hide true name space handle ERROR _ => space;
   175     val tab' = Symtab.delete_safe name tab;
   176   in make_facts (space', tab') props end;
   177 
   178 end;