# Theory Fix

Up to index of Isabelle/HOLCF

theory Fix
imports Cfun Cprod Adm
uses [Fix.ML]
begin

    (*  Title:      HOLCF/Fix.thy
        ID:         $Id: Fix.thy,v 1.36 2005/09/22 17:06:05 huffman Exp $
        Author:     Franz Regensburger

    Definitions for fixed point operator and admissibility.
    *)

    header {* Fixed point operator and admissibility *}

    theory Fix
    imports Cfun Cprod Adm
    begin

    defaultsort pcpo

    subsection {* Definitions *}

    consts
      iterate :: "nat => ('a -> 'a) => 'a => 'a"
      Ifix    :: "('a -> 'a) => 'a"
      "fix"   :: "('a -> 'a) -> 'a"
      admw    :: "('a => bool) => bool"

    primrec
      iterate_0:   "iterate 0 F x = x"
      iterate_Suc: "iterate (Suc n) F x  = F·(iterate n F x)"

    defs
      Ifix_def:      "Ifix ≡ λF. \<Squnion>i. iterate i F ⊥"
      fix_def:       "fix ≡ Λ F. Ifix F"

      admw_def:      "admw P ≡ ∀F. (∀n. P (iterate n F ⊥)) -->
                                P (\<Squnion>i. iterate i F ⊥)"

    subsection {* Binder syntax for @{term fix} *}

    syntax
      "@FIX" :: "('a => 'a) => 'a"  (binder "FIX " 10)
      "@FIXP" :: "[patterns, 'a] => 'a"  ("(3FIX <_>./ _)" [0, 10] 10)

    syntax (xsymbols)
      "FIX " :: "[idt, 'a] => 'a"  ("(3μ_./ _)" [0, 10] 10)
      "@FIXP" :: "[patterns, 'a] => 'a"  ("(3μ()<_>./ _)" [0, 10] 10)

    translations
      "FIX x. LAM y. t" == "fix·(LAM x y. t)"
      "FIX x. t" == "fix·(LAM x. t)"
      "FIX <xs>. t" == "fix·(LAM <xs>. t)"

    subsection {* Properties of @{term iterate} and @{term fix} *}

    text {* derive inductive properties of iterate from primitive recursion *}

    lemma iterate_Suc2: "iterate (Suc n) F x = iterate n F (F·x)"
    by (induct_tac n, auto)

    text {*
      The sequence of function iterations is a chain.
      This property is essential since monotonicity of iterate makes no sense.
    *}

    lemma chain_iterate2: "x \<sqsubseteq> F·x ==> chain (λi. iterate i F x)"
    by (rule chainI, induct_tac i, auto elim: monofun_cfun_arg)

    lemma chain_iterate: "chain (λi. iterate i F ⊥)"
    by (rule chain_iterate2 [OF minimal])

    text {*
      Kleene's fixed point theorems for continuous functions in pointed
      omega cpo's
    *}

    lemma Ifix_eq: "Ifix F = F·(Ifix F)"
    apply (unfold Ifix_def)
    apply (subst lub_range_shift [of _ 1, symmetric])
    apply (rule chain_iterate)
    apply (subst contlub_cfun_arg)
    apply (rule chain_iterate)
    apply simp
    done

    lemma Ifix_least: "F·x = x ==> Ifix F \<sqsubseteq> x"
    apply (unfold Ifix_def)
    apply (rule is_lub_thelub)
    apply (rule chain_iterate)
    apply (rule ub_rangeI)
    apply (induct_tac i)
    apply simp
    apply simp
    apply (erule subst)
    apply (erule monofun_cfun_arg)
    done

    text {* continuity of @{term iterate} *}

    lemma cont_iterate1: "cont (λF. iterate n F x)"
    by (induct_tac n, simp_all)

    lemma cont_iterate2: "cont (λx. iterate n F x)"
    by (induct_tac n, simp_all)

    lemma cont_iterate: "cont (iterate n)"
    by (rule cont_iterate1 [THEN cont2cont_lambda])

    lemmas monofun_iterate2 = cont_iterate2 [THEN cont2mono, standard]
    lemmas contlub_iterate2 = cont_iterate2 [THEN cont2contlub, standard]

    text {* continuity of @{term Ifix} *}

    lemma cont_Ifix: "cont Ifix"
    apply (unfold Ifix_def)
    apply (rule cont2cont_lub)
    apply (rule ch2ch_fun_rev)
    apply (rule chain_iterate)
    apply (rule cont_iterate1)
    done

    text {* propagate properties of @{term Ifix} to its continuous counterpart *}

    lemma fix_eq: "fix·F = F·(fix·F)"
    apply (unfold fix_def)
    apply (simp add: cont_Ifix)
    apply (rule Ifix_eq)
    done

    lemma fix_least: "F·x = x ==> fix·F \<sqsubseteq> x"
    apply (unfold fix_def)
    apply (simp add: cont_Ifix)
    apply (erule Ifix_least)
    done

    lemma fix_eqI: "[|F·x = x; ∀z. F·z = z --> x \<sqsubseteq> z|] ==> x = fix·F"
    apply (rule antisym_less)
    apply (erule allE)
    apply (erule mp)
    apply (rule fix_eq [symmetric])
    apply (erule fix_least)
    done

    lemma fix_eq2: "f ≡ fix·F ==> f = F·f"
    by (simp add: fix_eq [symmetric])

    lemma fix_eq3: "f ≡ fix·F ==> f·x = F·f·x"
    by (erule fix_eq2 [THEN cfun_fun_cong])

    lemma fix_eq4: "f = fix·F ==> f = F·f"
    apply (erule ssubst)
    apply (rule fix_eq)
    done

    lemma fix_eq5: "f = fix·F ==> f·x = F·f·x"
    by (erule fix_eq4 [THEN cfun_fun_cong])

    text {* direct connection between @{term fix} and iteration without @{term Ifix} *}

    lemma fix_def2: "fix·F = (\<Squnion>i. iterate i F ⊥)"
    apply (unfold fix_def)
    apply (simp add: cont_Ifix)
    apply (simp add: Ifix_def)
    done

    text {* strictness of @{term fix} *}

    lemma fix_defined_iff: "(fix·F = ⊥) = (F·⊥ = ⊥)"
    apply (rule iffI)
    apply (erule subst)
    apply (rule fix_eq [symmetric])
    apply (erule fix_least [THEN UU_I])
    done

    lemma fix_strict: "F·⊥ = ⊥ ==> fix·F = ⊥"
    by (simp add: fix_defined_iff)

    lemma fix_defined: "F·⊥ ≠ ⊥ ==> fix·F ≠ ⊥"
    by (simp add: fix_defined_iff)

    text {* @{term fix} applied to identity and constant functions *}

    lemma fix_id: "(μ x. x) = ⊥"
    by (simp add: fix_strict)

    lemma fix_const: "(μ x. c) = c"
    by (rule fix_eq [THEN trans], simp)

    subsection {* Admissibility and fixed point induction *}

    text {* an admissible formula is also weak admissible *}

    lemma adm_impl_admw: "adm P ==> admw P"
    apply (unfold admw_def)
    apply (intro strip)
    apply (erule admD)
    apply (rule chain_iterate)
    apply assumption
    done

    text {* some lemmata for functions with flat/chfin domain/range types *}

    lemma adm_chfindom: "adm (λ(u::'a::cpo -> 'b::chfin). P(u·s))"
    apply (unfold adm_def)
    apply (intro strip)
    apply (drule chfin_Rep_CFunR)
    apply (erule_tac x = "s" in allE)
    apply clarsimp
    done

    (* adm_flat not needed any more, since it is a special case of adm_chfindom *)

    text {* fixed point induction *}

    lemma fix_ind: "[|adm P; P ⊥; !!x. P x ==> P (F·x)|] ==> P (fix·F)"
    apply (subst fix_def2)
    apply (erule admD)
    apply (rule chain_iterate)
    apply (rule allI)
    apply (induct_tac "i")
    apply simp
    apply simp
    done

    lemma def_fix_ind:
      "[|f ≡ fix·F; adm P; P ⊥; !!x. P x ==> P (F·x)|] ==> P f"
    apply simp
    apply (erule fix_ind)
    apply assumption
    apply fast
    done

    text {* computational induction for weak admissible formulae *}

    lemma wfix_ind: "[|admw P; ∀n. P (iterate n F ⊥)|] ==> P (fix·F)"
    by (simp add: fix_def2 admw_def)

    lemma def_wfix_ind:
      "[|f ≡ fix·F; admw P; ∀n. P (iterate n F ⊥)|] ==> P f"
    by (simp, rule wfix_ind)

    end

