Discussion:
[Ur] UR/Web SVG (ffi) question
Jonas Mellin
2018-04-16 08:58:15 UTC
Permalink
Dear all, I am a novice with respect to UR/Web and I want to use it together with SVG. I found the prototype ffi for SVG at https://github.com/karsar/urweb-examples/tree/master/SVGTest
after reading through the material, doing all the tutorials and generally digging around internet.

Anyway, I took the example from https://github.com/karsar/urweb-examples/tree/master/SVGTest
and tried to modify it; I fail repeatedly, so now I turn to this email list.

Instead of the SVGTest.ur that has all the functionality in main, I tried to break it out to a separate function "proc". I took the idea from http://www.impredicative.com/ur/demo/sql.html
to generate an html page into a variable and then add that as return statement in the main function as seen below.

===== Modified SVGTest.ur with respect to https://github.com/karsar/urweb-examples/tree/master/SVGTest

open SVG

con intPair = int*int

fun fst (x: intPair) = x.1
fun snd (x: intPair) = x.2
fun circleDraw(x: intPair): xsvg =
<xml>
<circle cx={show ((fst x)-60)} cy={show ((snd x)-100)} r="10" stroke="red" fill="blue"/>
</xml>


fun proc (x: intPair) =
x <- source x;
return
<xml>
<body onmousemove={fn ev => set x (ev.ScreenX,ev.SceenY)}>
<div style="width:800; heigth:800">
<dyn signal={x<-signal x;
return
<xml>
<svg width="800" height="800">
{circleDraw x}
</svg>
</xml>}/>
</div>
</body>
</xml>



fun main(): transaction page =
xml <- proc (0,0);
return
<xml>
<head>
Banzai
</head>
<body>
{xml}
</body>
</xml>




I have tried different options (e.g., that the proc should return transaction page, xbody, xsvg) and I fail in different ways. (I tried to add other functionality initially, but I removed it to try to get to the bottom of the problem).

The compiler tells me:



urweb SVGTest
/home/a/SVGTest2/SVGTest.ur:33:4: (to 44:2) Error in final record unification
Can't unify record constructors
Have: [Dyn = (), MakeForm = (), Body = ()]
Need: [Html = ()]
/home/a/SVGTest2/SVGTest.ur:33:4: (to 44:2) Stuck unifying these records after canceling matching pieces:
Have: [Dyn = (), MakeForm = (), Body = ()]
Need: [Html = ()]
/home/a/SVGTest2/SVGTest.ur:17:7: (to 17:64) Error in final record unification
Can't unify record constructors
Have:
[Data = data_attr, Onload = transaction {},
Onresize = transaction {}, Onunload = transaction {},
Onhashchange = transaction {},
[<<< REMOVED to reduce size of message>>>]
Value 1:
{ScreenX : int, ScreenY : int, ClientX : int, ClientY : int,
CtrlKey : bool, ShiftKey : bool, AltKey : bool, MetaKey : bool,
Button : mouseButton} -> transaction {}
Value 2:
$(([ScreenX = int, SceenY = int]) ++ <UNIF:U141::{Type}>) ->
transaction {}
Can't unify record constructors
Have:
[ScreenX = int, ScreenY = int, ClientX = int, ClientY = int,
CtrlKey = bool, ShiftKey = bool, AltKey = bool, MetaKey = bool,
Button = mouseButton]
Need: <UNIF:U141::{Type}> ++ [ScreenX = int, SceenY = int]

Compilation exited abnormally with code 1 at Wed Apr 11 08:45:06

Thanks in advance, Jonas Mellin, Senior Lecturer, University of Skövde
Adam Chlipala
2018-04-16 11:28:59 UTC
Permalink
I have not read all of your code, but I did spot the problem behind the
first compiler error message, which I think all web developers would
agree is problematic. (I.e., it isn't just a question of a fussy Ur/Web
type system.)
Post by Jonas Mellin
fun proc (x: intPair) =
    x <- source x;
    return
                             <xml>
                               <body onmousemove={fn ev => set x
(ev.ScreenX,ev.SceenY)}>
<!-- ... -->
                               </xml>
fun main(): transaction page =
    xml <- proc (0,0);
    return
                             <xml>
                               <head>
                                 Banzai
                               </head>
                               <body>
                                 {xml}
                               </body>
                             </xml>
[...]
/home/a/SVGTest2/SVGTest.ur:33:4: (to 44:2) Error in final record unification
Can't unify record constructors
Have:  [Dyn = (), MakeForm = (), Body = ()]
Need:  [Html = ()]
Notice that your [proc] code includes a <body> tag, but then you nest it
within another <body> tag!
Jonas Mellin
2018-04-17 09:53:32 UTC
Permalink
Thx.

One trial, that I believe should work, is:
open SVG

(*con intPair = int*int*)

fun fst (x: int*int) = x.1
fun snd (x: int*int) = x.2
fun circleDraw(x: int*int): xsvg =
<xml>
<circle cx={show ((fst x)-60)} cy={show ((snd x)-100)} r="10" stroke="red" fill="blue"/>
</xml>


fun proc (x: int*int): transaction page =
x <- source x;
return
<xml>
<body onmousemove={fn ev => set x (ev.ScreenX,ev.SceenY)}>

<div style="width:800; heigth:800">
<dyn signal={x<-signal x;
return
<xml>
<svg width="800" height="800">
{circleDraw x}
</svg>
</xml>}/>
</div>
</body>
</xml>



fun main(): transaction page =
let
val a = (0,0)
in
xml <- proc a;
return
<xml>
<head>
Banzai
</head>
{xml}
</xml>
end


But I get:
Can't unify record constructors
Have:
[ScreenX = int, ScreenY = int, ClientX = int, ClientY = int,
CtrlKey = bool, ShiftKey = bool, AltKey = bool, MetaKey = bool,
Button = mouseButton]
Need: <UNIF:U140::{Type}> ++ [ScreenX = int, SceenY = int]

On " <body onmousemove={fn ev => set x (ev.ScreenX,ev.SceenY)}>"



From: Ur [mailto:ur-***@impredicative.com] On Behalf Of Adam Chlipala
Sent: den 16 april 2018 13:29
To: ***@impredicative.com
Subject: Re: [Ur] UR/Web SVG (ffi) question

I have not read all of your code, but I did spot the problem behind the first compiler error message, which I think all web developers would agree is problematic. (I.e., it isn't just a question of a fussy Ur/Web type system.)

On 04/16/2018 04:58 AM, Jonas Mellin wrote:
[Jonas Mellin] <<< collapsed answer >>>

Notice that your [proc] code includes a <body> tag, but then you nest it within another <body> tag!
Adam Chlipala
2018-04-17 11:19:40 UTC
Permalink
For future questions, I think it would be good for you to explain what
you understand from error messages, highlighting what additional
understanding you need to diagnose the error.  Here again the problem is
a simple one that would be recognized as a bug in any programming language.

Notice the difference in spellings of field names, between the "Have"
and "Need" parts of the error message.
...
  <body onmousemove={fn ev => set x (ev.ScreenX,ev.SceenY)}>
...
*But I get:***
Can't unify record constructors
[ScreenX = int, ScreenY = int, ClientX = int, ClientY = int,
  CtrlKey = bool, ShiftKey = bool, AltKey = bool, MetaKey = bool,
  Button = mouseButton]
Need: <UNIF:U140::{Type}> ++ [ScreenX = int, SceenY = int]
Jonas Mellin
2018-04-17 12:00:00 UTC
Permalink
My bad, sorry. It took me a short while to see the misspelling. Got it working. I will be more careful about that in the future.

I move on to trying multiple objects and then adding "foreignObject" tag:
val foreignObject : svgTag([X = string, Y = string, Width = string, Height = string] ++ typicalAttrs)
an addition to the SVG.urs (cf. original at https://github.com/karsar/urweb-examples/blob/master/SVGTest/SVG.urs) , this require somehow to mix xsvg with xbody. Anything in particular that has to be taken into account when mixing this?

/Jonas Mellin


From: Ur [mailto:ur-***@impredicative.com] On Behalf Of Adam Chlipala
Sent: den 17 april 2018 13:20
To: ***@impredicative.com
Subject: Re: [Ur] UR/Web SVG (ffi) question

For future questions, I think it would be good for you to explain what you understand from error messages, highlighting what additional understanding you need to diagnose the error. Here again the problem is a simple one that would be recognized as a bug in any programming language.

Notice the difference in spellings of field names, between the "Have" and "Need" parts of the error message.

On 04/17/2018 05:53 AM, Jonas Mellin wrote:
...
<body onmousemove={fn ev => set x (ev.ScreenX,ev.SceenY)}>
...


But I get:
Can't unify record constructors
Have:
[ScreenX = int, ScreenY = int, ClientX = int, ClientY = int,
CtrlKey = bool, ShiftKey = bool, AltKey = bool, MetaKey = bool,
Button = mouseButton]
Need: <UNIF:U140::{Type}> ++ [ScreenX = int, SceenY = int]
Adam Chlipala
2018-04-17 13:44:19 UTC
Permalink
I think it should all "just work," with your new tag added in an FFI
.urs file like that!
Post by Jonas Mellin
My bad, sorry. It took me a short while to see the misspelling. Got it
working. I will be more careful about that in the future.
val foreignObject : svgTag([X = string, Y = string, Width = string,
Height = string] ++ typicalAttrs)
an addition to the SVG.urs (cf. original at
https://github.com/karsar/urweb-examples/blob/master/SVGTest/SVG.urs)
, this require somehow to mix xsvg with xbody. Anything in particular
that has to be taken into account when mixing this?
/Jonas Mellin
Chlipala
*Sent:* den 17 april 2018 13:20
*Subject:* Re: [Ur] UR/Web SVG (ffi) question
For future questions, I think it would be good for you to explain what
you understand from error messages, highlighting what additional
understanding you need to diagnose the error. Here again the problem
is a simple one that would be recognized as a bug in any programming
language.
Notice the difference in spellings of field names, between the "Have"
and "Need" parts of the error message.
...
  <body onmousemove={fn ev => set x (ev.ScreenX,ev.SceenY)}>
...
*But I get:*
Can't unify record constructors
[ScreenX = int, ScreenY = int, ClientX = int, ClientY = int,
  CtrlKey = bool, ShiftKey = bool, AltKey = bool, MetaKey = bool,
  Button = mouseButton]
Need: <UNIF:U140::{Type}> ++ [ScreenX = int, SceenY = int]
_______________________________________________
Ur mailing list
http://www.impredicative.com/cgi-bin/mailman/listinfo/ur
Loading...