89 fun lookup_btab ctxt btab (n, T) = |
89 fun lookup_btab ctxt btab (n, T) = |
90 (case Symtab.lookup btab n of |
90 (case Symtab.lookup btab n of |
91 NONE => NONE |
91 NONE => NONE |
92 | SOME ttab => lookup_ttab ctxt ttab T) |
92 | SOME ttab => lookup_ttab ctxt ttab T) |
93 |
93 |
94 |
94 type 'a bfun = Proof.context -> typ -> term list -> 'a |
95 (* built-in types *) |
95 |
96 |
96 type bfunr = string * int * term list * (term list -> term) |
97 (* FIXME just one data slot (record) per program unit *) |
97 |
98 structure Builtin_Types = Generic_Data |
98 structure Builtins = Generic_Data |
99 ( |
99 ( |
100 type T = |
100 type T = |
101 (typ -> bool, (typ -> string option) * (typ -> int -> string option)) ttab |
101 (typ -> bool, (typ -> string option) * (typ -> int -> string option)) ttab * |
102 val empty = [] |
102 (term list bfun, bfunr option bfun) btab |
|
103 val empty = ([], Symtab.empty) |
103 val extend = I |
104 val extend = I |
104 val merge = merge_ttab |
105 fun merge ((t1, b1), (t2, b2)) = (merge_ttab (t1, t2), merge_btab (b1, b2)) |
105 ) |
106 ) |
106 |
107 |
|
108 (* built-in types *) |
|
109 |
107 fun add_builtin_typ cs (T, f, g) = |
110 fun add_builtin_typ cs (T, f, g) = |
108 Builtin_Types.map (insert_ttab cs T (Int (f, g))) |
111 Builtins.map (apfst (insert_ttab cs T (Int (f, g)))) |
109 |
112 |
110 fun add_builtin_typ_ext (T, f) = |
113 fun add_builtin_typ_ext (T, f) = |
111 Builtin_Types.map (insert_ttab SMT_Utils.basicC T (Ext f)) |
114 Builtins.map (apfst (insert_ttab SMT_Utils.basicC T (Ext f))) |
112 |
115 |
113 fun lookup_builtin_typ ctxt = |
116 fun lookup_builtin_typ ctxt = |
114 lookup_ttab ctxt (Builtin_Types.get (Context.Proof ctxt)) |
117 lookup_ttab ctxt (fst (Builtins.get (Context.Proof ctxt))) |
115 |
118 |
116 fun dest_builtin_typ ctxt T = |
119 fun dest_builtin_typ ctxt T = |
117 (case lookup_builtin_typ ctxt T of |
120 (case lookup_builtin_typ ctxt T of |
118 SOME (_, Int (f, _)) => f T |
121 SOME (_, Int (f, _)) => f T |
119 | _ => NONE) |
122 | _ => NONE) |
143 | SOME (T, _) => is_builtin_typ_ext ctxt T) |
146 | SOME (T, _) => is_builtin_typ_ext ctxt T) |
144 |
147 |
145 |
148 |
146 (* built-in functions *) |
149 (* built-in functions *) |
147 |
150 |
148 type 'a bfun = Proof.context -> typ -> term list -> 'a |
|
149 |
|
150 type bfunr = string * int * term list * (term list -> term) |
|
151 |
|
152 (* FIXME just one data slot (record) per program unit *) |
|
153 structure Builtin_Funcs = Generic_Data |
|
154 ( |
|
155 type T = (term list bfun, bfunr option bfun) btab |
|
156 val empty = Symtab.empty |
|
157 val extend = I |
|
158 val merge = merge_btab |
|
159 ) |
|
160 |
|
161 fun add_builtin_fun cs ((n, T), f) = |
151 fun add_builtin_fun cs ((n, T), f) = |
162 Builtin_Funcs.map (insert_btab cs n T (Int f)) |
152 Builtins.map (apsnd (insert_btab cs n T (Int f))) |
163 |
153 |
164 fun add_builtin_fun' cs (t, n) = |
154 fun add_builtin_fun' cs (t, n) = |
165 let |
155 let |
166 val c as (m, T) = Term.dest_Const t |
156 val c as (m, T) = Term.dest_Const t |
167 fun app U ts = Term.list_comb (Const (m, U), ts) |
157 fun app U ts = Term.list_comb (Const (m, U), ts) |
168 fun bfun _ U ts = SOME (n, length (Term.binder_types T), ts, app U) |
158 fun bfun _ U ts = SOME (n, length (Term.binder_types T), ts, app U) |
169 in add_builtin_fun cs (c, bfun) end |
159 in add_builtin_fun cs (c, bfun) end |
170 |
160 |
171 fun add_builtin_fun_ext ((n, T), f) = |
161 fun add_builtin_fun_ext ((n, T), f) = |
172 Builtin_Funcs.map (insert_btab SMT_Utils.basicC n T (Ext f)) |
162 Builtins.map (apsnd (insert_btab SMT_Utils.basicC n T (Ext f))) |
173 |
163 |
174 fun add_builtin_fun_ext' c = add_builtin_fun_ext (c, fn _ => fn _ => I) |
164 fun add_builtin_fun_ext' c = add_builtin_fun_ext (c, fn _ => fn _ => I) |
175 |
165 |
176 fun add_builtin_fun_ext'' n context = |
166 fun add_builtin_fun_ext'' n context = |
177 let val thy = Context.theory_of context |
167 let val thy = Context.theory_of context |
178 in add_builtin_fun_ext' (n, Sign.the_const_type thy n) context end |
168 in add_builtin_fun_ext' (n, Sign.the_const_type thy n) context end |
179 |
169 |
180 fun lookup_builtin_fun ctxt = |
170 fun lookup_builtin_fun ctxt = |
181 lookup_btab ctxt (Builtin_Funcs.get (Context.Proof ctxt)) |
171 lookup_btab ctxt (snd (Builtins.get (Context.Proof ctxt))) |
182 |
172 |
183 fun dest_builtin_fun ctxt (c as (_, T)) ts = |
173 fun dest_builtin_fun ctxt (c as (_, T)) ts = |
184 (case lookup_builtin_fun ctxt c of |
174 (case lookup_builtin_fun ctxt c of |
185 SOME (_, Int f) => f ctxt T ts |
175 SOME (_, Int f) => f ctxt T ts |
186 | _ => NONE) |
176 | _ => NONE) |