au-ts / cogent

Cogent Project
https://trustworthy.systems/projects/TS/cogent.pml
Other
158 stars 26 forks source link

Wrong shallow embedding for abstract polymorphic functions. #382

Closed gteege closed 3 years ago

gteege commented 4 years ago

The shallow embedding generated by --shallow-desugar-tuples is wrong when there are "hidden" type parameters in terms, even if the type is specified in Cogent. Example:

a1 : all(t). t -> ()
a2 : all(t). () -> t
f : () -> ()
f() = a1[U32](a2[U32]())
g : () -> ()
g () = f ()

When the shallow embedding is loaded into isabelle based on session CogentShallow the following error message is caused:

Loading theory "Abspoly-ShallowTuples.Abspoly_Shallow_Desugar_Tuples"
### Additional type variable(s) in specification of "f": 'a
### theory "Abspoly-ShallowTuples.Abspoly_Shallow_Desugar_Tuples"
### 0.033s elapsed time, 0.060s cpu time, 0.000s GC time
*** Type unification failed: Clash of types "unit" and "_ itself"
*** 
*** Type error in application: incompatible operand type
*** 
*** Operator:  f :: ??'a itself \<Rightarrow> unit \<Rightarrow> unit
*** Operand:   () :: unit
*** 
*** At command "definition" (line 15 of "~/work/projekte/code/HoBit/devel/experiments/cogent_shallow_error/Abspoly_Shallow_Desugar_Tuples.thy")

The reason is that the type for type parameter t cannot be inferred by Isabelle because there is no relation to other types. Thus t remains as an implicit type variable in the body of f and causes the shallow embedding of f to be polymorphic. In Cogent the type to be used is explicitly specified as U32, however, this information is not transferred to the shallow embedding.

I can see two possible solutions: