src/HOL/Prolog/Test.thy
author blanchet
Tue Nov 07 15:16:42 2017 +0100 (20 months ago)
changeset 67022 49309fe530fd
parent 63167 0909deb8059b
child 67399 eab6ce8368fa
permissions -rw-r--r--
more robust parsing for THF proofs (esp. polymorphic Leo-III proofs)
oheimb@13208
     1
(*  Title:    HOL/Prolog/Test.thy
oheimb@13208
     2
    Author:   David von Oheimb (based on a lecture on Lambda Prolog by Nadathur)
oheimb@13208
     3
*)
oheimb@9015
     4
wenzelm@63167
     5
section \<open>Basic examples\<close>
oheimb@9015
     6
wenzelm@17311
     7
theory Test
wenzelm@17311
     8
imports HOHH
wenzelm@17311
     9
begin
oheimb@9015
    10
wenzelm@17311
    11
typedecl nat
wenzelm@17311
    12
wenzelm@17311
    13
typedecl 'a list
oheimb@9015
    14
wenzelm@17311
    15
consts
wenzelm@17311
    16
  Nil   :: "'a list"                                  ("[]")
wenzelm@17311
    17
  Cons  :: "'a => 'a list => 'a list"                 (infixr "#"  65)
oheimb@9015
    18
oheimb@9015
    19
syntax
oheimb@9015
    20
  (* list Enumeration *)
wenzelm@35109
    21
  "_list"     :: "args => 'a list"                          ("[(_)]")
oheimb@9015
    22
oheimb@9015
    23
translations
oheimb@9015
    24
  "[x, xs]"     == "x#[xs]"
oheimb@9015
    25
  "[x]"         == "x#[]"
oheimb@9015
    26
wenzelm@17311
    27
typedecl person
oheimb@9015
    28
wenzelm@51311
    29
axiomatization
wenzelm@51311
    30
  append  :: "['a list, 'a list, 'a list]            => bool" and
wenzelm@51311
    31
  reverse :: "['a list, 'a list]                     => bool" and
oheimb@9015
    32
wenzelm@51311
    33
  mappred :: "[('a => 'b => bool), 'a list, 'b list] => bool" and
wenzelm@51311
    34
  mapfun  :: "[('a => 'b), 'a list, 'b list]         => bool" and
oheimb@9015
    35
wenzelm@51311
    36
  bob     :: person and
wenzelm@51311
    37
  sue     :: person and
wenzelm@51311
    38
  ned     :: person and
oheimb@9015
    39
wenzelm@51311
    40
  nat23   :: nat          ("23") and
wenzelm@51311
    41
  nat24   :: nat          ("24") and
wenzelm@51311
    42
  nat25   :: nat          ("25") and
oheimb@9015
    43
wenzelm@51311
    44
  age     :: "[person, nat]                          => bool" and
oheimb@9015
    45
wenzelm@51311
    46
  eq      :: "['a, 'a]                               => bool" and
oheimb@9015
    47
wenzelm@51311
    48
  empty   :: "['b]                                   => bool" and
wenzelm@51311
    49
  add     :: "['a, 'b, 'b]                           => bool" and
wenzelm@51311
    50
  remove  :: "['a, 'b, 'b]                           => bool" and
wenzelm@17311
    51
  bag_appl:: "['a, 'a, 'a, 'a]                       => bool"
wenzelm@51311
    52
where
wenzelm@51311
    53
        append:  "\<And>x xs ys zs. append  []    xs  xs    ..
