]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/ocaml/cic_notation/cicNotationPres.ml
ocaml 3.09 transition
[helm.git] / helm / ocaml / cic_notation / cicNotationPres.ml
index ebe5bf6ebb000f28e81885e0d9722e6cdc07d906..cc3a204a4e3874ba3ee55ccb3477932ae66447bc 100644 (file)
  * http://helm.cs.unibo.it/
  *)
 
-type mathml_markup = boxml_markup Mpresentation.mpres
+module Ast = CicNotationPt
+module Mpres = Mpresentation
+
+type mathml_markup = boxml_markup Mpres.mpres
 and boxml_markup = mathml_markup Box.box
 
 type markup = mathml_markup
 
 let atop_attributes = [None, "linethickness", "0pt"]
-let binder_attributes = [None, "mathcolor", "blue"]
-let indent_attributes = [None, "indent", "1em"]
-
-let mpres_arrow = Mpresentation.Mo (binder_attributes, "->")
-  (* TODO unicode symbol "to" *)
-let mpres_implicit = Mpresentation.Mtext ([], "?")
-
-let map_tex use_unicode texcmd =
-  let default_map_tex = Printf.sprintf "\\%s " in
-  if use_unicode then
-    try
-      Utf8Macro.expand texcmd
-    with Utf8Macro.Macro_not_found _ -> default_map_tex texcmd
-  else
-    default_map_tex texcmd
 
-let resolve_binder use_unicode = function
-  | `Lambda -> map_tex use_unicode "lambda"
-  | `Pi -> map_tex use_unicode "Pi"
-  | `Forall -> map_tex use_unicode "forall"
-  | `Exists -> map_tex use_unicode "exists"
+let to_unicode = Utf8Macro.unicode_of_tex
 
 let rec make_attributes l1 = function
   | [] -> []
-  | None :: tl -> make_attributes (List.tl l1) tl
-  | Some s :: tl ->
-      let p,n = List.hd l1 in
-      (p,n,s) :: make_attributes (List.tl l1) tl
+  | hd :: tl ->
+      (match hd with
+      | None -> make_attributes (List.tl l1) tl
+      | Some s ->
+          let p,n = List.hd l1 in
+          (p,n,s) :: make_attributes (List.tl l1) tl)
 
 let box_of_mpres =
   function
-    Mpresentation.Mobject (_, box) -> box
+  | Mpresentation.Mobject (attrs, box) ->
+      assert (attrs = []);
+      box
   | mpres -> Box.Object ([], mpres)
 
 let mpres_of_box =
   function
-    Box.Object (_, mpres) -> mpres
+  | Box.Object (attrs, mpres) ->
+      assert (attrs = []);
+      mpres
   | box -> Mpresentation.Mobject ([], box)
 
-let genuine_math =
+let rec genuine_math =
   function
-  | Mpresentation.Mobject _ -> false
+  | Mpresentation.Mobject ([], obj) -> not (genuine_box obj)
+  | _ -> true
+and genuine_box =
+  function
+  | Box.Object ([], mpres) -> not (genuine_math mpres)
   | _ -> true
 
