src/HOL/Prolog/Type.thy
author blanchet
Fri, 27 May 2011 10:30:08 +0200
changeset 43016 42330f25142c
parent 36319 8feb2c4bef1a
child 51311 337cfc42c9c8
permissions -rw-r--r--
renamed "try" "try_methods"
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13208
965f95a3abd9 added the usual file headers
oheimb
parents: 12338
diff changeset
     1
(*  Title:    HOL/Prolog/Type.thy
965f95a3abd9 added the usual file headers
oheimb
parents: 12338
diff changeset
     2
    Author:   David von Oheimb (based on a lecture on Lambda Prolog by Nadathur)
965f95a3abd9 added the usual file headers
oheimb
parents: 12338
diff changeset
     3
*)
9015
8006e9009621 added HOL/Prolog
oheimb
parents:
diff changeset
     4
17311
5b1d47d920ce converted to Isar theory format;
wenzelm
parents: 14981
diff changeset
     5
header {* Type inference *}
9015
8006e9009621 added HOL/Prolog
oheimb
parents:
diff changeset
     6
17311
5b1d47d920ce converted to Isar theory format;
wenzelm
parents: 14981
diff changeset
     7
theory Type
5b1d47d920ce converted to Isar theory format;
wenzelm
parents: 14981
diff changeset
     8
imports Func
5b1d47d920ce converted to Isar theory format;
wenzelm
parents: 14981
diff changeset
     9
begin
9015
8006e9009621 added HOL/Prolog
oheimb
parents:
diff changeset
    10
17311
5b1d47d920ce converted to Isar theory format;
wenzelm
parents: 14981
diff changeset
    11
typedecl ty
9015
8006e9009621 added HOL/Prolog
oheimb
parents:
diff changeset
    12
17311
5b1d47d920ce converted to Isar theory format;
wenzelm
parents: 14981
diff changeset
    13
consts
5b1d47d920ce converted to Isar theory format;
wenzelm
parents: 14981
diff changeset
    14
  bool    :: ty
5b1d47d920ce converted to Isar theory format;
wenzelm
parents: 14981
diff changeset
    15
  nat     :: ty
21425
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents: 17311
diff changeset
    16
  arrow   :: "ty => ty => ty"       (infixr "->" 20)
17311
5b1d47d920ce converted to Isar theory format;
wenzelm
parents: 14981
diff changeset
    17
  typeof  :: "[tm, ty] => bool"
5b1d47d920ce converted to Isar theory format;
wenzelm
parents: 14981
diff changeset
    18
  anyterm :: tm
9015
8006e9009621 added HOL/Prolog
oheimb
parents:
diff changeset
    19
17311
5b1d47d920ce converted to Isar theory format;
wenzelm
parents: 14981
diff changeset
    20
axioms  common_typeof:   "
9015
8006e9009621 added HOL/Prolog
oheimb
parents:
diff changeset
    21
typeof (app M N) B       :- typeof M (A -> B) & typeof N A..
8006e9009621 added HOL/Prolog
oheimb
parents:
diff changeset
    22
8006e9009621 added HOL/Prolog
oheimb
parents:
diff changeset
    23
typeof (cond C L R) A :- typeof C bool & typeof L A & typeof R A..
8006e9009621 added HOL/Prolog
oheimb
parents:
diff changeset
    24
typeof (fix F)   A       :- (!x. typeof x A => typeof (F  x) A)..
8006e9009621 added HOL/Prolog
oheimb
parents:
diff changeset
    25
8006e9009621 added HOL/Prolog
oheimb
parents:
diff changeset
    26
typeof true  bool..
8006e9009621 added HOL/Prolog
oheimb
parents:
diff changeset
    27
typeof false bool..
8006e9009621 added HOL/Prolog
oheimb
parents:
diff changeset
    28
typeof (M and N) bool :- typeof M bool & typeof N bool..
8006e9009621 added HOL/Prolog
oheimb
parents:
diff changeset
    29
8006e9009621 added HOL/Prolog
oheimb
parents:
diff changeset
    30
typeof (M eq  N) bool :- typeof M T    & typeof N T   ..
8006e9009621 added HOL/Prolog
oheimb
parents:
diff changeset
    31
8006e9009621 added HOL/Prolog
oheimb
parents:
diff changeset
    32
typeof  Z    nat..
8006e9009621 added HOL/Prolog
oheimb
parents:
diff changeset
    33
typeof (S N) nat :- typeof N nat..
8006e9009621 added HOL/Prolog
oheimb
parents:
diff changeset
    34
typeof (M + N) nat :- typeof M nat & typeof N nat..
8006e9009621 added HOL/Prolog
oheimb
parents:
diff changeset
    35
typeof (M - N) nat :- typeof M nat & typeof N nat..
8006e9009621 added HOL/Prolog
oheimb
parents:
diff changeset
    36
typeof (M * N) nat :- typeof M nat & typeof N nat"
8006e9009621 added HOL/Prolog
oheimb
parents:
diff changeset
    37
17311
5b1d47d920ce converted to Isar theory format;
wenzelm
parents: 14981
diff changeset
    38
axioms good_typeof:     "
9015
8006e9009621 added HOL/Prolog
oheimb
parents:
diff changeset
    39
typeof (abs Bo) (A -> B) :- (!x. typeof x A => typeof (Bo x) B)"
8006e9009621 added HOL/Prolog
oheimb
parents:
diff changeset
    40
