Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file algebraic.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237(*
* Copyright (c) 2019-2020 Craig Ferguson <me@craigfe.io>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*)includeAlgebraic_intfopenTypopenPpxlibmoduleLocated(A:Ast_builder.S)(M:Monad.S):SwithmoduleM=M=structmoduleM=MopenUtilsopenUtils.Make(A)openAletgenerate_identifiersn=List.initn(funi->Printf.sprintf"x%d"(i+1))letdsl~lib=(function|`field->"field"|`case1->"case1"|`case0->"case0"|`add_case->"|~"|`add_field->"|+"|`sealr->"sealr"|`sealv->"sealv"|`record->"record"|`variant->"variant")>>(matchlibwithSomel->(^)(l^".")|None->funx->x)>>evar(** {1 Helper functions for various subfragments} *)letconstruct~polymorphic?bodyname=ifpolymorphicthenpexp_variantnamebodyelsepexp_construct(Located.lidentname)body(** {[ |~ case0 "cons_name" (`)Cons_name ]} *)letvariant_case0~lib~polymorphic~cons_namee=[%expr[%edsl~lib`add_case][%ee]([%edsl~lib`case0][%eestringcons_name][%econstruct~polymorphiccons_name])](** {[
|~ case1 "cons_name" component_type (fun (x1, ..., xN) -> (`)Cons_name (x1, ..., xN))
]} *)letvariant_case1~lib~polymorphic~cons_name~component_type~identse=lettuple_pat=idents>|=pvar|>ppat_tupleinlettuple_exp=idents>|=evar|>pexp_tuplein[%expr[%edsl~lib`add_case][%ee]([%edsl~lib`case1][%eestringcons_name][%ecomponent_type](fun[%ptuple_pat]->[%econstruct~polymorphic~body:tuple_expcons_name]))](** Wrapper for {!variant_case0} and {!variant_case1} *)letvariant_case~polymorphic{case_name;case_cons}=matchcase_conswith|None->variant_case0~polymorphic~cons_name:case_name|Some(component_type,n)->letidents=generate_identifiersninvariant_case1~polymorphic~cons_name:case_name~component_type~idents(** [|+ field "field_name" (field_type) (fun t -> t.field_name)] *)letrecord_field~lib{field_name;field_repr}e=[%expr[%edsl~lib`add_field][%ee]([%edsl~lib`field][%eestringfield_name][%efield_repr](funt->[%epexp_field(evar"t")(Located.lidentfield_name)]))](** Record composites are encoded as a constructor function
{[ fun field1 field2 ... fieldN -> { field1; field2; ...; fieldN }) ]} *)letrecord_compositefields=letfields=fields>|=funl->l.pld_name.txtinletrecord=letrfields=fields>|=funs->(Located.lidents,evars)inpexp_recordrfieldsNoneinlambdafieldsrecord(** {[ | Cons_name (x1, x2, x3) -> cons_name x1 x2 x3 ] ]} *)letvariant_patterncons_namepatternn=letfparam_of_namename=String.lowercase_asciinameinmatchnwith|0->letlhs=patternNoneinletrhs=evar(fparam_of_namecons_name)incase~lhs~guard:None~rhs|n->letidents=generate_identifiersninletlhs=idents>|=pvar|>ppat_tuple|>funx->pattern(Somex)inletrhs=idents>|=evar|>pexp_tuple|>funx->[%expr[%eevar(fparam_of_namecons_name)][%ex]]incase~lhs~guard:None~rhs(** Variant composites are encoded as a destructor function:
{[
fun case1 case2 ... caseN -> function
| Case1 x -> case1 c
| Case2 (x1, x2) -> case2 x1 x2
...
| CaseN -> casen
]} *)letvariant_compositecs=letfparam_of_cdeclc=c.pcd_name.txt|>String.lowercase_asciiinletpattern_of_cdeclc=letpattern=ppat_construct(Located.map_lidentc.pcd_name)inletn=matchc.pcd_argswith|Pcstr_tupleargs->List.lengthargs|Pcstr_record_->invalid_arg"Inline record types unsupported"invariant_patternc.pcd_name.txtpatternnincs>|=pattern_of_cdecl|>pexp_function|>lambda(cs>|=fparam_of_cdecl)(** Analogous to {!variant_composite} but using AST fragments for polymorphic
variants. *)letpolyvariant_compositefs=letfparam_of_rowfieldf=matchf.prf_descwith|Rtag(label,_,_)->String.lowercase_asciilabel.txt|Rinherit_->assertfalseinletpattern_case_of_rowfieldf=matchf.prf_descwith|Rtag({txt;_},_,typs)->letpattern=ppat_varianttxtinletn=List.lengthtypsinvariant_patterntxtpatternn|Rinherit_->assertfalseinfs>|=pattern_case_of_rowfield|>pexp_function|>lambda(fs>|=fparam_of_rowfield)(** {1 Functional encodings of composite types}
The functional encodings have a standard form:
{[
<combinator> <type_name> <composite>
|> <augment> <subcomponent_1>
|> <augment> <subcomponent_2>
|> <augment> <subcomponent_3>
|> <sealer>
]}
That is, they initially construct an 'open' representation of the
composite, then add each of the subcomponents to the open representation
using an 'augmenter', and finally 'seal' the representation.
The following function extracts the necessary terms for each algebraic
type. *)type('a,'b)dsl_terms={combinator:expression;composite:'alist->expression;augment:'b->expression->expression;sealer:expression;}letterms_of_typ:typeab.lib:stringoption->(a,b)Typ.t->(a,b)dsl_terms=fun~libtyp->letdsl=dsl~libinletcombinator=dsl(matchtypwith|Record->`record|Variant->`variant|Polyvariant->`variant)andcomposite:alist->expression=matchtypwith|Record->record_composite|Variant->variant_composite|Polyvariant->polyvariant_compositeandaugment:b->expression->expression=matchtypwith|Record->record_field~lib|Variant->variant_case~lib~polymorphic:false|Polyvariant->variant_case~lib~polymorphic:trueandsealer=dsl(matchtypwith|Record->`sealr|Variant->`sealv|Polyvariant->`sealv)in{combinator;composite;augment;sealer}letencode:typeabe.(a,b)Typ.t->subderive:(a->(b,e)M.t)->lib:stringoption->type_name:string->alist->(expression,e)M.t=funtyp~subderive~lib~type_namets->letopenM.Syntaxinletdsl=terms_of_typ~libtypinletcomposite=dsl.compositetsinlet+apply_augments=ts>|=(subderive>>M.mapdsl.augment)|>M.sequence|>M.map(List.rev>>compose_all)inletopen_repr=[%expr[%edsl.combinator][%eestringtype_name][%ecomposite]]|>apply_augmentsin[%expr[%edsl.sealer][%eopen_repr]]end