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