博客
关于我
Cats(2)- Free语法组合,Coproduct-ADT composition
阅读量:468 次
发布时间:2019-03-06

本文共 2688 字,大约阅读时间需要 8 分钟。

如何在Free编程中构建多语法的DSL

在函数式编程中,Free类型作为一种嵌入式编程语言DSL,能够有效地描述特定功能需求。然而,随着应用需求的不断扩展,单独为每个功能设计DSL会导致代码冗余和复杂性增加。函数组合(compositionality)的核心思想提醒我们,应该通过组合基础语法单元(ADT - Abstract Data Type)来构建适合特定应用需求的DSL。在上篇文章中,我们已经探讨了如何使用Interact DSL来描述交互功能。现在,我们将尝试在此基础上增加Login功能,并探索如何构建一个混合语法的DSL。

增加Login功能

首先,我们需要为Login功能定义一个新的DSL。通过观察,我们发现直接使用Free类型的DSL编程无法满足Login功能的需求。因此,我们需要构建一个能够支持多种操作的混合语法DSL。

使用Coproduct构建多语法DSL

Cats提供了Coproduct类型,这是一个树形数据结构,能够容纳多种子类型。Coproduct的每个节点可以表示为Either[F[A], G[A]]的形式,其中F和G分别代表两个子类型。我们可以通过递归地构建Coproduct结构来定义多层次的语法单元。

例如:

type H[A] = Coproduct[F, G, A]type I[A] = Coproduct[H, X, A]type J[A] = Coproduct[J, Y, A]

这里,F、G、X、Y分别代表不同的语法单元。

在我们的案例中,Interact和Login功能需要组合在一起,形成一个新的语法单元InteractLogin。因此,我们定义:

type InteractLogin[A] = Coproduct[Interact, Login, A]

使用Inject构建Coproduct

Cats提供了Inject类型用于构建Coproduct。Inject定义了三种关键实例:

  • catsFreeReflexiveInjectInstance[F[_]]:用于单一语法类型,无需构建Coproduct。
  • catsFreeLeftInjectInstance[F[_], G[_]]:将F注入到Coproduct[F, G, ?]中,作为左边子类型。
  • catsFreeRightInjectInstance[F[_], G[_], H[_]]:将F注入到已经包含H和G的Coproduct[H, G, ?]中。
  • 通过这些实例,我们可以根据具体的语法组合需求,构建适合的Coproduct结构。

    实现InteractLogin DSL

    我们通过Inject实例和Free.liftF,将Interact和Login功能整合到Coproduct结构中,实现一个混合语法的DSL。具体实现如下:

    object DSLs {  import ADTs._  import Interact._  import Login._    type InteractLogin[A] = Coproduct[Interact, Login, A]    val interactLoginDSL: Free[InteractLogin, Boolean] = for {    uid <- ask[InteractLogin]("Enter your User ID:")    pwd <- ask[InteractLogin]("Enter your Password:")    aut <- authenticate[InteractLogin](uid, pwd)  } yield aut}

    在这个DSL中,askauthenticate方法通过Inject实例被自动升级为可以运行在Coproduct[Interact, Login, ?]类型上的操作。

    实现Login功能

    为了实现Login功能,我们定义了一个新的ADTLogin,包含 Authenticate方法。通过Inject实例,我们可以将Login功能与Interact功能整合到同一个Coproduct结构中。

    object Login {  type FreeLogin[A] = Free[Login, A]  case class Authenticate(user: String, pswd: String) extends Login[Boolean]  def authenticate[G[_]](user: String, pswd: String)(implicit I: Inject[Login, G]): Free[G, Boolean] =     Free.liftF(I.inj(Authenticate(user, pswd)))}

    结合多个DSL

    在实际应用中,我们可能需要将多个DSL结合起来,形成更复杂的功能需求。例如,结合InteractLogin和Auth功能,可以定义一个新的Coproduct结构:

    type Permit[A] = Coproduct[Auth, InteractLogin, A]

    然后,构建一个新的DSL:

    val userPermitDSL: Free[Permit, Unit] = for {  uid <- ask[Permit]("Enter your User ID:")  pwd <- ask[Permit]("Enter your Password:")  auth <- authenticate[Permit](uid, pwd)  perm <- if (auth) authorize[Permit](uid) else Free.pure[Permit, Boolean](false)  _ <- if (perm) tell[Permit](s"Hello $uid, welcome to the program!") else tell[Permit]("Sorry, no no no!")} yield ()

    结论

    通过上述方法,我们成功地构建了一个支持多语法的DSL。这种方法展示了如何利用Coproduct和Inject在Free编程中构建灵活的语法组合能力。然而,随着应用需求的复杂化,Free编程在复杂处理上的局限性逐渐显现。因此,在下一篇文章中,我们将探索freeK库,探索更加灵活和高效的多语法DSL编程方式。

    转载地址:http://fbefz.baihongyu.com/

    你可能感兴趣的文章
    No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
    查看>>
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
    查看>>
    No module named 'crispy_forms'等使用pycharm开发
    查看>>
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>
    No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
    查看>>
    No new migrations found. Your system is up-to-date.
    查看>>
    No qualifying bean of type XXX found for dependency XXX.
    查看>>
    No resource identifier found for attribute 'srcCompat' in package的解决办法
    查看>>
    no session found for current thread
    查看>>
    No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
    查看>>
    NO.23 ZenTaoPHP目录结构
    查看>>
    no1
    查看>>
    NO32 网络层次及OSI7层模型--TCP三次握手四次断开--子网划分
    查看>>
    NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
    查看>>
    Node JS: < 一> 初识Node JS
    查看>>
    Node Sass does not yet support your current environment: Windows 64-bit with Unsupported runtime(72)
    查看>>
    node, nvm, npm,pnpm,以前简单的前端环境为什么越来越复杂
    查看>>