include "basics/types.ma". include "ASM/String.ma". include "utilities/binary/positive.ma". include "utilities/lists.ma". include "common/Errors.ma". (* identifiers and their generators are tagged to differentiate them, and to provide extra type checking. *) (* in common/PreIdentifiers.ma, via Errors.ma. inductive identifier (tag:String) : Type[0] ≝ an_identifier : Pos → identifier tag. *) record universe (tag:String) : Type[0] ≝ { next_identifier : Pos }. definition new_universe : ∀tag:String. universe tag ≝ λtag. mk_universe tag one. let rec fresh (tag:String) (u:universe tag) on u : identifier tag × (universe tag) ≝ let id ≝ next_identifier ? u in 〈an_identifier tag id, mk_universe tag (succ id)〉. let rec fresh_for_univ tag (id:identifier tag) (u:universe tag) on id : Prop ≝ match id with [ an_identifier p ⇒ p < next_identifier … u ]. lemma fresh_is_fresh : ∀tag,id,u,u'. 〈id,u〉 = fresh tag u' → fresh_for_univ tag id u. #tag * #id * #u * #u' #E whd in E:(???%); destruct // qed. lemma fresh_remains_fresh : ∀tag,id,id',u,u'. fresh_for_univ tag id u → 〈id',u'〉 = fresh tag u → fresh_for_univ tag id u'. #tag * #id * #id' * #u * #u' normalize #H #E destruct /2 by le_S/ qed. lemma fresh_distinct : ∀tag,id,id',u,u'. fresh_for_univ tag id u → 〈id',u'〉 = fresh tag u → id ≠ id'. #tag * #id * #id' * #u * #u' normalize #H #E destruct % #E' destruct /2 by absurd/ qed. let rec env_fresh_for_univ tag A (env:list (identifier tag × A)) (u:universe tag) on u : Prop ≝ All ? (λida. fresh_for_univ tag (\fst ida) u) env. lemma fresh_env_extend : ∀tag,A,env,u,u',id,a. env_fresh_for_univ tag A env u → 〈id,u'〉 = fresh tag u → env_fresh_for_univ tag A (〈id,a〉::env) u'. #tag #A #env * #u * #u' #id #a #H #E whd % [ @(fresh_is_fresh … E) | @(All_mp … H) * #id #a #H' /2 by fresh_remains_fresh/ ] qed. definition eq_identifier : ∀t. identifier t → identifier t → bool ≝ λt,l,r. match l with [ an_identifier l' ⇒ match r with [ an_identifier r' ⇒ eqb l' r' ] ]. lemma eq_identifier_elim : ∀P:bool → Type[0]. ∀t,x,y. (x = y → P true) → (x ≠ y → P false) → P (eq_identifier t x y). #P #t * #x * #y #T #F change with (P (eqb ??)) @(eqb_elim x y P) [ /2 by / | * #H @F % #E destruct /2 by / ] qed. definition word_of_identifier ≝ λt. λl: identifier t. match l with [ an_identifier l' ⇒ l' ]. lemma eq_identifier_refl : ∀tag,id. eq_identifier tag id id = true. #tag * #id whd in ⊢ (??%?); >eqb_n_n @refl qed. axiom eq_identifier_sym: ∀tag: String. ∀l : identifier tag. ∀r : identifier tag. eq_identifier tag l r = eq_identifier tag r l. lemma eq_identifier_false : ∀tag,x,y. x≠y → eq_identifier tag x y = false. #tag * #x * #y #NE normalize @not_eq_to_eqb_false /2 by not_to_not/ qed. definition identifier_eq : ∀tag:String. ∀x,y:identifier tag. (x=y) + (x≠y). #tag * #x * #y lapply (refl ? (eqb x y)) cases (eqb x y) in ⊢ (???% → %); #E [ % | %2 ] lapply E @eqb_elim [ #H #_ >H @refl | 2,3: #_ #H destruct | #H #_ % #H' destruct /2 by absurd/ ] qed. definition identifier_of_nat : ∀tag:String. nat → identifier tag ≝ λtag,n. an_identifier tag (succ_pos_of_nat n). (* States that all identifiers in an environment are distinct from one another. *) let rec distinct_env tag (A:Type[0]) (l:list (identifier tag × A)) on l : Prop ≝ match l with [ nil ⇒ True | cons hd tl ⇒ All ? (λia. \fst hd ≠ \fst ia) tl ∧ distinct_env tag A tl ]. lemma distinct_env_append_l : ∀tag,A,l,r. distinct_env tag A (l@r) → distinct_env tag A l. #tag #A #l elim l [ // | * #id #a #tl #IH #r * #H1 #H2 % /2 by All_append_l/ ] qed. lemma distinct_env_append_r : ∀tag,A,l,r. distinct_env tag A (l@r) → distinct_env tag A r. #tag #A #l elim l [ // | * #id #a #tl #IH #r * #H1 #H2 /2 by / ] qed. (* check_distinct_env is quadratic - we could pay more attention when building maps to make sure that the original environment was distinct. *) axiom DuplicateVariable : String. let rec check_member_env tag (A:Type[0]) (id:identifier tag) (l:list (identifier tag × A)) on l : res (All ? (λia. id ≠ \fst ia) l) ≝ match l return λl.res (All ?? l) with [ nil ⇒ OK ? I | cons hd tl ⇒ match identifier_eq tag id (\fst hd) with [ inl _ ⇒ Error ? [MSG DuplicateVariable; CTX ? id] | inr NE ⇒ do Htl ← check_member_env tag A id tl; OK ? (conj ?? NE Htl) ] ]. let rec check_distinct_env tag (A:Type[0]) (l:list (identifier tag × A)) on l : res (distinct_env tag A l) ≝ match l return λl.res (distinct_env tag A l) with [ nil ⇒ OK ? I | cons hd tl ⇒ do Hhd ← check_member_env tag A (\fst hd) tl; do Htl ← check_distinct_env tag A tl; OK ? (conj ?? Hhd Htl) ]. (* Maps from identifiers to arbitrary types. *) include "common/PositiveMap.ma". inductive identifier_map (tag:String) (A:Type[0]) : Type[0] ≝ an_id_map : positive_map A → identifier_map tag A. definition empty_map : ∀tag:String. ∀A. identifier_map tag A ≝ λtag,A. an_id_map tag A (pm_leaf A). let rec lookup tag A (m:identifier_map tag A) (l:identifier tag) on m : option A ≝ lookup_opt A (match l with [ an_identifier l' ⇒ l' ]) (match m with [ an_id_map m' ⇒ m' ]). definition lookup_def ≝ λtag,A,m,l,d. match lookup tag A m l with [ None ⇒ d | Some x ⇒ x]. let rec member tag A (m:identifier_map tag A) (l:identifier tag) on m : bool ≝ match lookup tag A m l with [ None ⇒ false | _ ⇒ true ]. (* Always adds the identifier to the map. *) let rec add tag A (m:identifier_map tag A) (l:identifier tag) (a:A) on m : identifier_map tag A ≝ an_id_map tag A (insert A (match l with [ an_identifier l' ⇒ l' ]) a (match m with [ an_id_map m' ⇒ m' ])). lemma lookup_add_hit : ∀tag,A,m,i,a. lookup tag A (add tag A m i a) i = Some ? a. #tag #A * #m * #i #a @lookup_opt_insert_hit qed. lemma lookup_def_add_hit : ∀tag,A,m,i,a,d. lookup_def tag A (add tag A m i a) i d = a. #tag #A * #m * #i #a #d @lookup_insert_hit qed. lemma lookup_add_miss : ∀tag,A,m,i,j,a. i ≠ j → lookup tag A (add tag A m j a) i = lookup tag A m i. #tag #A * #m * #i * #j #a #H @lookup_opt_insert_miss /2 by not_to_not/ qed. axiom lookup_def_add_miss : ∀tag,A,m,i,j,a,d. i ≠ j → lookup_def tag A (add tag A m j a) i d = lookup_def tag A m i d. lemma lookup_add_oblivious : ∀tag,A,m,i,j,a. (lookup tag A m i ≠ None ?) → lookup tag A (add tag A m j a) i ≠ None ?. #tag #A #m #i #j #a #H cases (identifier_eq ? i j) [ #E >E >lookup_add_hit % #N destruct | #NE >lookup_add_miss // ] qed. lemma lookup_add_cases : ∀tag,A,m,i,j,a,v. lookup tag A (add tag A m i a) j = Some ? v → (i=j ∧ v = a) ∨ lookup tag A m j = Some ? v. #tag #A #m #i #j #a #v cases (identifier_eq ? i j) [ #E >E >lookup_add_hit #H %1 destruct % // | #NE >lookup_add_miss /2 by or_intror, sym_not_eq/ ] qed. (* Extract every identifier, value pair from the map. *) definition elements : ∀tag,A. identifier_map tag A → list (identifier tag × A) ≝ λtag,A,m. fold ?? (λl,a,el. 〈an_identifier tag l, a〉::el) (match m with [ an_id_map m' ⇒ m' ]) [ ]. axiom MissingId : String. (* Only updates an existing entry; fails with an error otherwise. *) definition update : ∀tag,A. identifier_map tag A → identifier tag → A → res (identifier_map tag A) ≝ λtag,A,m,l,a. match update A (match l with [ an_identifier l' ⇒ l' ]) a (match m with [ an_id_map m' ⇒ m' ]) with [ None ⇒ Error ? ([MSG MissingId; CTX tag l]) (* missing identifier *) | Some m' ⇒ OK ? (an_id_map tag A m') ]. definition foldi: ∀A, B: Type[0]. ∀tag: String. (identifier tag -> A -> B -> B) -> identifier_map tag A -> B -> B ≝ λA,B,tag,f,m,b. match m with [ an_id_map m' ⇒ fold A B (λbv. f (an_identifier ? bv)) m' b ]. (* A predicate that an identifier is in a map, and a failure-avoiding lookup and update using it. *) definition present : ∀tag,A. identifier_map tag A → identifier tag → Prop ≝ λtag,A,m,i. lookup … m i ≠ None ?. lemma member_present : ∀tag,A,m,id. member tag A m id = true → present tag A m id. #tag #A * #m #id normalize cases (lookup_opt A ??) normalize [ #E destruct | #x #E % #E' destruct ] qed. include "ASM/Util.ma". definition lookup_present : ∀tag,A. ∀m:identifier_map tag A. ∀id. present ?? m id → A ≝ λtag,A,m,id. match lookup ?? m id return λx. x ≠ None ? → ? with [ Some a ⇒ λ_. a | None ⇒ λH.⊥ ]. cases H #H' cases (H' (refl ??)) qed. lemma lookup_lookup_present : ∀tag,A,m,id,p. lookup tag A m id = Some ? (lookup_present tag A m id p). #tag #A #m #id #p whd in p ⊢ (???(??%)); cases (lookup tag A m id) in p ⊢ %; [ * #H @⊥ @H @refl | #a #H @refl ] qed. definition update_present : ∀tag,A. ∀m:identifier_map tag A. ∀id. present ?? m id → A → identifier_map tag A ≝ λtag,A,m,l,p,a. let l' ≝ match l with [ an_identifier l' ⇒ l' ] in let m' ≝ match m with [ an_id_map m' ⇒ m' ] in let u' ≝ update A l' a m' in match u' return λx. update ???? = x → ? with [ None ⇒ λE.⊥ | Some m' ⇒ λ_. an_id_map tag A m' ] (refl ? u'). cases l in p E; cases m; -l' -m' #m' #l' whd in ⊢ (% → ?); whd in ⊢ (?(??(???%%)?) → ??(??%?%)? → ?); #NL #U cases NL #H @H @(update_fail … U) qed. lemma update_still_present : ∀tag,A,m,id,a,id'. ∀H:present tag A m id. ∀H':present tag A m id'. present tag A (update_present tag A m id' H' a) id. #tag #A * #m * #id #a * #id' #H #H' whd whd in ⊢ (?(??(???(%??????)?)?)); normalize nodelta cases (identifier_eq ? (an_identifier tag id) (an_identifier tag id')) [ #E >E @refute_none_by_refl #m' #U whd in ⊢ (?(??%?)); >(update_lookup_opt_same ????? U) % #E' destruct | #NE @refute_none_by_refl #m' #U whd in ⊢ (?(??%?)); whd in ⊢ (?(??(??%%)?)); <(update_lookup_opt_other ????? U id) [ @H | % #E cases NE >E #H @H @refl ] ] qed. let rec fresh_for_map tag A (id:identifier tag) (m:identifier_map tag A) on id : Prop ≝ lookup … m id = None A. lemma fresh_for_empty_map : ∀tag,A,id. fresh_for_map tag A id (empty_map tag A). #tag #A * #id // qed. definition fresh_map_for_univ ≝ λtag,A. λm:identifier_map tag A. λu:universe tag. ∀id. present tag A m id → fresh_for_univ tag id u. lemma fresh_fresh_for_map : ∀tag,A,m,id,u,u'. fresh_map_for_univ tag A m u → 〈id,u'〉 = fresh tag u → fresh_for_map tag A id m. #tag #A * #m * #id * #u * #u' whd in ⊢ (% → ???% → %); #FMU #E destruct lapply (FMU (an_identifier tag u)) whd in ⊢ ((% → %) → ?); generalize in ⊢ ((?(??%?) → ?) → ??%?); * [ // | #a #H @False_ind lapply (H ?) /2 by absurd/ % #E destruct qed. lemma fresh_map_preserved : ∀tag,A,m,u,u',id. fresh_map_for_univ tag A m u → 〈id,u'〉 = fresh tag u → fresh_map_for_univ tag A m u'. #tag #A #m #u * #u' #id whd in ⊢ (% → ? → %); #H #E #id' #PR @(fresh_remains_fresh … E) @H // qed. lemma fresh_map_add : ∀tag,A,m,u,id,a. fresh_map_for_univ tag A m u → fresh_for_univ tag id u → fresh_map_for_univ tag A (add tag A m id a) u. #tag #A * #m #u #id #a #Hm #Hi #id' #PR cases (identifier_eq tag id' id) [ #E >E @Hi | #NE @Hm whd in PR; change with (add tag A (an_id_map tag A m) id a) in PR:(?(??(???%?)?)); >lookup_add_miss in PR; // ] qed. lemma present_not_fresh : ∀tag,A,m,id,id'. present tag A m id → fresh_for_map tag A id' m → id ≠ id'. #tag #A #m #id * #id' whd in ⊢ (% → % → ?); * #NE #E % #E' destruct @(NE E) qed. lemma fresh_for_map_add : ∀tag,A,id,m,id',a. id ≠ id' → fresh_for_map tag A id m → fresh_for_map tag A id (add tag A m id' a). #tag #A * #id #m #id' #a #NE #F whd >lookup_add_miss // qed. (* Sets *) inductive identifier_set (tag:String) : Type[0] ≝ an_id_set : positive_map unit → identifier_set tag. definition empty_set : ∀tag:String. identifier_set tag ≝ λtag. an_id_set tag (pm_leaf unit). let rec add_set (tag:String) (s:identifier_set tag) (i:identifier tag) on s : identifier_set tag ≝ an_id_set tag (insert unit (match i with [ an_identifier i' ⇒ i' ]) it (match s with [ an_id_set s' ⇒ s' ])). definition singleton_set : ∀tag:String. identifier tag → identifier_set tag ≝ λtag,i. add_set tag (empty_set tag) i. let rec mem_set (tag:String) (s:identifier_set tag) (i:identifier tag) on s : bool ≝ match lookup_opt ? (match i with [ an_identifier i' ⇒ i' ]) (match s with [ an_id_set s' ⇒ s' ]) with [ None ⇒ false | Some _ ⇒ true ]. let rec union_set (tag:String) (s:identifier_set tag) (s':identifier_set tag) on s : identifier_set tag ≝ an_id_set tag (merge unit (match s with [ an_id_set s0 ⇒ s0 ]) (match s' with [ an_id_set s1 ⇒ s1 ])). interpretation "identifier set union" 'union a b = (union_set ? a b). notation "∅" non associative with precedence 90 for @{ 'empty }. interpretation "empty identifier set" 'empty = (empty_set ?). interpretation "singleton identifier set" 'singl a = (add_set ? (empty_set ?) a). interpretation "identifier set membership" 'mem a b = (mem_set ? b a). lemma union_empty_l : ∀tag.∀s:identifier_set tag. ∅ ∪ s = s. #tag * // qed. lemma union_empty_r : ∀tag.∀s:identifier_set tag. s ∪ ∅ = s. #tag * * // qed.