Theory Watched_Literals_List_Inprocessing

theory Watched_Literals_List_Inprocessing
imports Watched_Literals_List Watched_Literals_Algorithm_Reduce
begin

definition all_learned_lits_of_l :: 'v twl_st_l  'v clause where
  all_learned_lits_of_l S'  all_lits_of_mm (mset `# learned_clss_lf (get_clauses_l S') + get_unit_learned_clss_l S' +
  get_subsumed_learned_clauses_l S' + get_learned_clauses0_l S')
definition all_init_lits_of_l :: 'v twl_st_l  'v clause where
  all_init_lits_of_l S'  all_lits_of_mm (mset `# get_init_clss_l S' + get_unit_init_clauses_l S' +
  get_subsumed_init_clauses_l S' + get_init_clauses0_l S')

inductive cdcl_twl_subsumed_l :: 'v twl_st_l  'v twl_st_l  bool where
subsumed_II:
  cdcl_twl_subsumed_l (M, N, D, NE, UE, NEk, UEk, NS, US, N0, U0, {#}, Q)
  (M, fmdrop C' N, D, NE, UE, NEk, UEk, add_mset (mset (N  C')) NS, US, N0, U0, {#}, Q)
  if mset (N  C) ⊆# mset (N  C') C ∈# dom_m N C' ∈# dom_m N
    irred N C'  irred N C C  C' C'  set (get_all_mark_of_propagated M)|
subsumed_RR:
    cdcl_twl_subsumed_l (M, N, D, NE, UE, NEk, UEk, NS, US, N0, U0, {#}, Q)
    (M, fmdrop C' N, D, NE, UE, NEk, UEk, NS, add_mset (mset (N  C')) US, N0, U0, {#}, Q)
  if mset (N  C) ⊆# mset (N  C') C ∈# dom_m N C' ∈# dom_m N
    ¬irred N C' ¬irred N CC  C' C'  set (get_all_mark_of_propagated M)|
subsumed_IR:
    cdcl_twl_subsumed_l (M, N, D, NE, UE, NEk, UEk, NS, US, N0, U0, {#}, Q)
    (M, fmdrop C' N, D, NE, UE, NEk, UEk, NS, add_mset (mset (N  C')) US, N0, U0, {#}, Q)
  if mset (N  C) ⊆# mset (N  C') C ∈# dom_m N C' ∈# dom_m N
    irred N C ¬irred N C' C'  set (get_all_mark_of_propagated M) |
subsumed_RI:
    cdcl_twl_subsumed_l (M, N, D, NE, UE, NEk, UEk, NS, US, N0, U0, {#}, Q)
    (M, fmupd C (N  C, True) (fmdrop C' N), D, NE, UE, NEk, UEk, add_mset (mset (N  C')) NS, US, N0, U0, {#}, Q)
  if mset (N  C) ⊆# mset (N  C') C ∈# dom_m N C' ∈# dom_m N
    ¬irred N C irred N C' C'  set (get_all_mark_of_propagated M)
    ¬tautology (mset (N  C))
    distinct_mset (mset (N  C))

lemma convert_lits_l_drop:
  C  set (get_all_mark_of_propagated M) 
  (M, M')  convert_lits_l (fmdrop C N) E  (M, M')  convert_lits_l N E
  unfolding convert_lits_l_def list_rel_def in_pair_collect_simp
  apply (rule iffI; (rule list.rel_mono_strong, assumption))
  apply (auto simp: convert_lits_l_def list_rel_def p2rel_def convert_lit.simps
    cdclW_restart_mset.in_get_all_mark_of_propagated_in_trail)
  done

lemma convert_lits_l_update_sel:
  C ∈# dom_m N  C' = N  C 
  (M, M')  convert_lits_l (fmupd C (C', b) N) E  (M, M')  convert_lits_l N E
  unfolding convert_lits_l_def list_rel_def in_pair_collect_simp
  apply (rule iffI; (rule list.rel_mono_strong, assumption))
  apply (auto simp: convert_lits_l_def list_rel_def p2rel_def convert_lit.simps
    cdclW_restart_mset.in_get_all_mark_of_propagated_in_trail)
  done

lemma learned_clss_l_mapsto_upd_in_irrelev: C ∈# dom_m N  ¬irred N C 
  learned_clss_l (fmupd C  (C', True) N) = remove1_mset (N  C, irred N C) (learned_clss_l N)
  by (auto simp: ran_m_mapsto_upd_notin ran_m_mapsto_upd)

lemma init_clss_l_mapsto_upd_irrelev:
  C ∈# dom_m N  ¬irred N C 
   init_clss_l (fmupd C (C', True) N) = add_mset (C', True) ((init_clss_l N))
  by (auto simp: ran_m_mapsto_upd)

lemma cdcl_twl_subsumed_l_cdcl_twl_subsumed:
  assumes cdcl_twl_subsumed_l S T and
    SS': (S, S')  twl_st_l None
  shows T'. (T, T')  twl_st_l None  cdcl_twl_subsumed S' T'
proof -
  obtain M' N' U' D' NE' UE' NS' US' WS' N0' U0' Q'  where
    S': S' = (M', N', U', D', NE', UE',NS', US', N0', U0', WS', Q')
    by (cases S')

  show ?thesis
    using assms(1)
  proof (cases rule: cdcl_twl_subsumed_l.cases)
    case (subsumed_II N C C' M D NE UE NEk UEk NS US N0 U0 Q)
    define N'' where N'' = (N' - {#twl_clause_of (N  C), twl_clause_of (N  C')#})
    let ?E = twl_clause_of (N  C)
    let ?E' = twl_clause_of (N  C')
    have (N  C, irred N C) ∈# init_clss_l N  and
      H: (N  C', irred N C') ∈# remove1_mset (N  C, irred N C)(init_clss_l N)
      using subsumed_II
      apply (auto dest!: multi_member_split simp: ran_m_def add_mset_eq_add_mset remove1_mset_add_mset_If
        split: if_splits)
      apply (metis prod.collapse)+
      done
    from multi_member_split[OF this(1)] multi_member_split[OF this(2)]
    have N' = N'' + {#twl_clause_of (N  C), twl_clause_of (N  C')#}
      using subsumed_II SS' unfolding N''_def
      by (auto simp: ran_m_def S' twl_st_l_def add_mset_eq_add_mset dest!: multi_member_split)
    then show ?thesis
      using SS' subsumed_II H unfolding S'
      apply (auto simp: add_mset_eq_add_mset image_mset_Diff conj_disj_distribL
        conj_disj_distribR eq_commute[of twl_clause_of _] eq_commute[of remove1_mset _ _]
        S' convert_lits_l_drop learned_clss_l_l_fmdrop_irrelev init_clss_l_fmdrop_irrelev image_mset_remove1_mset_if
        eq_commute[of add_mset _ (add_mset _ _) image_mset _ _]
        twl_st_l_def mset_take_mset_drop_mset' learned_clss_l_l_fmdrop_irrelev
        simp del: twl_clause_of.simps
        simp flip: twl_clause_of.simps
        dest!: intro!:  exI[of _ get_trail S'])
      apply (auto simp: init_clss_l_fmdrop_irrelev image_mset_remove1_mset_if
        eq_commute[of add_mset _ (add_mset _ _) image_mset _ _]
        twl_st_l_def mset_take_mset_drop_mset' learned_clss_l_l_fmdrop_irrelev init_clss_l_fmdrop
        intro!: cdcl_twl_subsumed_II_simp[where C = ?E and C' = ?E'])
      done
  next
    case (subsumed_RR N C C' M D NE UE NEk UEk NS US N0 U0 Q)
    define U'' where U'' = (U' - {#twl_clause_of (N  C), twl_clause_of (N  C')#})
    let ?E = twl_clause_of (N  C)
    let ?E' = twl_clause_of (N  C')
    have (N  C, irred N C) ∈# learned_clss_l N  and
      H: (N  C', irred N C') ∈# remove1_mset (N  C, irred N C)(learned_clss_l N)
      using subsumed_RR
      apply (auto dest!: multi_member_split simp: ran_m_def add_mset_eq_add_mset remove1_mset_add_mset_If
        split: if_splits)
      apply (metis prod.collapse)+
      done
    from multi_member_split[OF this(1)] multi_member_split[OF this(2)]
    have U' = U'' + {#twl_clause_of (N  C), twl_clause_of (N  C')#}
      using subsumed_RR SS' unfolding U''_def
      by (auto simp: ran_m_def S' twl_st_l_def add_mset_eq_add_mset dest!: multi_member_split)
    then show ?thesis
      using SS' subsumed_RR H unfolding S'
      apply (auto simp: add_mset_eq_add_mset image_mset_Diff conj_disj_distribL
        conj_disj_distribR eq_commute[of twl_clause_of _] eq_commute[of remove1_mset _ _]
        S' convert_lits_l_drop learned_clss_l_l_fmdrop_irrelev init_clss_l_fmdrop_irrelev image_mset_remove1_mset_if
        eq_commute[of add_mset _ (add_mset _ _) image_mset _ _]
        twl_st_l_def mset_take_mset_drop_mset'
        simp del: twl_clause_of.simps
        simp flip: twl_clause_of.simps
        dest!: intro!:  exI[of _ get_trail S'])
      apply (auto simp: init_clss_l_fmdrop_irrelev image_mset_remove1_mset_if
        eq_commute[of add_mset _ (add_mset _ _) image_mset _ _]
        twl_st_l_def mset_take_mset_drop_mset' learned_clss_l_l_fmdrop_irrelev init_clss_l_fmdrop
        learned_clss_l_l_fmdrop
        cdcl_twl_subsumed_RR_simp[where C = ?E and C' = ?E'])
      done
  next
    case (subsumed_IR N C C' M D NE UE NEk UEk NS US N0 U0 Q)
    define U'' where U'' = (U' - {#twl_clause_of (N  C')#})
    define N'' where N'' = (N' - {#twl_clause_of (N  C)#})
    let ?E = twl_clause_of (N  C)
    let ?E' = twl_clause_of (N  C')
    have (N  C, irred N C) ∈# init_clss_l N  and
      H: (N  C', irred N C') ∈# (learned_clss_l N)
      using subsumed_IR
      apply (auto dest!: multi_member_split simp: ran_m_def add_mset_eq_add_mset remove1_mset_add_mset_If
        split: if_splits)
      apply (metis prod.collapse)+
      done
    from multi_member_split[OF this(1)] multi_member_split[OF this(2)]
    have U' = U'' + {#twl_clause_of (N  C')#} N' = N'' + {#twl_clause_of (N  C)#}
      using subsumed_IR SS' unfolding U''_def N''_def
      by (auto simp: ran_m_def S' twl_st_l_def add_mset_eq_add_mset dest!: multi_member_split)
    then show ?thesis
      using SS' subsumed_IR H unfolding S'
      apply (auto simp: add_mset_eq_add_mset image_mset_Diff conj_disj_distribL
        conj_disj_distribR eq_commute[of twl_clause_of _] eq_commute[of remove1_mset _ _]
        S' convert_lits_l_drop learned_clss_l_l_fmdrop_irrelev init_clss_l_fmdrop_irrelev image_mset_remove1_mset_if
        eq_commute[of add_mset _ (add_mset _ _) image_mset _ _]
        twl_st_l_def mset_take_mset_drop_mset'
        simp del: twl_clause_of.simps
        simp flip: twl_clause_of.simps
        dest!: intro!:  exI[of _ get_trail S'])
      apply (auto simp: init_clss_l_fmdrop_irrelev image_mset_remove1_mset_if
        eq_commute[of add_mset _ (add_mset _ _) image_mset _ _]
        twl_st_l_def mset_take_mset_drop_mset' learned_clss_l_l_fmdrop_irrelev init_clss_l_fmdrop
        learned_clss_l_l_fmdrop
        cdcl_twl_subsumed_IR_simp[where C = ?E and C' = ?E'])
      done
  next
    case (subsumed_RI N C C' M D NE UE NEk UEk NS US N0 U0 Q)
    define U'' where U'' = (U' - {#twl_clause_of (N  C)#})
    define N'' where N'' = (N' - {#twl_clause_of (N  C')#})
    let ?E = twl_clause_of (N  C)
    let ?E' = twl_clause_of (N  C')
    have (N  C', irred N C') ∈# init_clss_l N  and
      H: (N  C, irred N C) ∈# (learned_clss_l N) and
      [simp,iff]: C ∈# remove1_mset C' (dom_m N) ¬ irred (fmdrop C' N) C C  C'
      (N  C, False) ∈# learned_clss_l N
      using subsumed_RI
      apply (auto dest!: multi_member_split simp: ran_m_def add_mset_eq_add_mset remove1_mset_add_mset_If
        split: if_splits)
      apply (metis prod.collapse)+
      done
    from multi_member_split[OF this(1)] multi_member_split[OF this(2)]
    have U' = U'' + {#twl_clause_of (N  C)#} N' = N'' + {#twl_clause_of (N  C')#}
      using subsumed_RI SS' unfolding U''_def N''_def
      by (auto simp: ran_m_def S' twl_st_l_def add_mset_eq_add_mset dest!: multi_member_split)
    then show ?thesis
      using SS' subsumed_RI H unfolding S'
      apply (auto simp: add_mset_eq_add_mset image_mset_Diff conj_disj_distribL
        conj_disj_distribR eq_commute[of twl_clause_of _] eq_commute[of remove1_mset _ _]
        S' convert_lits_l_drop learned_clss_l_l_fmdrop_irrelev init_clss_l_fmdrop_irrelev image_mset_remove1_mset_if
        eq_commute[of add_mset _ (add_mset _ _) image_mset _ _] convert_lits_l_update_sel
        twl_st_l_def mset_take_mset_drop_mset'
        simp del: twl_clause_of.simps
        simp flip: twl_clause_of.simps
        dest!: intro!:  exI[of _ get_trail S'])
      apply (auto simp: init_clss_l_fmdrop_irrelev image_mset_remove1_mset_if
        eq_commute[of add_mset _ (add_mset _ _) image_mset _ _]
        twl_st_l_def mset_take_mset_drop_mset' learned_clss_l_l_fmdrop_irrelev init_clss_l_fmdrop
        learned_clss_l_l_fmdrop convert_lits_l_drop learned_clss_l_mapsto_upd_in_irrelev
        init_clss_l_mapsto_upd init_clss_l_mapsto_upd_irrelev
        intro!: cdcl_twl_subsumed_RI_simp[where C = ?E and C' = ?E'])
      done
  qed
qed

lemma cdcl_twl_subsumed_l_list_invs:
  cdcl_twl_subsumed_l S T  twl_list_invs S  twl_list_invs T
  apply (induction rule: cdcl_twl_subsumed_l.induct)
 apply (auto simp: twl_list_invs_def cdclW_restart_mset.in_get_all_mark_of_propagated_in_trail
   ran_m_mapsto_upd distinct_mset_remove1_All distinct_mset_dom
   ran_m_lf_fmdrop
   dest: in_diffD)
 apply (subst (asm) ran_m_mapsto_upd)
 apply (auto dest!: in_diffD simp: distinct_mset_remove1_All distinct_mset_dom
   ran_m_lf_fmdrop)
 done

inductive cdcl_twl_subresolution_l :: 'v twl_st_l  'v twl_st_l  bool where
twl_subresolution_II_nonunit:
  cdcl_twl_subresolution_l (M, N, None, NE, UE, NEk, UEk, NS, US, N0, U0, {#}, Q)
    (M, fmupd C' (E, True) N, None, NE, UE, NEk, UEk, add_mset (mset (N  C')) NS, US, N0, U0, {#}, Q)
  if
    C ∈# dom_m N
    C' ∈# dom_m N
    mset (N  C) = add_mset L D
    mset (N  C') = add_mset (-L) D'
    count_decided M = 0 D ⊆# D' 
    mset E = remdups_mset D' length E  2 distinct E L ∈# mset E. undefined_lit M L
    C'  set (get_all_mark_of_propagated M) irred N C irred N C' |
twl_subresolution_II_unit:
  cdcl_twl_subresolution_l (M, N, None, NE, UE, NEk, UEk, NS, US, N0, U0, {#}, Q)
    (Propagated K 0 # M, fmdrop C' N, None, NE, UE, add_mset {#K#} NEk, UEk, add_mset (mset (N  C')) NS, US, N0, U0, {#}, add_mset (-K) Q)
  if
    C ∈# dom_m N
    C' ∈# dom_m N
    mset (N  C) = add_mset L D
    mset (N  C') = add_mset (-L) D'
    count_decided M = 0 D ⊆# D'
    remdups_mset D' = {#K#}
    undefined_lit M K
    C'  set (get_all_mark_of_propagated M) irred N C irred N C' |
twl_subresolution_LL_nonunit:
  cdcl_twl_subresolution_l (M, N, None, NE, UE, NEk, UEk, NS, US, N0, U0, {#}, Q)
    (M, fmupd C' (E, False) N, None, NE, UE, NEk, UEk, NS, add_mset (mset (N  C'))  US, N0, U0, {#}, Q)
  if
    C ∈# dom_m N
    C' ∈# dom_m N
    mset (N  C) = add_mset L D
    mset (N  C') = add_mset (-L) D'
    count_decided M = 0 D ⊆# D'  ¬tautology (D + D')
    mset E = remdups_mset D' length E  2 distinct E L ∈# mset E. undefined_lit M L
    C'  set (get_all_mark_of_propagated M) ¬irred N C ¬irred N C' |
twl_subresolution_LL_unit:
  cdcl_twl_subresolution_l (M, N, None, NE, UE, NEk, UEk, NS, US, N0, U0, {#}, Q)
   (Propagated K 0 # M, fmdrop C' N, None, NE, UE, NEk, add_mset {#K#} UEk, NS,  add_mset (mset (N  C')) US, N0, U0, {#}, add_mset (-K) Q)
  if
    C ∈# dom_m N
    C' ∈# dom_m N
    mset (N  C) = add_mset L D
    mset (N  C') = add_mset (-L) D'
    count_decided M = 0 D ⊆# D'
    remdups_mset D' = {#K#}
    undefined_lit M K
    C'  set (get_all_mark_of_propagated M) ¬irred N C ¬irred N C' |
twl_subresolution_LI_nonunit:
  cdcl_twl_subresolution_l (M, N, None, NE, UE, NEk, UEk, NS, US, N0, U0, {#}, Q)
    (M, fmupd C' (E, False) N, None, NE, UE, NEk, UEk, NS, add_mset (mset (N  C'))  US, N0, U0, {#}, Q)
  if
    C ∈# dom_m N
    C' ∈# dom_m N
    mset (N  C) = add_mset L D
    mset (N  C') = add_mset (-L) D'
    count_decided M = 0 D ⊆# D'
    mset E = remdups_mset D' length E  2 distinct E L ∈# mset E. undefined_lit M L
    C'  set (get_all_mark_of_propagated M) irred N C ¬irred N C' |
twl_subresolution_LI_unit:
  cdcl_twl_subresolution_l (M, N, None, NE, UE, NEk, UEk, NS, US, N0, U0, {#}, Q)
   (Propagated K 0 # M, fmdrop C' N, None, NE, UE, NEk, add_mset {#K#} UEk,  NS,  add_mset (mset (N  C')) US, N0, U0, {#}, add_mset (-K) Q)
  if
    C ∈# dom_m N
    C' ∈# dom_m N
    mset (N  C) = add_mset L D
    mset (N  C') = add_mset (-L) D'
    count_decided M = 0 D ⊆# D'
    remdups_mset D' = {#K#}
    undefined_lit M K
    C'  set (get_all_mark_of_propagated M) irred N C ¬irred N C' |
twl_subresolution_IL_nonunit:
  cdcl_twl_subresolution_l (M, N, None, NE, UE, NEk, UEk, NS, US, N0, U0, {#}, Q)
    (M, fmupd C' (E, True) N, None, NE, UE, NEk, UEk, add_mset (mset (N  C')) NS, US, N0, U0, {#}, Q)
  if
    C ∈# dom_m N
    C' ∈# dom_m N
    mset (N  C) = add_mset L D
    mset (N  C') = add_mset (-L) D'
    count_decided M = 0 D ⊆# D'
    mset E = remdups_mset D' length E  2 distinct E L ∈# mset E. undefined_lit M L
    C'  set (get_all_mark_of_propagated M) ¬irred N C irred N C' |
twl_subresolution_IL_unit:
  cdcl_twl_subresolution_l (M, N, None, NE, UE, NEk, UEk, NS, US, N0, U0, {#}, Q)
    (Propagated K 0 # M, fmdrop C' N, None, NE, UE, add_mset {#K#} NEk, UEk, add_mset (mset (N  C')) NS, US, N0, U0, {#}, add_mset (-K) Q)
  if
    C ∈# dom_m N
    C' ∈# dom_m N
    mset (N  C) = add_mset L D
    mset (N  C') = add_mset (-L) D'
    count_decided M = 0 D ⊆# D'
    remdups_mset D' = {#K#}
    undefined_lit M K
    C'  set (get_all_mark_of_propagated M) ¬irred N C irred N C'


lemma convert_lits_l_update_sel2:
  C ∈# dom_m N  C  set (get_all_mark_of_propagated M)  
  (M, M')  convert_lits_l (fmupd C (C', b) N) E  (M, M')  convert_lits_l N E
  unfolding convert_lits_l_def list_rel_def in_pair_collect_simp
  apply (rule iffI; (rule list.rel_mono_strong, assumption))
  apply (auto simp: convert_lits_l_def list_rel_def p2rel_def convert_lit.simps
    cdclW_restart_mset.in_get_all_mark_of_propagated_in_trail)
  done

lemma twl_subresolution_II_nonunit_simps:
  cdcl_twl_subresolution S T
  if
    S = (M, N, U, None, NE, UE, NS, US, N0, U0, {#}, Q)
    T = (M, add_mset E (remove1_mset C' N), U, None, NE, UE, add_mset (clause C') NS, US, N0, U0, {#}, Q)
    clause C = add_mset L D
    clause C' = add_mset (-L) D'
    count_decided M = 0 D ⊆# D'  ¬tautology (D + D')
    clause E = remdups_mset D' size (watched E) = 2 struct_wf_twl_cls E
    L ∈# clause E. undefined_lit M L
    C ∈# N
    C' ∈# N
  using cdcl_twl_subresolution.twl_subresolution_II_nonunit[of C L D C' D' M E N - {#C, C'#} U] that
  by (auto dest!: multi_member_split simp: add_mset_eq_add_mset add_mset_commute)

lemma twl_subresolution_II_unit_simps:
  cdcl_twl_subresolution S T
  if
    S = (M, N, U, None, NE, UE, NS, US, N0, U0, {#}, Q)
    T = (Propagated K {#K#} # M, (remove1_mset C' N), U, None, add_mset {#K#} NE, UE, add_mset (clause C') NS, US,  N0, U0, {#}, add_mset (-K) Q)
    clause C = add_mset L D
    clause C' = add_mset (-L) D'
    count_decided M = 0 D ⊆# D'  ¬tautology (D + D')
    remdups_mset D' = {#K#}
    undefined_lit M K
    C ∈# N
    C' ∈# N
  using cdcl_twl_subresolution.twl_subresolution_II_unit[of C L D C' D' M K N - {#C, C'#} U] that
  by (auto dest!: multi_member_split simp: add_mset_eq_add_mset add_mset_commute)

lemma twl_subresolution_LL_nonunit_simps:
  cdcl_twl_subresolution S T
  if
    S = (M, N, U, None, NE, UE, NS, US, N0, U0, {#}, Q)
    T = (M, N, add_mset E (remove1_mset C' U), None, NE, UE, NS, add_mset (clause C') US, N0, U0, {#}, Q)
    clause C = add_mset L D
    clause C' = add_mset (-L) D'
    count_decided M = 0 D ⊆# D'  ¬tautology (D + D')
    clause E = remdups_mset D' size (watched E) = 2 struct_wf_twl_cls E
    L ∈# clause E. undefined_lit M L
    C ∈# U
    C' ∈# U
  using cdcl_twl_subresolution.twl_subresolution_LL_nonunit[of C L D C' D' M E N U - {#C, C'#}] that
  by (auto dest!: multi_member_split simp: add_mset_eq_add_mset add_mset_commute)

lemma twl_subresolution_LL_unit_simps:
  cdcl_twl_subresolution S T
  if
    S = (M, N, U, None, NE, UE, NS, US, N0, U0, {#}, Q)
    T = (Propagated K {#K#} # M, (N), remove1_mset C' U, None, NE, add_mset {#K#} UE,
    NS, add_mset (clause C') US, N0, U0, {#}, add_mset (-K) Q)
    clause C = add_mset L D
    clause C' = add_mset (-L) D'
    count_decided M = 0 D ⊆# D'  ¬tautology (D + D')
    remdups_mset D' = {#K#}
    undefined_lit M K
    C ∈# U
    C' ∈# U
  using cdcl_twl_subresolution.twl_subresolution_LL_unit[of C L D C' D' M K N U - {#C, C'#}] that
  by (auto dest!: multi_member_split simp: add_mset_eq_add_mset add_mset_commute)

lemma twl_subresolution_LI_nonunit_simps:
  cdcl_twl_subresolution S T
  if
    S = (M, N, U, None, NE, UE, NS, US, N0, U0, {#}, Q)
    T = (M, N, add_mset E (remove1_mset C' U), None, NE, UE, NS, add_mset (clause C') US, N0, U0, {#}, Q)
    clause C = add_mset L D
    clause C' = add_mset (-L) D'
    count_decided M = 0 D ⊆# D'  ¬tautology (D + D')
    clause E = remdups_mset D' size (watched E) = 2 struct_wf_twl_cls E
    L ∈# clause E. undefined_lit M L
    C ∈# N
    C' ∈# U
  using cdcl_twl_subresolution.twl_subresolution_LI_nonunit[of C L D C' D' M E N - {#C#} U - {#C'#}] that
  by (auto dest!: multi_member_split simp: add_mset_eq_add_mset add_mset_commute)

lemma twl_subresolution_LI_unit_simps:
  cdcl_twl_subresolution S T
  if
    S = (M, N, U, None, NE, UE, NS, US, N0, U0, {#}, Q)
    T = (Propagated K {#K#} # M, (N), remove1_mset C' U, None, NE, add_mset {#K#} UE,
    NS, add_mset (clause C') US, N0, U0, {#}, add_mset (-K) Q)
    clause C = add_mset L D
    clause C' = add_mset (-L) D'
    count_decided M = 0 D ⊆# D'  ¬tautology (D + D')
    remdups_mset D' = {#K#}
    undefined_lit M K
    C ∈# N
    C' ∈# U
  using cdcl_twl_subresolution.twl_subresolution_LI_unit[of C L D C' D' M K N - {#C#} U - {#C'#}] that
  by (auto dest!: multi_member_split simp: add_mset_eq_add_mset add_mset_commute)

lemma twl_subresolution_IL_nonunit_simps:
  cdcl_twl_subresolution S T
  if
    S = (M, N, U, None, NE, UE, NS, US, N0, U0, {#}, Q)
    T = (M, add_mset E (remove1_mset C' N), U, None, NE, UE, add_mset (clause C') NS, US, N0, U0, {#}, Q)
    clause C = add_mset L D
    clause C' = add_mset (-L) D'
    count_decided M = 0 D ⊆# D'  ¬tautology (D + D')
    clause E = remdups_mset D' size (watched E) = 2 struct_wf_twl_cls E
    L ∈# clause E. undefined_lit M L
    C' ∈# N
    C ∈# U
  using cdcl_twl_subresolution.twl_subresolution_IL_nonunit[of C L D C' D' M E N - {#C'#} U - {#C#}] that
  by (auto dest!: multi_member_split simp: add_mset_eq_add_mset add_mset_commute)

lemma twl_subresolution_IL_unit_simps:
  cdcl_twl_subresolution S T
  if
    S = (M, N, U, None, NE, UE, NS, US, N0, U0, {#}, Q)
    T = (Propagated K {#K#} # M, remove1_mset C' N,  U, None, add_mset {#K#} NE, UE,
    add_mset (clause C') NS, US, N0, U0, {#}, add_mset (-K) Q)
    clause C = add_mset L D
    clause C' = add_mset (-L) D'
    count_decided M = 0 D ⊆# D'  ¬tautology (D + D')
    remdups_mset D' = {#K#}
    undefined_lit M K
    C' ∈# N
    C ∈# U
  using cdcl_twl_subresolution.twl_subresolution_IL_unit[of C L D C' D' M K N - {#C'#} U - {#C#}] that
  by (auto dest!: multi_member_split simp: add_mset_eq_add_mset add_mset_commute)

lemma cdcl_twl_subresolution_l_cdcl_twl_subresolution:
  assumes cdcl_twl_subresolution_l S T and
    SS': (S, S')  twl_st_l None and
    list_invs: twl_list_invs S
  shows T'. (T, T')  twl_st_l None  cdcl_twl_subresolution S' T'
proof -
  have tauto: C∈#dom_m (get_clauses_l S). ¬tautology (mset (get_clauses_l S  C))
    using list_invs unfolding twl_list_invs_def
    by (auto simp: dest!: multi_member_split)
  have H1: C ∈# dom_m N 
    C' ∈# dom_m N 
    mset (N  C) = add_mset L D 
    mset (N  C') = add_mset (- L) D' 
    D ⊆# D' 
    C∈#dom_m N. ¬ tautology (mset (N  C))  tautology (D + D')  False for C C' L D N D'
   by (metis mset_subset_eqD tautology_add_mset tautology_minus tautology_union)
  from assms tauto show ?thesis
    apply -
    supply [simp] = convert_lits_l_update_sel2
    apply (induction rule: cdcl_twl_subresolution_l.induct)
    subgoal for C N C' L D D' M E NE UE NEk UEk NS US N0 U0 Q
      using H1[of C N C' L D D']
      apply (auto simp: twl_st_l_def)
      apply (rule_tac x = x in exI)
      apply auto
      apply (rule twl_subresolution_II_nonunit_simps[where
          C' = (TWL_Clause (mset (watched_l (N  C'))) (mset (unwatched_l (N  C')))) and
        C = (TWL_Clause (mset (watched_l (N  C))) (mset (unwatched_l (N  C))))])
      apply (auto simp: init_clss_l_mapsto_upd image_mset_remove1_mset_if
        learned_clss_l_mapsto_upd_irrel
        mset_take_mset_drop_mset'
        )
      done
    subgoal for C N C' L D D' M K NE UE NS US N0 U0 Q
      using H1[of C N C' L D D']
      apply (auto simp: twl_st_l_def)
      apply (rule_tac x = Propagated K {#K#} # x in exI)
      apply (auto simp: convert_lit.simps convert_lits_l_drop convert_lits_l_add_mset)
      apply (rule twl_subresolution_II_unit_simps[where
          C' = (TWL_Clause (mset (watched_l (N  C'))) (mset (unwatched_l (N  C')))) and
          C = (TWL_Clause (mset (watched_l (N  C))) (mset (unwatched_l (N  C))))])
      apply (auto simp: init_clss_l_mapsto_upd image_mset_remove1_mset_if
        learned_clss_l_mapsto_upd_irrel
        mset_take_mset_drop_mset'
        init_clss_l_fmdrop learned_clss_l_l_fmdrop_irrelev; fail
        )+
      done
    subgoal for C N C' L D D' M E NE UE NS US N0 U0 Q
      apply (auto simp: twl_st_l_def)
      apply (rule_tac x = x in exI)
      apply (auto simp: convert_lit.simps convert_lits_l_drop convert_lits_l_add_mset
        intro!: twl_subresolution_LL_nonunit_simps[where
          C' = (TWL_Clause (mset (watched_l (N  C'))) (mset (unwatched_l (N  C')))) and
        C = (TWL_Clause (mset (watched_l (N  C))) (mset (unwatched_l (N  C))))]
        simp:  init_clss_l_mapsto_upd image_mset_remove1_mset_if
        learned_clss_l_mapsto_upd_irrel
        mset_take_mset_drop_mset'
        init_clss_l_fmdrop_irrelev learned_clss_l_l_fmdrop
        init_clss_l_fmdrop learned_clss_l_l_fmdrop_irrelev
        learned_clss_l_mapsto_upd_irrel
        mset_take_mset_drop_mset' init_clss_l_mapsto_upd_irrelev
          init_clss_l_fmdrop_irrelev learned_clss_l_l_fmdrop
          init_clss_l_mapsto_upd_irrel_notin init_clss_l_mapsto_upd_irrel
          learned_clss_l_fmupd_if learned_clss_l_mapsto_upd
        init_clss_l_fmdrop learned_clss_l_l_fmdrop_irrelev; fail
          )+
      done
    subgoal for C N C' L D D' M K NE UE NS US N0 U0 Q
      using H1[of C N C' L D D']
      apply (auto simp: twl_st_l_def)
      apply (rule_tac x = Propagated K {#K#} # x in exI)
      apply (auto simp: convert_lit.simps convert_lits_l_drop convert_lits_l_add_mset
        intro!: twl_subresolution_LL_unit_simps[where
          C' = (TWL_Clause (mset (watched_l (N  C'))) (mset (unwatched_l (N  C')))) and
        C = (TWL_Clause (mset (watched_l (N  C))) (mset (unwatched_l (N  C))))]
        simp:  init_clss_l_mapsto_upd image_mset_remove1_mset_if
        learned_clss_l_mapsto_upd_irrel
        mset_take_mset_drop_mset'
        init_clss_l_fmdrop_irrelev learned_clss_l_l_fmdrop
        init_clss_l_fmdrop learned_clss_l_l_fmdrop_irrelev
        learned_clss_l_mapsto_upd_irrel
        mset_take_mset_drop_mset' init_clss_l_mapsto_upd_irrelev
          init_clss_l_fmdrop_irrelev learned_clss_l_l_fmdrop
          init_clss_l_mapsto_upd_irrel_notin init_clss_l_mapsto_upd_irrel
          learned_clss_l_fmupd_if learned_clss_l_mapsto_upd
        init_clss_l_fmdrop learned_clss_l_l_fmdrop_irrelev
          )
        done
    subgoal for C N C' L D D' M E NE UE NS US N0 U0 Q
      using H1[of C N C' L D D']
      apply (auto simp: twl_st_l_def)
      apply (rule_tac x = get_trail S' in exI)
      apply (auto simp: convert_lit.simps convert_lits_l_drop convert_lits_l_add_mset
        intro!: twl_subresolution_LI_nonunit_simps[where
          C' = (TWL_Clause (mset (watched_l (N  C'))) (mset (unwatched_l (N  C')))) and
        C = (TWL_Clause (mset (watched_l (N  C))) (mset (unwatched_l (N  C))))]
        simp:  init_clss_l_mapsto_upd image_mset_remove1_mset_if
        learned_clss_l_mapsto_upd_irrel
        mset_take_mset_drop_mset'
        init_clss_l_fmdrop_irrelev learned_clss_l_l_fmdrop
        init_clss_l_fmdrop learned_clss_l_l_fmdrop_irrelev
        learned_clss_l_mapsto_upd_irrel
        mset_take_mset_drop_mset' init_clss_l_mapsto_upd_irrelev
          init_clss_l_fmdrop_irrelev learned_clss_l_l_fmdrop
          init_clss_l_mapsto_upd_irrel_notin init_clss_l_mapsto_upd_irrel
          learned_clss_l_fmupd_if learned_clss_l_mapsto_upd
        init_clss_l_fmdrop learned_clss_l_l_fmdrop_irrelev; fail
          )+
        done
    subgoal for C N C' L D D' M K NE UE NS US N0 U0 Q
      using H1[of C N C' L D D']
      apply (auto simp: twl_st_l_def)
      apply (rule_tac x = Propagated K {#K#} # x in exI)
      apply (auto simp: convert_lit.simps convert_lits_l_drop convert_lits_l_add_mset
        intro!: twl_subresolution_LI_unit_simps[where
          C' = (TWL_Clause (mset (watched_l (N  C'))) (mset (unwatched_l (N  C')))) and
        C = (TWL_Clause (mset (watched_l (N  C))) (mset (unwatched_l (N  C))))]
        simp:  init_clss_l_mapsto_upd image_mset_remove1_mset_if
        learned_clss_l_mapsto_upd_irrel
        mset_take_mset_drop_mset'
        init_clss_l_fmdrop_irrelev learned_clss_l_l_fmdrop
        init_clss_l_fmdrop learned_clss_l_l_fmdrop_irrelev
        learned_clss_l_mapsto_upd_irrel
        mset_take_mset_drop_mset' init_clss_l_mapsto_upd_irrelev
          init_clss_l_fmdrop_irrelev learned_clss_l_l_fmdrop
          init_clss_l_mapsto_upd_irrel_notin init_clss_l_mapsto_upd_irrel
          learned_clss_l_fmupd_if learned_clss_l_mapsto_upd
        init_clss_l_fmdrop learned_clss_l_l_fmdrop_irrelev
          )
        done
    subgoal for C N C' L D D' M E NE UE NS US N0 U0 Q
      using H1[of C N C' L D D']
      apply (auto simp: twl_st_l_def)
      apply (rule_tac x = get_trail S' in exI)
      apply (auto simp: convert_lit.simps convert_lits_l_drop convert_lits_l_add_mset
        intro!: twl_subresolution_IL_nonunit_simps[where
          C' = (TWL_Clause (mset (watched_l (N  C'))) (mset (unwatched_l (N  C')))) and
        C = (TWL_Clause (mset (watched_l (N  C))) (mset (unwatched_l (N  C))))]
        simp:  init_clss_l_mapsto_upd image_mset_remove1_mset_if
        learned_clss_l_mapsto_upd_irrel
        mset_take_mset_drop_mset'
        init_clss_l_fmdrop_irrelev learned_clss_l_l_fmdrop
        init_clss_l_fmdrop learned_clss_l_l_fmdrop_irrelev
        learned_clss_l_mapsto_upd_irrel
        mset_take_mset_drop_mset' init_clss_l_mapsto_upd_irrelev
          init_clss_l_fmdrop_irrelev learned_clss_l_l_fmdrop
          init_clss_l_mapsto_upd_irrel_notin init_clss_l_mapsto_upd_irrel
          learned_clss_l_fmupd_if learned_clss_l_mapsto_upd
        init_clss_l_fmdrop learned_clss_l_l_fmdrop_irrelev
        init_clss_l_fmdrop_irrelev learned_clss_l_l_fmdrop
        init_clss_l_fmdrop learned_clss_l_l_fmdrop_irrelev; fail
        )+
        done
    subgoal for C N C' L D D' M K NE UE NS US N0 U0 Q
      using H1[of C N C' L D D']
      apply (auto simp: twl_st_l_def)
      apply (rule_tac x = Propagated K {#K#} # x in exI)
      apply (auto simp: convert_lit.simps convert_lits_l_drop convert_lits_l_add_mset
        intro!: twl_subresolution_IL_unit_simps[where
          C' = (TWL_Clause (mset (watched_l (N  C'))) (mset (unwatched_l (N  C')))) and
        C = (TWL_Clause (mset (watched_l (N  C))) (mset (unwatched_l (N  C))))]
        simp:  init_clss_l_mapsto_upd image_mset_remove1_mset_if
        learned_clss_l_mapsto_upd_irrel
        mset_take_mset_drop_mset'
        init_clss_l_fmdrop_irrelev learned_clss_l_l_fmdrop
        init_clss_l_fmdrop learned_clss_l_l_fmdrop_irrelev
        learned_clss_l_mapsto_upd_irrel
        mset_take_mset_drop_mset' init_clss_l_mapsto_upd_irrelev
          init_clss_l_fmdrop_irrelev learned_clss_l_l_fmdrop
          init_clss_l_mapsto_upd_irrel_notin init_clss_l_mapsto_upd_irrel
          learned_clss_l_fmupd_if learned_clss_l_mapsto_upd
        init_clss_l_fmdrop learned_clss_l_l_fmdrop_irrelev
          )
        done
      done
qed

inductive cdcl_twl_unitres_true_l :: 'v twl_st_l  'v twl_st_l  bool where
  cdcl_twl_unitres_true_l (M, N, None, NE, UE, NEk, UEk, NS, US, N0, U0, {#}, Q)
  (M, fmdrop C N, None, add_mset (mset (N  C)) NE, UE, NEk, UEk, NS, US, N0, U0, {#}, Q)
  if L ∈# mset (N  C) get_level M L = 0 L  lits_of_l M
    C ∈# dom_m N irred N C
    C  set (get_all_mark_of_propagated M) |
  cdcl_twl_unitres_true_l (M, N, None, NE, UE, NEk, UEk, NS, US, N0, U0, {#}, Q)
    (M, fmdrop C N, None, NE, add_mset (mset (N  C)) UE, NEk, UEk, NS, US, N0, U0, {#}, Q)
  if L ∈# mset (N  C) get_level M L = 0 L  lits_of_l M
    C ∈# dom_m N ¬irred N C
    C  set (get_all_mark_of_propagated M)

lemma cdcl_twl_unitres_true_intro1:
  cdcl_twl_unitres_true S T
  if S = (M, N, U, None, NE, UE, NS, US, N0, U0, {#}, Q)
    T = (M, remove1_mset C N, U, None, add_mset (clause C) NE, UE, NS, US, N0, U0, {#}, Q)
    L ∈# clause C get_level M L = 0 L  lits_of_l M C ∈# N
  using cdcl_twl_unitres_true.intros(1)[of L C M N - {#C#} U] that
  by auto

lemma cdcl_twl_unitres_true_intro2:
  cdcl_twl_unitres_true S T
  if  S = (M, N, U, None, NE, UE, NS, US, N0, U0, {#}, Q)
    T = (M, N, remove1_mset C U, None, NE, add_mset (clause C) UE, NS, US, N0, U0, {#}, Q)
    L ∈# clause C get_level M L = 0 L  lits_of_l M C ∈# U
  using cdcl_twl_unitres_true.intros(2)[of L C M N U - {#C#}] that
  by auto

lemma cdcl_twl_unitres_true_l_cdcl_twl_unitres_true:
  assumes cdcl_twl_unitres_true_l S T and
    SS': (S, S')  twl_st_l None
  shows T'. (T, T')  twl_st_l None  cdcl_twl_unitres_true S' T'
  using assms
  apply (induction rule: cdcl_twl_unitres_true_l.induct)
  subgoal for L N C M NE UE NS US Q
    apply (auto simp: twl_st_l_def)
    apply (rule_tac x=x in exI)
    apply (auto simp: convert_lit.simps convert_lits_l_drop convert_lits_l_add_mset
      intro!: cdcl_twl_unitres_true_intro1[where
      C = (TWL_Clause (mset (watched_l (N  C))) (mset (unwatched_l (N  C))))]
      simp:  init_clss_l_mapsto_upd image_mset_remove1_mset_if
      learned_clss_l_mapsto_upd_irrel
      mset_take_mset_drop_mset'
      init_clss_l_fmdrop_irrelev learned_clss_l_l_fmdrop
      init_clss_l_fmdrop learned_clss_l_l_fmdrop_irrelev
      learned_clss_l_mapsto_upd_irrel convert_lits_l_drop
      mset_take_mset_drop_mset' init_clss_l_mapsto_upd_irrelev
      init_clss_l_fmdrop_irrelev learned_clss_l_l_fmdrop
      init_clss_l_mapsto_upd_irrel_notin init_clss_l_mapsto_upd_irrel
      learned_clss_l_fmupd_if learned_clss_l_mapsto_upd
      init_clss_l_fmdrop learned_clss_l_l_fmdrop_irrelev
      )
    done
  subgoal for L N C M NE UE NS US N0 U0 Q
    apply (auto simp: twl_st_l_def)
    apply (rule_tac x=x in exI)
    apply (auto simp: convert_lit.simps convert_lits_l_drop convert_lits_l_add_mset
      intro!: cdcl_twl_unitres_true_intro2[where
      C = (TWL_Clause (mset (watched_l (N  C))) (mset (unwatched_l (N  C))))]
      simp:  init_clss_l_mapsto_upd image_mset_remove1_mset_if
      learned_clss_l_mapsto_upd_irrel
      mset_take_mset_drop_mset'
      init_clss_l_fmdrop_irrelev learned_clss_l_l_fmdrop
      init_clss_l_fmdrop learned_clss_l_l_fmdrop_irrelev
      learned_clss_l_mapsto_upd_irrel convert_lits_l_drop
      mset_take_mset_drop_mset' init_clss_l_mapsto_upd_irrelev
      init_clss_l_fmdrop_irrelev learned_clss_l_l_fmdrop
      init_clss_l_mapsto_upd_irrel_notin init_clss_l_mapsto_upd_irrel
      learned_clss_l_fmupd_if learned_clss_l_mapsto_upd
      init_clss_l_fmdrop learned_clss_l_l_fmdrop_irrelev
      )
    done
  done

lemma cdcl_twl_subresolution_l_list_invs:
  cdcl_twl_subresolution_l S T  twl_list_invs S  twl_list_invs T
  using distinct_mset_dom[of get_clauses_l S] apply -
  by (induction rule: cdcl_twl_subresolution_l.induct)
   (auto simp: twl_list_invs_def cdclW_restart_mset.in_get_all_mark_of_propagated_in_trail
     ran_m_mapsto_upd ran_m_def add_mset_eq_add_mset tautology_add_mset
    dest: in_diffD dest!: multi_member_split)

lemma cdcl_twl_unitres_True_l_list_invs:
  cdcl_twl_unitres_true_l S T  twl_list_invs S  twl_list_invs T
  by (induction rule: cdcl_twl_unitres_true_l.induct)
   (auto simp: twl_list_invs_def cdclW_restart_mset.in_get_all_mark_of_propagated_in_trail
    dest: in_diffD)

inductive cdcl_twl_unitres_l :: 'v twl_st_l  'v twl_st_l  bool where
cdcl_twl_unitres_l (M, N, None, NE, UE, NEk, UEk, NS, US, N0, U0, {#}, Q)
    (M, fmupd D (E, irred N D) N, None, NE, UE, NEk, UEk, add_mset (mset (N  D)) NS, US, N0, U0, {#}, Q)
  if
    D ∈# dom_m N
    count_decided M = 0 and
    mset (N  D) = C +C'
    (mset `# init_clss_lf N + NE + NEk + NS + N0) ⊨psm mset_set (CNot C')
    ¬tautology C distinct_mset C
    struct_wf_twl_cls (twl_clause_of E)
    Multiset.Ball (mset E) (undefined_lit M)
    mset E = C
    D  set (get_all_mark_of_propagated M) irred N D |
cdcl_twl_unitres_l (M, N, None, NE, UE, NEk, UEk, NS, US, N0, U0, {#}, Q)
    (Propagated K 0 # M, fmdrop D N, None, NE, UE, add_mset {#K#} NEk, UEk, add_mset (mset (N  D)) NS, US, N0, U0, {#}, add_mset (-K) Q)
  if
    D ∈# dom_m N
    count_decided M = 0 and
    mset (N  D) = {#K#} +C'
    (mset `# init_clss_lf N + NE + NEk + NS + N0) ⊨psm mset_set (CNot C')
    D  set (get_all_mark_of_propagated M) irred N D
    undefined_lit M K |
cdcl_twl_unitres_l (M, N, None, NE, UE, NEk, UEk, NS, US, N0, U0, {#}, Q)
    (M, fmupd D (E, irred N D) N, None, NE, UE, NEk, UEk, NS, add_mset (mset (N  D)) US, N0, U0, {#}, Q)
  if
    D ∈# dom_m N
    count_decided M = 0 and
    mset (N  D) = C +C'
    (mset `# init_clss_lf N + NE + NEk + NS + N0) ⊨psm mset_set (CNot C')
    ¬tautology C distinct_mset C
    struct_wf_twl_cls (twl_clause_of E)
    Multiset.Ball (mset E) (undefined_lit M)
    mset E = C
    D  set (get_all_mark_of_propagated M) ¬irred N D
    atms_of (mset E)  atms_of_mm (mset `# init_clss_lf N)  atms_of_mm NE  atms_of_mm NEk 
       atms_of_mm NS  atms_of_mm N0 |
cdcl_twl_unitres_l (M, N, None, NE, UE, NEk, UEk, NS, US, N0, U0, {#}, Q)
    (Propagated K 0 # M, fmdrop D N, None, NE, UE, NEk, add_mset {#K#} UEk, NS, add_mset (mset (N  D)) US, N0, U0, {#}, add_mset (-K) Q)
  if
    D ∈# dom_m N
    count_decided M = 0 and
    mset (N  D) = {#K#} +C'
    (mset `# init_clss_lf N + NE + NEk + NS + N0) ⊨psm mset_set (CNot C')
    D  set (get_all_mark_of_propagated M) ¬irred N D
    undefined_lit M K
    atm_of K  atms_of_mm (mset `# init_clss_lf N)  atms_of_mm NE  atms_of_mm NEk 
       atms_of_mm NS  atms_of_mm N0 |
cdcl_twl_unitres_l (M, N, None, NE, UE, NEk, UEk, NS, US, N0, U0, {#}, Q)
    (M, fmdrop D N, Some {#}, NE, UE, NEk, UEk, NS, add_mset (mset (N  D)) US, N0, add_mset {#} U0, {#}, {#})
  if
    D ∈# dom_m N
    (mset `# init_clss_lf N + NE + NEk + NS + N0) ⊨psm mset_set (CNot (mset (N  D)))
    ¬irred N D
    count_decided M = 0
    D  set (get_all_mark_of_propagated M) |
cdcl_twl_unitres_l (M, N, None, NE, UE, NEk, UEk, NS, US, N0, U0, {#}, Q)
    (M, fmdrop D N, Some {#}, NE, UE, NEk, UEk, add_mset (mset (N  D)) NS, US, add_mset {#} N0, U0, {#}, {#})
  if
    D ∈# dom_m N
    (mset `# init_clss_lf N + NE + NEk + NS + N0) ⊨psm mset_set (CNot (mset (N  D)))
    irred N D
    count_decided M = 0
    D  set (get_all_mark_of_propagated M)

lemma cdcl_twl_unitres_I1:
  cdcl_twl_unitres S T
  if S = (M, N, U, None, NE, UE, NS, US, N0, U0, {#}, Q)
    T = (M, add_mset E (remove1_mset D N), U, None, NE, UE, add_mset (clause D)  NS, US, N0, U0, {#}, Q)
    count_decided M = 0 and
    D ∈# N
    clause D = C+C'
    (clauses N + NE + NS + N0) ⊨psm mset_set (CNot C')
    ¬tautology C distinct_mset C
    struct_wf_twl_cls E
    Multiset.Ball (clause E) (undefined_lit M)
    clause E = C
  using that cdcl_twl_unitres.intros(1)[of M D C C' N - {#D#} NE NS N0 E U UE US U0 Q]
  by (auto dest!: multi_member_split)

lemma cdcl_twl_unitres_I2:
  cdcl_twl_unitres S T
  if S = (M, N, U, None, NE, UE, NS, US, N0, U0, {#}, Q)
    T = (Propagated K {#K#} # M, (remove1_mset D N), U, None, add_mset {#K#} NE, UE, add_mset (clause D)  NS, US, N0, U0, {#}, add_mset (-K) Q)
    count_decided M = 0 and
    D ∈# N
    clause D = {#K#}+C'
    (clauses N + NE + NS + N0) ⊨psm mset_set (CNot C')
    undefined_lit M K
  using that cdcl_twl_unitres.intros(2)[of M D {#K#} C' N - {#D#} NE NS N0 K U UE US U0 Q]
  by (auto dest!: multi_member_split)

lemma cdcl_twl_unitres_I3:
  cdcl_twl_unitres S T
  if S = (M, N, U, None, NE, UE, NS, US, N0, U0, {#}, Q)
    T = (M, N, add_mset E (remove1_mset D U), None, NE, UE, NS, add_mset (clause D) US, N0, U0, {#}, Q)
    count_decided M = 0 and
    D ∈# U
    clause D = C+C'
    (clauses N + NE + NS + N0) ⊨psm mset_set (CNot C')
    ¬tautology C distinct_mset C
    struct_wf_twl_cls E
    Multiset.Ball (clause E) (undefined_lit M)
    clause E = C
    atms_of C  atms_of_ms (clause ` set_mset N)  atms_of_mm NE  atms_of_mm NS  atms_of_mm N0
  using that cdcl_twl_unitres.intros(3)[of M D C C' N NE NS N0 E U - {#D#} UE US U0 Q]
  by (auto dest!: multi_member_split)

lemma cdcl_twl_unitres_I4:
  cdcl_twl_unitres S T
  if S = (M, N, U, None, NE, UE, NS, US, N0, U0, {#}, Q)
    T = (Propagated K {#K#} # M, N, remove1_mset D U, None, NE, add_mset {#K#} UE, NS, add_mset (clause D) US, N0, U0, {#}, add_mset (-K) Q)
    count_decided M = 0 and
    D ∈# U
    clause D = {#K#}+C'
    (clauses N + NE + NS + N0) ⊨psm mset_set (CNot C')
    undefined_lit M K
    atm_of K  atms_of_ms (clause ` set_mset N)  atms_of_mm NE  atms_of_mm NS  atms_of_mm N0
  using that cdcl_twl_unitres.intros(4)[of M D {#K#} C' N NE NS N0 K U - {#D#} UE US U0 Q]
  by (auto dest!: multi_member_split)

lemma cdcl_twl_unitres_I5:
  cdcl_twl_unitres S T
  if S = (M, N, U, None, NE, UE, NS, US, N0, U0, {#}, Q)
    T = (M, N, remove1_mset D U, Some {#}, NE, UE, NS, add_mset (clause D) US, N0, add_mset {#} U0, {#}, {#})
    count_decided M = 0 and
    D ∈# U
    (clauses N + NE + NS + N0) ⊨psm mset_set (CNot (clause D))
  using that cdcl_twl_unitres.intros(5)[of M N NE NS N0 D remove1_mset D U UE US U0 Q]
  by (auto dest!: multi_member_split)

lemma cdcl_twl_unitres_I6:
  cdcl_twl_unitres S T
  if S = (M, N, U, None, NE, UE, NS, US, N0, U0, {#}, Q)
    T = (M, remove1_mset D N, U, Some {#}, NE, UE, add_mset (clause D) NS, US, add_mset {#} N0, U0, {#}, {#})
    count_decided M = 0 and
    D ∈# N
    (clauses (add_mset D N) + NE + NS + N0) ⊨psm mset_set (CNot (clause D))
  using that cdcl_twl_unitres.intros(6)[of M D remove1_mset D N NE NS N0 U UE US U0 Q]
  by (auto dest!: multi_member_split)

lemma cdcl_twl_unitres_l_cdcl_twl_unitres:
  assumes cdcl_twl_unitres_l S T and
    SS': (S, S')  twl_st_l None
  shows T'. (T, T')  twl_st_l None  cdcl_twl_unitres S' T'
  using assms
  apply (induction rule: cdcl_twl_unitres_l.induct)
  subgoal for D N M C C' NE NEk NS N0 E UE UEk US U0 Q
    apply (auto simp: twl_st_l_def)[]
    apply (rule_tac x=x in exI)
    apply (auto simp: twl_st_l_def
      intro!: cdcl_twl_unitres_I1[where E= twl_clause_of E and D = twl_clause_of (N  D)]
      simp:  init_clss_l_mapsto_upd image_mset_remove1_mset_if
      learned_clss_l_mapsto_upd_irrel
      mset_take_mset_drop_mset'
      init_clss_l_fmdrop_irrelev learned_clss_l_l_fmdrop
      init_clss_l_fmdrop learned_clss_l_l_fmdrop_irrelev Un_assoc
      learned_clss_l_mapsto_upd_irrel convert_lits_l_drop
      mset_take_mset_drop_mset' init_clss_l_mapsto_upd_irrelev
      init_clss_l_fmdrop_irrelev learned_clss_l_l_fmdrop convert_lits_l_update_sel2
      init_clss_l_mapsto_upd_irrel_notin init_clss_l_mapsto_upd_irrel
      learned_clss_l_fmupd_if learned_clss_l_mapsto_upd
      init_clss_l_fmdrop learned_clss_l_l_fmdrop_irrelev)[]
    done
  subgoal for D N M K C' NE NS N0 UE US U0 Q
    apply (auto simp: twl_st_l_def)[]
    apply (rule_tac x= Propagated K {#K#} # x in exI)
    apply (auto simp: twl_st_l_def
      intro!: cdcl_twl_unitres_I2[where K=K and D = twl_clause_of (N  D)]
      simp:  init_clss_l_mapsto_upd image_mset_remove1_mset_if
      learned_clss_l_mapsto_upd_irrel
      mset_take_mset_drop_mset'
      init_clss_l_fmdrop_irrelev learned_clss_l_l_fmdrop
      init_clss_l_fmdrop learned_clss_l_l_fmdrop_irrelev Un_assoc
      learned_clss_l_mapsto_upd_irrel convert_lits_l_drop
      mset_take_mset_drop_mset' init_clss_l_mapsto_upd_irrelev
      init_clss_l_fmdrop_irrelev learned_clss_l_l_fmdrop convert_lits_l_update_sel2
      init_clss_l_mapsto_upd_irrel_notin init_clss_l_mapsto_upd_irrel convert_lits_l_add_mset
      learned_clss_l_fmupd_if learned_clss_l_mapsto_upd convert_lit.intros(3)
      init_clss_l_fmdrop learned_clss_l_l_fmdrop_irrelev)[]
    done
  subgoal for D N M C C' NE NEk NS N0 E UE UEk US U0 Q
    apply (auto simp: twl_st_l_def)[]
    apply (rule_tac x=x in exI)
    apply (auto 5 3 simp: twl_st_l_def
      intro!: cdcl_twl_unitres_I3[where E= twl_clause_of E and D = twl_clause_of (N  D)]
      simp:  init_clss_l_mapsto_upd image_mset_remove1_mset_if
      learned_clss_l_mapsto_upd_irrel
      mset_take_mset_drop_mset'
      init_clss_l_fmdrop_irrelev learned_clss_l_l_fmdrop
      init_clss_l_fmdrop learned_clss_l_l_fmdrop_irrelev Un_assoc
      learned_clss_l_mapsto_upd_irrel convert_lits_l_drop
      mset_take_mset_drop_mset' init_clss_l_mapsto_upd_irrelev
      init_clss_l_fmdrop_irrelev learned_clss_l_l_fmdrop convert_lits_l_update_sel2
      init_clss_l_mapsto_upd_irrel_notin init_clss_l_mapsto_upd_irrel
      learned_clss_l_fmupd_if learned_clss_l_mapsto_upd image_image
      init_clss_l_fmdrop learned_clss_l_l_fmdrop_irrelev)[]
    done
  subgoal for D N M K C' NE NS N0 UE US U0 Q
    apply (auto simp: twl_st_l_def; rule_tac x= Propagated K {#K#} # x in exI)
    apply (auto simp: twl_st_l_def
      intro!: cdcl_twl_unitres_I4[where K=K and D = twl_clause_of (N  D)]
      simp:  init_clss_l_mapsto_upd image_mset_remove1_mset_if
      learned_clss_l_mapsto_upd_irrel
      mset_take_mset_drop_mset'
      init_clss_l_fmdrop_irrelev learned_clss_l_l_fmdrop
      init_clss_l_fmdrop learned_clss_l_l_fmdrop_irrelev image_image
      learned_clss_l_mapsto_upd_irrel convert_lits_l_drop Un_assoc
      mset_take_mset_drop_mset' init_clss_l_mapsto_upd_irrelev
      init_clss_l_fmdrop_irrelev learned_clss_l_l_fmdrop convert_lits_l_update_sel2
      init_clss_l_mapsto_upd_irrel_notin init_clss_l_mapsto_upd_irrel convert_lits_l_add_mset
      learned_clss_l_fmupd_if learned_clss_l_mapsto_upd convert_lit.intros(3)
      init_clss_l_fmdrop learned_clss_l_l_fmdrop_irrelev)
    done
  subgoal for D N NE NS N0 M UE US U0 Q
    by (auto simp: twl_st_l_def; rule_tac x= x in exI)
     (auto simp: twl_st_l_def
      intro!: cdcl_twl_unitres_I5[where D = twl_clause_of (N  D)]
      simp:  init_clss_l_mapsto_upd image_mset_remove1_mset_if
      learned_clss_l_mapsto_upd_irrel
      mset_take_mset_drop_mset'
      init_clss_l_fmdrop_irrelev learned_clss_l_l_fmdrop
      init_clss_l_fmdrop learned_clss_l_l_fmdrop_irrelev image_image
      learned_clss_l_mapsto_upd_irrel convert_lits_l_drop Un_assoc
      mset_take_mset_drop_mset' init_clss_l_mapsto_upd_irrelev
      init_clss_l_fmdrop_irrelev learned_clss_l_l_fmdrop convert_lits_l_update_sel2
      init_clss_l_mapsto_upd_irrel_notin init_clss_l_mapsto_upd_irrel convert_lits_l_add_mset
      learned_clss_l_fmupd_if learned_clss_l_mapsto_upd convert_lit.intros(3)
      init_clss_l_fmdrop learned_clss_l_l_fmdrop_irrelev)
    subgoal for D N NE NS N0 M UE US U0 Q
      by (auto simp: twl_st_l_def; rule_tac x= x in exI)
       (auto simp: twl_st_l_def
        intro!: cdcl_twl_unitres_I6[where D = twl_clause_of (N  D)]
        simp:  init_clss_l_mapsto_upd image_mset_remove1_mset_if
        learned_clss_l_mapsto_upd_irrel
        mset_take_mset_drop_mset'
        init_clss_l_fmdrop_irrelev learned_clss_l_l_fmdrop
        init_clss_l_fmdrop learned_clss_l_l_fmdrop_irrelev image_image
        learned_clss_l_mapsto_upd_irrel convert_lits_l_drop Un_assoc
        mset_take_mset_drop_mset' init_clss_l_mapsto_upd_irrelev
        init_clss_l_fmdrop_irrelev learned_clss_l_l_fmdrop convert_lits_l_update_sel2
        init_clss_l_mapsto_upd_irrel_notin init_clss_l_mapsto_upd_irrel convert_lits_l_add_mset
        learned_clss_l_fmupd_if learned_clss_l_mapsto_upd convert_lit.intros(3)
        init_clss_l_fmdrop learned_clss_l_l_fmdrop_irrelev)
  done

definition simplify_clause_with_unit :: nat  ('v, nat) ann_lits  'v clauses_l  (bool × bool × 'v clauses_l) nres where
  simplify_clause_with_unit = (λC M N. do {
  SPEC(λ(unc, b, N'). fmdrop C N = fmdrop C N'  mset (N'  C) ⊆# mset (N  C)  C ∈# dom_m N' 
     (¬b  (L ∈# mset (N'  C). undefined_lit M L)) 
     (L ∈# mset (N  C) - mset (N'  C). defined_lit M L) 
     (irred N C = irred N' C) 
     (b  (L. L ∈# mset (N  C)  L  lits_of_l M)) 
     (unc  (N = N'  ¬b)))
  })

definition simplify_clause_with_unit_st_pre :: nat  'v twl_st_l  bool where
  simplify_clause_with_unit_st_pre = (λC S.
  C ∈# dom_m (get_clauses_l S)  count_decided (get_trail_l S) = 0  get_conflict_l S = None  clauses_to_update_l S = {#} 
  twl_list_invs S 
  set (get_all_mark_of_propagated (get_trail_l S))  {0} 
  (T. (S, T)  twl_st_l None  twl_struct_invs T 
  cdclW_restart_mset.cdclW_learned_clauses_entailed_by_init ((stateW_of T)))
)

definition simplify_clause_with_unit_st :: nat  'v twl_st_l  'v twl_st_l nres where
  simplify_clause_with_unit_st = (λC (M, N0, D, NE, UE, NEk, UEk, NS, US, N0, U0, WS, Q). do {
    ASSERT(simplify_clause_with_unit_st_pre C  (M, N0, D, NE, UE, NEk, UEk, NS, US, N0, U0, WS, Q));
    ASSERT (C ∈# dom_m N0  count_decided M = 0  D = None  WS = {#}  no_dup M  C  0);
    let S = (M, N0, D, NE, UE, NEk, UEk, NS, US, N0, U0, WS, Q);
    if False
    then RETURN (M, N0, D, NE, UE, NEk, UEk, NS, US, N0, U0, WS, Q)
    else do {
      let E = mset (N0  C);
      let irr = irred N0 C;
      (unc, b, N)  simplify_clause_with_unit C M N0;
      ASSERT(fmdrop C N = fmdrop C N0  irred N C = irred N0 C  mset (N  C) ⊆# mset (N0  C) 
        C ∈# dom_m N);
      if unc then do {
        ASSERT (N = N0);
        RETURN (M, N0, D, NE, UE, NEk, UEk, NS, US, N0, U0, WS, Q)
      }
      else if b then do {
        let T = (M, fmdrop C N, D, (if irr then add_mset E else id) NE, (if ¬irr then add_mset E else id) UE, NEk, UEk, NS, US, N0, U0, WS, Q);
          ASSERT (set_mset (all_init_lits_of_l T) = set_mset (all_init_lits_of_l S));
          ASSERT (set_mset (all_learned_lits_of_l T) = set_mset (all_learned_lits_of_l S));
        RETURN T
      }
      else if size (N  C) = 1
      then do {
        let L = ((N  C) ! 0);
        let T = (Propagated L 0 # M, fmdrop C N, D, NE, UE, (if irr then add_mset {#L#} else id) NEk,
            (if ¬irr then add_mset {#L#} else id)UEk, (if irr then add_mset E else id) NS,
            (if ¬irr then add_mset E else id)US, N0, U0, WS, add_mset (-L) Q);
          ASSERT (set_mset (all_init_lits_of_l T) = set_mset (all_init_lits_of_l S));
          ASSERT (set_mset (all_learned_lits_of_l T) = set_mset (all_learned_lits_of_l S));
          ASSERT (undefined_lit M L  L ∈# all_init_lits_of_l S);
        RETURN T}
      else if size (N  C) = 0
      then do {
          let T = (M, fmdrop C N, Some {#}, NE, UE, NEk, UEk, (if irr then add_mset E else id) NS, (if ¬irr then add_mset E else id) US, (if irr then add_mset {#} else id) N0, (if ¬irr then add_mset {#} else id)U0, WS, {#});
          ASSERT (set_mset (all_init_lits_of_l T) = set_mset (all_init_lits_of_l S));
          ASSERT (set_mset (all_learned_lits_of_l T) = set_mset (all_learned_lits_of_l S));
          RETURN T
      }
      else do {
          let T = (M, N, D, NE, UE, NEk, UEk, (if irr then add_mset E else id) NS, (if ¬irr then add_mset E else id) US, N0, U0, WS, Q);
            ASSERT (set_mset (all_init_lits_of_l T) = set_mset (all_init_lits_of_l S));
            ASSERT (set_mset (all_learned_lits_of_l T) = set_mset (all_learned_lits_of_l S));
            RETURN T
      }
    }
  })

(*TODO Move*)
lemma true_clss_clss_def_more_atms:
  N ⊨ps N'  (I. total_over_m I (N  N'  N'')  consistent_interp I  I ⊨s N  I ⊨s N')
  (is ?A  ?B)
proof (rule iffI)
  assume ?A
  then show ?B
    by (simp add: true_clss_clss_def)
next
  assume ?B
  show ?A
    unfolding true_clss_clss_def
  proof (intro allI impI)
    fix I
    assume tot: total_over_m I (N  N') and
      cons: consistent_interp I and
      IN: I ⊨s N
    let ?I = I  Pos ` {A. A  atms_of_ms N''  A  atm_of ` I}
    have total_over_m ?I (N  N'  N'')
      using tot atms_of_s_def by (fastforce simp: total_over_m_def total_over_set_def)
    moreover have consistent_interp ?I
      using cons unfolding consistent_interp_def
      by (force simp: uminus_lit_swap)
    moreover have ?I ⊨s N
      using IN by auto
    ultimately have ?I ⊨s N'
      using ?B by blast
    then show I ⊨s N'
      by (smt atms_of_ms_mono atms_of_s_def imageE literal.sel(1) mem_Collect_eq
        notin_vars_union_true_clss_true_clss subsetD sup_ge2 tot total_over_m_alt_def)
  qed
qed

(*TODO Move*)
lemma true_clss_clss_def_iff_negation_in_model:
  A ⊨ps CNot C'  (L ∈# C'. A ⊨ps {{#-L#}})
  apply (rule iffI)
  subgoal
    by (simp add: true_clss_clss_in_imp_true_clss_cls)
  subgoal
    apply (subst (asm)true_clss_clss_def_more_atms[of _ _ {C'}])
    apply (auto simp: true_clss_clss_def true_clss_def_iff_negation_in_model
      dest!: multi_member_split)
    done
  done

lemma
  fixes M :: ('v, nat) ann_lit list and N NE UE NS US N0 U0 Q NEk UEk
  defines S  (M, N, None, NE, UE, NEk, UEk, NS, US, N0, U0, {#}, Q)
  assumes
    D ∈# dom_m N
    count_decided M = 0 and
    D  set (get_all_mark_of_propagated M) and
    ST: (S, T)  twl_st_l None and
    st_invs: twl_struct_invs T and
    dec: count_decided (get_trail_l S) = 0 and
    false: L ∈# C'. ¬undefined_lit (get_trail_l S) L
      L ∈# C'. L  lits_of_l (get_trail_l S) and
    ent: cdclW_restart_mset.cdclW_learned_clauses_entailed_by_init (stateW_of T)
  shows cdcl_twl_unitres_l_intros2':
      irred N D  undefined_lit M K  mset (N  D) = {#K#} +C' 
    cdcl_twl_unitres_l (M, N, None, NE, UE, NEk, UEk, NS, US, N0, U0, {#}, Q)
       (Propagated K 0 # M, fmdrop D N, None, NE, UE, add_mset {#K#} NEk, UEk,  add_mset (mset (N  D))  NS, US, N0, U0, {#}, add_mset (-K) Q)
       (is ?A  _  _  ?B) and
    cdcl_twl_unitres_l_intros4':
    ¬irred N D  undefined_lit M K  mset (N  D) = {#K#} +C'  
    cdcl_twl_unitres_l (M, N, None, NE, UE, NEk, UEk, NS, US, N0, U0, {#}, Q)
    (Propagated K 0 # M, fmdrop D N, None, NE, UE, NEk, add_mset {#K#} UEk, NS, add_mset (mset (N  D)) US, N0, U0, {#}, add_mset (-K) Q)
    (is ?A'  _  _  ?B') and
    cdcl_twl_unitres_false_entailed:
       (mset `# init_clss_lf (get_clauses_l S) + get_unit_init_clauses_l S + get_subsumed_init_clauses_l S + get_init_clauses0_l S) ⊨psm mset_set (CNot C') and
    cdcl_twl_unitres_l_intros5':
      ¬irred N D  mset (N  D) = C' 
      cdcl_twl_unitres_l (M, N, None, NE, UE, NEk, UEk, NS, US, N0, U0, {#}, Q)
         (M, fmdrop D N, Some {#}, NE, UE, NEk, UEk, NS, add_mset (mset (N  D)) US, N0,
           add_mset {#} U0, {#}, {#})
       (is ?E  _  ?F)  and
  cdcl_twl_unitres_l_intros6':
    irred N D  mset (N  D) = C' 
    cdcl_twl_unitres_l (M, N, None, NE, UE, NEk, UEk, NS, US, N0, U0, {#}, Q)
    (M, fmdrop D N, Some {#}, NE, UE, NEk, UEk, add_mset (mset (N  D)) NS, US, add_mset {#} N0,
    U0, {#}, {#})
    (is ?E'  _  ?F') and
  cdcl_twl_unitres_l_intros1':
    irred N D  mset (N  D) = mset C +C'  length C  2  ¬tautology (mset (N  D)) 
    Lset C. undefined_lit M L  N' = fmupd D (C, irred N D) N 
      cdcl_twl_unitres_l (M, N, None, NE, UE, NEk, UEk, NS, US, N0, U0, {#}, Q)
        (M, N', None, NE, UE, NEk, UEk, add_mset (mset (N  D)) NS, US, N0, U0, {#}, Q)
    (is ?G  _  _  _  _   _  ?H) and
  cdcl_twl_unitres_l_intros3':
    ¬irred N D  mset (N  D) = mset C +C'  length C  2  ¬tautology (mset (N  D)) 
    Lset C. undefined_lit M L  N' = fmupd D (C, irred N D) N 
    cdcl_twl_unitres_l (M, N, None, NE, UE, NEk, UEk, NS, US, N0, U0, {#}, Q)
    (M, N', None, NE, UE, NEk, UEk, NS, add_mset (mset (N  D)) US, N0, U0, {#}, Q)
    (is ?G'  _  _  _  _  _  ?H')
proof -
  have all_decomposition_implies_m (cdclW_restart_mset.clauses ((stateW_of T)))
    (get_all_ann_decomposition (trail ((stateW_of T)))) and
    alien: cdclW_restart_mset.no_strange_atm (stateW_of T) and
    dist: cdclW_restart_mset.distinct_cdclW_state (stateW_of T) and
    tauto: s∈#learned_clss (stateW_of T). ¬ tautology s and
    nd: no_dup (trail (state_of (pstateW_of T)))
    using st_invs unfolding twl_struct_invs_def unfolding pcdcl_all_struct_invs_def
      cdclW_restart_mset.cdclW_all_struct_inv_def stateW_of_def pcdcl_all_struct_invs_def
      cdclW_restart_mset.cdclW_M_level_inv_def
    by fast+
  moreover have H: (λx. mset (fst x)) ` {a. a ∈# ran_m b  snd a}  (set_mset d  set_mset d')  f  h  U⊨ps
       unmark_l aa 
       (a, aa)  convert_lits_l b (d' + e) 
    (λx. mset (fst x)) ` {a. a ∈# ran_m b  snd a}  (set_mset d  set_mset d')  f  h ⊨ps U 
       - L  lits_of_l a 
    (λx. mset (fst x)) ` {a. a ∈# ran_m b  snd a}  (set_mset d  set_mset d')  f  h ⊨p {#- L#}
    for aa :: ('v, 'v clause) ann_lits and a :: ('v, nat) ann_lit list and b d e f L U h d'
    by (smt in_unmark_l_in_lits_of_l_iff list_of_l_convert_lits_l
      true_clss_clss_in_imp_true_clss_cls true_clss_clss_left_right
      true_clss_clss_union_and)
  moreover have false: L ∈# C'. -L  lits_of_l (get_trail_l S)
    using false nd by (auto dest!: multi_member_split
      simp: Decided_Propagated_in_iff_in_lits_of_l)
  ultimately show ent2: (mset `# init_clss_lf (get_clauses_l S) + get_unit_init_clauses_l S + get_subsumed_init_clauses_l S + get_init_clauses0_l S) ⊨psm mset_set (CNot C')
    using dec ST false ent
    by (cases S; cases T)
     (auto simp: twl_st_l_def all_decomposition_implies_def clauses_def
      get_all_ann_decomposition_count_decided0 image_image mset_take_mset_drop_mset'
      cdclW_restart_mset.cdclW_learned_clauses_entailed_by_init_def true_clss_clss_def_iff_negation_in_model
      dest!: multi_member_split)
  moreover {
    have N  D ∈# init_clss_lf N  N  D ∈# learned_clss_lf N
      using assms(1,2) by (auto simp: ran_m_def)
    then have atms_of (mset (N  D))  atms_of_mm (mset `# init_clss_lf N)  atms_of_mm NE  atms_of_mm NEk  atms_of_mm NS  atms_of_mm N0
      using alien assms(1) ST
      by (fastforce simp: twl_st_l_def clauses_def mset_take_mset_drop_mset' image_image
        cdclW_restart_mset.no_strange_atm_def conj_disj_distribR Collect_disj_eq Collect_conv_if
        dest!: multi_member_split[of _ ran_m N])
   } note H = this
   ultimately show ?A   ?B ?A'  undefined_lit M K  ?B'
      if mset (N  D) = {#K#} + C' undefined_lit M K
    using assms cdcl_twl_unitres_l.intros(2)[of D N M K C' NE NEk NS N0 UE UEk US U0 Q]
      cdcl_twl_unitres_l.intros(4)[of D N M K C' NE NEk NS N0 UE UEk US U0 Q] that
    by (auto simp: Un_assoc)

  show ?F if ?E mset (N  D) = C'
  proof -
    have mset `# init_clss_lf N + NE + NEk + NS + N0 ⊨psm mset_set (CNot (mset (N  D)))
      using ent2 that assms by (auto simp: Un_assoc)
    then show ?thesis
      using cdcl_twl_unitres_l.intros(5)[of D N NE NEk NS N0 M UE UEk US U0 Q] assms that
      by (auto simp: Un_assoc)
  qed

  show ?F' if ?E' mset (N  D) = C'
  proof -
    have mset `# init_clss_lf N + NE + NEk + NS + N0 ⊨psm mset_set (CNot (mset (N  D)))
      using ent2 that assms by (auto simp: Un_assoc)
    then show ?thesis
      using cdcl_twl_unitres_l.intros(6)[of D N NE NEk NS N0 M UE UEk US U0 Q] assms that
      by auto
  qed
  show ?H if ?G mset (N  D) = mset C +C' length C  2 Lset C. undefined_lit M L
    ¬tautology (mset (N  D)) N' = fmupd D (C, irred N D) N
  proof -
    have dist_C: distinct_mset (mset (N  D))
      using that dist ST assms(2)
      by (auto simp: twl_st_l_def cdclW_restart_mset.distinct_cdclW_state_def ran_m_def
          S_def mset_take_mset_drop_mset'
        dest!: multi_member_split)
    moreover have struct_wf_twl_cls (twl_clause_of C) distinct C
      using that distinct_mset_mono[of mset C mset (N  D)] dist_C
      by (auto simp: twl_st_l_def cdclW_restart_mset.distinct_cdclW_state_def
        mset_take_mset_drop_mset')
    moreover have ¬tautology (mset C)
      using not_tautology_mono[of mset C mset C + C'] that assms by auto
    ultimately show ?thesis
      using cdcl_twl_unitres_l.intros(1)[of D N M mset C C' NE NEk NS N0 C UE UEk US U0] assms that ent2
      by (auto simp: Un_assoc)
  qed
  show ?H' if ?G' mset (N  D) = mset C +C' length C  2 Lset C. undefined_lit M L
    ¬tautology (mset (N  D)) N' = fmupd D (C, irred N D) N
  proof -
    have dist_C: distinct_mset (mset (N  D))
      using that dist ST assms(2)
      by (auto simp: twl_st_l_def cdclW_restart_mset.distinct_cdclW_state_def ran_m_def
          S_def mset_take_mset_drop_mset'
        dest!: multi_member_split)
    moreover have struct_wf_twl_cls (twl_clause_of C) distinct C
      using that distinct_mset_mono[of mset C mset (N  D)] dist_C
      by (auto simp: twl_st_l_def cdclW_restart_mset.distinct_cdclW_state_def
        mset_take_mset_drop_mset')
    moreover have ¬tautology (mset C)
      using not_tautology_mono[of mset C mset C + C'] that assms by auto
    ultimately show ?thesis
      using cdcl_twl_unitres_l.intros(3)[of D N M mset C C' NE NEk NS N0 C UE UEk US U0] assms that ent2 H
      by (auto simp: Un_assoc)
  qed
qed


lemma fmdrop_eq_update_eq: fmdrop C aa = fmdrop C bh  C ∈# dom_m bh 
  bh = fmupd C (bh  C, irred bh C) aa
  apply (subst (asm) fmlookup_inject[symmetric])
  apply (subst fmlookup_inject[symmetric])
  apply (intro ext)
  apply auto[]
  by (metis fmlookup_drop)

lemma fmdrop_eq_update_eq2: fmdrop C aa = fmdrop C bh  C ∈# dom_m bh  b = irred bh C 
  bh = fmupd C (bh  C, b) aa
  using fmdrop_eq_update_eq by fast

lemma twl_st_l_struct_invs_distinct:
  assumes 
    ST: (S, T)  twl_st_l b and
    C: C ∈# dom_m (get_clauses_l S) and
    invs: twl_struct_invs T
  shows distinct (get_clauses_l S  C)
proof -
  have (C ∈# get_clauses T. struct_wf_twl_cls C)
    using invs unfolding twl_struct_invs_def by (cases T) (auto simp: twl_st_inv.simps)
  moreover have twl_clause_of (get_clauses_l S  C) ∈# (get_clauses T)
    using ST C by (cases S; cases T) (auto simp: twl_st_l_def)
  ultimately show ?thesis
    by (auto dest!: multi_member_split simp: mset_take_mset_drop_mset')
qed

lemma list_length_2_isabelle_come_on:
  length C  Suc 0  C  []  length C  2
  by (cases C; cases tl C) auto

lemma in_all_lits_of_mm_init_clss_l_single_out:
  xa ∈# all_lits_of_m (mset (aa  C)) 
  C ∈# dom_m aa  irred aa C  xa ∈# all_lits_of_mm {#mset (fst x). x ∈# init_clss_l aa#} and
  in_all_lits_of_mm_learned_clss_l_single_out:
  xa ∈# all_lits_of_m (mset (aa  C)) 
  C ∈# dom_m aa  ¬irred aa C  xa ∈# all_lits_of_mm {#mset (fst x). x ∈# learned_clss_l aa#}
  by (auto simp: ran_m_def all_lits_of_mm_add_mset dest!: multi_member_split)

(*TODO simp: twl_st_l*)
lemma pget_all_learned_clss_get_all_learned_clss_l:
 (S, x)  twl_st_l b 
  pget_all_learned_clss (pstateW_of x) = get_all_learned_clss_l S
  by (cases x; cases S)
   (auto simp: get_learned_clss_l_def twl_st_l_def mset_take_mset_drop_mset')
lemma pget_all_init_clss_get_all_init_clss_l:
 (S, x)  twl_st_l b 
  pget_all_init_clss (pstateW_of x) = get_all_init_clss_l S
  by (cases x; cases S)
   (auto simp: get_init_clss_l_def twl_st_l_def mset_take_mset_drop_mset')

lemma simplify_clause_with_unit_st_spec:
  assumes simplify_clause_with_unit_st_pre C S
  shows simplify_clause_with_unit_st C S  Id (SPEC(λT.
      (S = T  cdcl_twl_unitres_l S T  cdcl_twl_unitres_true_l S T) 
    (set (get_all_mark_of_propagated (get_trail_l T)) 
       set (get_all_mark_of_propagated (get_trail_l S))  {0}) 
      (dom_m (get_clauses_l T) = dom_m (get_clauses_l S) 
           dom_m (get_clauses_l T) = remove1_mset C (dom_m (get_clauses_l S))) 
      (C' ∈# dom_m (get_clauses_l T). C  C'  fmlookup (get_clauses_l S) C' = fmlookup (get_clauses_l T) C') 
      (C ∈# dom_m (get_clauses_l T)  (Lset (get_clauses_l T  C). undefined_lit (get_trail_l T) L))))
proof -
  obtain T where
    C: C ∈# dom_m (get_clauses_l S) count_decided (get_trail_l S) = 0 and
    confl: get_conflict_l S = None and
    clss: clauses_to_update_l S = {#} and
    ST: (S, T)  twl_st_l None and
    st_invs: twl_struct_invs T and
    list_invs: twl_list_invs S and
    ent: cdclW_restart_mset.cdclW_learned_clauses_entailed_by_init ((stateW_of T)) and
    annot: set (get_all_mark_of_propagated (get_trail_l S))  {0}
    using assms unfolding simplify_clause_with_unit_st_pre_def
    by auto
  have C0: C  0 C  set (get_all_mark_of_propagated (get_trail_l S))
    using list_invs C annot
    by (auto simp: twl_list_invs_def)
  have add_new: L ∈# all_init_lits_of_l S  L ∈# all_init_lits_of_l S + all_learned_lits_of_l S
    if simplify_clause_with_unit_st_pre C S for L S
    using that unfolding simplify_clause_with_unit_st_pre_def twl_struct_invs_def
      pcdcl_all_struct_invs_def cdclW_restart_mset.cdclW_all_struct_inv_def
      cdclW_restart_mset.no_strange_atm_def stateW_of_def[symmetric] apply -
    by normalize_goal+
      (auto simp add: all_init_lits_of_l_def all_learned_lits_of_l_def
      in_all_lits_of_mm_ain_atms_of_iff pget_all_learned_clss_get_all_learned_clss_l
      pget_all_init_clss_get_all_init_clss_l image_Un get_learned_clss_l_def)

  have [dest]: fmdrop C aa = fmdrop C bj 
    remove1_mset C (dom_m bj) = remove1_mset C (dom_m aa) for C ab bj aa
    by (metis dom_m_fmdrop) 
  have n_d:no_dup (get_trail_l S)
    using ST st_invs unfolding twl_struct_invs_def pcdcl_all_struct_invs_def
      cdclW_restart_mset.cdclW_all_struct_inv_def cdclW_restart_mset.cdclW_M_level_inv_def
    by simp
  have in_lits:
    C ∈# dom_m aa  count_decided a = 0  ab = None  ci = {#}  no_dup a  C  0
  case x of
    (unc, b, N') 
    fmdrop C aa = fmdrop C N' 
    mset (N'  C) ⊆# mset (aa  C) 
    C ∈# dom_m N' 
    (¬ b  (L∈#mset (N'  C). undefined_lit a L)) 
    Multiset.Ball (mset (aa  C) - mset (N'  C)) (defined_lit a) 
    irred aa C = irred N' C  b = (L. L ∈# mset (aa  C)  L  lits_of_l a) 
    (unc  (aa = N'  ¬b)) 
    fmdrop C bj = fmdrop C aa  irred bj C = irred aa C  mset (bj  C) ⊆# mset (aa  C)  C ∈# dom_m bj 
    x = (unc, bj') 
    bj' = (aj, bj) 
    ¬unc  aj 
    set_mset
    (all_init_lits_of_l
    (a, fmdrop C bj, ab, (if irred aa C then add_mset (mset (aa  C)) else id) ac,
    (if ¬ irred aa C then add_mset (mset (aa  C)) else id) ad, ae, af, ag, ah, ai, bi, ci, di)) =
    set_mset (all_init_lits_of_l (a, aa, ab, ac, ad, ae, af, ag, ah, ai, bi, ci, di))

    C ∈# dom_m aa  count_decided a = 0  ab = None  ci = {#}  no_dup a  C  0 
  case x of
    (unc, b, N') 
    fmdrop C aa = fmdrop C N' 
    mset (N'  C) ⊆# mset (aa  C) 
    C ∈# dom_m N' 
    (¬ b  (L∈#mset (N'  C). undefined_lit a L)) 
    Multiset.Ball (mset (aa  C) - mset (N'  C)) (defined_lit a) 
    irred aa C = irred N' C  b = (L. L ∈# mset (aa  C)  L  lits_of_l a) 
    (unc  (aa = N'  ¬b))  
    fmdrop C bj = fmdrop C aa  irred bj C = irred aa C  mset (bj  C) ⊆# mset (aa  C)  C ∈# dom_m bj 
    x = (unc, bj') 
    bj' = (aj, bj) 
    ¬unc  aj 
    set_mset
    (all_learned_lits_of_l
      (a, fmdrop C bj, ab, (if irred aa C then add_mset (mset (aa  C)) else id) ac,
      (if ¬ irred aa C then add_mset (mset (aa  C)) else id) ad, ae, af, ag, ah, ai, bi, ci, di)) =
    set_mset (all_learned_lits_of_l (a, aa, ab, ac, ad, ae, af, ag, ah, ai, bi, ci, di)) 
    for a b aa ba ab bb ac bc ad bd ae be af bf ag bg ah bh ai bi x aj bj unc bj' ci di
    using assms distinct_mset_dom[of bj]  distinct_mset_dom[of aa]
    apply (auto simp: all_init_lits_of_l_def get_init_clss_l_def ran_m_def
      all_lits_of_mm_add_mset all_lits_of_mm_union
      all_learned_lits_of_l_def get_learned_clss_l_def
      dest!: multi_member_split[of _ dom_m _]
      cong: )
    apply (smt (verit, best) image_mset_cong2)+
    done
   have in_lits_prop:
     C ∈# dom_m aa  count_decided a = 0  ab = None  di = {#}  no_dup a  C  0 
    case x of
    (unc, b, N') 
   fmdrop C aa = fmdrop C N' 
   mset (N'  C) ⊆# mset (aa  C) 
   C ∈# dom_m N' 
   (¬ b  (L∈#mset (N'  C). undefined_lit a L)) 
   Multiset.Ball (mset (aa  C) - mset (N'  C)) (defined_lit a) 
   irred aa C = irred N' C  b = (L. L ∈# mset (aa  C)  L  lits_of_l a) 
      (unc  (aa = N' ¬b)) 
      x = (unc, bj') 
      bj' = (aj, bj) 
      ¬unc  ¬aj 
    fmdrop C bj = fmdrop C aa  irred bj C = irred aa C  mset (bj  C) ⊆# mset (aa  C)  C ∈# dom_m bj 
    length (bj  C) = 1 
    set_mset
  (all_init_lits_of_l
    (Propagated (bj  C ! 0) 0 # a, fmdrop C bj, ab, ah, ai, (if irred aa C then add_mset {#bj  C ! 0#} else id) ac,
     (if ¬ irred aa C then add_mset {#bj  C ! 0#} else id) ad, (if irred aa C then add_mset (mset (aa  C)) else id) ae,
     (if ¬ irred aa C then add_mset (mset (aa  C)) else id) af, ag, ci, di, add_mset (- bj  C ! 0) bi)) =
    set_mset (all_init_lits_of_l (a, aa, ab, ah, ai, ac, ad, ae, af, ag, ci, di, bi))
      
      C ∈# dom_m aa  count_decided a = 0  ab = None  di = {#}  no_dup a  C  0 
    case x of
    (unc, b, N') 
   fmdrop C aa = fmdrop C N' 
   mset (N'  C) ⊆# mset (aa  C) 
   C ∈# dom_m N' 
   (¬ b  (L∈#mset (N'  C). undefined_lit a L)) 
   Multiset.Ball (mset (aa  C) - mset (N'  C)) (defined_lit a) 
   irred aa C = irred N' C  b = (L. L ∈# mset (aa  C)  L  lits_of_l a) 
    (unc  (aa = N' ¬b)) 
      x = (unc, bj') 
      bj' = (aj, bj) 
      ¬unc  ¬aj 
    fmdrop C bj = fmdrop C aa  irred bj C = irred aa C  mset (bj  C) ⊆# mset (aa  C)  C ∈# dom_m bj 
    length (bj  C) = 1 
    set_mset
  (all_learned_lits_of_l
    (Propagated (bj  C ! 0) 0 # a, fmdrop C bj, ab, ah, ai, (if irred aa C then add_mset {#bj  C ! 0#} else id) ac,
     (if ¬ irred aa C then add_mset {#bj  C ! 0#} else id) ad, (if irred aa C then add_mset (mset (aa  C)) else id) ae,
     (if ¬ irred aa C then add_mset (mset (aa  C)) else id) af, ag, ci, di, add_mset (- bj  C ! 0) bi)) =
    set_mset (all_learned_lits_of_l (a, aa, ab, ah, ai, ac, ad, ae, af, ag, ci, di, bi))
      
      for a b aa ba ab bb ac bc ad bd ae be af bf ag bg ah bh ai bi x aj bj bj' unc ci di
      subgoal
        using assms distinct_mset_dom[of bj]  distinct_mset_dom[of aa]
          image_mset_cong2[of dom_m (fmdrop C aa)
          λx. the (fmlookup (fmdrop C aa) x)  λx. the (fmlookup aa x)  dom_m (fmdrop C aa),
          simplified]
        apply (cases bj  C)
        apply (auto simp: all_init_lits_of_l_def get_init_clss_l_def ran_m_def
          all_lits_of_mm_add_mset all_lits_of_mm_union all_lits_of_m_add_mset
          all_learned_lits_of_l_def get_learned_clss_l_def
          dest!: multi_member_split[of _ dom_m _]
          cong: )
        apply (metis in_clause_in_all_lits_of_m set_mset_mset)
        apply (metis all_lits_of_m_add_mset member_add_mset multi_member_split set_mset_mset)
        apply (smt (verit, best) image_mset_cong2)+
        done
      subgoal
        using assms distinct_mset_dom[of bj]  distinct_mset_dom[of aa]
          image_mset_cong2[of dom_m (fmdrop C aa)
          λx. the (fmlookup (fmdrop C aa) x)  λx. the (fmlookup aa x)  dom_m (fmdrop C aa),
          simplified]
        apply (cases bj  C)
        apply (auto simp: all_init_lits_of_l_def get_init_clss_l_def ran_m_def
          all_lits_of_mm_add_mset all_lits_of_mm_union all_lits_of_m_add_mset
          all_learned_lits_of_l_def get_learned_clss_l_def
          dest!: multi_member_split[of _ dom_m _]
          cong: )
        apply (smt (verit, best) image_mset_cong2)+
        apply (metis in_clause_in_all_lits_of_m set_mset_mset)
        apply (metis all_lits_of_m_add_mset member_add_mset multi_member_split set_mset_mset)
        apply (smt (verit, best) image_mset_cong2)+
        done
      done

  have in_lits2: mset (bj  C) ⊆# mset (aa  C) 
    C ∈# dom_m bj  C ∈# dom_m aa  length (bj  C) > 0 
    irred bj C  irred aa C  bj  C ! 0 ∈# all_lits_of_mm {#mset (fst x). x ∈# init_clss_l aa#} mset (bj  C) ⊆# mset (aa  C) 
    C ∈# dom_m bj  C ∈# dom_m aa  length (bj  C) > 0 
    irred bj C  irred aa C  -bj  C ! 0 ∈# all_lits_of_mm {#mset (fst x). x ∈# init_clss_l aa#}
    mset (bj  C) ⊆# mset (aa  C) 
    C ∈# dom_m bj  C ∈# dom_m aa  length (bj  C) > 0 
    ¬irred bj C  ¬irred aa C  bj  C ! 0 ∈# all_lits_of_mm {#mset (fst x). x ∈# learned_clss_l aa#} mset (bj  C) ⊆# mset (aa  C) 
    C ∈# dom_m bj  C ∈# dom_m aa  length (bj  C) > 0 
    ¬irred bj C  ¬irred aa C  -bj  C ! 0 ∈# all_lits_of_mm {#mset (fst x). x ∈# learned_clss_l aa#}
    mset (bj  C) ⊆# mset (aa  C) 
    fmdrop C bj = fmdrop C aa 
    C ∈# dom_m bj  irred bj C  irred aa C  
    C ∈# dom_m aa 
    xa ∈# all_lits_of_mm {#mset (fst x). x ∈# init_clss_l bj#}  xa ∈# all_lits_of_mm {#mset (fst x). x ∈# init_clss_l aa#}
    
    mset (bj  C) ⊆# mset (aa  C) 
    fmdrop C bj = fmdrop C aa 
    C ∈# dom_m bj  ¬irred bj C  ¬irred aa C  
    C ∈# dom_m aa 
    xa ∈# all_lits_of_mm {#mset (fst x). x ∈#  learned_clss_l bj#}  xa ∈# all_lits_of_mm {#mset (fst x). x ∈# learned_clss_l aa#}
    
    for bj C aa xa
    apply (auto 5 3 dest!: multi_member_split[of _ dom_m _] simp: ran_m_def all_lits_of_mm_add_mset
      in_all_lits_of_mm_uminus_iff dest: all_lits_of_m_mono)
    apply ((metis (no_types, lifting) in_clause_in_all_lits_of_m length_greater_0_conv mset_subset_eq_exists_conv nth_mem_mset union_iff)+)[4]
    apply (blast dest: all_lits_of_m_mono)
    apply (metis (no_types, lifting) add_mset_remove_trivial distinct_mset_add_mset distinct_mset_dom dom_m_fmdrop fmlookup_drop image_mset_cong2)
    apply (blast dest: all_lits_of_m_mono)
    by (metis (no_types, lifting) add_mset_remove_trivial distinct_mset_add_mset distinct_mset_dom dom_m_fmdrop fmlookup_drop image_mset_cong2)
  have tauto: ¬tautology (mset ((get_clauses_l S)  C))
    using list_invs C
    by (auto simp: twl_list_invs_def)
  then show ?thesis
    supply [[goals_limit=1]]
    using assms
    unfolding simplify_clause_with_unit_st_def simplify_clause_with_unit_def Let_def
    apply refine_vcg
    subgoal using assms by blast
    subgoal using C by auto
    subgoal using C by auto
    subgoal using confl by auto
    subgoal using clss by auto
    subgoal using n_d by simp
    subgoal using C list_invs by (auto simp: twl_list_invs_def)
    subgoal using assms by auto
    subgoal using assms by auto
    subgoal using assms by auto
    subgoal using assms by auto
    subgoal using assms by auto
    subgoal using assms by auto
    subgoal using assms by auto
    subgoal using assms by auto
    subgoal using assms by auto
    subgoal using assms by auto
    subgoal using assms by auto
    subgoal using assms by auto
    subgoal using assms by auto
    subgoal using assms by auto
    subgoal 
      by (rule in_lits)
    subgoal 
      by (rule in_lits)
    subgoal for a b aa ba ab bb ac bc ad bd ae be af bf ag bg x ah bh
      supply[[goals_limit=1]]
      using count_decided_ge_get_level[of get_trail_l S] C0
      by (auto simp: cdcl_twl_unitres_true_l.simps
        intro!: exI[of _ C])
    subgoal using assms by auto
    subgoal using assms by auto
    subgoal using assms by auto
    subgoal using assms by auto (metis dom_m_fmdrop fmlookup_drop in_dom_m_lookup_iff)+
    subgoal
      by (rule in_lits_prop)
    subgoal
      by (rule in_lits_prop)
    subgoal by (auto simp: all_init_lits_of_l_def)
    subgoal
      by (subst add_new, subst (asm) eq_commute[of set_mset (all_init_lits_of_l _)],
        subst (asm) eq_commute[of set_mset (all_learned_lits_of_l _)])
       (auto simp: all_init_lits_of_l_def get_init_clss_l_def
        all_learned_lits_of_l_def
        all_lits_of_mm_add_mset all_lits_of_m_add_mset)
    subgoal for a b aa ba ab bb ac bc ad bd ae be af bf ag bg ah bh ai bi x _ _ _ _ _ _ aj bj
      using ST st_invs C0 ent apply -
      apply (rule disjI2, rule disjI1)
      apply (auto simp: length_list_Suc_0
        dest: in_diffD
        intro!: cdcl_twl_unitres_l_intros2' cdcl_twl_unitres_l_intros4'
        intro:  cdcl_twl_unitres_l_intros2'[where C' = mset (get_clauses_l S  C) - mset (bj  C)
        and T = T]
        cdcl_twl_unitres_l_intros4'[where C' = mset (get_clauses_l S  C) - mset (bj  C)
        and T = T])
      done
    subgoal using assms by auto
    subgoal using assms by auto
    subgoal using assms by auto
    subgoal using assms by auto (metis dom_m_fmdrop fmlookup_drop in_dom_m_lookup_iff)+
    subgoal using assms apply (auto simp: all_init_lits_of_l_def
      all_lits_of_mm_union init_clss_l_fmdrop_if image_mset_remove1_mset_if
      get_init_clss_l_def all_lits_of_mm_add_mset all_lits_of_m_add_mset
      dest: all_lits_of_mm_diffD in_lits
      intro: in_all_lits_of_mm_init_clss_l_single_out)
      by (metis all_lits_of_mm_add_mset diff_single_trivial insert_DiffM union_iff)
    subgoal using assms apply (auto simp: all_learned_lits_of_l_def
      all_lits_of_mm_union learned_clss_l_fmdrop_if image_mset_remove1_mset_if
      get_learned_clss_l_def all_lits_of_mm_add_mset all_lits_of_m_add_mset in_lits
      dest: all_lits_of_mm_diffD
      intro: in_all_lits_of_mm_learned_clss_l_single_out)
      by (metis all_lits_of_mm_add_mset diff_single_trivial insert_DiffM union_iff)
    subgoal for a b aa ba ab bb ac bc ad bd ae be af bf ag bg ah bh ai bi x _ _ _ _ _ _ aj bj
      using count_decided_ge_get_level[of get_trail_l S] ST st_invs C0 ent
        cdcl_twl_unitres_false_entailed[of C get_clauses_l S get_trail_l S
         get_unkept_init_clauses_l S get_unkept_learned_clss_l S
         get_kept_init_clauses_l S get_kept_learned_clss_l S
        get_subsumed_init_clauses_l S get_subsumed_learned_clauses_l S
        get_init_clauses0_l S get_learned_clauses0_l S
        literals_to_update_l S T
        mset (get_clauses_l S  C) - mset (bj  C)]
        twl_st_l_struct_invs_distinct[of S T None C]
      apply (auto simp: cdcl_twl_unitres_true_l.simps cdcl_twl_unitres_l.simps Un_assoc
        dest: distinct_mset_mono[of mset _ mset _, unfolded distinct_mset_mset_distinct]
        intro!: exI[of _ C])
      done
    subgoal using assms by auto
    subgoal using assms by auto
    subgoal using assms by auto
    subgoal using assms by auto (metis dom_m_fmdrop fmlookup_drop in_dom_m_lookup_iff)+
    subgoal using assms apply (auto simp: all_init_lits_of_l_def
      all_lits_of_mm_union init_clss_l_fmdrop_if image_mset_remove1_mset_if
      get_init_clss_l_def all_lits_of_mm_add_mset all_lits_of_m_add_mset
      dest: all_lits_of_mm_diffD in_lits2
      intro: in_all_lits_of_mm_init_clss_l_single_out)
      apply (smt (z3) Watched_Literals_Clauses.ran_m_mapsto_upd all_lits_of_mm_add_mset filter_mset_add_mset fmupd_same image_mset_add_mset prod.collapse ran_m_lf_fmdrop union_iff)+
      done
    subgoal using assms apply (auto simp: all_init_lits_of_l_def
      all_lits_of_mm_union init_clss_l_fmdrop_if image_mset_remove1_mset_if
      get_init_clss_l_def all_lits_of_mm_add_mset all_lits_of_m_add_mset
      get_learned_clss_l_def all_learned_lits_of_l_def
      dest: all_lits_of_mm_diffD in_lits2
      intro: in_all_lits_of_mm_init_clss_l_single_out)
      apply (smt (z3) Watched_Literals_Clauses.ran_m_mapsto_upd all_lits_of_mm_add_mset filter_mset_add_mset fmupd_same image_mset_add_mset prod.collapse ran_m_lf_fmdrop union_iff)+
      done
    subgoal for a b aa ba ab bb ac bc ad bd ae be af bf ag bg ah bh ai bi x _ _ _ _ _ _ aj bj
      using assms
      by (auto  simp: list_length_2_isabelle_come_on twl_list_invs_def simplify_clause_with_unit_st_pre_def
        intro!: cdcl_twl_unitres_l_intros3' cdcl_twl_unitres_l_intros1'
        dest: distinct_mset_mono[of mset _ mset _, unfolded distinct_mset_mset_distinct]
        intro!: exI[of _ C] fmdrop_eq_update_eq2)
    subgoal using assms apply (auto simp: all_init_lits_of_l_def
      all_lits_of_mm_union init_clss_l_fmdrop_if image_mset_remove1_mset_if
      get_init_clss_l_def all_lits_of_mm_add_mset all_lits_of_m_add_mset
      dest: all_lits_of_mm_diffD in_lits2
      intro: in_all_lits_of_mm_init_clss_l_single_out)
    done
    subgoal using assms by auto
       (metis dom_m_fmdrop insert_DiffM)+
    subgoal using assms apply auto
      apply (metis fmlookup_drop)+
      done
    subgoal using assms by auto
    done
qed

text 

  We implement forward subsumption (even if it is not a complete algorithm...). We initially tried
  to implement a very limited version similar to Splatz, i.e., putting all clauses in an array
  sorted by length and checking for sub-res in that array. This is more efficient in the sense that
  we know that all previous clauses are smaller... unless you want to strengthen binary clauses too.
  In this case list have to resorted.

  After some time, we decided to implement forward subsumption directly. The implementation works in
  three steps:

   the one-step› version tries to sub-res the clause with a given set of indices. Typically, one
  entry in the watch list.

   then the try_to_forward_subsume› iterates over multiple of whatever. Typically, the iteration
  goes over all watch lists from the literals in the clause (and their negation) to find strengthening
  clauses.

   finally, the all› version goes over all clauses sorted in a set of indices.

After much thinking, we decided to follow the version from CaDiCaL that ignores all clauses that
contain a fixed literal. At first we tried to simplify the clauses, but this means that down in the
real implementation, sorting clauses does not work (because the units are removed).


definition forward_subsumption_one_pre :: nat  nat multiset  'v twl_st_l  bool where
  forward_subsumption_one_pre = (λC xs S.
  T. C  0 
  (S, T)  twl_st_l None 
  twl_struct_invs T 
  twl_list_invs S 
  clauses_to_update_l S = {#} 
  get_conflict_l S = None 
  cdclW_restart_mset.cdclW_learned_clauses_entailed_by_init (stateW_of T) 
  count_decided (get_trail_l S) = 0 
  set (get_all_mark_of_propagated (get_trail_l S))  {0} 
  C ∈# dom_m (get_clauses_l S) 
  (L ∈# mset (get_clauses_l S  C). undefined_lit (get_trail_l S) L) 
  length (get_clauses_l S  C) > 2)

datatype 'v subsumption =
  is_subsumed: SUBSUMED_BY (subsumed_by: nat) |
  is_strengthened: STRENGTHENED_BY (strengthened_on_lit: 'v literal)
    (strengthened_by: nat) |
  NONE

definition try_to_subsume :: nat  nat  'v clauses_l  'v subsumption  bool where
  try_to_subsume C C' N s = (case s of
    NONE  True
  | SUBSUMED_BY C''  mset (N  C') ⊆# mset (N  C)  C'' = C'
  | STRENGTHENED_BY L C''  L ∈# mset (N  C')  -L ∈# mset (N  C) 
   mset (N  C') - {#L#} ⊆# mset (N  C) - {#-L#}  C'' = C')

definition strengthen_clause_pre where
  strengthen_clause_pre C C' L S  (C  C'  C∈#dom_m (get_clauses_l S)  C'∈#dom_m (get_clauses_l S))

definition strengthen_clause :: nat  nat  'v literal 
   'v twl_st_l    'v twl_st_l nres where
  strengthen_clause = (λC C' L (M, N, D, NE, UE, NEk, UEk, NS, US, N0, U0, WS, Q). do {
   ASSERT (strengthen_clause_pre C C' L (M, N, D, NE, UE, NEk, UEk, NS, US, N0, U0, WS, Q));
  E  SPEC (λE. mset E = mset (remove1 (-L) (N  C)));
  if length (N  C) = 2
  then do {
     ASSERT (length (remove1 (-L) (N  C)) = 1);
     let L = hd E;
     RETURN (Propagated L 0 # M, fmdrop C' (fmdrop C N), D,
       (if irred N C' then add_mset (mset (N  C')) else id) NE,
       (if ¬irred N C' then add_mset (mset (N  C')) else id) UE,
        (if irred N C then add_mset {#L#} else id) NEk, (if ¬irred N C then add_mset {#L#} else id) UEk,
        ((if irred N C then add_mset (mset (N  C)) else id)) NS,
       ((if ¬irred N C then add_mset (mset (N  C)) else id)) US,
       N0, U0, WS, add_mset (-L) Q)
  }
  else if length (N  C) = length (N  C')
  then RETURN (M, fmdrop C' (fmupd C (E, irred N C  irred N C') N), D, NE, UE, NEk, UEk,
     ((if irred N C' then add_mset (mset (N  C')) else id)  o (if irred N C then add_mset (mset (N  C)) else id)) NS,
    ((if ¬irred N C' then add_mset (mset (N  C')) else id) o (if ¬irred N C then add_mset (mset (N  C)) else id)) US,
     N0, U0, WS, Q)
  else RETURN (M, fmupd C (E, irred N C) N, D, NE, UE, NEk, UEk,
    (if irred N C then add_mset (mset (N  C)) else id) NS,
    (if ¬irred N C then add_mset (mset (N  C)) else id) US, N0, U0, WS, Q)
  })

definition subsume_or_strengthen_pre :: nat  'v subsumption  'v twl_st_l  bool where
  subsume_or_strengthen_pre = (λC s S. S'.  length (get_clauses_l S  C)  2  C ∈# dom_m (get_clauses_l S) 
  count_decided (get_trail_l S) = 0  distinct (get_clauses_l S  C)  (Lset (get_clauses_l S  C). undefined_lit (get_trail_l S) L)  get_conflict_l S = None 
  C  set (get_all_mark_of_propagated (get_trail_l S))  clauses_to_update_l S = {#} 
  twl_list_invs S 
  (S,S')twl_st_l None  cdclW_restart_mset.cdclW_all_struct_inv (stateW_of S')  
    twl_struct_invs S' 
    cdclW_restart_mset.cdclW_learned_clauses_entailed_by_init (stateW_of S') 
   (case s of
    NONE  True
  | SUBSUMED_BY C'  mset (get_clauses_l S  C') ⊆# mset (get_clauses_l S  C)  C  set (get_all_mark_of_propagated (get_trail_l S))  distinct ((get_clauses_l S)  C')  C  C'  ¬tautology (mset ((get_clauses_l S)  C'))  C' ∈# dom_m (get_clauses_l S)
  | STRENGTHENED_BY L C'  L ∈# mset ((get_clauses_l S)  C')  -L ∈# mset ((get_clauses_l S)  C)  ¬tautology (mset ((get_clauses_l S)  C'))  C'  0 
  mset ((get_clauses_l S)  C') - {#L#} ⊆# mset ((get_clauses_l S)  C) - {#-L#}  C' ∈# dom_m (get_clauses_l S)  distinct ((get_clauses_l S)  C')  
  C'  set (get_all_mark_of_propagated (get_trail_l S)) 2  length ((get_clauses_l S)  C') 
  ¬ tautology (remove1_mset L (mset ((get_clauses_l S)  C')) + remove1_mset (- L) (mset ((get_clauses_l S)  C)))))

definition subsume_or_strengthen :: nat  'v subsumption  'v twl_st_l  _ nres where
  subsume_or_strengthen = (λC s (M, N, D, NE, UE, NEk, UEk, NS, US, N0, U0, WS, Q). do {
   ASSERT(subsume_or_strengthen_pre C s (M, N, D, NE, UE, NEk, UEk, NS, US, N0, U0, WS, Q));
   (case s of
     NONE  RETURN (M, N, D, NE, UE, NEk, UEk, NS, US, N0, U0, WS, Q)
   | SUBSUMED_BY C'  do { let T= (M, fmdrop C (if ¬irred N C'  irred N C then fmupd C' (N  C', True) N else N), D,
          NE, UE, NEk, UEk, (if irred N C then add_mset (mset (N  C)) else id) NS,
           (if ¬irred N C then add_mset (mset (N  C)) else id) US, N0, U0, WS, Q);
       ASSERT (set_mset (all_init_lits_of_l T) = set_mset (all_init_lits_of_l  (M, N, D, NE, UE, NEk, UEk, NS, US, N0, U0, WS, Q)));
       RETURN T
     }
   | STRENGTHENED_BY L C'  strengthen_clause C C' L (M, N, D, NE, UE, NEk, UEk, NS, US, N0, U0, WS, Q))
  })

inductive cdcl_twl_pure_remove_l :: 'v twl_st_l  'v twl_st_l  bool where
  cdcl_twl_pure_remove_l (M, N, None, NE, UE, NEk, UEk, NS, US, N0, U0, {#}, Q)
    (Propagated L 0 # M, N, None, NE, UE, add_mset {#L#} NEk, UEk, NS, US, N0, U0,
      {#}, add_mset (-L)Q)
  if
    undefined_lit M L
    -L  (set_mset ` set_mset (mset `# init_clss_lf N))
    L ∈# all_lits_of_mm (mset `# init_clss_lf N + NE + NEk + NS + N0)
    count_decided M = 0

lemma cdcl_twl_pure_remove_l_cdcl_twl_pure_remove:
  assumes cdcl_twl_pure_remove_l S T
    (S, S')  twl_st_l None
  shows T'. (T, T')  twl_st_l None  cdcl_twl_pure_remove S' T'
  using assms
  by (auto 5 3 simp: cdcl_twl_pure_remove_l.simps twl_st_l_def
    cdcl_twl_pure_remove.simps image_image in_all_lits_of_mm_ain_atms_of_iff
    intro!: convert_lit.intros(3)
    simp: convert_lits_l_add_mset mset_take_mset_drop_mset'
    dest: in_set_takeD in_set_dropD)

lemma rtranclp_cdcl_twl_pure_remove_l_cdcl_twl_pure_remove:
  assumes cdcl_twl_pure_remove_l** S T
    (S, S')  twl_st_l None
  shows T'. (T, T')  twl_st_l None  cdcl_twl_pure_remove** S' T'
  using assms apply (induction rule: rtranclp_induct)
  subgoal by blast
  subgoal for T U
    using cdcl_twl_pure_remove_l_cdcl_twl_pure_remove[of T U] by fastforce
  done

lemma cdcl_twl_pure_remove_l_twl_list_invs:
  cdcl_twl_pure_remove_l S T  twl_list_invs S  twl_list_invs T
  by (auto simp: cdcl_twl_pure_remove_l.simps
    twl_list_invs_def)

inductive cdcl_twl_inprocessing_l for S T where
  cdcl_twl_unitres_l S T  cdcl_twl_inprocessing_l S T |
  cdcl_twl_unitres_true_l S T  cdcl_twl_inprocessing_l S T |
  cdcl_twl_subsumed_l S T  cdcl_twl_inprocessing_l S T|
  cdcl_twl_subresolution_l S T  cdcl_twl_inprocessing_l S T|
  cdcl_twl_pure_remove_l S T  cdcl_twl_inprocessing_l S T

lemma subseteq_mset_size_eql_iff: size Aa = size A  Aa ⊆# A  Aa = A
  by (auto simp: subseteq_mset_size_eql)

lemma subresolution_strengtheningI:
  C ∈# dom_m N  C' ∈# dom_m N  -L ∈# mset (N  C)  L ∈# mset (N  C')  count_decided M = 0 
  remove1_mset (-L) (mset (N  C)) ⊆# remove1_mset L (mset (N  C')) 
  distinct (N  C')  3  length (N  C') 
  L set (N  C'). undefined_lit M L 
  C'  set (get_all_mark_of_propagated M) 
  irred N C'  mset E = mset (remove1 (L) (N  C')) 
  cdcl_twl_inprocessing_l** (M, N, None, NE, UE, NEk, UEk, NS, US, N0, U0, {#}, Q)
  (M, fmupd C' (E, True) N, None, NE, UE, NEk, UEk, add_mset (mset (N  C')) NS,
  US, N0, U0, {#}, Q)
  C ∈# dom_m N  C' ∈# dom_m N  -L ∈# mset (N  C)  L ∈# mset (N  C')  count_decided M = 0 
  remove1_mset (-L) (mset (N  C)) ⊆# remove1_mset L (mset (N  C')) 
  ¬ tautology (remove1_mset (-L) (mset (N  C)) + remove1_mset L (mset (N  C'))) 
  distinct (N  C')  3  length (N  C') 
  L set (N  C'). undefined_lit M L 
  C'  set (get_all_mark_of_propagated M) 
  ¬irred N C'  mset E = mset (remove1 (L) (N  C')) 
  cdcl_twl_inprocessing_l** (M, N, None, NE, UE, NEk, UEk, NS, US, N0, U0, {#}, Q)
  (M, fmupd C' (E, False) N, None, NE, UE, NEk, UEk, NS, add_mset (mset (N  C')) US, N0, U0, {#}, Q)
  C ∈# dom_m N  C' ∈# dom_m N  -L ∈# mset (N  C)  L ∈# mset (N  C')  count_decided M = 0 
  remove1_mset (-L) (mset (N  C)) ⊆# remove1_mset L (mset (N  C')) 
  distinct (N  C')  3  length (N  C') 
  L set (N  C'). undefined_lit M L 
  C  set (get_all_mark_of_propagated M) 
  C'  set (get_all_mark_of_propagated M)  mset E = mset (remove1 (L) (N  C')) 
  irred N C'  irred N C  length (N  C') = length (N  C) 
  cdcl_twl_inprocessing_l** (M, N, None, NE, UE, NEk, UEk, NS, US, N0, U0, {#}, Q)
  (M, fmdrop C (fmupd C' (E, True) N), None, NE, UE, NEk, UEk,
  add_mset (mset (N  C))  (add_mset (mset (N  C')) NS),
  US, N0, U0, {#}, Q)
  C ∈# dom_m N  C' ∈# dom_m N  -L ∈# mset (N  C)  L ∈# mset (N  C')  count_decided M = 0 
  remove1_mset (-L) (mset (N  C)) ⊆# remove1_mset L (mset (N  C')) 
  distinct (N  C')  3  length (N  C') 
  L set (N  C'). undefined_lit M L 
  C  set (get_all_mark_of_propagated M) 
  C'  set (get_all_mark_of_propagated M)  mset E = mset (remove1 (L) (N  C')) 
  ¬ tautology (remove1_mset (- L) (mset (N  C)) + remove1_mset L (mset (N  C'))) 
  ¬irred N C'  ¬irred N C  length (N  C') = length (N  C) 
  cdcl_twl_inprocessing_l** (M, N, None, NE, UE, NEk, UEk, NS, US, N0, U0, {#}, Q)
  (M, fmdrop C (fmupd C' (E, False) N), None, NE, UE, NEk, UEk, NS,
  add_mset (mset (N  C))  (add_mset (mset (N  C')) US), N0, U0, {#}, Q)
  C ∈# dom_m N  C' ∈# dom_m N  -L ∈# mset (N  C)  L ∈# mset (N  C')  count_decided M = 0 
  remove1_mset (-L) (mset (N  C)) ⊆# remove1_mset L (mset (N  C')) 
  distinct (N  C')  3  length (N  C') 
  L set (N  C'). undefined_lit M L 
  C  set (get_all_mark_of_propagated M) 
  C'  set (get_all_mark_of_propagated M) 
  ¬ tautology (remove1_mset (- L) (mset (N  C)) + remove1_mset L (mset (N  C'))) 
  ¬irred N C'  irred N C  length (N  C') = length (N  C)  mset E = mset (remove1 (L) (N  C')) 
  cdcl_twl_inprocessing_l** (M, N, None, NE, UE, NEk, UEk, NS, US, N0, U0, {#}, Q)
  (M, fmdrop C (fmupd C' (E, True) N), None, NE, UE, NEk, UEk, add_mset (mset (N  C)) NS,
  (add_mset (mset (N  C')) US), N0, U0, {#}, Q)
  C ∈# dom_m N  C' ∈# dom_m N  -L ∈# mset (N  C)  L ∈# mset (N  C')  count_decided M = 0 
  remove1_mset (-L) (mset (N  C)) ⊆# remove1_mset L (mset (N  C')) 
  distinct (N  C')  3  length (N  C') 
  L set (N  C'). undefined_lit M L 
  C  set (get_all_mark_of_propagated M) 
  C'  set (get_all_mark_of_propagated M)  mset E = mset (remove1 (L) (N  C')) 
  ¬ tautology (remove1_mset (- L) (mset (N  C)) + remove1_mset L (mset (N  C'))) 
  irred N C'  ¬irred N C  length (N  C') = length (N  C) 
  cdcl_twl_inprocessing_l** (M, N, None, NE, UE, NEk, UEk, NS, US, N0, U0, {#}, Q)
  (M, fmdrop C (fmupd C' (E, True) N), None, NE, UE, NEk, UEk, add_mset (mset (N  C')) NS,
  (add_mset (mset (N  C)) US), N0, U0, {#}, Q)
  and

  subresolution_strengtheningI_binary:
  C ∈# dom_m N  C' ∈# dom_m N  -L ∈# mset (N  C)  L ∈# mset (N  C')  count_decided M = 0 
  length (N  C) = 2  remove1_mset (L) (mset (N  C')) ⊆# remove1_mset (-L) (mset (N  C)) 
  distinct (N  C')  length (N  C')  2  
  L set (N  C). undefined_lit M L 
  irred N C  C'  0  mset E = mset (remove1 (-L) (N  C)) 
  irred N C'  C  set (get_all_mark_of_propagated M)  C'  set (get_all_mark_of_propagated M) 
  cdcl_twl_inprocessing_l**
  (M, N, None, NE, UE, NEk, UEk, NS, US, N0, U0, {#}, Q)
  (Propagated (hd E) 0 # M, fmdrop C' (fmdrop C N), None, add_mset (mset (N  C')) NE, UE, add_mset {#hd E#} NEk, UEk,
   (add_mset (mset (N  C)) NS), US, N0, U0, {#}, add_mset (-hd E) Q)
  C ∈# dom_m N  C' ∈# dom_m N  -L ∈# mset (N  C)  L ∈# mset (N  C')  count_decided M = 0 
  length (N  C) = 2  remove1_mset (L) (mset (N  C')) ⊆# remove1_mset (-L) (mset (N  C)) 
  distinct (N  C')  length (N  C')  2  
  L set (N  C). undefined_lit M L 
  ¬irred N C  ¬irred N C'  C'  0  mset E = mset (remove1 (-L) (N  C)) 
  C  set (get_all_mark_of_propagated M)  C'  set (get_all_mark_of_propagated M) 
  cdcl_twl_inprocessing_l**
  (M, N, None, NE, UE, NEk, UEk, NS, US, N0, U0, {#}, Q)
  (Propagated (hd E) 0 # M, fmdrop C' (fmdrop C N), None, NE, add_mset (mset (N  C')) UE, NEk,
   add_mset {#hd E#} UEk, NS, add_mset (mset (N  C)) US, N0, U0,
   {#}, add_mset (- hd E) Q)
  C ∈# dom_m N  C' ∈# dom_m N  -L ∈# mset (N  C)  L ∈# mset (N  C')  count_decided M = 0 
  length (N  C) = 2  remove1_mset (L) (mset (N  C')) ⊆# remove1_mset (-L) (mset (N  C)) 
  distinct (N  C')  length (N  C')  2  
  L set (N  C). undefined_lit M L 
  ¬irred N C  irred N C'  C'  0  mset E = mset (remove1 (-L) (N  C)) 
  C  set (get_all_mark_of_propagated M)  C'  set (get_all_mark_of_propagated M) 
  cdcl_twl_inprocessing_l**
  (M, N, None, NE, UE, NEk, UEk, NS, US, N0, U0, {#}, Q)
  (Propagated (hd E) 0 # M, fmdrop C' (fmdrop C N), None, add_mset (mset (N  C'))  NE, UE, NEk,
   add_mset {#hd E#} UEk, NS, add_mset (mset (N  C)) US, N0, U0,
   {#}, add_mset (- hd E) Q)
  C ∈# dom_m N  C' ∈# dom_m N  -L ∈# mset (N  C)  L ∈# mset (N  C')  count_decided M = 0 
  length (N  C) = 2  remove1_mset (L) (mset (N  C')) ⊆# remove1_mset (-L) (mset (N  C)) 
  distinct (N  C')  length (N  C')  2  
  L set (N  C). undefined_lit M L 
  irred N C  ¬irred N C'  C'  0  mset E = mset (remove1 (-L) (N  C)) 
  C  set (get_all_mark_of_propagated M)  C'  set (get_all_mark_of_propagated M) 
  cdcl_twl_inprocessing_l**
  (M, N, None, NE, UE, NEk, UEk, NS, US, N0, U0, {#}, Q)
  (Propagated (hd E) 0 # M, fmdrop C' (fmdrop C N), None, NE,
   add_mset (mset (N  C')) UE, add_mset {#hd E#} NEk, UEk,
   add_mset (mset (N  C)) NS, US, N0, U0, {#}, add_mset (- hd E) Q)
  unfolding distinct_mset_mset_distinct[symmetric] set_mset_mset[symmetric]
  supply [simp del] = distinct_mset_mset_distinct set_mset_mset
  supply [simp] = distinct_mset_mset_distinct[symmetric]
  subgoal
    apply (drule multi_member_split[of L] multi_member_split[of -L])+
    apply (rule converse_rtranclp_into_rtranclp)
    apply (rule cdcl_twl_inprocessing_l.intros(4))
    apply (cases irred N C)
    apply (rule cdcl_twl_subresolution_l.intros(1)[of C N C' -L remove1_mset (-L) (mset (N  C))
      remove1_mset L (mset (N  C')) _ E])
    apply (auto dest!: in_diffD simp: distinct_mset_remdups_mset_id length_remove1 simp flip: size_mset; fail)+
    apply (rule cdcl_twl_subresolution_l.intros(7)[of C N C' -L remove1_mset (-L) (mset (N  C))
      remove1_mset L (mset (N  C')) _ E])
    apply (auto dest!: in_diffD simp: distinct_mset_remdups_mset_id length_remove1 simp flip: size_mset; fail)+
    done
  subgoal
    apply (drule multi_member_split[of L] multi_member_split[of -L])+
    apply (rule r_into_rtranclp)
    apply (rule cdcl_twl_inprocessing_l.intros(4))
    apply (cases ¬irred N C)
    apply (rule cdcl_twl_subresolution_l.intros(3)[of C N C' -L remove1_mset (-L) (mset (N  C))
      remove1_mset (L) (mset (N  C')) _ E])
    apply (auto dest!: in_diffD simp: distinct_mset_remdups_mset_id length_remove1 simp flip: size_mset; fail)+
    apply (rule cdcl_twl_subresolution_l.intros(5)[of C N C' -L remove1_mset (-L) (mset (N  C))
      remove1_mset (L) (mset (N  C')) _ E])
    apply (auto dest!: in_diffD simp: distinct_mset_remdups_mset_id length_remove1 simp flip: size_mset; fail)+
    done
  subgoal
    apply (drule multi_member_split[of L] multi_member_split[of -L])+
    apply (rule converse_rtranclp_into_rtranclp)
    apply (rule cdcl_twl_inprocessing_l.intros(4))
    apply (cases irred N C)
    apply (rule cdcl_twl_subresolution_l.intros(1)[of C N C' -L remove1_mset (-L) (mset (N  C))
      remove1_mset L (mset (N  C')) _ E])
    apply (auto dest!: in_diffD simp: distinct_mset_remdups_mset_id length_remove1 simp flip: size_mset; fail)+
    apply (rule converse_rtranclp_into_rtranclp)
    apply (rule cdcl_twl_inprocessing_l.intros(3))
    apply (rule cdcl_twl_subsumed_l.intros(1)[where C=C' and C'=C])
    apply normalize_goal+
    using subseteq_mset_size_eql[of remove1_mset (-L) (mset (N  C))  remove1_mset L (mset (N  C'))]  apply -
    apply ((subst (asm) size_mset[symmetric])+; hypsubst?)
    apply (simp only: add_mset_remove_trivial size_add_mset nat.inject)
    apply (simp; fail)
    apply (auto; fail)+
    done
  subgoal
    apply (drule multi_member_split[of L] multi_member_split[of -L])+
    apply (rule converse_rtranclp_into_rtranclp)
    apply (rule cdcl_twl_inprocessing_l.intros(4))
    apply (rule cdcl_twl_subresolution_l.intros(3)[of C N C' -L remove1_mset (-L) (mset (N  C))
      remove1_mset (L) (mset (N  C')) _ E])
    apply (auto dest!: in_diffD simp: distinct_mset_remdups_mset_id length_remove1  simp flip: size_mset; fail)+
    apply (rule converse_rtranclp_into_rtranclp)
    apply (rule cdcl_twl_inprocessing_l.intros(3))
    apply (rule cdcl_twl_subsumed_l.intros(2)[where C=C' and C'=C])
    apply normalize_goal+
    using subseteq_mset_size_eql[of remove1_mset (-L) (mset (N  C))  remove1_mset L (mset (N  C'))]  apply -
    apply ((subst (asm) size_mset[symmetric])+; hypsubst?)
    apply (simp only: add_mset_remove_trivial size_add_mset nat.inject)
    apply (simp; fail)
    apply (auto; fail)+
    done
  subgoal
    apply (drule multi_member_split[of L] multi_member_split[of -L])+
    apply (rule converse_rtranclp_into_rtranclp)
    apply (rule cdcl_twl_inprocessing_l.intros(4))
    apply (rule cdcl_twl_subresolution_l.intros(5)[of C N C' -L remove1_mset (-L) (mset (N  C))
      remove1_mset (L) (mset (N  C')) _ E])
    apply (auto dest!: in_diffD simp: distinct_mset_remdups_mset_id length_remove1  simp flip: size_mset; fail)+
    apply (rule converse_rtranclp_into_rtranclp)
    apply (rule cdcl_twl_inprocessing_l.intros(3))
    apply (rule cdcl_twl_subsumed_l.intros(4)[where C=C' and C'=C])
    apply normalize_goal+
    using subseteq_mset_size_eql[of remove1_mset (-L) (mset (N  C))  remove1_mset L (mset (N  C'))]  apply -
    apply ((subst (asm) size_mset[symmetric])+; hypsubst?)
    apply (simp only: add_mset_remove_trivial size_add_mset nat.inject)
    apply (simp; fail)
    apply (auto simp: tautology_union; fail)+
    apply (auto simp: fmdrop_fmupd)
    done
  subgoal
    apply (drule multi_member_split[of L] multi_member_split[of -L])+
    apply (rule converse_rtranclp_into_rtranclp)
    apply (rule cdcl_twl_inprocessing_l.intros(4))
    apply (rule cdcl_twl_subresolution_l.intros(7)[of C N C' -L remove1_mset (-L) (mset (N  C))
      remove1_mset (L) (mset (N  C')) _ E])
    apply (auto dest!: in_diffD simp: distinct_mset_remdups_mset_id length_remove1  simp flip: size_mset; fail)+
    apply (rule converse_rtranclp_into_rtranclp)
    apply (rule cdcl_twl_inprocessing_l.intros(3))
    apply (rule cdcl_twl_subsumed_l.intros(3)[where C=C' and C'=C])
    apply normalize_goal+
    using subseteq_mset_size_eql[of remove1_mset (-L) (mset (N  C))  remove1_mset L (mset (N  C'))]  apply -
    apply ((subst (asm) size_mset[symmetric])+; hypsubst?)
    apply (simp only: add_mset_remove_trivial size_add_mset nat.inject)
    apply (simp; fail)
    apply (auto simp: tautology_union; fail)+
    done
  ― ‹Now binary:
  subgoal
    using distinct_mset_dom[of N] apply -
    apply (drule multi_member_split[of L] multi_member_split[of -L])+
    apply (rule converse_rtranclp_into_rtranclp)
    apply (rule cdcl_twl_inprocessing_l.intros(4))
    apply (rule cdcl_twl_subresolution_l.intros(2)[of C' N C L remove1_mset (L) (mset (N  C'))
      remove1_mset (-L) (mset (N  C)) _  hd E])
    apply (auto dest!: in_diffD simp: distinct_mset_remdups_mset_id length_remove1 length_list_2
      add_mset_eq_add_mset subset_eq_mset_single_iff; fail)+
    apply (rule converse_rtranclp_into_rtranclp)
    apply (rule cdcl_twl_inprocessing_l.intros(2))
    apply (rule cdcl_twl_unitres_true_l.intros(1)[where N=fmdrop C N and C=C' and
      L = hd (remove1 (- L) (N  C))])
    apply (auto simp: add_mset_eq_add_mset length_list_2 subset_eq_mset_single_iff)[]
    apply (metis add_mset_commute set_mset_mset union_single_eq_member)
    apply (metis add_mset_commute set_mset_mset union_single_eq_member)
    apply (auto simp: add_mset_eq_add_mset length_list_2 subset_eq_mset_single_iff
      dest!: multi_member_split[of _ dom_m N]
      ; fail)+
    done
  subgoal
    using distinct_mset_dom[of N] apply -
    apply (drule multi_member_split[of L] multi_member_split[of -L])+
    apply (rule converse_rtranclp_into_rtranclp)
    apply (rule cdcl_twl_inprocessing_l.intros(4))
    apply (rule cdcl_twl_subresolution_l.intros(4)[of C' N C L remove1_mset (L) (mset (N  C'))
      remove1_mset (-L) (mset (N  C)) _  hd (remove1 (-L) (N  C))])
    apply (auto dest!: in_diffD simp: distinct_mset_remdups_mset_id length_remove1 length_list_2
      add_mset_eq_add_mset; fail)+
    apply (rule converse_rtranclp_into_rtranclp)
    apply (rule cdcl_twl_inprocessing_l.intros(2))
    apply (rule cdcl_twl_unitres_true_l.intros(2)[where N=fmdrop C N and C=C' and
      L = hd (remove1 (- L) (N  C))])
    apply (auto simp: add_mset_eq_add_mset length_list_2 subset_eq_mset_single_iff)[]
    apply (metis add_mset_commute set_mset_mset union_single_eq_member)
    apply (metis add_mset_commute set_mset_mset union_single_eq_member)
    apply (auto simp: add_mset_eq_add_mset length_list_2 subset_eq_mset_single_iff
      dest!: multi_member_split[of _ dom_m N]
      ; fail)+
    done
  subgoal
    using distinct_mset_dom[of N] apply -
    apply (drule multi_member_split[of L] multi_member_split[of -L])+
    apply (rule converse_rtranclp_into_rtranclp)
    apply (rule cdcl_twl_inprocessing_l.intros(4))
    apply (rule cdcl_twl_subresolution_l.intros(6)[of C' N C L remove1_mset (L) (mset (N  C'))
      remove1_mset (-L) (mset (N  C)) _  hd E])
    apply (auto dest!: in_diffD simp: distinct_mset_remdups_mset_id length_remove1 length_list_2
      add_mset_eq_add_mset subset_eq_mset_single_iff; fail)+
    apply (rule converse_rtranclp_into_rtranclp)
    apply (rule cdcl_twl_inprocessing_l.intros(2))
    apply (rule cdcl_twl_unitres_true_l.intros(1)[where N=fmdrop C N and C=C' and
      L = hd E])
    apply (auto simp: add_mset_eq_add_mset length_list_2 subset_eq_mset_single_iff)[]
    apply (metis add_mset_commute set_mset_mset union_single_eq_member)
    apply (metis add_mset_commute set_mset_mset union_single_eq_member)
    apply (auto simp: add_mset_eq_add_mset length_list_2 subset_eq_mset_single_iff
      dest!: multi_member_split[of _ dom_m N]
      ; fail)+
    done
  subgoal
    using distinct_mset_dom[of N] apply -
    apply (drule multi_member_split[of L] multi_member_split[of -L])+
    apply (rule converse_rtranclp_into_rtranclp)
    apply (rule cdcl_twl_inprocessing_l.intros(4))
    apply (rule cdcl_twl_subresolution_l.intros(8)[of C' N C L remove1_mset (L) (mset (N  C'))
      remove1_mset (-L) (mset (N  C)) _  hd E])
    apply (auto dest!: in_diffD simp: distinct_mset_remdups_mset_id length_remove1 length_list_2
      add_mset_eq_add_mset subset_eq_mset_single_iff; fail)+
    apply (rule converse_rtranclp_into_rtranclp)
    apply (rule cdcl_twl_inprocessing_l.intros(2))
    apply (rule cdcl_twl_unitres_true_l.intros(2)[where N=fmdrop C N and C=C' and
      L = hd E])
    apply (auto simp: add_mset_eq_add_mset length_list_2 subset_eq_mset_single_iff)[]
    apply (metis add_mset_commute set_mset_mset union_single_eq_member)
    apply (metis add_mset_commute set_mset_mset union_single_eq_member)
    apply (auto simp: add_mset_eq_add_mset length_list_2 subset_eq_mset_single_iff
      dest!: multi_member_split[of _ dom_m N]
      ; fail)+
    done
  done

lemma cdcl_twl_inprocessing_l_all_init_lits_of_l:
  assumes cdcl_twl_inprocessing_l S T
  shows set_mset (all_init_lits_of_l S) = set_mset (all_init_lits_of_l T)
proof -
  have [simp]: D ∉# A  {#the (if D = x then b else fmlookup N x). x ∈# A#} =
    {#the (fmlookup N x). x ∈# A#}
    D ∉# A  {#the (if x = D then b else fmlookup N x). x ∈# A#} =
    {#the (fmlookup N x). x ∈# A#} for D E N A b
    by (auto intro!: image_mset_cong)
  have dups_uniq[dest]: remdups_mset D' = {#K#}  set_mset (all_lits_of_m D') = {-K,K} for D' K
    by (metis all_lits_of_m_add_mset all_lits_of_m_empty all_lits_of_m_remdups_mset
      insert_commute set_mset_add_mset_insert set_mset_empty)
  have [simp]: - L ∈# all_lits_of_m a  L ∈# all_lits_of_m a
     - L ∈# all_lits_of_mm b  L ∈# all_lits_of_mm b
    L ∈# xb  L ∈# all_lits_of_m xb for L a xb b
    by (solves cases L, auto simp: rev_image_eqI all_lits_of_m_def all_lits_of_mm_def)+

  have [simp]: L ∈# all_lits_of_m (mset (N  xa))  L  set (N  xa)  -L  set (N  xa)
    for L N xa xb
    by (simp add: atm_of_in_atm_of_set_iff_in_set_or_uminus_in_set in_all_lits_of_m_ain_atms_of_iff)

  show ?thesis
    using assms
    using distinct_mset_dom[of get_clauses_l S] apply -
    supply [[goals_limit=1]]
    apply (induction rule: cdcl_twl_inprocessing_l.induct)
    by (auto 4 3 simp: cdcl_twl_unitres_l.simps
      cdcl_twl_unitres_true_l.simps
      cdcl_twl_subsumed_l.simps
      cdcl_twl_subresolution_l.simps
      all_init_lits_of_l_def
      add_mset_eq_add_mset removeAll_notin
      get_init_clss_l_def init_clss_l_mapsto_upd_notin
      init_clss_l_mapsto_upd ran_m_def all_lits_of_m_union
      all_lits_of_m_add_mset distinct_mset_remove1_All
      init_clss_l_fmupd_if all_lits_of_mm_add_mset
      all_lits_of_m_remdups_mset
      dest!: multi_member_split[of _ :: nat _]
      dest: all_lits_of_m_mono)[4]
    (auto simp: cdcl_twl_pure_remove_l.simps
      all_init_lits_of_l_def
      add_mset_eq_add_mset removeAll_notin
      get_init_clss_l_def init_clss_l_mapsto_upd_notin
      init_clss_l_mapsto_upd ran_m_def all_lits_of_m_union
      all_lits_of_m_add_mset distinct_mset_remove1_All
      init_clss_l_fmupd_if all_lits_of_mm_add_mset all_lits_of_mm_union
      all_lits_of_m_remdups_mset
    dest!: multi_member_split[of _ _ :: _ clauses]
        multi_member_split[of _ :: nat _]
    dest: all_lits_of_m_mono
    intro: in_clause_in_all_lits_of_m)
qed

lemma rtranclp_cdcl_twl_inprocessing_l_all_init_lits_of_l:
  assumes cdcl_twl_inprocessing_l** S T
  shows set_mset (all_init_lits_of_l S) = set_mset (all_init_lits_of_l T)
  using assms
  by (induction rule: rtranclp_induct) (auto dest: cdcl_twl_inprocessing_l_all_init_lits_of_l)

lemma subsume_or_strengthen:
  assumes subsume_or_strengthen_pre C s S C ∈# dom_m (get_clauses_l S)
  shows
    subsume_or_strengthen C s S Id (SPEC(λT. cdcl_twl_inprocessing_l** S T 
      (s = NONE  T = S) 
    (length (get_clauses_l S  C) > 2  get_trail_l S = get_trail_l T) 
     (D∈#remove1_mset C (dom_m (get_clauses_l T)).
    D ∈# dom_m (get_clauses_l S)  get_clauses_l T  D = get_clauses_l S  D) 
    (D. D  C  is_strengthened s  D  strengthened_by s  (D ∈# dom_m (get_clauses_l T)) = (D ∈# dom_m (get_clauses_l S))) 
    (D. D  C  is_subsumed s  D  subsumed_by s  (D ∈# dom_m (get_clauses_l T)) = (D ∈# dom_m (get_clauses_l S)))))
proof -
  have [iff]: C ∈# remove1_mset C (dom_m N)  False
    C ∈# dom_m N - {#C, C#}  Falsefor N
    using distinct_mset_dom[of N] by (cases C∈#dom_m N; auto dest: multi_member_split)+
  have [iff]: C ∈# dom_m N - {#C, x22, C#}  False  C ∈# dom_m N - {#C, x22#}  False
    x22 ∈# dom_m N - {#C, x22, C#}  False
    x22 ∈# dom_m N - {#C, x22#}  False
    for N x22
    using distinct_mset_dom[of N] by (cases C∈#dom_m N;cases x22∈#dom_m N; cases C=x22; auto dest: multi_member_split; fail)+
  have [iff]: C ∈# add_mset C' (remove1_mset C' (dom_m N)) - {#C, C#}  False for C' N
    using distinct_mset_dom[of N] by (cases C∈#dom_m N;cases C'∈#dom_m N; cases C=C'; auto dest: multi_member_split)
  show ?thesis
  using assms unfolding subsume_or_strengthen_def
  apply refine_vcg+
  subgoal by auto
  subgoal for M b N ba NE bb UE bc NEk bd UEk be NS bf US bg NS0 bh US0 bi WS bj Q occs
    apply (cases s)
    subgoal for C'
      apply (simp only: Let_def subsumption.case(1))
      apply refine_vcg
      subgoal
        apply (subgoal_tac cdcl_twl_inprocessing_l**
          (M, N, NE, UE, NEk, UEk, NS, US, NS0, US0, WS, Q, occs)
          (M, fmdrop C (if ¬ irred N C'  irred N C then fmupd C' (N  C', True) N else N),
             NE, UE, NEk, UEk, NS,
             (if irred N C then add_mset (mset (N  C)) else id) US,
             (if ¬ irred N C then add_mset (mset (N  C)) else id) NS0, US0, WS, Q,  occs))
        subgoal
          by (frule rtranclp_cdcl_twl_inprocessing_l_all_init_lits_of_l)
           presburger
        subgoal by auto (auto 8 8 simp: subsume_or_strengthen_pre_def cdcl_twl_subsumed_l.simps fmdrop_fmupd
           intro!: cdcl_twl_inprocessing_l.intros(3) r_into_rtranclp dest!: in_diffD)
        done
      subgoal by auto (auto 8 8 simp: subsume_or_strengthen_pre_def cdcl_twl_subsumed_l.simps fmdrop_fmupd
           intro!: cdcl_twl_inprocessing_l.intros(3) r_into_rtranclp dest!: in_diffD)
      subgoal by auto
      subgoal by auto
      subgoal by auto  (auto 8 8 simp: subsume_or_strengthen_pre_def cdcl_twl_subsumed_l.simps fmdrop_fmupd
           intro!: cdcl_twl_inprocessing_l.intros(3) r_into_rtranclp dest!: in_diffD)
      subgoal by auto 
      subgoal by auto
      done
    subgoal
      apply (clarsimp simp only: strengthen_clause_def subsumption.case Let_def
        intro!: ASSERT_leI impI)
      apply (intro ASSERT_leI intro_spec_iff[THEN iffD2]; (split if_splits[of _ _ = (2::nat)])?; (intro conjI impI ballI ASSERT_leI)?)
      subgoal by (auto simp: length_remove1 strengthen_clause_pre_def subsume_or_strengthen_pre_def)
      subgoal
        by (auto simp: strengthen_clause_def subsume_or_strengthen_pre_def length_remove1 Let_def
           subresolution_strengtheningI_binary
          intro!: ASSERT_leI
          dest: in_diffD)
      subgoal
        by (auto simp: strengthen_clause_def subsume_or_strengthen_pre_def length_remove1 Let_def
           intro: subresolution_strengtheningI_binary
          intro!: ASSERT_leI
          dest: in_diffD)
      subgoal
        by (auto simp: strengthen_clause_def subsume_or_strengthen_pre_def length_remove1 Let_def
          intro!: subresolution_strengtheningI(1)[of strengthened_by s]
          subresolution_strengtheningI(2)[of strengthened_by s]
          subresolution_strengtheningI(3-)
          intro!: ASSERT_leI
          dest: in_diffD)
      done
    subgoal
      by (auto dest: in_diffD)
    done
  done
qed

lemma
  assumes cdcl_twl_inprocessing_l** S T
  shows
    rtranclp_cdcl_twl_inprocessing_l_count_decided:
    count_decided (get_trail_l T) = count_decided (get_trail_l S) (is ?A) and
    rtranclp_cdcl_twl_inprocessing_l_clauses_to_update_l:
    clauses_to_update_l T = clauses_to_update_l S (is ?B) and
    rtranclp_cdcl_twl_inprocessing_l_get_all_mark_of_propagated:
    set (get_all_mark_of_propagated (get_trail_l T))  set (get_all_mark_of_propagated (get_trail_l S))  {0} (is ?C)
proof -
  have [dest]:
    cdcl_twl_inprocessing_l S T  count_decided (get_trail_l T) = count_decided (get_trail_l S) 
    cdcl_twl_inprocessing_l S T  clauses_to_update_l T = clauses_to_update_l S
    cdcl_twl_inprocessing_l S T  set (get_all_mark_of_propagated (get_trail_l T))  set (get_all_mark_of_propagated (get_trail_l S))  {0} for S T
    by (auto simp: cdcl_twl_inprocessing_l.simps cdcl_twl_subsumed_l.simps
      cdcl_twl_unitres_l.simps cdcl_twl_subresolution_l.simps cdcl_twl_unitres_true_l.simps
      cdcl_twl_pure_remove_l.simps)
      (*set (get_all_mark_of_propagated (get_trail_l T))*)
  show ?A ?B ?C
    using assms
    by (induction rule: rtranclp_induct; auto; fail)+
qed


lemma cdcl_twl_inprocessing_l_twl_st_l0:
  assumes cdcl_twl_inprocessing_l S U and
    (S, T)  twl_st_l None and
    twl_struct_invs T and
    twl_list_invs S
 obtains V where
   (U, V)  twl_st_l None and
   cdcl_twl_unitres T V  cdcl_twl_unitres_true T V  cdcl_twl_subsumed T V 
   cdcl_twl_subresolution T V  cdcl_twl_pure_remove T V
  using assms
  apply (induction rule: cdcl_twl_inprocessing_l.induct)
  using cdcl_twl_unitres_l_cdcl_twl_unitres apply blast
  using cdcl_twl_unitres_true_l_cdcl_twl_unitres_true apply blast
  using cdcl_twl_subsumed_l_cdcl_twl_subsumed apply blast
  using cdcl_twl_subresolution_l_cdcl_twl_subresolution apply blast
  using cdcl_twl_pure_remove_l_cdcl_twl_pure_remove by blast


lemma cdcl_twl_unitres_l_list_invs:
  cdcl_twl_unitres_l S T  twl_list_invs S  twl_list_invs T
  by (induction rule: cdcl_twl_unitres_l.induct)
   (auto simp: twl_list_invs_def cdclW_restart_mset.in_get_all_mark_of_propagated_in_trail
    ran_m_mapsto_upd
    dest: in_diffD)


lemma cdcl_twl_inprocessing_l_twl_list_invs:
  assumes cdcl_twl_inprocessing_l S U and
    twl_list_invs S
  shows
    twl_list_invs U
  using assms by (induction rule: cdcl_twl_inprocessing_l.induct)
   (auto simp: cdcl_twl_unitres_True_l_list_invs
    cdcl_twl_unitres_l_list_invs cdcl_twl_subsumed_l_list_invs
    cdcl_twl_subresolution_l_list_invs cdcl_twl_pure_remove_l_twl_list_invs)

lemma rtranclp_cdcl_twl_inprocessing_l_twl_list_invs:
  assumes cdcl_twl_inprocessing_l** S U and
    twl_list_invs S
  shows
    twl_list_invs U
  using assms by (induction rule: rtranclp_induct)
    (auto simp: cdcl_twl_inprocessing_l_twl_list_invs)

lemma cdcl_twl_inprocessing_l_twl_st_l:
  assumes cdcl_twl_inprocessing_l S U and
    (S, T)  twl_st_l None and
    twl_struct_invs T and
    twl_list_invs S and
    cdclW_restart_mset.cdclW_learned_clauses_entailed_by_init (stateW_of T)
  obtains V where
    (U, V)  twl_st_l None and
    cdcl_twl_unitres T V  cdcl_twl_unitres_true T V  cdcl_twl_subsumed T V 
    cdcl_twl_subresolution T V  cdcl_twl_pure_remove T V and
    twl_struct_invs V and
    twl_list_invs Uand
    cdclW_restart_mset.cdclW_learned_clauses_entailed_by_init (stateW_of V)
  apply (rule cdcl_twl_inprocessing_l_twl_st_l0[OF assms(1-4)])
  subgoal premises p for V
    using p(2-) cdcl_twl_inprocessing_l_twl_list_invs[OF assms(1)] apply -
    apply (rule that[of V])
    apply (auto intro: cdcl_twl_unitres_true_twl_struct_invs
      cdcl_twl_unitres_struct_invs assms cdcl_twl_subsumed_struct_invs
      cdcl_twl_subresolution_twl_struct_invs cdcl_twl_unitres_twl_stgy_invs
      cdcl_twl_subresolution_twl_stgy_invs cdcl_twl_unitres_true_twl_stgy_invs
      cdcl_twl_subsumed_twl_stgy_invs
      cdcl_twl_pure_remove_twl_struct_invs)
    apply (metis assms(3) assms(5) cdcl_twl_inp.intros cdcl_twl_inp_invs(3) stateW_of_def)+
    done
  done


lemma rtranclp_cdcl_twl_inprocessing_l_twl_st_l:
  assumes cdcl_twl_inprocessing_l** S U and
    (S, T)  twl_st_l None and
    twl_struct_invs T and
    twl_list_invs S and
    cdclW_restart_mset.cdclW_learned_clauses_entailed_by_init (stateW_of T)
  obtains V where
    (U, V)  twl_st_l None and
    twl_struct_invs V and
    twl_list_invs U and
    cdclW_restart_mset.cdclW_learned_clauses_entailed_by_init (stateW_of V)
  using assms
  apply (induction rule: rtranclp_induct)
  subgoal by blast
  subgoal premises p for X Y
    apply (rule p(3)[OF _ p(5-)])
    apply (rule cdcl_twl_inprocessing_l_twl_st_l[of X Y, OF p(2)]; assumption?)
    apply (rule p(4))
    apply assumption+
    done
  done

text Forward subsumption is done in two steps. First we work on the binary clauses
(also deduplicationg them), and only then we work on other clauses.

Short version:
   first, we work only on binary clauses ;
   second, we work on all other clauses.


Longer version: We already implement the functions towards how we will need implement it (although
this just slightly more general that what would be needed to implement the Splatz version).

The termforward_all schedules all clauses. This functions leaves the work to subsume one clause to
the function termtry_to_subsume. This is the function that is slightly more specialized: it allows
to test subsumption on termn different times (potentially only once). Finally it is the funtion
termforward_one that compares two clauses and checks for subsumption. We assume that no new unit
clause is produced (as only binary clauses can produce new clauses).

The names follow the corresponding functions from CaDiCaL. In newer minimal solvers from Armin (like
satch or Gimsatul), only vivification is implemented.


definition clause_remove_duplicate_clause_pre :: _ where
  clause_remove_duplicate_clause_pre C S  (C' S'.
  (S,S')  twl_st_l None 
  mset ((get_clauses_l S)  C) = mset ((get_clauses_l S)  C') 
  (¬irred (get_clauses_l S) C'  ¬irred (get_clauses_l S) C) 
  C'  set (get_all_mark_of_propagated (get_trail_l S)) 
  C ∈# dom_m (get_clauses_l S) 
  clauses_to_update_l S = {#} 
  C' ∈# dom_m (get_clauses_l S) 
  C  set (get_all_mark_of_propagated (get_trail_l S)) 
  C  C' 
  twl_list_invs S  twl_struct_invs S' 
  cdclW_restart_mset.cdclW_learned_clauses_entailed_by_init (stateW_of S'))

definition clause_remove_duplicate_clause :: nat  'v twl_st_l  'v twl_st_l nres where
clause_remove_duplicate_clause C = (λ(M, N, D, NE, UE, NEk, UEk, NS, US, N0, U0, WS, Q). do {
   ASSERT (clause_remove_duplicate_clause_pre C (M, N, D, NE, UE, NEk, UEk, NS, US, N0, U0, WS, Q));
   RETURN (M, fmdrop C N, D, NE, UE, NEk, UEk, (if irred N C then add_mset (mset (N  C)) else id) NS, (if irred N C then id else add_mset (mset (N  C))) US, N0, U0, WS, Q)
 })

definition clause_remove_duplicate_clause_spec where
  clause_remove_duplicate_clause_spec C S T  cdcl_twl_inprocessing_l S T 
    get_clauses_l T = fmdrop C (get_clauses_l S)  get_trail_l T = get_trail_l S

lemma clause_remove_duplicate_clause:
  assumes clause_remove_duplicate_clause_pre C S
  shows clause_remove_duplicate_clause C S  SPEC(clause_remove_duplicate_clause_spec C S)
  using assms
  unfolding clause_remove_duplicate_clause_def clause_remove_duplicate_clause_pre_def
    clause_remove_duplicate_clause_spec_def apply -
  apply normalize_goal+
  apply (cases S; hypsubst)
  unfolding prod.simps
  apply (intro ASSERT_leI, rule_tac x=x in exI, rule_tac x=xa in exI)
  apply auto []
  apply simp
  apply (intro impI conjI cdcl_twl_inprocessing_l.intros(3))
  using cdcl_twl_subsumed_l.intros[of get_clauses_l S _ C get_trail_l S get_conflict_l S
    get_unkept_init_clauses_l S get_unkept_learned_clss_l S get_kept_init_clauses_l S get_kept_learned_clss_l S
      get_subsumed_init_clauses_l S get_subsumed_learned_clauses_l S get_init_clauses0_l S get_learned_clauses0_l S
      literals_to_update_l S]
  apply (auto simp: )
  done


definition binary_clause_subres_lits_pre :: _ where
  binary_clause_subres_lits_pre C L L' S  (C' S'.
  (S,S')  twl_st_l None 
   mset (get_clauses_l S  C) = {#L, -L'#}  mset (get_clauses_l S  C')= {#L, L'#} 
  get_conflict_l S = None 
  clauses_to_update_l S = {#} 
  C ∈# dom_m (get_clauses_l S) 
  C' ∈# dom_m (get_clauses_l S) 
  count_decided (get_trail_l S) = 0 
  undefined_lit (get_trail_l S) L 
  C  set (get_all_mark_of_propagated (get_trail_l S)) 
  twl_list_invs S 
  twl_struct_invs S' 
  cdclW_restart_mset.cdclW_learned_clauses_entailed_by_init (stateW_of S'))

definition binary_clause_subres :: nat  'v literal  'v literal  'v twl_st_l  'v twl_st_l nres where
binary_clause_subres C L L' = (λ(M, N, D, NE, UE, NEk, UEk, NS, US, N0, U0, WS, Q). do {
   ASSERT (binary_clause_subres_lits_pre C L L' (M, N, D, NE, UE, NEk, UEk, NS, US, N0, U0, WS, Q));
   RETURN (Propagated L 0 # M, fmdrop C N, D, NE, UE,
      (if irred N C then add_mset {#L#} else id) NEk, (if irred N C then id else add_mset {#L#}) UEk,
      (if irred N C then add_mset (mset (N  C)) else id) NS, (if irred N C then id else add_mset (mset (N  C))) US,
       N0, U0, WS, add_mset (-L) Q)
 })


lemma binary_clause_subres_binary_clause:
  assumes binary_clause_subres_lits_pre C L L' S
  shows binary_clause_subres C L L' S  SPEC(cdcl_twl_inprocessing_l S)
  using assms unfolding binary_clause_subres_def binary_clause_subres_lits_pre_def binary_clause_subres_lits_pre_def apply -
  apply normalize_goal+
  subgoal for C' xa
    apply (cases S; hypsubst)
    unfolding prod.simps
    apply (intro ASSERT_leI exI[of _ C'] exI[of _ xa])
    apply auto []
    using cdcl_twl_subresolution_l.intros(2,4,6,8)[of C' get_clauses_l S C L' {#L#}  {#L#} get_trail_l S
      L get_unkept_init_clauses_l S get_unkept_learned_clss_l S get_kept_init_clauses_l S get_kept_learned_clss_l S
      get_subsumed_init_clauses_l S get_subsumed_learned_clauses_l S get_init_clauses0_l S get_learned_clauses0_l S
      literals_to_update_l S, unfolded assms, simplified]
    apply (cases irred (get_clauses_l S) C')
    apply (auto simp add: add_mset_commute cdcl_twl_inprocessing_l.intros(4))
    done
   done

definition deduplicate_binary_clauses_pre where
  deduplicate_binary_clauses_pre L S 
    (T. (S,T)  twl_st_l None  set (get_all_mark_of_propagated (get_trail_l S))  {0} 
      cdclW_restart_mset.cdclW_learned_clauses_entailed_by_init (stateW_of T)  count_decided (get_trail_l S) = 0 
      clauses_to_update_l S = {#}  twl_list_invs S  twl_struct_invs T  L ∈# all_init_lits_of_l S)

definition deduplicate_binary_clauses_correctly_marked where
  deduplicate_binary_clauses_correctly_marked L xs0 xs CS T 
     (C L' b. CS L' = Some (C, b)  (C ∈# dom_m (get_clauses_l T)  C∈# xs0 - xs  mset (get_clauses_l T  C) = {#L,L'#}  irred (get_clauses_l T) C = b))

definition deduplicate_binary_clauses_inv
  :: 'v literal  _  'v twl_st_l  bool × nat multiset × _ × 'v twl_st_l  bool
where
deduplicate_binary_clauses_inv L xs0 S = (λ(abort, xs, CS, T). 
  S'. (S,S')  twl_st_l None  twl_struct_invs S'  cdclW_restart_mset.cdclW_learned_clauses_entailed_by_init (stateW_of S') 
    cdcl_twl_inprocessing_l** S T  xs ⊆# xs0 
    (¬abort  deduplicate_binary_clauses_correctly_marked L xs0 xs CS T) 
    twl_list_invs S 
    count_decided (get_trail_l S) = 0 
    clauses_to_update_l S = {#} 
    set (get_all_mark_of_propagated (get_trail_l S))  {0} 
    (¬abort  undefined_lit (get_trail_l T) L))

lemma deduplicate_binary_clauses_inv_alt_def:
deduplicate_binary_clauses_inv L xs0 S = (λ(abort, xs, CS, T).
  S' T'. (S,S')  twl_st_l None  twl_struct_invs S'  cdclW_restart_mset.cdclW_learned_clauses_entailed_by_init (stateW_of S') 
     (T,T')  twl_st_l None  twl_struct_invs T'   cdclW_restart_mset.cdclW_learned_clauses_entailed_by_init (stateW_of T') 
   cdcl_twl_inprocessing_l** S T  xs ⊆# xs0 
   (¬abort  deduplicate_binary_clauses_correctly_marked L xs0 xs CS T) 
  twl_list_invs S 
  count_decided (get_trail_l S) = 0 
  clauses_to_update_l S = {#} 
  set (get_all_mark_of_propagated (get_trail_l S))  {0} 
  twl_list_invs T 
  count_decided (get_trail_l T) = 0 
  clauses_to_update_l T = {#} 
  set (get_all_mark_of_propagated (get_trail_l T))  {0} 
  (¬abort undefined_lit (get_trail_l T) L))
  unfolding deduplicate_binary_clauses_inv_def
  apply (intro ext iffI)
  unfolding case_prod_beta apply normalize_goal+
  apply (rule_tac x=xa in exI)
  apply (frule rtranclp_cdcl_twl_inprocessing_l_twl_st_l; assumption?)
  apply (rule_tac x=V in exI)
  apply (auto 5 3 dest: rtranclp_cdcl_twl_inprocessing_l_count_decided
    rtranclp_cdcl_twl_inprocessing_l_clauses_to_update_l rtranclp_cdcl_twl_inprocessing_l_twl_list_invs
    rtranclp_cdcl_twl_inprocessing_l_get_all_mark_of_propagated
    elim: rtranclp_cdcl_twl_inprocessing_l_twl_st_l)[]
  unfolding case_prod_beta apply normalize_goal+
  apply (rule_tac x=xa in exI)
  apply (auto 5 3 dest: rtranclp_cdcl_twl_inprocessing_l_count_decided
    rtranclp_cdcl_twl_inprocessing_l_clauses_to_update_l rtranclp_cdcl_twl_inprocessing_l_twl_list_invs
    rtranclp_cdcl_twl_inprocessing_l_get_all_mark_of_propagated rtranclp_cdcl_twl_inprocessing_l_twl_st_l)[]
  done

lemma deduplicate_binary_clauses_inv_alt_def2:
   deduplicate_binary_clauses_pre L S 
  deduplicate_binary_clauses_inv L xs0 S = (λ(abort, xs, CS, T). 
    cdcl_twl_inprocessing_l** S T  xs ⊆# xs0 
    (¬abort  deduplicate_binary_clauses_correctly_marked L xs0 xs CS T) 
    (¬abort  undefined_lit (get_trail_l T) L))
  unfolding deduplicate_binary_clauses_pre_def deduplicate_binary_clauses_inv_def case_prod_beta
  apply (intro ext iffI)
  apply normalize_goal+
  apply simp
  apply normalize_goal+
  apply (rule_tac x=x in exI)
  apply simp
  done


definition deduplicate_binary_clauses :: 'v literal  'v twl_st_l  'v twl_st_l nres where
deduplicate_binary_clauses L S = do {
    ASSERT (deduplicate_binary_clauses_pre L S);
    let CS = (λ_. None);
    xs  SPEC (λCS. C. (C ∈# CS  C ∈# dom_m (get_clauses_l S)  L  set (get_clauses_l S  C))  distinct_mset CS);
    (_, _, _, S)  WHILETdeduplicate_binary_clauses_inv L xs S(λ(abort, xs, CS, S). ¬abort  xs  {#}  get_conflict_l S = None)
      (λ(abort, xs, CS, S).
      do {
         C  SPEC (λC. C ∈# xs);
         if C ∉# dom_m (get_clauses_l S)  length (get_clauses_l S  C)  2 then
           RETURN (abort, xs - {#C#}, CS, S)
         else do {
           L'  SPEC (λL'. mset (get_clauses_l S  C) = {#L, L'#});
           if defined_lit (get_trail_l S) L' then do {
             U  simplify_clause_with_unit_st C S;
             ASSERT (cdcl_twl_inprocessing_l** S U);
             RETURN (defined_lit (get_trail_l U) L, xs - {#C#}, CS, U)
           }
           else if CS L'  None then do {
             let C' = (if (¬snd (the (CS L'))  ¬irred (get_clauses_l S) C) then C else fst (the (CS L')));
             let CS = (if (¬snd (the (CS L'))  ¬irred (get_clauses_l S) C) then CS else CS (L' := Some (C, irred (get_clauses_l S) C)));
             S  clause_remove_duplicate_clause C' S;
             RETURN (abort, xs - {#C#}, CS, S)
           } else if CS (-L')  None then do {
             S  binary_clause_subres C L (-L') S;
             RETURN (True, xs - {#C#}, CS, S)
           } else do {
             RETURN (abort, xs - {#C#}, CS (L' := Some (C, irred (get_clauses_l S) C)), S)
           }
        }
      })
      (defined_lit (get_trail_l S) L, xs, CS, S);
   RETURN S
}

lemma deduplicate_binary_clauses_correctly_marked_remove1:
  deduplicate_binary_clauses_correctly_marked L xs0 xs CS T  distinct_mset xs0  xs ⊆# xs0 
  deduplicate_binary_clauses_correctly_marked L xs0 (remove1_mset C xs) CS T
  apply (cases C ∈# xs)
  apply (auto simp: deduplicate_binary_clauses_correctly_marked_def dest!: multi_member_split)
  apply (metis Multiset.diff_add add_mset_add_single in_diffD)+
  done

lemma fmdrop_eq_dom_m_iff: C ∈# dom_m N  fmdrop C N = fmdrop C' N  C = C'
  by (metis distinct_mset_add_mset distinct_mset_dom dom_m_fmdrop in_remove1_mset_neq insert_DiffM)

lemma deduplicate_binary_clauses_inv_cdcl_twl_unitres_l:
  cdcl_twl_inprocessing_l bb xc  deduplicate_binary_clauses_pre L S 
  deduplicate_binary_clauses_inv L x S (False, aa, ab, bb) 
  xa ∈# aa 
      xa ∈# dom_m (get_clauses_l xc) 
      defined_lit (get_trail_l bb) xb 
      C. C ∈# x  C ∈# dom_m (get_clauses_l S)  L  set (get_clauses_l S  C) 
      distinct_mset x 
      ¬ a 
      aa  {#} 
      get_conflict_l bb = None 
      set (get_all_mark_of_propagated (get_trail_l xc))
       insert 0 (set (get_all_mark_of_propagated (get_trail_l bb))) 
      Lset (get_clauses_l xc  xa). undefined_lit (get_trail_l xc) L 
     dom_m (get_clauses_l bb) = dom_m (get_clauses_l xc) 
     C'∈#dom_m (get_clauses_l xc). xa  C'  fmlookup (get_clauses_l bb) C' = fmlookup (get_clauses_l xc) C' 
      deduplicate_binary_clauses_inv L x S
    (defined_lit (get_trail_l xc) L, remove1_mset xa aa, ab, xc)

    apply (subst deduplicate_binary_clauses_inv_alt_def2, assumption)
    apply (subst (asm)deduplicate_binary_clauses_inv_alt_def2, assumption)
    unfolding prod.simps deduplicate_binary_clauses_pre_def
    apply normalize_goal+
    apply (auto dest: )[]
    apply (clarsimp simp: deduplicate_binary_clauses_correctly_marked_def)
    apply (intro conjI impI allI)
    apply (auto dest: in_diffD simp: distinct_mset_in_diff)
    apply (metis)+
    done

lemma deduplicate_binary_clauses_inv_deleted:
  deduplicate_binary_clauses_pre L S 
      deduplicate_binary_clauses_inv L x S (False, aa, ab, bb) 
      s = (False, aa, ab, bb) 
      b = (aa, ab, bb) 
      ba = (ab, bb) 
      xa ∈# aa 
      xa ∈# dom_m (get_clauses_l bb) 
      mset (get_clauses_l bb  xa) = {#L, xb#} 
      defined_lit (get_trail_l bb) xb 
      C. C ∈# x  C ∈# dom_m (get_clauses_l S)  L  set (get_clauses_l S  C) 
      distinct_mset x 
      ¬ a 
      aa  {#} 
      get_conflict_l bb = None 
      set (get_all_mark_of_propagated (get_trail_l xc))  insert 0 (set (get_all_mark_of_propagated (get_trail_l bb))) 
      C'∈#dom_m (get_clauses_l xc). xa  C'  fmlookup (get_clauses_l bb) C' = fmlookup (get_clauses_l xc) C' 
      xa ∈# dom_m (get_clauses_l xc)  (Lset (get_clauses_l xc  xa). undefined_lit (get_trail_l xc) L) 
      remove1_mset xa (dom_m (get_clauses_l bb)) = dom_m (get_clauses_l xc) 
      cdcl_twl_inprocessing_l bb xc 
      deduplicate_binary_clauses_inv L x S (defined_lit (get_trail_l xc) L, remove1_mset xa aa, ab, xc)

    apply (subst deduplicate_binary_clauses_inv_alt_def2, assumption)
    apply (subst (asm)deduplicate_binary_clauses_inv_alt_def2, assumption)
    unfolding prod.simps deduplicate_binary_clauses_pre_def
    apply normalize_goal+
    apply (auto dest: )[]
    apply (clarsimp simp: deduplicate_binary_clauses_correctly_marked_def)
    apply (intro conjI impI allI)
    apply (auto dest: in_diffD simp: distinct_mset_in_diff)
    apply (metis in_dom_m_lookup_iff in_remove1_msetI)
    apply (metis in_dom_m_lookup_iff insert_DiffM insert_iff set_mset_add_mset_insert)
    apply (metis in_dom_m_lookup_iff insert_DiffM insert_iff set_mset_add_mset_insert)
    apply (metis in_dom_m_lookup_iff insert_DiffM insert_iff set_mset_add_mset_insert)
    apply (clarsimp simp: deduplicate_binary_clauses_correctly_marked_def)
    apply (intro conjI impI allI)
    apply (auto dest: in_diffD simp: distinct_mset_in_diff)
    apply (metis in_dom_m_lookup_iff in_remove1_msetI)
    apply (metis in_dom_m_lookup_iff insert_DiffM insert_iff set_mset_add_mset_insert)
    apply (metis in_dom_m_lookup_iff insert_DiffM insert_iff set_mset_add_mset_insert)
    apply (metis in_dom_m_lookup_iff insert_DiffM insert_iff set_mset_add_mset_insert)
    done

lemma deduplicate_binary_clauses:
  assumes deduplicate_binary_clauses_pre L S
  shows deduplicate_binary_clauses L S  SPEC(cdcl_twl_inprocessing_l** S)
proof -
  have clause_remove_duplicate_clause_pre:
    clause_remove_duplicate_clause_pre (if ¬ snd (the (ab xb))  ¬ irred (get_clauses_l bb) xa then xa
      else fst (the (ab xb))) bb
    if
      pre: deduplicate_binary_clauses_pre L S and
      x_spec: C. (C ∈# x  C ∈# dom_m (get_clauses_l S)  L  set (get_clauses_l S  C))  distinct_mset x and
      inv: deduplicate_binary_clauses_inv L x S s and
      abort: case s of (abort, xs, CS, S)  ¬ abort  xs  {#}  get_conflict_l S = None and
      st: s = (a, b)
      b = (aa, ba)
      ba = (ab, bb) and
      aa: xa ∈# aa and
      xa: ¬ (xa ∉# dom_m (get_clauses_l bb)  length (get_clauses_l bb  xa)  2) and
      xa_L: mset (get_clauses_l bb  xa) = {#L, xb#} and
      undefined_lit (get_trail_l bb) xb and
      ab: ab xb  None
    for x s a b aa ba ab bb xa xb
  proof -
    let ?C = (if ¬ snd (the (ab xb))  ¬ irred (get_clauses_l bb) xa then xa
      else fst (the (ab xb)))
    let ?C' = (if ¬(¬ snd (the (ab xb))  ¬ irred (get_clauses_l bb) xa) then xa
      else fst (the (ab xb)))
    obtain T' where
      T': (bb, T')  twl_st_l None  twl_struct_invs T'  cdclW_restart_mset.cdclW_learned_clauses_entailed_by_init (stateW_of T')
      twl_list_invs bb
      using inv pre xa abort apply -
      unfolding clause_remove_duplicate_clause_pre_def st prod.simps deduplicate_binary_clauses_inv_alt_def
      by blast
    have H: ab xb = Some (C, b)  C  0  C  set (get_all_mark_of_propagated (get_trail_l bb)) 
      C ∈# dom_m (get_clauses_l bb) 
      C ∈# x - aa  mset (get_clauses_l bb  C) = {#L, xb#}  irred (get_clauses_l bb) C = b
      clauses_to_update_l bb = {#}
      xa  set (get_all_mark_of_propagated (get_trail_l bb)) for C b
      using inv pre xa abort T' apply -
      unfolding clause_remove_duplicate_clause_pre_def st prod.simps
      apply (clarsimp_all simp: deduplicate_binary_clauses_inv_alt_def deduplicate_binary_clauses_correctly_marked_def
        twl_list_invs_def)
      apply (intro conjI impI)
      apply (metis neq0_conv)
      apply (metis empty_iff in_mono insert_iff)
      by (metis empty_iff singletonD subset_singletonD)

    show ?thesis
      using pre H ab xa xa_L x_spec aa T' apply -
      unfolding clause_remove_duplicate_clause_pre_def st
      apply (rule exI[of _ ?C'])
      apply (rule exI[of _ T'])
      apply (auto simp: deduplicate_binary_clauses_inv_alt_def deduplicate_binary_clauses_correctly_marked_def)
      apply (meson distinct_mset_in_diff)+
      done
  qed
  have clause_remove_duplicate_clause_post:
    deduplicate_binary_clauses_inv L x S (a, remove1_mset xa aa, if ¬ snd (the (ab xb))  ¬ irred (get_clauses_l bb) xa then ab
      else ab(xb  (xa, irred (get_clauses_l bb) xa)), xc)
    if
      deduplicate_binary_clauses_pre L S and
      C. (C ∈# x  C ∈# dom_m (get_clauses_l S)  L  set (get_clauses_l S  C))  distinct_mset x and
      deduplicate_binary_clauses_inv L x S s and
      case s of (abort, xs, CS, S)  ¬ abort  xs  {#}  get_conflict_l S = None and
      st: s = (a, b)
        b = (aa, ba)
        ba = (ab, bb) and
      xa ∈# aa and
      ¬ (xa ∉# dom_m (get_clauses_l bb)  length (get_clauses_l bb  xa)  2) and
      mset (get_clauses_l bb  xa) = {#L, xb#} and
      undefined_lit (get_trail_l bb) xb and
      ab xb  None and
      clause_remove_duplicate_clause_spec (if ¬ snd (the (ab xb))  ¬ irred (get_clauses_l bb) xa then xa
      else fst (the (ab xb))) bb xc
      for x s a b aa ba ab bb xa xb xc
  proof -
    let ?C = (if ¬ snd (the (ab xb))  ¬ irred (get_clauses_l bb) xa then xa
      else fst (the (ab xb)))
    let ?C' = (if ¬(¬ snd (the (ab xb))  ¬ irred (get_clauses_l bb) xa) then xa
      else fst (the (ab xb)))
    show ?thesis
      using that
      apply (subst deduplicate_binary_clauses_inv_alt_def2)
      apply assumption
      apply (subst (asm)deduplicate_binary_clauses_inv_alt_def)
      unfolding st prod.simps clause_remove_duplicate_clause_spec_def
      apply normalize_goal+
      apply (intro conjI)
      apply (auto simp add: deduplicate_binary_clauses_correctly_marked_def
        distinct_mset_remove1_All distinct_mset_dom distinct_mset_in_diff dest: in_diffD)
      apply (metis in_multiset_nempty member_add_mset)
      by (meson Duplicate_Free_Multiset.distinct_mset_mono distinct_mem_diff_mset union_single_eq_member)
  qed
  have binary_clause_subres_lits_pre: binary_clause_subres_lits_pre xa L (-xb) bb
    if
      pre: deduplicate_binary_clauses_pre L S and
      C. (C ∈# x  C ∈# dom_m (get_clauses_l S)  L  set (get_clauses_l S  C))  distinct_mset x and
      inv: deduplicate_binary_clauses_inv L x S s and
      abort: case s of (abort, xs, CS, S)  ¬ abort  xs  {#}  get_conflict_l S = None and
      st: s = (a, b)
        b = (aa, ba)
        ba = (ab, bb) and
      xa ∈# aa and
      ¬ (xa ∉# dom_m (get_clauses_l bb) length (get_clauses_l bb  xa)  2) and
      mset (get_clauses_l bb  xa) = {#L, xb#} and
      undefined_lit (get_trail_l bb) xb and
      ¬ ab xb  None and
      xb: ab (- xb)  None
    for x s a b aa ba ab bb xa xb
  proof -
    obtain T' where
      T': (bb, T')  twl_st_l None twl_struct_invs T'cdclW_restart_mset.cdclW_learned_clauses_entailed_by_init (stateW_of T')
      using inv pre xb abort apply -
      unfolding clause_remove_duplicate_clause_pre_def st prod.simps deduplicate_binary_clauses_inv_alt_def
      apply normalize_goal+
      by blast
    have H: ab (- xb) = Some (C, b)  C  0  C  set (get_all_mark_of_propagated (get_trail_l bb)) 
      C ∈# dom_m (get_clauses_l bb) 
      C ∈# x - aa  mset (get_clauses_l bb  C) = {#L, (- xb)#}  irred (get_clauses_l bb) C = b
      clauses_to_update_l bb = {#}
      xa  set (get_all_mark_of_propagated (get_trail_l bb)) for C b
      using inv pre xb abort apply -
      unfolding clause_remove_duplicate_clause_pre_def st prod.simps deduplicate_binary_clauses_inv_alt_def
      apply normalize_goal+
      apply (clarsimp_all simp: deduplicate_binary_clauses_inv_alt_def deduplicate_binary_clauses_correctly_marked_def
        twl_list_invs_def)
      apply (intro conjI)
      apply (metis neq0_conv)
      apply (metis empty_iff in_mono insert_iff)
      by (metis in_mono singletonD that(9))
    show ?thesis
      using that H T' apply -
      unfolding binary_clause_subres_lits_pre_def
      by (rule exI[of _ fst (the (ab (-xb)))], rule exI[of _ T'])
       (auto simp: deduplicate_binary_clauses_inv_alt_def deduplicate_binary_clauses_correctly_marked_def)
  qed
  have new_lit: deduplicate_binary_clauses_inv L x S
    (a, remove1_mset xa aa, ab(xb  (xa, irred (get_clauses_l bb) xa)), bb)
    if
      deduplicate_binary_clauses_pre L S and
      C. (C ∈# x  C ∈# dom_m (get_clauses_l S)  L  set (get_clauses_l S  C))  distinct_mset x and
      deduplicate_binary_clauses_inv L x S s and
      case s of
      (abort, xs, CS, S)  ¬ abort  xs  {#}  get_conflict_l S = None and
      st: s = (a, b)
        b = (aa, ba)
        ba = (ab, bb) and
      xa ∈# aa and
      ¬ (xa ∉# dom_m (get_clauses_l bb)  length (get_clauses_l bb  xa)  2) and
      mset (get_clauses_l bb  xa) = {#L, xb#} and
      undefined_lit (get_trail_l bb) xb and
      ¬ (ab xb  None) and
      ¬ ab (- xb)  None
      for x s a b aa ba ab bb xa xb
  proof -
    show ?thesis
      using that unfolding prod.simps st apply -
      apply (subst deduplicate_binary_clauses_inv_alt_def2, assumption)
      apply (subst (asm) deduplicate_binary_clauses_inv_alt_def2, assumption)
      apply normalize_goal+
      apply (auto simp: deduplicate_binary_clauses_correctly_marked_def distinct_mset_in_diff
        dest!: multi_member_split dest: distinct_mset_mono)
      apply (meson distinct_mset_add_mset distinct_mset_mono member_add_mset)
      done
  qed
  let ?R = measure (λ(abort, xs, CS, S). size xs)
  show ?thesis
    unfolding deduplicate_binary_clauses_def Let_def
    apply (refine_vcg assms WHILEIT_rule[where R = ?R] simplify_clause_with_unit_st_spec[THEN order_trans]
      clause_remove_duplicate_clause[THEN order_trans] binary_clause_subres_binary_clause[THEN order_trans])
    subgoal
      by auto
    subgoal
      by (fastforce simp: deduplicate_binary_clauses_inv_def deduplicate_binary_clauses_pre_def deduplicate_binary_clauses_correctly_marked_def)
    subgoal
      by (subst deduplicate_binary_clauses_inv_alt_def2, assumption)
       (auto simp: deduplicate_binary_clauses_inv_def deduplicate_binary_clauses_correctly_marked_remove1)
    subgoal
      by (auto dest: multi_member_split)
    subgoal
      unfolding deduplicate_binary_clauses_inv_alt_def simplify_clause_with_unit_st_pre_def
        case_prod_beta ex_simps(2)[symmetric]
      apply normalize_goal+
      by (rule_tac x=xd in exI) simp
    subgoal
      apply simp
      apply standard
      apply simp
      apply normalize_goal+
      apply (intro ASSERT_leI)
      apply (metis cdcl_twl_inprocessing_l.intros(1,2) r_into_rtranclp rtranclp.rtrancl_refl)
      apply simp
      apply (elim disjE; intro conjI; subst (asm) eq_commute[of dom_m _])
      apply (subst deduplicate_binary_clauses_inv_alt_def2, assumption)
      apply (subst (asm)deduplicate_binary_clauses_inv_alt_def2, assumption)
      unfolding prod.simps
      apply (intro conjI)
      apply (simp_all add: deduplicate_binary_clauses_correctly_marked_remove1 size_mset_remove1_mset_le_iff)
      apply (force dest: multi_member_split)
      apply (rule deduplicate_binary_clauses_inv_cdcl_twl_unitres_l)
      apply (rule cdcl_twl_inprocessing_l.intros; assumption)
      apply assumption+
      apply (rule deduplicate_binary_clauses_inv_cdcl_twl_unitres_l)
      apply (rule cdcl_twl_inprocessing_l.intros; assumption)
      apply assumption+
      apply (rule deduplicate_binary_clauses_inv_deleted)
      apply assumption+
      apply (rule cdcl_twl_inprocessing_l.intros; assumption)
      apply (rule deduplicate_binary_clauses_inv_deleted)
      apply assumption+
      apply (rule cdcl_twl_inprocessing_l.intros; assumption)
      done
    subgoal by (rule clause_remove_duplicate_clause_pre)
    subgoal by (rule clause_remove_duplicate_clause_post)
    subgoal
      by (auto dest!: multi_member_split)
    subgoal by (rule binary_clause_subres_lits_pre)
    subgoal
      by (subst deduplicate_binary_clauses_inv_alt_def2, assumption,
          subst (asm)deduplicate_binary_clauses_inv_alt_def2, assumption)
      (auto simp: )
    subgoal by (auto dest!: multi_member_split)
    subgoal by (rule new_lit)
    subgoal by (auto dest!: multi_member_split)
    subgoal unfolding deduplicate_binary_clauses_inv_def by fast
    done
qed

definition mark_duplicated_binary_clauses_as_garbage_pre :: 'v twl_st_l  bool where
  mark_duplicated_binary_clauses_as_garbage_pre = (λS. (T. (S,T)  twl_st_l None  set (get_all_mark_of_propagated (get_trail_l S))  {0}  
      cdclW_restart_mset.cdclW_learned_clauses_entailed_by_init (stateW_of T)  count_decided (get_trail_l S) = 0 
      clauses_to_update_l S = {#}  twl_list_invs S  twl_struct_invs T))

definition mark_duplicated_binary_clauses_as_garbage_inv :: 'v multiset  'v twl_st_l  'v twl_st_l × 'v multiset  bool where
  mark_duplicated_binary_clauses_as_garbage_inv = (λxs S (U, ys). ys ⊆# xs  cdcl_twl_inprocessing_l** S U)

lemma mark_duplicated_binary_clauses_as_garbage_inv_alt_def:
  assumes mark_duplicated_binary_clauses_as_garbage_pre S
  shows mark_duplicated_binary_clauses_as_garbage_inv xs S (U, Ls)  
    (T. (U,T)  twl_st_l None  set (get_all_mark_of_propagated (get_trail_l U))  {0}  
      Ls ⊆# xs cdclW_restart_mset.cdclW_learned_clauses_entailed_by_init (stateW_of T)  count_decided (get_trail_l U) = 0 
      clauses_to_update_l U = {#}  twl_list_invs U  twl_struct_invs T  cdcl_twl_inprocessing_l** S U)
  using assms rtranclp_cdcl_twl_inprocessing_l_get_all_mark_of_propagated[of S U] rtranclp_cdcl_twl_inprocessing_l_clauses_to_update_l[of S U]
    rtranclp_cdcl_twl_inprocessing_l_count_decided[of S U] rtranclp_cdcl_twl_inprocessing_l_get_all_mark_of_propagated[of S U]
  unfolding mark_duplicated_binary_clauses_as_garbage_pre_def
    mark_duplicated_binary_clauses_as_garbage_inv_def prod.simps apply -
  apply normalize_goal+
  apply (rule iffI)
  apply normalize_goal+
   apply (rule rtranclp_cdcl_twl_inprocessing_l_twl_st_l; assumption?)
   apply (rename_tac x V; rule_tac x=V in exI)
   apply (solves auto)
  by meson

definition mark_duplicated_binary_clauses_as_garbage :: _  'v twl_st_l nres where
  mark_duplicated_binary_clauses_as_garbage S = do {
     ASSERT (mark_duplicated_binary_clauses_as_garbage_pre S);
     Ls  SPEC (λLs:: 'v multiset. L∈#Ls. L ∈# atm_of `# all_init_lits_of_l S);
     (S, _)  WHILETmark_duplicated_binary_clauses_as_garbage_inv Ls S(λ(S, Ls). Ls  {#}  get_conflict_l S = None)
      (λ(S, Ls). do {
        L  SPEC (λL. L ∈# Ls);
        ASSERT (L ∈# atm_of `# all_init_lits_of_l S);
        skip  RES (UNIV :: bool set);
        if skip then RETURN (S, remove1_mset L Ls)
        else do {
          S  deduplicate_binary_clauses (Pos L) S;
          S  deduplicate_binary_clauses (Neg L) S;
          RETURN (S, remove1_mset L Ls)
        }
     })
     (S, Ls);
    RETURN S
  }

lemma cdcl_twl_inprocessing_l_all_learned_lits_of_l:
  assumes cdcl_twl_inprocessing_l S T
  shows set_mset (all_learned_lits_of_l T)  set_mset (all_learned_lits_of_l S)
proof -
  have [simp]: D ∉# A  {#the (if D = x then b else fmlookup N x). x ∈# A#} =
    {#the (fmlookup N x). x ∈# A#}
    D ∉# A  {#the (if x = D then b else fmlookup N x). x ∈# A#} =
    {#the (fmlookup N x). x ∈# A#} for D E N A b
    by (auto intro!: image_mset_cong)
  have dups_uniq[dest]: remdups_mset D' = {#K#}  set_mset (all_lits_of_m D') = {-K,K} for D' K
    by (metis all_lits_of_m_add_mset all_lits_of_m_empty all_lits_of_m_remdups_mset
      insert_commute set_mset_add_mset_insert set_mset_empty)
  show ?thesis
    using assms
    using distinct_mset_dom[of get_clauses_l S] apply -
    supply [[goals_limit=1]]
    by (induction rule: cdcl_twl_inprocessing_l.induct)
     (auto 4 3 simp: cdcl_twl_unitres_l.simps 
      cdcl_twl_unitres_true_l.simps
      cdcl_twl_subsumed_l.simps
      cdcl_twl_subresolution_l.simps cdcl_twl_pure_remove_l.simps
      all_learned_lits_of_l_def
      add_mset_eq_add_mset removeAll_notin
      get_init_clss_l_def init_clss_l_mapsto_upd_notin
      init_clss_l_mapsto_upd ran_m_def all_lits_of_m_union
      all_lits_of_m_add_mset distinct_mset_remove1_All
      init_clss_l_fmupd_if all_lits_of_mm_add_mset
      all_lits_of_m_remdups_mset
      dest!: multi_member_split[of _ :: nat _]
      dest: all_lits_of_m_mono)
qed

lemma rtranclp_cdcl_twl_inprocessing_l_all_learned_lits_of_l:
  assumes cdcl_twl_inprocessing_l** S T
  shows set_mset (all_learned_lits_of_l T)  set_mset (all_learned_lits_of_l S)
  using assms
  by (induction rule: rtranclp_induct) (auto dest: cdcl_twl_inprocessing_l_all_learned_lits_of_l)

lemma mark_duplicated_binary_clauses_as_garbage:
  fixes S :: 'v twl_st_l
  assumes pre: mark_duplicated_binary_clauses_as_garbage_pre S
  shows mark_duplicated_binary_clauses_as_garbage S   Id (SPEC(cdcl_twl_inprocessing_l** S))
proof -
  have deduplicate_binary_clauses_pre: deduplicate_binary_clauses_pre (Pos xa) xb (is ?A)
     deduplicate_binary_clauses_pre (Neg xa) xb (is ?B)
    if
      mark_duplicated_binary_clauses_as_garbage_inv xs S s and
      case s of (S, Ls)  Ls  {#}  get_conflict_l S = None and
      st: s = (a, b) and
      cdcl_twl_inprocessing_l** a xb and
      xs: L∈#xs. L ∈# atm_of `# all_init_lits_of_l S
      xa ∈# b
      for s a xa xb and xs b :: 'v multiset
    proof -
      have mark_duplicated_binary_clauses_as_garbage_inv xs S (xb, b) xa ∈# xs
        using that unfolding mark_duplicated_binary_clauses_as_garbage_inv_def
        by auto
      moreover have set_mset (all_init_lits_of_l xb) = set_mset (all_init_lits_of_l S)
        using that by (auto dest!: rtranclp_cdcl_twl_inprocessing_l_all_init_lits_of_l
          simp: mark_duplicated_binary_clauses_as_garbage_inv_def)
      then have Pos xa ∈# all_init_lits_of_l xb Neg xa ∈# all_init_lits_of_l xb
        using xa ∈# xs xs by (auto dest!: multi_member_split[of xa xs]
          simp: all_init_lits_of_l_def)
         (metis in_all_lits_of_mm_ain_atms_of_iff literal.sel)+
      ultimately show ?A ?B
        using pre unfolding deduplicate_binary_clauses_pre_def st
        by (meson mark_duplicated_binary_clauses_as_garbage_inv_alt_def)+
  qed
  let ?R = measure (λ(_, Ls). size Ls)
  have wf: wf ?R
    by auto
  show ?thesis
    unfolding mark_duplicated_binary_clauses_as_garbage_def
    apply (refine_vcg deduplicate_binary_clauses[THEN order_trans] wf assms)
    subgoal by (auto simp: mark_duplicated_binary_clauses_as_garbage_inv_def)
    subgoal by (auto simp: mark_duplicated_binary_clauses_as_garbage_inv_def
      dest!: rtranclp_cdcl_twl_inprocessing_l_all_init_lits_of_l)
    subgoal by (auto simp: mark_duplicated_binary_clauses_as_garbage_inv_def)
    subgoal by (auto dest!: multi_member_split)
    subgoal unfolding deduplicate_binary_clauses_pre_def
      by hypsubst
        (metis deduplicate_binary_clauses_pre deduplicate_binary_clauses_pre_def rtranclp.rtrancl_refl)
    subgoal by (rule deduplicate_binary_clauses_pre)
    subgoal by (auto simp: mark_duplicated_binary_clauses_as_garbage_inv_def)
    subgoal by (auto dest!: multi_member_split)
    subgoal by (auto simp: mark_duplicated_binary_clauses_as_garbage_inv_def)
    done
qed

definition forward_subsumption_one_inv :: nat  'v twl_st_l  nat multiset  _  bool where
  forward_subsumption_one_inv = (λC S zs (xs, s).
  (S' . (S, S')  twl_st_l None 
      twl_struct_invs S' 
      cdclW_restart_mset.cdclW_learned_clauses_entailed_by_init (stateW_of S') 
    C ∉# xs  C ∈# dom_m (get_clauses_l S)  count_decided (get_trail_l S) = 0 
    clauses_to_update_l S = {#}  twl_list_invs S  twl_struct_invs S' 
   set (get_all_mark_of_propagated (get_trail_l S))  {0} 
   (C'∈#xs. C' ∈# dom_m (get_clauses_l S)  length (get_clauses_l S   C')  length (get_clauses_l S  C) )
  cdclW_restart_mset.cdclW_learned_clauses_entailed_by_init (stateW_of S') 
  subsume_or_strengthen_pre C s S  xs ⊆# zs 
    (is_subsumed s  subsumed_by s ∈# zs) 
    (is_strengthened s  strengthened_by s ∈# zs)))

definition forward_subsumption_one_select where
  forward_subsumption_one_select C ys S = (λxs. C ∉# xs  ys ∩# xs = {#} 
  (D∈#xs. D ∈# dom_m (get_clauses_l S) 
    (L set (get_clauses_l S  D). undefined_lit (get_trail_l S) L) 
    (length (get_clauses_l S  D)  length (get_clauses_l S  C))))

definition forward_subsumption_one :: nat  nat multiset  'v twl_st_l  ('v twl_st_l × bool) nres where
  forward_subsumption_one = (λC cands S0. do {
    ASSERT(forward_subsumption_one_pre C cands S0);
    xs0  SPEC (forward_subsumption_one_select C cands S0);
    (xs, s) 
      WHILETforward_subsumption_one_inv C S0 xs0(λ(xs, s). xs  {#}  s = NONE)
      (λ(xs, s). do {
        C'  SPEC(λC'. C' ∈# xs);
        if C' ∉# dom_m (get_clauses_l S0)
        then RETURN (remove1_mset C' xs, s)
        else do {
          s  SPEC(try_to_subsume C C' (get_clauses_l S0));
          RETURN (remove1_mset C' xs, s)
        }
      })
      (xs0, NONE);
    ASSERT (forward_subsumption_one_inv C S0 xs0 (xs, s));
    S  subsume_or_strengthen C s S0;
    ASSERT (set_mset (all_learned_lits_of_l S)  set_mset (all_learned_lits_of_l S0)  set_mset (all_init_lits_of_l S) = set_mset (all_init_lits_of_l S0));
    RETURN (S, s  NONE)
  }
)

lemma forward_subsumption_one:
  assumes forward_subsumption_one_pre C cands S
  shows forward_subsumption_one C cands S  {((st, SR), st'). st = st'  (¬SR  st = S)}
    (SPEC(λT. cdcl_twl_inprocessing_l** S T  get_trail_l T = get_trail_l S 
      (D∈#cands. D  C  (D ∈# dom_m (get_clauses_l T)) = (D ∈# dom_m (get_clauses_l S))) 
      (D∈#remove1_mset C (dom_m (get_clauses_l T)).
       D ∈# dom_m (get_clauses_l S)  get_clauses_l T  D = get_clauses_l S  D)))
proof -
  obtain x where
    Sx: (S, x)  twl_st_l None and
    struct: twl_struct_invs x and
    st_inv: twl_st_inv x and
    list_invs: twl_list_invs S and
    C: C ∈# dom_m (get_clauses_l S) and
    all_struct_x: cdclW_restart_mset.cdclW_all_struct_inv (stateW_of x) and
    clss: clauses_to_update_l S = {#} get_conflict_l S = None and
    no_annot: set (get_all_mark_of_propagated (get_trail_l S))  {0} and
    init: cdclW_restart_mset.cdclW_learned_clauses_entailed_by_init (stateW_of x) and
    dec: count_decided (get_trail_l S) = 0 and
    undef_C: L∈#mset (get_clauses_l S  C). undefined_lit (get_trail_l S) L and
    le_C: 2 < length (get_clauses_l S  C)
    using assms unfolding forward_subsumption_one_pre_def twl_struct_invs_def
      pcdcl_all_struct_invs_def stateW_of_def
    apply -
    by normalize_goal+ blast  have C  0
    using list_invs C by (auto simp: twl_list_invs_def)
  then have C_annot: C  set (get_all_mark_of_propagated (get_trail_l S))
    using no_annot by blast

  have [refine0]: wf (measure (λ(d, _). size d))
    by auto
  have H: length (get_clauses_l S  D) 2 distinct (get_clauses_l S  D)  D  0
    D  set (get_all_mark_of_propagated (get_trail_l S))
    ¬tautology (mset (get_clauses_l S  D))
    if D ∈# dom_m (get_clauses_l S)for D
    using st_inv that Sx list_invs no_annot unfolding twl_struct_invs_def twl_list_invs_def apply -
    by (auto simp: twl_struct_invs_def twl_st_l_def ran_m_def conj_disj_distribR
      twl_st_inv.simps Collect_disj_eq image_image image_Un Collect_conv_if mset_take_mset_drop_mset'
      dest!: multi_member_split split: if_splits
      simp flip: insert_compr)

  have H2: ¬tautology (mset (get_clauses_l S  D)) if D ∈# dom_m (get_clauses_l S) for D
    using list_invs that by (auto simp: twl_list_invs_def ran_m_def)

  have forward_subsumption_one_inv: forward_subsumption_one_inv C S xs0 (remove1_mset xa aa, xb)
    if
      select: forward_subsumption_one_select C cands S xs and
      inv: forward_subsumption_one_inv C S xs0 s and
      case s of (xs, s)  xs  {#}  s = NONE and
      st[simp]: s = (aa, ba) and
      xa_aa: xa ∈# aa and
      xa: ¬ xa ∉# dom_m (get_clauses_l S) and
      subs: try_to_subsume C xa (get_clauses_l S) xb
    for xs s b aa ba xa xb xs0
  proof -
    have [simp]: C  xa
      using xa_aa select inv unfolding forward_subsumption_one_select_def forward_subsumption_one_inv_def
      by auto

    have [intro]: x21  set (get_clauses_l S  xa) 
    - x21  set (get_clauses_l S  C) 
    remove1_mset x21 (mset (get_clauses_l S  xa)) ⊆# remove1_mset (- x21) (mset (get_clauses_l S  C)) 
    tautology (mset (get_clauses_l S  xa) + mset (get_clauses_l S  C) - {#- x21, x21#})  False for a x21
      using multi_member_split[of x21 mset (get_clauses_l S  xa)]
        multi_member_split[of -x21 mset (get_clauses_l S  C)] H[of C] C
      by (auto simp: tautology_add_subset tautology_add_mset)

    have subsume_or_strengthen_pre C xb S
      using undef_C le_C dec clss C_annot subs H xa C Sx struct init all_struct_x st_inv list_invs 
        H[of C] unfolding subsume_or_strengthen_pre_def
      apply -
      apply (rule exI[of _ x])
      by (auto simp: try_to_subsume_def split: subsumption.splits intro!: exI[of _ x])
    then show ?thesis
      using inv
      unfolding forward_subsumption_one_inv_def prod.simps st apply -
      apply normalize_goal+
      apply (rule exI[of _ x])
      apply (use Sx struct st_inv init dec subs xa_aa in auto dest!: in_diffD)
        using subs xa_aa
      apply (case_tac xb; auto simp: try_to_subsume_def; fail)+
      done
  qed
  show ?thesis
    unfolding forward_subsumption_one_def conc_fun_RES
    apply (rewrite at _   RES_SPEC_conv)
    apply (refine_vcg subsume_or_strengthen[unfolded Down_id_eq] if_rule)
    subgoal using assms by auto
    subgoal for ax
      unfolding forward_subsumption_one_inv_def prod.simps forward_subsumption_one_select_def
        subsume_or_strengthen_pre_def ex_simps[symmetric]
      apply (rule_tac x=x in exI)+
      using all_struct_x Sx H struct st_inv C init by (auto simp add: subsume_or_strengthen_pre_def forward_subsumption_one_pre_def )
   subgoal for xs
     unfolding simplify_clause_with_unit_st_pre_def forward_subsumption_one_inv_def case_prod_beta
     apply normalize_goal+
     by (rule_tac x=x in exI)
      (auto dest: in_diffD)
   subgoal
     unfolding simplify_clause_with_unit_st_pre_def forward_subsumption_one_inv_def case_prod_beta
     apply normalize_goal+
     by  (auto dest: multi_member_split)
   subgoal by (rule forward_subsumption_one_inv)
   subgoal by (auto dest!: multi_member_split)
   subgoal by auto
   subgoal
     unfolding forward_subsumption_one_inv_def
     by fast
   subgoal using C by auto
   subgoal by (simp add: rtranclp_cdcl_twl_inprocessing_l_all_learned_lits_of_l)
   subgoal by (auto dest:  rtranclp_cdcl_twl_inprocessing_l_all_init_lits_of_l)
   subgoal premises p for x s a b xa
     using p(1-7) le_C unfolding forward_subsumption_one_inv_def prod.simps p(5) apply -
     apply normalize_goal+
     apply standard
     apply (auto simp add: forward_subsumption_one_inv_def)[]
     apply simp
     apply (intro ballI)
     subgoal for xx D
     apply (subgoal_tac (is_strengthened (b)  D  strengthened_by (b)) 
       (is_subsumed (b)  D  subsumed_by (b)))
     apply (cases b)
     apply (simp add:)
     apply (simp add:)
     apply blast
     apply (cases b)
     apply (intro conjI)
     apply (auto simp add: forward_subsumption_one_select_def dest!: multi_member_split[of D])[3]
     apply (simp only: subsumption.disc simp_thms)
     done
   done
   done
qed


definition simplify_clauses_with_unit_st_pre where
  simplify_clauses_with_unit_st_pre S  (T.
  (S, T)  twl_st_l None 
  twl_struct_invs T 
  twl_list_invs S 
  clauses_to_update_l S = {#} 
  cdclW_restart_mset.cdclW_learned_clauses_entailed_by_init (stateW_of T) 
  count_decided (get_trail_l S) = 0 
  set (get_all_mark_of_propagated (get_trail_l S))  {0})

definition simplify_clauses_with_unit_st_inv :: 'v twl_st_l  nat set  'v twl_st_l  bool where
  simplify_clauses_with_unit_st_inv S0 it S  (
    cdcl_twl_inprocessing_l** S0 S 
  set (get_all_mark_of_propagated (get_trail_l S)) 
    set (get_all_mark_of_propagated (get_trail_l S0))  {0})

text Hard-coding the invariants was a lot faster than the alternative way, namely proving that
  the loop invariants still holds at the end of the loop. No this makes not sense, I know.
definition simplify_clauses_with_unit_st :: 'v twl_st_l  'v twl_st_l nres  where
  simplify_clauses_with_unit_st S =
  do {
  ASSERT(simplify_clauses_with_unit_st_pre S);
  xs  SPEC(λxs. finite xs);
  T  FOREACHci(λit T. simplify_clauses_with_unit_st_inv S it T)
  (xs)
  (λS. get_conflict_l S = None)
  (λi S. if i ∈# dom_m (get_clauses_l S) then simplify_clause_with_unit_st i S else RETURN S)
  S;
  ASSERT(set_mset (all_learned_lits_of_l T)  set_mset (all_learned_lits_of_l S));
  ASSERT(set_mset (all_init_lits_of_l T) = set_mset (all_init_lits_of_l S));
  RETURN T
  }

lemma simplify_clauses_with_unit_st_inv_simplify_clauses_with_unit_st_preD:
  assumes
    inv: simplify_clauses_with_unit_st_inv S0 it T and
    pre: simplify_clauses_with_unit_st_pre S0
  shows simplify_clauses_with_unit_st_pre T
proof -
  have st: cdcl_twl_inprocessing_l** S0 T
    using inv unfolding simplify_clauses_with_unit_st_inv_def
    by blast
  obtain S where
    S0S: (S0, S)  twl_st_l None and
    struct_S: twl_struct_invs S and
    list_S: twl_list_invs S0 and
    clss_upd_S: clauses_to_update_l S0 = {#} and
    ent_S: cdclW_restart_mset.cdclW_learned_clauses_entailed_by_init (stateW_of S) and
    lvl_S: count_decided (get_trail_l S0) = 0 and
    empty: set (get_all_mark_of_propagated (get_trail_l S0))  {0}
    using pre unfolding simplify_clauses_with_unit_st_pre_def
    by blast

  obtain U where
    TU: (T, U)  twl_st_l None and
    struct_T: twl_struct_invs U and
    list_T: twl_list_invs T and
    ent_T: cdclW_restart_mset.cdclW_learned_clauses_entailed_by_init (stateW_of U)
    using rtranclp_cdcl_twl_inprocessing_l_twl_st_l[OF st S0S struct_S list_S ent_S]
    by blast
  have
    clss_upd_T: clauses_to_update_l T = {#} and
    lvl_S: count_decided (get_trail_l T) = 0
    subgoal
      using st apply (subst (asm) rtranclp_unfold)
       apply (elim disjE)
      subgoal using clss_upd_S by auto
      subgoal
        by (subst (asm) tranclp_unfold_end)
         (auto simp: cdcl_twl_inprocessing_l.simps cdcl_twl_pure_remove_l.simps
          cdcl_twl_unitres_l.simps cdcl_twl_unitres_true_l.simps
          cdcl_twl_subsumed_l.simps cdcl_twl_subresolution_l.simps)
      done
    subgoal
      using st apply (induction rule: rtranclp_induct)
      subgoal using lvl_S by auto
      subgoal
        by (auto simp: cdcl_twl_inprocessing_l.simps cdcl_twl_pure_remove_l.simps
          cdcl_twl_unitres_l.simps cdcl_twl_unitres_true_l.simps
          cdcl_twl_subsumed_l.simps cdcl_twl_subresolution_l.simps)
      done
    done
  have empty: set (get_all_mark_of_propagated (get_trail_l T))  {0}
    using st apply (induction rule: rtranclp_induct)
    subgoal using empty by auto
    subgoal by (auto simp: cdcl_twl_inprocessing_l.simps
      cdcl_twl_unitres_l.simps cdcl_twl_unitres_true_l.simps cdcl_twl_pure_remove_l.simps
      cdcl_twl_subsumed_l.simps cdcl_twl_subresolution_l.simps)
    done
  show ?thesis
    unfolding simplify_clauses_with_unit_st_pre_def
    by (rule exI[of _ U])
     (use TU struct_T list_T ent_T clss_upd_T lvl_S empty in auto)
qed

lemma simplify_clauses_with_unit_st_spec:
  assumes count_decided (get_trail_l S) = 0
    get_conflict_l S = None and
    clauses_to_update_l S = {#} and
    ST: (S, T)  twl_st_l None and
    st_invs: twl_struct_invs T and
    list_invs: twl_list_invs S and
    empty: set (get_all_mark_of_propagated (get_trail_l S))  {0} and
    cdclW_restart_mset.cdclW_learned_clauses_entailed_by_init ((stateW_of T)) 
  shows simplify_clauses_with_unit_st S  Id (SPEC(λT. cdcl_twl_inprocessing_l** S T 
    simplify_clauses_with_unit_st_inv S {} T))
proof -
  show ?thesis
    unfolding simplify_clauses_with_unit_st_def
    apply (refine_vcg)
    subgoal using assms apply -
      unfolding simplify_clauses_with_unit_st_pre_def
      by (rule exI[of _ T]) auto
    subgoal
      unfolding simplify_clauses_with_unit_st_inv_def
        simplify_clauses_with_unit_st_pre_def
      by blast
    subgoal  for x xa it σ
      apply (frule simplify_clauses_with_unit_st_inv_simplify_clauses_with_unit_st_preD,
        assumption)
       apply (unfold simplify_clauses_with_unit_st_pre_def)
       apply normalize_goal+
      apply (rule simplify_clause_with_unit_st_spec[THEN order_trans])
      subgoal
        unfolding simplify_clauses_with_unit_st_inv_def simplify_clause_with_unit_st_pre_def
        by normalize_goal+ fast
      subgoal
        using empty
        by (auto 5 3 simp: simplify_clauses_with_unit_st_inv_def distinct_mset_dom
          rtranclp.rtrancl_into_rtrancl[of _ S] distinct_mset_remove1_All
          dest!: cdcl_twl_inprocessing_l.intros)
      done
    subgoal for x σ
      unfolding simplify_clauses_with_unit_st_inv_def
      by (auto dest: rtranclp_cdcl_twl_inprocessing_l_all_learned_lits_of_l)
    subgoal
      unfolding simplify_clauses_with_unit_st_inv_def
      by (auto dest: rtranclp_cdcl_twl_inprocessing_l_all_learned_lits_of_l)
    subgoal
      unfolding simplify_clauses_with_unit_st_inv_def
      by (auto dest: rtranclp_cdcl_twl_inprocessing_l_all_init_lits_of_l)
    subgoal
      unfolding simplify_clauses_with_unit_st_inv_def
      by auto
    subgoal
      unfolding simplify_clauses_with_unit_st_inv_def
      by (auto dest: rtranclp_cdcl_twl_inprocessing_l_all_learned_lits_of_l)
    subgoal
      unfolding simplify_clauses_with_unit_st_inv_def
      by (auto dest: rtranclp_cdcl_twl_inprocessing_l_all_init_lits_of_l)
    subgoal
      unfolding simplify_clauses_with_unit_st_inv_def
      by auto
    subgoal
      unfolding simplify_clauses_with_unit_st_inv_def
      by auto
    done
qed

definition simplify_clauses_with_units_st_pre where
  simplify_clauses_with_units_st_pre S 
  (T. (S, T)  twl_st_l None  twl_struct_invs T  twl_list_invs S 
    cdclW_restart_mset.cdclW_learned_clauses_entailed_by_init ((stateW_of T)) 
    set (get_all_mark_of_propagated (get_trail_l S))  {0} 
    clauses_to_update_l S = {#} 
    count_decided (get_trail_l S) = 0)

definition simplify_clauses_with_units_st where
  simplify_clauses_with_units_st S = do {
    ASSERT(simplify_clauses_with_units_st_pre S);
    new_units  SPEC (λb. b  get_conflict_l S = None);
    if new_units
    then simplify_clauses_with_unit_st S
    else RETURN S}


lemma simplify_clauses_with_units_st_spec:
  assumes count_decided (get_trail_l S) = 0
    clauses_to_update_l S = {#} and
    ST: (S, T)  twl_st_l None and
    st_invs: twl_struct_invs T and
    list_invs: twl_list_invs S and
    cdclW_restart_mset.cdclW_learned_clauses_entailed_by_init ((stateW_of T)) and
    set (get_all_mark_of_propagated (get_trail_l S))  {0}
  shows simplify_clauses_with_units_st S  Id (SPEC(λT. cdcl_twl_inprocessing_l** S T 
    simplify_clauses_with_unit_st_inv S {} T))
  using assms unfolding simplify_clauses_with_units_st_def
  apply (refine_vcg simplify_clauses_with_unit_st_spec[THEN order_trans])
  subgoal unfolding simplify_clauses_with_units_st_pre_def by blast
  subgoal by auto
  apply assumption+
  subgoal by auto
  subgoal by auto
  subgoal unfolding simplify_clauses_with_unit_st_inv_def by auto
  done

definition try_to_forward_subsume_inv :: 'v twl_st_l  nat multiset  nat  nat × bool × 'v twl_st_l  bool where
  try_to_forward_subsume_inv S0 = (λcands C (i,brk,S).
  (cdcl_twl_inprocessing_l** S0 S 
  clauses_to_update_l S = {#} 
  count_decided (get_trail_l S) = 0 
  set (get_all_mark_of_propagated (get_trail_l S))  {0} 
  (¬brk  (get_conflict_l S = None  C ∈# dom_m (get_clauses_l S) 
  (L ∈# mset (get_clauses_l S  C). undefined_lit (get_trail_l S) L) 
  length (get_clauses_l S  C) > 2)) 
  (get_trail_l S = get_trail_l S0) 
  (D∈#remove1_mset C (dom_m (get_clauses_l S)).
  D ∈# dom_m (get_clauses_l S0)  get_clauses_l S  D = get_clauses_l S0  D) 
  (D∈#cands. D  C  (D ∈# dom_m (get_clauses_l S0)) = (D ∈# dom_m (get_clauses_l S)))))

definition try_to_forward_subsume_pre :: nat  nat multiset  'v twl_st_l  bool where
  try_to_forward_subsume_pre = (λC xs S.
  T. C  0 
  (S, T)  twl_st_l None 
  twl_struct_invs T 
  twl_list_invs S 
  clauses_to_update_l S = {#} 
  get_conflict_l S = None 
  cdclW_restart_mset.cdclW_learned_clauses_entailed_by_init (stateW_of T) 
  count_decided (get_trail_l S) = 0 
  set (get_all_mark_of_propagated (get_trail_l S))  {0} 
  C ∈# dom_m (get_clauses_l S) 
  (L ∈# mset (get_clauses_l S  C). undefined_lit (get_trail_l S) L) 
  length (get_clauses_l S  C) > 2)

definition try_to_forward_subsume :: nat  nat multiset  'v twl_st_l  'v twl_st_l nres where
  try_to_forward_subsume C xs S0 = do {
  ASSERT (try_to_forward_subsume_pre C xs S0);
  n  RES {_::nat. True};
  ebreak  RES {_::bool. True};
  (_, _, S)  WHILETtry_to_forward_subsume_inv S0 xs C(λ(i, break, S). ¬break  i < n)
    (λ(i, break, S). do {
      (S, subs)  forward_subsumption_one C xs S;
      ebreak  RES {_::bool. True};
      RETURN (i+1, subs  ebreak, S)
    })
  (0, ebreak, S0);
  RETURN S
  }
  

lemma try_to_forward_forward_subsumption_one_pre:
  try_to_forward_subsume_pre C xs S 
  try_to_forward_subsume_inv S xs C (a, False, ba)  forward_subsumption_one_pre C xs ba
  unfolding try_to_forward_subsume_inv_def forward_subsumption_one_pre_def case_prod_beta
    try_to_forward_subsume_pre_def
  apply normalize_goal+
  apply (elim rtranclp_cdcl_twl_inprocessing_l_twl_st_l)
  apply assumption+
  apply (rule_tac x=V in exI)
  apply simp
  done

lemma in_remove1_mset_dom_m_iff[simp]: a ∈# remove1_mset C (dom_m N)  a  C  a ∈# dom_m N
  using distinct_mset_dom[of N]
  by (cases C ∈# dom_m N; auto dest: multi_member_split)

lemma try_to_forward_subsume:
  assumes try_to_forward_subsume_pre C cands S
  shows try_to_forward_subsume C cands S  Id (SPEC(λT. cdcl_twl_inprocessing_l** S T 
      (length (get_clauses_l S  C) > 2  get_trail_l T = get_trail_l S) 
     (D∈#cands. D  C ((D ∈# dom_m (get_clauses_l T)) = (D ∈# dom_m (get_clauses_l S)))) 
     (D∈#remove1_mset C (dom_m (get_clauses_l T)).
       D ∈# dom_m (get_clauses_l S)  get_clauses_l T  D = get_clauses_l S  D)))
proof -
  have wf: wf (measure (λ(i, _, _). Suc n - i)) for n
    by auto
  show ?thesis
    unfolding try_to_forward_subsume_def
    apply (refine_vcg forward_subsumption_one[THEN order_trans])
    subgoal using assms by auto
    apply (rule_tac n1=x in wf)
    subgoal unfolding try_to_forward_subsume_inv_def try_to_forward_subsume_pre_def prod.simps
      by (auto dest: in_diffD)
    subgoal for x s a b aa ba
      by (auto intro!: try_to_forward_forward_subsumption_one_pre)
    subgoal
      unfolding conc_fun_RES RES_RETURN_RES
      apply (rule RES_rule, unfold Image_iff)
      apply (elim bexE, unfold mem_Collect_eq)
      apply (case_tac xa, hypsubst, unfold prod.simps)
      apply (rule RES_rule)
      apply (simp add: try_to_forward_subsume_inv_def)
      apply (intro conjI impI)
      apply  (auto simp: conc_fun_RES RES_RETURN_RES
        try_to_forward_subsume_inv_def dest: in_diffD
        dest: rtranclp_cdcl_twl_inprocessing_l_clauses_to_update_l
          rtranclp_cdcl_twl_inprocessing_l_count_decided rtranclp_cdcl_twl_inprocessing_l_get_all_mark_of_propagated)
      done
    subgoal by (auto simp: try_to_forward_subsume_inv_def)
    subgoal by (auto simp: try_to_forward_subsume_inv_def)
    subgoal by (auto simp: try_to_forward_subsume_inv_def)
    subgoal by (auto simp: try_to_forward_subsume_inv_def)
    done
qed

definition forward_subsumption_all_pre :: 'v twl_st_l  bool where
  forward_subsumption_all_pre = (λS.
  T.
    (S, T)  twl_st_l None 
    twl_struct_invs T 
    twl_list_invs S 
    clauses_to_update_l S = {#} 
    cdclW_restart_mset.cdclW_learned_clauses_entailed_by_init (stateW_of T) 
    count_decided (get_trail_l S) = 0 
    set (get_all_mark_of_propagated (get_trail_l S))  {0})


definition forward_subsumption_all_inv :: 'v twl_st_l  nat multiset × 'v twl_st_l  bool where
  forward_subsumption_all_inv S = (λ(xs, T). S'. (S,S') twl_st_l None   cdcl_twl_inprocessing_l** S T  xs ⊆# dom_m (get_clauses_l S) 
    twl_struct_invs S' 
    twl_list_invs S 
    clauses_to_update_l S = {#} 
    cdclW_restart_mset.cdclW_learned_clauses_entailed_by_init (stateW_of S') 
    count_decided (get_trail_l S) = 0 
    get_trail_l T = get_trail_l S  (C∈#xs. get_clauses_l T  C = get_clauses_l S  C)  xs ⊆# dom_m (get_clauses_l T))

definition forward_subsumption_all_cands where
  forward_subsumption_all_cands S xs  xs ⊆# dom_m (get_clauses_l S) 
  (C∈#xs. (Lset (get_clauses_l S  C). undefined_lit (get_trail_l S) L) 
  length (get_clauses_l S  C) > 2)

definition forward_subsumption_all :: 'v twl_st_l  'v twl_st_l nres where
  forward_subsumption_all = (λS. do {
  ASSERT (forward_subsumption_all_pre S);
  xs  SPEC (forward_subsumption_all_cands S);
  (xs, S) 
    WHILETforward_subsumption_all_inv S(λ(xs, S). xs  {#}  get_conflict_l S = None)
    (λ(xs, S). do {
       C  SPEC(λC'. C' ∈# xs);
       T  try_to_forward_subsume C xs S;
       ASSERT (D∈#remove1_mset C xs. get_clauses_l T  D = get_clauses_l S  D);
       RETURN (remove1_mset C xs, T)
    })
    (xs, S);
  RETURN S
  }
)


lemma forward_subsumption_all:
  assumes forward_subsumption_all_pre S
  shows forward_subsumption_all S  Id (SPEC(cdcl_twl_inprocessing_l** S))
proof -
  let ?R = measure (λ(xs, _). size xs)
  have simplify_clause_with_unit_st_pre: simplify_clause_with_unit_st_pre D T
    if
      forward_subsumption_all_pre S and
      C ⊆# dom_m (get_clauses_l S) and
      forward_subsumption_all_inv S xsS and
      case xsS of (xs, S)  xs  {#}  get_conflict_l S = None and
      st: xsS = (Cs, T) and
      D ∈# Cs and
      ¬ D ∉# dom_m (get_clauses_l T)
    for C xsS Cs T D
  proof -
    have cdcl_twl_inprocessing_l** S T
      using that unfolding simplify_clause_with_unit_st_pre_def forward_subsumption_all_inv_def st prod.simps
      apply - apply normalize_goal+
      by auto
  show ?thesis
      using rtranclp_cdcl_twl_inprocessing_l_get_all_mark_of_propagated[of S T] rtranclp_cdcl_twl_inprocessing_l_twl_st_l[of S T]
      using that unfolding simplify_clause_with_unit_st_pre_def forward_subsumption_all_inv_def st prod.simps
        forward_subsumption_all_pre_def
      apply - apply normalize_goal+
      apply (auto dest: rtranclp_cdcl_twl_inprocessing_l_count_decided
        rtranclp_cdcl_twl_inprocessing_l_clauses_to_update_l
        rtranclp_cdcl_twl_inprocessing_l_get_all_mark_of_propagated)
      apply (meson rtranclp_cdcl_twl_inprocessing_l_twl_list_invs)
      by metis
  qed
  have 0: Id  (do {
  ASSERT (forward_subsumption_all_pre S);
  xs  SPEC (λxs. xs ⊆# (dom_m (get_clauses_l S)));
  (xs, S) 
    WHILETλ(xs, T). cdcl_twl_inprocessing_l** S T  xs ⊆# dom_m (get_clauses_l S)(λ(xs, S). xs  {#}  get_conflict_l S = None)
    (λ(xs, S'). do {
       C  SPEC(λC'. C' ∈# xs);
       S  SPEC(λT. cdcl_twl_inprocessing_l** S' T  get_trail_l T = get_trail_l S  length (get_clauses_l S  C) > 2 
          remove1_mset C xs ⊆# dom_m (get_clauses_l T) 
          (D∈#remove1_mset C (dom_m (get_clauses_l T)). D ∈# dom_m (get_clauses_l S')  get_clauses_l T  D = get_clauses_l S'  D));
        RETURN (remove1_mset C xs, S)
    })
    (xs, S);
  RETURN S
           })  Id (SPEC(cdcl_twl_inprocessing_l** S))
    apply (subst (1) Down_id_eq)
    apply (refine_vcg WHILEIT_rule[where R= ?R] forward_subsumption_one[THEN order_trans]
      simplify_clause_with_unit_st_spec[THEN order_trans])
    subgoal using assms by auto
    subgoal by auto
    subgoal by (auto simp: forward_subsumption_all_inv_def)
    subgoal by (auto simp: forward_subsumption_all_inv_def)
    subgoal by (auto simp: size_mset_remove1_mset_le_iff)
    subgoal by (auto simp: forward_subsumption_all_inv_def)
    subgoal by (auto simp: size_mset_remove1_mset_le_iff)
    subgoal by (auto simp: forward_subsumption_all_inv_def)
    done
  let ?R = λxs0. {((xs, U), (ys,V)). (xs,ys)Id  xs ⊆# xs0  distinct_mset xs  (U, V)  Id  get_trail_l U = get_trail_l S 
      (C∈# xs. C ∈#dom_m (get_clauses_l U)  get_clauses_l U  C = get_clauses_l S  C)  xs ⊆# dom_m (get_clauses_l U)}
  have try_to_forward_subsume_pre: try_to_forward_subsume_pre C x1a x2a
    if
     pre: forward_subsumption_all_pre S and
      (xs, xsa)  Id and
      cands: xs  Collect (forward_subsumption_all_cands S) and
      xsa  {xs. xs ⊆# dom_m (get_clauses_l S)} and
      xx': (x, x')  ?R xsa and
      break: case x of (xs, S)  xs  {#}  get_conflict_l S = None and
      case x' of (xs, S)  xs  {#}  get_conflict_l S = None and
      inv: forward_subsumption_all_inv S x and
      st: x' = (x1, x2) x = (x1a, x2a) and
      CCa: (C, Ca)  nat_rel and
       C  {C'. C' ∈# x1a} and
      Ca  {C'. C' ∈# x1} 
    for xs xsa x x' x1 x2 x1a x2a C Ca T U
  proof -
    have TU: x2 = x2a and
      Sx2a: cdcl_twl_inprocessing_l** S x2 and
      st': x1 = x1a x2 = x2a C=Ca and
      C:  C ∈# dom_m (get_clauses_l x2a)  and
      undef: Lset (get_clauses_l x2  C). undefined_lit (get_trail_l x2a) L and
      [intro]: 2 < length (get_clauses_l x2a  Ca) and
      confl: get_conflict_l x2a = None
      using inv xx' CCa cands break
      unfolding forward_subsumption_all_inv_def st forward_subsumption_all_cands_def
      by (auto dest!: multi_member_split simp: ball_conj_distrib)
       (metis mem_Collect_eq pair_in_Id_conv subset_mset.le_iff_add that(13) that(2) union_iff)+
    obtain x where
      Sx: (S, x)  twl_st_l None and
      struct_S: twl_struct_invs x and
      list_S: twl_list_invs S and
      [simp]: clauses_to_update_l S = {#} and
      ent_S: cdclW_restart_mset.cdclW_learned_clauses_entailed_by_init (stateW_of x) and
      [simp]: count_decided (get_trail_l S) = 0 and
      marks: set (get_all_mark_of_propagated (get_trail_l S))  {0}
      using pre unfolding forward_subsumption_all_pre_def by fast
    have [intro]: 2 < length (get_clauses_l x2a  Ca)
        using CCa by auto

    show ?thesis
      using undef C confl apply -
      apply (rule rtranclp_cdcl_twl_inprocessing_l_twl_st_l[OF Sx2a Sx struct_S list_S ent_S])
      subgoal for V
      unfolding st' TU
      unfolding try_to_forward_subsume_pre_def TU
      apply (rule_tac x=V in exI)
      using rtranclp_cdcl_twl_inprocessing_l_clauses_to_update_l[OF Sx2a]
        rtranclp_cdcl_twl_inprocessing_l_get_all_mark_of_propagated[OF Sx2a]
        rtranclp_cdcl_twl_inprocessing_l_count_decided[OF Sx2a]
        rtranclp_cdcl_twl_inprocessing_l_get_all_mark_of_propagated[OF Sx2a]
        marks
      by (auto simp add: twl_list_invs_def TU)
      done
  qed
  have [refine]: (xs,xsa)Id xsa ⊆# dom_m (get_clauses_l S)((xs, S), xsa, S)  ?R xsa for xs xsa
    by (auto simp add: Duplicate_Free_Multiset.distinct_mset_mono distinct_mset_dom)
  show ?thesis
    apply (rule order_trans)
    prefer 2
    apply (rule 0)
    unfolding forward_subsumption_all_def
    apply (refine_vcg WHILEIT_refine[where R=?R xs for xs]
      try_to_forward_subsume
      forward_subsumption_one[THEN order_trans])
    subgoal using assms by (auto simp: forward_subsumption_all_cands_def)
    subgoal by auto
    subgoal unfolding forward_subsumption_all_pre_def forward_subsumption_all_inv_def case_prod_beta apply normalize_goal+
      by (rename_tac xa, rule_tac x=xa in exI) (auto simp: forward_subsumption_all_inv_def)
    subgoal by (auto simp: forward_subsumption_all_inv_def)
    subgoal by (auto simp: size_mset_remove1_mset_le_iff)
    apply (rule try_to_forward_subsume[THEN order_trans])
    subgoal by (rule try_to_forward_subsume_pre)
    subgoal unfolding Down_id_eq
      by (rule SPEC_rule) (auto simp: forward_subsumption_all_cands_def Ball_def
        forward_subsumption_all_inv_def distinct_mset_remove1_All 
        intro!: distinct_subseteq_iff[THEN iffD1])
    subgoal
      by (auto dest: in_diffD simp: distinct_mset_remove1_All)
    subgoal
      by (auto dest: in_diffD simp: distinct_mset_remove1_All)
    subgoal by auto
    done
qed


definition forward_subsumption_needed_l :: _ where
  forward_subsumption_needed_l S = SPEC (λ_. True)

definition forward_subsume_l :: _ where
  forward_subsume_l S = do {
     ASSERT (forward_subsumption_all_pre S);
     b  forward_subsumption_needed_l S;
     if b then forward_subsumption_all S else RETURN S
}

lemma forward_subsume_l:
  assumes forward_subsumption_all_pre S
  shows forward_subsume_l S  Id (SPEC(cdcl_twl_inprocessing_l** S))
  unfolding forward_subsume_l_def forward_subsumption_needed_l_def
  by (refine_vcg forward_subsumption_all[unfolded Down_id_eq]) (use assms in auto)

subsection Pure Literal Deletion
definition propagate_pure_l_pre:: 'v literal  'v twl_st_l  bool where
  propagate_pure_l_pre L S 
  (S'. (S, S')  twl_st_l None  L ∈# all_init_lits_of_l S  undefined_lit (get_trail_l S) L 
  clauses_to_update_l S = {#}  get_conflict_l S = None 
  count_decided (get_trail_l S) = 0  -L  (set_mset ` set_mset (mset `# get_init_clss_l S)))

definition propagate_pure_bt_l :: 'v literal  'v twl_st_l  'v twl_st_l nres where
  propagate_pure_bt_l = (λL (M, N, D, NE, UE, NEk, UEk, NS, US, N0, U0, WS, Q). do {
    ASSERT(propagate_pure_l_pre L (M, N, D, NE, UE, NEk, UEk, NS, US, N0, U0, WS, Q));
    M  cons_trail_propagate_l L 0 M;
    RETURN (M, N, D, NE, UE, add_mset {#L#} NEk, UEk, NS, US, N0, U0, WS, add_mset (-L) Q)})

lemma in_all_lits_of_mm_Pos_Neg:
  L ∈# all_lits_of_mm A  L  (set_mset ` set_mset A)   -L  (set_mset ` set_mset A)
  by (cases L)
   (auto simp: all_lits_of_mm_def image_UN dest!: multi_member_split)

lemma propagate_pure_bt_l_spec:
  assumes
    propagate_pure_l_pre L S and
    undefined_lit (get_trail_l S) L
  shows propagate_pure_bt_l L S  SPEC (λT. cdcl_twl_pure_remove_l S T)
proof -
  have [iff]: ¬(b. ¬ b) (b. b)
    by blast+
  show ?thesis
    using assms
    unfolding propagate_pure_bt_l_def cons_trail_propagate_l_def
    apply (cases S)
    apply refine_vcg
    subgoal by simp
    subgoal by auto
    subgoal
      unfolding propagate_pure_l_pre_def
      apply (subst cdcl_twl_pure_remove_l.simps)
      apply simp
      apply (clarsimp simp add: propagate_pure_l_pre_def get_init_clss_l_def
        all_init_lits_of_l_def all_lits_of_mm_union)
      apply metis
      done
    done
qed


lemma in_all_lits_of_mm_direct_inI: -L  (set_mset ` set_mset A)  L ∈# all_lits_of_mm A
  by (auto simp: all_lits_of_mm_add_mset all_lits_of_m_add_mset
    dest!: multi_member_split)
lemma cdcl_twl_pure_remove_l_same:
  cdcl_twl_pure_remove_l S T  count_decided (get_trail_l T) = count_decided (get_trail_l S)
  cdcl_twl_pure_remove_l S T  clauses_to_update_l T = clauses_to_update_l S
  cdcl_twl_pure_remove_l S T  get_conflict_l T = get_conflict_l S
  cdcl_twl_pure_remove_l S T  get_init_clss_l T = get_init_clss_l S
  cdcl_twl_pure_remove_l S T  get_subsumed_init_clauses_l T = get_subsumed_init_clauses_l S
  cdcl_twl_pure_remove_l S T  get_init_clauses0_l T = get_init_clauses0_l S
  cdcl_twl_pure_remove_l S T  set_mset (all_init_lits_of_l T) = set_mset (all_init_lits_of_l S) and
 cdcl_twl_pure_remove_l_same_unit_init_subsetD:
  cdcl_twl_pure_remove_l S T  get_unit_init_clauses_l S ⊆# get_unit_init_clauses_l T
proof -
  have [iff]: ¬(b. ¬ b)
    by blast
  have [simp]: L ∈# a -L ∈# all_lits_of_m (a)L ∈# a L ∈# all_lits_of_m (a) for L a
    by (simp_all add: atm_of_lit_in_atms_of in_all_lits_of_m_ain_atms_of_iff in_clause_in_all_lits_of_m)
  have [simp]: - L ∈# all_lits_of_mm b  L ∈# all_lits_of_mm b for b L
    by (cases L; auto simp: all_lits_of_mm_def)
  show
    cdcl_twl_pure_remove_l S T  count_decided (get_trail_l T) = count_decided (get_trail_l S)
    cdcl_twl_pure_remove_l S T  clauses_to_update_l T = clauses_to_update_l S
    cdcl_twl_pure_remove_l S T  get_conflict_l T = get_conflict_l S
    cdcl_twl_pure_remove_l S T  get_init_clss_l T = get_init_clss_l S
    cdcl_twl_pure_remove_l S T  get_subsumed_init_clauses_l T = get_subsumed_init_clauses_l S
    cdcl_twl_pure_remove_l S T  get_init_clauses0_l T = get_init_clauses0_l S
    cdcl_twl_pure_remove_l S T  set_mset (all_init_lits_of_l T) = set_mset (all_init_lits_of_l S)
    cdcl_twl_pure_remove_l S T  get_unit_init_clauses_l S ⊆# get_unit_init_clauses_l T
    by (solves auto simp: cdcl_twl_pure_remove_l.simps all_init_lits_of_l_def get_init_clss_l_def
      all_lits_of_mm_add_mset all_lits_of_m_add_mset
      intro: in_all_lits_of_mm_direct_inI
      dest!: multi_member_split[of _ ran_m _]
        multi_member_split[of _ _ :: _ clauses])+
     (auto simp: cdcl_twl_pure_remove_l.simps all_init_lits_of_l_def get_init_clss_l_def
      all_lits_of_mm_add_mset all_lits_of_m_add_mset all_lits_of_mm_union
      intro: in_all_lits_of_mm_direct_inI
      dest!: multi_member_split[of _ ran_m _]
        multi_member_split[of _ _ :: _ clauses])
qed

lemma rtranclp_cdcl_twl_pure_remove_l_same:
  cdcl_twl_pure_remove_l** S T  count_decided (get_trail_l T) = count_decided (get_trail_l S)
  cdcl_twl_pure_remove_l** S T  clauses_to_update_l T = clauses_to_update_l S
  cdcl_twl_pure_remove_l** S T  get_conflict_l T = get_conflict_l S
  cdcl_twl_pure_remove_l** S T  get_init_clss_l T = get_init_clss_l S
  cdcl_twl_pure_remove_l** S T  get_subsumed_init_clauses_l T = get_subsumed_init_clauses_l S
  cdcl_twl_pure_remove_l** S T  get_init_clauses0_l T = get_init_clauses0_l S
  cdcl_twl_pure_remove_l** S T  set_mset (all_init_lits_of_l T) = set_mset (all_init_lits_of_l S) and
  rtranclp_cdcl_twl_pure_remove_l_same_unit_init_subsetD:
  cdcl_twl_pure_remove_l** S T  get_unit_init_clauses_l S ⊆# get_unit_init_clauses_l T
  by (solves induction rule: rtranclp_induct; auto simp: cdcl_twl_pure_remove_l_same
    dest: cdcl_twl_pure_remove_l_same_unit_init_subsetD)+


subsection Pure Literal deletion

text 
  Pure literal deletion is not really used nowadays (as it is subsumed by variable elimination),
  but it is the first non-model preserving model we intend to implement and it should be easy to
  implement.

  In the implementation, it would better to simplify also when going over the clauses, instead of 
  either (i) no simplifying anything or (ii) simplify all clauses. However, in the current version,
  I can reuse the other proofs.

  

definition pure_literal_deletion_pre where
  pure_literal_deletion_pre S 
  (S'. (S, S')  twl_st_l None 
  clauses_to_update_l S = {#}  get_conflict_l S = None  cdclW_restart_mset.cdclW_learned_clauses_entailed_by_init (stateW_of S') 
  count_decided (get_trail_l S) = 0  twl_list_invs S  twl_struct_invs S')

definition pure_literal_deletion_candidates where
  pure_literal_deletion_candidates S = SPEC (λLs. set_mset Ls  atms_of_mm (get_all_init_clss_l S))

definition pure_literal_deletion_l_inv where
  pure_literal_deletion_l_inv S xs0 = (λ(T, xs).
     S'. (S, S')  twl_st_l None  cdcl_twl_pure_remove_l** S T  xs ⊆# xs0)


definition pure_literal_deletion_l :: 'v twl_st_l  'v twl_st_l nres where
  pure_literal_deletion_l S = do {
    ASSERT (pure_literal_deletion_pre S);
    let As = (set_mset ` set_mset (mset `# get_init_clss_l S));
    xs  pure_literal_deletion_candidates S;
    (S, xs)  WHILETpure_literal_deletion_l_inv S xs(λ(S, xs). xs  {#})
      (λ(S, xs). do {
        L  SPEC (λL. L ∈# xs);
        A  RES {Pos L, Neg L};
        if -A  As  undefined_lit (get_trail_l S) A
        then do {S  propagate_pure_bt_l A S;
          RETURN (S, remove1_mset L xs)}
        else RETURN (S, remove1_mset L xs)
      })
    (S, xs);
  RETURN S
  }


lemma pure_literal_deletion_l_spec:
  assumes pure_literal_deletion_pre S
  shows
    pure_literal_deletion_l S  SPEC (λT. cdcl_twl_pure_remove_l** S T)
proof -
  have [simp]: a - Suc b < a - b  Suc b  a for a b
    by auto
  have [refine0]: wf (measure (λ(_, x). size x))
    by auto
  show ?thesis
    unfolding pure_literal_deletion_l_def Let_def pure_literal_deletion_candidates_def
    apply (refine_vcg propagate_pure_bt_l_spec[THEN order_trans])
    subgoal using assms by fast
    subgoal unfolding pure_literal_deletion_l_inv_def pure_literal_deletion_pre_def
      by fast
    subgoal for x s T xs L unfolding propagate_pure_l_pre_def pure_literal_deletion_l_inv_def
       case_prod_beta pure_literal_deletion_pre_def
      apply normalize_goal+
      apply (frule rtranclp_cdcl_twl_pure_remove_l_cdcl_twl_pure_remove)
      apply assumption+
      apply normalize_goal+
      apply (rule_tac x=xd in exI)
      using rtranclp_cdcl_twl_pure_remove_l_same_unit_init_subsetD[of S T]
      unfolding atms_of_ms_def[symmetric]
      apply (auto simp add: rtranclp_cdcl_twl_pure_remove_l_same
        all_init_lits_of_l_def in_all_lits_of_mm_ain_atms_of_iff 
        dest!: multi_member_split[of L xs] multi_member_split[of L x]
        mset_subset_eq_insertD)
      apply (metis UnCI atms_of_ms_union set_mset_union subset_mset.le_iff_add)
      by (metis Un_iff atms_of_ms_union set_mset_union subset_mset.le_iff_add)
    subgoal by simp
    subgoal by (auto dest!: multi_member_split
      simp: pure_literal_deletion_l_inv_def)
    subgoal by (auto dest!: multi_member_split
      simp: pure_literal_deletion_l_inv_def)
    subgoal
      by (simp add: pure_literal_deletion_l_inv_def)
    subgoal
      by (auto dest!: multi_member_split)
    subgoal by (simp add: pure_literal_deletion_l_inv_def)
    done
qed




definition pure_literal_count_occs_l_clause_invs :: nat  'v twl_st_l _ nat × ('v literal  bool)  bool where
  pure_literal_count_occs_l_clause_invs C S occs = (λ(i, occs2). i  length (get_clauses_l S  C) 
     (L. occs2 L = (occs L  (L  set (take i (get_clauses_l S  C))))))

definition pure_literal_count_occs_l_clause_pre :: nat  'v twl_st_l  _  bool where
  pure_literal_count_occs_l_clause_pre C S occs = (C ∈# dom_m (get_clauses_l S)  irred (get_clauses_l S) C)

definition pure_literal_count_occs_l_clause :: nat  'v twl_st_l  _  ('v literal  bool) nres where
  pure_literal_count_occs_l_clause C S occs = do {
    ASSERT (pure_literal_count_occs_l_clause_pre C S occs);
    (i, occs)  WHILETpure_literal_count_occs_l_clause_invs C S occs(λ(i, occs). i < length (get_clauses_l S  C))
      (λ(i, occs). do {
        let L = get_clauses_l S  C ! i;
        let occs = occs (L := True);
        RETURN (i+1, occs)
      })
      (0, occs);
   RETURN occs
 }


lemma pure_literal_count_occs_l_clause_spec:
  assumes pure_literal_count_occs_l_clause_pre C S occs
  shows pure_literal_count_occs_l_clause C S occs  SPEC (λoccs2.
     (occs2 = (λL. if L  set (get_clauses_l S  C) then True else occs L)))
proof -
  have [iff]: (j. ¬ j < a)  a = 0 for a :: nat
    by auto
  have [simp]: filter_mset P xs - xs = {#} for P xs
    by (induction xs) auto
  have [refine0]: wf (measure (λ(A, _). length (get_clauses_l S  C) - A))
    by auto
  show ?thesis
    unfolding pure_literal_count_occs_l_clause_def
    apply (refine_vcg)
    subgoal using assms by fast
    subgoal unfolding pure_literal_count_occs_l_clause_invs_def
      by (auto dest: in_diffD dest: multi_member_split)
    subgoal unfolding pure_literal_count_occs_l_clause_invs_def
      apply simp
      by (metis in_set_conv_iff less_Suc_eq)
    subgoal
      by auto
    subgoal unfolding pure_literal_count_occs_l_clause_invs_def
      by auto
    done
qed

definition pure_literal_count_occs_l_pre :: _ where
  pure_literal_count_occs_l_pre S 
    (S'. (S, S')  twl_st_l None 
    clauses_to_update_l S = {#}  get_conflict_l S = None 
    set (get_all_mark_of_propagated (get_trail_l S))  {0} 
    cdclW_restart_mset.cdclW_learned_clauses_entailed_by_init (stateW_of S') 
    count_decided (get_trail_l S) = 0  twl_list_invs S  twl_struct_invs S')

definition pure_literal_count_occs_l_inv :: 'v twl_st_l  nat multiset  nat multiset × ('v literal  bool) × bool  _ where
  pure_literal_count_occs_l_inv S xs0 = (λ(A, occs, _). A ⊆# xs0 
    (occs = (λL. L(set_mset ` set_mset (mset `# (λC. get_clauses_l S  C) `# ({#C ∈# xs0. (C ∈# dom_m (get_clauses_l S)  irred (get_clauses_l S) C)#} - A))))))

text We allow the solver to abort the search (for example if you already know that there are no 
pure literals).

definition pure_literal_count_occs_l :: 'v twl_st_l  _ where
  pure_literal_count_occs_l S = do {
  ASSERT (pure_literal_count_occs_l_pre S);
  xs  SPEC (λxs. distinct_mset xs  (C∈#dom_m (get_clauses_l S). irred (get_clauses_l S) C  C ∈# xs));
  abort  RES (UNIV :: bool set);
  let occs = (λ_. False);
  (_, occs, abort)  WHILETpure_literal_count_occs_l_inv S xs(λ(A, occs, abort). A  {#}  ¬abort)
      (λ(A, occs, abort). do {
        ASSERT (A  {#});
        C  SPEC (λC. C ∈# A);
        if (C ∈# dom_m (get_clauses_l S)  irred (get_clauses_l S) C) 
        then do {
            occs  pure_literal_count_occs_l_clause C S occs;
            abort  RES (UNIV :: bool set);
            RETURN (remove1_mset C A, occs, abort)
        } else RETURN  (remove1_mset C A, occs, abort)
      })
      (xs, occs, abort);
   RETURN (abort, occs)
  }

(*TODO Move*)
lemma filter_mset_remove_itself [simp]: filter_mset P xs - xs = {#} for P xs
  by (induction xs) auto
(*END Move*)

lemma pure_literal_count_occs_l_spec:
  assumes pure_literal_count_occs_l_pre S
  shows pure_literal_count_occs_l S  SPEC (λ(abort, occs). ¬abort 
     (L. occs L = (L  (set_mset ` set_mset (mset `# get_init_clss_l S)))))
proof -
  have H: y∉# A  x ∈# A - remove1_mset y B  x ∈# A - B  for x y A B
    apply (auto simp: minus_remove1_mset_if)
    done
  have [refine0]: wf (measure (λ(A, _). size A))
    by auto
  show ?thesis
    unfolding pure_literal_count_occs_l_def
    apply (refine_vcg pure_literal_count_occs_l_clause_spec)
    subgoal using assms by fast
    subgoal unfolding pure_literal_count_occs_l_inv_def
      by (auto dest: in_diffD dest: multi_member_split)
    subgoal by auto
    subgoal
      unfolding pure_literal_count_occs_l_clause_pre_def
      by simp
    subgoal for x abort s a b aa ba xa xb xc (*TODO Proof*)
      using distinct_mset_dom[of get_clauses_l S]
      apply (auto dest!: multi_member_split[of _ :: nat a] multi_member_split[of xa] dest: mset_subset_eq_insertD
        simp add: pure_literal_count_occs_l_inv_def intro!: ext split: if_splits)
      apply (meson in_multiset_minus_notin_snd mset_subset_eqD union_single_eq_member)
      apply (metis (no_types, lifting) add_mset_remove_trivial distinct_mem_diff_mset distinct_mset_filter in_diffD in_multiset_minus_notin_snd)
      by (metis (no_types, lifting) diff_add_mset_swap insert_DiffM insert_noteq_member mset_subset_eqD)
    subgoal
      by (auto dest!: multi_member_split)
    subgoal for x abort s a b aa ba xa
      by (auto simp: pure_literal_count_occs_l_inv_def minus_remove1_mset_if)
    subgoal
      by (auto dest!: multi_member_split)
    subgoal
      by (auto simp: pure_literal_count_occs_l_inv_def get_init_clss_l_def ran_m_def
        eq_commute[of _ the _] all_conj_distrib conj_disj_distribR ex_disj_distrib
        dest!: multi_member_split)
    done
qed


definition pure_literal_deletion_l2 :: _  'v twl_st_l  'v twl_st_l nres where
  pure_literal_deletion_l2 occs S = do {
    ASSERT (pure_literal_deletion_pre S);
    let As = (set_mset ` set_mset (mset `# get_init_clss_l S));
    xs  pure_literal_deletion_candidates S;
    (S, xs)  WHILETpure_literal_deletion_l_inv S xs(λ(S, xs). xs  {#})
      (λ(S, xs). do {
        L  SPEC (λL. L ∈# xs);
        let A = (if occs (Pos L)  ¬occs (Neg L) then Pos L else Neg L);
        if ¬occs (-A)  undefined_lit (get_trail_l S) A
        then do {S  propagate_pure_bt_l A S;
          RETURN (S, remove1_mset L xs)}
        else RETURN (S, remove1_mset L xs)
      })
    (S, xs);
  RETURN S
  }

lemma pure_literal_deletion_l2_pure_literal_deletion_l:
  assumes (L. occs L = (L  (set_mset ` set_mset (mset `# get_init_clss_l S))))
  shows pure_literal_deletion_l2 occs S  Id (pure_literal_deletion_l S)
proof -
  have 1: a=b  (a,b)  Id and 2: c  Φ  RETURN c Id (RES Φ) and
    3: e = f  e Id f for a b c Φ e f
    by auto
  show ?thesis
    unfolding pure_literal_deletion_l2_def pure_literal_deletion_l_def
    apply (refine_vcg)
    apply (rule 1)
    subgoal by auto
    subgoal by auto
    subgoal by auto
    subgoal by auto
    apply (rule 2)
    subgoal by auto
    subgoal using assms by (auto split: if_splits)
    apply (rule 3)
    subgoal by auto
    subgoal by auto
    subgoal by auto
    subgoal by auto
    done
qed


definition pure_literal_elimination_round_pre where
  pure_literal_elimination_round_pre S 
  (T. (S, T)  twl_st_l None  twl_struct_invs T  twl_list_invs S 
    cdclW_restart_mset.cdclW_learned_clauses_entailed_by_init ((stateW_of T)) 
    set (get_all_mark_of_propagated (get_trail_l S))  {0} 
    clauses_to_update_l S = {#} 
    count_decided (get_trail_l S) = 0)

definition pure_literal_elimination_round where
  pure_literal_elimination_round S = do {
    ASSERT (pure_literal_elimination_round_pre S);
    S  simplify_clauses_with_units_st S;
    if get_conflict_l S = None
    then do {
     (abort, occs)  pure_literal_count_occs_l S;
      if ¬abort then pure_literal_deletion_l2 occs S
      else RETURN S}
    else RETURN S
}

lemma pure_literal_elimination_round:
  assumes pure_literal_elimination_round_pre S
  shows pure_literal_elimination_round S  SPEC (λT. cdcl_twl_inprocessing_l** S T)
proof -
  have pure_literal_deletion_pre: pure_literal_elimination_round_pre S 
    cdcl_twl_inprocessing_l** S x  simplify_clauses_with_unit_st_inv S {} x 
    get_conflict_l x = None 
    pure_literal_deletion_pre x for x
    unfolding pure_literal_deletion_pre_def pure_literal_elimination_round_pre_def apply normalize_goal+
    apply (frule rtranclp_cdcl_twl_inprocessing_l_twl_st_l; assumption?)
    apply (rule_tac x=V in exI)
    apply (simp add: rtranclp_cdcl_twl_inprocessing_l_clauses_to_update_l
      rtranclp_cdcl_twl_inprocessing_l_count_decided)
    done
  have pure_literal_count_occs_l_pre: pure_literal_elimination_round_pre S 
    cdcl_twl_inprocessing_l** S x  simplify_clauses_with_unit_st_inv S {} x 
    get_conflict_l x = None 
    pure_literal_count_occs_l_pre x for x
    unfolding pure_literal_count_occs_l_pre_def pure_literal_elimination_round_pre_def apply normalize_goal+
    apply (frule rtranclp_cdcl_twl_inprocessing_l_twl_st_l; assumption?)
    apply (rule_tac x=V in exI)
    apply (auto simp add: rtranclp_cdcl_twl_inprocessing_l_clauses_to_update_l simplify_clauses_with_unit_st_inv_def
      rtranclp_cdcl_twl_inprocessing_l_count_decided)
    done

  obtain T where
    T:  (S, T)  twl_st_l None  twl_struct_invs T  twl_list_invs S 
    cdclW_restart_mset.cdclW_learned_clauses_entailed_by_init ((stateW_of T)) 
    set (get_all_mark_of_propagated (get_trail_l S))  {0} 
    clauses_to_update_l S = {#} 
    count_decided (get_trail_l S) = 0
    using assms unfolding pure_literal_elimination_round_pre_def by blast
  show ?thesis
    unfolding pure_literal_elimination_round_def
    apply (refine_vcg
      simplify_clauses_with_units_st_spec[THEN order_trans, of _ T])
    subgoal using assms by auto
    subgoal using T by blast
    subgoal using T by blast
    subgoal using T by blast
    subgoal using T by blast
    subgoal using T by blast
    subgoal using T by blast
    subgoal using T by blast
    apply (subst Down_id_eq)
    apply (refine_vcg pure_literal_count_occs_l_spec
      pure_literal_deletion_l2_pure_literal_deletion_l[THEN order_trans]
      pure_literal_deletion_l_spec[THEN order_trans])
    subgoal by (rule pure_literal_count_occs_l_pre)
    subgoal by auto
    subgoal
      apply (subst Down_id_eq)
      apply (rule pure_literal_deletion_l_spec[THEN order_trans])
      apply (simp_all add: pure_literal_deletion_pre)
      by (smt (verit, best) Collect_mono cdcl_twl_inprocessing_l.intros(5) mono_rtranclp rtranclp_trans)
    subgoal by auto
    subgoal by auto
    done
qed


definition pure_literal_elimination_l_pre where
  pure_literal_elimination_l_pre S 
  (T. (S, T)  twl_st_l None  twl_struct_invs T  twl_list_invs S 
    cdclW_restart_mset.cdclW_learned_clauses_entailed_by_init ((stateW_of T)) 
    set (get_all_mark_of_propagated (get_trail_l S))  {0} 
    clauses_to_update_l S = {#} 
    count_decided (get_trail_l S) = 0)

definition pure_literal_elimination_l_inv where
  pure_literal_elimination_l_inv S max_rounds = (λ(T,n,_). n  max_rounds  cdcl_twl_inprocessing_l** S T)

definition pure_literal_elimination_l :: _ where
  pure_literal_elimination_l S = do {
     ASSERT (pure_literal_elimination_l_pre S);
     max_rounds  RES (UNIV :: nat set);
    (S, _, _)  WHILETpure_literal_elimination_l_inv S max_rounds(λ(S, m, abort). m < max_rounds  ¬abort)
     (λ(S, m, abort). do {
         S  pure_literal_elimination_round S;
         abort  RES (UNIV :: bool set);
         RETURN (S, m+1, abort)
       })
    (S, 0, False);
   RETURN S
  }


lemma pure_literal_elimination_l:
  assumes pure_literal_elimination_l_pre S
  shows pure_literal_elimination_l S  SPEC (λT. cdcl_twl_inprocessing_l** S T)
proof -
  have [refine0]: max_rounds  UNIV  wf (measure (λ(S,n,m). max_rounds - n)) for max_rounds
    by auto
  have H: pure_literal_elimination_l_pre S 
    x  UNIV 
    pure_literal_elimination_l_inv S x s 
    case s of (S, m, abort)  m < x  ¬ abort 
      s = (a, b)  b = (aa, ba)  pure_literal_elimination_round_pre a for x s a aa ba b S
    apply hypsubst
    unfolding pure_literal_elimination_round_pre_def pure_literal_elimination_l_inv_def
        pure_literal_elimination_l_pre_def prod.simps
    apply normalize_goal+
    apply (frule rtranclp_cdcl_twl_inprocessing_l_twl_st_l; assumption?)
    apply (rule_tac x=V in exI)
    apply (auto simp add: rtranclp_cdcl_twl_inprocessing_l_clauses_to_update_l simplify_clauses_with_unit_st_inv_def
      rtranclp_cdcl_twl_inprocessing_l_count_decided
      dest: rtranclp_cdcl_twl_inprocessing_l_get_all_mark_of_propagated)
    done
  show ?thesis
    unfolding pure_literal_elimination_l_def
    apply (refine_vcg pure_literal_elimination_round)
    subgoal using assms by auto
    apply assumption
    subgoal unfolding pure_literal_elimination_l_inv_def by auto
    subgoal by (rule H)
    subgoal by (auto simp: pure_literal_elimination_l_inv_def)
    subgoal by auto
    subgoal by (auto simp: pure_literal_elimination_l_inv_def)
    done
qed


definition pure_literal_elimination_l_needed :: 'v twl_st_l  bool nres where
  pure_literal_elimination_l_needed S = SPEC (λ_. True)

definition pure_literal_eliminate_l :: _ where
  pure_literal_eliminate_l S = do {
     ASSERT (pure_literal_elimination_l_pre S);
     b  pure_literal_elimination_l_needed S;
     if b then pure_literal_elimination_l S else RETURN S
  }


lemma pure_literal_eliminate_l:
  assumes pure_literal_elimination_l_pre S
  shows pure_literal_eliminate_l S  SPEC (λT. cdcl_twl_inprocessing_l** S T)
  unfolding pure_literal_eliminate_l_def pure_literal_elimination_l_needed_def
  by (refine_vcg pure_literal_elimination_l)
   (use assms in auto)

end