### Definitions

### Binder syntax for @{term fix}

### Properties of @{term iterate} and @{term fix}

lemma iterate_Suc2:

      iterate (Suc n) F x = iterate n F (F·x)

lemma chain_iterate2:

      x << F·x ==> chain (%i. iterate i F x)

lemma chain_iterate:

      chain (%i. iterate i F UU)

lemma Ifix_eq:

      Ifix F = F·(Ifix F)

lemma Ifix_least:

      F·x = x ==> Ifix F << x

lemma cont_iterate1:

      cont (%F. iterate n F x)

lemma cont_iterate2:

      cont (iterate n F)

lemma cont_iterate:

      cont (iterate n)

lemmas monofun_iterate2:

      monofun (iterate n F)

lemmas monofun_iterate2:

      monofun (iterate n F)

lemmas contlub_iterate2:

      contlub (iterate n F)

lemmas contlub_iterate2:

      contlub (iterate n F)

lemma cont_Ifix:

      cont Ifix

lemma fix_eq:

      fix·F = F·(fix·F)

lemma fix_least:

      F·x = x ==> fix·F << x

lemma fix_eqI:

      [| F·x = x; ∀z. F·z = z --> x << z |] ==> x = fix·F

lemma fix_eq2:

      f == fix·F ==> f = F·f

lemma fix_eq3:

      f == fix·F ==> f·x = F·f·x

lemma fix_eq4:

      f = fix·F ==> f = F·f

lemma fix_eq5:

      f = fix·F ==> f·x = F·f·x

lemma fix_def2:

      fix·F = (LUB i. iterate i F UU)

lemma fix_defined_iff:

      (fix·F = UU) = (F·UU = UU)

lemma fix_strict:

      F·UU = UU ==> fix·F = UU

lemma fix_defined:

      F·UU ≠ UU ==> fix·F ≠ UU

lemma fix_id:

      (FIX x. x) = UU

lemma fix_const:

      (FIX x. c) = c

### Admissibility and fixed point induction

lemma adm_impl_admw:

      adm P ==> admw P

lemma adm_chfindom:

      adm (%u. P (u·s))

lemma fix_ind:

      [| adm P; P UU; !!x. P x ==> P (F·x) |] ==> P (fix·F)

lemma def_fix_ind:

      [| f == fix·F; adm P; P UU; !!x. P x ==> P (F·x) |] ==> P f

lemma wfix_ind:

      [| admw P; ∀n. P (iterate n F UU) |] ==> P (fix·F)

lemma def_wfix_ind:

      [| f == fix·F; admw P; ∀n. P (iterate n F UU) |] ==> P f