17311
5b1d47d920ce converted to Isar theory format;
wenzelm
parents: 14981
diff changeset
    41
axioms bad1_typeof:     "
9015
8006e9009621 added HOL/Prolog
oheimb
parents:
diff changeset
    42
typeof (abs Bo) (A -> B) :- (typeof varterm A => typeof (Bo varterm) B)"
8006e9009621 added HOL/Prolog
oheimb
parents:
diff changeset
    43
17311
5b1d47d920ce converted to Isar theory format;
wenzelm
parents: 14981
diff changeset
    44
axioms bad2_typeof:     "
9015
8006e9009621 added HOL/Prolog
oheimb
parents:
diff changeset
    45
typeof (abs Bo) (A -> B) :- (typeof anyterm A => typeof (Bo anyterm) B)"
8006e9009621 added HOL/Prolog
oheimb
parents:
diff changeset
    46
21425
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents: 17311
diff changeset
    47
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents: 17311
diff changeset
    48
lemmas prog_Type = prog_Func good_typeof common_typeof
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents: 17311
diff changeset
    49
36319
8feb2c4bef1a mark schematic statements explicitly;
wenzelm
parents: 34974
diff changeset
    50
schematic_lemma "typeof (abs(%n. abs(%m. abs(%p. p and (n eq m))))) ?T"
21425
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents: 17311
diff changeset
    51
  apply (prolog prog_Type)
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents: 17311
diff changeset
    52
  done
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents: 17311
diff changeset
    53
36319
8feb2c4bef1a mark schematic statements explicitly;
wenzelm
parents: 34974
diff changeset
    54
schematic_lemma "typeof (fix (%x. x)) ?T"
21425
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents: 17311
diff changeset
    55
  apply (prolog prog_Type)
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents: 17311
diff changeset
    56
  done
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents: 17311
diff changeset
    57
36319
8feb2c4bef1a mark schematic statements explicitly;
wenzelm
parents: 34974
diff changeset
    58
schematic_lemma "typeof (fix (%fact. abs(%n. (app fact (n - Z))))) ?T"
21425
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents: 17311
diff changeset
    59
  apply (prolog prog_Type)
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents: 17311
diff changeset
    60
  done
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents: 17311
diff changeset
    61
36319
8feb2c4bef1a mark schematic statements explicitly;
wenzelm
parents: 34974
diff changeset
    62
schematic_lemma "typeof (fix (%fact. abs(%n. cond (n eq Z) (S Z)
21425
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents: 17311
diff changeset
    63
  (n * (app fact (n - (S Z))))))) ?T"
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents: 17311
diff changeset
    64
  apply (prolog prog_Type)
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents: 17311
diff changeset
    65
  done
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents: 17311
diff changeset
    66
36319
8feb2c4bef1a mark schematic statements explicitly;
wenzelm
parents: 34974
diff changeset
    67
schematic_lemma "typeof (abs(%v. Z)) ?T" (*correct only solution (?A1 -> nat) *)
21425
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents: 17311
diff changeset
    68
  apply (prolog prog_Type)
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents: 17311
diff changeset
    69
  done
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents: 17311
diff changeset
    70
36319
8feb2c4bef1a mark schematic statements explicitly;
wenzelm
parents: 34974
diff changeset
    71
schematic_lemma "typeof (abs(%v. Z)) ?T"
21425
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents: 17311
diff changeset
    72
  apply (prolog bad1_typeof common_typeof) (* 1st result ok*)
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents: 17311
diff changeset
    73
  done
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents: 17311
diff changeset
    74
36319
8feb2c4bef1a mark schematic statements explicitly;
wenzelm
parents: 34974
diff changeset
    75
schematic_lemma "typeof (abs(%v. Z)) ?T"
21425
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents: 17311
diff changeset
    76
  apply (prolog bad1_typeof common_typeof)
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents: 17311
diff changeset
    77
  back (* 2nd result (?A1 -> ?A1) wrong *)
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents: 17311
diff changeset
    78
  done
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents: 17311
diff changeset
    79
36319
8feb2c4bef1a mark schematic statements explicitly;
wenzelm
parents: 34974
diff changeset
    80
schematic_lemma "typeof (abs(%v. abs(%v. app v v))) ?T"
21425
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents: 17311
diff changeset
    81
  apply (prolog prog_Type)?  (*correctly fails*)
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents: 17311
diff changeset
    82
  oops
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents: 17311
diff changeset
    83
36319
8feb2c4bef1a mark schematic statements explicitly;
wenzelm
parents: 34974
diff changeset
    84
schematic_lemma "typeof (abs(%v. abs(%v. app v v))) ?T"
21425
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents: 17311
diff changeset
    85
  apply (prolog bad2_typeof common_typeof) (* wrong result ((?A3 -> ?B3) -> ?A3 -> ?B3)*)
c11ab38b78a7 HOL-Prolog: converted legacy ML scripts;
wenzelm
parents: 17311
diff changeset
    86
  done
17311
5b1d47d920ce converted to Isar theory format;
wenzelm
parents: 14981
diff changeset
    87
9015
8006e9009621 added HOL/Prolog
oheimb
parents:
diff changeset
    88
end