ocsigen / tyxml

Build valid HTML and SVG documents
https://ocsigen.org/tyxml/
Other
163 stars 57 forks source link

Typing & composability issue #175

Open Armael opened 7 years ago

Armael commented 7 years ago
open Tyxml

let page title content = [%html {|                                                  
  <html>                                                                            
    <head>                                                                          
     <title>|} title {|</title>                                                     
     <link rel=stylesheet href="/style.css" />                                      
    </head>                                                                         
    <body>|} content {|</body>                                                      
    </html>                                                                         
 |}]

let a_or_button contents =
  if true then
    Html.a contents
  else
    Html.button contents

let _ = page [%html "foo"] [Html.a []]
let _ = page [%html "foo"] [Html.button []]
let _ = page [%html "foo"] [a_or_button []]

Surprisingly, the last line fails to typecheck, with the following error: https://paste.isomorphis.me/Tgo

shepard8 commented 6 years ago

I compiled the above without error using eliomc -c with TyXML 4.1.0.

Can you consider adding additional steps on how to reproduce?

Armael commented 6 years ago

If test.ml is a file containing the snippet above, then I can reproduce the error by running

ocamlfind ocamlc -package tyxml.ppx -c test.ml

with both Tyxml 4.1.0 and Tyxml master.

Could you give more details of the commands you ran in order to compile with eliomc? I don't know how to reproduce what you mention.

shepard8 commented 6 years ago

Indeed with that command I have the same issue, I don't know why on the system I was two days ago I could compile test.ml using eliomc -c test.ml, but the system I am currently on can't compile it that way either.

I don't know if it is useful, but the following gives the same error and sheds some light on what is going on:

open Tyxml

let f contents = Html.body [
  Html.a contents;
  Html.button contents
]

Html.a expects flow5_without_interactive while Html.button expects phrasing_without_interactive. I believe that flow5_without_interactive should be a supertype of phrasing_without_interactive, but is not compatible in the current implementation.

shepard8 commented 6 years ago

Well after some more investigation, it appears that my previous comment was inaccurate. The type flow5_without_interactive is a supertype of phrasing_without_interactive. However it seems that the OCaml compiler needs a hint to be able to detect it.

open Tyxml

let g contents = Html.body [
  Html.a (contents :> Html_types.flow5_without_interactive Html.elt list);
  Html.button contents;
]