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.
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_______________________________________________________________ *)
12 (* $Id: nCic.ml 9058 2008-10-13 17:42:30Z tassi $ *)
14 let rec repeat f l = match f l with None -> l | Some l -> repeat f l;;
16 type ('s,'g) tree = Node of 's * ('g * ('s,'g) tree) list | Nil
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
23 type ('a,'s,'g) position = Loc of ('s,'g) tree_ctx * ('s,'g) tree
25 let start (t : ('s,'g) tree) : ('a,'s,'g) position = Loc (Top, t);;
28 | Loc (_,(Nil|Node (_,[]))) -> None
29 | Loc (ctx, Node (s,(x,t)::tl)) -> Some (Loc (Ctx (ctx,s,[],x,tl),t))
33 | Loc (_,(Nil|Node (_,[]))) -> None
34 | Loc (ctx, Node (s,(_::_ as l))) ->
38 Some (Loc (Ctx (ctx,s,tl,x,[]),t))
43 | Loc (Ctx(ctx,s,l,x,r),t) -> Some (Loc (ctx,Node (s,(List.rev l)@[x,t]@r)))
47 | Loc (Top,_) | Loc (Ctx(_,_,_,_,[]),_) -> None
48 | Loc (Ctx(ctx,s,l,x,(rx,rt)::tl),t)-> Some (Loc(Ctx(ctx,s,(x,t)::l,rx,tl),rt))
52 | Loc (Top,_) | Loc (Ctx(_,_,[],_,_),_) -> None
53 | Loc (Ctx(ctx,s,(lx,lt)::tl,x,r),t)->Some (Loc(Ctx(ctx,s,tl,lx,(x,t)::r),lt))
57 | Loc(Top,_) -> assert false
58 | Loc(Ctx(_,s,_,x,_),_) -> s,x
61 let is_top = function Loc(Top,_) -> true | _ -> false;;
63 let set s x = function
64 | Loc(Top,_) -> assert false
65 | Loc(Ctx(c,_,l,_,r),t) -> Loc(Ctx(c,s,l,x,r),t)
69 match repeat up t with
74 let eject (Loc (_,t)) = t ;;
76 let inject t (Loc (c,_)) = Loc(c,t) ;;
78 let dump ppg pps pos fmt =
79 let module Pp = GraphvizPp.Dot in
81 let skip = Node (Obj.magic (),[]) in
82 let rec aux_t = function
84 Pp.node ("node"^string_of_int !c) ~attrs:["shape","point"] fmt
87 Pp.node ("node"^string_of_int j) ~attrs:["shape","record";
88 "label",String.concat "|"
90 (fun (x,_) i -> "<f"^string_of_int i^"> " ^ ppg x)
92 pps s ("node"^string_of_int j) fmt;
93 ignore(HExtlib.list_mapi
94 (fun (_,t) i -> if t != skip then begin
98 ("node"^string_of_int j^":f"^string_of_int i)
99 ("node"^string_of_int k) fmt;
106 | Ctx(ctx,s,l,x,r) ->
107 let t = Node(s,(List.rev l)@[x,skip]@r) in
113 ("node"^string_of_int !c)
114 ("node"^string_of_int cur^":f"^string_of_int( List.length l))
118 let Loc (ctx, t) = pos in
119 let l = match ctx with Top -> assert false | Ctx (_,_,l,_,_)->List.rev l in
121 Pp.node ("node"^string_of_int !c)
122 ~attrs:["style","filled";"fillcolor","red";"color","red"] fmt;
123 Pp.edge ("node"^string_of_int !c) ("node"^string_of_int !c) fmt;
127 ("node"^string_of_int !c^":f"^string_of_int (List.length l))
128 ("node"^string_of_int cur)