| 13208 |      1 | (*  Title:    HOL/Prolog/Type.thy
 | 
|  |      2 |     Author:   David von Oheimb (based on a lecture on Lambda Prolog by Nadathur)
 | 
|  |      3 | *)
 | 
| 9015 |      4 | 
 | 
| 17311 |      5 | header {* Type inference *}
 | 
| 9015 |      6 | 
 | 
| 17311 |      7 | theory Type
 | 
|  |      8 | imports Func
 | 
|  |      9 | begin
 | 
| 9015 |     10 | 
 | 
| 17311 |     11 | typedecl ty
 | 
| 9015 |     12 | 
 | 
| 17311 |     13 | consts
 | 
|  |     14 |   bool    :: ty
 | 
|  |     15 |   nat     :: ty
 | 
| 21425 |     16 |   arrow   :: "ty => ty => ty"       (infixr "->" 20)
 | 
| 17311 |     17 |   typeof  :: "[tm, ty] => bool"
 | 
|  |     18 |   anyterm :: tm
 | 
| 9015 |     19 | 
 | 
| 17311 |     20 | axioms  common_typeof:   "
 | 
| 9015 |     21 | typeof (app M N) B       :- typeof M (A -> B) & typeof N A..
 | 
|  |     22 | 
 | 
|  |     23 | typeof (cond C L R) A :- typeof C bool & typeof L A & typeof R A..
 | 
|  |     24 | typeof (fix F)   A       :- (!x. typeof x A => typeof (F  x) A)..
 | 
|  |     25 | 
 | 
|  |     26 | typeof true  bool..
 | 
|  |     27 | typeof false bool..
 | 
|  |     28 | typeof (M and N) bool :- typeof M bool & typeof N bool..
 | 
|  |     29 | 
 | 
|  |     30 | typeof (M eq  N) bool :- typeof M T    & typeof N T   ..
 | 
|  |     31 | 
 | 
|  |     32 | typeof  Z    nat..
 | 
|  |     33 | typeof (S N) nat :- typeof N nat..
 | 
|  |     34 | typeof (M + N) nat :- typeof M nat & typeof N nat..
 | 
|  |     35 | typeof (M - N) nat :- typeof M nat & typeof N nat..
 | 
|  |     36 | typeof (M * N) nat :- typeof M nat & typeof N nat"
 | 
|  |     37 | 
 | 
| 17311 |     38 | axioms good_typeof:     "
 | 
| 9015 |     39 | typeof (abs Bo) (A -> B) :- (!x. typeof x A => typeof (Bo x) B)"
 | 
|  |     40 | 
 | 
| 17311 |     41 | axioms bad1_typeof:     "
 | 
| 9015 |     42 | typeof (abs Bo) (A -> B) :- (typeof varterm A => typeof (Bo varterm) B)"
 | 
|  |     43 | 
 | 
| 17311 |     44 | axioms bad2_typeof:     "
 | 
| 9015 |     45 | typeof (abs Bo) (A -> B) :- (typeof anyterm A => typeof (Bo anyterm) B)"
 | 
|  |     46 | 
 | 
| 21425 |     47 | 
 | 
|  |     48 | lemmas prog_Type = prog_Func good_typeof common_typeof
 | 
|  |     49 | 
 | 
| 36319 |     50 | schematic_lemma "typeof (abs(%n. abs(%m. abs(%p. p and (n eq m))))) ?T"
 | 
| 21425 |     51 |   apply (prolog prog_Type)
 | 
|  |     52 |   done
 | 
|  |     53 | 
 | 
| 36319 |     54 | schematic_lemma "typeof (fix (%x. x)) ?T"
 | 
| 21425 |     55 |   apply (prolog prog_Type)
 | 
|  |     56 |   done
 | 
|  |     57 | 
 | 
| 36319 |     58 | schematic_lemma "typeof (fix (%fact. abs(%n. (app fact (n - Z))))) ?T"
 | 
| 21425 |     59 |   apply (prolog prog_Type)
 | 
|  |     60 |   done
 | 
|  |     61 | 
 | 
| 36319 |     62 | schematic_lemma "typeof (fix (%fact. abs(%n. cond (n eq Z) (S Z)
 | 
| 21425 |     63 |   (n * (app fact (n - (S Z))))))) ?T"
 | 
|  |     64 |   apply (prolog prog_Type)
 | 
|  |     65 |   done
 | 
|  |     66 | 
 | 
| 36319 |     67 | schematic_lemma "typeof (abs(%v. Z)) ?T" (*correct only solution (?A1 -> nat) *)
 | 
| 21425 |     68 |   apply (prolog prog_Type)
 | 
|  |     69 |   done
 | 
|  |     70 | 
 | 
| 36319 |     71 | schematic_lemma "typeof (abs(%v. Z)) ?T"
 | 
| 21425 |     72 |   apply (prolog bad1_typeof common_typeof) (* 1st result ok*)
 | 
|  |     73 |   done
 | 
|  |     74 | 
 | 
| 36319 |     75 | schematic_lemma "typeof (abs(%v. Z)) ?T"
 | 
| 21425 |     76 |   apply (prolog bad1_typeof common_typeof)
 | 
|  |     77 |   back (* 2nd result (?A1 -> ?A1) wrong *)
 | 
|  |     78 |   done
 | 
|  |     79 | 
 | 
| 36319 |     80 | schematic_lemma "typeof (abs(%v. abs(%v. app v v))) ?T"
 | 
| 21425 |     81 |   apply (prolog prog_Type)?  (*correctly fails*)
 | 
|  |     82 |   oops
 | 
|  |     83 | 
 | 
| 36319 |     84 | schematic_lemma "typeof (abs(%v. abs(%v. app v v))) ?T"
 | 
| 21425 |     85 |   apply (prolog bad2_typeof common_typeof) (* wrong result ((?A3 -> ?B3) -> ?A3 -> ?B3)*)
 | 
|  |     86 |   done
 | 
| 17311 |     87 | 
 | 
| 9015 |     88 | end
 |