From a245a3e2194b4d5a6e7fbac8fa10598e96307e57 Mon Sep 17 00:00:00 2001 From: Josh Chen Date: Wed, 30 Jan 2019 15:43:47 +0100 Subject: Begin refactor --- HoTT_Base.thy | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/HoTT_Base.thy b/HoTT_Base.thy index 9e8fee7..93ef70e 100644 --- a/HoTT_Base.thy +++ b/HoTT_Base.thy @@ -1,9 +1,7 @@ (* -Title: HoTT_Base.thy -Author: Joshua Chen -Date: 2018 +Homotopy Type Theory -Basic setup of a homotopy type theory object logic with a cumulative Russell-style universe hierarchy. +Foundational setup and definitions *) theory HoTT_Base @@ -11,13 +9,12 @@ imports Pure begin - section \Basic setup\ declare[[eta_contract=false]] -typedecl t \ \Type of object types and terms\ -typedecl ord \ \Type of meta-level numerals\ +typedecl t \ \Type of object-logic terms (which includes the types)\ +typedecl ord \ \Type of meta-numerals\ axiomatization O :: ord and @@ -34,12 +31,10 @@ declare leq_min [intro!] lt_trans [intro] - -section \Judgment\ +section \Typing judgment\ judgment hastype :: "[t, t] \ prop" ("(3_:/ _)") - section \Universes\ axiomatization @@ -50,8 +45,8 @@ where U_cumulative': "\A: U i; i \ j\ \ A: U j" text \ -Using method @{method rule} with @{thm U_cumulative} is unsafe, if applied blindly it will typically lead to non-termination. -One should instead use @{method elim}, or instantiate @{thm U_cumulative} suitably. +Using method @{method rule} with @{thm U_cumulative} is unsafe, if applied blindly it will very easily lead to non-termination. +Instead use @{method elim}, or instantiate @{thm U_cumulative} suitably. @{thm U_cumulative'} is an alternative rule used by the method \cumulativity\ in @{file HoTT_Methods.thy}. \ -- cgit v1.2.3 From 843d53d64983593d765a203605cd2aab00ed8361 Mon Sep 17 00:00:00 2001 From: Josh Chen Date: Wed, 30 Jan 2019 16:09:50 +0100 Subject: Remove "constrained" notation for type families. Clean and document. --- HoTT_Base.thy | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/HoTT_Base.thy b/HoTT_Base.thy index 93ef70e..99ab5ce 100644 --- a/HoTT_Base.thy +++ b/HoTT_Base.thy @@ -1,8 +1,15 @@ -(* -Homotopy Type Theory +(******** +Isabelle/HoTT: Foundational definitions and notation +Jan 2019 -Foundational setup and definitions -*) +This file is the starting point, and contains the base setup and definitions of the object logic. +Among other things, it defines: + +* Meta-numerals to index the universe hierarchy, and related axioms. +* The universe hierarchy and its governing rules. +* Named theorems for later use by proof methods. + +********) theory HoTT_Base imports Pure @@ -11,7 +18,7 @@ begin section \Basic setup\ -declare[[eta_contract=false]] +declare[[eta_contract=false]] (* I'm not actually sure this does anything... *) typedecl t \ \Type of object-logic terms (which includes the types)\ typedecl ord \ \Type of meta-numerals\ @@ -51,26 +58,13 @@ Instead use @{method elim}, or instantiate @{thm U_cumulative} suitably. @{thm U_cumulative'} is an alternative rule used by the method \cumulativity\ in @{file HoTT_Methods.thy}. \ - -section \Type families\ - -abbreviation (input) constrained :: "[t \ t, t, t] \ prop" ("(1_:/ _ \ _)") - where "f: A \ B \ (\x. x : A \ f x: B)" - -text \ -The abbreviation @{abbrev constrained} is used to define type families, which are constrained expressions @{term "P: A \ B"}, where @{term "A::t"} and @{term "B::t"} are small types. -\ - -type_synonym tf = "t \ t" \ \Type of type families\ - - section \Named theorems\ named_theorems comp named_theorems form text \ -Declare named theorems to be used by proof methods defined in @{file HoTT_Methods.thy}. +The named theorems above will be used by proof methods defined in @{file HoTT_Methods.thy}. @{attribute comp} declares computation rules, which are used by the \compute\ method, and may also be passed to invocations of the method \subst\ to perform equational rewriting. @@ -80,5 +74,4 @@ These are mainly used by the \cumulativity\ method, which lifts typ (* Todo: Set up the Simplifier! *) - end -- cgit v1.2.3 From def15c3748599d5c05cae164653e85ea03da6be6 Mon Sep 17 00:00:00 2001 From: Josh Chen Date: Wed, 30 Jan 2019 16:38:38 +0100 Subject: Clean and comment methods file --- HoTT_Methods.thy | 75 +++++++++++++++++++++++++------------------------------- 1 file changed, 34 insertions(+), 41 deletions(-) diff --git a/HoTT_Methods.thy b/HoTT_Methods.thy index f0cee6c..99f3446 100644 --- a/HoTT_Methods.thy +++ b/HoTT_Methods.thy @@ -1,80 +1,73 @@ -(* -Title: HoTT_Methods.thy -Author: Joshua Chen -Date: 2018 +(******** +Isabelle/HoTT: Proof methods +Jan 2019 -Method setup for the HoTT logic. -*) +********) theory HoTT_Methods imports HoTT_Base "HOL-Eisbach.Eisbach" "HOL-Eisbach.Eisbach_Tools" begin +section \Substitution and simplification\ -section \Handling universes\ +ML_file "~~/src/Tools/misc_legacy.ML" +ML_file "~~/src/Tools/IsaPlanner/isand.ML" +ML_file "~~/src/Tools/IsaPlanner/rw_inst.ML" +ML_file "~~/src/Tools/IsaPlanner/zipper.ML" +ML_file "~~/src/Tools/eqsubst.ML" +\ \Import the @{method subst} method, used for substituting definitional equalities.\ -method provelt = (rule lt_Suc | (rule lt_trans, (rule lt_Suc)+)+) +text \ +Method @{theory_text compute} performs single-step simplifications, using any rules declared @{attribute comp} in the context. +Premises of the rule(s) applied are added as new subgoals. +\ -method hierarchy = (rule U_hierarchy, provelt) +method compute declares comp = (subst comp) -method cumulativity declares form = ( - ((elim U_cumulative' | (rule U_cumulative', rule form)), rule leq_min) | - ((elim U_cumulative | (rule U_cumulative, rule form)), provelt) -) +section \Handling universes\ text \ -Methods @{method provelt}, @{method hierarchy}, and @{method cumulativity} prove statements of the form +Methods @{theory_text provelt}, @{theory_text hierarchy}, and @{theory_text cumulativity} prove propositions of the form + \<^item> \n < (Suc (... (Suc n) ...))\, \<^item> \U i: U (Suc (... (Suc i) ...))\, and -\<^item> @{prop "A: U i \ A: U j"}, where @{prop "i \ j"} -respectively. -\ - - -section \Deriving typing judgments\ +\<^item> @{prop "A: U i \ A: U j"}, where @{prop "i \ j"}, -method routine uses add = (assumption | rule add | rule)+ - -text \ -The method @{method routine} proves type judgments @{prop "a : A"} using the rules declared @{attribute intro} in the respective theory files, as well as additional provided lemmas passed using the modifier \add\. +respectively. \ +method provelt = (rule lt_Suc | (rule lt_trans, (rule lt_Suc)+)+) -section \Substitution and simplification\ +method hierarchy = (rule U_hierarchy, provelt) -ML_file "~~/src/Tools/misc_legacy.ML" -ML_file "~~/src/Tools/IsaPlanner/isand.ML" -ML_file "~~/src/Tools/IsaPlanner/rw_inst.ML" -ML_file "~~/src/Tools/IsaPlanner/zipper.ML" -ML_file "~~/src/Tools/eqsubst.ML" +method cumulativity declares form = ( + ((elim U_cumulative' | (rule U_cumulative', rule form)), rule leq_min) | + ((elim U_cumulative | (rule U_cumulative, rule form)), provelt) +) -\ \Import the @{method subst} method, used for substituting definitional equalities.\ +section \Deriving typing judgments\ -method compute declares comp = (subst comp) +method routine uses add = (assumption | rule add | rule)+ text \ -Method @{method compute} performs single-step simplifications, using any rules declared @{attribute comp} in the context. -Premises of the rule(s) applied are added as new subgoals. +The method @{method routine} proves typing judgments @{prop "a: A"} using the rules declared @{attribute intro} in the respective theory files, as well as any additional lemmas provided with the modifier @{theory_text add}. \ - section \Derivation search\ text \ -Combine @{method routine} and @{method compute} to search for derivations of judgments. -Also handle universes using @{method hierarchy} and @{method cumulativity}. +The method @{theory_text derive} combines @{method routine} and @{method compute} to search for derivations of judgments. +It also handles universes using @{method hierarchy} and @{method cumulativity}. \ method derive uses lems = (routine add: lems | compute comp: lems | cumulativity form: lems | hierarchy)+ - section \Induction\ text \ -Placeholder section for the automation of induction, i.e. using the elimination rules. -At the moment one must directly apply them with \rule X_elim\. +Placeholder section for the automation of induction, i.e. using the type elimination rules. +At the moment one must directly apply them with @{method rule} or @{method elim}. \ - end -- cgit v1.2.3 From 07d312b312c3058551353bcf403a1dc7c7c83311 Mon Sep 17 00:00:00 2001 From: Josh Chen Date: Thu, 31 Jan 2019 15:51:24 +0100 Subject: Text remarks --- HoTT_Base.thy | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/HoTT_Base.thy b/HoTT_Base.thy index 99ab5ce..bb47a74 100644 --- a/HoTT_Base.thy +++ b/HoTT_Base.thy @@ -2,7 +2,7 @@ Isabelle/HoTT: Foundational definitions and notation Jan 2019 -This file is the starting point, and contains the base setup and definitions of the object logic. +This file is the starting point of the Isabelle/HoTT object logic, and contains the basic setup and definitions. Among other things, it defines: * Meta-numerals to index the universe hierarchy, and related axioms. @@ -18,7 +18,7 @@ begin section \Basic setup\ -declare[[eta_contract=false]] (* I'm not actually sure this does anything... *) +declare[[eta_contract=false]] (* Does this option actually do anything? *) typedecl t \ \Type of object-logic terms (which includes the types)\ typedecl ord \ \Type of meta-numerals\ @@ -55,9 +55,13 @@ text \ Using method @{method rule} with @{thm U_cumulative} is unsafe, if applied blindly it will very easily lead to non-termination. Instead use @{method elim}, or instantiate @{thm U_cumulative} suitably. -@{thm U_cumulative'} is an alternative rule used by the method \cumulativity\ in @{file HoTT_Methods.thy}. +@{thm U_cumulative'} is an alternative rule used by the method @{theory_text cumulativity} in @{file HoTT_Methods.thy}. \ +section \Type families\ + +abbrev (input) + section \Named theorems\ named_theorems comp -- cgit v1.2.3 From 7a7e27f4a1efd69e9ab43b95d3c7ead61a743e55 Mon Sep 17 00:00:00 2001 From: Josh Chen Date: Mon, 4 Feb 2019 11:53:47 +0100 Subject: 1. Change syntax to rely less on unicode/control symbols. 2. Begin work on object-level type inference and input syntax help. --- HoTT_Base.thy | 65 ++++++++++++++++++++++++++++++------- Prod.thy | 101 +++++++++++++++++++++++++++++++++++----------------------- ROOT | 3 +- 3 files changed, 116 insertions(+), 53 deletions(-) diff --git a/HoTT_Base.thy b/HoTT_Base.thy index bb47a74..a5280ce 100644 --- a/HoTT_Base.thy +++ b/HoTT_Base.thy @@ -5,7 +5,7 @@ Jan 2019 This file is the starting point of the Isabelle/HoTT object logic, and contains the basic setup and definitions. Among other things, it defines: -* Meta-numerals to index the universe hierarchy, and related axioms. +* Containers for storing type information of object-level terms. * The universe hierarchy and its governing rules. * Named theorems for later use by proof methods. @@ -16,12 +16,56 @@ imports Pure begin -section \Basic setup\ +section \Terms and typing\ -declare[[eta_contract=false]] (* Does this option actually do anything? *) +typedecl t \ \Type of object-logic terms (which includes the types)\ -typedecl t \ \Type of object-logic terms (which includes the types)\ -typedecl ord \ \Type of meta-numerals\ +consts has_type :: "[t, t] \ prop" \ \The judgment coercion\ + +text \ +We create a dedicated container in which to store object-level typing information whenever a typing judgment that does not depend on assumptions is proved. +\ + +ML \ +signature TYPINGS = +sig + val get: term -> term +end + +structure Typings: TYPINGS = +struct + +structure Typing_Data = Theory_Data +( + type T = term Symtab.table + val empty = Symtab.empty + val extend = I + + fun merge (tbl, tbl') = + let + val key_vals = map (Symtab.lookup_key tbl') (Symtab.keys tbl') + val updates = map (fn SOME kv => Symtab.update kv) key_vals + fun f u t = u t + in fold f updates tbl end +) + +fun get tm = + case Symtab.lookup (Typing_Data.get @{theory}) (Syntax.string_of_term @{context} tm) of + SOME tm' => tm' + | NONE => raise Fail "No typing information available" + +end +\ + +syntax "_has_type" :: "[t, t] \ prop" ("3(_:/ _)") + +parse_translation \ + +\ + +section \Universes\ + +typedecl ord \ \Type of meta-numerals\ axiomatization O :: ord and @@ -38,12 +82,6 @@ declare leq_min [intro!] lt_trans [intro] -section \Typing judgment\ - -judgment hastype :: "[t, t] \ prop" ("(3_:/ _)") - -section \Universes\ - axiomatization U :: "ord \ t" where @@ -60,7 +98,10 @@ Instead use @{method elim}, or instantiate @{thm U_cumulative} suitably. section \Type families\ -abbrev (input) +abbreviation (input) constraint :: "[t \ t, t, t] \ prop" ("(1_:/ _ \ _)") +where "f: A \ B \ (\x. x: A \ f x: B)" + +text \We use the notation @{prop "B: A \ U i"} to abbreviate type families.\ section \Named theorems\ diff --git a/Prod.thy b/Prod.thy index 8d840bd..68c9405 100644 --- a/Prod.thy +++ b/Prod.thy @@ -1,91 +1,112 @@ -(* -Title: Prod.thy -Author: Joshua Chen -Date: 2018 +(******** +Isabelle/HoTT: Dependent product (dependent function) +Jan 2019 -Dependent product type -*) +********) theory Prod imports HoTT_Base HoTT_Methods begin - -section \Basic definitions\ +section \Basic type definitions\ axiomatization - Prod :: "[t, tf] \ t" and - lambda :: "(t \ t) \ t" (binder "\<^bold>\" 30) and - appl :: "[t, t] \ t" ("(1_`_)" [120, 121] 120) \ \Application binds tighter than abstraction.\ + Prod :: "[t, t \ t] \ t" and + lam :: "[t, t \ t] \ t" and + app :: "[t, t] \ t" ("(1_ ` _)" [120, 121] 120) + \ \Application should bind tighter than abstraction.\ syntax - "_prod" :: "[idt, t, t] \ t" ("(3\_: _./ _)" 30) - "_prod_ascii" :: "[idt, t, t] \ t" ("(3II _: _./ _)" 30) - -text \The translations below bind the variable @{term x} in the expressions @{term B} and @{term b}.\ - + "_Prod" :: "[idt, t, t] \ t" ("(3TT '(_: _')./ _)" 30) + "_Prod'" :: "[idt, t, t] \ t" ("(3TT _: _./ _)" 30) + "_lam" :: "[idt, t, t] \ t" ("(3,\\ '(_: _')./ _)" 30) + "_lam'" :: "[idt, t, t] \ t" ("(3,\\ _: _./ _)" 30) translations - "\x:A. B" \ "CONST Prod A (\x. B)" - "II x:A. B" \ "CONST Prod A (\x. B)" + "TT(x: A). B" \ "(CONST Prod) A (\x. B)" + "TT x: A. B" \ "(CONST Prod) A (\x. B)" + ",\\(x: A). b" \ "(CONST lam) A (\x. b)" + ",\\x: A. b" \ "(CONST lam) A (\x. b)" + +text \ +The syntax translations above bind the variable @{term x} in the expressions @{term B} and @{term b}. +\ -text \Non-dependent functions are a special case.\ +text \Non-dependent functions are a special case:\ abbreviation Fun :: "[t, t] \ t" (infixr "\" 40) - where "A \ B \ \_: A. B" +where "A \ B \ TT(_: A). B" axiomatization where \ \Type rules\ - Prod_form: "\A: U i; B: A \ U i\ \ \x:A. B x: U i" and + Prod_form: "\A: U i; \x. x: A \ B x: U i\ \ TT x: A. B x: U i" and - Prod_intro: "\\x. x: A \ b x: B x; A: U i\ \ \<^bold>\x. b x: \x:A. B x" and + Prod_intro: "\A: U i; \x. x: A \ b x: B x\ \ ,\\x: A. b x: TT x: A. B x" and - Prod_elim: "\f: \x:A. B x; a: A\ \ f`a: B a" and + Prod_elim: "\f: TT x: A. B x; a: A\ \ f`a: B a" and - Prod_comp: "\a: A; \x. x: A \ b x: B x\ \ (\<^bold>\x. b x)`a \ b a" and + Prod_cmp: "\\x. x: A \ b x: B x; a: A\ \ (,\\x: A. b x)`a \ b a" and - Prod_uniq: "f: \x:A. B x \ \<^bold>\x. f`x \ f" and + Prod_uniq: "f: TT x: A. B x \ ,\\x: A. f`x \ f" and \ \Congruence rules\ - Prod_form_eq: "\A: U i; B: A \ U i; C: A \ U i; \x. x: A \ B x \ C x\ \ \x:A. B x \ \x:A. C x" and + Prod_form_eq: "\A: U i; B: A \ U i; C: A \ U i; \x. x: A \ B x \ C x\ + \ TT x: A. B x \ TT x: A. C x" and - Prod_intro_eq: "\\x. x: A \ b x \ c x; A: U i\ \ \<^bold>\x. b x \ \<^bold>\x. c x" + Prod_intro_eq: "\\x. x: A \ b x \ c x; A: U i\ \ ,\\x: A. b x \ ,\\x: A. c x" text \ The Pure rules for \\\ only let us judge strict syntactic equality of object lambda expressions. -The actual definitional equality rule is @{thm Prod_intro_eq}. +The actual definitional equality rule in the type theory is @{thm Prod_intro_eq}. Note that this is a separate rule from function extensionality. - -Note that the bold lambda symbol \\<^bold>\\ used for dependent functions clashes with the proof term syntax (cf. \
2.5.2 of the Isabelle/Isar Implementation). \ lemmas Prod_form [form] lemmas Prod_routine [intro] = Prod_form Prod_intro Prod_elim -lemmas Prod_comps [comp] = Prod_comp Prod_uniq - +lemmas Prod_comp [comp] = Prod_cmp Prod_uniq section \Additional definitions\ -definition compose :: "[t, t] \ t" (infixr "o" 110) where "g o f \ \<^bold>\x. g`(f`x)" - -syntax "_compose" :: "[t, t] \ t" (infixr "\" 110) -translations "g \ f" \ "g o f" +definition compose :: "[t, t, t] \ t" +where "compose A g f \ ,\\x: A. g`(f`x)" declare compose_def [comp] +syntax "_compose" :: "[t, t] \ t" (infixr "o" 110) + +parse_translation \ +let fun compose_tr ctxt tms = + let + val f::g::_ = tms; + fun domain f = @{term "A :: t"} + in + @{const compose} $ (domain f) $ f $ g + end +in + [("_compose", compose_tr)] +end +\ + +ML_val \ +Proof_Context.get_thms @{context} "compose_def" +\ + lemma compose_assoc: - assumes "A: U i" and "f: A \ B" "g: B \ C" "h: \x:C. D x" - shows "(h \ g) \ f \ h \ (g \ f)" -by (derive lems: assms Prod_intro_eq) + assumes "A: U i" "f: A \ B" "g: B \ C" "h: TT x: C. D x" + shows "(h o g) o f \ h o (g o f)" +ML_prf \ +@{thms assms}; +\ +(* (derive lems: assms Prod_intro_eq) *) lemma compose_comp: assumes "A: U i" and "\x. x: A \ b x: B" and "\x. x: B \ c x: C x" shows "(\<^bold>\x. c x) \ (\<^bold>\x. b x) \ \<^bold>\x. c (b x)" by (derive lems: assms Prod_intro_eq) -abbreviation id :: t where "id \ \<^bold>\x. x" \ \Polymorphic identity function\ +abbreviation id :: "t \ t" where "id A \ ,\\x: A. x" end diff --git a/ROOT b/ROOT index cecdd32..b05d022 100644 --- a/ROOT +++ b/ROOT @@ -6,7 +6,8 @@ session HoTT = Pure + hierarchy à la Russell. Follows the development of the theory in - The Univalent Foundations Program, Homotopy Type Theory: Univalent Foundations of Mathematics, + The Univalent Foundations Program, + Homotopy Type Theory: Univalent Foundations of Mathematics, Institute for Advanced Study, (2013). Available online at https://homotopytypetheory.org/book. -- cgit v1.2.3 From d70da13b7fd36509c1d843d139a0b99c6acb8cc6 Mon Sep 17 00:00:00 2001 From: Josh Chen Date: Tue, 5 Feb 2019 18:15:58 +0100 Subject: Type inference setup begun - first use-case for function composition. --- HoTT_Base.thy | 57 ++++++------------------------------- Prod.thy | 31 +++++++++++--------- typing.ML | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 118 insertions(+), 61 deletions(-) create mode 100644 typing.ML diff --git a/HoTT_Base.thy b/HoTT_Base.thy index a5280ce..aa13815 100644 --- a/HoTT_Base.thy +++ b/HoTT_Base.thy @@ -3,11 +3,11 @@ Isabelle/HoTT: Foundational definitions and notation Jan 2019 This file is the starting point of the Isabelle/HoTT object logic, and contains the basic setup and definitions. -Among other things, it defines: +Among other things, it: -* Containers for storing type information of object-level terms. -* The universe hierarchy and its governing rules. -* Named theorems for later use by proof methods. +* Sets up object-level type inference functionality (via typing.ML). +* Defines the universe hierarchy and its governing rules. +* Defines named theorems for later use by proof methods. ********) @@ -20,48 +20,9 @@ section \Terms and typing\ typedecl t \ \Type of object-logic terms (which includes the types)\ -consts has_type :: "[t, t] \ prop" \ \The judgment coercion\ +judgment has_type :: "[t, t] \ prop" ("(3_:/ _)") -text \ -We create a dedicated container in which to store object-level typing information whenever a typing judgment that does not depend on assumptions is proved. -\ - -ML \ -signature TYPINGS = -sig - val get: term -> term -end - -structure Typings: TYPINGS = -struct - -structure Typing_Data = Theory_Data -( - type T = term Symtab.table - val empty = Symtab.empty - val extend = I - - fun merge (tbl, tbl') = - let - val key_vals = map (Symtab.lookup_key tbl') (Symtab.keys tbl') - val updates = map (fn SOME kv => Symtab.update kv) key_vals - fun f u t = u t - in fold f updates tbl end -) - -fun get tm = - case Symtab.lookup (Typing_Data.get @{theory}) (Syntax.string_of_term @{context} tm) of - SOME tm' => tm' - | NONE => raise Fail "No typing information available" - -end -\ - -syntax "_has_type" :: "[t, t] \ prop" ("3(_:/ _)") - -parse_translation \ - -\ +ML_file "typing.ML" section \Universes\ @@ -85,13 +46,13 @@ declare axiomatization U :: "ord \ t" where - U_hierarchy: "i < j \ U i: U j" and + U_hierarchy: "i < j \ U i: U j" and U_cumulative: "\A: U i; i < j\ \ A: U j" and U_cumulative': "\A: U i; i \ j\ \ A: U j" text \ -Using method @{method rule} with @{thm U_cumulative} is unsafe, if applied blindly it will very easily lead to non-termination. -Instead use @{method elim}, or instantiate @{thm U_cumulative} suitably. +Using method @{method rule} with @{thm U_cumulative} and @{thm U_cumulative'} is unsafe: if applied blindly it will very easily lead to non-termination. +Instead use @{method elim}, or instantiate the rules suitably. @{thm U_cumulative'} is an alternative rule used by the method @{theory_text cumulativity} in @{file HoTT_Methods.thy}. \ diff --git a/Prod.thy b/Prod.thy index 68c9405..4c572ce 100644 --- a/Prod.thy +++ b/Prod.thy @@ -79,32 +79,37 @@ syntax "_compose" :: "[t, t] \ t" (infixr "o" 110) parse_translation \ let fun compose_tr ctxt tms = let - val f::g::_ = tms; - fun domain f = @{term "A :: t"} + val g :: f :: _ = tms |> map (Typing.tm_of_ptm ctxt) + val dom = + case Typing.get_typing f (Typing.typing_assms ctxt) of + SOME (Const ("Prod.Prod", _) $ T $ _) => T + | SOME _ => Exn.error "Can't compose with a non-function" + | NONE => Exn.error "Cannot infer domain of composition—please state this explicitly" in - @{const compose} $ (domain f) $ f $ g + @{const compose} $ dom $ g $ f end in [("_compose", compose_tr)] end \ -ML_val \ -Proof_Context.get_thms @{context} "compose_def" -\ - lemma compose_assoc: - assumes "A: U i" "f: A \ B" "g: B \ C" "h: TT x: C. D x" + assumes "A: U i" "f: A \ B" "g: B \ C" "h: TT x: C. D x" "(,\\(x: A). b x): TT x: A. D x" shows "(h o g) o f \ h o (g o f)" -ML_prf \ -@{thms assms}; -\ (* (derive lems: assms Prod_intro_eq) *) +sorry lemma compose_comp: assumes "A: U i" and "\x. x: A \ b x: B" and "\x. x: B \ c x: C x" - shows "(\<^bold>\x. c x) \ (\<^bold>\x. b x) \ \<^bold>\x. c (b x)" -by (derive lems: assms Prod_intro_eq) + shows "(,\\x: B. c x) o (,\\x: A. b x) \ ,\\x: A. c (b x)" +ML_prf \ +Assumption.all_assms_of @{context}; +nth (Assumption.all_assms_of @{context}) 1 |> Thm.term_of; +Assumption.all_prems_of @{context}; +nth (Assumption.all_prems_of @{context}) 0 |> Thm.prop_of; +typing_assms @{context} +\ +(*by (derive lems: assms Prod_intro_eq)*) abbreviation id :: "t \ t" where "id A \ ,\\x: A. x" diff --git a/typing.ML b/typing.ML new file mode 100644 index 0000000..bfe8409 --- /dev/null +++ b/typing.ML @@ -0,0 +1,91 @@ +(******** +Isabelle/HoTT: typing.ML +Feb 2019 + +Functionality for object-level type inference. + +********) + +signature TYPING = +sig + type jmt = term + val term_of_jmt: jmt -> term + val type_of_jmt: jmt -> term + val typing_assms: Proof.context -> jmt list + val get_typing: term -> jmt list -> term option + val get_global_typing: theory -> Proof.context -> term -> term option + val tm_of_ptm: Proof.context -> term -> term +end + +structure Typing: TYPING = +struct + +type jmt = term + +(* Functions to extract a and A from propositions "a: A" *) +fun term_of_jmt (@{const has_type} $ t $ _) = t + | term_of_jmt _ = Exn.error "Not a typing judgment" + +fun type_of_jmt (@{const has_type} $ _ $ T) = T + | type_of_jmt _ = Exn.error "Not a typing judgment" + +(* Get the typing assumptions in the current context *) +fun typing_assms ctxt = + let + fun is_typing_jmt (@{const has_type} $ _ $ _) = true + | is_typing_jmt _ = false + in + Assumption.all_prems_of ctxt |> map Thm.prop_of |> filter is_typing_jmt + end + +(* Search for a term typing in a list of judgments*) +fun get_typing tm jmts = + let val find_type = + fn jmt => if Term.aconv_untyped (tm, term_of_jmt jmt) then SOME (type_of_jmt jmt) else NONE + in get_first find_type jmts end + +(* Private storage space for global typing data *) +structure Typing_Data = Theory_Data +( + type T = term Symtab.table + val empty = Symtab.empty + val extend = I + + fun merge (tbl, tbl') = + let + val key_vals = map (Symtab.lookup_key tbl') (Symtab.keys tbl') + val updates = map (fn SOME kv => Symtab.update kv) key_vals + fun f u t = u t + in fold f updates tbl end +) + +(* Accessor for the above data *) +fun get_global_typing thy ctxt tm = Symtab.lookup (Typing_Data.get thy) (Syntax.string_of_term ctxt tm) + +(* Convert a Pure pre-term to an unchecked Pure term *) +fun tm_of_ptm ctxt ptm = + let val ptm = Term_Position.strip_positions ptm + in + (case ptm of + Const ("_constrain", _) $ Free (t, _) $ (Const ("\<^type>fun", _) $ Const(n1, _) $ Const (n2, _)) => + let + val typ1 = unprefix "\<^type>" n1 + val typ2 = unprefix "\<^type>" n2 + val typ = typ1 ^ "\" ^ typ2 |> Syntax.read_typ ctxt + in Free (t, typ) end + | Const ("_constrain", _) $ Free (t, _) $ Const (T, _) => + Free (t, Syntax.read_typ ctxt (unprefix "\<^type>" T)) + | ptm1 $ ptm2 => + (tm_of_ptm ctxt ptm1) $ (tm_of_ptm ctxt ptm2) + | Const (t, T) => + let val t' = + unprefix "\<^const>" t handle Fail "unprefix" => + unprefix "\<^type>" t handle Fail "unprefix" => + t + in Const (t', T) end + | Abs (t, T, ptm') => + Abs (t, T, tm_of_ptm ctxt ptm') + | t => t) + end + +end -- cgit v1.2.3 From 8759a6ff0dbe5f0e9dbc28ab2711c54bed7ffa44 Mon Sep 17 00:00:00 2001 From: Josh Chen Date: Tue, 5 Feb 2019 18:29:10 +0100 Subject: Decided to keep terms eta-expanded --- HoTT_Base.thy | 2 ++ 1 file changed, 2 insertions(+) diff --git a/HoTT_Base.thy b/HoTT_Base.thy index aa13815..88cf986 100644 --- a/HoTT_Base.thy +++ b/HoTT_Base.thy @@ -18,6 +18,8 @@ begin section \Terms and typing\ +declare[[eta_contract=false]] + typedecl t \ \Type of object-logic terms (which includes the types)\ judgment has_type :: "[t, t] \ prop" ("(3_:/ _)") -- cgit v1.2.3 From 9d21e7e4ca3f22055acf72f15f19d4c9248882ca Mon Sep 17 00:00:00 2001 From: Josh Chen Date: Tue, 5 Feb 2019 18:29:37 +0100 Subject: Basic type inference for composed lambda terms! --- Prod.thy | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/Prod.thy b/Prod.thy index 4c572ce..a28c52a 100644 --- a/Prod.thy +++ b/Prod.thy @@ -67,7 +67,7 @@ lemmas Prod_form [form] lemmas Prod_routine [intro] = Prod_form Prod_intro Prod_elim lemmas Prod_comp [comp] = Prod_cmp Prod_uniq -section \Additional definitions\ +section \Function composition\ definition compose :: "[t, t, t] \ t" where "compose A g f \ ,\\x: A. g`(f`x)" @@ -81,10 +81,12 @@ let fun compose_tr ctxt tms = let val g :: f :: _ = tms |> map (Typing.tm_of_ptm ctxt) val dom = - case Typing.get_typing f (Typing.typing_assms ctxt) of - SOME (Const ("Prod.Prod", _) $ T $ _) => T - | SOME _ => Exn.error "Can't compose with a non-function" - | NONE => Exn.error "Cannot infer domain of composition—please state this explicitly" + case f of + Const ("Prod.lam", _) $ T $ _ => T + | _ => (case Typing.get_typing f (Typing.typing_assms ctxt) of + SOME (Const ("Prod.Prod", _) $ T $ _) => T + | SOME _ => Exn.error "Can't compose with a non-function" + | NONE => Exn.error "Cannot infer domain of composition: please state this explicitly") in @{const compose} $ dom $ g $ f end @@ -94,24 +96,15 @@ end \ lemma compose_assoc: - assumes "A: U i" "f: A \ B" "g: B \ C" "h: TT x: C. D x" "(,\\(x: A). b x): TT x: A. D x" - shows "(h o g) o f \ h o (g o f)" -(* (derive lems: assms Prod_intro_eq) *) -sorry + assumes "A: U i" "f: A \ B" "g: B \ C" "h: TT x: C. D x" + shows "compose A (compose B h g) f \ compose A h (compose A g f)" +by (derive lems: assms Prod_intro_eq) lemma compose_comp: assumes "A: U i" and "\x. x: A \ b x: B" and "\x. x: B \ c x: C x" shows "(,\\x: B. c x) o (,\\x: A. b x) \ ,\\x: A. c (b x)" -ML_prf \ -Assumption.all_assms_of @{context}; -nth (Assumption.all_assms_of @{context}) 1 |> Thm.term_of; -Assumption.all_prems_of @{context}; -nth (Assumption.all_prems_of @{context}) 0 |> Thm.prop_of; -typing_assms @{context} -\ -(*by (derive lems: assms Prod_intro_eq)*) +by (derive lems: assms Prod_intro_eq) abbreviation id :: "t \ t" where "id A \ ,\\x: A. x" - end -- cgit v1.2.3 From 64d2a5c60acce40113362c9d7eca8cd633362d23 Mon Sep 17 00:00:00 2001 From: Josh Chen Date: Tue, 5 Feb 2019 18:32:43 +0100 Subject: Add cong named theorem for congruence rules --- HoTT_Base.thy | 9 ++++++--- Prod.thy | 5 +++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/HoTT_Base.thy b/HoTT_Base.thy index 88cf986..e74ef31 100644 --- a/HoTT_Base.thy +++ b/HoTT_Base.thy @@ -68,16 +68,19 @@ text \We use the notation @{prop "B: A \ U i"} to abbreviate type section \Named theorems\ -named_theorems comp named_theorems form +named_theorems comp +named_theorems cong text \ The named theorems above will be used by proof methods defined in @{file HoTT_Methods.thy}. -@{attribute comp} declares computation rules, which are used by the \compute\ method, and may also be passed to invocations of the method \subst\ to perform equational rewriting. - @{attribute form} declares type formation rules. These are mainly used by the \cumulativity\ method, which lifts types into higher universes. + +@{attribute comp} declares computation rules, which are used by the \compute\ method, and may also be passed to invocations of the method \subst\ to perform equational rewriting. + +@{attribute cong} declares congruence rules, the definitional equality rules of the type theory. \ (* Todo: Set up the Simplifier! *) diff --git a/Prod.thy b/Prod.thy index a28c52a..0de7d89 100644 --- a/Prod.thy +++ b/Prod.thy @@ -66,6 +66,7 @@ Note that this is a separate rule from function extensionality. lemmas Prod_form [form] lemmas Prod_routine [intro] = Prod_form Prod_intro Prod_elim lemmas Prod_comp [comp] = Prod_cmp Prod_uniq +lemmas Prod_cong [cong] = Prod_form_eq Prod_intro_eq section \Function composition\ @@ -98,12 +99,12 @@ end lemma compose_assoc: assumes "A: U i" "f: A \ B" "g: B \ C" "h: TT x: C. D x" shows "compose A (compose B h g) f \ compose A h (compose A g f)" -by (derive lems: assms Prod_intro_eq) +by (derive lems: assms cong) lemma compose_comp: assumes "A: U i" and "\x. x: A \ b x: B" and "\x. x: B \ c x: C x" shows "(,\\x: B. c x) o (,\\x: A. b x) \ ,\\x: A. c (b x)" -by (derive lems: assms Prod_intro_eq) +by (derive lems: assms cong) abbreviation id :: "t \ t" where "id A \ ,\\x: A. x" -- cgit v1.2.3