Coder Social home page Coder Social logo

ad-hoc-poly's People

Contributors

coord-e avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

iomeone

ad-hoc-poly's Issues

Support default implementations

Problem

an example in mlx1:

 class<T> Num {
   zero :: T,
   sub :: T -> T -> T,
   neg :: T -> T = sub zero
 } in
 impl Num for Int {
   zero = 0,
   sub x y = x - y
 } in
 neg 1

direct translation (mlx1-like syntax):

 type Num = ΛT. constraint numD (T, T -> T -> T, T -> T) in
 overload (∀'a. Num 'a) in
 let zero = numD#0 in
 let sub = numD#1 in
 let neg = numD#2 in
 instance (Num Int) = (0, sub_int, sub zero) in
 neg 1

It is possible (by adding the instance itself to the context in translation of rhs of instance) to type and translate this example to OCaml, however, the resulting translation cannot be compiled because a value recursion happens in the dictionary for Num Int.

A possible translation is shown below. Here, numD_37 is occurred in the rhs of its definition.

let zero = (fun numD_22 -> fst3 (numD_22)) in
let sub = (fun numD_29 -> snd3 (numD_29)) in
let neg = (fun numD_36 -> thd3 (numD_36)) in
let numD_37 = (0, sub_int, sub (numD_37) (zero (numD_37))) in
neg (numD_37) (1)

Possible solution

Using lazy evaluation:

let zero = (fun numD_22 -> fst3 (numD_22)) in
let sub = (fun numD_29 -> snd3 (numD_29)) in
let neg = (fun numD_36 -> Lazy.force (thd3 (numD_36))) in
let rec numD_37 = (0, sub_int, lazy (sub (numD_37) (zero (numD_37)))) in
print_int (neg (numD_37) (1))

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.