]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_tactics/zipTree.ml
- dual_rg: renamed to complete_rg [as suggested in the ToCL documentation]
[helm.git] / helm / software / components / ng_tactics / zipTree.ml
1 (*
2     ||M||  This file is part of HELM, an Hypertextual, Electronic        
3     ||A||  Library of Mathematics, developed at the Computer Science     
4     ||T||  Department, University of Bologna, Italy.                     
5     ||I||                                                                
6     ||T||  HELM is free software; you can redistribute it and/or         
7     ||A||  modify it under the terms of the GNU General Public License   
8     \   /  version 2 or (at your option) any later version.      
9      \ /   This software is distributed as is, NO WARRANTY.     
10       V_______________________________________________________________ *)
11
12 (* $Id: nCic.ml 9058 2008-10-13 17:42:30Z tassi $ *)
13
14 let rec repeat f l = match f l with None -> l | Some l -> repeat f l;;
15
16 type ('s,'g) tree = Node of 's * ('g * ('s,'g) tree) list | Nil
17
18 type ('s,'g) tree_ctx = 
19   | Ctx of ('s,'g) tree_ctx * 's * 
20              ('g * ('s,'g) tree) list * 'g *  ('g * ('s,'g) tree) list 
21   | Top
22
23 type ('a,'s,'g) position = Loc of ('s,'g) tree_ctx * ('s,'g) tree
24
25 let start (t : ('s,'g) tree) : ('a,'s,'g) position = Loc (Top, t);;
26
27 let down = function 
28   | Loc (_,(Nil|Node (_,[]))) -> None
29   | Loc (ctx, Node (s,(x,t)::tl)) -> Some (Loc (Ctx (ctx,s,[],x,tl),t))
30 ;;
31
32 let up = function
33   | Loc (Top,_) -> None
34   | Loc (Ctx(ctx,s,l,x,r),t) -> Some (Loc (ctx,Node (s,(List.rev l)@[x,t]@r)))
35 ;;
36
37 let right = function
38   | Loc (Top,_) | Loc (Ctx(_,_,_,_,[]),_) -> None
39   | Loc (Ctx(ctx,s,l,x,(rx,rt)::tl),t)-> Some (Loc(Ctx(ctx,s,(x,t)::l,rx,tl),rt))
40 ;;
41
42 let left = function
43   | Loc (Top,_) | Loc (Ctx(_,_,[],_,_),_) -> None
44   | Loc (Ctx(ctx,s,(lx,lt)::tl,x,r),t)->Some (Loc(Ctx(ctx,s,tl,lx,(x,t)::r),lt))
45 ;;
46
47 let get = function
48   | Loc(Top,_) -> assert false
49   | Loc(Ctx(_,s,_,x,_),_) -> s,x
50 ;;
51
52 let is_top = function Loc(Top,_) -> true | _ -> false;;
53
54 let set s x = function
55   | Loc(Top,_) -> assert false
56   | Loc(Ctx(c,_,l,_,r),t) -> Loc(Ctx(c,s,l,x,r),t)
57 ;;
58
59 let root t =
60   match repeat up t with
61   | Loc(Top,t) -> t
62   | _ -> assert false
63 ;;
64
65 let eject (Loc (_,t)) = t ;;
66
67 let inject t (Loc (c,_)) = Loc(c,t) ;;
68
69 let dump ppg pps pos fmt =
70   let module Pp = GraphvizPp.Dot in
71   let c = ref 0 in
72   let skip = Node (Obj.magic (),[]) in
73   let rec aux_t = function 
74     | Nil -> 
75         Pp.node ("node"^string_of_int !c) ~attrs:["shape","point"] fmt
76     | Node (s,l) ->
77         let j = !c in
78         Pp.node ("node"^string_of_int j) ~attrs:["shape","record"; 
79           "label",String.concat "|"
80             (HExtlib.list_mapi 
81               (fun (x,_) i -> "<f"^string_of_int i^"> " ^ ppg x)
82             l)] fmt;
83         pps s ("node"^string_of_int j) fmt;
84         ignore(HExtlib.list_mapi 
85           (fun (_,t) i -> if t != skip then begin 
86              incr c;
87              let k = !c in
88              Pp.edge 
89                ("node"^string_of_int j^":f"^string_of_int i)
90                ("node"^string_of_int k) fmt;
91                aux_t t end)
92           l)
93   in
94   let rec aux pos = 
95     match pos with
96     | Top -> ()
97     | Ctx(ctx,s,l,x,r) ->
98         let t = Node(s,l@[x,skip]@r) in
99         let cur = !c in
100         aux_t t;
101         incr c;
102         if ctx <> Top then
103           Pp.edge 
104             ("node"^string_of_int !c)
105             ("node"^string_of_int cur^":f"^string_of_int( List.length l))
106             fmt; 
107         aux ctx
108   in
109   let Loc (ctx, t) = pos in
110   let l = match ctx with Top -> assert false | Ctx (_,_,l,_,_)->l in
111   let cur = !c in
112   Pp.node ("node"^string_of_int !c) 
113     ~attrs:["style","filled";"fillcolor","red";"color","red"] fmt;
114   aux_t t;
115   incr c;
116   Pp.edge 
117     ("node"^string_of_int !c^":f"^string_of_int (List.length l))
118     ("node"^string_of_int cur)
119     fmt; 
120   aux ctx
121 ;;
122