Skip to content

Commit

Permalink
fix #293
Browse files Browse the repository at this point in the history
fix #348
  • Loading branch information
Ingo60 committed Mar 31, 2018
1 parent 5dc535d commit 2e6215c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
24 changes: 23 additions & 1 deletion frege/compiler/gen/java/Common.fr
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,22 @@ asKinded jt n

canBeKinded Kinded{} !k = false
canBeKinded Constr{} !k = false
canBeKinded jt !k = k > 0 && jt.{gargs?} && length jt.gargs >= k
canBeKinded jt !k = k > 0 && jt.{gargs?} && length jt.gargs >= k

--- check if a Java type can be used as instance of a higher kinded type class.
--- For example:
--- > TTuple<A,?>
--- is okay, but
--- > TTuple<?, A>
--- is not
implementsKinded Int JType Bool
implementsKinded k jt
| jt.{gargs?}, not (null jt.gargs), length jt.gargs >= k = all isWild (drop (length jt.gargs - k) jt.gargs)
| otherwise = false -- type is not even generic
where
isWild Wild{bounds = UNBOUNDED} = true
isWild _ = false


--- The opposite of 'asKInded', such that for any sufficiently generic 'JType'
--- > fromKinded (asKinded jt n) == jt
Expand Down Expand Up @@ -808,6 +823,13 @@ isArrayClassName _ = false
isArrayClass SymC{name} = isArrayClassName name
isArrayClass _ = false

--- check if a type class is higher kinded
isHigherKindedClass Symbols.SymbolT α Bool
isHigherKindedClass SymC{tau} = case tau.kind of
KApp{} true
other false
isHigherKindedClass other = false

{--
The (abstract) instance functions for some class members need a
Expand Down
34 changes: 28 additions & 6 deletions frege/compiler/gen/java/InstanceCode.fr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module frege.compiler.gen.java.InstanceCode where

import frege.Prelude hiding (<+>)

import Lib.PP (text, <+>, </>)
import Lib.PP (text, <+>, </>, <+/>)
import Data.TreeMap as Map(values, lookup, delete, insert, TreeMap)
import Data.List(zip4)

Expand All @@ -28,7 +28,7 @@ import Compiler.common.Binders(allBinders)
import Compiler.common.SymbolTable(changeSym)
import Compiler.common.JavaName(symJavaName, javaName)

import Compiler.classes.Nice (nice, nicer)
import Compiler.classes.Nice (nice, nicer, nicest)

import Compiler.tc.Util(sameCtx)

Expand Down Expand Up @@ -243,8 +243,6 @@ instanceCode (sym@SymI {sid}) = do -- instance definition

let vals = values sym.env

instFuns <- mapM (instFun csym sym) methods
instImpls <- mapM (varCode empty) vals
let constraints = zipWith (constraintDef g) sym.typ.rho.context (getCtxs g)
constrargs = zipWith (constraintArg g) sym.typ.rho.context (getArgs g)

Expand Down Expand Up @@ -300,7 +298,31 @@ instanceCode (sym@SymI {sid}) = do -- instance definition
where
gargs = take (length gvars) wilds

result = JClass {attr,
-- check for implementation restriction
let k = kArity csym.tau.kind
jt = head etype.gargs
implementationRestriction = not special && isHigherKindedClass csym && not (implementsKinded k jt)
when (implementationRestriction) do
E.error sym.pos (
text "implementation restriction: the type"
<+> nicest g sym.typ.rho.{context=[]}
<+> text "cannot be an instance of" <+> text (csym.name.nicer g)
<+/> text "because attempting to represent"
<+/> text "it as a higher kinded type"
<+/> text "results in the invalid java type" <+> text jt.show <+> text ". "
<+/> text "To be valid," <+> text (show k)
<+> text "wild card type argument(s)"
<+> text "should appear from the right,"
<+> text "but this is not the case here. "
<+/> text "Maybe this can be corrected by"
<+> text "re-arranging type arguments."
</> text "Also, if this was a newtype, it'll probably help to change it to data."
)
when (isHigherKindedClass csym) do
E.logmsg TRACEG sym.pos (text "instanceCode" <+> text (csym.name.nicer g) <+> text jt.show)
instFuns <- mapM (instFun csym sym) (if implementationRestriction then [] else methods)
instImpls <- mapM (varCode empty) (if implementationRestriction then [] else vals)
let result = JClass {attr,
name = instName.base,
gvars,
extend = Nothing,
Expand All @@ -309,7 +331,7 @@ instanceCode (sym@SymI {sid}) = do -- instance definition
++ singleton
++ make
++ instFuns
++ concat instImpls}
++ concat instImpls}
pure [JComment (nice sym g ++ " :: " ++ nice sym.typ g), result]

--- If given something else than a type class this is a fatal compiler error
Expand Down

0 comments on commit 2e6215c

Please sign in to comment.