Skip to content

Instantly share code, notes, and snippets.

@mizunashi-mana
Created January 12, 2020 10:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mizunashi-mana/4522da17c8ade08b3376b1596c06be72 to your computer and use it in GitHub Desktop.
Save mizunashi-mana/4522da17c8ade08b3376b1596c06be72 to your computer and use it in GitHub Desktop.
An idea of new programming language
module Main where
dict Integral a = Integral {
fromInteger: Integer -> a,
+: a -> a -> a,
-: a -> a -> a,
iszero: a -> Bool,
}
impl integralForInteger: Integral Integer
integralForInteger = Integral have
fromInteger x = x
x + y = x Main.+ y
x - y = x Main.- y
iszero = \
0 -> True
_ -> False
fromInteger: forall a. Integral a => Integer -> a
fromInteger %d = d.fromInteger
module Fib begin
fib: forall a. Integral a => a -> a
fib @a %d n = go n a0 a1
where
a0 = fromInteger %integralForInteger 0
a1 = fromInteger 1 -- implicit dictionary inference
-- `go` can have a signature `go: a -> a -> a -> a`
go m x y = \
| d.iszero m -> x
| else -> go (m - 1) y (x d.+ y)
end
main: IO ()
main = print $ Fib.fib 10
@mizunashi-mana
Copy link
Author

mizunashi-mana commented Jan 12, 2020

@TheMatten

  • In strict language (this is not Haskell with Strict extension), this may be not diverge.

    f :: ()
    f = undefined

    but this should be diverge

    main = pure f

    If we see type classes as implicit module, this may not be diverge similary

    dict A = A { a :: () }
    
    default defA :: A
    defA = A { a = undefined }
    
    f :: A => ()
    f %d = %d `seq` ()
    
    main = pure $ f %defA

    but this should be diverge

    f :: A => ()
    f %d = %d.a
    
    main = pure $ f %defA

    If we expect dictionary as data type, both should be diverge. But if we expect dictionary as module, first case should be not diverge. This discussion is also opened on GHC especially about unlifted data type. Now, GHC's class constraint is always lifted to avoid this discussion.

  • I agree with dictionary is normal data type internally. However, on language, dictionary instance form may lead undecidability for constraint solving method. So, Haskell require strict instance form; we can relax this constraint using some GHC extensions. Moreover, if constraint solving method is decidable for all instance form, performance of it is too bad. Therefore, we should restrict dictionary type and instance form similar to Haskell.

@TheMatten
Copy link

  • In strict language (this is not Haskell with Strict extension), this may be not diverge.
f :: ()
f = undefined

This diverges - when it's used somewhere.

but this should be diverge

main = pure f

Yes.

If we see type classes as implicit module, this may not be diverge similary

dict A = A { a :: () }

default defA :: A
defA = A { a = undefined }

f :: A => ()
f %d = %d `seq` ()

main = pure $ f %defA

This main should - in strict language, A { a = undefined } = A undefined = undefined, so in pure $ f %defA, you're basically passing undefined into f - just the fact that it's passed here as an argument is reason for it to panic. BTW, in strict language, seq should be no-op - you never get thunk to evaluate, argument of some type a is evaluated value of such type, not a computation that produces it.

When it comes to decidability in instance search, it isn't really that useful in non-total language - if it gets stuck, it will happen at compile-time, not runtime, plus we're already disabling it in Haskell in many cases.

@mizunashi-mana
Copy link
Author

mizunashi-mana commented Jan 13, 2020

@TheMatten

in strict language, A { a = undefined } = A undefined = undefined, so in pure $ f %defA, you're basically passing undefined into f - just the fact that it's passed here as an argument is reason for it to panic.

This is not the unique solution and I expect A { a = undefined } is a normal form as Main { f = undefined } is not diverge if A is a dictionary constructor (module constructor).

When it comes to decidability in instance search, it isn't really that useful in non-total language

In many cases, decidability in instance search is related compile performance directly. I agree with it isn't useful for language user. However, it is important for language provider because instance search may be bottleneck. And, it is a choice to restrict instance form for compile performance although it may make language power is weaker.

@TheMatten
Copy link

This is not the unique solution and I expect A { a = undefined } is a normal form as Main { f = undefined } is not diverge if A is a dictionary constructor (module constructor).

Alright, so you want modules/dictionaries to behave in a specific way. Yeah, then they aren't ordinary values after all.

In many cases, decidability in instance search is related compile performance directly. I agree with it isn't useful for language user. However, it is important for language provider because instance search may be bottleneck. And, it is a choice to restrict instance form for compile performance although it may make language power is weaker.

Part of GHC's manual about overlapping instances describes how instance search works in GHC, plus part about UndecidableInstances mentions that it's used to lift "rules that ensure that instance resolution will terminate". I don't see any mention about performance effect in either of these, neither I'm sure how ensuring termination of instance search can help performance directly - but maybe you can point me to some useful material.

@mizunashi-mana
Copy link
Author

@TheMatten

In many cases, algorithms for undecidable problem are unpredictable; this is from my experience and not principle.

In many cases, decidability in instance search is related compile performance directly.

may have mistake, sorry. Correctly, guarantee of decidability of instance search is important for language designer to maintain the compiler.

Decidability of instance search is more generic concept. A group of instances with cyclic super context is just one of undecidability. Haskell have special syntax for class and instance declaration and the designers of the language thought decidability of instance resolution deeply. Then, undecidable problem of instance resolution in Haskell is just "cyclic context". However, we can choose another design that the instance resolution of our language is turing complete (e.g. arithmetic expression is allowed). This is expressive but compile time depends strongly on the program.

Scala has powerful instance searcher which is a solution of undecidable problem. However, it is a troublemaker for compile performance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment