前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >「SF-LC」13 ImpParser

「SF-LC」13 ImpParser

作者头像
零式的天空
发布于 2022-03-14 06:43:49
发布于 2022-03-14 06:43:49
36800
代码可运行
举报
文章被收录于专栏:零域Blog零域Blog
运行总次数:0
代码可运行

the parser relies on some “monadic” programming idioms

basically, parser combinator (But 非常麻烦 in Coq)

Lex

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Inductive chartype := white | alpha | digit | other.

Definition classifyChar (c : ascii) : chartype :=
  if      isWhite c then white
  else if isAlpha c then alpha
  else if isDigit c then digit
  else                   other.
  

Definition token := string.

Syntax

带 error msg 的 option:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Inductive optionE (X:Type) : Type :=
  | SomeE (x : X)
  | NoneE (s : string).       (** w/ error msg **)

Arguments SomeE {X}.
Arguments NoneE {X}.

Monadic:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Notation "' p <- e1 ;; e2"
   := (match e1 with
       | SomeE p ⇒ e2
       | NoneE err ⇒ NoneE err
       end)
   (right associativity, p pattern, at level 60, e1 at next level).

Notation "'TRY' ' p <- e1 ;; e2 'OR' e3"
   := (match e1 with
       | SomeE p ⇒ e2
       | NoneE _ ⇒ e3
       end)
   (right associativity, p pattern,
    at level 60, e1 at next level, e2 at next level).
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Definition parser (T : Type) :=
  list token → optionE (T * list token).
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
newtype Parser a = Parser (String -> [(a,String)])

instance Monad Parser where
   -- (>>=) :: Parser a -> (a -> Parser b) -> Parser b
   p >>= f = P (\inp -> case parse p inp of
                           []        -> []
                           [(v,out)] -> parse (f v) out)

combinator many

Coq vs. Haskell

  1. explicit recursion depth, .e. step-indexed
  2. explicit exception optionE (in Haskell, it’s hidden behind the Parser Monad as [])
  3. explicit string state xs (in Haskell, it’s hidden behind the Parser Monad as String -> String)
  4. explicit accepted token (in Haskell, it’s hidden behind the Parser Monad as a, argument)
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Fixpoint many_helper {T} (p : parser T) acc steps xs :=
  match steps, p xs with
  | 0, _ ⇒
      NoneE "Too many recursive calls"
  | _, NoneE _ ⇒
      SomeE ((rev acc), xs)
  | S steps', SomeE (t, xs') ⇒
      many_helper p (t :: acc) steps' xs'
  end.

Fixpoint many {T} (p : parser T) (steps : nat) : parser (list T) :=
  many_helper p [] steps.
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
manyL :: Parser a -> Parser [a]
manyL p = many1L p <++ return []   -- left biased OR

many1L :: Parser a -> Parser [a]
many1L p = (:) <$> p <*> manyL p
-- or
many1L p = do x <- p
              xs <- manyL p
              return (x : xs)

ident

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Definition parseIdentifier (xs : list token) : optionE (string * list token) :=
  match xs with
  | [] ⇒ NoneE "Expected identifier"
  | x::xs' ⇒ if forallb isLowerAlpha (list_of_string x)
             then SomeE (x, xs')
             else NoneE ("Illegal identifier:'" ++ x ++ "'")
  end.
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
ident :: Parser String
ident = do x  <- lower
           xs <- many alphanum
           return (x:xs)
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-03-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
「SF-LC」3 List
Pair of Numbers Q: Why name inductive? A: Inductive means building things bottom-up, it doesn’t have
零式的天空
2022/03/14
4210
「SF-LC」1 Basics
The .v code is a gorgeous example of literal programming and the compiled .html website is full-fledged. So this note is intended to be NOT self-contained and only focus on things I found essential or interesting. This note is intended to be very personal and potentially mix English with Chinese (You can Lol) So yeah. Don’t expect it to be well organized and well written. I posted it on blog mainly for my own references purpose. The quotes could either come from the book or saying from someone (even including me).
零式的天空
2022/03/14
4000
「SF-PLF」13 References
computational effects - “side effects” of computation - impure features
零式的天空
2022/03/02
2590
「SF-PLF」9 MoreStlc
SF here, same as TaPL, treat it less derived by _compute the type T1 from t1.
零式的天空
2022/03/02
5040
「SF-LC」12 Imp
A weird convention through out all IMP is:
零式的天空
2022/03/14
1.7K0
「SF-LC」4 Poly
Until today, We were living in the monomorphic world of Coq. So if we want a list, we have to define it for each type:
零式的天空
2022/03/14
1.3K0
「SF-PLF」11. TypeChecking
首先我们需要 check equality for types. 这里非常简单,如果是 SystemF 会麻烦很多,对 ∀ 要做 local nameless 或者 alpha renaming:
零式的天空
2022/03/02
2770
「SF-LC」6 Logic
The equality operator = is also a function that returns a Prop. (property: equality)
零式的天空
2022/03/14
5920
「SF-LC」15 Extraction
如果不做任何处理的话…生成的 ml 里的 nat 则都会是 Church Numeral…
零式的天空
2022/03/14
5230
「SF-LC」14 ImpCEvalFun
Step-Indexed Evaluator …Copied from 12-imp.md: Chapter ImpCEvalFun provide some workarounds to make functional evalution works: step-indexed evaluator, i.e. limit the recursion depth. (think about Depth-Limited Search). return option to tell if it’s a norm
零式的天空
2022/03/14
4710
「SF-LC」9 ProofObjects
So the book material is designed to be gradually reveal the facts that
零式的天空
2022/03/14
5570
「SF-PLF」7 Stlc
“Base Types”, only Bool for now. — 基类型 …again, exactly following TAPL.
零式的天空
2022/03/02
3630
「SF-LC」7 Ind Prop
we can write an Inductive definition of the even property!
零式的天空
2022/03/14
6550
「SF-LC」5 Tactics
It also works with conditional hypotheses:
零式的天空
2022/03/14
5340
「SF-LC」8 Maps
From now on, importing from std lib. (but should not notice much difference)
零式的天空
2022/03/14
3410
「SF-LC」11 Rel
I have been long confused with Unary Relations vs. Binary Relation on the Same Set (homogeneous relation) I thought they were same…but turns out they are totally different!
零式的天空
2022/03/14
3900
「SF-PLF」5 Smallstep
not just input state get mapped to output state. but also intermediate state (which could be observed by concurrent code!)
零式的天空
2022/03/02
5950
Monadic Function_Haskell笔记12
只是把context换成了Monad而已,此外没什么区别。并且对于遵守Functor laws和Monad laws的类型,这两个函数是完全等价的,例如:
ayqy贾杰
2019/06/12
9540
「SF-PLF」6 Types
The toy lang from SmallStep is too “safe” to demonstrate any runtime (or dynamic) type errors. — 运行时类型错误 So that’s add some operations (common church numeral ones), and bool type.
零式的天空
2022/03/02
4460
「SF-LC」16 Auto
Ltac - automated forward reasoning (hypothesis matching machinery)
零式的天空
2022/03/14
3670
相关推荐
「SF-LC」3 List
更多 >
领券
💥开发者 MCP广场重磅上线!
精选全网热门MCP server,让你的AI更好用 🚀
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验