(* Conjunctions and disjunctions of literals as ternary threshold units. *) From Stdlib Require Import List Bool Arith ZArith Lia. Import ListNotations. From TH Require Import TwoHead Net. (* a literal: a signal and its polarity *) Definition lit := (sref * bool)%type. Definition lw (l : lit) : sref * Z := (fst l, if snd l then 1%Z else (-1)%Z). Definition npos (ls : list lit) : nat := length (filter snd ls). Definition nneg (ls : list lit) : nat := length (filter (fun l => negb (snd l)) ls). (* H(sum of the positive literals - sum of the negated ones - #positive) and H(sum of the positive literals - sum of the negated ones + #negated - 1) *) Definition sAND (ls : list lit) : su := SU (- Z.of_nat (npos ls)) (map lw ls). Definition sOR (ls : list lit) : su := SU (Z.of_nat (nneg ls) - 1) (map lw ls). (* a two-way choice placed at units r0, r0 + 1, r0 + 2 of member j of block b *) Definition mux3 (b j r0 : nat) (sel a c : sref) : list su := [sAND [(sel, true); (a, true)]; sAND [(sel, false); (c, true)]; sOR [(SRef b j r0, true); (SRef b j (S r0), true)]]. Definition litv (V : sref -> bool) (l : lit) : bool := Bool.eqb (V (fst l)) (snd l). Lemma swsum_lits : forall ls V, swsum (map lw ls) V = (Z.of_nat (length (filter (litv V) ls)) - Z.of_nat (nneg ls))%Z. Proof. induction ls as [|[x p] t IH]; intro V; [reflexivity|]. unfold nneg in *. cbn [map swsum lw fst snd filter] in *. rewrite IH. change (litv V (x, p)) with (Bool.eqb (V x) p). destruct p, (V x); cbn [Bool.eqb negb length b2z]; rewrite ?Nat2Z.inj_succ; lia. Qed. Lemma filter_length_le : forall (A : Type) (f : A -> bool) l, length (filter f l) <= length l. Proof. intros A f l. induction l as [|x t IH]; cbn [filter length]; [lia|]. destruct (f x); cbn [length]; lia. Qed. Lemma filter_all : forall (A : Type) (f : A -> bool) l, length (filter f l) = length l <-> forallb f l = true. Proof. intros A f l. induction l as [|x t IH]; cbn [filter length forallb]; [tauto|]. pose proof (filter_length_le A f t). destruct (f x); cbn [length andb]. - rewrite <- IH. lia. - split; [lia | discriminate]. Qed. Lemma filter_some : forall (A : Type) (f : A -> bool) l, 1 <= length (filter f l) <-> existsb f l = true. Proof. intros A f l. induction l as [|x t IH]; cbn [filter length existsb]; [split; [lia|discriminate]|]. destruct (f x); cbn [length orb]; [split; [reflexivity|lia]|exact IH]. Qed. Lemma npos_nneg : forall ls, npos ls + nneg ls = length ls. Proof. induction ls as [|[x p] t IH]; [reflexivity|]. unfold npos, nneg in *. cbn [filter length snd negb]. destruct p; cbn [length negb]; lia. Qed. Lemma sAND_fire : forall ls V, sfire' (sAND ls) V = forallb (litv V) ls. Proof. intros ls V. unfold sfire', sAND. cbn [sb sin]. rewrite swsum_lits. pose proof (npos_nneg ls). pose proof (filter_length_le _ (litv V) ls). destruct (forallb (litv V) ls) eqn:E. - apply filter_all in E. apply Z.leb_le. lia. - apply Z.leb_gt. destruct (Nat.eq_dec (length (filter (litv V) ls)) (length ls)) as [H1|H1]. + apply filter_all in H1. congruence. + lia. Qed. Lemma sOR_fire : forall ls V, sfire' (sOR ls) V = existsb (litv V) ls. Proof. intros ls V. unfold sfire', sOR. cbn [sb sin]. rewrite swsum_lits. destruct (existsb (litv V) ls) eqn:E. - apply filter_some in E. apply Z.leb_le. lia. - apply Z.leb_gt. destruct (Nat.le_gt_cases 1 (length (filter (litv V) ls))) as [H1|H1]. + apply filter_some in H1. congruence. + lia. Qed. Lemma litv_pos : forall V x, litv V (x, true) = V x. Proof. intros V x. unfold litv. cbn [fst snd]. destruct (V x); reflexivity. Qed. Lemma litv_neg : forall V x, litv V (x, false) = negb (V x). Proof. intros V x. unfold litv. cbn [fst snd]. destruct (V x); reflexivity. Qed.