From 023c11470e32744a43b7e3c7c248f3c47ebdc687 Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Mon, 21 Nov 2016 17:06:19 +0100 Subject: Use gadt for function catalog --- sheet.ml | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) (limited to 'sheet.ml') diff --git a/sheet.ml b/sheet.ml index 773c784..b604c03 100755 --- a/sheet.ml +++ b/sheet.ml @@ -21,6 +21,9 @@ module Raw = struct sink : Cell.Set.t; (** All the cell which references this one *) } + (** The sheet is a map which always contains evaluated values. When a cell is + updated, all the cell which references this value are also updated. + *) and t = content Map.t (** An empty cell which does contains nothing *) @@ -30,39 +33,37 @@ module Raw = struct sink = Cell.Set.empty; } - let create = Map.empty - let get_value (id: cell) t = begin + let get_value id t = begin try (Map.find id t).value with Not_found -> ScTypes.Result ScTypes.Undefined end - let get_expr (id: cell) t = begin + let get_expr id t = begin try (Map.find id t).expr - with Not_found -> Expression.load @@ UTF8.empty + with Not_found -> empty_cell.expr end - (** Extract a value from a reference. *) - let get_ref (from:cell) (t:t) : ScTypes.refs -> ScTypes.types = begin + (** Extract a value from a reference. + This function is given to the evaluator for getting the values from a reference. + *) + let get_ref from t ref = begin let extract_values = begin function | ScTypes.Result v -> v | ScTypes.Error e -> raise e end in - begin function - | ScTypes.Cell c -> - let coord = Cell.to_pair c in - if coord = from then raise Cycle; extract_values (get_value coord t) - | ScTypes.Range _ as r -> - ScTypes.Refs.collect r - |> List.map (fun x -> if x = from then raise Cycle; extract_values (get_value x t)) - |> (fun x -> ScTypes.List x) - end + ScTypes.Refs.collect ref + |> ScTypes.Refs.map (fun coord -> extract_values (get_value coord t)) + end - (** Update the value for the given cell *) + (** Update the value for the given cell. + Evaluate the new expression and compare it with the previous value. + @return the map updated if the result differ. + *) let update cell content t = begin let new_val = Expression.eval content.expr (get_ref cell t) in if not (ScTypes.Result.(=) new_val content.value) then @@ -73,8 +74,11 @@ module Raw = struct end (** Parse all the successors from [init] and call [f] for each of them. + + As long as [f] return [Some _], the cell successors will also be updated. + [f] is called only once for each successor. - @return all the successors collected + @return all the successors collected, and the map updated. *) let successors (f:(cell -> content -> t -> t option)) (init:content) (state:Cell.Set.t * t) = begin let rec fold cell (succ, t) = begin @@ -82,7 +86,8 @@ module Raw = struct (* The element has already been parsed, do not cycle *) (succ, t) else ( - (* Map.find cannot raise Not_found here *) + (* Map.find cannot raise Not_found here : we look for a successor from a registered cell. + *) let content = Map.find cell t in match f cell content t with | None -> (succ, t) @@ -176,7 +181,6 @@ module Raw = struct add_element id f t end - let paste id shift content t = begin let expr = Expression.shift shift content.expr in let f cell t = -- cgit v1.2.3