482 |
482 |
483 fun should_tag_with_type ctxt (Tags full_types) ty = |
483 fun should_tag_with_type ctxt (Tags full_types) ty = |
484 full_types orelse is_combtyp_dangerous ctxt ty |
484 full_types orelse is_combtyp_dangerous ctxt ty |
485 | should_tag_with_type _ _ _ = false |
485 | should_tag_with_type _ _ _ = false |
486 |
486 |
487 val fname_table = |
487 val proxy_table = |
488 [("c_False", (0, ("c_fFalse", @{const_name Metis.fFalse}))), |
488 [("c_False", ("c_fFalse", @{const_name Metis.fFalse})), |
489 ("c_True", (0, ("c_fTrue", @{const_name Metis.fTrue}))), |
489 ("c_True", ("c_fTrue", @{const_name Metis.fTrue})), |
490 ("c_Not", (1, ("c_fNot", @{const_name Metis.fNot}))), |
490 ("c_Not", ("c_fNot", @{const_name Metis.fNot})), |
491 ("c_conj", (2, ("c_fconj", @{const_name Metis.fconj}))), |
491 ("c_conj", ("c_fconj", @{const_name Metis.fconj})), |
492 ("c_disj", (2, ("c_fdisj", @{const_name Metis.fdisj}))), |
492 ("c_disj", ("c_fdisj", @{const_name Metis.fdisj})), |
493 ("c_implies", (2, ("c_fimplies", @{const_name Metis.fimplies}))), |
493 ("c_implies", ("c_fimplies", @{const_name Metis.fimplies})), |
494 ("equal", (2, ("c_fequal", @{const_name Metis.fequal})))] |
494 ("equal", ("c_fequal", @{const_name Metis.fequal}))] |
495 |
495 |
496 fun repair_combterm_consts type_sys = |
496 fun repair_combterm_consts type_sys = |
497 let |
497 let |
498 fun aux (CombApp tmp) = CombApp (pairself aux tmp) |
498 fun aux top_level (CombApp (tm1, tm2)) = |
499 | aux (CombConst (name as (s, _), ty, ty_args)) = |
499 CombApp (aux top_level tm1, aux false tm2) |
|
500 | aux top_level (CombConst (name as (s, _), ty, ty_args)) = |
500 (case strip_prefix_and_unascii const_prefix s of |
501 (case strip_prefix_and_unascii const_prefix s of |
501 NONE => (name, ty_args) |
502 NONE => (name, ty_args) |
502 | SOME s'' => |
503 | SOME s'' => |
503 let val s'' = invert_const s'' in |
504 let val s'' = invert_const s'' in |
504 case type_arg_policy type_sys s'' of |
505 case type_arg_policy type_sys s'' of |
505 No_Type_Args => (name, []) |
506 No_Type_Args => (name, []) |
506 | Mangled_Types => (mangled_const_name ty_args name, []) |
507 | Mangled_Types => (mangled_const_name ty_args name, []) |
507 | Explicit_Type_Args => (name, ty_args) |
508 | Explicit_Type_Args => (name, ty_args) |
508 end) |
509 end) |
|
510 |> (fn (name as (s, s'), ty_args) => |
|
511 case AList.lookup (op =) proxy_table s of |
|
512 SOME proxy_name => |
|
513 if top_level then |
|
514 (case s of |
|
515 "c_False" => ("$false", s') |
|
516 | "c_True" => ("$true", s') |
|
517 | _ => name, []) |
|
518 else |
|
519 (proxy_name, ty_args) |
|
520 | NONE => (name, ty_args)) |
509 |> (fn (name, ty_args) => CombConst (name, ty, ty_args)) |
521 |> (fn (name, ty_args) => CombConst (name, ty, ty_args)) |
510 | aux tm = tm |
522 | aux _ tm = tm |
511 in aux end |
523 in aux true end |
512 |
524 |
513 fun pred_combtyp ty = |
525 fun pred_combtyp ty = |
514 case combtyp_from_typ @{typ "unit => bool"} of |
526 case combtyp_from_typ @{typ "unit => bool"} of |
515 CombType (name, [_, bool_ty]) => CombType (name, [ty, bool_ty]) |
527 CombType (name, [_, bool_ty]) => CombType (name, [ty, bool_ty]) |
516 | _ => raise Fail "expected two-argument type constructor" |
528 | _ => raise Fail "expected two-argument type constructor" |
521 |> repair_combterm_consts type_sys |
533 |> repair_combterm_consts type_sys |
522 |> AAtom |
534 |> AAtom |
523 |
535 |
524 fun formula_for_combformula ctxt type_sys = |
536 fun formula_for_combformula ctxt type_sys = |
525 let |
537 let |
526 fun do_term top_level u = |
538 fun do_term u = |
527 let |
539 let |
528 val (head, args) = strip_combterm_comb u |
540 val (head, args) = strip_combterm_comb u |
529 val (x, ty_args) = |
541 val (x, ty_args) = |
530 case head of |
542 case head of |
531 CombConst (name as (s, s'), _, ty_args) => |
543 CombConst (name, _, ty_args) => (name, ty_args) |
532 (case AList.lookup (op =) fname_table s of |
|
533 SOME (n, fname) => |
|
534 (if top_level andalso length args = n then |
|
535 case s of |
|
536 "c_False" => ("$false", s') |
|
537 | "c_True" => ("$true", s') |
|
538 | _ => name |
|
539 else |
|
540 fname, []) |
|
541 | NONE => (name, ty_args)) |
|
542 | CombVar (name, _) => (name, []) |
544 | CombVar (name, _) => (name, []) |
543 | CombApp _ => raise Fail "impossible \"CombApp\"" |
545 | CombApp _ => raise Fail "impossible \"CombApp\"" |
544 val t = ATerm (x, map fo_term_for_combtyp ty_args @ |
546 val t = ATerm (x, map fo_term_for_combtyp ty_args @ map do_term args) |
545 map (do_term false) args) |
|
546 val ty = combtyp_of u |
547 val ty = combtyp_of u |
547 in |
548 in |
548 t |> (if should_tag_with_type ctxt type_sys ty then |
549 t |> (if should_tag_with_type ctxt type_sys ty then |
549 tag_with_type (fo_term_for_combtyp ty) |
550 tag_with_type (fo_term_for_combtyp ty) |
550 else |
551 else |
566 (map_filter |
567 (map_filter |
567 (fn (_, NONE) => NONE |
568 (fn (_, NONE) => NONE |
568 | (s, SOME ty) => do_out_of_bound_type (s, ty)) xs) |
569 | (s, SOME ty) => do_out_of_bound_type (s, ty)) xs) |
569 (do_formula phi)) |
570 (do_formula phi)) |
570 | do_formula (AConn (c, phis)) = AConn (c, map do_formula phis) |
571 | do_formula (AConn (c, phis)) = AConn (c, map do_formula phis) |
571 | do_formula (AAtom tm) = AAtom (do_term true tm) |
572 | do_formula (AAtom tm) = AAtom (do_term tm) |
572 in do_formula end |
573 in do_formula end |
573 |
574 |
574 fun formula_for_fact ctxt type_sys |
575 fun formula_for_fact ctxt type_sys |
575 ({combformula, ctypes_sorts, ...} : translated_formula) = |
576 ({combformula, ctypes_sorts, ...} : translated_formula) = |
576 mk_ahorn (map (formula_for_fo_literal o fo_literal_for_type_literal) |
577 mk_ahorn (map (formula_for_fo_literal o fo_literal_for_type_literal) |