-let box_of mathonly kind attrs children =
-  if mathonly then Mpresentation.Mrow (attrs, children)
-  else
-    match kind with
-    | CicNotationPt.H when List.for_all genuine_math children ->
-        Mpresentation.Mrow (attrs, children)
-    | CicNotationPt.H ->
-        mpres_of_box (Box.H (attrs, List.map box_of_mpres children))
-    | CicNotationPt.V ->
-        mpres_of_box (Box.V (attrs, List.map box_of_mpres children))
-    | CicNotationPt.HV ->
-        mpres_of_box (Box.HV (attrs, List.map box_of_mpres children))
-    | CicNotationPt.HOV ->
-        mpres_of_box (Box.HOV (attrs, List.map box_of_mpres children))
-
-let open_paren   = Mpresentation.Mo ([], "(")
-let closed_paren = Mpresentation.Mo ([], ")")
-let open_box_paren = Box.Text ([], "(")
-let closed_box_paren = Box.Text ([], ")")
+let rec eligible_math =
+  function
+  | Mpresentation.Mobject ([], Box.Object ([], mpres)) -> eligible_math mpres
+  | Mpresentation.Mobject ([], _) -> false
+  | _ -> true
+
+let rec promote_to_math =
+  function
+  | Mpresentation.Mobject ([], Box.Object ([], mpres)) -> promote_to_math mpres
+  | math -> math
+
+let small_skip =
+  Mpresentation.Mspace (RenderingAttrs.small_skip_attributes `MathML)
+
+let rec add_mpres_attributes new_attr = function
+  | Mpresentation.Mobject (attr, box) ->
+      Mpresentation.Mobject (attr, add_box_attributes new_attr box)
+  | mpres ->
+      Mpresentation.set_attr (new_attr @ Mpresentation.get_attr mpres) mpres
+and add_box_attributes new_attr = function
+  | Box.Object (attr, mpres) ->
+      Box.Object (attr, add_mpres_attributes new_attr mpres)
+  | box -> Box.set_attr (new_attr @ Box.get_attr box) box
+
+let box_of mathonly spec attrs children =
+  match children with
+    | [t] -> add_mpres_attributes attrs t
+    | _ ->
+       let kind, spacing, indent = spec in
+       let dress children =
+         if spacing then
+           CicNotationUtil.dress small_skip children
+         else
+           children
+       in
+         if mathonly then Mpresentation.Mrow (attrs, dress children)
+         else
+            let attrs' =
+             (if spacing then RenderingAttrs.spacing_attributes `BoxML else [])
+              @ (if indent then RenderingAttrs.indent_attributes `BoxML else [])
+              @ attrs
+            in
+              match kind with
+                | Ast.H ->
+                    if List.for_all eligible_math children then
+                      Mpresentation.Mrow (attrs',
+                        dress (List.map promote_to_math children))
+                    else
+                      mpres_of_box (Box.H (attrs',
+                        List.map box_of_mpres children))
+(*                 | Ast.H when List.for_all genuine_math children ->
+                    Mpresentation.Mrow (attrs', dress children) *)
+               | Ast.V ->
+                   mpres_of_box (Box.V (attrs',
+                      List.map box_of_mpres children))
+               | Ast.HV ->
+                   mpres_of_box (Box.HV (attrs',
+                      List.map box_of_mpres children))
+               | Ast.HOV ->
+                   mpres_of_box (Box.HOV (attrs',
+                      List.map box_of_mpres children))
+
+let open_paren        = Mpresentation.Mo ([], "(")
+let closed_paren      = Mpresentation.Mo ([], ")")
+let open_brace        = Mpresentation.Mo ([], "{")
+let closed_brace      = Mpresentation.Mo ([], "}")
+let hidden_substs     = Mpresentation.Mtext ([], "{...}")
+let open_box_paren    = Box.Text ([], "(")
+let closed_box_paren  = Box.Text ([], ")")
+let semicolon         = Mpresentation.Mo ([], ";")
+let toggle_action children =
+  Mpresentation.Maction ([None, "actiontype", "toggle"], children)
 
 type child_pos = [ `Left | `Right | `Inner ]
 
+let pp_assoc =
+  function
+  | Gramext.LeftA -> "LeftA"
+  | Gramext.RightA -> "RightA"
+  | Gramext.NonA -> "NonA"
+
+let is_atomic t =
+  let rec aux_mpres = function
+    | Mpres.Mi _
+    | Mpres.Mo _
+    | Mpres.Mn _
+    | Mpres.Ms _
+    | Mpres.Mtext _
+    | Mpres.Mspace _ -> true
+    | Mpres.Mobject (_, box) -> aux_box box
+    | Mpres.Maction (_, [mpres])
+    | Mpres.Mrow (_, [mpres]) -> aux_mpres mpres
+    | _ -> false
+  and aux_box = function
+    | Box.Space _
+    | Box.Ink _
+    | Box.Text _ -> true
+    | Box.Object (_, mpres) -> aux_mpres mpres
+    | Box.H (_, [box])
+    | Box.V (_, [box])
+    | Box.HV (_, [box])
+    | Box.HOV (_, [box])
+    | Box.Action (_, [box]) -> aux_box box
+    | _ -> false
+  in
+  aux_mpres t
+
 let add_parens child_prec child_assoc child_pos curr_prec t =
