src/HOL/Tools/Predicate_Compile/pred_compile_aux.ML
author bulwahn
Wed, 23 Sep 2009 16:20:12 +0200
changeset 32668 b2de45007537
child 33113 0f6e30b87cf1
permissions -rw-r--r--
added first prototype of the extended predicate compiler
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
     1
(* Author: Lukas Bulwahn, TU Muenchen
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
     2
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
     3
Auxilary functions for predicate compiler
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
     4
*)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
     5
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
     6
structure Predicate_Compile_Aux =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
     7
struct
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
     8
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
     9
(* syntactic functions *)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    10
 
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    11
fun is_equationlike_term (Const ("==", _) $ _ $ _) = true
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    12
  | is_equationlike_term (Const ("Trueprop", _) $ (Const ("op =", _) $ _ $ _)) = true
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    13
  | is_equationlike_term _ = false
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    14
  
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    15
val is_equationlike = is_equationlike_term o prop_of 
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    16
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    17
fun is_pred_equation_term (Const ("==", _) $ u $ v) =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    18
  (fastype_of u = @{typ bool}) andalso (fastype_of v = @{typ bool})
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    19
  | is_pred_equation_term _ = false
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    20
  
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    21
val is_pred_equation = is_pred_equation_term o prop_of 
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    22
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    23
fun is_intro_term constname t =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    24
  case fst (strip_comb (HOLogic.dest_Trueprop (Logic.strip_imp_concl t))) of
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    25
    Const (c, _) => c = constname
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    26
  | _ => false
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    27
  
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    28
fun is_intro constname t = is_intro_term constname (prop_of t)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    29
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    30
fun is_pred thy constname =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    31
  let
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    32
    val T = (Sign.the_const_type thy constname)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    33
  in body_type T = @{typ "bool"} end;
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    34
  
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    35
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    36
fun is_predT (T as Type("fun", [_, _])) = (snd (strip_type T) = HOLogic.boolT)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    37
  | is_predT _ = false
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    38
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    39
  
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    40
(*** check if a term contains only constructor functions ***)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    41
fun is_constrt thy =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    42
  let
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    43
    val cnstrs = flat (maps
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    44
      (map (fn (_, (Tname, _, cs)) => map (apsnd (rpair Tname o length)) cs) o #descr o snd)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    45
      (Symtab.dest (Datatype.get_all thy)));
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    46
    fun check t = (case strip_comb t of
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    47
        (Free _, []) => true
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    48
      | (Const (s, T), ts) => (case (AList.lookup (op =) cnstrs s, body_type T) of
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    49
            (SOME (i, Tname), Type (Tname', _)) => length ts = i andalso Tname = Tname' andalso forall check ts
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    50
          | _ => false)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    51
      | _ => false)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    52
  in check end;  
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    53
  
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    54
fun strip_ex (Const ("Ex", _) $ Abs (x, T, t)) =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    55
  let
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    56
    val (xTs, t') = strip_ex t
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    57
  in
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    58
    ((x, T) :: xTs, t')
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    59
  end
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    60
  | strip_ex t = ([], t)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    61
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    62
fun focus_ex t nctxt =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    63
  let
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    64
    val ((xs, Ts), t') = apfst split_list (strip_ex t) 
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    65
    val (xs', nctxt') = Name.variants xs nctxt;
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    66
    val ps' = xs' ~~ Ts;
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    67
    val vs = map Free ps';
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    68
    val t'' = Term.subst_bounds (rev vs, t');
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    69
  in ((ps', t''), nctxt') end;
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    70
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    71
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    72
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    73
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    74
(*
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    75
fun map_atoms f intro = 
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    76
fun fold_atoms f intro =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    77
*)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    78
fun fold_map_atoms f intro s =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    79
  let
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    80
    val (literals, head) = Logic.strip_horn intro
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    81
    fun appl t s = (case t of
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    82
      (@{term "Not"} $ t') =>
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    83
        let
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    84
          val (t'', s') = f t' s
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    85
        in (@{term "Not"} $ t'', s') end
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    86
      | _ => f t s)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    87
    val (literals', s') = fold_map appl (map HOLogic.dest_Trueprop literals) s
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    88
  in
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    89
    (Logic.list_implies (map HOLogic.mk_Trueprop literals', head), s')
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    90
  end;
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    91
  
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    92
(*
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    93
fun equals_conv lhs_cv rhs_cv ct =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    94
  case Thm.term_of ct of
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    95
    Const ("==", _) $ _ $ _ => Conv.arg_conv cv ct  
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    96
  | _ => error "equals_conv"  
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    97
*)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    98
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    99
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   100
end;