Created
January 30, 2025 03:56
-
-
Save ClarkeRemy/14e60c136936fb321cea168e8dc35401 to your computer and use it in GitHub Desktop.
Tagless Final chat discord
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait Num { | |
type Repr; | |
fn num(n : i32)->Self::Repr; | |
fn plus(l : Self::Repr, r : Self::Repr)->Self::Repr; | |
} | |
struct ReprInt; | |
impl Num for ReprInt { | |
type Repr = i32; | |
fn num(n : i32)->Self::Repr { n } | |
fn plus(l : Self::Repr, r : Self::Repr)->Self::Repr { l + r }; | |
} | |
struct ReprString; | |
impl Num for ReprInt { | |
type Repr = String; | |
fn num(n : i32)->Self::Repr { format!{"{}"n} } | |
fn plus(l : Self::Repr, r : Self::Repr)->Self::Repr { format!{"({l} + {r})"} }; | |
} | |
fn one_plus_one<N : NUM>() -> N { N::plus(N::num(1), N::num(1)) } | |
fn g() { | |
let s = one_plus_one::<ReprString>(); | |
let i = one_plus_one::<ReprInt>(); | |
} | |
trait Num { | |
type Repr; | |
fn num(&self, n : i32)->Self::Repr; | |
fn plus(&self, l : Self::Repr, r : Self::Repr)->Self::Repr; | |
} | |
struct ReprInt; | |
impl Num for ReprInt { | |
type Repr = i32; | |
fn num(&self, n : i32)->Self::Repr { n } | |
fn plus(&self, l : Self::Repr, r : Self::Repr)->Self::Repr { l + r }; | |
} | |
struct ReprString; | |
impl Num for ReprInt { | |
type Repr = String; | |
fn num(&self, n : i32)->Self::Repr { format!{"{}"n} } | |
fn plus(&self, l : Self::Repr, r : Self::Repr)->Self::Repr { format!{"({l} + {r})"} }; | |
} | |
fn one_plus_x(n : &dyn Num, x : i32) -> N { | |
n.plus(n.num(1), n.num(x)) | |
} | |
fn g() { | |
let s = one_plus_one(&ReprString); | |
let i = one_plus_one(&ReprInt); | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
datatype expr = PLUS of expr * expr | |
| NUM of int | |
fun eval_expr (NUM n) = n | |
| eval_expr (PLUS (l,r)) = eval_expr l + eval_expr r | |
fun pretty_expr (NUM n) = Int.toString n | |
| pretty_expr (PLUS (l,r)) = "( " ^ pretty_expr l ^ " + " ^ pretty_expr r ^ " )" | |
val one_plus_one = PLUS (NUM 1, NUM 1) | |
fun num (repr, _ ) n = repr n | |
fun plus (_ , plus_repr) l r = plus_repr l r | |
fun one_plus_one d = plus d (num d 1) (num d 1) | |
val repr_string = ( Int.toString, | |
fn l => fn r => "( " ^ l ^ " + " ^ r ^ " )" | |
) | |
val repr_num = ( fn x => x , | |
fn l => fn r => l + r | |
) | |
val repr_ast = (NUM, fn l => fn r => PLUS(l,r) ) | |
val _ = print (one_plus_one repr_string) | |
val _ = (print o Int.toString) (one_plus_one repr_num) | |
val _ = one_plus_one repr_ast | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment