module public import Std.Data.HashMap public import Std.Data.HashSet public section namespace Regula.Closed open Lean (Name) inductive Kind where | «axiom» | «definition» | «theorem» | «opaque» | quotient | «inductive» | constructor | recursor deriving DecidableEq, Repr inductive Safety where | safe | «unsafe» | «partial» deriving DecidableEq, Repr inductive CtorIdxForm where | none | defn (induct : Name) | impl (induct : Name) deriving DecidableEq, Repr structure Decl where name : Name module : Name kind : Kind safety : Safety uses : List Name valueUses : List Name implementedBy : Option Name isExtern : Bool initFn : Option Name inToolchain : Bool ctorIdxForm : CtorIdxForm deriving Repr inductive Profile where | kernelOnly | choiceFree | standardLogical deriving DecidableEq, Repr namespace Profile def spelling : Profile → String | .kernelOnly => "kernel-only" | .choiceFree => "choice-free" | .standardLogical => "standard-logical" def all : List Profile := [.kernelOnly, .choiceFree, .standardLogical] theorem mem_all (p : Profile) : p ∈ all := by cases p <;> simp [all] theorem spelling_injective : Function.Injective spelling := by intro a b h cases a <;> cases b <;> first | rfl | exact absurd h (by decide) def ofSpelling? (text : String) : Option Profile := all.find? (·.spelling == text) theorem ofSpelling?_eq_some {text : String} {p : Profile} : ofSpelling? text = some p ↔ p.spelling = text := by constructor · intro h simpa using List.find?_some h · rintro rfl cases p <;> decide end Profile structure CSimpEntry where module : Name thm : Name deriving DecidableEq, Repr structure Draft where profile : Profile modules : List Name moduleFiles : List Name claimedConstants : List Name csimps : List CSimpEntry decls : List Decl unique : (decls.map (·.name)).Nodup structure Input extends Draft where closed : ∀ d ∈ decls, ∀ u ∈ d.uses, ∃ e ∈ decls, e.name = u claims : modules ≠ [] def setOf (l : List Name) : Std.HashSet Name := l.foldl (·.insert ·) {} theorem mem_insertFold (l : List Name) (init : Std.HashSet Name) (x : Name) : (l.foldl (·.insert ·) init).contains x = true ↔ x ∈ l ∨ init.contains x = true := by induction l generalizing init with | nil => simp | cons a l ih => simp only [List.foldl_cons, ih, Std.HashSet.contains_insert, Bool.or_eq_true, beq_iff_eq, List.mem_cons] constructor · rintro (h | rfl | h) · exact .inl (.inr h) · exact .inl (.inl rfl) · exact .inr h · rintro ((rfl | h) | h) · exact .inr (.inl rfl) · exact .inl h · exact .inr (.inr h) theorem mem_setOf (l : List Name) (x : Name) : (setOf l).contains x = true ↔ x ∈ l := by rw [setOf, mem_insertFold] simp theorem map_task_spawn_get {α β : Type} (xs : List α) (f : α → β) : (xs.map fun a => Task.spawn (prio := .default) fun _ => f a).map Task.get = xs.map f := by induction xs with | nil => rfl | cons x rest ih => simp only [List.map_cons, ih] rfl def splitChunksGo {α : Type} (chunkSize : Nat) : Nat → List α → List (List α) → List (List α) | 0, [], acc => acc.reverse | 0, xs, acc => (xs :: acc).reverse | _ + 1, [], acc => acc.reverse | fuel + 1, xs, acc => let (h, t) := xs.splitAt chunkSize splitChunksGo chunkSize fuel t (h :: acc) def splitChunks {α : Type} (chunkSize : Nat) (xs : List α) : List (List α) := if chunkSize == 0 then [xs] else splitChunksGo chunkSize xs.length xs [] theorem splitChunksGo_flatten {α : Type} (chunkSize : Nat) : ∀ (fuel : Nat) (xs : List α) (acc : List (List α)), (splitChunksGo chunkSize fuel xs acc).flatten = acc.reverse.flatten ++ xs | 0, [], acc => by simp [splitChunksGo] | 0, x :: xs, acc => by simp [splitChunksGo] | _ + 1, [], acc => by simp [splitChunksGo] | fuel + 1, x :: xs, acc => by simp only [splitChunksGo, List.splitAt_eq] rw [splitChunksGo_flatten chunkSize fuel ((x :: xs).drop chunkSize) ((x :: xs).take chunkSize :: acc)] simp only [List.reverse_cons, List.flatten_append, List.flatten_cons, List.flatten_nil, List.append_nil, List.append_assoc, List.take_append_drop] theorem splitChunks_flatten {α : Type} (chunkSize : Nat) (xs : List α) : (splitChunks chunkSize xs).flatten = xs := by unfold splitChunks split · simp · simpa using splitChunksGo_flatten chunkSize xs.length xs [] def mapMPar {α β ε : Type} (chunkSize : Nat) (f : α → Except ε β) (xs : List α) : Except ε (List β) := let tasks := (splitChunks chunkSize xs).map fun chunk => Task.spawn (prio := .default) fun _ => chunk.mapM f ((tasks.map Task.get).mapM id).map List.flatten theorem except_mapM_append {α β ε : Type} (f : α → Except ε β) (xs ys : List α) : (xs ++ ys).mapM f = match xs.mapM f with | .error e => .error e | .ok r1 => (ys.mapM f).map (r1 ++ ·) := by induction xs with | nil => simp only [List.nil_append, List.mapM_nil, pure, Except.pure] cases ys.mapM f <;> rfl | cons a xs ih => simp only [List.cons_append, List.mapM_cons, bind, Except.bind, pure, Except.pure] cases f a with | error e => rfl | ok b => rw [ih] cases xs.mapM f with | error e => rfl | ok r1 => cases ys.mapM f <;> rfl theorem except_mapM_chunks {α β ε : Type} (f : α → Except ε β) (chunks : List (List α)) : ((chunks.map fun c => List.mapM f c).mapM id).map List.flatten = chunks.flatten.mapM f := by induction chunks with | nil => rfl | cons c cs ih => rw [List.map_cons, List.mapM_cons, List.flatten_cons, except_mapM_append] simp only [id_eq, bind, Except.bind, pure, Except.pure] cases c.mapM f with | error e => rfl | ok r1 => rw [← ih] cases (cs.map fun c => List.mapM f c).mapM id <;> rfl theorem mapMPar_eq_mapM {α β ε : Type} (chunkSize : Nat) (f : α → Except ε β) (xs : List α) : mapMPar chunkSize f xs = xs.mapM f := by unfold mapMPar dsimp only rw [map_task_spawn_get, except_mapM_chunks, splitChunks_flatten] def findSomePar {α β : Type} (chunkSize : Nat) (f : α → Option β) (xs : List α) : Option β := let tasks := (splitChunks chunkSize xs).map fun chunk => Task.spawn (prio := .default) fun _ => chunk.findSome? f (tasks.map Task.get).findSome? id theorem findSome?_chunks {α β : Type} (f : α → Option β) (chunks : List (List α)) : (chunks.map fun c => c.findSome? f).findSome? id = chunks.flatten.findSome? f := by induction chunks with | nil => rfl | cons c cs ih => simp only [List.map_cons, List.findSome?_cons, List.flatten_cons, List.findSome?_append, id_eq] cases c.findSome? f <;> simp [ih] theorem findSomePar_eq_findSome {α β : Type} (chunkSize : Nat) (f : α → Option β) (xs : List α) : findSomePar chunkSize f xs = xs.findSome? f := by unfold findSomePar dsimp only rw [map_task_spawn_get, findSome?_chunks, splitChunks_flatten] def arrayChunkCount (total chunkSize : Nat) : Nat := if chunkSize == 0 then 1 else (total + chunkSize - 1) / chunkSize def extractChunksGo {α : Type} (xs : Array α) (chunkSize : Nat) : Nat → Nat → List (Array α) → List (Array α) | 0, start, acc => if start < xs.size then (xs.extract start xs.size :: acc).reverse else acc.reverse | fuel + 1, start, acc => if start < xs.size then let stop := min (start + chunkSize) xs.size extractChunksGo xs chunkSize fuel stop (xs.extract start stop :: acc) else acc.reverse def arrayChunks {α : Type} (chunkSize : Nat) (xs : Array α) : List (Array α) := if chunkSize == 0 then [xs] else extractChunksGo xs chunkSize (arrayChunkCount xs.size chunkSize) 0 [] def concatArrays {α : Type} (cap : Nat) (chunks : List (Array α)) : Array α := chunks.foldl (init := Array.emptyWithCapacity cap) fun acc c => acc ++ c theorem foldl_append_eq {α : Type} (chunks : List (Array α)) (init : Array α) : chunks.foldl (fun acc c => acc ++ c) init = init ++ chunks.foldl (fun acc c => acc ++ c) #[] := by induction chunks generalizing init with | nil => simp | cons c cs ih => simp only [List.foldl_cons, Array.empty_append] rw [ih (init ++ c), ih c, Array.append_assoc] theorem concatArrays_append {α : Type} (cap : Nat) (xs ys : List (Array α)) : concatArrays cap (xs ++ ys) = concatArrays cap xs ++ concatArrays cap ys := by unfold concatArrays rw [List.foldl_append, foldl_append_eq ys (xs.foldl _ _), foldl_append_eq ys (Array.emptyWithCapacity cap)] simp [Array.emptyWithCapacity_eq] theorem concatArrays_cons {α : Type} (cap : Nat) (c : Array α) (cs : List (Array α)) : concatArrays cap (c :: cs) = c ++ concatArrays cap cs := by unfold concatArrays rw [List.foldl_cons, foldl_append_eq cs, foldl_append_eq cs (Array.emptyWithCapacity cap)] simp [Array.emptyWithCapacity_eq] theorem concatArrays_nil {α : Type} (cap : Nat) : concatArrays cap ([] : List (Array α)) = #[] := by simp [concatArrays, Array.emptyWithCapacity_eq] theorem extractChunksGo_concat {α : Type} (xs : Array α) (chunkSize cap : Nat) : ∀ (fuel start : Nat) (acc : List (Array α)), concatArrays cap (extractChunksGo xs chunkSize fuel start acc) = concatArrays cap acc.reverse ++ xs.extract start xs.size | 0, start, acc => by unfold extractChunksGo split · rw [List.reverse_cons, concatArrays_append, concatArrays_cons, concatArrays_nil, Array.append_empty] · rename_i h have hge : min xs.size xs.size ≤ start := by omega rw [Array.extract_eq_empty_of_le hge, Array.append_empty] | fuel + 1, start, acc => by unfold extractChunksGo split · rename_i h rw [extractChunksGo_concat xs chunkSize cap fuel (min (start + chunkSize) xs.size) (xs.extract start (min (start + chunkSize) xs.size) :: acc)] rw [List.reverse_cons, concatArrays_append, concatArrays_cons, concatArrays_nil, Array.append_empty, Array.append_assoc] have h1 : start ≤ min (start + chunkSize) xs.size := by omega have h2 : min (start + chunkSize) xs.size ≤ xs.size := Nat.min_le_right _ _ rw [Array.extract_append_extract, Nat.min_eq_left h1, Nat.max_eq_right h2] · rename_i h have hge : min xs.size xs.size ≤ start := by omega rw [Array.extract_eq_empty_of_le hge, Array.append_empty] theorem arrayChunks_concat {α : Type} (chunkSize cap : Nat) (xs : Array α) : concatArrays cap (arrayChunks chunkSize xs) = xs := by unfold arrayChunks split · rw [concatArrays_cons, concatArrays_nil, Array.append_empty] · rw [extractChunksGo_concat, List.reverse_nil, concatArrays_nil, Array.empty_append, Array.extract_eq_self_of_le (Nat.le_refl _)] theorem concatArrays_map {α β : Type} (cap : Nat) (f : α → β) (chunks : List (Array α)) : concatArrays cap (chunks.map (Array.map f)) = (concatArrays cap chunks).map f := by induction chunks with | nil => simp [concatArrays_nil] | cons c cs ih => rw [List.map_cons, concatArrays_cons, concatArrays_cons, ih, Array.map_append] def mapArrayPar {α β : Type} (chunkSize : Nat) (f : α → β) (xs : Array α) : Array β := let tasks := (arrayChunks chunkSize xs).map fun chunk => Task.spawn (prio := .default) fun _ => chunk.map f concatArrays xs.size (tasks.map Task.get) theorem mapArrayPar_eq_map {α β : Type} (chunkSize : Nat) (f : α → β) (xs : Array α) : mapArrayPar chunkSize f xs = xs.map f := by unfold mapArrayPar dsimp only rw [map_task_spawn_get, concatArrays_map, arrayChunks_concat] def nameSetOfDecls (decls : List Decl) : Std.HashSet Name := decls.foldl (init := Std.HashSet.emptyWithCapacity decls.length) fun s d => s.insert d.name theorem mem_nameSetOfDeclsFold (decls : List Decl) (init : Std.HashSet Name) (x : Name) : (decls.foldl (fun s d => s.insert d.name) init).contains x = true ↔ (∃ e ∈ decls, e.name = x) ∨ init.contains x = true := by induction decls generalizing init with | nil => simp | cons a l ih => simp only [List.foldl_cons, ih, Std.HashSet.contains_insert, Bool.or_eq_true, beq_iff_eq, List.mem_cons] constructor · rintro (⟨e, he, rfl⟩ | rfl | h) · exact .inl ⟨e, .inr he, rfl⟩ · exact .inl ⟨a, .inl rfl, rfl⟩ · exact .inr h · rintro (⟨e, rfl | he, rfl⟩ | h) · exact .inr (.inl rfl) · exact .inl ⟨e, he, rfl⟩ · exact .inr (.inr h) theorem mem_nameSetOfDecls (decls : List Decl) (x : Name) : (nameSetOfDecls decls).contains x = true ↔ ∃ e ∈ decls, e.name = x := by rw [nameSetOfDecls, mem_nameSetOfDeclsFold] simp namespace Draft variable (d : Draft) def openMention? : Option (Name × Name) := let names := nameSetOfDecls d.decls findSomePar 4096 (fun x => (x.uses.find? (!names.contains ·)).map (x.name, ·)) d.decls theorem openMention?_eq_none : d.openMention? = none ↔ ∀ x ∈ d.decls, ∀ u ∈ x.uses, ∃ e ∈ d.decls, e.name = u := by simp only [openMention?, findSomePar_eq_findSome, List.findSome?_eq_none_iff, Option.map_eq_none_iff, List.find?_eq_none] constructor · intro h x hx u hu exact (mem_nameSetOfDecls d.decls u).mp (by simpa using h x hx u hu) · intro h x hx u hu simp [(mem_nameSetOfDecls d.decls u).mpr (h x hx u hu)] def admit : Except String Input := match h : d.openMention? with | some (n, u) => .error s!"the constant {n} mentions {u}, which the environment does not hold" | none => match hm : d.modules with | [] => .error "the build claims no module, so nothing is checked" | _ :: _ => .ok { toDraft := d, closed := d.openMention?_eq_none.mp h, claims := by simp [hm] } end Draft end Regula.Closed