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