haskell - Ambiguous type variable with Haskeline auto-completion -


i'm trying implement autocompletion function haskeline :

import system.console.haskeline import system.console.haskeline.io import data.list  mysettings :: settings io mysettings = defaultsettings {                                 historyfile = "myhist"                               , complete = completeword nothing " \t" $ return . search                               }  keywords :: [string] keywords = ["point","line","circle","sphere"]  search :: string -> [completion] search str = map simplecompletion $ filter (str `isprefixof`) keywords  main :: io () main =         inputline <- initializeinput mysettings         putstrln "done" 

but bit disappointed ghc error :

ambiguous type variable `t0' in constraint:   (control.monad.io.class.monadio t0)     arising use of `defaultsettings' probable fix: add type signature fixes these type variable(s) 

i set type each function didn't solve problem.

do have idea type ambiguity come , how remove it?

a quick fix:

mysettings :: settings io mysettings = (defaultsettings :: settings io)   {  historyfile = "myhist"    , complete    = completeword nothing " \t" $ return . search }  

the problem rare corner case, it's no wonder above solution may seem arbitrary or inscrutable. try explain nevertheless.

defaultsettings has type monadio m => settings m. it's polymorphic value, , such values cause hiccups in type inference. generally, can computation (pattern matching, field projections, etc.) on polymorphic values if ghc can infer polymorphic parameter context. settings m might have different content depending on exact m , exact type class methods belong m.

now, issue settings m parameter present in complete field, has type completionfunc m. in our example ignore old complete field, , replace new field. therefore, far ghc knows, old complete field have been of any type whatsoever. , since old complete field source possibly gain information m parameter of defaultsettings, , left unconstrained, ghc can't infer that m monadio.

if add (defaultsettings :: settings io), old m parameter instantiated io , there's no issue anymore. note new m parameter unrelated old m parameter, because ignored old complete field , replaced new function. new m parameter determined io top-level mysettings :: settings io annotation.

in fact, instantiate defaultsettings monadio type, , result same. again, because ignore old value of complete.


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -