It's easier if you split up Eq

#include "share/atspre_define.hats" 
#include "share/atspre_staload.hats" 


datatype Expr(a:t@ype)  = 
  | I(int) of int 
  | B(bool) of bool 
  | Add(int) of (Expr int, Expr int) 
  | Mul(int) of (Expr int, Expr int) 
  | Eqi(bool) of (Expr int, Expr int) 
  | Eqb(bool) of (Expr bool, Expr bool) 

extern fun{a:t@ype} equals(t1:a, t2:a): bool 
implement equals<int>(t1,t2) = g0int_eq(t1,t2) 
implement equals<bool>(t1,t2) = eq_bool0_bool0(t1,t2) 

extern fun {a:t@ype} eval(x:Expr a): a

implement eval<int>(x) =
  case- x of 
  | I i => i 
  | Add (t1, t2) => eval(t1) + eval(t2) 
  | Mul (t1, t2) => eval(t1) * eval(t2) 

implement eval<bool>(x) =
  case- x of 
  | B b => b 
  | Eqi (t1, t2) => equals<int>(eval(t1), eval(t2))
  | Eqb (t1, t2) => equals<bool>(eval(t1), eval(t2))

implement main0() = let 
  val term1 = Eqi(I(5), Add(I(1), I(4))) 
  val term2 = Mul(I(2), I(4)) 
  val term3 = Eqb(B(true), B(false))
  val res1 = eval(term1) 
  val res2 = eval(term2) 
  val res3 = eval(term3)
in 
  println!("res1=", res1, " res2=", res2, " and res3=", res3) 
end 


On Monday, October 15, 2018 at 8:51:15 PM UTC-5, Chris Double wrote:
>
> I have some GADT code that fails at the C compilation stage. It's a 
> variant of: 
>
> https://gist.github.com/doublec/a3cc8f3431cabe9a319c8e7ba27e7890 
>
> But the Eq constructor of Expr uses the type index instead of being 
> 'int' so equality can be done against booleans and ints. The full code 
> is: 
>
> ----------------8<--------------- 
> #include "share/atspre_define.hats" 
> #include "share/atspre_staload.hats" 
>
> datatype Expr(a:t@ype)  = 
>   | I(int) of int 
>   | B(bool) of bool 
>   | Add(int) of (Expr int, Expr int) 
>   | Mul(int) of (Expr int, Expr int) 
>   | Eq(bool) of (Expr a, Expr a) 
>
> extern fun{a:t@ype} equals(t1:a, t2:a): bool 
> implement equals<int>(t1,t2) = g0int_eq(t1,t2) 
> implement equals<bool>(t1,t2) = eq_bool0_bool0(t1,t2) 
>
> fun{a:t@ype} eval(x:Expr a): a = 
>   case+ x of 
>   | I i => i 
>   | B b => b 
>   | Add (t1, t2) => eval(t1) + eval(t2) 
>   | Mul (t1, t2) => eval(t1) * eval(t2) 
>   | Eq (t1, t2) => equals(t1, t2) 
>
> implement main0() = let 
>   val term1 = Eq(I(5), Add(I(1), I(4))) 
>   val term2 = Mul(I(2), I(4)) 
>   val res1 = eval(term1) 
>   val res2 = eval(term2) 
> in 
>   println!("res1=", res1, " and res2=", res2) 
> end 
> ----------------8<--------------- 
>
> This fails to compile at the C stage with: 
>
> In file included from arith3_dats.c:15:0: 
> arith3_dats.c: In function ‘eval_2__2__1’: 
> arith3_dats.c:1098:24: error: ‘PMVtmpltcstmat’ undeclared (first use 
> in this function) 
>  ATSINSmove(tmpret2__1, 
> PMVtmpltcstmat[0](equals<S2EVar(5554)>)(tmp9__1, tmp10__1)) ; 
>                         ^ 
> ATS2-Postiats-0.3.11//ccomp/runtime/pats_ccomp_instrset.h:276:37: 
> note: in definition of macro ‘ATSINSmove’ 
>  #define ATSINSmove(tmp, val) (tmp = val) 
> .... 
>
> Any thoughts on what I'm doing wrong? 
>
> -- 
> https://bluishcoder.co.nz 
>

-- 
You received this message because you are subscribed to the Google Groups 
"ats-lang-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ats-lang-users+unsubscr...@googlegroups.com.
To post to this group, send email to ats-lang-users@googlegroups.com.
Visit this group at https://groups.google.com/group/ats-lang-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ats-lang-users/de1a3962-e1b1-42d5-82ef-0d1980b68ef4%40googlegroups.com.

Reply via email to