From 8ba8357a0b4640a3be817fd0645d026b568bc552 Mon Sep 17 00:00:00 2001 From: Josh Chen Date: Tue, 26 May 2020 16:39:35 +0200 Subject: Maybe and more List --- spartan/data/List.thy | 74 +++++++++++++++++++++++++++++++++++++++++++++----- spartan/data/Maybe.thy | 56 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+), 7 deletions(-) create mode 100644 spartan/data/Maybe.thy (limited to 'spartan') diff --git a/spartan/data/List.thy b/spartan/data/List.thy index a6c41c6..3399da6 100644 --- a/spartan/data/List.thy +++ b/spartan/data/List.thy @@ -1,5 +1,5 @@ theory List -imports Spartan +imports Maybe begin @@ -17,7 +17,7 @@ where List_nil: "A: U i \ nil A: List A" and - List_cons: "\x: A; l: List A\ \ cons A x l: List A" and + List_cons: "\x: A; xs: List A\ \ cons A x xs: List A" and ListE: "\ xs: List A; @@ -46,24 +46,78 @@ lemmas [elims "?xs"] = ListE and [comps] = List_comp_nil List_comp_cons -abbreviation "ListRec A C \ ListInd A (K C)" - - -section \Implicit notation\ +abbreviation "ListRec A C \ ListInd A (\_. C)" definition nil_i ("[]") where [implicit]: "[] \ nil ?" definition cons_i (infixr "#" 50) - where [implicit]: "x # l \ cons ? x l" + where [implicit]: "x # xs \ cons ? x xs" + +translations + "[]" \ "CONST List.nil A" + "x # xs" \ "CONST List.cons A x xs" + + +section \List notation\ + +syntax + "_list" :: \args \ o\ ("[_]") +translations + "[x, xs]" \ "x # [xs]" + "[x]" \ "x # []" section \Standard functions\ +\ \ +definition "head A \ \xs: List A. + ListRec A (Maybe A) (Maybe.none A) (\x _ _. Maybe.some A x) xs" +\ + +Lemma (derive) head: + assumes "A: U i" "xs: List A" + shows "Maybe A" +proof (elim xs) + show "none: Maybe A" by intro + show "\x. x: A \ some x: Maybe A" by intro +qed + +thm head_def \ \head ?A ?xs \ ListRec ?A (Maybe ?A) none (\x xs c. some x) ?xs\ + +\ \ definition "tail A \ \xs: List A. ListRec A (List A) (nil A) (\x xs _. xs) xs" +\ + +Lemma (derive) tail: + assumes "A: U i" "xs: List A" + shows "List A" +proof (elim xs) + show "[]: List A" by intro + show "\xs. xs: List A \ xs: List A" . +qed +thm tail_def + +\ \ definition "map A B \ \f: A \ B. \xs: List A. ListRec A (List B) (nil B) (\x _ c. cons B (f `x) c) xs" +\ + +Lemma (derive) map: + assumes "A: U i" "B: U i" "f: A \ B" "xs: List A" + shows "List B" +proof (elim xs) + show "[]: List B" by intro + next fix x xs ys + assume "x: A" "xs: List A" "ys: List B" + show "f x # ys: List B" by typechk +qed + +thm map_def + +definition head_i ("head") + where [implicit]: "head xs \ List.head ? xs" definition tail_i ("tail") where [implicit]: "tail xs \ List.tail ? xs" @@ -72,9 +126,15 @@ definition map_i ("map") where [implicit]: "map \ List.map ? ?" translations + "head" \ "CONST List.head A" "tail" \ "CONST List.tail A" "map" \ "CONST List.map A B" +Lemma head_type [typechk]: + assumes "A: U i" "xs: List A" + shows "head xs: Maybe A" + unfolding head_def by typechk + Lemma tail_type [typechk]: assumes "A: U i" "xs: List A" shows "tail xs: List A" diff --git a/spartan/data/Maybe.thy b/spartan/data/Maybe.thy new file mode 100644 index 0000000..532879a --- /dev/null +++ b/spartan/data/Maybe.thy @@ -0,0 +1,56 @@ +theory Maybe +imports Spartan + +begin + +axiomatization + Maybe :: \o \ o\ and + none :: \o \ o\ and + some :: \o \ o \ o\ and + MaybeInd :: \o \ (o \ o) \ o \ (o \ o) \ o \ o\ +where + MaybeF: "A: U i \ Maybe A: U i" and + + Maybe_none: "A: U i \ none A: Maybe A" and + + Maybe_some: "a: A \ some A a: Maybe A" and + + MaybeE: "\ + m: Maybe A; + c\<^sub>0: C (none A); + \a. a: A \ f a: C (some A a); + \m. m: Maybe A \ C m: U i + \ \ MaybeInd A (\m. C m) c\<^sub>0 (\a. f a) m: C m" and + + Maybe_comp_none: "\ + c\<^sub>0: C (none A); + \a. a: A \ f a: C (some A a); + \m. m: Maybe A \ C m: U i + \ \ MaybeInd A (\m. C m) c\<^sub>0 (\a. f a) (none A) \ c\<^sub>0" and + + Maybe_comp_some: "\ + m: Maybe A; + c\<^sub>0: C (none A); + \a. a: A \ f a: C (some A a); + \m. m: Maybe A \ C m: U i + \ \ MaybeInd A (\m. C m) c\<^sub>0 (\a. f a) (some A a) \ f a" + +lemmas + [intros] = MaybeF Maybe_none Maybe_some and + [elims "?m"] = MaybeE and + [comps] = Maybe_comp_none Maybe_comp_some + +abbreviation "MaybeRec A C \ MaybeInd A (K C)" + +definition none_i ("none") + where [implicit]: "none \ Maybe.none ?" + +definition some_i ("some") + where [implicit]: "some a \ Maybe.some ? a" + +translations + "none" \ "CONST Maybe.none A" + "some a" \ "CONST Maybe.some A a" + + +end -- cgit v1.2.3