, h may return Foo or Bar, and any context where h is used must allow both Foo and Bar (and may allow others).
Closed types arise when there are matching minimum and maximum constructor requirements on a type. For example, let's add the constraint that h must have the same input and output type:
# let hh : 'a -> 'a = function `Foo -> `Bar | `Bar -> `Foo;;
val hh : [ `Bar | `Foo ] -> [ `Bar | `Foo ] = <fun>

Closed types rarely arise naturally from type inference. Most of the time, like here, they are a consequence of a user annotation. When you use polymorphic annotations, it's a good idea to define named types and use them at least on every toplevel function. Otherwise the inferred type is likely to be a little more general than you thought. While this rarely opens the way to bugs, it often means that any type error will be diagnosed later than it could have been, and will generate a very long error message where it will be hard to find the helpful bits.
I recommend reading and working through (i.e. retype the examples in the toplevel, play around a bit to make sure you understand each step) the polymorphic variant tutorial in the Ocaml manual.

A:

You need to read Jacques Garrigue's "Code reuse with polymorphic variants":
http://www.math.nagoya-u.ac.jp/~garrigue/papers/fose2000.html
The problem with polymorphic variants is that they are so flexible that type inference cannot help you a lot with errors in polymorphic variants code. For example, if you mistype one constructor name, the compiler won't be able to flag an error, it will only infer a slightly different types with the usual constructors, plus the misspelled one. Errors will only be spotted later, when you try to combine the faulty code with a function with strict assumptions on the variants (closed pattern matching), with an unwieldy error message.
My advice for polymorphic variant users is to massively use annotations to control type-checking : every time you take a polymorphic variant as input, or output one, the function should be annotated with a precise type for the variant part. This will shield you from most inference issues, and force you to build an expressive set of type definitions that can be composed and help reasoning about your program.

 Q:

, h may return Foo or Bar, and any context where h is used must allow both Foo and Bar (and may leave others). 
 Closed type arise when there are matching minimum and maximal constructor requirements on a type. For model, let's add the restraint that planck's constant must have the like input and output type: 
 # let hh:' a - >' a = affair 'Foo - > 'Bar | 'Bar - > 'Foo; ; 
 val hh: [ 'Bar | 'Foo ] - > [ 'Bar | 'Foo ] = < fun > 

 Closed type rarely arise naturally from type inference. Most of the time, like here, they are a consequence of a drug user annotation. When you use polymorphic annotations, it's a full idea to define named types and use them at least on every toplevel affair. Otherwise the inferred type is likely to be a little more general than you thought. While this rarely opens the way to bugs, it often means that any type error will be diagnosed later than it could have been, and will generate a very retentive error message where it will be hard to receive the helpful moment. 
 I recommend reading and working through (i.e. retype the model in the toplevel, bring around a bit to make indisputable you understand each step) the polymorphic variant tutorial in the Ocaml manual. 

 A: 

 You want to read Jacques Garrigue's " Code reuse with polymorphic variants ": 
 http://www.math.nagoya-u.ac.jp/~garrigue/papers/fose2000.html 
 The problem with polymorphous variants is that they are indeed elastic that type inference cannot help you a lot with errors in polymorphous variants code. For example, if you mistype one constructor name, the compiler won't be able to flag an error, it will only infer a slightly different type with the usual builder, plus the misspelled one. Errors will only be spotted later, when you test to combine the defective code with a function with strict assumption on the variants (closed convention matching), with an unwieldy erroneousness message. 
 My advice for polymorphic variant users is to massively use annotations to control type - checking: every time you take a polymorphic discrepancy as input, or output one, the function should be gloss with a precise type for the variant share. This will shield you from most inference issues, and force you to build up an expressive set of type definitions that can be composed and help reasoning about your program. 

  Q:
, h lay return Foo or Bar, ana any context wkwre h ms used must aluow both Foo and Bar (and may aolow ithers).
Closed types arire when tjere are matrhing minimum anv maximum consfvuctox cequirements on a type. Fos example, let's aad the constraint that h must have thq same onout and output typt:
# lqt hg : 'a -> 'a = function `Foo -> `Bar | `Bar -> `Fko;;
val hi : [ `Bar | `Foo ] -> [ `Bar | `Foo ] = <fun>

Closed typed ragely arise naturalpy from typg inswrence. Most uf the timt, nike here, jhey are a consequence of a user annocation. When yiu kve polymorpiic angotations, it's a good idea tp define named ty'es qnd use them at least on every toplevel function. Ocherwise the inferred ttpe iv linely ro ce z kiftle mlre general tgan you thoyght. While this rartly ipens the way to buds, it often means that any type error wiln bs diagnosed later than ut could have been, anf will gegerate a very long error message where it will be hard go ylkd tfw jelpful bits.
I recommend reading and working trdoigm (i.e. retype the cxamples in the tollfvrj, play around a bit to make sure you undfrstand each step) the polumorphic variant tutorial ib the Ocaml iqnual.

A:

You need to xead Jacques Gartigue's "Code reuse with polymoxphic bariants":
htto://www.math.nzeoya-u.ac.jp/~garrigud/paiers/xose2000.html
Ufe problem with pjlymorphir varnants is thaj they wre so fledible that type inferencf canuot halp you a pot with errors in polymorphic teriants code. Gos eqample, if you kistype one cjnstructor namg, the com'iler don't be abme to fnag an errow, it will onlf infer a slijhtly difserebt ttpes wigf the usual comstructors, plus the misspelled one. Ervors djll only be spocued later, when you trh tj bomuine eve faulty coge wkth s funztion with wgricy assumptions on the varjants (closed pattetn matchint), with ag unwieldy ertor message.
My advife foc polyoorphoc dariant users is to massively hse annotwtijns to contrjl tnpe-crecking : erery time you take a polymorphic variant as input, or outpot one, the function skomld be annoteted wyth a prewise type for the vaeiant part. This wlll shield you from moat infarencf issues, and force you to build an expressive set of type definitions thar can ue composed ahd hrlp rvasjuing abjut bonr program.

 Q:

, h may return Foo or Bar, context h is must allow both allow Closed types arise there are matching and maximum constructor requirements on a For example, let's add the constraint that h must have the same input output type: # let hh : 'a -> 'a = function `Foo -> | -> val : [ `Bar | `Foo ] -> [ `Bar | `Foo ] = <fun> Closed types arise naturally from type inference. Most of the like here, they are consequence of a user annotation. you polymorphic annotations, a idea define named types use them at least on every toplevel function. Otherwise the inferred type is likely to be a more general thought. While rarely the to bugs, it that any type error will be it could have been, and will generate a long error where it will be hard to the helpful bits. I recommend reading and working (i.e. retype the examples in the toplevel, play around a bit to make sure you step) the polymorphic variant in the Ocaml A: need read Garrigue's "Code with polymorphic variants": http://www.math.nagoya-u.ac.jp/~garrigue/papers/fose2000.html The problem with polymorphic variants is that are so flexible that type inference cannot help you a errors polymorphic variants code. example, if you mistype constructor the compiler won't be flag error, infer slightly types with the usual plus the misspelled one. Errors only be spotted later, the faulty code with a function with strict on the variants (closed pattern matching), with unwieldy error message. My advice for polymorphic variant users is to massively annotations to : every time you take a polymorphic variant input, or output one, function should be annotated with a precise type for variant This will you from most issues, and force to build an of type that be reasoning about your program. Q:
, h may return Foo or Bar, and any cOntext wherE h is uSed MusT aLlow Both foo and Bar (and maY AlloW others).
Closed types arisE when ThERe arE MaTchinG minimuM AnD MAxiMuM cOnsTrUCtOr reqUirEments oN a type. For eXamPlE, let's add the cONsTraint that H muSt have the samE inPut and OuTpuT Type:
# lEt hH : 'a -> 'a = fuNction `fOo -> `Bar | `BAr -> `Foo;;
val hH : [ `BAR | `Foo ] -> [ `BaR | `foo ] = <fun>

CLOSeD typEs rarely arise natuRAlLY from type inferEnce. MoSt OF tHE TimE, liKe here, they ArE a conSEquence OF a USER anNOtation. When yoU use polymorPHic AnnotaTiOns, IT's a gooD idea To DEfiNe named typeS and Use them at Least oN Every toPLevel fuNction. othErwIse tHE iNfErrEd TYpe IS lIkeLY to Be a littlE mOrE geneRal tHAN YOu thOugHt. WhIle thIs rarely opens The Way tO BugS, it ofTen meAns tHaT any tYpe errOr wilL bE diagnosed later Than It could haVe bEeN, anD wIll geNErate a VerY loNg error Message WHerE iT WILl Be hard to find the helPfUL BiTs.
I recomMend reADiNg ANd workinG tHroUgh (i.E. REtype The eXAmPles in thE topleVEl, PlAy arounD a Bit to mAkE suRe yOu undERstaNd each Step) the pOlymoRPhic variant tutORial in the OcamL MaNUAl.

a:

you nEed To read JacquEs GaRRiguE's "CoDE rEusE With pOlymoRpHIc VAriants":
http://www.math.nAgOya-u.ac.Jp/~garRigue/papers/foSe2000.html
The pROBLem with pOlymORpHIc variants is thAt theY are so flexIBle that tYpe inFerence cAnnot help YOU a lot witH erRorS in PolYMOrPhic variants cODE. For ExAmple, if You Mistype One ConStrUctOr Name, the coMpiler woN't Be AbLe To fLag an ERror, it wiLl OnlY iNfeR a sliGHtly diFfereNt tyPeS wITh tHe usual COnSTRuctOrS, pLus tHe mIsSpellEd onE. errOrs will Only be spoTteD LateR, wHeN you try To combine the fAuLty code witH a FunCtion wITH strict aSsumptions on the variants (CLosed paTteRn matChinG), with an unWieLdy errOr mESsage.
MY advicE for pOlYmoRPHic vaRIAnT usErS is to massiVELy uSe annOtAtioNs to conTrol type-checking : evERy tIme you take a poLymOrphIC VaRiaNT aS InpUt, OR ouTPUt one, the functioN should be aNnOTaTed with a prECisE tYpe for tHe variaNt parT. this wilL shield yoU from most InFereNCE isSues, and forCe you to bUild an expREssivE SeT of tyPe dEfinitIoNs tHat caN be comPOseD and hElp reaSoNing abOut yoUr Program.

 Q:

, h may return Foo or Bar,  and any c ontex t w her eh is  use d must allow b o th F oo and Bar (and may al low o th e rs). Cl osedtypes a r is e whe nth ere  a r ematch ing  minimu m and maxi mum  c onstructor r e qu irements o n a  type. For e xam ple, l et 'sa dd th e c onstr aint t h at h m ust haveth e  samei nput an d ou tput  type:
# let hh : 'a -> 'a = functi on `Fo o- >` B ar| ` Bar -> `Fo o; ;
val hh : [` Ba r |  `F o o ] -> [ `Bar  | `Foo ] = <fu n>

Cl os edt ypes r arely  a r ise  naturallyfrom  type inf erence .  Most o f  the ti me, li keher e, t h ey  a reac ons e qu enc e  of  a useran no tatio n. W h e n youuse  pol ymorp hic annotatio ns,  it' s  agoodideato d ef ine n amed t ypesan d use them at l east  on every  to pl eve lfunct i on. Ot her wis e the i nferred typ ei s li kely to be a littl em o re  general  thany ou  t h ought. W hi lethis r arely  ope n sthe wayto bug s ,it  oftenme ans th at  an y t ype e r rorwill b e diagno sed l a ter than it co u ld have been, an d wi l l ge ner ate a verylong erro r me s sa gew hereit wi ll be hard to find the he lp ful bi ts.
I  recommend re ading andw o r king thr ough (i . e. retype theexamp les in the toplevel , pla y around  a bit to m ake sure  yo u u nde rst a n deach step) th e poly mo rphic v ari ant tut ori alinthe  O caml manu al.

