Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
freem
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Openai/693343d7-a38c-8012-a67c-11cbed4c0fd9
(section)
Add languages
Page
Discussion
English
Read
Edit
Edit source
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
Edit source
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Below is a self-contained Coq-like file (uses standard Coq libraries). This is explicit: Tree A is the free-monad-like data type with Leaf and Node where Node carries an operator label (e.g., Merge, SimNode) and a list of children. We define map, eta (ret), join (mu), bind, then prove the monad laws. === You can paste into a .v and adapt imports (I write it compactly β proofs are constructive and sketched where appropriate). <syntaxhighlight lang="coq">('' Obatala_Tree.v β Coq sketch for Obatala = Tree monad '') Require Import List. Import ListNotations. Require Import Coq.Program.Basics. Require Import Coq.Program.Tactics. Require Import Coq.Init.Nat. Require Import Coq.Logic.FunctionalExtensionality. Set Implicit Arguments. Set Asymmetric Patterns. Generalizable All Variables. ('' --- operator labels for internal nodes (customize as needed) --- '') Inductive Op : Type := | MergeOp | SimOp | CustomOp (label : nat). ('' placeholder '') ('' --- Free-tree type --- '') Inductive Tree (A : Type) : Type := | Leaf : A -> Tree A | Node : Op -> list (Tree A) -> Tree A. Arguments Leaf {A} _. Arguments Node {A} _ _. ('' --- Functor map --- '') Fixpoint fmap {A B} (f : A -> B) (t : Tree A) : Tree B := match t with | Leaf a => Leaf (f a) | Node op children => Node op (map (fmap f) children) end. ('' --- Monad unit (eta) --- '') Definition ret {A} (a : A) : Tree A := Leaf a. ('' --- flatten / join --- '') Fixpoint join {A} (t : Tree (Tree A)) : Tree A := match t with | Leaf inner => inner | Node op children => ('' graft: flatten any child Leaf/Node by mapping join recursively '') let children' := map (fun tt => join tt) children in Node op children' end. ('' --- bind in terms of fmap + join --- '') Definition bind {A B} (t : Tree A) (f : A -> Tree B) : Tree B := join (fmap f t). ('' --- Functor & Monad laws proofs (sketches) --- '') ('' Functor identity: fmap id = id '') Theorem fmap_id {A} (t : Tree A) : fmap (fun x => x) t = t. Proof. induction t. * reflexivity. '' simpl. rewrite IHt. ('' IHt is on list, need map lemma *) induction l. + simpl. reflexivity. + simpl. rewrite IHt0. rewrite IHl. reflexivity. Qed. ('' Functor composition: fmap (g β f) = fmap g β fmap f '') Theorem fmap_compose {A B C} (f : A -> B) (g : B -> C) (t : Tree A) : fmap (fun x => g (f x)) t = fmap g (fmap f t). Proof. induction t; simpl. * reflexivity. * f_equal. induction l; simpl. + reflexivity. + rewrite IHt0. rewrite IHl. reflexivity. Qed. ('' Left identity: bind ret = id '') Theorem bind_ret_left {A B} (t : Tree A) : bind t (@ret A) = t. Proof. unfold bind, ret. induction t; simpl. * reflexivity. * f_equal. induction l; simpl. + reflexivity. + rewrite IHt0. rewrite IHl. reflexivity. Qed. ('' Right identity: bind ret = id for Kleisli? need: bind (ret a) f = f a '') Theorem bind_ret_right {A B} (a : A) (f : A -> Tree B) : bind (ret a) f = f a. Proof. simpl. reflexivity. Qed. ('' Associativity: bind (bind t f) g = bind t (fun x => bind (f x) g) '') Theorem bind_assoc {A B C} (t : Tree A) (f : A -> Tree B) (g : B -> Tree C) : bind (bind t f) g = bind t (fun x => bind (f x) g). Proof. unfold bind. ('' reduces to join (fmap g (join (fmap f t))) = join (fmap (fun x => join (fmap g (f x))) t) '') ('' A full formal proof requires lemmas about join/fmap interactions. '') Admitted. ('' Note: bind_assoc typically proven by induction on t and list lemmas. '') ('' --- Eilenberg-Moore algebra example: interpret tree into a carrier type --- '') Section EM_example. Variable A : Type. Variable carrier : Type. Variable interpret_leaf : A -> carrier. Variable interpret_node : Op -> list carrier -> carrier. Fixpoint algebra (t : Tree A) : carrier := match t with | Leaf x => interpret_leaf x | Node op ch => interpret_node op (map algebra ch) end. ('' algebra : Tree A -> carrier is an EM-algebra: algebra β ret = interpret_leaf etc. '') End EM_example. ('' End of Coq sketch '') </syntaxhighlight> Notes & next steps for Coq: * bind_assoc is left Admitted β it's straightforward but requires standard list-induction lemmas; I can finish it fully if you want the runnable file. * The above defines a concrete O := Tree with ret, join, bind. Tree is the canonical free-monad-like Obatala instance. * Extend Op with richer labels for simulation, merge semantics, and provide interpret_node as the EM-algebra. If you want, Iβll finish the bind_assoc proof and produce a fully checked .v file.
Summary:
Please note that all contributions to freem are considered to be released under the Creative Commons Attribution-ShareAlike 4.0 (see
Freem:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)