wenzelm@51311
    54
                  append (x#xs) ys (x#zs) :- append xs ys zs" and
wenzelm@51311
    55
        reverse: "\<And>L1 L2. reverse L1 L2 :- (!rev_aux.
wenzelm@17311
    56
                  (!L.          rev_aux  []    L  L )..
wenzelm@17311
    57
                  (!X L1 L2 L3. rev_aux (X#L1) L2 L3 :- rev_aux L1 L2 (X#L3))
wenzelm@51311
    58
                  => rev_aux L1 L2 [])" and
oheimb@9015
    59
wenzelm@51311
    60
        mappred: "\<And>x xs y ys P. mappred P  []     []    ..
wenzelm@51311
    61
                  mappred P (x#xs) (y#ys) :- P x y  &  mappred P xs ys" and
wenzelm@51311
    62
        mapfun:  "\<And>x xs ys f. mapfun f  []     []      ..
wenzelm@51311
    63
                  mapfun f (x#xs) (f x#ys) :- mapfun f xs ys" and
oheimb@9015
    64
wenzelm@17311
    65
        age:     "age bob 24 ..
wenzelm@17311
    66
                  age sue 23 ..
wenzelm@51311
    67
                  age ned 23" and
oheimb@9015
    68
wenzelm@51311
    69
        eq:      "\<And>x. eq x x" and
oheimb@9015
    70
oheimb@9015
    71
(* actual definitions of empty and add is hidden -> yields abstract data type *)
oheimb@9015
    72
wenzelm@51311
    73
        bag_appl: "\<And>A B X Y. bag_appl A B X Y:- (? S1 S2 S3 S4 S5.
wenzelm@17311
    74
                                empty    S1    &
wenzelm@17311
    75
                                add A    S1 S2 &
wenzelm@17311
    76
                                add B    S2 S3 &
wenzelm@17311
    77
                                remove X S3 S4 &
wenzelm@17311
    78
                                remove Y S4 S5 &
wenzelm@17311
    79
                                empty    S5)"
wenzelm@17311
    80
wenzelm@21425
    81
lemmas prog_Test = append reverse mappred mapfun age eq bag_appl
wenzelm@21425
    82
wenzelm@61337
    83
schematic_goal "append ?x ?y [a,b,c,d]"
wenzelm@21425
    84
  apply (prolog prog_Test)
wenzelm@21425
    85
  back
wenzelm@21425
    86
  back
wenzelm@21425
    87
  back
wenzelm@21425
    88
  back
wenzelm@21425
    89
  done
wenzelm@21425
    90
wenzelm@61337
    91
schematic_goal "append [a,b] y ?L"
wenzelm@21425
    92
  apply (prolog prog_Test)
wenzelm@21425
    93
  done
wenzelm@21425
    94
wenzelm@61337
    95
schematic_goal "!y. append [a,b] y (?L y)"
wenzelm@21425
    96
  apply (prolog prog_Test)
wenzelm@21425
    97
  done
wenzelm@21425
    98
wenzelm@61337
    99
schematic_goal "reverse [] ?L"
wenzelm@21425
   100
  apply (prolog prog_Test)
wenzelm@21425
   101
  done
wenzelm@21425
   102
wenzelm@61337
   103
schematic_goal "reverse [23] ?L"
wenzelm@21425
   104
  apply (prolog prog_Test)
wenzelm@21425
   105
  done
wenzelm@21425
   106
wenzelm@61337
   107
schematic_goal "reverse [23,24,?x] ?L"
wenzelm@21425
   108
  apply (prolog prog_Test)
wenzelm@21425
   109
  done
wenzelm@21425
   110
wenzelm@61337
   111
schematic_goal "reverse ?L [23,24,?x]"
wenzelm@21425
   112
  apply (prolog prog_Test)
wenzelm@21425
   113
  done
wenzelm@21425
   114
wenzelm@61337
   115
schematic_goal "mappred age ?x [23,24]"
wenzelm@21425
   116
  apply (prolog prog_Test)
wenzelm@21425
   117
  back
wenzelm@21425
   118
  done
wenzelm@21425
   119
wenzelm@61337
   120
schematic_goal "mappred (%x y. ? z. age z y) ?x [23,24]"
wenzelm@21425
   121
  apply (prolog prog_Test)
wenzelm@21425
   122
  done
wenzelm@21425
   123
wenzelm@61337
   124
schematic_goal "mappred ?P [bob,sue] [24,23]"
wenzelm@21425
   125
  apply (prolog prog_Test)
wenzelm@21425
   126
  done
wenzelm@21425
   127
wenzelm@61337
   128
schematic_goal "mapfun f [bob,bob,sue] [?x,?y,?z]"
wenzelm@21425
   129
  apply (prolog prog_Test)
wenzelm@21425
   130
  done
wenzelm@21425
   131
wenzelm@61337
   132
schematic_goal "mapfun (%x. h x 25) [bob,sue] ?L"
wenzelm@21425
   133
  apply (prolog prog_Test)
wenzelm@21425
   134
  done
wenzelm@21425
   135
wenzelm@61337
   136
schematic_goal "mapfun ?F [24,25] [h bob 24,h bob 25]"
wenzelm@21425
   137
  apply (prolog prog_Test)
wenzelm@21425
   138
  done
wenzelm@21425
   139
wenzelm@61337
   140
schematic_goal "mapfun ?F [24] [h 24 24]"
wenzelm@21425
   141
  apply (prolog prog_Test)
wenzelm@21425
   142
  back
wenzelm@21425
   143
  back
wenzelm@21425
   144
  back
wenzelm@21425
   145
  done
wenzelm@21425
   146
wenzelm@21425
   147
lemma "True"
wenzelm@21425
   148
  apply (prolog prog_Test)
wenzelm@21425
   149
  done
wenzelm@21425
   150
wenzelm@61337
   151
schematic_goal "age ?x 24 & age ?y 23"
wenzelm@21425
   152
  apply (prolog prog_Test)
wenzelm@21425
   153
  back
wenzelm@21425
   154
  done
wenzelm@21425
   155
wenzelm@61337
   156
schematic_goal "age ?x 24 | age ?x 23"
wenzelm@21425
   157
  apply (prolog prog_Test)
wenzelm@21425
   158
  back
wenzelm@21425
   159
  back
wenzelm@21425
   160
  done
wenzelm@21425
   161
wenzelm@21425
   162
lemma "? x y. age x y"
wenzelm@21425
   163
  apply (prolog prog_Test)
wenzelm@21425
   164
  done
wenzelm@21425
   165
wenzelm@21425
   166
lemma "!x y. append [] x x"
wenzelm@21425
   167
  apply (prolog prog_Test)
wenzelm@21425
   168
  done
wenzelm@21425
   169
wenzelm@61337
   170
schematic_goal "age sue 24 .. age bob 23 => age ?x ?y"
wenzelm@21425
   171
  apply (prolog prog_Test)
wenzelm@21425
   172
  back
wenzelm@21425
   173
  back
wenzelm@21425
   174
  back
wenzelm@21425
   175
  back
wenzelm@21425
   176
  done
wenzelm@21425
   177
wenzelm@21425
   178
(*set trace_DEPTH_FIRST;*)
wenzelm@21425
   179
lemma "age bob 25 :- age bob 24 => age bob 25"
wenzelm@21425
   180
  apply (prolog prog_Test)
wenzelm@21425
   181
  done
wenzelm@21425
   182
(*reset trace_DEPTH_FIRST;*)
wenzelm@21425
   183
wenzelm@61337
   184
schematic_goal "(!x. age x 25 :- age x 23) => age ?x 25 & age ?y 25"
wenzelm@21425
   185
  apply (prolog prog_Test)
wenzelm@21425
   186
  back
wenzelm@21425
   187
  back
wenzelm@21425
   188
  back
wenzelm@21425
   189
  done
wenzelm@21425
   190
wenzelm@21425
   191
lemma "!x. ? y. eq x y"
wenzelm@21425
   192
  apply (prolog prog_Test)
wenzelm@21425
   193
  done
wenzelm@21425
   194
wenzelm@61337
   195
schematic_goal "? P. P & eq P ?x"
wenzelm@21425
   196
  apply (prolog prog_Test)
wenzelm@21425
   197
(*
wenzelm@21425
   198
  back
wenzelm@21425
   199
  back
wenzelm@21425
   200
  back
wenzelm@21425
   201
  back
wenzelm@21425
   202
  back
wenzelm@21425
   203
  back
wenzelm@21425
   204
  back
wenzelm@21425
   205
  back
wenzelm@21425
   206
*)
wenzelm@21425
   207
  done
wenzelm@21425
   208
wenzelm@21425
   209
lemma "? P. eq P (True & True) & P"
wenzelm@21425
   210
  apply (prolog prog_Test)
wenzelm@21425
   211
  done
wenzelm@21425
   212
wenzelm@21425
   213
lemma "? P. eq P op | & P k True"
wenzelm@21425
   214
  apply (prolog prog_Test)
wenzelm@21425
   215
  done
wenzelm@21425
   216
wenzelm@21425
   217
lemma "? P. eq P (Q => True) & P"
wenzelm@21425
   218
  apply (prolog prog_Test)
wenzelm@21425
   219
  done
wenzelm@21425
   220
wenzelm@21425
   221
(* P flexible: *)
wenzelm@21425
   222
lemma "(!P k l. P k l :- eq P Q) => Q a b"
wenzelm@21425
   223
  apply (prolog prog_Test)
wenzelm@21425
   224
  done
wenzelm@21425
   225
(*
wenzelm@21425
   226
loops:
wenzelm@21425
   227
lemma "(!P k l. P k l :- eq P (%x y. x | y)) => a | b"
wenzelm@21425
   228
*)
wenzelm@21425
   229
wenzelm@21425
   230
(* implication and disjunction in atom: *)
wenzelm@21425
   231
lemma "? Q. (!p q. R(q :- p) => R(Q p q)) & Q (t | s) (s | t)"
wenzelm@21425
   232
  by fast
wenzelm@21425
   233
wenzelm@21425
   234
(* disjunction in atom: *)
wenzelm@21425
   235
lemma "(!P. g P :- (P => b | a)) => g(a | b)"
wenzelm@42793
   236
  apply (tactic "step_tac (put_claset HOL_cs @{context}) 1")
wenzelm@42793
   237
  apply (tactic "step_tac (put_claset HOL_cs @{context}) 1")
wenzelm@42793
   238
  apply (tactic "step_tac (put_claset HOL_cs @{context}) 1")
wenzelm@21425
   239
   prefer 2
wenzelm@21425
   240
   apply fast
wenzelm@21425
   241
  apply fast
wenzelm@21425
   242
  done
wenzelm@21425
   243
wenzelm@21425
   244
(*
wenzelm@21425
   245
hangs:
wenzelm@21425
   246
lemma "(!P. g P :- (P => b | a)) => g(a | b)"
wenzelm@21425
   247
  by fast
wenzelm@21425
   248
*)
wenzelm@21425
   249
wenzelm@61337
   250
schematic_goal "!Emp Stk.(
wenzelm@21425
   251
                       empty    (Emp::'b) .. 
wenzelm@21425
   252
         (!(X::nat) S. add    X (S::'b)         (Stk X S)) .. 
wenzelm@21425
   253
         (!(X::nat) S. remove X ((Stk X S)::'b) S))
wenzelm@21425
   254
 => bag_appl 23 24 ?X ?Y"
wenzelm@21425
   255
  oops
wenzelm@21425
   256
wenzelm@61337
   257
schematic_goal "!Qu. ( 
wenzelm@21425
   258
          (!L.            empty    (Qu L L)) .. 
wenzelm@21425
   259
          (!(X::nat) L K. add    X (Qu L (X#K)) (Qu L K)) ..
wenzelm@21425
   260
          (!(X::nat) L K. remove X (Qu (X#L) K) (Qu L K)))
wenzelm@21425
   261
 => bag_appl 23 24 ?X ?Y"
wenzelm@21425
   262
  oops
wenzelm@21425
   263
wenzelm@21425
   264
lemma "D & (!y. E) :- (!x. True & True) :- True => D"
wenzelm@21425
   265
  apply (prolog prog_Test)
wenzelm@21425
   266
  done
wenzelm@21425
   267
wenzelm@61337
   268
schematic_goal "P x .. P y => P ?X"
wenzelm@21425
   269
  apply (prolog prog_Test)
wenzelm@21425
   270
  back
wenzelm@21425
   271
  done
wenzelm@17311
   272
oheimb@9015
   273
end