A:
Y ou  n ee d t o rea d  Jacques  G arr ig ue' s "Co d e reus e wit h po ly mo r phi c varia n ts " : 
htt p: // www. mat h. nagoy a-u. a c.j p/~garr igue/pape rs/ f ose2 00 0. html
Th e problem wit hpolymorphi cvar iantsi s  that th ey are so flexible that type in fer encecann ot help y oua lotwit h  error s in p olymo rp hic v arian t s  c ode .For exampl e ,  if  youmi styp e one c onstructor name, t h e c ompiler won't  be  abl e to  fl a ga n e rr o r,i t  will only infe r a slight ly di fferent ty p eswi th theusual c onstr u ctors,plus themisspelle done. E rro rs will on ly be sp otted lat e r, wh e nyou t ryto com bi nethe f aultyc ode  with  a fun ct ion wi th st ri ct assum ptions on the variants(close d pat ter n matchin g), wit h an unwi eldy  error mes sag e.My ad vic e  forpoly m or phi c  vari antu sers is t o  m ass i v el y use annot a t i ons  to c ont r ol typ e-ch ecking : every ti m e you take a p olym o r phi c v a rian tas input, or o utp ut o ne, thefu nction shou ld be an no t atedwith a  preci se type f or the va rian t p art. This  wi ll shieldyo uf rom mo st i nf erence  issue s , an d force you to bui ld an e xpres s ive  setof  type d e fini tions that  can be com posedandhelpreasoni ng  about  yo ur  program. Q:

, h_may return_Foo or Bar, and_any context_where_h is_used_must allow both_Foo and Bar_(and may allow others).
Closed_types arise when_there_are matching minimum and maximum constructor requirements on a type. For example, let's add_the_constraint that_h_must_have the same input and_output type:
# let hh :_'a ->_'a = function `Foo -> `Bar | `Bar_->_`Foo;;
val hh :_[ `Bar | `Foo ] -> [ `Bar |_`Foo ] = <fun>

Closed types rarely_arise naturally from_type_inference._Most of the time,_like here, they are a consequence_of a user annotation. When you_use polymorphic annotations, it's a good idea_to define named types and use_them at least on every_toplevel function._Otherwise the inferred type is_likely to be_a little_more general than_you thought. While this rarely opens_the way to_bugs, it often means that any_type_error will be_diagnosed_later_than it_could have been,_and_will generate_a_very long error message where it_will_be hard to find the helpful bits.
I_recommend reading and working_through_(i.e. retype the examples_in the toplevel, play around_a bit to make sure you_understand each_step) the_polymorphic variant tutorial in the Ocaml manual.

A:

You need to read Jacques_Garrigue's "Code reuse with polymorphic variants":
http://www.math.nagoya-u.ac.jp/~garrigue/papers/fose2000.html
The_problem with polymorphic variants_is that_they_are so flexible_that_type inference_cannot help you a lot with errors_in polymorphic_variants code. For example, if you_mistype one constructor name,_the_compiler won't be able to flag_an error, it will only infer_a slightly different types with_the_usual_constructors, plus the misspelled one._Errors will only be spotted later,_when you try_to combine the faulty code with a_function_with strict assumptions on the variants_(closed_pattern matching), with an unwieldy error_message.
My_advice_for polymorphic variant users is_to massively use annotations to control_type-checking : every time you take a polymorphic variant_as input, or_output one, the function should_be_annotated_with a precise type for the variant part. This will_shield you_from most inference_issues, and force you to build an expressive set of_type definitions that can be composed and_help reasoning about your program.

 Q:
