varieties.semirings

Require
  theory.rings categories.varieties.
Require Import
  abstract_algebra universal_algebra ua_homomorphisms workaround_tactics.

Inductive op := plus | mult | zero | one.

Definition sig: Signature := single_sorted_signature
  (λ o, match o with zero | one => O | plus | mult => 2%nat end).

Section laws.
  Global Instance: RingPlus (Term0 sig nat tt) :=
    λ x, App sig _ _ _ (App sig _ _ _ (Op sig _ plus) x).
  Global Instance: RingMult (Term0 sig nat tt) :=
    λ x, App sig _ _ _ (App sig _ _ _ (Op sig _ mult) x).
  Global Instance: RingZero (Term0 sig nat tt) := Op sig _ zero.
  Global Instance: RingOne (Term0 sig nat tt) := Op sig _ one.

  Local Notation x := (Var sig _ 0%nat tt).
  Local Notation y := (Var sig _ 1%nat tt).
  Local Notation z := (Var sig _ 2%nat tt).

  Import notations.

  Inductive Laws: EqEntailment sig Prop :=
    |e_plus_assoc: Laws (x + (y + z) === (x + y) + z)
    |e_plus_comm: Laws (x + y === y + x)
    |e_plus_0_l: Laws (0 + x === x)
    |e_mult_assoc: Laws (x * (y * z) === (x * y) * z)
    |e_mult_comm: Laws (x * y === y * x)
    |e_mult_1_l: Laws (1 * x === x)
    |e_mult_0_l: Laws (0 * x === 0)
    |e_distr_l: Laws (x * (y + z) === x * y + x * z)
    |e_distr_r: Laws ((x + y) * z === x * z + y * z).
End laws.

Definition theory: EquationalTheory := Build_EquationalTheory sig Laws.


Section from_instance.
  Context A `{SemiRing A}.

  Instance implementation: AlgebraOps sig (λ _, A) := λ o,
    match o with plus => (+) | mult => (.*.) | zero => 0: A | one => 1:A end.

  Global Instance: Algebra sig _.

  Lemma laws en (l: Laws en) vars: eval_stmt sig vars en.

  Global Instance variety: InVariety theory (λ _, A).

  Definition Object := varieties.Object theory.
  Definition object: Object := varieties.object theory (λ _, A).
End from_instance.


Section ops_from_alg_to_sr.
  Context `{AlgebraOps theory A}.
  Global Instance: RingPlus (A tt) := algebra_op plus.
  Global Instance: RingMult (A tt) := algebra_op mult.
  Global Instance: RingZero (A tt) := algebra_op zero.
  Global Instance: RingOne (A tt) := algebra_op one.
End ops_from_alg_to_sr.

Lemma mor_from_sr_to_alg `{InVariety theory A} `{InVariety theory B}
  (f: u, A u B u) `{!SemiRing_Morphism (f tt)}: HomoMorphism sig A B f.

Instance decode_variety_and_ops `{v: InVariety theory A}: SemiRing (A tt).

Lemma decode_morphism_and_ops
  `{InVariety theory x} `{InVariety theory y} `{!HomoMorphism theory x y f}:
    SemiRing_Morphism (f tt).