-  if child_prec < curr_prec
-    || (child_prec = curr_prec &&
-        child_assoc = Gramext.LeftA &&
-        child_pos <> `Left)
-    || (child_prec = curr_prec &&
-        child_assoc = Gramext.RightA &&
-        child_pos <> `Right)
+  if is_atomic t then t
+  else if child_prec >= 0
+    && (child_prec < curr_prec
+      || (child_prec = curr_prec &&
+          child_assoc = Gramext.LeftA &&
+          child_pos = `Right)
+      || (child_prec = curr_prec &&
+          child_assoc = Gramext.RightA &&
+          child_pos = `Left))
   then  (* parens should be added *)
+(*     (prerr_endline "adding parens";
+    prerr_endline (Printf.sprintf "child_prec = %d\nchild_assoc = %s\nchild_pos = %s\ncurr_prec= %d"
+      child_prec (pp_assoc child_assoc) (CicNotationPp.pp_pos
+      child_pos) curr_prec); *)
     match t with
     | Mpresentation.Mobject (_, box) ->
         mpres_of_box (Box.H ([], [ open_box_paren; box; closed_box_paren ]))
@@ -111,107 +196,232 @@ let add_parens child_prec child_assoc child_pos curr_prec t =
   else
     t
 
-let render ids_to_uris =
-  let module A = CicNotationPt in
+let render ids_to_uris =
+  let module A = Ast in
   let module P = Mpresentation in
   let use_unicode = true in
-  let lookup_uri = function
-    | None -> None
-    | Some id -> (try Some (Hashtbl.find ids_to_uris id) with Not_found -> None)
+  let lookup_uri id =
+    (try
+      let uri = Hashtbl.find ids_to_uris id in
+      Some (UriManager.string_of_uri uri)
+    with Not_found -> None)
   in
-  let make_href xref =
-    let uri = lookup_uri xref in
-    make_attributes [Some "helm","xref"; Some "xlink","href"] [xref;uri]
-  in
-  let make_xref xref = make_attributes [Some "helm","xref"] [xref] in
-  let make_box = function
-    | P.Mobject (attrs, box) ->
-        assert (attrs = []);
-        box
-    | m -> Box.Object ([], m)
+  let make_href xmlattrs xref =
+    let xref_uris =
+      List.fold_right
+        (fun xref uris ->
+          match lookup_uri xref with
+          | None -> uris
+          | Some uri -> uri :: uris)
+        !xref []
+    in
+    let xmlattrs_uris, xmlattrs =
+      let xref_attrs, other_attrs =
+        List.partition
+          (function Some "xlink", "href", _ -> true | _ -> false)
+          xmlattrs
+      in
+      List.map (fun (_, _, uri) -> uri) xref_attrs,
+      other_attrs
+    in
+    let uris =
+      match xmlattrs_uris @ xref_uris with
+      | [] -> None
+      | uris ->
+          Some (String.concat " "
+            (HExtlib.list_uniq (List.sort String.compare uris)))
+    in
+    let xrefs =
+      match !xref with [] -> None | xrefs -> Some (String.concat " " xrefs)
+    in
+    xref := [];
+    xmlattrs
+    @ make_attributes [Some "helm", "xref"; Some "xlink", "href"]
+        [xrefs; uris]
   in
-  let make_hv xref children =
-    let attrs = indent_attributes @ make_href xref in
-    P.Mobject ([], Box.HV (indent_attributes, List.map make_box children))
+  let make_xref xref =
+    let xrefs =
+      match !xref with [] -> None | xrefs -> Some (String.concat " " xrefs)
+    in
+    xref := [];
+    make_attributes [Some "helm","xref"] [xrefs]
   in
-  let rec invoke mathonly xref prec assoc t =
-    fst (aux mathonly xref prec assoc t)
   (* when mathonly is true no boxes should be generated, only mrows *)
-  and aux mathonly xref prec assoc t =
-    let return t = t, (prec, assoc) in
+  (* "xref" is  *)
+  let rec aux xmlattrs mathonly xref pos prec t =
     match t with
-    | A.AttributedTerm (`Loc _, t) -> return (invoke mathonly xref prec assoc t)
-    | A.AttributedTerm (`Level (prec, assoc), t) ->
-        return (invoke mathonly xref prec assoc t)
-    | A.AttributedTerm (`IdRef xref, t) ->
-        return (invoke mathonly (Some xref) prec assoc t)
-
-    | A.Ident (literal, None) -> return (P.Mi (make_href xref, literal))
-    | A.Num (literal, _)      -> return (P.Mn (make_href xref, literal))
-    | A.Symbol (literal, _)   -> return (P.Mo (make_href xref, literal))
-    | A.Uri (literal, None)   -> return (P.Mi (make_href xref, literal))
-
-    (* default pretty printing shant' be implemented here *)
-(*     | A.Appl terms ->
-        let children = aux_children mathonly xref prec assoc terms in
-        make_hv xref children
-    | A.Binder (`Pi, (A.Ident ("_", None), ty_opt), body)
-    | A.Binder (`Forall, (A.Ident ("_", None), ty_opt), body) ->
-        let ty' =
-          match ty_opt with
-          | None -> mpres_implicit
-          | Some ty -> invoke mathonly None prec assoc ty
+    | A.AttributedTerm _ ->
+        aux_attributes xmlattrs mathonly xref pos prec t
+    | A.Num (literal, _) ->
+        let attrs =
+          (RenderingAttrs.number_attributes `MathML)
+          @ make_href xmlattrs xref
         in
-        let body' = invoke mathonly None prec assoc body in
-        return (make_hv xref [ty'; make_h None [mpres_arrow; body']]) *)
-
-    | A.Literal l -> aux_literal xref prec assoc l
-    | A.Layout l -> aux_layout mathonly xref prec assoc l
+        Mpres.Mn (attrs, literal)
+    | A.Symbol (literal, _) ->
+        let attrs =
+          (RenderingAttrs.symbol_attributes `MathML)
+          @ make_href xmlattrs xref
+        in
+        Mpres.Mo (attrs, to_unicode literal)
+    | A.Ident (literal, subst)
+    | A.Uri (literal, subst) ->
+        let attrs =
+          (RenderingAttrs.ident_attributes `MathML)
+          @ make_href xmlattrs xref
+        in
+        let name = Mpres.Mi (attrs, to_unicode literal) in
+        (match subst with
+        | Some []
+        | None -> name
+        | Some substs ->
+            let substs' =
+              box_of mathonly (A.H, false, false) []
+                (open_brace
+                :: (CicNotationUtil.dress semicolon
+                    (List.map
+                      (fun (name, t) ->
+                        box_of mathonly (A.H, false, false) [] [
+                          Mpres.Mi ([], name);
+                          Mpres.Mo ([], to_unicode "\\def");
+                          aux [] mathonly xref pos prec t ])
+                      substs))
+                @ [ closed_brace ])
+            in
+            let substs_maction = toggle_action [ hidden_substs; substs' ] in
+            box_of mathonly (A.H, false, false) [] [ name; substs_maction ])
+    | A.Literal l -> aux_literal xmlattrs xref prec l
+    | A.UserInput -> Mpres.Mtext ([], "%")
+    | A.Layout l -> aux_layout mathonly xref pos prec l
     | A.Magic _
     | A.Variable _ -> assert false  (* should have been instantiated *)
-
-    | _ -> assert false
-
-  and aux_literal xref prec assoc l =
-    let return t = t, (prec, assoc) in
-    let attrs = make_href xref in
-    match l with
-    | `Symbol s
-    | `Keyword s -> return (P.Mo (attrs, s))
-    | `Number s  -> return (P.Mn (attrs, s))
-  and aux_layout mathonly xref prec assoc l =
-    let return t = t, (prec, assoc) in
+    | t ->
+        prerr_endline ("unexpected ast: " ^ CicNotationPp.pp_term t);
+        assert false
+  and aux_attributes xmlattrs mathonly xref pos prec t =
+    let reset = ref false in
+    let new_level = ref None in
+    let new_xref = ref [] in
+    let new_xmlattrs = ref [] in
+    let new_pos = ref pos in
+    let reinit = ref false in
+    let rec aux_attribute =
+      function
+      | A.AttributedTerm (attr, t) ->
+          (match attr with
+          | `Loc _
+          | `Raw _ -> ()
+          | `Level (-1, _) -> reset := true
+          | `Level (child_prec, child_assoc) ->
+              new_level := Some (child_prec, child_assoc)
+          | `IdRef xref -> new_xref := xref :: !new_xref
+          | `ChildPos pos -> new_pos := pos
+          | `XmlAttrs attrs -> new_xmlattrs := attrs @ !new_xmlattrs);
+          aux_attribute t
+      | t ->
+          (match !new_level with
+          | None -> aux !new_xmlattrs mathonly new_xref !new_pos prec t
+          | Some (child_prec, child_assoc) ->
+              let t' = 
+                aux !new_xmlattrs mathonly new_xref !new_pos child_prec t
+              in
+              if !reset then t'
+              else add_parens child_prec child_assoc !new_pos prec t')
+    in
+    aux_attribute t
+  and aux_literal xmlattrs xref prec l =
+    let attrs = make_href xmlattrs xref in
+    (match l with
+    | `Symbol s -> Mpres.Mo (attrs, to_unicode s)
+    | `Keyword s -> Mpres.Mo (attrs, to_unicode s)
+    | `Number s  -> Mpres.Mn (attrs, to_unicode s))
+  and aux_layout mathonly xref pos prec l =
     let attrs = make_xref xref in
-    let invoke' t = invoke true None prec assoc t in
+    let invoke' t = aux [] true (ref []) pos prec t in
+      (* use the one below to reset precedence and associativity *)
+    let invoke_reinit t = aux [] mathonly xref `Inner ~-1 t in
     match l with
-    | A.Sub (t1, t2) -> return (P.Msub (attrs, invoke' t1, invoke' t2))
-    | A.Sup (t1, t2) -> return (P.Msup (attrs, invoke' t1, invoke' t2))
-    | A.Below (t1, t2) -> return (P.Munder (attrs, invoke' t1, invoke' t2))
-    | A.Above (t1, t2) -> return (P.Mover (attrs, invoke' t1, invoke' t2))
+    | A.Sub (t1, t2) -> Mpres.Msub (attrs, invoke' t1, invoke_reinit t2)
+    | A.Sup (t1, t2) -> Mpres.Msup (attrs, invoke' t1, invoke_reinit t2)
+    | A.Below (t1, t2) -> Mpres.Munder (attrs, invoke' t1, invoke_reinit t2)
+    | A.Above (t1, t2) -> Mpres.Mover (attrs, invoke' t1, invoke_reinit t2)
     | A.Frac (t1, t2)
-    | A.Over (t1, t2) -> return (P.Mfrac (attrs, invoke' t1, invoke' t2))
+    | A.Over (t1, t2) ->
+        Mpres.Mfrac (attrs, invoke_reinit t1, invoke_reinit t2)
     | A.Atop (t1, t2) ->
-        return (P.Mfrac (atop_attributes @ attrs, invoke' t1, invoke' t2))
-    | A.Sqrt t -> return (P.Msqrt (attrs, invoke' t))
-    | A.Root (t1, t2) -> return (P.Mroot (attrs, invoke' t1, invoke' t2))
-    | A.Box (kind, terms) ->
-        let children = aux_children mathonly xref prec assoc terms in
-        return (box_of mathonly kind attrs children)
-  and aux_children mathonly xref prec assoc terms =
-    let rec aux_list first =
+        Mpres.Mfrac (atop_attributes @ attrs, invoke_reinit t1,
+          invoke_reinit t2)
+    | A.Sqrt t -> Mpres.Msqrt (attrs, invoke_reinit t)
+    | A.Root (t1, t2) ->
+        Mpres.Mroot (attrs, invoke_reinit t1, invoke_reinit t2)
+    | A.Box ((_, spacing, _) as kind, terms) ->
+        let children =
+          aux_children mathonly spacing xref pos prec
+            (CicNotationUtil.ungroup terms)
+        in
+        box_of mathonly kind attrs children
+    | A.Group terms ->
+       let children =
+          aux_children mathonly false xref pos prec
+            (CicNotationUtil.ungroup terms)
+        in
+        box_of mathonly (A.H, false, false) attrs children
+    | A.Break -> assert false (* TODO? *)
+  and aux_children mathonly spacing xref pos prec terms =
+    let find_clusters =
+      let rec aux_list first clusters acc =
+       function
+           [] when acc = [] -> List.rev clusters
+         | [] -> aux_list first (List.rev acc :: clusters) [] []
+         | (A.Layout A.Break) :: tl when acc = [] ->
+              aux_list first clusters [] tl
+         | (A.Layout A.Break) :: tl ->
+              aux_list first (List.rev acc :: clusters) [] tl
+         | [hd] ->
+(*               let pos' = 
+                if first then
+                  pos
+                else
+                  match pos with
+                      `None -> `Right
+                    | `Inner -> `Inner
+                    | `Right -> `Right
+                    | `Left -> `Inner
+              in *)
+               aux_list false clusters
+                  (aux [] mathonly xref pos prec hd :: acc) []
+         | hd :: tl ->
+(*               let pos' =
+                match pos, first with
+                    `None, true -> `Left
+                  | `None, false -> `Inner
+                  | `Left, true -> `Left
+                  | `Left, false -> `Inner
+                  | `Right, _ -> `Inner
+                  | `Inner, _ -> `Inner
+              in *)
+               aux_list false clusters
+                  (aux [] mathonly xref pos prec hd :: acc) tl
+      in
+       aux_list true [] []
+    in
+    let boxify_pres =
       function
-        [] -> []
-      | [t] ->
-          let t', (child_prec, child_assoc) = aux mathonly xref prec assoc t in
-            [add_parens child_prec child_assoc `Right prec t']
-      | t :: tl ->
-          let t', (child_prec, child_assoc) = aux mathonly xref prec assoc t in
-          let child_pos = if first then `Left else `Inner in
-            add_parens child_prec child_assoc child_pos prec t' :: aux_list false tl
+         [t] -> t
+       | tl -> box_of mathonly (A.H, spacing, false) [] tl
     in
-      match terms with
-        [t] -> [invoke mathonly xref prec assoc t]
-      | tl -> aux_list true tl
+      List.map boxify_pres (find_clusters terms)
   in
-  fst (aux false None 0 Gramext.NonA t)
+  aux [] false (ref []) `Inner ~-1
+
+let rec print_box (t: boxml_markup) =
+  Box.box2xml print_mpres t
+and print_mpres (t: mathml_markup) =
+  Mpresentation.print_mpres print_box t
+
+let print_xml = print_mpres
+
+(* let render_to_boxml id_to_uri t =
+  let xml_stream = print_box (box_of_mpres (render id_to_uri t)) in
+  Xml.add_xml_declaration xml_stream *)