In Haskell, using listify with Data can extract values of a specific type from a deeply nested structure without lots of getters. For example, with the following code:
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE OverloadedStrings #-}
module Lib
( john
, listStrings
) where
import Data.Data
import Data.Generics.Schemes
import Data.Text
data Person = Person
{ name :: Name
, address :: Address
} deriving (Data, Show)
data Name = Name
{ firstName :: Text
, lastName :: Text
} deriving (Data, Show)
data Address = Address
{ addressLine :: Text
, city :: Text
, province :: Text
, country :: Text
} deriving (Data, Show)
john :: Person
john =
Person
{ name = Name {firstName = "John", lastName = "Smith"}
, address =
Address
{ addressLine = "1-2-3 Nantoka-kantoka-machi"
, city = "Nishinomiya"
, province = "Hyogo"
, country = "Japan"
}
}
listStrings :: Person -> [Text]
listStrings = listify (const True)
listStrings john returns a list of Texts in john.
ghci> listStrings john
["John","Smith","1-2-3 Nantoka-kantoka-machi","Nishinomiya","Hyogo","Japan"]
Is there something like this in OCaml?
Context
I'm writing a Coq code formatter, and having difficulty pretty-printing notations (e.g., _ = _.) The node constructor for notations is CNotation in constr_expr_r.
Coq's OCaml API provides find_notation_printing_rule which takes a value in CNotation and returns the printing rule for the notation, but I also don't understand what the returned rule means. This led me to write a printing rule dumper; a program that takes a Coq code as an input, extracts notations from the code, and dumps the notations with printing rules and other useful information. For the extraction phase, I need a function like listify to get CNotations from the code.
After all, I converted an AST into an S-expression using functions in
coq-serapi, filtered it, and converted them back to the concrete typesIt turned out that
sertopcould dump printing rules for some notations with theUnparsingquery.