Datasets:

Modalities:
Text
Languages:
English
Libraries:
Datasets
License:
File size: 1,164 Bytes
4365a98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
From mathcomp Require Import all_ssreflect.

Set Implicit Arguments.
Unset Strict Implicit.
Unset Printing Implicit Defensive.

Inductive tree := Node { children : seq tree }.

Inductive ptree (T : Type) := singleton of T | branch of list (ptree T).

(* has *)
Fixpoint tree_has (T : Type) (p : pred T) (t : ptree T) : bool :=
  match t with
    | singleton x => p x
    | branch ts => has (tree_has p) ts
  end.

(* all *)
Fixpoint tree_all (T : Type) (p : pred T) (t : ptree T) : bool :=
  match t with
    | singleton x => p x
    | branch ts => all (tree_all p) ts
  end.

(* map *)
Fixpoint traverse_id (t : tree) : tree :=
  Node (map traverse_id (children t)).

(* foldr *)
Fixpoint tree_foldr (T R : Type) (f : T -> R -> R) (z : R) (t : ptree T) : R :=
  match t with
    | singleton x => f x z
    | branch ts => foldr (fun t z' => tree_foldr f z' t) z ts
  end.

(* foldl *)
Fixpoint tree_foldl (T R : Type) (f : R -> T -> R) (z : R) (t : ptree T) : R :=
  match t with
    | singleton x => f z x
    | branch ts => foldl (tree_foldl f) z ts
  end.

(* all2 *)
Fixpoint eq_tree (x y : tree) {struct x} : bool :=
  all2 eq_tree (children x) (children y).